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