Skip to content

Commit ab88ce5

Browse files
thejhsashalevin
authored andcommitted
ptrace: use fsuid, fsgid, effective creds for fs access checks
[ Upstream commit caaee62 ] 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]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 93d310f commit ab88ce5

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
@@ -384,7 +384,7 @@ static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
384384

385385
state = *get_task_state(task);
386386
vsize = eip = esp = 0;
387-
permitted = ptrace_may_access(task, PTRACE_MODE_READ | PTRACE_MODE_NOAUDIT);
387+
permitted = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS | PTRACE_MODE_NOAUDIT);
388388
mm = get_task_mm(task);
389389
if (mm) {
390390
vsize = task_vsize(mm);

fs/proc/base.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ static int proc_pid_cmdline(struct seq_file *m, struct pid_namespace *ns,
211211
static int proc_pid_auxv(struct seq_file *m, struct pid_namespace *ns,
212212
struct pid *pid, struct task_struct *task)
213213
{
214-
struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
214+
struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
215215
if (mm && !IS_ERR(mm)) {
216216
unsigned int nwords = 0;
217217
do {
@@ -238,7 +238,8 @@ static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
238238

239239
wchan = get_wchan(task);
240240

241-
if (wchan && ptrace_may_access(task, PTRACE_MODE_READ) && !lookup_symbol_name(wchan, symname))
241+
if (wchan && ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)
242+
&& !lookup_symbol_name(wchan, symname))
242243
seq_printf(m, "%s", symname);
243244
else
244245
seq_putc(m, '0');
@@ -252,7 +253,7 @@ static int lock_trace(struct task_struct *task)
252253
int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
253254
if (err)
254255
return err;
255-
if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
256+
if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
256257
mutex_unlock(&task->signal->cred_guard_mutex);
257258
return -EPERM;
258259
}
@@ -502,7 +503,7 @@ static int proc_fd_access_allowed(struct inode *inode)
502503
*/
503504
task = get_proc_task(inode);
504505
if (task) {
505-
allowed = ptrace_may_access(task, PTRACE_MODE_READ);
506+
allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
506507
put_task_struct(task);
507508
}
508509
return allowed;
@@ -537,7 +538,7 @@ static bool has_pid_permissions(struct pid_namespace *pid,
537538
return true;
538539
if (in_group_p(pid->pid_gid))
539540
return true;
540-
return ptrace_may_access(task, PTRACE_MODE_READ);
541+
return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
541542
}
542543

543544

@@ -614,7 +615,7 @@ struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
614615
struct mm_struct *mm = ERR_PTR(-ESRCH);
615616

616617
if (task) {
617-
mm = mm_access(task, mode);
618+
mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
618619
put_task_struct(task);
619620

620621
if (!IS_ERR_OR_NULL(mm)) {
@@ -1676,7 +1677,7 @@ static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
16761677
if (!task)
16771678
goto out_notask;
16781679

1679-
mm = mm_access(task, PTRACE_MODE_READ);
1680+
mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
16801681
if (IS_ERR_OR_NULL(mm))
16811682
goto out;
16821683

@@ -1808,7 +1809,7 @@ static struct dentry *proc_map_files_lookup(struct inode *dir,
18081809
goto out;
18091810

18101811
result = -EACCES;
1811-
if (!ptrace_may_access(task, PTRACE_MODE_READ))
1812+
if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
18121813
goto out_put_task;
18131814

18141815
result = -ENOENT;
@@ -1865,7 +1866,7 @@ proc_map_files_readdir(struct file *file, struct dir_context *ctx)
18651866
goto out;
18661867

18671868
ret = -EACCES;
1868-
if (!ptrace_may_access(task, PTRACE_MODE_READ))
1869+
if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
18691870
goto out_put_task;
18701871

18711872
ret = 0;
@@ -2345,7 +2346,7 @@ static int do_io_accounting(struct task_struct *task, struct seq_file *m, int wh
23452346
if (result)
23462347
return result;
23472348

2348-
if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2349+
if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
23492350
result = -EACCES;
23502351
goto out_unlock;
23512352
}

fs/proc/namespaces.c

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

6284
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
@@ -3314,7 +3314,7 @@ find_lively_task_by_vpid(pid_t vpid)
33143314

33153315
/* Reuse ptrace permission checks for now. */
33163316
err = -EACCES;
3317-
if (!ptrace_may_access(task, PTRACE_MODE_READ))
3317+
if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS))
33183318
goto errout;
33193319

33203320
return task;

kernel/futex.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2763,7 +2763,7 @@ SYSCALL_DEFINE3(get_robust_list, int, pid,
27632763
}
27642764

27652765
ret = -EPERM;
2766-
if (!ptrace_may_access(p, PTRACE_MODE_READ))
2766+
if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS))
27672767
goto err_unlock;
27682768

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