All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/2] ovl: allow O_PATH file descriptor when specifying layers
@ 2025-02-10 12:38 Christian Brauner
  2025-02-10 12:38 ` [PATCH v2 1/2] fs: support O_PATH fds with FSCONFIG_SET_FD Christian Brauner
  2025-02-10 12:39 ` [PATCH v2 2/2] selftests/overlayfs: test specifying layers as O_PATH file descriptors Christian Brauner
  0 siblings, 2 replies; 5+ messages in thread
From: Christian Brauner @ 2025-02-10 12:38 UTC (permalink / raw)
  To: linux-unionfs
  Cc: Miklos Szeredi, Amir Goldstein, Mike Baynton, linux-fsdevel,
	Christian Brauner

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]

Signed-off-by: Christian Brauner <brauner@kernel.org>
---
Changes in v2:
- Unconditionally enable O_PATH file descriptors.
- Link to v1: https://lore.kernel.org/r/20250207-work-overlayfs-v1-0-611976e73373@kernel.org

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 1/2] fs: support O_PATH fds with FSCONFIG_SET_FD
  2025-02-10 12:38 [PATCH v2 0/2] ovl: allow O_PATH file descriptor when specifying layers Christian Brauner
@ 2025-02-10 12:38 ` Christian Brauner
  2025-02-10 13:26   ` Amir Goldstein
  2025-02-10 12:39 ` [PATCH v2 2/2] selftests/overlayfs: test specifying layers as O_PATH file descriptors Christian Brauner
  1 sibling, 1 reply; 5+ messages in thread
From: Christian Brauner @ 2025-02-10 12:38 UTC (permalink / raw)
  To: linux-unionfs
  Cc: Miklos Szeredi, Amir Goldstein, Mike Baynton, linux-fsdevel,
	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")
Signed-off-by: Christian Brauner <brauner@kernel.org>
---
 fs/autofs/autofs_i.h | 2 ++
 fs/fsopen.c          | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/fs/autofs/autofs_i.h b/fs/autofs/autofs_i.h
index 77c7991d89aa..23cea74f9933 100644
--- a/fs/autofs/autofs_i.h
+++ b/fs/autofs/autofs_i.h
@@ -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))
diff --git a/fs/fsopen.c b/fs/fsopen.c
index 094a7f510edf..1aaf4cb2afb2 100644
--- a/fs/fsopen.c
+++ b/fs/fsopen.c
@@ -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


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2 2/2] selftests/overlayfs: test specifying layers as O_PATH file descriptors
  2025-02-10 12:38 [PATCH v2 0/2] ovl: allow O_PATH file descriptor when specifying layers Christian Brauner
  2025-02-10 12:38 ` [PATCH v2 1/2] fs: support O_PATH fds with FSCONFIG_SET_FD Christian Brauner
@ 2025-02-10 12:39 ` Christian Brauner
  1 sibling, 0 replies; 5+ messages in thread
From: Christian Brauner @ 2025-02-10 12:39 UTC (permalink / raw)
  To: linux-unionfs
  Cc: Miklos Szeredi, Amir Goldstein, Mike Baynton, linux-fsdevel,
	Christian Brauner

Verify that userspace can specify layers via O_PATH file descriptors.

Reviewed-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Christian Brauner <brauner@kernel.org>
---
 .../filesystems/overlayfs/set_layers_via_fds.c     | 65 ++++++++++++++++++++++
 1 file changed, 65 insertions(+)

diff --git a/tools/testing/selftests/filesystems/overlayfs/set_layers_via_fds.c b/tools/testing/selftests/filesystems/overlayfs/set_layers_via_fds.c
index 1d0ae785a667..e693e4102d22 100644
--- a/tools/testing/selftests/filesystems/overlayfs/set_layers_via_fds.c
+++ b/tools/testing/selftests/filesystems/overlayfs/set_layers_via_fds.c
@@ -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

-- 
2.47.2


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 1/2] fs: support O_PATH fds with FSCONFIG_SET_FD
  2025-02-10 12:38 ` [PATCH v2 1/2] fs: support O_PATH fds with FSCONFIG_SET_FD Christian Brauner
