]> git.ipfire.org Git - people/ms/linux.git/blame - fs/proc/fd.c
proc: Avoid mixing integer types in mem_rw()
[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>
9#include <linux/security.h>
ddd3e077
CG
10#include <linux/file.h>
11#include <linux/seq_file.h>
6c8c9031 12#include <linux/fs.h>
faf60af1
CG
13
14#include <linux/proc_fs.h>
15
49d063cb 16#include "../mount.h"
faf60af1
CG
17#include "internal.h"
18#include "fd.h"
19
ddd3e077 20static int seq_show(struct seq_file *m, void *v)
faf60af1 21{
faf60af1 22 struct files_struct *files = NULL;
ddd3e077
CG
23 int f_flags = 0, ret = -ENOENT;
24 struct file *file = NULL;
25 struct task_struct *task;
26
27 task = get_proc_task(m->private);
28 if (!task)
29 return -ENOENT;
30
775e0656
EB
31 task_lock(task);
32 files = task->files;
faf60af1 33 if (files) {
771187d6 34 unsigned int fd = proc_fd(m->private);
ddd3e077 35
faf60af1 36 spin_lock(&files->file_lock);
120ce2b0 37 file = files_lookup_fd_locked(files, fd);
faf60af1 38 if (file) {
ddd3e077 39 struct fdtable *fdt = files_fdtable(files);
faf60af1 40
c6f3d811 41 f_flags = file->f_flags;
faf60af1
CG
42 if (close_on_exec(fd, fdt))
43 f_flags |= O_CLOEXEC;
44
ddd3e077
CG
45 get_file(file);
46 ret = 0;
faf60af1
CG
47 }
48 spin_unlock(&files->file_lock);
faf60af1 49 }
775e0656
EB
50 task_unlock(task);
51 put_task_struct(task);
ddd3e077 52
6c8c9031
AV
53 if (ret)
54 return ret;
55
56 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
775e0656 60 /* show_fd_locks() never deferences files so a stale value is safe */
6c8c9031
AV
61 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);
ddd3e077 67
6c8c9031
AV
68out:
69 fput(file);
70 return 0;
faf60af1
CG
71}
72
ddd3e077
CG
73static 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
1ae9bd8b
AV
85static bool tid_fd_mode(struct task_struct *task, unsigned fd, fmode_t *mode)
86{
1ae9bd8b
AV
87 struct file *file;
88
1ae9bd8b 89 rcu_read_lock();
64eb661f 90 file = task_lookup_fd_rcu(task, fd);
1ae9bd8b
AV
91 if (file)
92 *mode = file->f_mode;
93 rcu_read_unlock();
1ae9bd8b
AV
94 return !!file;
95}
96
98836386
AV
97static 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
faf60af1
CG
113static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags)
114{
faf60af1 115 struct task_struct *task;
faf60af1 116 struct inode *inode;
771187d6 117 unsigned int fd;
faf60af1
CG
118
119 if (flags & LOOKUP_RCU)
120 return -ECHILD;
121
2b0143b5 122 inode = d_inode(dentry);
faf60af1
CG
123 task = get_proc_task(inode);
124 fd = proc_fd(inode);
125
126 if (task) {
98836386 127 fmode_t f_mode;
1ae9bd8b 128 if (tid_fd_mode(task, fd, &f_mode)) {
98836386 129 tid_fd_update_inode(task, inode, f_mode);
1ae9bd8b
AV
130 put_task_struct(task);
131 return 1;
faf60af1
CG
132 }
133 put_task_struct(task);
134 }
faf60af1
CG
135 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{
ddd3e077
CG
145 struct task_struct *task;
146 int ret = -ENOENT;
147
2b0143b5 148 task = get_proc_task(d_inode(dentry));
ddd3e077 149 if (task) {
771187d6 150 unsigned int fd = proc_fd(d_inode(dentry));
ddd3e077
CG
151 struct file *fd_file;
152
439be326 153 fd_file = fget_task(task, fd);
ddd3e077
CG
154 if (fd_file) {
155 *path = fd_file->f_path;
156 path_get(&fd_file->f_path);
157 ret = 0;
439be326 158 fput(fd_file);
ddd3e077 159 }
439be326 160 put_task_struct(task);
ddd3e077
CG
161 }
162
163 return ret;
faf60af1
CG
164}
165
98836386
AV
166struct fd_data {
167 fmode_t mode;
168 unsigned fd;
169};
170
0168b9e3
AV
171static struct dentry *proc_fd_instantiate(struct dentry *dentry,
172 struct task_struct *task, const void *ptr)
faf60af1 173{
98836386 174 const struct fd_data *data = ptr;
faf60af1
CG
175 struct proc_inode *ei;
176 struct inode *inode;
177
0168b9e3 178 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK);
faf60af1 179 if (!inode)
0168b9e3 180 return ERR_PTR(-ENOENT);
faf60af1
CG
181
182 ei = PROC_I(inode);
98836386 183 ei->fd = data->fd;
faf60af1 184
faf60af1
CG
185 inode->i_op = &proc_pid_link_inode_operations;
186 inode->i_size = 64;
187
188 ei->op.proc_get_link = proc_fd_link;
98836386 189 tid_fd_update_inode(task, inode, data->mode);
faf60af1
CG
190
191 d_set_d_op(dentry, &tid_fd_dentry_operations);
0168b9e3 192 return d_splice_alias(inode, dentry);
faf60af1
CG
193}
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);
98836386 200 struct fd_data data = {.fd = name_to_int(&dentry->d_name)};
0168b9e3 201 struct dentry *result = ERR_PTR(-ENOENT);
faf60af1
CG
202
203 if (!task)
204 goto out_no_task;
98836386 205 if (data.fd == ~0U)
faf60af1 206 goto out;
98836386 207 if (!tid_fd_mode(task, data.fd, &data.mode))
1ae9bd8b 208 goto out;
faf60af1 209
0168b9e3 210 result = instantiate(dentry, task, &data);
faf60af1
CG
211out:
212 put_task_struct(task);
213out_no_task:
0168b9e3 214 return result;
faf60af1
CG
215}
216
f0c3b509
AV
217static int proc_readfd_common(struct file *file, struct dir_context *ctx,
218 instantiate_t instantiate)
faf60af1 219{
f0c3b509 220 struct task_struct *p = get_proc_task(file_inode(file));
f0c3b509 221 unsigned int fd;
faf60af1 222
faf60af1 223 if (!p)
f0c3b509 224 return -ENOENT;
faf60af1 225
f0c3b509
AV
226 if (!dir_emit_dots(file, ctx))
227 goto out;
f0c3b509
AV
228
229 rcu_read_lock();
5b17b618 230 for (fd = ctx->pos - 2;; fd++) {
98836386
AV
231 struct file *f;
232 struct fd_data data;
e3912ac3 233 char name[10 + 1];
a4ef3895 234 unsigned int len;
f0c3b509 235
5b17b618
EB
236 f = task_lookup_next_fd_rcu(p, &fd);
237 ctx->pos = fd + 2LL;
98836386 238 if (!f)
5b17b618 239 break;
98836386 240 data.mode = f->f_mode;
f0c3b509 241 rcu_read_unlock();
98836386 242 data.fd = fd;
f0c3b509 243
771187d6 244 len = snprintf(name, sizeof(name), "%u", fd);
f0c3b509
AV
245 if (!proc_fill_cache(file, ctx,
246 name, len, instantiate, p,
98836386 247 &data))
5b17b618 248 goto out;
3cc4a84e 249 cond_resched();
f0c3b509 250 rcu_read_lock();
faf60af1 251 }
f0c3b509 252 rcu_read_unlock();
faf60af1
CG
253out:
254 put_task_struct(p);
f0c3b509 255 return 0;
faf60af1
CG
256}
257
f0c3b509 258static int proc_readfd(struct file *file, struct dir_context *ctx)
faf60af1 259{
f0c3b509 260 return proc_readfd_common(file, ctx, proc_fd_instantiate);
faf60af1
CG
261}
262
263const struct file_operations proc_fd_operations = {
264 .read = generic_read_dir,
f50752ea
AV
265 .iterate_shared = proc_readfd,
266 .llseek = generic_file_llseek,
faf60af1
CG
267};
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 */
549c7297
CB
279int proc_fd_permission(struct user_namespace *mnt_userns,
280 struct inode *inode, int mask)
faf60af1 281{
54708d28
ON
282 struct task_struct *p;
283 int rv;
284
47291baa 285 rv = generic_permission(&init_user_ns, inode, mask);
faf60af1 286 if (rv == 0)
54708d28
ON
287 return rv;
288
289 rcu_read_lock();
290 p = pid_task(proc_pid(inode), PIDTYPE_PID);
291 if (p && same_thread_group(p, current))
faf60af1 292 rv = 0;
54708d28
ON
293 rcu_read_unlock();
294
faf60af1
CG
295 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
0168b9e3
AV
304static struct dentry *proc_fdinfo_instantiate(struct dentry *dentry,
305 struct task_struct *task, const void *ptr)
faf60af1 306{
98836386 307 const struct fd_data *data = ptr;
faf60af1
CG
308 struct proc_inode *ei;
309 struct inode *inode;
310
0168b9e3 311 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFREG | S_IRUSR);
faf60af1 312 if (!inode)
0168b9e3 313 return ERR_PTR(-ENOENT);
faf60af1
CG
314
315 ei = PROC_I(inode);
98836386 316 ei->fd = data->fd;
faf60af1 317
faf60af1 318 inode->i_fop = &proc_fdinfo_file_operations;
98836386 319 tid_fd_update_inode(task, inode, 0);
faf60af1
CG
320
321 d_set_d_op(dentry, &tid_fd_dentry_operations);
0168b9e3 322 return d_splice_alias(inode, dentry);
faf60af1
CG
323}
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
f0c3b509 331static int proc_readfdinfo(struct file *file, struct dir_context *ctx)
faf60af1 332{
f0c3b509 333 return proc_readfd_common(file, ctx,
faf60af1
CG
334 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,
f50752ea
AV
344 .iterate_shared = proc_readfdinfo,
345 .llseek = generic_file_llseek,
faf60af1 346};