[PATCH v2 bpf-next 3/6] bpf: Don't allow the IMA hash helpers on hooks with the inode locked

From: Anton Protopopov

Date: Fri Dec 04 2026 - 10:43:32 EST


The bpf_ima_inode_hash() and bpf_ima_file_hash() helpers are allowed
on every sleepable LSM hook. Both take iint->mutex in
__ima_inode_hash(). IMA itself takes iint->mutex before the inode
lock: process_measurement() holds it and locks the inode to appraise
the file. IMA also holds it while it sets security.ima in
ima_fix_xattr() and ima_update_xattr().

So a program which calls the helpers on a hook called with the inode
locked can deadlock. On inode_setxattr, inode_removexattr,
inode_setattr, inode_unlink, path_chmod or path_chown it waits for
iint->mutex, while a concurrent open of the file holds it and waits
for the inode lock. On inode_post_setxattr it may wait for
iint->mutex which the task itself holds when IMA sets security.ima.

These hooks are in d_inode_locked_hooks. Don't allow the helpers
there.

Fixes: 27672f0d280a ("bpf: Add a BPF helper for getting the IMA hash of an inode")
Signed-off-by: Anton Protopopov <a.s.protopopov@xxxxxxxxx>
---
kernel/bpf/bpf_lsm.c | 1 +
1 file changed, 1 insertion(+)

diff --git a/kernel/bpf/bpf_lsm.c b/kernel/bpf/bpf_lsm.c
index 608d188ffe0e..6c79d4786f67 100644
--- a/kernel/bpf/bpf_lsm.c
+++ b/kernel/bpf/bpf_lsm.c
@@ -207,6 +207,7 @@ BTF_SET_END(bpf_ima_inode_hash_denied_hooks)
static bool bpf_ima_inode_hash_allowed(const struct bpf_prog *prog)
{
return bpf_lsm_is_sleepable_hook(prog->aux->attach_btf_id) &&
+ !bpf_lsm_has_d_inode_locked(prog) &&
!btf_id_set_contains(&bpf_ima_inode_hash_denied_hooks,
prog->aux->attach_btf_id);
}
--
2.43.0