@ 2025-02-10 13:26   ` Amir Goldstein
  2025-02-10 13:55     ` Christian Brauner
  0 siblings, 1 reply; 5+ messages in thread
From: Amir Goldstein @ 2025-02-10 13:26 UTC (permalink / raw)
  To: Christian Brauner
  Cc: linux-unionfs, Miklos Szeredi, Mike Baynton, linux-fsdevel

On Mon, Feb 10, 2025 at 1:39 PM Christian Brauner <brauner@kernel.org> wrote:
>
> 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")
> Signed-off-by: Christian Brauner <brauner@kernel.org>
> ---
>  fs/autofs/autofs_i.h | 2 ++
>  fs/fsopen.c          | 2 +-
>  2 files changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/fs/autofs/autofs_i.h b/fs/autofs/autofs_i.h
> index 77c7991d89aa..23cea74f9933 100644
> --- a/fs/autofs/autofs_i.h
> +++ b/fs/autofs/autofs_i.h
> @@ -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

Reviewed-by: Amir Goldstein <amir73il@gmail.com>

>         if (!S_ISFIFO(file_inode(pipe)->i_mode))
> diff --git a/fs/fsopen.c b/fs/fsopen.c
> index 094a7f510edf..1aaf4cb2afb2 100644
> --- a/fs/fsopen.c
> +++ b/fs/fsopen.c
> @@ -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
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 1/2] fs: support O_PATH fds with FSCONFIG_SET_FD
  2025-02-10 13:26   ` Amir Goldstein
@ 2025-02-10 13:55     ` Christian Brauner
  0 siblings, 0 replies; 5+ messages in thread
From: Christian Brauner @ 2025-02-10 13:55 UTC (permalink / raw)
  To: Amir Goldstein; +Cc: linux-unionfs, Miklos Szeredi, Mike Baynton, linux-fsdevel

On Mon, Feb 10, 2025 at 02:26:56PM +0100, Amir Goldstein wrote:
> On Mon, Feb 10, 2025 at 1:39 PM Christian Brauner <brauner@kernel.org> wrote:
> >
> > 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")
> > Signed-off-by: Christian Brauner <brauner@kernel.org>
> > ---
> >  fs/autofs/autofs_i.h | 2 ++
> >  fs/fsopen.c          | 2 +-
> >  2 files changed, 3 insertions(+), 1 deletion(-)
> >
> > diff --git a/fs/autofs/autofs_i.h b/fs/autofs/autofs_i.h
> > index 77c7991d89aa..23cea74f9933 100644
> > --- a/fs/autofs/autofs_i.h
> > +++ b/fs/autofs/autofs_i.h
> > @@ -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.

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

> 
> In any case feel free to add
> 
> Reviewed-by: Amir Goldstein <amir73il@gmail.com>
> 
> >         if (!S_ISFIFO(file_inode(pipe)->i_mode))
> > diff --git a/fs/fsopen.c b/fs/fsopen.c
> > index 094a7f510edf..1aaf4cb2afb2 100644
> > --- a/fs/fsopen.c
> > +++ b/fs/fsopen.c
> > @@ -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
> >

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-02-10 13:55 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-10 12:38 [PATCH v2 0/2] ovl: allow O_PATH file descriptor when specifying layers Christian Brauner
2025-02-10 12:38 ` [PATCH v2 1/2] fs: support O_PATH fds with FSCONFIG_SET_FD Christian Brauner
2025-02-10 13:26   ` Amir Goldstein
2025-02-10 13:55     ` Christian Brauner
2025-02-10 12:39 ` [PATCH v2 2/2] selftests/overlayfs: test specifying layers as O_PATH file descriptors Christian Brauner

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.