Skip to content
This repository was archived by the owner on Oct 5, 2018. It is now read-only.

Commit 969624b

Browse files
thejhgregkh
authored andcommitted
ptrace: use fsuid, fsgid, effective creds for fs access checks
commit caaee62 upstream. By checking the effective credentials instead of the real UID / permitted capabilities, ensure that the calling process actually intended to use its credentials. To ensure that all ptrace checks use the correct caller credentials (e.g. in case out-of-tree code or newly added code omits the PTRACE_MODE_*CREDS flag), use two new flags and require one of them to be set. The problem was that when a privileged task had temporarily dropped its privileges, e.g. by calling setreuid(0, user_uid), with the intent to perform following syscalls with the credentials of a user, it still passed ptrace access checks that the user would not be able to pass. While an attacker should not be able to convince the privileged task to perform a ptrace() syscall, this is a problem because the ptrace access check is reused for things in procfs. In particular, the following somewhat interesting procfs entries only rely on ptrace access checks: /proc/$pid/stat - uses the check for determining whether pointers should be visible, useful for bypassing ASLR /proc/$pid/maps - also useful for bypassing ASLR /proc/$pid/cwd - useful for gaining access to restricted directories that contain files with lax permissions, e.g. in this scenario: lrwxrwxrwx root root /proc/13020/cwd -> /root/foobar drwx------ root root /root drwxr-xr-x root root /root/foobar -rw-r--r-- root root /root/foobar/secret Therefore, on a system where a root-owned mode 6755 binary changes its effective credentials as described and then dumps a user-specified file, this could be used by an attacker to reveal the memory layout of root's processes or reveal the contents of files he is not allowed to access (through /proc/$pid/cwd). [akpm@linux-foundation.org: fix warning] Signed-off-by: Jann Horn <jann@thejh.net> Acked-by: Kees Cook <keescook@chromium.org> Cc: Casey Schaufler <casey@schaufler-ca.com> Cc: Oleg Nesterov <oleg@redhat.com> Cc: Ingo Molnar <mingo@redhat.com> Cc: James Morris <james.l.morris@oracle.com> Cc: "Serge E. Hallyn" <serge.hallyn@ubuntu.com> Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Cc: Andy Lutomirski <luto@kernel.org> Cc: Al Viro <viro@zeniv.linux.org.uk> Cc: "Eric W. Biederman" <ebiederm@xmission.com> Cc: Willy Tarreau <w@1wt.eu> 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@linuxfoundation.org>
1 parent ba6d928 commit 969624b

File tree

11 files changed

+80
-29
lines changed

11 files changed

+80
-29
lines changed

fs/proc/array.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
395395

