base.c (a1f817dc8ed79cdff2b76bc51d545fb9465982be) base.c (b18b6a9cef7f30e9a8b7738d5fc8d568cf660855)
1/*
2 * linux/fs/proc/base.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * proc base directory handling functions
7 *
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.

--- 33 unchanged lines hidden (view full) ---

42 *
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
45 *
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
48 */
49
1/*
2 * linux/fs/proc/base.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * proc base directory handling functions
7 *
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.

--- 33 unchanged lines hidden (view full) ---

42 *
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
45 *
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
48 */
49
50#include <asm/uaccess.h>
50#include <linux/uaccess.h>
51
52#include <linux/errno.h>
53#include <linux/time.h>
54#include <linux/proc_fs.h>
55#include <linux/stat.h>
56#include <linux/task_io_accounting_ops.h>
57#include <linux/init.h>
58#include <linux/capability.h>

--- 40 unchanged lines hidden (view full) ---

99 * certainly an error. Permission checks need to happen during
100 * each system call not at open time. The reason is that most of
101 * what we wish to check for permissions in /proc varies at runtime.
102 *
103 * The classic example of a problem is opening file descriptors
104 * in /proc for a task before it execs a suid executable.
105 */
106
51
52#include <linux/errno.h>
53#include <linux/time.h>
54#include <linux/proc_fs.h>
55#include <linux/stat.h>
56#include <linux/task_io_accounting_ops.h>
57#include <linux/init.h>
58#include <linux/capability.h>

--- 40 unchanged lines hidden (view full) ---

99 * certainly an error. Permission checks need to happen during
100 * each system call not at open time. The reason is that most of
101 * what we wish to check for permissions in /proc varies at runtime.
102 *
103 * The classic example of a problem is opening file descriptors
104 * in /proc for a task before it execs a suid executable.
105 */
106
107static u8 nlink_tid;
108static u8 nlink_tgid;
109
107struct pid_entry {
108 const char *name;
110struct pid_entry {
111 const char *name;
109 int len;
112 unsigned int len;
110 umode_t mode;
111 const struct inode_operations *iop;
112 const struct file_operations *fop;
113 union proc_op op;
114};
115
116#define NOD(NAME, MODE, IOP, FOP, OP) { \
117 .name = (NAME), \

--- 16 unchanged lines hidden (view full) ---

134 NOD(NAME, (S_IFREG|(MODE)), \
135 NULL, &proc_single_file_operations, \
136 { .proc_show = show } )
137
138/*
139 * Count the number of hardlinks for the pid_entry table, excluding the .
140 * and .. links.
141 */
113 umode_t mode;
114 const struct inode_operations *iop;
115 const struct file_operations *fop;
116 union proc_op op;
117};
118
119#define NOD(NAME, MODE, IOP, FOP, OP) { \
120 .name = (NAME), \

--- 16 unchanged lines hidden (view full) ---

137 NOD(NAME, (S_IFREG|(MODE)), \
138 NULL, &proc_single_file_operations, \
139 { .proc_show = show } )
140
141/*
142 * Count the number of hardlinks for the pid_entry table, excluding the .
143 * and .. links.
144 */
142static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
145static unsigned int __init pid_entry_nlink(const struct pid_entry *entries,
143 unsigned int n)
144{
145 unsigned int i;
146 unsigned int count;
147
146 unsigned int n)
147{
148 unsigned int i;
149 unsigned int count;
150
148 count = 0;
151 count = 2;
149 for (i = 0; i < n; ++i) {
150 if (S_ISDIR(entries[i].mode))
151 ++count;
152 }
153
154 return count;
155}
156

--- 1081 unchanged lines hidden (view full) ---

1238
1239static const struct file_operations proc_oom_score_adj_operations = {
1240 .read = oom_score_adj_read,
1241 .write = oom_score_adj_write,
1242 .llseek = default_llseek,
1243};
1244
1245#ifdef CONFIG_AUDITSYSCALL
152 for (i = 0; i < n; ++i) {
153 if (S_ISDIR(entries[i].mode))
154 ++count;
155 }
156
157 return count;
158}
159

