Some files in /proc/ check access to the task when they are opened but look at the task whenever they are read. Such a file keeps working after the task has executed a set-user-ID program and then shows the state of a program that its opener has no access to. Checking again with the credentials of the reader on every read would break readers for which nothing changed, e.g. a reader that dropped its privileges after it opened the file or that got the file from a more privileged process. And it turns a privileged reader into a confused deputy. An unprivileged opener hands the file to a set-user-ID program, e.g. as stdin, and that program reads what its opener isn't allowed to see. So go by the opener and only once the program has changed: - proc_exec_id_open() remembers the task's ->self_exec_id when the file is opened. The permission check of the open may have seen the previous program so it's repeated with exec_update_lock held. - proc_exec_id_lock() takes exec_update_lock for reading and compares. If the task executed another program since, the file only keeps working if its opener would be allowed to open it now. If so, the file is as good as opened again and we remember the new program. The task's own threads take no lock. de_thread() waits until all threads but the one that calls execve() are gone before exec_update_lock is taken so they can't see the program change. That's /proc/self, i.e. nearly all users. No user yet. Suggested-by: Jann Horn Signed-off-by: Christian Brauner (Amutable) --- fs/proc/base.c | 137 +++++++++++++++++++++++++++++++++++++++++++++++++++++ fs/proc/internal.h | 8 ++++ 2 files changed, 145 insertions(+) diff --git a/fs/proc/base.c b/fs/proc/base.c index 2a9ad9e01047..600cdbfdefac 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -2036,6 +2036,143 @@ void pid_update_inode(struct task_struct *task, struct inode *inode) security_task_to_inode(task, inode); } +/* + * For files that check access when they're opened but look at the task whenever + * they're used. ->self_exec_id names the program: execve() bumps it under + * exec_update_lock and all threads share it, also the two that de_thread() + * swaps the pid between, so it doesn't matter which task get_proc_task() + * returns. + */ + +/* Would the opener of @file be allowed to open it now? */ +static int proc_exec_id_permission(const struct file *file, + struct task_struct *task) +{ + struct inode *inode = file_inode(file); + int mask = 0; + + if (file->f_mode & FMODE_READ) + mask |= MAY_READ; + if (file->f_mode & FMODE_WRITE) + mask |= MAY_WRITE; + + pid_update_inode(task, inode); + scoped_with_creds(file->f_cred) + return inode_permission(&nop_mnt_idmap, inode, mask); +} + +/* For ->open(). ->permission() ran without exec_update_lock, so check again. */ +static u64 *proc_exec_id_open(struct inode *inode, struct file *file) +{ + struct task_struct *task __free(put_task) = NULL; + u64 *exec_id __free(kfree) = NULL; + int err; + + exec_id = kzalloc_obj(*exec_id, GFP_KERNEL_ACCOUNT); + if (!exec_id) + return ERR_PTR(-ENOMEM); + + /* The file won't find the task either, it never comes back. */ + task = get_proc_task(inode); + if (!task) + return no_free_ptr(exec_id); + + /* See proc_exec_id_lock(). */ + if (same_thread_group(task, current)) { + *exec_id = task->self_exec_id; + return no_free_ptr(exec_id); + } + + err = down_read_killable(&task->signal->exec_update_lock); + if (err) + return ERR_PTR(err); + *exec_id = task->self_exec_id; + err = proc_exec_id_permission(file, task); + up_read(&task->signal->exec_update_lock); + if (err) + return ERR_PTR(err); + return no_free_ptr(exec_id); +} + +int proc_exec_id_single_open(struct inode *inode, struct file *file, + int (*show)(struct seq_file *, void *)) +{ + u64 *exec_id __free(kfree) = NULL; + int err; + + exec_id = proc_exec_id_open(inode, file); + if (IS_ERR(exec_id)) + return PTR_ERR(exec_id); + + err = single_open(file, show, exec_id); + if (err) + return err; + + retain_and_null_ptr(exec_id); + return 0; +} + +int proc_exec_id_single_release(struct inode *inode, struct file *file) +{ + struct seq_file *m = file->private_data; + + kfree(m->private); + return single_release(inode, file); +} + +int proc_exec_id_dir_open(struct inode *inode, struct file *file) +{ + u64 *exec_id = proc_exec_id_open(inode, file); + + if (IS_ERR(exec_id)) + return PTR_ERR(exec_id); + + file->private_data = exec_id; + return 0; +} + +int proc_exec_id_dir_release(struct inode *inode, struct file *file) +{ + kfree(file->private_data); + return 0; +} + +/* + * Pins the task's program until proc_exec_id_unlock(). If it changed since the + * open, the file only keeps working if its opener would be allowed to open it + * now. Whoever uses the file doesn't count. Our own threads need no lock, + * de_thread() waits for them before exec_update_lock is taken. + */ +int proc_exec_id_lock(const struct file *file, struct task_struct *task, + u64 *exec_id) +{ + int err; + + if (!same_thread_group(task, current)) { + err = down_read_killable(&task->signal->exec_update_lock); + if (err) + return err; + } + + if (task->self_exec_id == READ_ONCE(*exec_id)) + return 0; + + err = proc_exec_id_permission(file, task); + if (err) { + proc_exec_id_unlock(task); + return err; + } + + WRITE_ONCE(*exec_id, task->self_exec_id); + return 0; +} + +void proc_exec_id_unlock(struct task_struct *task) +{ + if (!same_thread_group(task, current)) + up_read(&task->signal->exec_update_lock); +} + /* * Rewrite the inode's ownerships here because the owning task may have * performed a setuid(), etc. diff --git a/fs/proc/internal.h b/fs/proc/internal.h index df873df9afc5..67d9e965162c 100644 --- a/fs/proc/internal.h +++ b/fs/proc/internal.h @@ -265,6 +265,14 @@ int proc_nochmod_setattr(const struct mnt_idmap *idmap, struct dentry *dentry, extern void proc_pid_evict_inode(struct proc_inode *); extern struct inode *proc_pid_make_inode(struct super_block *, struct task_struct *, umode_t); extern void pid_update_inode(struct task_struct *, struct inode *); +int proc_exec_id_single_open(struct inode *inode, struct file *file, + int (*show)(struct seq_file *, void *)); +int proc_exec_id_single_release(struct inode *inode, struct file *file); +int proc_exec_id_dir_open(struct inode *inode, struct file *file); +int proc_exec_id_dir_release(struct inode *inode, struct file *file); +int proc_exec_id_lock(const struct file *file, struct task_struct *task, + u64 *exec_id); +void proc_exec_id_unlock(struct task_struct *task); extern int pid_delete_dentry(const struct dentry *); extern int proc_pid_readdir(struct file *, struct dir_context *); struct dentry *proc_pid_lookup(struct dentry *, unsigned int); -- 2.53.0