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