|
| 1 | +From 31e62c2ebbfdc3fe3dbdf5e02c92a9dc67087a3a Mon Sep 17 00:00:00 2001 |
| 2 | +From: Linus Torvalds <torvalds@linux-foundation.org> |
| 3 | +Date: Wed, 13 May 2026 11:37:18 -0700 |
| 4 | +Subject: [PATCH] ptrace: slightly saner 'get_dumpable()' logic |
| 5 | + |
| 6 | +The 'dumpability' of a task is fundamentally about the memory image of |
| 7 | +the task - the concept comes from whether it can core dump or not - and |
| 8 | +makes no sense when you don't have an associated mm. |
| 9 | + |
| 10 | +And almost all users do in fact use it only for the case where the task |
| 11 | +has a mm pointer. |
| 12 | + |
| 13 | +But we have one odd special case: ptrace_may_access() uses 'dumpable' to |
| 14 | +check various other things entirely independently of the MM (typically |
| 15 | +explicitly using flags like PTRACE_MODE_READ_FSCREDS). Including for |
| 16 | +threads that no longer have a VM (and maybe never did, like most kernel |
| 17 | +threads). |
| 18 | + |
| 19 | +It's not what this flag was designed for, but it is what it is. |
| 20 | + |
| 21 | +The ptrace code does check that the uid/gid matches, so you do have to |
| 22 | +be uid-0 to see kernel thread details, but this means that the |
| 23 | +traditional "drop capabilities" model doesn't make any difference for |
| 24 | +this all. |
| 25 | + |
| 26 | +Make it all make a *bit* more sense by saying that if you don't have a |
| 27 | +MM pointer, we'll use a cached "last dumpability" flag if the thread |
| 28 | +ever had a MM (it will be zero for kernel threads since it is never |
| 29 | +set), and require a proper CAP_SYS_PTRACE capability to override. |
| 30 | + |
| 31 | +Reported-by: Qualys Security Advisory <qsa@qualys.com> |
| 32 | +Cc: Oleg Nesterov <oleg@redhat.com> |
| 33 | +Cc: Kees Cook <kees@kernel.org> |
| 34 | +Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> |
| 35 | +--- |
| 36 | + include/linux/sched.h | 3 +++ |
| 37 | + kernel/exit.c | 1 + |
| 38 | + kernel/ptrace.c | 22 ++++++++++++++++------ |
| 39 | + 3 files changed, 20 insertions(+), 6 deletions(-) |
| 40 | + |
| 41 | +diff --git a/include/linux/sched.h b/include/linux/sched.h |
| 42 | +index 368c7b4d7cb510..ee06cba5c6f538 100644 |
| 43 | +--- a/include/linux/sched.h |
| 44 | ++++ b/include/linux/sched.h |
| 45 | +@@ -1002,6 +1002,9 @@ struct task_struct { |
| 46 | + unsigned sched_rt_mutex:1; |
| 47 | + #endif |
| 48 | + |
| 49 | ++ /* Save user-dumpable when mm goes away */ |
| 50 | ++ unsigned user_dumpable:1; |
| 51 | ++ |
| 52 | + /* Bit to tell TOMOYO we're in execve(): */ |
| 53 | + unsigned in_execve:1; |
| 54 | + unsigned in_iowait:1; |
| 55 | +diff --git a/kernel/exit.c b/kernel/exit.c |
| 56 | +index 9a909993ab1d8b..f50d73c272d6ee 100644 |
| 57 | +--- a/kernel/exit.c |
| 58 | ++++ b/kernel/exit.c |
| 59 | +@@ -571,6 +571,7 @@ static void exit_mm(void) |
| 60 | + */ |
| 61 | + smp_mb__after_spinlock(); |
| 62 | + local_irq_disable(); |
| 63 | ++ current->user_dumpable = (get_dumpable(mm) == SUID_DUMP_USER); |
| 64 | + current->mm = NULL; |
| 65 | + membarrier_update_current_mm(NULL); |
| 66 | + enter_lazy_tlb(mm, current); |
| 67 | +diff --git a/kernel/ptrace.c b/kernel/ptrace.c |
| 68 | +index 68c17daef8d40b..130043bfc2091c 100644 |
| 69 | +--- a/kernel/ptrace.c |
| 70 | ++++ b/kernel/ptrace.c |
| 71 | +@@ -272,11 +272,24 @@ static bool ptrace_has_cap(struct user_namespace *ns, unsigned int mode) |
| 72 | + return ns_capable(ns, CAP_SYS_PTRACE); |
| 73 | + } |
| 74 | + |
| 75 | ++static bool task_still_dumpable(struct task_struct *task, unsigned int mode) |
| 76 | ++{ |
| 77 | ++ struct mm_struct *mm = task->mm; |
| 78 | ++ if (mm) { |
| 79 | ++ if (get_dumpable(mm) == SUID_DUMP_USER) |
| 80 | ++ return true; |
| 81 | ++ return ptrace_has_cap(mm->user_ns, mode); |
| 82 | ++ } |
| 83 | ++ |
| 84 | ++ if (task->user_dumpable) |
| 85 | ++ return true; |
| 86 | ++ return ptrace_has_cap(&init_user_ns, mode); |
| 87 | ++} |
| 88 | ++ |
| 89 | + /* Returns 0 on success, -errno on denial. */ |
| 90 | + static int __ptrace_may_access(struct task_struct *task, unsigned int mode) |
| 91 | + { |
| 92 | + const struct cred *cred = current_cred(), *tcred; |
| 93 | +- struct mm_struct *mm; |
| 94 | + kuid_t caller_uid; |
| 95 | + kgid_t caller_gid; |
| 96 | + |
| 97 | +@@ -337,11 +350,8 @@ static int __ptrace_may_access(struct task_struct *task, unsigned int mode) |
| 98 | + * Pairs with a write barrier in commit_creds(). |
| 99 | + */ |
| 100 | + smp_rmb(); |
| 101 | +- mm = task->mm; |
| 102 | +- if (mm && |
| 103 | +- ((get_dumpable(mm) != SUID_DUMP_USER) && |
| 104 | +- !ptrace_has_cap(mm->user_ns, mode))) |
| 105 | +- return -EPERM; |
| 106 | ++ if (!task_still_dumpable(task, mode)) |
| 107 | ++ return -EPERM; |
| 108 | + |
| 109 | + return security_ptrace_access_check(task, mode); |
| 110 | + } |
0 commit comments