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/security.h>
10 #include <linux/file.h>
11 #include <linux/seq_file.h>
14 #include <linux/proc_fs.h>
20 static int seq_show(struct seq_file *m, void *v)
22 struct files_struct *files = NULL;
23 int f_flags = 0, ret = -ENOENT;
24 struct file *file = NULL;
25 struct task_struct *task;
27 task = get_proc_task(m->private);
34 unsigned int fd = proc_fd(m->private);
36 spin_lock(&files->file_lock);
37 file = files_lookup_fd_locked(files, fd);
39 struct fdtable *fdt = files_fdtable(files);
41 f_flags = file->f_flags;
42 if (close_on_exec(fd, fdt))
48 spin_unlock(&files->file_lock);
51 put_task_struct(task);
56 seq_printf(m, "pos:\t%lli\nflags:\t0%o\nmnt_id:\t%i\n",
57 (long long)file->f_pos, f_flags,
58 real_mount(file->f_path.mnt)->mnt_id);
60 /* show_fd_locks() never deferences files so a stale value is safe */
61 show_fd_locks(m, file, files);
62 if (seq_has_overflowed(m))
65 if (file->f_op->show_fdinfo)
66 file->f_op->show_fdinfo(m, file);
73 static int seq_fdinfo_open(struct inode *inode, struct file *file)
75 return single_open(file, seq_show, inode);
78 static const struct file_operations proc_fdinfo_file_operations = {
79 .open = seq_fdinfo_open,
82 .release = single_release,
85 static bool tid_fd_mode(struct task_struct *task, unsigned fd, fmode_t *mode)
90 file = task_lookup_fd_rcu(task, fd);
97 static void tid_fd_update_inode(struct task_struct *task, struct inode *inode,
100 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
102 if (S_ISLNK(inode->i_mode)) {
103 unsigned i_mode = S_IFLNK;
104 if (f_mode & FMODE_READ)
105 i_mode |= S_IRUSR | S_IXUSR;
106 if (f_mode & FMODE_WRITE)
107 i_mode |= S_IWUSR | S_IXUSR;
108 inode->i_mode = i_mode;
110 security_task_to_inode(task, inode);
113 static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags)
115 struct task_struct *task;
119 if (flags & LOOKUP_RCU)
122 inode = d_inode(dentry);
123 task = get_proc_task(inode);
128 if (tid_fd_mode(task, fd, &f_mode)) {
129 tid_fd_update_inode(task, inode, f_mode);
130 put_task_struct(task);
133 put_task_struct(task);
138 static const struct dentry_operations tid_fd_dentry_operations = {
139 .d_revalidate = tid_fd_revalidate,
140 .d_delete = pid_delete_dentry,
143 static int proc_fd_link(struct dentry *dentry, struct path *path)
145 struct task_struct *task;
148 task = get_proc_task(d_inode(dentry));
150 unsigned int fd = proc_fd(d_inode(dentry));
151 struct file *fd_file;
153 fd_file = fget_task(task, fd);
155 *path = fd_file->f_path;
156 path_get(&fd_file->f_path);
160 put_task_struct(task);
171 static struct dentry *proc_fd_instantiate(struct dentry *dentry,
172 struct task_struct *task, const void *ptr)
174 const struct fd_data *data = ptr;
175 struct proc_inode *ei;
178 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK);
180 return ERR_PTR(-ENOENT);
185 inode->i_op = &proc_pid_link_inode_operations;
188 ei->op.proc_get_link = proc_fd_link;
189 tid_fd_update_inode(task, inode, data->mode);
191 d_set_d_op(dentry, &tid_fd_dentry_operations);
192 return d_splice_alias(inode, dentry);
195 static struct dentry *proc_lookupfd_common(struct inode *dir,
196 struct dentry *dentry,
197 instantiate_t instantiate)
199 struct task_struct *task = get_proc_task(dir);
200 struct fd_data data = {.fd = name_to_int(&dentry->d_name)};
201 struct dentry *result = ERR_PTR(-ENOENT);
207 if (!tid_fd_mode(task, data.fd, &data.mode))
210 result = instantiate(dentry, task, &data);
212 put_task_struct(task);
217 static int proc_readfd_common(struct file *file, struct dir_context *ctx,
218 instantiate_t instantiate)
220 struct task_struct *p = get_proc_task(file_inode(file));
226 if (!dir_emit_dots(file, ctx))
230 for (fd = ctx->pos - 2;; fd++) {
236 f = task_lookup_next_fd_rcu(p, &fd);
240 data.mode = f->f_mode;
244 len = snprintf(name, sizeof(name), "%u", fd);
245 if (!proc_fill_cache(file, ctx,
246 name, len, instantiate, p,
258 static int proc_readfd(struct file *file, struct dir_context *ctx)
260 return proc_readfd_common(file, ctx, proc_fd_instantiate);
263 const struct file_operations proc_fd_operations = {
264 .read = generic_read_dir,
265 .iterate_shared = proc_readfd,
266 .llseek = generic_file_llseek,
269 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
272 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
276 * /proc/pid/fd needs a special permission handler so that a process can still
277 * access /proc/self/fd after it has executed a setuid().
279 int proc_fd_permission(struct user_namespace *mnt_userns,
280 struct inode *inode, int mask)
282 struct task_struct *p;
285 rv = generic_permission(&init_user_ns, inode, mask);
290 p = pid_task(proc_pid(inode), PIDTYPE_PID);
291 if (p && same_thread_group(p, current))
298 const struct inode_operations proc_fd_inode_operations = {
299 .lookup = proc_lookupfd,
300 .permission = proc_fd_permission,
301 .setattr = proc_setattr,
304 static struct dentry *proc_fdinfo_instantiate(struct dentry *dentry,
305 struct task_struct *task, const void *ptr)
307 const struct fd_data *data = ptr;
308 struct proc_inode *ei;
311 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFREG | S_IRUSR);
313 return ERR_PTR(-ENOENT);
318 inode->i_fop = &proc_fdinfo_file_operations;
319 tid_fd_update_inode(task, inode, 0);
321 d_set_d_op(dentry, &tid_fd_dentry_operations);
322 return d_splice_alias(inode, dentry);
325 static struct dentry *
326 proc_lookupfdinfo(struct inode *dir, struct dentry *dentry, unsigned int flags)
328 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
331 static int proc_readfdinfo(struct file *file, struct dir_context *ctx)
333 return proc_readfd_common(file, ctx,
334 proc_fdinfo_instantiate);
337 const struct inode_operations proc_fdinfo_inode_operations = {
338 .lookup = proc_lookupfdinfo,
339 .setattr = proc_setattr,
342 const struct file_operations proc_fdinfo_operations = {
343 .read = generic_read_dir,
344 .iterate_shared = proc_readfdinfo,
345 .llseek = generic_file_llseek,