blob: 8e5ad83b629aad2ebe8f1d1bc01c764147f69be1 [file] [log] [blame]
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001#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>
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +04009#include <linux/file.h>
10#include <linux/seq_file.h>
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040011
12#include <linux/proc_fs.h>
13
Andrey Vagin49d063c2014-04-07 15:38:34 -070014#include "../mount.h"
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040015#include "internal.h"
16#include "fd.h"
17
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +040018static int seq_show(struct seq_file *m, void *v)
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040019{
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040020 struct files_struct *files = NULL;
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +040021 int f_flags = 0, ret = -ENOENT;
22 struct file *file = NULL;
23 struct task_struct *task;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040024
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +040025 task = get_proc_task(m->private);
26 if (!task)
27 return -ENOENT;
28
29 files = get_files_struct(task);
30 put_task_struct(task);
31
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040032 if (files) {
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +040033 int fd = proc_fd(m->private);
34
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040035 spin_lock(&files->file_lock);
36 file = fcheck_files(files, fd);
37 if (file) {
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +040038 struct fdtable *fdt = files_fdtable(files);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040039
Al Viroc6f3d812012-08-26 11:01:04 -040040 f_flags = file->f_flags;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040041 if (close_on_exec(fd, fdt))
42 f_flags |= O_CLOEXEC;
43
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +040044 get_file(file);
45 ret = 0;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040046 }
47 spin_unlock(&files->file_lock);
48 put_files_struct(files);
49 }
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +040050
51 if (!ret) {
Andrey Vagin49d063c2014-04-07 15:38:34 -070052 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);
Cyrill Gorcunov55985dd2012-12-17 16:04:55 -080055 if (file->f_op->show_fdinfo)
Joe Perchesa3816ab2014-09-29 16:08:25 -070056 file->f_op->show_fdinfo(m, file);
57 ret = seq_has_overflowed(m);
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +040058 fput(file);
59 }
60
61 return ret;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040062}
63
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +040064static int seq_fdinfo_open(struct inode *inode, struct file *file)
65{
66 return single_open(file, seq_show, inode);
67}
68
69static 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
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040076static 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 }
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400133 return 0;
134}
135
136static const struct dentry_operations tid_fd_dentry_operations = {
137 .d_revalidate = tid_fd_revalidate,
138 .d_delete = pid_delete_dentry,
139};
140
141static int proc_fd_link(struct dentry *dentry, struct path *path)
142{
Cyrill Gorcunovddd3e072012-08-26 18:28:20 +0400143 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;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400169}
170
Al Viroc52a47a2013-06-15 11:15:20 +0400171static int
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400172proc_fd_instantiate(struct inode *dir, struct dentry *dentry,
173 struct task_struct *task, const void *ptr)
174{
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400175 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))
Al Viroc52a47a2013-06-15 11:15:20 +0400197 return 0;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400198 out:
Al Viroc52a47a2013-06-15 11:15:20 +0400199 return -ENOENT;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400200}
201
202static 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);
Al Viroc52a47a2013-06-15 11:15:20 +0400207 int result = -ENOENT;
Alexey Dobriyandbcdb502014-08-08 14:21:25 -0700208 unsigned fd = name_to_int(&dentry->d_name);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400209
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);
216out:
217 put_task_struct(task);
218out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +0400219 return ERR_PTR(result);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400220}
221
Al Virof0c3b502013-05-16 12:07:31 -0400222static int proc_readfd_common(struct file *file, struct dir_context *ctx,
223 instantiate_t instantiate)
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400224{
Al Virof0c3b502013-05-16 12:07:31 -0400225 struct task_struct *p = get_proc_task(file_inode(file));
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400226 struct files_struct *files;
Al Virof0c3b502013-05-16 12:07:31 -0400227 unsigned int fd;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400228
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400229 if (!p)
Al Virof0c3b502013-05-16 12:07:31 -0400230 return -ENOENT;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400231
Al Virof0c3b502013-05-16 12:07:31 -0400232 if (!dir_emit_dots(file, ctx))
233 goto out;
Al Virof0c3b502013-05-16 12:07:31 -0400234 files = get_files_struct(p);
235 if (!files)
236 goto out;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400237
Al Virof0c3b502013-05-16 12:07:31 -0400238 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;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400244
Al Virof0c3b502013-05-16 12:07:31 -0400245 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();
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400255 }
Al Virof0c3b502013-05-16 12:07:31 -0400256 rcu_read_unlock();
257out_fd_loop:
258 put_files_struct(files);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400259out:
260 put_task_struct(p);
Al Virof0c3b502013-05-16 12:07:31 -0400261 return 0;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400262}
263
Al Virof0c3b502013-05-16 12:07:31 -0400264static int proc_readfd(struct file *file, struct dir_context *ctx)
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400265{
Al Virof0c3b502013-05-16 12:07:31 -0400266 return proc_readfd_common(file, ctx, proc_fd_instantiate);
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400267}
268
269const struct file_operations proc_fd_operations = {
270 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -0400271 .iterate = proc_readfd,
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400272 .llseek = default_llseek,
273};
274
275static 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 */
285int proc_fd_permission(struct inode *inode, int mask)
286{
287 int rv = generic_permission(inode, mask);
288 if (rv == 0)
289 return 0;
Oleg Nesterov96d0df72013-09-11 14:24:37 -0700290 if (task_tgid(current) == proc_pid(inode))
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400291 rv = 0;
292 return rv;
293}
294
295const struct inode_operations proc_fd_inode_operations = {
296 .lookup = proc_lookupfd,
297 .permission = proc_fd_permission,
298 .setattr = proc_setattr,
299};
300
Al Viroc52a47a2013-06-15 11:15:20 +0400301static int
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400302proc_fdinfo_instantiate(struct inode *dir, struct dentry *dentry,
303 struct task_struct *task, const void *ptr)
304{
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400305 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))
Al Viroc52a47a2013-06-15 11:15:20 +0400324 return 0;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400325 out:
Al Viroc52a47a2013-06-15 11:15:20 +0400326 return -ENOENT;
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400327}
328
329static struct dentry *
330proc_lookupfdinfo(struct inode *dir, struct dentry *dentry, unsigned int flags)
331{
332 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
333}
334
Al Virof0c3b502013-05-16 12:07:31 -0400335static int proc_readfdinfo(struct file *file, struct dir_context *ctx)
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400336{
Al Virof0c3b502013-05-16 12:07:31 -0400337 return proc_readfd_common(file, ctx,
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400338 proc_fdinfo_instantiate);
339}
340
341const struct inode_operations proc_fdinfo_inode_operations = {
342 .lookup = proc_lookupfdinfo,
343 .setattr = proc_setattr,
344};
345
346const struct file_operations proc_fdinfo_operations = {
347 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -0400348 .iterate = proc_readfdinfo,
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400349 .llseek = default_llseek,
350};