Skip to content

Commit 01363cb

Browse files
torvaldsgregkh
authored andcommitted
ptrace: slightly saner 'get_dumpable()' logic
commit 31e62c2 upstream. The 'dumpability' of a task is fundamentally about the memory image of the task - the concept comes from whether it can core dump or not - and makes no sense when you don't have an associated mm. And almost all users do in fact use it only for the case where the task has a mm pointer. But we have one odd special case: ptrace_may_access() uses 'dumpable' to check various other things entirely independently of the MM (typically explicitly using flags like PTRACE_MODE_READ_FSCREDS). Including for threads that no longer have a VM (and maybe never did, like most kernel threads). It's not what this flag was designed for, but it is what it is. The ptrace code does check that the uid/gid matches, so you do have to be uid-0 to see kernel thread details, but this means that the traditional "drop capabilities" model doesn't make any difference for this all. Make it all make a *bit* more sense by saying that if you don't have a MM pointer, we'll use a cached "last dumpability" flag if the thread ever had a MM (it will be zero for kernel threads since it is never set), and require a proper CAP_SYS_PTRACE capability to override. Reported-by: Qualys Security Advisory <qsa@qualys.com> Cc: Oleg Nesterov <oleg@redhat.com> Cc: Kees Cook <kees@kernel.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 4d7dbcd commit 01363cb

3 files changed

Lines changed: 20 additions & 6 deletions

File tree

include/linux/sched.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,9 @@ struct task_struct {
998998
unsigned sched_rt_mutex:1;
999999
#endif
10001000

1001+
/* Save user-dumpable when mm goes away */
1002+
unsigned user_dumpable:1;
1003+
10011004
/* Bit to tell TOMOYO we're in execve(): */
10021005
unsigned in_execve:1;
10031006
unsigned in_iowait:1;

kernel/exit.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ static void exit_mm(void)
571571
*/
572572
smp_mb__after_spinlock();
573573
local_irq_disable();
574+
current->user_dumpable = (get_dumpable(mm) == SUID_DUMP_USER);
574575
current->mm = NULL;
575576
membarrier_update_current_mm(NULL);
576577
enter_lazy_tlb(mm, current);

kernel/ptrace.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,24 @@ static bool ptrace_has_cap(struct user_namespace *ns, unsigned int mode)
272272
return ns_capable(ns, CAP_SYS_PTRACE);
273273
}
274274

275+
static bool task_still_dumpable(struct task_struct *task, unsigned int mode)
276+
{
277+
struct mm_struct *mm = task->mm;
278+
if (mm) {
279+
if (get_dumpable(mm) == SUID_DUMP_USER)
280+
return true;
281+
return ptrace_has_cap(mm->user_ns, mode);
282+
}
283+
284+
if (task->user_dumpable)
285+
return true;
286+
return ptrace_has_cap(&init_user_ns, mode);
287+
}
288+
275289
/* Returns 0 on success, -errno on denial. */
276290
static int __ptrace_may_access(struct task_struct *task, unsigned int mode)
277291
{
278292
const struct cred *cred = current_cred(), *tcred;
279-
struct mm_struct *mm;
280293
kuid_t caller_uid;
281294
kgid_t caller_gid;
282295

@@ -337,11 +350,8 @@ static int __ptrace_may_access(struct task_struct *task, unsigned int mode)
337350
* Pairs with a write barrier in commit_creds().
338351
*/
339352
smp_rmb();
340-
mm = task->mm;
341-
if (mm &&
342-
((get_dumpable(mm) != SUID_DUMP_USER) &&
343-
!ptrace_has_cap(mm->user_ns, mode)))
344-
return -EPERM;
353+
if (!task_still_dumpable(task, mode))
354+
return -EPERM;
345355

346356
return security_ptrace_access_check(task, mode);
347357
}

0 commit comments

Comments
 (0)