/proc//fd can only be opened by the owner of the task and /proc//fdinfo only with PTRACE_MODE_READ. But once open the directory lists the task's descriptors for as long as it lives, also after it executed a set-user-ID program. Use the exec id helpers. A single check at the start of getdents() isn't enough. The entries are copied to userspace while the file table is walked and the caller decides how long that takes. With a buffer backed by userfaultfd he can sleep in the first copy until the task has executed the other program and then read its file table. Holding exec_update_lock across the copies isn't an option either because then the reader blocks the execve(). So take it around every fget_task_next() instead. All this tells is which descriptors a set-user-ID program has open and how, so there's not much at stake. But there's no reason to allow it either. Reported-by: Josef Bacik Suggested-by: Jann Horn Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: Christian Brauner (Amutable) --- fs/proc/fd.c | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/fs/proc/fd.c b/fs/proc/fd.c index e13015a17898..2d51344267ee 100644 --- a/fs/proc/fd.c +++ b/fs/proc/fd.c @@ -249,17 +249,33 @@ static struct dentry *proc_lookupfd_common(struct inode *dir, return result; } +/* fget_task_next(), unless the task now runs a program the opener can't see */ +static struct file *proc_fd_next(struct file *file, struct task_struct *task, + unsigned int *fd) +{ + struct file *f; + int err; + + err = proc_exec_id_lock(file, task, file->private_data); + if (err) + return ERR_PTR(err); + f = fget_task_next(task, fd); + proc_exec_id_unlock(task); + return f; +} + static int proc_readfd_common(struct file *file, struct dir_context *ctx, instantiate_t instantiate) { - struct task_struct *p = get_proc_task(file_inode(file)); + struct task_struct *p __free(put_task) = NULL; unsigned int fd; + p = get_proc_task(file_inode(file)); if (!p) return -ENOENT; if (!dir_emit_dots(file, ctx)) - goto out; + return 0; for (fd = ctx->pos - 2;; fd++) { struct file *f; @@ -267,10 +283,13 @@ static int proc_readfd_common(struct file *file, struct dir_context *ctx, char name[10 + 1]; unsigned int len; - f = fget_task_next(p, &fd); + /* Don't hold off execve() across the copy to userspace. */ + f = proc_fd_next(file, p, &fd); + if (IS_ERR(f)) + return PTR_ERR(f); ctx->pos = fd + 2LL; if (!f) - break; + return 0; data.mode = f->f_mode; fput(f); data.fd = fd; @@ -279,12 +298,9 @@ static int proc_readfd_common(struct file *file, struct dir_context *ctx, if (!proc_fill_cache(file, ctx, name, len, instantiate, p, &data)) - break; + return 0; cond_resched(); } -out: - put_task_struct(p); - return 0; } static int proc_readfd_count(struct inode *inode, loff_t *count) @@ -317,6 +333,8 @@ static int proc_fd_iterate(struct file *file, struct dir_context *ctx) } const struct file_operations proc_fd_operations = { + .open = proc_exec_id_dir_open, + .release = proc_exec_id_dir_release, .read = generic_read_dir, .iterate_shared = proc_fd_iterate, .llseek = generic_file_llseek, @@ -410,6 +428,8 @@ const struct inode_operations proc_fdinfo_inode_operations = { }; const struct file_operations proc_fdinfo_operations = { + .open = proc_exec_id_dir_open, + .release = proc_exec_id_dir_release, .read = generic_read_dir, .iterate_shared = proc_fdinfo_iterate, .llseek = generic_file_llseek, -- 2.53.0