Skip to contentSkip to conversation

lsm,selinux: Add LSM blob support for BPF objects

PATCH In review Blaise Boscaccy 2 participants 4 messages Jul 22, 2025 – Aug 11, 2025 1 message not in the archive

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

PATCH v2 1/1 lsm,selinux: Add LSM blob support for BPF objects

This patch introduces LSM blob support for BPF maps, programs, and tokens to enable LSM stacking and multiplexing of LSM modules that govern BPF objects. Additionally, the existing BPF hooks used by SELinux have been updated to utilize the new blob infrastructure, removing the assumption of exclusive ownership of the security pointer.

---
v2:
- Use lsm_blob_alloc
- Remove unneded null check
- ifdef guard bpf alloc helpers

---
 include/linux/lsm_hooks.h         |  3 ++
 security/security.c               | 86 +++++++++++++++++++++++++++++--
 security/selinux/hooks.c          | 56 ++++----------------
 security/selinux/include/objsec.h | 17 ++++++
 4 files changed, 113 insertions(+), 49 deletions(-)
include/linux/lsm_hooks.h +3 -0
index 090d1d3e19fed..79ec5a2bdcca7 100644
@@ -116,6 +116,9 @@ 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_bpf_map;
+	int lbs_bpf_prog;
+	int lbs_bpf_token;
 };
 
 /*
security/security.c +83 -3

140 lines not on this page · raw message

security/selinux/hooks.c +10 -46

122 lines not on this page · raw message

security/selinux/include/objsec.h +17 -0
index 6ee7dc4dfd6e0..9f935ed9a761f 100644
@@ -26,6 +26,7 @@
 #include <linux/lsm_hooks.h>
 #include <linux/msg.h>
 #include <net/net_namespace.h>
+#include <linux/bpf.h>
 #include "flask.h"
 #include "avc.h"
 
@@ -237,4 +238,20 @@ selinux_perf_event(void *perf_event)
 	return perf_event + selinux_blob_sizes.lbs_perf_event;
 }
 
+#ifdef CONFIG_BPF_SYSCALL
+static inline struct bpf_security_struct *selinux_bpf_map_security(struct bpf_map *map)
+{
+	return map->security + selinux_blob_sizes.lbs_bpf_map;
+}
+
+static inline struct bpf_security_struct *selinux_bpf_prog_security(struct bpf_prog *prog)
+{
+	return prog->aux->security + selinux_blob_sizes.lbs_bpf_prog;
+}
+
+static inline struct bpf_security_struct *selinux_bpf_token_security(struct bpf_token *token)
+{
+	return token->security + selinux_blob_sizes.lbs_bpf_token;
+}
+#endif /* CONFIG_BPF_SYSCALL */
 #endif /* _SELINUX_OBJSEC_H_ */
Signature · 1 line
2.48.1
maintainer
#1 lore raw
Quoted 14 lines · On Jul 22, 2025 Blaise Boscaccy <[email protected]> wrote:

This patch introduces LSM blob support for BPF maps, programs, and tokens to enable LSM stacking and multiplexing of LSM modules that govern BPF objects. Additionally, the existing BPF hooks used by SELinux have been updated to utilize the new blob infrastructure, removing the assumption of exclusive ownership of the security pointer.

---
v2:
- Use lsm_blob_alloc
- Remove unneded null check
- ifdef guard bpf alloc helpers

Quoted 6 lines
---
 include/linux/lsm_hooks.h         |  3 ++
 security/security.c               | 86 +++++++++++++++++++++++++++++--
 security/selinux/hooks.c          | 56 ++++----------------
 security/selinux/include/objsec.h | 17 ++++++
 4 files changed, 113 insertions(+), 49 deletions(-)

This looks good to me, one nit/question below ...

security/security.c int security_bpf_prog(struct bpf_prog *prog) · line 5734

Quoted 6 lines
security/security.c +1 -1
@@ -5684,7 +5731,16 @@ int security_bpf_prog(struct bpf_prog *prog)
 int security_bpf_map_create(struct bpf_map *map, union bpf_attr *attr,
 			    struct bpf_token *token, bool kernel)
 {
-	return call_int_hook(bpf_map_create, map, attr, token, kernel);
+	int rc = 0;

I understand the motivation behind initializing @rc to zero, but to be honest it is redundant and will surely result in a follow on patch from someone to remove the initialization.

Do you have any objection to me removing the initialization during the merge? This would obviously apply to the other two as well.

security/security.c int security_bpf_prog(struct bpf_prog *prog) · line 5744

Quoted 9 lines
security/security.c +8 -0
+	rc = lsm_bpf_map_alloc(map);
+	if (unlikely(rc))
+		return rc;
+
+	rc = call_int_hook(bpf_map_create, map, attr, token, kernel);
+	if (unlikely(rc))
+		security_bpf_map_free(map);
+	return rc;
 }

