[PATCH 28/32] nsfs: support exhaustive file handles

Christian Brauner posted 32 patches 9 hours ago
[PATCH 28/32] nsfs: support exhaustive file handles
Posted by Christian Brauner 9 hours ago
Pidfd file handles are exhaustive meaning they don't require a handle on
another pidfd to pass to open_by_handle_at() so it can derive the
filesystem to decode in. Instead it can be derived from the file
handle itself. The same is possible for namespace file handles.

Signed-off-by: Christian Brauner <brauner@kernel.org>
---
 fs/fhandle.c               |  6 ++++++
 fs/internal.h              |  1 +
 fs/nsfs.c                  | 10 ++++++++++
 include/uapi/linux/fcntl.h |  1 +
 4 files changed, 18 insertions(+)

diff --git a/fs/fhandle.c b/fs/fhandle.c
index 7c236f64cdea..f18c855bb0c2 100644
--- a/fs/fhandle.c
+++ b/fs/fhandle.c
@@ -11,6 +11,7 @@
 #include <linux/personality.h>
 #include <linux/uaccess.h>
 #include <linux/compat.h>
+#include <linux/nsfs.h>
 #include "internal.h"
 #include "mount.h"
 
@@ -189,6 +190,11 @@ static int get_path_anchor(int fd, struct path *root)
 		return 0;
 	}
 
+	if (fd == FD_NSFS_ROOT) {
+		nsfs_get_root(root);
+		return 0;
+	}
+
 	return -EBADF;
 }
 
diff --git a/fs/internal.h b/fs/internal.h
index 38e8aab27bbd..a33d18ee5b74 100644
--- a/fs/internal.h
+++ b/fs/internal.h
@@ -355,3 +355,4 @@ int anon_inode_getattr(struct mnt_idmap *idmap, const struct path *path,
 int anon_inode_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
 		       struct iattr *attr);
 void pidfs_get_root(struct path *path);
+void nsfs_get_root(struct path *path);
diff --git a/fs/nsfs.c b/fs/nsfs.c
index a1585a2f4f03..3c6fcf652633 100644
--- a/fs/nsfs.c
+++ b/fs/nsfs.c
@@ -25,6 +25,14 @@
 
 static struct vfsmount *nsfs_mnt;
 
+static struct path nsfs_root_path = {};
+
+void nsfs_get_root(struct path *path)
+{
+	*path = nsfs_root_path;
+	path_get(path);
+}
+
 static long ns_ioctl(struct file *filp, unsigned int ioctl,
 			unsigned long arg);
 static const struct file_operations ns_file_operations = {
@@ -616,4 +624,6 @@ void __init nsfs_init(void)
 	if (IS_ERR(nsfs_mnt))
 		panic("can't set nsfs up\n");
 	nsfs_mnt->mnt_sb->s_flags &= ~SB_NOUSER;
+	nsfs_root_path.mnt = nsfs_mnt;
+	nsfs_root_path.dentry = nsfs_mnt->mnt_root;
 }
diff --git a/include/uapi/linux/fcntl.h b/include/uapi/linux/fcntl.h
index f291ab4f94eb..3741ea1b73d8 100644
--- a/include/uapi/linux/fcntl.h
+++ b/include/uapi/linux/fcntl.h
@@ -111,6 +111,7 @@
 #define PIDFD_SELF_THREAD_GROUP		-10001 /* Current thread group leader. */
 
 #define FD_PIDFS_ROOT			-10002 /* Root of the pidfs filesystem */
+#define FD_NSFS_ROOT			-10003 /* Root of the nsfs filesystem */
 #define FD_INVALID			-10009 /* Invalid file descriptor: -10000 - EBADF = -10009 */
 
 /* Generic flags for the *at(2) family of syscalls. */

