Skip to contentSkip to conversation

LSM: Hook registration exculsivity

RFC PATCH In review Casey Schaufler 2 participants 11 messages Feb 25 – Apr 25 1 message not in the archive

Subsystems (7)
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

cover LSM: Hook registration exculsivity

This patch set represents a reasonably radical change to the LSM stacking model. The notion of an "exclusive" LSM begins to give way to exclusive LSM hooks. Instead of disallowing more than one exclusive LSM, individual hooks are marked as exclusive and only one is allowed to be registered. Subsequent LSMs that attempt to register these hooks are denied them. This can have consequences. The hooks relative to secmarks have been used here to demonstrate that the scheme can work, and that it isn't always as clean as one might like.

Please comment fully. Quite a bit of tinkering went into devising this approach, which is intended to address a number of concerns.

Add a flags field to the LSM hook definition.

The first LSM that requests a hook with the LSM_FLAG_EXCLUSIVE flag will be the only LSM that can register any hook thus marked. Attempts by other LSMs to register such hooks are ignored.

Direct access to skb->secmark within LSMs are wrapped with a helper function lsm_secmark_from_skb(). This function checks to see if the secmark related LSM hooks, which are marked LSM_FLAG_EXCLUSIVE, are registered by the calling LSM. If they are, the secmark value is returned. Otherwise, the invalid secmark value 0 is returned.

Future implementations of lsm_secmark_from_skb() could use some form of secmark encoding that would allow more than one LSM to use secmarks at the same time.

The LSMs that currently support use of secmarks are taught how to identify if they are allowed use of the secmark. Each sets secmark values differently. At initialization the LSMs have the opportunity to take steps to ensure correct behavior if they don't have secmark access.

https://github.com/cschaufler/lsm-stacking#secmark-6.19-rc8-v1

Casey Schaufler (3):
LSM: add a flags field to the LSM hook definitions
LSM: Enforce exclusive hooks
LSM: Reserve use of secmarks

 include/linux/bpf_lsm.h          |   2 +-
 include/linux/lsm_hook_defs.h    | 614 ++++++++++++++++---------------
 include/linux/lsm_hooks.h        |   4 +-
 include/linux/security.h         |   3 +
 kernel/bpf/bpf_lsm.c             |  10 +-
 security/apparmor/lsm.c          |  24 +-
 security/bpf/hooks.c             |   2 +-
 security/lsm_init.c              |  66 ++++
 security/security.c              |  21 +-
 security/selinux/hooks.c         |  35 +-
 security/selinux/ss/services.c   |   3 +
 security/smack/smack_lsm.c       |   6 +-
 security/smack/smack_netfilter.c |   6 +
 13 files changed, 473 insertions(+), 323 deletions(-)
Signature · 1 line
2.52.0
author maintainer
#2 lore raw

RFC PATCH 1/3 LSM: add a flags field to the LSM hook definitions

Add a field for flags to the definition of LSM hooks. This allows for hooks to be identified at system initialization for special processing.

---
 include/linux/bpf_lsm.h       |   2 +-
 include/linux/lsm_hook_defs.h | 614 ++++++++++++++++++----------------
 include/linux/lsm_hooks.h     |   4 +-
 kernel/bpf/bpf_lsm.c          |  10 +-
 security/bpf/hooks.c          |   2 +-
 security/security.c           |   6 +-
 6 files changed, 331 insertions(+), 307 deletions(-)
include/linux/bpf_lsm.h +1 -1
index 643809cc78c3..d71ba8c87e79 100644
@@ -14,7 +14,7 @@
 
 #ifdef CONFIG_BPF_LSM
 
-#define LSM_HOOK(RET, DEFAULT, NAME, ...) \
+#define LSM_HOOK(RET, DEFAULT, FLAGS, NAME, ...) \
 	RET bpf_lsm_##NAME(__VA_ARGS__);
 #include <linux/lsm_hook_defs.h>
 #undef LSM_HOOK
include/linux/lsm_hook_defs.h +319 -295

770 lines not on this page · raw message

