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