1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/kernel/ptrace.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * (C) Copyright 1999 Linus Torvalds 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Common interfaces for "ptrace()" which we do not want 81da177e4SLinus Torvalds * to continually duplicate across every architecture. 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 11c59ede7bSRandy.Dunlap #include <linux/capability.h> 129984de1aSPaul Gortmaker #include <linux/export.h> 131da177e4SLinus Torvalds #include <linux/sched.h> 146e84f315SIngo Molnar #include <linux/sched/mm.h> 15f7ccbae4SIngo Molnar #include <linux/sched/coredump.h> 1629930025SIngo Molnar #include <linux/sched/task.h> 171da177e4SLinus Torvalds #include <linux/errno.h> 181da177e4SLinus Torvalds #include <linux/mm.h> 191da177e4SLinus Torvalds #include <linux/highmem.h> 201da177e4SLinus Torvalds #include <linux/pagemap.h> 211da177e4SLinus Torvalds #include <linux/ptrace.h> 221da177e4SLinus Torvalds #include <linux/security.h> 237ed20e1aSJesper Juhl #include <linux/signal.h> 24a27bb332SKent Overstreet #include <linux/uio.h> 25a5cb013dSAl Viro #include <linux/audit.h> 26b488893aSPavel Emelyanov #include <linux/pid_namespace.h> 27f17d30a8SAdrian Bunk #include <linux/syscalls.h> 283a709703SRoland McGrath #include <linux/uaccess.h> 292225a122SSuresh Siddha #include <linux/regset.h> 30bf26c018SFrederic Weisbecker #include <linux/hw_breakpoint.h> 31f701e5b7SVladimir Zapolskiy #include <linux/cn_proc.h> 3284c751bdSAndrey Vagin #include <linux/compat.h> 33fcfc2aa0SAndrei Vagin #include <linux/sched/signal.h> 341da177e4SLinus Torvalds 3584d77d3fSEric W. Biederman /* 3684d77d3fSEric W. Biederman * Access another process' address space via ptrace. 3784d77d3fSEric W. Biederman * Source/target buffer must be kernel space, 3884d77d3fSEric W. Biederman * Do not walk the page table directly, use get_user_pages 3984d77d3fSEric W. Biederman */ 4084d77d3fSEric W. Biederman int ptrace_access_vm(struct task_struct *tsk, unsigned long addr, 4184d77d3fSEric W. Biederman void *buf, int len, unsigned int gup_flags) 4284d77d3fSEric W. Biederman { 4384d77d3fSEric W. Biederman struct mm_struct *mm; 4484d77d3fSEric W. Biederman int ret; 4584d77d3fSEric W. Biederman 4684d77d3fSEric W. Biederman mm = get_task_mm(tsk); 4784d77d3fSEric W. Biederman if (!mm) 4884d77d3fSEric W. Biederman return 0; 4984d77d3fSEric W. Biederman 5084d77d3fSEric W. Biederman if (!tsk->ptrace || 5184d77d3fSEric W. Biederman (current != tsk->parent) || 5284d77d3fSEric W. Biederman ((get_dumpable(mm) != SUID_DUMP_USER) && 5384d77d3fSEric W. Biederman !ptracer_capable(tsk, mm->user_ns))) { 5484d77d3fSEric W. Biederman mmput(mm); 5584d77d3fSEric W. Biederman return 0; 5684d77d3fSEric W. Biederman } 5784d77d3fSEric W. Biederman 5884d77d3fSEric W. Biederman ret = __access_remote_vm(tsk, mm, addr, buf, len, gup_flags); 5984d77d3fSEric W. Biederman mmput(mm); 6084d77d3fSEric W. Biederman 6184d77d3fSEric W. Biederman return ret; 6284d77d3fSEric W. Biederman } 6384d77d3fSEric W. Biederman 64bf53de90SMarkus Metzger 65c70d9d80SEric W. Biederman void __ptrace_link(struct task_struct *child, struct task_struct *new_parent, 66c70d9d80SEric W. Biederman const struct cred *ptracer_cred) 67c70d9d80SEric W. Biederman { 68c70d9d80SEric W. Biederman BUG_ON(!list_empty(&child->ptrace_entry)); 69c70d9d80SEric W. Biederman list_add(&child->ptrace_entry, &new_parent->ptraced); 70c70d9d80SEric W. Biederman child->parent = new_parent; 71c70d9d80SEric W. Biederman child->ptracer_cred = get_cred(ptracer_cred); 72c70d9d80SEric W. Biederman } 73c70d9d80SEric W. Biederman 74bf53de90SMarkus Metzger /* 751da177e4SLinus Torvalds * ptrace a task: make the debugger its new parent and 761da177e4SLinus Torvalds * move it to the ptrace list. 771da177e4SLinus Torvalds * 781da177e4SLinus Torvalds * Must be called with the tasklist lock write-held. 791da177e4SLinus Torvalds */ 80c70d9d80SEric W. Biederman static void ptrace_link(struct task_struct *child, struct task_struct *new_parent) 811da177e4SLinus Torvalds { 826994eefbSJann Horn __ptrace_link(child, new_parent, current_cred()); 831da177e4SLinus Torvalds } 841da177e4SLinus Torvalds 85e3bd058fSTejun Heo /** 86e3bd058fSTejun Heo * __ptrace_unlink - unlink ptracee and restore its execution state 87e3bd058fSTejun Heo * @child: ptracee to be unlinked 881da177e4SLinus Torvalds * 890e9f0a4aSTejun Heo * Remove @child from the ptrace list, move it back to the original parent, 900e9f0a4aSTejun Heo * and restore the execution state so that it conforms to the group stop 910e9f0a4aSTejun Heo * state. 920e9f0a4aSTejun Heo * 930e9f0a4aSTejun Heo * Unlinking can happen via two paths - explicit PTRACE_DETACH or ptracer 940e9f0a4aSTejun Heo * exiting. For PTRACE_DETACH, unless the ptracee has been killed between 950e9f0a4aSTejun Heo * ptrace_check_attach() and here, it's guaranteed to be in TASK_TRACED. 960e9f0a4aSTejun Heo * If the ptracer is exiting, the ptracee can be in any state. 970e9f0a4aSTejun Heo * 980e9f0a4aSTejun Heo * After detach, the ptracee should be in a state which conforms to the 990e9f0a4aSTejun Heo * group stop. If the group is stopped or in the process of stopping, the 1000e9f0a4aSTejun Heo * ptracee should be put into TASK_STOPPED; otherwise, it should be woken 1010e9f0a4aSTejun Heo * up from TASK_TRACED. 1020e9f0a4aSTejun Heo * 1030e9f0a4aSTejun Heo * If the ptracee is in TASK_TRACED and needs to be moved to TASK_STOPPED, 1040e9f0a4aSTejun Heo * it goes through TRACED -> RUNNING -> STOPPED transition which is similar 1050e9f0a4aSTejun Heo * to but in the opposite direction of what happens while attaching to a 1060e9f0a4aSTejun Heo * stopped task. However, in this direction, the intermediate RUNNING 1070e9f0a4aSTejun Heo * state is not hidden even from the current ptracer and if it immediately 1080e9f0a4aSTejun Heo * re-attaches and performs a WNOHANG wait(2), it may fail. 109e3bd058fSTejun Heo * 110e3bd058fSTejun Heo * CONTEXT: 111e3bd058fSTejun Heo * write_lock_irq(tasklist_lock) 1121da177e4SLinus Torvalds */ 11336c8b586SIngo Molnar void __ptrace_unlink(struct task_struct *child) 1141da177e4SLinus Torvalds { 11564b875f7SEric W. Biederman const struct cred *old_cred; 1165ecfbae0SOleg Nesterov BUG_ON(!child->ptrace); 1175ecfbae0SOleg Nesterov 1180a5bf409SAles Novak clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); 1190a5bf409SAles Novak 1201da177e4SLinus Torvalds child->parent = child->real_parent; 121f470021aSRoland McGrath list_del_init(&child->ptrace_entry); 12264b875f7SEric W. Biederman old_cred = child->ptracer_cred; 12364b875f7SEric W. Biederman child->ptracer_cred = NULL; 12464b875f7SEric W. Biederman put_cred(old_cred); 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds spin_lock(&child->sighand->siglock); 1271333ab03SOleg Nesterov child->ptrace = 0; 1281da177e4SLinus Torvalds /* 12973ddff2bSTejun Heo * Clear all pending traps and TRAPPING. TRAPPING should be 13073ddff2bSTejun Heo * cleared regardless of JOBCTL_STOP_PENDING. Do it explicitly. 13173ddff2bSTejun Heo */ 13273ddff2bSTejun Heo task_clear_jobctl_pending(child, JOBCTL_TRAP_MASK); 13373ddff2bSTejun Heo task_clear_jobctl_trapping(child); 13473ddff2bSTejun Heo 13573ddff2bSTejun Heo /* 136a8f072c1STejun Heo * Reinstate JOBCTL_STOP_PENDING if group stop is in effect and 1370e9f0a4aSTejun Heo * @child isn't dead. 1381da177e4SLinus Torvalds */ 1390e9f0a4aSTejun Heo if (!(child->flags & PF_EXITING) && 1400e9f0a4aSTejun Heo (child->signal->flags & SIGNAL_STOP_STOPPED || 1418a88951bSOleg Nesterov child->signal->group_stop_count)) { 142a8f072c1STejun Heo child->jobctl |= JOBCTL_STOP_PENDING; 1430e9f0a4aSTejun Heo 1440e9f0a4aSTejun Heo /* 1458a88951bSOleg Nesterov * This is only possible if this thread was cloned by the 1468a88951bSOleg Nesterov * traced task running in the stopped group, set the signal 1478a88951bSOleg Nesterov * for the future reports. 1488a88951bSOleg Nesterov * FIXME: we should change ptrace_init_task() to handle this 1498a88951bSOleg Nesterov * case. 1508a88951bSOleg Nesterov */ 1518a88951bSOleg Nesterov if (!(child->jobctl & JOBCTL_STOP_SIGMASK)) 1528a88951bSOleg Nesterov child->jobctl |= SIGSTOP; 1538a88951bSOleg Nesterov } 1548a88951bSOleg Nesterov 1558a88951bSOleg Nesterov /* 1560e9f0a4aSTejun Heo * If transition to TASK_STOPPED is pending or in TASK_TRACED, kick 1570e9f0a4aSTejun Heo * @child in the butt. Note that @resume should be used iff @child 1580e9f0a4aSTejun Heo * is in TASK_TRACED; otherwise, we might unduly disrupt 1590e9f0a4aSTejun Heo * TASK_KILLABLE sleeps. 1600e9f0a4aSTejun Heo */ 161a8f072c1STejun Heo if (child->jobctl & JOBCTL_STOP_PENDING || task_is_traced(child)) 162910ffdb1SOleg Nesterov ptrace_signal_wake_up(child, true); 1630e9f0a4aSTejun Heo 1641da177e4SLinus Torvalds spin_unlock(&child->sighand->siglock); 1651da177e4SLinus Torvalds } 1661da177e4SLinus Torvalds 1679899d11fSOleg Nesterov /* Ensure that nothing can wake it up, even SIGKILL */ 1689899d11fSOleg Nesterov static bool ptrace_freeze_traced(struct task_struct *task) 1699899d11fSOleg Nesterov { 1709899d11fSOleg Nesterov bool ret = false; 1719899d11fSOleg Nesterov 1729899d11fSOleg Nesterov /* Lockless, nobody but us can set this flag */ 1739899d11fSOleg Nesterov if (task->jobctl & JOBCTL_LISTENING) 1749899d11fSOleg Nesterov return ret; 1759899d11fSOleg Nesterov 1769899d11fSOleg Nesterov spin_lock_irq(&task->sighand->siglock); 1779899d11fSOleg Nesterov if (task_is_traced(task) && !__fatal_signal_pending(task)) { 1789899d11fSOleg Nesterov task->state = __TASK_TRACED; 1799899d11fSOleg Nesterov ret = true; 1809899d11fSOleg Nesterov } 1819899d11fSOleg Nesterov spin_unlock_irq(&task->sighand->siglock); 1829899d11fSOleg Nesterov 1839899d11fSOleg Nesterov return ret; 1849899d11fSOleg Nesterov } 1859899d11fSOleg Nesterov 1869899d11fSOleg Nesterov static void ptrace_unfreeze_traced(struct task_struct *task) 1879899d11fSOleg Nesterov { 1889899d11fSOleg Nesterov if (task->state != __TASK_TRACED) 1899899d11fSOleg Nesterov return; 1909899d11fSOleg Nesterov 1919899d11fSOleg Nesterov WARN_ON(!task->ptrace || task->parent != current); 1929899d11fSOleg Nesterov 1935402e97aSbsegall@google.com /* 1945402e97aSbsegall@google.com * PTRACE_LISTEN can allow ptrace_trap_notify to wake us up remotely. 1955402e97aSbsegall@google.com * Recheck state under the lock to close this race. 1965402e97aSbsegall@google.com */ 1979899d11fSOleg Nesterov spin_lock_irq(&task->sighand->siglock); 1985402e97aSbsegall@google.com if (task->state == __TASK_TRACED) { 1999899d11fSOleg Nesterov if (__fatal_signal_pending(task)) 2009899d11fSOleg Nesterov wake_up_state(task, __TASK_TRACED); 2019899d11fSOleg Nesterov else 2029899d11fSOleg Nesterov task->state = TASK_TRACED; 2035402e97aSbsegall@google.com } 2049899d11fSOleg Nesterov spin_unlock_irq(&task->sighand->siglock); 2059899d11fSOleg Nesterov } 2069899d11fSOleg Nesterov 207755e276bSTejun Heo /** 208755e276bSTejun Heo * ptrace_check_attach - check whether ptracee is ready for ptrace operation 209755e276bSTejun Heo * @child: ptracee to check for 210755e276bSTejun Heo * @ignore_state: don't check whether @child is currently %TASK_TRACED 211755e276bSTejun Heo * 212755e276bSTejun Heo * Check whether @child is being ptraced by %current and ready for further 213755e276bSTejun Heo * ptrace operations. If @ignore_state is %false, @child also should be in 214755e276bSTejun Heo * %TASK_TRACED state and on return the child is guaranteed to be traced 215755e276bSTejun Heo * and not executing. If @ignore_state is %true, @child can be in any 216755e276bSTejun Heo * state. 217755e276bSTejun Heo * 218755e276bSTejun Heo * CONTEXT: 219755e276bSTejun Heo * Grabs and releases tasklist_lock and @child->sighand->siglock. 220755e276bSTejun Heo * 221755e276bSTejun Heo * RETURNS: 222755e276bSTejun Heo * 0 on success, -ESRCH if %child is not ready. 2231da177e4SLinus Torvalds */ 224edea0d03SOleg Nesterov static int ptrace_check_attach(struct task_struct *child, bool ignore_state) 2251da177e4SLinus Torvalds { 2261da177e4SLinus Torvalds int ret = -ESRCH; 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds /* 2291da177e4SLinus Torvalds * We take the read lock around doing both checks to close a 2301da177e4SLinus Torvalds * possible race where someone else was tracing our child and 2311da177e4SLinus Torvalds * detached between these two checks. After this locked check, 2321da177e4SLinus Torvalds * we are sure that this is our traced child and that can only 2331da177e4SLinus Torvalds * be changed by us so it's not changing right after this. 2341da177e4SLinus Torvalds */ 2351da177e4SLinus Torvalds read_lock(&tasklist_lock); 2369899d11fSOleg Nesterov if (child->ptrace && child->parent == current) { 2379899d11fSOleg Nesterov WARN_ON(child->state == __TASK_TRACED); 238c0c0b649SOleg Nesterov /* 239c0c0b649SOleg Nesterov * child->sighand can't be NULL, release_task() 240c0c0b649SOleg Nesterov * does ptrace_unlink() before __exit_signal(). 241c0c0b649SOleg Nesterov */ 2429899d11fSOleg Nesterov if (ignore_state || ptrace_freeze_traced(child)) 243321fb561SOleg Nesterov ret = 0; 2441da177e4SLinus Torvalds } 2451da177e4SLinus Torvalds read_unlock(&tasklist_lock); 2461da177e4SLinus Torvalds 2479899d11fSOleg Nesterov if (!ret && !ignore_state) { 2489899d11fSOleg Nesterov if (!wait_task_inactive(child, __TASK_TRACED)) { 2499899d11fSOleg Nesterov /* 2509899d11fSOleg Nesterov * This can only happen if may_ptrace_stop() fails and 2519899d11fSOleg Nesterov * ptrace_stop() changes ->state back to TASK_RUNNING, 2529899d11fSOleg Nesterov * so we should not worry about leaking __TASK_TRACED. 2539899d11fSOleg Nesterov */ 2549899d11fSOleg Nesterov WARN_ON(child->state == __TASK_TRACED); 2559899d11fSOleg Nesterov ret = -ESRCH; 2569899d11fSOleg Nesterov } 2579899d11fSOleg Nesterov } 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds return ret; 2601da177e4SLinus Torvalds } 2611da177e4SLinus Torvalds 26269f594a3SEric Paris static int ptrace_has_cap(struct user_namespace *ns, unsigned int mode) 26369f594a3SEric Paris { 26469f594a3SEric Paris if (mode & PTRACE_MODE_NOAUDIT) 26569f594a3SEric Paris return has_ns_capability_noaudit(current, ns, CAP_SYS_PTRACE); 26669f594a3SEric Paris else 26769f594a3SEric Paris return has_ns_capability(current, ns, CAP_SYS_PTRACE); 26869f594a3SEric Paris } 26969f594a3SEric Paris 2709f99798fSTetsuo Handa /* Returns 0 on success, -errno on denial. */ 2719f99798fSTetsuo Handa static int __ptrace_may_access(struct task_struct *task, unsigned int mode) 272ab8d11beSMiklos Szeredi { 273c69e8d9cSDavid Howells const struct cred *cred = current_cred(), *tcred; 274bfedb589SEric W. Biederman struct mm_struct *mm; 275caaee623SJann Horn kuid_t caller_uid; 276caaee623SJann Horn kgid_t caller_gid; 277caaee623SJann Horn 278caaee623SJann Horn if (!(mode & PTRACE_MODE_FSCREDS) == !(mode & PTRACE_MODE_REALCREDS)) { 279caaee623SJann Horn WARN(1, "denying ptrace access check without PTRACE_MODE_*CREDS\n"); 280caaee623SJann Horn return -EPERM; 281caaee623SJann Horn } 282b6dff3ecSDavid Howells 283df26c40eSEric W. Biederman /* May we inspect the given task? 284df26c40eSEric W. Biederman * This check is used both for attaching with ptrace 285df26c40eSEric W. Biederman * and for allowing access to sensitive information in /proc. 286df26c40eSEric W. Biederman * 287df26c40eSEric W. Biederman * ptrace_attach denies several cases that /proc allows 288df26c40eSEric W. Biederman * because setting up the necessary parent/child relationship 289df26c40eSEric W. Biederman * or halting the specified task is impossible. 290df26c40eSEric W. Biederman */ 291caaee623SJann Horn 292df26c40eSEric W. Biederman /* Don't let security modules deny introspection */ 29373af963fSMark Grondona if (same_thread_group(task, current)) 294df26c40eSEric W. Biederman return 0; 295c69e8d9cSDavid Howells rcu_read_lock(); 296caaee623SJann Horn if (mode & PTRACE_MODE_FSCREDS) { 297caaee623SJann Horn caller_uid = cred->fsuid; 298caaee623SJann Horn caller_gid = cred->fsgid; 299caaee623SJann Horn } else { 300caaee623SJann Horn /* 301caaee623SJann Horn * Using the euid would make more sense here, but something 302caaee623SJann Horn * in userland might rely on the old behavior, and this 303caaee623SJann Horn * shouldn't be a security problem since 304caaee623SJann Horn * PTRACE_MODE_REALCREDS implies that the caller explicitly 305caaee623SJann Horn * used a syscall that requests access to another process 306caaee623SJann Horn * (and not a filesystem syscall to procfs). 307caaee623SJann Horn */ 308caaee623SJann Horn caller_uid = cred->uid; 309caaee623SJann Horn caller_gid = cred->gid; 310caaee623SJann Horn } 311c69e8d9cSDavid Howells tcred = __task_cred(task); 312caaee623SJann Horn if (uid_eq(caller_uid, tcred->euid) && 313caaee623SJann Horn uid_eq(caller_uid, tcred->suid) && 314caaee623SJann Horn uid_eq(caller_uid, tcred->uid) && 315caaee623SJann Horn gid_eq(caller_gid, tcred->egid) && 316caaee623SJann Horn gid_eq(caller_gid, tcred->sgid) && 317caaee623SJann Horn gid_eq(caller_gid, tcred->gid)) 3188409cca7SSerge E. Hallyn goto ok; 319c4a4d603SEric W. Biederman if (ptrace_has_cap(tcred->user_ns, mode)) 3208409cca7SSerge E. Hallyn goto ok; 321c69e8d9cSDavid Howells rcu_read_unlock(); 322ab8d11beSMiklos Szeredi return -EPERM; 3238409cca7SSerge E. Hallyn ok: 324c69e8d9cSDavid Howells rcu_read_unlock(); 325f6581f5bSJann Horn /* 326f6581f5bSJann Horn * If a task drops privileges and becomes nondumpable (through a syscall 327f6581f5bSJann Horn * like setresuid()) while we are trying to access it, we must ensure 328f6581f5bSJann Horn * that the dumpability is read after the credentials; otherwise, 329f6581f5bSJann Horn * we may be able to attach to a task that we shouldn't be able to 330f6581f5bSJann Horn * attach to (as if the task had dropped privileges without becoming 331f6581f5bSJann Horn * nondumpable). 332f6581f5bSJann Horn * Pairs with a write barrier in commit_creds(). 333f6581f5bSJann Horn */ 334f6581f5bSJann Horn smp_rmb(); 335bfedb589SEric W. Biederman mm = task->mm; 336bfedb589SEric W. Biederman if (mm && 337bfedb589SEric W. Biederman ((get_dumpable(mm) != SUID_DUMP_USER) && 338bfedb589SEric W. Biederman !ptrace_has_cap(mm->user_ns, mode))) 339ab8d11beSMiklos Szeredi return -EPERM; 340ab8d11beSMiklos Szeredi 3419e48858fSIngo Molnar return security_ptrace_access_check(task, mode); 342ab8d11beSMiklos Szeredi } 343ab8d11beSMiklos Szeredi 344006ebb40SStephen Smalley bool ptrace_may_access(struct task_struct *task, unsigned int mode) 345ab8d11beSMiklos Szeredi { 346ab8d11beSMiklos Szeredi int err; 347ab8d11beSMiklos Szeredi task_lock(task); 348006ebb40SStephen Smalley err = __ptrace_may_access(task, mode); 349ab8d11beSMiklos Szeredi task_unlock(task); 3503a709703SRoland McGrath return !err; 351ab8d11beSMiklos Szeredi } 352ab8d11beSMiklos Szeredi 3533544d72aSTejun Heo static int ptrace_attach(struct task_struct *task, long request, 354aa9147c9SDenys Vlasenko unsigned long addr, 3553544d72aSTejun Heo unsigned long flags) 3561da177e4SLinus Torvalds { 3573544d72aSTejun Heo bool seize = (request == PTRACE_SEIZE); 3581da177e4SLinus Torvalds int retval; 359f5b40e36SLinus Torvalds 3603544d72aSTejun Heo retval = -EIO; 361aa9147c9SDenys Vlasenko if (seize) { 362aa9147c9SDenys Vlasenko if (addr != 0) 3633544d72aSTejun Heo goto out; 364aa9147c9SDenys Vlasenko if (flags & ~(unsigned long)PTRACE_O_MASK) 365aa9147c9SDenys Vlasenko goto out; 366aa9147c9SDenys Vlasenko flags = PT_PTRACED | PT_SEIZED | (flags << PT_OPT_FLAG_SHIFT); 367aa9147c9SDenys Vlasenko } else { 368aa9147c9SDenys Vlasenko flags = PT_PTRACED; 369aa9147c9SDenys Vlasenko } 3703544d72aSTejun Heo 371a5cb013dSAl Viro audit_ptrace(task); 372a5cb013dSAl Viro 3731da177e4SLinus Torvalds retval = -EPERM; 374b79b7ba9SOleg Nesterov if (unlikely(task->flags & PF_KTHREAD)) 375b79b7ba9SOleg Nesterov goto out; 376bac0abd6SPavel Emelyanov if (same_thread_group(task, current)) 377f5b40e36SLinus Torvalds goto out; 378f5b40e36SLinus Torvalds 379f2f0b00aSOleg Nesterov /* 380f2f0b00aSOleg Nesterov * Protect exec's credential calculations against our interference; 38186b6c1f3SDenys Vlasenko * SUID, SGID and LSM creds get determined differently 3825e751e99SDavid Howells * under ptrace. 383d84f4f99SDavid Howells */ 384793285fcSOleg Nesterov retval = -ERESTARTNOINTR; 3859b1bf12dSKOSAKI Motohiro if (mutex_lock_interruptible(&task->signal->cred_guard_mutex)) 386d84f4f99SDavid Howells goto out; 3874b105cbbSOleg Nesterov 388f5b40e36SLinus Torvalds task_lock(task); 389caaee623SJann Horn retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH_REALCREDS); 3904b105cbbSOleg Nesterov task_unlock(task); 3911da177e4SLinus Torvalds if (retval) 3924b105cbbSOleg Nesterov goto unlock_creds; 3931da177e4SLinus Torvalds 3944b105cbbSOleg Nesterov write_lock_irq(&tasklist_lock); 395b79b7ba9SOleg Nesterov retval = -EPERM; 396b79b7ba9SOleg Nesterov if (unlikely(task->exit_state)) 3974b105cbbSOleg Nesterov goto unlock_tasklist; 398f2f0b00aSOleg Nesterov if (task->ptrace) 3994b105cbbSOleg Nesterov goto unlock_tasklist; 400b79b7ba9SOleg Nesterov 4013544d72aSTejun Heo if (seize) 402aa9147c9SDenys Vlasenko flags |= PT_SEIZED; 403aa9147c9SDenys Vlasenko task->ptrace = flags; 4041da177e4SLinus Torvalds 405c70d9d80SEric W. Biederman ptrace_link(task, current); 4063544d72aSTejun Heo 4073544d72aSTejun Heo /* SEIZE doesn't trap tracee on attach */ 4083544d72aSTejun Heo if (!seize) 409079b22dcSEric W. Biederman send_sig_info(SIGSTOP, SEND_SIG_PRIV, task); 410b79b7ba9SOleg Nesterov 411d79fdd6dSTejun Heo spin_lock(&task->sighand->siglock); 412d79fdd6dSTejun Heo 413d79fdd6dSTejun Heo /* 41473ddff2bSTejun Heo * If the task is already STOPPED, set JOBCTL_TRAP_STOP and 415d79fdd6dSTejun Heo * TRAPPING, and kick it so that it transits to TRACED. TRAPPING 416d79fdd6dSTejun Heo * will be cleared if the child completes the transition or any 417d79fdd6dSTejun Heo * event which clears the group stop states happens. We'll wait 418d79fdd6dSTejun Heo * for the transition to complete before returning from this 419d79fdd6dSTejun Heo * function. 420d79fdd6dSTejun Heo * 421d79fdd6dSTejun Heo * This hides STOPPED -> RUNNING -> TRACED transition from the 422d79fdd6dSTejun Heo * attaching thread but a different thread in the same group can 423d79fdd6dSTejun Heo * still observe the transient RUNNING state. IOW, if another 424d79fdd6dSTejun Heo * thread's WNOHANG wait(2) on the stopped tracee races against 425d79fdd6dSTejun Heo * ATTACH, the wait(2) may fail due to the transient RUNNING. 426d79fdd6dSTejun Heo * 427d79fdd6dSTejun Heo * The following task_is_stopped() test is safe as both transitions 428d79fdd6dSTejun Heo * in and out of STOPPED are protected by siglock. 429d79fdd6dSTejun Heo */ 4307dd3db54STejun Heo if (task_is_stopped(task) && 43173ddff2bSTejun Heo task_set_jobctl_pending(task, JOBCTL_TRAP_STOP | JOBCTL_TRAPPING)) 432910ffdb1SOleg Nesterov signal_wake_up_state(task, __TASK_STOPPED); 433d79fdd6dSTejun Heo 434d79fdd6dSTejun Heo spin_unlock(&task->sighand->siglock); 435d79fdd6dSTejun Heo 436b79b7ba9SOleg Nesterov retval = 0; 4374b105cbbSOleg Nesterov unlock_tasklist: 4384b105cbbSOleg Nesterov write_unlock_irq(&tasklist_lock); 4394b105cbbSOleg Nesterov unlock_creds: 4409b1bf12dSKOSAKI Motohiro mutex_unlock(&task->signal->cred_guard_mutex); 441f5b40e36SLinus Torvalds out: 442f701e5b7SVladimir Zapolskiy if (!retval) { 4437c3b00e0SOleg Nesterov /* 4447c3b00e0SOleg Nesterov * We do not bother to change retval or clear JOBCTL_TRAPPING 4457c3b00e0SOleg Nesterov * if wait_on_bit() was interrupted by SIGKILL. The tracer will 4467c3b00e0SOleg Nesterov * not return to user-mode, it will exit and clear this bit in 4477c3b00e0SOleg Nesterov * __ptrace_unlink() if it wasn't already cleared by the tracee; 4487c3b00e0SOleg Nesterov * and until then nobody can ptrace this task. 4497c3b00e0SOleg Nesterov */ 4507c3b00e0SOleg Nesterov wait_on_bit(&task->jobctl, JOBCTL_TRAPPING_BIT, TASK_KILLABLE); 451f701e5b7SVladimir Zapolskiy proc_ptrace_connector(task, PTRACE_ATTACH); 452f701e5b7SVladimir Zapolskiy } 453f701e5b7SVladimir Zapolskiy 4541da177e4SLinus Torvalds return retval; 4551da177e4SLinus Torvalds } 4561da177e4SLinus Torvalds 457f2f0b00aSOleg Nesterov /** 458f2f0b00aSOleg Nesterov * ptrace_traceme -- helper for PTRACE_TRACEME 459f2f0b00aSOleg Nesterov * 460f2f0b00aSOleg Nesterov * Performs checks and sets PT_PTRACED. 461f2f0b00aSOleg Nesterov * Should be used by all ptrace implementations for PTRACE_TRACEME. 462f2f0b00aSOleg Nesterov */ 463e3e89cc5SLinus Torvalds static int ptrace_traceme(void) 464f2f0b00aSOleg Nesterov { 465f2f0b00aSOleg Nesterov int ret = -EPERM; 466f2f0b00aSOleg Nesterov 4674b105cbbSOleg Nesterov write_lock_irq(&tasklist_lock); 4684b105cbbSOleg Nesterov /* Are we already being traced? */ 469f2f0b00aSOleg Nesterov if (!current->ptrace) { 470f2f0b00aSOleg Nesterov ret = security_ptrace_traceme(current->parent); 471f2f0b00aSOleg Nesterov /* 472f2f0b00aSOleg Nesterov * Check PF_EXITING to ensure ->real_parent has not passed 473f2f0b00aSOleg Nesterov * exit_ptrace(). Otherwise we don't report the error but 474f2f0b00aSOleg Nesterov * pretend ->real_parent untraces us right after return. 475f2f0b00aSOleg Nesterov */ 476f2f0b00aSOleg Nesterov if (!ret && !(current->real_parent->flags & PF_EXITING)) { 477f2f0b00aSOleg Nesterov current->ptrace = PT_PTRACED; 478c70d9d80SEric W. Biederman ptrace_link(current, current->real_parent); 479f2f0b00aSOleg Nesterov } 480f2f0b00aSOleg Nesterov } 4814b105cbbSOleg Nesterov write_unlock_irq(&tasklist_lock); 4824b105cbbSOleg Nesterov 483f2f0b00aSOleg Nesterov return ret; 484f2f0b00aSOleg Nesterov } 485f2f0b00aSOleg Nesterov 48639c626aeSOleg Nesterov /* 48739c626aeSOleg Nesterov * Called with irqs disabled, returns true if childs should reap themselves. 48839c626aeSOleg Nesterov */ 48939c626aeSOleg Nesterov static int ignoring_children(struct sighand_struct *sigh) 49039c626aeSOleg Nesterov { 49139c626aeSOleg Nesterov int ret; 49239c626aeSOleg Nesterov spin_lock(&sigh->siglock); 49339c626aeSOleg Nesterov ret = (sigh->action[SIGCHLD-1].sa.sa_handler == SIG_IGN) || 49439c626aeSOleg Nesterov (sigh->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT); 49539c626aeSOleg Nesterov spin_unlock(&sigh->siglock); 49639c626aeSOleg Nesterov return ret; 49739c626aeSOleg Nesterov } 49839c626aeSOleg Nesterov 49939c626aeSOleg Nesterov /* 50039c626aeSOleg Nesterov * Called with tasklist_lock held for writing. 50139c626aeSOleg Nesterov * Unlink a traced task, and clean it up if it was a traced zombie. 50239c626aeSOleg Nesterov * Return true if it needs to be reaped with release_task(). 50339c626aeSOleg Nesterov * (We can't call release_task() here because we already hold tasklist_lock.) 50439c626aeSOleg Nesterov * 50539c626aeSOleg Nesterov * If it's a zombie, our attachedness prevented normal parent notification 50639c626aeSOleg Nesterov * or self-reaping. Do notification now if it would have happened earlier. 50739c626aeSOleg Nesterov * If it should reap itself, return true. 50839c626aeSOleg Nesterov * 509a7f0765eSOleg Nesterov * If it's our own child, there is no notification to do. But if our normal 510a7f0765eSOleg Nesterov * children self-reap, then this child was prevented by ptrace and we must 511a7f0765eSOleg Nesterov * reap it now, in that case we must also wake up sub-threads sleeping in 512a7f0765eSOleg Nesterov * do_wait(). 51339c626aeSOleg Nesterov */ 51439c626aeSOleg Nesterov static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p) 51539c626aeSOleg Nesterov { 5169843a1e9SOleg Nesterov bool dead; 5179843a1e9SOleg Nesterov 51839c626aeSOleg Nesterov __ptrace_unlink(p); 51939c626aeSOleg Nesterov 5209843a1e9SOleg Nesterov if (p->exit_state != EXIT_ZOMBIE) 5219843a1e9SOleg Nesterov return false; 5229843a1e9SOleg Nesterov 5239843a1e9SOleg Nesterov dead = !thread_group_leader(p); 5249843a1e9SOleg Nesterov 5259843a1e9SOleg Nesterov if (!dead && thread_group_empty(p)) { 52639c626aeSOleg Nesterov if (!same_thread_group(p->real_parent, tracer)) 5279843a1e9SOleg Nesterov dead = do_notify_parent(p, p->exit_signal); 528a7f0765eSOleg Nesterov else if (ignoring_children(tracer->sighand)) { 529a7f0765eSOleg Nesterov __wake_up_parent(p, tracer); 5309843a1e9SOleg Nesterov dead = true; 53139c626aeSOleg Nesterov } 532a7f0765eSOleg Nesterov } 53339c626aeSOleg Nesterov /* Mark it as in the process of being reaped. */ 5349843a1e9SOleg Nesterov if (dead) 53539c626aeSOleg Nesterov p->exit_state = EXIT_DEAD; 5369843a1e9SOleg Nesterov return dead; 53739c626aeSOleg Nesterov } 53839c626aeSOleg Nesterov 539e3e89cc5SLinus Torvalds static int ptrace_detach(struct task_struct *child, unsigned int data) 5401da177e4SLinus Torvalds { 5417ed20e1aSJesper Juhl if (!valid_signal(data)) 5421da177e4SLinus Torvalds return -EIO; 5431da177e4SLinus Torvalds 5441da177e4SLinus Torvalds /* Architecture-specific hardware disable .. */ 5451da177e4SLinus Torvalds ptrace_disable(child); 5461da177e4SLinus Torvalds 54795c3eb76SOleg Nesterov write_lock_irq(&tasklist_lock); 54839c626aeSOleg Nesterov /* 54964a4096cSOleg Nesterov * We rely on ptrace_freeze_traced(). It can't be killed and 55064a4096cSOleg Nesterov * untraced by another thread, it can't be a zombie. 55139c626aeSOleg Nesterov */ 55264a4096cSOleg Nesterov WARN_ON(!child->ptrace || child->exit_state); 55364a4096cSOleg Nesterov /* 55464a4096cSOleg Nesterov * tasklist_lock avoids the race with wait_task_stopped(), see 55564a4096cSOleg Nesterov * the comment in ptrace_resume(). 55664a4096cSOleg Nesterov */ 55795c3eb76SOleg Nesterov child->exit_code = data; 55864a4096cSOleg Nesterov __ptrace_detach(current, child); 5591da177e4SLinus Torvalds write_unlock_irq(&tasklist_lock); 5601da177e4SLinus Torvalds 561f701e5b7SVladimir Zapolskiy proc_ptrace_connector(child, PTRACE_DETACH); 5624576145cSOleg Nesterov 5631da177e4SLinus Torvalds return 0; 5641da177e4SLinus Torvalds } 5651da177e4SLinus Torvalds 56639c626aeSOleg Nesterov /* 567c7e49c14SOleg Nesterov * Detach all tasks we were using ptrace on. Called with tasklist held 5687c8bd232SOleg Nesterov * for writing. 56939c626aeSOleg Nesterov */ 5707c8bd232SOleg Nesterov void exit_ptrace(struct task_struct *tracer, struct list_head *dead) 57139c626aeSOleg Nesterov { 57239c626aeSOleg Nesterov struct task_struct *p, *n; 573c7e49c14SOleg Nesterov 57439c626aeSOleg Nesterov list_for_each_entry_safe(p, n, &tracer->ptraced, ptrace_entry) { 575992fb6e1SOleg Nesterov if (unlikely(p->ptrace & PT_EXITKILL)) 576079b22dcSEric W. Biederman send_sig_info(SIGKILL, SEND_SIG_PRIV, p); 577992fb6e1SOleg Nesterov 57839c626aeSOleg Nesterov if (__ptrace_detach(tracer, p)) 5797c8bd232SOleg Nesterov list_add(&p->ptrace_entry, dead); 58039c626aeSOleg Nesterov } 58139c626aeSOleg Nesterov } 58239c626aeSOleg Nesterov 5831da177e4SLinus Torvalds int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len) 5841da177e4SLinus Torvalds { 5851da177e4SLinus Torvalds int copied = 0; 5861da177e4SLinus Torvalds 5871da177e4SLinus Torvalds while (len > 0) { 5881da177e4SLinus Torvalds char buf[128]; 5891da177e4SLinus Torvalds int this_len, retval; 5901da177e4SLinus Torvalds 5911da177e4SLinus Torvalds this_len = (len > sizeof(buf)) ? sizeof(buf) : len; 59284d77d3fSEric W. Biederman retval = ptrace_access_vm(tsk, src, buf, this_len, FOLL_FORCE); 59384d77d3fSEric W. Biederman 5941da177e4SLinus Torvalds if (!retval) { 5951da177e4SLinus Torvalds if (copied) 5961da177e4SLinus Torvalds break; 5971da177e4SLinus Torvalds return -EIO; 5981da177e4SLinus Torvalds } 5991da177e4SLinus Torvalds if (copy_to_user(dst, buf, retval)) 6001da177e4SLinus Torvalds return -EFAULT; 6011da177e4SLinus Torvalds copied += retval; 6021da177e4SLinus Torvalds src += retval; 6031da177e4SLinus Torvalds dst += retval; 6041da177e4SLinus Torvalds len -= retval; 6051da177e4SLinus Torvalds } 6061da177e4SLinus Torvalds return copied; 6071da177e4SLinus Torvalds } 6081da177e4SLinus Torvalds 6091da177e4SLinus Torvalds int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len) 6101da177e4SLinus Torvalds { 6111da177e4SLinus Torvalds int copied = 0; 6121da177e4SLinus Torvalds 6131da177e4SLinus Torvalds while (len > 0) { 6141da177e4SLinus Torvalds char buf[128]; 6151da177e4SLinus Torvalds int this_len, retval; 6161da177e4SLinus Torvalds 6171da177e4SLinus Torvalds this_len = (len > sizeof(buf)) ? sizeof(buf) : len; 6181da177e4SLinus Torvalds if (copy_from_user(buf, src, this_len)) 6191da177e4SLinus Torvalds return -EFAULT; 62084d77d3fSEric W. Biederman retval = ptrace_access_vm(tsk, dst, buf, this_len, 621f307ab6dSLorenzo Stoakes FOLL_FORCE | FOLL_WRITE); 6221da177e4SLinus Torvalds if (!retval) { 6231da177e4SLinus Torvalds if (copied) 6241da177e4SLinus Torvalds break; 6251da177e4SLinus Torvalds return -EIO; 6261da177e4SLinus Torvalds } 6271da177e4SLinus Torvalds copied += retval; 6281da177e4SLinus Torvalds src += retval; 6291da177e4SLinus Torvalds dst += retval; 6301da177e4SLinus Torvalds len -= retval; 6311da177e4SLinus Torvalds } 6321da177e4SLinus Torvalds return copied; 6331da177e4SLinus Torvalds } 6341da177e4SLinus Torvalds 6354abf9869SNamhyung Kim static int ptrace_setoptions(struct task_struct *child, unsigned long data) 6361da177e4SLinus Torvalds { 63786b6c1f3SDenys Vlasenko unsigned flags; 63886b6c1f3SDenys Vlasenko 6398c5cf9e5SDenys Vlasenko if (data & ~(unsigned long)PTRACE_O_MASK) 6408c5cf9e5SDenys Vlasenko return -EINVAL; 6418c5cf9e5SDenys Vlasenko 64213c4a901STycho Andersen if (unlikely(data & PTRACE_O_SUSPEND_SECCOMP)) { 64397f2645fSMasahiro Yamada if (!IS_ENABLED(CONFIG_CHECKPOINT_RESTORE) || 64497f2645fSMasahiro Yamada !IS_ENABLED(CONFIG_SECCOMP)) 64513c4a901STycho Andersen return -EINVAL; 64613c4a901STycho Andersen 64713c4a901STycho Andersen if (!capable(CAP_SYS_ADMIN)) 64813c4a901STycho Andersen return -EPERM; 64913c4a901STycho Andersen 65013c4a901STycho Andersen if (seccomp_mode(¤t->seccomp) != SECCOMP_MODE_DISABLED || 65113c4a901STycho Andersen current->ptrace & PT_SUSPEND_SECCOMP) 65213c4a901STycho Andersen return -EPERM; 65313c4a901STycho Andersen } 65413c4a901STycho Andersen 65586b6c1f3SDenys Vlasenko /* Avoid intermediate state when all opts are cleared */ 65686b6c1f3SDenys Vlasenko flags = child->ptrace; 65786b6c1f3SDenys Vlasenko flags &= ~(PTRACE_O_MASK << PT_OPT_FLAG_SHIFT); 65886b6c1f3SDenys Vlasenko flags |= (data << PT_OPT_FLAG_SHIFT); 65986b6c1f3SDenys Vlasenko child->ptrace = flags; 6601da177e4SLinus Torvalds 6618c5cf9e5SDenys Vlasenko return 0; 6621da177e4SLinus Torvalds } 6631da177e4SLinus Torvalds 664ae7795bcSEric W. Biederman static int ptrace_getsiginfo(struct task_struct *child, kernel_siginfo_t *info) 6651da177e4SLinus Torvalds { 666e4961254SOleg Nesterov unsigned long flags; 6671da177e4SLinus Torvalds int error = -ESRCH; 6681da177e4SLinus Torvalds 669e4961254SOleg Nesterov if (lock_task_sighand(child, &flags)) { 6701da177e4SLinus Torvalds error = -EINVAL; 6711da177e4SLinus Torvalds if (likely(child->last_siginfo != NULL)) { 6720752d7bfSEric W. Biederman copy_siginfo(info, child->last_siginfo); 6731da177e4SLinus Torvalds error = 0; 6741da177e4SLinus Torvalds } 675e4961254SOleg Nesterov unlock_task_sighand(child, &flags); 6761da177e4SLinus Torvalds } 6771da177e4SLinus Torvalds return error; 6781da177e4SLinus Torvalds } 6791da177e4SLinus Torvalds 680ae7795bcSEric W. Biederman static int ptrace_setsiginfo(struct task_struct *child, const kernel_siginfo_t *info) 6811da177e4SLinus Torvalds { 682e4961254SOleg Nesterov unsigned long flags; 6831da177e4SLinus Torvalds int error = -ESRCH; 6841da177e4SLinus Torvalds 685e4961254SOleg Nesterov if (lock_task_sighand(child, &flags)) { 6861da177e4SLinus Torvalds error = -EINVAL; 6871da177e4SLinus Torvalds if (likely(child->last_siginfo != NULL)) { 6880752d7bfSEric W. Biederman copy_siginfo(child->last_siginfo, info); 6891da177e4SLinus Torvalds error = 0; 6901da177e4SLinus Torvalds } 691e4961254SOleg Nesterov unlock_task_sighand(child, &flags); 6921da177e4SLinus Torvalds } 6931da177e4SLinus Torvalds return error; 6941da177e4SLinus Torvalds } 6951da177e4SLinus Torvalds 69684c751bdSAndrey Vagin static int ptrace_peek_siginfo(struct task_struct *child, 69784c751bdSAndrey Vagin unsigned long addr, 69884c751bdSAndrey Vagin unsigned long data) 69984c751bdSAndrey Vagin { 70084c751bdSAndrey Vagin struct ptrace_peeksiginfo_args arg; 70184c751bdSAndrey Vagin struct sigpending *pending; 70284c751bdSAndrey Vagin struct sigqueue *q; 70384c751bdSAndrey Vagin int ret, i; 70484c751bdSAndrey Vagin 70584c751bdSAndrey Vagin ret = copy_from_user(&arg, (void __user *) addr, 70684c751bdSAndrey Vagin sizeof(struct ptrace_peeksiginfo_args)); 70784c751bdSAndrey Vagin if (ret) 70884c751bdSAndrey Vagin return -EFAULT; 70984c751bdSAndrey Vagin 71084c751bdSAndrey Vagin if (arg.flags & ~PTRACE_PEEKSIGINFO_SHARED) 71184c751bdSAndrey Vagin return -EINVAL; /* unknown flags */ 71284c751bdSAndrey Vagin 71384c751bdSAndrey Vagin if (arg.nr < 0) 71484c751bdSAndrey Vagin return -EINVAL; 71584c751bdSAndrey Vagin 716f6e2aa91SEric W. Biederman /* Ensure arg.off fits in an unsigned long */ 717f6e2aa91SEric W. Biederman if (arg.off > ULONG_MAX) 718f6e2aa91SEric W. Biederman return 0; 719f6e2aa91SEric W. Biederman 72084c751bdSAndrey Vagin if (arg.flags & PTRACE_PEEKSIGINFO_SHARED) 72184c751bdSAndrey Vagin pending = &child->signal->shared_pending; 72284c751bdSAndrey Vagin else 72384c751bdSAndrey Vagin pending = &child->pending; 72484c751bdSAndrey Vagin 72584c751bdSAndrey Vagin for (i = 0; i < arg.nr; ) { 726ae7795bcSEric W. Biederman kernel_siginfo_t info; 727f6e2aa91SEric W. Biederman unsigned long off = arg.off + i; 728f6e2aa91SEric W. Biederman bool found = false; 72984c751bdSAndrey Vagin 73084c751bdSAndrey Vagin spin_lock_irq(&child->sighand->siglock); 73184c751bdSAndrey Vagin list_for_each_entry(q, &pending->list, list) { 73284c751bdSAndrey Vagin if (!off--) { 733f6e2aa91SEric W. Biederman found = true; 73484c751bdSAndrey Vagin copy_siginfo(&info, &q->info); 73584c751bdSAndrey Vagin break; 73684c751bdSAndrey Vagin } 73784c751bdSAndrey Vagin } 73884c751bdSAndrey Vagin spin_unlock_irq(&child->sighand->siglock); 73984c751bdSAndrey Vagin 740f6e2aa91SEric W. Biederman if (!found) /* beyond the end of the list */ 74184c751bdSAndrey Vagin break; 74284c751bdSAndrey Vagin 74384c751bdSAndrey Vagin #ifdef CONFIG_COMPAT 7445c465217SAndy Lutomirski if (unlikely(in_compat_syscall())) { 74584c751bdSAndrey Vagin compat_siginfo_t __user *uinfo = compat_ptr(data); 74684c751bdSAndrey Vagin 747cc731525SEric W. Biederman if (copy_siginfo_to_user32(uinfo, &info)) { 748706b23bdSMathieu Desnoyers ret = -EFAULT; 749706b23bdSMathieu Desnoyers break; 750706b23bdSMathieu Desnoyers } 751706b23bdSMathieu Desnoyers 75284c751bdSAndrey Vagin } else 75384c751bdSAndrey Vagin #endif 75484c751bdSAndrey Vagin { 75584c751bdSAndrey Vagin siginfo_t __user *uinfo = (siginfo_t __user *) data; 75684c751bdSAndrey Vagin 757cc731525SEric W. Biederman if (copy_siginfo_to_user(uinfo, &info)) { 75884c751bdSAndrey Vagin ret = -EFAULT; 75984c751bdSAndrey Vagin break; 76084c751bdSAndrey Vagin } 761706b23bdSMathieu Desnoyers } 76284c751bdSAndrey Vagin 76384c751bdSAndrey Vagin data += sizeof(siginfo_t); 76484c751bdSAndrey Vagin i++; 76584c751bdSAndrey Vagin 76684c751bdSAndrey Vagin if (signal_pending(current)) 76784c751bdSAndrey Vagin break; 76884c751bdSAndrey Vagin 76984c751bdSAndrey Vagin cond_resched(); 77084c751bdSAndrey Vagin } 77184c751bdSAndrey Vagin 77284c751bdSAndrey Vagin if (i > 0) 77384c751bdSAndrey Vagin return i; 77484c751bdSAndrey Vagin 77584c751bdSAndrey Vagin return ret; 77684c751bdSAndrey Vagin } 77736df29d7SRoland McGrath 77836df29d7SRoland McGrath #ifdef PTRACE_SINGLESTEP 77936df29d7SRoland McGrath #define is_singlestep(request) ((request) == PTRACE_SINGLESTEP) 78036df29d7SRoland McGrath #else 78136df29d7SRoland McGrath #define is_singlestep(request) 0 78236df29d7SRoland McGrath #endif 78336df29d7SRoland McGrath 7845b88abbfSRoland McGrath #ifdef PTRACE_SINGLEBLOCK 7855b88abbfSRoland McGrath #define is_singleblock(request) ((request) == PTRACE_SINGLEBLOCK) 7865b88abbfSRoland McGrath #else 7875b88abbfSRoland McGrath #define is_singleblock(request) 0 7885b88abbfSRoland McGrath #endif 7895b88abbfSRoland McGrath 79036df29d7SRoland McGrath #ifdef PTRACE_SYSEMU 79136df29d7SRoland McGrath #define is_sysemu_singlestep(request) ((request) == PTRACE_SYSEMU_SINGLESTEP) 79236df29d7SRoland McGrath #else 79336df29d7SRoland McGrath #define is_sysemu_singlestep(request) 0 79436df29d7SRoland McGrath #endif 79536df29d7SRoland McGrath 7964abf9869SNamhyung Kim static int ptrace_resume(struct task_struct *child, long request, 7974abf9869SNamhyung Kim unsigned long data) 79836df29d7SRoland McGrath { 799b72c1869SOleg Nesterov bool need_siglock; 800b72c1869SOleg Nesterov 80136df29d7SRoland McGrath if (!valid_signal(data)) 80236df29d7SRoland McGrath return -EIO; 80336df29d7SRoland McGrath 80436df29d7SRoland McGrath if (request == PTRACE_SYSCALL) 80536df29d7SRoland McGrath set_tsk_thread_flag(child, TIF_SYSCALL_TRACE); 80636df29d7SRoland McGrath else 80736df29d7SRoland McGrath clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); 80836df29d7SRoland McGrath 80936df29d7SRoland McGrath #ifdef TIF_SYSCALL_EMU 81036df29d7SRoland McGrath if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP) 81136df29d7SRoland McGrath set_tsk_thread_flag(child, TIF_SYSCALL_EMU); 81236df29d7SRoland McGrath else 81336df29d7SRoland McGrath clear_tsk_thread_flag(child, TIF_SYSCALL_EMU); 81436df29d7SRoland McGrath #endif 81536df29d7SRoland McGrath 8165b88abbfSRoland McGrath if (is_singleblock(request)) { 8175b88abbfSRoland McGrath if (unlikely(!arch_has_block_step())) 8185b88abbfSRoland McGrath return -EIO; 8195b88abbfSRoland McGrath user_enable_block_step(child); 8205b88abbfSRoland McGrath } else if (is_singlestep(request) || is_sysemu_singlestep(request)) { 82136df29d7SRoland McGrath if (unlikely(!arch_has_single_step())) 82236df29d7SRoland McGrath return -EIO; 82336df29d7SRoland McGrath user_enable_single_step(child); 8243a709703SRoland McGrath } else { 82536df29d7SRoland McGrath user_disable_single_step(child); 8263a709703SRoland McGrath } 82736df29d7SRoland McGrath 828b72c1869SOleg Nesterov /* 829b72c1869SOleg Nesterov * Change ->exit_code and ->state under siglock to avoid the race 830b72c1869SOleg Nesterov * with wait_task_stopped() in between; a non-zero ->exit_code will 831b72c1869SOleg Nesterov * wrongly look like another report from tracee. 832b72c1869SOleg Nesterov * 833b72c1869SOleg Nesterov * Note that we need siglock even if ->exit_code == data and/or this 834b72c1869SOleg Nesterov * status was not reported yet, the new status must not be cleared by 835b72c1869SOleg Nesterov * wait_task_stopped() after resume. 836b72c1869SOleg Nesterov * 837b72c1869SOleg Nesterov * If data == 0 we do not care if wait_task_stopped() reports the old 838b72c1869SOleg Nesterov * status and clears the code too; this can't race with the tracee, it 839b72c1869SOleg Nesterov * takes siglock after resume. 840b72c1869SOleg Nesterov */ 841b72c1869SOleg Nesterov need_siglock = data && !thread_group_empty(current); 842b72c1869SOleg Nesterov if (need_siglock) 843b72c1869SOleg Nesterov spin_lock_irq(&child->sighand->siglock); 84436df29d7SRoland McGrath child->exit_code = data; 8450666fb51SOleg Nesterov wake_up_state(child, __TASK_TRACED); 846b72c1869SOleg Nesterov if (need_siglock) 847b72c1869SOleg Nesterov spin_unlock_irq(&child->sighand->siglock); 84836df29d7SRoland McGrath 84936df29d7SRoland McGrath return 0; 85036df29d7SRoland McGrath } 85136df29d7SRoland McGrath 8522225a122SSuresh Siddha #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 8532225a122SSuresh Siddha 8542225a122SSuresh Siddha static const struct user_regset * 8552225a122SSuresh Siddha find_regset(const struct user_regset_view *view, unsigned int type) 8562225a122SSuresh Siddha { 8572225a122SSuresh Siddha const struct user_regset *regset; 8582225a122SSuresh Siddha int n; 8592225a122SSuresh Siddha 8602225a122SSuresh Siddha for (n = 0; n < view->n; ++n) { 8612225a122SSuresh Siddha regset = view->regsets + n; 8622225a122SSuresh Siddha if (regset->core_note_type == type) 8632225a122SSuresh Siddha return regset; 8642225a122SSuresh Siddha } 8652225a122SSuresh Siddha 8662225a122SSuresh Siddha return NULL; 8672225a122SSuresh Siddha } 8682225a122SSuresh Siddha 8692225a122SSuresh Siddha static int ptrace_regset(struct task_struct *task, int req, unsigned int type, 8702225a122SSuresh Siddha struct iovec *kiov) 8712225a122SSuresh Siddha { 8722225a122SSuresh Siddha const struct user_regset_view *view = task_user_regset_view(task); 8732225a122SSuresh Siddha const struct user_regset *regset = find_regset(view, type); 8742225a122SSuresh Siddha int regset_no; 8752225a122SSuresh Siddha 8762225a122SSuresh Siddha if (!regset || (kiov->iov_len % regset->size) != 0) 877c6a0dd7eSSuresh Siddha return -EINVAL; 8782225a122SSuresh Siddha 8792225a122SSuresh Siddha regset_no = regset - view->regsets; 8802225a122SSuresh Siddha kiov->iov_len = min(kiov->iov_len, 8812225a122SSuresh Siddha (__kernel_size_t) (regset->n * regset->size)); 8822225a122SSuresh Siddha 8832225a122SSuresh Siddha if (req == PTRACE_GETREGSET) 8842225a122SSuresh Siddha return copy_regset_to_user(task, view, regset_no, 0, 8852225a122SSuresh Siddha kiov->iov_len, kiov->iov_base); 8862225a122SSuresh Siddha else 8872225a122SSuresh Siddha return copy_regset_from_user(task, view, regset_no, 0, 8882225a122SSuresh Siddha kiov->iov_len, kiov->iov_base); 8892225a122SSuresh Siddha } 8902225a122SSuresh Siddha 891e8440c14SJosh Stone /* 892e8440c14SJosh Stone * This is declared in linux/regset.h and defined in machine-dependent 893e8440c14SJosh Stone * code. We put the export here, near the primary machine-neutral use, 894e8440c14SJosh Stone * to ensure no machine forgets it. 895e8440c14SJosh Stone */ 896e8440c14SJosh Stone EXPORT_SYMBOL_GPL(task_user_regset_view); 8972225a122SSuresh Siddha #endif 8982225a122SSuresh Siddha 8991da177e4SLinus Torvalds int ptrace_request(struct task_struct *child, long request, 9004abf9869SNamhyung Kim unsigned long addr, unsigned long data) 9011da177e4SLinus Torvalds { 902fca26f26STejun Heo bool seized = child->ptrace & PT_SEIZED; 9031da177e4SLinus Torvalds int ret = -EIO; 904ae7795bcSEric W. Biederman kernel_siginfo_t siginfo, *si; 9059fed81dcSNamhyung Kim void __user *datavp = (void __user *) data; 9069fed81dcSNamhyung Kim unsigned long __user *datalp = datavp; 907fca26f26STejun Heo unsigned long flags; 9081da177e4SLinus Torvalds 9091da177e4SLinus Torvalds switch (request) { 91016c3e389SRoland McGrath case PTRACE_PEEKTEXT: 91116c3e389SRoland McGrath case PTRACE_PEEKDATA: 91216c3e389SRoland McGrath return generic_ptrace_peekdata(child, addr, data); 91316c3e389SRoland McGrath case PTRACE_POKETEXT: 91416c3e389SRoland McGrath case PTRACE_POKEDATA: 91516c3e389SRoland McGrath return generic_ptrace_pokedata(child, addr, data); 91616c3e389SRoland McGrath 9171da177e4SLinus Torvalds #ifdef PTRACE_OLDSETOPTIONS 9181da177e4SLinus Torvalds case PTRACE_OLDSETOPTIONS: 9191da177e4SLinus Torvalds #endif 9201da177e4SLinus Torvalds case PTRACE_SETOPTIONS: 9211da177e4SLinus Torvalds ret = ptrace_setoptions(child, data); 9221da177e4SLinus Torvalds break; 9231da177e4SLinus Torvalds case PTRACE_GETEVENTMSG: 9249fed81dcSNamhyung Kim ret = put_user(child->ptrace_message, datalp); 9251da177e4SLinus Torvalds break; 926e16b2781SRoland McGrath 92784c751bdSAndrey Vagin case PTRACE_PEEKSIGINFO: 92884c751bdSAndrey Vagin ret = ptrace_peek_siginfo(child, addr, data); 92984c751bdSAndrey Vagin break; 93084c751bdSAndrey Vagin 9311da177e4SLinus Torvalds case PTRACE_GETSIGINFO: 932e16b2781SRoland McGrath ret = ptrace_getsiginfo(child, &siginfo); 933e16b2781SRoland McGrath if (!ret) 9349fed81dcSNamhyung Kim ret = copy_siginfo_to_user(datavp, &siginfo); 9351da177e4SLinus Torvalds break; 936e16b2781SRoland McGrath 9371da177e4SLinus Torvalds case PTRACE_SETSIGINFO: 9384cd2e0e7SEric W. Biederman ret = copy_siginfo_from_user(&siginfo, datavp); 9394cd2e0e7SEric W. Biederman if (!ret) 940e16b2781SRoland McGrath ret = ptrace_setsiginfo(child, &siginfo); 9411da177e4SLinus Torvalds break; 942e16b2781SRoland McGrath 943fcfc2aa0SAndrei Vagin case PTRACE_GETSIGMASK: { 944fcfc2aa0SAndrei Vagin sigset_t *mask; 945fcfc2aa0SAndrei Vagin 94629000caeSAndrey Vagin if (addr != sizeof(sigset_t)) { 94729000caeSAndrey Vagin ret = -EINVAL; 94829000caeSAndrey Vagin break; 94929000caeSAndrey Vagin } 95029000caeSAndrey Vagin 951fcfc2aa0SAndrei Vagin if (test_tsk_restore_sigmask(child)) 952fcfc2aa0SAndrei Vagin mask = &child->saved_sigmask; 953fcfc2aa0SAndrei Vagin else 954fcfc2aa0SAndrei Vagin mask = &child->blocked; 955fcfc2aa0SAndrei Vagin 956fcfc2aa0SAndrei Vagin if (copy_to_user(datavp, mask, sizeof(sigset_t))) 95729000caeSAndrey Vagin ret = -EFAULT; 95829000caeSAndrey Vagin else 95929000caeSAndrey Vagin ret = 0; 96029000caeSAndrey Vagin 96129000caeSAndrey Vagin break; 962fcfc2aa0SAndrei Vagin } 96329000caeSAndrey Vagin 96429000caeSAndrey Vagin case PTRACE_SETSIGMASK: { 96529000caeSAndrey Vagin sigset_t new_set; 96629000caeSAndrey Vagin 96729000caeSAndrey Vagin if (addr != sizeof(sigset_t)) { 96829000caeSAndrey Vagin ret = -EINVAL; 96929000caeSAndrey Vagin break; 97029000caeSAndrey Vagin } 97129000caeSAndrey Vagin 97229000caeSAndrey Vagin if (copy_from_user(&new_set, datavp, sizeof(sigset_t))) { 97329000caeSAndrey Vagin ret = -EFAULT; 97429000caeSAndrey Vagin break; 97529000caeSAndrey Vagin } 97629000caeSAndrey Vagin 97729000caeSAndrey Vagin sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP)); 97829000caeSAndrey Vagin 97929000caeSAndrey Vagin /* 98029000caeSAndrey Vagin * Every thread does recalc_sigpending() after resume, so 98129000caeSAndrey Vagin * retarget_shared_pending() and recalc_sigpending() are not 98229000caeSAndrey Vagin * called here. 98329000caeSAndrey Vagin */ 98429000caeSAndrey Vagin spin_lock_irq(&child->sighand->siglock); 98529000caeSAndrey Vagin child->blocked = new_set; 98629000caeSAndrey Vagin spin_unlock_irq(&child->sighand->siglock); 98729000caeSAndrey Vagin 988fcfc2aa0SAndrei Vagin clear_tsk_restore_sigmask(child); 989fcfc2aa0SAndrei Vagin 99029000caeSAndrey Vagin ret = 0; 99129000caeSAndrey Vagin break; 99229000caeSAndrey Vagin } 99329000caeSAndrey Vagin 994fca26f26STejun Heo case PTRACE_INTERRUPT: 995fca26f26STejun Heo /* 996fca26f26STejun Heo * Stop tracee without any side-effect on signal or job 997fca26f26STejun Heo * control. At least one trap is guaranteed to happen 998fca26f26STejun Heo * after this request. If @child is already trapped, the 999fca26f26STejun Heo * current trap is not disturbed and another trap will 1000fca26f26STejun Heo * happen after the current trap is ended with PTRACE_CONT. 1001fca26f26STejun Heo * 1002fca26f26STejun Heo * The actual trap might not be PTRACE_EVENT_STOP trap but 1003fca26f26STejun Heo * the pending condition is cleared regardless. 1004fca26f26STejun Heo */ 1005fca26f26STejun Heo if (unlikely(!seized || !lock_task_sighand(child, &flags))) 1006fca26f26STejun Heo break; 1007fca26f26STejun Heo 1008544b2c91STejun Heo /* 1009544b2c91STejun Heo * INTERRUPT doesn't disturb existing trap sans one 1010544b2c91STejun Heo * exception. If ptracer issued LISTEN for the current 1011544b2c91STejun Heo * STOP, this INTERRUPT should clear LISTEN and re-trap 1012544b2c91STejun Heo * tracee into STOP. 1013544b2c91STejun Heo */ 1014fca26f26STejun Heo if (likely(task_set_jobctl_pending(child, JOBCTL_TRAP_STOP))) 1015910ffdb1SOleg Nesterov ptrace_signal_wake_up(child, child->jobctl & JOBCTL_LISTENING); 1016544b2c91STejun Heo 1017544b2c91STejun Heo unlock_task_sighand(child, &flags); 1018544b2c91STejun Heo ret = 0; 1019544b2c91STejun Heo break; 1020544b2c91STejun Heo 1021544b2c91STejun Heo case PTRACE_LISTEN: 1022544b2c91STejun Heo /* 1023544b2c91STejun Heo * Listen for events. Tracee must be in STOP. It's not 1024544b2c91STejun Heo * resumed per-se but is not considered to be in TRACED by 1025544b2c91STejun Heo * wait(2) or ptrace(2). If an async event (e.g. group 1026544b2c91STejun Heo * stop state change) happens, tracee will enter STOP trap 1027544b2c91STejun Heo * again. Alternatively, ptracer can issue INTERRUPT to 1028544b2c91STejun Heo * finish listening and re-trap tracee into STOP. 1029544b2c91STejun Heo */ 1030544b2c91STejun Heo if (unlikely(!seized || !lock_task_sighand(child, &flags))) 1031544b2c91STejun Heo break; 1032544b2c91STejun Heo 1033544b2c91STejun Heo si = child->last_siginfo; 1034f9d81f61SOleg Nesterov if (likely(si && (si->si_code >> 8) == PTRACE_EVENT_STOP)) { 1035544b2c91STejun Heo child->jobctl |= JOBCTL_LISTENING; 1036544b2c91STejun Heo /* 1037f9d81f61SOleg Nesterov * If NOTIFY is set, it means event happened between 1038f9d81f61SOleg Nesterov * start of this trap and now. Trigger re-trap. 1039544b2c91STejun Heo */ 1040544b2c91STejun Heo if (child->jobctl & JOBCTL_TRAP_NOTIFY) 1041910ffdb1SOleg Nesterov ptrace_signal_wake_up(child, true); 1042fca26f26STejun Heo ret = 0; 1043f9d81f61SOleg Nesterov } 1044f9d81f61SOleg Nesterov unlock_task_sighand(child, &flags); 1045fca26f26STejun Heo break; 1046fca26f26STejun Heo 10471bcf5482SAlexey Dobriyan case PTRACE_DETACH: /* detach a process that was attached. */ 10481bcf5482SAlexey Dobriyan ret = ptrace_detach(child, data); 10491bcf5482SAlexey Dobriyan break; 105036df29d7SRoland McGrath 10519c1a1259SMike Frysinger #ifdef CONFIG_BINFMT_ELF_FDPIC 10529c1a1259SMike Frysinger case PTRACE_GETFDPIC: { 1053e0129ef9SOleg Nesterov struct mm_struct *mm = get_task_mm(child); 10549c1a1259SMike Frysinger unsigned long tmp = 0; 10559c1a1259SMike Frysinger 1056e0129ef9SOleg Nesterov ret = -ESRCH; 1057e0129ef9SOleg Nesterov if (!mm) 1058e0129ef9SOleg Nesterov break; 1059e0129ef9SOleg Nesterov 10609c1a1259SMike Frysinger switch (addr) { 10619c1a1259SMike Frysinger case PTRACE_GETFDPIC_EXEC: 1062e0129ef9SOleg Nesterov tmp = mm->context.exec_fdpic_loadmap; 10639c1a1259SMike Frysinger break; 10649c1a1259SMike Frysinger case PTRACE_GETFDPIC_INTERP: 1065e0129ef9SOleg Nesterov tmp = mm->context.interp_fdpic_loadmap; 10669c1a1259SMike Frysinger break; 10679c1a1259SMike Frysinger default: 10689c1a1259SMike Frysinger break; 10699c1a1259SMike Frysinger } 1070e0129ef9SOleg Nesterov mmput(mm); 10719c1a1259SMike Frysinger 10729fed81dcSNamhyung Kim ret = put_user(tmp, datalp); 10739c1a1259SMike Frysinger break; 10749c1a1259SMike Frysinger } 10759c1a1259SMike Frysinger #endif 10769c1a1259SMike Frysinger 107736df29d7SRoland McGrath #ifdef PTRACE_SINGLESTEP 107836df29d7SRoland McGrath case PTRACE_SINGLESTEP: 107936df29d7SRoland McGrath #endif 10805b88abbfSRoland McGrath #ifdef PTRACE_SINGLEBLOCK 10815b88abbfSRoland McGrath case PTRACE_SINGLEBLOCK: 10825b88abbfSRoland McGrath #endif 108336df29d7SRoland McGrath #ifdef PTRACE_SYSEMU 108436df29d7SRoland McGrath case PTRACE_SYSEMU: 108536df29d7SRoland McGrath case PTRACE_SYSEMU_SINGLESTEP: 108636df29d7SRoland McGrath #endif 108736df29d7SRoland McGrath case PTRACE_SYSCALL: 108836df29d7SRoland McGrath case PTRACE_CONT: 108936df29d7SRoland McGrath return ptrace_resume(child, request, data); 109036df29d7SRoland McGrath 109136df29d7SRoland McGrath case PTRACE_KILL: 109236df29d7SRoland McGrath if (child->exit_state) /* already dead */ 109336df29d7SRoland McGrath return 0; 109436df29d7SRoland McGrath return ptrace_resume(child, request, SIGKILL); 109536df29d7SRoland McGrath 10962225a122SSuresh Siddha #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 10972225a122SSuresh Siddha case PTRACE_GETREGSET: 109829000caeSAndrey Vagin case PTRACE_SETREGSET: { 10992225a122SSuresh Siddha struct iovec kiov; 11009fed81dcSNamhyung Kim struct iovec __user *uiov = datavp; 11012225a122SSuresh Siddha 110296d4f267SLinus Torvalds if (!access_ok(uiov, sizeof(*uiov))) 11032225a122SSuresh Siddha return -EFAULT; 11042225a122SSuresh Siddha 11052225a122SSuresh Siddha if (__get_user(kiov.iov_base, &uiov->iov_base) || 11062225a122SSuresh Siddha __get_user(kiov.iov_len, &uiov->iov_len)) 11072225a122SSuresh Siddha return -EFAULT; 11082225a122SSuresh Siddha 11092225a122SSuresh Siddha ret = ptrace_regset(child, request, addr, &kiov); 11102225a122SSuresh Siddha if (!ret) 11112225a122SSuresh Siddha ret = __put_user(kiov.iov_len, &uiov->iov_len); 11122225a122SSuresh Siddha break; 11132225a122SSuresh Siddha } 11142225a122SSuresh Siddha #endif 1115f8e529edSTycho Andersen 1116f8e529edSTycho Andersen case PTRACE_SECCOMP_GET_FILTER: 1117f8e529edSTycho Andersen ret = seccomp_get_filter(child, addr, datavp); 1118f8e529edSTycho Andersen break; 1119f8e529edSTycho Andersen 112026500475STycho Andersen case PTRACE_SECCOMP_GET_METADATA: 112126500475STycho Andersen ret = seccomp_get_metadata(child, addr, datavp); 112226500475STycho Andersen break; 112326500475STycho Andersen 11241da177e4SLinus Torvalds default: 11251da177e4SLinus Torvalds break; 11261da177e4SLinus Torvalds } 11271da177e4SLinus Torvalds 11281da177e4SLinus Torvalds return ret; 11291da177e4SLinus Torvalds } 1130481bed45SChristoph Hellwig 11310ac15559SChristoph Hellwig #ifndef arch_ptrace_attach 11320ac15559SChristoph Hellwig #define arch_ptrace_attach(child) do { } while (0) 11330ac15559SChristoph Hellwig #endif 11340ac15559SChristoph Hellwig 11354abf9869SNamhyung Kim SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr, 11364abf9869SNamhyung Kim unsigned long, data) 1137481bed45SChristoph Hellwig { 1138481bed45SChristoph Hellwig struct task_struct *child; 1139481bed45SChristoph Hellwig long ret; 1140481bed45SChristoph Hellwig 11416b9c7ed8SChristoph Hellwig if (request == PTRACE_TRACEME) { 11426b9c7ed8SChristoph Hellwig ret = ptrace_traceme(); 11436ea6dd93SHaavard Skinnemoen if (!ret) 11446ea6dd93SHaavard Skinnemoen arch_ptrace_attach(current); 1145481bed45SChristoph Hellwig goto out; 11466b9c7ed8SChristoph Hellwig } 11476b9c7ed8SChristoph Hellwig 11482ee08260SMike Rapoport child = find_get_task_by_vpid(pid); 11492ee08260SMike Rapoport if (!child) { 11502ee08260SMike Rapoport ret = -ESRCH; 11516b9c7ed8SChristoph Hellwig goto out; 11526b9c7ed8SChristoph Hellwig } 1153481bed45SChristoph Hellwig 11543544d72aSTejun Heo if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) { 1155aa9147c9SDenys Vlasenko ret = ptrace_attach(child, request, addr, data); 11560ac15559SChristoph Hellwig /* 11570ac15559SChristoph Hellwig * Some architectures need to do book-keeping after 11580ac15559SChristoph Hellwig * a ptrace attach. 11590ac15559SChristoph Hellwig */ 11600ac15559SChristoph Hellwig if (!ret) 11610ac15559SChristoph Hellwig arch_ptrace_attach(child); 1162005f18dfSChristoph Hellwig goto out_put_task_struct; 1163481bed45SChristoph Hellwig } 1164481bed45SChristoph Hellwig 1165fca26f26STejun Heo ret = ptrace_check_attach(child, request == PTRACE_KILL || 1166fca26f26STejun Heo request == PTRACE_INTERRUPT); 1167481bed45SChristoph Hellwig if (ret < 0) 1168481bed45SChristoph Hellwig goto out_put_task_struct; 1169481bed45SChristoph Hellwig 1170481bed45SChristoph Hellwig ret = arch_ptrace(child, request, addr, data); 11719899d11fSOleg Nesterov if (ret || request != PTRACE_DETACH) 11729899d11fSOleg Nesterov ptrace_unfreeze_traced(child); 1173481bed45SChristoph Hellwig 1174481bed45SChristoph Hellwig out_put_task_struct: 1175481bed45SChristoph Hellwig put_task_struct(child); 1176481bed45SChristoph Hellwig out: 1177481bed45SChristoph Hellwig return ret; 1178481bed45SChristoph Hellwig } 117976647323SAlexey Dobriyan 11804abf9869SNamhyung Kim int generic_ptrace_peekdata(struct task_struct *tsk, unsigned long addr, 11814abf9869SNamhyung Kim unsigned long data) 118276647323SAlexey Dobriyan { 118376647323SAlexey Dobriyan unsigned long tmp; 118476647323SAlexey Dobriyan int copied; 118576647323SAlexey Dobriyan 118684d77d3fSEric W. Biederman copied = ptrace_access_vm(tsk, addr, &tmp, sizeof(tmp), FOLL_FORCE); 118776647323SAlexey Dobriyan if (copied != sizeof(tmp)) 118876647323SAlexey Dobriyan return -EIO; 118976647323SAlexey Dobriyan return put_user(tmp, (unsigned long __user *)data); 119076647323SAlexey Dobriyan } 1191f284ce72SAlexey Dobriyan 11924abf9869SNamhyung Kim int generic_ptrace_pokedata(struct task_struct *tsk, unsigned long addr, 11934abf9869SNamhyung Kim unsigned long data) 1194f284ce72SAlexey Dobriyan { 1195f284ce72SAlexey Dobriyan int copied; 1196f284ce72SAlexey Dobriyan 119784d77d3fSEric W. Biederman copied = ptrace_access_vm(tsk, addr, &data, sizeof(data), 1198f307ab6dSLorenzo Stoakes FOLL_FORCE | FOLL_WRITE); 1199f284ce72SAlexey Dobriyan return (copied == sizeof(data)) ? 0 : -EIO; 1200f284ce72SAlexey Dobriyan } 1201032d82d9SRoland McGrath 120296b8936aSChristoph Hellwig #if defined CONFIG_COMPAT 1203032d82d9SRoland McGrath 1204032d82d9SRoland McGrath int compat_ptrace_request(struct task_struct *child, compat_long_t request, 1205032d82d9SRoland McGrath compat_ulong_t addr, compat_ulong_t data) 1206032d82d9SRoland McGrath { 1207032d82d9SRoland McGrath compat_ulong_t __user *datap = compat_ptr(data); 1208032d82d9SRoland McGrath compat_ulong_t word; 1209ae7795bcSEric W. Biederman kernel_siginfo_t siginfo; 1210032d82d9SRoland McGrath int ret; 1211032d82d9SRoland McGrath 1212032d82d9SRoland McGrath switch (request) { 1213032d82d9SRoland McGrath case PTRACE_PEEKTEXT: 1214032d82d9SRoland McGrath case PTRACE_PEEKDATA: 121584d77d3fSEric W. Biederman ret = ptrace_access_vm(child, addr, &word, sizeof(word), 1216f307ab6dSLorenzo Stoakes FOLL_FORCE); 1217032d82d9SRoland McGrath if (ret != sizeof(word)) 1218032d82d9SRoland McGrath ret = -EIO; 1219032d82d9SRoland McGrath else 1220032d82d9SRoland McGrath ret = put_user(word, datap); 1221032d82d9SRoland McGrath break; 1222032d82d9SRoland McGrath 1223032d82d9SRoland McGrath case PTRACE_POKETEXT: 1224032d82d9SRoland McGrath case PTRACE_POKEDATA: 122584d77d3fSEric W. Biederman ret = ptrace_access_vm(child, addr, &data, sizeof(data), 1226f307ab6dSLorenzo Stoakes FOLL_FORCE | FOLL_WRITE); 1227032d82d9SRoland McGrath ret = (ret != sizeof(data) ? -EIO : 0); 1228032d82d9SRoland McGrath break; 1229032d82d9SRoland McGrath 1230032d82d9SRoland McGrath case PTRACE_GETEVENTMSG: 1231032d82d9SRoland McGrath ret = put_user((compat_ulong_t) child->ptrace_message, datap); 1232032d82d9SRoland McGrath break; 1233032d82d9SRoland McGrath 1234e16b2781SRoland McGrath case PTRACE_GETSIGINFO: 1235e16b2781SRoland McGrath ret = ptrace_getsiginfo(child, &siginfo); 1236e16b2781SRoland McGrath if (!ret) 1237e16b2781SRoland McGrath ret = copy_siginfo_to_user32( 1238e16b2781SRoland McGrath (struct compat_siginfo __user *) datap, 1239e16b2781SRoland McGrath &siginfo); 1240e16b2781SRoland McGrath break; 1241e16b2781SRoland McGrath 1242e16b2781SRoland McGrath case PTRACE_SETSIGINFO: 12434cd2e0e7SEric W. Biederman ret = copy_siginfo_from_user32( 12444cd2e0e7SEric W. Biederman &siginfo, (struct compat_siginfo __user *) datap); 12454cd2e0e7SEric W. Biederman if (!ret) 1246e16b2781SRoland McGrath ret = ptrace_setsiginfo(child, &siginfo); 1247e16b2781SRoland McGrath break; 12482225a122SSuresh Siddha #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 12492225a122SSuresh Siddha case PTRACE_GETREGSET: 12502225a122SSuresh Siddha case PTRACE_SETREGSET: 12512225a122SSuresh Siddha { 12522225a122SSuresh Siddha struct iovec kiov; 12532225a122SSuresh Siddha struct compat_iovec __user *uiov = 12542225a122SSuresh Siddha (struct compat_iovec __user *) datap; 12552225a122SSuresh Siddha compat_uptr_t ptr; 12562225a122SSuresh Siddha compat_size_t len; 12572225a122SSuresh Siddha 125896d4f267SLinus Torvalds if (!access_ok(uiov, sizeof(*uiov))) 12592225a122SSuresh Siddha return -EFAULT; 12602225a122SSuresh Siddha 12612225a122SSuresh Siddha if (__get_user(ptr, &uiov->iov_base) || 12622225a122SSuresh Siddha __get_user(len, &uiov->iov_len)) 12632225a122SSuresh Siddha return -EFAULT; 12642225a122SSuresh Siddha 12652225a122SSuresh Siddha kiov.iov_base = compat_ptr(ptr); 12662225a122SSuresh Siddha kiov.iov_len = len; 12672225a122SSuresh Siddha 12682225a122SSuresh Siddha ret = ptrace_regset(child, request, addr, &kiov); 12692225a122SSuresh Siddha if (!ret) 12702225a122SSuresh Siddha ret = __put_user(kiov.iov_len, &uiov->iov_len); 12712225a122SSuresh Siddha break; 12722225a122SSuresh Siddha } 12732225a122SSuresh Siddha #endif 1274e16b2781SRoland McGrath 1275032d82d9SRoland McGrath default: 1276032d82d9SRoland McGrath ret = ptrace_request(child, request, addr, data); 1277032d82d9SRoland McGrath } 1278032d82d9SRoland McGrath 1279032d82d9SRoland McGrath return ret; 1280032d82d9SRoland McGrath } 1281c269f196SRoland McGrath 128262a6fa97SHeiko Carstens COMPAT_SYSCALL_DEFINE4(ptrace, compat_long_t, request, compat_long_t, pid, 128362a6fa97SHeiko Carstens compat_long_t, addr, compat_long_t, data) 1284c269f196SRoland McGrath { 1285c269f196SRoland McGrath struct task_struct *child; 1286c269f196SRoland McGrath long ret; 1287c269f196SRoland McGrath 1288c269f196SRoland McGrath if (request == PTRACE_TRACEME) { 1289c269f196SRoland McGrath ret = ptrace_traceme(); 1290c269f196SRoland McGrath goto out; 1291c269f196SRoland McGrath } 1292c269f196SRoland McGrath 12932ee08260SMike Rapoport child = find_get_task_by_vpid(pid); 12942ee08260SMike Rapoport if (!child) { 12952ee08260SMike Rapoport ret = -ESRCH; 1296c269f196SRoland McGrath goto out; 1297c269f196SRoland McGrath } 1298c269f196SRoland McGrath 12993544d72aSTejun Heo if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) { 1300aa9147c9SDenys Vlasenko ret = ptrace_attach(child, request, addr, data); 1301c269f196SRoland McGrath /* 1302c269f196SRoland McGrath * Some architectures need to do book-keeping after 1303c269f196SRoland McGrath * a ptrace attach. 1304c269f196SRoland McGrath */ 1305c269f196SRoland McGrath if (!ret) 1306c269f196SRoland McGrath arch_ptrace_attach(child); 1307c269f196SRoland McGrath goto out_put_task_struct; 1308c269f196SRoland McGrath } 1309c269f196SRoland McGrath 1310fca26f26STejun Heo ret = ptrace_check_attach(child, request == PTRACE_KILL || 1311fca26f26STejun Heo request == PTRACE_INTERRUPT); 13129899d11fSOleg Nesterov if (!ret) { 1313c269f196SRoland McGrath ret = compat_arch_ptrace(child, request, addr, data); 13149899d11fSOleg Nesterov if (ret || request != PTRACE_DETACH) 13159899d11fSOleg Nesterov ptrace_unfreeze_traced(child); 13169899d11fSOleg Nesterov } 1317c269f196SRoland McGrath 1318c269f196SRoland McGrath out_put_task_struct: 1319c269f196SRoland McGrath put_task_struct(child); 1320c269f196SRoland McGrath out: 1321c269f196SRoland McGrath return ret; 1322c269f196SRoland McGrath } 132396b8936aSChristoph Hellwig #endif /* CONFIG_COMPAT */ 1324