include/linux/lsm_hooks.h +2 -2
index b92008641242..4411b47f9a8d 100644
@@ -36,7 +36,7 @@
 #include <linux/lsm_count.h>
 
 union security_list_options {
-	#define LSM_HOOK(RET, DEFAULT, NAME, ...) RET (*NAME)(__VA_ARGS__);
+	#define LSM_HOOK(RET, DEFAULT, FLAGS, NAME, ...) RET (*NAME)(__VA_ARGS__);
 	#include "lsm_hook_defs.h"
 	#undef LSM_HOOK
 	void *lsm_func_addr;
@@ -65,7 +65,7 @@ struct lsm_static_call {
  * dynamic to adapt the number of static calls to the number of callbacks.
  */
 struct lsm_static_calls_table {
-	#define LSM_HOOK(RET, DEFAULT, NAME, ...) \
+	#define LSM_HOOK(RET, DEFAULT, FLAGS, NAME, ...) \
 		struct lsm_static_call NAME[MAX_LSM_COUNT];
 	#include <linux/lsm_hook_defs.h>
 	#undef LSM_HOOK
kernel/bpf/bpf_lsm.c +5 -5
index 7cb6e8d4282c..53d5c0b69b2c 100644
@@ -20,16 +20,16 @@
 /* For every LSM hook that allows attachment of BPF programs, declare a nop
  * function where a BPF program can be attached.
  */
-#define LSM_HOOK(RET, DEFAULT, NAME, ...)	\
-noinline RET bpf_lsm_##NAME(__VA_ARGS__)	\
-{						\
-	return DEFAULT;				\
+#define LSM_HOOK(RET, DEFAULT, FLAGS, NAME, ...)	\
+noinline RET bpf_lsm_##NAME(__VA_ARGS__)		\
+{							\
+	return DEFAULT;					\
 }
 
 #include <linux/lsm_hook_defs.h>
 #undef LSM_HOOK
 
-#define LSM_HOOK(RET, DEFAULT, NAME, ...) BTF_ID(func, bpf_lsm_##NAME)
+#define LSM_HOOK(RET, DEFAULT, FLAGS, NAME, ...) BTF_ID(func, bpf_lsm_##NAME)
 BTF_SET_START(bpf_lsm_hooks)
 #include <linux/lsm_hook_defs.h>
 #undef LSM_HOOK
security/bpf/hooks.c +1 -1
index 40efde233f3a..8c4b0ed2b22e 100644
@@ -8,7 +8,7 @@
 #include <uapi/linux/lsm.h>
 
 static struct security_hook_list bpf_lsm_hooks[] __ro_after_init = {
-	#define LSM_HOOK(RET, DEFAULT, NAME, ...) \
+	#define LSM_HOOK(RET, DEFAULT, FLAGS, NAME, ...) \
 	LSM_HOOK_INIT(NAME, bpf_lsm_##NAME),
 	#include <linux/lsm_hook_defs.h>
 	#undef LSM_HOOK
security/security.c +3 -3
index 31a688650601..25e7cfc96f20 100644
@@ -117,7 +117,7 @@ do {						\
 				*((RET(*)(__VA_ARGS__))NULL));		\
 	DEFINE_STATIC_KEY_FALSE(SECURITY_HOOK_ACTIVE_KEY(NAME, NUM));
 
-#define LSM_HOOK(RET, DEFAULT, NAME, ...)				\
+#define LSM_HOOK(RET, DEFAULT, FLAGS, NAME, ...)			\
 	LSM_DEFINE_UNROLL(DEFINE_LSM_STATIC_CALL, NAME, RET, __VA_ARGS__)
 #include <linux/lsm_hook_defs.h>
 #undef LSM_HOOK
@@ -142,7 +142,7 @@ struct lsm_static_calls_table
 		.trampoline = LSM_HOOK_TRAMP(NAME, NUM),		\
 		.active = &SECURITY_HOOK_ACTIVE_KEY(NAME, NUM),		\
 	},
-#define LSM_HOOK(RET, DEFAULT, NAME, ...)				\
+#define LSM_HOOK(RET, DEFAULT, FLAGS, NAME, ...)			\
 	.NAME = {							\
 		LSM_DEFINE_UNROLL(INIT_LSM_STATIC_CALL, NAME)		\
 	},
@@ -422,7 +422,7 @@ int lsm_fill_user_ctx(struct lsm_ctx __user *uctx, u32 *uctx_len,
 #define DECLARE_LSM_RET_DEFAULT_void(DEFAULT, NAME)
 #define DECLARE_LSM_RET_DEFAULT_int(DEFAULT, NAME) \
 	static const int __maybe_unused LSM_RET_DEFAULT(NAME) = (DEFAULT);
-#define LSM_HOOK(RET, DEFAULT, NAME, ...) \
+#define LSM_HOOK(RET, DEFAULT, FLAGS, NAME, ...) \
 	DECLARE_LSM_RET_DEFAULT_##RET(DEFAULT, NAME)
 
 #include <linux/lsm_hook_defs.h>
Signature · 1 line
2.52.0
maintainer
#3 lore raw

Re: [PATCH RFC 1/3] LSM: add a flags field to the LSM hook definitions

Quoted 42 lines · On Feb 25, 2026 Casey Schaufler <[email protected]> wrote:

Add a field for flags to the definition of LSM hooks. This allows for hooks to be identified at system initialization for special processing.

---
 include/linux/bpf_lsm.h       |   2 +-
 include/linux/lsm_hook_defs.h | 614 ++++++++++++++++++----------------
 include/linux/lsm_hooks.h     |   4 +-
 kernel/bpf/bpf_lsm.c          |  10 +-
 security/bpf/hooks.c          |   2 +-
 security/security.c           |   6 +-
 6 files changed, 331 insertions(+), 307 deletions(-)
include/linux/bpf_lsm.h +1 -1
index 643809cc78c3..d71ba8c87e79 100644
@@ -14,7 +14,7 @@
 
 #ifdef CONFIG_BPF_LSM
 
-#define LSM_HOOK(RET, DEFAULT, NAME, ...) \
+#define LSM_HOOK(RET, DEFAULT, FLAGS, NAME, ...) \
 	RET bpf_lsm_##NAME(__VA_ARGS__);
 #include <linux/lsm_hook_defs.h>
 #undef LSM_HOOK
include/linux/lsm_hook_defs.h +2 -2
index 8c42b4bde09c..acda3a02da97 100644
@@ -18,451 +18,475 @@
  * The macro LSM_HOOK is used to define the data structures required by
  * the LSM framework using the pattern:
  *
- *	LSM_HOOK(<return_type>, <default_value>, <hook_name>, args...)
+ *	LSM_HOOK(<return_type>, <default_value>, <flags>, <single>,
+ *		 <hook_name>, args...)
  *
  * struct security_hook_heads {
- *   #define LSM_HOOK(RET, DEFAULT, NAME, ...) struct hlist_head NAME;

include/linux/lsm_hook_defs.h line 32

Quoted 10 lines
include/linux/lsm_hook_defs.h +3 -2
+ *   #define LSM_HOOK(RET, DEFAULT, FLAGS, NAME, ...) struct hlist_head NAME;
  *   #include <linux/lsm_hook_defs.h>
  *   #undef LSM_HOOK
  * };
  */
-LSM_HOOK(int, 0, binder_set_context_mgr, const struct cred *mgr)
-LSM_HOOK(int, 0, binder_transaction, const struct cred *from,
+LSM_HOOK(int, 0, 0, binder_set_context_mgr, const struct cred *mgr)
+LSM_HOOK(int, 0, 0, binder_transaction, const struct cred *from,
 	 const struct cred *to)

I think adding a flag field to the LSM_HOOK() macro/definitions is a good and useful addition, but I'd prefer if we created a LSM_FLAG_NONE #define and used it here just so we could avoid the back-to-back 0's and do a bit of self-documentation.

--
paul-moore.com

author maintainer
#4 lore raw

Re: [PATCH RFC 1/3] LSM: add a flags field to the LSM hook definitions

Quoted 48 lines · Paul Moore
Quoted 47 lines · On Feb 25, 2026 Casey Schaufler <[email protected]> wrote:

Add a field for flags to the definition of LSM hooks. This allows for hooks to be identified at system initialization for special processing.

---
 include/linux/bpf_lsm.h       |   2 +-
 include/linux/lsm_hook_defs.h | 614 ++++++++++++++++++----------------
 include/linux/lsm_hooks.h     |   4 +-
 kernel/bpf/bpf_lsm.c          |  10 +-
 security/bpf/hooks.c          |   2 +-
 security/security.c           |   6 +-
 6 files changed, 331 insertions(+), 307 deletions(-)
include/linux/bpf_lsm.h +1 -1
index 643809cc78c3..d71ba8c87e79 100644
@@ -14,7 +14,7 @@
 
 #ifdef CONFIG_BPF_LSM
 
-#define LSM_HOOK(RET, DEFAULT, NAME, ...) \
+#define LSM_HOOK(RET, DEFAULT, FLAGS, NAME, ...) \
 	RET bpf_lsm_##NAME(__VA_ARGS__);
 #include <linux/lsm_hook_defs.h>
 #undef LSM_HOOK
include/linux/lsm_hook_defs.h +3 -3
index 8c42b4bde09c..acda3a02da97 100644
@@ -18,451 +18,475 @@
  * The macro LSM_HOOK is used to define the data structures required by
  * the LSM framework using the pattern:
  *
- *	LSM_HOOK(<return_type>, <default_value>, <hook_name>, args...)
+ *	LSM_HOOK(<return_type>, <default_value>, <flags>, <single>,
+ *		 <hook_name>, args...)
  *
  * struct security_hook_heads {
- *   #define LSM_HOOK(RET, DEFAULT, NAME, ...) struct hlist_head NAME;
+ *   #define LSM_HOOK(RET, DEFAULT, FLAGS, NAME, ...) struct hlist_head NAME;
  *   #include <linux/lsm_hook_defs.h>
  *   #undef LSM_HOOK
  * };
  */
-LSM_HOOK(int, 0, binder_set_context_mgr, const struct cred *mgr)
Quoted 8 lines

include/linux/lsm_hook_defs.h line 32

Quoted 4 lines
include/linux/lsm_hook_defs.h +2 -1
-LSM_HOOK(int, 0, binder_transaction, const struct cred *from,
+LSM_HOOK(int, 0, 0, binder_set_context_mgr, const struct cred *mgr)
+LSM_HOOK(int, 0, 0, binder_transaction, const struct cred *from,
 	 const struct cred *to)

I think adding a flag field to the LSM_HOOK() macro/definitions is a good and useful addition, but I'd prefer if we created a LSM_FLAG_NONE #define and used it here just so we could avoid the back-to-back 0's and do a bit of self-documentation.

I had LSM_FLAG_NONE initially, but removed it when I saw the amount of code churn it introduced. I'm happy to put it back.

maintainer
#5 lore raw

Re: [PATCH RFC 1/3] LSM: add a flags field to the LSM hook definitions

Quoted 58 lines · Casey Schaufler
Quoted 48 lines · Paul Moore
Quoted 47 lines · On Feb 25, 2026 Casey Schaufler <[email protected]> wrote:

Add a field for flags to the definition of LSM hooks. This allows for hooks to be identified at system initialization for special processing.

---
 include/linux/bpf_lsm.h       |   2 +-
 include/linux/lsm_hook_defs.h | 614 ++++++++++++++++++----------------
 include/linux/lsm_hooks.h     |   4 +-
 kernel/bpf/bpf_lsm.c          |  10 +-
 security/bpf/hooks.c          |   2 +-
 security/security.c           |   6 +-
 6 files changed, 331 insertions(+), 307 deletions(-)
include/linux/bpf_lsm.h +1 -1
index 643809cc78c3..d71ba8c87e79 100644
@@ -14,7 +14,7 @@
 
 #ifdef CONFIG_BPF_LSM
 
-#define LSM_HOOK(RET, DEFAULT, NAME, ...) \
+#define LSM_HOOK(RET, DEFAULT, FLAGS, NAME, ...) \
     RET bpf_lsm_##NAME(__VA_ARGS__);
 #include <linux/lsm_hook_defs.h>
 #undef LSM_HOOK
include/linux/lsm_hook_defs.h +3 -3
index 8c42b4bde09c..acda3a02da97 100644
@@ -18,451 +18,475 @@
  * The macro LSM_HOOK is used to define the data structures required by
  * the LSM framework using the pattern:
  *
- *  LSM_HOOK(<return_type>, <default_value>, <hook_name>, args...)
+ *  LSM_HOOK(<return_type>, <default_value>, <flags>, <single>,
+ *           <hook_name>, args...)
  *
  * struct security_hook_heads {
- *   #define LSM_HOOK(RET, DEFAULT, NAME, ...) struct hlist_head NAME;
+ *   #define LSM_HOOK(RET, DEFAULT, FLAGS, NAME, ...) struct hlist_head NAME;
  *   #include <linux/lsm_hook_defs.h>
  *   #undef LSM_HOOK
  * };
  */
-LSM_HOOK(int, 0, binder_set_context_mgr, const struct cred *mgr)
Quoted 8 lines

include/linux/lsm_hook_defs.h line 32

Quoted 4 lines
include/linux/lsm_hook_defs.h +2 -1
-LSM_HOOK(int, 0, binder_transaction, const struct cred *from,
+LSM_HOOK(int, 0, 0, binder_set_context_mgr, const struct cred *mgr)
+LSM_HOOK(int, 0, 0, binder_transaction, const struct cred *from,
      const struct cred *to)

I think adding a flag field to the LSM_HOOK() macro/definitions is a good and useful addition, but I'd prefer if we created a LSM_FLAG_NONE #define and used it here just so we could avoid the back-to-back 0's and do a bit of self-documentation.

Quoted 2 lines

I had LSM_FLAG_NONE initially, but removed it when I saw the amount of code churn it introduced. I'm happy to put it back.

You're already touching every LSM_HOOK() definition to add a another parameter, changing that parameter from 0 to LSM_FLAG_NONE shouldn't make it that much worse.

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

RFC PATCH 2/3 LSM: Enforce exclusive hooks

In reply to Casey Schaufler

If an LSM hook is marked as exclusive via LSM_FLAG_EXCLUSIVE in lsm_hook_defs.h it will not be added to the set of hooks to be executed if an different LSM has already registered an exclusive hook.

---
 include/linux/security.h |  2 ++
 security/lsm_init.c      | 66 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 68 insertions(+)
include/linux/security.h +2 -0
index 83a646d72f6f..e3c137a1b30a 100644
@@ -2404,4 +2404,6 @@ static inline void security_initramfs_populated(void)
 }
 #endif /* CONFIG_SECURITY */
 
+extern u64 lsm_exclusive_hooks;
+
 #endif /* ! __LINUX_SECURITY_H */
security/lsm_init.c +66 -0

78 lines not on this page · raw message

Signature · 1 line
2.52.0
maintainer
#7 lore raw

Re: [PATCH RFC 2/3] LSM: Enforce exclusive hooks

Quoted 17 lines · On Feb 25, 2026 Casey Schaufler <[email protected]> wrote:

If an LSM hook is marked as exclusive via LSM_FLAG_EXCLUSIVE in lsm_hook_defs.h it will not be added to the set of hooks to be executed if an different LSM has already registered an exclusive hook.

---
 include/linux/security.h |  2 ++
 security/lsm_init.c      | 66 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 68 insertions(+)
include/linux/security.h +0 -0
index 83a646d72f6f..e3c137a1b30a 100644

include/linux/security.h static inline void security_initramfs_populated(void) · line 2407

Quoted 4 lines
include/linux/security.h +1 -0
@@ -2404,4 +2404,6 @@ static inline void security_initramfs_populated(void)
 }
 #endif /* CONFIG_SECURITY */
 
+extern u64 lsm_exclusive_hooks;

We already have the 'lsm_exclusive' variable in lsm_init.c, don't create another variable that does the same thing. If the scope of the existing variable isn't what you need, change that.

Although to be honest, I'm not in love with what you're doing with the variable anyway, more on that later in the review.

Quoted 6 lines
include/linux/security.h +0 -0
 #endif /* ! __LINUX_SECURITY_H */
security/lsm_init.c +0 -0
index 05bd52e6b1f2..dc3c84387a7e 100644

security/lsm_init.c static int __init lsm_static_call_init(struct security_hook_list *hl) · line 363

Quoted 8 lines
security/lsm_init.c +5 -0
@@ -356,6 +356,70 @@ static int __init lsm_static_call_init(struct security_hook_list *hl)
 	return -ENOSPC;
 }
 
+/*
+ * Hooks that are restricted to use by a single security module.
+ *
+ * Secmark hooks have not been converted from secids to lsm_props
+ * due to space limitations in packet headers.

If this is a general purpose mechanism for all types of LSM hooks, please don't put commentary in here about a single class of hook.

If this only reason for doing all of this is for secmark, just fix secmark instead of going through all of this trouble.

security/lsm_init.c static int __init lsm_static_call_init(struct security_hook_list *hl) · line 368

Quoted 4 lines
security/lsm_init.c +4 -0
+ * Conversions from a secid to a secctx are restricted to the
+ * single security module. All cases where there may be multiple
+ * modules providing the input data have been converted to use
+ * a lsm_prop instead of a secid.

Okay, yes, the paragraph above is true, I'm just not sure why it is important here?

Quoted 5 lines
security/lsm_init.c +5 -0
+ */
+struct lsm_exclusive {
+	struct lsm_static_call *name;
+	char *namestr;
+	u32 flags;

security/lsm_init.c static int __init lsm_static_call_init(struct security_hook_list *hl) · line 383

Quoted 10 lines
security/lsm_init.c +10 -0
+};
+
+static __initdata struct lsm_exclusive lsm_exclusive_set[] = {
+#define LSM_HOOK(RET, DEFAULT, FLAGS, NAME, ...)	\
+	{ .name = static_calls_table.NAME, .flags = FLAGS, .namestr = "" #NAME "" , },
+#include <linux/lsm_hook_defs.h>
+#undef LSM_HOOK
+};
+u64 lsm_exclusive_hooks;
+EXPORT_SYMBOL(lsm_exclusive_hooks);

Unless I missed something, we really shouldn't need to export this, why did you need EXPORT_SYMBOL() here?

Quoted 10 lines
security/lsm_init.c +10 -0
+/**
+ * lsm_exclusive_hook_denial - Check if exclusive hook is in use
+ * @hook: the hook to check
+ *
+ * Check if the hook in question is restricted to a single using LSM,
+ * and if the LSM providing single LSM hooks is defined.
+ *
+ * Returns true if the hook is exclusive and they are already provided,
+ * false otherwise.
+ */

security/lsm_init.c static int __init lsm_static_call_init(struct security_hook_list *hl) · line 404

Quoted 10 lines
security/lsm_init.c +10 -0
+static bool __init lsm_exclusive_hook_denial(struct security_hook_list *hook)
+{
+	int i;
+
+	if (lsm_exclusive_hooks == hook->lsmid->id)
+		return false;
+
+	for (i = 0; i < ARRAY_SIZE(lsm_exclusive_set); i++) {
+		if (!(lsm_exclusive_set[i].flags & LSM_FLAG_EXCLUSIVE))
+			continue;

The logic on this looks a bit odd. What is wrong with something like the pseduo code below?

    for (i = 0; ARRAY_SIZE(lsm_hooks); i++) {
      if (lsm_hooks[i] == hook) {
        if (lsm_hooks[i].flags & LSM_FLAG_EXCLUSIVE)
          return true;
        else
          return false;
      }
    }

security/lsm_init.c static int __init lsm_static_call_init(struct security_hook_list *hl) · line 410

Quoted 6 lines
security/lsm_init.c +6 -0
+		if (hook->scalls == lsm_exclusive_set[i].name) {
+			if (lsm_exclusive_hooks) {
+				if (lsm_debug)
+					lsm_pr("%s denied for %s.\n",
+						lsm_exclusive_set[i].namestr,
+						hook->lsmid->name);

The lsm_pr_dbg() macro exists for this very reason.

security/lsm_init.c static int __init lsm_static_call_init(struct security_hook_list *hl) · line 415

Quoted 5 lines
security/lsm_init.c +5 -0
+				return true;
+			}
+			if (lsm_debug)
+				lsm_pr("Exclusive hooks limited to %s.\n",
+					hook->lsmid->name);

Same as above.

Quoted 11 lines
security/lsm_init.c +6 -0
+			lsm_exclusive_hooks = hook->lsmid->id;
+			break;
+		}
+	}
+	return false;
+}
 
 /**
  * security_add_hooks - Add a LSM's hooks to the LSM framework's hook lists
  * @hooks: LSM hooks to add

security/lsm_init.c void __init security_add_hooks(struct security_hook_list *hooks, int count, · line 442

Quoted 8 lines
security/lsm_init.c +2 -0
@@ -371,6 +435,8 @@ void __init security_add_hooks(struct security_hook_list *hooks, int count,
 
 	for (i = 0; i < count; i++) {
 		hooks[i].lsmid = lsmid;
+		if (lsm_exclusive_hook_denial(&hooks[i]))
+			continue;
 		if (lsm_static_call_init(&hooks[i]))
 			panic("exhausted LSM callback slots with LSM %s\n",
 			      lsmid->name);

I don't think we'd want to simply skip over a hook registration if the LSM doesn't have access to an exclusive hook. At the very least it risks unexpected behavior in the LSM and at the worst it prevents the LSM from properly enforcing it's security policy. There is a reason we have the panic() call in the existing code if we are not able to register a hook.

The simpliest solution here would be to panic, like we do today.

However, if you want to get fancy and enable LSMs to optionally adjust their behavior to cope with the loss of a hook callback (I'm looking at your patch 3/3 now), come up with a mechanism to report back to the LSM that one or more of the callbacks were not registered. One quick thought would be to return a non-zero error code and add an indicator/bool/flag to the security_hook_list that would be set by security_add_hooks().

--
paul-moore.com

author maintainer
#8 lore raw

Re: [PATCH RFC 2/3] LSM: Enforce exclusive hooks

Quoted 20 lines · Paul Moore
Quoted 19 lines · On Feb 25, 2026 Casey Schaufler <[email protected]> wrote:

If an LSM hook is marked as exclusive via LSM_FLAG_EXCLUSIVE in lsm_hook_defs.h it will not be added to the set of hooks to be executed if an different LSM has already registered an exclusive hook.

---
 include/linux/security.h |  2 ++
 security/lsm_init.c      | 66 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 68 insertions(+)
include/linux/security.h +0 -0
index 83a646d72f6f..e3c137a1b30a 100644
@@ -2404,4 +2404,6 @@ static inline void security_initramfs_populated(void)
 }
 #endif /* CONFIG_SECURITY */
 
Quoted 4 lines

include/linux/security.h static inline void security_initramfs_populated(void) · line 2407

Quoted 1 line
include/linux/security.h +1 -0
+extern u64 lsm_exclusive_hooks;

We already have the 'lsm_exclusive' variable in lsm_init.c, don't create another variable that does the same thing. If the scope of the existing variable isn't what you need, change that.

Reusing lsm_exclusive is probably doable. For the RFC it made more sense to make them separate.

Quoted 12 lines

Although to be honest, I'm not in love with what you're doing with the variable anyway, more on that later in the review.

Quoted 9 lines
include/linux/security.h +0 -0
 #endif /* ! __LINUX_SECURITY_H */
security/lsm_init.c +0 -0
index 05bd52e6b1f2..dc3c84387a7e 100644
@@ -356,6 +356,70 @@ static int __init lsm_static_call_init(struct security_hook_list *hl)
 	return -ENOSPC;
 }
 
Quoted 7 lines

security/lsm_init.c static int __init lsm_static_call_init(struct security_hook_list *hl) · line 363

Quoted 5 lines
security/lsm_init.c +5 -0
+/*
+ * Hooks that are restricted to use by a single security module.
+ *
+ * Secmark hooks have not been converted from secids to lsm_props
+ * due to space limitations in packet headers.

If this is a general purpose mechanism for all types of LSM hooks, please don't put commentary in here about a single class of hook.

OKey Dokey.

Quoted 2 lines

If this only reason for doing all of this is for secmark, just fix secmark instead of going through all of this trouble.

Secmark is the bellwether for using the mechanism.

Quoted 6 lines

security/lsm_init.c static int __init lsm_static_call_init(struct security_hook_list *hl) · line 368

Quoted 4 lines
security/lsm_init.c +4 -0
+ * Conversions from a secid to a secctx are restricted to the
+ * single security module. All cases where there may be multiple
+ * modules providing the input data have been converted to use
+ * a lsm_prop instead of a secid.

Okay, yes, the paragraph above is true, I'm just not sure why it is important here?

It's important because it addresses the objection that the secmark issue might be addressed by converting the secmark to a lsm_prop pointer. I can certainly make that clearer.

Quoted 9 lines
Quoted 9 lines
security/lsm_init.c +9 -0
+ */
+struct lsm_exclusive {
+	struct lsm_static_call *name;
+	char *namestr;
+	u32 flags;
+};
+
+static __initdata struct lsm_exclusive lsm_exclusive_set[] = {
+#define LSM_HOOK(RET, DEFAULT, FLAGS, NAME, ...)	\
Quoted 8 lines

security/lsm_init.c static int __init lsm_static_call_init(struct security_hook_list *hl) · line 383

Quoted 6 lines
security/lsm_init.c +6 -0
+	{ .name = static_calls_table.NAME, .flags = FLAGS, .namestr = "" #NAME "" , },
+#include <linux/lsm_hook_defs.h>
+#undef LSM_HOOK
+};
+u64 lsm_exclusive_hooks;
+EXPORT_SYMBOL(lsm_exclusive_hooks);

Unless I missed something, we really shouldn't need to export this, why did you need EXPORT_SYMBOL() here?

No, you didn't miss anything. It's a carryover from an early experiment.

Quoted 14 lines
Quoted 14 lines
security/lsm_init.c +14 -0
+/**
+ * lsm_exclusive_hook_denial - Check if exclusive hook is in use
+ * @hook: the hook to check
+ *
+ * Check if the hook in question is restricted to a single using LSM,
+ * and if the LSM providing single LSM hooks is defined.
+ *
+ * Returns true if the hook is exclusive and they are already provided,
+ * false otherwise.
+ */
+static bool __init lsm_exclusive_hook_denial(struct security_hook_list *hook)
+{
+	int i;
+
Quoted 8 lines

security/lsm_init.c static int __init lsm_static_call_init(struct security_hook_list *hl) · line 404

Quoted 6 lines
security/lsm_init.c +6 -0
+	if (lsm_exclusive_hooks == hook->lsmid->id)
+		return false;
+
+	for (i = 0; i < ARRAY_SIZE(lsm_exclusive_set); i++) {
+		if (!(lsm_exclusive_set[i].flags & LSM_FLAG_EXCLUSIVE))
+			continue;

The logic on this looks a bit odd. What is wrong with something like the pseduo code below?

This code isn't just looking to see if the hook is exclusive, it's looking to see if the hook is exclusive and if the LSM requesting it has been given the privilege to register exclusive hooks. This may not be the cleanest way to go about it, so I will reconsider how best to do that.

Quoted 9 lines
    for (i = 0; ARRAY_SIZE(lsm_hooks); i++) {
      if (lsm_hooks[i] == hook) {
        if (lsm_hooks[i].flags & LSM_FLAG_EXCLUSIVE)
          return true;
        else
          return false;
      }
    }
Quoted 7 lines

security/lsm_init.c static int __init lsm_static_call_init(struct security_hook_list *hl) · line 410

Quoted 6 lines
security/lsm_init.c +6 -0
+		if (hook->scalls == lsm_exclusive_set[i].name) {
+			if (lsm_exclusive_hooks) {
+				if (lsm_debug)
+					lsm_pr("%s denied for %s.\n",
+						lsm_exclusive_set[i].namestr,
+						hook->lsmid->name);

The lsm_pr_dbg() macro exists for this very reason.

Indeed. I missed it here. Will correct.

Quoted 6 lines

security/lsm_init.c static int __init lsm_static_call_init(struct security_hook_list *hl) · line 415

Quoted 5 lines
security/lsm_init.c +5 -0
+				return true;
+			}
+			if (lsm_debug)
+				lsm_pr("Exclusive hooks limited to %s.\n",
+					hook->lsmid->name);

Same as above.

Same as above.

Quoted 25 lines
Quoted 11 lines
security/lsm_init.c +6 -0
+			lsm_exclusive_hooks = hook->lsmid->id;
+			break;
+		}
+	}
+	return false;
+}
 
 /**
  * security_add_hooks - Add a LSM's hooks to the LSM framework's hook lists
  * @hooks: LSM hooks to add

security/lsm_init.c void __init security_add_hooks(struct security_hook_list *hooks, int count, · line 442

Quoted 8 lines
security/lsm_init.c +2 -0
@@ -371,6 +435,8 @@ void __init security_add_hooks(struct security_hook_list *hooks, int count,
 
 	for (i = 0; i < count; i++) {
 		hooks[i].lsmid = lsmid;
+		if (lsm_exclusive_hook_denial(&hooks[i]))
+			continue;
 		if (lsm_static_call_init(&hooks[i]))
 			panic("exhausted LSM callback slots with LSM %s\n",
 			      lsmid->name);

I don't think we'd want to simply skip over a hook registration if the LSM doesn't have access to an exclusive hook. At the very least it risks unexpected behavior in the LSM and at the worst it prevents the LSM from properly enforcing it's security policy. There is a reason we have the panic() call in the existing code if we are not able to register a hook.

Quoted 8 lines

The simpliest solution here would be to panic, like we do today.

However, if you want to get fancy and enable LSMs to optionally adjust their behavior to cope with the loss of a hook callback (I'm looking at your patch 3/3 now), come up with a mechanism to report back to the LSM that one or more of the callbacks were not registered. One quick thought would be to return a non-zero error code and add an indicator/bool/flag to the security_hook_list that would be set by security_add_hooks().

An LSM can easily check to see if it doesn't get exclusive hooks by looking at lsm_exclusive_hooks after registration.

	if (lsm_exclusive_hooks != LSM_ID_ME)
		... do something (in)appropriate
author maintainer
#9 lore raw

RFC PATCH 3/3 LSM: Reserve use of secmarks

In reply to Casey Schaufler

Use of "exclusive" LSM hooks are limited to the first LSM registering them. These hooks include those use to process network secmarks. The hooks required to process secmarks are flagged with LSM_FLAG_EXCLUSIVE in their definitions. This includes secid_to_secctx and secctx_to_secid, which are used by netfilter.

The various LSMs that use secmarks are updated to detect whether they are providing exclusive hooks, and to eschew the use of secmarks if they are not.

SELinux has a peculiar behavior in that it may decide that it must have network controls, but only after policy is loaded. This patch includes a warning should policy capability alwaysnetwork be set when another LSM holds the exclusive hooks. It has been suggested that SELinux would consider this a fatal condition, in which case a panic might be a favored, if draconian, option.

---
 include/linux/lsm_hook_defs.h    | 12 +++++------
 include/linux/security.h         |  1 +
 security/apparmor/lsm.c          | 24 ++++++++++++++++------
 security/security.c              | 15 ++++++++++++++
 security/selinux/hooks.c         | 35 ++++++++++++++++++++++++--------
 security/selinux/ss/services.c   |  3 +++
 security/smack/smack_lsm.c       |  6 ++++--
 security/smack/smack_netfilter.c |  6 ++++++
 8 files changed, 80 insertions(+), 22 deletions(-)
include/linux/lsm_hook_defs.h +6 -6
index acda3a02da97..e6d4d9c80ac6 100644
@@ -309,12 +309,12 @@ LSM_HOOK(int, -EINVAL, 0, getprocattr, struct task_struct *p, const char *name,
 LSM_HOOK(int, -EINVAL, 0, setprocattr, const char *name, void *value,
 	 size_t size)
 LSM_HOOK(int, 0, 0, ismaclabel, const char *name)
-LSM_HOOK(int, -EOPNOTSUPP, 0, secid_to_secctx, u32 secid,
+LSM_HOOK(int, -EOPNOTSUPP, LSM_FLAG_EXCLUSIVE, secid_to_secctx, u32 secid,
 	 struct lsm_context *cp)
 LSM_HOOK(int, -EOPNOTSUPP, 0, lsmprop_to_secctx, struct lsm_prop *prop,
 	 struct lsm_context *cp)
-LSM_HOOK(int, 0, 0, secctx_to_secid, const char *secdata, u32 seclen,
-	 u32 *secid)
+LSM_HOOK(int, 0, LSM_FLAG_EXCLUSIVE, secctx_to_secid, const char *secdata,
+	 u32 seclen, u32 *secid)
 LSM_HOOK(void, LSM_RET_VOID, 0, release_secctx, struct lsm_context *cp)
 LSM_HOOK(void, LSM_RET_VOID, 0, inode_invalidate_secctx, struct inode *inode)
 LSM_HOOK(int, 0, 0, inode_notifysecctx, struct inode *inode, void *ctx,
@@ -379,9 +379,9 @@ LSM_HOOK(void, LSM_RET_VOID, 0, inet_csk_clone, struct sock *newsk,
 	 const struct request_sock *req)
 LSM_HOOK(void, LSM_RET_VOID, 0, inet_conn_established, struct sock *sk,
 	 struct sk_buff *skb)
-LSM_HOOK(int, 0, 0, secmark_relabel_packet, u32 secid)
-LSM_HOOK(void, LSM_RET_VOID, 0, secmark_refcount_inc, void)
-LSM_HOOK(void, LSM_RET_VOID, 0, secmark_refcount_dec, void)
+LSM_HOOK(int, 0, LSM_FLAG_EXCLUSIVE, secmark_relabel_packet, u32 secid)
+LSM_HOOK(void, LSM_RET_VOID, LSM_FLAG_EXCLUSIVE, secmark_refcount_inc, void)
+LSM_HOOK(void, LSM_RET_VOID, LSM_FLAG_EXCLUSIVE, secmark_refcount_dec, void)
 LSM_HOOK(void, LSM_RET_VOID, 0, req_classify_flow,
 	 const struct request_sock *req, struct flowi_common *flic)
 LSM_HOOK(int, 0, 0, tun_dev_alloc_security, void *security)
include/linux/security.h +1 -0
index e3c137a1b30a..638436b9b748 100644
@@ -326,6 +326,7 @@ int unregister_blocking_lsm_notifier(struct notifier_block *nb);
 extern int security_init(void);
 extern int early_security_init(void);
 extern u64 lsm_name_to_attr(const char *name);
+extern u32 lsm_secmark_from_skb(struct sk_buff *skb, const u64 lsmid);
 
 /* Security operations */
 int security_binder_set_context_mgr(const struct cred *mgr);
security/apparmor/lsm.c +18 -6
index a87cd60ed206..2ce0d9a73973 100644
@@ -1511,9 +1511,11 @@ static int apparmor_socket_shutdown(struct socket *sock, int how)
 static int apparmor_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
 {
 	struct aa_sk_ctx *ctx = aa_sock(sk);
+	u32 secmark;
 	int error;
 
-	if (!skb->secmark)
+	secmark = lsm_secmark_from_skb(skb, LSM_ID_APPARMOR);
+	if (!secmark)
 		return 0;
 
 	/*
@@ -1525,7 +1527,7 @@ static int apparmor_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
 
 	rcu_read_lock();
 	error = apparmor_secmark_check(rcu_dereference(ctx->label), OP_RECVMSG,
-				       AA_MAY_RECEIVE, skb->secmark, sk);
+				       AA_MAY_RECEIVE, secmark, sk);
 	rcu_read_unlock();
 
 	return error;
@@ -1640,14 +1642,16 @@ static int apparmor_inet_conn_request(const struct sock *sk, struct sk_buff *skb
 				      struct request_sock *req)
 {
 	struct aa_sk_ctx *ctx = aa_sock(sk);
+	u32 secmark;
 	int error;
 
-	if (!skb->secmark)
+	secmark = lsm_secmark_from_skb(skb, LSM_ID_APPARMOR);
+	if (!secmark)
 		return 0;
 
 	rcu_read_lock();
 	error = apparmor_secmark_check(rcu_dereference(ctx->label), OP_CONNECT,
-				       AA_MAY_CONNECT, skb->secmark, sk);
+				       AA_MAY_CONNECT, secmark, sk);
 	rcu_read_unlock();
 
 	return error;
@@ -2359,9 +2363,11 @@ static unsigned int apparmor_ip_postroute(void *priv,
 {
 	struct aa_sk_ctx *ctx;
 	struct sock *sk;
+	u32 secmark;
 	int error;
 
-	if (!skb->secmark)
+	secmark = lsm_secmark_from_skb(skb, LSM_ID_APPARMOR);
+	if (!secmark)
 		return NF_ACCEPT;
 
 	sk = skb_to_full_sk(skb);
@@ -2371,7 +2377,7 @@ static unsigned int apparmor_ip_postroute(void *priv,
 	ctx = aa_sock(sk);
 	rcu_read_lock();
 	error = apparmor_secmark_check(rcu_dereference(ctx->label), OP_SENDMSG,
-				       AA_MAY_SEND, skb->secmark, sk);
+				       AA_MAY_SEND, secmark, sk);
 	rcu_read_unlock();
 	if (!error)
 		return NF_ACCEPT;
@@ -2399,12 +2405,18 @@ static const struct nf_hook_ops apparmor_nf_ops[] = {
 
 static int __net_init apparmor_nf_register(struct net *net)
 {
+	if (lsm_exclusive_hooks != LSM_ID_APPARMOR)
+		return 0;
+
 	return nf_register_net_hooks(net, apparmor_nf_ops,
 				    ARRAY_SIZE(apparmor_nf_ops));
 }
 
 static void __net_exit apparmor_nf_unregister(struct net *net)
 {
+	if (lsm_exclusive_hooks != LSM_ID_APPARMOR)
+		return;
+
 	nf_unregister_net_hooks(net, apparmor_nf_ops,
 				ARRAY_SIZE(apparmor_nf_ops));
 }
security/security.c +15 -0
index 25e7cfc96f20..754b16004677 100644
@@ -4509,6 +4509,21 @@ void security_inet_conn_established(struct sock *sk,
 }
 EXPORT_SYMBOL(security_inet_conn_established);
 
+/**
+ * lsm_secmark_from_skb - secid for the specified LSM from the packet
+ * @skb: the packet
+ * @lsm: which LSM is asking
+ *
+ * If the specified LSM has use of the secmark, return its value.
+ * Otherwise, return the invalid secid 0.
+ */
+u32 lsm_secmark_from_skb(struct sk_buff *skb, const u64 lsmid)
+{
+	if (lsmid == lsm_exclusive_hooks)
+		return skb->secmark;
+	return 0;
+}
+
 /**
  * security_secmark_relabel_packet() - Check if setting a secmark is allowed
  * @secid: new secmark value
security/selinux/hooks.c +27 -8
index d053ce562370..1dff2121a834 100644
@@ -5269,6 +5269,7 @@ static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
 	int err = 0;
 	struct sk_security_struct *sksec = selinux_sock(sk);
 	u32 sk_sid = sksec->sid;
+	u32 secmark;
 	struct common_audit_data ad;
 	struct lsm_network_audit net;
 	char *addrp;
@@ -5279,7 +5280,8 @@ static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
 		return err;
 
 	if (selinux_secmark_enabled()) {
-		err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
+		secmark = lsm_secmark_from_skb(skb, LSM_ID_SELINUX);
+		err = avc_has_perm(sk_sid, secmark, SECCLASS_PACKET,
 				   PACKET__RECV, &ad);
 		if (err)
 			return err;
@@ -5299,6 +5301,7 @@ static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
 	struct sk_security_struct *sksec = selinux_sock(sk);
 	u16 family = sk->sk_family;
 	u32 sk_sid = sksec->sid;
+	u32 secmark;
 	struct common_audit_data ad;
 	struct lsm_network_audit net;
 	char *addrp;
@@ -5348,7 +5351,8 @@ static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
 	}
 
 	if (secmark_active) {
-		err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
+		secmark = lsm_secmark_from_skb(skb, LSM_ID_SELINUX);
+		err = avc_has_perm(sk_sid, secmark, SECCLASS_PACKET,
 				   PACKET__RECV, &ad);
 		if (err)
 			return err;
@@ -5850,6 +5854,7 @@ static unsigned int selinux_ip_forward(void *priv, struct sk_buff *skb,
 	int ifindex;
 	u16 family;
 	char *addrp;
+	u32 secmark;
 	u32 peer_sid;
 	struct common_audit_data ad;
 	struct lsm_network_audit net;
@@ -5883,10 +5888,12 @@ static unsigned int selinux_ip_forward(void *priv, struct sk_buff *skb,
 		}
 	}
 
-	if (secmark_active)
-		if (avc_has_perm(peer_sid, skb->secmark,
+	if (secmark_active) {
+		secmark = lsm_secmark_from_skb(skb, LSM_ID_SELINUX);
+		if (avc_has_perm(peer_sid, secmark,
 				 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
 			return NF_DROP;
+	}
 
 	if (netlbl_enabled())
 		/* we do this in the FORWARD path and not the POST_ROUTING
@@ -5950,6 +5957,7 @@ static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
 	struct common_audit_data ad;
 	struct lsm_network_audit net;
 	u8 proto = 0;
+	u32 secmark;
 
 	sk = skb_to_full_sk(skb);
 	if (sk == NULL)
@@ -5960,10 +5968,12 @@ static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
 	if (selinux_parse_skb(skb, &ad, NULL, 0, &proto))
 		return NF_DROP;
 
-	if (selinux_secmark_enabled())
-		if (avc_has_perm(sksec->sid, skb->secmark,
+	if (selinux_secmark_enabled()) {
+		secmark = lsm_secmark_from_skb(skb, LSM_ID_SELINUX);
+		if (avc_has_perm(sksec->sid, secmark,
 				 SECCLASS_PACKET, PACKET__SEND, &ad))
 			return NF_DROP_ERR(-ECONNREFUSED);
+	}
 
 	if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
 		return NF_DROP_ERR(-ECONNREFUSED);
@@ -5978,6 +5988,7 @@ static unsigned int selinux_ip_postroute(void *priv,
 	u16 family;
 	u32 secmark_perm;
 	u32 peer_sid;
+	u32 secmark;
 	int ifindex;
 	struct sock *sk;
 	struct common_audit_data ad;
@@ -6082,10 +6093,12 @@ static unsigned int selinux_ip_postroute(void *priv,
 	if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
 		return NF_DROP;
 
-	if (secmark_active)
-		if (avc_has_perm(peer_sid, skb->secmark,
+	if (secmark_active) {
+		secmark = lsm_secmark_from_skb(skb, LSM_ID_SELINUX);
+		if (avc_has_perm(peer_sid, secmark,
 				 SECCLASS_PACKET, secmark_perm, &ad))
 			return NF_DROP_ERR(-ECONNREFUSED);
+	}
 
 	if (peerlbl_active) {
 		u32 if_sid;
@@ -7718,12 +7731,18 @@ static const struct nf_hook_ops selinux_nf_ops[] = {
 
 static int __net_init selinux_nf_register(struct net *net)
 {
+	if (lsm_exclusive_hooks != LSM_ID_SELINUX)
+		return 0;
+
 	return nf_register_net_hooks(net, selinux_nf_ops,
 				     ARRAY_SIZE(selinux_nf_ops));
 }
 
 static void __net_exit selinux_nf_unregister(struct net *net)
 {
+	if (lsm_exclusive_hooks != LSM_ID_SELINUX)
+		return;
+
 	nf_unregister_net_hooks(net, selinux_nf_ops,
 				ARRAY_SIZE(selinux_nf_ops));
 }
security/selinux/ss/services.c +3 -0
index 13fc712d5923..269ad09f8dca 100644
@@ -2189,6 +2189,9 @@ static void security_load_policycaps(struct selinux_policy *policy)
 			pr_info("SELinux:  unknown policy capability %u\n",
 				i);
 	}
+	if (selinux_state.policycap[POLICYDB_CAP_ALWAYSNETWORK] &&
+	    lsm_exclusive_hooks != LSM_ID_SELINUX)
+		pr_warn("SELinux:  policy capability alwaysnetwork is set, but secmark is used by another LSM.\n");
 }
 
 static int security_preserve_bools(struct selinux_policy *oldpolicy,
security/smack/smack_lsm.c +4 -2
index a0bd4919a9d9..7a98dcc4c67d 100644
@@ -4195,10 +4195,12 @@ static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
 #ifdef CONFIG_NETWORK_SECMARK
 static struct smack_known *smack_from_skb(struct sk_buff *skb)
 {
-	if (skb == NULL || skb->secmark == 0)
+	u32 secmark = lsm_secmark_from_skb(skb, LSM_ID_SMACK);
+
+	if (skb == NULL || secmark == 0)
 		return NULL;
 
-	return smack_from_secid(skb->secmark);
+	return smack_from_secid(secmark);
 }
 #else
 static inline struct smack_known *smack_from_skb(struct sk_buff *skb)
security/smack/smack_netfilter.c +6 -0
index 17ba578b1308..47426973843b 100644
@@ -54,12 +54,18 @@ static const struct nf_hook_ops smack_nf_ops[] = {
 
 static int __net_init smack_nf_register(struct net *net)
 {
+	if (lsm_exclusive_hooks != LSM_ID_SMACK)
+		return 0;
+
 	return nf_register_net_hooks(net, smack_nf_ops,
 				     ARRAY_SIZE(smack_nf_ops));
 }
 
 static void __net_exit smack_nf_unregister(struct net *net)
 {
+	if (lsm_exclusive_hooks != LSM_ID_SMACK)
+		return;
+
 	nf_unregister_net_hooks(net, smack_nf_ops, ARRAY_SIZE(smack_nf_ops));
 }
 
Signature · 1 line
2.52.0
maintainer
#10 lore raw

Re: [PATCH RFC 3/3] LSM: Reserve use of secmarks

Quoted 29 lines · On Feb 25, 2026 Casey Schaufler <[email protected]> wrote:

Use of "exclusive" LSM hooks are limited to the first LSM registering them. These hooks include those use to process network secmarks. The hooks required to process secmarks are flagged with LSM_FLAG_EXCLUSIVE in their definitions. This includes secid_to_secctx and secctx_to_secid, which are used by netfilter.

The various LSMs that use secmarks are updated to detect whether they are providing exclusive hooks, and to eschew the use of secmarks if they are not.

SELinux has a peculiar behavior in that it may decide that it must have network controls, but only after policy is loaded. This patch includes a warning should policy capability alwaysnetwork be set when another LSM holds the exclusive hooks. It has been suggested that SELinux would consider this a fatal condition, in which case a panic might be a favored, if draconian, option.

---
 include/linux/lsm_hook_defs.h    | 12 +++++------
 include/linux/security.h         |  1 +
 security/apparmor/lsm.c          | 24 ++++++++++++++++------
 security/security.c              | 15 ++++++++++++++
 security/selinux/hooks.c         | 35 ++++++++++++++++++++++++--------
 security/selinux/ss/services.c   |  3 +++
 security/smack/smack_lsm.c       |  6 ++++--
 security/smack/smack_netfilter.c |  6 ++++++
 8 files changed, 80 insertions(+), 22 deletions(-)

...

Quoted 29 lines
include/linux/security.h +1 -0
index e3c137a1b30a..638436b9b748 100644
@@ -326,6 +326,7 @@ int unregister_blocking_lsm_notifier(struct notifier_block *nb);
 extern int security_init(void);
 extern int early_security_init(void);
 extern u64 lsm_name_to_attr(const char *name);
+extern u32 lsm_secmark_from_skb(struct sk_buff *skb, const u64 lsmid);
 
 /* Security operations */
 int security_binder_set_context_mgr(const struct cred *mgr);
security/apparmor/lsm.c +3 -1
index a87cd60ed206..2ce0d9a73973 100644
@@ -1511,9 +1511,11 @@ static int apparmor_socket_shutdown(struct socket *sock, int how)
 static int apparmor_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
 {
 	struct aa_sk_ctx *ctx = aa_sock(sk);
+	u32 secmark;
 	int error;
 
-	if (!skb->secmark)
+	secmark = lsm_secmark_from_skb(skb, LSM_ID_APPARMOR);
+	if (!secmark)
 		return 0;
 
 	/*

...

Quoted 12 lines
security/security.c +4 -0
index 25e7cfc96f20..754b16004677 100644
@@ -4509,6 +4509,21 @@ void security_inet_conn_established(struct sock *sk,
 }
 EXPORT_SYMBOL(security_inet_conn_established);
 
+/**
+ * lsm_secmark_from_skb - secid for the specified LSM from the packet
+ * @skb: the packet
+ * @lsm: which LSM is asking

security/security.c void security_inet_conn_established(struct sock *sk, · line 4525

Quoted 10 lines
security/security.c +10 -0
+ *
+ * If the specified LSM has use of the secmark, return its value.
+ * Otherwise, return the invalid secid 0.
+ */
+u32 lsm_secmark_from_skb(struct sk_buff *skb, const u64 lsmid)
+{
+	if (lsmid == lsm_exclusive_hooks)
+		return skb->secmark;
+	return 0;
+}

Ooof. Not a fan. A better way to handle this would be to like I mentioned in my comments on patch 2/3: have the LSM detect that it lost the LSM_FLAG_EXCLUSIVE battle at callback registration time and do whatever adjustments are necessary at init time. In a number of cases I believe that simply not registering the callback will be sufficient.

If the only way to solve this is via runtime checks like this, unfortunately my answer is going to be "no".

... and of course the proper way to solve this for secmark is to just do the idr/xarray conversion for secmarks so every LSM can have their own secmark. Yes, it does require some work, but to be honest I think you've spent more time avoiding that work then it would be to just do it. I'm growing increasing inclined to simply state that this is the only solution I'm going to accept.

--
paul-moore.com

author maintainer
#11 lore raw

Re: [PATCH RFC 3/3] LSM: Reserve use of secmarks

Quoted 81 lines · Paul Moore
Quoted 28 lines · On Feb 25, 2026 Casey Schaufler <[email protected]> wrote:

Use of "exclusive" LSM hooks are limited to the first LSM registering them. These hooks include those use to process network secmarks. The hooks required to process secmarks are flagged with LSM_FLAG_EXCLUSIVE in their definitions. This includes secid_to_secctx and secctx_to_secid, which are used by netfilter.

The various LSMs that use secmarks are updated to detect whether they are providing exclusive hooks, and to eschew the use of secmarks if they are not.

SELinux has a peculiar behavior in that it may decide that it must have network controls, but only after policy is loaded. This patch includes a warning should policy capability alwaysnetwork be set when another LSM holds the exclusive hooks. It has been suggested that SELinux would consider this a fatal condition, in which case a panic might be a favored, if draconian, option.

---
 include/linux/lsm_hook_defs.h    | 12 +++++------
 include/linux/security.h         |  1 +
 security/apparmor/lsm.c          | 24 ++++++++++++++++------
 security/security.c              | 15 ++++++++++++++
 security/selinux/hooks.c         | 35 ++++++++++++++++++++++++--------
 security/selinux/ss/services.c   |  3 +++
 security/smack/smack_lsm.c       |  6 ++++--
 security/smack/smack_netfilter.c |  6 ++++++
 8 files changed, 80 insertions(+), 22 deletions(-)

..

Quoted 29 lines
include/linux/security.h +1 -0
index e3c137a1b30a..638436b9b748 100644
@@ -326,6 +326,7 @@ int unregister_blocking_lsm_notifier(struct notifier_block *nb);
 extern int security_init(void);
 extern int early_security_init(void);
 extern u64 lsm_name_to_attr(const char *name);
+extern u32 lsm_secmark_from_skb(struct sk_buff *skb, const u64 lsmid);
 
 /* Security operations */
 int security_binder_set_context_mgr(const struct cred *mgr);
security/apparmor/lsm.c +3 -1
index a87cd60ed206..2ce0d9a73973 100644
@@ -1511,9 +1511,11 @@ static int apparmor_socket_shutdown(struct socket *sock, int how)
 static int apparmor_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
 {
 	struct aa_sk_ctx *ctx = aa_sock(sk);
+	u32 secmark;
 	int error;
 
-	if (!skb->secmark)
+	secmark = lsm_secmark_from_skb(skb, LSM_ID_APPARMOR);
+	if (!secmark)
 		return 0;
 
 	/*

..

Quoted 19 lines
security/security.c +11 -0
index 25e7cfc96f20..754b16004677 100644
@@ -4509,6 +4509,21 @@ void security_inet_conn_established(struct sock *sk,
 }
 EXPORT_SYMBOL(security_inet_conn_established);
 
+/**
+ * lsm_secmark_from_skb - secid for the specified LSM from the packet
+ * @skb: the packet
+ * @lsm: which LSM is asking
+ *
+ * If the specified LSM has use of the secmark, return its value.
+ * Otherwise, return the invalid secid 0.
+ */
+u32 lsm_secmark_from_skb(struct sk_buff *skb, const u64 lsmid)
+{
+	if (lsmid == lsm_exclusive_hooks)
Quoted 8 lines

security/security.c void security_inet_conn_established(struct sock *sk, · line 4525

Quoted 3 lines
security/security.c +3 -0
+		return skb->secmark;
+	return 0;
+}

Ooof. Not a fan. A better way to handle this would be to like I mentioned in my comments on patch 2/3: have the LSM detect that it lost the LSM_FLAG_EXCLUSIVE battle at callback registration time and do whatever adjustments are necessary at init time. In a number of cases I believe that simply not registering the callback will be sufficient.

In no case (AppArmor, Smack, SELinux) will that be sufficient. All access skb->secmark directly. The hooks that use the secmark also use other mechanisms (e.g. netlabel) to obtain security attributes.

Quoted 2 lines

If the only way to solve this is via runtime checks like this, unfortunately my answer is going to be "no".

OK. I hear you.

Quoted 6 lines

.. and of course the proper way to solve this for secmark is to just do the idr/xarray conversion for secmarks so every LSM can have their own secmark. Yes, it does require some work, but to be honest I think you've spent more time avoiding that work then it would be to just do it. I'm growing increasing inclined to simply state that this is the only solution I'm going to accept.

Thank you for the clarity. If you happen to have a preferred example of how idr/xarray is used I expect that we can reduce the review cycle of such an implementation.

... Or, if there's someone out there who's itching to get involved and eager to tackle a moderately complicated integration effort, I'd happily outline the pain points.

Quoted 3 lines

--
paul-moore.com

With that I think this RFC has been useful in that the LSM define flag scheme might be useful in the future, but doesn't address the task at hand.