11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/kernel/ptrace.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * (C) Copyright 1999 Linus Torvalds 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Common interfaces for "ptrace()" which we do not want 71da177e4SLinus Torvalds * to continually duplicate across every architecture. 81da177e4SLinus Torvalds */ 91da177e4SLinus Torvalds 10c59ede7bSRandy.Dunlap #include <linux/capability.h> 119984de1aSPaul Gortmaker #include <linux/export.h> 121da177e4SLinus Torvalds #include <linux/sched.h> 136e84f315SIngo Molnar #include <linux/sched/mm.h> 141da177e4SLinus Torvalds #include <linux/errno.h> 151da177e4SLinus Torvalds #include <linux/mm.h> 161da177e4SLinus Torvalds #include <linux/highmem.h> 171da177e4SLinus Torvalds #include <linux/pagemap.h> 181da177e4SLinus Torvalds #include <linux/ptrace.h> 191da177e4SLinus Torvalds #include <linux/security.h> 207ed20e1aSJesper Juhl #include <linux/signal.h> 21a27bb332SKent Overstreet #include <linux/uio.h> 22a5cb013dSAl Viro #include <linux/audit.h> 23b488893aSPavel Emelyanov #include <linux/pid_namespace.h> 24f17d30a8SAdrian Bunk #include <linux/syscalls.h> 253a709703SRoland McGrath #include <linux/uaccess.h> 262225a122SSuresh Siddha #include <linux/regset.h> 27bf26c018SFrederic Weisbecker #include <linux/hw_breakpoint.h> 28f701e5b7SVladimir Zapolskiy #include <linux/cn_proc.h> 2984c751bdSAndrey Vagin #include <linux/compat.h> 301da177e4SLinus Torvalds 3184d77d3fSEric W. Biederman /* 3284d77d3fSEric W. Biederman * Access another process' address space via ptrace. 3384d77d3fSEric W. Biederman * Source/target buffer must be kernel space, 3484d77d3fSEric W. Biederman * Do not walk the page table directly, use get_user_pages 3584d77d3fSEric W. Biederman */ 3684d77d3fSEric W. Biederman int ptrace_access_vm(struct task_struct *tsk, unsigned long addr, 3784d77d3fSEric W. Biederman void *buf, int len, unsigned int gup_flags) 3884d77d3fSEric W. Biederman { 3984d77d3fSEric W. Biederman struct mm_struct *mm; 4084d77d3fSEric W. Biederman int ret; 4184d77d3fSEric W. Biederman 4284d77d3fSEric W. Biederman mm = get_task_mm(tsk); 4384d77d3fSEric W. Biederman if (!mm) 4484d77d3fSEric W. Biederman return 0; 4584d77d3fSEric W. Biederman 4684d77d3fSEric W. Biederman if (!tsk->ptrace || 4784d77d3fSEric W. Biederman (current != tsk->parent) || 4884d77d3fSEric W. Biederman ((get_dumpable(mm) != SUID_DUMP_USER) && 4984d77d3fSEric W. Biederman !ptracer_capable(tsk, mm->user_ns))) { 5084d77d3fSEric W. Biederman mmput(mm); 5184d77d3fSEric W. Biederman return 0; 5284d77d3fSEric W. Biederman } 5384d77d3fSEric W. Biederman 5484d77d3fSEric W. Biederman ret = __access_remote_vm(tsk, mm, addr, buf, len, gup_flags); 5584d77d3fSEric W. Biederman mmput(mm); 5684d77d3fSEric W. Biederman 5784d77d3fSEric W. Biederman return ret; 5884d77d3fSEric W. Biederman } 5984d77d3fSEric W. Biederman 60bf53de90SMarkus Metzger 61bf53de90SMarkus Metzger /* 621da177e4SLinus Torvalds * ptrace a task: make the debugger its new parent and 631da177e4SLinus Torvalds * move it to the ptrace list. 641da177e4SLinus Torvalds * 651da177e4SLinus Torvalds * Must be called with the tasklist lock write-held. 661da177e4SLinus Torvalds */ 6736c8b586SIngo Molnar void __ptrace_link(struct task_struct *child, struct task_struct *new_parent) 681da177e4SLinus Torvalds { 69f470021aSRoland McGrath BUG_ON(!list_empty(&child->ptrace_entry)); 70f470021aSRoland McGrath list_add(&child->ptrace_entry, &new_parent->ptraced); 711da177e4SLinus Torvalds child->parent = new_parent; 7264b875f7SEric W. Biederman rcu_read_lock(); 7364b875f7SEric W. Biederman child->ptracer_cred = get_cred(__task_cred(new_parent)); 7464b875f7SEric W. Biederman rcu_read_unlock(); 751da177e4SLinus Torvalds } 761da177e4SLinus Torvalds 77e3bd058fSTejun Heo /** 78e3bd058fSTejun Heo * __ptrace_unlink - unlink ptracee and restore its execution state 79e3bd058fSTejun Heo * @child: ptracee to be unlinked 801da177e4SLinus Torvalds * 810e9f0a4aSTejun Heo * Remove @child from the ptrace list, move it back to the original parent, 820e9f0a4aSTejun Heo * and restore the execution state so that it conforms to the group stop 830e9f0a4aSTejun Heo * state. 840e9f0a4aSTejun Heo * 850e9f0a4aSTejun Heo * Unlinking can happen via two paths - explicit PTRACE_DETACH or ptracer 860e9f0a4aSTejun Heo * exiting. For PTRACE_DETACH, unless the ptracee has been killed between 870e9f0a4aSTejun Heo * ptrace_check_attach() and here, it's guaranteed to be in TASK_TRACED. 880e9f0a4aSTejun Heo * If the ptracer is exiting, the ptracee can be in any state. 890e9f0a4aSTejun Heo * 900e9f0a4aSTejun Heo * After detach, the ptracee should be in a state which conforms to the 910e9f0a4aSTejun Heo * group stop. If the group is stopped or in the process of stopping, the 920e9f0a4aSTejun Heo * ptracee should be put into TASK_STOPPED; otherwise, it should be woken 930e9f0a4aSTejun Heo * up from TASK_TRACED. 940e9f0a4aSTejun Heo * 950e9f0a4aSTejun Heo * If the ptracee is in TASK_TRACED and needs to be moved to TASK_STOPPED, 960e9f0a4aSTejun Heo * it goes through TRACED -> RUNNING -> STOPPED transition which is similar 970e9f0a4aSTejun Heo * to but in the opposite direction of what happens while attaching to a 980e9f0a4aSTejun Heo * stopped task. However, in this direction, the intermediate RUNNING 990e9f0a4aSTejun Heo * state is not hidden even from the current ptracer and if it immediately 1000e9f0a4aSTejun Heo * re-attaches and performs a WNOHANG wait(2), it may fail. 101e3bd058fSTejun Heo * 102e3bd058fSTejun Heo * CONTEXT: 103e3bd058fSTejun Heo * write_lock_irq(tasklist_lock) 1041da177e4SLinus Torvalds */ 10536c8b586SIngo Molnar void __ptrace_unlink(struct task_struct *child) 1061da177e4SLinus Torvalds { 10764b875f7SEric W. Biederman const struct cred *old_cred; 1085ecfbae0SOleg Nesterov BUG_ON(!child->ptrace); 1095ecfbae0SOleg Nesterov 1100a5bf409SAles Novak clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); 1110a5bf409SAles Novak 1121da177e4SLinus Torvalds child->parent = child->real_parent; 113f470021aSRoland McGrath list_del_init(&child->ptrace_entry); 11464b875f7SEric W. Biederman old_cred = child->ptracer_cred; 11564b875f7SEric W. Biederman child->ptracer_cred = NULL; 11664b875f7SEric W. Biederman put_cred(old_cred); 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds spin_lock(&child->sighand->siglock); 1191333ab03SOleg Nesterov child->ptrace = 0; 1201da177e4SLinus Torvalds /* 12173ddff2bSTejun Heo * Clear all pending traps and TRAPPING. TRAPPING should be 12273ddff2bSTejun Heo * cleared regardless of JOBCTL_STOP_PENDING. Do it explicitly. 12373ddff2bSTejun Heo */ 12473ddff2bSTejun Heo task_clear_jobctl_pending(child, JOBCTL_TRAP_MASK); 12573ddff2bSTejun Heo task_clear_jobctl_trapping(child); 12673ddff2bSTejun Heo 12773ddff2bSTejun Heo /* 128a8f072c1STejun Heo * Reinstate JOBCTL_STOP_PENDING if group stop is in effect and 1290e9f0a4aSTejun Heo * @child isn't dead. 1301da177e4SLinus Torvalds */ 1310e9f0a4aSTejun Heo if (!(child->flags & PF_EXITING) && 1320e9f0a4aSTejun Heo (child->signal->flags & SIGNAL_STOP_STOPPED || 1338a88951bSOleg Nesterov child->signal->group_stop_count)) { 134a8f072c1STejun Heo child->jobctl |= JOBCTL_STOP_PENDING; 1350e9f0a4aSTejun Heo 1360e9f0a4aSTejun Heo /* 1378a88951bSOleg Nesterov * This is only possible if this thread was cloned by the 1388a88951bSOleg Nesterov * traced task running in the stopped group, set the signal 1398a88951bSOleg Nesterov * for the future reports. 1408a88951bSOleg Nesterov * FIXME: we should change ptrace_init_task() to handle this 1418a88951bSOleg Nesterov * case. 1428a88951bSOleg Nesterov */ 1438a88951bSOleg Nesterov if (!(child->jobctl & JOBCTL_STOP_SIGMASK)) 1448a88951bSOleg Nesterov child->jobctl |= SIGSTOP; 1458a88951bSOleg Nesterov } 1468a88951bSOleg Nesterov 1478a88951bSOleg Nesterov /* 1480e9f0a4aSTejun Heo * If transition to TASK_STOPPED is pending or in TASK_TRACED, kick 1490e9f0a4aSTejun Heo * @child in the butt. Note that @resume should be used iff @child 1500e9f0a4aSTejun Heo * is in TASK_TRACED; otherwise, we might unduly disrupt 1510e9f0a4aSTejun Heo * TASK_KILLABLE sleeps. 1520e9f0a4aSTejun Heo */ 153a8f072c1STejun Heo if (child->jobctl & JOBCTL_STOP_PENDING || task_is_traced(child)) 154910ffdb1SOleg Nesterov ptrace_signal_wake_up(child, true); 1550e9f0a4aSTejun Heo 1561da177e4SLinus Torvalds spin_unlock(&child->sighand->siglock); 1571da177e4SLinus Torvalds } 1581da177e4SLinus Torvalds 1599899d11fSOleg Nesterov /* Ensure that nothing can wake it up, even SIGKILL */ 1609899d11fSOleg Nesterov static bool ptrace_freeze_traced(struct task_struct *task) 1619899d11fSOleg Nesterov { 1629899d11fSOleg Nesterov bool ret = false; 1639899d11fSOleg Nesterov 1649899d11fSOleg Nesterov /* Lockless, nobody but us can set this flag */ 1659899d11fSOleg Nesterov if (task->jobctl & JOBCTL_LISTENING) 1669899d11fSOleg Nesterov return ret; 1679899d11fSOleg Nesterov 1689899d11fSOleg Nesterov spin_lock_irq(&task->sighand->siglock); 1699899d11fSOleg Nesterov if (task_is_traced(task) && !__fatal_signal_pending(task)) { 1709899d11fSOleg Nesterov task->state = __TASK_TRACED; 1719899d11fSOleg Nesterov ret = true; 1729899d11fSOleg Nesterov } 1739899d11fSOleg Nesterov spin_unlock_irq(&task->sighand->siglock); 1749899d11fSOleg Nesterov 1759899d11fSOleg Nesterov return ret; 1769899d11fSOleg Nesterov } 1779899d11fSOleg Nesterov 1789899d11fSOleg Nesterov static void ptrace_unfreeze_traced(struct task_struct *task) 1799899d11fSOleg Nesterov { 1809899d11fSOleg Nesterov if (task->state != __TASK_TRACED) 1819899d11fSOleg Nesterov return; 1829899d11fSOleg Nesterov 1839899d11fSOleg Nesterov WARN_ON(!task->ptrace || task->parent != current); 1849899d11fSOleg Nesterov 1859899d11fSOleg Nesterov spin_lock_irq(&task->sighand->siglock); 1869899d11fSOleg Nesterov if (__fatal_signal_pending(task)) 1879899d11fSOleg Nesterov wake_up_state(task, __TASK_TRACED); 1889899d11fSOleg Nesterov else 1899899d11fSOleg Nesterov task->state = TASK_TRACED; 1909899d11fSOleg Nesterov spin_unlock_irq(&task->sighand->siglock); 1919899d11fSOleg Nesterov } 1929899d11fSOleg Nesterov 193755e276bSTejun Heo /** 194755e276bSTejun Heo * ptrace_check_attach - check whether ptracee is ready for ptrace operation 195755e276bSTejun Heo * @child: ptracee to check for 196755e276bSTejun Heo * @ignore_state: don't check whether @child is currently %TASK_TRACED 197755e276bSTejun Heo * 198755e276bSTejun Heo * Check whether @child is being ptraced by %current and ready for further 199755e276bSTejun Heo * ptrace operations. If @ignore_state is %false, @child also should be in 200755e276bSTejun Heo * %TASK_TRACED state and on return the child is guaranteed to be traced 201755e276bSTejun Heo * and not executing. If @ignore_state is %true, @child can be in any 202755e276bSTejun Heo * state. 203755e276bSTejun Heo * 204755e276bSTejun Heo * CONTEXT: 205755e276bSTejun Heo * Grabs and releases tasklist_lock and @child->sighand->siglock. 206755e276bSTejun Heo * 207755e276bSTejun Heo * RETURNS: 208755e276bSTejun Heo * 0 on success, -ESRCH if %child is not ready. 2091da177e4SLinus Torvalds */ 210edea0d03SOleg Nesterov static int ptrace_check_attach(struct task_struct *child, bool ignore_state) 2111da177e4SLinus Torvalds { 2121da177e4SLinus Torvalds int ret = -ESRCH; 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds /* 2151da177e4SLinus Torvalds * We take the read lock around doing both checks to close a 2161da177e4SLinus Torvalds * possible race where someone else was tracing our child and 2171da177e4SLinus Torvalds * detached between these two checks. After this locked check, 2181da177e4SLinus Torvalds * we are sure that this is our traced child and that can only 2191da177e4SLinus Torvalds * be changed by us so it's not changing right after this. 2201da177e4SLinus Torvalds */ 2211da177e4SLinus Torvalds read_lock(&tasklist_lock); 2229899d11fSOleg Nesterov if (child->ptrace && child->parent == current) { 2239899d11fSOleg Nesterov WARN_ON(child->state == __TASK_TRACED); 224c0c0b649SOleg Nesterov /* 225c0c0b649SOleg Nesterov * child->sighand can't be NULL, release_task() 226c0c0b649SOleg Nesterov * does ptrace_unlink() before __exit_signal(). 227c0c0b649SOleg Nesterov */ 2289899d11fSOleg Nesterov if (ignore_state || ptrace_freeze_traced(child)) 229321fb561SOleg Nesterov ret = 0; 2301da177e4SLinus Torvalds } 2311da177e4SLinus Torvalds read_unlock(&tasklist_lock); 2321da177e4SLinus Torvalds 2339899d11fSOleg Nesterov if (!ret && !ignore_state) { 2349899d11fSOleg Nesterov if (!wait_task_inactive(child, __TASK_TRACED)) { 2359899d11fSOleg Nesterov /* 2369899d11fSOleg Nesterov * This can only happen if may_ptrace_stop() fails and 2379899d11fSOleg Nesterov * ptrace_stop() changes ->state back to TASK_RUNNING, 2389899d11fSOleg Nesterov * so we should not worry about leaking __TASK_TRACED. 2399899d11fSOleg Nesterov */ 2409899d11fSOleg Nesterov WARN_ON(child->state == __TASK_TRACED); 2419899d11fSOleg Nesterov ret = -ESRCH; 2429899d11fSOleg Nesterov } 2439899d11fSOleg Nesterov } 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds return ret; 2461da177e4SLinus Torvalds } 2471da177e4SLinus Torvalds 24869f594a3SEric Paris static int ptrace_has_cap(struct user_namespace *ns, unsigned int mode) 24969f594a3SEric Paris { 25069f594a3SEric Paris if (mode & PTRACE_MODE_NOAUDIT) 25169f594a3SEric Paris return has_ns_capability_noaudit(current, ns, CAP_SYS_PTRACE); 25269f594a3SEric Paris else 25369f594a3SEric Paris return has_ns_capability(current, ns, CAP_SYS_PTRACE); 25469f594a3SEric Paris } 25569f594a3SEric Paris 2569f99798fSTetsuo Handa /* Returns 0 on success, -errno on denial. */ 2579f99798fSTetsuo Handa static int __ptrace_may_access(struct task_struct *task, unsigned int mode) 258ab8d11beSMiklos Szeredi { 259c69e8d9cSDavid Howells const struct cred *cred = current_cred(), *tcred; 260bfedb589SEric W. Biederman struct mm_struct *mm; 261caaee623SJann Horn kuid_t caller_uid; 262caaee623SJann Horn kgid_t caller_gid; 263caaee623SJann Horn 264caaee623SJann Horn if (!(mode & PTRACE_MODE_FSCREDS) == !(mode & PTRACE_MODE_REALCREDS)) { 265caaee623SJann Horn WARN(1, "denying ptrace access check without PTRACE_MODE_*CREDS\n"); 266caaee623SJann Horn return -EPERM; 267caaee623SJann Horn } 268b6dff3ecSDavid Howells 269df26c40eSEric W. Biederman /* May we inspect the given task? 270df26c40eSEric W. Biederman * This check is used both for attaching with ptrace 271df26c40eSEric W. Biederman * and for allowing access to sensitive information in /proc. 272df26c40eSEric W. Biederman * 273df26c40eSEric W. Biederman * ptrace_attach denies several cases that /proc allows 274df26c40eSEric W. Biederman * because setting up the necessary parent/child relationship 275df26c40eSEric W. Biederman * or halting the specified task is impossible. 276df26c40eSEric W. Biederman */ 277caaee623SJann Horn 278df26c40eSEric W. Biederman /* Don't let security modules deny introspection */ 27973af963fSMark Grondona if (same_thread_group(task, current)) 280df26c40eSEric W. Biederman return 0; 281c69e8d9cSDavid Howells rcu_read_lock(); 282caaee623SJann Horn if (mode & PTRACE_MODE_FSCREDS) { 283caaee623SJann Horn caller_uid = cred->fsuid; 284caaee623SJann Horn caller_gid = cred->fsgid; 285caaee623SJann Horn } else { 286caaee623SJann Horn /* 287caaee623SJann Horn * Using the euid would make more sense here, but something 288caaee623SJann Horn * in userland might rely on the old behavior, and this 289caaee623SJann Horn * shouldn't be a security problem since 290caaee623SJann Horn * PTRACE_MODE_REALCREDS implies that the caller explicitly 291caaee623SJann Horn * used a syscall that requests access to another process 292caaee623SJann Horn * (and not a filesystem syscall to procfs). 293caaee623SJann Horn */ 294caaee623SJann Horn caller_uid = cred->uid; 295caaee623SJann Horn caller_gid = cred->gid; 296caaee623SJann Horn } 297c69e8d9cSDavid Howells tcred = __task_cred(task); 298caaee623SJann Horn if (uid_eq(caller_uid, tcred->euid) && 299caaee623SJann Horn uid_eq(caller_uid, tcred->suid) && 300caaee623SJann Horn uid_eq(caller_uid, tcred->uid) && 301caaee623SJann Horn gid_eq(caller_gid, tcred->egid) && 302caaee623SJann Horn gid_eq(caller_gid, tcred->sgid) && 303caaee623SJann Horn gid_eq(caller_gid, tcred->gid)) 3048409cca7SSerge E. Hallyn goto ok; 305c4a4d603SEric W. Biederman if (ptrace_has_cap(tcred->user_ns, mode)) 3068409cca7SSerge E. Hallyn goto ok; 307c69e8d9cSDavid Howells rcu_read_unlock(); 308ab8d11beSMiklos Szeredi return -EPERM; 3098409cca7SSerge E. Hallyn ok: 310c69e8d9cSDavid Howells rcu_read_unlock(); 311bfedb589SEric W. Biederman mm = task->mm; 312bfedb589SEric W. Biederman if (mm && 313bfedb589SEric W. Biederman ((get_dumpable(mm) != SUID_DUMP_USER) && 314bfedb589SEric W. Biederman !ptrace_has_cap(mm->user_ns, mode))) 315ab8d11beSMiklos Szeredi return -EPERM; 316ab8d11beSMiklos Szeredi 3179e48858fSIngo Molnar return security_ptrace_access_check(task, mode); 318ab8d11beSMiklos Szeredi } 319ab8d11beSMiklos Szeredi 320006ebb40SStephen Smalley bool ptrace_may_access(struct task_struct *task, unsigned int mode) 321ab8d11beSMiklos Szeredi { 322ab8d11beSMiklos Szeredi int err; 323ab8d11beSMiklos Szeredi task_lock(task); 324006ebb40SStephen Smalley err = __ptrace_may_access(task, mode); 325ab8d11beSMiklos Szeredi task_unlock(task); 3263a709703SRoland McGrath return !err; 327ab8d11beSMiklos Szeredi } 328ab8d11beSMiklos Szeredi 3293544d72aSTejun Heo static int ptrace_attach(struct task_struct *task, long request, 330aa9147c9SDenys Vlasenko unsigned long addr, 3313544d72aSTejun Heo unsigned long flags) 3321da177e4SLinus Torvalds { 3333544d72aSTejun Heo bool seize = (request == PTRACE_SEIZE); 3341da177e4SLinus Torvalds int retval; 335f5b40e36SLinus Torvalds 3363544d72aSTejun Heo retval = -EIO; 337aa9147c9SDenys Vlasenko if (seize) { 338aa9147c9SDenys Vlasenko if (addr != 0) 3393544d72aSTejun Heo goto out; 340aa9147c9SDenys Vlasenko if (flags & ~(unsigned long)PTRACE_O_MASK) 341aa9147c9SDenys Vlasenko goto out; 342aa9147c9SDenys Vlasenko flags = PT_PTRACED | PT_SEIZED | (flags << PT_OPT_FLAG_SHIFT); 343aa9147c9SDenys Vlasenko } else { 344aa9147c9SDenys Vlasenko flags = PT_PTRACED; 345aa9147c9SDenys Vlasenko } 3463544d72aSTejun Heo 347a5cb013dSAl Viro audit_ptrace(task); 348a5cb013dSAl Viro 3491da177e4SLinus Torvalds retval = -EPERM; 350b79b7ba9SOleg Nesterov if (unlikely(task->flags & PF_KTHREAD)) 351b79b7ba9SOleg Nesterov goto out; 352bac0abd6SPavel Emelyanov if (same_thread_group(task, current)) 353f5b40e36SLinus Torvalds goto out; 354f5b40e36SLinus Torvalds 355f2f0b00aSOleg Nesterov /* 356f2f0b00aSOleg Nesterov * Protect exec's credential calculations against our interference; 35786b6c1f3SDenys Vlasenko * SUID, SGID and LSM creds get determined differently 3585e751e99SDavid Howells * under ptrace. 359d84f4f99SDavid Howells */ 360793285fcSOleg Nesterov retval = -ERESTARTNOINTR; 3619b1bf12dSKOSAKI Motohiro if (mutex_lock_interruptible(&task->signal->cred_guard_mutex)) 362d84f4f99SDavid Howells goto out; 3634b105cbbSOleg Nesterov 364f5b40e36SLinus Torvalds task_lock(task); 365caaee623SJann Horn retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH_REALCREDS); 3664b105cbbSOleg Nesterov task_unlock(task); 3671da177e4SLinus Torvalds if (retval) 3684b105cbbSOleg Nesterov goto unlock_creds; 3691da177e4SLinus Torvalds 3704b105cbbSOleg Nesterov write_lock_irq(&tasklist_lock); 371b79b7ba9SOleg Nesterov retval = -EPERM; 372b79b7ba9SOleg Nesterov if (unlikely(task->exit_state)) 3734b105cbbSOleg Nesterov goto unlock_tasklist; 374f2f0b00aSOleg Nesterov if (task->ptrace) 3754b105cbbSOleg Nesterov goto unlock_tasklist; 376b79b7ba9SOleg Nesterov 3773544d72aSTejun Heo if (seize) 378aa9147c9SDenys Vlasenko flags |= PT_SEIZED; 379aa9147c9SDenys Vlasenko task->ptrace = flags; 3801da177e4SLinus Torvalds 3811da177e4SLinus Torvalds __ptrace_link(task, current); 3823544d72aSTejun Heo 3833544d72aSTejun Heo /* SEIZE doesn't trap tracee on attach */ 3843544d72aSTejun Heo if (!seize) 38533e9fc7dSOleg Nesterov send_sig_info(SIGSTOP, SEND_SIG_FORCED, task); 386b79b7ba9SOleg Nesterov 387d79fdd6dSTejun Heo spin_lock(&task->sighand->siglock); 388d79fdd6dSTejun Heo 389d79fdd6dSTejun Heo /* 39073ddff2bSTejun Heo * If the task is already STOPPED, set JOBCTL_TRAP_STOP and 391d79fdd6dSTejun Heo * TRAPPING, and kick it so that it transits to TRACED. TRAPPING 392d79fdd6dSTejun Heo * will be cleared if the child completes the transition or any 393d79fdd6dSTejun Heo * event which clears the group stop states happens. We'll wait 394d79fdd6dSTejun Heo * for the transition to complete before returning from this 395d79fdd6dSTejun Heo * function. 396d79fdd6dSTejun Heo * 397d79fdd6dSTejun Heo * This hides STOPPED -> RUNNING -> TRACED transition from the 398d79fdd6dSTejun Heo * attaching thread but a different thread in the same group can 399d79fdd6dSTejun Heo * still observe the transient RUNNING state. IOW, if another 400d79fdd6dSTejun Heo * thread's WNOHANG wait(2) on the stopped tracee races against 401d79fdd6dSTejun Heo * ATTACH, the wait(2) may fail due to the transient RUNNING. 402d79fdd6dSTejun Heo * 403d79fdd6dSTejun Heo * The following task_is_stopped() test is safe as both transitions 404d79fdd6dSTejun Heo * in and out of STOPPED are protected by siglock. 405d79fdd6dSTejun Heo */ 4067dd3db54STejun Heo if (task_is_stopped(task) && 40773ddff2bSTejun Heo task_set_jobctl_pending(task, JOBCTL_TRAP_STOP | JOBCTL_TRAPPING)) 408910ffdb1SOleg Nesterov signal_wake_up_state(task, __TASK_STOPPED); 409d79fdd6dSTejun Heo 410d79fdd6dSTejun Heo spin_unlock(&task->sighand->siglock); 411d79fdd6dSTejun Heo 412b79b7ba9SOleg Nesterov retval = 0; 4134b105cbbSOleg Nesterov unlock_tasklist: 4144b105cbbSOleg Nesterov write_unlock_irq(&tasklist_lock); 4154b105cbbSOleg Nesterov unlock_creds: 4169b1bf12dSKOSAKI Motohiro mutex_unlock(&task->signal->cred_guard_mutex); 417f5b40e36SLinus Torvalds out: 418f701e5b7SVladimir Zapolskiy if (!retval) { 4197c3b00e0SOleg Nesterov /* 4207c3b00e0SOleg Nesterov * We do not bother to change retval or clear JOBCTL_TRAPPING 4217c3b00e0SOleg Nesterov * if wait_on_bit() was interrupted by SIGKILL. The tracer will 4227c3b00e0SOleg Nesterov * not return to user-mode, it will exit and clear this bit in 4237c3b00e0SOleg Nesterov * __ptrace_unlink() if it wasn't already cleared by the tracee; 4247c3b00e0SOleg Nesterov * and until then nobody can ptrace this task. 4257c3b00e0SOleg Nesterov */ 4267c3b00e0SOleg Nesterov wait_on_bit(&task->jobctl, JOBCTL_TRAPPING_BIT, TASK_KILLABLE); 427f701e5b7SVladimir Zapolskiy proc_ptrace_connector(task, PTRACE_ATTACH); 428f701e5b7SVladimir Zapolskiy } 429f701e5b7SVladimir Zapolskiy 4301da177e4SLinus Torvalds return retval; 4311da177e4SLinus Torvalds } 4321da177e4SLinus Torvalds 433f2f0b00aSOleg Nesterov /** 434f2f0b00aSOleg Nesterov * ptrace_traceme -- helper for PTRACE_TRACEME 435f2f0b00aSOleg Nesterov * 436f2f0b00aSOleg Nesterov * Performs checks and sets PT_PTRACED. 437f2f0b00aSOleg Nesterov * Should be used by all ptrace implementations for PTRACE_TRACEME. 438f2f0b00aSOleg Nesterov */ 439e3e89cc5SLinus Torvalds static int ptrace_traceme(void) 440f2f0b00aSOleg Nesterov { 441f2f0b00aSOleg Nesterov int ret = -EPERM; 442f2f0b00aSOleg Nesterov 4434b105cbbSOleg Nesterov write_lock_irq(&tasklist_lock); 4444b105cbbSOleg Nesterov /* Are we already being traced? */ 445f2f0b00aSOleg Nesterov if (!current->ptrace) { 446f2f0b00aSOleg Nesterov ret = security_ptrace_traceme(current->parent); 447f2f0b00aSOleg Nesterov /* 448f2f0b00aSOleg Nesterov * Check PF_EXITING to ensure ->real_parent has not passed 449f2f0b00aSOleg Nesterov * exit_ptrace(). Otherwise we don't report the error but 450f2f0b00aSOleg Nesterov * pretend ->real_parent untraces us right after return. 451f2f0b00aSOleg Nesterov */ 452f2f0b00aSOleg Nesterov if (!ret && !(current->real_parent->flags & PF_EXITING)) { 453f2f0b00aSOleg Nesterov current->ptrace = PT_PTRACED; 454f2f0b00aSOleg Nesterov __ptrace_link(current, current->real_parent); 455f2f0b00aSOleg Nesterov } 456f2f0b00aSOleg Nesterov } 4574b105cbbSOleg Nesterov write_unlock_irq(&tasklist_lock); 4584b105cbbSOleg Nesterov 459f2f0b00aSOleg Nesterov return ret; 460f2f0b00aSOleg Nesterov } 461f2f0b00aSOleg Nesterov 46239c626aeSOleg Nesterov /* 46339c626aeSOleg Nesterov * Called with irqs disabled, returns true if childs should reap themselves. 46439c626aeSOleg Nesterov */ 46539c626aeSOleg Nesterov static int ignoring_children(struct sighand_struct *sigh) 46639c626aeSOleg Nesterov { 46739c626aeSOleg Nesterov int ret; 46839c626aeSOleg Nesterov spin_lock(&sigh->siglock); 46939c626aeSOleg Nesterov ret = (sigh->action[SIGCHLD-1].sa.sa_handler == SIG_IGN) || 47039c626aeSOleg Nesterov (sigh->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT); 47139c626aeSOleg Nesterov spin_unlock(&sigh->siglock); 47239c626aeSOleg Nesterov return ret; 47339c626aeSOleg Nesterov } 47439c626aeSOleg Nesterov 47539c626aeSOleg Nesterov /* 47639c626aeSOleg Nesterov * Called with tasklist_lock held for writing. 47739c626aeSOleg Nesterov * Unlink a traced task, and clean it up if it was a traced zombie. 47839c626aeSOleg Nesterov * Return true if it needs to be reaped with release_task(). 47939c626aeSOleg Nesterov * (We can't call release_task() here because we already hold tasklist_lock.) 48039c626aeSOleg Nesterov * 48139c626aeSOleg Nesterov * If it's a zombie, our attachedness prevented normal parent notification 48239c626aeSOleg Nesterov * or self-reaping. Do notification now if it would have happened earlier. 48339c626aeSOleg Nesterov * If it should reap itself, return true. 48439c626aeSOleg Nesterov * 485a7f0765eSOleg Nesterov * If it's our own child, there is no notification to do. But if our normal 486a7f0765eSOleg Nesterov * children self-reap, then this child was prevented by ptrace and we must 487a7f0765eSOleg Nesterov * reap it now, in that case we must also wake up sub-threads sleeping in 488a7f0765eSOleg Nesterov * do_wait(). 48939c626aeSOleg Nesterov */ 49039c626aeSOleg Nesterov static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p) 49139c626aeSOleg Nesterov { 4929843a1e9SOleg Nesterov bool dead; 4939843a1e9SOleg Nesterov 49439c626aeSOleg Nesterov __ptrace_unlink(p); 49539c626aeSOleg Nesterov 4969843a1e9SOleg Nesterov if (p->exit_state != EXIT_ZOMBIE) 4979843a1e9SOleg Nesterov return false; 4989843a1e9SOleg Nesterov 4999843a1e9SOleg Nesterov dead = !thread_group_leader(p); 5009843a1e9SOleg Nesterov 5019843a1e9SOleg Nesterov if (!dead && thread_group_empty(p)) { 50239c626aeSOleg Nesterov if (!same_thread_group(p->real_parent, tracer)) 5039843a1e9SOleg Nesterov dead = do_notify_parent(p, p->exit_signal); 504a7f0765eSOleg Nesterov else if (ignoring_children(tracer->sighand)) { 505a7f0765eSOleg Nesterov __wake_up_parent(p, tracer); 5069843a1e9SOleg Nesterov dead = true; 50739c626aeSOleg Nesterov } 508a7f0765eSOleg Nesterov } 50939c626aeSOleg Nesterov /* Mark it as in the process of being reaped. */ 5109843a1e9SOleg Nesterov if (dead) 51139c626aeSOleg Nesterov p->exit_state = EXIT_DEAD; 5129843a1e9SOleg Nesterov return dead; 51339c626aeSOleg Nesterov } 51439c626aeSOleg Nesterov 515e3e89cc5SLinus Torvalds static int ptrace_detach(struct task_struct *child, unsigned int data) 5161da177e4SLinus Torvalds { 5177ed20e1aSJesper Juhl if (!valid_signal(data)) 5181da177e4SLinus Torvalds return -EIO; 5191da177e4SLinus Torvalds 5201da177e4SLinus Torvalds /* Architecture-specific hardware disable .. */ 5211da177e4SLinus Torvalds ptrace_disable(child); 5221da177e4SLinus Torvalds 52395c3eb76SOleg Nesterov write_lock_irq(&tasklist_lock); 52439c626aeSOleg Nesterov /* 52564a4096cSOleg Nesterov * We rely on ptrace_freeze_traced(). It can't be killed and 52664a4096cSOleg Nesterov * untraced by another thread, it can't be a zombie. 52739c626aeSOleg Nesterov */ 52864a4096cSOleg Nesterov WARN_ON(!child->ptrace || child->exit_state); 52964a4096cSOleg Nesterov /* 53064a4096cSOleg Nesterov * tasklist_lock avoids the race with wait_task_stopped(), see 53164a4096cSOleg Nesterov * the comment in ptrace_resume(). 53264a4096cSOleg Nesterov */ 53395c3eb76SOleg Nesterov child->exit_code = data; 53464a4096cSOleg Nesterov __ptrace_detach(current, child); 5351da177e4SLinus Torvalds write_unlock_irq(&tasklist_lock); 5361da177e4SLinus Torvalds 537f701e5b7SVladimir Zapolskiy proc_ptrace_connector(child, PTRACE_DETACH); 5384576145cSOleg Nesterov 5391da177e4SLinus Torvalds return 0; 5401da177e4SLinus Torvalds } 5411da177e4SLinus Torvalds 54239c626aeSOleg Nesterov /* 543c7e49c14SOleg Nesterov * Detach all tasks we were using ptrace on. Called with tasklist held 5447c8bd232SOleg Nesterov * for writing. 54539c626aeSOleg Nesterov */ 5467c8bd232SOleg Nesterov void exit_ptrace(struct task_struct *tracer, struct list_head *dead) 54739c626aeSOleg Nesterov { 54839c626aeSOleg Nesterov struct task_struct *p, *n; 549c7e49c14SOleg Nesterov 55039c626aeSOleg Nesterov list_for_each_entry_safe(p, n, &tracer->ptraced, ptrace_entry) { 551992fb6e1SOleg Nesterov if (unlikely(p->ptrace & PT_EXITKILL)) 552992fb6e1SOleg Nesterov send_sig_info(SIGKILL, SEND_SIG_FORCED, p); 553992fb6e1SOleg Nesterov 55439c626aeSOleg Nesterov if (__ptrace_detach(tracer, p)) 5557c8bd232SOleg Nesterov list_add(&p->ptrace_entry, dead); 55639c626aeSOleg Nesterov } 55739c626aeSOleg Nesterov } 55839c626aeSOleg Nesterov 5591da177e4SLinus Torvalds int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len) 5601da177e4SLinus Torvalds { 5611da177e4SLinus Torvalds int copied = 0; 5621da177e4SLinus Torvalds 5631da177e4SLinus Torvalds while (len > 0) { 5641da177e4SLinus Torvalds char buf[128]; 5651da177e4SLinus Torvalds int this_len, retval; 5661da177e4SLinus Torvalds 5671da177e4SLinus Torvalds this_len = (len > sizeof(buf)) ? sizeof(buf) : len; 56884d77d3fSEric W. Biederman retval = ptrace_access_vm(tsk, src, buf, this_len, FOLL_FORCE); 56984d77d3fSEric W. Biederman 5701da177e4SLinus Torvalds if (!retval) { 5711da177e4SLinus Torvalds if (copied) 5721da177e4SLinus Torvalds break; 5731da177e4SLinus Torvalds return -EIO; 5741da177e4SLinus Torvalds } 5751da177e4SLinus Torvalds if (copy_to_user(dst, buf, retval)) 5761da177e4SLinus Torvalds return -EFAULT; 5771da177e4SLinus Torvalds copied += retval; 5781da177e4SLinus Torvalds src += retval; 5791da177e4SLinus Torvalds dst += retval; 5801da177e4SLinus Torvalds len -= retval; 5811da177e4SLinus Torvalds } 5821da177e4SLinus Torvalds return copied; 5831da177e4SLinus Torvalds } 5841da177e4SLinus Torvalds 5851da177e4SLinus Torvalds int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len) 5861da177e4SLinus Torvalds { 5871da177e4SLinus Torvalds int copied = 0; 5881da177e4SLinus Torvalds 5891da177e4SLinus Torvalds while (len > 0) { 5901da177e4SLinus Torvalds char buf[128]; 5911da177e4SLinus Torvalds int this_len, retval; 5921da177e4SLinus Torvalds 5931da177e4SLinus Torvalds this_len = (len > sizeof(buf)) ? sizeof(buf) : len; 5941da177e4SLinus Torvalds if (copy_from_user(buf, src, this_len)) 5951da177e4SLinus Torvalds return -EFAULT; 59684d77d3fSEric W. Biederman retval = ptrace_access_vm(tsk, dst, buf, this_len, 597f307ab6dSLorenzo Stoakes FOLL_FORCE | FOLL_WRITE); 5981da177e4SLinus Torvalds if (!retval) { 5991da177e4SLinus Torvalds if (copied) 6001da177e4SLinus Torvalds break; 6011da177e4SLinus Torvalds return -EIO; 6021da177e4SLinus Torvalds } 6031da177e4SLinus Torvalds copied += retval; 6041da177e4SLinus Torvalds src += retval; 6051da177e4SLinus Torvalds dst += retval; 6061da177e4SLinus Torvalds len -= retval; 6071da177e4SLinus Torvalds } 6081da177e4SLinus Torvalds return copied; 6091da177e4SLinus Torvalds } 6101da177e4SLinus Torvalds 6114abf9869SNamhyung Kim static int ptrace_setoptions(struct task_struct *child, unsigned long data) 6121da177e4SLinus Torvalds { 61386b6c1f3SDenys Vlasenko unsigned flags; 61486b6c1f3SDenys Vlasenko 6158c5cf9e5SDenys Vlasenko if (data & ~(unsigned long)PTRACE_O_MASK) 6168c5cf9e5SDenys Vlasenko return -EINVAL; 6178c5cf9e5SDenys Vlasenko 61813c4a901STycho Andersen if (unlikely(data & PTRACE_O_SUSPEND_SECCOMP)) { 61997f2645fSMasahiro Yamada if (!IS_ENABLED(CONFIG_CHECKPOINT_RESTORE) || 62097f2645fSMasahiro Yamada !IS_ENABLED(CONFIG_SECCOMP)) 62113c4a901STycho Andersen return -EINVAL; 62213c4a901STycho Andersen 62313c4a901STycho Andersen if (!capable(CAP_SYS_ADMIN)) 62413c4a901STycho Andersen return -EPERM; 62513c4a901STycho Andersen 62613c4a901STycho Andersen if (seccomp_mode(¤t->seccomp) != SECCOMP_MODE_DISABLED || 62713c4a901STycho Andersen current->ptrace & PT_SUSPEND_SECCOMP) 62813c4a901STycho Andersen return -EPERM; 62913c4a901STycho Andersen } 63013c4a901STycho Andersen 63186b6c1f3SDenys Vlasenko /* Avoid intermediate state when all opts are cleared */ 63286b6c1f3SDenys Vlasenko flags = child->ptrace; 63386b6c1f3SDenys Vlasenko flags &= ~(PTRACE_O_MASK << PT_OPT_FLAG_SHIFT); 63486b6c1f3SDenys Vlasenko flags |= (data << PT_OPT_FLAG_SHIFT); 63586b6c1f3SDenys Vlasenko child->ptrace = flags; 6361da177e4SLinus Torvalds 6378c5cf9e5SDenys Vlasenko return 0; 6381da177e4SLinus Torvalds } 6391da177e4SLinus Torvalds 640e16b2781SRoland McGrath static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info) 6411da177e4SLinus Torvalds { 642e4961254SOleg Nesterov unsigned long flags; 6431da177e4SLinus Torvalds int error = -ESRCH; 6441da177e4SLinus Torvalds 645e4961254SOleg Nesterov if (lock_task_sighand(child, &flags)) { 6461da177e4SLinus Torvalds error = -EINVAL; 6471da177e4SLinus Torvalds if (likely(child->last_siginfo != NULL)) { 648e16b2781SRoland McGrath *info = *child->last_siginfo; 6491da177e4SLinus Torvalds error = 0; 6501da177e4SLinus Torvalds } 651e4961254SOleg Nesterov unlock_task_sighand(child, &flags); 6521da177e4SLinus Torvalds } 6531da177e4SLinus Torvalds return error; 6541da177e4SLinus Torvalds } 6551da177e4SLinus Torvalds 656e16b2781SRoland McGrath static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info) 6571da177e4SLinus Torvalds { 658e4961254SOleg Nesterov unsigned long flags; 6591da177e4SLinus Torvalds int error = -ESRCH; 6601da177e4SLinus Torvalds 661e4961254SOleg Nesterov if (lock_task_sighand(child, &flags)) { 6621da177e4SLinus Torvalds error = -EINVAL; 6631da177e4SLinus Torvalds if (likely(child->last_siginfo != NULL)) { 664e16b2781SRoland McGrath *child->last_siginfo = *info; 6651da177e4SLinus Torvalds error = 0; 6661da177e4SLinus Torvalds } 667e4961254SOleg Nesterov unlock_task_sighand(child, &flags); 6681da177e4SLinus Torvalds } 6691da177e4SLinus Torvalds return error; 6701da177e4SLinus Torvalds } 6711da177e4SLinus Torvalds 67284c751bdSAndrey Vagin static int ptrace_peek_siginfo(struct task_struct *child, 67384c751bdSAndrey Vagin unsigned long addr, 67484c751bdSAndrey Vagin unsigned long data) 67584c751bdSAndrey Vagin { 67684c751bdSAndrey Vagin struct ptrace_peeksiginfo_args arg; 67784c751bdSAndrey Vagin struct sigpending *pending; 67884c751bdSAndrey Vagin struct sigqueue *q; 67984c751bdSAndrey Vagin int ret, i; 68084c751bdSAndrey Vagin 68184c751bdSAndrey Vagin ret = copy_from_user(&arg, (void __user *) addr, 68284c751bdSAndrey Vagin sizeof(struct ptrace_peeksiginfo_args)); 68384c751bdSAndrey Vagin if (ret) 68484c751bdSAndrey Vagin return -EFAULT; 68584c751bdSAndrey Vagin 68684c751bdSAndrey Vagin if (arg.flags & ~PTRACE_PEEKSIGINFO_SHARED) 68784c751bdSAndrey Vagin return -EINVAL; /* unknown flags */ 68884c751bdSAndrey Vagin 68984c751bdSAndrey Vagin if (arg.nr < 0) 69084c751bdSAndrey Vagin return -EINVAL; 69184c751bdSAndrey Vagin 69284c751bdSAndrey Vagin if (arg.flags & PTRACE_PEEKSIGINFO_SHARED) 69384c751bdSAndrey Vagin pending = &child->signal->shared_pending; 69484c751bdSAndrey Vagin else 69584c751bdSAndrey Vagin pending = &child->pending; 69684c751bdSAndrey Vagin 69784c751bdSAndrey Vagin for (i = 0; i < arg.nr; ) { 69884c751bdSAndrey Vagin siginfo_t info; 69984c751bdSAndrey Vagin s32 off = arg.off + i; 70084c751bdSAndrey Vagin 70184c751bdSAndrey Vagin spin_lock_irq(&child->sighand->siglock); 70284c751bdSAndrey Vagin list_for_each_entry(q, &pending->list, list) { 70384c751bdSAndrey Vagin if (!off--) { 70484c751bdSAndrey Vagin copy_siginfo(&info, &q->info); 70584c751bdSAndrey Vagin break; 70684c751bdSAndrey Vagin } 70784c751bdSAndrey Vagin } 70884c751bdSAndrey Vagin spin_unlock_irq(&child->sighand->siglock); 70984c751bdSAndrey Vagin 71084c751bdSAndrey Vagin if (off >= 0) /* beyond the end of the list */ 71184c751bdSAndrey Vagin break; 71284c751bdSAndrey Vagin 71384c751bdSAndrey Vagin #ifdef CONFIG_COMPAT 7145c465217SAndy Lutomirski if (unlikely(in_compat_syscall())) { 71584c751bdSAndrey Vagin compat_siginfo_t __user *uinfo = compat_ptr(data); 71684c751bdSAndrey Vagin 717706b23bdSMathieu Desnoyers if (copy_siginfo_to_user32(uinfo, &info) || 718706b23bdSMathieu Desnoyers __put_user(info.si_code, &uinfo->si_code)) { 719706b23bdSMathieu Desnoyers ret = -EFAULT; 720706b23bdSMathieu Desnoyers break; 721706b23bdSMathieu Desnoyers } 722706b23bdSMathieu Desnoyers 72384c751bdSAndrey Vagin } else 72484c751bdSAndrey Vagin #endif 72584c751bdSAndrey Vagin { 72684c751bdSAndrey Vagin siginfo_t __user *uinfo = (siginfo_t __user *) data; 72784c751bdSAndrey Vagin 728706b23bdSMathieu Desnoyers if (copy_siginfo_to_user(uinfo, &info) || 729706b23bdSMathieu Desnoyers __put_user(info.si_code, &uinfo->si_code)) { 73084c751bdSAndrey Vagin ret = -EFAULT; 73184c751bdSAndrey Vagin break; 73284c751bdSAndrey Vagin } 733706b23bdSMathieu Desnoyers } 73484c751bdSAndrey Vagin 73584c751bdSAndrey Vagin data += sizeof(siginfo_t); 73684c751bdSAndrey Vagin i++; 73784c751bdSAndrey Vagin 73884c751bdSAndrey Vagin if (signal_pending(current)) 73984c751bdSAndrey Vagin break; 74084c751bdSAndrey Vagin 74184c751bdSAndrey Vagin cond_resched(); 74284c751bdSAndrey Vagin } 74384c751bdSAndrey Vagin 74484c751bdSAndrey Vagin if (i > 0) 74584c751bdSAndrey Vagin return i; 74684c751bdSAndrey Vagin 74784c751bdSAndrey Vagin return ret; 74884c751bdSAndrey Vagin } 74936df29d7SRoland McGrath 75036df29d7SRoland McGrath #ifdef PTRACE_SINGLESTEP 75136df29d7SRoland McGrath #define is_singlestep(request) ((request) == PTRACE_SINGLESTEP) 75236df29d7SRoland McGrath #else 75336df29d7SRoland McGrath #define is_singlestep(request) 0 75436df29d7SRoland McGrath #endif 75536df29d7SRoland McGrath 7565b88abbfSRoland McGrath #ifdef PTRACE_SINGLEBLOCK 7575b88abbfSRoland McGrath #define is_singleblock(request) ((request) == PTRACE_SINGLEBLOCK) 7585b88abbfSRoland McGrath #else 7595b88abbfSRoland McGrath #define is_singleblock(request) 0 7605b88abbfSRoland McGrath #endif 7615b88abbfSRoland McGrath 76236df29d7SRoland McGrath #ifdef PTRACE_SYSEMU 76336df29d7SRoland McGrath #define is_sysemu_singlestep(request) ((request) == PTRACE_SYSEMU_SINGLESTEP) 76436df29d7SRoland McGrath #else 76536df29d7SRoland McGrath #define is_sysemu_singlestep(request) 0 76636df29d7SRoland McGrath #endif 76736df29d7SRoland McGrath 7684abf9869SNamhyung Kim static int ptrace_resume(struct task_struct *child, long request, 7694abf9869SNamhyung Kim unsigned long data) 77036df29d7SRoland McGrath { 771b72c1869SOleg Nesterov bool need_siglock; 772b72c1869SOleg Nesterov 77336df29d7SRoland McGrath if (!valid_signal(data)) 77436df29d7SRoland McGrath return -EIO; 77536df29d7SRoland McGrath 77636df29d7SRoland McGrath if (request == PTRACE_SYSCALL) 77736df29d7SRoland McGrath set_tsk_thread_flag(child, TIF_SYSCALL_TRACE); 77836df29d7SRoland McGrath else 77936df29d7SRoland McGrath clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); 78036df29d7SRoland McGrath 78136df29d7SRoland McGrath #ifdef TIF_SYSCALL_EMU 78236df29d7SRoland McGrath if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP) 78336df29d7SRoland McGrath set_tsk_thread_flag(child, TIF_SYSCALL_EMU); 78436df29d7SRoland McGrath else 78536df29d7SRoland McGrath clear_tsk_thread_flag(child, TIF_SYSCALL_EMU); 78636df29d7SRoland McGrath #endif 78736df29d7SRoland McGrath 7885b88abbfSRoland McGrath if (is_singleblock(request)) { 7895b88abbfSRoland McGrath if (unlikely(!arch_has_block_step())) 7905b88abbfSRoland McGrath return -EIO; 7915b88abbfSRoland McGrath user_enable_block_step(child); 7925b88abbfSRoland McGrath } else if (is_singlestep(request) || is_sysemu_singlestep(request)) { 79336df29d7SRoland McGrath if (unlikely(!arch_has_single_step())) 79436df29d7SRoland McGrath return -EIO; 79536df29d7SRoland McGrath user_enable_single_step(child); 7963a709703SRoland McGrath } else { 79736df29d7SRoland McGrath user_disable_single_step(child); 7983a709703SRoland McGrath } 79936df29d7SRoland McGrath 800b72c1869SOleg Nesterov /* 801b72c1869SOleg Nesterov * Change ->exit_code and ->state under siglock to avoid the race 802b72c1869SOleg Nesterov * with wait_task_stopped() in between; a non-zero ->exit_code will 803b72c1869SOleg Nesterov * wrongly look like another report from tracee. 804b72c1869SOleg Nesterov * 805b72c1869SOleg Nesterov * Note that we need siglock even if ->exit_code == data and/or this 806b72c1869SOleg Nesterov * status was not reported yet, the new status must not be cleared by 807b72c1869SOleg Nesterov * wait_task_stopped() after resume. 808b72c1869SOleg Nesterov * 809b72c1869SOleg Nesterov * If data == 0 we do not care if wait_task_stopped() reports the old 810b72c1869SOleg Nesterov * status and clears the code too; this can't race with the tracee, it 811b72c1869SOleg Nesterov * takes siglock after resume. 812b72c1869SOleg Nesterov */ 813b72c1869SOleg Nesterov need_siglock = data && !thread_group_empty(current); 814b72c1869SOleg Nesterov if (need_siglock) 815b72c1869SOleg Nesterov spin_lock_irq(&child->sighand->siglock); 81636df29d7SRoland McGrath child->exit_code = data; 8170666fb51SOleg Nesterov wake_up_state(child, __TASK_TRACED); 818b72c1869SOleg Nesterov if (need_siglock) 819b72c1869SOleg Nesterov spin_unlock_irq(&child->sighand->siglock); 82036df29d7SRoland McGrath 82136df29d7SRoland McGrath return 0; 82236df29d7SRoland McGrath } 82336df29d7SRoland McGrath 8242225a122SSuresh Siddha #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 8252225a122SSuresh Siddha 8262225a122SSuresh Siddha static const struct user_regset * 8272225a122SSuresh Siddha find_regset(const struct user_regset_view *view, unsigned int type) 8282225a122SSuresh Siddha { 8292225a122SSuresh Siddha const struct user_regset *regset; 8302225a122SSuresh Siddha int n; 8312225a122SSuresh Siddha 8322225a122SSuresh Siddha for (n = 0; n < view->n; ++n) { 8332225a122SSuresh Siddha regset = view->regsets + n; 8342225a122SSuresh Siddha if (regset->core_note_type == type) 8352225a122SSuresh Siddha return regset; 8362225a122SSuresh Siddha } 8372225a122SSuresh Siddha 8382225a122SSuresh Siddha return NULL; 8392225a122SSuresh Siddha } 8402225a122SSuresh Siddha 8412225a122SSuresh Siddha static int ptrace_regset(struct task_struct *task, int req, unsigned int type, 8422225a122SSuresh Siddha struct iovec *kiov) 8432225a122SSuresh Siddha { 8442225a122SSuresh Siddha const struct user_regset_view *view = task_user_regset_view(task); 8452225a122SSuresh Siddha const struct user_regset *regset = find_regset(view, type); 8462225a122SSuresh Siddha int regset_no; 8472225a122SSuresh Siddha 8482225a122SSuresh Siddha if (!regset || (kiov->iov_len % regset->size) != 0) 849c6a0dd7eSSuresh Siddha return -EINVAL; 8502225a122SSuresh Siddha 8512225a122SSuresh Siddha regset_no = regset - view->regsets; 8522225a122SSuresh Siddha kiov->iov_len = min(kiov->iov_len, 8532225a122SSuresh Siddha (__kernel_size_t) (regset->n * regset->size)); 8542225a122SSuresh Siddha 8552225a122SSuresh Siddha if (req == PTRACE_GETREGSET) 8562225a122SSuresh Siddha return copy_regset_to_user(task, view, regset_no, 0, 8572225a122SSuresh Siddha kiov->iov_len, kiov->iov_base); 8582225a122SSuresh Siddha else 8592225a122SSuresh Siddha return copy_regset_from_user(task, view, regset_no, 0, 8602225a122SSuresh Siddha kiov->iov_len, kiov->iov_base); 8612225a122SSuresh Siddha } 8622225a122SSuresh Siddha 863e8440c14SJosh Stone /* 864e8440c14SJosh Stone * This is declared in linux/regset.h and defined in machine-dependent 865e8440c14SJosh Stone * code. We put the export here, near the primary machine-neutral use, 866e8440c14SJosh Stone * to ensure no machine forgets it. 867e8440c14SJosh Stone */ 868e8440c14SJosh Stone EXPORT_SYMBOL_GPL(task_user_regset_view); 8692225a122SSuresh Siddha #endif 8702225a122SSuresh Siddha 8711da177e4SLinus Torvalds int ptrace_request(struct task_struct *child, long request, 8724abf9869SNamhyung Kim unsigned long addr, unsigned long data) 8731da177e4SLinus Torvalds { 874fca26f26STejun Heo bool seized = child->ptrace & PT_SEIZED; 8751da177e4SLinus Torvalds int ret = -EIO; 876544b2c91STejun Heo siginfo_t siginfo, *si; 8779fed81dcSNamhyung Kim void __user *datavp = (void __user *) data; 8789fed81dcSNamhyung Kim unsigned long __user *datalp = datavp; 879fca26f26STejun Heo unsigned long flags; 8801da177e4SLinus Torvalds 8811da177e4SLinus Torvalds switch (request) { 88216c3e389SRoland McGrath case PTRACE_PEEKTEXT: 88316c3e389SRoland McGrath case PTRACE_PEEKDATA: 88416c3e389SRoland McGrath return generic_ptrace_peekdata(child, addr, data); 88516c3e389SRoland McGrath case PTRACE_POKETEXT: 88616c3e389SRoland McGrath case PTRACE_POKEDATA: 88716c3e389SRoland McGrath return generic_ptrace_pokedata(child, addr, data); 88816c3e389SRoland McGrath 8891da177e4SLinus Torvalds #ifdef PTRACE_OLDSETOPTIONS 8901da177e4SLinus Torvalds case PTRACE_OLDSETOPTIONS: 8911da177e4SLinus Torvalds #endif 8921da177e4SLinus Torvalds case PTRACE_SETOPTIONS: 8931da177e4SLinus Torvalds ret = ptrace_setoptions(child, data); 8941da177e4SLinus Torvalds break; 8951da177e4SLinus Torvalds case PTRACE_GETEVENTMSG: 8969fed81dcSNamhyung Kim ret = put_user(child->ptrace_message, datalp); 8971da177e4SLinus Torvalds break; 898e16b2781SRoland McGrath 89984c751bdSAndrey Vagin case PTRACE_PEEKSIGINFO: 90084c751bdSAndrey Vagin ret = ptrace_peek_siginfo(child, addr, data); 90184c751bdSAndrey Vagin break; 90284c751bdSAndrey Vagin 9031da177e4SLinus Torvalds case PTRACE_GETSIGINFO: 904e16b2781SRoland McGrath ret = ptrace_getsiginfo(child, &siginfo); 905e16b2781SRoland McGrath if (!ret) 9069fed81dcSNamhyung Kim ret = copy_siginfo_to_user(datavp, &siginfo); 9071da177e4SLinus Torvalds break; 908e16b2781SRoland McGrath 9091da177e4SLinus Torvalds case PTRACE_SETSIGINFO: 9109fed81dcSNamhyung Kim if (copy_from_user(&siginfo, datavp, sizeof siginfo)) 911e16b2781SRoland McGrath ret = -EFAULT; 912e16b2781SRoland McGrath else 913e16b2781SRoland McGrath ret = ptrace_setsiginfo(child, &siginfo); 9141da177e4SLinus Torvalds break; 915e16b2781SRoland McGrath 91629000caeSAndrey Vagin case PTRACE_GETSIGMASK: 91729000caeSAndrey Vagin if (addr != sizeof(sigset_t)) { 91829000caeSAndrey Vagin ret = -EINVAL; 91929000caeSAndrey Vagin break; 92029000caeSAndrey Vagin } 92129000caeSAndrey Vagin 92229000caeSAndrey Vagin if (copy_to_user(datavp, &child->blocked, sizeof(sigset_t))) 92329000caeSAndrey Vagin ret = -EFAULT; 92429000caeSAndrey Vagin else 92529000caeSAndrey Vagin ret = 0; 92629000caeSAndrey Vagin 92729000caeSAndrey Vagin break; 92829000caeSAndrey Vagin 92929000caeSAndrey Vagin case PTRACE_SETSIGMASK: { 93029000caeSAndrey Vagin sigset_t new_set; 93129000caeSAndrey Vagin 93229000caeSAndrey Vagin if (addr != sizeof(sigset_t)) { 93329000caeSAndrey Vagin ret = -EINVAL; 93429000caeSAndrey Vagin break; 93529000caeSAndrey Vagin } 93629000caeSAndrey Vagin 93729000caeSAndrey Vagin if (copy_from_user(&new_set, datavp, sizeof(sigset_t))) { 93829000caeSAndrey Vagin ret = -EFAULT; 93929000caeSAndrey Vagin break; 94029000caeSAndrey Vagin } 94129000caeSAndrey Vagin 94229000caeSAndrey Vagin sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP)); 94329000caeSAndrey Vagin 94429000caeSAndrey Vagin /* 94529000caeSAndrey Vagin * Every thread does recalc_sigpending() after resume, so 94629000caeSAndrey Vagin * retarget_shared_pending() and recalc_sigpending() are not 94729000caeSAndrey Vagin * called here. 94829000caeSAndrey Vagin */ 94929000caeSAndrey Vagin spin_lock_irq(&child->sighand->siglock); 95029000caeSAndrey Vagin child->blocked = new_set; 95129000caeSAndrey Vagin spin_unlock_irq(&child->sighand->siglock); 95229000caeSAndrey Vagin 95329000caeSAndrey Vagin ret = 0; 95429000caeSAndrey Vagin break; 95529000caeSAndrey Vagin } 95629000caeSAndrey Vagin 957fca26f26STejun Heo case PTRACE_INTERRUPT: 958fca26f26STejun Heo /* 959fca26f26STejun Heo * Stop tracee without any side-effect on signal or job 960fca26f26STejun Heo * control. At least one trap is guaranteed to happen 961fca26f26STejun Heo * after this request. If @child is already trapped, the 962fca26f26STejun Heo * current trap is not disturbed and another trap will 963fca26f26STejun Heo * happen after the current trap is ended with PTRACE_CONT. 964fca26f26STejun Heo * 965fca26f26STejun Heo * The actual trap might not be PTRACE_EVENT_STOP trap but 966fca26f26STejun Heo * the pending condition is cleared regardless. 967fca26f26STejun Heo */ 968fca26f26STejun Heo if (unlikely(!seized || !lock_task_sighand(child, &flags))) 969fca26f26STejun Heo break; 970fca26f26STejun Heo 971544b2c91STejun Heo /* 972544b2c91STejun Heo * INTERRUPT doesn't disturb existing trap sans one 973544b2c91STejun Heo * exception. If ptracer issued LISTEN for the current 974544b2c91STejun Heo * STOP, this INTERRUPT should clear LISTEN and re-trap 975544b2c91STejun Heo * tracee into STOP. 976544b2c91STejun Heo */ 977fca26f26STejun Heo if (likely(task_set_jobctl_pending(child, JOBCTL_TRAP_STOP))) 978910ffdb1SOleg Nesterov ptrace_signal_wake_up(child, child->jobctl & JOBCTL_LISTENING); 979544b2c91STejun Heo 980544b2c91STejun Heo unlock_task_sighand(child, &flags); 981544b2c91STejun Heo ret = 0; 982544b2c91STejun Heo break; 983544b2c91STejun Heo 984544b2c91STejun Heo case PTRACE_LISTEN: 985544b2c91STejun Heo /* 986544b2c91STejun Heo * Listen for events. Tracee must be in STOP. It's not 987544b2c91STejun Heo * resumed per-se but is not considered to be in TRACED by 988544b2c91STejun Heo * wait(2) or ptrace(2). If an async event (e.g. group 989544b2c91STejun Heo * stop state change) happens, tracee will enter STOP trap 990544b2c91STejun Heo * again. Alternatively, ptracer can issue INTERRUPT to 991544b2c91STejun Heo * finish listening and re-trap tracee into STOP. 992544b2c91STejun Heo */ 993544b2c91STejun Heo if (unlikely(!seized || !lock_task_sighand(child, &flags))) 994544b2c91STejun Heo break; 995544b2c91STejun Heo 996544b2c91STejun Heo si = child->last_siginfo; 997f9d81f61SOleg Nesterov if (likely(si && (si->si_code >> 8) == PTRACE_EVENT_STOP)) { 998544b2c91STejun Heo child->jobctl |= JOBCTL_LISTENING; 999544b2c91STejun Heo /* 1000f9d81f61SOleg Nesterov * If NOTIFY is set, it means event happened between 1001f9d81f61SOleg Nesterov * start of this trap and now. Trigger re-trap. 1002544b2c91STejun Heo */ 1003544b2c91STejun Heo if (child->jobctl & JOBCTL_TRAP_NOTIFY) 1004910ffdb1SOleg Nesterov ptrace_signal_wake_up(child, true); 1005fca26f26STejun Heo ret = 0; 1006f9d81f61SOleg Nesterov } 1007f9d81f61SOleg Nesterov unlock_task_sighand(child, &flags); 1008fca26f26STejun Heo break; 1009fca26f26STejun Heo 10101bcf5482SAlexey Dobriyan case PTRACE_DETACH: /* detach a process that was attached. */ 10111bcf5482SAlexey Dobriyan ret = ptrace_detach(child, data); 10121bcf5482SAlexey Dobriyan break; 101336df29d7SRoland McGrath 10149c1a1259SMike Frysinger #ifdef CONFIG_BINFMT_ELF_FDPIC 10159c1a1259SMike Frysinger case PTRACE_GETFDPIC: { 1016e0129ef9SOleg Nesterov struct mm_struct *mm = get_task_mm(child); 10179c1a1259SMike Frysinger unsigned long tmp = 0; 10189c1a1259SMike Frysinger 1019e0129ef9SOleg Nesterov ret = -ESRCH; 1020e0129ef9SOleg Nesterov if (!mm) 1021e0129ef9SOleg Nesterov break; 1022e0129ef9SOleg Nesterov 10239c1a1259SMike Frysinger switch (addr) { 10249c1a1259SMike Frysinger case PTRACE_GETFDPIC_EXEC: 1025e0129ef9SOleg Nesterov tmp = mm->context.exec_fdpic_loadmap; 10269c1a1259SMike Frysinger break; 10279c1a1259SMike Frysinger case PTRACE_GETFDPIC_INTERP: 1028e0129ef9SOleg Nesterov tmp = mm->context.interp_fdpic_loadmap; 10299c1a1259SMike Frysinger break; 10309c1a1259SMike Frysinger default: 10319c1a1259SMike Frysinger break; 10329c1a1259SMike Frysinger } 1033e0129ef9SOleg Nesterov mmput(mm); 10349c1a1259SMike Frysinger 10359fed81dcSNamhyung Kim ret = put_user(tmp, datalp); 10369c1a1259SMike Frysinger break; 10379c1a1259SMike Frysinger } 10389c1a1259SMike Frysinger #endif 10399c1a1259SMike Frysinger 104036df29d7SRoland McGrath #ifdef PTRACE_SINGLESTEP 104136df29d7SRoland McGrath case PTRACE_SINGLESTEP: 104236df29d7SRoland McGrath #endif 10435b88abbfSRoland McGrath #ifdef PTRACE_SINGLEBLOCK 10445b88abbfSRoland McGrath case PTRACE_SINGLEBLOCK: 10455b88abbfSRoland McGrath #endif 104636df29d7SRoland McGrath #ifdef PTRACE_SYSEMU 104736df29d7SRoland McGrath case PTRACE_SYSEMU: 104836df29d7SRoland McGrath case PTRACE_SYSEMU_SINGLESTEP: 104936df29d7SRoland McGrath #endif 105036df29d7SRoland McGrath case PTRACE_SYSCALL: 105136df29d7SRoland McGrath case PTRACE_CONT: 105236df29d7SRoland McGrath return ptrace_resume(child, request, data); 105336df29d7SRoland McGrath 105436df29d7SRoland McGrath case PTRACE_KILL: 105536df29d7SRoland McGrath if (child->exit_state) /* already dead */ 105636df29d7SRoland McGrath return 0; 105736df29d7SRoland McGrath return ptrace_resume(child, request, SIGKILL); 105836df29d7SRoland McGrath 10592225a122SSuresh Siddha #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 10602225a122SSuresh Siddha case PTRACE_GETREGSET: 106129000caeSAndrey Vagin case PTRACE_SETREGSET: { 10622225a122SSuresh Siddha struct iovec kiov; 10639fed81dcSNamhyung Kim struct iovec __user *uiov = datavp; 10642225a122SSuresh Siddha 10652225a122SSuresh Siddha if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov))) 10662225a122SSuresh Siddha return -EFAULT; 10672225a122SSuresh Siddha 10682225a122SSuresh Siddha if (__get_user(kiov.iov_base, &uiov->iov_base) || 10692225a122SSuresh Siddha __get_user(kiov.iov_len, &uiov->iov_len)) 10702225a122SSuresh Siddha return -EFAULT; 10712225a122SSuresh Siddha 10722225a122SSuresh Siddha ret = ptrace_regset(child, request, addr, &kiov); 10732225a122SSuresh Siddha if (!ret) 10742225a122SSuresh Siddha ret = __put_user(kiov.iov_len, &uiov->iov_len); 10752225a122SSuresh Siddha break; 10762225a122SSuresh Siddha } 10772225a122SSuresh Siddha #endif 1078f8e529edSTycho Andersen 1079f8e529edSTycho Andersen case PTRACE_SECCOMP_GET_FILTER: 1080f8e529edSTycho Andersen ret = seccomp_get_filter(child, addr, datavp); 1081f8e529edSTycho Andersen break; 1082f8e529edSTycho Andersen 10831da177e4SLinus Torvalds default: 10841da177e4SLinus Torvalds break; 10851da177e4SLinus Torvalds } 10861da177e4SLinus Torvalds 10871da177e4SLinus Torvalds return ret; 10881da177e4SLinus Torvalds } 1089481bed45SChristoph Hellwig 10908053bdd5SOleg Nesterov static struct task_struct *ptrace_get_task_struct(pid_t pid) 10916b9c7ed8SChristoph Hellwig { 10926b9c7ed8SChristoph Hellwig struct task_struct *child; 10936b9c7ed8SChristoph Hellwig 10948053bdd5SOleg Nesterov rcu_read_lock(); 1095228ebcbeSPavel Emelyanov child = find_task_by_vpid(pid); 1096481bed45SChristoph Hellwig if (child) 1097481bed45SChristoph Hellwig get_task_struct(child); 10988053bdd5SOleg Nesterov rcu_read_unlock(); 1099f400e198SSukadev Bhattiprolu 1100481bed45SChristoph Hellwig if (!child) 11016b9c7ed8SChristoph Hellwig return ERR_PTR(-ESRCH); 11026b9c7ed8SChristoph Hellwig return child; 1103481bed45SChristoph Hellwig } 1104481bed45SChristoph Hellwig 11050ac15559SChristoph Hellwig #ifndef arch_ptrace_attach 11060ac15559SChristoph Hellwig #define arch_ptrace_attach(child) do { } while (0) 11070ac15559SChristoph Hellwig #endif 11080ac15559SChristoph Hellwig 11094abf9869SNamhyung Kim SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr, 11104abf9869SNamhyung Kim unsigned long, data) 1111481bed45SChristoph Hellwig { 1112481bed45SChristoph Hellwig struct task_struct *child; 1113481bed45SChristoph Hellwig long ret; 1114481bed45SChristoph Hellwig 11156b9c7ed8SChristoph Hellwig if (request == PTRACE_TRACEME) { 11166b9c7ed8SChristoph Hellwig ret = ptrace_traceme(); 11176ea6dd93SHaavard Skinnemoen if (!ret) 11186ea6dd93SHaavard Skinnemoen arch_ptrace_attach(current); 1119481bed45SChristoph Hellwig goto out; 11206b9c7ed8SChristoph Hellwig } 11216b9c7ed8SChristoph Hellwig 11226b9c7ed8SChristoph Hellwig child = ptrace_get_task_struct(pid); 11236b9c7ed8SChristoph Hellwig if (IS_ERR(child)) { 11246b9c7ed8SChristoph Hellwig ret = PTR_ERR(child); 11256b9c7ed8SChristoph Hellwig goto out; 11266b9c7ed8SChristoph Hellwig } 1127481bed45SChristoph Hellwig 11283544d72aSTejun Heo if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) { 1129aa9147c9SDenys Vlasenko ret = ptrace_attach(child, request, addr, data); 11300ac15559SChristoph Hellwig /* 11310ac15559SChristoph Hellwig * Some architectures need to do book-keeping after 11320ac15559SChristoph Hellwig * a ptrace attach. 11330ac15559SChristoph Hellwig */ 11340ac15559SChristoph Hellwig if (!ret) 11350ac15559SChristoph Hellwig arch_ptrace_attach(child); 1136005f18dfSChristoph Hellwig goto out_put_task_struct; 1137481bed45SChristoph Hellwig } 1138481bed45SChristoph Hellwig 1139fca26f26STejun Heo ret = ptrace_check_attach(child, request == PTRACE_KILL || 1140fca26f26STejun Heo request == PTRACE_INTERRUPT); 1141481bed45SChristoph Hellwig if (ret < 0) 1142481bed45SChristoph Hellwig goto out_put_task_struct; 1143481bed45SChristoph Hellwig 1144481bed45SChristoph Hellwig ret = arch_ptrace(child, request, addr, data); 11459899d11fSOleg Nesterov if (ret || request != PTRACE_DETACH) 11469899d11fSOleg Nesterov ptrace_unfreeze_traced(child); 1147481bed45SChristoph Hellwig 1148481bed45SChristoph Hellwig out_put_task_struct: 1149481bed45SChristoph Hellwig put_task_struct(child); 1150481bed45SChristoph Hellwig out: 1151481bed45SChristoph Hellwig return ret; 1152481bed45SChristoph Hellwig } 115376647323SAlexey Dobriyan 11544abf9869SNamhyung Kim int generic_ptrace_peekdata(struct task_struct *tsk, unsigned long addr, 11554abf9869SNamhyung Kim unsigned long data) 115676647323SAlexey Dobriyan { 115776647323SAlexey Dobriyan unsigned long tmp; 115876647323SAlexey Dobriyan int copied; 115976647323SAlexey Dobriyan 116084d77d3fSEric W. Biederman copied = ptrace_access_vm(tsk, addr, &tmp, sizeof(tmp), FOLL_FORCE); 116176647323SAlexey Dobriyan if (copied != sizeof(tmp)) 116276647323SAlexey Dobriyan return -EIO; 116376647323SAlexey Dobriyan return put_user(tmp, (unsigned long __user *)data); 116476647323SAlexey Dobriyan } 1165f284ce72SAlexey Dobriyan 11664abf9869SNamhyung Kim int generic_ptrace_pokedata(struct task_struct *tsk, unsigned long addr, 11674abf9869SNamhyung Kim unsigned long data) 1168f284ce72SAlexey Dobriyan { 1169f284ce72SAlexey Dobriyan int copied; 1170f284ce72SAlexey Dobriyan 117184d77d3fSEric W. Biederman copied = ptrace_access_vm(tsk, addr, &data, sizeof(data), 1172f307ab6dSLorenzo Stoakes FOLL_FORCE | FOLL_WRITE); 1173f284ce72SAlexey Dobriyan return (copied == sizeof(data)) ? 0 : -EIO; 1174f284ce72SAlexey Dobriyan } 1175032d82d9SRoland McGrath 117696b8936aSChristoph Hellwig #if defined CONFIG_COMPAT 1177032d82d9SRoland McGrath 1178032d82d9SRoland McGrath int compat_ptrace_request(struct task_struct *child, compat_long_t request, 1179032d82d9SRoland McGrath compat_ulong_t addr, compat_ulong_t data) 1180032d82d9SRoland McGrath { 1181032d82d9SRoland McGrath compat_ulong_t __user *datap = compat_ptr(data); 1182032d82d9SRoland McGrath compat_ulong_t word; 1183e16b2781SRoland McGrath siginfo_t siginfo; 1184032d82d9SRoland McGrath int ret; 1185032d82d9SRoland McGrath 1186032d82d9SRoland McGrath switch (request) { 1187032d82d9SRoland McGrath case PTRACE_PEEKTEXT: 1188032d82d9SRoland McGrath case PTRACE_PEEKDATA: 118984d77d3fSEric W. Biederman ret = ptrace_access_vm(child, addr, &word, sizeof(word), 1190f307ab6dSLorenzo Stoakes FOLL_FORCE); 1191032d82d9SRoland McGrath if (ret != sizeof(word)) 1192032d82d9SRoland McGrath ret = -EIO; 1193032d82d9SRoland McGrath else 1194032d82d9SRoland McGrath ret = put_user(word, datap); 1195032d82d9SRoland McGrath break; 1196032d82d9SRoland McGrath 1197032d82d9SRoland McGrath case PTRACE_POKETEXT: 1198032d82d9SRoland McGrath case PTRACE_POKEDATA: 119984d77d3fSEric W. Biederman ret = ptrace_access_vm(child, addr, &data, sizeof(data), 1200f307ab6dSLorenzo Stoakes FOLL_FORCE | FOLL_WRITE); 1201032d82d9SRoland McGrath ret = (ret != sizeof(data) ? -EIO : 0); 1202032d82d9SRoland McGrath break; 1203032d82d9SRoland McGrath 1204032d82d9SRoland McGrath case PTRACE_GETEVENTMSG: 1205032d82d9SRoland McGrath ret = put_user((compat_ulong_t) child->ptrace_message, datap); 1206032d82d9SRoland McGrath break; 1207032d82d9SRoland McGrath 1208e16b2781SRoland McGrath case PTRACE_GETSIGINFO: 1209e16b2781SRoland McGrath ret = ptrace_getsiginfo(child, &siginfo); 1210e16b2781SRoland McGrath if (!ret) 1211e16b2781SRoland McGrath ret = copy_siginfo_to_user32( 1212e16b2781SRoland McGrath (struct compat_siginfo __user *) datap, 1213e16b2781SRoland McGrath &siginfo); 1214e16b2781SRoland McGrath break; 1215e16b2781SRoland McGrath 1216e16b2781SRoland McGrath case PTRACE_SETSIGINFO: 1217e16b2781SRoland McGrath memset(&siginfo, 0, sizeof siginfo); 1218e16b2781SRoland McGrath if (copy_siginfo_from_user32( 1219e16b2781SRoland McGrath &siginfo, (struct compat_siginfo __user *) datap)) 1220e16b2781SRoland McGrath ret = -EFAULT; 1221e16b2781SRoland McGrath else 1222e16b2781SRoland McGrath ret = ptrace_setsiginfo(child, &siginfo); 1223e16b2781SRoland McGrath break; 12242225a122SSuresh Siddha #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 12252225a122SSuresh Siddha case PTRACE_GETREGSET: 12262225a122SSuresh Siddha case PTRACE_SETREGSET: 12272225a122SSuresh Siddha { 12282225a122SSuresh Siddha struct iovec kiov; 12292225a122SSuresh Siddha struct compat_iovec __user *uiov = 12302225a122SSuresh Siddha (struct compat_iovec __user *) datap; 12312225a122SSuresh Siddha compat_uptr_t ptr; 12322225a122SSuresh Siddha compat_size_t len; 12332225a122SSuresh Siddha 12342225a122SSuresh Siddha if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov))) 12352225a122SSuresh Siddha return -EFAULT; 12362225a122SSuresh Siddha 12372225a122SSuresh Siddha if (__get_user(ptr, &uiov->iov_base) || 12382225a122SSuresh Siddha __get_user(len, &uiov->iov_len)) 12392225a122SSuresh Siddha return -EFAULT; 12402225a122SSuresh Siddha 12412225a122SSuresh Siddha kiov.iov_base = compat_ptr(ptr); 12422225a122SSuresh Siddha kiov.iov_len = len; 12432225a122SSuresh Siddha 12442225a122SSuresh Siddha ret = ptrace_regset(child, request, addr, &kiov); 12452225a122SSuresh Siddha if (!ret) 12462225a122SSuresh Siddha ret = __put_user(kiov.iov_len, &uiov->iov_len); 12472225a122SSuresh Siddha break; 12482225a122SSuresh Siddha } 12492225a122SSuresh Siddha #endif 1250e16b2781SRoland McGrath 1251032d82d9SRoland McGrath default: 1252032d82d9SRoland McGrath ret = ptrace_request(child, request, addr, data); 1253032d82d9SRoland McGrath } 1254032d82d9SRoland McGrath 1255032d82d9SRoland McGrath return ret; 1256032d82d9SRoland McGrath } 1257c269f196SRoland McGrath 125862a6fa97SHeiko Carstens COMPAT_SYSCALL_DEFINE4(ptrace, compat_long_t, request, compat_long_t, pid, 125962a6fa97SHeiko Carstens compat_long_t, addr, compat_long_t, data) 1260c269f196SRoland McGrath { 1261c269f196SRoland McGrath struct task_struct *child; 1262c269f196SRoland McGrath long ret; 1263c269f196SRoland McGrath 1264c269f196SRoland McGrath if (request == PTRACE_TRACEME) { 1265c269f196SRoland McGrath ret = ptrace_traceme(); 1266c269f196SRoland McGrath goto out; 1267c269f196SRoland McGrath } 1268c269f196SRoland McGrath 1269c269f196SRoland McGrath child = ptrace_get_task_struct(pid); 1270c269f196SRoland McGrath if (IS_ERR(child)) { 1271c269f196SRoland McGrath ret = PTR_ERR(child); 1272c269f196SRoland McGrath goto out; 1273c269f196SRoland McGrath } 1274c269f196SRoland McGrath 12753544d72aSTejun Heo if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) { 1276aa9147c9SDenys Vlasenko ret = ptrace_attach(child, request, addr, data); 1277c269f196SRoland McGrath /* 1278c269f196SRoland McGrath * Some architectures need to do book-keeping after 1279c269f196SRoland McGrath * a ptrace attach. 1280c269f196SRoland McGrath */ 1281c269f196SRoland McGrath if (!ret) 1282c269f196SRoland McGrath arch_ptrace_attach(child); 1283c269f196SRoland McGrath goto out_put_task_struct; 1284c269f196SRoland McGrath } 1285c269f196SRoland McGrath 1286fca26f26STejun Heo ret = ptrace_check_attach(child, request == PTRACE_KILL || 1287fca26f26STejun Heo request == PTRACE_INTERRUPT); 12889899d11fSOleg Nesterov if (!ret) { 1289c269f196SRoland McGrath ret = compat_arch_ptrace(child, request, addr, data); 12909899d11fSOleg Nesterov if (ret || request != PTRACE_DETACH) 12919899d11fSOleg Nesterov ptrace_unfreeze_traced(child); 12929899d11fSOleg Nesterov } 1293c269f196SRoland McGrath 1294c269f196SRoland McGrath out_put_task_struct: 1295c269f196SRoland McGrath put_task_struct(child); 1296c269f196SRoland McGrath out: 1297c269f196SRoland McGrath return ret; 1298c269f196SRoland McGrath } 129996b8936aSChristoph Hellwig #endif /* CONFIG_COMPAT */ 1300