--- 1081 unchanged lines hidden (view full) ---

1241
1242static const struct file_operations proc_oom_score_adj_operations = {
1243 .read = oom_score_adj_read,
1244 .write = oom_score_adj_write,
1245 .llseek = default_llseek,
1246};
1247
1248#ifdef CONFIG_AUDITSYSCALL
1246#define TMPBUFLEN 21
1249#define TMPBUFLEN 11
1247static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1248 size_t count, loff_t *ppos)
1249{
1250 struct inode * inode = file_inode(file);
1251 struct task_struct *task = get_proc_task(inode);
1252 ssize_t length;
1253 char tmpbuf[TMPBUFLEN];
1254

--- 404 unchanged lines hidden (view full) ---

1659 .readlink = proc_pid_readlink,
1660 .get_link = proc_pid_get_link,
1661 .setattr = proc_setattr,
1662};
1663
1664
1665/* building an inode */
1666
1250static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1251 size_t count, loff_t *ppos)
1252{
1253 struct inode * inode = file_inode(file);
1254 struct task_struct *task = get_proc_task(inode);
1255 ssize_t length;
1256 char tmpbuf[TMPBUFLEN];
1257

--- 404 unchanged lines hidden (view full) ---

1662 .readlink = proc_pid_readlink,
1663 .get_link = proc_pid_get_link,
1664 .setattr = proc_setattr,
1665};
1666
1667
1668/* building an inode */
1669
1667struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1670struct inode *proc_pid_make_inode(struct super_block * sb,
1671 struct task_struct *task, umode_t mode)
1668{
1669 struct inode * inode;
1670 struct proc_inode *ei;
1671 const struct cred *cred;
1672
1673 /* We need a new inode */
1674
1675 inode = new_inode(sb);
1676 if (!inode)
1677 goto out;
1678
1679 /* Common stuff */
1680 ei = PROC_I(inode);
1672{
1673 struct inode * inode;
1674 struct proc_inode *ei;
1675 const struct cred *cred;
1676
1677 /* We need a new inode */
1678
1679 inode = new_inode(sb);
1680 if (!inode)
1681 goto out;
1682
1683 /* Common stuff */
1684 ei = PROC_I(inode);
1685 inode->i_mode = mode;
1681 inode->i_ino = get_next_ino();
1682 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
1683 inode->i_op = &proc_def_inode_operations;
1684
1685 /*
1686 * grab the reference to task.
1687 */
1688 ei->pid = get_task_pid(task, PIDTYPE_PID);

--- 273 unchanged lines hidden (view full) ---

1962out_mmput:
1963 mmput(mm);
1964out:
1965 return rc;
1966}
1967
1968struct map_files_info {
1969 fmode_t mode;
1686 inode->i_ino = get_next_ino();
1687 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
1688 inode->i_op = &proc_def_inode_operations;
1689
1690 /*
1691 * grab the reference to task.
1692 */
1693 ei->pid = get_task_pid(task, PIDTYPE_PID);

--- 273 unchanged lines hidden (view full) ---

1967out_mmput:
1968 mmput(mm);
1969out:
1970 return rc;
1971}
1972
1973struct map_files_info {
1974 fmode_t mode;
1970 unsigned long len;
1975 unsigned int len;
1971 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1972};
1973
1974/*
1975 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
1976 * symlinks may be used to bypass permissions on ancestor directories in the
1977 * path to the file in question.
1978 */

--- 20 unchanged lines hidden (view full) ---

1999static int
2000proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2001 struct task_struct *task, const void *ptr)
2002{
2003 fmode_t mode = (fmode_t)(unsigned long)ptr;
2004 struct proc_inode *ei;
2005 struct inode *inode;
2006
1976 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1977};
1978
1979/*
1980 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
1981 * symlinks may be used to bypass permissions on ancestor directories in the
1982 * path to the file in question.
1983 */

--- 20 unchanged lines hidden (view full) ---

