Merge branch 'stable-4.10' of git://git.infradead.org/users/pcmoore/audit
[platform/kernel/linux-rpi.git] / fs / proc / base.c
index 9cdb3e4..5ea8363 100644 (file)
  *     in /proc for a task before it execs a suid executable.
  */
 
+static u8 nlink_tid;
+static u8 nlink_tgid;
+
 struct pid_entry {
        const char *name;
-       int len;
+       unsigned int len;
        umode_t mode;
        const struct inode_operations *iop;
        const struct file_operations *fop;
@@ -139,13 +142,13 @@ struct pid_entry {
  * Count the number of hardlinks for the pid_entry table, excluding the .
  * and .. links.
  */
-static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
+static unsigned int __init pid_entry_nlink(const struct pid_entry *entries,
        unsigned int n)
 {
        unsigned int i;
        unsigned int count;
 
-       count = 0;
+       count = 2;
        for (i = 0; i < n; ++i) {
                if (S_ISDIR(entries[i].mode))
                        ++count;
@@ -400,23 +403,6 @@ static const struct file_operations proc_pid_cmdline_ops = {
        .llseek = generic_file_llseek,
 };
 
-static int proc_pid_auxv(struct seq_file *m, struct pid_namespace *ns,
-                        struct pid *pid, struct task_struct *task)
-{
-       struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
-       if (mm && !IS_ERR(mm)) {
-               unsigned int nwords = 0;
-               do {
-                       nwords += 2;
-               } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
-               seq_write(m, mm->saved_auxv, nwords * sizeof(mm->saved_auxv[0]));
-               mmput(mm);
-               return 0;
-       } else
-               return PTR_ERR(mm);
-}
-
-
 #ifdef CONFIG_KALLSYMS
 /*
  * Provides a wchan file via kallsyms in a proper one-value-per-file format.
@@ -483,7 +469,7 @@ static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
                save_stack_trace_tsk(task, &trace);
 
                for (i = 0; i < trace.nr_entries; i++) {
-                       seq_printf(m, "[<%pK>] %pS\n",
+                       seq_printf(m, "[<%pK>] %pB\n",
                                   (void *)entries[i], (void *)entries[i]);
                }
                unlock_trace(task);
@@ -709,7 +695,7 @@ int proc_setattr(struct dentry *dentry, struct iattr *attr)
        if (attr->ia_valid & ATTR_MODE)
                return -EPERM;
 
-       error = inode_change_ok(inode, attr);
+       error = setattr_prepare(dentry, attr);
        if (error)
                return error;
 
@@ -849,6 +835,7 @@ static ssize_t mem_rw(struct file *file, char __user *buf,
        unsigned long addr = *ppos;
        ssize_t copied;
        char *page;
+       unsigned int flags;
 
        if (!mm)
                return 0;
@@ -861,6 +848,11 @@ static ssize_t mem_rw(struct file *file, char __user *buf,
        if (!atomic_inc_not_zero(&mm->mm_users))
                goto free;
 
+       /* Maybe we should limit FOLL_FORCE to actual ptrace users? */
+       flags = FOLL_FORCE;
+       if (write)
+               flags |= FOLL_WRITE;
+
        while (count > 0) {
                int this_len = min_t(int, count, PAGE_SIZE);
 
@@ -869,7 +861,7 @@ static ssize_t mem_rw(struct file *file, char __user *buf,
                        break;
                }
 
-               this_len = access_remote_vm(mm, addr, page, this_len, write);
+               this_len = access_remote_vm(mm, addr, page, this_len, flags);
                if (!this_len) {
                        if (!copied)
                                copied = -EIO;
@@ -981,8 +973,7 @@ static ssize_t environ_read(struct file *file, char __user *buf,
                max_len = min_t(size_t, PAGE_SIZE, count);
                this_len = min(max_len, this_len);
 
-               retval = access_remote_vm(mm, (env_start + src),
-                       page, this_len, 0);
+               retval = access_remote_vm(mm, (env_start + src), page, this_len, 0);
 
                if (retval <= 0) {
                        ret = retval;
@@ -1014,6 +1005,33 @@ static const struct file_operations proc_environ_operations = {
        .release        = mem_release,
 };
 
+static int auxv_open(struct inode *inode, struct file *file)
+{
+       return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
+}
+
+static ssize_t auxv_read(struct file *file, char __user *buf,
+                       size_t count, loff_t *ppos)
+{
+       struct mm_struct *mm = file->private_data;
+       unsigned int nwords = 0;
+
+       if (!mm)
+               return 0;
+       do {
+               nwords += 2;
+       } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
+       return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
+                                      nwords * sizeof(mm->saved_auxv[0]));
+}
+
+static const struct file_operations proc_auxv_operations = {
+       .open           = auxv_open,
+       .read           = auxv_read,
+       .llseek         = generic_file_llseek,
+       .release        = mem_release,
+};
+
 static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
                            loff_t *ppos)
 {
@@ -1649,7 +1667,8 @@ const struct inode_operations proc_pid_link_inode_operations = {
 
 /* building an inode */
 
-struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
+struct inode *proc_pid_make_inode(struct super_block * sb,
+                                 struct task_struct *task, umode_t mode)
 {
        struct inode * inode;
        struct proc_inode *ei;
@@ -1663,8 +1682,9 @@ struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *t
 
        /* Common stuff */
        ei = PROC_I(inode);
+       inode->i_mode = mode;
        inode->i_ino = get_next_ino();
-       inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
+       inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
        inode->i_op = &proc_def_inode_operations;
 
        /*
@@ -1952,7 +1972,7 @@ out:
 
 struct map_files_info {
        fmode_t         mode;
-       unsigned long   len;
+       unsigned int    len;
        unsigned char   name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
 };
 
@@ -1989,7 +2009,9 @@ proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
        struct proc_inode *ei;
        struct inode *inode;
 
-       inode = proc_pid_make_inode(dir->i_sb, task);
+       inode = proc_pid_make_inode(dir->i_sb, task, S_IFLNK |
+                                   ((mode & FMODE_READ ) ? S_IRUSR : 0) |
+                                   ((mode & FMODE_WRITE) ? S_IWUSR : 0));
        if (!inode)
                return -ENOENT;
 
@@ -1998,12 +2020,6 @@ proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
 
        inode->i_op = &proc_map_files_link_inode_operations;
        inode->i_size = 64;
-       inode->i_mode = S_IFLNK;
-
-       if (mode & FMODE_READ)
-               inode->i_mode |= S_IRUSR;
-       if (mode & FMODE_WRITE)
-               inode->i_mode |= S_IWUSR;
 
        d_set_d_op(dentry, &tid_map_files_dentry_operations);
        d_add(dentry, inode);
@@ -2280,16 +2296,27 @@ static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
        if (!p)
                return -ESRCH;
 
-       if (ptrace_may_access(p, PTRACE_MODE_ATTACH_FSCREDS)) {
-               task_lock(p);
-               if (slack_ns == 0)
-                       p->timer_slack_ns = p->default_timer_slack_ns;
-               else
-                       p->timer_slack_ns = slack_ns;
-               task_unlock(p);
-       } else
-               count = -EPERM;
+       if (p != current) {
+               if (!capable(CAP_SYS_NICE)) {
+                       count = -EPERM;
+                       goto out;
+               }
 
+               err = security_task_setscheduler(p);
+               if (err) {
+                       count = err;
+                       goto out;
+               }
+       }
+
+       task_lock(p);
+       if (slack_ns == 0)
+               p->timer_slack_ns = p->default_timer_slack_ns;
+       else
+               p->timer_slack_ns = slack_ns;
+       task_unlock(p);
+
+out:
        put_task_struct(p);
 
        return count;
@@ -2299,19 +2326,28 @@ static int timerslack_ns_show(struct seq_file *m, void *v)
 {
        struct inode *inode = m->private;
        struct task_struct *p;
-       int err =  0;
+       int err = 0;
 
        p = get_proc_task(inode);
        if (!p)
                return -ESRCH;
 
-       if (ptrace_may_access(p, PTRACE_MODE_ATTACH_FSCREDS)) {
-               task_lock(p);
-               seq_printf(m, "%llu\n", p->timer_slack_ns);
-               task_unlock(p);
-       } else
-               err = -EPERM;
+       if (p != current) {
+
+               if (!capable(CAP_SYS_NICE)) {
+                       err = -EPERM;
+                       goto out;
+               }
+               err = security_task_getscheduler(p);
+               if (err)
+                       goto out;
+       }
+
+       task_lock(p);
+       seq_printf(m, "%llu\n", p->timer_slack_ns);
+       task_unlock(p);
 
+out:
        put_task_struct(p);
 
        return err;
@@ -2337,12 +2373,11 @@ static int proc_pident_instantiate(struct inode *dir,
        struct inode *inode;
        struct proc_inode *ei;
 
-       inode = proc_pid_make_inode(dir->i_sb, task);
+       inode = proc_pid_make_inode(dir->i_sb, task, p->mode);
        if (!inode)
                goto out;
 
        ei = PROC_I(inode);
-       inode->i_mode = p->mode;
        if (S_ISDIR(inode->i_mode))
                set_nlink(inode, 2);    /* Use getattr to fix if necessary */
        if (p->iop)
@@ -2377,14 +2412,14 @@ static struct dentry *proc_pident_lookup(struct inode *dir,
         * Yes, it does not scale. And it should not. Don't add
         * new entries into /proc/<tgid>/ without very good reasons.
         */
-       last = &ents[nents - 1];
-       for (p = ents; p <= last; p++) {
+       last = &ents[nents];
+       for (p = ents; p < last; p++) {
                if (p->len != dentry->d_name.len)
                        continue;
                if (!memcmp(dentry->d_name.name, p->name, p->len))
                        break;
        }
-       if (p > last)
+       if (p >= last)
                goto out;
 
        error = proc_pident_instantiate(dir, dentry, task, p);
@@ -2409,7 +2444,7 @@ static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
        if (ctx->pos >= nents + 2)
                goto out;
 
-       for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
+       for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
                if (!proc_fill_cache(file, ctx, p->name, p->len,
                                proc_pident_instantiate, task, p))
                        break;
@@ -2822,7 +2857,7 @@ static const struct pid_entry tgid_base_stuff[] = {
        DIR("net",        S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
 #endif
        REG("environ",    S_IRUSR, proc_environ_operations),
-       ONE("auxv",       S_IRUSR, proc_pid_auxv),
+       REG("auxv",       S_IRUSR, proc_auxv_operations),
        ONE("status",     S_IRUGO, proc_pid_status),
        ONE("personality", S_IRUSR, proc_pid_personality),
        ONE("limits",     S_IRUGO, proc_pid_limits),
@@ -3024,17 +3059,15 @@ static int proc_pid_instantiate(struct inode *dir,
 {
        struct inode *inode;
 
-       inode = proc_pid_make_inode(dir->i_sb, task);
+       inode = proc_pid_make_inode(dir->i_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
        if (!inode)
                goto out;
 
-       inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
        inode->i_op = &proc_tgid_base_inode_operations;
        inode->i_fop = &proc_tgid_base_operations;
        inode->i_flags|=S_IMMUTABLE;
 
-       set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
-                                                 ARRAY_SIZE(tgid_base_stuff)));
+       set_nlink(inode, nlink_tgid);
 
        d_set_d_op(dentry, &pid_dentry_operations);
 
@@ -3210,7 +3243,7 @@ static const struct pid_entry tid_base_stuff[] = {
        DIR("net",        S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
 #endif
        REG("environ",   S_IRUSR, proc_environ_operations),
-       ONE("auxv",      S_IRUSR, proc_pid_auxv),
+       REG("auxv",      S_IRUSR, proc_auxv_operations),
        ONE("status",    S_IRUGO, proc_pid_status),
        ONE("personality", S_IRUSR, proc_pid_personality),
        ONE("limits",    S_IRUGO, proc_pid_limits),
@@ -3317,17 +3350,15 @@ static int proc_task_instantiate(struct inode *dir,
        struct dentry *dentry, struct task_struct *task, const void *ptr)
 {
        struct inode *inode;
-       inode = proc_pid_make_inode(dir->i_sb, task);
+       inode = proc_pid_make_inode(dir->i_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
 
        if (!inode)
                goto out;
-       inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
        inode->i_op = &proc_tid_base_inode_operations;
        inode->i_fop = &proc_tid_base_operations;
        inode->i_flags|=S_IMMUTABLE;
 
-       set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
-                                                 ARRAY_SIZE(tid_base_stuff)));
+       set_nlink(inode, nlink_tid);
 
        d_set_d_op(dentry, &pid_dentry_operations);
 
@@ -3517,3 +3548,9 @@ static const struct file_operations proc_task_operations = {
        .iterate_shared = proc_task_readdir,
        .llseek         = generic_file_llseek,
 };
+
+void __init set_proc_pid_nlink(void)
+{
+       nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
+       nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
+}