[PATCH v2 bpf-next 2/6] bpf: Don't allow the IMA helpers on inode_getxattr and inode_getattr
From: Anton Protopopov
Date: Fri Dec 04 2026 - 10:43:13 EST
The bpf_ima_inode_hash() and bpf_ima_file_hash() helpers take
iint->mutex in __ima_inode_hash(). IMA takes iint->mutex before the
inode lock in process_measurement(). So a program which calls them
with the inode locked can deadlock against a concurrent open of the
file.
The inode_getxattr and inode_getattr hooks are sometimes called with
the inode locked. EVM reads xattrs with vfs_getxattr() to update
security.evm from the post setxattr, removexattr and setattr hooks,
which run with the inode locked. And nfsd_setattr() locks the inode
before fh_fill_pre_attrs() calls vfs_getattr(). IMA also verifies
security.evm with iint->mutex held and EVM reads the xattrs with
vfs_getxattr(). So a program on inode_getxattr can wait for the mutex
which its own task holds.
The hooks can't go to d_inode_locked_hooks as most callers don't hold
the lock. Don't allow the helpers on them.
Fixes: 423f16108c9d ("bpf: Augment the set of sleepable LSM hooks")
Signed-off-by: Anton Protopopov <a.s.protopopov@xxxxxxxxx>
---
kernel/bpf/bpf_lsm.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/kernel/bpf/bpf_lsm.c b/kernel/bpf/bpf_lsm.c
index 1e2acb7581e7..608d188ffe0e 100644
--- a/kernel/bpf/bpf_lsm.c
+++ b/kernel/bpf/bpf_lsm.c
@@ -198,9 +198,17 @@ BPF_CALL_3(bpf_ima_inode_hash, struct inode *, inode, void *, dst, u32, size)
return ima_inode_hash(inode, dst, size);
}
+/* Hooks which may be called with the inode locked or with iint->mutex held */
+BTF_SET_START(bpf_ima_inode_hash_denied_hooks)
+BTF_ID(func, bpf_lsm_inode_getattr)
+BTF_ID(func, bpf_lsm_inode_getxattr)
+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);
+ return bpf_lsm_is_sleepable_hook(prog->aux->attach_btf_id) &&
+ !btf_id_set_contains(&bpf_ima_inode_hash_denied_hooks,
+ prog->aux->attach_btf_id);
}
BTF_ID_LIST_SINGLE(bpf_ima_inode_hash_btf_ids, struct, inode)
--
2.43.0