Skip to contentSkip to conversation

LSM: Multiple LSM mount options

PATCH In review Casey Schaufler 3 participants 6 messages Sep 25, 2025 – Nov 4, 2025 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Casey Schaufler

author maintainer
#1 lore raw

v2 cover LSM: Multiple LSM mount options

Linux Security Module (LSM) that support mount options, currently SELinux and Smack, allocate their own data for those options. This patch set moves the handling of mount option data out of the individual LSMs and into the LSM infrastructure. This allows for multiple LSMs to support mount options at the same time.

https://github.com/cschaufler/lsm-stacking#mount-opts-6.17-rc6-v2

v2: Significant rewrite to move allocations out of the LSMs.

Casey Schaufler (2):
LSM: Add mount opts blob size tracking
LSM: Infrastructure management of the mnt_opts security blob

 include/linux/lsm_hooks.h         |  1 +
 security/security.c               | 44 ++++++++++++++++++
 security/selinux/hooks.c          | 76 ++++++++++---------------------
 security/selinux/include/objsec.h |  8 ++++
 security/smack/smack.h            |  8 ++++
 security/smack/smack_lsm.c        | 45 +++++-------------
 6 files changed, 97 insertions(+), 85 deletions(-)
Signature · 1 line
2.51.0
author maintainer
#2 lore raw

PATCH v2 1/2 LSM: Add mount opts blob size tracking

Add mount option data to the blob size accounting in anticipation of using a shared mnt_opts blob.

---
 include/linux/lsm_hooks.h  | 1 +
 security/security.c        | 2 ++
 security/selinux/hooks.c   | 1 +
 security/smack/smack_lsm.c | 1 +
 4 files changed, 5 insertions(+)
include/linux/lsm_hooks.h +1 -0
index 090d1d3e19fe..0de143d7c094 100644
@@ -116,6 +116,7 @@ struct lsm_blob_sizes {
 	int lbs_xattr_count; /* number of xattr slots in new_xattrs array */
 	int lbs_tun_dev;
 	int lbs_bdev;
+	int lbs_mnt_opts;
 };
 
 /*
security/security.c +2 -0
index ad163f06bf7a..8390410aec91 100644
@@ -283,6 +283,7 @@ static void __init lsm_set_blob_sizes(struct lsm_blob_sizes *needed)
 	lsm_set_blob_size(&needed->lbs_xattr_count,
 			  &blob_sizes.lbs_xattr_count);
 	lsm_set_blob_size(&needed->lbs_bdev, &blob_sizes.lbs_bdev);
+	lsm_set_blob_size(&needed->lbs_mnt_opts, &blob_sizes.lbs_mnt_opts);
 }
 
 /* Prepare LSM for initialization. */
@@ -480,6 +481,7 @@ static void __init ordered_lsm_init(void)
 	init_debug("tun device blob size = %d\n", blob_sizes.lbs_tun_dev);
 	init_debug("xattr slots          = %d\n", blob_sizes.lbs_xattr_count);
 	init_debug("bdev blob size       = %d\n", blob_sizes.lbs_bdev);
