libfs: set SB_I_NOEXEC and SB_I_NODEV in init_pseudo()
Subsystems (8)
v3 cover libfs: set SB_I_NOEXEC and SB_I_NODEV in init_pseudo()
This began as a one-line dma-buf fix for a path_noexec() warning added by commit 1e7ab6f67824 ("anon_inode: rework assertions"). Christoph pointed out that the fix belongs higher up: a pseudo filesystem has no reason not to set SB_I_NOEXEC by default. This series does that.
* Patch 1 sets both flags in init_pseudo(), so every pseudo filesystem gets them. This is the only patch that changes a flag, and the only one with Fixes:/Cc: stable.
* Patch 2 drops the assignments that are now redundant in the callers that set them by hand.
Most callers already set one or both flags. I audited every init_pseudo() caller. Here is what patch 1 actually changes for each. The only visible effect is on dma-buf, where SB_I_NOEXEC silences the warning. SB_I_NODEV is never consulted on these SB_NOUSER mounts, and none of the callers that gain SB_I_NOEXEC are executed from.
caller had patch 1 adds --------------------------- -------- -------------- fs/anon_inodes.c both nothing new mm/secretmem.c both nothing new virt/kvm/guest_memfd.c both nothing new fs/nsfs.c both nothing new fs/pidfs.c both nothing new fs/aio.c NOEXEC NODEV drivers/dma-buf/dma-buf.c neither NOEXEC + NODEV net/socket.c neither NOEXEC + NODEV fs/pipe.c neither NOEXEC + NODEV kernel/resource.c neither NOEXEC + NODEV fs/erofs/super.c neither NOEXEC + NODEV fs/btrfs/tests/... neither NOEXEC + NODEV drivers/vfio/vfio_main.c neither NOEXEC + NODEV drivers/gpu/drm/drm_drv.c neither NOEXEC + NODEV drivers/dax/super.c neither NOEXEC + NODEV block/bdev.c neither NOEXEC + NODEV
John Hubbard (2):
libfs: set SB_I_NOEXEC and SB_I_NODEV by default in init_pseudo()
libfs: drop redundant SB_I_NOEXEC/SB_I_NODEV in init_pseudo() callers
fs/aio.c | 1 - fs/anon_inodes.c | 2 -- fs/libfs.c | 1 + fs/nsfs.c | 1 - fs/pidfs.c | 2 -- mm/secretmem.c | 2 -- virt/kvm/guest_memfd.c | 2 -- 7 files changed, 1 insertion(+), 10 deletions(-)
base-commit: ba3e43a9e601636f5edb54e259a74f96ca3b8fd8
Signature · 1 line
2.54.0
PATCH v3 1/2 libfs: set SB_I_NOEXEC and SB_I_NODEV by default in init_pseudo()
Since commit 1e7ab6f67824 ("anon_inode: rework assertions"), path_noexec() warns when an anonymous-inode file is mmap'd from a superblock that has not set SB_I_NOEXEC. dma-buf backs its files this way and never set the flag, so mmap of any exported buffer trips the warning on a CONFIG_DEBUG_VFS=y kernel:
WARNING: CPU: 11 PID: 121813 at fs/exec.c:118 path_noexec+0x47/0x50 do_mmap+0x2b5/0x680 vm_mmap_pgoff+0x129/0x210 ksys_mmap_pgoff+0x177/0x240 __x64_sys_mmap+0x33/0x70
init_pseudo() sets up internal SB_NOUSER mounts that are never path-reachable. Set both flags here so every pseudo filesystem gets them by default instead of each caller setting them.
SB_I_NODEV is inert for unreachable mounts. SB_I_NOEXEC has one visible effect: an executable mapping of a pseudo-fs fd, such as a dma-buf, now fails with -EPERM, which is the invariant the assertion enforces. No in-tree caller maps these executable.
Reproduce on CONFIG_DEBUG_VFS=y:
make -C tools/testing/selftests/dmabuf-heaps
sudo ./tools/testing/selftests/dmabuf-heaps/dmabuf-heap -t system
--- fs/libfs.c | 1 + 1 file changed, 1 insertion(+)
fs/libfs.c +1 -0
@@ -736,6 +736,7 @@ struct pseudo_fs_context *init_pseudo(struct fs_context *fc, fc->fs_private = ctx; fc->ops = &pseudo_fs_context_ops; fc->sb_flags |=SB_NOUSER ; + fc->s_iflags |=SB_I_NOEXEC |SB_I_NODEV ; fc->global = true; }return ctx;
Signature · 1 line
2.54.0
Re: [PATCH v3 1/2] libfs: set SB_I_NOEXEC and SB_I_NODEV by default in init_pseudo()
Looks good:
Re: [PATCH v3 1/2] libfs: set SB_I_NOEXEC and SB_I_NODEV by default in init_pseudo()
In reply to John HubbardQuoted 23 lines · John Hubbard
Since commit
1e7ab6f67824("anon_inode: rework assertions"),path_noexec()warns when an anonymous-inode file is mmap'd from a superblock that has not setSB_I_NOEXEC. dma-buf backs its files this way and never set the flag, so mmap of any exported buffer trips the warning on aCONFIG_DEBUG_VFS=ykernel:WARNING: CPU: 11 PID: 121813 at fs/exec.c:118 path_noexec+0x47/0x50 do_mmap+0x2b5/0x680 vm_mmap_pgoff+0x129/0x210 ksys_mmap_pgoff+0x177/0x240 __x64_sys_mmap+0x33/0x70
init_pseudo()sets up internalSB_NOUSERmounts that are never path-reachable. Set both flags here so every pseudo filesystem gets them by default instead of each caller setting them.
SB_I_NODEVis inert for unreachable mounts.SB_I_NOEXEChas one visible effect: an executable mapping of a pseudo-fs fd, such as a dma-buf, now fails with-EPERM, which is the invariant the assertion enforces. No in-tree caller maps these executable.Reproduce on
CONFIG_DEBUG_VFS=y:
Quoted 7 lines
make -C
tools/testing/selftests/dmabuf-heaps
sudo./tools/testing/selftests/dmabuf-heaps/dmabuf-heap-t systemFixes 1e7ab6f67824 ("anon_inode: rework assertions")Suggested-by Christoph Hellwig <[email protected]>Signed-off-by John Hubbard <[email protected]>
Looks good. Feel free to add:
Honza
Quoted 19 lines
--- fs/libfs.c | 1 + 1 file changed, 1 insertion(+)fs/libfs.c +1
-0@@ -736,6 +736,7 @@ struct pseudo_fs_context *init_pseudo(struct fs_context *fc, fc->fs_private = ctx; fc->ops = &pseudo_fs_context_ops; fc->sb_flags |=SB_NOUSER ; + fc->s_iflags |=SB_I_NOEXEC |SB_I_NODEV ; fc->global = true; }return ctx;Signature · 1 line
2.54.0
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
PATCH v3 2/2 libfs: drop redundant SB_I_NOEXEC/SB_I_NODEV in init_pseudo() callers
In reply to John Hubbardinit_pseudo() now sets SB_I_NOEXEC and SB_I_NODEV by default, so the per-caller assignments are redundant. Drop them.
--- fs/aio.c | 1 - fs/anon_inodes.c | 2 -- fs/nsfs.c | 1 - fs/pidfs.c | 2 -- mm/secretmem.c | 2 -- virt/kvm/guest_memfd.c | 2 -- 6 files changed, 10 deletions(-)
fs/aio.c +0 -1
@@ -318,7 +318,6 @@ static int aio_init_fs_context(struct fs_context *fc) pfc =init_pseudo (fc,AIO_RING_MAGIC );if (!pfc)return -ENOMEM ; - fc->s_iflags |=SB_I_NOEXEC ; pfc->ops = &aio_super_operations;return 0 ; }
fs/anon_inodes.c +0 -2
@@ -86,8 +86,6 @@ static int anon_inodefs_init_fs_context(struct fs_context *fc)struct pseudo_fs_context *ctx =init_pseudo (fc,ANON_INODE_FS_MAGIC );if (!ctx)return -ENOMEM ; - fc->s_iflags |=SB_I_NOEXEC ; - fc->s_iflags |=SB_I_NODEV ; ctx->dops = &anon_inodefs_dentry_operations;return 0 ; }
fs/nsfs.c +0 -1
@@ -664,7 +664,6 @@ static int nsfs_init_fs_context(struct fs_context *fc)struct pseudo_fs_context *ctx =init_pseudo (fc,NSFS_MAGIC );if (!ctx)return -ENOMEM ; - fc->s_iflags |=SB_I_NOEXEC |SB_I_NODEV ; ctx->s_d_flags |=DCACHE_DONTCACHE ; ctx->ops = &nsfs_ops; ctx->eops = &nsfs_export_operations;
fs/pidfs.c +0 -2
@@ -1115,8 +1115,6 @@ static int pidfs_init_fs_context(struct fs_context *fc)if (!ctx)return -ENOMEM ; - fc->s_iflags |=SB_I_NOEXEC ; - fc->s_iflags |=SB_I_NODEV ; ctx->s_d_flags |=DCACHE_DONTCACHE ; ctx->ops = &pidfs_sops; ctx->eops = &pidfs_export_operations;
mm/secretmem.c +0 -2
@@ -245,8 +245,6 @@ static int secretmem_init_fs_context(struct fs_context *fc)if (!ctx)return -ENOMEM ; - fc->s_iflags |=SB_I_NOEXEC ; - fc->s_iflags |=SB_I_NODEV ;return 0 ; }
virt/kvm/guest_memfd.c +0 -2
@@ -973,8 +973,6 @@ static int kvm_gmem_init_fs_context(struct fs_context *fc)if (!init_pseudo (fc,GUEST_MEMFD_MAGIC ))return -ENOMEM ; - fc->s_iflags |=SB_I_NOEXEC ; - fc->s_iflags |=SB_I_NODEV ; ctx = fc->fs_private; ctx->ops = &kvm_gmem_super_operations;
Signature · 1 line
2.54.0
Re: [PATCH v3 2/2] libfs: drop redundant SB_I_NOEXEC/SB_I_NODEV in init_pseudo() callers
Looks good:
Re: [PATCH v3 2/2] libfs: drop redundant SB_I_NOEXEC/SB_I_NODEV in init_pseudo() callers
In reply to John HubbardQuoted 4 lines · John Hubbard
init_pseudo()now setsSB_I_NOEXECandSB_I_NODEVby default, so the per-caller assignments are redundant. Drop them.Signed-off-by John Hubbard <[email protected]>
Looks good. Feel free to add:
Honza
Quoted 88 lines
--- fs/aio.c | 1 - fs/anon_inodes.c | 2 -- fs/nsfs.c | 1 - fs/pidfs.c | 2 -- mm/secretmem.c | 2 -- virt/kvm/guest_memfd.c | 2 -- 6 files changed, 10 deletions(-)fs/aio.c +0
-1@@ -318,7 +318,6 @@ static int aio_init_fs_context(struct fs_context *fc) pfc =init_pseudo (fc,AIO_RING_MAGIC );if (!pfc)return -ENOMEM ; - fc->s_iflags |=SB_I_NOEXEC ; pfc->ops = &aio_super_operations;return 0 ; }fs/anon_inodes.c +0
-2@@ -86,8 +86,6 @@ static int anon_inodefs_init_fs_context(struct fs_context *fc)struct pseudo_fs_context *ctx =init_pseudo (fc,ANON_INODE_FS_MAGIC );if (!ctx)return -ENOMEM ; - fc->s_iflags |=SB_I_NOEXEC ; - fc->s_iflags |=SB_I_NODEV ; ctx->dops = &anon_inodefs_dentry_operations;return 0 ; }fs/nsfs.c +0
-1@@ -664,7 +664,6 @@ static int nsfs_init_fs_context(struct fs_context *fc)struct pseudo_fs_context *ctx =init_pseudo (fc,NSFS_MAGIC );if (!ctx)return -ENOMEM ; - fc->s_iflags |=SB_I_NOEXEC |SB_I_NODEV ; ctx->s_d_flags |=DCACHE_DONTCACHE ; ctx->ops = &nsfs_ops; ctx->eops = &nsfs_export_operations;fs/pidfs.c +0
-2@@ -1115,8 +1115,6 @@ static int pidfs_init_fs_context(struct fs_context *fc)if (!ctx)return -ENOMEM ; - fc->s_iflags |=SB_I_NOEXEC ; - fc->s_iflags |=SB_I_NODEV ; ctx->s_d_flags |=DCACHE_DONTCACHE ; ctx->ops = &pidfs_sops; ctx->eops = &pidfs_export_operations;mm/secretmem.c +0
-2@@ -245,8 +245,6 @@ static int secretmem_init_fs_context(struct fs_context *fc)if (!ctx)return -ENOMEM ; - fc->s_iflags |=SB_I_NOEXEC ; - fc->s_iflags |=SB_I_NODEV ;return 0 ; }virt/kvm/guest_memfd.c +0
-2@@ -973,8 +973,6 @@ static int kvm_gmem_init_fs_context(struct fs_context *fc)if (!init_pseudo (fc,GUEST_MEMFD_MAGIC ))return -ENOMEM ; - fc->s_iflags |=SB_I_NOEXEC ; - fc->s_iflags |=SB_I_NODEV ; ctx = fc->fs_private; ctx->ops = &kvm_gmem_super_operations;Signature · 1 line
2.54.0
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR