Blame view
fs/proc/fd.c
7.5 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, }; |
faf60af17 procfs: Move /pro... |
82 83 84 85 |
static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags) { struct files_struct *files; struct task_struct *task; |
faf60af17 procfs: Move /pro... |
86 |
struct inode *inode; |
771187d61 proc: unsigned fi... |
87 |
unsigned int fd; |
faf60af17 procfs: Move /pro... |
88 89 90 |
if (flags & LOOKUP_RCU) return -ECHILD; |
2b0143b5c VFS: normal files... |
91 |
inode = d_inode(dentry); |
faf60af17 procfs: Move /pro... |
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
task = get_proc_task(inode); fd = proc_fd(inode); if (task) { files = get_files_struct(task); if (files) { struct file *file; rcu_read_lock(); file = fcheck_files(files, fd); if (file) { unsigned f_mode = file->f_mode; rcu_read_unlock(); put_files_struct(files); |
68eb94f16 proc: Better owne... |
107 |
task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid); |
faf60af17 procfs: Move /pro... |
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
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); put_task_struct(task); return 1; } rcu_read_unlock(); put_files_struct(files); } put_task_struct(task); } |
faf60af17 procfs: Move /pro... |
127 128 129 130 131 132 133 134 135 136 |
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 /... |
137 138 139 |
struct files_struct *files = NULL; struct task_struct *task; int ret = -ENOENT; |
2b0143b5c VFS: normal files... |
140 |
task = get_proc_task(d_inode(dentry)); |
ddd3e0771 procfs: Convert /... |
141 142 143 144 145 146 |
if (task) { files = get_files_struct(task); put_task_struct(task); } if (files) { |
771187d61 proc: unsigned fi... |
147 |
unsigned int fd = proc_fd(d_inode(dentry)); |
ddd3e0771 procfs: Convert /... |
148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
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... |
162 |
} |
c52a47ace proc_fill_cache()... |
163 |
static int |
faf60af17 procfs: Move /pro... |
164 165 166 |
proc_fd_instantiate(struct inode *dir, struct dentry *dentry, struct task_struct *task, const void *ptr) { |
faf60af17 procfs: Move /pro... |
167 168 169 |
unsigned fd = (unsigned long)ptr; struct proc_inode *ei; struct inode *inode; |
db978da8f proc: Pass file m... |
170 |
inode = proc_pid_make_inode(dir->i_sb, task, S_IFLNK); |
faf60af17 procfs: Move /pro... |
171 172 173 174 175 |
if (!inode) goto out; ei = PROC_I(inode); ei->fd = fd; |
faf60af17 procfs: Move /pro... |
176 177 178 179 180 181 182 183 184 185 |
inode->i_op = &proc_pid_link_inode_operations; inode->i_size = 64; ei->op.proc_get_link = proc_fd_link; d_set_d_op(dentry, &tid_fd_dentry_operations); d_add(dentry, inode); /* Close the race of the process dying before we return the dentry */ if (tid_fd_revalidate(dentry, 0)) |
c52a47ace proc_fill_cache()... |
186 |
return 0; |
faf60af17 procfs: Move /pro... |
187 |
out: |
c52a47ace proc_fill_cache()... |
188 |
return -ENOENT; |
faf60af17 procfs: Move /pro... |
189 190 191 192 193 194 195 |
} static struct dentry *proc_lookupfd_common(struct inode *dir, struct dentry *dentry, instantiate_t instantiate) { struct task_struct *task = get_proc_task(dir); |
c52a47ace proc_fill_cache()... |
196 |
int result = -ENOENT; |
dbcdb5044 proc: add and rem... |
197 |
unsigned fd = name_to_int(&dentry->d_name); |
faf60af17 procfs: Move /pro... |
198 199 200 201 202 203 204 205 206 207 |
if (!task) goto out_no_task; if (fd == ~0U) goto out; result = instantiate(dir, dentry, task, (void *)(unsigned long)fd); out: put_task_struct(task); out_no_task: |
c52a47ace proc_fill_cache()... |
208 |
return ERR_PTR(result); |
faf60af17 procfs: Move /pro... |
209 |
} |
f0c3b5093 [readdir] convert... |
210 211 |
static int proc_readfd_common(struct file *file, struct dir_context *ctx, instantiate_t instantiate) |
faf60af17 procfs: Move /pro... |
212 |
{ |
f0c3b5093 [readdir] convert... |
213 |
struct task_struct *p = get_proc_task(file_inode(file)); |
faf60af17 procfs: Move /pro... |
214 |
struct files_struct *files; |
f0c3b5093 [readdir] convert... |
215 |
unsigned int fd; |
faf60af17 procfs: Move /pro... |
216 |
|
faf60af17 procfs: Move /pro... |
217 |
if (!p) |
f0c3b5093 [readdir] convert... |
218 |
return -ENOENT; |
faf60af17 procfs: Move /pro... |
219 |
|
f0c3b5093 [readdir] convert... |
220 221 |
if (!dir_emit_dots(file, ctx)) goto out; |
f0c3b5093 [readdir] convert... |
222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
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++) { char name[PROC_NUMBUF]; int len; if (!fcheck_files(files, fd)) continue; rcu_read_unlock(); |
771187d61 proc: unsigned fi... |
236 |
len = snprintf(name, sizeof(name), "%u", fd); |
f0c3b5093 [readdir] convert... |
237 238 239 240 |
if (!proc_fill_cache(file, ctx, name, len, instantiate, p, (void *)(unsigned long)fd)) goto out_fd_loop; |
3cc4a84e0 proc: add a resch... |
241 |
cond_resched(); |
f0c3b5093 [readdir] convert... |
242 |
rcu_read_lock(); |
faf60af17 procfs: Move /pro... |
243 |
} |
f0c3b5093 [readdir] convert... |
244 245 246 |
rcu_read_unlock(); out_fd_loop: put_files_struct(files); |
faf60af17 procfs: Move /pro... |
247 248 |
out: put_task_struct(p); |
f0c3b5093 [readdir] convert... |
249 |
return 0; |
faf60af17 procfs: Move /pro... |
250 |
} |
f0c3b5093 [readdir] convert... |
251 |
static int proc_readfd(struct file *file, struct dir_context *ctx) |
faf60af17 procfs: Move /pro... |
252 |
{ |
f0c3b5093 [readdir] convert... |
253 |
return proc_readfd_common(file, ctx, proc_fd_instantiate); |
faf60af17 procfs: Move /pro... |
254 255 256 257 |
} const struct file_operations proc_fd_operations = { .read = generic_read_dir, |
f50752eaa switch all procfs... |
258 259 |
.iterate_shared = proc_readfd, .llseek = generic_file_llseek, |
faf60af17 procfs: Move /pro... |
260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
}; 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... |
274 275 276 277 |
struct task_struct *p; int rv; rv = generic_permission(inode, mask); |
faf60af17 procfs: Move /pro... |
278 |
if (rv == 0) |
54708d285 proc: actually ma... |
279 280 281 282 283 |
return rv; rcu_read_lock(); p = pid_task(proc_pid(inode), PIDTYPE_PID); if (p && same_thread_group(p, current)) |
faf60af17 procfs: Move /pro... |
284 |
rv = 0; |
54708d285 proc: actually ma... |
285 |
rcu_read_unlock(); |
faf60af17 procfs: Move /pro... |
286 287 288 289 290 291 292 293 |
return rv; } const struct inode_operations proc_fd_inode_operations = { .lookup = proc_lookupfd, .permission = proc_fd_permission, .setattr = proc_setattr, }; |
c52a47ace proc_fill_cache()... |
294 |
static int |
faf60af17 procfs: Move /pro... |
295 296 297 |
proc_fdinfo_instantiate(struct inode *dir, struct dentry *dentry, struct task_struct *task, const void *ptr) { |
faf60af17 procfs: Move /pro... |
298 299 300 |
unsigned fd = (unsigned long)ptr; struct proc_inode *ei; struct inode *inode; |
db978da8f proc: Pass file m... |
301 |
inode = proc_pid_make_inode(dir->i_sb, task, S_IFREG | S_IRUSR); |
faf60af17 procfs: Move /pro... |
302 303 304 305 306 |
if (!inode) goto out; ei = PROC_I(inode); ei->fd = fd; |
faf60af17 procfs: Move /pro... |
307 308 309 310 311 312 313 |
inode->i_fop = &proc_fdinfo_file_operations; d_set_d_op(dentry, &tid_fd_dentry_operations); d_add(dentry, inode); /* Close the race of the process dying before we return the dentry */ if (tid_fd_revalidate(dentry, 0)) |
c52a47ace proc_fill_cache()... |
314 |
return 0; |
faf60af17 procfs: Move /pro... |
315 |
out: |
c52a47ace proc_fill_cache()... |
316 |
return -ENOENT; |
faf60af17 procfs: Move /pro... |
317 318 319 320 321 322 323 |
} 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... |
324 |
static int proc_readfdinfo(struct file *file, struct dir_context *ctx) |
faf60af17 procfs: Move /pro... |
325 |
{ |
f0c3b5093 [readdir] convert... |
326 |
return proc_readfd_common(file, ctx, |
faf60af17 procfs: Move /pro... |
327 328 329 330 331 332 333 334 335 336 |
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... |
337 338 |
.iterate_shared = proc_readfdinfo, .llseek = generic_file_llseek, |
faf60af17 procfs: Move /pro... |
339 |
}; |