396396
state = *get_task_state(task);
397397
vsize = eip = esp = 0;
398-
permitted = ptrace_may_access(task, PTRACE_MODE_READ | PTRACE_MODE_NOAUDIT);
398+
permitted = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS | PTRACE_MODE_NOAUDIT);
399399
mm = get_task_mm(task);
400400
if (mm) {
401401
vsize = task_vsize(mm);

fs/proc/base.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ static const struct file_operations proc_pid_cmdline_ops = {
403403
static int proc_pid_auxv(struct seq_file *m, struct pid_namespace *ns,
404404
struct pid *pid, struct task_struct *task)
405405
{
406-
struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
406+
struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
407407
if (mm && !IS_ERR(mm)) {
408408
unsigned int nwords = 0;
409409
do {
@@ -430,7 +430,8 @@ static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
430430

431431
wchan = get_wchan(task);
432432

433-
if (wchan && ptrace_may_access(task, PTRACE_MODE_READ) && !lookup_symbol_name(wchan, symname))
433+
if (wchan && ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)
434+
&& !lookup_symbol_name(wchan, symname))
434435
seq_printf(m, "%s", symname);
435436
else
436437
seq_putc(m, '0');
@@ -444,7 +445,7 @@ static int lock_trace(struct task_struct *task)
444445
int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
445446
if (err)
446447
return err;
447-
if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
448+
if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
448449
mutex_unlock(&task->signal->cred_guard_mutex);
449450
return -EPERM;
450451
}
@@ -697,7 +698,7 @@ static int proc_fd_access_allowed(struct inode *inode)
697698
*/
698699
task = get_proc_task(inode);
699700
if (task) {
700-
allowed = ptrace_may_access(task, PTRACE_MODE_READ);
701+
allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
701702
put_task_struct(task);
702703
}
703704
return allowed;
@@ -732,7 +733,7 @@ static bool has_pid_permissions(struct pid_namespace *pid,
732733
return true;
733734
if (in_group_p(pid->pid_gid))
734735
return true;
735-
return ptrace_may_access(task, PTRACE_MODE_READ);
736+
return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
736737
}
737738

738739

@@ -809,7 +810,7 @@ struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
809810
struct mm_struct *mm = ERR_PTR(-ESRCH);
810811

811812
if (task) {
812-
mm = mm_access(task, mode);
813+
mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
813814
put_task_struct(task);
814815

815816
if (!IS_ERR_OR_NULL(mm)) {
@@ -1856,7 +1857,7 @@ static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
18561857
if (!task)
18571858
goto out_notask;
18581859

1859-
mm = mm_access(task, PTRACE_MODE_READ);
1860+
mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
18601861
if (IS_ERR_OR_NULL(mm))
18611862
goto out;
18621863

@@ -2007,7 +2008,7 @@ static struct dentry *proc_map_files_lookup(struct inode *dir,
20072008
goto out;
20082009

20092010
result = -EACCES;
2010-
if (!ptrace_may_access(task, PTRACE_MODE_READ))
2011+
if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
20112012
goto out_put_task;
20122013

20132014
result = -ENOENT;
@@ -2060,7 +2061,7 @@ proc_map_files_readdir(struct file *file, struct dir_context *ctx)
20602061
goto out;
20612062

20622063
ret = -EACCES;
2063-
if (!ptrace_may_access(task, PTRACE_MODE_READ))
2064+
if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
20642065
goto out_put_task;
20652066

20662067
ret = 0;
@@ -2530,7 +2531,7 @@ static int do_io_accounting(struct task_struct *task, struct seq_file *m, int wh
25302531
if (result)
25312532
return result;
25322533

2533-
if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2534+
if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
25342535
result = -EACCES;
25352536
goto out_unlock;
25362537
}

fs/proc/namespaces.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static const char *proc_ns_follow_link(struct dentry *dentry, void **cookie)
4242
if (!task)
4343
return error;
4444

45-
if (ptrace_may_access(task, PTRACE_MODE_READ)) {
45+
if (ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
4646
error = ns_get_path(&ns_path, task, ns_ops);
4747
if (!error)
4848
nd_jump_link(&ns_path);
@@ -63,7 +63,7 @@ static int proc_ns_readlink(struct dentry *dentry, char __user *buffer, int bufl
6363
if (!task)
6464
return res;
6565

66-
if (ptrace_may_access(task, PTRACE_MODE_READ)) {
66+
if (ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
6767
res = ns_get_name(name, sizeof(name), task, ns_ops);
6868
if (res >= 0)
6969
res = readlink_copy(buffer, buflen, name);

include/linux/ptrace.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,29 @@ extern void exit_ptrace(struct task_struct *tracer, struct list_head *dead);
5757
#define PTRACE_MODE_READ 0x01
5858
#define PTRACE_MODE_ATTACH 0x02
5959
#define PTRACE_MODE_NOAUDIT 0x04
60-
/* Returns true on success, false on denial. */
60+
#define PTRACE_MODE_FSCREDS 0x08
61+
#define PTRACE_MODE_REALCREDS 0x10
62+
63+
/* shorthands for READ/ATTACH and FSCREDS/REALCREDS combinations */
64+
#define PTRACE_MODE_READ_FSCREDS (PTRACE_MODE_READ | PTRACE_MODE_FSCREDS)
65+
#define PTRACE_MODE_READ_REALCREDS (PTRACE_MODE_READ | PTRACE_MODE_REALCREDS)
66+
#define PTRACE_MODE_ATTACH_FSCREDS (PTRACE_MODE_ATTACH | PTRACE_MODE_FSCREDS)
67+
#define PTRACE_MODE_ATTACH_REALCREDS (PTRACE_MODE_ATTACH | PTRACE_MODE_REALCREDS)
68+
69+
/**
70+
* ptrace_may_access - check whether the caller is permitted to access
71+
* a target task.
72+
* @task: target task
73+
* @mode: selects type of access and caller credentials
74+
*
75+
* Returns true on success, false on denial.
76+
*
77+
* One of the flags PTRACE_MODE_FSCREDS and PTRACE_MODE_REALCREDS must
78+
* be set in @mode to specify whether the access was requested through
79+
* a filesystem syscall (should use effective capabilities and fsuid
80+
* of the caller) or through an explicit syscall such as
81+
* process_vm_writev or ptrace (and should use the real credentials).
82+
*/
6183
extern bool ptrace_may_access(struct task_struct *task, unsigned int mode);
6284

6385
static inline int ptrace_reparented(struct task_struct *child)

kernel/events/core.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3434,7 +3434,7 @@ find_lively_task_by_vpid(pid_t vpid)
34343434

34353435
/* Reuse ptrace permission checks for now. */
34363436
err = -EACCES;
3437-
if (!ptrace_may_access(task, PTRACE_MODE_READ))
3437+
if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS))
34383438
goto errout;
34393439

34403440
return task;

kernel/futex.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2881,7 +2881,7 @@ SYSCALL_DEFINE3(get_robust_list, int, pid,
28812881
}
28822882

28832883
ret = -EPERM;
2884-
if (!ptrace_may_access(p, PTRACE_MODE_READ))
2884+
if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS))
28852885
goto err_unlock;
28862886

28872887
head = p->robust_list;

kernel/futex_compat.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ COMPAT_SYSCALL_DEFINE3(get_robust_list, int, pid,
155155
}
156156

157157
ret = -EPERM;
158-
if (!ptrace_may_access(p, PTRACE_MODE_READ))
158+
if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS))
159159
goto err_unlock;
160160

161161
head = p->compat_robust_list;

kernel/kcmp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ SYSCALL_DEFINE5(kcmp, pid_t, pid1, pid_t, pid2, int, type,
122122
&task2->signal->cred_guard_mutex);
123123
if (ret)
124124
goto err;
125-
if (!ptrace_may_access(task1, PTRACE_MODE_READ) ||
126-
!ptrace_may_access(task2, PTRACE_MODE_READ)) {
125+
if (!ptrace_may_access(task1, PTRACE_MODE_READ_REALCREDS) ||
126+
!ptrace_may_access(task2, PTRACE_MODE_READ_REALCREDS)) {
127127
ret = -EPERM;
128128
goto err_unlock;
129129
}

kernel/ptrace.c

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,14 @@ static int ptrace_has_cap(struct user_namespace *ns, unsigned int mode)
219219
static int __ptrace_may_access(struct task_struct *task, unsigned int mode)
220220
{
221221
const struct cred *cred = current_cred(), *tcred;
222+
int dumpable = 0;
223+
kuid_t caller_uid;
224+
kgid_t caller_gid;
225+
226+
if (!(mode & PTRACE_MODE_FSCREDS) == !(mode & PTRACE_MODE_REALCREDS)) {
227+
WARN(1, "denying ptrace access check without PTRACE_MODE_*CREDS\n");
228+
return -EPERM;
229+
}
222230

223231
/* May we inspect the given task?
224232
* This check is used both for attaching with ptrace
@@ -228,18 +236,33 @@ static int __ptrace_may_access(struct task_struct *task, unsigned int mode)
228236
* because setting up the necessary parent/child relationship
229237
* or halting the specified task is impossible.
230238
*/
231-
int dumpable = 0;
239+
232240
/* Don't let security modules deny introspection */
233241
if (same_thread_group(task, current))
234242
return 0;
235243
rcu_read_lock();
244+
if (mode & PTRACE_MODE_FSCREDS) {
245+
caller_uid = cred->fsuid;
246+
caller_gid = cred->fsgid;
247+
} else {
248+
/*
249+
* Using the euid would make more sense here, but something
250+
* in userland might rely on the old behavior, and this
251+
* shouldn't be a security problem since
252+
* PTRACE_MODE_REALCREDS implies that the caller explicitly
253+
* used a syscall that requests access to another process
254+
* (and not a filesystem syscall to procfs).
255+
*/
256+
caller_uid = cred->uid;
257+
caller_gid = cred->gid;
258+
}
236259
tcred = __task_cred(task);
237-
if (uid_eq(cred->uid, tcred->euid) &&
238-
uid_eq(cred->uid, tcred->suid) &&
239-
uid_eq(cred->uid, tcred->uid) &&
240-
gid_eq(cred->gid, tcred->egid) &&
241-
gid_eq(cred->gid, tcred->sgid) &&
242-
gid_eq(cred->gid, tcred->gid))
260+
if (uid_eq(caller_uid, tcred->euid) &&
261+
uid_eq(caller_uid, tcred->suid) &&
262+
uid_eq(caller_uid, tcred->uid) &&
263+
gid_eq(caller_gid, tcred->egid) &&
264+
gid_eq(caller_gid, tcred->sgid) &&
265+
gid_eq(caller_gid, tcred->gid))
243266
goto ok;
244267
if (ptrace_has_cap(tcred->user_ns, mode))
245268
goto ok;
@@ -306,7 +329,7 @@ static int ptrace_attach(struct task_struct *task, long request,
306329
goto out;
307330

308331
task_lock(task);
309-
retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH);
332+
retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH_REALCREDS);
310333
task_unlock(task);
311334
if (retval)
312335
goto unlock_creds;

mm/process_vm_access.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ static ssize_t process_vm_rw_core(pid_t pid, struct iov_iter *iter,
194194
goto free_proc_pages;
195195
}
196196

197-
mm = mm_access(task, PTRACE_MODE_ATTACH);
197+
mm = mm_access(task, PTRACE_MODE_ATTACH_REALCREDS);
198198
if (!mm || IS_ERR(mm)) {
199199
rc = IS_ERR(mm) ? PTR_ERR(mm) : -ESRCH;
200200
/*

0 commit comments

Comments
 (0)