Skip to contentSkip to conversation

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

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

Subsystems (6)

v8 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Jeff Layton

author maintainer
#0 lore raw

PATCH v8 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.

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

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

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

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

ver #6)
- Rebase onto v6.5.0-rc4
- Link to v6: https://lore.kernel.org/r/[email protected]

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

ver #8)
- Remove spurious semicolon in smack_fs_context_init
- Make fs_context_init take a superblock as reference instead of dentry
- WARN_ON_ONCE's when fc->purpose != FS_CONTEXT_FOR_SUBMOUNT
- Call the security hook from fs_context_for_submount instead of alloc_fs_context

---
 fs/fs_context.c               | 23 +++++++++++++++++-
 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, 122 insertions(+), 1 deletion(-)
fs/fs_context.c +22 -1
index 851214d1d013..a76d7c82e091 100644
@@ -315,10 +315,31 @@ struct fs_context *fs_context_for_reconfigure(struct dentry *dentry,
 }
 EXPORT_SYMBOL(fs_context_for_reconfigure);
 
+/**
+ * fs_context_for_submount: allocate a new fs_context for a submount
+ * @type: file_system_type of the new context
+ * @reference: reference dentry from which to copy relevant info
+ *
+ * Allocate a new fs_context suitable for a submount. This also ensures that
+ * the fc->security object is inherited from @reference (if needed).
+ */
 struct fs_context *fs_context_for_submount(struct file_system_type *type,
 					   struct dentry *reference)
 {
-	return alloc_fs_context(type, reference, 0, 0, FS_CONTEXT_FOR_SUBMOUNT);
+	struct fs_context *fc;
+	int ret;
+
+	fc = alloc_fs_context(type, reference, 0, 0, FS_CONTEXT_FOR_SUBMOUNT);
+	if (IS_ERR(fc))
+		return fc;
+
+	ret = security_fs_context_init(fc, reference->d_sb);
+	if (ret) {
+		put_fs_context(fc);
+		return ERR_PTR(ret);
+	}
+
+	return fc;
 }
 EXPORT_SYMBOL(fs_context_for_submount);
 
include/linux/lsm_hook_defs.h +1 -0
index 7308a1a7599b..2876dd6114c0 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 super_block *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..fe9bf5e805ee 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 super_block *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 super_block *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..f8a666d089f9 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
+ * @reference: 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 super_block *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..c8fb0d77104f 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 super_block *reference)
+{
+	const struct superblock_security_struct *sbsec;
+	struct selinux_mnt_opts *opts;
+
+	if (!reference || WARN_ON_ONCE(fc->purpose != FS_CONTEXT_FOR_SUBMOUNT))
+		return 0;
+
+	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
+	if (!opts)
+		return -ENOMEM;
+
+	sbsec = selinux_superblock(reference);
+	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: 52a93d39b17dc7eb98b6aa3edb93943248e03b2f
change-id: 20230802-master-3082090e8d69

Best regards,

Signature · 1 line
Jeff Layton <[email protected]>
#1 lore raw
Quoted 127 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.

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

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

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

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

ver #6)
- Rebase onto v6.5.0-rc4
- Link to v6: https://lore.kernel.org/r/[email protected]

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

ver #8)
- Remove spurious semicolon in smack_fs_context_init
- Make fs_context_init take a superblock as reference instead of dentry
- WARN_ON_ONCE's when fc->purpose != FS_CONTEXT_FOR_SUBMOUNT
- Call the security hook from fs_context_for_submount instead of alloc_fs_context

---
 fs/fs_context.c               | 23 +++++++++++++++++-
 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, 122 insertions(+), 1 deletion(-)
fs/fs_context.c +22 -1
index 851214d1d013..a76d7c82e091 100644
@@ -315,10 +315,31 @@ struct fs_context *fs_context_for_reconfigure(struct dentry *dentry,
 }
 EXPORT_SYMBOL(fs_context_for_reconfigure);
 
+/**
+ * fs_context_for_submount: allocate a new fs_context for a submount
+ * @type: file_system_type of the new context
+ * @reference: reference dentry from which to copy relevant info
+ *
+ * Allocate a new fs_context suitable for a submount. This also ensures that
+ * the fc->security object is inherited from @reference (if needed).
+ */
 struct fs_context *fs_context_for_submount(struct file_system_type *type,
 					   struct dentry *reference)
 {
-	return alloc_fs_context(type, reference, 0, 0, FS_CONTEXT_FOR_SUBMOUNT);
+	struct fs_context *fc;
+	int ret;
+
+	fc = alloc_fs_context(type, reference, 0, 0, FS_CONTEXT_FOR_SUBMOUNT);
+	if (IS_ERR(fc))
+		return fc;
+
+	ret = security_fs_context_init(fc, reference->d_sb);
+	if (ret) {
+		put_fs_context(fc);
+		return ERR_PTR(ret);
+	}
+
+	return fc;
 }
 EXPORT_SYMBOL(fs_context_for_submount);
 
include/linux/lsm_hook_defs.h +1 -0
index 7308a1a7599b..2876dd6114c0 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 super_block *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..fe9bf5e805ee 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 super_block *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 637

Quoted 8 lines
include/linux/security.h +5 -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 super_block *reference)
+{
+	return 0;
+}

Sorry, my point is we shouldn't be adding a generic security_fs_context_init() hook at all. Pre superblock creation we have a hook during parameter parsing for LSMs and another one during actual superblock creation in vfs_get_tree() and yet another one for fs_context duplicaton. We don't need another generic one during fs_context allocation.

Yes, we may need a hook for submount allocation but then we'll add one exactly for that. And then for fs_context_for_submount @sb can't be empty so there's also no point in checking whether it is empty because you've already crashed in fs_context_for_submount(). All the checks below for !reference and fc->purpose != FS_CONTEXT_FOR_SUBMOUNT can go away then as well.

So we end up with something easier and stricter.

Quoted 44 lines
include/linux/security.h +0 -0
 static inline int security_fs_context_dup(struct fs_context *fc,
 					  struct fs_context *src_fc)
 {
security/security.c +14 -0
index b720424ca37d..f8a666d089f9 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
+ * @reference: 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 super_block *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 +8 -0
index d06e350fedee..c8fb0d77104f 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 super_block *reference)
+{
+	const struct superblock_security_struct *sbsec;
+	struct selinux_mnt_opts *opts;
+
+	if (!reference || WARN_ON_ONCE(fc->purpose != FS_CONTEXT_FOR_SUBMOUNT))
+		return 0;
author maintainer
#2 lore raw
Quoted 144 lines · Christian Brauner
Quoted 127 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.

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

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

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

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

ver #6)
- Rebase onto v6.5.0-rc4
- Link to v6: https://lore.kernel.org/r/[email protected]

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

ver #8)
- Remove spurious semicolon in smack_fs_context_init
- Make fs_context_init take a superblock as reference instead of dentry
- WARN_ON_ONCE's when fc->purpose != FS_CONTEXT_FOR_SUBMOUNT
- Call the security hook from fs_context_for_submount instead of alloc_fs_context

---
 fs/fs_context.c               | 23 +++++++++++++++++-
 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, 122 insertions(+), 1 deletion(-)
fs/fs_context.c +22 -1
index 851214d1d013..a76d7c82e091 100644
@@ -315,10 +315,31 @@ struct fs_context *fs_context_for_reconfigure(struct dentry *dentry,
 }
 EXPORT_SYMBOL(fs_context_for_reconfigure);
 
+/**
+ * fs_context_for_submount: allocate a new fs_context for a submount
+ * @type: file_system_type of the new context
+ * @reference: reference dentry from which to copy relevant info
+ *
+ * Allocate a new fs_context suitable for a submount. This also ensures that
+ * the fc->security object is inherited from @reference (if needed).
+ */
 struct fs_context *fs_context_for_submount(struct file_system_type *type,
 					   struct dentry *reference)
 {
-	return alloc_fs_context(type, reference, 0, 0, FS_CONTEXT_FOR_SUBMOUNT);
+	struct fs_context *fc;
+	int ret;
+
+	fc = alloc_fs_context(type, reference, 0, 0, FS_CONTEXT_FOR_SUBMOUNT);
+	if (IS_ERR(fc))
+		return fc;
+
+	ret = security_fs_context_init(fc, reference->d_sb);
+	if (ret) {
+		put_fs_context(fc);
+		return ERR_PTR(ret);
+	}
+
+	return fc;
 }
 EXPORT_SYMBOL(fs_context_for_submount);
 
include/linux/lsm_hook_defs.h +1 -0
index 7308a1a7599b..2876dd6114c0 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 super_block *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..fe9bf5e805ee 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 super_block *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 637

Quoted 8 lines
include/linux/security.h +5 -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 super_block *reference)
+{
+	return 0;
+}

Sorry, my point is we shouldn't be adding a generic security_fs_context_init() hook at all. Pre superblock creation we have a hook during parameter parsing for LSMs and another one during actual superblock creation in vfs_get_tree() and yet another one for fs_context duplicaton. We don't need another generic one during fs_context allocation.

Quoted 9 lines

Yes, we may need a hook for submount allocation but then we'll add one exactly for that. And then for fs_context_for_submount @sb can't be empty so there's also no point in checking whether it is empty because you've already crashed in fs_context_for_submount(). All the checks below for !reference and fc->purpose != FS_CONTEXT_FOR_SUBMOUNT can go away then as well.

So we end up with something easier and stricter.

I'm not sure I understand what you're asking for here. fc->security field initialization is dependent on the LSM in use. Where will we do this initialization if we don't add this new hook? Are you suggesting that we fold this into an existing LSM hook? If so, which one?

Quoted 44 lines
Quoted 44 lines
include/linux/security.h +0 -0
 static inline int security_fs_context_dup(struct fs_context *fc,
 					  struct fs_context *src_fc)
 {
security/security.c +14 -0
index b720424ca37d..f8a666d089f9 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
+ * @reference: 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 super_block *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 +8 -0
index d06e350fedee..c8fb0d77104f 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 super_block *reference)
+{
+	const struct superblock_security_struct *sbsec;
+	struct selinux_mnt_opts *opts;
+
+	if (!reference || WARN_ON_ONCE(fc->purpose != FS_CONTEXT_FOR_SUBMOUNT))
+		return 0;
Signature · 1 line
Jeff Layton <[email protected]>
#3 lore raw
Quoted 152 lines · Jeff Layton
Quoted 151 lines · Christian Brauner
Quoted 127 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.

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

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

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

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

ver #6)
- Rebase onto v6.5.0-rc4
- Link to v6: https://lore.kernel.org/r/[email protected]

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

ver #8)
- Remove spurious semicolon in smack_fs_context_init
- Make fs_context_init take a superblock as reference instead of dentry
- WARN_ON_ONCE's when fc->purpose != FS_CONTEXT_FOR_SUBMOUNT
- Call the security hook from fs_context_for_submount instead of alloc_fs_context

---
 fs/fs_context.c               | 23 +++++++++++++++++-
 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, 122 insertions(+), 1 deletion(-)
fs/fs_context.c +22 -1
index 851214d1d013..a76d7c82e091 100644
@@ -315,10 +315,31 @@ struct fs_context *fs_context_for_reconfigure(struct dentry *dentry,
 }
 EXPORT_SYMBOL(fs_context_for_reconfigure);
 
+/**
+ * fs_context_for_submount: allocate a new fs_context for a submount
+ * @type: file_system_type of the new context
+ * @reference: reference dentry from which to copy relevant info
+ *
+ * Allocate a new fs_context suitable for a submount. This also ensures that
+ * the fc->security object is inherited from @reference (if needed).
+ */
 struct fs_context *fs_context_for_submount(struct file_system_type *type,
 					   struct dentry *reference)
 {
-	return alloc_fs_context(type, reference, 0, 0, FS_CONTEXT_FOR_SUBMOUNT);
+	struct fs_context *fc;
+	int ret;
+
+	fc = alloc_fs_context(type, reference, 0, 0, FS_CONTEXT_FOR_SUBMOUNT);
+	if (IS_ERR(fc))
+		return fc;
+
+	ret = security_fs_context_init(fc, reference->d_sb);
+	if (ret) {
+		put_fs_context(fc);
+		return ERR_PTR(ret);
+	}
+
+	return fc;
 }
 EXPORT_SYMBOL(fs_context_for_submount);
 
include/linux/lsm_hook_defs.h +1 -0
index 7308a1a7599b..2876dd6114c0 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 super_block *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..fe9bf5e805ee 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 super_block *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 637

Quoted 8 lines
include/linux/security.h +5 -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 super_block *reference)
+{
+	return 0;
+}

