Skip to content

Commit

Permalink
Merge patch series "ovl: allow O_PATH file descriptor when specifying…
Browse files Browse the repository at this point in the history
… layers"

Christian Brauner <brauner@kernel.org> says:

Allow overlayfs to use O_PATH file descriptors when specifying layers.
Userspace must currently use non-O_PATH file desriptors which is often
pointless especially if the file descriptors have been created via
open_tree(OPEN_TREE_CLONE). This has been a frequent request and came up
again in [1].

Link: https://lore.kernel.org/r/fd8f6574-f737-4743-b220-79c815ee1554@mbaynton.com [1]

* patches from https://lore.kernel.org/r/20250210-work-overlayfs-v2-0-ed2a949b674b@kernel.org:
  selftests/overlayfs: test specifying layers as O_PATH file descriptors
  fs: support O_PATH fds with FSCONFIG_SET_FD

Link: https://lore.kernel.org/r/20250210-work-overlayfs-v2-0-ed2a949b674b@kernel.org
Signed-off-by: Christian Brauner <brauner@kernel.org>
  • Loading branch information
Christian Brauner committed Feb 12, 2025
2 parents 2cc0b7f + 85c8700 commit 29349a3
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
2 changes: 2 additions & 0 deletions fs/autofs/autofs_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ void autofs_clean_ino(struct autofs_info *);

static inline int autofs_check_pipe(struct file *pipe)
{
if (pipe->f_mode & FMODE_PATH)
return -EINVAL;
if (!(pipe->f_mode & FMODE_CAN_WRITE))
return -EINVAL;
if (!S_ISFIFO(file_inode(pipe)->i_mode))
Expand Down
2 changes: 1 addition & 1 deletion fs/fsopen.c
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ SYSCALL_DEFINE5(fsconfig,
case FSCONFIG_SET_FD:
param.type = fs_value_is_file;
ret = -EBADF;
param.file = fget(aux);
param.file = fget_raw(aux);
if (!param.file)
goto out_key;
param.dirfd = aux;
Expand Down
65 changes: 65 additions & 0 deletions tools/testing/selftests/filesystems/overlayfs/set_layers_via_fds.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,69 @@ TEST_F(set_layers_via_fds, set_500_layers_via_fds)
ASSERT_EQ(close(fd_overlay), 0);
}

TEST_F(set_layers_via_fds, set_500_layers_via_opath_fds)
{
int fd_context, fd_tmpfs, fd_overlay, fd_work, fd_upper, fd_lower;
int layer_fds[500] = { [0 ... 499] = -EBADF };

ASSERT_EQ(unshare(CLONE_NEWNS), 0);
ASSERT_EQ(sys_mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL), 0);

fd_context = sys_fsopen("tmpfs", 0);
ASSERT_GE(fd_context, 0);

ASSERT_EQ(sys_fsconfig(fd_context, FSCONFIG_CMD_CREATE, NULL, NULL, 0), 0);
fd_tmpfs = sys_fsmount(fd_context, 0, 0);
ASSERT_GE(fd_tmpfs, 0);
ASSERT_EQ(close(fd_context), 0);

for (int i = 0; i < ARRAY_SIZE(layer_fds); i++) {
char path[100];

sprintf(path, "l%d", i);
ASSERT_EQ(mkdirat(fd_tmpfs, path, 0755), 0);
layer_fds[i] = openat(fd_tmpfs, path, O_DIRECTORY | O_PATH);
ASSERT_GE(layer_fds[i], 0);
}

ASSERT_EQ(mkdirat(fd_tmpfs, "w", 0755), 0);
fd_work = openat(fd_tmpfs, "w", O_DIRECTORY | O_PATH);
ASSERT_GE(fd_work, 0);

ASSERT_EQ(mkdirat(fd_tmpfs, "u", 0755), 0);
fd_upper = openat(fd_tmpfs, "u", O_DIRECTORY | O_PATH);
ASSERT_GE(fd_upper, 0);

ASSERT_EQ(mkdirat(fd_tmpfs, "l501", 0755), 0);
fd_lower = openat(fd_tmpfs, "l501", O_DIRECTORY | O_PATH);
ASSERT_GE(fd_lower, 0);

ASSERT_EQ(sys_move_mount(fd_tmpfs, "", -EBADF, "/tmp", MOVE_MOUNT_F_EMPTY_PATH), 0);
ASSERT_EQ(close(fd_tmpfs), 0);

fd_context = sys_fsopen("overlay", 0);
ASSERT_GE(fd_context, 0);

ASSERT_EQ(sys_fsconfig(fd_context, FSCONFIG_SET_FD, "workdir", NULL, fd_work), 0);
ASSERT_EQ(close(fd_work), 0);

ASSERT_EQ(sys_fsconfig(fd_context, FSCONFIG_SET_FD, "upperdir", NULL, fd_upper), 0);
ASSERT_EQ(close(fd_upper), 0);

for (int i = 0; i < ARRAY_SIZE(layer_fds); i++) {
ASSERT_EQ(sys_fsconfig(fd_context, FSCONFIG_SET_FD, "lowerdir+", NULL, layer_fds[i]), 0);
ASSERT_EQ(close(layer_fds[i]), 0);
}

ASSERT_NE(sys_fsconfig(fd_context, FSCONFIG_SET_FD, "lowerdir+", NULL, fd_lower), 0);
ASSERT_EQ(close(fd_lower), 0);

ASSERT_EQ(sys_fsconfig(fd_context, FSCONFIG_CMD_CREATE, NULL, NULL, 0), 0);

fd_overlay = sys_fsmount(fd_context, 0, 0);
ASSERT_GE(fd_overlay, 0);
ASSERT_EQ(close(fd_context), 0);
ASSERT_EQ(close(fd_overlay), 0);
}

TEST_HARNESS_MAIN

0 comments on commit 29349a3

Please sign in to comment.