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>
10 #include <linux/proc_fs.h>
15 #define PROC_FDINFO_MAX 64
17 static int proc_fd_info(struct inode *inode, struct path *path, char *info)
19 struct task_struct *task = get_proc_task(inode);
20 struct files_struct *files = NULL;
21 int fd = proc_fd(inode);
25 files = get_files_struct(task);
26 put_task_struct(task);
30 * We are not taking a ref to the file structure, so we must
33 spin_lock(&files->file_lock);
34 file = fcheck_files(files, fd);
39 fdt = files_fdtable(files);
40 f_flags = file->f_flags & ~O_CLOEXEC;
41 if (close_on_exec(fd, fdt))
46 path_get(&file->f_path);
49 snprintf(info, PROC_FDINFO_MAX,
52 (long long) file->f_pos,
54 spin_unlock(&files->file_lock);
55 put_files_struct(files);
58 spin_unlock(&files->file_lock);
59 put_files_struct(files);
64 static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags)
66 struct files_struct *files;
67 struct task_struct *task;
68 const struct cred *cred;
72 if (flags & LOOKUP_RCU)
75 inode = dentry->d_inode;
76 task = get_proc_task(inode);
80 files = get_files_struct(task);
85 file = fcheck_files(files, fd);
87 unsigned f_mode = file->f_mode;
90 put_files_struct(files);
92 if (task_dumpable(task)) {
94 cred = __task_cred(task);
95 inode->i_uid = cred->euid;
96 inode->i_gid = cred->egid;
99 inode->i_uid = GLOBAL_ROOT_UID;
100 inode->i_gid = GLOBAL_ROOT_GID;
103 if (S_ISLNK(inode->i_mode)) {
104 unsigned i_mode = S_IFLNK;
105 if (f_mode & FMODE_READ)
106 i_mode |= S_IRUSR | S_IXUSR;
107 if (f_mode & FMODE_WRITE)
108 i_mode |= S_IWUSR | S_IXUSR;
109 inode->i_mode = i_mode;
112 security_task_to_inode(task, inode);
113 put_task_struct(task);
117 put_files_struct(files);
119 put_task_struct(task);
126 static const struct dentry_operations tid_fd_dentry_operations = {
127 .d_revalidate = tid_fd_revalidate,
128 .d_delete = pid_delete_dentry,
131 static int proc_fd_link(struct dentry *dentry, struct path *path)
133 return proc_fd_info(dentry->d_inode, path, NULL);
136 static struct dentry *
137 proc_fd_instantiate(struct inode *dir, struct dentry *dentry,
138 struct task_struct *task, const void *ptr)
140 struct dentry *error = ERR_PTR(-ENOENT);
141 unsigned fd = (unsigned long)ptr;
142 struct proc_inode *ei;
145 inode = proc_pid_make_inode(dir->i_sb, task);
152 inode->i_mode = S_IFLNK;
153 inode->i_op = &proc_pid_link_inode_operations;
156 ei->op.proc_get_link = proc_fd_link;
158 d_set_d_op(dentry, &tid_fd_dentry_operations);
159 d_add(dentry, inode);
161 /* Close the race of the process dying before we return the dentry */
162 if (tid_fd_revalidate(dentry, 0))
168 static struct dentry *proc_lookupfd_common(struct inode *dir,
169 struct dentry *dentry,
170 instantiate_t instantiate)
172 struct task_struct *task = get_proc_task(dir);
173 struct dentry *result = ERR_PTR(-ENOENT);
174 unsigned fd = name_to_int(dentry);
181 result = instantiate(dir, dentry, task, (void *)(unsigned long)fd);
183 put_task_struct(task);
188 static int proc_readfd_common(struct file * filp, void * dirent,
189 filldir_t filldir, instantiate_t instantiate)
191 struct dentry *dentry = filp->f_path.dentry;
192 struct inode *inode = dentry->d_inode;
193 struct task_struct *p = get_proc_task(inode);
194 struct files_struct *files;
195 unsigned int fd, ino;
206 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
210 ino = parent_ino(dentry);
211 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
215 files = get_files_struct(p);
219 for (fd = filp->f_pos - 2;
220 fd < files_fdtable(files)->max_fds;
221 fd++, filp->f_pos++) {
222 char name[PROC_NUMBUF];
226 if (!fcheck_files(files, fd))
230 len = snprintf(name, sizeof(name), "%d", fd);
231 rv = proc_fill_cache(filp, dirent, filldir,
232 name, len, instantiate, p,
233 (void *)(unsigned long)fd);
240 put_files_struct(files);
248 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
249 size_t len, loff_t *ppos)
251 char tmp[PROC_FDINFO_MAX];
252 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
254 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
258 static const struct file_operations proc_fdinfo_file_operations = {
259 .open = nonseekable_open,
260 .read = proc_fdinfo_read,
264 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
266 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
269 const struct file_operations proc_fd_operations = {
270 .read = generic_read_dir,
271 .readdir = 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_pid(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,
301 static struct dentry *
302 proc_fdinfo_instantiate(struct inode *dir, struct dentry *dentry,
303 struct task_struct *task, const void *ptr)
305 struct dentry *error = ERR_PTR(-ENOENT);
306 unsigned fd = (unsigned long)ptr;
307 struct proc_inode *ei;
310 inode = proc_pid_make_inode(dir->i_sb, task);
317 inode->i_mode = S_IFREG | S_IRUSR;
318 inode->i_fop = &proc_fdinfo_file_operations;
320 d_set_d_op(dentry, &tid_fd_dentry_operations);
321 d_add(dentry, inode);
323 /* Close the race of the process dying before we return the dentry */
324 if (tid_fd_revalidate(dentry, 0))
330 static struct dentry *
331 proc_lookupfdinfo(struct inode *dir, struct dentry *dentry, unsigned int flags)
333 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
336 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
338 return proc_readfd_common(filp, dirent, filldir,
339 proc_fdinfo_instantiate);
342 const struct inode_operations proc_fdinfo_inode_operations = {
343 .lookup = proc_lookupfdinfo,
344 .setattr = proc_setattr,
347 const struct file_operations proc_fdinfo_operations = {
348 .read = generic_read_dir,
349 .readdir = proc_readfdinfo,
350 .llseek = default_llseek,