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 3/3] selftests/bpf: Test xattr kfuncs on the new sleepable inode hooks
Date: Thu, 3 Dec 2026 13:44:07 +0000 [thread overview]
Message-ID: <20261203134411.1092433-4-a.s.protopopov@gmail.com> (raw)
In-Reply-To: <20261203134411.1092433-1-a.s.protopopov@gmail.com>
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.
Signed-off-by: Anton Protopopov <a.s.protopopov@gmail.com>
---
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
diff --git a/tools/testing/selftests/bpf/bpf_kfuncs.h b/tools/testing/selftests/bpf/bpf_kfuncs.h
index 1b408fe9e0fb..71601eafae6f 100644
--- a/tools/testing/selftests/bpf/bpf_kfuncs.h
+++ b/tools/testing/selftests/bpf/bpf_kfuncs.h
@@ -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
diff --git a/tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c b/tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c
index 43a26ec69a8e..ad467c766380 100644
--- a/tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c
+++ b/tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c
@@ -1,17 +1,25 @@
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
+#include <endian.h>
#include <stdlib.h>
+#include <sys/ioctl.h>
+#include <sys/stat.h>
#include <sys/types.h>
#include <sys/xattr.h>
+#include <linux/fs.h>
#include <linux/fsverity.h>
+#include <linux/posix_acl.h>
+#include <linux/posix_acl_xattr.h>
#include <unistd.h>
#include <test_progs.h>
#include "test_get_xattr.skel.h"
#include "test_set_remove_xattr.skel.h"
+#include "test_inode_xattr.skel.h"
#include "test_fsverity.skel.h"
static const char testfile[] = "/tmp/test_progs_fs_kfuncs";
+static const char testdir[] = "/tmp/test_progs_fs_kfuncs_dir";
static void test_get_xattr(const char *name, const char *value, bool allow_access)
{
@@ -194,6 +202,102 @@ static void test_set_remove_xattr(void)
remove(testfile);
}
+static bool has_bar(struct test_inode_xattr *skel, const char *path)
+{
+ char value_out[32];
+
+ return getxattr(path, skel->rodata->xattr_bar, value_out, sizeof(value_out)) ==
+ sizeof(skel->data->value_bar) &&
+ !memcmp(value_out, skel->data->value_bar, sizeof(skel->data->value_bar));
+}
+
+static void test_inode_xattr(void)
+{
+ /* An ACL with a named user, so that it is not folded into the mode */
+ const struct {
+ struct posix_acl_xattr_header header;
+ struct posix_acl_xattr_entry entries[5];
+ } acl = {
+ .header = { htole32(POSIX_ACL_XATTR_VERSION) },
+ .entries = {
+ { htole16(ACL_USER_OBJ), htole16(ACL_READ | ACL_WRITE),
+ htole32(ACL_UNDEFINED_ID) },
+ { htole16(ACL_USER), htole16(ACL_READ), htole32(1) },
+ { htole16(ACL_GROUP_OBJ), htole16(ACL_READ), htole32(ACL_UNDEFINED_ID) },
+ { htole16(ACL_MASK), htole16(ACL_READ), htole32(ACL_UNDEFINED_ID) },
+ { htole16(ACL_OTHER), htole16(ACL_READ), htole32(ACL_UNDEFINED_ID) },
+ },
+ };
+ char child[64], file[64], link_path[64];
+ struct test_inode_xattr *skel = NULL;
+ int fd, flags, err;
+
+ snprintf(child, sizeof(child), "%s/child", testdir);
+ snprintf(file, sizeof(file), "%s/file", testdir);
+ snprintf(link_path, sizeof(link_path), "%s/link", testdir);
+
+ if (!ASSERT_OK(mkdir(testdir, 0755), "mkdir testdir"))
+ return;
+ fd = open(file, O_CREAT | O_RDONLY, 0644);
+ if (!ASSERT_GE(fd, 0, "create_file"))
+ goto out;
+
+ skel = test_inode_xattr__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "test_inode_xattr__open_and_load"))
+ goto out;
+
+ err = setxattr(testdir, skel->rodata->xattr_foo, value_foo, sizeof(value_foo), 0);
+ if (err && errno == EOPNOTSUPP) {
+ printf("%s:SKIP:local fs doesn't support xattr (%d)\n"
+ "To run this test, make sure /tmp filesystem supports xattr.\n",
+ __func__, errno);
+ test__skip();
+ goto out;
+ }
+ if (!ASSERT_OK(err, "setxattr testdir"))
+ goto out;
+
+ skel->bss->monitored_pid = getpid();
+ if (!ASSERT_OK(test_inode_xattr__attach(skel), "test_inode_xattr__attach"))
+ goto out;
+
+ /* inode_mkdir and inode_link read the label of the directory */
+ ASSERT_OK(mkdir(child, 0755), "mkdir child");
+ ASSERT_EQ(skel->data->mkdir_ret, sizeof(value_foo), "mkdir_ret");
+ ASSERT_STREQ(skel->bss->mkdir_value, value_foo, "mkdir_value");
+
+ ASSERT_OK(link(file, link_path), "link");
+ ASSERT_EQ(skel->data->link_ret, sizeof(value_foo), "link_ret");
+ ASSERT_STREQ(skel->bss->link_value, value_foo, "link_value");
+
+ /* 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");
+ 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");
+ }
+
+out:
+ if (fd >= 0)
+ close(fd);
+ test_inode_xattr__destroy(skel);
+ remove(link_path);
+ remove(file);
+ rmdir(child);
+ rmdir(testdir);
+}
+
#ifndef SHA256_DIGEST_SIZE
#define SHA256_DIGEST_SIZE 32
#endif
@@ -286,6 +390,9 @@ void test_fs_kfuncs(void)
if (test__start_subtest("set_remove_xattr"))
test_set_remove_xattr();
+ if (test__start_subtest("inode_xattr"))
+ test_inode_xattr();
+
if (test__start_subtest("fsverity"))
test_fsverity();
}
diff --git a/tools/testing/selftests/bpf/progs/test_inode_xattr.c b/tools/testing/selftests/bpf/progs/test_inode_xattr.c
new file mode 100644
index 000000000000..f82b9c0ad067
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_inode_xattr.c
@@ -0,0 +1,89 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include "vmlinux.h"
+#include <errno.h>
+#include <bpf/bpf_tracing.h>
+#include "bpf_kfuncs.h"
+#include "bpf_misc.h"
+
+char _license[] SEC("license") = "GPL";
+
+__u32 monitored_pid;
+
+const char xattr_foo[] = "security.bpf.foo";
+const char xattr_bar[] = "security.bpf.bar";
+char value_bar[] = "world";
+
+char mkdir_value[32];
+char link_value[32];
+int mkdir_ret = -1;
+int link_ret = -1;
+int set_acl_ret = -1;
+int remove_acl_ret = -1;
+int file_setattr_ret = -1;
+
+static bool monitored(void)
+{
+ return bpf_get_current_pid_tgid() >> 32 == monitored_pid;
+}
+
+static int read_dir_foo(struct inode *dir, char *value, __u32 size)
+{
+ struct bpf_dynptr value_ptr;
+
+ bpf_dynptr_from_mem(value, size, 0, &value_ptr);
+ return bpf_get_inode_xattr(dir, xattr_foo, &value_ptr);
+}
+
+static int set_bar(struct dentry *dentry)
+{
+ struct bpf_dynptr value_ptr;
+
+ bpf_dynptr_from_mem(value_bar, sizeof(value_bar), 0, &value_ptr);
+ return bpf_set_dentry_xattr(dentry, xattr_bar, &value_ptr, 0);
+}
+
+SEC("lsm.s/inode_mkdir")
+int BPF_PROG(test_inode_mkdir, struct mnt_idmap *idmap, struct inode *dir,
+ struct dentry *dentry, umode_t mode)
+{
+ if (monitored())
+ mkdir_ret = read_dir_foo(dir, mkdir_value, sizeof(mkdir_value));
+ return 0;
+}
+
+SEC("lsm.s/inode_link")
+int BPF_PROG(test_inode_link, struct mnt_idmap *idmap, struct dentry *old_dentry,
+ struct inode *dir, struct dentry *new_dentry)
+{
+ if (monitored())
+ link_ret = read_dir_foo(dir, link_value, sizeof(link_value));
+ return 0;
+}
+
+/* The verifier calls the _locked setters on these three hooks. */
+SEC("lsm.s/inode_set_acl")
+int BPF_PROG(test_inode_set_acl, struct mnt_idmap *idmap, struct dentry *dentry,
+ const char *acl_name, struct posix_acl *kacl)
+{
+ if (monitored())
+ set_acl_ret = set_bar(dentry);
+ return 0;
+}
+
+SEC("lsm.s/inode_remove_acl")
+int BPF_PROG(test_inode_remove_acl, struct mnt_idmap *idmap, struct dentry *dentry,
+ const char *acl_name)
+{
+ if (monitored())
+ remove_acl_ret = bpf_remove_dentry_xattr(dentry, xattr_bar);
+ return 0;
+}
+
+SEC("lsm.s/inode_file_setattr")
+int BPF_PROG(test_inode_file_setattr, struct dentry *dentry, struct file_kattr *fa)
+{
+ if (monitored())
+ file_setattr_ret = set_bar(dentry);
+ return 0;
+}
diff --git a/tools/testing/selftests/bpf/progs/verifier_vfs_accept.c b/tools/testing/selftests/bpf/progs/verifier_vfs_accept.c
index 25f37a3dd7cf..38edcf1b34e8 100644
--- a/tools/testing/selftests/bpf/progs/verifier_vfs_accept.c
+++ b/tools/testing/selftests/bpf/progs/verifier_vfs_accept.c
@@ -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";
diff --git a/tools/testing/selftests/bpf/progs/verifier_vfs_reject.c b/tools/testing/selftests/bpf/progs/verifier_vfs_reject.c
index ed2bd2faebf3..96ad9d895af9 100644
--- a/tools/testing/selftests/bpf/progs/verifier_vfs_reject.c
+++ b/tools/testing/selftests/bpf/progs/verifier_vfs_reject.c
@@ -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";
--
2.43.0
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 " 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 ` [PATCH v1 bpf-next 2/3] bpf: Add a bpf_get_inode_xattr kfunc Anton Protopopov
2026-12-03 13:44 ` Anton Protopopov [this message]
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-4-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®