Merge tag 'trace-v6.4' of git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux...
[linux-block.git] / fs / proc / fd.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
3f07c014 2#include <linux/sched/signal.h>
faf60af1
CG
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>
7bc3fa01 9#include <linux/ptrace.h>
f1f1f256 10#include <linux/bitmap.h>
faf60af1 11#include <linux/security.h>
ddd3e077
CG
12#include <linux/file.h>
13#include <linux/seq_file.h>
6c8c9031 14#include <linux/fs.h>
5970e15d 15#include <linux/filelock.h>
faf60af1
CG
16
17#include <linux/proc_fs.h>
18
49d063cb 19#include "../mount.h"
faf60af1
CG
20#include "internal.h"
21#include "fd.h"
22
ddd3e077 23static int seq_show(struct seq_file *m, void *v)
faf60af1 24{
faf60af1 25 struct files_struct *files = NULL;
ddd3e077
CG
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
775e0656
EB
34 task_lock(task);
35 files = task->files;
faf60af1 36 if (files) {
771187d6 37 unsigned int fd = proc_fd(m->private);
ddd3e077 38
faf60af1 39 spin_lock(&files->file_lock);
120ce2b0 40 file = files_lookup_fd_locked(files, fd);
faf60af1 41 if (file) {
ddd3e077 42 struct fdtable *fdt = files_fdtable(files);
faf60af1 43
c6f3d811 44 f_flags = file->f_flags;
faf60af1
CG
45 if (close_on_exec(fd, fdt))
46 f_flags |= O_CLOEXEC;
47
ddd3e077
CG
48 get_file(file);
49 ret = 0;
faf60af1
CG
50 }
51 spin_unlock(&files->file_lock);
faf60af1 52 }
775e0656
EB
53 task_unlock(task);
54 put_task_struct(task);
ddd3e077 55
6c8c9031
AV
56 if (ret)
57 return ret;
58
3845f256 59 seq_printf(m, "pos:\t%lli\nflags:\t0%o\nmnt_id:\t%i\nino:\t%lu\n",
6c8c9031 60 (long long)file->f_pos, f_flags,
3845f256
KS
61 real_mount(file->f_path.mnt)->mnt_id,
62 file_inode(file)->i_ino);
6c8c9031 63
775e0656 64 /* show_fd_locks() never deferences files so a stale value is safe */
6c8c9031
AV
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);
ddd3e077 71
6c8c9031
AV
72out:
73 fput(file);
74 return 0;
faf60af1
CG
75}
76
1927e498 77static int proc_fdinfo_access_allowed(struct inode *inode)
ddd3e077 78{
7bc3fa01
KS
79 bool allowed = false;
80 struct task_struct *task = get_proc_task(inode);
81
82 if (!task)
83 return -ESRCH;
84
85 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
86 put_task_struct(task);
87
88 if (!allowed)
89 return -EACCES;
90
1927e498
KS
91 return 0;
92}
93
94static int seq_fdinfo_open(struct inode *inode, struct file *file)
95{
96 int ret = proc_fdinfo_access_allowed(inode);
97
98 if (ret)
99 return ret;
100
ddd3e077
CG
101 return single_open(file, seq_show, inode);
102}
103
104static const struct file_operations proc_fdinfo_file_operations = {
105 .open = seq_fdinfo_open,
106 .read = seq_read,
107 .llseek = seq_lseek,
108 .release = single_release,
109};
110
1ae9bd8b
AV
111static bool tid_fd_mode(struct task_struct *task, unsigned fd, fmode_t *mode)
112{
1ae9bd8b
AV
113 struct file *file;
114
1ae9bd8b 115 rcu_read_lock();
64eb661f 116 file = task_lookup_fd_rcu(task, fd);
1ae9bd8b
AV
117 if (file)
118 *mode = file->f_mode;
119 rcu_read_unlock();
1ae9bd8b
AV
120 return !!file;
121}
122
98836386
AV
123static void tid_fd_update_inode(struct task_struct *task, struct inode *inode,
124 fmode_t f_mode)
125{
126 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
127
128 if (S_ISLNK(inode->i_mode)) {
129 unsigned i_mode = S_IFLNK;
130 if (f_mode & FMODE_READ)
131 i_mode |= S_IRUSR | S_IXUSR;
132 if (f_mode & FMODE_WRITE)
133 i_mode |= S_IWUSR | S_IXUSR;
134 inode->i_mode = i_mode;
135 }
136 security_task_to_inode(task, inode);
137}
138
faf60af1
CG
139static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags)
140{
faf60af1 141 struct task_struct *task;
faf60af1 142 struct inode *inode;
771187d6 143 unsigned int fd;
faf60af1
CG
144
145 if (flags & LOOKUP_RCU)
146 return -ECHILD;
147
2b0143b5 148 inode = d_inode(dentry);
faf60af1
CG
149 task = get_proc_task(inode);
150 fd = proc_fd(inode);
151
152 if (task) {
98836386 153 fmode_t f_mode;
1ae9bd8b 154 if (tid_fd_mode(task, fd, &f_mode)) {
98836386 155 tid_fd_update_inode(task, inode, f_mode);
1ae9bd8b
AV
156 put_task_struct(task);
157 return 1;
faf60af1
CG
158 }
159 put_task_struct(task);
160 }
faf60af1
CG
161 return 0;
162}
163
164static const struct dentry_operations tid_fd_dentry_operations = {
165 .d_revalidate = tid_fd_revalidate,
166 .d_delete = pid_delete_dentry,
167};
168
169static int proc_fd_link(struct dentry *dentry, struct path *path)
170{
ddd3e077
CG
171 struct task_struct *task;
172 int ret = -ENOENT;
173
2b0143b5 174 task = get_proc_task(d_inode(dentry));
ddd3e077 175 if (task) {
771187d6 176 unsigned int fd = proc_fd(d_inode(dentry));
ddd3e077
CG
177 struct file *fd_file;
178
439be326 179 fd_file = fget_task(task, fd);
ddd3e077
CG
180 if (fd_file) {
181 *path = fd_file->f_path;
182 path_get(&fd_file->f_path);
183 ret = 0;
439be326 184 fput(fd_file);
ddd3e077 185 }
439be326 186 put_task_struct(task);
ddd3e077
CG
187 }
188
189 return ret;
faf60af1
CG
190}
191
98836386
AV
192struct fd_data {
193 fmode_t mode;
194 unsigned fd;
195};
196
0168b9e3
AV
197static struct dentry *proc_fd_instantiate(struct dentry *dentry,
198 struct task_struct *task, const void *ptr)
faf60af1 199{
98836386 200 const struct fd_data *data = ptr;
faf60af1
CG
201 struct proc_inode *ei;
202 struct inode *inode;
203
0168b9e3 204 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK);
faf60af1 205 if (!inode)
0168b9e3 206 return ERR_PTR(-ENOENT);
faf60af1
CG
207
208 ei = PROC_I(inode);
98836386 209 ei->fd = data->fd;
faf60af1 210
faf60af1
CG
211 inode->i_op = &proc_pid_link_inode_operations;
212 inode->i_size = 64;
213
214 ei->op.proc_get_link = proc_fd_link;
98836386 215 tid_fd_update_inode(task, inode, data->mode);
faf60af1
CG
216
217 d_set_d_op(dentry, &tid_fd_dentry_operations);
0168b9e3 218 return d_splice_alias(inode, dentry);
faf60af1
CG
219}
220
221static struct dentry *proc_lookupfd_common(struct inode *dir,
222 struct dentry *dentry,
223 instantiate_t instantiate)
224{
225 struct task_struct *task = get_proc_task(dir);
98836386 226 struct fd_data data = {.fd = name_to_int(&dentry->d_name)};
0168b9e3 227 struct dentry *result = ERR_PTR(-ENOENT);
faf60af1
CG
228
229 if (!task)
230 goto out_no_task;
98836386 231 if (data.fd == ~0U)
faf60af1 232 goto out;
98836386 233 if (!tid_fd_mode(task, data.fd, &data.mode))
1ae9bd8b 234 goto out;
faf60af1 235
0168b9e3 236 result = instantiate(dentry, task, &data);
faf60af1
CG
237out:
238 put_task_struct(task);
239out_no_task:
0168b9e3 240 return result;
faf60af1
CG
241}
242
f0c3b509
AV
243static int proc_readfd_common(struct file *file, struct dir_context *ctx,
244 instantiate_t instantiate)
faf60af1 245{
f0c3b509 246 struct task_struct *p = get_proc_task(file_inode(file));
f0c3b509 247 unsigned int fd;
faf60af1 248
faf60af1 249 if (!p)
f0c3b509 250 return -ENOENT;
faf60af1 251
f0c3b509
AV
252 if (!dir_emit_dots(file, ctx))
253 goto out;
f0c3b509
AV
254
255 rcu_read_lock();
5b17b618 256 for (fd = ctx->pos - 2;; fd++) {
98836386
AV
257 struct file *f;
258 struct fd_data data;
e3912ac3 259 char name[10 + 1];
a4ef3895 260 unsigned int len;
f0c3b509 261
5b17b618
EB
262 f = task_lookup_next_fd_rcu(p, &fd);
263 ctx->pos = fd + 2LL;
98836386 264 if (!f)
5b17b618 265 break;
98836386 266 data.mode = f->f_mode;
f0c3b509 267 rcu_read_unlock();
98836386 268 data.fd = fd;
f0c3b509 269
771187d6 270 len = snprintf(name, sizeof(name), "%u", fd);
f0c3b509
AV
271 if (!proc_fill_cache(file, ctx,
272 name, len, instantiate, p,
98836386 273 &data))
5b17b618 274 goto out;
3cc4a84e 275 cond_resched();
f0c3b509 276 rcu_read_lock();
faf60af1 277 }
f0c3b509 278 rcu_read_unlock();
faf60af1
CG
279out:
280 put_task_struct(p);
f0c3b509 281 return 0;
faf60af1
CG
282}
283
f1f1f256
IB
284static int proc_readfd_count(struct inode *inode, loff_t *count)
285{
286 struct task_struct *p = get_proc_task(inode);
287 struct fdtable *fdt;
288
289 if (!p)
290 return -ENOENT;
291
292 task_lock(p);
293 if (p->files) {
294 rcu_read_lock();
295
296 fdt = files_fdtable(p->files);
297 *count = bitmap_weight(fdt->open_fds, fdt->max_fds);
298
299 rcu_read_unlock();
300 }
301 task_unlock(p);
302
303 put_task_struct(p);
304
305 return 0;
306}
307
f0c3b509 308static int proc_readfd(struct file *file, struct dir_context *ctx)
faf60af1 309{
f0c3b509 310 return proc_readfd_common(file, ctx, proc_fd_instantiate);
faf60af1
CG
311}
312
313const struct file_operations proc_fd_operations = {
314 .read = generic_read_dir,
f50752ea
AV
315 .iterate_shared = proc_readfd,
316 .llseek = generic_file_llseek,
faf60af1
CG
317};
318
319static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
320 unsigned int flags)
321{
322 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
323}
324
325/*
326 * /proc/pid/fd needs a special permission handler so that a process can still
327 * access /proc/self/fd after it has executed a setuid().
328 */
4609e1f1 329int proc_fd_permission(struct mnt_idmap *idmap,
549c7297 330 struct inode *inode, int mask)
faf60af1 331{
54708d28
ON
332 struct task_struct *p;
333 int rv;
334
4609e1f1 335 rv = generic_permission(&nop_mnt_idmap, inode, mask);
faf60af1 336 if (rv == 0)
54708d28
ON
337 return rv;
338
339 rcu_read_lock();
340 p = pid_task(proc_pid(inode), PIDTYPE_PID);
341 if (p && same_thread_group(p, current))
faf60af1 342 rv = 0;
54708d28
ON
343 rcu_read_unlock();
344
faf60af1
CG
345 return rv;
346}
347
b74d24f7 348static int proc_fd_getattr(struct mnt_idmap *idmap,
f1f1f256
IB
349 const struct path *path, struct kstat *stat,
350 u32 request_mask, unsigned int query_flags)
351{
352 struct inode *inode = d_inode(path->dentry);
353 int rv = 0;
354
b74d24f7 355 generic_fillattr(&nop_mnt_idmap, inode, stat);
f1f1f256
IB
356
357 /* If it's a directory, put the number of open fds there */
358 if (S_ISDIR(inode->i_mode)) {
359 rv = proc_readfd_count(inode, &stat->size);
360 if (rv < 0)
361 return rv;
362 }
363
364 return rv;
365}
366
faf60af1
CG
367const struct inode_operations proc_fd_inode_operations = {
368 .lookup = proc_lookupfd,
369 .permission = proc_fd_permission,
f1f1f256 370 .getattr = proc_fd_getattr,
faf60af1
CG
371 .setattr = proc_setattr,
372};
373
0168b9e3
AV
374static struct dentry *proc_fdinfo_instantiate(struct dentry *dentry,
375 struct task_struct *task, const void *ptr)
faf60af1 376{
98836386 377 const struct fd_data *data = ptr;
faf60af1
CG
378 struct proc_inode *ei;
379 struct inode *inode;
380
7bc3fa01 381 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFREG | S_IRUGO);
faf60af1 382 if (!inode)
0168b9e3 383 return ERR_PTR(-ENOENT);
faf60af1
CG
384
385 ei = PROC_I(inode);
98836386 386 ei->fd = data->fd;
faf60af1 387
faf60af1 388 inode->i_fop = &proc_fdinfo_file_operations;
98836386 389 tid_fd_update_inode(task, inode, 0);
faf60af1
CG
390
391 d_set_d_op(dentry, &tid_fd_dentry_operations);
0168b9e3 392 return d_splice_alias(inode, dentry);
faf60af1
CG
393}
394
395static struct dentry *
396proc_lookupfdinfo(struct inode *dir, struct dentry *dentry, unsigned int flags)
397{
398 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
399}
400
f0c3b509 401static int proc_readfdinfo(struct file *file, struct dir_context *ctx)
faf60af1 402{
f0c3b509 403 return proc_readfd_common(file, ctx,
faf60af1
CG
404 proc_fdinfo_instantiate);
405}
406
1927e498
KS
407static int proc_open_fdinfo(struct inode *inode, struct file *file)
408{
409 int ret = proc_fdinfo_access_allowed(inode);
410
411 if (ret)
412 return ret;
413
414 return 0;
415}
416
faf60af1
CG
417const struct inode_operations proc_fdinfo_inode_operations = {
418 .lookup = proc_lookupfdinfo,
419 .setattr = proc_setattr,
420};
421
422const struct file_operations proc_fdinfo_operations = {
1927e498 423 .open = proc_open_fdinfo,
faf60af1 424 .read = generic_read_dir,
f50752ea
AV
425 .iterate_shared = proc_readfdinfo,
426 .llseek = generic_file_llseek,
faf60af1 427};