mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [RFC PATCH 0/2] security: Add pre-lookup pathname open check
@ 2026-11-20  9:48 Anastasios Papagiannis
  2026-11-20  9:48 ` [RFC PATCH 1/2] security: Add hook for unresolved pathname opens Anastasios Papagiannis
  2026-11-20  9:48 ` [RFC PATCH 2/2] selftests/bpf: Test pathname open enforcement Anastasios Papagiannis
  0 siblings, 2 replies; 3+ messages in thread
From: Anastasios Papagiannis @ 2026-11-20  9:48 UTC (permalink / raw)
  To: linux-security-module
  Cc: bpf, linux-kernel, linux-fsdevel, paul, jmorris, serge, brauner,
	viro, jack, ast, daniel, andrii, kpsingh, matt, eddyz87,
	tasos.papagiannnis

Some security policies need to inspect the original open pathname, for
example to report or block suspected path traversal attempts. A resolved
pathname no longer preserves components such as "..", and requests that
fail lookup may never reach a hook exposing the resolved target.

Today, in Tetragon[1] we capture kernel-copied names through
getname/getname_flags probes and correlates them with open
results. Enforcement additionally probes do_open and passes decisions
through BPF maps to the file_open LSM hook. This requires correlating
state across several internal probes and the enforcement hook.

This series proposes security_file_open_name(), an LSM hook exposing
the kernel-copied pathname, the VFS-selected starting path, effective
open flags, creation mode and lookup flags before pathname traversal.
BPF LSM programs can inspect these arguments and reject requests before
file creation or truncation. Existing checks on the resolved object
continue to apply.

This is submitted as an RFC in order to get feedback on whether the
proposed LSM hook is a reasonable addition.

[1] https://github.com/cilium/tetragon

Anastasios Papagiannis (2):
  security: Add hook for unresolved pathname opens
  selftests/bpf: Test pathname open enforcement

 fs/namei.c                                    |  15 ++-
 include/linux/lsm_hook_defs.h                 |   2 +
 include/linux/security.h                      |  10 ++
 security/security.c                           |  27 +++++
 .../selftests/bpf/prog_tests/file_open_name.c | 103 ++++++++++++++++++
 .../selftests/bpf/progs/file_open_name.c      |  56 ++++++++++
 6 files changed, 211 insertions(+), 2 deletions(-)
 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


base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
-- 
2.56.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [RFC PATCH 1/2] security: Add hook for unresolved pathname opens
  2026-11-20  9:48 [RFC PATCH 0/2] security: Add pre-lookup pathname open check Anastasios Papagiannis
@ 2026-11-20  9:48 ` Anastasios Papagiannis
  2026-11-20  9:48 ` [RFC PATCH 2/2] selftests/bpf: Test pathname open enforcement Anastasios Papagiannis
  1 sibling, 0 replies; 3+ messages in thread
From: Anastasios Papagiannis @ 2026-11-20  9:48 UTC (permalink / raw)
  To: linux-security-module
  Cc: bpf, linux-kernel, linux-fsdevel, paul, jmorris, serge, brauner,
	viro, jack, ast, daniel, andrii, kpsingh, matt, eddyz87,
	tasos.papagiannnis

Check the copied pathname against the selected base before traversal or
file changes, including O_PATH and O_TMPFILE.

Pass lookup flags so policy can request a referenced retry. Recheck the
base after each restart and cover kernel opens with an explicit root.

Signed-off-by: Anastasios Papagiannis <tasos.papagiannnis@gmail.com>
---
 fs/namei.c                    | 15 +++++++++++++--
 include/linux/lsm_hook_defs.h |  2 ++
 include/linux/security.h      | 10 ++++++++++
 security/security.c           | 27 +++++++++++++++++++++++++++
 4 files changed, 52 insertions(+), 2 deletions(-)

diff --git a/fs/namei.c b/fs/namei.c
index 925aeb63bfa1..fde11d36d434 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -749,6 +749,7 @@ struct nameidata {
 	} *stack, internal[EMBEDDED_LEVELS];
 	struct filename	*name;
 	const char *pathname;
+	const struct open_flags *open;
 	struct nameidata *saved;
 	unsigned	root_seq;
 	int		dfd;
@@ -768,6 +769,7 @@ static void __set_nameidata(struct nameidata *p, int dfd, struct filename *name)
 	p->dfd = dfd;
 	p->name = name;
 	p->pathname = likely(name) ? name->name : "";
+	p->open = NULL;
 	p->path.mnt = NULL;
 	p->path.dentry = NULL;
 	p->total_link_count = old ? old->total_link_count : 0;
@@ -2716,7 +2718,7 @@ static const char *path_init(struct nameidata *nd, unsigned flags)
 		} else {
 			path_get(&nd->path);
 		}
-		return s;
+		goto out;
 	}
 
 	nd->root.mnt = NULL;
@@ -2726,7 +2728,7 @@ static const char *path_init(struct nameidata *nd, unsigned flags)
 		error = nd_jump_root(nd);
 		if (unlikely(error))
 			return ERR_PTR(error);
-		return s;
+		goto out;
 	}
 
 	/* Relative pathname -- get the starting-point it is relative to. */
