Linux Kernel Selftest development
 help / color / mirror / Atom feed
* [PATCH RFC v3 00/44] fs: don't use pseudo filesystem files as storage
@ 2026-11-16 11:04 Christian Brauner
  2026-11-16 11:04 ` [PATCH RFC v3 01/44] loop: refuse backing files that aren't open for reading Christian Brauner
                   ` (43 more replies)
  0 siblings, 44 replies; 48+ messages in thread
From: Christian Brauner @ 2026-11-16 11:04 UTC (permalink / raw)
  To: linux-fsdevel
  Cc: Jann Horn, Chris Mason, Josef Bacik, Christoph Hellwig,
	linux-block, linux-kernel, linux-nvme, Alexander Viro, Jan Kara,
	linux-scsi, target-devel, driver-core, linux-mm, bpf,
	linux-security-module, selinux, apparmor, linux-nfs, netdev,
	fuse-devel, linux-efi, xen-devel, ocfs2-devel, linux-s390,
	linuxppc-dev, linux-usb, Amir Goldstein, linux-erofs,
	linux-kselftest, Christian Brauner (Amutable), stable,
	Alexander Mikhalitsyn, Chris Down, Breno Leitao, Casey Schaufler,
	Chuck Lever, Sashiko, Gao Xiang

Loop happily takes any regular file with ->read_iter() as a backing
file and any file that also has ->write_iter() as a writable one. A few
examples:

* /proc/kcore attaches read-only as a loop device the size of the
  kernel's virtual address space (64 PiB with 5-level paging)
* sysfs attributes attach read-write as 4096 byte loop devices
* using /sys/kernel/mm/transparent_hugepage/enabled for the loop device
  and writing a block to the loop device changes the THP setting

It's not just silly. It's abusable (is that a word?). Loop calls
->read_iter() and ->write_iter() of the backing file directly from its
kworker which runs with kernel credentials.

But pseudo files enforce part of their access rules at read and write
against the caller. So dozens of sysfs ->show() and ->store() methods
call capable().

But setting up a loop device requires no capabilities. It only requires
access to the device nodes. So whoever has a loop device skips all of
the capable() checks.

A couple of fun examples:

On v7.4-rc1 uid 0 without any capabilities can't kill a process of uid
1000 and can't poison its memory either:

    $ grep CapEff /proc/self/status
    CapEff: 0000000000000000
    $ kill -9 327
    bash: kill: (327) - Operation not permitted
    $ echo 0x220689000 > /sys/devices/system/memory/hard_offline_page
    bash: echo: write error: Operation not permitted

But put hard_offline_page behind a loop device, write a block
containing 0x220689000 to the loop device and the loop worker happily
does it:

    Memory failure: 0x220689: recovery action for dirty LRU page: Recovered
    MCE: Killing victim:327 due to hardware memory corruption fault at 7f468e972000

That hard_offline_page is a write-only file doesn't stop loop from
using it as a writable backing file either.

Other fun examples:

(1) soft-block radios via /sys/class/rfkill/rfkill*/soft
    (CAP_NET_ADMIN)

(2) change the reboot mode via /sys/kernel/reboot/mode (CAP_SYS_BOOT)

(3) reconfigure md arrays via /sys/block/md*/md/. md_attr_store()
    requires CAP_SYS_ADMIN for every md attribute.

