From: Anton Protopopov <a.s.protopopov@gmail.com>
To: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Eduard Zingerman <eddyz87@gmail.com>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>,
Martin KaFai Lau <martin.lau@linux.dev>,
Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
Jiri Olsa <jolsa@kernel.org>,
Emil Tsalapatis <emil@etsalapatis.com>,
Ihor Solodrai <ihor.solodrai@linux.dev>,
KP Singh <kpsingh@kernel.org>,
Matt Bobrowski <matt@bobrowski.net>,
Alexander Viro <viro@zeniv.linux.org.uk>,
Christian Brauner <brauner@kernel.org>, Jan Kara <jack@suse.cz>,
bpf@vger.kernel.org, linux-fsdevel@vger.kernel.org,
linux-kernel@vger.kernel.org
Cc: Anton Protopopov <a.s.protopopov@gmail.com>
Subject: [PATCH v1 bpf-next 2/3] bpf: Add a bpf_get_inode_xattr kfunc
Date: Thu, 3 Dec 2026 13:44:06 +0000 [thread overview]
Message-ID: <20261203134411.1092433-3-a.s.protopopov@gmail.com> (raw)
In-Reply-To: <20261203134411.1092433-1-a.s.protopopov@gmail.com>
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@gmail.com>
---
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
next prev parent reply other threads:[~2026-12-03 13:32 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-03 13:44 [PATCH v1 bpf-next 0/3] bpf: Use the xattr kfuncs on six more inode hooks Anton Protopopov
2026-12-03 13:44 ` [PATCH v1 bpf-next 1/3] bpf: Let sleepable LSM programs attach to " Anton Protopopov
2026-12-03 13:44 ` Anton Protopopov [this message]
2026-12-03 13:44 ` [PATCH v1 bpf-next 3/3] selftests/bpf: Test xattr kfuncs on the new sleepable " Anton Protopopov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261203134411.1092433-3-a.s.protopopov@gmail.com \
--to=a.s.protopopov@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=brauner@kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=ihor.solodrai@linux.dev \
--cc=jack@suse.cz \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=matt@bobrowski.net \
--cc=memxor@gmail.com \
--cc=song@kernel.org \
--cc=viro@zeniv.linux.org.uk \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®