BPF List
 help / color / mirror / Atom feed
* [PATCH v2 bpf-next 0/6] bpf: Use the xattr kfuncs on six more inode hooks
@ 2026-12-04 15:54 Anton Protopopov
  2026-12-04 15:54 ` [PATCH v2 bpf-next 1/6] bpf: Add path_chmod and path_chown to d_inode_locked_hooks Anton Protopopov
                   ` (6 more replies)
  0 siblings, 7 replies; 14+ messages in thread
From: Anton Protopopov @ 2026-12-04 15:54 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau,
	Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis,
	Ihor Solodrai, KP Singh, Matt Bobrowski, Alexander Viro,
	Christian Brauner, Jan Kara, Mimi Zohar, Roberto Sassu, bpf,
	linux-integrity, linux-fsdevel, linux-kernel
  Cc: Anton Protopopov

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.

Patches 1-3 fix deadlocks of the bpf_ima_inode_hash() and
bpf_ima_file_hash() helpers. Both take iint->mutex, which IMA takes
before the inode lock. So the helpers can deadlock on hooks called
with the inode locked. Patch 1 adds path_chmod and path_chown, which
are called with the inode locked, to d_inode_locked_hooks. Patch 2
doesn't allow the helpers on inode_getxattr and inode_getattr, which
are sometimes called with the inode locked. Patch 3 doesn't allow
them on the hooks in d_inode_locked_hooks.

Patch 4 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. The IMA
helpers are refused there too, since IMA may copy up a file while it
holds iint->mutex. Patch 5 adds the bpf_get_inode_xattr() kfunc.
Patch 6 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.

Some deadlocks stay open, robots will report on them, but they're not
introduced, and are out of scope of this patch series:

  * A program on inode_getxattr may call the setters while EVM holds
    the inode lock to update security.evm. Refusing the setters there
    would break existing programs: the selftest of the setters uses
    this hook.

  * IMA reopens a file which isn't open for reading to hash it, and
    it holds iint->mutex while it does. So a file_open program which
    calls the IMA helpers can deadlock.

  * A program can get the executable of the task with
    bpf_get_task_exe_file() and pass it to the IMA helpers. A stacked
    filesystem may call hooks like file_open or inode_rename while
    the inode of that executable is locked.

(These need a fix in IMA or in the IMA helpers rather than in the
lists of hooks.)

v1 -> v2:
  * Add patches 1-3 which fix deadlocks of the IMA hash helpers
  * Don't allow the IMA hash helpers on inode_copy_up (patch 4)
  * Read the xattr through a hashed dentry if the inode has one
    (patch 5)

v1: https://lore.kernel.org/bpf/20261203134411.1092433-1-a.s.protopopov@gmail.com

Anton Protopopov (6):
  bpf: Add path_chmod and path_chown to d_inode_locked_hooks
  bpf: Don't allow the IMA helpers on inode_getxattr and inode_getattr
  bpf: Don't allow the IMA hash helpers on hooks with the inode locked
  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                            |  55 ++++++++-
 kernel/bpf/bpf_lsm.c                          |  18 ++-
 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, 313 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/test_inode_xattr.c

-- 
2.43.0


^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2026-12-04 19:59 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 15:54 [PATCH v2 bpf-next 0/6] bpf: Use the xattr kfuncs on six more inode hooks Anton Protopopov
2026-12-04 15:54 ` [PATCH v2 bpf-next 1/6] bpf: Add path_chmod and path_chown to d_inode_locked_hooks Anton Protopopov
2026-12-04 15:54 ` [PATCH v2 bpf-next 2/6] bpf: Don't allow the IMA helpers on inode_getxattr and inode_getattr Anton Protopopov
2026-12-04 16:07   ` sashiko-bot
2026-12-04 16:15   ` bot+bpf-ci
2026-12-04 15:54 ` [PATCH v2 bpf-next 3/6] bpf: Don't allow the IMA hash helpers on hooks with the inode locked Anton Protopopov
2026-12-04 16:15   ` bot+bpf-ci
2026-12-04 15:54 ` [PATCH v2 bpf-next 4/6] bpf: Let sleepable LSM programs attach to six more inode hooks Anton Protopopov
2026-12-04 16:13   ` sashiko-bot
2026-12-04 15:54 ` [PATCH v2 bpf-next 5/6] bpf: Add a bpf_get_inode_xattr kfunc Anton Protopopov
2026-12-04 15:54 ` [PATCH v2 bpf-next 6/6] selftests/bpf: Test xattr kfuncs on the new sleepable inode hooks Anton Protopopov
2026-12-04 16:09   ` sashiko-bot
2026-12-04 19:48 ` [PATCH v2 bpf-next 0/6] bpf: Use the xattr kfuncs on six more " Alexei Starovoitov
2026-12-04 19:59   ` Anton Protopopov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox