blob: 172c86270b3127571e745528544583ae55ff449c [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>
Kalesh Singh7bc3fa02021-06-30 18:54:44 -07009#include <linux/ptrace.h>
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040010#include <linux/security.h>
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +040011#include <linux/file.h>
12#include <linux/seq_file.h>
Andrey Vagin6c8c9032015-04-16 12:49:38 -070013#include <linux/fs.h>
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040014
15#include <linux/proc_fs.h>
16
Andrey Vagin49d063c2014-04-07 15:38:34 -070017#include "../mount.h"
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040018#include "internal.h"
19#include "fd.h"
20
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +040021static int seq_show(struct seq_file *m, void *v)
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040022{
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040023 struct files_struct *files = NULL;
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +040024 int f_flags = 0, ret = -ENOENT;
25 struct file *file = NULL;
26 struct task_struct *task;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040027
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +040028 task = get_proc_task(m->private);
29 if (!task)
30 return -ENOENT;
31
Eric W. Biederman775e0652020-11-20 17:14:34 -060032 task_lock(task);
33 files = task->files;
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);
Eric W. Biederman120ce2b2020-11-20 17:14:25 -060038 file = files_lookup_fd_locked(files, fd);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040039 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);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040050 }
Eric W. Biederman775e0652020-11-20 17:14:34 -060051 task_unlock(task);
52 put_task_struct(task);
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +040053
Andrey Vagin6c8c9032015-04-16 12:49:38 -070054 if (ret)
55 return ret;
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +040056
Kalesh Singh3845f252021-06-30 18:54:49 -070057 seq_printf(m, "pos:\t%lli\nflags:\t0%o\nmnt_id:\t%i\nino:\t%lu\n",
Andrey Vagin6c8c9032015-04-16 12:49:38 -070058 (long long)file->f_pos, f_flags,
Kalesh Singh3845f252021-06-30 18:54:49 -070059 real_mount(file->f_path.mnt)->mnt_id,
60 file_inode(file)->i_ino);
Andrey Vagin6c8c9032015-04-16 12:49:38 -070061
Eric W. Biederman775e0652020-11-20 17:14:34 -060062 /* show_fd_locks() never deferences files so a stale value is safe */
Andrey Vagin6c8c9032015-04-16 12:49:38 -070063 show_fd_locks(m, file, files);
64 if (seq_has_overflowed(m))
65 goto out;
66
67 if (file->f_op->show_fdinfo)
68 file->f_op->show_fdinfo(m, file);
69
70out:
71 fput(file);
72 return 0;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040073}
74
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +040075static int seq_fdinfo_open(struct inode *inode, struct file *file)
76{
Kalesh Singh7bc3fa02021-06-30 18:54:44 -070077 bool allowed = false;
78 struct task_struct *task = get_proc_task(inode);
79
80 if (!task)
81 return -ESRCH;
82
83 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
84 put_task_struct(task);
85
86 if (!allowed)
87 return -EACCES;
88
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +040089 return single_open(file, seq_show, inode);
90}
91
92static const struct file_operations proc_fdinfo_file_operations = {
93 .open = seq_fdinfo_open,
94 .read = seq_read,
95 .llseek = seq_lseek,
96 .release = single_release,
97};
98
Al Viro1ae9bd82018-05-02 22:42:22 -040099static bool tid_fd_mode(struct task_struct *task, unsigned fd, fmode_t *mode)
100{
Al Viro1ae9bd82018-05-02 22:42:22 -0400101 struct file *file;
102
Al Viro1ae9bd82018-05-02 22:42:22 -0400103 rcu_read_lock();
Eric W. Biederman64eb6612020-11-20 17:14:29 -0600104 file = task_lookup_fd_rcu(task, fd);
Al Viro1ae9bd82018-05-02 22:42:22 -0400105 if (file)
106 *mode = file->f_mode;
107 rcu_read_unlock();
Al Viro1ae9bd82018-05-02 22:42:22 -0400108 return !!file;
109}
110
Al Viro98836382018-05-03 09:00:33 -0400111static void tid_fd_update_inode(struct task_struct *task, struct inode *inode,
112 fmode_t f_mode)
113{
114 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
115
116 if (S_ISLNK(inode->i_mode)) {
117 unsigned i_mode = S_IFLNK;
118 if (f_mode & FMODE_READ)
119 i_mode |= S_IRUSR | S_IXUSR;
120 if (f_mode & FMODE_WRITE)
121 i_mode |= S_IWUSR | S_IXUSR;
122 inode->i_mode = i_mode;
123 }
124 security_task_to_inode(task, inode);
125}
126
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400127static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags)
128{
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400129 struct task_struct *task;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400130 struct inode *inode;
Alexey Dobriyan771187d2016-09-02 00:42:02 +0300131 unsigned int fd;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400132
133 if (flags & LOOKUP_RCU)
134 return -ECHILD;
135
David Howells2b0143b2015-03-17 22:25:59 +0000136 inode = d_inode(dentry);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400137 task = get_proc_task(inode);
138 fd = proc_fd(inode);
139
140 if (task) {
Al Viro98836382018-05-03 09:00:33 -0400141 fmode_t f_mode;
Al Viro1ae9bd82018-05-02 22:42:22 -0400142 if (tid_fd_mode(task, fd, &f_mode)) {
Al Viro98836382018-05-03 09:00:33 -0400143 tid_fd_update_inode(task, inode, f_mode);
Al Viro1ae9bd82018-05-02 22:42:22 -0400144 put_task_struct(task);
145 return 1;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400146 }
147 put_task_struct(task);
148 }
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400149 return 0;
150}
151
152static const struct dentry_operations tid_fd_dentry_operations = {
153 .d_revalidate = tid_fd_revalidate,
154 .d_delete = pid_delete_dentry,
155};
156
157static int proc_fd_link(struct dentry *dentry, struct path *path)
158{
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +0400159 struct task_struct *task;
160 int ret = -ENOENT;
161
David Howells2b0143b2015-03-17 22:25:59 +0000162 task = get_proc_task(d_inode(dentry));
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +0400163 if (task) {
Alexey Dobriyan771187d2016-09-02 00:42:02 +0300164 unsigned int fd = proc_fd(d_inode(dentry));
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +0400165 struct file *fd_file;
166
Eric W. Biederman439be322020-11-20 17:14:23 -0600167 fd_file = fget_task(task, fd);
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +0400168 if (fd_file) {
169 *path = fd_file->f_path;
170 path_get(&fd_file->f_path);
171 ret = 0;
Eric W. Biederman439be322020-11-20 17:14:23 -0600172 fput(fd_file);
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +0400173 }
Eric W. Biederman439be322020-11-20 17:14:23 -0600174 put_task_struct(task);
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +0400175 }
176
177 return ret;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400178}
179
Al Viro98836382018-05-03 09:00:33 -0400180struct fd_data {
181 fmode_t mode;
182 unsigned fd;
183};
184
Al Viro0168b9e2018-05-03 09:21:05 -0400185static struct dentry *proc_fd_instantiate(struct dentry *dentry,
186 struct task_struct *task, const void *ptr)
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400187{
Al Viro98836382018-05-03 09:00:33 -0400188 const struct fd_data *data = ptr;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400189 struct proc_inode *ei;
190 struct inode *inode;
191
Al Viro0168b9e2018-05-03 09:21:05 -0400192 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400193 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -0400194 return ERR_PTR(-ENOENT);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400195
196 ei = PROC_I(inode);
Al Viro98836382018-05-03 09:00:33 -0400197 ei->fd = data->fd;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400198
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400199 inode->i_op = &proc_pid_link_inode_operations;
200 inode->i_size = 64;
201
202 ei->op.proc_get_link = proc_fd_link;
Al Viro98836382018-05-03 09:00:33 -0400203 tid_fd_update_inode(task, inode, data->mode);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400204
205 d_set_d_op(dentry, &tid_fd_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -0400206 return d_splice_alias(inode, dentry);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400207}
208
209static struct dentry *proc_lookupfd_common(struct inode *dir,
210 struct dentry *dentry,
211 instantiate_t instantiate)
212{
213 struct task_struct *task = get_proc_task(dir);
Al Viro98836382018-05-03 09:00:33 -0400214 struct fd_data data = {.fd = name_to_int(&dentry->d_name)};
Al Viro0168b9e2018-05-03 09:21:05 -0400215 struct dentry *result = ERR_PTR(-ENOENT);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400216
217 if (!task)
218 goto out_no_task;
Al Viro98836382018-05-03 09:00:33 -0400219 if (data.fd == ~0U)
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400220 goto out;
Al Viro98836382018-05-03 09:00:33 -0400221 if (!tid_fd_mode(task, data.fd, &data.mode))
Al Viro1ae9bd82018-05-02 22:42:22 -0400222 goto out;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400223
Al Viro0168b9e2018-05-03 09:21:05 -0400224 result = instantiate(dentry, task, &data);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400225out:
226 put_task_struct(task);
227out_no_task:
Al Viro0168b9e2018-05-03 09:21:05 -0400228 return result;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400229}
230
Al Virof0c3b502013-05-16 12:07:31 -0400231static int proc_readfd_common(struct file *file, struct dir_context *ctx,
232 instantiate_t instantiate)
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400233{
Al Virof0c3b502013-05-16 12:07:31 -0400234 struct task_struct *p = get_proc_task(file_inode(file));
Al Virof0c3b502013-05-16 12:07:31 -0400235 unsigned int fd;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400236
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400237 if (!p)
Al Virof0c3b502013-05-16 12:07:31 -0400238 return -ENOENT;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400239
Al Virof0c3b502013-05-16 12:07:31 -0400240 if (!dir_emit_dots(file, ctx))
241 goto out;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400242
Al Virof0c3b502013-05-16 12:07:31 -0400243 rcu_read_lock();
Eric W. Biederman5b17b612020-11-20 17:14:32 -0600244 for (fd = ctx->pos - 2;; fd++) {
Al Viro98836382018-05-03 09:00:33 -0400245 struct file *f;
246 struct fd_data data;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -0800247 char name[10 + 1];
Alexey Dobriyana4ef3892018-06-07 17:10:10 -0700248 unsigned int len;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400249
Eric W. Biederman5b17b612020-11-20 17:14:32 -0600250 f = task_lookup_next_fd_rcu(p, &fd);
251 ctx->pos = fd + 2LL;
Al Viro98836382018-05-03 09:00:33 -0400252 if (!f)
Eric W. Biederman5b17b612020-11-20 17:14:32 -0600253 break;
Al Viro98836382018-05-03 09:00:33 -0400254 data.mode = f->f_mode;
Al Virof0c3b502013-05-16 12:07:31 -0400255 rcu_read_unlock();
Al Viro98836382018-05-03 09:00:33 -0400256 data.fd = fd;
Al Virof0c3b502013-05-16 12:07:31 -0400257
Alexey Dobriyan771187d2016-09-02 00:42:02 +0300258 len = snprintf(name, sizeof(name), "%u", fd);
Al Virof0c3b502013-05-16 12:07:31 -0400259 if (!proc_fill_cache(file, ctx,
260 name, len, instantiate, p,
Al Viro98836382018-05-03 09:00:33 -0400261 &data))
Eric W. Biederman5b17b612020-11-20 17:14:32 -0600262 goto out;
Eric Dumazet3cc4a842015-12-03 11:12:07 -0800263 cond_resched();
Al Virof0c3b502013-05-16 12:07:31 -0400264 rcu_read_lock();
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400265 }
Al Virof0c3b502013-05-16 12:07:31 -0400266 rcu_read_unlock();
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400267out:
268 put_task_struct(p);
Al Virof0c3b502013-05-16 12:07:31 -0400269 return 0;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400270}
271
Al Virof0c3b502013-05-16 12:07:31 -0400272static int proc_readfd(struct file *file, struct dir_context *ctx)
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400273{
Al Virof0c3b502013-05-16 12:07:31 -0400274 return proc_readfd_common(file, ctx, proc_fd_instantiate);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400275}
276
277const struct file_operations proc_fd_operations = {
278 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -0400279 .iterate_shared = proc_readfd,
280 .llseek = generic_file_llseek,
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400281};
282
283static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
284 unsigned int flags)
285{
286 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
287}
288
289/*
290 * /proc/pid/fd needs a special permission handler so that a process can still
291 * access /proc/self/fd after it has executed a setuid().
292 */
Christian Brauner549c7292021-01-21 14:19:43 +0100293int proc_fd_permission(struct user_namespace *mnt_userns,
294 struct inode *inode, int mask)
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400295{
Oleg Nesterov54708d22015-11-06 16:30:06 -0800296 struct task_struct *p;
297 int rv;
298
Christian Brauner47291ba2021-01-21 14:19:24 +0100299 rv = generic_permission(&init_user_ns, inode, mask);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400300 if (rv == 0)
Oleg Nesterov54708d22015-11-06 16:30:06 -0800301 return rv;
302
303 rcu_read_lock();
304 p = pid_task(proc_pid(inode), PIDTYPE_PID);
305 if (p && same_thread_group(p, current))
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400306 rv = 0;
Oleg Nesterov54708d22015-11-06 16:30:06 -0800307 rcu_read_unlock();
308
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400309 return rv;
310}
311
312const struct inode_operations proc_fd_inode_operations = {
313 .lookup = proc_lookupfd,
314 .permission = proc_fd_permission,
315 .setattr = proc_setattr,
316};
317
Al Viro0168b9e2018-05-03 09:21:05 -0400318static struct dentry *proc_fdinfo_instantiate(struct dentry *dentry,
319 struct task_struct *task, const void *ptr)
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400320{
Al Viro98836382018-05-03 09:00:33 -0400321 const struct fd_data *data = ptr;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400322 struct proc_inode *ei;
323 struct inode *inode;
324
Kalesh Singh7bc3fa02021-06-30 18:54:44 -0700325 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFREG | S_IRUGO);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400326 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -0400327 return ERR_PTR(-ENOENT);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400328
329 ei = PROC_I(inode);
Al Viro98836382018-05-03 09:00:33 -0400330 ei->fd = data->fd;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400331
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400332 inode->i_fop = &proc_fdinfo_file_operations;
Al Viro98836382018-05-03 09:00:33 -0400333 tid_fd_update_inode(task, inode, 0);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400334
335 d_set_d_op(dentry, &tid_fd_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -0400336 return d_splice_alias(inode, dentry);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400337}
338
339static struct dentry *
340proc_lookupfdinfo(struct inode *dir, struct dentry *dentry, unsigned int flags)
341{
342 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
343}
344
Al Virof0c3b502013-05-16 12:07:31 -0400345static int proc_readfdinfo(struct file *file, struct dir_context *ctx)
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400346{
Al Virof0c3b502013-05-16 12:07:31 -0400347 return proc_readfd_common(file, ctx,
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400348 proc_fdinfo_instantiate);
349}
350
351const struct inode_operations proc_fdinfo_inode_operations = {
352 .lookup = proc_lookupfdinfo,
353 .setattr = proc_setattr,
354};
355
356const struct file_operations proc_fdinfo_operations = {
357 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -0400358 .iterate_shared = proc_readfdinfo,
359 .llseek = generic_file_llseek,
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400360};