2004static int
2005proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2006 struct task_struct *task, const void *ptr)
2007{
2008 fmode_t mode = (fmode_t)(unsigned long)ptr;
2009 struct proc_inode *ei;
2010 struct inode *inode;
2011
2007 inode = proc_pid_make_inode(dir->i_sb, task);
2012 inode = proc_pid_make_inode(dir->i_sb, task, S_IFLNK |
2013 ((mode & FMODE_READ ) ? S_IRUSR : 0) |
2014 ((mode & FMODE_WRITE) ? S_IWUSR : 0));
2008 if (!inode)
2009 return -ENOENT;
2010
2011 ei = PROC_I(inode);
2012 ei->op.proc_get_link = map_files_get_link;
2013
2014 inode->i_op = &proc_map_files_link_inode_operations;
2015 inode->i_size = 64;
2015 if (!inode)
2016 return -ENOENT;
2017
2018 ei = PROC_I(inode);
2019 ei->op.proc_get_link = map_files_get_link;
2020
2021 inode->i_op = &proc_map_files_link_inode_operations;
2022 inode->i_size = 64;
2016 inode->i_mode = S_IFLNK;
2017
2023
2018 if (mode & FMODE_READ)
2019 inode->i_mode |= S_IRUSR;
2020 if (mode & FMODE_WRITE)
2021 inode->i_mode |= S_IWUSR;
2022
2023 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2024 d_add(dentry, inode);
2025
2026 return 0;
2027}
2028
2029static struct dentry *proc_map_files_lookup(struct inode *dir,
2030 struct dentry *dentry, unsigned int flags)

--- 142 unchanged lines hidden (view full) ---

2173}
2174
2175static const struct file_operations proc_map_files_operations = {
2176 .read = generic_read_dir,
2177 .iterate_shared = proc_map_files_readdir,
2178 .llseek = generic_file_llseek,
2179};
2180
2024 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2025 d_add(dentry, inode);
2026
2027 return 0;
2028}
2029
2030static struct dentry *proc_map_files_lookup(struct inode *dir,
2031 struct dentry *dentry, unsigned int flags)

--- 142 unchanged lines hidden (view full) ---

2174}
2175
2176static const struct file_operations proc_map_files_operations = {
2177 .read = generic_read_dir,
2178 .iterate_shared = proc_map_files_readdir,
2179 .llseek = generic_file_llseek,
2180};
2181
2181#ifdef CONFIG_CHECKPOINT_RESTORE
2182#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
2182struct timers_private {
2183 struct pid *pid;
2184 struct task_struct *task;
2185 struct sighand_struct *sighand;
2186 struct pid_namespace *ns;
2187 unsigned long flags;
2188};
2189

--- 177 unchanged lines hidden (view full) ---

