blob: 6b80cd1e419a6638f95c497f07dbc6bc877953f5 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Ingo Molnar3f07c012017-02-08 18:51:30 +01002#include <linux/sched/signal.h>
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04003#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/security.h>
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +040010#include <linux/file.h>
11#include <linux/seq_file.h>
Andrey Vagin6c8c9032015-04-16 12:49:38 -070012#include <linux/fs.h>
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040013
14#include <linux/proc_fs.h>
15
Andrey Vagin49d063c2014-04-07 15:38:34 -070016#include "../mount.h"
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040017#include "internal.h"
18#include "fd.h"
19
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +040020static int seq_show(struct seq_file *m, void *v)
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040021{
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040022 struct files_struct *files = NULL;
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +040023 int f_flags = 0, ret = -ENOENT;
24 struct file *file = NULL;
25 struct task_struct *task;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040026
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +040027 task = get_proc_task(m->private);
28 if (!task)
29 return -ENOENT;
30
31 files = get_files_struct(task);
32 put_task_struct(task);
33
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040034 if (files) {
Alexey Dobriyan771187d2016-09-02 00:42:02 +030035 unsigned int fd = proc_fd(m->private);
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +040036
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040037 spin_lock(&files->file_lock);
38 file = fcheck_files(files, fd);
39 if (file) {
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +040040 struct fdtable *fdt = files_fdtable(files);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040041
Al Viroc6f3d812012-08-26 11:01:04 -040042 f_flags = file->f_flags;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040043 if (close_on_exec(fd, fdt))
44 f_flags |= O_CLOEXEC;
45
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +040046 get_file(file);
47 ret = 0;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040048 }
49 spin_unlock(&files->file_lock);
50 put_files_struct(files);
51 }
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +040052
Andrey Vagin6c8c9032015-04-16 12:49:38 -070053 if (ret)
54 return ret;
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +040055
Andrey Vagin6c8c9032015-04-16 12:49:38 -070056 seq_printf(m, "pos:\t%lli\nflags:\t0%o\nmnt_id:\t%i\n",
57 (long long)file->f_pos, f_flags,
58 real_mount(file->f_path.mnt)->mnt_id);
59
60 show_fd_locks(m, file, files);
61 if (seq_has_overflowed(m))
62 goto out;
63
64 if (file->f_op->show_fdinfo)
65 file->f_op->show_fdinfo(m, file);
66
67out:
68 fput(file);
69 return 0;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040070}
71
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +040072static int seq_fdinfo_open(struct inode *inode, struct file *file)
73{
74 return single_open(file, seq_show, inode);
75}
76
77static const struct file_operations proc_fdinfo_file_operations = {
78 .open = seq_fdinfo_open,
79 .read = seq_read,
80 .llseek = seq_lseek,
81 .release = single_release,
82};
83
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040084static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags)
85{
86 struct files_struct *files;
87 struct task_struct *task;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040088 struct inode *inode;
Alexey Dobriyan771187d2016-09-02 00:42:02 +030089 unsigned int fd;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040090
91 if (flags & LOOKUP_RCU)
92 return -ECHILD;
93
David Howells2b0143b2015-03-17 22:25:59 +000094 inode = d_inode(dentry);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040095 task = get_proc_task(inode);
96 fd = proc_fd(inode);
97
98 if (task) {
99 files = get_files_struct(task);
100 if (files) {
101 struct file *file;
102
103 rcu_read_lock();
104 file = fcheck_files(files, fd);
105 if (file) {
106 unsigned f_mode = file->f_mode;
107
108 rcu_read_unlock();
109 put_files_struct(files);
110
Eric W. Biederman68eb94f2017-01-03 10:23:11 +1300111 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400112
113 if (S_ISLNK(inode->i_mode)) {
114 unsigned i_mode = S_IFLNK;
115 if (f_mode & FMODE_READ)
116 i_mode |= S_IRUSR | S_IXUSR;
117 if (f_mode & FMODE_WRITE)
118 i_mode |= S_IWUSR | S_IXUSR;
119 inode->i_mode = i_mode;
120 }
121
122 security_task_to_inode(task, inode);
123 put_task_struct(task);
124 return 1;
125 }
126 rcu_read_unlock();
127 put_files_struct(files);
128 }
129 put_task_struct(task);
130 }
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400131 return 0;
132}
133
134static const struct dentry_operations tid_fd_dentry_operations = {
135 .d_revalidate = tid_fd_revalidate,
136 .d_delete = pid_delete_dentry,
137};
138
139static int proc_fd_link(struct dentry *dentry, struct path *path)
140{
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +0400141 struct files_struct *files = NULL;
142 struct task_struct *task;
143 int ret = -ENOENT;
144
David Howells2b0143b2015-03-17 22:25:59 +0000145 task = get_proc_task(d_inode(dentry));
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +0400146 if (task) {
147 files = get_files_struct(task);
148 put_task_struct(task);
149 }
150
151 if (files) {
Alexey Dobriyan771187d2016-09-02 00:42:02 +0300152 unsigned int fd = proc_fd(d_inode(dentry));
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +0400153 struct file *fd_file;
154
155 spin_lock(&files->file_lock);
156 fd_file = fcheck_files(files, fd);
157 if (fd_file) {
158 *path = fd_file->f_path;
159 path_get(&fd_file->f_path);
160 ret = 0;
161 }
162 spin_unlock(&files->file_lock);
163 put_files_struct(files);
164 }
165
166 return ret;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400167}
168
Al Viroc52a47a2013-06-15 11:15:20 +0400169static int
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400170proc_fd_instantiate(struct inode *dir, struct dentry *dentry,
171 struct task_struct *task, const void *ptr)
172{
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400173 unsigned fd = (unsigned long)ptr;
174 struct proc_inode *ei;
175 struct inode *inode;
176
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +0100177 inode = proc_pid_make_inode(dir->i_sb, task, S_IFLNK);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400178 if (!inode)
179 goto out;
180
181 ei = PROC_I(inode);
182 ei->fd = fd;
183
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400184 inode->i_op = &proc_pid_link_inode_operations;
185 inode->i_size = 64;
186
187 ei->op.proc_get_link = proc_fd_link;
188
189 d_set_d_op(dentry, &tid_fd_dentry_operations);
190 d_add(dentry, inode);
191
192 /* Close the race of the process dying before we return the dentry */
193 if (tid_fd_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +0400194 return 0;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400195 out:
Al Viroc52a47a2013-06-15 11:15:20 +0400196 return -ENOENT;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400197}
198
199static struct dentry *proc_lookupfd_common(struct inode *dir,
200 struct dentry *dentry,
201 instantiate_t instantiate)
202{
203 struct task_struct *task = get_proc_task(dir);
Al Viroc52a47a2013-06-15 11:15:20 +0400204 int result = -ENOENT;
Alexey Dobriyandbcdb502014-08-08 14:21:25 -0700205 unsigned fd = name_to_int(&dentry->d_name);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400206
207 if (!task)
208 goto out_no_task;
209 if (fd == ~0U)
210 goto out;
211
212 result = instantiate(dir, dentry, task, (void *)(unsigned long)fd);
213out:
214 put_task_struct(task);
215out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +0400216 return ERR_PTR(result);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400217}
218
Al Virof0c3b502013-05-16 12:07:31 -0400219static int proc_readfd_common(struct file *file, struct dir_context *ctx,
220 instantiate_t instantiate)
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400221{
Al Virof0c3b502013-05-16 12:07:31 -0400222 struct task_struct *p = get_proc_task(file_inode(file));
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400223 struct files_struct *files;
Al Virof0c3b502013-05-16 12:07:31 -0400224 unsigned int fd;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400225
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400226 if (!p)
Al Virof0c3b502013-05-16 12:07:31 -0400227 return -ENOENT;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400228
Al Virof0c3b502013-05-16 12:07:31 -0400229 if (!dir_emit_dots(file, ctx))
230 goto out;
Al Virof0c3b502013-05-16 12:07:31 -0400231 files = get_files_struct(p);
232 if (!files)
233 goto out;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400234
Al Virof0c3b502013-05-16 12:07:31 -0400235 rcu_read_lock();
236 for (fd = ctx->pos - 2;
237 fd < files_fdtable(files)->max_fds;
238 fd++, ctx->pos++) {
Alexey Dobriyane3912ac2018-02-06 15:36:51 -0800239 char name[10 + 1];
Al Virof0c3b502013-05-16 12:07:31 -0400240 int len;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400241
Al Virof0c3b502013-05-16 12:07:31 -0400242 if (!fcheck_files(files, fd))
243 continue;
244 rcu_read_unlock();
245
Alexey Dobriyan771187d2016-09-02 00:42:02 +0300246 len = snprintf(name, sizeof(name), "%u", fd);
Al Virof0c3b502013-05-16 12:07:31 -0400247 if (!proc_fill_cache(file, ctx,
248 name, len, instantiate, p,
249 (void *)(unsigned long)fd))
250 goto out_fd_loop;
Eric Dumazet3cc4a842015-12-03 11:12:07 -0800251 cond_resched();
Al Virof0c3b502013-05-16 12:07:31 -0400252 rcu_read_lock();
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400253 }
Al Virof0c3b502013-05-16 12:07:31 -0400254 rcu_read_unlock();
255out_fd_loop:
256 put_files_struct(files);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400257out:
258 put_task_struct(p);
Al Virof0c3b502013-05-16 12:07:31 -0400259 return 0;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400260}
261
Al Virof0c3b502013-05-16 12:07:31 -0400262static int proc_readfd(struct file *file, struct dir_context *ctx)
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400263{
Al Virof0c3b502013-05-16 12:07:31 -0400264 return proc_readfd_common(file, ctx, proc_fd_instantiate);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400265}
266
267const struct file_operations proc_fd_operations = {
268 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -0400269 .iterate_shared = proc_readfd,
270 .llseek = generic_file_llseek,
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400271};
272
273static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
274 unsigned int flags)
275{
276 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
277}
278
279/*
280 * /proc/pid/fd needs a special permission handler so that a process can still
281 * access /proc/self/fd after it has executed a setuid().
282 */
283int proc_fd_permission(struct inode *inode, int mask)
284{
Oleg Nesterov54708d22015-11-06 16:30:06 -0800285 struct task_struct *p;
286 int rv;
287
288 rv = generic_permission(inode, mask);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400289 if (rv == 0)
Oleg Nesterov54708d22015-11-06 16:30:06 -0800290 return rv;
291
292 rcu_read_lock();
293 p = pid_task(proc_pid(inode), PIDTYPE_PID);
294 if (p && same_thread_group(p, current))
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400295 rv = 0;
Oleg Nesterov54708d22015-11-06 16:30:06 -0800296 rcu_read_unlock();
297
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400298 return rv;
299}
300
301const struct inode_operations proc_fd_inode_operations = {
302 .lookup = proc_lookupfd,
303 .permission = proc_fd_permission,
304 .setattr = proc_setattr,
305};
306
Al Viroc52a47a2013-06-15 11:15:20 +0400307static int
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400308proc_fdinfo_instantiate(struct inode *dir, struct dentry *dentry,
309 struct task_struct *task, const void *ptr)
310{
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400311 unsigned fd = (unsigned long)ptr;
312 struct proc_inode *ei;
313 struct inode *inode;
314
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +0100315 inode = proc_pid_make_inode(dir->i_sb, task, S_IFREG | S_IRUSR);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400316 if (!inode)
317 goto out;
318
319 ei = PROC_I(inode);
320 ei->fd = fd;
321
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400322 inode->i_fop = &proc_fdinfo_file_operations;
323
324 d_set_d_op(dentry, &tid_fd_dentry_operations);
325 d_add(dentry, inode);
326
327 /* Close the race of the process dying before we return the dentry */
328 if (tid_fd_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +0400329 return 0;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400330 out:
Al Viroc52a47a2013-06-15 11:15:20 +0400331 return -ENOENT;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400332}
333
334static struct dentry *
335proc_lookupfdinfo(struct inode *dir, struct dentry *dentry, unsigned int flags)
336{
337 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
338}
339
Al Virof0c3b502013-05-16 12:07:31 -0400340static int proc_readfdinfo(struct file *file, struct dir_context *ctx)
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400341{
Al Virof0c3b502013-05-16 12:07:31 -0400342 return proc_readfd_common(file, ctx,
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400343 proc_fdinfo_instantiate);
344}
345
346const struct inode_operations proc_fdinfo_inode_operations = {
347 .lookup = proc_lookupfdinfo,
348 .setattr = proc_setattr,
349};
350
351const struct file_operations proc_fdinfo_operations = {
352 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -0400353 .iterate_shared = proc_readfdinfo,
354 .llseek = generic_file_llseek,
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400355};