--
paul-moore.com

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

maintainer
#3 lore raw
Quoted 37 lines · Blaise Boscaccy
Quoted 35 lines · Paul Moore
Quoted 14 lines · On Jul 22, 2025 Blaise Boscaccy <[email protected]> wrote:

This patch introduces LSM blob support for BPF maps, programs, and tokens to enable LSM stacking and multiplexing of LSM modules that govern BPF objects. Additionally, the existing BPF hooks used by SELinux have been updated to utilize the new blob infrastructure, removing the assumption of exclusive ownership of the security pointer.

---
v2:
- Use lsm_blob_alloc
- Remove unneded null check
- ifdef guard bpf alloc helpers

Quoted 6 lines
---
 include/linux/lsm_hooks.h         |  3 ++
 security/security.c               | 86 +++++++++++++++++++++++++++++--
 security/selinux/hooks.c          | 56 ++++----------------
 security/selinux/include/objsec.h | 17 ++++++
 4 files changed, 113 insertions(+), 49 deletions(-)

This looks good to me, one nit/question below ...

security/security.c int security_bpf_prog(struct bpf_prog *prog) · line 5734

Quoted 6 lines
security/security.c +1 -1
@@ -5684,7 +5731,16 @@ int security_bpf_prog(struct bpf_prog *prog)
 int security_bpf_map_create(struct bpf_map *map, union bpf_attr *attr,
                         struct bpf_token *token, bool kernel)
 {
-    return call_int_hook(bpf_map_create, map, attr, token, kernel);
+    int rc = 0;

I understand the motivation behind initializing @rc to zero, but to be honest it is redundant and will surely result in a follow on patch from someone to remove the initialization.

Quoted 5 lines
Quoted 3 lines

Do you have any objection to me removing the initialization during the merge? This would obviously apply to the other two as well.

No objections on my end. Thanks.

Okay, merged to lsm/dev-staging with plans to move it to lsm/dev once the merge window closes.

Signature · 1 line
paul-moore.com
maintainer
#4 lore raw
Quoted 39 lines · Paul Moore
Quoted 37 lines · Blaise Boscaccy
Quoted 35 lines · Paul Moore
Quoted 14 lines · On Jul 22, 2025 Blaise Boscaccy <[email protected]> wrote:

This patch introduces LSM blob support for BPF maps, programs, and tokens to enable LSM stacking and multiplexing of LSM modules that govern BPF objects. Additionally, the existing BPF hooks used by SELinux have been updated to utilize the new blob infrastructure, removing the assumption of exclusive ownership of the security pointer.

---
v2:
- Use lsm_blob_alloc
- Remove unneded null check
- ifdef guard bpf alloc helpers

Quoted 6 lines
---
 include/linux/lsm_hooks.h         |  3 ++
 security/security.c               | 86 +++++++++++++++++++++++++++++--
 security/selinux/hooks.c          | 56 ++++----------------
 security/selinux/include/objsec.h | 17 ++++++
 4 files changed, 113 insertions(+), 49 deletions(-)

This looks good to me, one nit/question below ...

security/security.c int security_bpf_prog(struct bpf_prog *prog) · line 5734

Quoted 6 lines
security/security.c +1 -1
@@ -5684,7 +5731,16 @@ int security_bpf_prog(struct bpf_prog *prog)
 int security_bpf_map_create(struct bpf_map *map, union bpf_attr *attr,
                         struct bpf_token *token, bool kernel)
 {
-    return call_int_hook(bpf_map_create, map, attr, token, kernel);
+    int rc = 0;

I understand the motivation behind initializing @rc to zero, but to be honest it is redundant and will surely result in a follow on patch from someone to remove the initialization.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Do you have any objection to me removing the initialization during the merge? This would obviously apply to the other two as well.

No objections on my end. Thanks.

Okay, merged to lsm/dev-staging with plans to move it to lsm/dev once the merge window closes.

Now merged into lsm/dev, thanks!

Signature · 1 line
paul-moore.com