+	init_debug("mnt_opts blob size   = %d\n", blob_sizes.lbs_mnt_opts);
 
 	/*
 	 * Create any kmem_caches needed for blobs
security/selinux/hooks.c +1 -0
index c95a5874bf7d..4bba9d119713 100644
@@ -7183,6 +7183,7 @@ struct lsm_blob_sizes selinux_blob_sizes __ro_after_init = {
 	.lbs_xattr_count = SELINUX_INODE_INIT_XATTRS,
 	.lbs_tun_dev = sizeof(struct tun_security_struct),
 	.lbs_ib = sizeof(struct ib_security_struct),
+	.lbs_mnt_opts = sizeof(struct selinux_mnt_opts),
 };
 
 #ifdef CONFIG_PERF_EVENTS
security/smack/smack_lsm.c +1 -0
index fc340a6f0dde..1f236014e7d8 100644
@@ -5030,6 +5030,7 @@ struct lsm_blob_sizes smack_blob_sizes __ro_after_init = {
 	.lbs_sock = sizeof(struct socket_smack),
 	.lbs_superblock = sizeof(struct superblock_smack),
 	.lbs_xattr_count = SMACK_INODE_INIT_XATTRS,
+	.lbs_mnt_opts = sizeof(struct smack_mnt_opts),
 };
 
 static const struct lsm_id smack_lsmid = {
Signature · 1 line
2.51.0
author maintainer
#3 lore raw

PATCH v2 2/2 LSM: Infrastructure management of the mnt_opts security blob

In reply to Casey Schaufler

Move management of the mnt_opts->security blob out of the individual security modules and into the security infrastructure. The modules tell the infrastructure how much space is required, and the space is allocated as required in the interfaces that use the blob.

---
 security/security.c               | 42 +++++++++++++++++
 security/selinux/hooks.c          | 75 ++++++++++---------------------
 security/selinux/include/objsec.h |  8 ++++
 security/smack/smack.h            |  8 ++++
 security/smack/smack_lsm.c        | 44 +++++-------------
 5 files changed, 92 insertions(+), 85 deletions(-)
security/security.c +42 -0
index 8390410aec91..b16c0843dafa 100644
@@ -29,6 +29,7 @@
 #include <linux/overflow.h>
 #include <linux/perf_event.h>
 #include <linux/fs.h>
+#include <linux/fs_context.h>
 #include <net/flow.h>
 #include <net/sock.h>
 
@@ -1337,6 +1338,19 @@ void security_bprm_committed_creds(const struct linux_binprm *bprm)
 	call_void_hook(bprm_committed_creds, bprm);
 }
 
+/**
+ * lsm_mnt_opts_alloc - allocate a mnt_opts blob
+ * @opts: pointer to options
+ *
+ * Allocate a mount options blob.
+ *
+ * Returns 0, or -ENOMEM if memory isn't available.
+ */
+static int lsm_mnt_opts_alloc(void **opts)
+{
+	return lsm_blob_alloc(opts, blob_sizes.lbs_mnt_opts, GFP_KERNEL);
+}
+
 /**
  * security_fs_context_submount() - Initialise fc->security
  * @fc: new filesystem context
@@ -1348,6 +1362,13 @@ void security_bprm_committed_creds(const struct linux_binprm *bprm)
  */
 int security_fs_context_submount(struct fs_context *fc, struct super_block *reference)
 {
+	int rc;
+
+	if (!fc->security) {
+		rc = lsm_mnt_opts_alloc(&fc->security);
+		if (rc)
+			return rc;
+	}
 	return call_int_hook(fs_context_submount, fc, reference);
 }
 
@@ -1364,6 +1385,13 @@ int security_fs_context_submount(struct fs_context *fc, struct super_block *refe
  */
 int security_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc)
 {
+	int rc;
+
+	if (!fc->security) {
+		rc = lsm_mnt_opts_alloc(&fc->security);
+		if (rc)
+			return rc;
+	}
 	return call_int_hook(fs_context_dup, fc, src_fc);
 }
 
