blob: d58960f6ee524db7fbc7642a2b1424b272359bd3 [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
Al Viro1ae9bd82018-05-02 22:42:22 -040084static bool tid_fd_mode(struct task_struct *task, unsigned fd, fmode_t *mode)
85{
86 struct files_struct *files = get_files_struct(task);
87 struct file *file;
88
89 if (!files)
90 return false;
91
92 rcu_read_lock();
93 file = fcheck_files(files, fd);
94 if (file)
95 *mode = file->f_mode;
96 rcu_read_unlock();
97 put_files_struct(files);
98 return !!file;
99}
100
Al Viro98836382018-05-03 09:00:33 -0400101static void tid_fd_update_inode(struct task_struct *task, struct inode *inode,
102 fmode_t f_mode)
103{
104 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
105
106 if (S_ISLNK(inode->i_mode)) {
107 unsigned i_mode = S_IFLNK;
108 if (f_mode & FMODE_READ)
109 i_mode |= S_IRUSR | S_IXUSR;
110 if (f_mode & FMODE_WRITE)
111 i_mode |= S_IWUSR | S_IXUSR;
112 inode->i_mode = i_mode;
113 }
114 security_task_to_inode(task, inode);
115}
116
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400117static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags)
118{
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400119 struct task_struct *task;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400120 struct inode *inode;
Alexey Dobriyan771187d2016-09-02 00:42:02 +0300121 unsigned int fd;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400122
123 if (flags & LOOKUP_RCU)
124 return -ECHILD;
125
David Howells2b0143b2015-03-17 22:25:59 +0000126 inode = d_inode(dentry);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400127 task = get_proc_task(inode);
128 fd = proc_fd(inode);
129
130 if (task) {
Al Viro98836382018-05-03 09:00:33 -0400131 fmode_t f_mode;
Al Viro1ae9bd82018-05-02 22:42:22 -0400132 if (tid_fd_mode(task, fd, &f_mode)) {
Al Viro98836382018-05-03 09:00:33 -0400133 tid_fd_update_inode(task, inode, f_mode);
Al Viro1ae9bd82018-05-02 22:42:22 -0400134 put_task_struct(task);
135 return 1;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400136 }
137 put_task_struct(task);
138 }
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400139 return 0;
140}
141
142static const struct dentry_operations tid_fd_dentry_operations = {
143 .d_revalidate = tid_fd_revalidate,
144 .d_delete = pid_delete_dentry,
145};
146
147static int proc_fd_link(struct dentry *dentry, struct path *path)
148{
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +0400149 struct task_struct *task;
150 int ret = -ENOENT;
151
David Howells2b0143b2015-03-17 22:25:59 +0000152 task = get_proc_task(d_inode(dentry));
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +0400153 if (task) {
Alexey Dobriyan771187d2016-09-02 00:42:02 +0300154 unsigned int fd = proc_fd(d_inode(dentry));
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +0400155 struct file *fd_file;
156
Eric W. Biederman439be322020-11-20 17:14:23 -0600157 fd_file = fget_task(task, fd);
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +0400158 if (fd_file) {
159 *path = fd_file->f_path;
160 path_get(&fd_file->f_path);
161 ret = 0;
Eric W. Biederman439be322020-11-20 17:14:23 -0600162 fput(fd_file);
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +0400163 }
Eric W. Biederman439be322020-11-20 17:14:23 -0600164 put_task_struct(task);
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +0400165 }
166
167 return ret;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400168}
169
Al Viro98836382018-05-03 09:00:33 -0400170struct fd_data {
171 fmode_t mode;
172 unsigned fd;
173};
174
Al Viro0168b9e2018-05-03 09:21:05 -0400175static struct dentry *proc_fd_instantiate(struct dentry *dentry,
176 struct task_struct *task, const void *ptr)
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400177{
Al Viro98836382018-05-03 09:00:33 -0400178 const struct fd_data *data = ptr;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400179 struct proc_inode *ei;
180 struct inode *inode;
181
Al Viro0168b9e2018-05-03 09:21:05 -0400182 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400183 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -0400184 return ERR_PTR(-ENOENT);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400185
186 ei = PROC_I(inode);
Al Viro98836382018-05-03 09:00:33 -0400187 ei->fd = data->fd;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400188
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400189 inode->i_op = &proc_pid_link_inode_operations;
190 inode->i_size = 64;
191
192 ei->op.proc_get_link = proc_fd_link;
Al Viro98836382018-05-03 09:00:33 -0400193 tid_fd_update_inode(task, inode, data->mode);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400194
195 d_set_d_op(dentry, &tid_fd_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -0400196 return d_splice_alias(inode, dentry);
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 Viro98836382018-05-03 09:00:33 -0400204 struct fd_data data = {.fd = name_to_int(&dentry->d_name)};
Al Viro0168b9e2018-05-03 09:21:05 -0400205 struct dentry *result = ERR_PTR(-ENOENT);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400206
207 if (!task)
208 goto out_no_task;
Al Viro98836382018-05-03 09:00:33 -0400209 if (data.fd == ~0U)
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400210 goto out;
Al Viro98836382018-05-03 09:00:33 -0400211 if (!tid_fd_mode(task, data.fd, &data.mode))
Al Viro1ae9bd82018-05-02 22:42:22 -0400212 goto out;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400213
Al Viro0168b9e2018-05-03 09:21:05 -0400214 result = instantiate(dentry, task, &data);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400215out:
216 put_task_struct(task);
217out_no_task:
Al Viro0168b9e2018-05-03 09:21:05 -0400218 return result;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400219}
220
Al Virof0c3b502013-05-16 12:07:31 -0400221static int proc_readfd_common(struct file *file, struct dir_context *ctx,
222 instantiate_t instantiate)
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400223{
Al Virof0c3b502013-05-16 12:07:31 -0400224 struct task_struct *p = get_proc_task(file_inode(file));
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400225 struct files_struct *files;
Al Virof0c3b502013-05-16 12:07:31 -0400226 unsigned int fd;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400227
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400228 if (!p)
Al Virof0c3b502013-05-16 12:07:31 -0400229 return -ENOENT;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400230
Al Virof0c3b502013-05-16 12:07:31 -0400231 if (!dir_emit_dots(file, ctx))
232 goto out;
Al Virof0c3b502013-05-16 12:07:31 -0400233 files = get_files_struct(p);
234 if (!files)
235 goto out;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400236
Al Virof0c3b502013-05-16 12:07:31 -0400237 rcu_read_lock();
238 for (fd = ctx->pos - 2;
239 fd < files_fdtable(files)->max_fds;
240 fd++, ctx->pos++) {
Al Viro98836382018-05-03 09:00:33 -0400241 struct file *f;
242 struct fd_data data;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -0800243 char name[10 + 1];
Alexey Dobriyana4ef3892018-06-07 17:10:10 -0700244 unsigned int len;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400245
Al Viro98836382018-05-03 09:00:33 -0400246 f = fcheck_files(files, fd);
247 if (!f)
Al Virof0c3b502013-05-16 12:07:31 -0400248 continue;
Al Viro98836382018-05-03 09:00:33 -0400249 data.mode = f->f_mode;
Al Virof0c3b502013-05-16 12:07:31 -0400250 rcu_read_unlock();
Al Viro98836382018-05-03 09:00:33 -0400251 data.fd = fd;
Al Virof0c3b502013-05-16 12:07:31 -0400252
Alexey Dobriyan771187d2016-09-02 00:42:02 +0300253 len = snprintf(name, sizeof(name), "%u", fd);
Al Virof0c3b502013-05-16 12:07:31 -0400254 if (!proc_fill_cache(file, ctx,
255 name, len, instantiate, p,
Al Viro98836382018-05-03 09:00:33 -0400256 &data))
Al Virof0c3b502013-05-16 12:07:31 -0400257 goto out_fd_loop;
Eric Dumazet3cc4a842015-12-03 11:12:07 -0800258 cond_resched();
Al Virof0c3b502013-05-16 12:07:31 -0400259 rcu_read_lock();
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400260 }
Al Virof0c3b502013-05-16 12:07:31 -0400261 rcu_read_unlock();
262out_fd_loop:
263 put_files_struct(files);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400264out:
265 put_task_struct(p);
Al Virof0c3b502013-05-16 12:07:31 -0400266 return 0;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400267}
268
Al Virof0c3b502013-05-16 12:07:31 -0400269static int proc_readfd(struct file *file, struct dir_context *ctx)
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400270{
Al Virof0c3b502013-05-16 12:07:31 -0400271 return proc_readfd_common(file, ctx, proc_fd_instantiate);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400272}
273
274const struct file_operations proc_fd_operations = {
275 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -0400276 .iterate_shared = proc_readfd,
277 .llseek = generic_file_llseek,
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400278};
279
280static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
281 unsigned int flags)
282{
283 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
284}
285
286/*
287 * /proc/pid/fd needs a special permission handler so that a process can still
288 * access /proc/self/fd after it has executed a setuid().
289 */
290int proc_fd_permission(struct inode *inode, int mask)
291{
Oleg Nesterov54708d22015-11-06 16:30:06 -0800292 struct task_struct *p;
293 int rv;
294
295 rv = generic_permission(inode, mask);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400296 if (rv == 0)
Oleg Nesterov54708d22015-11-06 16:30:06 -0800297 return rv;
298
299 rcu_read_lock();
300 p = pid_task(proc_pid(inode), PIDTYPE_PID);
301 if (p && same_thread_group(p, current))
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400302 rv = 0;
Oleg Nesterov54708d22015-11-06 16:30:06 -0800303 rcu_read_unlock();
304
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400305 return rv;
306}
307
308const struct inode_operations proc_fd_inode_operations = {
309 .lookup = proc_lookupfd,
310 .permission = proc_fd_permission,
311 .setattr = proc_setattr,
312};
313
Al Viro0168b9e2018-05-03 09:21:05 -0400314static struct dentry *proc_fdinfo_instantiate(struct dentry *dentry,
315 struct task_struct *task, const void *ptr)
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400316{
Al Viro98836382018-05-03 09:00:33 -0400317 const struct fd_data *data = ptr;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400318 struct proc_inode *ei;
319 struct inode *inode;
320
Al Viro0168b9e2018-05-03 09:21:05 -0400321 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFREG | S_IRUSR);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400322 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -0400323 return ERR_PTR(-ENOENT);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400324
325 ei = PROC_I(inode);
Al Viro98836382018-05-03 09:00:33 -0400326 ei->fd = data->fd;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400327
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400328 inode->i_fop = &proc_fdinfo_file_operations;
Al Viro98836382018-05-03 09:00:33 -0400329 tid_fd_update_inode(task, inode, 0);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400330
331 d_set_d_op(dentry, &tid_fd_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -0400332 return d_splice_alias(inode, dentry);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400333}
334
335static struct dentry *
336proc_lookupfdinfo(struct inode *dir, struct dentry *dentry, unsigned int flags)
337{
338 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
339}
340
Al Virof0c3b502013-05-16 12:07:31 -0400341static int proc_readfdinfo(struct file *file, struct dir_context *ctx)
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400342{
Al Virof0c3b502013-05-16 12:07:31 -0400343 return proc_readfd_common(file, ctx,
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400344 proc_fdinfo_instantiate);
345}
346
347const struct inode_operations proc_fdinfo_inode_operations = {
348 .lookup = proc_lookupfdinfo,
349 .setattr = proc_setattr,
350};
351
352const struct file_operations proc_fdinfo_operations = {
353 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -0400354 .iterate_shared = proc_readfdinfo,
355 .llseek = generic_file_llseek,
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400356};