1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/sched/signal.h>
3 #include <linux/errno.h>
4 #include <linux/dcache.h>
5 #include <linux/path.h>
6 #include <linux/fdtable.h>
7 #include <linux/namei.h>
9 #include <linux/ptrace.h>
10 #include <linux/security.h>
11 #include <linux/file.h>
12 #include <linux/seq_file.h>
15 #include <linux/proc_fs.h>
21 static int seq_show(struct seq_file *m, void *v)
23 struct files_struct *files = NULL;
24 int f_flags = 0, ret = -ENOENT;
25 struct file *file = NULL;
26 struct task_struct *task;
28 task = get_proc_task(m->private);
35 unsigned int fd = proc_fd(m->private);
37 spin_lock(&files->file_lock);
38 file = files_lookup_fd_locked(files, fd);
40 struct fdtable *fdt = files_fdtable(files);
42 f_flags = file->f_flags;
43 if (close_on_exec(fd, fdt))
49 spin_unlock(&files->file_lock);
52 put_task_struct(task);
57 seq_printf(m, "pos:\t%lli\nflags:\t0%o\nmnt_id:\t%i\nino:\t%lu\n",
58 (long long)file->f_pos, f_flags,
59 real_mount(file->f_path.mnt)->mnt_id,
60 file_inode(file)->i_ino);
62 /* show_fd_locks() never deferences files so a stale value is safe */
63 show_fd_locks(m, file, files);
64 if (seq_has_overflowed(m))
67 if (file->f_op->show_fdinfo)
68 file->f_op->show_fdinfo(m, file);
75 static int proc_fdinfo_access_allowed(struct inode *inode)
78 struct task_struct *task = get_proc_task(inode);
83 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
84 put_task_struct(task);
92 static int seq_fdinfo_open(struct inode *inode, struct file *file)
94 int ret = proc_fdinfo_access_allowed(inode);
99 return single_open(file, seq_show, inode);
102 static const struct file_operations proc_fdinfo_file_operations = {
103 .open = seq_fdinfo_open,
106 .release = single_release,
109 static bool tid_fd_mode(struct task_struct *task, unsigned fd, fmode_t *mode)
114 file = task_lookup_fd_rcu(task, fd);
116 *mode = file->f_mode;
121 static void tid_fd_update_inode(struct task_struct *task, struct inode *inode,
124 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
126 if (S_ISLNK(inode->i_mode)) {
127 unsigned i_mode = S_IFLNK;
128 if (f_mode & FMODE_READ)
129 i_mode |= S_IRUSR | S_IXUSR;
130 if (f_mode & FMODE_WRITE)
131 i_mode |= S_IWUSR | S_IXUSR;
132 inode->i_mode = i_mode;
134 security_task_to_inode(task, inode);
137 static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags)
139 struct task_struct *task;
143 if (flags & LOOKUP_RCU)
146 inode = d_inode(dentry);
147 task = get_proc_task(inode);
152 if (tid_fd_mode(task, fd, &f_mode)) {
153 tid_fd_update_inode(task, inode, f_mode);
154 put_task_struct(task);
157 put_task_struct(task);
162 static const struct dentry_operations tid_fd_dentry_operations = {
163 .d_revalidate = tid_fd_revalidate,
164 .d_delete = pid_delete_dentry,
167 static int proc_fd_link(struct dentry *dentry, struct path *path)
169 struct task_struct *task;
172 task = get_proc_task(d_inode(dentry));
174 unsigned int fd = proc_fd(d_inode(dentry));
175 struct file *fd_file;
177 fd_file = fget_task(task, fd);
179 *path = fd_file->f_path;
180 path_get(&fd_file->f_path);
184 put_task_struct(task);
195 static struct dentry *proc_fd_instantiate(struct dentry *dentry,
196 struct task_struct *task, const void *ptr)
198 const struct fd_data *data = ptr;
199 struct proc_inode *ei;
202 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK);
204 return ERR_PTR(-ENOENT);
209 inode->i_op = &proc_pid_link_inode_operations;
212 ei->op.proc_get_link = proc_fd_link;
213 tid_fd_update_inode(task, inode, data->mode);
215 d_set_d_op(dentry, &tid_fd_dentry_operations);
216 return d_splice_alias(inode, dentry);
219 static struct dentry *proc_lookupfd_common(struct inode *dir,
220 struct dentry *dentry,
221 instantiate_t instantiate)
223 struct task_struct *task = get_proc_task(dir);
224 struct fd_data data = {.fd = name_to_int(&dentry->d_name)};
225 struct dentry *result = ERR_PTR(-ENOENT);
231 if (!tid_fd_mode(task, data.fd, &data.mode))
234 result = instantiate(dentry, task, &data);
236 put_task_struct(task);
241 static int proc_readfd_common(struct file *file, struct dir_context *ctx,
242 instantiate_t instantiate)
244 struct task_struct *p = get_proc_task(file_inode(file));
250 if (!dir_emit_dots(file, ctx))
254 for (fd = ctx->pos - 2;; fd++) {
260 f = task_lookup_next_fd_rcu(p, &fd);
264 data.mode = f->f_mode;
268 len = snprintf(name, sizeof(name), "%u", fd);
269 if (!proc_fill_cache(file, ctx,
270 name, len, instantiate, p,
282 static int proc_readfd(struct file *file, struct dir_context *ctx)
284 return proc_readfd_common(file, ctx, proc_fd_instantiate);
287 const struct file_operations proc_fd_operations = {
288 .read = generic_read_dir,
289 .iterate_shared = proc_readfd,
290 .llseek = generic_file_llseek,
293 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
296 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
300 * /proc/pid/fd needs a special permission handler so that a process can still
301 * access /proc/self/fd after it has executed a setuid().
303 int proc_fd_permission(struct user_namespace *mnt_userns,
304 struct inode *inode, int mask)
306 struct task_struct *p;
309 rv = generic_permission(&init_user_ns, inode, mask);
314 p = pid_task(proc_pid(inode), PIDTYPE_PID);
315 if (p && same_thread_group(p, current))
322 const struct inode_operations proc_fd_inode_operations = {
323 .lookup = proc_lookupfd,
324 .permission = proc_fd_permission,
325 .setattr = proc_setattr,
328 static struct dentry *proc_fdinfo_instantiate(struct dentry *dentry,
329 struct task_struct *task, const void *ptr)
331 const struct fd_data *data = ptr;
332 struct proc_inode *ei;
335 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFREG | S_IRUGO);
337 return ERR_PTR(-ENOENT);
342 inode->i_fop = &proc_fdinfo_file_operations;
343 tid_fd_update_inode(task, inode, 0);
345 d_set_d_op(dentry, &tid_fd_dentry_operations);
346 return d_splice_alias(inode, dentry);
349 static struct dentry *
350 proc_lookupfdinfo(struct inode *dir, struct dentry *dentry, unsigned int flags)
352 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
355 static int proc_readfdinfo(struct file *file, struct dir_context *ctx)
357 return proc_readfd_common(file, ctx,
358 proc_fdinfo_instantiate);
361 static int proc_open_fdinfo(struct inode *inode, struct file *file)
363 int ret = proc_fdinfo_access_allowed(inode);
371 const struct inode_operations proc_fdinfo_inode_operations = {
372 .lookup = proc_lookupfdinfo,
373 .setattr = proc_setattr,
376 const struct file_operations proc_fdinfo_operations = {
377 .open = proc_open_fdinfo,
378 .read = generic_read_dir,
379 .iterate_shared = proc_readfdinfo,
380 .llseek = generic_file_llseek,