Skip to contentSkip to conversation

ovl: allow O_PATH file descriptor when specifying layers

PATCH In review Christian Brauner 2 participants 5 messages Feb 10, 2025

Subsystems (3)
lore
Share & tools
lore

Christian Brauner

author maintainer
#0 lore raw

v2 cover ovl: allow O_PATH file descriptor when specifying layers

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].

---
Changes in v2:
- Unconditionally enable O_PATH file descriptors.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Christian Brauner (2):
fs: support O_PATH fds with FSCONFIG_SET_FD selftests/overlayfs: test specifying layers as O_PATH file descriptors

 fs/autofs/autofs_i.h                               |  2 +
 fs/fsopen.c                                        |  2 +-
 .../filesystems/overlayfs/set_layers_via_fds.c     | 65 ++++++++++++++++++++++
 3 files changed, 68 insertions(+), 1 deletion(-)

---
base-commit: 2014c95afecee3e76ca4a56956a936e23283f05b
change-id: 20250207-work-overlayfs-38fb9156d4c4

author maintainer
#1 lore raw

PATCH v2 1/2 fs: support O_PATH fds with FSCONFIG_SET_FD

Let FSCONFIG_SET_FD handle O_PATH file descriptors. This is particularly useful in the context of overlayfs where layers can be specified via file descriptors instead of paths. But 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).

Fixes a08557d19ef41 ("ovl: specify layers via file descriptors")
---
 fs/autofs/autofs_i.h | 2 ++
 fs/fsopen.c          | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
fs/autofs/autofs_i.h +2 -0
index 77c7991d89aa..23cea74f9933 100644
@@ -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))
fs/fsopen.c +1 -1
index 094a7f510edf..1aaf4cb2afb2 100644
@@ -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;
Signature · 1 line
2.47.2
Reviewed-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] fs: support O_PATH fds with FSCONFIG_SET_FD

Quoted 19 lines · Christian Brauner

Let FSCONFIG_SET_FD handle O_PATH file descriptors. This is particularly useful in the context of overlayfs where layers can be specified via file descriptors instead of paths. But 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).

Fixes a08557d19ef41 ("ovl: specify layers via file descriptors")
---
 fs/autofs/autofs_i.h | 2 ++
 fs/fsopen.c          | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
fs/autofs/autofs_i.h +0 -0
index 77c7991d89aa..23cea74f9933 100644

fs/autofs/autofs_i.h void autofs_clean_ino(struct autofs_info *); · line 224

Quoted 7 lines
fs/autofs/autofs_i.h +2 -0
@@ -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;

I thought you said the above check is redundant due to the lower check.

In any case feel free to add

Quoted 18 lines
fs/autofs/autofs_i.h +0 -0
        if (!S_ISFIFO(file_inode(pipe)->i_mode))
fs/fsopen.c +1 -2
index 094a7f510edf..1aaf4cb2afb2 100644
@@ -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;
 
--

2.47.2

author maintainer
#3 lore raw

Re: [PATCH v2 1/2] fs: support O_PATH fds with FSCONFIG_SET_FD

Quoted 21 lines · Amir Goldstein
Quoted 20 lines · Christian Brauner

Let FSCONFIG_SET_FD handle O_PATH file descriptors. This is particularly useful in the context of overlayfs where layers can be specified via file descriptors instead of paths. But 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).

Fixes a08557d19ef41 ("ovl: specify layers via file descriptors")
---
 fs/autofs/autofs_i.h | 2 ++
 fs/fsopen.c          | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
fs/autofs/autofs_i.h +0 -0
index 77c7991d89aa..23cea74f9933 100644
@@ -218,6 +218,8 @@ void autofs_clean_ino(struct autofs_info *);
 
Quoted 8 lines

fs/autofs/autofs_i.h void autofs_clean_ino(struct autofs_info *); · line 224

Quoted 6 lines
fs/autofs/autofs_i.h +2 -0
 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;

I thought you said the above check is redundant due to the lower check.

It is but that's only obvious to people quite familiar with VFS code. So I like the explicitly check here.

Quoted 23 lines

In any case feel free to add

Quoted 18 lines
fs/autofs/autofs_i.h +0 -0
        if (!S_ISFIFO(file_inode(pipe)->i_mode))
fs/fsopen.c +1 -2
index 094a7f510edf..1aaf4cb2afb2 100644
@@ -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;
 
--

2.47.2

author maintainer
#4 lore raw

PATCH v2 2/2 selftests/overlayfs: test specifying layers as O_PATH file descriptors

In reply to Christian Brauner

Verify that userspace can specify layers via O_PATH file descriptors.

---
 .../filesystems/overlayfs/set_layers_via_fds.c     | 65 ++++++++++++++++++++++
 1 file changed, 65 insertions(+)
tools/testing/selftests/filesystems/overlayfs/set_layers_via_fds.c +65 -0
index 1d0ae785a667..e693e4102d22 100644
@@ -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
Signature · 1 line
2.47.2