(4) reconfigure scsi disks via /sys/class/scsi_disk/*/allow_restart and
    friends (CAP_SYS_ADMIN)

(5) read dmi-sysfs attributes (CAP_SYS_ADMIN)

And an unprivileged container that was handed a loop device can change
gro_flush_timeout of its own network devices. That usually requires
CAP_NET_ADMIN in the initial user namespace (via sysfs and via netlink).

I've reproduced that all on v7.4-rc1.

Aaaand zloop, the nvme target's file backend, the scsi target's fileio
backend, fuse passthrough and the usb mass storage gadget have the same
problem. erofs takes pseudo files as images for file-backed mounts as
well. It reads them with the mounter's credentials though so that's just
silly and not abusable. Same for acct(2) which happily writes its
records to cgroup2 or binfmt_misc files.

Fwiw, device mapper doesn't need anything. It only takes block devices.
md's deprecated bitmap file is fine as well. It maps the file with
bmap() which no pseudo filesystem implements.

So this series adds SB_I_PSEUDO to state that the regular files of a
filesystem are kernel interfaces and not storage and makes all eight
refuse such files.

I didn't want to sprinkle SB_I_PSEUDO over all pseudo filesystems one by
one. Instead there's now a common initialization point for all of them
via fc_init_pseudo().

init_pseudo() uses it for the internal pseudo filesystems and all
mountable pseudo filesystems call it from their ->init_fs_context().

kernfs does it once in kernfs_get_tree(). It raises SB_I_PSEUDO,
SB_I_NOEXEC and SB_I_NODEV. Pseudo filesystems never hold executables
and generally never hold device nodes. Some of them raised SB_I_NOEXEC
and SB_I_NODEV already, most didn't.

I went through every single one of them. None of them needs to execute
anything. The files that can be mmap()ed map data and the tracing ring
buffer refuses VM_EXEC anyway.

SB_I_NODEV is cleared again by devpts, binderfs, selinuxfs and
securityfs. The first two did that already. selinuxfs needs it because
Android init opens /sys/fs/selinux/null by path as /dev/null doesn't
exist yet at that point. securityfs needs it for AppArmor's .null device
node. On exec AppArmor puts it in place of file descriptors the new
profile isn't allowed to use and those can be reopened via
/proc/<pid>/fd/<nr>. Note that systemd mounts most of these filesystems
with noexec and nodev already.

file_check_backing() looks at d_real_data_inode(). overlayfs happily
stacks on top of sysfs, cgroup2, debugfs, tracefs, configfs and
securityfs and reads of such overlayfs files are served by the pseudo
file. Only procfs and binfmt_misc are refused as they set s_stack_depth
to FILESYSTEM_MAX_STACK_DEPTH and efivarfs as it has its own ->d_hash()
and ->d_compare().

Whether all pseudo filesystems should refuse stacking is a separate
discussion. overlayfs reads the lower file with the mounter's
credentials so a root-mounted overlayfs on top of a sysfs directory
hands capability gated reads to every reader. With SB_I_PSEUDO overlayfs
could easily refuse them.

The first three patches are independent fixes. Loop never checked
whether its backing file is open for reading. It relied on
vfs_iter_read() refusing such files. Direct I/O never went through
vfs_iter_read() and since v6.15 buffered I/O doesn't either. So whoever
has a loop device can read files they could only open for writing. And
the nvme target's file backend and the scsi target's fileio backend call
->read_iter() and ->write_iter() without checking that they exist.

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
Changes in v3:
- Rebase onto vfs-7.5.lookup which has "fs: let d_real() fail" v2:
  https://patch.msgid.link/20261116-work-ovl-d_real-v2-0-90564c4ae347@kernel.org
- file_check_backing(): use d_real_data_inode() and fail with its error.
  For an O_PATH file on overlayfs the data might not have been looked up
  yet and when that lookup failed ovl_d_real() warned and the check
  passed. (Chris, sashiko, bpf-ci)
- ovl: refuse data-only layers on pseudo filesystems. (Amir)
- fuse: also check the backing file that fuse_passthrough_open() opens
  for io. (Amir)
- securityfs: clear SB_I_NODEV again. AppArmor's .null device node can
  be opened by path and via /proc/<pid>/fd/<nr>. (Chris)
- selftests: add fuse passthrough tests and tests for overlayfs
  data-only layers on pseudo filesystems.
- Collect tags.
- Link to v2: https://patch.msgid.link/20261114-work-loop-pseudo-v2-0-98639a2286fb@kernel.org

Changes in v2:
- Add file_check_backing() and use it in all consumers. It replaces
  file_is_pseudo() and most of the open-coded file type, open mode and
  method checks.
- nvmet, fileio and zloop refuse everything that isn't a regular file
  or, for nvmet and fileio, a block device.
- fileio: check the protection file as well. (sashiko)
- acct: refuse files on all pseudo filesystems. (Alexander)
- selftests: add nvmet (Bui), fileio, zloop, link, FIFO and character
  device tests.
- Collect tags. They are dropped on the patches that changed.
- Link to v1: https://patch.msgid.link/20261110-work-loop-pseudo-v1-0-4c998976c6c7@kernel.org

---
Christian Brauner (44):
      loop: refuse backing files that aren't open for reading
      nvmet: check that the file backend supports ->{read,write}_iter
      target: fileio: check that the backing file supports ->{read,write}_iter
      fs: add SB_I_PSEUDO
      kernfs: use fc_init_pseudo()
      proc: use fc_init_pseudo()
      binfmt_misc: use fc_init_pseudo()
      mqueue: use fc_init_pseudo()
      bpf: use fc_init_pseudo() for bpffs
      nullfs: use fc_init_pseudo()
      failfs: use fc_init_pseudo()
      rust_binderfs: use fc_init_pseudo()
      devpts: use fc_init_pseudo()
      debugfs: use fc_init_pseudo()
      tracefs: use fc_init_pseudo()
      configfs: use fc_init_pseudo()
      securityfs: use fc_init_pseudo()
      selinux: use fc_init_pseudo() for selinuxfs
      smack: use fc_init_pseudo() for smackfs
      apparmor: use fc_init_pseudo() for apparmorfs
      nfsd: use fc_init_pseudo()
      sunrpc: use fc_init_pseudo() for rpc_pipefs
      fuse: use fc_init_pseudo() for fusectl
      efivarfs: use fc_init_pseudo()
      pstore: use fc_init_pseudo()
      xenfs: use fc_init_pseudo()
      openpromfs: use fc_init_pseudo()
      ocfs2: use fc_init_pseudo() for dlmfs
      s390/hypfs: use fc_init_pseudo()
      powerpc/spufs: use fc_init_pseudo()
      usb: gadget: f_fs: use fc_init_pseudo()
      usb: gadget: gadgetfs: use fc_init_pseudo()
      ovl: refuse data-only layers on pseudo filesystems
      selftests/ovl: test data-only layers on pseudo filesystems
      fs: add file_check_backing()
      loop: refuse files on pseudo filesystems as backing files
      zloop: refuse zone files on pseudo filesystems
      nvmet: refuse files on pseudo filesystems as namespace backing files
      target: fileio: refuse files on pseudo filesystems as backing files
      fuse: refuse files on pseudo filesystems as passthrough backing files
      usb: gadget: f_mass_storage: refuse files on pseudo filesystems
      erofs: refuse files on pseudo filesystems as backing files
      acct: refuse files on pseudo filesystems
      selftests/filesystems: add backing file tests for pseudo files

 arch/powerpc/platforms/cell/spufs/inode.c          |   2 +
 arch/s390/hypfs/inode.c                            |   2 +
 drivers/android/binder/rust_binderfs.c             |   3 +-
 drivers/block/loop.c                               |  14 +-
 drivers/block/zloop.c                              |   5 +-
 drivers/nvme/target/io-cmd-file.c                  |   4 +
 drivers/target/target_core_file.c                  |  14 +
 drivers/usb/gadget/function/f_fs.c                 |   2 +
 drivers/usb/gadget/function/storage_common.c       |  11 +-
 drivers/usb/gadget/legacy/inode.c                  |   2 +
 drivers/xen/xenfs/super.c                          |   2 +
 fs/binfmt_misc.c                                   |   5 +-
 fs/configfs/mount.c                                |   2 +
 fs/debugfs/inode.c                                 |   2 +
 fs/devpts/inode.c                                  |   2 +
 fs/efivarfs/super.c                                |   2 +
 fs/erofs/super.c                                   |   5 +-
 fs/failfs.c                                        |   3 +-
 fs/fuse/backing.c                                  |   4 +
 fs/fuse/control.c                                  |   2 +
 fs/fuse/passthrough.c                              |   7 +
 fs/kernfs/mount.c                                  |   4 +-
 fs/libfs.c                                         |   2 +-
 fs/nfsd/nfsctl.c                                   |   2 +
 fs/nullfs.c                                        |   3 +-
 fs/ocfs2/dlmfs/dlmfs.c                             |   2 +
 fs/openpromfs/inode.c                              |   2 +
 fs/overlayfs/super.c                               |   8 +
 fs/proc/root.c                                     |   4 +-
 fs/pstore/inode.c                                  |   2 +
 fs/read_write.c                                    |  53 ++
 fs/tracefs/inode.c                                 |   2 +
 include/linux/fs.h                                 |   2 +
 include/linux/fs/super_types.h                     |   1 +
 include/linux/pseudo_fs.h                          |  12 +
 ipc/mqueue.c                                       |   3 +-
 kernel/acct.c                                      |   6 +-
 kernel/bpf/inode.c                                 |   4 +-
 net/sunrpc/rpc_pipe.c                              |   2 +
 security/apparmor/apparmorfs.c                     |   2 +
 security/inode.c                                   |   4 +
 security/selinux/selinuxfs.c                       |   5 +
 security/smack/smackfs.c                           |   2 +
 tools/testing/selftests/filesystems/.gitignore     |   1 +
 tools/testing/selftests/filesystems/Makefile       |   2 +-
 tools/testing/selftests/filesystems/config         |  16 +
 .../selftests/filesystems/overlayfs/.gitignore     |   1 +
 .../selftests/filesystems/overlayfs/Makefile       |   1 +
 .../filesystems/overlayfs/pseudo_datadir.c         | 131 +++
 .../selftests/filesystems/pseudo_backing_test.c    | 936 +++++++++++++++++++++
 50 files changed, 1271 insertions(+), 39 deletions(-)
---
base-commit: 68cb948e620cf297cee090d6effd04dfef6429de
change-id: 20261109-work-loop-pseudo-890844168ef1


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

end of thread, other threads:[~2026-11-16 12:00 UTC | newest]

Thread overview: 48+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-16 11:04 [PATCH RFC v3 00/44] fs: don't use pseudo filesystem files as storage Christian Brauner
2026-11-16 11:04 ` [PATCH RFC v3 01/44] loop: refuse backing files that aren't open for reading Christian Brauner
2026-11-16 11:04 ` [PATCH RFC v3 02/44] nvmet: check that the file backend supports ->{read,write}_iter Christian Brauner
2026-11-16 11:04 ` [PATCH RFC v3 03/44] target: fileio: check that the backing file " Christian Brauner
2026-11-16 11:04 ` [PATCH RFC v3 04/44] fs: add SB_I_PSEUDO Christian Brauner
2026-11-16 11:04 ` [PATCH RFC v3 05/44] kernfs: use fc_init_pseudo() Christian Brauner
2026-11-16 11:04 ` [PATCH RFC v3 06/44] proc: " Christian Brauner
2026-11-16 11:04 ` [PATCH RFC v3 07/44] binfmt_misc: " Christian Brauner
2026-11-16 11:04 ` [PATCH RFC v3 08/44] mqueue: " Christian Brauner
2026-11-16 11:04 ` [PATCH RFC v3 09/44] bpf: use fc_init_pseudo() for bpffs Christian Brauner
2026-11-16 11:04 ` [PATCH RFC v3 10/44] nullfs: use fc_init_pseudo() Christian Brauner
2026-11-16 11:04 ` [PATCH RFC v3 11/44] failfs: " Christian Brauner
2026-11-16 11:04 ` [PATCH RFC v3 12/44] rust_binderfs: " Christian Brauner
2026-11-16 12:00   ` Alice Ryhl
2026-11-16 11:04 ` [PATCH RFC v3 13/44] devpts: " Christian Brauner
2026-11-16 11:04 ` [PATCH RFC v3 14/44] debugfs: " Christian Brauner
2026-11-16 11:04 ` [PATCH RFC v3 15/44] tracefs: " Christian Brauner
2026-11-16 11:04 ` [PATCH RFC v3 16/44] configfs: " Christian Brauner
2026-11-16 11:04 ` [PATCH RFC v3 17/44] securityfs: " Christian Brauner
2026-11-16 11:04 ` [PATCH RFC v3 18/44] selinux: use fc_init_pseudo() for selinuxfs Christian Brauner
2026-11-16 11:04 ` [PATCH RFC v3 19/44] smack: use fc_init_pseudo() for smackfs Christian Brauner
2026-11-16 11:04 ` [PATCH RFC v3 20/44] apparmor: use fc_init_pseudo() for apparmorfs Christian Brauner
2026-11-16 11:04 ` [PATCH RFC v3 21/44] nfsd: use fc_init_pseudo() Christian Brauner
2026-11-16 11:04 ` [PATCH RFC v3 22/44] sunrpc: use fc_init_pseudo() for rpc_pipefs Christian Brauner
2026-11-16 11:04 ` [PATCH RFC v3 23/44] fuse: use fc_init_pseudo() for fusectl Christian Brauner
2026-11-16 11:04 ` [PATCH RFC v3 24/44] efivarfs: use fc_init_pseudo() Christian Brauner
2026-11-16 11:04 ` [PATCH RFC v3 25/44] pstore: " Christian Brauner
2026-11-16 11:04 ` [PATCH RFC v3 26/44] xenfs: " Christian Brauner
2026-11-16 11:05 ` [PATCH RFC v3 27/44] openpromfs: " Christian Brauner
2026-11-16 11:05 ` [PATCH RFC v3 28/44] ocfs2: use fc_init_pseudo() for dlmfs Christian Brauner
2026-11-16 11:05 ` [PATCH RFC v3 29/44] s390/hypfs: use fc_init_pseudo() Christian Brauner
2026-11-16 11:05 ` [PATCH RFC v3 30/44] powerpc/spufs: " Christian Brauner
2026-11-16 11:05 ` [PATCH RFC v3 31/44] usb: gadget: f_fs: " Christian Brauner
2026-11-16 11:05 ` [PATCH RFC v3 32/44] usb: gadget: gadgetfs: " Christian Brauner
2026-11-16 11:05 ` [PATCH RFC v3 33/44] ovl: refuse data-only layers on pseudo filesystems Christian Brauner
2026-11-16 11:05 ` [PATCH RFC v3 34/44] selftests/ovl: test " Christian Brauner
2026-11-16 11:05 ` [PATCH RFC v3 35/44] fs: add file_check_backing() Christian Brauner
2026-11-16 11:05 ` [PATCH RFC v3 36/44] loop: refuse files on pseudo filesystems as backing files Christian Brauner
2026-11-16 11:05 ` [PATCH RFC v3 37/44] zloop: refuse zone files on pseudo filesystems Christian Brauner
2026-11-16 11:05 ` [PATCH RFC v3 38/44] nvmet: refuse files on pseudo filesystems as namespace backing files Christian Brauner
2026-11-16 12:00   ` bot+bpf-ci
2026-11-16 11:05 ` [PATCH RFC v3 39/44] target: fileio: refuse files on pseudo filesystems as " Christian Brauner
2026-11-16 11:05 ` [PATCH RFC v3 40/44] fuse: refuse files on pseudo filesystems as passthrough " Christian Brauner
2026-11-16 11:05 ` [PATCH RFC v3 41/44] usb: gadget: f_mass_storage: refuse files on pseudo filesystems Christian Brauner
2026-11-16 11:05 ` [PATCH RFC v3 42/44] erofs: refuse files on pseudo filesystems as backing files Christian Brauner
2026-11-16 11:05 ` [PATCH RFC v3 43/44] acct: refuse files on pseudo filesystems Christian Brauner
2026-11-16 11:05 ` [PATCH RFC v3 44/44] selftests/filesystems: add backing file tests for pseudo files Christian Brauner
2026-11-16 12:00   ` bot+bpf-ci

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox