Skip to contentSkip to conversation

bpf: Use the xattr kfuncs on six more inode hooks

PATCH Superseded Anton Protopopov 2 participants 11 messages Dec 3

Subsystems (5)
VFS Kselftest BPF BPF selftests +1 more subsystems

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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.

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

PATCH 1/3 bpf: Let sleepable LSM programs attach to six more inode hooks

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.

---
 fs/bpf_fs_kfuncs.c   | 18 +++++++++++++++++-
 kernel/bpf/bpf_lsm.c |  6 ++++++
 2 files changed, 23 insertions(+), 1 deletion(-)
fs/bpf_fs_kfuncs.c +17 -1
index 35ccd98a4de6..1b973bd4e535 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 +6 -0
index 1e2acb7581e7..62303210dde9 100644
@@ -340,18 +340,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
#2 lore raw

Re: [PATCH v1 bpf-next 1/3] bpf: Let sleepable LSM programs attach to six more inode hooks

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] bpf: deadlocks on iint->mutex via bpf_ima_inode_hash() on new sleepable hooks --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] bpf: deadlocks on iint->mutex via bpf_ima_inode_hash() on new sleepable hooks
--

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

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

This commit adds six inode LSM hooks to sleepable_lsm_hooks, adds three of them to d_inode_locked_hooks for xattr setters, and blocks xattr setters on inode_copy_up.

Quoted 19 lines
kernel/bpf/bpf_lsm.c +4 -0
index 1e2acb7581e70..62303210dde91 100644
@@ -340,18 +340,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)

kernel/bpf/bpf_lsm.c BTF_ID(func, bpf_lsm_file_open) · line 363

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

[Severity: High]
Can adding these hooks to sleepable_lsm_hooks deadlock on iint->mutex via bpf_ima_inode_hash()?

bpf_ima_inode_hash_allowed() only checks bpf_lsm_is_sleepable_hook(), so sleepable LSM programs on inode_set_acl, inode_remove_acl, inode_file_setattr, and inode_copy_up can now call bpf_ima_inode_hash() on dentry->d_inode, which locks iint->mutex in __ima_inode_hash().

vfs_set_acl(), vfs_remove_acl(), and vfs_fileattr_set() hold inode->i_rwsem across their hooks, inverting IMA's iint->mutex -> inode->i_rwsem lock order and causing an ABBA deadlock:

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

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

*** DEADLOCK ***

CPU0: process_measurement() -> inode_lock() -> ima_appraise_measurement()
CPU1: vfs_set_acl() -> security_inode_set_acl() -> bpf_ima_inode_hash()

On overlayfs under IMA or EVM fix mode, security_inode_copy_up() can also run while iint->mutex is already held, causing a single-thread AA deadlock (and normal copy-up inverts OVL_I(inode)->lock against iint->mutex across CPUs):

process_measurement()
mutex_lock(&iint->mutex)
inode_lock(inode)
ima_appraise_measurement()

    ima_fix_xattr()
      __vfs_setxattr_noperm()
        ovl_xattr_set()
          ovl_copy_up()
            ovl_copy_up_start()  /* locks OVL_I(inode)->lock */
            ...
              security_inode_copy_up()
                bpf_ima_inode_hash()
                  mutex_lock(&iint->mutex)  <-- deadlocks

Should bpf_ima_inode_hash_allowed() exclude hooks called with inode->i_rwsem or OVL_I(inode)->lock held, or reachable under iint->mutex?

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

Re: [PATCH v1 bpf-next 1/3] bpf: Let sleepable LSM programs attach to six more inode hooks

Quoted 88 lines · On 26/12/03 01:54PM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] bpf: deadlocks on iint->mutex via bpf_ima_inode_hash() on new sleepable hooks
--

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

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

This commit adds six inode LSM hooks to sleepable_lsm_hooks, adds three of them to d_inode_locked_hooks for xattr setters, and blocks xattr setters on inode_copy_up.

Quoted 19 lines
kernel/bpf/bpf_lsm.c +4 -0
index 1e2acb7581e70..62303210dde91 100644
@@ -340,18 +340,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)

kernel/bpf/bpf_lsm.c BTF_ID(func, bpf_lsm_file_open) · line 363

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

[Severity: High]
Can adding these hooks to sleepable_lsm_hooks deadlock on iint->mutex via bpf_ima_inode_hash()?

bpf_ima_inode_hash_allowed() only checks bpf_lsm_is_sleepable_hook(), so sleepable LSM programs on inode_set_acl, inode_remove_acl, inode_file_setattr, and inode_copy_up can now call bpf_ima_inode_hash() on dentry->d_inode, which locks iint->mutex in __ima_inode_hash().

vfs_set_acl(), vfs_remove_acl(), and vfs_fileattr_set() hold inode->i_rwsem across their hooks, inverting IMA's iint->mutex -> inode->i_rwsem lock order and causing an ABBA deadlock:

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

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

*** DEADLOCK ***

CPU0: process_measurement() -> inode_lock() -> ima_appraise_measurement()
CPU1: vfs_set_acl() -> security_inode_set_acl() -> bpf_ima_inode_hash()

On overlayfs under IMA or EVM fix mode, security_inode_copy_up() can also run while iint->mutex is already held, causing a single-thread AA deadlock (and normal copy-up inverts OVL_I(inode)->lock against iint->mutex across CPUs):

process_measurement()
mutex_lock(&iint->mutex)
inode_lock(inode)
ima_appraise_measurement()

    ima_fix_xattr()
      __vfs_setxattr_noperm()
        ovl_xattr_set()
          ovl_copy_up()
            ovl_copy_up_start()  /* locks OVL_I(inode)->lock */
            ...
              security_inode_copy_up()
                bpf_ima_inode_hash()
                  mutex_lock(&iint->mutex)  <-- deadlocks
Quoted 2 lines

Should bpf_ima_inode_hash_allowed() exclude hooks called with inode->i_rwsem or OVL_I(inode)->lock held, or reachable under iint->mutex?

Yeah. I think I will add another patch with

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) &&
+ !bpf_lsm_has_d_inode_locked(prog);
}

as it might affect older hooks as well, not only those added by the series.

And then add the security_inode_copy_up as an exception (because it doesn't look to fit into the d_inode_locked list).

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

Re: [PATCH v1 bpf-next 1/3] bpf: Let sleepable LSM programs attach to six more inode hooks

Quoted 102 lines · Anton Protopopov
Quoted 88 lines · On 26/12/03 01:54PM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] bpf: deadlocks on iint->mutex via bpf_ima_inode_hash() on new sleepable hooks
--

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

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

This commit adds six inode LSM hooks to sleepable_lsm_hooks, adds three of them to d_inode_locked_hooks for xattr setters, and blocks xattr setters on inode_copy_up.

Quoted 19 lines
kernel/bpf/bpf_lsm.c +4 -0
index 1e2acb7581e70..62303210dde91 100644
@@ -340,18 +340,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)

kernel/bpf/bpf_lsm.c BTF_ID(func, bpf_lsm_file_open) · line 363

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

[Severity: High]
Can adding these hooks to sleepable_lsm_hooks deadlock on iint->mutex via bpf_ima_inode_hash()?

bpf_ima_inode_hash_allowed() only checks bpf_lsm_is_sleepable_hook(), so sleepable LSM programs on inode_set_acl, inode_remove_acl, inode_file_setattr, and inode_copy_up can now call bpf_ima_inode_hash() on dentry->d_inode, which locks iint->mutex in __ima_inode_hash().

vfs_set_acl(), vfs_remove_acl(), and vfs_fileattr_set() hold inode->i_rwsem across their hooks, inverting IMA's iint->mutex -> inode->i_rwsem lock order and causing an ABBA deadlock:

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

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

*** DEADLOCK ***

CPU0: process_measurement() -> inode_lock() -> ima_appraise_measurement()
CPU1: vfs_set_acl() -> security_inode_set_acl() -> bpf_ima_inode_hash()

On overlayfs under IMA or EVM fix mode, security_inode_copy_up() can also run while iint->mutex is already held, causing a single-thread AA deadlock (and normal copy-up inverts OVL_I(inode)->lock against iint->mutex across CPUs):

process_measurement()
mutex_lock(&iint->mutex)
inode_lock(inode)
ima_appraise_measurement()

    ima_fix_xattr()
      __vfs_setxattr_noperm()
        ovl_xattr_set()
          ovl_copy_up()
            ovl_copy_up_start()  /* locks OVL_I(inode)->lock */
            ...
              security_inode_copy_up()
                bpf_ima_inode_hash()
                  mutex_lock(&iint->mutex)  <-- deadlocks
Quoted 2 lines

Should bpf_ima_inode_hash_allowed() exclude hooks called with inode->i_rwsem or OVL_I(inode)->lock held, or reachable under iint->mutex?

Yeah. I think I will add another patch with

static bool bpf_ima_inode_hash_allowed(const struct bpf_prog *prog) {

(quoted diff) +2 -1
-       return bpf_lsm_is_sleepable_hook(prog->aux->attach_btf_id);
+       return bpf_lsm_is_sleepable_hook(prog->aux->attach_btf_id) &&
+              !bpf_lsm_has_d_inode_locked(prog);
 }
