Skip to content

Commit 29349a3

Browse files
committed
Merge patch series "ovl: allow O_PATH file descriptor when specifying 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>
2 parents 2cc0b7f + 85c8700 commit 29349a3

3 files changed

Lines changed: 68 additions & 1 deletion

File tree

fs/autofs/autofs_i.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ void autofs_clean_ino(struct autofs_info *);
218218

219219
static inline int autofs_check_pipe(struct file *pipe)
220220
{
221+
if (pipe->f_mode & FMODE_PATH)
222+
return -EINVAL;
221223
if (!(pipe->f_mode & FMODE_CAN_WRITE))
222224
return -EINVAL;
223225
if (!S_ISFIFO(file_inode(pipe)->i_mode))

fs/fsopen.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ SYSCALL_DEFINE5(fsconfig,
453453
case FSCONFIG_SET_FD:
454454
param.type = fs_value_is_file;
455455
ret = -EBADF;
456-
param.file = fget(aux);
456+
param.file = fget_raw(aux);
457457
if (!param.file)
458458
goto out_key;
459459
param.dirfd = aux;

tools/testing/selftests/filesystems/overlayfs/set_layers_via_fds.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,69 @@ TEST_F(set_layers_via_fds, set_500_layers_via_fds)
214214
ASSERT_EQ(close(fd_overlay), 0);
215215
}
216216

217+
TEST_F(set_layers_via_fds, set_500_layers_via_opath_fds)
218+
{
219+
int fd_context, fd_tmpfs, fd_overlay, fd_work, fd_upper, fd_lower;
220+
int layer_fds[500] = { [0 ... 499] = -EBADF };
221+
222+
ASSERT_EQ(unshare(CLONE_NEWNS), 0);
223+
ASSERT_EQ(sys_mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL), 0);
224+
225+
fd_context = sys_fsopen("tmpfs", 0);
226+
ASSERT_GE(fd_context, 0);
227+
228+
ASSERT_EQ(sys_fsconfig(fd_context, FSCONFIG_CMD_CREATE, NULL, NULL, 0), 0);
229+
fd_tmpfs = sys_fsmount(fd_context, 0, 0);
230+
ASSERT_GE(fd_tmpfs, 0);
231+
ASSERT_EQ(close(fd_context), 0);
232+
233+
for (int i = 0; i < ARRAY_SIZE(layer_fds); i++) {
234+
char path[100];
235+
236+
sprintf(path, "l%d", i);
237+
ASSERT_EQ(mkdirat(fd_tmpfs, path, 0755), 0);
238+
layer_fds[i] = openat(fd_tmpfs, path, O_DIRECTORY | O_PATH);
239+
ASSERT_GE(layer_fds[i], 0);
240+
}
241+
242+
ASSERT_EQ(mkdirat(fd_tmpfs, "w", 0755), 0);
243+
fd_work = openat(fd_tmpfs, "w", O_DIRECTORY | O_PATH);
244+
ASSERT_GE(fd_work, 0);
245+
246+
ASSERT_EQ(mkdirat(fd_tmpfs, "u", 0755), 0);
247+
fd_upper = openat(fd_tmpfs, "u", O_DIRECTORY | O_PATH);
248+
ASSERT_GE(fd_upper, 0);
249+
250+
ASSERT_EQ(mkdirat(fd_tmpfs, "l501", 0755), 0);
251+
fd_lower = openat(fd_tmpfs, "l501", O_DIRECTORY | O_PATH);
252+
ASSERT_GE(fd_lower, 0);
253+
254+
ASSERT_EQ(sys_move_mount(fd_tmpfs, "", -EBADF, "/tmp", MOVE_MOUNT_F_EMPTY_PATH), 0);
255+
ASSERT_EQ(close(fd_tmpfs), 0);
256+
257+
fd_context = sys_fsopen("overlay", 0);
258+
ASSERT_GE(fd_context, 0);
259+
260+
ASSERT_EQ(sys_fsconfig(fd_context, FSCONFIG_SET_FD, "workdir", NULL, fd_work), 0);
261+
ASSERT_EQ(close(fd_work), 0);
262+
263+
ASSERT_EQ(sys_fsconfig(fd_context, FSCONFIG_SET_FD, "upperdir", NULL, fd_upper), 0);
264+
ASSERT_EQ(close(fd_upper), 0);
265+
266+
for (int i = 0; i < ARRAY_SIZE(layer_fds); i++) {
267+
ASSERT_EQ(sys_fsconfig(fd_context, FSCONFIG_SET_FD, "lowerdir+", NULL, layer_fds[i]), 0);
268+
ASSERT_EQ(close(layer_fds[i]), 0);
269+
}
270+
271+
ASSERT_NE(sys_fsconfig(fd_context, FSCONFIG_SET_FD, "lowerdir+", NULL, fd_lower), 0);
272+
ASSERT_EQ(close(fd_lower), 0);
273+
274+
ASSERT_EQ(sys_fsconfig(fd_context, FSCONFIG_CMD_CREATE, NULL, NULL, 0), 0);
275+
276+
fd_overlay = sys_fsmount(fd_context, 0, 0);
277+
ASSERT_GE(fd_overlay, 0);
278+
ASSERT_EQ(close(fd_context), 0);
279+
ASSERT_EQ(close(fd_overlay), 0);
280+
}
281+
217282
TEST_HARNESS_MAIN

0 commit comments

Comments
 (0)