Skip to contentSkip to conversation

vfs, security: Fix automount superblock LSM init problem, preventing NFS sb sharing

PATCH Superseded Jeff Layton 3 participants 8 messages Aug 4, 2023 – Aug 7, 2023

Subsystems (6)

v7 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Jeff Layton

author maintainer
#0 lore raw

PATCH v7 1/1 vfs, security: Fix automount superblock LSM init problem, preventing NFS sb sharing

From: David Howells <[email protected]>

When NFS superblocks are created by automounting, their LSM parameters aren't set in the fs_context struct prior to sget_fc() being called, leading to failure to match existing superblocks.

This bug leads to messages like the following appearing in dmesg when fscache is enabled:

    NFS: Cache volume key already in use (nfs,4.2,2,108,106a8c0,1,,,,100000,100000,2ee,3a98,1d4c,3a98,1)

Fix this by adding a new LSM hook to load fc->security for submount creation when alloc_fs_context() is creating the fs_context for it.

---
ver #7)
- Drop lsm_set boolean
- Link to v6: https://lore.kernel.org/r/[email protected]

ver #6)
- Rebase onto v6.5.0-rc4

ver #5)
- Removed unused variable.
- Only allocate smack_mnt_opts if we're dealing with a submount.

ver #4)
- When doing a FOR_SUBMOUNT mount, don't set the root label in SELinux or Smack.

ver #3)
- Made LSM parameter extraction dependent on fc->purpose == FS_CONTEXT_FOR_SUBMOUNT. Shouldn't happen on FOR_RECONFIGURE.

ver #2)
- Added Smack support
- Made LSM parameter extraction dependent on reference != NULL.

---
 fs/fs_context.c               |  4 ++++
 include/linux/lsm_hook_defs.h |  1 +
 include/linux/security.h      |  6 +++++
 security/security.c           | 14 +++++++++++
 security/selinux/hooks.c      | 25 ++++++++++++++++++++
 security/smack/smack_lsm.c    | 54 +++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 104 insertions(+)
fs/fs_context.c +4 -0
index 851214d1d013..a523aea956c4 100644
@@ -282,6 +282,10 @@ static struct fs_context *alloc_fs_context(struct file_system_type *fs_type,
 		break;
 	}
 
+	ret = security_fs_context_init(fc, reference);
+	if (ret < 0)
+		goto err_fc;
+
 	/* TODO: Make all filesystems support this unconditionally */
 	init_fs_context = fc->fs_type->init_fs_context;
 	if (!init_fs_context)
include/linux/lsm_hook_defs.h +1 -0
index 7308a1a7599b..7ce3550154b1 100644
@@ -54,6 +54,7 @@ LSM_HOOK(int, 0, bprm_creds_from_file, struct linux_binprm *bprm, struct file *f
 LSM_HOOK(int, 0, bprm_check_security, struct linux_binprm *bprm)
 LSM_HOOK(void, LSM_RET_VOID, bprm_committing_creds, struct linux_binprm *bprm)
 LSM_HOOK(void, LSM_RET_VOID, bprm_committed_creds, struct linux_binprm *bprm)
+LSM_HOOK(int, 0, fs_context_init, struct fs_context *fc, struct dentry *reference)
 LSM_HOOK(int, 0, fs_context_dup, struct fs_context *fc,
 	 struct fs_context *src_sc)
 LSM_HOOK(int, -ENOPARAM, fs_context_parse_param, struct fs_context *fc,
include/linux/security.h +6 -0
index 32828502f09e..61fda06fac9d 100644
@@ -293,6 +293,7 @@ int security_bprm_creds_from_file(struct linux_binprm *bprm, struct file *file);
 int security_bprm_check(struct linux_binprm *bprm);
 void security_bprm_committing_creds(struct linux_binprm *bprm);
 void security_bprm_committed_creds(struct linux_binprm *bprm);
+int security_fs_context_init(struct fs_context *fc, struct dentry *reference);
 int security_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc);
 int security_fs_context_parse_param(struct fs_context *fc, struct fs_parameter *param);
 int security_sb_alloc(struct super_block *sb);
@@ -629,6 +630,11 @@ static inline void security_bprm_committed_creds(struct linux_binprm *bprm)
 {
 }
 
+static inline int security_fs_context_init(struct fs_context *fc,
+					   struct dentry *reference)
+{
+	return 0;
+}
 static inline int security_fs_context_dup(struct fs_context *fc,
 					  struct fs_context *src_fc)
 {
security/security.c +14 -0
index b720424ca37d..8a6dc6f7cda0 100644
@@ -1138,6 +1138,20 @@ void security_bprm_committed_creds(struct linux_binprm *bprm)
 	call_void_hook(bprm_committed_creds, bprm);
 }
 
+/**
+ * security_fs_context_init() - Initialise fc->security
+ * @fc: new filesystem context
+ * @dentry: dentry reference for submount/remount
+ *
+ * Fill out the ->security field for a new fs_context.
+ *
+ * Return: Returns 0 on success or negative error code on failure.
+ */
+int security_fs_context_init(struct fs_context *fc, struct dentry *reference)
+{
+	return call_int_hook(fs_context_init, 0, fc, reference);
+}
+
 /**
  * security_fs_context_dup() - Duplicate a fs_context LSM blob
  * @fc: destination filesystem context
security/selinux/hooks.c +25 -0
index d06e350fedee..97fe5aab82dc 100644
@@ -2745,6 +2745,30 @@ static int selinux_umount(struct vfsmount *mnt, int flags)
 				   FILESYSTEM__UNMOUNT, NULL);
 }
 
+static int selinux_fs_context_init(struct fs_context *fc,
+				   struct dentry *reference)
+{
+	const struct superblock_security_struct *sbsec;
+	struct selinux_mnt_opts *opts;
+
+	if (!reference || fc->purpose != FS_CONTEXT_FOR_SUBMOUNT)
+		return 0;
+
+	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
+	if (!opts)
+		return -ENOMEM;
+
+	sbsec = selinux_superblock(reference->d_sb);
+	if (sbsec->flags & FSCONTEXT_MNT)
+		opts->fscontext_sid = sbsec->sid;
+	if (sbsec->flags & CONTEXT_MNT)
+		opts->context_sid = sbsec->mntpoint_sid;
+	if (sbsec->flags & DEFCONTEXT_MNT)
+		opts->defcontext_sid = sbsec->def_sid;
+	fc->security = opts;
+	return 0;
+}
+
 static int selinux_fs_context_dup(struct fs_context *fc,
 				  struct fs_context *src_fc)
 {
@@ -7182,6 +7206,7 @@ static struct security_hook_list selinux_hooks[] __ro_after_init = {
 	/*
 	 * PUT "CLONING" (ACCESSING + ALLOCATING) HOOKS HERE
 	 */
+	LSM_HOOK_INIT(fs_context_init, selinux_fs_context_init),
 	LSM_HOOK_INIT(fs_context_dup, selinux_fs_context_dup),
 	LSM_HOOK_INIT(fs_context_parse_param, selinux_fs_context_parse_param),
 	LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts),