Sorry, my point is we shouldn't be adding a generic security_fs_context_init() hook at all. Pre superblock creation we have a hook during parameter parsing for LSMs and another one during actual superblock creation in vfs_get_tree() and yet another one for fs_context duplicaton. We don't need another generic one during fs_context allocation.

Yes, we may need a hook for submount allocation but then we'll add one exactly for that. And then for fs_context_for_submount @sb can't be empty so there's also no point in checking whether it is empty because you've already crashed in fs_context_for_submount(). All the checks below for !reference and fc->purpose != FS_CONTEXT_FOR_SUBMOUNT can go away then as well.

Quoted 7 lines
Quoted 2 lines

So we end up with something easier and stricter.

I'm not sure I understand what you're asking for here. fc->security field initialization is dependent on the LSM in use. Where will we do this initialization if we don't add this new hook? Are you suggesting that we fold this into an existing LSM hook? If so, which one?

I'm sorry, I didn't mean to confuse. I just mean the following:
* renaming security_fs_context_init() to security_fs_context_submount() to reflect that it is only called during submount allocation now
* Now that security_fs_context_submount() is called in fs_context_for_submount() you can remove all the checks for @reference and fc->purpose == FS_CONTEXT_FOR_SUBMOUNT checks because they can't be unset otherwise you would've a bug in fs_context_for_submount().

These two things were missing after you did move the LSM call into fs_context_for_submount(). That's basically it. So folded into what you sent out this amounts to:

 fs/fs_context.c               | 23 +++++++++++++++-
 include/linux/lsm_hook_defs.h |  1 +
 include/linux/security.h      |  6 +++++
 security/security.c           | 14 ++++++++++
 security/selinux/hooks.c      | 22 +++++++++++++++
 security/smack/smack_lsm.c    | 51 +++++++++++++++++++++++++++++++++++
 6 files changed, 116 insertions(+), 1 deletion(-)
fs/fs_context.c +22 -1
index 851214d1d013..375023e40161 100644
@@ -315,10 +315,31 @@ struct fs_context *fs_context_for_reconfigure(struct dentry *dentry,
 }
 EXPORT_SYMBOL(fs_context_for_reconfigure);
 
+/**
+ * fs_context_for_submount: allocate a new fs_context for a submount
+ * @type: file_system_type of the new context
+ * @reference: reference dentry from which to copy relevant info
+ *
+ * Allocate a new fs_context suitable for a submount. This also ensures that
+ * the fc->security object is inherited from @reference (if needed).
+ */
 struct fs_context *fs_context_for_submount(struct file_system_type *type,
 					   struct dentry *reference)
 {
-	return alloc_fs_context(type, reference, 0, 0, FS_CONTEXT_FOR_SUBMOUNT);
+	struct fs_context *fc;
+	int ret;
+
+	fc = alloc_fs_context(type, reference, 0, 0, FS_CONTEXT_FOR_SUBMOUNT);
+	if (IS_ERR(fc))
+		return fc;
+
+	ret = security_fs_context_submount(fc, reference->d_sb);
+	if (ret) {
+		put_fs_context(fc);
+		return ERR_PTR(ret);
+	}
+
+	return fc;
 }
 EXPORT_SYMBOL(fs_context_for_submount);
 