@@ -2784,6 +2786,14 @@ static const char *path_init(struct nameidata *nd, unsigned flags)
 			nd->state |= ND_ROOT_GRABBED;
 		}
 	}
+out:
+	if (nd->open) {
+		error = security_file_open_name(nd->name, &nd->path,
+						nd->open->open_flag, nd->open->mode,
+						nd->flags);
+		if (error)
+			return ERR_PTR(error);
+	}
 	return s;
 }
 
@@ -4999,6 +5009,7 @@ static struct file *path_openat(struct nameidata *nd,
 	struct file *file;
 	int error;
 
+	nd->open = op;
 	file = alloc_empty_file(op->open_flag, current_cred());
 	if (IS_ERR(file))
 		return file;
diff --git a/include/linux/lsm_hook_defs.h b/include/linux/lsm_hook_defs.h
index 87d9d06710cb..3537f2b73d8c 100644
--- a/include/linux/lsm_hook_defs.h
+++ b/include/linux/lsm_hook_defs.h
@@ -213,6 +213,8 @@ LSM_HOOK(void, LSM_RET_VOID, file_set_fowner, struct file *file)
 LSM_HOOK(int, 0, file_send_sigiotask, struct task_struct *tsk,
 	 struct fown_struct *fown, int sig)
 LSM_HOOK(int, 0, file_receive, struct file *file)
+LSM_HOOK(int, 0, file_open_name, const struct filename *name,
+	 const struct path *base, int flags, umode_t mode, unsigned int lookup_flags)
 LSM_HOOK(int, 0, file_open, struct file *file)
 LSM_HOOK(int, 0, file_post_open, struct file *file, int mask)
 LSM_HOOK(int, 0, file_truncate, struct file *file)
diff --git a/include/linux/security.h b/include/linux/security.h
index 40831fab5470..e6c66cf1cd1c 100644
--- a/include/linux/security.h
+++ b/include/linux/security.h
@@ -498,6 +498,9 @@ void security_file_set_fowner(struct file *file);
 int security_file_send_sigiotask(struct task_struct *tsk,
 				 struct fown_struct *fown, int sig);
 int security_file_receive(struct file *file);
+int security_file_open_name(const struct filename *name,
+			    const struct path *base, int flags, umode_t mode,
+			    unsigned int lookup_flags);
 int security_file_open(struct file *file);
 int security_file_post_open(struct file *file, int mask);
 int security_file_truncate(struct file *file);
@@ -1238,6 +1241,13 @@ static inline int security_file_receive(struct file *file)
 	return 0;
 }
 
+static inline int security_file_open_name(const struct filename *name,
+					  const struct path *base, int flags, umode_t mode,
+					  unsigned int lookup_flags)
+{
+	return 0;
+}
+
 static inline int security_file_open(struct file *file)
 {
 	return 0;
diff --git a/security/security.c b/security/security.c
index f6583d507395..d265f9347791 100644
--- a/security/security.c
+++ b/security/security.c
@@ -2742,6 +2742,33 @@ int security_file_receive(struct file *file)
 	return call_int_hook(file_receive, file);
 }
 
+/**
+ * security_file_open_name() - Authorize a pathname open before lookup
+ * @name: kernel copy of the unresolved pathname, which may be empty
+ * @base: starting path selected by the lookup
+ * @flags: effective open flags
+ * @mode: creation mode, including the file type, before applying the umask
+ * @lookup_flags: current pathname lookup flags, including LOOKUP_RCU
+ *
+ * Check the original pathname before lookup, creation, or opening the file.
+ * @base is the starting path for this attempt, or the file itself for an empty
+ * pathname.  This hook also covers O_PATH and io_uring opens.
+ *
+ * This hook must not sleep.  @name and @base are borrowed for the call.  With
+ * LOOKUP_RCU, @base is RCU-protected; otherwise it holds path references.
+ *
+ * Return: 0 to allow the open or -EACCES or -EPERM to deny it.
+ * During RCU walk, -ECHILD requests a referenced retry. A cached-only
+ * lookup returns -EAGAIN instead.
+ */
+int security_file_open_name(const struct filename *name,
+			    const struct path *base, int flags, umode_t mode,
+			    unsigned int lookup_flags)
+{
+	return call_int_hook(file_open_name, name, base, flags, mode,
+			     lookup_flags);
+}
+
 /**
  * security_file_open() - Save open() time state for late use by the LSM
  * @file:
-- 
2.56.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [RFC PATCH 2/2] selftests/bpf: Test pathname open enforcement
  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  9:48 ` Anastasios Papagiannis
  1 sibling, 0 replies; 3+ messages in thread
From: Anastasios Papagiannis @ 2026-11-20  9:48 UTC (permalink / raw)
  To: linux-security-module
  Cc: bpf, linux-kernel, linux-fsdevel, paul, jmorris, serge, brauner,
	viro, jack, ast, daniel, andrii, kpsingh, matt, eddyz87,
	tasos.papagiannnis

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-20  9:48 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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  9:48 ` [RFC PATCH 2/2] selftests/bpf: Test pathname open enforcement Anastasios Papagiannis

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®