[PATCH 1/8] lsm: always call the namespace_free hook
Christian Brauner
brauner at kernel.org
Mon Oct 26 09:57:09 UTC 2026
security_namespace_free() returns early if ns->ns_security is NULL and
skips the namespace_free hook. But ns->ns_security is only allocated if
an LSM requests a namespace blob via lbs_ns and currently no LSM does.
So the namespace_free hook is never called at all.
Landlock only uses namespace_init and namespace_install so it doesn't
care. But the bpf LSM attaches to every hook and a program attached to
namespace_free never runs. We want to use it in systemd-nsresourced to
get notified when a user namespace goes away. Right now it has to kprobe
retire_userns_sysctls() for that.
Note that a bpf program can't work around this. The size of the
namespace blob is fixed at boot and the bpf LSM doesn't request one.
And security_namespace_init() allocates the blob before the
namespace_init hooks are called anyway.
The check isn't needed. __ns_common_free() is only ever called after
__ns_common_init() succeeded and security_namespace_init() only calls
security_namespace_free() after the blob has been allocated. So if an
LSM requests a namespace blob it is always there when the hook runs.
Call the hook unconditionally like security_sb_free(),
security_file_free() and security_bpf_map_free() do.
The bpf LSM attaches programs to its bpf_lsm_*() stubs so fentry on the
stubs shows what a bpf LSM program sees. Booted with lsm.debug, which
prints "LSM: blob(ns) size 0", and created 100 user and uts namespaces:
bpftrace -e 'fentry:bpf_lsm_namespace_init { @init = count(); }
fentry:bpf_lsm_namespace_free { @free = count(); }' &
for i in $(seq 100); do unshare -Uu true; done
before: @init: 200 @free: 0
after: @init: 200 @free: 200
Fixes: f675d2e95569 ("lsm: add LSM blob and hooks for namespaces")
Cc: <stable at kernel.org> # mainline only
Signed-off-by: Christian Brauner (Amutable) <brauner at kernel.org>
---
security/security.c | 3 ---
1 file changed, 3 deletions(-)
diff --git a/security/security.c b/security/security.c
index f6583d507395..1e76c097b28d 100644
--- a/security/security.c
+++ b/security/security.c
@@ -3412,9 +3412,6 @@ int security_namespace_init(struct ns_common *ns)
*/
void security_namespace_free(struct ns_common *ns)
{
- if (!ns->ns_security)
- return;
-
call_void_hook(namespace_free, ns);
kfree(ns->ns_security);
--
2.53.0
More information about the Linux-security-module-archive
mailing list