include/linux/lsm_hook_defs.h +1 -0
index 7308a1a7599b..2876dd6114c0 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 super_block *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..02b41f4d05d1 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_submount(struct fs_context *fc, struct super_block *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_submount(struct fs_context *fc,
+					       struct super_block *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..3611ebdf31d9 100644
@@ -1138,6 +1138,20 @@ void security_bprm_committed_creds(struct linux_binprm *bprm)
 	call_void_hook(bprm_committed_creds, bprm);
 }
 
+/**
+ * security_fs_context_submount() - Initialise fc->security
+ * @fc: new filesystem context
+ * @reference: 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_submount(struct fs_context *fc, struct super_block *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 +22 -0
index d06e350fedee..8b950274138b 100644
@@ -2745,6 +2745,27 @@ static int selinux_umount(struct vfsmount *mnt, int flags)
 				   FILESYSTEM__UNMOUNT, NULL);
 }
 
+static int selinux_fs_context_submount(struct fs_context *fc,
+				       struct super_block *reference)
+{
+	const struct superblock_security_struct *sbsec;
+	struct selinux_mnt_opts *opts;
+
+	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
+	if (!opts)
+		return -ENOMEM;
+
+	sbsec = selinux_superblock(reference);
+	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 +7203,7 @@ static struct security_hook_list selinux_hooks[] __ro_after_init = {
 	/*
 	 * PUT "CLONING" (ACCESSING + ALLOCATING) HOOKS HERE
 	 */
+	LSM_HOOK_INIT(fs_context_submount, selinux_fs_context_submount),
 	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 +51 -0

63 lines not on this page · raw message

Signature · 1 line
2.34.1
author maintainer
#4 lore raw
Quoted 169 lines · Christian Brauner
Quoted 152 lines · Jeff Layton
Quoted 151 lines · Christian Brauner
Quoted 127 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.

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

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

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

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

ver #6)
- Rebase onto v6.5.0-rc4
- Link to v6: https://lore.kernel.org/r/[email protected]

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

ver #8)
- Remove spurious semicolon in smack_fs_context_init
- Make fs_context_init take a superblock as reference instead of dentry
- WARN_ON_ONCE's when fc->purpose != FS_CONTEXT_FOR_SUBMOUNT
- Call the security hook from fs_context_for_submount instead of alloc_fs_context

---
 fs/fs_context.c               | 23 +++++++++++++++++-
 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, 122 insertions(+), 1 deletion(-)
fs/fs_context.c +22 -1
index 851214d1d013..a76d7c82e091 100644
@@ -315,10 +315,31 @@ struct fs_context *fs_context_for_reconfigure(struct dentry *dentry,
 }
 EXPORT_SYMBOL(fs_context_for_reconfigure);
 
+/**
+ * fs_context_for_submount: allocate a new fs_context for a submount
+ * @type: file_system_type of the new context
+ * @reference: reference dentry from which to copy relevant info
+ *
+ * Allocate a new fs_context suitable for a submount. This also ensures that
+ * the fc->security object is inherited from @reference (if needed).
+ */
 struct fs_context *fs_context_for_submount(struct file_system_type *type,
 					   struct dentry *reference)
 {
-	return alloc_fs_context(type, reference, 0, 0, FS_CONTEXT_FOR_SUBMOUNT);
+	struct fs_context *fc;
+	int ret;
+
+	fc = alloc_fs_context(type, reference, 0, 0, FS_CONTEXT_FOR_SUBMOUNT);
+	if (IS_ERR(fc))
+		return fc;
+
+	ret = security_fs_context_init(fc, reference->d_sb);
+	if (ret) {
+		put_fs_context(fc);
+		return ERR_PTR(ret);
+	}
+
+	return fc;
 }
 EXPORT_SYMBOL(fs_context_for_submount);
 
include/linux/lsm_hook_defs.h +1 -0
index 7308a1a7599b..2876dd6114c0 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 super_block *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..fe9bf5e805ee 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 super_block *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 637

Quoted 8 lines
include/linux/security.h +5 -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 super_block *reference)
+{
+	return 0;
+}

Sorry, my point is we shouldn't be adding a generic security_fs_context_init() hook at all. Pre superblock creation we have a hook during parameter parsing for LSMs and another one during actual superblock creation in vfs_get_tree() and yet another one for fs_context duplicaton. We don't need another generic one during fs_context allocation.

Yes, we may need a hook for submount allocation but then we'll add one exactly for that. And then for fs_context_for_submount @sb can't be empty so there's also no point in checking whether it is empty because you've already crashed in fs_context_for_submount(). All the checks below for !reference and fc->purpose != FS_CONTEXT_FOR_SUBMOUNT can go away then as well.

Quoted 7 lines
Quoted 2 lines

So we end up with something easier and stricter.

I'm not sure I understand what you're asking for here. fc->security field initialization is dependent on the LSM in use. Where will we do this initialization if we don't add this new hook? Are you suggesting that we fold this into an existing LSM hook? If so, which one?

I'm sorry, I didn't mean to confuse. I just mean the following:
* renaming security_fs_context_init() to security_fs_context_submount() to reflect that it is only called during submount allocation now
* Now that security_fs_context_submount() is called in fs_context_for_submount() you can remove all the checks for @reference and fc->purpose == FS_CONTEXT_FOR_SUBMOUNT checks because they can't be unset otherwise you would've a bug in fs_context_for_submount().

Quoted 4 lines

