Skip to contentSkip to conversation

binfmt_misc: don't let an 'F' entry pin its own instance

PATCH In review Christian Brauner 2 participants 5 messages Jul 28

Subsystems (6)
lore
Share & tools
lore

Christian Brauner

author maintainer
#0 lore raw

cover binfmt_misc: don't let an 'F' entry pin its own instance

An entry registered with 'F' opens its interpreter at registration time and holds that file until the entry is freed. Any entry nobody removes by hand only gets closed once the binfmt_misc superblock is shut down. If the interpreter lives on a mount that keeps that superblock alive the two pin each other:

    binfmt_misc sb -> inode -> entry -> interp_file -> vfsmount -> binfmt_misc sb

TL;DR the file is never closed. Once the mount namespace is gone there is nothing left to unregister through either.

There are two ways to trigger this bug:

- Point the interpreter at the instance itself. Its files are regular files owned by the mounter and both bm_get_inode() and simple_fill_super() leave i_op at empty_iops. So notify_change() falls back to simple_setattr() and chmod +x works. We never set SB_I_NOEXEC and so open_exec() accepts it.

- Use the instance as an overlayfs lower layer. The overlay superblock holds a clone_private_mount() of every layer until it is destroyed and that clone is in no namespace. So umount_tree() never reaches it.

That's a DoS. And it isn't only the superblock that leaks. It pins the user namespace it was mounted in, so every iteration permanently eats one of the caller's user namespace charges.

So let's just do the sane thing. SB_I_NOEXEC makes open_exec() fail on the instance's own files and s_stack_depth makes overlayfs reject the layer before it ever takes a clone. That also covers the ecryptfs and fuse passthrough variants. What 'F' promises is unchanged.

The stable tag is narrower than the Fixes tags on purpose. Before sandboxed mounts this needed global root against the single instance everyone shares, and the change doesn't apply to those trees anyway.

---
Christian Brauner (2):
binfmt_misc: don't let an 'F' entry pin its own instance selftests/exec: check that a binfmt_misc instance cannot be pinned

 fs/binfmt_misc.c                                   |   4 +
 tools/testing/selftests/exec/Makefile              |  10 ++
 tools/testing/selftests/exec/binfmt_misc_selfpin.c | 158 +++++++++++++++++++++
 tools/testing/selftests/exec/config                |   4 +
 4 files changed, 176 insertions(+)

---
base-commit: 744c5dd7c3c92dc7cac84a028934855cd9536a3c
change-id: 20260728-work-binfmt_misc-selfpin-d55b1794f0fe

author maintainer
#1 lore raw

PATCH 1/2 binfmt_misc: don't let an 'F' entry pin its own instance

An entry registered with 'F' opens its interpreter at registration time and holds that file until the entry is freed. Any entry nobody removes by hand only gets closed once the binfmt_misc superblock is shut down. If the interpreter lives on a mount that keeps that superblock alive the two pin each other:

    binfmt_misc sb -> inode -> entry -> interp_file -> vfsmount -> binfmt_misc sb

TL;DR the file is never closed. Once the mount namespace is gone there is nothing left to unregister through either.

There are two ways to trigger this bug:

- Point the interpreter at the instance itself. Its files are regular files owned by the mounter and both bm_get_inode() and simple_fill_super() leave i_op at empty_iops. So notify_change() falls back to simple_setattr() and chmod +x works. We never set SB_I_NOEXEC and so open_exec() accepts it.

- Use the instance as an overlayfs lower layer. The overlay superblock holds a clone_private_mount() of every layer until it is destroyed and that clone is in no namespace. So umount_tree() never reaches it.

That's a DoS. And it isn't only the superblock that leaks. It pins the user namespace it was mounted in, so every iteration permanently eats one of the caller's user namespace charges.

So let's just do the sane thing. SB_I_NOEXEC makes open_exec() fail on the instance's own files and s_stack_depth makes overlayfs reject the layer before it ever takes a clone. That also covers the ecryptfs and fuse passthrough variants. What 'F' promises is unchanged.

The stable tag is narrower than the Fixes tags on purpose. Before sandboxed mounts this needed global root against the single instance everyone shares, and the change doesn't apply to those trees anyway.

Note that SB_I_NODEV is implicitly raised for userns mounts but raise it explicitly here as well.

Fixes 948b701a607f ("binfmt_misc: add persistent opened binary handler for containers")
Fixes 21ca59b365c0 ("binfmt_misc: enable sandboxed mounts")
---
 fs/binfmt_misc.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/binfmt_misc.c +4 -0