2367
2368static int proc_pident_instantiate(struct inode *dir,
2369 struct dentry *dentry, struct task_struct *task, const void *ptr)
2370{
2371 const struct pid_entry *p = ptr;
2372 struct inode *inode;
2373 struct proc_inode *ei;
2374
2183struct timers_private {
2184 struct pid *pid;
2185 struct task_struct *task;
2186 struct sighand_struct *sighand;
2187 struct pid_namespace *ns;
2188 unsigned long flags;
2189};
2190

--- 177 unchanged lines hidden (view full) ---

2368
2369static int proc_pident_instantiate(struct inode *dir,
2370 struct dentry *dentry, struct task_struct *task, const void *ptr)
2371{
2372 const struct pid_entry *p = ptr;
2373 struct inode *inode;
2374 struct proc_inode *ei;
2375
2375 inode = proc_pid_make_inode(dir->i_sb, task);
2376 inode = proc_pid_make_inode(dir->i_sb, task, p->mode);
2376 if (!inode)
2377 goto out;
2378
2379 ei = PROC_I(inode);
2377 if (!inode)
2378 goto out;
2379
2380 ei = PROC_I(inode);
2380 inode->i_mode = p->mode;
2381 if (S_ISDIR(inode->i_mode))
2382 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2383 if (p->iop)
2384 inode->i_op = p->iop;
2385 if (p->fop)
2386 inode->i_fop = p->fop;
2387 ei->op = p->op;
2388 d_set_d_op(dentry, &pid_dentry_operations);

--- 18 unchanged lines hidden (view full) ---

2407
2408 if (!task)
2409 goto out_no_task;
2410
2411 /*
2412 * Yes, it does not scale. And it should not. Don't add
2413 * new entries into /proc/<tgid>/ without very good reasons.
2414 */
2381 if (S_ISDIR(inode->i_mode))
2382 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2383 if (p->iop)
2384 inode->i_op = p->iop;
2385 if (p->fop)
2386 inode->i_fop = p->fop;
2387 ei->op = p->op;
2388 d_set_d_op(dentry, &pid_dentry_operations);

--- 18 unchanged lines hidden (view full) ---

2407
2408 if (!task)
2409 goto out_no_task;
2410
2411 /*
2412 * Yes, it does not scale. And it should not. Don't add
2413 * new entries into /proc/<tgid>/ without very good reasons.
2414 */
2415 last = &ents[nents - 1];
2416 for (p = ents; p <= last; p++) {
2415 last = &ents[nents];
2416 for (p = ents; p < last; p++) {
2417 if (p->len != dentry->d_name.len)
2418 continue;
2419 if (!memcmp(dentry->d_name.name, p->name, p->len))
2420 break;
2421 }
2417 if (p->len != dentry->d_name.len)
2418 continue;
2419 if (!memcmp(dentry->d_name.name, p->name, p->len))
2420 break;
2421 }
2422 if (p > last)
2422 if (p >= last)
2423 goto out;
2424
2425 error = proc_pident_instantiate(dir, dentry, task, p);
2426out:
2427 put_task_struct(task);
2428out_no_task:
2429 return ERR_PTR(error);
2430}

--- 8 unchanged lines hidden (view full) ---

2439 return -ENOENT;
2440
2441 if (!dir_emit_dots(file, ctx))
2442 goto out;
2443
2444 if (ctx->pos >= nents + 2)
2445 goto out;
2446
2423 goto out;
2424
2425 error = proc_pident_instantiate(dir, dentry, task, p);
2426out:
2427 put_task_struct(task);
2428out_no_task:
2429 return ERR_PTR(error);
2430}

--- 8 unchanged lines hidden (view full) ---

2439 return -ENOENT;
2440
2441 if (!dir_emit_dots(file, ctx))
2442 goto out;
2443
2444 if (ctx->pos >= nents + 2)
2445 goto out;
2446
2447 for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2447 for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
2448 if (!proc_fill_cache(file, ctx, p->name, p->len,
2449 proc_pident_instantiate, task, p))
2450 break;
2451 ctx->pos++;
2452 }
2453out:
2454 put_task_struct(task);
2455 return 0;

--- 475 unchanged lines hidden (view full) ---

2931 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
2932#endif
2933#ifdef CONFIG_USER_NS
2934 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2935 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2936 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2937 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
2938#endif
2448 if (!proc_fill_cache(file, ctx, p->name, p->len,
2449 proc_pident_instantiate, task, p))
2450 break;
2451 ctx->pos++;
2452 }
2453out:
2454 put_task_struct(task);
2455 return 0;

--- 475 unchanged lines hidden (view full) ---

