[RFC PATCH bpf-next 07/12] selftests/bpf: exercise the path ancestor iterator
Justin Suess
utilityemal77 at gmail.com
Tue Oct 6 00:20:14 UTC 2026
Walk the ancestry of a mkdir's parent directory from a sleepable LSM
program and check that the iteration reaches the root: the temporary
directory, its parent and / are visited, and no position comes back
flagged.
Resolve the second position's pathname from the acquired position after
the step that yielded it has been taken, so that the acquired reference
is what the sleepable kfunc runs on rather than the walk's.
Signed-off-by: Justin Suess <utilityemal77 at gmail.com>
---
.../selftests/bpf/prog_tests/path_ancestors.c | 48 ++++++++++++++++++
.../selftests/bpf/progs/path_ancestors.c | 49 +++++++++++++++++++
2 files changed, 97 insertions(+)
create mode 100644 tools/testing/selftests/bpf/prog_tests/path_ancestors.c
create mode 100644 tools/testing/selftests/bpf/progs/path_ancestors.c
diff --git a/tools/testing/selftests/bpf/prog_tests/path_ancestors.c b/tools/testing/selftests/bpf/prog_tests/path_ancestors.c
new file mode 100644
index 000000000000..2de79673a13b
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/path_ancestors.c
@@ -0,0 +1,48 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Justin Suess */
+
+#include <sys/stat.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <test_progs.h>
+#include "path_ancestors.skel.h"
+
+void test_path_ancestors(void)
+{
+ char base[] = "/tmp/path_ancestors_XXXXXX";
+ struct path_ancestors *skel = NULL;
+ char suba[280], subb[280];
+
+ if (!ASSERT_OK_PTR(mkdtemp(base), "mkdtemp"))
+ return;
+ snprintf(suba, sizeof(suba), "%s/a", base);
+ snprintf(subb, sizeof(subb), "%s/a/b", base);
+ if (!ASSERT_OK(mkdir(suba, 0755), "mkdir_a"))
+ goto out_rm;
+
+ skel = path_ancestors__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "open_and_load"))
+ goto out_rm;
+ skel->bss->monitored_pid = getpid();
+ if (!ASSERT_OK(path_ancestors__attach(skel), "attach"))
+ goto out;
+
+ /* mkdir b: the hook sees dir == suba, whose ancestry is walked. */
+ if (!ASSERT_OK(mkdir(subb, 0755), "mkdir_b"))
+ goto out;
+
+ /* suba, base, /tmp, / at least. */
+ ASSERT_GE(skel->bss->ref_count, 3, "ref_count");
+ ASSERT_EQ(skel->bss->ref_flags, 0, "ref_flags");
+
+ /* The acquired second position, used after its step was taken. */
+ ASSERT_STREQ(skel->bss->second_path, base, "second_path");
+ ASSERT_EQ(skel->bss->second_len, strlen(base) + 1, "second_len");
+
+out:
+ path_ancestors__destroy(skel);
+out_rm:
+ rmdir(subb);
+ rmdir(suba);
+ rmdir(base);
+}
diff --git a/tools/testing/selftests/bpf/progs/path_ancestors.c b/tools/testing/selftests/bpf/progs/path_ancestors.c
new file mode 100644
index 000000000000..af6b777e8bec
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/path_ancestors.c
@@ -0,0 +1,49 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Justin Suess */
+
+#include "vmlinux.h"
+#include <errno.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include "bpf_kfuncs.h"
+
+char _license[] SEC("license") = "GPL";
+
+__u32 monitored_pid;
+
+int ref_count; /* positions seen by the pure referenced walk */
+int ref_flags; /* pos flags seen by the referenced walk */
+int second_len; /* d_path length of the walk's second position */
+char second_path[256];
+
+static bool monitored(void)
+{
+ return (bpf_get_current_pid_tgid() >> 32) == monitored_pid;
+}
+
+SEC("lsm.s/path_mkdir")
+int BPF_PROG(walk_modes, const struct path *dir, struct dentry *dentry,
+ umode_t mode)
+{
+ struct bpf_iter_path_ancestors it;
+ struct path *pos;
+
+ if (!monitored())
+ return 0;
+
+ /*
+ * Referenced walk: every position comes acquired, so it stays valid
+ * for sleepable work and past the step that yielded it.
+ */
+ bpf_iter_path_ancestors_new(&it, (struct path *)dir, 0);
+ while ((pos = bpf_iter_path_ancestors_next(&it))) {
+ ref_count++;
+ ref_flags |= bpf_path_ancestors_pos_flags(&it);
+ if (ref_count == 2)
+ second_len = bpf_path_d_path(pos, second_path,
+ sizeof(second_path));
+ bpf_path_put(pos);
+ }
+ bpf_iter_path_ancestors_destroy(&it);
+ return 0;
+}
--
2.55.0
More information about the Linux-security-module-archive
mailing list