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>
8 #include <linux/security.h>
9 #include <linux/file.h>
10 #include <linux/seq_file.h>
12 #include <linux/proc_fs.h>
18 static int seq_show(struct seq_file *m, void *v)
20 struct files_struct *files = NULL;
21 int f_flags = 0, ret = -ENOENT;
22 struct file *file = NULL;
23 struct task_struct *task;
25 task = get_proc_task(m->private);
29 files = get_files_struct(task);
30 put_task_struct(task);
33 int fd = proc_fd(m->private);
35 spin_lock(&files->file_lock);
36 file = fcheck_files(files, fd);
38 struct fdtable *fdt = files_fdtable(files);
40 f_flags = file->f_flags;
41 if (close_on_exec(fd, fdt))
47 spin_unlock(&files->file_lock);
48 put_files_struct(files);
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);
55 if (file->f_op->show_fdinfo)
56 file->f_op->show_fdinfo(m, file);
57 ret = seq_has_overflowed(m);
64 static int seq_fdinfo_open(struct inode *inode, struct file *file)
66 return single_open(file, seq_show, inode);
69 static const struct file_operations proc_fdinfo_file_operations = {
70 .open = seq_fdinfo_open,
73 .release = single_release,
76 static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags)
78 struct files_struct *files;
79 struct task_struct *task;
80 const struct cred *cred;
84 if (flags & LOOKUP_RCU)
87 inode = dentry->d_inode;
88 task = get_proc_task(inode);
92 files = get_files_struct(task);
97 file = fcheck_files(files, fd);
99 unsigned f_mode = file->f_mode;
102 put_files_struct(files);
104 if (task_dumpable(task)) {
106 cred = __task_cred(task);
107 inode->i_uid = cred->euid;
108 inode->i_gid = cred->egid;
111 inode->i_uid = GLOBAL_ROOT_UID;
112 inode->i_gid = GLOBAL_ROOT_GID;
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;
124 security_task_to_inode(task, inode);
125 put_task_struct(task);
129 put_files_struct(files);
131 put_task_struct(task);
136 static const struct dentry_operations tid_fd_dentry_operations = {
137 .d_revalidate = tid_fd_revalidate,
138 .d_delete = pid_delete_dentry,
141 static int proc_fd_link(struct dentry *dentry, struct path *path)
143 struct files_struct *files = NULL;
144 struct task_struct *task;
147 task = get_proc_task(dentry->d_inode);
149 files = get_files_struct(task);
150 put_task_struct(task);
154 int fd = proc_fd(dentry->d_inode);
155 struct file *fd_file;
157 spin_lock(&files->file_lock);
158 fd_file = fcheck_files(files, fd);
160 *path = fd_file->f_path;
161 path_get(&fd_file->f_path);
164 spin_unlock(&files->file_lock);
165 put_files_struct(files);
172 proc_fd_instantiate(struct inode *dir, struct dentry *dentry,
173 struct task_struct *task, const void *ptr)
175 unsigned fd = (unsigned long)ptr;
176 struct proc_inode *ei;
179 inode = proc_pid_make_inode(dir->i_sb, task);
186 inode->i_mode = S_IFLNK;
187 inode->i_op = &proc_pid_link_inode_operations;
190 ei->op.proc_get_link = proc_fd_link;
192 d_set_d_op(dentry, &tid_fd_dentry_operations);
193 d_add(dentry, inode);
195 /* Close the race of the process dying before we return the dentry */
196 if (tid_fd_revalidate(dentry, 0))
202 static struct dentry *proc_lookupfd_common(struct inode *dir,
203 struct dentry *dentry,
204 instantiate_t instantiate)
206 struct task_struct *task = get_proc_task(dir);
207 int result = -ENOENT;
208 unsigned fd = name_to_int(&dentry->d_name);
215 result = instantiate(dir, dentry, task, (void *)(unsigned long)fd);
217 put_task_struct(task);
219 return ERR_PTR(result);
222 static int proc_readfd_common(struct file *file, struct dir_context *ctx,
223 instantiate_t instantiate)
225 struct task_struct *p = get_proc_task(file_inode(file));
226 struct files_struct *files;
232 if (!dir_emit_dots(file, ctx))
234 files = get_files_struct(p);
239 for (fd = ctx->pos - 2;
240 fd < files_fdtable(files)->max_fds;
242 char name[PROC_NUMBUF];
245 if (!fcheck_files(files, fd))
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))
258 put_files_struct(files);
264 static int proc_readfd(struct file *file, struct dir_context *ctx)
266 return proc_readfd_common(file, ctx, proc_fd_instantiate);
269 const struct file_operations proc_fd_operations = {
270 .read = generic_read_dir,
271 .iterate = proc_readfd,
272 .llseek = default_llseek,
275 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
278 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
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().
285 int proc_fd_permission(struct inode *inode, int mask)
287 int rv = generic_permission(inode, mask);
290 if (task_tgid(current) == proc_pid(inode))
295 const struct inode_operations proc_fd_inode_operations = {
296 .lookup = proc_lookupfd,
297 .permission = proc_fd_permission,
298 .setattr = proc_setattr,
302 proc_fdinfo_instantiate(struct inode *dir, struct dentry *dentry,
303 struct task_struct *task, const void *ptr)
305 unsigned fd = (unsigned long)ptr;
306 struct proc_inode *ei;
309 inode = proc_pid_make_inode(dir->i_sb, task);
316 inode->i_mode = S_IFREG | S_IRUSR;
317 inode->i_fop = &proc_fdinfo_file_operations;
319 d_set_d_op(dentry, &tid_fd_dentry_operations);
320 d_add(dentry, inode);
322 /* Close the race of the process dying before we return the dentry */
323 if (tid_fd_revalidate(dentry, 0))
329 static struct dentry *
330 proc_lookupfdinfo(struct inode *dir, struct dentry *dentry, unsigned int flags)
332 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
335 static int proc_readfdinfo(struct file *file, struct dir_context *ctx)
337 return proc_readfd_common(file, ctx,
338 proc_fdinfo_instantiate);
341 const struct inode_operations proc_fdinfo_inode_operations = {
342 .lookup = proc_lookupfdinfo,
343 .setattr = proc_setattr,
346 const struct file_operations proc_fdinfo_operations = {
347 .read = generic_read_dir,
348 .iterate = proc_readfdinfo,
349 .llseek = default_llseek,