@@ -1386,6 +1414,12 @@ int security_fs_context_parse_param(struct fs_context *fc,
 	int trc;
 	int rc = -ENOPARAM;
 
+	if (!fc->security) {
+		trc = lsm_mnt_opts_alloc(&fc->security);
+		if (trc)
+			return trc;
+	}
+
 	lsm_for_each_hook(scall, fs_context_parse_param) {
 		trc = scall->hl->hook.fs_context_parse_param(fc, param);
 		if (trc == 0)
@@ -1455,6 +1489,7 @@ void security_free_mnt_opts(void **mnt_opts)
 	if (!*mnt_opts)
 		return;
 	call_void_hook(sb_free_mnt_opts, *mnt_opts);
+	kfree(*mnt_opts);
 	*mnt_opts = NULL;
 }
 EXPORT_SYMBOL(security_free_mnt_opts);
@@ -1470,6 +1505,13 @@ EXPORT_SYMBOL(security_free_mnt_opts);
  */
 int security_sb_eat_lsm_opts(char *options, void **mnt_opts)
 {
+	int rc;
+
+	if (!*mnt_opts) {
+		rc = lsm_mnt_opts_alloc(mnt_opts);
+		if (rc)
+			return rc;
+	}
 	return call_int_hook(sb_eat_lsm_opts, options, mnt_opts);
 }
 EXPORT_SYMBOL(security_sb_eat_lsm_opts);
security/selinux/hooks.c +23 -52

169 lines not on this page · raw message

security/selinux/include/objsec.h +8 -0
index 1d7ac59015a1..cefc6c550f74 100644
@@ -245,4 +245,12 @@ selinux_perf_event(void *perf_event)
 	return perf_event + selinux_blob_sizes.lbs_perf_event;
 }
 
+static inline struct selinux_mnt_opts *selinux_mnt_opts(void *opts)
+{
+	if (!opts)
+		return NULL;
+
+	return opts + selinux_blob_sizes.lbs_mnt_opts;
+}
+
 #endif /* _SELINUX_OBJSEC_H_ */
security/smack/smack.h +8 -0
index bf6a6ed3946c..828c913dd62d 100644
@@ -367,6 +367,14 @@ static inline struct socket_smack *smack_sock(const struct sock *sock)
 	return sock->sk_security + smack_blob_sizes.lbs_sock;
 }
 
