Skip to content

Commit

Permalink
Merge pull request #1517 from sohankunkerkar/fix-trailing-slash
Browse files Browse the repository at this point in the history
src/libcrun: fix handling of device paths with trailing slashes
  • Loading branch information
giuseppe committed Sep 4, 2024
2 parents dc993bd + dc31069 commit 841a0de
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 11 deletions.
29 changes: 18 additions & 11 deletions src/libcrun/linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -1611,7 +1611,14 @@ libcrun_create_dev (libcrun_container_t *container, int devfd, int srcfd,
int rootfsfd = get_private_data (container)->rootfsfd;
const char *rootfs = get_private_data (container)->rootfs;
size_t rootfs_len = get_private_data (container)->rootfs_len;
const char *rel_dev = relative_path_under_dev (device->path);
if (is_empty_string (fullname))
return crun_make_error (err, EINVAL, "device path is empty");
// Normalize the path by removing trailing slashes.
cleanup_free char *normalized_path = xstrdup (fullname);
consume_trailing_slashes (normalized_path);
if (normalized_path[0] == '\0')
strcpy (normalized_path, "/");
const char *rel_dev = relative_path_under_dev (normalized_path);

if (binds)
{
Expand All @@ -1631,7 +1638,7 @@ libcrun_create_dev (libcrun_container_t *container, int devfd, int srcfd,
}
else
{
const char *rel_path = consume_slashes (device->path);
const char *rel_path = consume_slashes (normalized_path);

fd = crun_safe_create_and_open_ref_at (false, rootfsfd, rootfs, rootfs_len, rel_path, 0755, err);
if (UNLIKELY (fd < 0))
Expand All @@ -1645,7 +1652,7 @@ libcrun_create_dev (libcrun_container_t *container, int devfd, int srcfd,
return 0;
}

ret = do_mount (container, fullname, fd, device->path, NULL, MS_BIND | MS_PRIVATE | MS_NOEXEC | MS_NOSUID, NULL, LABEL_MOUNT, err);
ret = do_mount (container, fullname, fd, normalized_path, NULL, MS_BIND | MS_PRIVATE | MS_NOEXEC | MS_NOSUID, NULL, LABEL_MOUNT, err);
if (UNLIKELY (ret < 0))
return ret;
}
Expand Down Expand Up @@ -1684,17 +1691,17 @@ libcrun_create_dev (libcrun_container_t *container, int devfd, int srcfd,
}
else
{
char *dirname;
cleanup_free char *buffer = NULL;
cleanup_close int dirfd = -1;
char *basename, *tmp;
cleanup_free char *dirname = NULL;
char *basename, *found;

buffer = xstrdup (device->path);
dirname = buffer;
dirname = xstrdup (normalized_path);

tmp = strrchr (buffer, '/');
*tmp = '\0';
basename = tmp + 1;
found = strrchr (dirname, '/');
if (found)
*found = '\0';

basename = found ? found + 1 : dirname;

if (dirname[0] == '\0')
dirfd = dup (rootfsfd);
Expand Down
14 changes: 14 additions & 0 deletions src/libcrun/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -2507,3 +2507,17 @@ get_overflow_gid (void)
}
return gid;
}

void
consume_trailing_slashes (char *path)
{
if (! path || path[0] == '\0')
return;

char *last = path + strlen (path);

while (last > path && *(last - 1) == '/')
last--;

*last = '\0';
}
2 changes: 2 additions & 0 deletions src/libcrun/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ xstrdup (const char *str)
return ret;
}

void consume_trailing_slashes (char *path);

static inline const char *
consume_slashes (const char *t)
{
Expand Down
14 changes: 14 additions & 0 deletions tests/test_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,19 @@ def test_mknod_device():
return -1
return 0

def test_trailing_slash_mknod_device():
if is_rootless():
return 77

conf = base_config()
add_all_namespaces(conf)
conf['process']['args'] = ['/init', 'true']
conf['linux']['devices'] = [{"path": "/mnt/", "type": "b", "major": 10, "minor": 229}]
try:
run_and_get_output(conf)
except Exception as e:
return -1
return 0

all_tests = {
"owner-device" : test_owner_device,
Expand All @@ -223,6 +236,7 @@ def test_mknod_device():
"mknod-device" : test_mknod_device,
"mode-device" : test_mode_device,
"create-or-bind-mount-device" : test_create_or_bind_mount_device,
"handle-device-trailing-slash" : test_trailing_slash_mknod_device,
}

if __name__ == "__main__":
Expand Down

1 comment on commit 841a0de

@packit-as-a-service
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

podman-next COPR build failed. @containers/packit-build please check.

Please sign in to comment.