2931 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
2932#endif
2933#ifdef CONFIG_USER_NS
2934 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2935 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2936 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2937 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
2938#endif
2939#ifdef CONFIG_CHECKPOINT_RESTORE
2939#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
2940 REG("timers", S_IRUGO, proc_timers_operations),
2941#endif
2942 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
2943};
2944
2945static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
2946{
2947 return proc_pident_readdir(file, ctx,

--- 106 unchanged lines hidden (view full) ---

3054}
3055
3056static int proc_pid_instantiate(struct inode *dir,
3057 struct dentry * dentry,
3058 struct task_struct *task, const void *ptr)
3059{
3060 struct inode *inode;
3061
2940 REG("timers", S_IRUGO, proc_timers_operations),
2941#endif
2942 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
2943};
2944
2945static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
2946{
2947 return proc_pident_readdir(file, ctx,

--- 106 unchanged lines hidden (view full) ---

3054}
3055
3056static int proc_pid_instantiate(struct inode *dir,
3057 struct dentry * dentry,
3058 struct task_struct *task, const void *ptr)
3059{
3060 struct inode *inode;
3061
3062 inode = proc_pid_make_inode(dir->i_sb, task);
3062 inode = proc_pid_make_inode(dir->i_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3063 if (!inode)
3064 goto out;
3065
3063 if (!inode)
3064 goto out;
3065
3066 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3067 inode->i_op = &proc_tgid_base_inode_operations;
3068 inode->i_fop = &proc_tgid_base_operations;
3069 inode->i_flags|=S_IMMUTABLE;
3070
3066 inode->i_op = &proc_tgid_base_inode_operations;
3067 inode->i_fop = &proc_tgid_base_operations;
3068 inode->i_flags|=S_IMMUTABLE;
3069
3071 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
3072 ARRAY_SIZE(tgid_base_stuff)));
3070 set_nlink(inode, nlink_tgid);
3073
3074 d_set_d_op(dentry, &pid_dentry_operations);
3075
3076 d_add(dentry, inode);
3077 /* Close the race of the process dying before we return the dentry */
3078 if (pid_revalidate(dentry, 0))
3079 return 0;
3080out:

--- 266 unchanged lines hidden (view full) ---

3347 .getattr = pid_getattr,
3348 .setattr = proc_setattr,
3349};
3350
3351static int proc_task_instantiate(struct inode *dir,
3352 struct dentry *dentry, struct task_struct *task, const void *ptr)
3353{
3354 struct inode *inode;
3071
3072 d_set_d_op(dentry, &pid_dentry_operations);
3073
3074 d_add(dentry, inode);
3075 /* Close the race of the process dying before we return the dentry */
3076 if (pid_revalidate(dentry, 0))
3077 return 0;
3078out:

--- 266 unchanged lines hidden (view full) ---

3345 .getattr = pid_getattr,
3346 .setattr = proc_setattr,
3347};
3348
3349static int proc_task_instantiate(struct inode *dir,
3350 struct dentry *dentry, struct task_struct *task, const void *ptr)
3351{
3352 struct inode *inode;
3355 inode = proc_pid_make_inode(dir->i_sb, task);
3353 inode = proc_pid_make_inode(dir->i_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3356
3357 if (!inode)
3358 goto out;
3354
3355 if (!inode)
3356 goto out;
3359 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3360 inode->i_op = &proc_tid_base_inode_operations;
3361 inode->i_fop = &proc_tid_base_operations;
3362 inode->i_flags|=S_IMMUTABLE;
3363
3357 inode->i_op = &proc_tid_base_inode_operations;
3358 inode->i_fop = &proc_tid_base_operations;
3359 inode->i_flags|=S_IMMUTABLE;
3360
3364 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3365 ARRAY_SIZE(tid_base_stuff)));
3361 set_nlink(inode, nlink_tid);
3366
3367 d_set_d_op(dentry, &pid_dentry_operations);
3368
3369 d_add(dentry, inode);
3370 /* Close the race of the process dying before we return the dentry */
3371 if (pid_revalidate(dentry, 0))
3372 return 0;
3373out:

--- 173 unchanged lines hidden (view full) ---

3547 .permission = proc_pid_permission,
3548};
3549
3550static const struct file_operations proc_task_operations = {
3551 .read = generic_read_dir,
3552 .iterate_shared = proc_task_readdir,
3553 .llseek = generic_file_llseek,
3554};
3362
3363 d_set_d_op(dentry, &pid_dentry_operations);
3364
3365 d_add(dentry, inode);
3366 /* Close the race of the process dying before we return the dentry */
3367 if (pid_revalidate(dentry, 0))
3368 return 0;
3369out:

--- 173 unchanged lines hidden (view full) ---

3543 .permission = proc_pid_permission,
3544};
3545
3546static const struct file_operations proc_task_operations = {
3547 .read = generic_read_dir,
3548 .iterate_shared = proc_task_readdir,
3549 .llseek = generic_file_llseek,
3550};
3551
3552void __init set_proc_pid_nlink(void)
3553{
3554 nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3555 nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3556}