+static inline struct smack_mnt_opts *smack_mnt_opts(void *opts)
+{
+	if (!opts)
+		return NULL;
+
+	return opts + smack_blob_sizes.lbs_mnt_opts;
+}
+
 #ifdef CONFIG_KEYS
 static inline struct smack_known **smack_key(const struct key *key)
 {
security/smack/smack_lsm.c +11 -33

91 lines not on this page · raw message

Signature · 1 line
2.51.0
maintainer
#4 lore raw

Re: [PATCH v2 2/2] LSM: Infrastructure management of the mnt_opts security blob

Quoted 22 lines · Casey Schaufler

Move management of the mnt_opts->security blob out of the individual security modules and into the security infrastructure. The modules tell the infrastructure how much space is required, and the space is allocated as required in the interfaces that use the blob.

---

security/selinux/hooks.c +0 -5
index 4bba9d119713..1ccf880e4894 100644
@@ -656,19 +651,13 @@ static int selinux_set_mnt_opts(struct super_block *sb,
        mutex_lock(&sbsec->lock);
 
        if (!selinux_initialized()) {
-               if (!opts) {
-                       /* Defer initialization until selinux_complete_init,
-                          after the initial policy is loaded and the security
-                          server is ready to handle calls. */
-                       if (kern_flags & SECURITY_LSM_NATIVE_LABELS) {

security/selinux/hooks.c static int selinux_set_mnt_opts(struct super_block *sb, · line 659

Quoted 10 lines
security/selinux/hooks.c +6 -4
-                               sbsec->flags |= SE_SBNATIVE;
-                               *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
-                       }
-                       goto out;
+               /* Defer initialization until selinux_complete_init,
+                  after the initial policy is loaded and the security
+                  server is ready to handle calls. */
+               if (kern_flags & SECURITY_LSM_NATIVE_LABELS) {
+                       sbsec->flags |= SE_SBNATIVE;
+                       *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;

This seemingly would produce a change in behavior for SELinux. Previously we would only do this if there were no SELinux mount options specified.

security/selinux/hooks.c static int selinux_set_mnt_opts(struct super_block *sb, · line 671

Quoted 4 lines
security/selinux/hooks.c +0 -3
                }
-               rc = -EINVAL;
-               pr_warn("SELinux: Unable to set superblock options "
-                       "before the security server is initialized\n");

Ditto.

Quoted 4 lines
security/selinux/hooks.c +0 -0
                goto out;
        }
 
maintainer
#5 lore raw

Re: [PATCH v2 2/2] LSM: Infrastructure management of the mnt_opts security blob

Quoted 29 lines · Stephen Smalley
Quoted 28 lines · Casey Schaufler

Move management of the mnt_opts->security blob out of the individual security modules and into the security infrastructure. The modules tell the infrastructure how much space is required, and the space is allocated as required in the interfaces that use the blob.

---

security/selinux/hooks.c +2 -9
index 4bba9d119713..1ccf880e4894 100644
@@ -656,19 +651,13 @@ static int selinux_set_mnt_opts(struct super_block *sb,
        mutex_lock(&sbsec->lock);
 
        if (!selinux_initialized()) {
-               if (!opts) {
-                       /* Defer initialization until selinux_complete_init,
-                          after the initial policy is loaded and the security
-                          server is ready to handle calls. */
-                       if (kern_flags & SECURITY_LSM_NATIVE_LABELS) {
-                               sbsec->flags |= SE_SBNATIVE;
-                               *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
-                       }
-                       goto out;
+               /* Defer initialization until selinux_complete_init,
+                  after the initial policy is loaded and the security
Quoted 8 lines

security/selinux/hooks.c static int selinux_set_mnt_opts(struct super_block *sb, · line 659

Quoted 4 lines
security/selinux/hooks.c +4 -0
+                  server is ready to handle calls. */
+               if (kern_flags & SECURITY_LSM_NATIVE_LABELS) {
+                       sbsec->flags |= SE_SBNATIVE;
+                       *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;

This seemingly would produce a change in behavior for SELinux. Previously we would only do this if there were no SELinux mount options specified.

What Stephen said. I think this is good work that needs to be done (thank you for doing it!), but we have to preserve existing behaviors.

Signature · 1 line
paul-moore.com
author maintainer
#6 lore raw

Re: [PATCH v2 2/2] LSM: Infrastructure management of the mnt_opts security blob

Quoted 26 lines · Paul Moore
Quoted 24 lines · Stephen Smalley
Quoted 23 lines · Casey Schaufler

Move management of the mnt_opts->security blob out of the individual security modules and into the security infrastructure. The modules tell the infrastructure how much space is required, and the space is allocated as required in the interfaces that use the blob.

---

security/selinux/hooks.c +0 -8
index 4bba9d119713..1ccf880e4894 100644
@@ -656,19 +651,13 @@ static int selinux_set_mnt_opts(struct super_block *sb,
        mutex_lock(&sbsec->lock);
 
        if (!selinux_initialized()) {
-               if (!opts) {
-                       /* Defer initialization until selinux_complete_init,
-                          after the initial policy is loaded and the security
-                          server is ready to handle calls. */
-                       if (kern_flags & SECURITY_LSM_NATIVE_LABELS) {
-                               sbsec->flags |= SE_SBNATIVE;
-                               *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
-                       }
Quoted 8 lines
Quoted 8 lines

security/selinux/hooks.c static int selinux_set_mnt_opts(struct super_block *sb, · line 659

Quoted 7 lines
security/selinux/hooks.c +6 -1
-                       goto out;
+               /* Defer initialization until selinux_complete_init,
+                  after the initial policy is loaded and the security
+                  server is ready to handle calls. */
+               if (kern_flags & SECURITY_LSM_NATIVE_LABELS) {
+                       sbsec->flags |= SE_SBNATIVE;
+                       *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;

This seemingly would produce a change in behavior for SELinux.

Except that it doesn't, at least from the tests I've been able to find. If multiple LSMs use mount options you can't use the !opts test, because there may be options for another LSM. Deferring initialization is harmless when there are options, as it's all checked again later.

Quoted 5 lines
Quoted 2 lines

Previously we would only do this if there were no SELinux mount options specified.

What Stephen said. I think this is good work that needs to be done (thank you for doing it!), but we have to preserve existing behaviors.