Skip to content

Commit caaee62

Browse files
thejhtorvalds
authored andcommitted
ptrace: use fsuid, fsgid, effective creds for fs access checks
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). [[email protected]: fix warning] Signed-off-by: Jann Horn <[email protected]> Acked-by: Kees Cook <[email protected]> Cc: Casey Schaufler <[email protected]> Cc: Oleg Nesterov <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: James Morris <[email protected]> Cc: "Serge E. Hallyn" <[email protected]> Cc: Andy Shevchenko <[email protected]> Cc: Andy Lutomirski <[email protected]> Cc: Al Viro <[email protected]> Cc: "Eric W. Biederman" <[email protected]> Cc: Willy Tarreau <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 3dfb7d8 commit caaee62

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)) {
@@ -1860,7 +1861,7 @@ static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
18601861
if (!task)
18611862
goto out_notask;
18621863

1863-
mm = mm_access(task, PTRACE_MODE_READ);
1864+
mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
18641865
if (IS_ERR_OR_NULL(mm))
18651866
goto out;
18661867

@@ -2013,7 +2014,7 @@ static struct dentry *proc_map_files_lookup(struct inode *dir,
20132014
goto out;
20142015

20152016
result = -EACCES;
2016-
if (!ptrace_may_access(task, PTRACE_MODE_READ))
2017+
if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
20172018
goto out_put_task;
20182019

20192020
result = -ENOENT;
@@ -2066,7 +2067,7 @@ proc_map_files_readdir(struct file *file, struct dir_context *ctx)
20662067
goto out;
20672068

20682069
ret = -EACCES;
2069-
if (!ptrace_may_access(task, PTRACE_MODE_READ))
2070+
if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
20702071
goto out_put_task;
20712072

20722073
ret = 0;
@@ -2533,7 +2534,7 @@ static int do_io_accounting(struct task_struct *task, struct seq_file *m, int wh
25332534
if (result)
25342535
return result;
25352536

2536-
if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2537+
if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
25372538
result = -EACCES;
25382539
goto out_unlock;
25392540
}

fs/proc/namespaces.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static const char *proc_ns_get_link(struct dentry *dentry,
4646
if (!task)
4747
return error;
4848

49-
if (ptrace_may_access(task, PTRACE_MODE_READ)) {
49+
if (ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
5050
error = ns_get_path(&ns_path, task, ns_ops);
5151
if (!error)
5252
nd_jump_link(&ns_path);
@@ -67,7 +67,7 @@ static int proc_ns_readlink(struct dentry *dentry, char __user *buffer, int bufl
6767
if (!task)
6868
return res;
6969

70-
if (ptrace_may_access(task, PTRACE_MODE_READ)) {
70+
if (ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
7171
res = ns_get_name(name, sizeof(name), task, ns_ops);
7272
if (res >= 0)
7373
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
@@ -3376,7 +3376,7 @@ find_lively_task_by_vpid(pid_t vpid)
33763376

33773377
/* Reuse ptrace permission checks for now. */
33783378
err = -EACCES;
3379-
if (!ptrace_may_access(task, PTRACE_MODE_READ))
3379+
if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS))
33803380
goto errout;
33813381

33823382
return task;

kernel/futex.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2884,7 +2884,7 @@ SYSCALL_DEFINE3(get_robust_list, int, pid,
28842884
}
28852885

28862886
ret = -EPERM;
2887-
if (!ptrace_may_access(p, PTRACE_MODE_READ))
2887+
if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS))
28882888
goto err_unlock;
28892889

28902890
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)