-- 
2.47.3
Re: [PATCH 28/32] nsfs: support exhaustive file handles
Posted by Amir Goldstein 6 hours ago
On Wed, Sep 10, 2025 at 4:39 PM Christian Brauner <brauner@kernel.org> wrote:
>
> Pidfd file handles are exhaustive meaning they don't require a handle on
> another pidfd to pass to open_by_handle_at() so it can derive the
> filesystem to decode in. Instead it can be derived from the file
> handle itself. The same is possible for namespace file handles.
>
> Signed-off-by: Christian Brauner <brauner@kernel.org>

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


> ---
>  fs/fhandle.c               |  6 ++++++
>  fs/internal.h              |  1 +
>  fs/nsfs.c                  | 10 ++++++++++
>  include/uapi/linux/fcntl.h |  1 +
>  4 files changed, 18 insertions(+)
>
> diff --git a/fs/fhandle.c b/fs/fhandle.c
> index 7c236f64cdea..f18c855bb0c2 100644
> --- a/fs/fhandle.c
> +++ b/fs/fhandle.c
> @@ -11,6 +11,7 @@
>  #include <linux/personality.h>
>  #include <linux/uaccess.h>
>  #include <linux/compat.h>
> +#include <linux/nsfs.h>
>  #include "internal.h"
>  #include "mount.h"
>
> @@ -189,6 +190,11 @@ static int get_path_anchor(int fd, struct path *root)
>                 return 0;
>         }
>
> +       if (fd == FD_NSFS_ROOT) {
> +               nsfs_get_root(root);
> +               return 0;
> +       }
> +
>         return -EBADF;
>  }
>
> diff --git a/fs/internal.h b/fs/internal.h
> index 38e8aab27bbd..a33d18ee5b74 100644
> --- a/fs/internal.h
> +++ b/fs/internal.h
> @@ -355,3 +355,4 @@ int anon_inode_getattr(struct mnt_idmap *idmap, const struct path *path,
>  int anon_inode_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
>                        struct iattr *attr);
>  void pidfs_get_root(struct path *path);
> +void nsfs_get_root(struct path *path);
> diff --git a/fs/nsfs.c b/fs/nsfs.c
> index a1585a2f4f03..3c6fcf652633 100644
> --- a/fs/nsfs.c
> +++ b/fs/nsfs.c
> @@ -25,6 +25,14 @@
>
>  static struct vfsmount *nsfs_mnt;
>
> +static struct path nsfs_root_path = {};
> +
> +void nsfs_get_root(struct path *path)
> +{
> +       *path = nsfs_root_path;
> +       path_get(path);
> +}
> +
>  static long ns_ioctl(struct file *filp, unsigned int ioctl,
>                         unsigned long arg);
>  static const struct file_operations ns_file_operations = {
> @@ -616,4 +624,6 @@ void __init nsfs_init(void)
>         if (IS_ERR(nsfs_mnt))
>                 panic("can't set nsfs up\n");
>         nsfs_mnt->mnt_sb->s_flags &= ~SB_NOUSER;
> +       nsfs_root_path.mnt = nsfs_mnt;
> +       nsfs_root_path.dentry = nsfs_mnt->mnt_root;
>  }
> diff --git a/include/uapi/linux/fcntl.h b/include/uapi/linux/fcntl.h
> index f291ab4f94eb..3741ea1b73d8 100644
> --- a/include/uapi/linux/fcntl.h
> +++ b/include/uapi/linux/fcntl.h
> @@ -111,6 +111,7 @@
>  #define PIDFD_SELF_THREAD_GROUP                -10001 /* Current thread group leader. */
>
>  #define FD_PIDFS_ROOT                  -10002 /* Root of the pidfs filesystem */
> +#define FD_NSFS_ROOT                   -10003 /* Root of the nsfs filesystem */
>  #define FD_INVALID                     -10009 /* Invalid file descriptor: -10000 - EBADF = -10009 */
>
>  /* Generic flags for the *at(2) family of syscalls. */
>
> --
> 2.47.3
>