blob: 07fc4fad2602e269c752fb4120d452403229a81e [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
Eric W. Biederman775e0652020-11-20 17:14:34 -060031 task_lock(task);
32 files = task->files;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040033 if (files) {
Alexey Dobriyan771187d2016-09-02 00:42:02 +030034 unsigned int fd = proc_fd(m->private);
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +040035
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040036 spin_lock(&files->file_lock);
Eric W. Biederman120ce2b2020-11-20 17:14:25 -060037 file = files_lookup_fd_locked(files, fd);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040038 if (file) {
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +040039 struct fdtable *fdt = files_fdtable(files);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040040
Al Viroc6f3d812012-08-26 11:01:04 -040041 f_flags = file->f_flags;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040042 if (close_on_exec(fd, fdt))
43 f_flags |= O_CLOEXEC;
44
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +040045 get_file(file);
46 ret = 0;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040047 }
48 spin_unlock(&files->file_lock);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040049 }
Eric W. Biederman775e0652020-11-20 17:14:34 -060050 task_unlock(task);
51 put_task_struct(task);
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
Eric W. Biederman775e0652020-11-20 17:14:34 -060060 /* show_fd_locks() never deferences files so a stale value is safe */
Andrey Vagin6c8c9032015-04-16 12:49:38 -070061 show_fd_locks(m, file, files);
62 if (seq_has_overflowed(m))
63 goto out;
64
65 if (file->f_op->show_fdinfo)
66 file->f_op->show_fdinfo(m, file);
67
68out:
69 fput(file);
70 return 0;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040071}
72
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +040073static int seq_fdinfo_open(struct inode *inode, struct file *file)
74{
75 return single_open(file, seq_show, inode);
76}
77
78static const struct file_operations proc_fdinfo_file_operations = {
79 .open = seq_fdinfo_open,
80 .read = seq_read,
81 .llseek = seq_lseek,
82 .release = single_release,
83};
84
Al Viro1ae9bd82018-05-02 22:42:22 -040085static bool tid_fd_mode(struct task_struct *task, unsigned fd, fmode_t *mode)
86{
Al Viro1ae9bd82018-05-02 22:42:22 -040087 struct file *file;
88
Al Viro1ae9bd82018-05-02 22:42:22 -040089 rcu_read_lock();
Eric W. Biederman64eb6612020-11-20 17:14:29 -060090 file = task_lookup_fd_rcu(task, fd);
Al Viro1ae9bd82018-05-02 22:42:22 -040091 if (file)
92 *mode = file->f_mode;
93 rcu_read_unlock();
Al Viro1ae9bd82018-05-02 22:42:22 -040094 return !!file;
95}
96
Al Viro98836382018-05-03 09:00:33 -040097static void tid_fd_update_inode(struct task_struct *task, struct inode *inode,
98 fmode_t f_mode)
99{
100 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
101
102 if (S_ISLNK(inode->i_mode)) {
103 unsigned i_mode = S_IFLNK;
104 if (f_mode & FMODE_READ)
105 i_mode |= S_IRUSR | S_IXUSR;
106 if (f_mode & FMODE_WRITE)
107 i_mode |= S_IWUSR | S_IXUSR;
108 inode->i_mode = i_mode;
109 }
110 security_task_to_inode(task, inode);
111}
112
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400113static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags)
114{
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400115 struct task_struct *task;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400116 struct inode *inode;
Alexey Dobriyan771187d2016-09-02 00:42:02 +0300117 unsigned int fd;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400118
119 if (flags & LOOKUP_RCU)
120 return -ECHILD;
121
David Howells2b0143b2015-03-17 22:25:59 +0000122 inode = d_inode(dentry);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400123 task = get_proc_task(inode);
124 fd = proc_fd(inode);
125
126 if (task) {
Al Viro98836382018-05-03 09:00:33 -0400127 fmode_t f_mode;
Al Viro1ae9bd82018-05-02 22:42:22 -0400128 if (tid_fd_mode(task, fd, &f_mode)) {
Al Viro98836382018-05-03 09:00:33 -0400129 tid_fd_update_inode(task, inode, f_mode);
Al Viro1ae9bd82018-05-02 22:42:22 -0400130 put_task_struct(task);
131 return 1;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400132 }
133 put_task_struct(task);
134 }
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400135 return 0;
136}
137
138static const struct dentry_operations tid_fd_dentry_operations = {
139 .d_revalidate = tid_fd_revalidate,
140 .d_delete = pid_delete_dentry,
141};
142
143static int proc_fd_link(struct dentry *dentry, struct path *path)
144{
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +0400145 struct task_struct *task;
146 int ret = -ENOENT;
147
David Howells2b0143b2015-03-17 22:25:59 +0000148 task = get_proc_task(d_inode(dentry));
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +0400149 if (task) {
Alexey Dobriyan771187d2016-09-02 00:42:02 +0300150 unsigned int fd = proc_fd(d_inode(dentry));
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +0400151 struct file *fd_file;
152
Eric W. Biederman439be322020-11-20 17:14:23 -0600153 fd_file = fget_task(task, fd);
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +0400154 if (fd_file) {
155 *path = fd_file->f_path;
156 path_get(&fd_file->f_path);
157 ret = 0;
Eric W. Biederman439be322020-11-20 17:14:23 -0600158 fput(fd_file);
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +0400159 }
Eric W. Biederman439be322020-11-20 17:14:23 -0600160 put_task_struct(task);
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +0400161 }
162
163 return ret;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400164}
165
Al Viro98836382018-05-03 09:00:33 -0400166struct fd_data {
167 fmode_t mode;
168 unsigned fd;
169};
170
Al Viro0168b9e2018-05-03 09:21:05 -0400171static struct dentry *proc_fd_instantiate(struct dentry *dentry,
172 struct task_struct *task, const void *ptr)
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400173{
Al Viro98836382018-05-03 09:00:33 -0400174 const struct fd_data *data = ptr;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400175 struct proc_inode *ei;
176 struct inode *inode;
177
Al Viro0168b9e2018-05-03 09:21:05 -0400178 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400179 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -0400180 return ERR_PTR(-ENOENT);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400181
182 ei = PROC_I(inode);
Al Viro98836382018-05-03 09:00:33 -0400183 ei->fd = data->fd;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400184
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400185 inode->i_op = &proc_pid_link_inode_operations;
186 inode->i_size = 64;
187
188 ei->op.proc_get_link = proc_fd_link;
Al Viro98836382018-05-03 09:00:33 -0400189 tid_fd_update_inode(task, inode, data->mode);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400190
191 d_set_d_op(dentry, &tid_fd_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -0400192 return d_splice_alias(inode, dentry);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400193}
194
195static struct dentry *proc_lookupfd_common(struct inode *dir,
196 struct dentry *dentry,
197 instantiate_t instantiate)
198{
199 struct task_struct *task = get_proc_task(dir);
Al Viro98836382018-05-03 09:00:33 -0400200 struct fd_data data = {.fd = name_to_int(&dentry->d_name)};
Al Viro0168b9e2018-05-03 09:21:05 -0400201 struct dentry *result = ERR_PTR(-ENOENT);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400202
203 if (!task)
204 goto out_no_task;
Al Viro98836382018-05-03 09:00:33 -0400205 if (data.fd == ~0U)
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400206 goto out;
Al Viro98836382018-05-03 09:00:33 -0400207 if (!tid_fd_mode(task, data.fd, &data.mode))
Al Viro1ae9bd82018-05-02 22:42:22 -0400208 goto out;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400209
Al Viro0168b9e2018-05-03 09:21:05 -0400210 result = instantiate(dentry, task, &data);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400211out:
212 put_task_struct(task);
213out_no_task:
Al Viro0168b9e2018-05-03 09:21:05 -0400214 return result;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400215}
216
Al Virof0c3b502013-05-16 12:07:31 -0400217static int proc_readfd_common(struct file *file, struct dir_context *ctx,
218 instantiate_t instantiate)
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400219{
Al Virof0c3b502013-05-16 12:07:31 -0400220 struct task_struct *p = get_proc_task(file_inode(file));
Al Virof0c3b502013-05-16 12:07:31 -0400221 unsigned int fd;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400222
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400223 if (!p)
Al Virof0c3b502013-05-16 12:07:31 -0400224 return -ENOENT;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400225
Al Virof0c3b502013-05-16 12:07:31 -0400226 if (!dir_emit_dots(file, ctx))
227 goto out;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400228
Al Virof0c3b502013-05-16 12:07:31 -0400229 rcu_read_lock();
Eric W. Biederman5b17b612020-11-20 17:14:32 -0600230 for (fd = ctx->pos - 2;; fd++) {
Al Viro98836382018-05-03 09:00:33 -0400231 struct file *f;
232 struct fd_data data;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -0800233 char name[10 + 1];
Alexey Dobriyana4ef3892018-06-07 17:10:10 -0700234 unsigned int len;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400235
Eric W. Biederman5b17b612020-11-20 17:14:32 -0600236 f = task_lookup_next_fd_rcu(p, &fd);
237 ctx->pos = fd + 2LL;
Al Viro98836382018-05-03 09:00:33 -0400238 if (!f)
Eric W. Biederman5b17b612020-11-20 17:14:32 -0600239 break;
Al Viro98836382018-05-03 09:00:33 -0400240 data.mode = f->f_mode;
Al Virof0c3b502013-05-16 12:07:31 -0400241 rcu_read_unlock();
Al Viro98836382018-05-03 09:00:33 -0400242 data.fd = fd;
Al Virof0c3b502013-05-16 12:07:31 -0400243
Alexey Dobriyan771187d2016-09-02 00:42:02 +0300244 len = snprintf(name, sizeof(name), "%u", fd);
Al Virof0c3b502013-05-16 12:07:31 -0400245 if (!proc_fill_cache(file, ctx,
246 name, len, instantiate, p,
Al Viro98836382018-05-03 09:00:33 -0400247 &data))
Eric W. Biederman5b17b612020-11-20 17:14:32 -0600248 goto out;
Eric Dumazet3cc4a842015-12-03 11:12:07 -0800249 cond_resched();
Al Virof0c3b502013-05-16 12:07:31 -0400250 rcu_read_lock();
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400251 }
Al Virof0c3b502013-05-16 12:07:31 -0400252 rcu_read_unlock();
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400253out:
254 put_task_struct(p);
Al Virof0c3b502013-05-16 12:07:31 -0400255 return 0;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400256}
257
Al Virof0c3b502013-05-16 12:07:31 -0400258static int proc_readfd(struct file *file, struct dir_context *ctx)
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400259{
Al Virof0c3b502013-05-16 12:07:31 -0400260 return proc_readfd_common(file, ctx, proc_fd_instantiate);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400261}
262
263const struct file_operations proc_fd_operations = {
264 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -0400265 .iterate_shared = proc_readfd,
266 .llseek = generic_file_llseek,
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400267};
268
269static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
270 unsigned int flags)
271{
272 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
273}
274
275/*
276 * /proc/pid/fd needs a special permission handler so that a process can still
277 * access /proc/self/fd after it has executed a setuid().
278 */
Christian Brauner549c7292021-01-21 14:19:43 +0100279int proc_fd_permission(struct user_namespace *mnt_userns,
280 struct inode *inode, int mask)
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400281{
Oleg Nesterov54708d22015-11-06 16:30:06 -0800282 struct task_struct *p;
283 int rv;
284
Christian Brauner47291ba2021-01-21 14:19:24 +0100285 rv = generic_permission(&init_user_ns, inode, mask);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400286 if (rv == 0)
Oleg Nesterov54708d22015-11-06 16:30:06 -0800287 return rv;
288
289 rcu_read_lock();
290 p = pid_task(proc_pid(inode), PIDTYPE_PID);
291 if (p && same_thread_group(p, current))
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400292 rv = 0;
Oleg Nesterov54708d22015-11-06 16:30:06 -0800293 rcu_read_unlock();
294
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400295 return rv;
296}
297
298const struct inode_operations proc_fd_inode_operations = {
299 .lookup = proc_lookupfd,
300 .permission = proc_fd_permission,
301 .setattr = proc_setattr,
302};
303
Al Viro0168b9e2018-05-03 09:21:05 -0400304static struct dentry *proc_fdinfo_instantiate(struct dentry *dentry,
305 struct task_struct *task, const void *ptr)
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400306{
Al Viro98836382018-05-03 09:00:33 -0400307 const struct fd_data *data = ptr;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400308 struct proc_inode *ei;
309 struct inode *inode;
310
Al Viro0168b9e2018-05-03 09:21:05 -0400311 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFREG | S_IRUSR);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400312 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -0400313 return ERR_PTR(-ENOENT);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400314
315 ei = PROC_I(inode);
Al Viro98836382018-05-03 09:00:33 -0400316 ei->fd = data->fd;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400317
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400318 inode->i_fop = &proc_fdinfo_file_operations;
Al Viro98836382018-05-03 09:00:33 -0400319 tid_fd_update_inode(task, inode, 0);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400320
321 d_set_d_op(dentry, &tid_fd_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -0400322 return d_splice_alias(inode, dentry);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400323}
324
325static struct dentry *
326proc_lookupfdinfo(struct inode *dir, struct dentry *dentry, unsigned int flags)
327{
328 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
329}
330
Al Virof0c3b502013-05-16 12:07:31 -0400331static int proc_readfdinfo(struct file *file, struct dir_context *ctx)
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400332{
Al Virof0c3b502013-05-16 12:07:31 -0400333 return proc_readfd_common(file, ctx,
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400334 proc_fdinfo_instantiate);
335}
336
337const struct inode_operations proc_fdinfo_inode_operations = {
338 .lookup = proc_lookupfdinfo,
339 .setattr = proc_setattr,
340};
341
342const struct file_operations proc_fdinfo_operations = {
343 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -0400344 .iterate_shared = proc_readfdinfo,
345 .llseek = generic_file_llseek,
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400346};