[PATCH v1 bpf-next 2/3] bpf: Add a bpf_get_inode_xattr kfunc
From: Anton Protopopov
Date: Thu Dec 03 2026 - 08:32:51 EST
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.
Signed-off-by: Anton Protopopov <a.s.protopopov@xxxxxxxxx>
---
fs/bpf_fs_kfuncs.c | 30 ++++++++++++++++++++++++++++++
1 file changed, 30 insertions(+)
diff --git a/fs/bpf_fs_kfuncs.c b/fs/bpf_fs_kfuncs.c
index 1b973bd4e535..654340a0bc70 100644
--- a/fs/bpf_fs_kfuncs.c
+++ b/fs/bpf_fs_kfuncs.c
@@ -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)
--
2.43.0