Skip to contentSkip to conversation

bpf: Use the xattr kfuncs on six more inode hooks

PATCH In review Anton Protopopov 4 participants 14 messages Dec 4

Subsystems (6)
VFS Kselftest BPF BPF selftests +2 more subsystems
lore
Share & tools
lore
#0 lore raw

v2 cover bpf: Use the xattr kfuncs on six more inode hooks

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/[email protected]

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
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/6 bpf: Add path_chmod and path_chown to d_inode_locked_hooks

The chmod_common() and chown_common() functions call the path_chmod and path_chown hooks with the inode locked. Both hooks are sleepable but not in d_inode_locked_hooks. A program can't call the xattr setters there now because path->dentry is not a trusted pointer. But the list names the hooks called with the inode locked, and the verifier would pick the wrong setters if path->dentry became trusted. Add the two hooks.

---
 fs/bpf_fs_kfuncs.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/bpf_fs_kfuncs.c +4 -0
index 35ccd98a4de6..a512d449b54d 100644
@@ -571,6 +571,10 @@ BTF_ID(func, bpf_lsm_inode_rmdir)
 BTF_ID(func, bpf_lsm_inode_setattr)
 BTF_ID(func, bpf_lsm_inode_setxattr)
 BTF_ID(func, bpf_lsm_inode_unlink)
+#ifdef CONFIG_SECURITY_PATH
+BTF_ID(func, bpf_lsm_path_chmod)
+BTF_ID(func, bpf_lsm_path_chown)
+#endif
 BTF_SET_END(d_inode_locked_hooks)
 
 bool bpf_lsm_has_d_inode_locked(const struct bpf_prog *prog)
Signature · 1 line
2.43.0
#2 lore raw

PATCH v2 2/6 bpf: Don't allow the IMA helpers on inode_getxattr and inode_getattr

In reply to Anton Protopopov

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")
---
 kernel/bpf/bpf_lsm.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
kernel/bpf/bpf_lsm.c +9 -1
index 1e2acb7581e7..608d188ffe0e 100644
@@ -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)
Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH v2 bpf-next 2/6] bpf: Don't allow the IMA helpers on inode_getxattr and inode_getattr

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] bpf: Fixes trailer cites 423f16108c9d instead of 27672f0d280a --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] bpf: Fixes trailer cites 423f16108c9d instead of 27672f0d280a --

commit 1530c83bfb984cb9b336b48e9f99703d6ca8c173
Author: Anton Protopopov <[email protected]>

Quoted 17 lines

bpf: Don't allow the IMA helpers on inode_getxattr and inode_getattr

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.

Quoted 5 lines

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")

[Severity: Low]
Commit 423f16108c9d ("bpf: Augment the set of sleepable LSM hooks") only marked bpf_lsm_inode_getattr and bpf_lsm_inode_getxattr as sleepable, and both remain sleepable after this patch. bpf_ima_inode_hash() and bpf_ima_inode_hash_allowed() were introduced later. Should this use:

Fixes 27672f0d280a ("bpf: Add a BPF helper for getting the IMA hash of an inode")
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#4 lore raw

Re: [PATCH v2 bpf-next 2/6] bpf: Don't allow the IMA helpers on inode_getxattr and inode_getattr

In reply to Anton Protopopov
TL;DR: - Possible self-deadlock on iint->mutex remains for sleepable lsm.s/file_open programs calling IMA hash helpers; the deny set misses it (pre-existing) - Fixes: tag may point at the wrong commit
Quoted 1 line

bpf: Don't allow the IMA helpers on inode_getxattr and inode_getattr

TL;DR:
- Possible self-deadlock on iint->mutex remains for sleepable lsm.s/file_open programs calling IMA hash helpers; the deny set misses it (pre-existing)
- Fixes: tag may point at the wrong commit

Quoted 12 lines

bpf: Don't allow the IMA helpers on inode_getxattr and inode_getattr

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.
[ ... ]
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.

Quoted 2 lines
Fixes 423f16108c9d ("bpf: Augment the set of sleepable LSM hooks")

The Fixes: tag points at the commit that made inode_getxattr and inode_getattr sleepable, which did not add the IMA helpers, so no deadlock existed at that point. The helper being allowed on every sleepable hook comes from the commit that introduced bpf_ima_inode_hash(). Should this be:

Fixes 27672f0d280a ("bpf: Add a BPF helper for getting the IMA hash of an inode")
Quoted 3 lines
kernel/bpf/bpf_lsm.c +0 -0

[ ... ]

Quoted 7 lines
kernel/bpf/bpf_lsm.c +3 -0
@@ -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)

kernel/bpf/bpf_lsm.c BPF_CALL_3(bpf_ima_inode_hash, struct inode *, inode, void *, dst, u32, size) · line 212

Quoted 10 lines
kernel/bpf/bpf_lsm.c +6 -1
+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);
 }

The comment says this set holds hooks which may be called with iint->mutex held, but file_open is also called that way and is still allowed. Can a sleepable lsm.s/file_open program deadlock here?

For a file opened without FMODE_READ (for example O_WRONLY):

do_open() -> security_file_post_open() -> ima_file_check()
process_measurement(..., MAY_WRITE, FILE_CHECK, ...)

    mutex_lock(&iint->mutex);
    ima_collect_measurement()
      ima_calc_file_hash()
        /* Open a new file instance in O_RDONLY if we cannot read */
        dentry_open(&file->f_path, flags, file->f_cred)
          do_dentry_open()
            security_file_open(f)
              bpf_lsm_file_open -> sleepable lsm.s/file_open program
                bpf_ima_file_hash(f, ...) -> ima_file_hash(f)
                  __ima_inode_hash()
                    iint = ima_iint_find(inode);  /* same iint */
                    mutex_lock(&iint->mutex);     /* already held */

bpf_ima_inode_hash(f->f_inode, ...) reaches the same mutex through ima_inode_hash(). iint->mutex is a plain mutex, so the task would hang, and so would later opens of that inode.

This looks reachable with the default ima_policy=appraise_tcb, whose rule {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .flags = IMA_FOWNER} has no func or mask restriction, so it matches FILE_CHECK with MAY_WRITE on root-owned files whenever IMA_COLLECTED is clear. Policies that measure on write trigger it as well.

bpf_ima_inode_hash_allowed() still returns true for bpf_lsm_file_open: it is sleepable, not in bpf_ima_inode_hash_denied_hooks, and not covered by the d_inode_locked_hooks check that ca7b97a49368 adds later in the series. None of the later patches in the series (up to bebb89f50f4a) address it.

This is the same self-deadlock the patch fixes for inode_getxattr, though it predates this patch (423f16108c9d added bpf_lsm_file_open to sleepable_lsm_hooks). Denying file_open would remove the main attach point for bpf_ima_file_hash(), so the fix may belong in IMA's internal reopen instead. Should the comment or the deny logic be adjusted, or the gap at least acknowledged?

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/47369735357

#5 lore raw

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

In reply to Anton Protopopov

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")
---
 kernel/bpf/bpf_lsm.c | 1 +
 1 file changed, 1 insertion(+)
kernel/bpf/bpf_lsm.c +1 -0
index 608d188ffe0e..6c79d4786f67 100644
@@ -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);
 }
Signature · 1 line
2.43.0
#6 lore raw

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

TL;DR: Possible self-deadlock on iint->mutex remains: a sleepable file_open BPF LSM program calling the IMA hash helpers from IMA's own dentry_open() may block, and file_open isn't denied by this…
Quoted 1 line

bpf: Don't allow the IMA hash helpers on hooks with the inode locked

TL;DR: Possible self-deadlock on iint->mutex remains: a sleepable file_open BPF LSM program calling the IMA hash helpers from IMA's own dentry_open() may block, and file_open isn't denied by this patch.

Quoted 3 lines
kernel/bpf/bpf_lsm.c +0 -0

[ ... ]

kernel/bpf/bpf_lsm.c BTF_SET_END(bpf_ima_inode_hash_denied_hooks) · line 213

Quoted 8 lines
kernel/bpf/bpf_lsm.c +1 -0
@@ -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);
 }

The commit message says inode_post_setxattr can wait for iint->mutex "which the task itself holds when IMA sets security.ima". Is inode_post_setxattr the only hook where that self-deadlock can happen?

While IMA holds iint->mutex in ima_update_xattr(), it calls ima_collect_measurement() before ima_fix_xattr(), and for a writer without FMODE_READ ima_calc_file_hash() opens a new file instance:

    /* Open a new file instance in O_RDONLY if we cannot read */
    if (!(file->f_mode & FMODE_READ)) {
        ...
        f = dentry_open(&file->f_path, flags, file->f_cred);

dentry_open() reaches security_file_open() through vfs_open() and do_dentry_open(), which runs a sleepable BPF LSM program attached to file_open. file_open is not in d_inode_locked_hooks and is not in bpf_ima_inode_hash_denied_hooks, so the helpers are still allowed there.

The helper then reaches __ima_inode_hash():

    iint = ima_iint_find(inode);
    if (iint)
        mutex_lock(&iint->mutex);

which finds the iint whose mutex the task already holds:

__fput()
security_file_release()

    ima_file_free()
      ima_check_last_writer()
        mutex_lock(&iint->mutex)
        ima_update_xattr()
          ima_collect_measurement()
            ima_calc_file_hash()
              dentry_open()
                do_dentry_open()
                  security_file_open()
                    bpf_lsm_file_open
                      bpf_ima_file_hash()
                        __ima_inode_hash()
                          mutex_lock(&iint->mutex)

The same can happen from process_measurement(), which holds iint->mutex and calls ima_collect_measurement(), for an O_WRONLY open matched by an appraise rule with no mask, such as the fowner=0 rule of ima_policy=appraise_tcb. In that case the mutex stays held, so later opens of the file also block in process_measurement().

file_open was already sleepable when 27672f0d280a added the helper, and the later commits in this series do not seem to cover it.

Could the changelog or the deny logic deal with file_open as well, for example when the hook runs from IMA's own dentry_open() in ima_calc_file_hash()?

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/47369735357

#7 lore raw

PATCH v2 4/6 bpf: Let sleepable LSM programs attach to six more inode hooks

In reply to Anton Protopopov

The bpf_get_dentry_xattr() and bpf_get_file_xattr() kfuncs may sleep, so only sleepable LSM programs can call them. The inode_mkdir, inode_link, inode_set_acl, inode_remove_acl, inode_file_setattr and inode_copy_up hooks are not in sleepable_lsm_hooks. So a program on these hooks can't read an xattr, for example to check a label kept in a security.bpf. xattr before a mkdir or an ACL change.

All of these hooks are called in process context and may sleep. The vfs_mkdir() and vfs_link() functions call their hooks with the parent directory locked. Cachefiles calls inode_mkdir with no lock held. The vfs_set_acl(), vfs_remove_acl() and vfs_fileattr_set() functions call their hooks with the target inode locked. Overlayfs calls inode_copy_up under its own copy-up lock of the inode. Add the hooks to the set.

The bpf_set_dentry_xattr() and bpf_remove_dentry_xattr() kfuncs lock the inode themselves, unless the hook is in d_inode_locked_hooks. For such hooks the verifier replaces them with the _locked variants. Add the ACL and file_setattr hooks to this list, otherwise a program which calls the setters there deadlocks. On inode_copy_up the setters can't be used at all: overlayfs would try to copy the inode up again and wait on the lock which the caller already holds. Refuse them there. Reading an xattr on inode_copy_up is fine. On inode_mkdir and inode_link the dentries passed to the hook are either negative or not locked, so the plain setters are right.

The bpf_ima_inode_hash() and bpf_ima_file_hash() helpers take iint->mutex, which IMA holds while it locks the inode or sets security.ima. The ACL and file_setattr hooks are in d_inode_locked_hooks, so the helpers are not allowed there. IMA may copy up an overlay file when it sets security.ima, so don't allow them on inode_copy_up either.

---
 fs/bpf_fs_kfuncs.c   | 18 +++++++++++++++++-
 kernel/bpf/bpf_lsm.c |  7 +++++++
 2 files changed, 24 insertions(+), 1 deletion(-)
fs/bpf_fs_kfuncs.c +17 -1
index a512d449b54d..6021e9f60ad2 100644
@@ -527,6 +527,15 @@ BTF_SET_START(bpf_inode_init_xattr_hooks)
 BTF_ID(func, bpf_lsm_inode_init_security)
 BTF_SET_END(bpf_inode_init_xattr_hooks)
 
+/*
+ * Hooks that may not set or remove an xattr. A setter on the overlay
+ * dentry of inode_copy_up copies it up again and waits on the copy-up
+ * lock the caller holds.
+ */
+BTF_SET_START(bpf_xattr_read_only_hooks)
+BTF_ID(func, bpf_lsm_inode_copy_up)
+BTF_SET_END(bpf_xattr_read_only_hooks)
+
 static int bpf_fs_kfuncs_filter(const struct bpf_prog *prog, u32 kfunc_id)
 {
 	if (!btf_id_set8_contains(&bpf_fs_kfunc_set_ids, kfunc_id))
@@ -539,8 +548,12 @@ static int bpf_fs_kfuncs_filter(const struct bpf_prog *prog, u32 kfunc_id)
 			return -EACCES;
 		return 0;
 	}
-	if (prog->type == BPF_PROG_TYPE_LSM)
+	if (prog->type == BPF_PROG_TYPE_LSM) {
+		if (btf_id_set_contains(&bpf_fs_kfunc_lsm_only_ids, kfunc_id) &&
+		    btf_id_set_contains(&bpf_xattr_read_only_hooks, prog->aux->attach_btf_id))
+			return -EACCES;
 		return 0;
+	}
 	if (prog->type != BPF_PROG_TYPE_STRUCT_OPS)
 		return -EACCES;
 	/* ->st_ops is unset during the cfg pass; enforced once it is set. */
@@ -563,11 +576,14 @@ static int bpf_fs_kfuncs_filter(const struct bpf_prog *prog, u32 kfunc_id)
  * should call bpf_[set|remove]_dentry_xattr.
  */
 BTF_SET_START(d_inode_locked_hooks)
+BTF_ID(func, bpf_lsm_inode_file_setattr)
 BTF_ID(func, bpf_lsm_inode_post_removexattr)
 BTF_ID(func, bpf_lsm_inode_post_setattr)
 BTF_ID(func, bpf_lsm_inode_post_setxattr)
+BTF_ID(func, bpf_lsm_inode_remove_acl)
 BTF_ID(func, bpf_lsm_inode_removexattr)
 BTF_ID(func, bpf_lsm_inode_rmdir)
+BTF_ID(func, bpf_lsm_inode_set_acl)
 BTF_ID(func, bpf_lsm_inode_setattr)
 BTF_ID(func, bpf_lsm_inode_setxattr)
 BTF_ID(func, bpf_lsm_inode_unlink)
kernel/bpf/bpf_lsm.c +7 -0
index 6c79d4786f67..c4b626b0dbda 100644
@@ -200,6 +200,7 @@ BPF_CALL_3(bpf_ima_inode_hash, struct inode *, inode, void *, dst, u32, 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_copy_up)
 BTF_ID(func, bpf_lsm_inode_getattr)
 BTF_ID(func, bpf_lsm_inode_getxattr)
 BTF_SET_END(bpf_ima_inode_hash_denied_hooks)
@@ -349,18 +350,24 @@ BTF_ID(func, bpf_lsm_file_open)
 BTF_ID(func, bpf_lsm_file_post_open)
 BTF_ID(func, bpf_lsm_file_receive)
 
+BTF_ID(func, bpf_lsm_inode_copy_up)
 BTF_ID(func, bpf_lsm_inode_create)
+BTF_ID(func, bpf_lsm_inode_file_setattr)
 BTF_ID(func, bpf_lsm_inode_free_security)
 BTF_ID(func, bpf_lsm_inode_getattr)
 BTF_ID(func, bpf_lsm_inode_getxattr)
+BTF_ID(func, bpf_lsm_inode_link)
+BTF_ID(func, bpf_lsm_inode_mkdir)
 BTF_ID(func, bpf_lsm_inode_mknod)
 BTF_ID(func, bpf_lsm_inode_need_killpriv)
 BTF_ID(func, bpf_lsm_inode_post_setxattr)
 BTF_ID(func, bpf_lsm_inode_post_removexattr)
 BTF_ID(func, bpf_lsm_inode_readlink)
+BTF_ID(func, bpf_lsm_inode_remove_acl)
 BTF_ID(func, bpf_lsm_inode_removexattr)
 BTF_ID(func, bpf_lsm_inode_rename)
 BTF_ID(func, bpf_lsm_inode_rmdir)
+BTF_ID(func, bpf_lsm_inode_set_acl)
 BTF_ID(func, bpf_lsm_inode_setattr)
 BTF_ID(func, bpf_lsm_inode_setxattr)
 BTF_ID(func, bpf_lsm_inode_symlink)
Signature · 1 line
2.43.0
#8 lore raw

Re: [PATCH v2 bpf-next 4/6] bpf: Let sleepable LSM programs attach to six more inode hooks

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] bpf: bpf_real_data_inode() can nest OVL_I()->lock on new sleepable LSM hooks - [High] bpf: missing…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] bpf: bpf_real_data_inode() can nest OVL_I()->lock on new sleepable LSM hooks
- [High] bpf: missing inode_mkdir and inode_link in bpf_ima_inode_hash_denied_hooks
--

commit 83267828169188f48079e5f449fe1b875d3553b4
Author: Anton Protopopov <[email protected]>

bpf: Let sleepable LSM programs attach to six more inode hooks

This patch adds inode_mkdir, inode_link, inode_set_acl, inode_remove_acl, inode_file_setattr, and inode_copy_up to sleepable_lsm_hooks, updates d_inode_locked_hooks for the ACL and file_setattr hooks, and restricts xattr setters and IMA hash helpers on inode_copy_up.

Quoted 4 lines
fs/bpf_fs_kfuncs.c +0 -0
index a512d449b54d9..6021e9f60ad28 100644

[ ... ]

Quoted 4 lines
fs/bpf_fs_kfuncs.c +0 -0
@@ -539,8 +548,12 @@ static int bpf_fs_kfuncs_filter(const struct bpf_prog *prog, u32 kfunc_id)
 			return -EACCES;
 		return 0;
 	}

fs/bpf_fs_kfuncs.c static int bpf_fs_kfuncs_filter(const struct bpf_prog *prog, u32 kfunc_id) · line 559

Quoted 10 lines
fs/bpf_fs_kfuncs.c +5 -1
-	if (prog->type == BPF_PROG_TYPE_LSM)
+	if (prog->type == BPF_PROG_TYPE_LSM) {
+		if (btf_id_set_contains(&bpf_fs_kfunc_lsm_only_ids, kfunc_id) &&
+		    btf_id_set_contains(&bpf_xattr_read_only_hooks, prog->aux->attach_btf_id))
+			return -EACCES;
 		return 0;
+	}
 	if (prog->type != BPF_PROG_TYPE_STRUCT_OPS)
 		return -EACCES;
 	/* ->st_ops is unset during the cfg pass; enforced once it is set. */

[Severity: High]
Can sleepable LSM programs on bpf_lsm_inode_link and bpf_lsm_inode_copy_up still call bpf_real_data_inode() and deadlock on OVL_I(inode)->lock?

bpf_fs_kfuncs_filter() only denies xattr setters on inode_copy_up, leaving bpf_real_data_inode() allowed on both newly sleepable hooks. On overlayfs mounted with metacopy=on and verity=on, a metadata-only copy-up in ovl_copy_up_tmpfile() or ovl_copy_up_workdir() sets OVL_HAS_DIGEST, clears OVL_VERIFIED_DIGEST, and leaves OVL_UPPERDATA unset.

When a process executing such an fs-verity lower file calls link() on its own executable, it self-deadlocks if a sleepable inode_link program calls bpf_real_data_inode(bpf_get_task_exe_file(current)):

ovl_link()
ovl_copy_up(old) /* metacopy: clears OVL_VERIFIED_DIGEST */
ovl_nlink_start(old) /* locks OVL_I(inode)->lock */
ovl_do_link() -> vfs_link() -> security_inode_link()
bpf_real_data_inode(bpf_get_task_exe_file(current))
ovl_d_real() -> ovl_maybe_validate_verity()
ovl_inode_lock_interruptible(inode) <-- self-deadlock

Two tasks concurrently linking each other's executables can also ABBA-deadlock on their OVL_I()->lock mutexes. On bpf_lsm_inode_copy_up, ovl_copy_up_one() holds OVL_I()->lock of the target file across security_inode_copy_up(), so calling bpf_real_data_inode() on a metacopied executable nests another OVL_I()->lock of the same lockdep class and triggers a recursive-locking violation.

Should bpf_fs_kfuncs_filter() also reject bpf_real_data_inode() on hooks that can run under OVL_I()->lock?

[ ... ]

kernel/bpf/bpf_lsm.c BPF_CALL_3(bpf_ima_inode_hash, struct inode *, inode, void *, dst, u32, size) · line 206

Quoted 12 lines
kernel/bpf/bpf_lsm.c +1 -0
index 6c79d4786f675..c4b626b0dbdac 100644
@@ -200,6 +200,7 @@ BPF_CALL_3(bpf_ima_inode_hash, struct inode *, inode, void *, dst, u32, 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_copy_up)
 BTF_ID(func, bpf_lsm_inode_getattr)
 BTF_ID(func, bpf_lsm_inode_getxattr)
 BTF_SET_END(bpf_ima_inode_hash_denied_hooks)

[Severity: High]
Should bpf_lsm_inode_mkdir and bpf_lsm_inode_link also be added to bpf_ima_inode_hash_denied_hooks?

Both hooks are added to sleepable_lsm_hooks without being in d_inode_locked_hooks or bpf_ima_inode_hash_denied_hooks, so bpf_ima_inode_hash_allowed() now permits bpf_ima_inode_hash() and bpf_ima_file_hash() there. However, the same IMA-triggered overlayfs copy-up path holds iint->mutex into both hooks: ovl_copy_up_workdir() -> ovl_create_real() -> vfs_mkdir() -> security_inode_mkdir() when copying up lower-only ancestor directories, and ovl_copy_up_tmpfile() -> ovl_do_link() -> vfs_link() -> security_inode_link() when linking the copied-up file.

With IMA or EVM appraisal in fix mode, a sleepable LSM program on either hook that calls bpf_ima_file_hash() or bpf_ima_inode_hash() (for example on bpf_get_task_exe_file(current)) causes an ABBA deadlock with the upper directory's i_rwsem:

CPU0 CPU1
---- ----
lock(&iint->mutex)

                                  lock(&upperdir->i_rwsem)
lock(&upperdir->i_rwsem)
                                  lock(&iint->mutex)

*** DEADLOCK ***

CPU0: process_measurement() -> ima_fix_xattr() -> ovl_copy_up()

      -> ovl_copy_up_tmpfile() -> ovl_do_link()
CPU1: ovl_link() -> ovl_start_creating_upper() -> vfs_link()
      -> security_inode_link() -> bpf_ima_file_hash()

It also self-deadlocks directly on iint->mutex when a task triggers appraisal on its own lower-only overlayfs executable.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#9 lore raw

PATCH v2 5/6 bpf: Add a bpf_get_inode_xattr kfunc

In reply to Anton Protopopov

The inode_create, inode_mkdir, inode_mknod, inode_symlink, inode_link and inode_rename hooks get the parent directory as an inode and the new entry as a dentry. To check a label of the directory, a program needs to read its xattr, but bpf_get_dentry_xattr() takes a dentry. The only dentry of the directory at hand is dentry->d_parent, and the verifier doesn't trust it.

Making d_parent trusted would be wrong. It would be trusted on every hook, and many hooks don't hold the lock of the parent directory. For example, inode_getxattr and inode_readlink hold no lock at all. A concurrent rename can then move the dentry to another directory, and the old parent can be freed while a sleepable program still uses it.

Add the bpf_get_inode_xattr() kfunc which reads an xattr of an inode through one of its dentries. Xattrs belong to the inode, but some filesystems like cifs build a path from the dentry. So take a hashed dentry with d_find_alias() if there is one. An unlinked file which is still open has none, so take any with d_find_any_alias() then, as inode_doinit_with_dentry() in SELinux does. The kfunc doesn't change xattrs, so the setters still get only the dentries passed to the hook, and d_inode_locked_hooks stays correct for them.

---
 fs/bpf_fs_kfuncs.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
fs/bpf_fs_kfuncs.c +33 -0
index 6021e9f60ad2..4e2b371fd0b5 100644
@@ -169,6 +169,38 @@ __bpf_kfunc int bpf_get_file_xattr(struct file *file, const char *name__str,
 	return bpf_get_dentry_xattr(dentry, name__str, value_p);
 }
 
+/**
+ * bpf_get_inode_xattr - get xattr of an inode
+ * @inode: inode to get xattr from
+ * @name__str: name of the xattr
+ * @value_p: output buffer of the xattr value
+ *
+ * Get xattr *name__str* of *inode* and store the output in *value_ptr*.
+ * The xattr is read through a hashed dentry of *inode* if it has one and
+ * through any dentry otherwise.
+ *
+ * For security reasons, only *name__str* with prefixes "user." or
+ * "security.bpf." are allowed.
+ *
+ * Return: length of the xattr value on success, -ENOENT if *inode* has
+ * no dentry, another negative value on error.
+ */
+__bpf_kfunc int bpf_get_inode_xattr(struct inode *inode, const char *name__str,
+				    struct bpf_dynptr *value_p)
+{
+	struct dentry *dentry;
+	int ret;
+
+	dentry = d_find_alias(inode);
+	if (!dentry)
+		dentry = d_find_any_alias(inode);
+	if (!dentry)
+		return -ENOENT;
+	ret = bpf_get_dentry_xattr(dentry, name__str, value_p);
+	dput(dentry);
+	return ret;
+}
+
 __bpf_kfunc_end_defs();
 
 static int bpf_xattr_write_permission(const char *name, struct inode *inode)
@@ -506,6 +538,7 @@ BTF_ID_FLAGS(func, bpf_put_file, KF_RELEASE)
 BTF_ID_FLAGS(func, bpf_path_d_path)
 BTF_ID_FLAGS(func, bpf_get_dentry_xattr, KF_SLEEPABLE)
 BTF_ID_FLAGS(func, bpf_get_file_xattr, KF_SLEEPABLE)
+BTF_ID_FLAGS(func, bpf_get_inode_xattr, KF_SLEEPABLE)
 BTF_ID_FLAGS(func, bpf_set_dentry_xattr, KF_SLEEPABLE)
 BTF_ID_FLAGS(func, bpf_remove_dentry_xattr, KF_SLEEPABLE)
 BTF_ID_FLAGS(func, bpf_real_data_inode, KF_SLEEPABLE | KF_RET_NULL)
Signature · 1 line
2.43.0
#10 lore raw

PATCH v2 6/6 selftests/bpf: Test xattr kfuncs on the new sleepable inode hooks

In reply to Anton Protopopov

Add the inode_xattr subtest to fs_kfuncs. Programs on inode_mkdir and inode_link read an xattr of the parent directory with the new bpf_get_inode_xattr(). Programs on inode_set_acl, inode_remove_acl and inode_file_setattr set and remove an xattr of the file. The verifier must replace the setters with the _locked variants there, otherwise these calls deadlock.

Add to verifier_vfs_accept a program on inode_copy_up which reads an xattr, and to verifier_vfs_reject two programs which try to set and remove one there.

---
 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 ++++
 5 files changed, 242 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/test_inode_xattr.c
tools/testing/selftests/bpf/bpf_kfuncs.h +11 -0
index 1b408fe9e0fb..71601eafae6f 100644
@@ -91,6 +91,17 @@ extern int bpf_set_dentry_xattr(struct dentry *dentry, const char *name__str,
 				const struct bpf_dynptr *value_p, int flags) __ksym __weak;
 extern int bpf_remove_dentry_xattr(struct dentry *dentry, const char *name__str) __ksym __weak;
 
+/*
+ * Description
+ *  Returns xattr of an inode, read through a dentry of the inode
+ * Returns
+ *  Length of the xattr value, -ENOENT if the inode has no dentry or
+ *  another negative error code
+ */
+struct inode;
+extern int bpf_get_inode_xattr(struct inode *inode, const char *name__str,
+			       struct bpf_dynptr *value_p) __ksym __weak;
+
 /*
  * Description
  *  Attach a xattr to an inode that is being created, from a program on the
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c +107 -0

136 lines not on this page · raw message

tools/testing/selftests/bpf/progs/test_inode_xattr.c new file +89 -0

89 lines not on this page · raw message

tools/testing/selftests/bpf/progs/verifier_vfs_accept.c +13 -0
index 25f37a3dd7cf..38edcf1b34e8 100644
@@ -8,8 +8,10 @@
 
 #include "bpf_misc.h"
 #include "bpf_experimental.h"
+#include "bpf_kfuncs.h"
 
 static char buf[64];
+static const char xattr_name[] = "security.bpf.foo";
 
 SEC("lsm.s/file_open")
 __success
@@ -117,4 +119,15 @@ int BPF_PROG(inode_rename, struct inode *old_dir, struct dentry *old_dentry,
 	return 0;
 }
 
+SEC("lsm.s/inode_copy_up")
+__success
+int BPF_PROG(inode_copy_up_get_xattr, struct dentry *src, struct cred **new)
+{
+	struct bpf_dynptr value_ptr;
+
+	bpf_dynptr_from_mem(buf, sizeof(buf), 0, &value_ptr);
+	bpf_get_dentry_xattr(src, xattr_name, &value_ptr);
+	return 0;
+}
+
 char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/progs/verifier_vfs_reject.c +22 -0
index ed2bd2faebf3..96ad9d895af9 100644
@@ -9,8 +9,10 @@
 
 #include "bpf_misc.h"
 #include "bpf_experimental.h"
+#include "bpf_kfuncs.h"
 
 static char buf[PATH_MAX];
+static const char xattr_name[] = "security.bpf.foo";
 
 SEC("lsm.s/file_open")
 __failure __msg("invalid mem access 'scalar'")
@@ -192,4 +194,24 @@ int BPF_PROG(inode_rename, struct inode *old_dir, struct dentry *old_dentry,
 		return -EACCES;
 	return 0;
 }
+
+SEC("lsm.s/inode_copy_up")
+__failure __msg("calling kernel function bpf_set_dentry_xattr is not allowed")
+int BPF_PROG(inode_copy_up_set_xattr, struct dentry *src, struct cred **new)
+{
+	struct bpf_dynptr value_ptr;
+
+	bpf_dynptr_from_mem(buf, 8, 0, &value_ptr);
+	bpf_set_dentry_xattr(src, xattr_name, &value_ptr, 0);
+	return 0;
+}
+
+SEC("lsm.s/inode_copy_up")
+__failure __msg("calling kernel function bpf_remove_dentry_xattr is not allowed")
+int BPF_PROG(inode_copy_up_remove_xattr, struct dentry *src, struct cred **new)
+{
+	bpf_remove_dentry_xattr(src, xattr_name);
+	return 0;
+}
+
 char _license[] SEC("license") = "GPL";
Signature · 1 line
2.43.0
#11 lore raw

Re: [PATCH v2 bpf-next 6/6] selftests/bpf: Test xattr kfuncs on the new sleepable inode hooks

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] selftests/bpf: test_inode_xattr() lacks ACL and fileattr skip handling --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] selftests/bpf: test_inode_xattr() lacks ACL and fileattr skip handling
--

commit ce364c49218b9ec40886dd37424d160ba972d101
Author: Anton Protopopov <[email protected]>

selftests/bpf: Test xattr kfuncs on the new sleepable inode hooks

This commit adds the inode_xattr subtest to fs_kfuncs to test reading, setting, and removing xattrs on the new sleepable inode LSM hooks, and adds positive and negative verifier tests for inode_copy_up.

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c +0 -0
index 43a26ec69a8e3..ad467c7663808 100644

[ ... ]

Quoted 1 line
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c +0 -0
@@ -194,6 +202,102 @@ static void test_set_remove_xattr(void)

[ ... ]

Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c +2 -0
+static void test_inode_xattr(void)
+{

[ ... ]

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c +7 -0
+	/* inode_set_acl, inode_remove_acl and inode_file_setattr change xattrs */
+	err = setxattr(file, "system.posix_acl_access", &acl, sizeof(acl), 0);
+	if (ASSERT_OK(err, "set acl")) {
+		ASSERT_EQ(skel->data->set_acl_ret, 0, "set_acl_ret");
+		ASSERT_TRUE(has_bar(skel, file), "bar after set acl");
+
+		ASSERT_OK(removexattr(file, "system.posix_acl_access"), "remove acl");

tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c static void test_set_remove_xattr(void) · line 289

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c +10 -0
+		ASSERT_EQ(skel->data->remove_acl_ret, 0, "remove_acl_ret");
+		err = getxattr(file, skel->rodata->xattr_bar, NULL, 0);
+		ASSERT_TRUE(err == -1 && errno == ENODATA, "bar after remove acl");
+	}
+
+	if (ASSERT_OK(ioctl(fd, FS_IOC_GETFLAGS, &flags), "FS_IOC_GETFLAGS") &&
+	    ASSERT_OK(ioctl(fd, FS_IOC_SETFLAGS, &flags), "FS_IOC_SETFLAGS")) {
+		ASSERT_EQ(skel->data->file_setattr_ret, 0, "file_setattr_ret");
+		ASSERT_TRUE(has_bar(skel, file), "bar after file setattr");
+	}

[Severity: Medium]
Should test_inode_xattr() skip on EOPNOTSUPP for system.posix_acl_access and on ENOTTY/EOPNOTSUPP for FS_IOC_GETFLAGS and FS_IOC_SETFLAGS?

The base tools/testing/selftests/bpf/config enables CONFIG_TMPFS_XATTR without CONFIG_TMPFS_POSIX_ACL, so a tmpfs /tmp passes the initial security.bpf.foo probe and then fails setxattr() on system.posix_acl_access with EOPNOTSUPP instead of skipping.

Under CONFIG_FS_POSIX_ACL=y without SB_POSIXACL, vfs_set_acl() calls security_inode_set_acl() before set_posix_acl() returns -EOPNOTSUPP. Skipping the removexattr() block then leaves security.bpf.bar on file for the subsequent FS_IOC_SETFLAGS has_bar() check.

Similarly, filesystems that support xattrs without fileattr_get or fileattr_set (such as 9p or NFS) return ENOTTY from FS_IOC_GETFLAGS or FS_IOC_SETFLAGS, causing the subtest to fail instead of skipping.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#13 lore raw
Quoted 5 lines · Alexei Starovoitov
Quoted 1 line · Anton Protopopov
 fs/bpf_fs_kfuncs.c                            |  55 ++++++++-

Pls do not respin due to bot findings until you get an ok from Christian.

Ok, got it, thanks.

Quoted 2 lines

pw-bot: cr