Skip to contentSkip to conversation

fs: add failfs

RFC PATCH Superseded Christian Brauner 6 participants 23 messages Jul 23 – Jul 27

Subsystems (20)
Documentation Arm (32-bit) x86 VFS +16 more subsystems

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Christian Brauner

author maintainer
#0 lore raw

cover fs: add failfs

nullfs provides a permanently empty and immutable directory. Lookups fail with ENOENT. The directory can be opened, read, stat, mounted upon. It behaves like nothing is there.

Add its counterpart failfs where the semantics are not "there is nothing here" but "nothing is supported here". Every operation that reaches the filesystem fails with EOPNOTSUPP. Even statfs()/fstatfs() fail so the filesystem cannot be discovered through an fd to it.

EOPNOTSUPP rather than a permission errno keeps that coherent. There is no permission model in which anything could ever be allowed and EACCES or EPERM would merely suggest that different credentials might succeed while EIO would suggest corruption. It also makes hitting the failfs boundary mostly quite dinstinguishable. A task anchoring its lookups at real directory file descriptors may be able to tell a failfs refusal from an ordinary permission failure. I wouldn't go so far as guaranteeing that but it should mostly work.

The root cannot be opened at all not even with O_PATH. It is never reached by a lookup in a parent directory. The only way to a path-walk terminal at the root is a jump through a /proc/<pid>/{root,cwd} magic link or by mountpoint traversal. The root also refuses ->d_weak_revalidate() which the VFS calls for jumped terminals. That closes every remaining way to reference it. An O_PATH
open is refused and name_to_handle_at() cannot encode it into a file handle, and following a magic link into it fails. A plain readlink() of such a link still works and shows "/".

There is a single instance of failfs mounted during early boot via kern_mount() making it logically distinct from every mount namespace.

Since the mount is a member of no mount namespace mounting onto it fails. So nothing can ever be mounted on top of it. It cannot be cloned via OPEN_TREE_CLONE and it does not show up in statmount()/listmount() or /proc/<pid>/mountinfo. The filesystem is not registered so it is not visible in /proc/filesystems and cannot be mounted from userspace.

This lets tasks shed their filesystem state completely. A process with its root directory or working directory in failfs must anchor every path lookup at an explicit file descriptor or is doomed to fail any lookup. Absolute paths, absolute symlinks, and AT_FDCWD-relative lookups simply fail. Followup patches will expose it via a new FD_FAILFS_ROOT file descriptor sentinel understood by fchdir() and the new fchroot() system call.

Fun fact, because of how dynamic binary execution work with PT_INTERP this also currently prevents execution of dynamic binaries because loaders have absolute paths (see selftests).

---
Christian Brauner (7):
fs: add failfs
fs: add FD_FAILFS_ROOT and support it in fchdir()
fs: add fchroot()
fs: support FD_FAILFS_ROOT in fchroot()
arch: hookup fchroot() system call selftests/filesystems: add failfs selftests
Documentation: add failfs documentation

 Documentation/filesystems/failfs.rst               |  64 +++
 Documentation/filesystems/index.rst                |   1 +
 arch/alpha/kernel/syscalls/syscall.tbl             |   1 +
 arch/arm/tools/syscall.tbl                         |   1 +
 arch/arm64/tools/syscall_32.tbl                    |   1 +
 arch/m68k/kernel/syscalls/syscall.tbl              |   1 +
 arch/microblaze/kernel/syscalls/syscall.tbl        |   1 +
 arch/mips/kernel/syscalls/syscall_n32.tbl          |   1 +
 arch/mips/kernel/syscalls/syscall_n64.tbl          |   1 +
 arch/mips/kernel/syscalls/syscall_o32.tbl          |   1 +
 arch/parisc/kernel/syscalls/syscall.tbl            |   1 +
 arch/powerpc/kernel/syscalls/syscall.tbl           |   1 +
 arch/s390/kernel/syscalls/syscall.tbl              |   1 +
 arch/sh/kernel/syscalls/syscall.tbl                |   1 +
 arch/sparc/kernel/syscalls/syscall.tbl             |   1 +
 arch/x86/entry/syscalls/syscall_32.tbl             |   1 +
 arch/x86/entry/syscalls/syscall_64.tbl             |   1 +
 arch/xtensa/kernel/syscalls/syscall.tbl            |   1 +
 fs/Makefile                                        |   2 +-
 fs/failfs.c                                        | 155 +++++++
 fs/internal.h                                      |   3 +
 fs/namespace.c                                     |   1 +
 fs/open.c                                          |  58 ++-
 include/linux/syscalls.h                           |   1 +
 include/uapi/asm-generic/unistd.h                  |   6 +-
 include/uapi/linux/fcntl.h                         |   1 +
 include/uapi/linux/magic.h                         |   1 +
 scripts/syscall.tbl                                |   1 +
 tools/testing/selftests/Makefile                   |   1 +
 .../selftests/filesystems/failfs/.gitignore        |   2 +
 .../testing/selftests/filesystems/failfs/Makefile  |   5 +
 .../selftests/filesystems/failfs/failfs_test.c     | 506 +++++++++++++++++++++
 32 files changed, 821 insertions(+), 3 deletions(-)

---
base-commit: 1590cf0329716306e948a8fc29f1d3ee87d3989f
change-id: 20260723-work-failfs-d86a0c1db48d

author maintainer
#1 lore raw

RFC PATCH 1/7 fs: add failfs

nullfs provides a permanently empty and immutable directory. Lookups fail with ENOENT. The directory can be opened, read, stat, mounted upon. It behaves like nothing is there.

Add its counterpart failfs where the semantics are not "there is nothing here" but "nothing is supported here". Every operation that reaches the filesystem fails with EOPNOTSUPP. Even statfs()/fstatfs() fail so the filesystem cannot be discovered through an fd to it.

EOPNOTSUPP rather than a permission errno keeps that coherent. There is no permission model in which anything could ever be allowed and EACCES or EPERM would merely suggest that different credentials might succeed while EIO would suggest corruption. It also makes hitting the failfs boundary mostly quite dinstinguishable. A task anchoring its lookups at real directory file descriptors may be able to tell a failfs refusal from an ordinary permission failure. I wouldn't go so far as guaranteeing that but it should mostly work.

The root cannot be opened at all not even with O_PATH. It is never reached by a lookup in a parent directory. The only way to a path-walk terminal at the root is a jump through a /proc/<pid>/{root,cwd} magic link or by mountpoint traversal. The root also refuses ->d_weak_revalidate() which the VFS calls for jumped terminals. That closes every remaining way to reference it. An O_PATH
open is refused and name_to_handle_at() cannot encode it into a file handle, and following a magic link into it fails. A plain readlink() of such a link still works and shows "/".

There is a single instance of failfs mounted during early boot via kern_mount() making it logically distinct from every mount namespace.

Since the mount is a member of no mount namespace mounting onto it fails. So nothing can ever be mounted on top of it. It cannot be cloned via OPEN_TREE_CLONE and it does not show up in statmount()/listmount() or /proc/<pid>/mountinfo. The filesystem is not registered so it is not visible in /proc/filesystems and cannot be mounted from userspace.

This lets tasks shed their filesystem state completely. A process with its root directory or working directory in failfs must anchor every path lookup at an explicit file descriptor or is doomed to fail any lookup. Absolute paths, absolute symlinks, and AT_FDCWD-relative lookups simply fail. Followup patches will expose it via a new FD_FAILFS_ROOT file descriptor sentinel understood by fchdir() and the new fchroot() system call.

---
 fs/Makefile                |   2 +-
 fs/failfs.c                | 144 +++++++++++++++++++++++++++++++++++++++++++++
 fs/internal.h              |   2 +
 fs/namespace.c             |   1 +
 include/uapi/linux/magic.h |   1 +
 5 files changed, 149 insertions(+), 1 deletion(-)
fs/Makefile +1 -1
index 89a8a9d207d1..73b6cab7738e 100644
@@ -16,7 +16,7 @@ obj-y :=	open.o read_write.o file_table.o super.o \
 		stack.o fs_struct.o statfs.o fs_pin.o nsfs.o \
 		fs_dirent.o fs_context.o fs_parser.o fsopen.o init.o \
 		kernel_read_file.o mnt_idmapping.o remap_range.o pidfs.o \
