xref: /openbmc/linux/fs/proc/fd.c (revision 0d72b92883c651a11059d93335f33d65c6eb653b)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
23f07c014SIngo Molnar #include <linux/sched/signal.h>
3faf60af1SCyrill Gorcunov #include <linux/errno.h>
4faf60af1SCyrill Gorcunov #include <linux/dcache.h>
5faf60af1SCyrill Gorcunov #include <linux/path.h>
6faf60af1SCyrill Gorcunov #include <linux/fdtable.h>
7faf60af1SCyrill Gorcunov #include <linux/namei.h>
8faf60af1SCyrill Gorcunov #include <linux/pid.h>
97bc3fa01SKalesh Singh #include <linux/ptrace.h>
10f1f1f256SIvan Babrou #include <linux/bitmap.h>
11faf60af1SCyrill Gorcunov #include <linux/security.h>
12ddd3e077SCyrill Gorcunov #include <linux/file.h>
13ddd3e077SCyrill Gorcunov #include <linux/seq_file.h>
146c8c9031SAndrey Vagin #include <linux/fs.h>
155970e15dSJeff Layton #include <linux/filelock.h>
16faf60af1SCyrill Gorcunov 
17faf60af1SCyrill Gorcunov #include <linux/proc_fs.h>
18faf60af1SCyrill Gorcunov 
1949d063cbSAndrey Vagin #include "../mount.h"
20faf60af1SCyrill Gorcunov #include "internal.h"
21faf60af1SCyrill Gorcunov #include "fd.h"
22faf60af1SCyrill Gorcunov 
23ddd3e077SCyrill Gorcunov static int seq_show(struct seq_file *m, void *v)
24faf60af1SCyrill Gorcunov {
25faf60af1SCyrill Gorcunov 	struct files_struct *files = NULL;
26ddd3e077SCyrill Gorcunov 	int f_flags = 0, ret = -ENOENT;
27ddd3e077SCyrill Gorcunov 	struct file *file = NULL;
28ddd3e077SCyrill Gorcunov 	struct task_struct *task;
29faf60af1SCyrill Gorcunov 
30ddd3e077SCyrill Gorcunov 	task = get_proc_task(m->private);
31ddd3e077SCyrill Gorcunov 	if (!task)
32ddd3e077SCyrill Gorcunov 		return -ENOENT;
33ddd3e077SCyrill Gorcunov 
34775e0656SEric W. Biederman 	task_lock(task);
35775e0656SEric W. Biederman 	files = task->files;
36faf60af1SCyrill Gorcunov 	if (files) {
37771187d6SAlexey Dobriyan 		unsigned int fd = proc_fd(m->private);
38ddd3e077SCyrill Gorcunov 
39faf60af1SCyrill Gorcunov 		spin_lock(&files->file_lock);
40120ce2b0SEric W. Biederman 		file = files_lookup_fd_locked(files, fd);
41faf60af1SCyrill Gorcunov 		if (file) {
42ddd3e077SCyrill Gorcunov 			struct fdtable *fdt = files_fdtable(files);
43faf60af1SCyrill Gorcunov 
44c6f3d811SAl Viro 			f_flags = file->f_flags;
45faf60af1SCyrill Gorcunov 			if (close_on_exec(fd, fdt))
46faf60af1SCyrill Gorcunov 				f_flags |= O_CLOEXEC;
47faf60af1SCyrill Gorcunov 
48ddd3e077SCyrill Gorcunov 			get_file(file);
49ddd3e077SCyrill Gorcunov 			ret = 0;
50faf60af1SCyrill Gorcunov 		}
51faf60af1SCyrill Gorcunov 		spin_unlock(&files->file_lock);
52faf60af1SCyrill Gorcunov 	}
53775e0656SEric W. Biederman 	task_unlock(task);
54775e0656SEric W. Biederman 	put_task_struct(task);
55ddd3e077SCyrill Gorcunov 
566c8c9031SAndrey Vagin 	if (ret)
576c8c9031SAndrey Vagin 		return ret;
586c8c9031SAndrey Vagin 
593845f256SKalesh Singh 	seq_printf(m, "pos:\t%lli\nflags:\t0%o\nmnt_id:\t%i\nino:\t%lu\n",
6049d063cbSAndrey Vagin 		   (long long)file->f_pos, f_flags,
613845f256SKalesh Singh 		   real_mount(file->f_path.mnt)->mnt_id,
623845f256SKalesh Singh 		   file_inode(file)->i_ino);
636c8c9031SAndrey Vagin 
64775e0656SEric W. Biederman 	/* show_fd_locks() never deferences files so a stale value is safe */
656c8c9031SAndrey Vagin 	show_fd_locks(m, file, files);
666c8c9031SAndrey Vagin 	if (seq_has_overflowed(m))
676c8c9031SAndrey Vagin 		goto out;
686c8c9031SAndrey Vagin 
6955985dd7SCyrill Gorcunov 	if (file->f_op->show_fdinfo)
70a3816ab0SJoe Perches 		file->f_op->show_fdinfo(m, file);
71faf60af1SCyrill Gorcunov 
726c8c9031SAndrey Vagin out:
736c8c9031SAndrey Vagin 	fput(file);
746c8c9031SAndrey Vagin 	return 0;
75ddd3e077SCyrill Gorcunov }
76ddd3e077SCyrill Gorcunov 
771927e498SKalesh Singh static int proc_fdinfo_access_allowed(struct inode *inode)
78ddd3e077SCyrill Gorcunov {
797bc3fa01SKalesh Singh 	bool allowed = false;
807bc3fa01SKalesh Singh 	struct task_struct *task = get_proc_task(inode);
817bc3fa01SKalesh Singh 
827bc3fa01SKalesh Singh 	if (!task)
837bc3fa01SKalesh Singh 		return -ESRCH;
847bc3fa01SKalesh Singh 
857bc3fa01SKalesh Singh 	allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
867bc3fa01SKalesh Singh 	put_task_struct(task);
877bc3fa01SKalesh Singh 
887bc3fa01SKalesh Singh 	if (!allowed)
897bc3fa01SKalesh Singh 		return -EACCES;
907bc3fa01SKalesh Singh 
911927e498SKalesh Singh 	return 0;
921927e498SKalesh Singh }
931927e498SKalesh Singh 
941927e498SKalesh Singh static int seq_fdinfo_open(struct inode *inode, struct file *file)
951927e498SKalesh Singh {
961927e498SKalesh Singh 	int ret = proc_fdinfo_access_allowed(inode);
971927e498SKalesh Singh 
981927e498SKalesh Singh 	if (ret)
991927e498SKalesh Singh 		return ret;
1001927e498SKalesh Singh 
101ddd3e077SCyrill Gorcunov 	return single_open(file, seq_show, inode);
102ddd3e077SCyrill Gorcunov }
103ddd3e077SCyrill Gorcunov 
104ddd3e077SCyrill Gorcunov static const struct file_operations proc_fdinfo_file_operations = {
105ddd3e077SCyrill Gorcunov 	.open		= seq_fdinfo_open,
106ddd3e077SCyrill Gorcunov 	.read		= seq_read,
107ddd3e077SCyrill Gorcunov 	.llseek		= seq_lseek,
108ddd3e077SCyrill Gorcunov 	.release	= single_release,
109ddd3e077SCyrill Gorcunov };
110ddd3e077SCyrill Gorcunov 
1111ae9bd8bSAl Viro static bool tid_fd_mode(struct task_struct *task, unsigned fd, fmode_t *mode)
1121ae9bd8bSAl Viro {
1131ae9bd8bSAl Viro 	struct file *file;
1141ae9bd8bSAl Viro 
1151ae9bd8bSAl Viro 	rcu_read_lock();
11664eb661fSEric W. Biederman 	file = task_lookup_fd_rcu(task, fd);
1171ae9bd8bSAl Viro 	if (file)
1181ae9bd8bSAl Viro 		*mode = file->f_mode;
1191ae9bd8bSAl Viro 	rcu_read_unlock();
1201ae9bd8bSAl Viro 	return !!file;
1211ae9bd8bSAl Viro }
1221ae9bd8bSAl Viro 
12398836386SAl Viro static void tid_fd_update_inode(struct task_struct *task, struct inode *inode,
12498836386SAl Viro 				fmode_t f_mode)
125faf60af1SCyrill Gorcunov {
12668eb94f1SEric W. Biederman 	task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
127faf60af1SCyrill Gorcunov 
128faf60af1SCyrill Gorcunov 	if (S_ISLNK(inode->i_mode)) {
129faf60af1SCyrill Gorcunov 		unsigned i_mode = S_IFLNK;
130faf60af1SCyrill Gorcunov 		if (f_mode & FMODE_READ)
131faf60af1SCyrill Gorcunov 			i_mode |= S_IRUSR | S_IXUSR;
132faf60af1SCyrill Gorcunov 		if (f_mode & FMODE_WRITE)
133faf60af1SCyrill Gorcunov 			i_mode |= S_IWUSR | S_IXUSR;
134faf60af1SCyrill Gorcunov 		inode->i_mode = i_mode;
135faf60af1SCyrill Gorcunov 	}
136faf60af1SCyrill Gorcunov 	security_task_to_inode(task, inode);
13798836386SAl Viro }
13898836386SAl Viro 
13998836386SAl Viro static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags)
14098836386SAl Viro {
14198836386SAl Viro 	struct task_struct *task;
14298836386SAl Viro 	struct inode *inode;
14398836386SAl Viro 	unsigned int fd;
14498836386SAl Viro 
14598836386SAl Viro 	if (flags & LOOKUP_RCU)
14698836386SAl Viro 		return -ECHILD;
14798836386SAl Viro 
14898836386SAl Viro 	inode = d_inode(dentry);
14998836386SAl Viro 	task = get_proc_task(inode);
15098836386SAl Viro 	fd = proc_fd(inode);
15198836386SAl Viro 
15298836386SAl Viro 	if (task) {
15398836386SAl Viro 		fmode_t f_mode;
15498836386SAl Viro 		if (tid_fd_mode(task, fd, &f_mode)) {
15598836386SAl Viro 			tid_fd_update_inode(task, inode, f_mode);
156faf60af1SCyrill Gorcunov 			put_task_struct(task);
157faf60af1SCyrill Gorcunov 			return 1;
158faf60af1SCyrill Gorcunov 		}
159faf60af1SCyrill Gorcunov 		put_task_struct(task);
160faf60af1SCyrill Gorcunov 	}
161faf60af1SCyrill Gorcunov 	return 0;
162faf60af1SCyrill Gorcunov }
163faf60af1SCyrill Gorcunov 
164faf60af1SCyrill Gorcunov static const struct dentry_operations tid_fd_dentry_operations = {
165faf60af1SCyrill Gorcunov 	.d_revalidate	= tid_fd_revalidate,
166faf60af1SCyrill Gorcunov 	.d_delete	= pid_delete_dentry,
167faf60af1SCyrill Gorcunov };
168faf60af1SCyrill Gorcunov 
169faf60af1SCyrill Gorcunov static int proc_fd_link(struct dentry *dentry, struct path *path)
170faf60af1SCyrill Gorcunov {
171ddd3e077SCyrill Gorcunov 	struct task_struct *task;
172ddd3e077SCyrill Gorcunov 	int ret = -ENOENT;
173ddd3e077SCyrill Gorcunov 
1742b0143b5SDavid Howells 	task = get_proc_task(d_inode(dentry));
175ddd3e077SCyrill Gorcunov 	if (task) {
176771187d6SAlexey Dobriyan 		unsigned int fd = proc_fd(d_inode(dentry));
177ddd3e077SCyrill Gorcunov 		struct file *fd_file;
178ddd3e077SCyrill Gorcunov 
179439be326SEric W. Biederman 		fd_file = fget_task(task, fd);
180ddd3e077SCyrill Gorcunov 		if (fd_file) {
181ddd3e077SCyrill Gorcunov 			*path = fd_file->f_path;
182ddd3e077SCyrill Gorcunov 			path_get(&fd_file->f_path);
183ddd3e077SCyrill Gorcunov 			ret = 0;
184439be326SEric W. Biederman 			fput(fd_file);
185ddd3e077SCyrill Gorcunov 		}
186439be326SEric W. Biederman 		put_task_struct(task);
187ddd3e077SCyrill Gorcunov 	}
188ddd3e077SCyrill Gorcunov 
189ddd3e077SCyrill Gorcunov 	return ret;
190faf60af1SCyrill Gorcunov }
191faf60af1SCyrill Gorcunov 
19298836386SAl Viro struct fd_data {
19398836386SAl Viro 	fmode_t mode;
19498836386SAl Viro 	unsigned fd;
19598836386SAl Viro };
19698836386SAl Viro 
1970168b9e3SAl Viro static struct dentry *proc_fd_instantiate(struct dentry *dentry,
198faf60af1SCyrill Gorcunov 	struct task_struct *task, const void *ptr)
199faf60af1SCyrill Gorcunov {
20098836386SAl Viro 	const struct fd_data *data = ptr;
201faf60af1SCyrill Gorcunov 	struct proc_inode *ei;
202faf60af1SCyrill Gorcunov 	struct inode *inode;
203faf60af1SCyrill Gorcunov 
2040168b9e3SAl Viro 	inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK);
205faf60af1SCyrill Gorcunov 	if (!inode)
2060168b9e3SAl Viro 		return ERR_PTR(-ENOENT);
207faf60af1SCyrill Gorcunov 
208faf60af1SCyrill Gorcunov 	ei = PROC_I(inode);
20998836386SAl Viro 	ei->fd = data->fd;
210faf60af1SCyrill Gorcunov 
211faf60af1SCyrill Gorcunov 	inode->i_op = &proc_pid_link_inode_operations;
212faf60af1SCyrill Gorcunov 	inode->i_size = 64;
213faf60af1SCyrill Gorcunov 
214faf60af1SCyrill Gorcunov 	ei->op.proc_get_link = proc_fd_link;
21598836386SAl Viro 	tid_fd_update_inode(task, inode, data->mode);
216faf60af1SCyrill Gorcunov 
217faf60af1SCyrill Gorcunov 	d_set_d_op(dentry, &tid_fd_dentry_operations);
2180168b9e3SAl Viro 	return d_splice_alias(inode, dentry);
219faf60af1SCyrill Gorcunov }
220faf60af1SCyrill Gorcunov 
221faf60af1SCyrill Gorcunov static struct dentry *proc_lookupfd_common(struct inode *dir,
222faf60af1SCyrill Gorcunov 					   struct dentry *dentry,
223faf60af1SCyrill Gorcunov 					   instantiate_t instantiate)
224faf60af1SCyrill Gorcunov {
225faf60af1SCyrill Gorcunov 	struct task_struct *task = get_proc_task(dir);
22698836386SAl Viro 	struct fd_data data = {.fd = name_to_int(&dentry->d_name)};
2270168b9e3SAl Viro 	struct dentry *result = ERR_PTR(-ENOENT);
228faf60af1SCyrill Gorcunov 
229faf60af1SCyrill Gorcunov 	if (!task)
230faf60af1SCyrill Gorcunov 		goto out_no_task;
23198836386SAl Viro 	if (data.fd == ~0U)
232faf60af1SCyrill Gorcunov 		goto out;
23398836386SAl Viro 	if (!tid_fd_mode(task, data.fd, &data.mode))
2341ae9bd8bSAl Viro 		goto out;
235faf60af1SCyrill Gorcunov 
2360168b9e3SAl Viro 	result = instantiate(dentry, task, &data);
237faf60af1SCyrill Gorcunov out:
238faf60af1SCyrill Gorcunov 	put_task_struct(task);
239faf60af1SCyrill Gorcunov out_no_task:
2400168b9e3SAl Viro 	return result;
241faf60af1SCyrill Gorcunov }
242faf60af1SCyrill Gorcunov 
243f0c3b509SAl Viro static int proc_readfd_common(struct file *file, struct dir_context *ctx,
244f0c3b509SAl Viro 			      instantiate_t instantiate)
245faf60af1SCyrill Gorcunov {
246f0c3b509SAl Viro 	struct task_struct *p = get_proc_task(file_inode(file));
247f0c3b509SAl Viro 	unsigned int fd;
248faf60af1SCyrill Gorcunov 
249faf60af1SCyrill Gorcunov 	if (!p)
250f0c3b509SAl Viro 		return -ENOENT;
251faf60af1SCyrill Gorcunov 
252f0c3b509SAl Viro 	if (!dir_emit_dots(file, ctx))
253faf60af1SCyrill Gorcunov 		goto out;
254f0c3b509SAl Viro 
255faf60af1SCyrill Gorcunov 	rcu_read_lock();
2565b17b618SEric W. Biederman 	for (fd = ctx->pos - 2;; fd++) {
25798836386SAl Viro 		struct file *f;
25898836386SAl Viro 		struct fd_data data;
259e3912ac3SAlexey Dobriyan 		char name[10 + 1];
260a4ef3895SAlexey Dobriyan 		unsigned int len;
261faf60af1SCyrill Gorcunov 
2625b17b618SEric W. Biederman 		f = task_lookup_next_fd_rcu(p, &fd);
2635b17b618SEric W. Biederman 		ctx->pos = fd + 2LL;
26498836386SAl Viro 		if (!f)
2655b17b618SEric W. Biederman 			break;
26698836386SAl Viro 		data.mode = f->f_mode;
267faf60af1SCyrill Gorcunov 		rcu_read_unlock();
26898836386SAl Viro 		data.fd = fd;
269faf60af1SCyrill Gorcunov 
270771187d6SAlexey Dobriyan 		len = snprintf(name, sizeof(name), "%u", fd);
271f0c3b509SAl Viro 		if (!proc_fill_cache(file, ctx,
272faf60af1SCyrill Gorcunov 				     name, len, instantiate, p,
27398836386SAl Viro 				     &data))
2745b17b618SEric W. Biederman 			goto out;
2753cc4a84eSEric Dumazet 		cond_resched();
276faf60af1SCyrill Gorcunov 		rcu_read_lock();
277faf60af1SCyrill Gorcunov 	}
278faf60af1SCyrill Gorcunov 	rcu_read_unlock();
279faf60af1SCyrill Gorcunov out:
280faf60af1SCyrill Gorcunov 	put_task_struct(p);
281f0c3b509SAl Viro 	return 0;
282faf60af1SCyrill Gorcunov }
283faf60af1SCyrill Gorcunov 
284f1f1f256SIvan Babrou static int proc_readfd_count(struct inode *inode, loff_t *count)
285f1f1f256SIvan Babrou {
286f1f1f256SIvan Babrou 	struct task_struct *p = get_proc_task(inode);
287f1f1f256SIvan Babrou 	struct fdtable *fdt;
288f1f1f256SIvan Babrou 
289f1f1f256SIvan Babrou 	if (!p)
290f1f1f256SIvan Babrou 		return -ENOENT;
291f1f1f256SIvan Babrou 
292f1f1f256SIvan Babrou 	task_lock(p);
293f1f1f256SIvan Babrou 	if (p->files) {
294f1f1f256SIvan Babrou 		rcu_read_lock();
295f1f1f256SIvan Babrou 
296f1f1f256SIvan Babrou 		fdt = files_fdtable(p->files);
297f1f1f256SIvan Babrou 		*count = bitmap_weight(fdt->open_fds, fdt->max_fds);
298f1f1f256SIvan Babrou 
299f1f1f256SIvan Babrou 		rcu_read_unlock();
300f1f1f256SIvan Babrou 	}
301f1f1f256SIvan Babrou 	task_unlock(p);
302f1f1f256SIvan Babrou 
303f1f1f256SIvan Babrou 	put_task_struct(p);
304f1f1f256SIvan Babrou 
305f1f1f256SIvan Babrou 	return 0;
306f1f1f256SIvan Babrou }
307f1f1f256SIvan Babrou 
308f0c3b509SAl Viro static int proc_readfd(struct file *file, struct dir_context *ctx)
309faf60af1SCyrill Gorcunov {
310f0c3b509SAl Viro 	return proc_readfd_common(file, ctx, proc_fd_instantiate);
311faf60af1SCyrill Gorcunov }
312faf60af1SCyrill Gorcunov 
313faf60af1SCyrill Gorcunov const struct file_operations proc_fd_operations = {
314faf60af1SCyrill Gorcunov 	.read		= generic_read_dir,
315f50752eaSAl Viro 	.iterate_shared	= proc_readfd,
316f50752eaSAl Viro 	.llseek		= generic_file_llseek,
317faf60af1SCyrill Gorcunov };
318faf60af1SCyrill Gorcunov 
319faf60af1SCyrill Gorcunov static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
320faf60af1SCyrill Gorcunov 				    unsigned int flags)
321faf60af1SCyrill Gorcunov {
322faf60af1SCyrill Gorcunov 	return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
323faf60af1SCyrill Gorcunov }
324faf60af1SCyrill Gorcunov 
325faf60af1SCyrill Gorcunov /*
326faf60af1SCyrill Gorcunov  * /proc/pid/fd needs a special permission handler so that a process can still
327faf60af1SCyrill Gorcunov  * access /proc/self/fd after it has executed a setuid().
328faf60af1SCyrill Gorcunov  */
3294609e1f1SChristian Brauner int proc_fd_permission(struct mnt_idmap *idmap,
330549c7297SChristian Brauner 		       struct inode *inode, int mask)
331faf60af1SCyrill Gorcunov {
33254708d28SOleg Nesterov 	struct task_struct *p;
33354708d28SOleg Nesterov 	int rv;
33454708d28SOleg Nesterov 
3354609e1f1SChristian Brauner 	rv = generic_permission(&nop_mnt_idmap, inode, mask);
336faf60af1SCyrill Gorcunov 	if (rv == 0)
33754708d28SOleg Nesterov 		return rv;
33854708d28SOleg Nesterov 
33954708d28SOleg Nesterov 	rcu_read_lock();
34054708d28SOleg Nesterov 	p = pid_task(proc_pid(inode), PIDTYPE_PID);
34154708d28SOleg Nesterov 	if (p && same_thread_group(p, current))
342faf60af1SCyrill Gorcunov 		rv = 0;
34354708d28SOleg Nesterov 	rcu_read_unlock();
34454708d28SOleg Nesterov 
345faf60af1SCyrill Gorcunov 	return rv;
346faf60af1SCyrill Gorcunov }
347faf60af1SCyrill Gorcunov 
348b74d24f7SChristian Brauner static int proc_fd_getattr(struct mnt_idmap *idmap,
349f1f1f256SIvan Babrou 			const struct path *path, struct kstat *stat,
350f1f1f256SIvan Babrou 			u32 request_mask, unsigned int query_flags)
351f1f1f256SIvan Babrou {
352f1f1f256SIvan Babrou 	struct inode *inode = d_inode(path->dentry);
353f1f1f256SIvan Babrou 	int rv = 0;
354f1f1f256SIvan Babrou 
355*0d72b928SJeff Layton 	generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
356f1f1f256SIvan Babrou 
357f1f1f256SIvan Babrou 	/* If it's a directory, put the number of open fds there */
358f1f1f256SIvan Babrou 	if (S_ISDIR(inode->i_mode)) {
359f1f1f256SIvan Babrou 		rv = proc_readfd_count(inode, &stat->size);
360f1f1f256SIvan Babrou 		if (rv < 0)
361f1f1f256SIvan Babrou 			return rv;
362f1f1f256SIvan Babrou 	}
363f1f1f256SIvan Babrou 
364f1f1f256SIvan Babrou 	return rv;
365f1f1f256SIvan Babrou }
366f1f1f256SIvan Babrou 
367faf60af1SCyrill Gorcunov const struct inode_operations proc_fd_inode_operations = {
368faf60af1SCyrill Gorcunov 	.lookup		= proc_lookupfd,
369faf60af1SCyrill Gorcunov 	.permission	= proc_fd_permission,
370f1f1f256SIvan Babrou 	.getattr	= proc_fd_getattr,
371faf60af1SCyrill Gorcunov 	.setattr	= proc_setattr,
372faf60af1SCyrill Gorcunov };
373faf60af1SCyrill Gorcunov 
3740168b9e3SAl Viro static struct dentry *proc_fdinfo_instantiate(struct dentry *dentry,
375faf60af1SCyrill Gorcunov 	struct task_struct *task, const void *ptr)
376faf60af1SCyrill Gorcunov {
37798836386SAl Viro 	const struct fd_data *data = ptr;
378faf60af1SCyrill Gorcunov 	struct proc_inode *ei;
379faf60af1SCyrill Gorcunov 	struct inode *inode;
380faf60af1SCyrill Gorcunov 
3817bc3fa01SKalesh Singh 	inode = proc_pid_make_inode(dentry->d_sb, task, S_IFREG | S_IRUGO);
382faf60af1SCyrill Gorcunov 	if (!inode)
3830168b9e3SAl Viro 		return ERR_PTR(-ENOENT);
384faf60af1SCyrill Gorcunov 
385faf60af1SCyrill Gorcunov 	ei = PROC_I(inode);
38698836386SAl Viro 	ei->fd = data->fd;
387faf60af1SCyrill Gorcunov 
388faf60af1SCyrill Gorcunov 	inode->i_fop = &proc_fdinfo_file_operations;
38998836386SAl Viro 	tid_fd_update_inode(task, inode, 0);
390faf60af1SCyrill Gorcunov 
391faf60af1SCyrill Gorcunov 	d_set_d_op(dentry, &tid_fd_dentry_operations);
3920168b9e3SAl Viro 	return d_splice_alias(inode, dentry);
393faf60af1SCyrill Gorcunov }
394faf60af1SCyrill Gorcunov 
395faf60af1SCyrill Gorcunov static struct dentry *
396faf60af1SCyrill Gorcunov proc_lookupfdinfo(struct inode *dir, struct dentry *dentry, unsigned int flags)
397faf60af1SCyrill Gorcunov {
398faf60af1SCyrill Gorcunov 	return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
399faf60af1SCyrill Gorcunov }
400faf60af1SCyrill Gorcunov 
401f0c3b509SAl Viro static int proc_readfdinfo(struct file *file, struct dir_context *ctx)
402faf60af1SCyrill Gorcunov {
403f0c3b509SAl Viro 	return proc_readfd_common(file, ctx,
404faf60af1SCyrill Gorcunov 				  proc_fdinfo_instantiate);
405faf60af1SCyrill Gorcunov }
406faf60af1SCyrill Gorcunov 
4071927e498SKalesh Singh static int proc_open_fdinfo(struct inode *inode, struct file *file)
4081927e498SKalesh Singh {
4091927e498SKalesh Singh 	int ret = proc_fdinfo_access_allowed(inode);
4101927e498SKalesh Singh 
4111927e498SKalesh Singh 	if (ret)
4121927e498SKalesh Singh 		return ret;
4131927e498SKalesh Singh 
4141927e498SKalesh Singh 	return 0;
4151927e498SKalesh Singh }
4161927e498SKalesh Singh 
417faf60af1SCyrill Gorcunov const struct inode_operations proc_fdinfo_inode_operations = {
418faf60af1SCyrill Gorcunov 	.lookup		= proc_lookupfdinfo,
419faf60af1SCyrill Gorcunov 	.setattr	= proc_setattr,
420faf60af1SCyrill Gorcunov };
421faf60af1SCyrill Gorcunov 
422faf60af1SCyrill Gorcunov const struct file_operations proc_fdinfo_operations = {
4231927e498SKalesh Singh 	.open		= proc_open_fdinfo,
424faf60af1SCyrill Gorcunov 	.read		= generic_read_dir,
425f50752eaSAl Viro 	.iterate_shared	= proc_readfdinfo,
426f50752eaSAl Viro 	.llseek		= generic_file_llseek,
427faf60af1SCyrill Gorcunov };
428