2 * linux/fs/proc/inode.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/sched.h>
8 #include <linux/proc_fs.h>
9 #include <linux/kernel.h>
11 #include <linux/string.h>
12 #include <linux/stat.h>
13 #include <linux/file.h>
14 #include <linux/locks.h>
15 #include <linux/limits.h>
16 #include <linux/config.h>
18 #include <asm/system.h>
19 #include <asm/uaccess.h>
21 extern void free_proc_entry(struct proc_dir_entry
*);
23 struct proc_dir_entry
* de_get(struct proc_dir_entry
*de
)
31 * Decrements the use count and checks for deferred deletion.
33 void de_put(struct proc_dir_entry
*de
)
37 printk("de_put: entry %s already free!\n", de
->name
);
43 printk("de_put: deferred delete of %s\n",
51 static void proc_put_inode(struct inode
*inode
)
53 #ifdef CONFIG_SUN_OPENPROMFS_MODULE
54 if ((inode
->i_ino
>= PROC_OPENPROM_FIRST
) &&
55 (inode
->i_ino
< PROC_OPENPROM_FIRST
+ PROC_NOPENPROM
) &&
57 (*proc_openprom_use
)(inode
, 0);
60 * Kill off unused inodes ... VFS will unhash and
61 * delete the inode if we set i_nlink to zero.
63 if (inode
->i_count
== 1)
68 * Decrement the use count of the proc_dir_entry.
70 static void proc_delete_inode(struct inode
*inode
)
72 struct proc_dir_entry
*de
= inode
->u
.generic_ip
;
74 #if defined(CONFIG_SUN_OPENPROMFS) || defined(CONFIG_SUN_OPENPROMFS_MODULE)
75 if ((inode
->i_ino
>= PROC_OPENPROM_FIRST
) &&
76 (inode
->i_ino
< PROC_OPENPROM_FIRST
+ PROC_NOPENPROM
))
82 * Call the fill_inode hook to release module counts.
85 de
->fill_inode(inode
, 0);
90 struct super_block
*proc_super_blocks
= NULL
;
92 static void proc_put_super(struct super_block
*sb
)
94 struct super_block
**p
= &proc_super_blocks
;
96 if (!*p
) /* should never happen */
98 p
= (struct super_block
**)&(*p
)->u
.generic_sbp
;
100 *p
= (struct super_block
*)(*p
)->u
.generic_sbp
;
103 static struct super_operations proc_sops
= {
107 proc_delete_inode
, /* delete_inode(struct inode *) */
116 static int parse_options(char *options
,uid_t
*uid
,gid_t
*gid
)
118 char *this_char
,*value
;
122 if (!options
) return 1;
123 for (this_char
= strtok(options
,","); this_char
; this_char
= strtok(NULL
,",")) {
124 if ((value
= strchr(this_char
,'=')) != NULL
)
126 if (!strcmp(this_char
,"uid")) {
127 if (!value
|| !*value
)
129 *uid
= simple_strtoul(value
,&value
,0);
133 else if (!strcmp(this_char
,"gid")) {
134 if (!value
|| !*value
)
136 *gid
= simple_strtoul(value
,&value
,0);
146 * The standard rules, copied from fs/namei.c:permission().
148 static int standard_permission(struct inode
*inode
, int mask
)
150 int mode
= inode
->i_mode
;
152 if ((mask
& S_IWOTH
) && IS_RDONLY(inode
) &&
153 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
)))
154 return -EROFS
; /* Nobody gets write access to a read-only fs */
155 else if ((mask
& S_IWOTH
) && IS_IMMUTABLE(inode
))
156 return -EACCES
; /* Nobody gets write access to an immutable file */
157 else if (current
->fsuid
== inode
->i_uid
)
159 else if (in_group_p(inode
->i_gid
))
161 if (((mode
& mask
& S_IRWXO
) == mask
) || capable(CAP_DAC_OVERRIDE
))
163 /* read and search access */
164 if ((mask
== S_IROTH
) ||
165 (S_ISDIR(mode
) && !(mask
& ~(S_IROTH
| S_IXOTH
))))
166 if (capable(CAP_DAC_READ_SEARCH
))
172 * Set up permission rules for processes looking at other processes.
173 * You're not allowed to see a process unless it has the same or more
174 * restricted root than your own. This prevents a chrooted processes
175 * from escaping through the /proc entries of less restricted
176 * processes, and thus allows /proc to be safely mounted in a chrooted
179 * Note that root (uid 0) doesn't get permission for this either,
180 * since chroot is stronger than root.
182 * XXX TODO: use the dentry mechanism to make off-limits procs simply
183 * invisible rather than denied? Does each namespace root get its own
186 * This also applies the default permissions checks, as it only adds
189 * Jeremy Fitzhardinge <jeremy@zip.com.au>
191 int proc_permission(struct inode
*inode
, int mask
)
193 struct task_struct
*p
;
194 unsigned long ino
= inode
->i_ino
;
196 struct dentry
*de
, *base
;
198 if (standard_permission(inode
, mask
) != 0)
202 * Find the root of the processes being examined (if any).
203 * XXX Surely there's a better way of doing this?
205 if (ino
>= PROC_OPENPROM_FIRST
&&
206 ino
< PROC_OPENPROM_FIRST
+ PROC_NOPENPROM
)
207 return 0; /* already allowed */
211 return 0; /* already allowed */
214 base
= current
->fs
->root
;
216 read_lock(&tasklist_lock
);
217 p
= find_task_by_pid(pid
);
221 read_unlock(&tasklist_lock
); /* FIXME! */
224 return -EACCES
; /* ENOENT? */
228 /* kswapd and bdflush don't have proper root or cwd... */
235 struct dentry
*parent
;
238 return 0; /* already allowed */
241 parent
= de
->d_parent
;
249 return -EACCES
; /* incompatible roots */
252 struct inode
* proc_get_inode(struct super_block
* sb
, int ino
,
253 struct proc_dir_entry
* de
)
255 struct inode
* inode
;
258 * Increment the use count so the dir entry can't disappear.
262 /* shouldn't ever happen */
263 if (de
&& de
->deleted
)
264 printk("proc_iget: using deleted entry %s, count=%d\n", de
->name
, de
->count
);
267 inode
= iget(sb
, ino
);
271 #ifdef CONFIG_SUN_OPENPROMFS_MODULE
272 if ((inode
->i_ino
>= PROC_OPENPROM_FIRST
)
273 && (inode
->i_ino
< PROC_OPENPROM_FIRST
+ PROC_NOPENPROM
)
274 && proc_openprom_use
)
275 (*proc_openprom_use
)(inode
, 1);
277 /* N.B. How can this test ever fail?? */
278 if (inode
->i_sb
!= sb
)
279 printk("proc_get_inode: inode fubar\n");
281 inode
->u
.generic_ip
= (void *) de
;
284 inode
->i_mode
= de
->mode
;
285 inode
->i_uid
= de
->uid
;
286 inode
->i_gid
= de
->gid
;
289 inode
->i_size
= de
->size
;
291 inode
->i_op
= de
->ops
;
293 inode
->i_nlink
= de
->nlink
;
295 * The fill_inode routine should use this call
296 * to increment module counts, if necessary.
299 de
->fill_inode(inode
, 1);
302 * Fixup the root inode's nlink value
304 if (inode
->i_ino
== PROC_ROOT_INO
) {
305 struct task_struct
*p
;
307 read_lock(&tasklist_lock
);
312 read_unlock(&tasklist_lock
);
322 struct super_block
*proc_read_super(struct super_block
*s
,void *data
,
325 struct inode
* root_inode
;
328 s
->s_blocksize
= 1024;
329 s
->s_blocksize_bits
= 10;
330 s
->s_magic
= PROC_SUPER_MAGIC
;
331 s
->s_op
= &proc_sops
;
332 root_inode
= proc_get_inode(s
, PROC_ROOT_INO
, &proc_root
);
335 s
->s_root
= d_alloc_root(root_inode
);
338 parse_options(data
, &root_inode
->i_uid
, &root_inode
->i_gid
);
339 s
->u
.generic_sbp
= (void*) proc_super_blocks
;
340 proc_super_blocks
= s
;
345 printk("proc_read_super: get root inode failed\n");
352 int proc_statfs(struct super_block
*sb
, struct statfs
*buf
, int bufsiz
)
356 tmp
.f_type
= PROC_SUPER_MAGIC
;
357 tmp
.f_bsize
= PAGE_SIZE
/sizeof(long);
363 tmp
.f_namelen
= NAME_MAX
;
364 return copy_to_user(buf
, &tmp
, bufsiz
) ? -EFAULT
: 0;
367 void proc_read_inode(struct inode
* inode
)
369 unsigned long ino
, pid
;
370 struct task_struct
* p
;
372 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
374 inode
->i_blksize
= 1024;
376 if (ino
>= PROC_OPENPROM_FIRST
&&
377 ino
< PROC_OPENPROM_FIRST
+ PROC_NOPENPROM
)
390 read_lock(&tasklist_lock
);
391 p
= find_task_by_pid(pid
);
396 if (ino
== PROC_PID_INO
|| p
->dumpable
) {
397 inode
->i_uid
= p
->euid
;
398 inode
->i_gid
= p
->egid
;
400 if (ino
& PROC_PID_FD_DIR
) {
403 if (!p
->files
) /* can we ever get here if that's the case? */
405 read_lock(&p
->files
->file_lock
);
406 file
= fcheck_task(p
, ino
);
410 inode
->i_op
= &proc_link_inode_operations
;
412 inode
->i_mode
= S_IFLNK
;
413 if (file
->f_mode
& 1)
414 inode
->i_mode
|= S_IRUSR
| S_IXUSR
;
415 if (file
->f_mode
& 2)
416 inode
->i_mode
|= S_IWUSR
| S_IXUSR
;
418 read_unlock(&p
->files
->file_lock
);
421 /* Defer unlocking until we're done with the task */
422 read_unlock(&tasklist_lock
);
428 void proc_write_inode(struct inode
* inode
)