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