These two things were missing after you did move the LSM call into fs_context_for_submount(). That's basically it. So folded into what you sent out this amounts to:

Thanks, I'll fold those changes into this patch and resend. There's also a bug that the kernel test robot spotted in SMACK code that I'll fix as well. I'll send v9 after I've had a chance to retest the changes.

Quoted 215 lines
 fs/fs_context.c               | 23 +++++++++++++++-
 include/linux/lsm_hook_defs.h |  1 +
 include/linux/security.h      |  6 +++++
 security/security.c           | 14 ++++++++++
 security/selinux/hooks.c      | 22 +++++++++++++++
 security/smack/smack_lsm.c    | 51 +++++++++++++++++++++++++++++++++++
 6 files changed, 116 insertions(+), 1 deletion(-)
fs/fs_context.c +22 -1
index 851214d1d013..375023e40161 100644
@@ -315,10 +315,31 @@ struct fs_context *fs_context_for_reconfigure(struct dentry *dentry,
 }
 EXPORT_SYMBOL(fs_context_for_reconfigure);
 
+/**
+ * fs_context_for_submount: allocate a new fs_context for a submount
+ * @type: file_system_type of the new context
+ * @reference: reference dentry from which to copy relevant info
+ *
+ * Allocate a new fs_context suitable for a submount. This also ensures that
+ * the fc->security object is inherited from @reference (if needed).
+ */
 struct fs_context *fs_context_for_submount(struct file_system_type *type,
 					   struct dentry *reference)
 {
-	return alloc_fs_context(type, reference, 0, 0, FS_CONTEXT_FOR_SUBMOUNT);
+	struct fs_context *fc;
+	int ret;
+
+	fc = alloc_fs_context(type, reference, 0, 0, FS_CONTEXT_FOR_SUBMOUNT);
+	if (IS_ERR(fc))
+		return fc;
+
+	ret = security_fs_context_submount(fc, reference->d_sb);
+	if (ret) {
+		put_fs_context(fc);
+		return ERR_PTR(ret);
+	}
+
+	return fc;
 }
 EXPORT_SYMBOL(fs_context_for_submount);
 
include/linux/lsm_hook_defs.h +1 -0
index 7308a1a7599b..2876dd6114c0 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 super_block *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..02b41f4d05d1 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_submount(struct fs_context *fc, struct super_block *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_submount(struct fs_context *fc,
+					       struct super_block *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..3611ebdf31d9 100644
@@ -1138,6 +1138,20 @@ void security_bprm_committed_creds(struct linux_binprm *bprm)
 	call_void_hook(bprm_committed_creds, bprm);
 }
 
+/**
+ * security_fs_context_submount() - Initialise fc->security
+ * @fc: new filesystem context
+ * @reference: 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_submount(struct fs_context *fc, struct super_block *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 +22 -0
index d06e350fedee..8b950274138b 100644
@@ -2745,6 +2745,27 @@ static int selinux_umount(struct vfsmount *mnt, int flags)
 				   FILESYSTEM__UNMOUNT, NULL);
 }
 
+static int selinux_fs_context_submount(struct fs_context *fc,
+				       struct super_block *reference)
+{
+	const struct superblock_security_struct *sbsec;
+	struct selinux_mnt_opts *opts;
+
+	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
+	if (!opts)
+		return -ENOMEM;
+
+	sbsec = selinux_superblock(reference);
+	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 +7203,7 @@ static struct security_hook_list selinux_hooks[] __ro_after_init = {
 	/*
 	 * PUT "CLONING" (ACCESSING + ALLOCATING) HOOKS HERE
 	 */
+	LSM_HOOK_INIT(fs_context_submount, selinux_fs_context_submount),
 	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 +51 -0
index 6e270cf3fd30..87f7e0506f4d 100644
@@ -614,6 +614,56 @@ static int smack_add_opt(int token, const char *s, void **mnt_opts)
 	return -EINVAL;
 }
 
