fs: take i_mutex during prepare_binprm for set[ug]id executables
[linux/fpc-iii.git] / fs / proc / fd.c
blob8e5ad83b629aad2ebe8f1d1bc01c764147f69be1
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>
9 #include <linux/file.h>
10 #include <linux/seq_file.h>
12 #include <linux/proc_fs.h>
14 #include "../mount.h"
15 #include "internal.h"
16 #include "fd.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);
26 if (!task)
27 return -ENOENT;
29 files = get_files_struct(task);
30 put_task_struct(task);
32 if (files) {
33 int fd = proc_fd(m->private);
35 spin_lock(&files->file_lock);
36 file = fcheck_files(files, fd);
37 if (file) {
38 struct fdtable *fdt = files_fdtable(files);
40 f_flags = file->f_flags;
41 if (close_on_exec(fd, fdt))
42 f_flags |= O_CLOEXEC;
44 get_file(file);
45 ret = 0;
47 spin_unlock(&files->file_lock);
48 put_files_struct(files);
51 if (!ret) {
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);
58 fput(file);
61 return ret;
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,
71 .read = seq_read,
72 .llseek = seq_lseek,
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;
81 struct inode *inode;
82 int fd;
84 if (flags & LOOKUP_RCU)
85 return -ECHILD;
87 inode = dentry->d_inode;
88 task = get_proc_task(inode);
89 fd = proc_fd(inode);
91 if (task) {
92 files = get_files_struct(task);
93 if (files) {
94 struct file *file;
96 rcu_read_lock();
97 file = fcheck_files(files, fd);
98 if (file) {
99 unsigned f_mode = file->f_mode;
101 rcu_read_unlock();
102 put_files_struct(files);
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;
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);
126 return 1;
128 rcu_read_unlock();
129 put_files_struct(files);
131 put_task_struct(task);
133 return 0;
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;
145 int ret = -ENOENT;
147 task = get_proc_task(dentry->d_inode);
148 if (task) {
149 files = get_files_struct(task);
150 put_task_struct(task);
153 if (files) {
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);
159 if (fd_file) {
160 *path = fd_file->f_path;
161 path_get(&fd_file->f_path);
162 ret = 0;
164 spin_unlock(&files->file_lock);
165 put_files_struct(files);
168 return ret;
171 static int
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;
177 struct inode *inode;
179 inode = proc_pid_make_inode(dir->i_sb, task);
180 if (!inode)
181 goto out;
183 ei = PROC_I(inode);
184 ei->fd = fd;
186 inode->i_mode = S_IFLNK;
187 inode->i_op = &proc_pid_link_inode_operations;
188 inode->i_size = 64;
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))
197 return 0;
198 out:
199 return -ENOENT;
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);
210 if (!task)
211 goto out_no_task;
212 if (fd == ~0U)
213 goto out;
215 result = instantiate(dir, dentry, task, (void *)(unsigned long)fd);
216 out:
217 put_task_struct(task);
218 out_no_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;
227 unsigned int fd;
229 if (!p)
230 return -ENOENT;
232 if (!dir_emit_dots(file, ctx))
233 goto out;
234 files = get_files_struct(p);
235 if (!files)
236 goto out;
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;
245 if (!fcheck_files(files, fd))
246 continue;
247 rcu_read_unlock();
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();
256 rcu_read_unlock();
257 out_fd_loop:
258 put_files_struct(files);
259 out:
260 put_task_struct(p);
261 return 0;
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,
276 unsigned int flags)
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);
288 if (rv == 0)
289 return 0;
290 if (task_tgid(current) == proc_pid(inode))
291 rv = 0;
292 return rv;
295 const struct inode_operations proc_fd_inode_operations = {
296 .lookup = proc_lookupfd,
297 .permission = proc_fd_permission,
298 .setattr = proc_setattr,
301 static int
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;
307 struct inode *inode;
309 inode = proc_pid_make_inode(dir->i_sb, task);
310 if (!inode)
311 goto out;
313 ei = PROC_I(inode);
314 ei->fd = fd;
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))
324 return 0;
325 out:
326 return -ENOENT;
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,