xref: /openbmc/linux/fs/proc/fd.c (revision 2b0143b5c986be1ce8408b3aadc4709e0a94429d)
1faf60af1SCyrill Gorcunov #include <linux/sched.h>
2faf60af1SCyrill Gorcunov #include <linux/errno.h>
3faf60af1SCyrill Gorcunov #include <linux/dcache.h>
4faf60af1SCyrill Gorcunov #include <linux/path.h>
5faf60af1SCyrill Gorcunov #include <linux/fdtable.h>
6faf60af1SCyrill Gorcunov #include <linux/namei.h>
7faf60af1SCyrill Gorcunov #include <linux/pid.h>
8faf60af1SCyrill Gorcunov #include <linux/security.h>
9ddd3e077SCyrill Gorcunov #include <linux/file.h>
10ddd3e077SCyrill Gorcunov #include <linux/seq_file.h>
11faf60af1SCyrill Gorcunov 
12faf60af1SCyrill Gorcunov #include <linux/proc_fs.h>
13faf60af1SCyrill Gorcunov 
1449d063cbSAndrey Vagin #include "../mount.h"
15faf60af1SCyrill Gorcunov #include "internal.h"
16faf60af1SCyrill Gorcunov #include "fd.h"
17faf60af1SCyrill Gorcunov 
18ddd3e077SCyrill Gorcunov static int seq_show(struct seq_file *m, void *v)
19faf60af1SCyrill Gorcunov {
20faf60af1SCyrill Gorcunov 	struct files_struct *files = NULL;
21ddd3e077SCyrill Gorcunov 	int f_flags = 0, ret = -ENOENT;
22ddd3e077SCyrill Gorcunov 	struct file *file = NULL;
23ddd3e077SCyrill Gorcunov 	struct task_struct *task;
24faf60af1SCyrill Gorcunov 
25ddd3e077SCyrill Gorcunov 	task = get_proc_task(m->private);
26ddd3e077SCyrill Gorcunov 	if (!task)
27ddd3e077SCyrill Gorcunov 		return -ENOENT;
28ddd3e077SCyrill Gorcunov 
29faf60af1SCyrill Gorcunov 	files = get_files_struct(task);
30faf60af1SCyrill Gorcunov 	put_task_struct(task);
31ddd3e077SCyrill Gorcunov 
32faf60af1SCyrill Gorcunov 	if (files) {
33ddd3e077SCyrill Gorcunov 		int fd = proc_fd(m->private);
34ddd3e077SCyrill Gorcunov 
35faf60af1SCyrill Gorcunov 		spin_lock(&files->file_lock);
36faf60af1SCyrill Gorcunov 		file = fcheck_files(files, fd);
37faf60af1SCyrill Gorcunov 		if (file) {
38ddd3e077SCyrill Gorcunov 			struct fdtable *fdt = files_fdtable(files);
39faf60af1SCyrill Gorcunov 
40c6f3d811SAl Viro 			f_flags = file->f_flags;
41faf60af1SCyrill Gorcunov 			if (close_on_exec(fd, fdt))
42faf60af1SCyrill Gorcunov 				f_flags |= O_CLOEXEC;
43faf60af1SCyrill Gorcunov 
44ddd3e077SCyrill Gorcunov 			get_file(file);
45ddd3e077SCyrill Gorcunov 			ret = 0;
46faf60af1SCyrill Gorcunov 		}
47faf60af1SCyrill Gorcunov 		spin_unlock(&files->file_lock);
48faf60af1SCyrill Gorcunov 		put_files_struct(files);
49faf60af1SCyrill Gorcunov 	}
50ddd3e077SCyrill Gorcunov 
51ddd3e077SCyrill Gorcunov 	if (!ret) {
5249d063cbSAndrey Vagin 		seq_printf(m, "pos:\t%lli\nflags:\t0%o\nmnt_id:\t%i\n",
5349d063cbSAndrey Vagin 			   (long long)file->f_pos, f_flags,
5449d063cbSAndrey Vagin 			   real_mount(file->f_path.mnt)->mnt_id);
5555985dd7SCyrill Gorcunov 		if (file->f_op->show_fdinfo)
56a3816ab0SJoe Perches 			file->f_op->show_fdinfo(m, file);
57a3816ab0SJoe Perches 		ret = seq_has_overflowed(m);
58ddd3e077SCyrill Gorcunov 		fput(file);
59faf60af1SCyrill Gorcunov 	}
60faf60af1SCyrill Gorcunov 
61ddd3e077SCyrill Gorcunov 	return ret;
62ddd3e077SCyrill Gorcunov }
63ddd3e077SCyrill Gorcunov 
64ddd3e077SCyrill Gorcunov static int seq_fdinfo_open(struct inode *inode, struct file *file)
65ddd3e077SCyrill Gorcunov {
66ddd3e077SCyrill Gorcunov 	return single_open(file, seq_show, inode);
67ddd3e077SCyrill Gorcunov }
68ddd3e077SCyrill Gorcunov 
69ddd3e077SCyrill Gorcunov static const struct file_operations proc_fdinfo_file_operations = {
70ddd3e077SCyrill Gorcunov 	.open		= seq_fdinfo_open,
71ddd3e077SCyrill Gorcunov 	.read		= seq_read,
72ddd3e077SCyrill Gorcunov 	.llseek		= seq_lseek,
73ddd3e077SCyrill Gorcunov 	.release	= single_release,
74ddd3e077SCyrill Gorcunov };
75ddd3e077SCyrill Gorcunov 
76faf60af1SCyrill Gorcunov static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags)
77faf60af1SCyrill Gorcunov {
78faf60af1SCyrill Gorcunov 	struct files_struct *files;
79faf60af1SCyrill Gorcunov 	struct task_struct *task;
80faf60af1SCyrill Gorcunov 	const struct cred *cred;
81faf60af1SCyrill Gorcunov 	struct inode *inode;
82faf60af1SCyrill Gorcunov 	int fd;
83faf60af1SCyrill Gorcunov 
84faf60af1SCyrill Gorcunov 	if (flags & LOOKUP_RCU)
85faf60af1SCyrill Gorcunov 		return -ECHILD;
86faf60af1SCyrill Gorcunov 
87*2b0143b5SDavid Howells 	inode = d_inode(dentry);
88faf60af1SCyrill Gorcunov 	task = get_proc_task(inode);
89faf60af1SCyrill Gorcunov 	fd = proc_fd(inode);
90faf60af1SCyrill Gorcunov 
91faf60af1SCyrill Gorcunov 	if (task) {
92faf60af1SCyrill Gorcunov 		files = get_files_struct(task);
93faf60af1SCyrill Gorcunov 		if (files) {
94faf60af1SCyrill Gorcunov 			struct file *file;
95faf60af1SCyrill Gorcunov 
96faf60af1SCyrill Gorcunov 			rcu_read_lock();
97faf60af1SCyrill Gorcunov 			file = fcheck_files(files, fd);
98faf60af1SCyrill Gorcunov 			if (file) {
99faf60af1SCyrill Gorcunov 				unsigned f_mode = file->f_mode;
100faf60af1SCyrill Gorcunov 
101faf60af1SCyrill Gorcunov 				rcu_read_unlock();
102faf60af1SCyrill Gorcunov 				put_files_struct(files);
103faf60af1SCyrill Gorcunov 
104faf60af1SCyrill Gorcunov 				if (task_dumpable(task)) {
105faf60af1SCyrill Gorcunov 					rcu_read_lock();
106faf60af1SCyrill Gorcunov 					cred = __task_cred(task);
107faf60af1SCyrill Gorcunov 					inode->i_uid = cred->euid;
108faf60af1SCyrill Gorcunov 					inode->i_gid = cred->egid;
109faf60af1SCyrill Gorcunov 					rcu_read_unlock();
110faf60af1SCyrill Gorcunov 				} else {
111faf60af1SCyrill Gorcunov 					inode->i_uid = GLOBAL_ROOT_UID;
112faf60af1SCyrill Gorcunov 					inode->i_gid = GLOBAL_ROOT_GID;
113faf60af1SCyrill Gorcunov 				}
114faf60af1SCyrill Gorcunov 
115faf60af1SCyrill Gorcunov 				if (S_ISLNK(inode->i_mode)) {
116faf60af1SCyrill Gorcunov 					unsigned i_mode = S_IFLNK;
117faf60af1SCyrill Gorcunov 					if (f_mode & FMODE_READ)
118faf60af1SCyrill Gorcunov 						i_mode |= S_IRUSR | S_IXUSR;
119faf60af1SCyrill Gorcunov 					if (f_mode & FMODE_WRITE)
120faf60af1SCyrill Gorcunov 						i_mode |= S_IWUSR | S_IXUSR;
121faf60af1SCyrill Gorcunov 					inode->i_mode = i_mode;
122faf60af1SCyrill Gorcunov 				}
123faf60af1SCyrill Gorcunov 
124faf60af1SCyrill Gorcunov 				security_task_to_inode(task, inode);
125faf60af1SCyrill Gorcunov 				put_task_struct(task);
126faf60af1SCyrill Gorcunov 				return 1;
127faf60af1SCyrill Gorcunov 			}
128faf60af1SCyrill Gorcunov 			rcu_read_unlock();
129faf60af1SCyrill Gorcunov 			put_files_struct(files);
130faf60af1SCyrill Gorcunov 		}
131faf60af1SCyrill Gorcunov 		put_task_struct(task);
132faf60af1SCyrill Gorcunov 	}
133faf60af1SCyrill Gorcunov 	return 0;
134faf60af1SCyrill Gorcunov }
135faf60af1SCyrill Gorcunov 
136faf60af1SCyrill Gorcunov static const struct dentry_operations tid_fd_dentry_operations = {
137faf60af1SCyrill Gorcunov 	.d_revalidate	= tid_fd_revalidate,
138faf60af1SCyrill Gorcunov 	.d_delete	= pid_delete_dentry,
139faf60af1SCyrill Gorcunov };
140faf60af1SCyrill Gorcunov 
141faf60af1SCyrill Gorcunov static int proc_fd_link(struct dentry *dentry, struct path *path)
142faf60af1SCyrill Gorcunov {
143ddd3e077SCyrill Gorcunov 	struct files_struct *files = NULL;
144ddd3e077SCyrill Gorcunov 	struct task_struct *task;
145ddd3e077SCyrill Gorcunov 	int ret = -ENOENT;
146ddd3e077SCyrill Gorcunov 
147*2b0143b5SDavid Howells 	task = get_proc_task(d_inode(dentry));
148ddd3e077SCyrill Gorcunov 	if (task) {
149ddd3e077SCyrill Gorcunov 		files = get_files_struct(task);
150ddd3e077SCyrill Gorcunov 		put_task_struct(task);
151ddd3e077SCyrill Gorcunov 	}
152ddd3e077SCyrill Gorcunov 
153ddd3e077SCyrill Gorcunov 	if (files) {
154*2b0143b5SDavid Howells 		int fd = proc_fd(d_inode(dentry));
155ddd3e077SCyrill Gorcunov 		struct file *fd_file;
156ddd3e077SCyrill Gorcunov 
157ddd3e077SCyrill Gorcunov 		spin_lock(&files->file_lock);
158ddd3e077SCyrill Gorcunov 		fd_file = fcheck_files(files, fd);
159ddd3e077SCyrill Gorcunov 		if (fd_file) {
160ddd3e077SCyrill Gorcunov 			*path = fd_file->f_path;
161ddd3e077SCyrill Gorcunov 			path_get(&fd_file->f_path);
162ddd3e077SCyrill Gorcunov 			ret = 0;
163ddd3e077SCyrill Gorcunov 		}
164ddd3e077SCyrill Gorcunov 		spin_unlock(&files->file_lock);
165ddd3e077SCyrill Gorcunov 		put_files_struct(files);
166ddd3e077SCyrill Gorcunov 	}
167ddd3e077SCyrill Gorcunov 
168ddd3e077SCyrill Gorcunov 	return ret;
169faf60af1SCyrill Gorcunov }
170faf60af1SCyrill Gorcunov 
171c52a47acSAl Viro static int
172faf60af1SCyrill Gorcunov proc_fd_instantiate(struct inode *dir, struct dentry *dentry,
173faf60af1SCyrill Gorcunov 		    struct task_struct *task, const void *ptr)
174faf60af1SCyrill Gorcunov {
175faf60af1SCyrill Gorcunov 	unsigned fd = (unsigned long)ptr;
176faf60af1SCyrill Gorcunov 	struct proc_inode *ei;
177faf60af1SCyrill Gorcunov 	struct inode *inode;
178faf60af1SCyrill Gorcunov 
179faf60af1SCyrill Gorcunov 	inode = proc_pid_make_inode(dir->i_sb, task);
180faf60af1SCyrill Gorcunov 	if (!inode)
181faf60af1SCyrill Gorcunov 		goto out;
182faf60af1SCyrill Gorcunov 
183faf60af1SCyrill Gorcunov 	ei = PROC_I(inode);
184faf60af1SCyrill Gorcunov 	ei->fd = fd;
185faf60af1SCyrill Gorcunov 
186faf60af1SCyrill Gorcunov 	inode->i_mode = S_IFLNK;
187faf60af1SCyrill Gorcunov 	inode->i_op = &proc_pid_link_inode_operations;
188faf60af1SCyrill Gorcunov 	inode->i_size = 64;
189faf60af1SCyrill Gorcunov 
190faf60af1SCyrill Gorcunov 	ei->op.proc_get_link = proc_fd_link;
191faf60af1SCyrill Gorcunov 
192faf60af1SCyrill Gorcunov 	d_set_d_op(dentry, &tid_fd_dentry_operations);
193faf60af1SCyrill Gorcunov 	d_add(dentry, inode);
194faf60af1SCyrill Gorcunov 
195faf60af1SCyrill Gorcunov 	/* Close the race of the process dying before we return the dentry */
196faf60af1SCyrill Gorcunov 	if (tid_fd_revalidate(dentry, 0))
197c52a47acSAl Viro 		return 0;
198faf60af1SCyrill Gorcunov  out:
199c52a47acSAl Viro 	return -ENOENT;
200faf60af1SCyrill Gorcunov }
201faf60af1SCyrill Gorcunov 
202faf60af1SCyrill Gorcunov static struct dentry *proc_lookupfd_common(struct inode *dir,
203faf60af1SCyrill Gorcunov 					   struct dentry *dentry,
204faf60af1SCyrill Gorcunov 					   instantiate_t instantiate)
205faf60af1SCyrill Gorcunov {
206faf60af1SCyrill Gorcunov 	struct task_struct *task = get_proc_task(dir);
207c52a47acSAl Viro 	int result = -ENOENT;
208dbcdb504SAlexey Dobriyan 	unsigned fd = name_to_int(&dentry->d_name);
209faf60af1SCyrill Gorcunov 
210faf60af1SCyrill Gorcunov 	if (!task)
211faf60af1SCyrill Gorcunov 		goto out_no_task;
212faf60af1SCyrill Gorcunov 	if (fd == ~0U)
213faf60af1SCyrill Gorcunov 		goto out;
214faf60af1SCyrill Gorcunov 
215faf60af1SCyrill Gorcunov 	result = instantiate(dir, dentry, task, (void *)(unsigned long)fd);
216faf60af1SCyrill Gorcunov out:
217faf60af1SCyrill Gorcunov 	put_task_struct(task);
218faf60af1SCyrill Gorcunov out_no_task:
219c52a47acSAl Viro 	return ERR_PTR(result);
220faf60af1SCyrill Gorcunov }
221faf60af1SCyrill Gorcunov 
222f0c3b509SAl Viro static int proc_readfd_common(struct file *file, struct dir_context *ctx,
223f0c3b509SAl Viro 			      instantiate_t instantiate)
224faf60af1SCyrill Gorcunov {
225f0c3b509SAl Viro 	struct task_struct *p = get_proc_task(file_inode(file));
226faf60af1SCyrill Gorcunov 	struct files_struct *files;
227f0c3b509SAl Viro 	unsigned int fd;
228faf60af1SCyrill Gorcunov 
229faf60af1SCyrill Gorcunov 	if (!p)
230f0c3b509SAl Viro 		return -ENOENT;
231faf60af1SCyrill Gorcunov 
232f0c3b509SAl Viro 	if (!dir_emit_dots(file, ctx))
233faf60af1SCyrill Gorcunov 		goto out;
234faf60af1SCyrill Gorcunov 	files = get_files_struct(p);
235faf60af1SCyrill Gorcunov 	if (!files)
236faf60af1SCyrill Gorcunov 		goto out;
237f0c3b509SAl Viro 
238faf60af1SCyrill Gorcunov 	rcu_read_lock();
239f0c3b509SAl Viro 	for (fd = ctx->pos - 2;
240faf60af1SCyrill Gorcunov 	     fd < files_fdtable(files)->max_fds;
241f0c3b509SAl Viro 	     fd++, ctx->pos++) {
242faf60af1SCyrill Gorcunov 		char name[PROC_NUMBUF];
243faf60af1SCyrill Gorcunov 		int len;
244faf60af1SCyrill Gorcunov 
245faf60af1SCyrill Gorcunov 		if (!fcheck_files(files, fd))
246faf60af1SCyrill Gorcunov 			continue;
247faf60af1SCyrill Gorcunov 		rcu_read_unlock();
248faf60af1SCyrill Gorcunov 
249faf60af1SCyrill Gorcunov 		len = snprintf(name, sizeof(name), "%d", fd);
250f0c3b509SAl Viro 		if (!proc_fill_cache(file, ctx,
251faf60af1SCyrill Gorcunov 				     name, len, instantiate, p,
252f0c3b509SAl Viro 				     (void *)(unsigned long)fd))
253faf60af1SCyrill Gorcunov 			goto out_fd_loop;
254faf60af1SCyrill Gorcunov 		rcu_read_lock();
255faf60af1SCyrill Gorcunov 	}
256faf60af1SCyrill Gorcunov 	rcu_read_unlock();
257faf60af1SCyrill Gorcunov out_fd_loop:
258faf60af1SCyrill Gorcunov 	put_files_struct(files);
259faf60af1SCyrill Gorcunov out:
260faf60af1SCyrill Gorcunov 	put_task_struct(p);
261f0c3b509SAl Viro 	return 0;
262faf60af1SCyrill Gorcunov }
263faf60af1SCyrill Gorcunov 
264f0c3b509SAl Viro static int proc_readfd(struct file *file, struct dir_context *ctx)
265faf60af1SCyrill Gorcunov {
266f0c3b509SAl Viro 	return proc_readfd_common(file, ctx, proc_fd_instantiate);
267faf60af1SCyrill Gorcunov }
268faf60af1SCyrill Gorcunov 
269faf60af1SCyrill Gorcunov const struct file_operations proc_fd_operations = {
270faf60af1SCyrill Gorcunov 	.read		= generic_read_dir,
271f0c3b509SAl Viro 	.iterate	= proc_readfd,
272faf60af1SCyrill Gorcunov 	.llseek		= default_llseek,
273faf60af1SCyrill Gorcunov };
274faf60af1SCyrill Gorcunov 
275faf60af1SCyrill Gorcunov static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
276faf60af1SCyrill Gorcunov 				    unsigned int flags)
277faf60af1SCyrill Gorcunov {
278faf60af1SCyrill Gorcunov 	return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
279faf60af1SCyrill Gorcunov }
280faf60af1SCyrill Gorcunov 
281faf60af1SCyrill Gorcunov /*
282faf60af1SCyrill Gorcunov  * /proc/pid/fd needs a special permission handler so that a process can still
283faf60af1SCyrill Gorcunov  * access /proc/self/fd after it has executed a setuid().
284faf60af1SCyrill Gorcunov  */
285faf60af1SCyrill Gorcunov int proc_fd_permission(struct inode *inode, int mask)
286faf60af1SCyrill Gorcunov {
287faf60af1SCyrill Gorcunov 	int rv = generic_permission(inode, mask);
288faf60af1SCyrill Gorcunov 	if (rv == 0)
289faf60af1SCyrill Gorcunov 		return 0;
29096d0df79SOleg Nesterov 	if (task_tgid(current) == proc_pid(inode))
291faf60af1SCyrill Gorcunov 		rv = 0;
292faf60af1SCyrill Gorcunov 	return rv;
293faf60af1SCyrill Gorcunov }
294faf60af1SCyrill Gorcunov 
295faf60af1SCyrill Gorcunov const struct inode_operations proc_fd_inode_operations = {
296faf60af1SCyrill Gorcunov 	.lookup		= proc_lookupfd,
297faf60af1SCyrill Gorcunov 	.permission	= proc_fd_permission,
298faf60af1SCyrill Gorcunov 	.setattr	= proc_setattr,
299faf60af1SCyrill Gorcunov };
300faf60af1SCyrill Gorcunov 
301c52a47acSAl Viro static int
302faf60af1SCyrill Gorcunov proc_fdinfo_instantiate(struct inode *dir, struct dentry *dentry,
303faf60af1SCyrill Gorcunov 			struct task_struct *task, const void *ptr)
304faf60af1SCyrill Gorcunov {
305faf60af1SCyrill Gorcunov 	unsigned fd = (unsigned long)ptr;
306faf60af1SCyrill Gorcunov 	struct proc_inode *ei;
307faf60af1SCyrill Gorcunov 	struct inode *inode;
308faf60af1SCyrill Gorcunov 
309faf60af1SCyrill Gorcunov 	inode = proc_pid_make_inode(dir->i_sb, task);
310faf60af1SCyrill Gorcunov 	if (!inode)
311faf60af1SCyrill Gorcunov 		goto out;
312faf60af1SCyrill Gorcunov 
313faf60af1SCyrill Gorcunov 	ei = PROC_I(inode);
314faf60af1SCyrill Gorcunov 	ei->fd = fd;
315faf60af1SCyrill Gorcunov 
316faf60af1SCyrill Gorcunov 	inode->i_mode = S_IFREG | S_IRUSR;
317faf60af1SCyrill Gorcunov 	inode->i_fop = &proc_fdinfo_file_operations;
318faf60af1SCyrill Gorcunov 
319faf60af1SCyrill Gorcunov 	d_set_d_op(dentry, &tid_fd_dentry_operations);
320faf60af1SCyrill Gorcunov 	d_add(dentry, inode);
321faf60af1SCyrill Gorcunov 
322faf60af1SCyrill Gorcunov 	/* Close the race of the process dying before we return the dentry */
323faf60af1SCyrill Gorcunov 	if (tid_fd_revalidate(dentry, 0))
324c52a47acSAl Viro 		return 0;
325faf60af1SCyrill Gorcunov  out:
326c52a47acSAl Viro 	return -ENOENT;
327faf60af1SCyrill Gorcunov }
328faf60af1SCyrill Gorcunov 
329faf60af1SCyrill Gorcunov static struct dentry *
330faf60af1SCyrill Gorcunov proc_lookupfdinfo(struct inode *dir, struct dentry *dentry, unsigned int flags)
331faf60af1SCyrill Gorcunov {
332faf60af1SCyrill Gorcunov 	return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
333faf60af1SCyrill Gorcunov }
334faf60af1SCyrill Gorcunov 
335f0c3b509SAl Viro static int proc_readfdinfo(struct file *file, struct dir_context *ctx)
336faf60af1SCyrill Gorcunov {
337f0c3b509SAl Viro 	return proc_readfd_common(file, ctx,
338faf60af1SCyrill Gorcunov 				  proc_fdinfo_instantiate);
339faf60af1SCyrill Gorcunov }
340faf60af1SCyrill Gorcunov 
341faf60af1SCyrill Gorcunov const struct inode_operations proc_fdinfo_inode_operations = {
342faf60af1SCyrill Gorcunov 	.lookup		= proc_lookupfdinfo,
343faf60af1SCyrill Gorcunov 	.setattr	= proc_setattr,
344faf60af1SCyrill Gorcunov };
345faf60af1SCyrill Gorcunov 
346faf60af1SCyrill Gorcunov const struct file_operations proc_fdinfo_operations = {
347faf60af1SCyrill Gorcunov 	.read		= generic_read_dir,
348f0c3b509SAl Viro 	.iterate	= proc_readfdinfo,
349faf60af1SCyrill Gorcunov 	.llseek		= default_llseek,
350faf60af1SCyrill Gorcunov };
351