-		file_attr.o fserror.o nullfs.o
+		file_attr.o fserror.o nullfs.o failfs.o
 
 obj-$(CONFIG_BUFFER_HEAD)	+= buffer.o mpage.o
 obj-$(CONFIG_PROC_FS)		+= proc_namespace.o
fs/failfs.c new file +144 -0

144 lines not on this page · raw message

fs/internal.h +2 -0
index 355d93f92208..b6d38a5794eb 100644
@@ -362,3 +362,5 @@ 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);
+void failfs_get_root(struct path *path);
+void __init failfs_init(void);
fs/namespace.c +1 -0
index 3d5cd5bf3b05..87c365f2f82b 100644
@@ -6274,6 +6274,7 @@ void __init mnt_init(void)
 	shmem_init();
 	init_rootfs();
 	init_mount_tree();
+	failfs_init();
 }
 
 void put_mnt_ns(struct mnt_namespace *ns)
include/uapi/linux/magic.h +1 -0
index 4f2da935a76c..fd5f0e95648e 100644
@@ -105,5 +105,6 @@
 #define PID_FS_MAGIC		0x50494446	/* "PIDF" */
 #define GUEST_MEMFD_MAGIC	0x474d454d	/* "GMEM" */
 #define NULL_FS_MAGIC		0x4E554C4C	/* "NULL" */
+#define FAIL_FS_MAGIC		0x4641494C	/* "FAIL" */
 
 #endif /* __LINUX_MAGIC_H__ */
Signature · 1 line
2.53.0
author maintainer
#2 lore raw

RFC PATCH 2/7 fs: add FD_FAILFS_ROOT and support it in fchdir()

In reply to Christian Brauner

Add a new file descriptor sentinel FD_FAILFS_ROOT following FD_PIDFS_ROOT and FD_NSFS_ROOT and teach fchdir() to accept it. A process calling fchdir(FD_FAILFS_ROOT) moves its working directory into failfs. Every AT_FDCWD-relative lookup afterwards fails with EOPNOTSUPP including "." and ".." and getcwd() reports the working directory as unreachable from the process root by returning a path prefixed with "(unreachable)". Lookups relative to explicit directory file descriptors are unaffected.

The sentinel is the only way in. No privilege or gating is required. Setting the working directory to a directory in which every operation fails grants nothing and loses nothing that closing file descriptors couldn't lose. An unlinked working directory behaves the same way today modulo errno. The working directory also plays no role in confining ".." resolution so no boundary is weakened.

---
 fs/failfs.c                | 11 +++++++++++
 fs/internal.h              |  1 +
 fs/open.c                  |  5 ++++-
 include/uapi/linux/fcntl.h |  1 +
 4 files changed, 17 insertions(+), 1 deletion(-)
fs/failfs.c +11 -0
index f9d4ba791928..f37e6af9c03c 100644
@@ -3,6 +3,7 @@
 #include <linux/fs.h>
 #include <linux/fs/super_types.h>
 #include <linux/fs_context.h>
+#include <linux/fs_struct.h>
 #include <linux/magic.h>
 #include <linux/mount.h>
 
@@ -124,6 +125,16 @@ static int failfs_init_fs_context(struct fs_context *fc)
 	return 0;
 }
 
