From: Anastasios Papagiannis <tasos.papagiannnis@gmail.com>
To: linux-security-module@vger.kernel.org
Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-fsdevel@vger.kernel.org, paul@paul-moore.com,
jmorris@namei.org, serge@hallyn.com, brauner@kernel.org,
viro@zeniv.linux.org.uk, jack@suse.cz, ast@kernel.org,
daniel@iogearbox.net, andrii@kernel.org, kpsingh@kernel.org,
matt@bobrowski.net, eddyz87@gmail.com,
tasos.papagiannnis@gmail.com
Subject: [RFC PATCH 2/2] selftests/bpf: Test pathname open enforcement
Date: Fri, 20 Nov 2026 11:48:20 +0200 [thread overview]
Message-ID: <20261120094820.11929-3-tasos.papagiannnis@gmail.com> (raw)
In-Reply-To: <20261120094820.11929-1-tasos.papagiannnis@gmail.com>
Verify that the hook sees the raw symlink pathname and the dirfd base.
Check denials before creation and truncation and on O_PATH and O_TMPFILE
opens. Check that -ECHILD makes the VFS retry the open with references.
Signed-off-by: Anastasios Papagiannis <tasos.papagiannnis@gmail.com>
---
.../selftests/bpf/prog_tests/file_open_name.c | 103 ++++++++++++++++++
.../selftests/bpf/progs/file_open_name.c | 56 ++++++++++
2 files changed, 159 insertions(+)
create mode 100644 tools/testing/selftests/bpf/prog_tests/file_open_name.c
create mode 100644 tools/testing/selftests/bpf/progs/file_open_name.c
diff --git a/tools/testing/selftests/bpf/prog_tests/file_open_name.c b/tools/testing/selftests/bpf/prog_tests/file_open_name.c
new file mode 100644
index 000000000000..553e8c9e75f7
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/file_open_name.c
@@ -0,0 +1,103 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include <test_progs.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include "file_open_name.skel.h"
+
+static void check_open(struct file_open_name *skel, int dfd, const char *name,
+ int flags, mode_t mode, int error)
+{
+ int fd, saved_errno;
+
+ skel->bss->calls = 0;
+ skel->bss->rcu_calls = 0;
+ skel->bss->deny = error != 0;
+ skel->bss->check_enabled = true;
+ fd = openat(dfd, name, flags, mode);
+ saved_errno = errno;
+ skel->bss->check_enabled = false;
+
+ if (error) {
+ ASSERT_EQ(fd, -1, "deny open");
+ ASSERT_EQ(saved_errno, error, "open errno");
+ } else {
+ ASSERT_OK_FD(fd, "allow open");
+ }
+ if (fd >= 0)
+ close(fd);
+ ASSERT_GT(skel->bss->calls, 0, "hook count");
+ ASSERT_STREQ(skel->bss->pathname, name, "raw pathname");
+ ASSERT_EQ(skel->bss->observed_flags & flags, flags, "open flags");
+ ASSERT_EQ(skel->bss->observed_mode & 0777, mode, "creation mode");
+}
+
+void test_file_open_name(void)
+{
+ char dir[] = "/tmp/file_open_name.XXXXXX";
+ struct file_open_name *skel = NULL;
+ int dfd = -1, fd = -1;
+ struct stat st;
+
+ if (!ASSERT_OK_PTR(mkdtemp(dir), "mkdtemp"))
+ return;
+ dfd = open(dir, O_RDONLY | O_DIRECTORY);
+ if (!ASSERT_OK_FD(dfd, "open directory"))
+ goto out;
+ fd = openat(dfd, "target", O_CREAT | O_RDWR, 0600);
+ if (!ASSERT_OK_FD(fd, "create target"))
+ goto out;
+ if (!ASSERT_EQ(write(fd, "data", 4), 4, "write target"))
+ goto out;
+ if (!ASSERT_OK(symlinkat("target", dfd, "alias"), "symlink"))
+ goto out;
+
+ skel = file_open_name__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "open and load"))
+ goto out;
+ if (!ASSERT_OK(stat("/proc/self/ns/pid", &st), "stat PID namespace"))
+ goto out;
+ skel->bss->pidns_dev = st.st_dev;
+ skel->bss->pidns_ino = st.st_ino;
+ skel->bss->monitored_tgid = getpid();
+ if (!ASSERT_OK(file_open_name__attach(skel), "attach"))
+ goto out;
+
+ if (test__start_subtest("raw_symlink")) {
+ check_open(skel, dfd, "./alias", O_RDONLY, 0, 0);
+ if (ASSERT_OK(fstat(dfd, &st), "stat base"))
+ ASSERT_EQ(skel->bss->base_ino, st.st_ino, "dirfd base inode");
+ }
+
+ if (test__start_subtest("deny_create")) {
+ check_open(skel, dfd, "new", O_CREAT | O_RDWR, 0600, EPERM);
+ ASSERT_EQ(fstatat(dfd, "new", &st, 0), -1, "file not created");
+ ASSERT_EQ(errno, ENOENT, "missing file");
+ }
+ if (test__start_subtest("deny_truncate")) {
+ check_open(skel, dfd, "target", O_WRONLY | O_TRUNC, 0, EPERM);
+ if (ASSERT_OK(fstat(fd, &st), "stat target"))
+ ASSERT_EQ(st.st_size, 4, "file not truncated");
+ }
+ if (test__start_subtest("deny_o_path"))
+ check_open(skel, dfd, "alias", O_PATH, 0, EPERM);
+ if (test__start_subtest("deny_tmpfile"))
+ check_open(skel, dfd, ".", O_TMPFILE | O_RDWR, 0600, EPERM);
+ if (test__start_subtest("rcu_retry")) {
+ skel->bss->require_ref = true;
+ check_open(skel, dfd, "target", O_RDONLY, 0, EPERM);
+ skel->bss->require_ref = false;
+ ASSERT_EQ(skel->bss->calls, 2, "hook count");
+ ASSERT_EQ(skel->bss->rcu_calls, 1, "RCU hook count");
+ }
+out:
+ file_open_name__destroy(skel);
+ if (fd >= 0)
+ close(fd);
+ if (dfd >= 0) {
+ unlinkat(dfd, "alias", 0);
+ unlinkat(dfd, "new", 0);
+ unlinkat(dfd, "target", 0);
+ close(dfd);
+ }
+ rmdir(dir);
+}
diff --git a/tools/testing/selftests/bpf/progs/file_open_name.c b/tools/testing/selftests/bpf/progs/file_open_name.c
new file mode 100644
index 000000000000..ea09af8fc238
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/file_open_name.c
@@ -0,0 +1,56 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include "vmlinux.h"
+#include <errno.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+#define LOOKUP_RCU (1U << 8)
+
+__u64 pidns_dev;
+__u64 pidns_ino;
+__u32 monitored_tgid;
+bool check_enabled;
+bool deny;
+bool require_ref;
+int calls;
+int rcu_calls;
+__u64 base_ino;
+int observed_flags;
+__u16 observed_mode;
+char pathname[256];
+
+SEC("lsm/file_open_name")
+int BPF_PROG(check_name, const struct filename *name, const struct path *base,
+ int flags, umode_t mode, unsigned int lookup_flags, int ret)
+{
+ struct bpf_pidns_info nsdata;
+
+ if (ret || !check_enabled)
+ return ret;
+ if (bpf_get_ns_current_pid_tgid(pidns_dev, pidns_ino, &nsdata, sizeof(nsdata)))
+ return ret;
+ if (nsdata.tgid != monitored_tgid)
+ return ret;
+
+ calls++;
+ if (lookup_flags & LOOKUP_RCU) {
+ rcu_calls++;
+ if (require_ref)
+ return -ECHILD;
+ }
+
+ bpf_probe_read_kernel_str(pathname, sizeof(pathname), name->name);
+ base_ino = base->dentry->d_inode->i_ino;
+ observed_flags = flags;
+ observed_mode = mode;
+ if (deny) {
+ int error = -EPERM;
+
+ /* Prevent GCC from replacing the branch with a negation. */
+ barrier_var(error);
+ return error;
+ }
+ return 0;
+}
+
+char LICENSE[] SEC("license") = "GPL";
--
2.56.0
prev parent reply other threads:[~2026-11-20 9:48 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-20 9:48 [RFC PATCH 0/2] security: Add pre-lookup pathname open check Anastasios Papagiannis
2026-11-20 9:48 ` [RFC PATCH 1/2] security: Add hook for unresolved pathname opens Anastasios Papagiannis
2026-11-20 10:44 ` bot+bpf-ci
2026-11-20 9:48 ` Anastasios Papagiannis [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=20261120094820.11929-3-tasos.papagiannnis@gmail.com \
--to=tasos.papagiannnis@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=jack@suse.cz \
--cc=jmorris@namei.org \
--cc=kpsingh@kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-security-module@vger.kernel.org \
--cc=matt@bobrowski.net \
--cc=paul@paul-moore.com \
--cc=serge@hallyn.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
all inboxes | Powered by JetHome®