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 static struct super_operations proc_sops
= {
94 proc_delete_inode
, /* delete_inode(struct inode *) */
103 static int parse_options(char *options
,uid_t
*uid
,gid_t
*gid
)
105 char *this_char
,*value
;
109 if (!options
) return 1;
110 for (this_char
= strtok(options
,","); this_char
; this_char
= strtok(NULL
,",")) {
111 if ((value
= strchr(this_char
,'=')) != NULL
)
113 if (!strcmp(this_char
,"uid")) {
114 if (!value
|| !*value
)
116 *uid
= simple_strtoul(value
,&value
,0);
120 else if (!strcmp(this_char
,"gid")) {
121 if (!value
|| !*value
)
123 *gid
= simple_strtoul(value
,&value
,0);
133 * The standard rules, copied from fs/namei.c:permission().
135 static int standard_permission(struct inode
*inode
, int mask
)
137 int mode
= inode
->i_mode
;
139 if ((mask
& S_IWOTH
) && IS_RDONLY(inode
) &&
140 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
)))
141 return -EROFS
; /* Nobody gets write access to a read-only fs */
142 else if ((mask
& S_IWOTH
) && IS_IMMUTABLE(inode
))
143 return -EACCES
; /* Nobody gets write access to an immutable file */
144 else if (current
->fsuid
== inode
->i_uid
)
146 else if (in_group_p(inode
->i_gid
))
148 if (((mode
& mask
& S_IRWXO
) == mask
) || capable(CAP_DAC_OVERRIDE
))
150 /* read and search access */
151 if ((mask
== S_IROTH
) ||
152 (S_ISDIR(mode
) && !(mask
& ~(S_IROTH
| S_IXOTH
))))
153 if (capable(CAP_DAC_READ_SEARCH
))
159 * Set up permission rules for processes looking at other processes.
160 * You're not allowed to see a process unless it has the same or more
161 * restricted root than your own. This prevents a chrooted processes
162 * from escaping through the /proc entries of less restricted
163 * processes, and thus allows /proc to be safely mounted in a chrooted
166 * Note that root (uid 0) doesn't get permission for this either,
167 * since chroot is stronger than root.
169 * XXX TODO: use the dentry mechanism to make off-limits procs simply
170 * invisible rather than denied? Does each namespace root get its own
173 * This also applies the default permissions checks, as it only adds
176 * Jeremy Fitzhardinge <jeremy@zip.com.au>
178 int proc_permission(struct inode
*inode
, int mask
)
180 struct task_struct
*p
;
181 unsigned long ino
= inode
->i_ino
;
183 struct dentry
*de
, *base
;
185 if (standard_permission(inode
, mask
) != 0)
189 * Find the root of the processes being examined (if any).
190 * XXX Surely there's a better way of doing this?
192 if (ino
>= PROC_OPENPROM_FIRST
&&
193 ino
< PROC_OPENPROM_FIRST
+ PROC_NOPENPROM
)
194 return 0; /* already allowed */
198 return 0; /* already allowed */
201 base
= current
->fs
->root
;
203 read_lock(&tasklist_lock
);
204 p
= find_task_by_pid(pid
);
208 read_unlock(&tasklist_lock
); /* FIXME! */
211 return -EACCES
; /* ENOENT? */
215 /* kswapd and bdflush don't have proper root or cwd... */
222 struct dentry
*parent
;
225 return 0; /* already allowed */
228 parent
= de
->d_parent
;
236 return -EACCES
; /* incompatible roots */
239 struct inode
* proc_get_inode(struct super_block
* sb
, int ino
,
240 struct proc_dir_entry
* de
)
242 struct inode
* inode
;
245 * Increment the use count so the dir entry can't disappear.
249 /* shouldn't ever happen */
250 if (de
&& de
->deleted
)
251 printk("proc_iget: using deleted entry %s, count=%d\n", de
->name
, de
->count
);
254 inode
= iget(sb
, ino
);
258 #ifdef CONFIG_SUN_OPENPROMFS_MODULE
259 if ((inode
->i_ino
>= PROC_OPENPROM_FIRST
)
260 && (inode
->i_ino
< PROC_OPENPROM_FIRST
+ PROC_NOPENPROM
)
261 && proc_openprom_use
)
262 (*proc_openprom_use
)(inode
, 1);
264 /* N.B. How can this test ever fail?? */
265 if (inode
->i_sb
!= sb
)
266 printk("proc_get_inode: inode fubar\n");
268 inode
->u
.generic_ip
= (void *) de
;
271 inode
->i_mode
= de
->mode
;
272 inode
->i_uid
= de
->uid
;
273 inode
->i_gid
= de
->gid
;
276 inode
->i_size
= de
->size
;
278 inode
->i_op
= de
->ops
;
280 inode
->i_nlink
= de
->nlink
;
282 * The fill_inode routine should use this call
283 * to increment module counts, if necessary.
286 de
->fill_inode(inode
, 1);
289 * Fixup the root inode's nlink value
291 if (inode
->i_ino
== PROC_ROOT_INO
) {
292 struct task_struct
*p
;
294 read_lock(&tasklist_lock
);
299 read_unlock(&tasklist_lock
);
309 struct super_block
*proc_read_super(struct super_block
*s
,void *data
,
312 struct inode
* root_inode
;
315 s
->s_blocksize
= 1024;
316 s
->s_blocksize_bits
= 10;
317 s
->s_magic
= PROC_SUPER_MAGIC
;
318 s
->s_op
= &proc_sops
;
319 root_inode
= proc_get_inode(s
, PROC_ROOT_INO
, &proc_root
);
322 s
->s_root
= d_alloc_root(root_inode
, NULL
);
325 parse_options(data
, &root_inode
->i_uid
, &root_inode
->i_gid
);
330 printk("proc_read_super: get root inode failed\n");
337 int proc_statfs(struct super_block
*sb
, struct statfs
*buf
, int bufsiz
)
341 tmp
.f_type
= PROC_SUPER_MAGIC
;
342 tmp
.f_bsize
= PAGE_SIZE
/sizeof(long);
348 tmp
.f_namelen
= NAME_MAX
;
349 return copy_to_user(buf
, &tmp
, bufsiz
) ? -EFAULT
: 0;
352 void proc_read_inode(struct inode
* inode
)
354 unsigned long ino
, pid
;
355 struct task_struct
* p
;
357 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
359 inode
->i_blksize
= 1024;
361 if (ino
>= PROC_OPENPROM_FIRST
&&
362 ino
< PROC_OPENPROM_FIRST
+ PROC_NOPENPROM
)
375 read_lock(&tasklist_lock
);
376 p
= find_task_by_pid(pid
);
381 if (ino
== PROC_PID_INO
|| p
->dumpable
) {
382 inode
->i_uid
= p
->euid
;
383 inode
->i_gid
= p
->egid
;
385 if (ino
& PROC_PID_FD_DIR
) {
388 file
= fcheck_task(p
, ino
);
392 inode
->i_op
= &proc_link_inode_operations
;
394 inode
->i_mode
= S_IFLNK
;
395 if (file
->f_mode
& 1)
396 inode
->i_mode
|= S_IRUSR
| S_IXUSR
;
397 if (file
->f_mode
& 2)
398 inode
->i_mode
|= S_IWUSR
| S_IXUSR
;
401 /* Defer unlocking until we're done with the task */
402 read_unlock(&tasklist_lock
);
408 void proc_write_inode(struct inode
* inode
)