+int failfs_current_chdir(void)
+{
+	struct path path;
+
+	failfs_get_root(&path);
+	set_fs_pwd(current->fs, &path);
+	path_put(&path);
+	return 0;
+}
+
 static struct file_system_type failfs_fs_type = {
 	.name			= "failfs",
 	.init_fs_context	= failfs_init_fs_context,
fs/internal.h +1 -0
index b6d38a5794eb..b3c88d999dec 100644
@@ -364,3 +364,4 @@ void pidfs_get_root(struct path *path);
 void nsfs_get_root(struct path *path);
 void failfs_get_root(struct path *path);
 void __init failfs_init(void);
+int failfs_current_chdir(void);
fs/open.c +4 -1
index 408925d7bd0b..56b6032d4d81 100644
@@ -570,9 +570,12 @@ SYSCALL_DEFINE1(chdir, const char __user *, filename)
 
 SYSCALL_DEFINE1(fchdir, unsigned int, fd)
 {
-	CLASS(fd_raw, f)(fd);
 	int error;
 
+	if ((int)fd == FD_FAILFS_ROOT)
+		return failfs_current_chdir();
+
+	CLASS(fd_raw, f)(fd);
 	if (fd_empty(f))
 		return -EBADF;
 
include/uapi/linux/fcntl.h +1 -0
index aadfbf6e0cb3..e43e3de3e9ee 100644
@@ -124,6 +124,7 @@ struct delegation {
 
 #define FD_PIDFS_ROOT			-10002 /* Root of the pidfs filesystem */
 #define FD_NSFS_ROOT			-10003 /* Root of the nsfs filesystem */
+#define FD_FAILFS_ROOT			-10004 /* Root of the failfs filesystem */
 #define FD_INVALID			-10009 /* Invalid file descriptor: -10000 - EBADF = -10009 */
 
 /* Generic flags for the *at(2) family of syscalls. */
Signature · 1 line
2.53.0
author maintainer
#3 lore raw

RFC PATCH 3/7 fs: add fchroot()

In reply to Christian Brauner

Add a file descriptor based counterpart to chroot(2). This has been overdue for a long time. It is the natural companion to fchdir() and avoids re-resolving a path that the caller already holds a file descriptor to. No TOCTOU between resolving the target and changing the root. It composes with modern fd-based APIs meaning it works with O_PATH file descriptors and file descriptors to detached mount trees created via open_tree(OPEN_TREE_CLONE).

The permission model is identical to chroot(2). Rhe caller must have CAP_SYS_CHROOT in its user namespace, must pass MAY_EXEC | MAY_CHDIR permission checks on the target directory, and LSMs are consulted via the same security_path_chroot() hook.

The system call takes a flags argument for future extensibility which must currently be zero.

---
 fs/open.c                | 29 +++++++++++++++++++++++++++++
 include/linux/syscalls.h |  1 +
 2 files changed, 30 insertions(+)
fs/open.c +29 -0
index 56b6032d4d81..c57f641f2e29 100644
@@ -618,6 +618,35 @@ SYSCALL_DEFINE1(chroot, const char __user *, filename)
 	return error;
 }
 
+SYSCALL_DEFINE2(fchroot, int, fd, unsigned int, flags)
+{
+	int error;
+
+	if (flags)
+		return -EINVAL;
+
+	CLASS(fd_raw, f)(fd);
+	if (fd_empty(f))
+		return -EBADF;
+
+	if (!d_can_lookup(fd_file(f)->f_path.dentry))
+		return -ENOTDIR;
+
+	error = file_permission(fd_file(f), MAY_EXEC | MAY_CHDIR);
+	if (error)
+		return error;
+
+	if (!ns_capable(current_user_ns(), CAP_SYS_CHROOT))
+		return -EPERM;
+
+	error = security_path_chroot(&fd_file(f)->f_path);
+	if (error)
+		return error;
+
+	set_fs_root(current->fs, &fd_file(f)->f_path);
+	return 0;
+}
+
 int chmod_common(const struct path *path, umode_t mode)
 {
 	struct inode *inode = path->dentry->d_inode;
include/linux/syscalls.h +1 -0
index 874d9067a43b..8413b624ad47 100644
@@ -457,6 +457,7 @@ asmlinkage long sys_faccessat2(int dfd, const char __user *filename, int mode,
 asmlinkage long sys_chdir(const char __user *filename);
 asmlinkage long sys_fchdir(unsigned int fd);
 asmlinkage long sys_chroot(const char __user *filename);
+asmlinkage long sys_fchroot(int fd, unsigned int flags);
 asmlinkage long sys_fchmod(unsigned int fd, umode_t mode);
 asmlinkage long sys_fchmodat(int dfd, const char __user *filename,
 			     umode_t mode);
Signature · 1 line
2.53.0
author maintainer
#4 lore raw

RFC PATCH 4/7 fs: support FD_FAILFS_ROOT in fchroot()

In reply to Christian Brauner

Allow a process to move its root directory into failfs via fchroot(FD_FAILFS_ROOT). From that point on every absolute path lookup and every absolute symlink fails with EOPNOTSUPP. Combined with fchdir(FD_FAILFS_ROOT) this leaves the process with lookups anchored at explicit directory file descriptors only. It is the fs_struct equivalent of RESOLVE_BENEATH. This allows taks to drop their filesystem state completely.

Callers with CAP_SYS_CHROOT in their user namespace may always do this, mirroring chroot(2). Unprivileged callers are subject to two requirements (which may be loosened later):

(1) no_new_privs must be set

After entering failfs suid binaries on regular mounts remain reachable via inherited directory file descriptors or the working directory. A setuid program executing with an unusable root directory might be tricked by this. I'm not 100% convinced that this is needed but it feels more secure initially and it also forces more no_new_privs on userspace. So win-win imo.

(2) The caller must not already be chrooted.

The root directory is what confines .. resolution. The failfs root can never be reached by walking up a real mount tree. A task whose root is failfs has no .. barrier left below the top of its mount tree. A .. walk from any real directory fd it still holds climbs to the mount-namespace root. Which is kinda the point if you want to do fd-based lookup only. If failfs prevented you from doing that then it doesn't make a lot of sense.

A task that a privileged manager chrooted into a subtree could use chroot()ing into failfs as a way to allow for an inherited fd to resolve it again.

So reject already-chrooted callers closing that issue without losing anything for the intended self-sandboxing use case.

There's also some thought needed around shared fs_struct state. It's obviously possible to chroot into failfs with a shared fs_struct if the caller has CAP_SYS_CHROOT and shares the fs_struct or if the caller is no_new_privs and shares the fs_struct. The non-chrooted-currently requirement still applies.

Once entered, failfs is a throw-away-the-key moment. The task is considered chrooted so it cannot create user namespaces to regain CAP_SYS_CHROOT. chroot()/fchroot() back out require CAP_SYS_CHROOT.

The only other exit is setns() to a mount namespace file descriptor which requires CAP_SYS_ADMIN over the target namespace plus CAP_SYS_CHROOT and CAP_SYS_ADMIN in the caller's user namespace and resets both root and working directory. A process that closes or never had such file descriptors and restricts *chdir()/*chroot()/setns() via seccomp has thrown away the key.

---
 fs/open.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
fs/open.c +24 -0
index c57f641f2e29..8adc9f00889a 100644
@@ -618,6 +618,27 @@ SYSCALL_DEFINE1(chroot, const char __user *, filename)
 	return error;
 }
 
+static int fchroot_failfs(void)
+{
+	struct path path;
+	int error;
+
+	if (!ns_capable(current_user_ns(), CAP_SYS_CHROOT)) {
+		if (!task_no_new_privs(current))
+			return -EPERM;
+		/* Moving the root to failfs lifts the old root's ".." barrier. */
+		if (current_chrooted())
+			return -EPERM;
+	}
+
+	failfs_get_root(&path);
+	error = security_path_chroot(&path);
+	if (!error)
+		set_fs_root(current->fs, &path);
+	path_put(&path);
+	return error;
+}
+
 SYSCALL_DEFINE2(fchroot, int, fd, unsigned int, flags)
 {
 	int error;
@@ -625,6 +646,9 @@ SYSCALL_DEFINE2(fchroot, int, fd, unsigned int, flags)
 	if (flags)
 		return -EINVAL;
 
+	if (fd == FD_FAILFS_ROOT)
+		return fchroot_failfs();
+
 	CLASS(fd_raw, f)(fd);
 	if (fd_empty(f))
 		return -EBADF;
Signature · 1 line
2.53.0
maintainer
#5 lore raw

Re: [PATCH RFC 4/7] fs: support FD_FAILFS_ROOT in fchroot()

Quoted 4 lines · Christian Brauner

Once entered, failfs is a throw-away-the-key moment. The task is considered chrooted so it cannot create user namespaces to regain CAP_SYS_CHROOT. chroot()/fchroot() back out require CAP_SYS_CHROOT.

I kind of alluded to this earlier, but I don't think we should promise this. In particular, I still think we should eventually allow a non-chrooted process with no_new_privs to chroot to any valid directory, and I think we should consider changing the definition of current_chrooted() such that failfs-as-root would cause it to return false. I'm also not sure why it's useful -- if I want to throw away the key to accessing the normal contents of my mountns without changing my mountns, I need to chroot somewhere (like failfs) *and* I need to somehow prevent myself from getting an fd or a magic link back to the ordinary mountns contents. If I somehow get such an fd, preventing my from fchrooting to it doesn't seem to accomplish anything, since I could traverse the fd with openat, etc just as easily, or even fchdir to it and use relative paths.

My understanding of the exact workings of the mount tree is possibly not good enough to specify the semantics I think are correct quite exactly, but I think a decent approximation is that a task should be considered to be not chrooted if its root is at the top of its mount tree or is not in a mount tree at all.

--Andy

author maintainer
#6 lore raw

Re: [PATCH RFC 4/7] fs: support FD_FAILFS_ROOT in fchroot()

Quoted 6 lines · Andy Lutomirski
Quoted 4 lines · Christian Brauner

Once entered, failfs is a throw-away-the-key moment. The task is considered chrooted so it cannot create user namespaces to regain CAP_SYS_CHROOT. chroot()/fchroot() back out require CAP_SYS_CHROOT.

Quoted 6 lines

I kind of alluded to this earlier, but I don't think we should promise this. In particular, I still think we should eventually allow a non-chrooted process with no_new_privs to chroot to any valid directory, and I think we should consider changing the definition of current_chrooted() such that failfs-as-root would cause it to return false. I'm also not sure why it's useful -- if I want to throw away

I had considered that but introducing this change alongside this series felt too spicy for me.

Quoted 7 lines

the key to accessing the normal contents of my mountns without changing my mountns, I need to chroot somewhere (like failfs) *and* I need to somehow prevent myself from getting an fd or a magic link back to the ordinary mountns contents. If I somehow get such an fd, preventing my from fchrooting to it doesn't seem to accomplish anything, since I could traverse the fd with openat, etc just as easily, or even fchdir to it and use relative paths.

I think I generally agree with you. My main concern here is mostly about changing very long-standing behavior here and making failfs special here feels like it could easily misused.

Quoted 6 lines

My understanding of the exact workings of the mount tree is possibly not good enough to specify the semantics I think are correct quite exactly, but I think a decent approximation is that a task should be considered to be not chrooted if its root is at the top of its mount tree or is not in a mount tree at all.

If we could altering this definition from the patchset I would be in favor of it. I'm curious what your take is though.

maintainer
#7 lore raw

Re: [PATCH RFC 4/7] fs: support FD_FAILFS_ROOT in fchroot()

Quoted 15 lines · Christian Brauner
Quoted 6 lines · Andy Lutomirski
Quoted 4 lines · Christian Brauner

Once entered, failfs is a throw-away-the-key moment. The task is considered chrooted so it cannot create user namespaces to regain CAP_SYS_CHROOT. chroot()/fchroot() back out require CAP_SYS_CHROOT.

Quoted 6 lines

I kind of alluded to this earlier, but I don't think we should promise this. In particular, I still think we should eventually allow a non-chrooted process with no_new_privs to chroot to any valid directory, and I think we should consider changing the definition of current_chrooted() such that failfs-as-root would cause it to return false. I'm also not sure why it's useful -- if I want to throw away

Quoted 2 lines

I had considered that but introducing this change alongside this series felt too spicy for me.

I think I agree. My only actual objection to your series as is is that your wording seems to make a promise that I think shouldn't be made.

Quoted 9 lines
Quoted 8 lines

the key to accessing the normal contents of my mountns without changing my mountns, I need to chroot somewhere (like failfs) *and* I need to somehow prevent myself from getting an fd or a magic link back to the ordinary mountns contents. If I somehow get such an fd, preventing my from fchrooting to it doesn't seem to accomplish anything, since I could traverse the fd with openat, etc just as easily, or even fchdir to it and use relative paths.

Quoted 3 lines

I think I generally agree with you. My main concern here is mostly about changing very long-standing behavior here and making failfs special here feels like it could easily misused.

I don't think either of us are really suggesting making failfs special. I agree about the long-standing behavior.

Quoted 11 lines
Quoted 7 lines

My understanding of the exact workings of the mount tree is possibly not good enough to specify the semantics I think are correct quite exactly, but I think a decent approximation is that a task should be considered to be not chrooted if its root is at the top of its mount tree or is not in a mount tree at all.

If we could altering this definition from the patchset I would be in favor of it. I'm curious what your take is though.

I can't quite parse your question.

I have three sort-of-concrete proposals:

(a) current_chrooted returns false if follow_dotdot starting at current root but with nd->root equal to the namespace root would succeed and return the current root. I think this is fairly solid. The main caveat I can think of is that someone could create a detached mount tree, chroot to its root, then fork and have the child:

- drop privileges
- set no_new_privs
- chroot somewhere else

then the parent or another privileged task mounts that tree somewhere, and the child could now access the parent of the mountpoint. Any actual exploit based on this seems farfetched.

(b) separate the concepts of fs-root-for-absolute-paths and fs-root-for-blocking-dotdot. IMO this actually makes sense but would would be a much more intrusive change. We would have fs->root_of_abspaths and fs->dotdot_blocking_root, and fchroot would only change root_of_abspaths unless a flag is set asking to change both. (And document that setting that flag is not advised and that one should create a detached mount tree instead for 90% of use cases.) And no_new_privs tasks would be allowed to freely change
root_of_abspaths.

I like (b) conceptually in the sense that I wish no one had ever invented chroot-for-security in the first place. I'm not sure I like it as an actual practical idea because it has a much larger scope. Also (a) is almost equivalent as long as no one ever sets the root to something that isn't the root of a mount tree, and it really would confuse some people if "/.." was not equivalent to "./".

(c) Add a bit to fs_struct called something like chroot_locked. chroot() sets the bit if it chroots anywhere other than "/" or its equivalent. fchroot does not set it unless a flag asking for it is set. Switching mount namespace clears the bit. If you have no_new_privs and don't have chroot_locked then you can chroot.

--Andy

author maintainer
#8 lore raw

Re: [PATCH RFC 4/7] fs: support FD_FAILFS_ROOT in fchroot()

Quoted 16 lines · Andy Lutomirski
Quoted 15 lines · Christian Brauner
Quoted 12 lines · Andy Lutomirski
Quoted 4 lines · Christian Brauner

Once entered, failfs is a throw-away-the-key moment. The task is considered chrooted so it cannot create user namespaces to regain CAP_SYS_CHROOT. chroot()/fchroot() back out require CAP_SYS_CHROOT.

I kind of alluded to this earlier, but I don't think we should promise this. In particular, I still think we should eventually allow a non-chrooted process with no_new_privs to chroot to any valid directory, and I think we should consider changing the definition of current_chrooted() such that failfs-as-root would cause it to return false. I'm also not sure why it's useful -- if I want to throw away

Quoted 6 lines
Quoted 2 lines

I had considered that but introducing this change alongside this series felt too spicy for me.

I think I agree. My only actual objection to your series as is is that your wording seems to make a promise that I think shouldn't be made.

Agreed.

Quoted 25 lines
Quoted 10 lines
Quoted 9 lines

the key to accessing the normal contents of my mountns without changing my mountns, I need to chroot somewhere (like failfs) *and* I need to somehow prevent myself from getting an fd or a magic link back to the ordinary mountns contents. If I somehow get such an fd, preventing my from fchrooting to it doesn't seem to accomplish anything, since I could traverse the fd with openat, etc just as easily, or even fchdir to it and use relative paths.

Quoted 3 lines

I think I generally agree with you. My main concern here is mostly about changing very long-standing behavior here and making failfs special here feels like it could easily misused.

I don't think either of us are really suggesting making failfs special. I agree about the long-standing behavior.

Quoted 8 lines
Quoted 7 lines

My understanding of the exact workings of the mount tree is possibly not good enough to specify the semantics I think are correct quite exactly, but I think a decent approximation is that a task should be considered to be not chrooted if its root is at the top of its mount tree or is not in a mount tree at all.

Quoted 5 lines
Quoted 3 lines

If we could altering this definition from the patchset I would be in favor of it. I'm curious what your take is though.

I can't quite parse your question.

Maybe I should shunt everything I write through an LLM so it catches grammatical nonsense.

It was basically just trying to say "let's not redesign current_chrooted()" in this series - which I already did above.

Quoted 1 line

I have three sort-of-concrete proposals:

The best proposals...

Quoted 6 lines

(a) current_chrooted returns false if follow_dotdot starting at current root but with nd->root equal to the namespace root would succeed and return the current root. I think this is fairly solid. The main caveat I can think of is that someone could create a detached mount tree, chroot to its root, then fork and have the child:

Quoted 7 lines

- drop privileges
- set no_new_privs
- chroot somewhere else

then the parent or another privileged task mounts that tree somewhere, and the child could now access the parent of the mountpoint. Any actual exploit based on this seems farfetched.

It would also be easy to prevent unprivileged chroot()ing into detached mount trees. They're easy to recognize and it's not a super common use-case.

Quoted 11 lines

(b) separate the concepts of fs-root-for-absolute-paths and fs-root-for-blocking-dotdot. IMO this actually makes sense but would would be a much more intrusive change. We would have fs->root_of_abspaths and fs->dotdot_blocking_root, and fchroot would only change root_of_abspaths unless a flag is set asking to change both. (And document that setting that flag is not advised and that one should create a detached mount tree instead for 90% of use cases.) And no_new_privs tasks would be allowed to freely change
root_of_abspaths.

Quoted 6 lines

I like (b) conceptually in the sense that I wish no one had ever invented chroot-for-security in the first place. I'm not sure I like it as an actual practical idea because it has a much larger scope. Also (a) is almost equivalent as long as no one ever sets the root to something that isn't the root of a mount tree, and it really would confuse some people if "/.." was not equivalent to "./".

Yeah, I'm not so excited about this part...

Quoted 6 lines

(c) Add a bit to fs_struct called something like chroot_locked. chroot() sets the bit if it chroots anywhere other than "/" or its equivalent. fchroot does not set it unless a flag asking for it is set. Switching mount namespace clears the bit. If you have no_new_privs and don't have chroot_locked then you can chroot.

That sounds reasonable - the fchroot() extension is also a nice touch.

maintainer
#9 lore raw

Re: [PATCH RFC 4/7] fs: support FD_FAILFS_ROOT in fchroot()

Quoted 78 lines · Christian Brauner
Quoted 16 lines · Andy Lutomirski
Quoted 15 lines · Christian Brauner
Quoted 12 lines · Andy Lutomirski
Quoted 4 lines · Christian Brauner

Once entered, failfs is a throw-away-the-key moment. The task is considered chrooted so it cannot create user namespaces to regain CAP_SYS_CHROOT. chroot()/fchroot() back out require CAP_SYS_CHROOT.

I kind of alluded to this earlier, but I don't think we should promise this. In particular, I still think we should eventually allow a non-chrooted process with no_new_privs to chroot to any valid directory, and I think we should consider changing the definition of current_chrooted() such that failfs-as-root would cause it to return false. I'm also not sure why it's useful -- if I want to throw away

Quoted 6 lines
Quoted 2 lines

I had considered that but introducing this change alongside this series felt too spicy for me.

I think I agree. My only actual objection to your series as is is that your wording seems to make a promise that I think shouldn't be made.

Agreed.

Quoted 25 lines
Quoted 10 lines
Quoted 9 lines

the key to accessing the normal contents of my mountns without changing my mountns, I need to chroot somewhere (like failfs) *and* I need to somehow prevent myself from getting an fd or a magic link back to the ordinary mountns contents. If I somehow get such an fd, preventing my from fchrooting to it doesn't seem to accomplish anything, since I could traverse the fd with openat, etc just as easily, or even fchdir to it and use relative paths.

Quoted 3 lines

I think I generally agree with you. My main concern here is mostly about changing very long-standing behavior here and making failfs special here feels like it could easily misused.

I don't think either of us are really suggesting making failfs special. I agree about the long-standing behavior.

Quoted 8 lines
Quoted 7 lines

My understanding of the exact workings of the mount tree is possibly not good enough to specify the semantics I think are correct quite exactly, but I think a decent approximation is that a task should be considered to be not chrooted if its root is at the top of its mount tree or is not in a mount tree at all.

Quoted 5 lines
Quoted 3 lines

If we could altering this definition from the patchset I would be in favor of it. I'm curious what your take is though.

I can't quite parse your question.

Maybe I should shunt everything I write through an LLM so it catches grammatical nonsense.

It was basically just trying to say "let's not redesign current_chrooted()" in this series - which I already did above.

Quoted 1 line

I have three sort-of-concrete proposals:

The best proposals...

Quoted 10 lines

(a) current_chrooted returns false if follow_dotdot starting at current root but with nd->root equal to the namespace root would succeed and return the current root. I think this is fairly solid. The main caveat I can think of is that someone could create a detached mount tree, chroot to its root, then fork and have the child:

- drop privileges
- set no_new_privs
- chroot somewhere else

Quoted 7 lines
Quoted 3 lines

then the parent or another privileged task mounts that tree somewhere, and the child could now access the parent of the mountpoint. Any actual exploit based on this seems farfetched.

It would also be easy to prevent unprivileged chroot()ing into detached mount trees. They're easy to recognize and it's not a super common use-case.

In some sense chrooting into a detached mount tree is an entirely reasonable operation. Make your favorite mount tree and chroot there -- it's like mount-namespaces minus the namespace.

#10 lore raw

Re: [PATCH RFC 4/7] fs: support FD_FAILFS_ROOT in fchroot()

Very happy to see this patch series appear; thanks Christian!

Also happy with where this specific thread is going. I will just add that an additional flag in fs_struct to force RESOLVE_BENEATH in *all* path lookup would also be nice. The combination of:

root = pwd = failfs;
chroot_locked = false;
always_resolve_beneath = true;

goes along way to getting Capsicum's "capability mode". Then we just need to figure out things like more capabilities for directory file descriptors per Andy's brainstorming thread thread (which I ought to get to replying to).

John

#11 lore raw

Re: [PATCH RFC 4/7] fs: support FD_FAILFS_ROOT in fchroot()

Quoted 10 lines · John Ericson

Very happy to see this patch series appear; thanks Christian!

Also happy with where this specific thread is going. I will just add that an additional flag in fs_struct to force RESOLVE_BENEATH in *all* path lookup would also be nice. The combination of:

Quoted 5 lines

root = pwd = failfs;
chroot_locked = false;
always_resolve_beneath = true;

goes along way to getting Capsicum's "capability mode".

One thing I dislike about this is that it’s (approximately) a task mode. Why should an fd representing a capability represent one capability (a subtree) to one task but a different capability (dotdot) in a different task? Do we need to prevent unsharing a userns if this bit is set? Do we want to have to worry about any task that could potentially SCM_RIGHTS and fd to a task without that bit set? I prefer beneathness being an OFD property.

Note that the obvious giant holes in capsicum-without-a-mode (root and cwd) and their respective dotdots are fixed, quite cleanly, by this current patch set.

I also think it’s a bit of an ancient mistake that paths like /.. work at all instead of generating errors. Maybe a future beneath-only fd could also disallow trying to dotdot past the root, even for symlinks that contain dotdots. This isn’t just an aesthetic thing - if I have ~/foo/bar being a symlink to ../bar, I think it would be a mistake for a beneath-only OFD to ~/foo to have “bar” resolve successfully to the wrong place.

Quoted 7 lines

Then we just
need to figure out things like more capabilities for directory file descriptors per Andy's brainstorming thread thread (which I ought to get to replying to).

John

#12 lore raw

Re: [PATCH RFC 4/7] fs: support FD_FAILFS_ROOT in fchroot()

Quoted 7 lines · Andy Lutomirski



Quoted 2 lines

goes along way to getting Capsicum's "capability mode".

One thing I dislike about this is that it’s (approximately) a task mode. Why should an fd representing a capability represent one capability (a subtree) to one task but a different capability (dotdot) in a different task? Do we need to prevent unsharing a userns if this bit is set? Do we want to have to worry about any task that could potentially SCM_RIGHTS and fd to a task without that bit set? I prefer beneathness being an OFD property.

Sigh, I keep thinking and typing OFD, and that won’t magically make it be correct. All these privilege restrictions applied to a file or path reference need to follow every user-controllable reference around correctly. So if I fchdir or fchroot to a restricted OFD, my cwd or root needs to preserve those restrictions, and reopening "/" or "." or any other path (via root or cwd or openat etc) needs to respect the restrictions as well. Putting it in struct path may or may not be a good idea.

And, if we allow cloning a restricted reference to a subtree to mount it, we need to make that work correctly too.

Off the top of my head, for a minimum viable product, we could disallow cloning/mounting from a restricted reference. And if we want to try delegation (as in my other braindump email), we could try *only* allowing delegation via clones/mounts and not in an OFD or cwd or root, since there are potentially mildly thorny or at least unintuitive interactions between symlinks and privilege delegation, especially if mounts can delegate. (And I think that delegation via OFD/cwd/root but not via mount is so weak that there's almost no point -- I want to delegate to a container, and that requires mounts.)

Quoted 12 lines

Note that the obvious giant holes in capsicum-without-a-mode (root and cwd) and their respective dotdots are fixed, quite cleanly, by this current patch set.

I also think it’s a bit of an ancient mistake that paths like /.. work at all instead of generating errors. Maybe a future beneath-only fd could also disallow trying to dotdot past the root, even for symlinks that contain dotdots. This isn’t just an aesthetic thing - if I have ~/foo/bar being a symlink to ../bar, I think it would be a mistake for a beneath-only OFD to ~/foo to have “bar” resolve successfully to the wrong place.

Quoted 7 lines

Then we just
need to figure out things like more capabilities for directory file descriptors per Andy's brainstorming thread thread (which I ought to get to replying to).

John

author maintainer
#13 lore raw

Re: [PATCH RFC 4/7] fs: support FD_FAILFS_ROOT in fchroot()

Quoted 8 lines · Andy Lutomirski
Quoted 7 lines · Andy Lutomirski



Quoted 2 lines

goes along way to getting Capsicum's "capability mode".

One thing I dislike about this is that it’s (approximately) a task mode. Why should an fd representing a capability represent one capability (a subtree) to one task but a different capability (dotdot) in a different task? Do we need to prevent unsharing a userns if this bit is set? Do we want to have to worry about any task that could potentially SCM_RIGHTS and fd to a task without that bit set? I prefer beneathness being an OFD property.

Quoted 8 lines

Sigh, I keep thinking and typing OFD, and that won’t magically make it be correct. All these privilege restrictions applied to a file or path reference need to follow every user-controllable reference around correctly. So if I fchdir or fchroot to a restricted OFD, my cwd or root needs to preserve those restrictions, and reopening "/" or "." or any other path (via root or cwd or openat etc) needs to respect the restrictions as well. Putting it in struct path may or may not be a good idea.

I think making struct path carry more meaning than it does right now will cause us endless amounts of subtle issues. I fear that change very very much. That will have such a fundamental ripple effect across _all_ corners of the codebase that I'm very unwilling to be led down that road. And I think Al had traditionally similar reservations as me. One of the really thorny issues with the O_PATH upgrade masks was precisely the struct path modification.

#14 lore raw

Re: [PATCH RFC 4/7] fs: support FD_FAILFS_ROOT in fchroot()

Quoted 22 lines · Christian Brauner
Quoted 8 lines · Andy Lutomirski
Quoted 7 lines
Quoted 1 line · Andy Lutomirski



Quoted 2 lines

goes along way to getting Capsicum's "capability mode".

One thing I dislike about this is that it’s (approximately) a task mode. Why should an fd representing a capability represent one capability (a subtree) to one task but a different capability (dotdot) in a different task? Do we need to prevent unsharing a userns if this bit is set? Do we want to have to worry about any task that could potentially SCM_RIGHTS and fd to a task without that bit set? I prefer beneathness being an OFD property.

Quoted 8 lines

Sigh, I keep thinking and typing OFD, and that won’t magically make it be correct. All these privilege restrictions applied to a file or path reference need to follow every user-controllable reference around correctly. So if I fchdir or fchroot to a restricted OFD, my cwd or root needs to preserve those restrictions, and reopening "/" or "." or any other path (via root or cwd or openat etc) needs to respect the restrictions as well. Putting it in struct path may or may not be a good idea.

Quoted 8 lines

I think making struct path carry more meaning than it does right now will cause us endless amounts of subtle issues. I fear that change very very much. That will have such a fundamental ripple effect across _all_ corners of the codebase that I'm very unwilling to be led down that road. And I think Al had traditionally similar reservations as me. One of the really thorny issues with the O_PATH upgrade masks was precisely the struct path modification.

The alternative (I think) is to enumerate all the places in the kernel that a path (or file?) reference exists and would want to carry various restriction bits.

I can think of:

Resolution (nameidata?)

struct file

cwd

root

optionally, vfsmount

Is that it? If I open a directory with a no-dotdot restriction, what can I possibly do with it that needs to respect that restriction? Each such operation either needs to respect the restriction or fail outright. fchdir really ought to work. fchroot probably should, too. We could just not allow clone/mount (at first?). All the /proc links either start at cwd, root, or a struct file, right?

There are things like overlayfs layers, but failing an attempt to use a restrictive reference to a path as an overlayfs layer doesn’t seem to terrible to me.

I think that, logically, we could create a struct path_restrictions and add it to struct file and two copies to fs_struct. And we wire it up into nameidata. And we carefully make every operation that doesn’t understand this fail if there are restrictions, which could be some new nameidata flag meaning “I understand path restrictions” where any attempt to resolve a path starting at root or cwd or a struct file with restrictions that doesn’t set that flag will fail immediately without even trying to resolve the path.

My intuition is that the implementation wouldn’t be so bad and that people could chip away at the unsupported bits that annoy them over time.

—Andy

#15 lore raw

Re: [PATCH RFC 4/7] fs: support FD_FAILFS_ROOT in fchroot()

In reply to Andy Lutomirski
Quoted 10 lines · Andy Lutomirski
Quoted 4 lines · Christian Brauner

Once entered, failfs is a throw-away-the-key moment. The task is considered chrooted so it cannot create user namespaces to regain CAP_SYS_CHROOT. chroot()/fchroot() back out require CAP_SYS_CHROOT.

I kind of alluded to this earlier, but I don't think we should promise this. In particular, I still think we should eventually allow a non-chrooted process with no_new_privs to chroot to any valid directory, and I think we should consider changing the definition of

FWIW, I kind of like that idea, but I think we need to look out for how this interacts with path-based security checks and auditing.

Examples I can think of immediately are:

- bpf_path_d_path() is intended for BPF LSMs that do
security/auditing, but uses d_path() which stops at the chroot.
- audit_log_d_path() uses d_path()

These are already kinda broken in the face of unprivileged user namespaces, but adding unprivileged arbitrary chroot() would break them even more.

#16 lore raw

Re: [PATCH RFC 4/7] fs: support FD_FAILFS_ROOT in fchroot()

In reply to Christian Brauner
Quoted 5 lines · Christian Brauner

There's also some thought needed around shared fs_struct state. It's obviously possible to chroot into failfs with a shared fs_struct if the caller has CAP_SYS_CHROOT and shares the fs_struct or if the caller is no_new_privs and shares the fs_struct. The non-chrooted-currently requirement still applies.

Having CAP_SYS_CHROOT and sharing the fs_struct should not be an issue because we already ensure that any tasks that share fs_struct are in the same user namespace, so CAP_SYS_CHROOT is implicitly also held relative to other users of the fs_struct. (In particular, userns_install() and unshare_fs() ensure this.) (Except if LSMs get involved with weird policy, I guess.) That's what makes the existing chroot() syscall safe.

But yeah, the no_new_privs check I suggested is kind of a pain...

maintainer
#17 lore raw

Re: [PATCH RFC 4/7] fs: support FD_FAILFS_ROOT in fchroot()

Quoted 16 lines · Jann Horn
Quoted 5 lines · Christian Brauner

There's also some thought needed around shared fs_struct state. It's obviously possible to chroot into failfs with a shared fs_struct if the caller has CAP_SYS_CHROOT and shares the fs_struct or if the caller is no_new_privs and shares the fs_struct. The non-chrooted-currently requirement still applies.

Having CAP_SYS_CHROOT and sharing the fs_struct should not be an issue because we already ensure that any tasks that share fs_struct are in the same user namespace, so CAP_SYS_CHROOT is implicitly also held relative to other users of the fs_struct. (In particular, userns_install() and unshare_fs() ensure this.) (Except if LSMs get involved with weird policy, I guess.) That's what makes the existing chroot() syscall safe.

Quoted 2 lines

But yeah, the no_new_privs check I suggested is kind of a pain...

It's dorky, but we could say that your fs_struct must be non-shared for unprivileged chroot to be allowed.

(I'm so mad at Docker for blocking *all* unshare calls in their default policy. Not a showstopper, but it's extremely annoying. Yes, I have an issue open that has been completely ignored.)

author maintainer
#18 lore raw

RFC PATCH 5/7 arch: hookup fchroot() system call

In reply to Christian Brauner

Wire up the fchroot() system call as number 472 on (nearly) all architectures.

---
 arch/alpha/kernel/syscalls/syscall.tbl      | 1 +
 arch/arm/tools/syscall.tbl                  | 1 +
 arch/arm64/tools/syscall_32.tbl             | 1 +
 arch/m68k/kernel/syscalls/syscall.tbl       | 1 +
 arch/microblaze/kernel/syscalls/syscall.tbl | 1 +
 arch/mips/kernel/syscalls/syscall_n32.tbl   | 1 +
 arch/mips/kernel/syscalls/syscall_n64.tbl   | 1 +
 arch/mips/kernel/syscalls/syscall_o32.tbl   | 1 +
 arch/parisc/kernel/syscalls/syscall.tbl     | 1 +
 arch/powerpc/kernel/syscalls/syscall.tbl    | 1 +
 arch/s390/kernel/syscalls/syscall.tbl       | 1 +
 arch/sh/kernel/syscalls/syscall.tbl         | 1 +
 arch/sparc/kernel/syscalls/syscall.tbl      | 1 +
 arch/x86/entry/syscalls/syscall_32.tbl      | 1 +
 arch/x86/entry/syscalls/syscall_64.tbl      | 1 +
 arch/xtensa/kernel/syscalls/syscall.tbl     | 1 +
 include/uapi/asm-generic/unistd.h           | 6 +++++-
 scripts/syscall.tbl                         | 1 +
 18 files changed, 22 insertions(+), 1 deletion(-)
arch/alpha/kernel/syscalls/syscall.tbl +1 -0
index f31b7afffc34..52e3538cc7df 100644
@@ -511,3 +511,4 @@
 579	common	file_setattr			sys_file_setattr
 580	common	listns				sys_listns
 581	common	rseq_slice_yield		sys_rseq_slice_yield
+582	common	fchroot				sys_fchroot
arch/arm/tools/syscall.tbl +1 -0
index 94351e22bfcf..55717ed32c27 100644
@@ -486,3 +486,4 @@
 469	common	file_setattr			sys_file_setattr
 470	common	listns				sys_listns
 471	common	rseq_slice_yield		sys_rseq_slice_yield
+472	common	fchroot				sys_fchroot
arch/arm64/tools/syscall_32.tbl +1 -0
index 62d93d88e0fe..df2d1d82fb3c 100644
@@ -483,3 +483,4 @@
 469	common	file_setattr			sys_file_setattr
 470	common	listns				sys_listns
 471	common	rseq_slice_yield		sys_rseq_slice_yield
+472	common	fchroot				sys_fchroot
arch/m68k/kernel/syscalls/syscall.tbl +1 -0
index 248934257101..ba7a4d8903d0 100644
@@ -471,3 +471,4 @@
 469	common	file_setattr			sys_file_setattr
 470	common	listns				sys_listns
 471	common	rseq_slice_yield		sys_rseq_slice_yield
+472	common	fchroot				sys_fchroot
arch/microblaze/kernel/syscalls/syscall.tbl +1 -0
index 223d26303627..c55a5c96f49b 100644
@@ -477,3 +477,4 @@
 469	common	file_setattr			sys_file_setattr
 470	common	listns				sys_listns
 471	common	rseq_slice_yield		sys_rseq_slice_yield
+472	common	fchroot				sys_fchroot
arch/mips/kernel/syscalls/syscall_n32.tbl +1 -0
index 7430714e2b8f..9ae88e4eac61 100644
@@ -410,3 +410,4 @@
 469	n32	file_setattr			sys_file_setattr
 470	n32	listns				sys_listns
 471	n32	rseq_slice_yield		sys_rseq_slice_yield
+472	n32	fchroot				sys_fchroot
arch/mips/kernel/syscalls/syscall_n64.tbl +1 -0
index 630aab9e5425..83dc93a0712f 100644
@@ -386,3 +386,4 @@
 469	n64	file_setattr			sys_file_setattr
 470	n64	listns				sys_listns
 471	n64	rseq_slice_yield		sys_rseq_slice_yield
+472	n64	fchroot				sys_fchroot
arch/mips/kernel/syscalls/syscall_o32.tbl +1 -0
index 128653112284..9c62429c9b7b 100644
@@ -459,3 +459,4 @@
 469	o32	file_setattr			sys_file_setattr
 470	o32	listns				sys_listns
 471	o32	rseq_slice_yield		sys_rseq_slice_yield
+472	o32	fchroot				sys_fchroot
arch/parisc/kernel/syscalls/syscall.tbl +1 -0
index c6331dad9461..88adc4016cce 100644
@@ -470,3 +470,4 @@
 469	common	file_setattr			sys_file_setattr
 470	common	listns				sys_listns
 471	common	rseq_slice_yield		sys_rseq_slice_yield
+472	common	fchroot				sys_fchroot
arch/powerpc/kernel/syscalls/syscall.tbl +1 -0
index 4fcc7c58a105..cfbb70039ff0 100644
@@ -562,3 +562,4 @@
 469	common	file_setattr			sys_file_setattr
 470	common	listns				sys_listns
 471	nospu	rseq_slice_yield		sys_rseq_slice_yield
+472	common	fchroot				sys_fchroot
arch/s390/kernel/syscalls/syscall.tbl +1 -0
index 09a7ef04d979..1b45e68a217b 100644
@@ -398,3 +398,4 @@
 469	common	file_setattr			sys_file_setattr
 470	common	listns				sys_listns
 471	common	rseq_slice_yield		sys_rseq_slice_yield
+472	common	fchroot				sys_fchroot
arch/sh/kernel/syscalls/syscall.tbl +1 -0
index 70b315cbe710..ace068dff0de 100644
@@ -475,3 +475,4 @@
 469	common	file_setattr			sys_file_setattr
 470	common	listns				sys_listns
 471	common	rseq_slice_yield		sys_rseq_slice_yield
+472	common	fchroot				sys_fchroot
arch/sparc/kernel/syscalls/syscall.tbl +1 -0
index 7e71bf7fcd14..5b9fe0e8140f 100644
@@ -517,3 +517,4 @@
 469	common	file_setattr			sys_file_setattr
 470	common	listns				sys_listns
 471	common	rseq_slice_yield		sys_rseq_slice_yield
+472	common	fchroot				sys_fchroot
arch/x86/entry/syscalls/syscall_32.tbl +1 -0
index f832ebd2d79b..2c172ef48dfd 100644
@@ -477,3 +477,4 @@
 469	i386	file_setattr		sys_file_setattr
 470	i386	listns			sys_listns
 471	i386	rseq_slice_yield	sys_rseq_slice_yield
+472	i386	fchroot			sys_fchroot
arch/x86/entry/syscalls/syscall_64.tbl +1 -0
index 524155d655da..d5b6045b0090 100644
@@ -396,6 +396,7 @@
 469	common	file_setattr		sys_file_setattr
 470	common	listns			sys_listns
 471	common	rseq_slice_yield	sys_rseq_slice_yield
+472	common	fchroot			sys_fchroot
 
 #
 # Due to a historical design error, certain syscalls are numbered differently
arch/xtensa/kernel/syscalls/syscall.tbl +1 -0
index a9bca4e484de..d354bb231796 100644
@@ -442,3 +442,4 @@
 469	common	file_setattr			sys_file_setattr
 470	common	listns				sys_listns
 471	common	rseq_slice_yield		sys_rseq_slice_yield
+472	common	fchroot				sys_fchroot
include/uapi/asm-generic/unistd.h +5 -1
index a627acc8fb5f..5b7e77a7c736 100644
@@ -863,8 +863,12 @@ __SYSCALL(__NR_listns, sys_listns)
 #define __NR_rseq_slice_yield 471
 __SYSCALL(__NR_rseq_slice_yield, sys_rseq_slice_yield)
 
+/* fs/open.c */
+#define __NR_fchroot 472
+__SYSCALL(__NR_fchroot, sys_fchroot)
+
 #undef __NR_syscalls
-#define __NR_syscalls 472
+#define __NR_syscalls 473
 
 /*
  * 32 bit systems traditionally used different
scripts/syscall.tbl +1 -0
index 7a42b32b6577..0ab531605120 100644
@@ -412,3 +412,4 @@
 469	common	file_setattr			sys_file_setattr
 470	common	listns				sys_listns
 471	common	rseq_slice_yield		sys_rseq_slice_yield
+472	common	fchroot				sys_fchroot
Signature · 1 line
2.53.0
author maintainer
#19 lore raw

RFC PATCH 6/7 selftests/filesystems: add failfs selftests

In reply to Christian Brauner

Test the failfs semantics and both new entry points:

- fchdir(FD_FAILFS_ROOT):

* working directory lookups and getcwd() fail
* other sentinels are rejected
* the state is recoverable while the root is untouched

- fchroot() with regular fds:
* chroot parity
* CAP_SYS_CHROOT required
* ENOTDIR/EBADF/EINVAL checks

- fchroot(FD_FAILFS_ROOT):

* absolute lookups, stat, statfs and opens of the root including O_PATH fail with EOPNOTSUPP
* dirfd-anchored I/O keeps working
* ".." walks clamp at the top of the mount tree
* /proc magic links resolve but can't be stat through
* absolute symlinks fail while relative symlinks keep resolving

- Unprivileged entry requires no_new_privs and is rejected for chrooted callers

- entering makes the task count as chrooted so user namespace creation fails

- Nothing can be mounted on top of failfs and OPEN_TREE_CLONE is rejected

- setns() to a kept mount namespace fd restores root and working directory

- The failfs root is inherited across fork() and absolute exec fails

- Exec by fd of a dynamically linked binary fails on opening its absolute PT_INTERP interpreter

---
 tools/testing/selftests/Makefile                   |   1 +
 .../selftests/filesystems/failfs/.gitignore        |   2 +
 .../testing/selftests/filesystems/failfs/Makefile  |   5 +
 .../selftests/filesystems/failfs/failfs_test.c     | 506 +++++++++++++++++++++
 4 files changed, 514 insertions(+)
tools/testing/selftests/Makefile +1 -0
index 8d4db2241cc2..f87167bcf582 100644
@@ -33,6 +33,7 @@ TARGETS += fchmodat2
 TARGETS += filesystems
 TARGETS += filesystems/binderfs
 TARGETS += filesystems/epoll
+TARGETS += filesystems/failfs
 TARGETS += filesystems/fat
 TARGETS += filesystems/overlayfs
 TARGETS += filesystems/statmount
tools/testing/selftests/filesystems/failfs/.gitignore new file +2 -0
new file mode 100644
index 000000000000..cd3b5d884d7e
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0-only
+failfs_test
tools/testing/selftests/filesystems/failfs/Makefile new file +5 -0
new file mode 100644
index 000000000000..3c5d98b4fe72
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: GPL-2.0
+CFLAGS += -Wall -O2 -g $(KHDR_INCLUDES)
+TEST_GEN_PROGS := failfs_test
+
+include ../../lib.mk
tools/testing/selftests/filesystems/failfs/failfs_test.c new file +506 -0

506 lines not on this page · raw message

Signature · 1 line
2.53.0
author maintainer
#20 lore raw

RFC PATCH 7/7 Documentation: add failfs documentation

In reply to Christian Brauner

Document the failfs semantics, the FD_FAILFS_ROOT sentinel, the fchroot() entry requirements, and the ways back out.

---
 Documentation/filesystems/failfs.rst | 64 ++++++++++++++++++++++++++++++++++++
 Documentation/filesystems/index.rst  |  1 +
 2 files changed, 65 insertions(+)
Documentation/filesystems/failfs.rst new file +64 -0
new file mode 100644
index 000000000000..46d91525916b
@@ -0,0 +1,64 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+======
+failfs
+======
+
+failfs is a kernel-internal filesystem that fails every operation
+reaching it with ``EOPNOTSUPP``. It is the counterpart to nullfs. Where
+nullfs is a permanently empty failfs means "nothing is supported here". It
+cannot be mounted from userspace, nothing can be mounted on top of it. It
+cannot be cloned.
+
+The only way into it is the ``FD_FAILFS_ROOT`` file descriptor sentinel which
+is understood by ``fchdir(2)`` and ``fchroot(2)``.
+
+Semantics
+=========
+
+Every path walk of a component through failfs fails with
+``EOPNOTSUPP`` before that component is parsed, including ``.``.
+
+The root itself cannot be opened at all not even with ``O_PATH``.
+
+A process with its working directory in failfs fails every
+``AT_FDCWD``-relative lookup. As with any working directory that is
+unreachable from the process root, the ``getcwd(2)`` system call returns
+a path prefixed with ``(unreachable)``.
+
+A process with its root directory in failfs fails every absolute path
+lookup including absolute symlinks and the interpreter of dynamically
+linked binaries. In other words, this fails exec.
+
+Lookups anchored at explicit directory file descriptors keep working. It
+is the ``fs_struct`` equivalent of ``RESOLVE_BENEATH``. The process must
+anchor every lookup at a file descriptor it explicitly holds.
+
+Entering
+========
+
+``fchroot(FD_FAILFS_ROOT, 0)`` requires ``CAP_SYS_CHROOT`` in the
+caller's user namespace, mirroring ``chroot(2)``. Unprivileged callers
+may enter if both of the following hold:
+
+* ``no_new_privs`` is set: setuid binaries on regular mounts remain
+  reachable via inherited directory file descriptors and executing them
+  with an unusable root directory is the classic confused deputy.
+
+* The caller is not already chrooted: the root directory is what
+  confines ``..`` resolution and the failfs root can never be reached by
+  walking up a real mount tree, so moving the root of a chrooted task to
+  failfs would allow it to escape its chroot via ``openat(fd, "..")``.
+
+Leaving
+=======
+
+A process that entered failfs counts as chrooted. It cannot create user
+namespaces to regain ``CAP_SYS_CHROOT``, and ``chroot(2)`` or
+``fchroot(2)`` back out require ``CAP_SYS_CHROOT``. The only other exit
+is ``setns(2)`` with a mount namespace file descriptor, which requires
+``CAP_SYS_ADMIN`` over the target mount namespace as well as
+``CAP_SYS_CHROOT`` and ``CAP_SYS_ADMIN`` in the caller's user namespace
+and resets both root and working directory. A process that holds no such
+file descriptor and restricts ``*chdir()``/``*chroot()``/``setns()`` via
+seccomp has thrown away the key.
Documentation/filesystems/index.rst +1 -0
index 1f71cf159547..734a45e51667 100644
@@ -91,6 +91,7 @@ Documentation for filesystem implementations.
    ext3
    ext4/index
    f2fs
+   failfs
    gfs2/index
    hfs
    hfsplus
Signature · 1 line
2.53.0
#21 lore raw

Re: [PATCH RFC 7/7] Documentation: add failfs documentation

Christian Brauner @ 2026-07-23 13:30 +02:

Quoted 16 lines

Document the failfs semantics, the FD_FAILFS_ROOT sentinel, the fchroot() entry requirements, and the ways back out.

---
 Documentation/filesystems/failfs.rst | 64 ++++++++++++++++++++++++++++++++++++
 Documentation/filesystems/index.rst  |  1 +
 2 files changed, 65 insertions(+)
Documentation/filesystems/failfs.rst new file +1 -0
new file mode 100644
index 000000000000..46d91525916b
@@ -0,0 +1,64 @@
+.. SPDX-License-Identifier: GPL-2.0

Documentation/filesystems/failfs.rst line 11

Quoted 10 lines
Documentation/filesystems/failfs.rst new file +10 -0
+
+======
+failfs
+======
+
+failfs is a kernel-internal filesystem that fails every operation
+reaching it with ``EOPNOTSUPP``. It is the counterpart to nullfs. Where
+nullfs is a permanently empty failfs means "nothing is supported here". It
+cannot be mounted from userspace, nothing can be mounted on top of it. It
+cannot be cloned.

A small change here makes the wording more clear: from 'Where nullfs is a permanently empty failfs means "nothing is supported here"' to 'Where nullfs is permanently empty, failfs means "nothing is supported here"'.

Quoted 65 lines
Documentation/filesystems/failfs.rst new file +53 -0
+
+The only way into it is the ``FD_FAILFS_ROOT`` file descriptor sentinel which
+is understood by ``fchdir(2)`` and ``fchroot(2)``.
+
+Semantics
+=========
+
+Every path walk of a component through failfs fails with
+``EOPNOTSUPP`` before that component is parsed, including ``.``.
+
+The root itself cannot be opened at all not even with ``O_PATH``.
+
+A process with its working directory in failfs fails every
+``AT_FDCWD``-relative lookup. As with any working directory that is
+unreachable from the process root, the ``getcwd(2)`` system call returns
+a path prefixed with ``(unreachable)``.
+
+A process with its root directory in failfs fails every absolute path
+lookup including absolute symlinks and the interpreter of dynamically
+linked binaries. In other words, this fails exec.
+
+Lookups anchored at explicit directory file descriptors keep working. It
+is the ``fs_struct`` equivalent of ``RESOLVE_BENEATH``. The process must
+anchor every lookup at a file descriptor it explicitly holds.
+
+Entering
+========
+
+``fchroot(FD_FAILFS_ROOT, 0)`` requires ``CAP_SYS_CHROOT`` in the
+caller's user namespace, mirroring ``chroot(2)``. Unprivileged callers
+may enter if both of the following hold:
+
+* ``no_new_privs`` is set: setuid binaries on regular mounts remain
+  reachable via inherited directory file descriptors and executing them
+  with an unusable root directory is the classic confused deputy.
+
+* The caller is not already chrooted: the root directory is what
+  confines ``..`` resolution and the failfs root can never be reached by
+  walking up a real mount tree, so moving the root of a chrooted task to
+  failfs would allow it to escape its chroot via ``openat(fd, "..")``.
+
+Leaving
+=======
+
+A process that entered failfs counts as chrooted. It cannot create user
+namespaces to regain ``CAP_SYS_CHROOT``, and ``chroot(2)`` or
+``fchroot(2)`` back out require ``CAP_SYS_CHROOT``. The only other exit
+is ``setns(2)`` with a mount namespace file descriptor, which requires
+``CAP_SYS_ADMIN`` over the target mount namespace as well as
+``CAP_SYS_CHROOT`` and ``CAP_SYS_ADMIN`` in the caller's user namespace
+and resets both root and working directory. A process that holds no such
+file descriptor and restricts ``*chdir()``/``*chroot()``/``setns()`` via
+seccomp has thrown away the key.
Documentation/filesystems/index.rst +1 -0
index 1f71cf159547..734a45e51667 100644
@@ -91,6 +91,7 @@ Documentation for filesystem implementations.
    ext3
    ext4/index
    f2fs
+   failfs
    gfs2/index
    hfs
    hfsplus
maintainer
#22 lore raw
In reply to Christian Brauner
Quoted 1 line · Christian Brauner

[snipped excellent list of things one cannot do with failfs]

Quoted 2 lines

A plain readlink() of
such a link still works and shows "/".

This seems awkward to me. I feel like ls -ld /proc/PID/{cwd,exe} should probably show something distinctive instead of "/". Maybe readlink() should show "[failfs] /" or similar? Both admins or developers trying to see what's going on and CRIU might want this. IMO it's too bad that we don't have a nicely parseable readlink output for oddities like links to deleted files, but oh well.

--Andy