Quoted 2 lines

as it might affect older hooks as well, not only those added by the series.

(A small update: looks like path_chmod and path_chown should be put into the locked list, and maybe more hooks. I will also add this change to v2.)

Quoted 7 lines

And then add the security_inode_copy_up as an exception (because it doesn't look to fit into the d_inode_locked list).

Quoted 2 lines

--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1

#5 lore raw

PATCH 2/3 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 any of its dentries, found with d_find_any_alias(). Xattrs belong to the inode, so any dentry will do. d_find_alias() would not:
it skips the unhashed dentry of an unlinked file which is still open. 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 | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
fs/bpf_fs_kfuncs.c +30 -0
index 1b973bd4e535..654340a0bc70 100644
@@ -169,6 +169,35 @@ __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 any dentry of *inode*.
+ *
+ * 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_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 +535,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
#6 lore raw

Re: [PATCH v1 bpf-next 2/3] bpf: Add a bpf_get_inode_xattr kfunc

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] bpf: bpf_get_inode_xattr() ignores hashed aliases via d_find_any_alias() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] bpf: bpf_get_inode_xattr() ignores hashed aliases via d_find_any_alias()
--

commit 411b1cdf4c29cccf8363d8423d9bb02ec61b59b9
Author: Anton Protopopov <[email protected]>

bpf: Add a bpf_get_inode_xattr kfunc

This commit adds the sleepable bpf_get_inode_xattr() kfunc to read an extended attribute from an inode by looking up one of its dentries with d_find_any_alias().

Quoted 5 lines
fs/bpf_fs_kfuncs.c +0 -0
index 1b973bd4e5355..654340a0bc70e 100644
@@ -169,6 +169,35 @@ __bpf_kfunc int bpf_get_file_xattr(struct file *file, const char *name__str,

[ ... ]

fs/bpf_fs_kfuncs.c __bpf_kfunc int bpf_get_file_xattr(struct file *file, const char *name__str, · line 199

Quoted 13 lines
fs/bpf_fs_kfuncs.c +13 -0
+__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_any_alias(inode);
+	if (!dentry)
+		return -ENOENT;
+	ret = bpf_get_dentry_xattr(dentry, name__str, value_p);
+	dput(dentry);
+	return ret;
+}

[Severity: Medium]
Should this try d_find_alias() before falling back to d_find_any_alias()?

d_find_any_alias() returns the first alias on inode->i_dentry without skipping unhashed or disconnected dentries. Path-based filesystems rely on the dentry in their xattr callbacks; for example, cifs_xattr_get() builds the server path from it via build_path_from_dentry().

When an inode has an open unlinked hard link ahead of a live hard link on inode->i_dentry, bpf_get_inode_xattr() passes the unhashed alias to __vfs_getxattr() and spuriously fails even though a hashed alias exists.

Trying d_find_alias(inode) first and falling back to d_find_any_alias(inode) on NULL, as inode_doinit_with_dentry() in security/selinux/hooks.c does, avoids this while still handling unlinked open files.

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

Re: [PATCH v1 bpf-next 2/3] bpf: Add a bpf_get_inode_xattr kfunc

Quoted 21 lines · On 26/12/03 02:01PM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] bpf: bpf_get_inode_xattr() ignores hashed aliases via d_find_any_alias()
--

commit 411b1cdf4c29cccf8363d8423d9bb02ec61b59b9
Author: Anton Protopopov <[email protected]>

bpf: Add a bpf_get_inode_xattr kfunc

This commit adds the sleepable bpf_get_inode_xattr() kfunc to read an extended attribute from an inode by looking up one of its dentries with d_find_any_alias().

