Blame view
fs/proc/fd.c
7.74 KB
b24413180 License cleanup: ... |
1 |
// SPDX-License-Identifier: GPL-2.0 |
3f07c0144 sched/headers: Pr... |
2 |
#include <linux/sched/signal.h> |
faf60af17 procfs: Move /pro... |
3 4 5 6 7 8 9 |
#include <linux/errno.h> #include <linux/dcache.h> #include <linux/path.h> #include <linux/fdtable.h> #include <linux/namei.h> #include <linux/pid.h> #include <linux/security.h> |
ddd3e0771 procfs: Convert /... |
10 11 |
#include <linux/file.h> #include <linux/seq_file.h> |
6c8c90319 proc: show locks ... |
12 |
#include <linux/fs.h> |
faf60af17 procfs: Move /pro... |
13 14 |
#include <linux/proc_fs.h> |
49d063cb3 proc: show mnt_id... |
15 |
#include "../mount.h" |
faf60af17 procfs: Move /pro... |
16 17 |
#include "internal.h" #include "fd.h" |
ddd3e0771 procfs: Convert /... |
18 |
static int seq_show(struct seq_file *m, void *v) |
faf60af17 procfs: Move /pro... |
19 |
{ |
faf60af17 procfs: Move /pro... |
20 |
struct files_struct *files = NULL; |
ddd3e0771 procfs: Convert /... |
21 22 23 24 25 26 27 28 29 30 |
int f_flags = 0, ret = -ENOENT; struct file *file = NULL; struct task_struct *task; task = get_proc_task(m->private); if (!task) return -ENOENT; files = get_files_struct(task); put_task_struct(task); |
faf60af17 procfs: Move /pro... |
31 |
|
faf60af17 procfs: Move /pro... |
32 |
if (files) { |
771187d61 proc: unsigned fi... |
33 |
unsigned int fd = proc_fd(m->private); |
ddd3e0771 procfs: Convert /... |
34 |
|
faf60af17 procfs: Move /pro... |
35 36 37 |
spin_lock(&files->file_lock); file = fcheck_files(files, fd); if (file) { |
ddd3e0771 procfs: Convert /... |
38 |
struct fdtable *fdt = files_fdtable(files); |
faf60af17 procfs: Move /pro... |
39 |
|
c6f3d8111 don't leak O_CLOE... |
40 |
f_flags = file->f_flags; |
faf60af17 procfs: Move /pro... |
41 42 |
if (close_on_exec(fd, fdt)) f_flags |= O_CLOEXEC; |
ddd3e0771 procfs: Convert /... |
43 44 |
get_file(file); ret = 0; |
faf60af17 procfs: Move /pro... |
45 46 47 48 |
} spin_unlock(&files->file_lock); put_files_struct(files); } |
ddd3e0771 procfs: Convert /... |
49 |
|
6c8c90319 proc: show locks ... |
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
if (ret) return ret; seq_printf(m, "pos:\t%lli flags:\t0%o mnt_id:\t%i ", (long long)file->f_pos, f_flags, real_mount(file->f_path.mnt)->mnt_id); show_fd_locks(m, file, files); if (seq_has_overflowed(m)) goto out; if (file->f_op->show_fdinfo) file->f_op->show_fdinfo(m, file); |
ddd3e0771 procfs: Convert /... |
66 |
|
6c8c90319 proc: show locks ... |
67 68 69 |
out: fput(file); return 0; |
faf60af17 procfs: Move /pro... |
70 |
} |
ddd3e0771 procfs: Convert /... |
71 72 73 74 75 76 77 78 79 80 81 |
static int seq_fdinfo_open(struct inode *inode, struct file *file) { return single_open(file, seq_show, inode); } static const struct file_operations proc_fdinfo_file_operations = { .open = seq_fdinfo_open, .read = seq_read, .llseek = seq_lseek, .release = single_release, }; |
1ae9bd8b7 proc_lookupfd_com... |
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
static bool tid_fd_mode(struct task_struct *task, unsigned fd, fmode_t *mode) { struct files_struct *files = get_files_struct(task); struct file *file; if (!files) return false; rcu_read_lock(); file = fcheck_files(files, fd); if (file) *mode = file->f_mode; rcu_read_unlock(); put_files_struct(files); return !!file; } |
988363864 don't bother with... |
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
static void tid_fd_update_inode(struct task_struct *task, struct inode *inode, fmode_t f_mode) { task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid); if (S_ISLNK(inode->i_mode)) { unsigned i_mode = S_IFLNK; if (f_mode & FMODE_READ) i_mode |= S_IRUSR | S_IXUSR; if (f_mode & FMODE_WRITE) i_mode |= S_IWUSR | S_IXUSR; inode->i_mode = i_mode; } security_task_to_inode(task, inode); } |
faf60af17 procfs: Move /pro... |
113 114 |
static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags) { |
faf60af17 procfs: Move /pro... |
115 |
struct task_struct *task; |
faf60af17 procfs: Move /pro... |
116 |
struct inode *inode; |
771187d61 proc: unsigned fi... |
117 |
unsigned int fd; |
faf60af17 procfs: Move /pro... |
118 119 120 |
if (flags & LOOKUP_RCU) return -ECHILD; |
2b0143b5c VFS: normal files... |
121 |
inode = d_inode(dentry); |
faf60af17 procfs: Move /pro... |
122 123 124 125 |
task = get_proc_task(inode); fd = proc_fd(inode); if (task) { |
988363864 don't bother with... |
126 |
fmode_t f_mode; |
1ae9bd8b7 proc_lookupfd_com... |
127 |
if (tid_fd_mode(task, fd, &f_mode)) { |
988363864 don't bother with... |
128 |
tid_fd_update_inode(task, inode, f_mode); |
1ae9bd8b7 proc_lookupfd_com... |
129 130 |
put_task_struct(task); return 1; |
faf60af17 procfs: Move /pro... |
131 132 133 |
} put_task_struct(task); } |
faf60af17 procfs: Move /pro... |
134 135 136 137 138 139 140 141 142 143 |
return 0; } static const struct dentry_operations tid_fd_dentry_operations = { .d_revalidate = tid_fd_revalidate, .d_delete = pid_delete_dentry, }; static int proc_fd_link(struct dentry *dentry, struct path *path) { |
ddd3e0771 procfs: Convert /... |
144 145 146 |
struct files_struct *files = NULL; struct task_struct *task; int ret = -ENOENT; |
2b0143b5c VFS: normal files... |
147 |
task = get_proc_task(d_inode(dentry)); |
ddd3e0771 procfs: Convert /... |
148 149 150 151 152 153 |
if (task) { files = get_files_struct(task); put_task_struct(task); } if (files) { |
771187d61 proc: unsigned fi... |
154 |
unsigned int fd = proc_fd(d_inode(dentry)); |
ddd3e0771 procfs: Convert /... |
155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
struct file *fd_file; spin_lock(&files->file_lock); fd_file = fcheck_files(files, fd); if (fd_file) { *path = fd_file->f_path; path_get(&fd_file->f_path); ret = 0; } spin_unlock(&files->file_lock); put_files_struct(files); } return ret; |
faf60af17 procfs: Move /pro... |
169 |
} |
988363864 don't bother with... |
170 171 172 173 |
struct fd_data { fmode_t mode; unsigned fd; }; |
0168b9e38 procfs: switch in... |
174 175 |
static struct dentry *proc_fd_instantiate(struct dentry *dentry, struct task_struct *task, const void *ptr) |
faf60af17 procfs: Move /pro... |
176 |
{ |
988363864 don't bother with... |
177 |
const struct fd_data *data = ptr; |
faf60af17 procfs: Move /pro... |
178 179 |
struct proc_inode *ei; struct inode *inode; |
0168b9e38 procfs: switch in... |
180 |
inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK); |
faf60af17 procfs: Move /pro... |
181 |
if (!inode) |
0168b9e38 procfs: switch in... |
182 |
return ERR_PTR(-ENOENT); |
faf60af17 procfs: Move /pro... |
183 184 |
ei = PROC_I(inode); |
988363864 don't bother with... |
185 |
ei->fd = data->fd; |
faf60af17 procfs: Move /pro... |
186 |
|
faf60af17 procfs: Move /pro... |
187 188 189 190 |
inode->i_op = &proc_pid_link_inode_operations; inode->i_size = 64; ei->op.proc_get_link = proc_fd_link; |
988363864 don't bother with... |
191 |
tid_fd_update_inode(task, inode, data->mode); |
faf60af17 procfs: Move /pro... |
192 193 |
d_set_d_op(dentry, &tid_fd_dentry_operations); |
0168b9e38 procfs: switch in... |
194 |
return d_splice_alias(inode, dentry); |
faf60af17 procfs: Move /pro... |
195 196 197 198 199 200 201 |
} static struct dentry *proc_lookupfd_common(struct inode *dir, struct dentry *dentry, instantiate_t instantiate) { struct task_struct *task = get_proc_task(dir); |
988363864 don't bother with... |
202 |
struct fd_data data = {.fd = name_to_int(&dentry->d_name)}; |
0168b9e38 procfs: switch in... |
203 |
struct dentry *result = ERR_PTR(-ENOENT); |
faf60af17 procfs: Move /pro... |
204 205 206 |
if (!task) goto out_no_task; |
988363864 don't bother with... |
207 |
if (data.fd == ~0U) |
faf60af17 procfs: Move /pro... |
208 |
goto out; |
988363864 don't bother with... |
209 |
if (!tid_fd_mode(task, data.fd, &data.mode)) |
1ae9bd8b7 proc_lookupfd_com... |
210 |
goto out; |
faf60af17 procfs: Move /pro... |
211 |
|
0168b9e38 procfs: switch in... |
212 |
result = instantiate(dentry, task, &data); |
faf60af17 procfs: Move /pro... |
213 214 215 |
out: put_task_struct(task); out_no_task: |
0168b9e38 procfs: switch in... |
216 |
return result; |
faf60af17 procfs: Move /pro... |
217 |
} |
f0c3b5093 [readdir] convert... |
218 219 |
static int proc_readfd_common(struct file *file, struct dir_context *ctx, instantiate_t instantiate) |
faf60af17 procfs: Move /pro... |
220 |
{ |
f0c3b5093 [readdir] convert... |
221 |
struct task_struct *p = get_proc_task(file_inode(file)); |
faf60af17 procfs: Move /pro... |
222 |
struct files_struct *files; |
f0c3b5093 [readdir] convert... |
223 |
unsigned int fd; |
faf60af17 procfs: Move /pro... |
224 |
|
faf60af17 procfs: Move /pro... |
225 |
if (!p) |
f0c3b5093 [readdir] convert... |
226 |
return -ENOENT; |
faf60af17 procfs: Move /pro... |
227 |
|
f0c3b5093 [readdir] convert... |
228 229 |
if (!dir_emit_dots(file, ctx)) goto out; |
f0c3b5093 [readdir] convert... |
230 231 232 233 234 235 236 237 |
files = get_files_struct(p); if (!files) goto out; rcu_read_lock(); for (fd = ctx->pos - 2; fd < files_fdtable(files)->max_fds; fd++, ctx->pos++) { |
988363864 don't bother with... |
238 239 |
struct file *f; struct fd_data data; |
e3912ac37 proc: use %u for ... |
240 |
char name[10 + 1]; |
a4ef38956 proc: use "unsign... |
241 |
unsigned int len; |
f0c3b5093 [readdir] convert... |
242 |
|
988363864 don't bother with... |
243 244 |
f = fcheck_files(files, fd); if (!f) |
f0c3b5093 [readdir] convert... |
245 |
continue; |
988363864 don't bother with... |
246 |
data.mode = f->f_mode; |
f0c3b5093 [readdir] convert... |
247 |
rcu_read_unlock(); |
988363864 don't bother with... |
248 |
data.fd = fd; |
f0c3b5093 [readdir] convert... |
249 |
|
771187d61 proc: unsigned fi... |
250 |
len = snprintf(name, sizeof(name), "%u", fd); |
f0c3b5093 [readdir] convert... |
251 252 |
if (!proc_fill_cache(file, ctx, name, len, instantiate, p, |
988363864 don't bother with... |
253 |
&data)) |
f0c3b5093 [readdir] convert... |
254 |
goto out_fd_loop; |
3cc4a84e0 proc: add a resch... |
255 |
cond_resched(); |
f0c3b5093 [readdir] convert... |
256 |
rcu_read_lock(); |
faf60af17 procfs: Move /pro... |
257 |
} |
f0c3b5093 [readdir] convert... |
258 259 260 |
rcu_read_unlock(); out_fd_loop: put_files_struct(files); |
faf60af17 procfs: Move /pro... |
261 262 |
out: put_task_struct(p); |
f0c3b5093 [readdir] convert... |
263 |
return 0; |
faf60af17 procfs: Move /pro... |
264 |
} |
f0c3b5093 [readdir] convert... |
265 |
static int proc_readfd(struct file *file, struct dir_context *ctx) |
faf60af17 procfs: Move /pro... |
266 |
{ |
f0c3b5093 [readdir] convert... |
267 |
return proc_readfd_common(file, ctx, proc_fd_instantiate); |
faf60af17 procfs: Move /pro... |
268 269 270 271 |
} const struct file_operations proc_fd_operations = { .read = generic_read_dir, |
f50752eaa switch all procfs... |
272 273 |
.iterate_shared = proc_readfd, .llseek = generic_file_llseek, |
faf60af17 procfs: Move /pro... |
274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
}; static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry, unsigned int flags) { return proc_lookupfd_common(dir, dentry, proc_fd_instantiate); } /* * /proc/pid/fd needs a special permission handler so that a process can still * access /proc/self/fd after it has executed a setuid(). */ int proc_fd_permission(struct inode *inode, int mask) { |
54708d285 proc: actually ma... |
288 289 290 291 |
struct task_struct *p; int rv; rv = generic_permission(inode, mask); |
faf60af17 procfs: Move /pro... |
292 |
if (rv == 0) |
54708d285 proc: actually ma... |
293 294 295 296 297 |
return rv; rcu_read_lock(); p = pid_task(proc_pid(inode), PIDTYPE_PID); if (p && same_thread_group(p, current)) |
faf60af17 procfs: Move /pro... |
298 |
rv = 0; |
54708d285 proc: actually ma... |
299 |
rcu_read_unlock(); |
faf60af17 procfs: Move /pro... |
300 301 302 303 304 305 306 307 |
return rv; } const struct inode_operations proc_fd_inode_operations = { .lookup = proc_lookupfd, .permission = proc_fd_permission, .setattr = proc_setattr, }; |
0168b9e38 procfs: switch in... |
308 309 |
static struct dentry *proc_fdinfo_instantiate(struct dentry *dentry, struct task_struct *task, const void *ptr) |
faf60af17 procfs: Move /pro... |
310 |
{ |
988363864 don't bother with... |
311 |
const struct fd_data *data = ptr; |
faf60af17 procfs: Move /pro... |
312 313 |
struct proc_inode *ei; struct inode *inode; |
0168b9e38 procfs: switch in... |
314 |
inode = proc_pid_make_inode(dentry->d_sb, task, S_IFREG | S_IRUSR); |
faf60af17 procfs: Move /pro... |
315 |
if (!inode) |
0168b9e38 procfs: switch in... |
316 |
return ERR_PTR(-ENOENT); |
faf60af17 procfs: Move /pro... |
317 318 |
ei = PROC_I(inode); |
988363864 don't bother with... |
319 |
ei->fd = data->fd; |
faf60af17 procfs: Move /pro... |
320 |
|
faf60af17 procfs: Move /pro... |
321 |
inode->i_fop = &proc_fdinfo_file_operations; |
988363864 don't bother with... |
322 |
tid_fd_update_inode(task, inode, 0); |
faf60af17 procfs: Move /pro... |
323 324 |
d_set_d_op(dentry, &tid_fd_dentry_operations); |
0168b9e38 procfs: switch in... |
325 |
return d_splice_alias(inode, dentry); |
faf60af17 procfs: Move /pro... |
326 327 328 329 330 331 332 |
} static struct dentry * proc_lookupfdinfo(struct inode *dir, struct dentry *dentry, unsigned int flags) { return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate); } |
f0c3b5093 [readdir] convert... |
333 |
static int proc_readfdinfo(struct file *file, struct dir_context *ctx) |
faf60af17 procfs: Move /pro... |
334 |
{ |
f0c3b5093 [readdir] convert... |
335 |
return proc_readfd_common(file, ctx, |
faf60af17 procfs: Move /pro... |
336 337 338 339 340 341 342 343 344 345 |
proc_fdinfo_instantiate); } const struct inode_operations proc_fdinfo_inode_operations = { .lookup = proc_lookupfdinfo, .setattr = proc_setattr, }; const struct file_operations proc_fdinfo_operations = { .read = generic_read_dir, |
f50752eaa switch all procfs... |
346 347 |
.iterate_shared = proc_readfdinfo, .llseek = generic_file_llseek, |
faf60af17 procfs: Move /pro... |
348 |
}; |