[PATCH bpf-next 02/13] lsm: Add LSM hook security_policy_kptr_put
Justin Suess <[email protected]> Thu, 30 Jul 2026 22:20:35 -0400
| Newsgroups | gmane.linux.kernel.lsm,gmane.linux.kernel.bpf,gmane.linux.kernel |
|---|---|
| Message-ID | <[email protected]> |
Add the generic LSM hook releasing a reference obtained through security_policy_kptr_from_fd(): security_policy_kptr_put(lsmid, &policy) The shim uses the same targeted dispatch by @lsmid as the get hook: only the implementation registered by the matching LSM is called, and it only ever reads its own member of union lsm_policy_kptr, so a policy object only ever travels back to the LSM that produced it. The hook is void: releasing a reference cannot fail. A reference can only come from the matching LSM's policy_kptr_from_fd hook, so a dispatch miss means a caller passed the wrong lsmid or an LSM implemented the get hook without the put hook; the shim warns instead of silently leaking the reference. An implementation must support being called from a context that cannot sleep: the release of BPF managed references may be driven from object destructors. Like the get hook, this hook is excluded from the "bpf" LSM's attachment points, as the targeted dispatch makes an attachment there unreachable. Cc: Paul Moore <[email protected]> Cc: Casey Schaufler <[email protected]> Signed-off-by: Justin Suess <[email protected]> --- include/linux/lsm_hook_defs.h | 1 + include/linux/security.h | 6 ++++++ kernel/bpf/bpf_lsm.c | 1 + security/security.c | 27 +++++++++++++++++++++++++++ 4 files changed, 35 insertions(+) diff --git a/include/linux/lsm_hook_defs.h b/include/linux/lsm_hook_defs.h index afd5b3f932a9..0800622e317f 100644 --- a/include/linux/lsm_hook_defs.h +++ b/include/linux/lsm_hook_defs.h @@ -454,6 +454,7 @@ LSM_HOOK(int, 0, bpf_token_cmd, const struct bpf_token *token, enum bpf_cmd cmd) LSM_HOOK(int, 0, bpf_token_capable, const struct bpf_token *token, int cap) LSM_HOOK(int, -EOPNOTSUPP, policy_kptr_from_fd, int fd, union lsm_policy_kptr *policy) +LSM_HOOK(void, LSM_RET_VOID, policy_kptr_put, union lsm_policy_kptr *policy) #endif /* CONFIG_BPF_SYSCALL */ LSM_HOOK(int, 0, locked_down, enum lockdown_reason what) diff --git a/include/linux/security.h b/include/linux/security.h index db807e61d310..5017a335918c 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -2331,6 +2331,7 @@ extern int security_bpf_token_cmd(const struct bpf_token *token, enum bpf_cmd cm extern int security_bpf_token_capable(const struct bpf_token *token, int cap); extern int security_policy_kptr_from_fd(u64 lsmid, int fd, union lsm_policy_kptr *policy); +extern void security_policy_kptr_put(u64 lsmid, union lsm_policy_kptr *policy); #else static inline int security_bpf(int cmd, union bpf_attr *attr, unsigned int size, bool kernel) @@ -2390,6 +2391,11 @@ static inline int security_policy_kptr_from_fd(u64 lsmid, int fd, { return -EOPNOTSUPP; } + +static inline void security_policy_kptr_put(u64 lsmid, + union lsm_policy_kptr *policy) +{ +} #endif /* CONFIG_SECURITY */ #endif /* CONFIG_BPF_SYSCALL */ diff --git a/kernel/bpf/bpf_lsm.c b/kernel/bpf/bpf_lsm.c index 9fa514204fb5..e9059d43e92c 100644 --- a/kernel/bpf/bpf_lsm.c +++ b/kernel/bpf/bpf_lsm.c @@ -57,6 +57,7 @@ BTF_ID(func, bpf_lsm_xfrm_decode_session) BTF_ID(func, bpf_lsm_ismaclabel) BTF_ID(func, bpf_lsm_file_alloc_security) BTF_ID(func, bpf_lsm_policy_kptr_from_fd) +BTF_ID(func, bpf_lsm_policy_kptr_put) BTF_SET_END(bpf_lsm_disabled_hooks) /* List of LSM hooks that should operate on 'current' cgroup regardless diff --git a/security/security.c b/security/security.c index 14fd8b878cd0..fd535bd00c24 100644 --- a/security/security.c +++ b/security/security.c @@ -5479,6 +5479,33 @@ int security_policy_kptr_from_fd(u64 lsmid, int fd, return LSM_RET_DEFAULT(policy_kptr_from_fd); } +/** + * security_policy_kptr_put() - Put a reference on an LSM policy object + * @lsmid: LSM_ID_* value of the LSM owning @policy + * @policy: the policy object, in the member of the LSM identified by + * @lsmid + * + * Release a reference previously obtained with + * security_policy_kptr_from_fd(). Only the hook implementation + * of the LSM identified by @lsmid is called, and @policy must have + * been obtained from that same LSM. An implementation must support + * being called from a context that cannot sleep: the release of BPF + * managed references may be driven from object destructors. + */ +void security_policy_kptr_put(u64 lsmid, union lsm_policy_kptr *policy) +{ + struct lsm_static_call *scall; + + lsm_for_each_hook(scall, policy_kptr_put) { + if (scall->hl->lsmid->id != lsmid) + continue; + scall->hl->hook.policy_kptr_put(policy); + return; + } + /* A held reference implies the matching LSM implements the hook. */ + WARN_ON_ONCE(1); +} + /** * security_bpf_map_free() - Free a bpf map's LSM blob * @map: bpf map -- 2.54.0