Quoted 5 lines
fs/bpf_fs_kfuncs.c +0 -0
index 1b973bd4e5355..654340a0bc70e 100644
@@ -169,6 +169,35 @@ __bpf_kfunc int bpf_get_file_xattr(struct file *file, const char *name__str,

[ ... ]

Quoted 7 lines

fs/bpf_fs_kfuncs.c __bpf_kfunc int bpf_get_file_xattr(struct file *file, const char *name__str, · line 193

Quoted 7 lines
fs/bpf_fs_kfuncs.c +7 -0
+__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_any_alias(inode);

Yes, thanks, will change this to

+ dentry = d_find_alias(inode);
+ if (!dentry)
+ dentry = d_find_any_alias(inode);

Quoted 26 lines

fs/bpf_fs_kfuncs.c __bpf_kfunc int bpf_get_file_xattr(struct file *file, const char *name__str, · line 199

Quoted 7 lines
fs/bpf_fs_kfuncs.c +6 -0
+	if (!dentry)
+		return -ENOENT;
 
+	ret = bpf_get_dentry_xattr(dentry, name__str, value_p);
+	dput(dentry);
+	return ret;
+}

[Severity: Medium]
Should this try d_find_alias() before falling back to d_find_any_alias()?

d_find_any_alias() returns the first alias on inode->i_dentry without skipping unhashed or disconnected dentries. Path-based filesystems rely on the dentry in their xattr callbacks; for example, cifs_xattr_get() builds the server path from it via build_path_from_dentry().

When an inode has an open unlinked hard link ahead of a live hard link on inode->i_dentry, bpf_get_inode_xattr() passes the unhashed alias to __vfs_getxattr() and spuriously fails even though a hashed alias exists.

Trying d_find_alias(inode) first and falling back to d_find_any_alias(inode) on NULL, as inode_doinit_with_dentry() in security/selinux/hooks.c does, avoids this while still handling unlinked open files.

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

PATCH 3/3 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
#9 lore raw

Re: [PATCH v1 bpf-next 3/3] 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() fails on missing ACL or fileattr support --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] selftests/bpf: test_inode_xattr() fails on missing ACL or fileattr support
--

commit 4210eebb6e4c6f13c9f673ce6c5fcebebb0b66cb
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 exercise bpf_get_inode_xattr(), bpf_set_dentry_xattr(), and bpf_remove_dentry_xattr() on the new sleepable inode LSM hooks, along with verifier accept and reject tests for inode_copy_up.

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c +0 -0
index 43a26ec69a8e3..ad467c7663808 100644
@@ -194,6 +202,102 @@ static void test_set_remove_xattr(void)
 	remove(testfile);
 }

[ ... ]

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 when POSIX ACLs or FS_IOC_GETFLAGS/FS_IOC_SETFLAGS are not supported on /tmp, instead of failing?

tools/testing/selftests/bpf/config enables CONFIG_TMPFS and CONFIG_TMPFS_XATTR without CONFIG_TMPFS_POSIX_ACL, so on a tmpfs /tmp the initial xattr probe passes and setting system.posix_acl_access fails with EOPNOTSUPP.

With CONFIG_FS_POSIX_ACL=y on a filesystem without POSIX ACL support, vfs_set_acl() calls security_inode_set_acl() before set_posix_acl() returns -EOPNOTSUPP. That leaves security.bpf.bar set on file when the if block is skipped, leaking into the FS_IOC_SETFLAGS has_bar() check.

Similarly, xattr-capable filesystems without fileattr_get/fileattr_set (such as 9p) return ENOTTY or EOPNOTSUPP from FS_IOC_GETFLAGS, failing ASSERT_OK() instead of calling test__skip().

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

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

Quoted 55 lines · On Thu, Dec 3, 2026 at 2:45 PM <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] selftests/bpf: test_inode_xattr() fails on missing ACL or fileattr support
--

commit 4210eebb6e4c6f13c9f673ce6c5fcebebb0b66cb
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 exercise bpf_get_inode_xattr(), bpf_set_dentry_xattr(), and bpf_remove_dentry_xattr() on the new sleepable inode LSM hooks, along with verifier accept and reject tests for inode_copy_up.

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c +0 -0
index 43a26ec69a8e3..ad467c7663808 100644
@@ -194,6 +202,102 @@ static void test_set_remove_xattr(void)
      remove(testfile);
 }

[ ... ]

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 when POSIX ACLs or FS_IOC_GETFLAGS/FS_IOC_SETFLAGS are not supported on /tmp, instead of failing?

tools/testing/selftests/bpf/config enables CONFIG_TMPFS and CONFIG_TMPFS_XATTR without CONFIG_TMPFS_POSIX_ACL, so on a tmpfs /tmp the initial xattr probe passes and setting system.posix_acl_access fails with EOPNOTSUPP.

Quoted 8 lines

With CONFIG_FS_POSIX_ACL=y on a filesystem without POSIX ACL support, vfs_set_acl() calls security_inode_set_acl() before set_posix_acl() returns -EOPNOTSUPP. That leaves security.bpf.bar set on file when the if block is skipped, leaking into the FS_IOC_SETFLAGS has_bar() check.

Similarly, xattr-capable filesystems without fileattr_get/fileattr_set (such as 9p) return ENOTTY or EOPNOTSUPP from FS_IOC_GETFLAGS, failing ASSERT_OK() instead of calling test__skip().

NO, CI enables CONFIG_TMPFS_XATTR and CONFIG_TMPFS_POSIX_ACL for every arch.

Quoted 2 lines

--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3