proc: fix a race in do_io_accounting()
authorVasiliy Kulikov <segoon@openwall.com>
Tue, 26 Jul 2011 23:08:38 +0000 (16:08 -0700)
committerGreg Kroah-Hartman <gregkh@suse.de>
Fri, 5 Aug 2011 04:58:40 +0000 (21:58 -0700)
commit 293eb1e7772b25a93647c798c7b89bf26c2da2e0 upstream.

If an inode's mode permits opening /proc/PID/io and the resulting file
descriptor is kept across execve() of a setuid or similar binary, the
ptrace_may_access() check tries to prevent using this fd against the
task with escalated privileges.

Unfortunately, there is a race in the check against execve().  If
execve() is processed after the ptrace check, but before the actual io
information gathering, io statistics will be gathered from the
privileged process.  At least in theory this might lead to gathering
sensible information (like ssh/ftp password length) that wouldn't be
available otherwise.

Holding task->signal->cred_guard_mutex while gathering the io
information should protect against the race.

The order of locking is similar to the one inside of ptrace_attach():
first goes cred_guard_mutex, then lock_task_sighand().

Signed-off-by: Vasiliy Kulikov <segoon@openwall.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
fs/proc/base.c

index fc5bc27..5bff4c6 100644 (file)
@@ -2707,9 +2707,16 @@ static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
 {
        struct task_io_accounting acct = task->ioac;
        unsigned long flags;
+       int result;
 
-       if (!ptrace_may_access(task, PTRACE_MODE_READ))
-               return -EACCES;
+       result = mutex_lock_killable(&task->signal->cred_guard_mutex);
+       if (result)
+               return result;
+
+       if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
+               result = -EACCES;
+               goto out_unlock;
+       }
 
        if (whole && lock_task_sighand(task, &flags)) {
                struct task_struct *t = task;
@@ -2720,7 +2727,7 @@ static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
 
                unlock_task_sighand(task, &flags);
        }
-       return sprintf(buffer,
+       result = sprintf(buffer,
                        "rchar: %llu\n"
                        "wchar: %llu\n"
                        "syscr: %llu\n"
@@ -2735,6 +2742,9 @@ static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
                        (unsigned long long)acct.read_bytes,
                        (unsigned long long)acct.write_bytes,
                        (unsigned long long)acct.cancelled_write_bytes);
+out_unlock:
+       mutex_unlock(&task->signal->cred_guard_mutex);
+       return result;
 }
 
 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)