1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun #include <linux/sched/signal.h>
3*4882a593Smuzhiyun #include <linux/errno.h>
4*4882a593Smuzhiyun #include <linux/dcache.h>
5*4882a593Smuzhiyun #include <linux/path.h>
6*4882a593Smuzhiyun #include <linux/fdtable.h>
7*4882a593Smuzhiyun #include <linux/namei.h>
8*4882a593Smuzhiyun #include <linux/pid.h>
9*4882a593Smuzhiyun #include <linux/ptrace.h>
10*4882a593Smuzhiyun #include <linux/security.h>
11*4882a593Smuzhiyun #include <linux/file.h>
12*4882a593Smuzhiyun #include <linux/seq_file.h>
13*4882a593Smuzhiyun #include <linux/fs.h>
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun #include <linux/proc_fs.h>
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #include "../mount.h"
18*4882a593Smuzhiyun #include "internal.h"
19*4882a593Smuzhiyun #include "fd.h"
20*4882a593Smuzhiyun
seq_show(struct seq_file * m,void * v)21*4882a593Smuzhiyun static int seq_show(struct seq_file *m, void *v)
22*4882a593Smuzhiyun {
23*4882a593Smuzhiyun struct files_struct *files = NULL;
24*4882a593Smuzhiyun int f_flags = 0, ret = -ENOENT;
25*4882a593Smuzhiyun struct file *file = NULL;
26*4882a593Smuzhiyun struct task_struct *task;
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun task = get_proc_task(m->private);
29*4882a593Smuzhiyun if (!task)
30*4882a593Smuzhiyun return -ENOENT;
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun files = get_files_struct(task);
33*4882a593Smuzhiyun put_task_struct(task);
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun if (files) {
36*4882a593Smuzhiyun unsigned int fd = proc_fd(m->private);
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun spin_lock(&files->file_lock);
39*4882a593Smuzhiyun file = fcheck_files(files, fd);
40*4882a593Smuzhiyun if (file) {
41*4882a593Smuzhiyun struct fdtable *fdt = files_fdtable(files);
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun f_flags = file->f_flags;
44*4882a593Smuzhiyun if (close_on_exec(fd, fdt))
45*4882a593Smuzhiyun f_flags |= O_CLOEXEC;
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun get_file(file);
48*4882a593Smuzhiyun ret = 0;
49*4882a593Smuzhiyun }
50*4882a593Smuzhiyun spin_unlock(&files->file_lock);
51*4882a593Smuzhiyun put_files_struct(files);
52*4882a593Smuzhiyun }
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun if (ret)
55*4882a593Smuzhiyun return ret;
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun seq_printf(m, "pos:\t%lli\nflags:\t0%o\nmnt_id:\t%i\nino:\t%lu\n",
58*4882a593Smuzhiyun (long long)file->f_pos, f_flags,
59*4882a593Smuzhiyun real_mount(file->f_path.mnt)->mnt_id,
60*4882a593Smuzhiyun file_inode(file)->i_ino);
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun show_fd_locks(m, file, files);
63*4882a593Smuzhiyun if (seq_has_overflowed(m))
64*4882a593Smuzhiyun goto out;
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun if (file->f_op->show_fdinfo)
67*4882a593Smuzhiyun file->f_op->show_fdinfo(m, file);
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun out:
70*4882a593Smuzhiyun fput(file);
71*4882a593Smuzhiyun return 0;
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun
proc_fdinfo_access_allowed(struct inode * inode)74*4882a593Smuzhiyun static int proc_fdinfo_access_allowed(struct inode *inode)
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun bool allowed = false;
77*4882a593Smuzhiyun struct task_struct *task = get_proc_task(inode);
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun if (!task)
80*4882a593Smuzhiyun return -ESRCH;
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
83*4882a593Smuzhiyun put_task_struct(task);
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun if (!allowed)
86*4882a593Smuzhiyun return -EACCES;
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun return 0;
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun
seq_fdinfo_open(struct inode * inode,struct file * file)91*4882a593Smuzhiyun static int seq_fdinfo_open(struct inode *inode, struct file *file)
92*4882a593Smuzhiyun {
93*4882a593Smuzhiyun int ret = proc_fdinfo_access_allowed(inode);
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun if (ret)
96*4882a593Smuzhiyun return ret;
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun return single_open(file, seq_show, inode);
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun static const struct file_operations proc_fdinfo_file_operations = {
102*4882a593Smuzhiyun .open = seq_fdinfo_open,
103*4882a593Smuzhiyun .read = seq_read,
104*4882a593Smuzhiyun .llseek = seq_lseek,
105*4882a593Smuzhiyun .release = single_release,
106*4882a593Smuzhiyun };
107*4882a593Smuzhiyun
tid_fd_mode(struct task_struct * task,unsigned fd,fmode_t * mode)108*4882a593Smuzhiyun static bool tid_fd_mode(struct task_struct *task, unsigned fd, fmode_t *mode)
109*4882a593Smuzhiyun {
110*4882a593Smuzhiyun struct files_struct *files = get_files_struct(task);
111*4882a593Smuzhiyun struct file *file;
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun if (!files)
114*4882a593Smuzhiyun return false;
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun rcu_read_lock();
117*4882a593Smuzhiyun file = fcheck_files(files, fd);
118*4882a593Smuzhiyun if (file)
119*4882a593Smuzhiyun *mode = file->f_mode;
120*4882a593Smuzhiyun rcu_read_unlock();
121*4882a593Smuzhiyun put_files_struct(files);
122*4882a593Smuzhiyun return !!file;
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun
tid_fd_update_inode(struct task_struct * task,struct inode * inode,fmode_t f_mode)125*4882a593Smuzhiyun static void tid_fd_update_inode(struct task_struct *task, struct inode *inode,
126*4882a593Smuzhiyun fmode_t f_mode)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun if (S_ISLNK(inode->i_mode)) {
131*4882a593Smuzhiyun unsigned i_mode = S_IFLNK;
132*4882a593Smuzhiyun if (f_mode & FMODE_READ)
133*4882a593Smuzhiyun i_mode |= S_IRUSR | S_IXUSR;
134*4882a593Smuzhiyun if (f_mode & FMODE_WRITE)
135*4882a593Smuzhiyun i_mode |= S_IWUSR | S_IXUSR;
136*4882a593Smuzhiyun inode->i_mode = i_mode;
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun security_task_to_inode(task, inode);
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun
tid_fd_revalidate(struct dentry * dentry,unsigned int flags)141*4882a593Smuzhiyun static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun struct task_struct *task;
144*4882a593Smuzhiyun struct inode *inode;
145*4882a593Smuzhiyun unsigned int fd;
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun if (flags & LOOKUP_RCU)
148*4882a593Smuzhiyun return -ECHILD;
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun inode = d_inode(dentry);
151*4882a593Smuzhiyun task = get_proc_task(inode);
152*4882a593Smuzhiyun fd = proc_fd(inode);
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun if (task) {
155*4882a593Smuzhiyun fmode_t f_mode;
156*4882a593Smuzhiyun if (tid_fd_mode(task, fd, &f_mode)) {
157*4882a593Smuzhiyun tid_fd_update_inode(task, inode, f_mode);
158*4882a593Smuzhiyun put_task_struct(task);
159*4882a593Smuzhiyun return 1;
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun put_task_struct(task);
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun return 0;
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun static const struct dentry_operations tid_fd_dentry_operations = {
167*4882a593Smuzhiyun .d_revalidate = tid_fd_revalidate,
168*4882a593Smuzhiyun .d_delete = pid_delete_dentry,
169*4882a593Smuzhiyun };
170*4882a593Smuzhiyun
proc_fd_link(struct dentry * dentry,struct path * path)171*4882a593Smuzhiyun static int proc_fd_link(struct dentry *dentry, struct path *path)
172*4882a593Smuzhiyun {
173*4882a593Smuzhiyun struct files_struct *files = NULL;
174*4882a593Smuzhiyun struct task_struct *task;
175*4882a593Smuzhiyun int ret = -ENOENT;
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun task = get_proc_task(d_inode(dentry));
178*4882a593Smuzhiyun if (task) {
179*4882a593Smuzhiyun files = get_files_struct(task);
180*4882a593Smuzhiyun put_task_struct(task);
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun if (files) {
184*4882a593Smuzhiyun unsigned int fd = proc_fd(d_inode(dentry));
185*4882a593Smuzhiyun struct file *fd_file;
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun spin_lock(&files->file_lock);
188*4882a593Smuzhiyun fd_file = fcheck_files(files, fd);
189*4882a593Smuzhiyun if (fd_file) {
190*4882a593Smuzhiyun *path = fd_file->f_path;
191*4882a593Smuzhiyun path_get(&fd_file->f_path);
192*4882a593Smuzhiyun ret = 0;
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun spin_unlock(&files->file_lock);
195*4882a593Smuzhiyun put_files_struct(files);
196*4882a593Smuzhiyun }
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun return ret;
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun struct fd_data {
202*4882a593Smuzhiyun fmode_t mode;
203*4882a593Smuzhiyun unsigned fd;
204*4882a593Smuzhiyun };
205*4882a593Smuzhiyun
proc_fd_instantiate(struct dentry * dentry,struct task_struct * task,const void * ptr)206*4882a593Smuzhiyun static struct dentry *proc_fd_instantiate(struct dentry *dentry,
207*4882a593Smuzhiyun struct task_struct *task, const void *ptr)
208*4882a593Smuzhiyun {
209*4882a593Smuzhiyun const struct fd_data *data = ptr;
210*4882a593Smuzhiyun struct proc_inode *ei;
211*4882a593Smuzhiyun struct inode *inode;
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK);
214*4882a593Smuzhiyun if (!inode)
215*4882a593Smuzhiyun return ERR_PTR(-ENOENT);
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun ei = PROC_I(inode);
218*4882a593Smuzhiyun ei->fd = data->fd;
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun inode->i_op = &proc_pid_link_inode_operations;
221*4882a593Smuzhiyun inode->i_size = 64;
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun ei->op.proc_get_link = proc_fd_link;
224*4882a593Smuzhiyun tid_fd_update_inode(task, inode, data->mode);
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun d_set_d_op(dentry, &tid_fd_dentry_operations);
227*4882a593Smuzhiyun return d_splice_alias(inode, dentry);
228*4882a593Smuzhiyun }
229*4882a593Smuzhiyun
proc_lookupfd_common(struct inode * dir,struct dentry * dentry,instantiate_t instantiate)230*4882a593Smuzhiyun static struct dentry *proc_lookupfd_common(struct inode *dir,
231*4882a593Smuzhiyun struct dentry *dentry,
232*4882a593Smuzhiyun instantiate_t instantiate)
233*4882a593Smuzhiyun {
234*4882a593Smuzhiyun struct task_struct *task = get_proc_task(dir);
235*4882a593Smuzhiyun struct fd_data data = {.fd = name_to_int(&dentry->d_name)};
236*4882a593Smuzhiyun struct dentry *result = ERR_PTR(-ENOENT);
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun if (!task)
239*4882a593Smuzhiyun goto out_no_task;
240*4882a593Smuzhiyun if (data.fd == ~0U)
241*4882a593Smuzhiyun goto out;
242*4882a593Smuzhiyun if (!tid_fd_mode(task, data.fd, &data.mode))
243*4882a593Smuzhiyun goto out;
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun result = instantiate(dentry, task, &data);
246*4882a593Smuzhiyun out:
247*4882a593Smuzhiyun put_task_struct(task);
248*4882a593Smuzhiyun out_no_task:
249*4882a593Smuzhiyun return result;
250*4882a593Smuzhiyun }
251*4882a593Smuzhiyun
proc_readfd_common(struct file * file,struct dir_context * ctx,instantiate_t instantiate)252*4882a593Smuzhiyun static int proc_readfd_common(struct file *file, struct dir_context *ctx,
253*4882a593Smuzhiyun instantiate_t instantiate)
254*4882a593Smuzhiyun {
255*4882a593Smuzhiyun struct task_struct *p = get_proc_task(file_inode(file));
256*4882a593Smuzhiyun struct files_struct *files;
257*4882a593Smuzhiyun unsigned int fd;
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun if (!p)
260*4882a593Smuzhiyun return -ENOENT;
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun if (!dir_emit_dots(file, ctx))
263*4882a593Smuzhiyun goto out;
264*4882a593Smuzhiyun files = get_files_struct(p);
265*4882a593Smuzhiyun if (!files)
266*4882a593Smuzhiyun goto out;
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun rcu_read_lock();
269*4882a593Smuzhiyun for (fd = ctx->pos - 2;
270*4882a593Smuzhiyun fd < files_fdtable(files)->max_fds;
271*4882a593Smuzhiyun fd++, ctx->pos++) {
272*4882a593Smuzhiyun struct file *f;
273*4882a593Smuzhiyun struct fd_data data;
274*4882a593Smuzhiyun char name[10 + 1];
275*4882a593Smuzhiyun unsigned int len;
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun f = fcheck_files(files, fd);
278*4882a593Smuzhiyun if (!f)
279*4882a593Smuzhiyun continue;
280*4882a593Smuzhiyun data.mode = f->f_mode;
281*4882a593Smuzhiyun rcu_read_unlock();
282*4882a593Smuzhiyun data.fd = fd;
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun len = snprintf(name, sizeof(name), "%u", fd);
285*4882a593Smuzhiyun if (!proc_fill_cache(file, ctx,
286*4882a593Smuzhiyun name, len, instantiate, p,
287*4882a593Smuzhiyun &data))
288*4882a593Smuzhiyun goto out_fd_loop;
289*4882a593Smuzhiyun cond_resched();
290*4882a593Smuzhiyun rcu_read_lock();
291*4882a593Smuzhiyun }
292*4882a593Smuzhiyun rcu_read_unlock();
293*4882a593Smuzhiyun out_fd_loop:
294*4882a593Smuzhiyun put_files_struct(files);
295*4882a593Smuzhiyun out:
296*4882a593Smuzhiyun put_task_struct(p);
297*4882a593Smuzhiyun return 0;
298*4882a593Smuzhiyun }
299*4882a593Smuzhiyun
proc_readfd(struct file * file,struct dir_context * ctx)300*4882a593Smuzhiyun static int proc_readfd(struct file *file, struct dir_context *ctx)
301*4882a593Smuzhiyun {
302*4882a593Smuzhiyun return proc_readfd_common(file, ctx, proc_fd_instantiate);
303*4882a593Smuzhiyun }
304*4882a593Smuzhiyun
305*4882a593Smuzhiyun const struct file_operations proc_fd_operations = {
306*4882a593Smuzhiyun .read = generic_read_dir,
307*4882a593Smuzhiyun .iterate_shared = proc_readfd,
308*4882a593Smuzhiyun .llseek = generic_file_llseek,
309*4882a593Smuzhiyun };
310*4882a593Smuzhiyun
proc_lookupfd(struct inode * dir,struct dentry * dentry,unsigned int flags)311*4882a593Smuzhiyun static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
312*4882a593Smuzhiyun unsigned int flags)
313*4882a593Smuzhiyun {
314*4882a593Smuzhiyun return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
315*4882a593Smuzhiyun }
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun /*
318*4882a593Smuzhiyun * /proc/pid/fd needs a special permission handler so that a process can still
319*4882a593Smuzhiyun * access /proc/self/fd after it has executed a setuid().
320*4882a593Smuzhiyun */
proc_fd_permission(struct inode * inode,int mask)321*4882a593Smuzhiyun int proc_fd_permission(struct inode *inode, int mask)
322*4882a593Smuzhiyun {
323*4882a593Smuzhiyun struct task_struct *p;
324*4882a593Smuzhiyun int rv;
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun rv = generic_permission(inode, mask);
327*4882a593Smuzhiyun if (rv == 0)
328*4882a593Smuzhiyun return rv;
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun rcu_read_lock();
331*4882a593Smuzhiyun p = pid_task(proc_pid(inode), PIDTYPE_PID);
332*4882a593Smuzhiyun if (p && same_thread_group(p, current))
333*4882a593Smuzhiyun rv = 0;
334*4882a593Smuzhiyun rcu_read_unlock();
335*4882a593Smuzhiyun
336*4882a593Smuzhiyun return rv;
337*4882a593Smuzhiyun }
338*4882a593Smuzhiyun
339*4882a593Smuzhiyun const struct inode_operations proc_fd_inode_operations = {
340*4882a593Smuzhiyun .lookup = proc_lookupfd,
341*4882a593Smuzhiyun .permission = proc_fd_permission,
342*4882a593Smuzhiyun .setattr = proc_setattr,
343*4882a593Smuzhiyun };
344*4882a593Smuzhiyun
proc_fdinfo_instantiate(struct dentry * dentry,struct task_struct * task,const void * ptr)345*4882a593Smuzhiyun static struct dentry *proc_fdinfo_instantiate(struct dentry *dentry,
346*4882a593Smuzhiyun struct task_struct *task, const void *ptr)
347*4882a593Smuzhiyun {
348*4882a593Smuzhiyun const struct fd_data *data = ptr;
349*4882a593Smuzhiyun struct proc_inode *ei;
350*4882a593Smuzhiyun struct inode *inode;
351*4882a593Smuzhiyun
352*4882a593Smuzhiyun inode = proc_pid_make_inode(dentry->d_sb, task, S_IFREG | S_IRUGO);
353*4882a593Smuzhiyun if (!inode)
354*4882a593Smuzhiyun return ERR_PTR(-ENOENT);
355*4882a593Smuzhiyun
356*4882a593Smuzhiyun ei = PROC_I(inode);
357*4882a593Smuzhiyun ei->fd = data->fd;
358*4882a593Smuzhiyun
359*4882a593Smuzhiyun inode->i_fop = &proc_fdinfo_file_operations;
360*4882a593Smuzhiyun tid_fd_update_inode(task, inode, 0);
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun d_set_d_op(dentry, &tid_fd_dentry_operations);
363*4882a593Smuzhiyun return d_splice_alias(inode, dentry);
364*4882a593Smuzhiyun }
365*4882a593Smuzhiyun
366*4882a593Smuzhiyun static struct dentry *
proc_lookupfdinfo(struct inode * dir,struct dentry * dentry,unsigned int flags)367*4882a593Smuzhiyun proc_lookupfdinfo(struct inode *dir, struct dentry *dentry, unsigned int flags)
368*4882a593Smuzhiyun {
369*4882a593Smuzhiyun return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
370*4882a593Smuzhiyun }
371*4882a593Smuzhiyun
proc_readfdinfo(struct file * file,struct dir_context * ctx)372*4882a593Smuzhiyun static int proc_readfdinfo(struct file *file, struct dir_context *ctx)
373*4882a593Smuzhiyun {
374*4882a593Smuzhiyun return proc_readfd_common(file, ctx,
375*4882a593Smuzhiyun proc_fdinfo_instantiate);
376*4882a593Smuzhiyun }
377*4882a593Smuzhiyun
proc_open_fdinfo(struct inode * inode,struct file * file)378*4882a593Smuzhiyun static int proc_open_fdinfo(struct inode *inode, struct file *file)
379*4882a593Smuzhiyun {
380*4882a593Smuzhiyun int ret = proc_fdinfo_access_allowed(inode);
381*4882a593Smuzhiyun
382*4882a593Smuzhiyun if (ret)
383*4882a593Smuzhiyun return ret;
384*4882a593Smuzhiyun
385*4882a593Smuzhiyun return 0;
386*4882a593Smuzhiyun }
387*4882a593Smuzhiyun
388*4882a593Smuzhiyun const struct inode_operations proc_fdinfo_inode_operations = {
389*4882a593Smuzhiyun .lookup = proc_lookupfdinfo,
390*4882a593Smuzhiyun .setattr = proc_setattr,
391*4882a593Smuzhiyun };
392*4882a593Smuzhiyun
393*4882a593Smuzhiyun const struct file_operations proc_fdinfo_operations = {
394*4882a593Smuzhiyun .open = proc_open_fdinfo,
395*4882a593Smuzhiyun .read = generic_read_dir,
396*4882a593Smuzhiyun .iterate_shared = proc_readfdinfo,
397*4882a593Smuzhiyun .llseek = generic_file_llseek,
398*4882a593Smuzhiyun };
399