+/**
+ * smack_fs_context_submount - Initialise security data for a filesystem context
+ * @fc: The filesystem context.
+ * @reference: Reference superblock (automount/reconfigure) or NULL
+ *
+ * Returns 0 on success or -ENOMEM on error.
+ */
+static int smack_fs_context_submount(struct fs_context *fc,
+				     struct dentry *reference)
+{
+	struct superblock_smack *sbsp;
+	struct smack_mnt_opts *ctx;
+	struct inode_smack *isp;
+
+	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
+	if (!ctx)
+		return -ENOMEM;
+	fc->security = ctx;
+
+	sbsp = smack_superblock(reference);
+	isp = smack_inode(reference->s_root->d_inode);
+
+	if (sbsp->smk_default) {
+		ctx->fsdefault = kstrdup(sbsp->smk_default->smk_known, GFP_KERNEL);
+		if (!ctx->fsdefault)
+			return -ENOMEM;
+	}
+
+	if (sbsp->smk_floor) {
+		ctx->fsfloor = kstrdup(sbsp->smk_floor->smk_known, GFP_KERNEL);
+		if (!ctx->fsfloor)
+			return -ENOMEM;
+	}
+
+	if (sbsp->smk_hat) {
+		ctx->fshat = kstrdup(sbsp->smk_hat->smk_known, GFP_KERNEL);
+		if (!ctx->fshat)
+			return -ENOMEM;
+	}
+
+	if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
+		if (sbsp->smk_root) {
+			ctx->fstransmute = kstrdup(sbsp->smk_root->smk_known, GFP_KERNEL);
+			if (!ctx->fstransmute)
+				return -ENOMEM;
+		}
+	}
+	return 0;
+}
+
 /**
  * smack_fs_context_dup - Duplicate the security data on fs_context duplication
  * @fc: The new filesystem context.
@@ -4876,6 +4926,7 @@ static struct security_hook_list smack_hooks[] __ro_after_init = {
 	LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
 	LSM_HOOK_INIT(syslog, smack_syslog),
 
+	LSM_HOOK_INIT(fs_context_submount, smack_fs_context_submount),
 	LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup),
 	LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),
 
Signature · 1 line
Jeff Layton <[email protected]>
#5 lore raw
In reply to Jeff Layton
kernel test robot noticed the following build errors:

Hi Jeff,

kernel test robot noticed the following build errors:

[auto build test ERROR on 52a93d39b17dc7eb98b6aa3edb93943248e03b2f]

url:    https://github.com/intel-lab-lkp/linux/commits/Jeff-Layton/vfs-security-Fix-automount-superblock-LSM-init-problem-preventing-NFS-sb-sharing/20230807-212036
base:   52a93d39b17dc7eb98b6aa3edb93943248e03b2f
patch link:    https://lore.kernel.org/r/20230807-master-v8-1-54e249595f10%40kernel.org
patch subject: [PATCH v8] vfs, security: Fix automount superblock LSM init problem, preventing NFS sb sharing
config: i386-randconfig-i015-20230807 (https://download.01.org/0day-ci/archive/20230808/[email protected]/config)
compiler: clang version 16.0.4 (https://github.com/llvm/llvm-project.git ae42196bc493ffe877a7e3dff8be32035dea4d07)
reproduce: (https://download.01.org/0day-ci/archive/20230808/[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 errors (new ones prefixed by >>):

>> security/smack/smack_lsm.c:639:26: error: incompatible pointer types passing 'struct dentry *' to parameter of type 'const struct super_block *' [-Werror,-Wincompatible-pointer-types]
           sbsp = smack_superblock(reference);
                                   ^~~~~~~~~
   security/smack/smack.h:353:32: note: passing argument to parameter 'superblock' here
                                           const struct super_block *superblock)
                                                                     ^
>> security/smack/smack_lsm.c:640:31: error: no member named 's_root' in 'struct dentry'
           isp = smack_inode(reference->s_root->d_inode);
                             ~~~~~~~~~  ^
>> security/smack/smack_lsm.c:4932:33: error: incompatible function pointer types initializing 'int (*)(struct fs_context *, struct super_block *)' with an expression of type 'int (struct fs_context *, struct dentry *)' [-Wincompatible-function-pointer-types]
           LSM_HOOK_INIT(fs_context_init, smack_fs_context_init),
                                          ^~~~~~~~~~~~~~~~~~~~~
   include/linux/lsm_hooks.h:81:57: note: expanded from macro 'LSM_HOOK_INIT'
           { .head = &security_hook_heads.HEAD, .hook = { .HEAD = HOOK } }
                                                                  ^~~~

3 errors generated.

vim +639 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 superblock (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 || WARN_ON_ONCE(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);
 > 640		isp = smack_inode(reference->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