security/smack/smack_lsm.c +54 -0

66 lines not on this page · raw message

---
base-commit: 5d0c230f1de8c7515b6567d9afba1f196fb4e2f4
change-id: 20230802-master-3082090e8d69

Best regards,

Signature · 1 line
Jeff Layton <[email protected]>
#1 lore raw
kernel test robot noticed the following build warnings:

Hi Jeff,

kernel test robot noticed the following build warnings:

[auto build test WARNING on 5d0c230f1de8c7515b6567d9afba1f196fb4e2f4]

url:    https://github.com/intel-lab-lkp/linux/commits/Jeff-Layton/vfs-security-Fix-automount-superblock-LSM-init-problem-preventing-NFS-sb-sharing/20230805-001234
base:   5d0c230f1de8c7515b6567d9afba1f196fb4e2f4
patch link:    https://lore.kernel.org/r/20230804-master-v7-1-5d4e48407298%40kernel.org
patch subject: [PATCH v7] vfs, security: Fix automount superblock LSM init problem, preventing NFS sb sharing
config: loongarch-allyesconfig (https://download.01.org/0day-ci/archive/20230805/[email protected]/config)
compiler: loongarch64-linux-gcc (GCC) 12.3.0
reproduce: (https://download.01.org/0day-ci/archive/20230805/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

security/smack/smack_lsm.c: In function 'smack_fs_context_init':

>> security/smack/smack_lsm.c:631:66: warning: suggest braces around empty body in an 'if' statement [-Wempty-body]
     631 |         if (!reference || fc->purpose != FS_CONTEXT_FOR_SUBMOUNT);
         |                                                                  ^
>> security/smack/smack_lsm.c:631:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
     631 |         if (!reference || fc->purpose != FS_CONTEXT_FOR_SUBMOUNT);
         |         ^~
   security/smack/smack_lsm.c:632:17: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
     632 |                 return 0;
         |                 ^~~~~~

vim +/if +631 security/smack/smack_lsm.c

616
617 /**
618 * smack_fs_context_init - Initialise security data for a filesystem context
619 * @fc: The filesystem context.
620 * @reference: Reference dentry (automount/reconfigure) or NULL
621 *
622 * Returns 0 on success or -ENOMEM on error.
623 */
624 static int smack_fs_context_init(struct fs_context *fc,
625 struct dentry *reference)
626 {
627 struct superblock_smack *sbsp;
628 struct smack_mnt_opts *ctx;
629 struct inode_smack *isp;
630

 > 631		if (!reference || fc->purpose != FS_CONTEXT_FOR_SUBMOUNT);

632 return 0;
633
634 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
635 if (!ctx)
636 return -ENOMEM;
637 fc->security = ctx;
638
639 sbsp = smack_superblock(reference->d_sb);
640 isp = smack_inode(reference->d_sb->s_root->d_inode);
641
642 if (sbsp->smk_default) {
643 ctx->fsdefault = kstrdup(sbsp->smk_default->smk_known, GFP_KERNEL);
644 if (!ctx->fsdefault)
645 return -ENOMEM;
646 }
647
648 if (sbsp->smk_floor) {
649 ctx->fsfloor = kstrdup(sbsp->smk_floor->smk_known, GFP_KERNEL);
650 if (!ctx->fsfloor)
651 return -ENOMEM;
652 }
653
654 if (sbsp->smk_hat) {
655 ctx->fshat = kstrdup(sbsp->smk_hat->smk_known, GFP_KERNEL);
656 if (!ctx->fshat)
657 return -ENOMEM;
658 }
659
660 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
661 if (sbsp->smk_root) {
662 ctx->fstransmute = kstrdup(sbsp->smk_root->smk_known, GFP_KERNEL);
663 if (!ctx->fstransmute)
664 return -ENOMEM;
665 }
666 }
667 return 0;
668 }
669

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
author maintainer
#2 lore raw
Quoted 35 lines · kernel test robot

Hi Jeff,

kernel test robot noticed the following build warnings:

[auto build test WARNING on 5d0c230f1de8c7515b6567d9afba1f196fb4e2f4]

url:    https://github.com/intel-lab-lkp/linux/commits/Jeff-Layton/vfs-security-Fix-automount-superblock-LSM-init-problem-preventing-NFS-sb-sharing/20230805-001234
base:   5d0c230f1de8c7515b6567d9afba1f196fb4e2f4
patch link:    https://lore.kernel.org/r/20230804-master-v7-1-5d4e48407298%40kernel.org
patch subject: [PATCH v7] vfs, security: Fix automount superblock LSM init problem, preventing NFS sb sharing
config: loongarch-allyesconfig (https://download.01.org/0day-ci/archive/20230805/[email protected]/config)
compiler: loongarch64-linux-gcc (GCC) 12.3.0
reproduce: (https://download.01.org/0day-ci/archive/20230805/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags

Quoted 2 lines

All warnings (new ones prefixed by >>):

security/smack/smack_lsm.c: In function 'smack_fs_context_init':

Quoted 1 line
Quoted 1 line

security/smack/smack_lsm.c:631:66: warning: suggest braces around empty body in an 'if' statement [-Wempty-body]

     631 |         if (!reference || fc->purpose != FS_CONTEXT_FOR_SUBMOUNT);
         |                                                                  ^
Quoted 1 line
Quoted 1 line

security/smack/smack_lsm.c:631:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]

     631 |         if (!reference || fc->purpose != FS_CONTEXT_FOR_SUBMOUNT);
         |         ^~
   security/smack/smack_lsm.c:632:17: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
     632 |                 return 0;
         |                 ^~~~~~

vim +/if +631 security/smack/smack_lsm.c

Quoted 16 lines
   616	
   617	/**
   618	 * smack_fs_context_init - Initialise security data for a filesystem context
   619	 * @fc: The filesystem context.
   620	 * @reference: Reference dentry (automount/reconfigure) or NULL
   621	 *
   622	 * Returns 0 on success or -ENOMEM on error.
   623	 */
   624	static int smack_fs_context_init(struct fs_context *fc,
   625					 struct dentry *reference)
   626	{
   627		struct superblock_smack *sbsp;
   628		struct smack_mnt_opts *ctx;
   629		struct inode_smack *isp;
   630	
Quoted 1 line

631 if (!reference || fc->purpose != FS_CONTEXT_FOR_SUBMOUNT);

Ouch! Bad semicolon on the end. Fixed in my tree.

Quoted 39 lines
   632			return 0;
   633	
   634		ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
   635		if (!ctx)
   636			return -ENOMEM;
   637		fc->security = ctx;
   638	
   639		sbsp = smack_superblock(reference->d_sb);
   640		isp = smack_inode(reference->d_sb->s_root->d_inode);
   641	
   642		if (sbsp->smk_default) {
   643			ctx->fsdefault = kstrdup(sbsp->smk_default->smk_known, GFP_KERNEL);
   644			if (!ctx->fsdefault)
   645				return -ENOMEM;
   646		}
   647	
   648		if (sbsp->smk_floor) {
   649			ctx->fsfloor = kstrdup(sbsp->smk_floor->smk_known, GFP_KERNEL);
   650			if (!ctx->fsfloor)
   651				return -ENOMEM;
   652		}
   653	
   654		if (sbsp->smk_hat) {
   655			ctx->fshat = kstrdup(sbsp->smk_hat->smk_known, GFP_KERNEL);
   656			if (!ctx->fshat)
   657				return -ENOMEM;
   658		}
   659	
   660		if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
   661			if (sbsp->smk_root) {
   662				ctx->fstransmute = kstrdup(sbsp->smk_root->smk_known, GFP_KERNEL);
   663				if (!ctx->fstransmute)
   664					return -ENOMEM;
   665			}
   666		}
   667		return 0;
   668	}
   669	
Signature · 1 line
Jeff Layton <[email protected]>
#3 lore raw
In reply to Jeff Layton
kernel test robot noticed the following build warnings:

Hi Jeff,

kernel test robot noticed the following build warnings:

[auto build test WARNING on 5d0c230f1de8c7515b6567d9afba1f196fb4e2f4]

url:    https://github.com/intel-lab-lkp/linux/commits/Jeff-Layton/vfs-security-Fix-automount-superblock-LSM-init-problem-preventing-NFS-sb-sharing/20230805-001234
base:   5d0c230f1de8c7515b6567d9afba1f196fb4e2f4
patch link:    https://lore.kernel.org/r/20230804-master-v7-1-5d4e48407298%40kernel.org
patch subject: [PATCH v7] vfs, security: Fix automount superblock LSM init problem, preventing NFS sb sharing
config: hexagon-randconfig-r041-20230801 (https://download.01.org/0day-ci/archive/20230805/[email protected]/config)
compiler: clang version 17.0.0 (https://github.com/llvm/llvm-project.git 4a5ac14ee968ff0ad5d2cc1ffa0299048db4c88a)
reproduce: (https://download.01.org/0day-ci/archive/20230805/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

In file included from security/smack/smack_lsm.c:19:
In file included from include/linux/pagemap.h:11:
In file included from include/linux/highmem.h:12:
In file included from include/linux/hardirq.h:11:
In file included from ./arch/hexagon/include/generated/asm/hardirq.h:1:
In file included from include/asm-generic/hardirq.h:17:
In file included from include/linux/irq.h:20:
In file included from include/linux/io.h:13:
In file included from arch/hexagon/include/asm/io.h:334:
include/asm-generic/io.h:547:31: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]

     547 |         val = __raw_readb(PCI_IOBASE + addr);
         |                           ~~~~~~~~~~ ^
   include/asm-generic/io.h:560:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     560 |         val = __le16_to_cpu((__le16 __force)__raw_readw(PCI_IOBASE + addr));
         |                                                         ~~~~~~~~~~ ^
   include/uapi/linux/byteorder/little_endian.h:37:51: note: expanded from macro '__le16_to_cpu'
      37 | #define __le16_to_cpu(x) ((__force __u16)(__le16)(x))
         |                                                   ^
   In file included from security/smack/smack_lsm.c:19:
   In file included from include/linux/pagemap.h:11:
   In file included from include/linux/highmem.h:12:
   In file included from include/linux/hardirq.h:11:
   In file included from ./arch/hexagon/include/generated/asm/hardirq.h:1:
   In file included from include/asm-generic/hardirq.h:17:
   In file included from include/linux/irq.h:20:
   In file included from include/linux/io.h:13:
   In file included from arch/hexagon/include/asm/io.h:334:
   include/asm-generic/io.h:573:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     573 |         val = __le32_to_cpu((__le32 __force)__raw_readl(PCI_IOBASE + addr));
         |                                                         ~~~~~~~~~~ ^
   include/uapi/linux/byteorder/little_endian.h:35:51: note: expanded from macro '__le32_to_cpu'
      35 | #define __le32_to_cpu(x) ((__force __u32)(__le32)(x))
         |                                                   ^
   In file included from security/smack/smack_lsm.c:19:
   In file included from include/linux/pagemap.h:11:
   In file included from include/linux/highmem.h:12:
   In file included from include/linux/hardirq.h:11:
   In file included from ./arch/hexagon/include/generated/asm/hardirq.h:1:
   In file included from include/asm-generic/hardirq.h:17:
   In file included from include/linux/irq.h:20:
   In file included from include/linux/io.h:13:
   In file included from arch/hexagon/include/asm/io.h:334:
   include/asm-generic/io.h:584:33: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     584 |         __raw_writeb(value, PCI_IOBASE + addr);
         |                             ~~~~~~~~~~ ^
   include/asm-generic/io.h:594:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     594 |         __raw_writew((u16 __force)cpu_to_le16(value), PCI_IOBASE + addr);
         |                                                       ~~~~~~~~~~ ^
   include/asm-generic/io.h:604:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
     604 |         __raw_writel((u32 __force)cpu_to_le32(value), PCI_IOBASE + addr);
         |                                                       ~~~~~~~~~~ ^
>> security/smack/smack_lsm.c:631:59: warning: if statement has empty body [-Wempty-body]
     631 |         if (!reference || fc->purpose != FS_CONTEXT_FOR_SUBMOUNT);
         |                                                                  ^

security/smack/smack_lsm.c:631:59: note: put the semicolon on a separate line to silence this warning 7 warnings generated.

vim +631 security/smack/smack_lsm.c

616
617 /**
618 * smack_fs_context_init - Initialise security data for a filesystem context
619 * @fc: The filesystem context.
620 * @reference: Reference dentry (automount/reconfigure) or NULL
621 *
622 * Returns 0 on success or -ENOMEM on error.
623 */
624 static int smack_fs_context_init(struct fs_context *fc,
625 struct dentry *reference)
626 {
627 struct superblock_smack *sbsp;
628 struct smack_mnt_opts *ctx;
629 struct inode_smack *isp;
630

 > 631		if (!reference || fc->purpose != FS_CONTEXT_FOR_SUBMOUNT);

632 return 0;
633
634 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
635 if (!ctx)
636 return -ENOMEM;
637 fc->security = ctx;
638
639 sbsp = smack_superblock(reference->d_sb);
640 isp = smack_inode(reference->d_sb->s_root->d_inode);
641
642 if (sbsp->smk_default) {
643 ctx->fsdefault = kstrdup(sbsp->smk_default->smk_known, GFP_KERNEL);
644 if (!ctx->fsdefault)
645 return -ENOMEM;
646 }
647
648 if (sbsp->smk_floor) {
649 ctx->fsfloor = kstrdup(sbsp->smk_floor->smk_known, GFP_KERNEL);
650 if (!ctx->fsfloor)
651 return -ENOMEM;
652 }
653
654 if (sbsp->smk_hat) {
655 ctx->fshat = kstrdup(sbsp->smk_hat->smk_known, GFP_KERNEL);
656 if (!ctx->fshat)
657 return -ENOMEM;
658 }
659
660 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
661 if (sbsp->smk_root) {
662 ctx->fstransmute = kstrdup(sbsp->smk_root->smk_known, GFP_KERNEL);
663 if (!ctx->fstransmute)
664 return -ENOMEM;
665 }
666 }
667 return 0;
668 }
669

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#4 lore raw
In reply to Jeff Layton
Quoted 99 lines · Jeff Layton

From: David Howells <[email protected]>

When NFS superblocks are created by automounting, their LSM parameters aren't set in the fs_context struct prior to sget_fc() being called, leading to failure to match existing superblocks.

This bug leads to messages like the following appearing in dmesg when fscache is enabled:

    NFS: Cache volume key already in use (nfs,4.2,2,108,106a8c0,1,,,,100000,100000,2ee,3a98,1d4c,3a98,1)

Fix this by adding a new LSM hook to load fc->security for submount creation when alloc_fs_context() is creating the fs_context for it.

---
ver #7)
- Drop lsm_set boolean
- Link to v6: https://lore.kernel.org/r/[email protected]

ver #6)
- Rebase onto v6.5.0-rc4

ver #5)
- Removed unused variable.
- Only allocate smack_mnt_opts if we're dealing with a submount.

ver #4)
- When doing a FOR_SUBMOUNT mount, don't set the root label in SELinux or Smack.

ver #3)
- Made LSM parameter extraction dependent on fc->purpose == FS_CONTEXT_FOR_SUBMOUNT. Shouldn't happen on FOR_RECONFIGURE.

ver #2)
- Added Smack support
- Made LSM parameter extraction dependent on reference != NULL.

---
 fs/fs_context.c               |  4 ++++
 include/linux/lsm_hook_defs.h |  1 +
 include/linux/security.h      |  6 +++++
 security/security.c           | 14 +++++++++++
 security/selinux/hooks.c      | 25 ++++++++++++++++++++
 security/smack/smack_lsm.c    | 54 +++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 104 insertions(+)
fs/fs_context.c +4 -0
index 851214d1d013..a523aea956c4 100644
@@ -282,6 +282,10 @@ static struct fs_context *alloc_fs_context(struct file_system_type *fs_type,
 		break;
 	}
 
+	ret = security_fs_context_init(fc, reference);
+	if (ret < 0)
+		goto err_fc;
+
 	/* TODO: Make all filesystems support this unconditionally */
 	init_fs_context = fc->fs_type->init_fs_context;
 	if (!init_fs_context)
include/linux/lsm_hook_defs.h +1 -0
index 7308a1a7599b..7ce3550154b1 100644
@@ -54,6 +54,7 @@ LSM_HOOK(int, 0, bprm_creds_from_file, struct linux_binprm *bprm, struct file *f
 LSM_HOOK(int, 0, bprm_check_security, struct linux_binprm *bprm)
 LSM_HOOK(void, LSM_RET_VOID, bprm_committing_creds, struct linux_binprm *bprm)
 LSM_HOOK(void, LSM_RET_VOID, bprm_committed_creds, struct linux_binprm *bprm)
+LSM_HOOK(int, 0, fs_context_init, struct fs_context *fc, struct dentry *reference)
 LSM_HOOK(int, 0, fs_context_dup, struct fs_context *fc,
 	 struct fs_context *src_sc)
 LSM_HOOK(int, -ENOPARAM, fs_context_parse_param, struct fs_context *fc,
include/linux/security.h +1 -0
index 32828502f09e..61fda06fac9d 100644
@@ -293,6 +293,7 @@ int security_bprm_creds_from_file(struct linux_binprm *bprm, struct file *file);
 int security_bprm_check(struct linux_binprm *bprm);
 void security_bprm_committing_creds(struct linux_binprm *bprm);
 void security_bprm_committed_creds(struct linux_binprm *bprm);
+int security_fs_context_init(struct fs_context *fc, struct dentry *reference);
 int security_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc);
 int security_fs_context_parse_param(struct fs_context *fc, struct fs_parameter *param);
 int security_sb_alloc(struct super_block *sb);

include/linux/security.h static inline void security_bprm_committed_creds(struct linux_binprm *bprm) · line 634

Quoted 5 lines
include/linux/security.h +2 -0
@@ -629,6 +630,11 @@ static inline void security_bprm_committed_creds(struct linux_binprm *bprm)
 {
 }
 
+static inline int security_fs_context_init(struct fs_context *fc,
+					   struct dentry *reference)

I think that's the wrong way of doing this hook. The security hook really doesn't belong into alloc_fs_context().

I think what we want is a dedicated helper similar to vfs_dup_context():

// Only pass the superblock. There's no need for the dentry. I would // avoid even passing fs_context but if that's preferred then sure. security_fs_context_submount(struct fs_context *fc, const struct super_block *sb)

vfs_submount_fs_context(struct file_system_type *fs_type, struct dentry *reference)

{
        fc = fs_context_for_submount(fs_type, reference);

        security_fs_context_for_submount(fc, reference->d_sb);
}

This automatically ensures it's only called for submounts, the LSM doesn't need to care about fc->purpose and this isn't called in a pure allocation function for all allocation calls.

The we should switch all callers over to that new helper and unexport that fs_context_for_submount() thing completely. Yes, that's more work but that's the correct thing to do. And we need to audit fuse, cifs, afs, and nfs anyway that they work fine with the new security hook.*

[1]: If really needed, then any additional fs specific work that needs
to be done during submount allocation should probably probably be done in a new callback.

struct fs_context_operations {
void (*free)(struct fs_context *fc);
int (*dup)(struct fs_context *fc, struct fs_context *src_fc);
+ int (*submount)(struct fs_context *fc, const struct super_block *sb);

#5 lore raw
Quoted 126 lines · Christian Brauner
Quoted 99 lines · Jeff Layton

From: David Howells <[email protected]>

When NFS superblocks are created by automounting, their LSM parameters aren't set in the fs_context struct prior to sget_fc() being called, leading to failure to match existing superblocks.

This bug leads to messages like the following appearing in dmesg when fscache is enabled:

    NFS: Cache volume key already in use (nfs,4.2,2,108,106a8c0,1,,,,100000,100000,2ee,3a98,1d4c,3a98,1)

Fix this by adding a new LSM hook to load fc->security for submount creation when alloc_fs_context() is creating the fs_context for it.

---
ver #7)
- Drop lsm_set boolean
- Link to v6: https://lore.kernel.org/r/[email protected]

ver #6)
- Rebase onto v6.5.0-rc4

ver #5)
- Removed unused variable.
- Only allocate smack_mnt_opts if we're dealing with a submount.

ver #4)
- When doing a FOR_SUBMOUNT mount, don't set the root label in SELinux or Smack.

ver #3)
- Made LSM parameter extraction dependent on fc->purpose == FS_CONTEXT_FOR_SUBMOUNT. Shouldn't happen on FOR_RECONFIGURE.

ver #2)
- Added Smack support
- Made LSM parameter extraction dependent on reference != NULL.

---
 fs/fs_context.c               |  4 ++++
 include/linux/lsm_hook_defs.h |  1 +
 include/linux/security.h      |  6 +++++
 security/security.c           | 14 +++++++++++
 security/selinux/hooks.c      | 25 ++++++++++++++++++++
 security/smack/smack_lsm.c    | 54 +++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 104 insertions(+)
fs/fs_context.c +4 -0
index 851214d1d013..a523aea956c4 100644
@@ -282,6 +282,10 @@ static struct fs_context *alloc_fs_context(struct file_system_type *fs_type,
 		break;
 	}
 
+	ret = security_fs_context_init(fc, reference);
+	if (ret < 0)
+		goto err_fc;
+
 	/* TODO: Make all filesystems support this unconditionally */
 	init_fs_context = fc->fs_type->init_fs_context;
 	if (!init_fs_context)
include/linux/lsm_hook_defs.h +1 -0
index 7308a1a7599b..7ce3550154b1 100644
@@ -54,6 +54,7 @@ LSM_HOOK(int, 0, bprm_creds_from_file, struct linux_binprm *bprm, struct file *f
 LSM_HOOK(int, 0, bprm_check_security, struct linux_binprm *bprm)
 LSM_HOOK(void, LSM_RET_VOID, bprm_committing_creds, struct linux_binprm *bprm)
 LSM_HOOK(void, LSM_RET_VOID, bprm_committed_creds, struct linux_binprm *bprm)
+LSM_HOOK(int, 0, fs_context_init, struct fs_context *fc, struct dentry *reference)
 LSM_HOOK(int, 0, fs_context_dup, struct fs_context *fc,
 	 struct fs_context *src_sc)
 LSM_HOOK(int, -ENOPARAM, fs_context_parse_param, struct fs_context *fc,
include/linux/security.h +1 -0
index 32828502f09e..61fda06fac9d 100644
@@ -293,6 +293,7 @@ int security_bprm_creds_from_file(struct linux_binprm *bprm, struct file *file);
 int security_bprm_check(struct linux_binprm *bprm);
 void security_bprm_committing_creds(struct linux_binprm *bprm);
 void security_bprm_committed_creds(struct linux_binprm *bprm);
+int security_fs_context_init(struct fs_context *fc, struct dentry *reference);
 int security_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc);
 int security_fs_context_parse_param(struct fs_context *fc, struct fs_parameter *param);
 int security_sb_alloc(struct super_block *sb);

include/linux/security.h static inline void security_bprm_committed_creds(struct linux_binprm *bprm) · line 634

Quoted 5 lines
include/linux/security.h +2 -0
@@ -629,6 +630,11 @@ static inline void security_bprm_committed_creds(struct linux_binprm *bprm)
 {
 }
 
+static inline int security_fs_context_init(struct fs_context *fc,
+					   struct dentry *reference)

I think that's the wrong way of doing this hook. The security hook really doesn't belong into alloc_fs_context().

I think what we want is a dedicated helper similar to vfs_dup_context():

// Only pass the superblock. There's no need for the dentry. I would // avoid even passing fs_context but if that's preferred then sure. security_fs_context_submount(struct fs_context *fc, const struct super_block *sb)

vfs_submount_fs_context(struct file_system_type *fs_type, struct dentry *reference)

{
        fc = fs_context_for_submount(fs_type, reference);

        security_fs_context_for_submount(fc, reference->d_sb);
}

This automatically ensures it's only called for submounts, the LSM doesn't need to care about fc->purpose and this isn't called in a pure allocation function for all allocation calls.

Quoted 8 lines

The we should switch all callers over to that new helper and unexport that fs_context_for_submount() thing completely. Yes, that's more work but that's the correct thing to do. And we need to audit fuse, cifs, afs, and nfs anyway that they work fine with the new security hook.*

[1]: If really needed, then any additional fs specific work that needs
to be done during submount allocation should probably probably be

s/allocation/creation/

Quoted 6 lines
     done in a new callback.

     struct fs_context_operations {
            void (*free)(struct fs_context *fc);
            int (*dup)(struct fs_context *fc, struct fs_context *src_fc);
    +       int (*submount)(struct fs_context *fc, const struct super_block *sb);

in vfs_submount_fs_context() mirroring the ->dup() call in vfs_dup_context().

author maintainer
#6 lore raw
In reply to Christian Brauner
Quoted 122 lines · Christian Brauner
Quoted 99 lines · Jeff Layton

From: David Howells <[email protected]>

When NFS superblocks are created by automounting, their LSM parameters aren't set in the fs_context struct prior to sget_fc() being called, leading to failure to match existing superblocks.

This bug leads to messages like the following appearing in dmesg when fscache is enabled:

    NFS: Cache volume key already in use (nfs,4.2,2,108,106a8c0,1,,,,100000,100000,2ee,3a98,1d4c,3a98,1)

Fix this by adding a new LSM hook to load fc->security for submount creation when alloc_fs_context() is creating the fs_context for it.

---
ver #7)
- Drop lsm_set boolean
- Link to v6: https://lore.kernel.org/r/[email protected]

ver #6)
- Rebase onto v6.5.0-rc4

ver #5)
- Removed unused variable.
- Only allocate smack_mnt_opts if we're dealing with a submount.

ver #4)
- When doing a FOR_SUBMOUNT mount, don't set the root label in SELinux or Smack.

ver #3)
- Made LSM parameter extraction dependent on fc->purpose == FS_CONTEXT_FOR_SUBMOUNT. Shouldn't happen on FOR_RECONFIGURE.

ver #2)
- Added Smack support
- Made LSM parameter extraction dependent on reference != NULL.

---
 fs/fs_context.c               |  4 ++++
 include/linux/lsm_hook_defs.h |  1 +
 include/linux/security.h      |  6 +++++
 security/security.c           | 14 +++++++++++
 security/selinux/hooks.c      | 25 ++++++++++++++++++++
 security/smack/smack_lsm.c    | 54 +++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 104 insertions(+)
fs/fs_context.c +4 -0
index 851214d1d013..a523aea956c4 100644
@@ -282,6 +282,10 @@ static struct fs_context *alloc_fs_context(struct file_system_type *fs_type,
 		break;
 	}
 
+	ret = security_fs_context_init(fc, reference);
+	if (ret < 0)
+		goto err_fc;
+
 	/* TODO: Make all filesystems support this unconditionally */
 	init_fs_context = fc->fs_type->init_fs_context;
 	if (!init_fs_context)
include/linux/lsm_hook_defs.h +1 -0
index 7308a1a7599b..7ce3550154b1 100644
@@ -54,6 +54,7 @@ LSM_HOOK(int, 0, bprm_creds_from_file, struct linux_binprm *bprm, struct file *f
 LSM_HOOK(int, 0, bprm_check_security, struct linux_binprm *bprm)
 LSM_HOOK(void, LSM_RET_VOID, bprm_committing_creds, struct linux_binprm *bprm)
 LSM_HOOK(void, LSM_RET_VOID, bprm_committed_creds, struct linux_binprm *bprm)
+LSM_HOOK(int, 0, fs_context_init, struct fs_context *fc, struct dentry *reference)
 LSM_HOOK(int, 0, fs_context_dup, struct fs_context *fc,
 	 struct fs_context *src_sc)
 LSM_HOOK(int, -ENOPARAM, fs_context_parse_param, struct fs_context *fc,
include/linux/security.h +1 -0
index 32828502f09e..61fda06fac9d 100644
@@ -293,6 +293,7 @@ int security_bprm_creds_from_file(struct linux_binprm *bprm, struct file *file);
 int security_bprm_check(struct linux_binprm *bprm);
 void security_bprm_committing_creds(struct linux_binprm *bprm);
 void security_bprm_committed_creds(struct linux_binprm *bprm);
+int security_fs_context_init(struct fs_context *fc, struct dentry *reference);
 int security_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc);
 int security_fs_context_parse_param(struct fs_context *fc, struct fs_parameter *param);
 int security_sb_alloc(struct super_block *sb);

include/linux/security.h static inline void security_bprm_committed_creds(struct linux_binprm *bprm) · line 634

Quoted 5 lines
include/linux/security.h +2 -0
@@ -629,6 +630,11 @@ static inline void security_bprm_committed_creds(struct linux_binprm *bprm)
 {
 }
 
+static inline int security_fs_context_init(struct fs_context *fc,
+					   struct dentry *reference)

I think that's the wrong way of doing this hook. The security hook really doesn't belong into alloc_fs_context().

I think what we want is a dedicated helper similar to vfs_dup_context():

// Only pass the superblock. There's no need for the dentry. I would // avoid even passing fs_context but if that's preferred then sure. security_fs_context_submount(struct fs_context *fc, const struct super_block *sb)

vfs_submount_fs_context(struct file_system_type *fs_type, struct dentry *reference)

{
        fc = fs_context_for_submount(fs_type, reference);

        security_fs_context_for_submount(fc, reference->d_sb);
}
Quoted 9 lines

This automatically ensures it's only called for submounts, the LSM doesn't need to care about fc->purpose and this isn't called in a pure allocation function for all allocation calls.

The we should switch all callers over to that new helper and unexport that fs_context_for_submount() thing completely. Yes, that's more work but that's the correct thing to do. And we need to audit fuse, cifs, afs, and nfs anyway that they work fine with the new security hook.*

It's the same prototype. We could just move the hook call to the end of fs_context_for_submount, and that would be less churn for its callers. Or were you wanting to do that to make this a more gradual changeover for some reason?

I will rework the security hook to take a sb pointer instead though.

Quoted 9 lines

[1]: If really needed, then any additional fs specific work that needs
to be done during submount allocation should probably probably be done in a new callback.

struct fs_context_operations {
void (*free)(struct fs_context *fc);
int (*dup)(struct fs_context *fc, struct fs_context *src_fc);
+ int (*submount)(struct fs_context *fc, const struct super_block *sb);

Signature · 1 line
Jeff Layton <[email protected]>
#7 lore raw
Quoted 127 lines · Jeff Layton
Quoted 126 lines · Christian Brauner
Quoted 99 lines · Jeff Layton

From: David Howells <[email protected]>

When NFS superblocks are created by automounting, their LSM parameters aren't set in the fs_context struct prior to sget_fc() being called, leading to failure to match existing superblocks.

This bug leads to messages like the following appearing in dmesg when fscache is enabled:

    NFS: Cache volume key already in use (nfs,4.2,2,108,106a8c0,1,,,,100000,100000,2ee,3a98,1d4c,3a98,1)

Fix this by adding a new LSM hook to load fc->security for submount creation when alloc_fs_context() is creating the fs_context for it.

---
ver #7)
- Drop lsm_set boolean
- Link to v6: https://lore.kernel.org/r/[email protected]

ver #6)
- Rebase onto v6.5.0-rc4

ver #5)
- Removed unused variable.
- Only allocate smack_mnt_opts if we're dealing with a submount.

ver #4)
- When doing a FOR_SUBMOUNT mount, don't set the root label in SELinux or Smack.

ver #3)
- Made LSM parameter extraction dependent on fc->purpose == FS_CONTEXT_FOR_SUBMOUNT. Shouldn't happen on FOR_RECONFIGURE.

ver #2)
- Added Smack support
- Made LSM parameter extraction dependent on reference != NULL.

---
 fs/fs_context.c               |  4 ++++
 include/linux/lsm_hook_defs.h |  1 +
 include/linux/security.h      |  6 +++++
 security/security.c           | 14 +++++++++++
 security/selinux/hooks.c      | 25 ++++++++++++++++++++
 security/smack/smack_lsm.c    | 54 +++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 104 insertions(+)
fs/fs_context.c +4 -0
index 851214d1d013..a523aea956c4 100644
@@ -282,6 +282,10 @@ static struct fs_context *alloc_fs_context(struct file_system_type *fs_type,
 		break;
 	}
 
+	ret = security_fs_context_init(fc, reference);
+	if (ret < 0)
+		goto err_fc;
+
 	/* TODO: Make all filesystems support this unconditionally */
 	init_fs_context = fc->fs_type->init_fs_context;
 	if (!init_fs_context)
include/linux/lsm_hook_defs.h +1 -0
index 7308a1a7599b..7ce3550154b1 100644
@@ -54,6 +54,7 @@ LSM_HOOK(int, 0, bprm_creds_from_file, struct linux_binprm *bprm, struct file *f
 LSM_HOOK(int, 0, bprm_check_security, struct linux_binprm *bprm)
 LSM_HOOK(void, LSM_RET_VOID, bprm_committing_creds, struct linux_binprm *bprm)
 LSM_HOOK(void, LSM_RET_VOID, bprm_committed_creds, struct linux_binprm *bprm)
+LSM_HOOK(int, 0, fs_context_init, struct fs_context *fc, struct dentry *reference)
 LSM_HOOK(int, 0, fs_context_dup, struct fs_context *fc,
 	 struct fs_context *src_sc)
 LSM_HOOK(int, -ENOPARAM, fs_context_parse_param, struct fs_context *fc,
include/linux/security.h +1 -0
index 32828502f09e..61fda06fac9d 100644
@@ -293,6 +293,7 @@ int security_bprm_creds_from_file(struct linux_binprm *bprm, struct file *file);
 int security_bprm_check(struct linux_binprm *bprm);
 void security_bprm_committing_creds(struct linux_binprm *bprm);
 void security_bprm_committed_creds(struct linux_binprm *bprm);
+int security_fs_context_init(struct fs_context *fc, struct dentry *reference);
 int security_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc);
 int security_fs_context_parse_param(struct fs_context *fc, struct fs_parameter *param);
 int security_sb_alloc(struct super_block *sb);

include/linux/security.h static inline void security_bprm_committed_creds(struct linux_binprm *bprm) · line 634

Quoted 5 lines
include/linux/security.h +2 -0
@@ -629,6 +630,11 @@ static inline void security_bprm_committed_creds(struct linux_binprm *bprm)
 {
 }
 
+static inline int security_fs_context_init(struct fs_context *fc,
+					   struct dentry *reference)

I think that's the wrong way of doing this hook. The security hook really doesn't belong into alloc_fs_context().

I think what we want is a dedicated helper similar to vfs_dup_context():

// Only pass the superblock. There's no need for the dentry. I would // avoid even passing fs_context but if that's preferred then sure. security_fs_context_submount(struct fs_context *fc, const struct super_block *sb)

vfs_submount_fs_context(struct file_system_type *fs_type, struct dentry *reference)

{
        fc = fs_context_for_submount(fs_type, reference);

        security_fs_context_for_submount(fc, reference->d_sb);
}

This automatically ensures it's only called for submounts, the LSM doesn't need to care about fc->purpose and this isn't called in a pure allocation function for all allocation calls.

Quoted 8 lines
Quoted 5 lines

The we should switch all callers over to that new helper and unexport that fs_context_for_submount() thing completely. Yes, that's more work but that's the correct thing to do. And we need to audit fuse, cifs, afs, and nfs anyway that they work fine with the new security hook.*

It's the same prototype. We could just move the hook call to the end of fs_context_for_submount, and that would be less churn for its callers.

If you just add it into fs_context_for_submount() after the allocation and then leave that as first class citizen it's fine ofc. It's the same result as what I mentioned earlier. We just shouldn't put generic hooks in an allocation function that allocates different types of filesystem contexts. I prefer to have them as targeted as possible and also avoid unnecessary trips into the LSM layer.

Quoted 2 lines

Or were you wanting to do that to make this a more gradual changeover for some reason?

No, I think it's fine if you switch it all in one go. I just don't know if fuse/virtiofs submounts expect an selinux context to be bestowed upon them.