Skip to contentSkip to conversation

bpf: lsm: disable xfrm_decode_session hook attachment

PATCH Applied Bradley Morgan 3 participants 4 messages Jun 19 – Sep 10

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 bpf: lsm: disable xfrm_decode_session hook attachment

BPF LSM programs can currently attach to xfrm_decode_session(). That hook may return an error, but security_skb_classify_flow() calls it from a void path and triggers BUG_ON() if an error is returned.

Disable BPF attachment to the hook to prevent a BPF LSM program from turning packet classification into a full panic.

Fixes 9e4e01dfd325 ("bpf: lsm: Implement attach, detach and execution")
---
 kernel/bpf/bpf_lsm.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/bpf/bpf_lsm.c +3 -0
index 564071a92d7d..1433809bb166 100644
@@ -51,6 +51,9 @@ BTF_ID(func, bpf_lsm_key_getsecurity)
 #ifdef CONFIG_AUDIT
 BTF_ID(func, bpf_lsm_audit_rule_match)
 #endif
+#ifdef CONFIG_SECURITY_NETWORK_XFRM
+BTF_ID(func, bpf_lsm_xfrm_decode_session)
+#endif
 BTF_ID(func, bpf_lsm_ismaclabel)
 BTF_ID(func, bpf_lsm_file_alloc_security)
 BTF_SET_END(bpf_lsm_disabled_hooks)
Signature · 1 line
2.53.0
#1 lore raw

PATCH 2/2 lsm: fix size queries for getselfattr with NULL buffer

The lsm_get_self_attr() syscall allows callers to pass in a NULL context buffer to find out the size of the output needed. That path still compared the computed entry size against the caller provided size first, so a NULL buffer with size 0 incorrectly returned -E2BIG rather than reporting the required size.

Only enforce the available buffer length after checking for the NULL buffer. Cover the zero length sizing query in the self test.

Fixes d7cf3412a9f6 ("lsm: consolidate buffer size handling into lsm_fill_user_ctx()")
---
 security/security.c                                  | 8 ++++----
 tools/testing/selftests/lsm/lsm_get_self_attr_test.c | 5 ++---
 2 files changed, 6 insertions(+), 7 deletions(-)
security/security.c +4 -4
index 71aea8fdf014..fa0d7e036249 100644
@@ -406,15 +406,15 @@ int lsm_fill_user_ctx(struct lsm_ctx __user *uctx, u32 *uctx_len,
 	int rc = 0;
 
 	nctx_len = ALIGN(struct_size(nctx, ctx, val_len), sizeof(void *));
+	/* no buffer - return success/0 and set @uctx_len to the req size */
+	if (!uctx)
+		goto out;
+
 	if (nctx_len > *uctx_len) {
 		rc = -E2BIG;
 		goto out;
 	}
 
-	/* no buffer - return success/0 and set @uctx_len to the req size */
-	if (!uctx)
-		goto out;
-
 	nctx = kzalloc(nctx_len, GFP_KERNEL);
 	if (nctx == NULL) {
 		rc = -ENOMEM;
tools/testing/selftests/lsm/lsm_get_self_attr_test.c +2 -3
index 60caf8528f81..2f5ababc2b95 100644
@@ -39,15 +39,14 @@ TEST(size_null_lsm_get_self_attr)
 
 TEST(ctx_null_lsm_get_self_attr)
 {
-	const long page_size = sysconf(_SC_PAGESIZE);
-	__u32 size = page_size;
+	__u32 size = 0;
 	int rc;
 
 	rc = lsm_get_self_attr(LSM_ATTR_CURRENT, NULL, &size, 0);
 
 	if (attr_lsm_count()) {
 		ASSERT_NE(-1, rc);
-		ASSERT_NE(1, size);
+		ASSERT_NE(0, size);
 	} else {
 		ASSERT_EQ(-1, rc);
 	}
Signature · 1 line
2.53.0
maintainer
#2 lore raw

Re: [PATCH 2/2] lsm: fix size queries for getselfattr with NULL buffer

Quoted 32 lines · On Jun 19, 2026 Bradley Morgan <[email protected]> wrote:

The lsm_get_self_attr() syscall allows callers to pass in a NULL context buffer to find out the size of the output needed. That path still compared the computed entry size against the caller provided size first, so a NULL buffer with size 0 incorrectly returned -E2BIG rather than reporting the required size.

Only enforce the available buffer length after checking for the NULL buffer. Cover the zero length sizing query in the self test.

Fixes d7cf3412a9f6 ("lsm: consolidate buffer size handling into lsm_fill_user_ctx()")
---
 security/security.c                                  | 8 ++++----
 tools/testing/selftests/lsm/lsm_get_self_attr_test.c | 5 ++---
 2 files changed, 6 insertions(+), 7 deletions(-)
security/security.c +4 -0
index 71aea8fdf014..fa0d7e036249 100644
@@ -406,15 +406,15 @@ int lsm_fill_user_ctx(struct lsm_ctx __user *uctx, u32 *uctx_len,
 	int rc = 0;
 
 	nctx_len = ALIGN(struct_size(nctx, ctx, val_len), sizeof(void *));
+	/* no buffer - return success/0 and set @uctx_len to the req size */
+	if (!uctx)
+		goto out;
+
 	if (nctx_len > *uctx_len) {
 		rc = -E2BIG;

security/security.c int lsm_fill_user_ctx(struct lsm_ctx __user *uctx, u32 *uctx_len, · line 420

Quoted 10 lines
security/security.c +0 -4
 		goto out;
 	}
 
-	/* no buffer - return success/0 and set @uctx_len to the req size */
-	if (!uctx)
-		goto out;
-
 	nctx = kzalloc(nctx_len, GFP_KERNEL);
 	if (nctx == NULL) {
 		rc = -ENOMEM;

I'm not sure the existing code is necessarily wrong. The existing code allows for an E2BIG length check against the current necessary size, so one can still do a length check regardless of if a buffer is passed. Even in the NULL/E2BIG case, the necessary buffer size should still be returned to the caller so that the process can size their allocation appropriately.

Or am I missing something else?

--
paul-moore.com

#3 lore raw
In reply to Bradley Morgan
Quoted 20 lines · Bradley Morgan

BPF LSM programs can currently attach to xfrm_decode_session(). That hook may return an error, but security_skb_classify_flow() calls it from a void path and triggers BUG_ON() if an error is returned.

Disable BPF attachment to the hook to prevent a BPF LSM program from turning packet classification into a full panic.

Fixes 9e4e01dfd325 ("bpf: lsm: Implement attach, detach and execution")
---
 kernel/bpf/bpf_lsm.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/bpf/bpf_lsm.c +0 -0
index 564071a92d7d..1433809bb166 100644

kernel/bpf/bpf_lsm.c BTF_ID(func, bpf_lsm_key_getsecurity) · line 56

Quoted 6 lines
kernel/bpf/bpf_lsm.c +3 -0
@@ -51,6 +51,9 @@ BTF_ID(func, bpf_lsm_key_getsecurity)
 #ifdef CONFIG_AUDIT
 BTF_ID(func, bpf_lsm_audit_rule_match)
 #endif
+#ifdef CONFIG_SECURITY_NETWORK_XFRM
+BTF_ID(func, bpf_lsm_xfrm_decode_session)
+#endif

Applied this fix to bpf tree.