index 5de615ca7a75..47aeb2b68d3e 100644
@@ -937,6 +937,10 @@ static int bm_fill_super(struct super_block *sb, struct fs_context *fc)
 	if (WARN_ON(user_ns != current_user_ns()))
 		return -EINVAL;
 
+	/* Never exec off this instance and never let anything stack on it. */
+	sb->s_iflags |= SB_I_NOEXEC | SB_I_NODEV;
+	sb->s_stack_depth = FILESYSTEM_MAX_STACK_DEPTH;
+
 	/*
 	 * Lazily allocate a new binfmt_misc instance for this namespace, i.e.
 	 * do it here during the first mount of binfmt_misc. We don't need to
Signature · 1 line
2.53.0
#2 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] The user_ns reference obtained in bm_get_tree and transferred to sb->s_fs_info by sget_fc is permanently leaked if bm_fill_super fails.
- [High] Deleting a binfmt_misc entry registered with the F (MISC_FMT_OPEN_FILE) flag leaks the negative i_writecount of the interpreter file, permanently preventing it from being opened for writing until reboot. --

--- Patch [1]: [PATCH 1/2] binfmt_misc: don't let an 'F' entry pin its own instance --- commit 69b9018135a8cbdf7775e3461daaa6ac7c2e2eb2
Author: Christian Brauner <[email protected]>

binfmt_misc: don't let an 'F' entry pin its own instance

An entry registered with 'F' opens its interpreter at registration time and holds that file until the entry is freed. Any entry nobody removes by hand only gets closed once the binfmt_misc superblock is shut down. If the interpreter lives on a mount that keeps that superblock alive the two pin each other. This patch ensures SB_I_NOEXEC and s_stack_depth are set to prevent this circular dependency and pinning.

Quoted 5 lines
fs/binfmt_misc.c +0 -0
index 84349fcb93f13..659a9f215db2c 100644

fs/binfmt_misc.c static int bm_fill_super(struct super_block *sb, struct fs_context *fc) · line 946

Quoted 9 lines
fs/binfmt_misc.c +4 -0
@@ -938,6 +938,10 @@ static int bm_fill_super(struct super_block *sb, struct fs_context *fc)
 	if (WARN_ON(user_ns != current_user_ns()))
 		return -EINVAL;
 
+	/* Never exec off this instance and never let anything stack on it. */
+	sb->s_iflags |= SB_I_NOEXEC | SB_I_NODEV;
+	sb->s_stack_depth = FILESYSTEM_MAX_STACK_DEPTH;
+
 	/*
 	 * Lazily allocate a new binfmt_misc instance for this namespace, i.e.

[ ... ]

Looking at the rest of bm_fill_super() in fs/binfmt_misc.c:

	err = simple_fill_super(sb, BINFMTFS_MAGIC, bm_files);
	if (!err)
		sb->s_op = &s_ops;
	return err;
}

[Severity: High]
This is a pre-existing issue, but does this error path permanently leak the user_namespace reference?

If simple_fill_super() fails, or if a memory allocation failure occurs earlier in bm_fill_super(), sb->s_root remains NULL. When vfs_get_super() handles the error, generic_shutdown_super() will skip calling s_op->put_super(sb).

Since fc->s_fs_info was already cleared during sget_fc(), does this mean the user_namespace reference in sb->s_fs_info is never released?

Could an unprivileged local user trigger this by intentionally failing userns mounts (e.g., via memory exhaustion) to cause resource exhaustion?

On a related note regarding pre-existing issues, looking at put_binfmt_handler() in fs/binfmt_misc.c:

static void put_binfmt_handler(struct binfmt_misc *e)

{
        ...
        if (e->flags & MISC_FMT_OPEN_FILE)
                filp_close(e->interp_file, NULL);
        kfree(e);
}

[Severity: High]
This is a pre-existing issue, but does this teardown path leak the negative i_writecount of the interpreter file?

When an entry is registered with the 'F' flag, bm_register_write() uses open_exec(), which internally decrements the inode's i_writecount to prevent concurrent writes. While the error path during registration calls exe_file_allow_write_access(), put_binfmt_handler() only calls filp_close().

Since filp_close() does not automatically restore executable write access, would deleting the binfmt_misc entry permanently leave the binary unwritable (returning -ETXTBSY) until reboot?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author maintainer
#3 lore raw
Quoted 9 lines

[ ... ]

Looking at the rest of bm_fill_super() in fs/binfmt_misc.c:

	err = simple_fill_super(sb, BINFMTFS_MAGIC, bm_files);
	if (!err)
		sb->s_op = &s_ops;
	return err;
}

This _seems_ legit but I need to double-check.

Quoted 31 lines

[Severity: High]
This is a pre-existing issue, but does this error path permanently leak the user_namespace reference?

If simple_fill_super() fails, or if a memory allocation failure occurs earlier in bm_fill_super(), sb->s_root remains NULL. When vfs_get_super() handles the error, generic_shutdown_super() will skip calling s_op->put_super(sb).

Since fc->s_fs_info was already cleared during sget_fc(), does this mean the user_namespace reference in sb->s_fs_info is never released?

Could an unprivileged local user trigger this by intentionally failing userns mounts (e.g., via memory exhaustion) to cause resource exhaustion?

On a related note regarding pre-existing issues, looking at put_binfmt_handler() in fs/binfmt_misc.c:

static void put_binfmt_handler(struct binfmt_misc *e)

{
        ...
        if (e->flags & MISC_FMT_OPEN_FILE)
                filp_close(e->interp_file, NULL);
        kfree(e);
}

[Severity: High]
This is a pre-existing issue, but does this teardown path leak the negative i_writecount of the interpreter file?

Quoted 8 lines

When an entry is registered with the 'F' flag, bm_register_write() uses open_exec(), which internally decrements the inode's i_writecount to prevent concurrent writes. While the error path during registration calls exe_file_allow_write_access(), put_binfmt_handler() only calls filp_close().

Since filp_close() does not automatically restore executable write access, would deleting the binfmt_misc entry permanently leave the binary unwritable (returning -ETXTBSY) until reboot?

This is fixed in another patch series of mine.

author maintainer
#4 lore raw

PATCH 2/2 selftests/exec: check that a binfmt_misc instance cannot be pinned

In reply to Christian Brauner

An 'F' entry whose interpreter keeps the binfmt_misc superblock alive pins the instance that owns it forever. Cover both ways to build that:

- an interpreter on the instance's own files, control file and entry file alike

- and an instance used as an overlayfs lower layer.

Check that an ordinary 'F' registration still succeeds so the fix stays honest about not changing what 'F' promises.

---
 tools/testing/selftests/exec/Makefile              |  10 ++
 tools/testing/selftests/exec/binfmt_misc_selfpin.c | 158 +++++++++++++++++++++
 tools/testing/selftests/exec/config                |   4 +
 3 files changed, 172 insertions(+)
tools/testing/selftests/exec/Makefile +10 -0
index 45a3cfc435cf..b15505f5e0bc 100644
@@ -21,6 +21,10 @@ TEST_GEN_PROGS += recursion-depth
 TEST_GEN_PROGS += null-argv
 TEST_GEN_PROGS += check-exec
 
+# binfmt_misc must not be reachable as an exec source or as a stacking layer,
+# or an 'F' entry can pin the instance that owns it. Unprivileged, no bpf.
+TEST_GEN_PROGS += binfmt_misc_selfpin
+
 EXTRA_CLEAN := $(OUTPUT)/subdir.moved $(OUTPUT)/execveat.moved $(OUTPUT)/xxxxx*	\
 	       $(OUTPUT)/S_I*.test
 
@@ -55,3 +59,9 @@ $(OUTPUT)/script-exec.inc: $(CHECK_EXEC_SAMPLES)/script-exec.inc
 	cp $< $@
 $(OUTPUT)/script-noexec.inc: $(CHECK_EXEC_SAMPLES)/script-noexec.inc
 	cp $< $@
+
+# Reuses setup_userns()/write_file() from the filesystems selftests. Their
+# wrappers.h wants the uapi headers, so ask for them here rather than widening
+# CFLAGS for every program in this directory.
+$(OUTPUT)/binfmt_misc_selfpin: CFLAGS += $(TOOLS_INCLUDES)
+$(OUTPUT)/binfmt_misc_selfpin: ../filesystems/utils.c
tools/testing/selftests/exec/binfmt_misc_selfpin.c new file +158 -0

158 lines not on this page · raw message

tools/testing/selftests/exec/config +4 -0
index c308079867b3..c03d52d8abee 100644
@@ -1,2 +1,6 @@
 CONFIG_BLK_DEV=y
 CONFIG_BLK_DEV_LOOP=y
+CONFIG_BINFMT_MISC=y
+CONFIG_OVERLAY_FS=y
+CONFIG_TMPFS=y
+CONFIG_USER_NS=y
Signature · 1 line
2.53.0