From: Song Liu <song@kernel.org>
To: bpf@vger.kernel.org, linux-fsdevel@vger.kernel.org,
linux-kernel@vger.kernel.org,
linux-security-module@vger.kernel.org
Cc: kernel-team@meta.com, andrii@kernel.org, eddyz87@gmail.com,
ast@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev,
viro@zeniv.linux.org.uk, brauner@kernel.org, jack@suse.cz,
kpsingh@kernel.org, mattbobrowski@google.com, amir73il@gmail.com,
repnop@google.com, jlayton@kernel.org, josef@toxicpanda.com,
mic@digikod.net, gnoack@google.com, m@maowtm.org,
Song Liu <song@kernel.org>
Subject: [PATCH v3 bpf-next 5/5] selftests/bpf: Path walk test
Date: Fri, 6 Jun 2025 14:30:15 -0700 [thread overview]
Message-ID: <20250606213015.255134-6-song@kernel.org> (raw)
In-Reply-To: <20250606213015.255134-1-song@kernel.org>
Add an end-to-end test with path_iter on security hook file_open.
A test file is created in folder /tmp/test_progs_path_iter/folder. On
file_open, walk file->f_path up to its parent and grand parent, and test
bpf_get_dentry_xattr and bpf_path_d_path on the folders.
Signed-off-by: Song Liu <song@kernel.org>
---
.../selftests/bpf/prog_tests/path_iter.c | 99 +++++++++++++++++++
tools/testing/selftests/bpf/progs/path_walk.c | 59 +++++++++++
2 files changed, 158 insertions(+)
create mode 100644 tools/testing/selftests/bpf/progs/path_walk.c
diff --git a/tools/testing/selftests/bpf/prog_tests/path_iter.c b/tools/testing/selftests/bpf/prog_tests/path_iter.c
index 3c99c24fbd96..b9772026fbf7 100644
--- a/tools/testing/selftests/bpf/prog_tests/path_iter.c
+++ b/tools/testing/selftests/bpf/prog_tests/path_iter.c
@@ -2,11 +2,110 @@
/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
#include <test_progs.h>
+#include <fcntl.h>
#include <bpf/libbpf.h>
#include <bpf/btf.h>
+#include <sys/stat.h>
+#include <sys/xattr.h>
+
#include "path_iter.skel.h"
+#include "path_walk.skel.h"
+
+static const char grand_parent_path[] = "/tmp/test_progs_path_iter";
+static const char parent_path[] = "/tmp/test_progs_path_iter/folder";
+static const char file_path[] = "/tmp/test_progs_path_iter/folder/file";
+static const char xattr_name[] = "user.bpf.selftests";
+static const char xattr_value[] = "selftest_path_iter";
+
+static void cleanup_files(void)
+{
+ remove(file_path);
+ rmdir(parent_path);
+ rmdir(grand_parent_path);
+}
+
+static int setup_files_and_xattrs(void)
+{
+ int ret = -1;
+
+ /* create test folders */
+ if (mkdir(grand_parent_path, 0755))
+ goto error;
+ if (mkdir(parent_path, 0755))
+ goto error;
+
+ /* setxattr for test folders */
+ ret = setxattr(grand_parent_path, xattr_name,
+ xattr_value, sizeof(xattr_value), 0);
+ if (ret < 0) {
+ /* return errno, so that we can handle EOPNOTSUPP in the caller */
+ ret = errno;
+ goto error;
+ }
+ ret = setxattr(parent_path, xattr_name,
+ xattr_value, sizeof(xattr_value), 0);
+ if (ret < 0) {
+ /* return errno, so that we can handle EOPNOTSUPP in the caller */
+ ret = errno;
+ goto error;
+ }
+
+ return 0;
+error:
+ cleanup_files();
+ return ret;
+}
+
+static void test_path_walk(void)
+{
+ struct path_walk *skel = NULL;
+ int file_fd;
+ int err;
+
+ err = setup_files_and_xattrs();
+ if (err == 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();
+ return;
+ }
+
+ if (!ASSERT_OK(err, "setup_file"))
+ return;
+
+ skel = path_walk__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "path_walk__open_and_load"))
+ goto cleanup;
+
+ skel->bss->monitored_pid = getpid();
+ if (!ASSERT_OK(path_walk__attach(skel), "path_walk__attach"))
+ goto cleanup;
+
+ file_fd = open(file_path, O_CREAT);
+ if (!ASSERT_OK_FD(file_fd, "open_file"))
+ goto cleanup;
+ close(file_fd);
+
+ ASSERT_OK(strncmp(skel->bss->parent_xattr_buf, xattr_value, strlen(xattr_value)),
+ "parent_xattr");
+ ASSERT_OK(strncmp(skel->bss->grand_parent_xattr_buf, xattr_value, strlen(xattr_value)),
+ "grand_parent_xattr");
+
+ ASSERT_OK(strncmp(skel->bss->parent_path_buf, parent_path, strlen(parent_path)),
+ "parent_d_path");
+ ASSERT_OK(strncmp(skel->bss->grand_parent_path_buf, grand_parent_path,
+ strlen(grand_parent_path)),
+ "grand_parent_d_path");
+
+cleanup:
+ path_walk__destroy(skel);
+ cleanup_files();
+}
void test_path_iter(void)
{
RUN_TESTS(path_iter);
+ if (test__start_subtest("path_walk_example"))
+ test_path_walk();
}
diff --git a/tools/testing/selftests/bpf/progs/path_walk.c b/tools/testing/selftests/bpf/progs/path_walk.c
new file mode 100644
index 000000000000..1e1ae82b47a2
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/path_walk.c
@@ -0,0 +1,59 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
+
+#include "vmlinux.h"
+#include <errno.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <bpf/bpf_core_read.h>
+#include "bpf_kfuncs.h"
+#include "bpf_misc.h"
+
+char _license[] SEC("license") = "GPL";
+
+__u32 monitored_pid;
+
+#define BUF_SIZE 1024
+char parent_path_buf[BUF_SIZE] = {};
+char parent_xattr_buf[BUF_SIZE] = {};
+char grand_parent_path_buf[BUF_SIZE] = {};
+char grand_parent_xattr_buf[BUF_SIZE] = {};
+
+static __always_inline void d_path_and_read_xattr(struct path *p, char *path, char *xattr)
+{
+ struct bpf_dynptr ptr;
+ struct dentry *dentry;
+
+ if (!p)
+ return;
+ bpf_path_d_path(p, path, BUF_SIZE);
+ bpf_dynptr_from_mem(xattr, BUF_SIZE, 0, &ptr);
+ dentry = p->dentry;
+ if (dentry)
+ bpf_get_dentry_xattr(dentry, "user.bpf.selftests", &ptr);
+}
+
+SEC("lsm.s/file_open")
+int BPF_PROG(test_file_open, struct file *f)
+{
+ __u32 pid = bpf_get_current_pid_tgid() >> 32;
+ struct bpf_iter_path path_it;
+ struct path *p;
+
+ if (pid != monitored_pid)
+ return 0;
+
+ bpf_iter_path_new(&path_it, &f->f_path, 0);
+
+ /* Get d_path and xattr for the parent directory */
+ p = bpf_iter_path_next(&path_it);
+ d_path_and_read_xattr(p, parent_path_buf, parent_xattr_buf);
+
+ /* Get d_path and xattr for the grand parent directory */
+ p = bpf_iter_path_next(&path_it);
+ d_path_and_read_xattr(p, grand_parent_path_buf, grand_parent_xattr_buf);
+
+ bpf_iter_path_destroy(&path_it);
+
+ return 0;
+}
--
2.47.1
next prev parent reply other threads:[~2025-06-06 21:30 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-06 21:30 [PATCH v3 bpf-next 0/5] bpf path iterator Song Liu
2025-06-06 21:30 ` [PATCH v3 bpf-next 1/5] namei: Introduce new helper function path_walk_parent() Song Liu
2025-06-10 17:18 ` Mickaël Salaün
2025-06-10 17:26 ` Song Liu
2025-06-10 22:26 ` Tingmao Wang
2025-06-10 22:34 ` Tingmao Wang
2025-06-10 23:08 ` Song Liu
2025-06-11 0:23 ` Tingmao Wang
2025-06-10 23:34 ` NeilBrown
2025-06-11 0:56 ` Song Liu
2025-06-11 15:42 ` Mickaël Salaün
2025-06-11 16:31 ` Song Liu
2025-06-11 17:50 ` Tingmao Wang
2025-06-11 18:08 ` Song Liu
2025-06-12 9:01 ` Jan Kara
2025-06-12 9:49 ` Jan Kara
2025-06-12 12:31 ` Christian Brauner
2025-06-16 0:24 ` Ref-less parent walk from Landlock (was: Re: [PATCH v3 bpf-next 1/5] namei: Introduce new helper function path_walk_parent()) Tingmao Wang
2025-06-17 6:20 ` Song Liu
2025-06-06 21:30 ` [PATCH v3 bpf-next 2/5] landlock: Use path_walk_parent() Song Liu
2025-06-08 18:45 ` Tingmao Wang
2025-06-06 21:30 ` [PATCH v3 bpf-next 3/5] bpf: Introduce path iterator Song Liu
2025-06-06 21:30 ` [PATCH v3 bpf-next 4/5] selftests/bpf: Add tests for bpf " Song Liu
2025-06-06 21:30 ` Song Liu [this message]
2025-06-08 17:32 ` [PATCH v3 bpf-next 0/5] " Tingmao Wang
2025-06-09 6:23 ` Song Liu
2025-06-09 8:08 ` Tingmao Wang
2025-06-11 11:36 ` Christian Brauner
2025-06-11 15:39 ` Mickaël Salaün
2025-06-08 17:32 ` Tingmao Wang
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=20250606213015.255134-6-song@kernel.org \
--to=song@kernel.org \
--cc=amir73il@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=gnoack@google.com \
--cc=jack@suse.cz \
--cc=jlayton@kernel.org \
--cc=josef@toxicpanda.com \
--cc=kernel-team@meta.com \
--cc=kpsingh@kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-security-module@vger.kernel.org \
--cc=m@maowtm.org \
--cc=martin.lau@linux.dev \
--cc=mattbobrowski@google.com \
--cc=mic@digikod.net \
--cc=repnop@google.com \
--cc=viro@zeniv.linux.org.uk \
/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;
as well as URLs for NNTP newsgroup(s).