From: David Windsor <dwindsor@gmail.com>
To: bpf@vger.kernel.org
Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org,
martin.lau@linux.dev, song@kernel.org, kpsingh@kernel.org,
john.fastabend@gmail.com, viro@zeniv.linux.org.uk,
brauner@kernel.org, jack@suse.cz, dwindsor@gmail.com,
linux-fsdevel@vger.kernel.org
Subject: [PATCH bpf-next 2/2] selftests/bpf: Add tests for dentry kfuncs
Date: Wed, 24 Sep 2025 19:24:34 -0400 [thread overview]
Message-ID: <20250924232434.74761-3-dwindsor@gmail.com> (raw)
In-Reply-To: <20250924232434.74761-1-dwindsor@gmail.com>
Add BPF selftests that exercise the new dentry kfuncs via an LSM program
attached to the file_open hook.
Signed-off-by: David Windsor <dwindsor@gmail.com>
---
.../selftests/bpf/prog_tests/dentry_lsm.c | 48 +++++++++++++++++
.../testing/selftests/bpf/progs/dentry_lsm.c | 51 +++++++++++++++++++
2 files changed, 99 insertions(+)
create mode 100644 tools/testing/selftests/bpf/prog_tests/dentry_lsm.c
create mode 100644 tools/testing/selftests/bpf/progs/dentry_lsm.c
diff --git a/tools/testing/selftests/bpf/prog_tests/dentry_lsm.c b/tools/testing/selftests/bpf/prog_tests/dentry_lsm.c
new file mode 100644
index 000000000000..3e8c68017954
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/dentry_lsm.c
@@ -0,0 +1,48 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2025 David Windsor <dwindsor@gmail.com> */
+
+#include <test_progs.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <limits.h>
+#include <string.h>
+#include "dentry_lsm.skel.h"
+
+void test_dentry_lsm(void)
+{
+ struct dentry_lsm *skel;
+ char test_file[PATH_MAX];
+ int fd, ret;
+
+ skel = dentry_lsm__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "dentry_lsm__open_and_load"))
+ return;
+
+ ret = dentry_lsm__attach(skel);
+ if (!ASSERT_OK(ret, "dentry_lsm__attach"))
+ goto cleanup;
+
+ /* Create a temporary file to trigger file_open LSM hook */
+ ret = snprintf(test_file, sizeof(test_file), "/tmp/bpf_test_file_%d", getpid());
+ if (!ASSERT_GT(ret, 0, "snprintf"))
+ goto cleanup_link;
+ if (!ASSERT_LT(ret, sizeof(test_file), "snprintf"))
+ goto cleanup_link;
+
+ fd = open(test_file, O_CREAT | O_RDWR, 0644);
+ if (!ASSERT_GE(fd, 0, "open"))
+ goto cleanup_link;
+ close(fd);
+
+ /* Test passes if BPF program loaded and ran without error */
+
+ /* Clean up test file */
+ unlink(test_file);
+
+cleanup_link:
+ unlink(test_file);
+cleanup:
+ dentry_lsm__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/dentry_lsm.c b/tools/testing/selftests/bpf/progs/dentry_lsm.c
new file mode 100644
index 000000000000..fa6d65d2c50f
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/dentry_lsm.c
@@ -0,0 +1,51 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2025 David Windsor <dwindsor@gmail.com> */
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <bpf/bpf_core_read.h>
+
+extern struct dentry *bpf_dget(struct dentry *dentry) __ksym;
+extern void bpf_dput(struct dentry *dentry) __ksym;
+extern struct dentry *bpf_dget_parent(struct dentry *dentry) __ksym;
+extern struct dentry *bpf_d_find_alias(struct inode *inode) __ksym;
+extern struct dentry *bpf_file_dentry(struct file *file) __ksym;
+extern struct vfsmount *bpf_file_vfsmount(struct file *file) __ksym;
+
+SEC("lsm.s/file_open")
+int BPF_PROG(file_open, struct file *file)
+{
+ struct dentry *dentry, *parent, *alias, *dentry_ref;
+ struct vfsmount *vfs_mnt;
+
+ if (!file)
+ return 0;
+
+ dentry = bpf_file_dentry(file);
+ if (dentry) {
+ dentry_ref = bpf_dget(dentry);
+ if (dentry_ref)
+ bpf_dput(dentry_ref);
+
+ parent = bpf_dget_parent(dentry);
+ if (parent)
+ bpf_dput(parent);
+ }
+
+ if (file->f_inode) {
+ alias = bpf_d_find_alias(file->f_inode);
+ if (alias)
+ bpf_dput(alias);
+ }
+
+ vfs_mnt = bpf_file_vfsmount(file);
+ if (vfs_mnt) {
+ /* Test that we can access vfsmount */
+ (void)vfs_mnt;
+ }
+
+ return 0;
+}
+
+char _license[] SEC("license") = "GPL";
--
2.43.0
prev parent reply other threads:[~2025-09-24 23:24 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-24 23:24 [PATCH bpf-next 0/2] bpf: Add more dentry kfuncs for BPF LSM programs David Windsor
2025-09-24 23:24 ` [PATCH bpf-next 1/2] bpf: Add " David Windsor
2025-09-24 23:55 ` Al Viro
2025-09-25 0:08 ` David Windsor
2025-09-25 0:29 ` Al Viro
2025-09-25 0:44 ` David Windsor
2025-09-25 0:47 ` Al Viro
2025-09-25 0:56 ` David Windsor
2025-09-29 8:24 ` Christian Brauner
2025-09-24 23:24 ` David Windsor [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=20250924232434.74761-3-dwindsor@gmail.com \
--to=dwindsor@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=brauner@kernel.org \
--cc=daniel@iogearbox.net \
--cc=jack@suse.cz \
--cc=john.fastabend@gmail.com \
--cc=kpsingh@kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=song@kernel.org \
--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).