mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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 1/2] security: Add hook for unresolved pathname opens
Date: Fri, 20 Nov 2026 11:48:19 +0200	[thread overview]
Message-ID: <20261120094820.11929-2-tasos.papagiannnis@gmail.com> (raw)
In-Reply-To: <20261120094820.11929-1-tasos.papagiannnis@gmail.com>

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


  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 ` Anastasios Papagiannis [this message]
2026-11-20 10:44   ` [RFC PATCH 1/2] security: Add hook for unresolved pathname opens bot+bpf-ci
2026-11-20  9:48 ` [RFC PATCH 2/2] selftests/bpf: Test pathname open enforcement Anastasios Papagiannis

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-2-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®