[PATCH v1 bpf-next 0/3] bpf: Use the xattr kfuncs on six more inode hooks

From: Anton Protopopov

Date: Thu Dec 03 2026 - 08:32:39 EST


A BPF LSM can keep labels in security.bpf. xattrs and check them in
its hooks. For example it can refuse a new entry in a labeled
directory or an ACL change on a labeled file. The xattr kfuncs may
sleep, so only sleepable programs can call them. But inode_mkdir,
inode_link, inode_set_acl, inode_remove_acl, inode_file_setattr and
inode_copy_up are not in sleepable_lsm_hooks. Also the hooks which
create an entry get the parent directory as an inode, and there is no
kfunc to read an xattr of an inode.

Patch 1 lets sleepable programs attach to the six hooks. The ACL and
file_setattr hooks are called with the inode locked, so they go to
d_inode_locked_hooks and the verifier replaces the setters with the
_locked variants there. On inode_copy_up the setters are refused: the
caller holds the copy-up lock and a setter would wait on it. Patch 2
adds the bpf_get_inode_xattr() kfunc. Patch 3 tests both.

The other way to read a label of the parent directory would be to
make dentry->d_parent a trusted pointer. But it would be trusted on
every hook, and many hooks don't hold the lock of the parent. There a
concurrent rename can move the dentry and the old parent can be freed
while a sleepable program still reads it. The dir inode is passed to
the hook by its caller, which holds it for the duration of the hook.

Anton Protopopov (3):
bpf: Let sleepable LSM programs attach to six more inode hooks
bpf: Add a bpf_get_inode_xattr kfunc
selftests/bpf: Test xattr kfuncs on the new sleepable inode hooks

fs/bpf_fs_kfuncs.c | 48 +++++++-
kernel/bpf/bpf_lsm.c | 6 +
tools/testing/selftests/bpf/bpf_kfuncs.h | 11 ++
.../selftests/bpf/prog_tests/fs_kfuncs.c | 107 ++++++++++++++++++
.../selftests/bpf/progs/test_inode_xattr.c | 89 +++++++++++++++
.../selftests/bpf/progs/verifier_vfs_accept.c | 13 +++
.../selftests/bpf/progs/verifier_vfs_reject.c | 22 ++++
7 files changed, 295 insertions(+), 1 deletion(-)
create mode 100644 tools/testing/selftests/bpf/progs/test_inode_xattr.c

--
2.43.0