Skip to content

Commit c1a19fb

Browse files
torvaldsopsiff
authored andcommitted
ptrace: slightly saner 'get_dumpable()' logic
mainline inclusion from mainline-v7.1-rc4 category: bugfix 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> Link: https://github.com/0xdeadbeefnetwork/ssh-keysign-pwn Link: https://www.phoronix.com/news/Linux-ssh-keysign-pwn Link: https://www.openwall.com/lists/oss-security/2026/05/15/2 Link: https://www.openwall.com/lists/oss-security/2026/05/15/3 Conflicts: include/linux/sched.h (cherry picked from commit 31e62c2ebbfdc3fe3dbdf5e02c92a9dc67087a3a) Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
1 parent 8209bf1 commit c1a19fb

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
@@ -947,6 +947,9 @@ struct task_struct {
947947
unsigned sched_rt_mutex:1;
948948
#endif
949949

950+
/* Save user-dumpable when mm goes away */
951+
unsigned user_dumpable:1;
952+
950953
/* Bit to tell LSMs we're in execve(): */
951954
unsigned in_execve:1;
952955
unsigned in_iowait:1;

kernel/exit.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ static void exit_mm(void)
562562
*/
563563
smp_mb__after_spinlock();
564564
local_irq_disable();
565+
current->user_dumpable = (get_dumpable(mm) == SUID_DUMP_USER);
565566
current->mm = NULL;
566567
#ifdef CONFIG_IEE_PTRP
567568
if(haoc_enabled)

kernel/ptrace.c

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

286+
static bool task_still_dumpable(struct task_struct *task, unsigned int mode)
287+
{
288+
struct mm_struct *mm = task->mm;
289+
if (mm) {
290+
if (get_dumpable(mm) == SUID_DUMP_USER)
291+
return true;
292+
return ptrace_has_cap(mm->user_ns, mode);
293+
}
294+
295+
if (task->user_dumpable)
296+
return true;
297+
return ptrace_has_cap(&init_user_ns, mode);
298+
}
299+
286300
/* Returns 0 on success, -errno on denial. */
287301
static int __ptrace_may_access(struct task_struct *task, unsigned int mode)
288302
{
289303
const struct cred *cred = current_cred(), *tcred;
290-
struct mm_struct *mm;
291304
kuid_t caller_uid;
292305
kgid_t caller_gid;
293306

@@ -348,11 +361,8 @@ static int __ptrace_may_access(struct task_struct *task, unsigned int mode)
348361
* Pairs with a write barrier in commit_creds().
349362
*/
350363
smp_rmb();
351-
mm = task->mm;
352-
if (mm &&
353-
((get_dumpable(mm) != SUID_DUMP_USER) &&
354-
!ptrace_has_cap(mm->user_ns, mode)))
355-
return -EPERM;
364+
if (!task_still_dumpable(task, mode))
365+
return -EPERM;
356366

357367
return security_ptrace_access_check(task, mode);
358368
}

0 commit comments

Comments
 (0)