xref: /openbmc/linux/fs/proc/fd.c (revision abb84c46)
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_fd_rcu(task, fd);
123 	if (file)
124 		*mode = file->f_mode;
125 	rcu_read_unlock();
126 	return !!file;
127 }
128 
129 static void tid_fd_update_inode(struct task_struct *task, struct inode *inode,
130 				fmode_t f_mode)
131 {
132 	task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
133 
134 	if (S_ISLNK(inode->i_mode)) {
135 		unsigned i_mode = S_IFLNK;
136 		if (f_mode & FMODE_READ)
137 			i_mode |= S_IRUSR | S_IXUSR;
138 		if (f_mode & FMODE_WRITE)
139 			i_mode |= S_IWUSR | S_IXUSR;
140 		inode->i_mode = i_mode;
141 	}
142 	security_task_to_inode(task, inode);
143 }
144 
145 static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags)
146 {
147 	struct task_struct *task;
148 	struct inode *inode;
149 	unsigned int fd;
150 
151 	if (flags & LOOKUP_RCU)
152 		return -ECHILD;
153 
154 	inode = d_inode(dentry);
155 	task = get_proc_task(inode);
156 	fd = proc_fd(inode);
157 
158 	if (task) {
159 		fmode_t f_mode;
160 		if (tid_fd_mode(task, fd, &f_mode)) {
161 			tid_fd_update_inode(task, inode, f_mode);
162 			put_task_struct(task);
163 			return 1;
164 		}
165 		put_task_struct(task);
166 	}
167 	return 0;
168 }
169 
170 static const struct dentry_operations tid_fd_dentry_operations = {
171 	.d_revalidate	= tid_fd_revalidate,
172 	.d_delete	= pid_delete_dentry,
173 };
174 
175 static int proc_fd_link(struct dentry *dentry, struct path *path)
176 {
177 	struct task_struct *task;
178 	int ret = -ENOENT;
179 
180 	task = get_proc_task(d_inode(dentry));
181 	if (task) {
182 		unsigned int fd = proc_fd(d_inode(dentry));
183 		struct file *fd_file;
184 
185 		fd_file = fget_task(task, fd);
186 		if (fd_file) {
187 			*path = fd_file->f_path;
188 			path_get(&fd_file->f_path);
189 			ret = 0;
190 			fput(fd_file);
191 		}
192 		put_task_struct(task);
193 	}
194 
195 	return ret;
196 }
197 
198 struct fd_data {
199 	fmode_t mode;
200 	unsigned fd;
201 };
202 
203 static struct dentry *proc_fd_instantiate(struct dentry *dentry,
204 	struct task_struct *task, const void *ptr)
205 {
206 	const struct fd_data *data = ptr;
207 	struct proc_inode *ei;
208 	struct inode *inode;
209 
210 	inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK);
211 	if (!inode)
212 		return ERR_PTR(-ENOENT);
213 
214 	ei = PROC_I(inode);
215 	ei->fd = data->fd;
216 
217 	inode->i_op = &proc_pid_link_inode_operations;
218 	inode->i_size = 64;
219 
220 	ei->op.proc_get_link = proc_fd_link;
221 	tid_fd_update_inode(task, inode, data->mode);
222 
223 	d_set_d_op(dentry, &tid_fd_dentry_operations);
224 	return d_splice_alias(inode, dentry);
225 }
226 
227 static struct dentry *proc_lookupfd_common(struct inode *dir,
228 					   struct dentry *dentry,
229 					   instantiate_t instantiate)
230 {
231 	struct task_struct *task = get_proc_task(dir);
232 	struct fd_data data = {.fd = name_to_int(&dentry->d_name)};
233 	struct dentry *result = ERR_PTR(-ENOENT);
234 
235 	if (!task)
236 		goto out_no_task;
237 	if (data.fd == ~0U)
238 		goto out;
239 	if (!tid_fd_mode(task, data.fd, &data.mode))
240 		goto out;
241 
242 	result = instantiate(dentry, task, &data);
243 out:
244 	put_task_struct(task);
245 out_no_task:
246 	return result;
247 }
248 
249 static int proc_readfd_common(struct file *file, struct dir_context *ctx,
250 			      instantiate_t instantiate)
251 {
252 	struct task_struct *p = get_proc_task(file_inode(file));
253 	unsigned int fd;
254 
255 	if (!p)
256 		return -ENOENT;
257 
258 	if (!dir_emit_dots(file, ctx))
259 		goto out;
260 
261 	rcu_read_lock();
262 	for (fd = ctx->pos - 2;; fd++) {
263 		struct file *f;
264 		struct fd_data data;
265 		char name[10 + 1];
266 		unsigned int len;
267 
268 		f = task_lookup_next_fd_rcu(p, &fd);
269 		ctx->pos = fd + 2LL;
270 		if (!f)
271 			break;
272 		data.mode = f->f_mode;
273 		rcu_read_unlock();
274 		data.fd = fd;
275 
276 		len = snprintf(name, sizeof(name), "%u", fd);
277 		if (!proc_fill_cache(file, ctx,
278 				     name, len, instantiate, p,
279 				     &data))
280 			goto out;
281 		cond_resched();
282 		rcu_read_lock();
283 	}
284 	rcu_read_unlock();
285 out:
286 	put_task_struct(p);
287 	return 0;
288 }
289 
290 static int proc_readfd_count(struct inode *inode, loff_t *count)
291 {
292 	struct task_struct *p = get_proc_task(inode);
293 	struct fdtable *fdt;
294 
295 	if (!p)
296 		return -ENOENT;
297 
298 	task_lock(p);
299 	if (p->files) {
300 		rcu_read_lock();
301 
302 		fdt = files_fdtable(p->files);
303 		*count = bitmap_weight(fdt->open_fds, fdt->max_fds);
304 
305 		rcu_read_unlock();
306 	}
307 	task_unlock(p);
308 
309 	put_task_struct(p);
310 
311 	return 0;
312 }
313 
314 static int proc_readfd(struct file *file, struct dir_context *ctx)
315 {
316 	return proc_readfd_common(file, ctx, proc_fd_instantiate);
317 }
318 
319 const struct file_operations proc_fd_operations = {
320 	.read		= generic_read_dir,
321 	.iterate_shared	= proc_readfd,
322 	.llseek		= generic_file_llseek,
323 };
324 
325 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
326 				    unsigned int flags)
327 {
328 	return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
329 }
330 
331 /*
332  * /proc/pid/fd needs a special permission handler so that a process can still
333  * access /proc/self/fd after it has executed a setuid().
334  */
335 int proc_fd_permission(struct mnt_idmap *idmap,
336 		       struct inode *inode, int mask)
337 {
338 	struct task_struct *p;
339 	int rv;
340 
341 	rv = generic_permission(&nop_mnt_idmap, inode, mask);
342 	if (rv == 0)
343 		return rv;
344 
345 	rcu_read_lock();
346 	p = pid_task(proc_pid(inode), PIDTYPE_PID);
347 	if (p && same_thread_group(p, current))
348 		rv = 0;
349 	rcu_read_unlock();
350 
351 	return rv;
352 }
353 
354 static int proc_fd_getattr(struct mnt_idmap *idmap,
355 			const struct path *path, struct kstat *stat,
356 			u32 request_mask, unsigned int query_flags)
357 {
358 	struct inode *inode = d_inode(path->dentry);
359 	int rv = 0;
360 
361 	generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
362 
363 	/* If it's a directory, put the number of open fds there */
364 	if (S_ISDIR(inode->i_mode)) {
365 		rv = proc_readfd_count(inode, &stat->size);
366 		if (rv < 0)
367 			return rv;
368 	}
369 
370 	return rv;
371 }
372 
373 const struct inode_operations proc_fd_inode_operations = {
374 	.lookup		= proc_lookupfd,
375 	.permission	= proc_fd_permission,
376 	.getattr	= proc_fd_getattr,
377 	.setattr	= proc_setattr,
378 };
379 
380 static struct dentry *proc_fdinfo_instantiate(struct dentry *dentry,
381 	struct task_struct *task, const void *ptr)
382 {
383 	const struct fd_data *data = ptr;
384 	struct proc_inode *ei;
385 	struct inode *inode;
386 
387 	inode = proc_pid_make_inode(dentry->d_sb, task, S_IFREG | S_IRUGO);
388 	if (!inode)
389 		return ERR_PTR(-ENOENT);
390 
391 	ei = PROC_I(inode);
392 	ei->fd = data->fd;
393 
394 	inode->i_op = &proc_fdinfo_file_inode_operations;
395 
396 	inode->i_fop = &proc_fdinfo_file_operations;
397 	tid_fd_update_inode(task, inode, 0);
398 
399 	d_set_d_op(dentry, &tid_fd_dentry_operations);
400 	return d_splice_alias(inode, dentry);
401 }
402 
403 static struct dentry *
404 proc_lookupfdinfo(struct inode *dir, struct dentry *dentry, unsigned int flags)
405 {
406 	return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
407 }
408 
409 static int proc_readfdinfo(struct file *file, struct dir_context *ctx)
410 {
411 	return proc_readfd_common(file, ctx,
412 				  proc_fdinfo_instantiate);
413 }
414 
415 const struct inode_operations proc_fdinfo_inode_operations = {
416 	.lookup		= proc_lookupfdinfo,
417 	.permission	= proc_fdinfo_permission,
418 	.setattr	= proc_setattr,
419 };
420 
421 const struct file_operations proc_fdinfo_operations = {
422 	.read		= generic_read_dir,
423 	.iterate_shared	= proc_readfdinfo,
424 	.llseek		= generic_file_llseek,
425 };
426