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> 131da177e4SLinus Torvalds #include <linux/errno.h> 141da177e4SLinus Torvalds #include <linux/mm.h> 151da177e4SLinus Torvalds #include <linux/highmem.h> 161da177e4SLinus Torvalds #include <linux/pagemap.h> 171da177e4SLinus Torvalds #include <linux/ptrace.h> 181da177e4SLinus Torvalds #include <linux/security.h> 197ed20e1aSJesper Juhl #include <linux/signal.h> 20a27bb332SKent Overstreet #include <linux/uio.h> 21a5cb013dSAl Viro #include <linux/audit.h> 22b488893aSPavel Emelyanov #include <linux/pid_namespace.h> 23f17d30a8SAdrian Bunk #include <linux/syscalls.h> 243a709703SRoland McGrath #include <linux/uaccess.h> 252225a122SSuresh Siddha #include <linux/regset.h> 26bf26c018SFrederic Weisbecker #include <linux/hw_breakpoint.h> 27f701e5b7SVladimir Zapolskiy #include <linux/cn_proc.h> 2884c751bdSAndrey Vagin #include <linux/compat.h> 291da177e4SLinus Torvalds 30bf53de90SMarkus Metzger 3162c124ffSTejun Heo static int ptrace_trapping_sleep_fn(void *flags) 3262c124ffSTejun Heo { 3362c124ffSTejun Heo schedule(); 3462c124ffSTejun Heo return 0; 3562c124ffSTejun Heo } 3662c124ffSTejun Heo 37bf53de90SMarkus Metzger /* 381da177e4SLinus Torvalds * ptrace a task: make the debugger its new parent and 391da177e4SLinus Torvalds * move it to the ptrace list. 401da177e4SLinus Torvalds * 411da177e4SLinus Torvalds * Must be called with the tasklist lock write-held. 421da177e4SLinus Torvalds */ 4336c8b586SIngo Molnar void __ptrace_link(struct task_struct *child, struct task_struct *new_parent) 441da177e4SLinus Torvalds { 45f470021aSRoland McGrath BUG_ON(!list_empty(&child->ptrace_entry)); 46f470021aSRoland McGrath list_add(&child->ptrace_entry, &new_parent->ptraced); 471da177e4SLinus Torvalds child->parent = new_parent; 481da177e4SLinus Torvalds } 491da177e4SLinus Torvalds 50e3bd058fSTejun Heo /** 51e3bd058fSTejun Heo * __ptrace_unlink - unlink ptracee and restore its execution state 52e3bd058fSTejun Heo * @child: ptracee to be unlinked 531da177e4SLinus Torvalds * 540e9f0a4aSTejun Heo * Remove @child from the ptrace list, move it back to the original parent, 550e9f0a4aSTejun Heo * and restore the execution state so that it conforms to the group stop 560e9f0a4aSTejun Heo * state. 570e9f0a4aSTejun Heo * 580e9f0a4aSTejun Heo * Unlinking can happen via two paths - explicit PTRACE_DETACH or ptracer 590e9f0a4aSTejun Heo * exiting. For PTRACE_DETACH, unless the ptracee has been killed between 600e9f0a4aSTejun Heo * ptrace_check_attach() and here, it's guaranteed to be in TASK_TRACED. 610e9f0a4aSTejun Heo * If the ptracer is exiting, the ptracee can be in any state. 620e9f0a4aSTejun Heo * 630e9f0a4aSTejun Heo * After detach, the ptracee should be in a state which conforms to the 640e9f0a4aSTejun Heo * group stop. If the group is stopped or in the process of stopping, the 650e9f0a4aSTejun Heo * ptracee should be put into TASK_STOPPED; otherwise, it should be woken 660e9f0a4aSTejun Heo * up from TASK_TRACED. 670e9f0a4aSTejun Heo * 680e9f0a4aSTejun Heo * If the ptracee is in TASK_TRACED and needs to be moved to TASK_STOPPED, 690e9f0a4aSTejun Heo * it goes through TRACED -> RUNNING -> STOPPED transition which is similar 700e9f0a4aSTejun Heo * to but in the opposite direction of what happens while attaching to a 710e9f0a4aSTejun Heo * stopped task. However, in this direction, the intermediate RUNNING 720e9f0a4aSTejun Heo * state is not hidden even from the current ptracer and if it immediately 730e9f0a4aSTejun Heo * re-attaches and performs a WNOHANG wait(2), it may fail. 74e3bd058fSTejun Heo * 75e3bd058fSTejun Heo * CONTEXT: 76e3bd058fSTejun Heo * write_lock_irq(tasklist_lock) 771da177e4SLinus Torvalds */ 7836c8b586SIngo Molnar void __ptrace_unlink(struct task_struct *child) 791da177e4SLinus Torvalds { 805ecfbae0SOleg Nesterov BUG_ON(!child->ptrace); 815ecfbae0SOleg Nesterov 821da177e4SLinus Torvalds child->ptrace = 0; 831da177e4SLinus Torvalds child->parent = child->real_parent; 84f470021aSRoland McGrath list_del_init(&child->ptrace_entry); 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds spin_lock(&child->sighand->siglock); 870e9f0a4aSTejun Heo 881da177e4SLinus Torvalds /* 8973ddff2bSTejun Heo * Clear all pending traps and TRAPPING. TRAPPING should be 9073ddff2bSTejun Heo * cleared regardless of JOBCTL_STOP_PENDING. Do it explicitly. 9173ddff2bSTejun Heo */ 9273ddff2bSTejun Heo task_clear_jobctl_pending(child, JOBCTL_TRAP_MASK); 9373ddff2bSTejun Heo task_clear_jobctl_trapping(child); 9473ddff2bSTejun Heo 9573ddff2bSTejun Heo /* 96a8f072c1STejun Heo * Reinstate JOBCTL_STOP_PENDING if group stop is in effect and 970e9f0a4aSTejun Heo * @child isn't dead. 981da177e4SLinus Torvalds */ 990e9f0a4aSTejun Heo if (!(child->flags & PF_EXITING) && 1000e9f0a4aSTejun Heo (child->signal->flags & SIGNAL_STOP_STOPPED || 1018a88951bSOleg Nesterov child->signal->group_stop_count)) { 102a8f072c1STejun Heo child->jobctl |= JOBCTL_STOP_PENDING; 1030e9f0a4aSTejun Heo 1040e9f0a4aSTejun Heo /* 1058a88951bSOleg Nesterov * This is only possible if this thread was cloned by the 1068a88951bSOleg Nesterov * traced task running in the stopped group, set the signal 1078a88951bSOleg Nesterov * for the future reports. 1088a88951bSOleg Nesterov * FIXME: we should change ptrace_init_task() to handle this 1098a88951bSOleg Nesterov * case. 1108a88951bSOleg Nesterov */ 1118a88951bSOleg Nesterov if (!(child->jobctl & JOBCTL_STOP_SIGMASK)) 1128a88951bSOleg Nesterov child->jobctl |= SIGSTOP; 1138a88951bSOleg Nesterov } 1148a88951bSOleg Nesterov 1158a88951bSOleg Nesterov /* 1160e9f0a4aSTejun Heo * If transition to TASK_STOPPED is pending or in TASK_TRACED, kick 1170e9f0a4aSTejun Heo * @child in the butt. Note that @resume should be used iff @child 1180e9f0a4aSTejun Heo * is in TASK_TRACED; otherwise, we might unduly disrupt 1190e9f0a4aSTejun Heo * TASK_KILLABLE sleeps. 1200e9f0a4aSTejun Heo */ 121a8f072c1STejun Heo if (child->jobctl & JOBCTL_STOP_PENDING || task_is_traced(child)) 122910ffdb1SOleg Nesterov ptrace_signal_wake_up(child, true); 1230e9f0a4aSTejun Heo 1241da177e4SLinus Torvalds spin_unlock(&child->sighand->siglock); 1251da177e4SLinus Torvalds } 1261da177e4SLinus Torvalds 1279899d11fSOleg Nesterov /* Ensure that nothing can wake it up, even SIGKILL */ 1289899d11fSOleg Nesterov static bool ptrace_freeze_traced(struct task_struct *task) 1299899d11fSOleg Nesterov { 1309899d11fSOleg Nesterov bool ret = false; 1319899d11fSOleg Nesterov 1329899d11fSOleg Nesterov /* Lockless, nobody but us can set this flag */ 1339899d11fSOleg Nesterov if (task->jobctl & JOBCTL_LISTENING) 1349899d11fSOleg Nesterov return ret; 1359899d11fSOleg Nesterov 1369899d11fSOleg Nesterov spin_lock_irq(&task->sighand->siglock); 1379899d11fSOleg Nesterov if (task_is_traced(task) && !__fatal_signal_pending(task)) { 1389899d11fSOleg Nesterov task->state = __TASK_TRACED; 1399899d11fSOleg Nesterov ret = true; 1409899d11fSOleg Nesterov } 1419899d11fSOleg Nesterov spin_unlock_irq(&task->sighand->siglock); 1429899d11fSOleg Nesterov 1439899d11fSOleg Nesterov return ret; 1449899d11fSOleg Nesterov } 1459899d11fSOleg Nesterov 1469899d11fSOleg Nesterov static void ptrace_unfreeze_traced(struct task_struct *task) 1479899d11fSOleg Nesterov { 1489899d11fSOleg Nesterov if (task->state != __TASK_TRACED) 1499899d11fSOleg Nesterov return; 1509899d11fSOleg Nesterov 1519899d11fSOleg Nesterov WARN_ON(!task->ptrace || task->parent != current); 1529899d11fSOleg Nesterov 1539899d11fSOleg Nesterov spin_lock_irq(&task->sighand->siglock); 1549899d11fSOleg Nesterov if (__fatal_signal_pending(task)) 1559899d11fSOleg Nesterov wake_up_state(task, __TASK_TRACED); 1569899d11fSOleg Nesterov else 1579899d11fSOleg Nesterov task->state = TASK_TRACED; 1589899d11fSOleg Nesterov spin_unlock_irq(&task->sighand->siglock); 1599899d11fSOleg Nesterov } 1609899d11fSOleg Nesterov 161755e276bSTejun Heo /** 162755e276bSTejun Heo * ptrace_check_attach - check whether ptracee is ready for ptrace operation 163755e276bSTejun Heo * @child: ptracee to check for 164755e276bSTejun Heo * @ignore_state: don't check whether @child is currently %TASK_TRACED 165755e276bSTejun Heo * 166755e276bSTejun Heo * Check whether @child is being ptraced by %current and ready for further 167755e276bSTejun Heo * ptrace operations. If @ignore_state is %false, @child also should be in 168755e276bSTejun Heo * %TASK_TRACED state and on return the child is guaranteed to be traced 169755e276bSTejun Heo * and not executing. If @ignore_state is %true, @child can be in any 170755e276bSTejun Heo * state. 171755e276bSTejun Heo * 172755e276bSTejun Heo * CONTEXT: 173755e276bSTejun Heo * Grabs and releases tasklist_lock and @child->sighand->siglock. 174755e276bSTejun Heo * 175755e276bSTejun Heo * RETURNS: 176755e276bSTejun Heo * 0 on success, -ESRCH if %child is not ready. 1771da177e4SLinus Torvalds */ 178edea0d03SOleg Nesterov static int ptrace_check_attach(struct task_struct *child, bool ignore_state) 1791da177e4SLinus Torvalds { 1801da177e4SLinus Torvalds int ret = -ESRCH; 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds /* 1831da177e4SLinus Torvalds * We take the read lock around doing both checks to close a 1841da177e4SLinus Torvalds * possible race where someone else was tracing our child and 1851da177e4SLinus Torvalds * detached between these two checks. After this locked check, 1861da177e4SLinus Torvalds * we are sure that this is our traced child and that can only 1871da177e4SLinus Torvalds * be changed by us so it's not changing right after this. 1881da177e4SLinus Torvalds */ 1891da177e4SLinus Torvalds read_lock(&tasklist_lock); 1909899d11fSOleg Nesterov if (child->ptrace && child->parent == current) { 1919899d11fSOleg Nesterov WARN_ON(child->state == __TASK_TRACED); 192c0c0b649SOleg Nesterov /* 193c0c0b649SOleg Nesterov * child->sighand can't be NULL, release_task() 194c0c0b649SOleg Nesterov * does ptrace_unlink() before __exit_signal(). 195c0c0b649SOleg Nesterov */ 1969899d11fSOleg Nesterov if (ignore_state || ptrace_freeze_traced(child)) 197321fb561SOleg Nesterov ret = 0; 1981da177e4SLinus Torvalds } 1991da177e4SLinus Torvalds read_unlock(&tasklist_lock); 2001da177e4SLinus Torvalds 2019899d11fSOleg Nesterov if (!ret && !ignore_state) { 2029899d11fSOleg Nesterov if (!wait_task_inactive(child, __TASK_TRACED)) { 2039899d11fSOleg Nesterov /* 2049899d11fSOleg Nesterov * This can only happen if may_ptrace_stop() fails and 2059899d11fSOleg Nesterov * ptrace_stop() changes ->state back to TASK_RUNNING, 2069899d11fSOleg Nesterov * so we should not worry about leaking __TASK_TRACED. 2079899d11fSOleg Nesterov */ 2089899d11fSOleg Nesterov WARN_ON(child->state == __TASK_TRACED); 2099899d11fSOleg Nesterov ret = -ESRCH; 2109899d11fSOleg Nesterov } 2119899d11fSOleg Nesterov } 2121da177e4SLinus Torvalds 2131da177e4SLinus Torvalds return ret; 2141da177e4SLinus Torvalds } 2151da177e4SLinus Torvalds 21669f594a3SEric Paris static int ptrace_has_cap(struct user_namespace *ns, unsigned int mode) 21769f594a3SEric Paris { 21869f594a3SEric Paris if (mode & PTRACE_MODE_NOAUDIT) 21969f594a3SEric Paris return has_ns_capability_noaudit(current, ns, CAP_SYS_PTRACE); 22069f594a3SEric Paris else 22169f594a3SEric Paris return has_ns_capability(current, ns, CAP_SYS_PTRACE); 22269f594a3SEric Paris } 22369f594a3SEric Paris 2249f99798fSTetsuo Handa /* Returns 0 on success, -errno on denial. */ 2259f99798fSTetsuo Handa static int __ptrace_may_access(struct task_struct *task, unsigned int mode) 226ab8d11beSMiklos Szeredi { 227c69e8d9cSDavid Howells const struct cred *cred = current_cred(), *tcred; 228b6dff3ecSDavid Howells 229df26c40eSEric W. Biederman /* May we inspect the given task? 230df26c40eSEric W. Biederman * This check is used both for attaching with ptrace 231df26c40eSEric W. Biederman * and for allowing access to sensitive information in /proc. 232df26c40eSEric W. Biederman * 233df26c40eSEric W. Biederman * ptrace_attach denies several cases that /proc allows 234df26c40eSEric W. Biederman * because setting up the necessary parent/child relationship 235df26c40eSEric W. Biederman * or halting the specified task is impossible. 236df26c40eSEric W. Biederman */ 237df26c40eSEric W. Biederman int dumpable = 0; 238df26c40eSEric W. Biederman /* Don't let security modules deny introspection */ 239df26c40eSEric W. Biederman if (task == current) 240df26c40eSEric W. Biederman return 0; 241c69e8d9cSDavid Howells rcu_read_lock(); 242c69e8d9cSDavid Howells tcred = __task_cred(task); 2435af66203SEric W. Biederman if (uid_eq(cred->uid, tcred->euid) && 2445af66203SEric W. Biederman uid_eq(cred->uid, tcred->suid) && 2455af66203SEric W. Biederman uid_eq(cred->uid, tcred->uid) && 2465af66203SEric W. Biederman gid_eq(cred->gid, tcred->egid) && 2475af66203SEric W. Biederman gid_eq(cred->gid, tcred->sgid) && 2485af66203SEric W. Biederman gid_eq(cred->gid, tcred->gid)) 2498409cca7SSerge E. Hallyn goto ok; 250c4a4d603SEric W. Biederman if (ptrace_has_cap(tcred->user_ns, mode)) 2518409cca7SSerge E. Hallyn goto ok; 252c69e8d9cSDavid Howells rcu_read_unlock(); 253ab8d11beSMiklos Szeredi return -EPERM; 2548409cca7SSerge E. Hallyn ok: 255c69e8d9cSDavid Howells rcu_read_unlock(); 256ab8d11beSMiklos Szeredi smp_rmb(); 257df26c40eSEric W. Biederman if (task->mm) 2586c5d5238SKawai, Hidehiro dumpable = get_dumpable(task->mm); 2594c44aaafSEric W. Biederman rcu_read_lock(); 2604c44aaafSEric W. Biederman if (!dumpable && !ptrace_has_cap(__task_cred(task)->user_ns, mode)) { 2614c44aaafSEric W. Biederman rcu_read_unlock(); 262ab8d11beSMiklos Szeredi return -EPERM; 2634c44aaafSEric W. Biederman } 2644c44aaafSEric W. Biederman rcu_read_unlock(); 265ab8d11beSMiklos Szeredi 2669e48858fSIngo Molnar return security_ptrace_access_check(task, mode); 267ab8d11beSMiklos Szeredi } 268ab8d11beSMiklos Szeredi 269006ebb40SStephen Smalley bool ptrace_may_access(struct task_struct *task, unsigned int mode) 270ab8d11beSMiklos Szeredi { 271ab8d11beSMiklos Szeredi int err; 272ab8d11beSMiklos Szeredi task_lock(task); 273006ebb40SStephen Smalley err = __ptrace_may_access(task, mode); 274ab8d11beSMiklos Szeredi task_unlock(task); 2753a709703SRoland McGrath return !err; 276ab8d11beSMiklos Szeredi } 277ab8d11beSMiklos Szeredi 2783544d72aSTejun Heo static int ptrace_attach(struct task_struct *task, long request, 279aa9147c9SDenys Vlasenko unsigned long addr, 2803544d72aSTejun Heo unsigned long flags) 2811da177e4SLinus Torvalds { 2823544d72aSTejun Heo bool seize = (request == PTRACE_SEIZE); 2831da177e4SLinus Torvalds int retval; 284f5b40e36SLinus Torvalds 2853544d72aSTejun Heo retval = -EIO; 286aa9147c9SDenys Vlasenko if (seize) { 287aa9147c9SDenys Vlasenko if (addr != 0) 2883544d72aSTejun Heo goto out; 289aa9147c9SDenys Vlasenko if (flags & ~(unsigned long)PTRACE_O_MASK) 290aa9147c9SDenys Vlasenko goto out; 291aa9147c9SDenys Vlasenko flags = PT_PTRACED | PT_SEIZED | (flags << PT_OPT_FLAG_SHIFT); 292aa9147c9SDenys Vlasenko } else { 293aa9147c9SDenys Vlasenko flags = PT_PTRACED; 294aa9147c9SDenys Vlasenko } 2953544d72aSTejun Heo 296a5cb013dSAl Viro audit_ptrace(task); 297a5cb013dSAl Viro 2981da177e4SLinus Torvalds retval = -EPERM; 299b79b7ba9SOleg Nesterov if (unlikely(task->flags & PF_KTHREAD)) 300b79b7ba9SOleg Nesterov goto out; 301bac0abd6SPavel Emelyanov if (same_thread_group(task, current)) 302f5b40e36SLinus Torvalds goto out; 303f5b40e36SLinus Torvalds 304f2f0b00aSOleg Nesterov /* 305f2f0b00aSOleg Nesterov * Protect exec's credential calculations against our interference; 30686b6c1f3SDenys Vlasenko * SUID, SGID and LSM creds get determined differently 3075e751e99SDavid Howells * under ptrace. 308d84f4f99SDavid Howells */ 309793285fcSOleg Nesterov retval = -ERESTARTNOINTR; 3109b1bf12dSKOSAKI Motohiro if (mutex_lock_interruptible(&task->signal->cred_guard_mutex)) 311d84f4f99SDavid Howells goto out; 3124b105cbbSOleg Nesterov 313f5b40e36SLinus Torvalds task_lock(task); 314006ebb40SStephen Smalley retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH); 3154b105cbbSOleg Nesterov task_unlock(task); 3161da177e4SLinus Torvalds if (retval) 3174b105cbbSOleg Nesterov goto unlock_creds; 3181da177e4SLinus Torvalds 3194b105cbbSOleg Nesterov write_lock_irq(&tasklist_lock); 320b79b7ba9SOleg Nesterov retval = -EPERM; 321b79b7ba9SOleg Nesterov if (unlikely(task->exit_state)) 3224b105cbbSOleg Nesterov goto unlock_tasklist; 323f2f0b00aSOleg Nesterov if (task->ptrace) 3244b105cbbSOleg Nesterov goto unlock_tasklist; 325b79b7ba9SOleg Nesterov 3263544d72aSTejun Heo if (seize) 327aa9147c9SDenys Vlasenko flags |= PT_SEIZED; 3284c44aaafSEric W. Biederman rcu_read_lock(); 3294c44aaafSEric W. Biederman if (ns_capable(__task_cred(task)->user_ns, CAP_SYS_PTRACE)) 330aa9147c9SDenys Vlasenko flags |= PT_PTRACE_CAP; 3314c44aaafSEric W. Biederman rcu_read_unlock(); 332aa9147c9SDenys Vlasenko task->ptrace = flags; 3331da177e4SLinus Torvalds 3341da177e4SLinus Torvalds __ptrace_link(task, current); 3353544d72aSTejun Heo 3363544d72aSTejun Heo /* SEIZE doesn't trap tracee on attach */ 3373544d72aSTejun Heo if (!seize) 33833e9fc7dSOleg Nesterov send_sig_info(SIGSTOP, SEND_SIG_FORCED, task); 339b79b7ba9SOleg Nesterov 340d79fdd6dSTejun Heo spin_lock(&task->sighand->siglock); 341d79fdd6dSTejun Heo 342d79fdd6dSTejun Heo /* 34373ddff2bSTejun Heo * If the task is already STOPPED, set JOBCTL_TRAP_STOP and 344d79fdd6dSTejun Heo * TRAPPING, and kick it so that it transits to TRACED. TRAPPING 345d79fdd6dSTejun Heo * will be cleared if the child completes the transition or any 346d79fdd6dSTejun Heo * event which clears the group stop states happens. We'll wait 347d79fdd6dSTejun Heo * for the transition to complete before returning from this 348d79fdd6dSTejun Heo * function. 349d79fdd6dSTejun Heo * 350d79fdd6dSTejun Heo * This hides STOPPED -> RUNNING -> TRACED transition from the 351d79fdd6dSTejun Heo * attaching thread but a different thread in the same group can 352d79fdd6dSTejun Heo * still observe the transient RUNNING state. IOW, if another 353d79fdd6dSTejun Heo * thread's WNOHANG wait(2) on the stopped tracee races against 354d79fdd6dSTejun Heo * ATTACH, the wait(2) may fail due to the transient RUNNING. 355d79fdd6dSTejun Heo * 356d79fdd6dSTejun Heo * The following task_is_stopped() test is safe as both transitions 357d79fdd6dSTejun Heo * in and out of STOPPED are protected by siglock. 358d79fdd6dSTejun Heo */ 3597dd3db54STejun Heo if (task_is_stopped(task) && 36073ddff2bSTejun Heo task_set_jobctl_pending(task, JOBCTL_TRAP_STOP | JOBCTL_TRAPPING)) 361910ffdb1SOleg Nesterov signal_wake_up_state(task, __TASK_STOPPED); 362d79fdd6dSTejun Heo 363d79fdd6dSTejun Heo spin_unlock(&task->sighand->siglock); 364d79fdd6dSTejun Heo 365b79b7ba9SOleg Nesterov retval = 0; 3664b105cbbSOleg Nesterov unlock_tasklist: 3674b105cbbSOleg Nesterov write_unlock_irq(&tasklist_lock); 3684b105cbbSOleg Nesterov unlock_creds: 3699b1bf12dSKOSAKI Motohiro mutex_unlock(&task->signal->cred_guard_mutex); 370f5b40e36SLinus Torvalds out: 371f701e5b7SVladimir Zapolskiy if (!retval) { 37262c124ffSTejun Heo wait_on_bit(&task->jobctl, JOBCTL_TRAPPING_BIT, 37362c124ffSTejun Heo ptrace_trapping_sleep_fn, TASK_UNINTERRUPTIBLE); 374f701e5b7SVladimir Zapolskiy proc_ptrace_connector(task, PTRACE_ATTACH); 375f701e5b7SVladimir Zapolskiy } 376f701e5b7SVladimir Zapolskiy 3771da177e4SLinus Torvalds return retval; 3781da177e4SLinus Torvalds } 3791da177e4SLinus Torvalds 380f2f0b00aSOleg Nesterov /** 381f2f0b00aSOleg Nesterov * ptrace_traceme -- helper for PTRACE_TRACEME 382f2f0b00aSOleg Nesterov * 383f2f0b00aSOleg Nesterov * Performs checks and sets PT_PTRACED. 384f2f0b00aSOleg Nesterov * Should be used by all ptrace implementations for PTRACE_TRACEME. 385f2f0b00aSOleg Nesterov */ 386e3e89cc5SLinus Torvalds static int ptrace_traceme(void) 387f2f0b00aSOleg Nesterov { 388f2f0b00aSOleg Nesterov int ret = -EPERM; 389f2f0b00aSOleg Nesterov 3904b105cbbSOleg Nesterov write_lock_irq(&tasklist_lock); 3914b105cbbSOleg Nesterov /* Are we already being traced? */ 392f2f0b00aSOleg Nesterov if (!current->ptrace) { 393f2f0b00aSOleg Nesterov ret = security_ptrace_traceme(current->parent); 394f2f0b00aSOleg Nesterov /* 395f2f0b00aSOleg Nesterov * Check PF_EXITING to ensure ->real_parent has not passed 396f2f0b00aSOleg Nesterov * exit_ptrace(). Otherwise we don't report the error but 397f2f0b00aSOleg Nesterov * pretend ->real_parent untraces us right after return. 398f2f0b00aSOleg Nesterov */ 399f2f0b00aSOleg Nesterov if (!ret && !(current->real_parent->flags & PF_EXITING)) { 400f2f0b00aSOleg Nesterov current->ptrace = PT_PTRACED; 401f2f0b00aSOleg Nesterov __ptrace_link(current, current->real_parent); 402f2f0b00aSOleg Nesterov } 403f2f0b00aSOleg Nesterov } 4044b105cbbSOleg Nesterov write_unlock_irq(&tasklist_lock); 4054b105cbbSOleg Nesterov 406f2f0b00aSOleg Nesterov return ret; 407f2f0b00aSOleg Nesterov } 408f2f0b00aSOleg Nesterov 40939c626aeSOleg Nesterov /* 41039c626aeSOleg Nesterov * Called with irqs disabled, returns true if childs should reap themselves. 41139c626aeSOleg Nesterov */ 41239c626aeSOleg Nesterov static int ignoring_children(struct sighand_struct *sigh) 41339c626aeSOleg Nesterov { 41439c626aeSOleg Nesterov int ret; 41539c626aeSOleg Nesterov spin_lock(&sigh->siglock); 41639c626aeSOleg Nesterov ret = (sigh->action[SIGCHLD-1].sa.sa_handler == SIG_IGN) || 41739c626aeSOleg Nesterov (sigh->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT); 41839c626aeSOleg Nesterov spin_unlock(&sigh->siglock); 41939c626aeSOleg Nesterov return ret; 42039c626aeSOleg Nesterov } 42139c626aeSOleg Nesterov 42239c626aeSOleg Nesterov /* 42339c626aeSOleg Nesterov * Called with tasklist_lock held for writing. 42439c626aeSOleg Nesterov * Unlink a traced task, and clean it up if it was a traced zombie. 42539c626aeSOleg Nesterov * Return true if it needs to be reaped with release_task(). 42639c626aeSOleg Nesterov * (We can't call release_task() here because we already hold tasklist_lock.) 42739c626aeSOleg Nesterov * 42839c626aeSOleg Nesterov * If it's a zombie, our attachedness prevented normal parent notification 42939c626aeSOleg Nesterov * or self-reaping. Do notification now if it would have happened earlier. 43039c626aeSOleg Nesterov * If it should reap itself, return true. 43139c626aeSOleg Nesterov * 432a7f0765eSOleg Nesterov * If it's our own child, there is no notification to do. But if our normal 433a7f0765eSOleg Nesterov * children self-reap, then this child was prevented by ptrace and we must 434a7f0765eSOleg Nesterov * reap it now, in that case we must also wake up sub-threads sleeping in 435a7f0765eSOleg Nesterov * do_wait(). 43639c626aeSOleg Nesterov */ 43739c626aeSOleg Nesterov static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p) 43839c626aeSOleg Nesterov { 4399843a1e9SOleg Nesterov bool dead; 4409843a1e9SOleg Nesterov 44139c626aeSOleg Nesterov __ptrace_unlink(p); 44239c626aeSOleg Nesterov 4439843a1e9SOleg Nesterov if (p->exit_state != EXIT_ZOMBIE) 4449843a1e9SOleg Nesterov return false; 4459843a1e9SOleg Nesterov 4469843a1e9SOleg Nesterov dead = !thread_group_leader(p); 4479843a1e9SOleg Nesterov 4489843a1e9SOleg Nesterov if (!dead && thread_group_empty(p)) { 44939c626aeSOleg Nesterov if (!same_thread_group(p->real_parent, tracer)) 4509843a1e9SOleg Nesterov dead = do_notify_parent(p, p->exit_signal); 451a7f0765eSOleg Nesterov else if (ignoring_children(tracer->sighand)) { 452a7f0765eSOleg Nesterov __wake_up_parent(p, tracer); 4539843a1e9SOleg Nesterov dead = true; 45439c626aeSOleg Nesterov } 455a7f0765eSOleg Nesterov } 45639c626aeSOleg Nesterov /* Mark it as in the process of being reaped. */ 4579843a1e9SOleg Nesterov if (dead) 45839c626aeSOleg Nesterov p->exit_state = EXIT_DEAD; 4599843a1e9SOleg Nesterov return dead; 46039c626aeSOleg Nesterov } 46139c626aeSOleg Nesterov 462e3e89cc5SLinus Torvalds static int ptrace_detach(struct task_struct *child, unsigned int data) 4631da177e4SLinus Torvalds { 46439c626aeSOleg Nesterov bool dead = false; 4654576145cSOleg Nesterov 4667ed20e1aSJesper Juhl if (!valid_signal(data)) 4671da177e4SLinus Torvalds return -EIO; 4681da177e4SLinus Torvalds 4691da177e4SLinus Torvalds /* Architecture-specific hardware disable .. */ 4701da177e4SLinus Torvalds ptrace_disable(child); 4717d941432SRoland McGrath clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); 4721da177e4SLinus Torvalds 47395c3eb76SOleg Nesterov write_lock_irq(&tasklist_lock); 47439c626aeSOleg Nesterov /* 47539c626aeSOleg Nesterov * This child can be already killed. Make sure de_thread() or 47639c626aeSOleg Nesterov * our sub-thread doing do_wait() didn't do release_task() yet. 47739c626aeSOleg Nesterov */ 47895c3eb76SOleg Nesterov if (child->ptrace) { 47995c3eb76SOleg Nesterov child->exit_code = data; 4804576145cSOleg Nesterov dead = __ptrace_detach(current, child); 48195c3eb76SOleg Nesterov } 4821da177e4SLinus Torvalds write_unlock_irq(&tasklist_lock); 4831da177e4SLinus Torvalds 484f701e5b7SVladimir Zapolskiy proc_ptrace_connector(child, PTRACE_DETACH); 4854576145cSOleg Nesterov if (unlikely(dead)) 4864576145cSOleg Nesterov release_task(child); 4874576145cSOleg Nesterov 4881da177e4SLinus Torvalds return 0; 4891da177e4SLinus Torvalds } 4901da177e4SLinus Torvalds 49139c626aeSOleg Nesterov /* 492c7e49c14SOleg Nesterov * Detach all tasks we were using ptrace on. Called with tasklist held 493c7e49c14SOleg Nesterov * for writing, and returns with it held too. But note it can release 494c7e49c14SOleg Nesterov * and reacquire the lock. 49539c626aeSOleg Nesterov */ 49639c626aeSOleg Nesterov void exit_ptrace(struct task_struct *tracer) 497c4b5ed25SNamhyung Kim __releases(&tasklist_lock) 498c4b5ed25SNamhyung Kim __acquires(&tasklist_lock) 49939c626aeSOleg Nesterov { 50039c626aeSOleg Nesterov struct task_struct *p, *n; 50139c626aeSOleg Nesterov LIST_HEAD(ptrace_dead); 50239c626aeSOleg Nesterov 503c7e49c14SOleg Nesterov if (likely(list_empty(&tracer->ptraced))) 504c7e49c14SOleg Nesterov return; 505c7e49c14SOleg Nesterov 50639c626aeSOleg Nesterov list_for_each_entry_safe(p, n, &tracer->ptraced, ptrace_entry) { 507992fb6e1SOleg Nesterov if (unlikely(p->ptrace & PT_EXITKILL)) 508992fb6e1SOleg Nesterov send_sig_info(SIGKILL, SEND_SIG_FORCED, p); 509992fb6e1SOleg Nesterov 51039c626aeSOleg Nesterov if (__ptrace_detach(tracer, p)) 51139c626aeSOleg Nesterov list_add(&p->ptrace_entry, &ptrace_dead); 51239c626aeSOleg Nesterov } 51339c626aeSOleg Nesterov 514c7e49c14SOleg Nesterov write_unlock_irq(&tasklist_lock); 51539c626aeSOleg Nesterov BUG_ON(!list_empty(&tracer->ptraced)); 51639c626aeSOleg Nesterov 51739c626aeSOleg Nesterov list_for_each_entry_safe(p, n, &ptrace_dead, ptrace_entry) { 51839c626aeSOleg Nesterov list_del_init(&p->ptrace_entry); 51939c626aeSOleg Nesterov release_task(p); 52039c626aeSOleg Nesterov } 521c7e49c14SOleg Nesterov 522c7e49c14SOleg Nesterov write_lock_irq(&tasklist_lock); 52339c626aeSOleg Nesterov } 52439c626aeSOleg Nesterov 5251da177e4SLinus Torvalds int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len) 5261da177e4SLinus Torvalds { 5271da177e4SLinus Torvalds int copied = 0; 5281da177e4SLinus Torvalds 5291da177e4SLinus Torvalds while (len > 0) { 5301da177e4SLinus Torvalds char buf[128]; 5311da177e4SLinus Torvalds int this_len, retval; 5321da177e4SLinus Torvalds 5331da177e4SLinus Torvalds this_len = (len > sizeof(buf)) ? sizeof(buf) : len; 5341da177e4SLinus Torvalds retval = access_process_vm(tsk, src, buf, this_len, 0); 5351da177e4SLinus Torvalds if (!retval) { 5361da177e4SLinus Torvalds if (copied) 5371da177e4SLinus Torvalds break; 5381da177e4SLinus Torvalds return -EIO; 5391da177e4SLinus Torvalds } 5401da177e4SLinus Torvalds if (copy_to_user(dst, buf, retval)) 5411da177e4SLinus Torvalds return -EFAULT; 5421da177e4SLinus Torvalds copied += retval; 5431da177e4SLinus Torvalds src += retval; 5441da177e4SLinus Torvalds dst += retval; 5451da177e4SLinus Torvalds len -= retval; 5461da177e4SLinus Torvalds } 5471da177e4SLinus Torvalds return copied; 5481da177e4SLinus Torvalds } 5491da177e4SLinus Torvalds 5501da177e4SLinus Torvalds int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len) 5511da177e4SLinus Torvalds { 5521da177e4SLinus Torvalds int copied = 0; 5531da177e4SLinus Torvalds 5541da177e4SLinus Torvalds while (len > 0) { 5551da177e4SLinus Torvalds char buf[128]; 5561da177e4SLinus Torvalds int this_len, retval; 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds this_len = (len > sizeof(buf)) ? sizeof(buf) : len; 5591da177e4SLinus Torvalds if (copy_from_user(buf, src, this_len)) 5601da177e4SLinus Torvalds return -EFAULT; 5611da177e4SLinus Torvalds retval = access_process_vm(tsk, dst, buf, this_len, 1); 5621da177e4SLinus Torvalds if (!retval) { 5631da177e4SLinus Torvalds if (copied) 5641da177e4SLinus Torvalds break; 5651da177e4SLinus Torvalds return -EIO; 5661da177e4SLinus Torvalds } 5671da177e4SLinus Torvalds copied += retval; 5681da177e4SLinus Torvalds src += retval; 5691da177e4SLinus Torvalds dst += retval; 5701da177e4SLinus Torvalds len -= retval; 5711da177e4SLinus Torvalds } 5721da177e4SLinus Torvalds return copied; 5731da177e4SLinus Torvalds } 5741da177e4SLinus Torvalds 5754abf9869SNamhyung Kim static int ptrace_setoptions(struct task_struct *child, unsigned long data) 5761da177e4SLinus Torvalds { 57786b6c1f3SDenys Vlasenko unsigned flags; 57886b6c1f3SDenys Vlasenko 5798c5cf9e5SDenys Vlasenko if (data & ~(unsigned long)PTRACE_O_MASK) 5808c5cf9e5SDenys Vlasenko return -EINVAL; 5818c5cf9e5SDenys Vlasenko 58286b6c1f3SDenys Vlasenko /* Avoid intermediate state when all opts are cleared */ 58386b6c1f3SDenys Vlasenko flags = child->ptrace; 58486b6c1f3SDenys Vlasenko flags &= ~(PTRACE_O_MASK << PT_OPT_FLAG_SHIFT); 58586b6c1f3SDenys Vlasenko flags |= (data << PT_OPT_FLAG_SHIFT); 58686b6c1f3SDenys Vlasenko child->ptrace = flags; 5871da177e4SLinus Torvalds 5888c5cf9e5SDenys Vlasenko return 0; 5891da177e4SLinus Torvalds } 5901da177e4SLinus Torvalds 591e16b2781SRoland McGrath static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info) 5921da177e4SLinus Torvalds { 593e4961254SOleg Nesterov unsigned long flags; 5941da177e4SLinus Torvalds int error = -ESRCH; 5951da177e4SLinus Torvalds 596e4961254SOleg Nesterov if (lock_task_sighand(child, &flags)) { 5971da177e4SLinus Torvalds error = -EINVAL; 5981da177e4SLinus Torvalds if (likely(child->last_siginfo != NULL)) { 599e16b2781SRoland McGrath *info = *child->last_siginfo; 6001da177e4SLinus Torvalds error = 0; 6011da177e4SLinus Torvalds } 602e4961254SOleg Nesterov unlock_task_sighand(child, &flags); 6031da177e4SLinus Torvalds } 6041da177e4SLinus Torvalds return error; 6051da177e4SLinus Torvalds } 6061da177e4SLinus Torvalds 607e16b2781SRoland McGrath static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info) 6081da177e4SLinus Torvalds { 609e4961254SOleg Nesterov unsigned long flags; 6101da177e4SLinus Torvalds int error = -ESRCH; 6111da177e4SLinus Torvalds 612e4961254SOleg Nesterov if (lock_task_sighand(child, &flags)) { 6131da177e4SLinus Torvalds error = -EINVAL; 6141da177e4SLinus Torvalds if (likely(child->last_siginfo != NULL)) { 615e16b2781SRoland McGrath *child->last_siginfo = *info; 6161da177e4SLinus Torvalds error = 0; 6171da177e4SLinus Torvalds } 618e4961254SOleg Nesterov unlock_task_sighand(child, &flags); 6191da177e4SLinus Torvalds } 6201da177e4SLinus Torvalds return error; 6211da177e4SLinus Torvalds } 6221da177e4SLinus Torvalds 62384c751bdSAndrey Vagin static int ptrace_peek_siginfo(struct task_struct *child, 62484c751bdSAndrey Vagin unsigned long addr, 62584c751bdSAndrey Vagin unsigned long data) 62684c751bdSAndrey Vagin { 62784c751bdSAndrey Vagin struct ptrace_peeksiginfo_args arg; 62884c751bdSAndrey Vagin struct sigpending *pending; 62984c751bdSAndrey Vagin struct sigqueue *q; 63084c751bdSAndrey Vagin int ret, i; 63184c751bdSAndrey Vagin 63284c751bdSAndrey Vagin ret = copy_from_user(&arg, (void __user *) addr, 63384c751bdSAndrey Vagin sizeof(struct ptrace_peeksiginfo_args)); 63484c751bdSAndrey Vagin if (ret) 63584c751bdSAndrey Vagin return -EFAULT; 63684c751bdSAndrey Vagin 63784c751bdSAndrey Vagin if (arg.flags & ~PTRACE_PEEKSIGINFO_SHARED) 63884c751bdSAndrey Vagin return -EINVAL; /* unknown flags */ 63984c751bdSAndrey Vagin 64084c751bdSAndrey Vagin if (arg.nr < 0) 64184c751bdSAndrey Vagin return -EINVAL; 64284c751bdSAndrey Vagin 64384c751bdSAndrey Vagin if (arg.flags & PTRACE_PEEKSIGINFO_SHARED) 64484c751bdSAndrey Vagin pending = &child->signal->shared_pending; 64584c751bdSAndrey Vagin else 64684c751bdSAndrey Vagin pending = &child->pending; 64784c751bdSAndrey Vagin 64884c751bdSAndrey Vagin for (i = 0; i < arg.nr; ) { 64984c751bdSAndrey Vagin siginfo_t info; 65084c751bdSAndrey Vagin s32 off = arg.off + i; 65184c751bdSAndrey Vagin 65284c751bdSAndrey Vagin spin_lock_irq(&child->sighand->siglock); 65384c751bdSAndrey Vagin list_for_each_entry(q, &pending->list, list) { 65484c751bdSAndrey Vagin if (!off--) { 65584c751bdSAndrey Vagin copy_siginfo(&info, &q->info); 65684c751bdSAndrey Vagin break; 65784c751bdSAndrey Vagin } 65884c751bdSAndrey Vagin } 65984c751bdSAndrey Vagin spin_unlock_irq(&child->sighand->siglock); 66084c751bdSAndrey Vagin 66184c751bdSAndrey Vagin if (off >= 0) /* beyond the end of the list */ 66284c751bdSAndrey Vagin break; 66384c751bdSAndrey Vagin 66484c751bdSAndrey Vagin #ifdef CONFIG_COMPAT 66584c751bdSAndrey Vagin if (unlikely(is_compat_task())) { 66684c751bdSAndrey Vagin compat_siginfo_t __user *uinfo = compat_ptr(data); 66784c751bdSAndrey Vagin 668706b23bdSMathieu Desnoyers if (copy_siginfo_to_user32(uinfo, &info) || 669706b23bdSMathieu Desnoyers __put_user(info.si_code, &uinfo->si_code)) { 670706b23bdSMathieu Desnoyers ret = -EFAULT; 671706b23bdSMathieu Desnoyers break; 672706b23bdSMathieu Desnoyers } 673706b23bdSMathieu Desnoyers 67484c751bdSAndrey Vagin } else 67584c751bdSAndrey Vagin #endif 67684c751bdSAndrey Vagin { 67784c751bdSAndrey Vagin siginfo_t __user *uinfo = (siginfo_t __user *) data; 67884c751bdSAndrey Vagin 679706b23bdSMathieu Desnoyers if (copy_siginfo_to_user(uinfo, &info) || 680706b23bdSMathieu Desnoyers __put_user(info.si_code, &uinfo->si_code)) { 68184c751bdSAndrey Vagin ret = -EFAULT; 68284c751bdSAndrey Vagin break; 68384c751bdSAndrey Vagin } 684706b23bdSMathieu Desnoyers } 68584c751bdSAndrey Vagin 68684c751bdSAndrey Vagin data += sizeof(siginfo_t); 68784c751bdSAndrey Vagin i++; 68884c751bdSAndrey Vagin 68984c751bdSAndrey Vagin if (signal_pending(current)) 69084c751bdSAndrey Vagin break; 69184c751bdSAndrey Vagin 69284c751bdSAndrey Vagin cond_resched(); 69384c751bdSAndrey Vagin } 69484c751bdSAndrey Vagin 69584c751bdSAndrey Vagin if (i > 0) 69684c751bdSAndrey Vagin return i; 69784c751bdSAndrey Vagin 69884c751bdSAndrey Vagin return ret; 69984c751bdSAndrey Vagin } 70036df29d7SRoland McGrath 70136df29d7SRoland McGrath #ifdef PTRACE_SINGLESTEP 70236df29d7SRoland McGrath #define is_singlestep(request) ((request) == PTRACE_SINGLESTEP) 70336df29d7SRoland McGrath #else 70436df29d7SRoland McGrath #define is_singlestep(request) 0 70536df29d7SRoland McGrath #endif 70636df29d7SRoland McGrath 7075b88abbfSRoland McGrath #ifdef PTRACE_SINGLEBLOCK 7085b88abbfSRoland McGrath #define is_singleblock(request) ((request) == PTRACE_SINGLEBLOCK) 7095b88abbfSRoland McGrath #else 7105b88abbfSRoland McGrath #define is_singleblock(request) 0 7115b88abbfSRoland McGrath #endif 7125b88abbfSRoland McGrath 71336df29d7SRoland McGrath #ifdef PTRACE_SYSEMU 71436df29d7SRoland McGrath #define is_sysemu_singlestep(request) ((request) == PTRACE_SYSEMU_SINGLESTEP) 71536df29d7SRoland McGrath #else 71636df29d7SRoland McGrath #define is_sysemu_singlestep(request) 0 71736df29d7SRoland McGrath #endif 71836df29d7SRoland McGrath 7194abf9869SNamhyung Kim static int ptrace_resume(struct task_struct *child, long request, 7204abf9869SNamhyung Kim unsigned long data) 72136df29d7SRoland McGrath { 72236df29d7SRoland McGrath if (!valid_signal(data)) 72336df29d7SRoland McGrath return -EIO; 72436df29d7SRoland McGrath 72536df29d7SRoland McGrath if (request == PTRACE_SYSCALL) 72636df29d7SRoland McGrath set_tsk_thread_flag(child, TIF_SYSCALL_TRACE); 72736df29d7SRoland McGrath else 72836df29d7SRoland McGrath clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); 72936df29d7SRoland McGrath 73036df29d7SRoland McGrath #ifdef TIF_SYSCALL_EMU 73136df29d7SRoland McGrath if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP) 73236df29d7SRoland McGrath set_tsk_thread_flag(child, TIF_SYSCALL_EMU); 73336df29d7SRoland McGrath else 73436df29d7SRoland McGrath clear_tsk_thread_flag(child, TIF_SYSCALL_EMU); 73536df29d7SRoland McGrath #endif 73636df29d7SRoland McGrath 7375b88abbfSRoland McGrath if (is_singleblock(request)) { 7385b88abbfSRoland McGrath if (unlikely(!arch_has_block_step())) 7395b88abbfSRoland McGrath return -EIO; 7405b88abbfSRoland McGrath user_enable_block_step(child); 7415b88abbfSRoland McGrath } else if (is_singlestep(request) || is_sysemu_singlestep(request)) { 74236df29d7SRoland McGrath if (unlikely(!arch_has_single_step())) 74336df29d7SRoland McGrath return -EIO; 74436df29d7SRoland McGrath user_enable_single_step(child); 7453a709703SRoland McGrath } else { 74636df29d7SRoland McGrath user_disable_single_step(child); 7473a709703SRoland McGrath } 74836df29d7SRoland McGrath 74936df29d7SRoland McGrath child->exit_code = data; 7500666fb51SOleg Nesterov wake_up_state(child, __TASK_TRACED); 75136df29d7SRoland McGrath 75236df29d7SRoland McGrath return 0; 75336df29d7SRoland McGrath } 75436df29d7SRoland McGrath 7552225a122SSuresh Siddha #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 7562225a122SSuresh Siddha 7572225a122SSuresh Siddha static const struct user_regset * 7582225a122SSuresh Siddha find_regset(const struct user_regset_view *view, unsigned int type) 7592225a122SSuresh Siddha { 7602225a122SSuresh Siddha const struct user_regset *regset; 7612225a122SSuresh Siddha int n; 7622225a122SSuresh Siddha 7632225a122SSuresh Siddha for (n = 0; n < view->n; ++n) { 7642225a122SSuresh Siddha regset = view->regsets + n; 7652225a122SSuresh Siddha if (regset->core_note_type == type) 7662225a122SSuresh Siddha return regset; 7672225a122SSuresh Siddha } 7682225a122SSuresh Siddha 7692225a122SSuresh Siddha return NULL; 7702225a122SSuresh Siddha } 7712225a122SSuresh Siddha 7722225a122SSuresh Siddha static int ptrace_regset(struct task_struct *task, int req, unsigned int type, 7732225a122SSuresh Siddha struct iovec *kiov) 7742225a122SSuresh Siddha { 7752225a122SSuresh Siddha const struct user_regset_view *view = task_user_regset_view(task); 7762225a122SSuresh Siddha const struct user_regset *regset = find_regset(view, type); 7772225a122SSuresh Siddha int regset_no; 7782225a122SSuresh Siddha 7792225a122SSuresh Siddha if (!regset || (kiov->iov_len % regset->size) != 0) 780c6a0dd7eSSuresh Siddha return -EINVAL; 7812225a122SSuresh Siddha 7822225a122SSuresh Siddha regset_no = regset - view->regsets; 7832225a122SSuresh Siddha kiov->iov_len = min(kiov->iov_len, 7842225a122SSuresh Siddha (__kernel_size_t) (regset->n * regset->size)); 7852225a122SSuresh Siddha 7862225a122SSuresh Siddha if (req == PTRACE_GETREGSET) 7872225a122SSuresh Siddha return copy_regset_to_user(task, view, regset_no, 0, 7882225a122SSuresh Siddha kiov->iov_len, kiov->iov_base); 7892225a122SSuresh Siddha else 7902225a122SSuresh Siddha return copy_regset_from_user(task, view, regset_no, 0, 7912225a122SSuresh Siddha kiov->iov_len, kiov->iov_base); 7922225a122SSuresh Siddha } 7932225a122SSuresh Siddha 794e8440c14SJosh Stone /* 795e8440c14SJosh Stone * This is declared in linux/regset.h and defined in machine-dependent 796e8440c14SJosh Stone * code. We put the export here, near the primary machine-neutral use, 797e8440c14SJosh Stone * to ensure no machine forgets it. 798e8440c14SJosh Stone */ 799e8440c14SJosh Stone EXPORT_SYMBOL_GPL(task_user_regset_view); 8002225a122SSuresh Siddha #endif 8012225a122SSuresh Siddha 8021da177e4SLinus Torvalds int ptrace_request(struct task_struct *child, long request, 8034abf9869SNamhyung Kim unsigned long addr, unsigned long data) 8041da177e4SLinus Torvalds { 805fca26f26STejun Heo bool seized = child->ptrace & PT_SEIZED; 8061da177e4SLinus Torvalds int ret = -EIO; 807544b2c91STejun Heo siginfo_t siginfo, *si; 8089fed81dcSNamhyung Kim void __user *datavp = (void __user *) data; 8099fed81dcSNamhyung Kim unsigned long __user *datalp = datavp; 810fca26f26STejun Heo unsigned long flags; 8111da177e4SLinus Torvalds 8121da177e4SLinus Torvalds switch (request) { 81316c3e389SRoland McGrath case PTRACE_PEEKTEXT: 81416c3e389SRoland McGrath case PTRACE_PEEKDATA: 81516c3e389SRoland McGrath return generic_ptrace_peekdata(child, addr, data); 81616c3e389SRoland McGrath case PTRACE_POKETEXT: 81716c3e389SRoland McGrath case PTRACE_POKEDATA: 81816c3e389SRoland McGrath return generic_ptrace_pokedata(child, addr, data); 81916c3e389SRoland McGrath 8201da177e4SLinus Torvalds #ifdef PTRACE_OLDSETOPTIONS 8211da177e4SLinus Torvalds case PTRACE_OLDSETOPTIONS: 8221da177e4SLinus Torvalds #endif 8231da177e4SLinus Torvalds case PTRACE_SETOPTIONS: 8241da177e4SLinus Torvalds ret = ptrace_setoptions(child, data); 8251da177e4SLinus Torvalds break; 8261da177e4SLinus Torvalds case PTRACE_GETEVENTMSG: 8279fed81dcSNamhyung Kim ret = put_user(child->ptrace_message, datalp); 8281da177e4SLinus Torvalds break; 829e16b2781SRoland McGrath 83084c751bdSAndrey Vagin case PTRACE_PEEKSIGINFO: 83184c751bdSAndrey Vagin ret = ptrace_peek_siginfo(child, addr, data); 83284c751bdSAndrey Vagin break; 83384c751bdSAndrey Vagin 8341da177e4SLinus Torvalds case PTRACE_GETSIGINFO: 835e16b2781SRoland McGrath ret = ptrace_getsiginfo(child, &siginfo); 836e16b2781SRoland McGrath if (!ret) 8379fed81dcSNamhyung Kim ret = copy_siginfo_to_user(datavp, &siginfo); 8381da177e4SLinus Torvalds break; 839e16b2781SRoland McGrath 8401da177e4SLinus Torvalds case PTRACE_SETSIGINFO: 8419fed81dcSNamhyung Kim if (copy_from_user(&siginfo, datavp, sizeof siginfo)) 842e16b2781SRoland McGrath ret = -EFAULT; 843e16b2781SRoland McGrath else 844e16b2781SRoland McGrath ret = ptrace_setsiginfo(child, &siginfo); 8451da177e4SLinus Torvalds break; 846e16b2781SRoland McGrath 84729000caeSAndrey Vagin case PTRACE_GETSIGMASK: 84829000caeSAndrey Vagin if (addr != sizeof(sigset_t)) { 84929000caeSAndrey Vagin ret = -EINVAL; 85029000caeSAndrey Vagin break; 85129000caeSAndrey Vagin } 85229000caeSAndrey Vagin 85329000caeSAndrey Vagin if (copy_to_user(datavp, &child->blocked, sizeof(sigset_t))) 85429000caeSAndrey Vagin ret = -EFAULT; 85529000caeSAndrey Vagin else 85629000caeSAndrey Vagin ret = 0; 85729000caeSAndrey Vagin 85829000caeSAndrey Vagin break; 85929000caeSAndrey Vagin 86029000caeSAndrey Vagin case PTRACE_SETSIGMASK: { 86129000caeSAndrey Vagin sigset_t new_set; 86229000caeSAndrey Vagin 86329000caeSAndrey Vagin if (addr != sizeof(sigset_t)) { 86429000caeSAndrey Vagin ret = -EINVAL; 86529000caeSAndrey Vagin break; 86629000caeSAndrey Vagin } 86729000caeSAndrey Vagin 86829000caeSAndrey Vagin if (copy_from_user(&new_set, datavp, sizeof(sigset_t))) { 86929000caeSAndrey Vagin ret = -EFAULT; 87029000caeSAndrey Vagin break; 87129000caeSAndrey Vagin } 87229000caeSAndrey Vagin 87329000caeSAndrey Vagin sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP)); 87429000caeSAndrey Vagin 87529000caeSAndrey Vagin /* 87629000caeSAndrey Vagin * Every thread does recalc_sigpending() after resume, so 87729000caeSAndrey Vagin * retarget_shared_pending() and recalc_sigpending() are not 87829000caeSAndrey Vagin * called here. 87929000caeSAndrey Vagin */ 88029000caeSAndrey Vagin spin_lock_irq(&child->sighand->siglock); 88129000caeSAndrey Vagin child->blocked = new_set; 88229000caeSAndrey Vagin spin_unlock_irq(&child->sighand->siglock); 88329000caeSAndrey Vagin 88429000caeSAndrey Vagin ret = 0; 88529000caeSAndrey Vagin break; 88629000caeSAndrey Vagin } 88729000caeSAndrey Vagin 888fca26f26STejun Heo case PTRACE_INTERRUPT: 889fca26f26STejun Heo /* 890fca26f26STejun Heo * Stop tracee without any side-effect on signal or job 891fca26f26STejun Heo * control. At least one trap is guaranteed to happen 892fca26f26STejun Heo * after this request. If @child is already trapped, the 893fca26f26STejun Heo * current trap is not disturbed and another trap will 894fca26f26STejun Heo * happen after the current trap is ended with PTRACE_CONT. 895fca26f26STejun Heo * 896fca26f26STejun Heo * The actual trap might not be PTRACE_EVENT_STOP trap but 897fca26f26STejun Heo * the pending condition is cleared regardless. 898fca26f26STejun Heo */ 899fca26f26STejun Heo if (unlikely(!seized || !lock_task_sighand(child, &flags))) 900fca26f26STejun Heo break; 901fca26f26STejun Heo 902544b2c91STejun Heo /* 903544b2c91STejun Heo * INTERRUPT doesn't disturb existing trap sans one 904544b2c91STejun Heo * exception. If ptracer issued LISTEN for the current 905544b2c91STejun Heo * STOP, this INTERRUPT should clear LISTEN and re-trap 906544b2c91STejun Heo * tracee into STOP. 907544b2c91STejun Heo */ 908fca26f26STejun Heo if (likely(task_set_jobctl_pending(child, JOBCTL_TRAP_STOP))) 909910ffdb1SOleg Nesterov ptrace_signal_wake_up(child, child->jobctl & JOBCTL_LISTENING); 910544b2c91STejun Heo 911544b2c91STejun Heo unlock_task_sighand(child, &flags); 912544b2c91STejun Heo ret = 0; 913544b2c91STejun Heo break; 914544b2c91STejun Heo 915544b2c91STejun Heo case PTRACE_LISTEN: 916544b2c91STejun Heo /* 917544b2c91STejun Heo * Listen for events. Tracee must be in STOP. It's not 918544b2c91STejun Heo * resumed per-se but is not considered to be in TRACED by 919544b2c91STejun Heo * wait(2) or ptrace(2). If an async event (e.g. group 920544b2c91STejun Heo * stop state change) happens, tracee will enter STOP trap 921544b2c91STejun Heo * again. Alternatively, ptracer can issue INTERRUPT to 922544b2c91STejun Heo * finish listening and re-trap tracee into STOP. 923544b2c91STejun Heo */ 924544b2c91STejun Heo if (unlikely(!seized || !lock_task_sighand(child, &flags))) 925544b2c91STejun Heo break; 926544b2c91STejun Heo 927544b2c91STejun Heo si = child->last_siginfo; 928f9d81f61SOleg Nesterov if (likely(si && (si->si_code >> 8) == PTRACE_EVENT_STOP)) { 929544b2c91STejun Heo child->jobctl |= JOBCTL_LISTENING; 930544b2c91STejun Heo /* 931f9d81f61SOleg Nesterov * If NOTIFY is set, it means event happened between 932f9d81f61SOleg Nesterov * start of this trap and now. Trigger re-trap. 933544b2c91STejun Heo */ 934544b2c91STejun Heo if (child->jobctl & JOBCTL_TRAP_NOTIFY) 935910ffdb1SOleg Nesterov ptrace_signal_wake_up(child, true); 936fca26f26STejun Heo ret = 0; 937f9d81f61SOleg Nesterov } 938f9d81f61SOleg Nesterov unlock_task_sighand(child, &flags); 939fca26f26STejun Heo break; 940fca26f26STejun Heo 9411bcf5482SAlexey Dobriyan case PTRACE_DETACH: /* detach a process that was attached. */ 9421bcf5482SAlexey Dobriyan ret = ptrace_detach(child, data); 9431bcf5482SAlexey Dobriyan break; 94436df29d7SRoland McGrath 9459c1a1259SMike Frysinger #ifdef CONFIG_BINFMT_ELF_FDPIC 9469c1a1259SMike Frysinger case PTRACE_GETFDPIC: { 947e0129ef9SOleg Nesterov struct mm_struct *mm = get_task_mm(child); 9489c1a1259SMike Frysinger unsigned long tmp = 0; 9499c1a1259SMike Frysinger 950e0129ef9SOleg Nesterov ret = -ESRCH; 951e0129ef9SOleg Nesterov if (!mm) 952e0129ef9SOleg Nesterov break; 953e0129ef9SOleg Nesterov 9549c1a1259SMike Frysinger switch (addr) { 9559c1a1259SMike Frysinger case PTRACE_GETFDPIC_EXEC: 956e0129ef9SOleg Nesterov tmp = mm->context.exec_fdpic_loadmap; 9579c1a1259SMike Frysinger break; 9589c1a1259SMike Frysinger case PTRACE_GETFDPIC_INTERP: 959e0129ef9SOleg Nesterov tmp = mm->context.interp_fdpic_loadmap; 9609c1a1259SMike Frysinger break; 9619c1a1259SMike Frysinger default: 9629c1a1259SMike Frysinger break; 9639c1a1259SMike Frysinger } 964e0129ef9SOleg Nesterov mmput(mm); 9659c1a1259SMike Frysinger 9669fed81dcSNamhyung Kim ret = put_user(tmp, datalp); 9679c1a1259SMike Frysinger break; 9689c1a1259SMike Frysinger } 9699c1a1259SMike Frysinger #endif 9709c1a1259SMike Frysinger 97136df29d7SRoland McGrath #ifdef PTRACE_SINGLESTEP 97236df29d7SRoland McGrath case PTRACE_SINGLESTEP: 97336df29d7SRoland McGrath #endif 9745b88abbfSRoland McGrath #ifdef PTRACE_SINGLEBLOCK 9755b88abbfSRoland McGrath case PTRACE_SINGLEBLOCK: 9765b88abbfSRoland McGrath #endif 97736df29d7SRoland McGrath #ifdef PTRACE_SYSEMU 97836df29d7SRoland McGrath case PTRACE_SYSEMU: 97936df29d7SRoland McGrath case PTRACE_SYSEMU_SINGLESTEP: 98036df29d7SRoland McGrath #endif 98136df29d7SRoland McGrath case PTRACE_SYSCALL: 98236df29d7SRoland McGrath case PTRACE_CONT: 98336df29d7SRoland McGrath return ptrace_resume(child, request, data); 98436df29d7SRoland McGrath 98536df29d7SRoland McGrath case PTRACE_KILL: 98636df29d7SRoland McGrath if (child->exit_state) /* already dead */ 98736df29d7SRoland McGrath return 0; 98836df29d7SRoland McGrath return ptrace_resume(child, request, SIGKILL); 98936df29d7SRoland McGrath 9902225a122SSuresh Siddha #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 9912225a122SSuresh Siddha case PTRACE_GETREGSET: 99229000caeSAndrey Vagin case PTRACE_SETREGSET: { 9932225a122SSuresh Siddha struct iovec kiov; 9949fed81dcSNamhyung Kim struct iovec __user *uiov = datavp; 9952225a122SSuresh Siddha 9962225a122SSuresh Siddha if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov))) 9972225a122SSuresh Siddha return -EFAULT; 9982225a122SSuresh Siddha 9992225a122SSuresh Siddha if (__get_user(kiov.iov_base, &uiov->iov_base) || 10002225a122SSuresh Siddha __get_user(kiov.iov_len, &uiov->iov_len)) 10012225a122SSuresh Siddha return -EFAULT; 10022225a122SSuresh Siddha 10032225a122SSuresh Siddha ret = ptrace_regset(child, request, addr, &kiov); 10042225a122SSuresh Siddha if (!ret) 10052225a122SSuresh Siddha ret = __put_user(kiov.iov_len, &uiov->iov_len); 10062225a122SSuresh Siddha break; 10072225a122SSuresh Siddha } 10082225a122SSuresh Siddha #endif 10091da177e4SLinus Torvalds default: 10101da177e4SLinus Torvalds break; 10111da177e4SLinus Torvalds } 10121da177e4SLinus Torvalds 10131da177e4SLinus Torvalds return ret; 10141da177e4SLinus Torvalds } 1015481bed45SChristoph Hellwig 10168053bdd5SOleg Nesterov static struct task_struct *ptrace_get_task_struct(pid_t pid) 10176b9c7ed8SChristoph Hellwig { 10186b9c7ed8SChristoph Hellwig struct task_struct *child; 10196b9c7ed8SChristoph Hellwig 10208053bdd5SOleg Nesterov rcu_read_lock(); 1021228ebcbeSPavel Emelyanov child = find_task_by_vpid(pid); 1022481bed45SChristoph Hellwig if (child) 1023481bed45SChristoph Hellwig get_task_struct(child); 10248053bdd5SOleg Nesterov rcu_read_unlock(); 1025f400e198SSukadev Bhattiprolu 1026481bed45SChristoph Hellwig if (!child) 10276b9c7ed8SChristoph Hellwig return ERR_PTR(-ESRCH); 10286b9c7ed8SChristoph Hellwig return child; 1029481bed45SChristoph Hellwig } 1030481bed45SChristoph Hellwig 10310ac15559SChristoph Hellwig #ifndef arch_ptrace_attach 10320ac15559SChristoph Hellwig #define arch_ptrace_attach(child) do { } while (0) 10330ac15559SChristoph Hellwig #endif 10340ac15559SChristoph Hellwig 10354abf9869SNamhyung Kim SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr, 10364abf9869SNamhyung Kim unsigned long, data) 1037481bed45SChristoph Hellwig { 1038481bed45SChristoph Hellwig struct task_struct *child; 1039481bed45SChristoph Hellwig long ret; 1040481bed45SChristoph Hellwig 10416b9c7ed8SChristoph Hellwig if (request == PTRACE_TRACEME) { 10426b9c7ed8SChristoph Hellwig ret = ptrace_traceme(); 10436ea6dd93SHaavard Skinnemoen if (!ret) 10446ea6dd93SHaavard Skinnemoen arch_ptrace_attach(current); 1045481bed45SChristoph Hellwig goto out; 10466b9c7ed8SChristoph Hellwig } 10476b9c7ed8SChristoph Hellwig 10486b9c7ed8SChristoph Hellwig child = ptrace_get_task_struct(pid); 10496b9c7ed8SChristoph Hellwig if (IS_ERR(child)) { 10506b9c7ed8SChristoph Hellwig ret = PTR_ERR(child); 10516b9c7ed8SChristoph Hellwig goto out; 10526b9c7ed8SChristoph Hellwig } 1053481bed45SChristoph Hellwig 10543544d72aSTejun Heo if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) { 1055aa9147c9SDenys Vlasenko ret = ptrace_attach(child, request, addr, data); 10560ac15559SChristoph Hellwig /* 10570ac15559SChristoph Hellwig * Some architectures need to do book-keeping after 10580ac15559SChristoph Hellwig * a ptrace attach. 10590ac15559SChristoph Hellwig */ 10600ac15559SChristoph Hellwig if (!ret) 10610ac15559SChristoph Hellwig arch_ptrace_attach(child); 1062005f18dfSChristoph Hellwig goto out_put_task_struct; 1063481bed45SChristoph Hellwig } 1064481bed45SChristoph Hellwig 1065fca26f26STejun Heo ret = ptrace_check_attach(child, request == PTRACE_KILL || 1066fca26f26STejun Heo request == PTRACE_INTERRUPT); 1067481bed45SChristoph Hellwig if (ret < 0) 1068481bed45SChristoph Hellwig goto out_put_task_struct; 1069481bed45SChristoph Hellwig 1070481bed45SChristoph Hellwig ret = arch_ptrace(child, request, addr, data); 10719899d11fSOleg Nesterov if (ret || request != PTRACE_DETACH) 10729899d11fSOleg Nesterov ptrace_unfreeze_traced(child); 1073481bed45SChristoph Hellwig 1074481bed45SChristoph Hellwig out_put_task_struct: 1075481bed45SChristoph Hellwig put_task_struct(child); 1076481bed45SChristoph Hellwig out: 1077481bed45SChristoph Hellwig return ret; 1078481bed45SChristoph Hellwig } 107976647323SAlexey Dobriyan 10804abf9869SNamhyung Kim int generic_ptrace_peekdata(struct task_struct *tsk, unsigned long addr, 10814abf9869SNamhyung Kim unsigned long data) 108276647323SAlexey Dobriyan { 108376647323SAlexey Dobriyan unsigned long tmp; 108476647323SAlexey Dobriyan int copied; 108576647323SAlexey Dobriyan 108676647323SAlexey Dobriyan copied = access_process_vm(tsk, addr, &tmp, sizeof(tmp), 0); 108776647323SAlexey Dobriyan if (copied != sizeof(tmp)) 108876647323SAlexey Dobriyan return -EIO; 108976647323SAlexey Dobriyan return put_user(tmp, (unsigned long __user *)data); 109076647323SAlexey Dobriyan } 1091f284ce72SAlexey Dobriyan 10924abf9869SNamhyung Kim int generic_ptrace_pokedata(struct task_struct *tsk, unsigned long addr, 10934abf9869SNamhyung Kim unsigned long data) 1094f284ce72SAlexey Dobriyan { 1095f284ce72SAlexey Dobriyan int copied; 1096f284ce72SAlexey Dobriyan 1097f284ce72SAlexey Dobriyan copied = access_process_vm(tsk, addr, &data, sizeof(data), 1); 1098f284ce72SAlexey Dobriyan return (copied == sizeof(data)) ? 0 : -EIO; 1099f284ce72SAlexey Dobriyan } 1100032d82d9SRoland McGrath 110196b8936aSChristoph Hellwig #if defined CONFIG_COMPAT 1102032d82d9SRoland McGrath #include <linux/compat.h> 1103032d82d9SRoland McGrath 1104032d82d9SRoland McGrath int compat_ptrace_request(struct task_struct *child, compat_long_t request, 1105032d82d9SRoland McGrath compat_ulong_t addr, compat_ulong_t data) 1106032d82d9SRoland McGrath { 1107032d82d9SRoland McGrath compat_ulong_t __user *datap = compat_ptr(data); 1108032d82d9SRoland McGrath compat_ulong_t word; 1109e16b2781SRoland McGrath siginfo_t siginfo; 1110032d82d9SRoland McGrath int ret; 1111032d82d9SRoland McGrath 1112032d82d9SRoland McGrath switch (request) { 1113032d82d9SRoland McGrath case PTRACE_PEEKTEXT: 1114032d82d9SRoland McGrath case PTRACE_PEEKDATA: 1115032d82d9SRoland McGrath ret = access_process_vm(child, addr, &word, sizeof(word), 0); 1116032d82d9SRoland McGrath if (ret != sizeof(word)) 1117032d82d9SRoland McGrath ret = -EIO; 1118032d82d9SRoland McGrath else 1119032d82d9SRoland McGrath ret = put_user(word, datap); 1120032d82d9SRoland McGrath break; 1121032d82d9SRoland McGrath 1122032d82d9SRoland McGrath case PTRACE_POKETEXT: 1123032d82d9SRoland McGrath case PTRACE_POKEDATA: 1124032d82d9SRoland McGrath ret = access_process_vm(child, addr, &data, sizeof(data), 1); 1125032d82d9SRoland McGrath ret = (ret != sizeof(data) ? -EIO : 0); 1126032d82d9SRoland McGrath break; 1127032d82d9SRoland McGrath 1128032d82d9SRoland McGrath case PTRACE_GETEVENTMSG: 1129032d82d9SRoland McGrath ret = put_user((compat_ulong_t) child->ptrace_message, datap); 1130032d82d9SRoland McGrath break; 1131032d82d9SRoland McGrath 1132e16b2781SRoland McGrath case PTRACE_GETSIGINFO: 1133e16b2781SRoland McGrath ret = ptrace_getsiginfo(child, &siginfo); 1134e16b2781SRoland McGrath if (!ret) 1135e16b2781SRoland McGrath ret = copy_siginfo_to_user32( 1136e16b2781SRoland McGrath (struct compat_siginfo __user *) datap, 1137e16b2781SRoland McGrath &siginfo); 1138e16b2781SRoland McGrath break; 1139e16b2781SRoland McGrath 1140e16b2781SRoland McGrath case PTRACE_SETSIGINFO: 1141e16b2781SRoland McGrath memset(&siginfo, 0, sizeof siginfo); 1142e16b2781SRoland McGrath if (copy_siginfo_from_user32( 1143e16b2781SRoland McGrath &siginfo, (struct compat_siginfo __user *) datap)) 1144e16b2781SRoland McGrath ret = -EFAULT; 1145e16b2781SRoland McGrath else 1146e16b2781SRoland McGrath ret = ptrace_setsiginfo(child, &siginfo); 1147e16b2781SRoland McGrath break; 11482225a122SSuresh Siddha #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 11492225a122SSuresh Siddha case PTRACE_GETREGSET: 11502225a122SSuresh Siddha case PTRACE_SETREGSET: 11512225a122SSuresh Siddha { 11522225a122SSuresh Siddha struct iovec kiov; 11532225a122SSuresh Siddha struct compat_iovec __user *uiov = 11542225a122SSuresh Siddha (struct compat_iovec __user *) datap; 11552225a122SSuresh Siddha compat_uptr_t ptr; 11562225a122SSuresh Siddha compat_size_t len; 11572225a122SSuresh Siddha 11582225a122SSuresh Siddha if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov))) 11592225a122SSuresh Siddha return -EFAULT; 11602225a122SSuresh Siddha 11612225a122SSuresh Siddha if (__get_user(ptr, &uiov->iov_base) || 11622225a122SSuresh Siddha __get_user(len, &uiov->iov_len)) 11632225a122SSuresh Siddha return -EFAULT; 11642225a122SSuresh Siddha 11652225a122SSuresh Siddha kiov.iov_base = compat_ptr(ptr); 11662225a122SSuresh Siddha kiov.iov_len = len; 11672225a122SSuresh Siddha 11682225a122SSuresh Siddha ret = ptrace_regset(child, request, addr, &kiov); 11692225a122SSuresh Siddha if (!ret) 11702225a122SSuresh Siddha ret = __put_user(kiov.iov_len, &uiov->iov_len); 11712225a122SSuresh Siddha break; 11722225a122SSuresh Siddha } 11732225a122SSuresh Siddha #endif 1174e16b2781SRoland McGrath 1175032d82d9SRoland McGrath default: 1176032d82d9SRoland McGrath ret = ptrace_request(child, request, addr, data); 1177032d82d9SRoland McGrath } 1178032d82d9SRoland McGrath 1179032d82d9SRoland McGrath return ret; 1180032d82d9SRoland McGrath } 1181c269f196SRoland McGrath 1182c269f196SRoland McGrath asmlinkage long compat_sys_ptrace(compat_long_t request, compat_long_t pid, 1183c269f196SRoland McGrath compat_long_t addr, compat_long_t data) 1184c269f196SRoland McGrath { 1185c269f196SRoland McGrath struct task_struct *child; 1186c269f196SRoland McGrath long ret; 1187c269f196SRoland McGrath 1188c269f196SRoland McGrath if (request == PTRACE_TRACEME) { 1189c269f196SRoland McGrath ret = ptrace_traceme(); 1190c269f196SRoland McGrath goto out; 1191c269f196SRoland McGrath } 1192c269f196SRoland McGrath 1193c269f196SRoland McGrath child = ptrace_get_task_struct(pid); 1194c269f196SRoland McGrath if (IS_ERR(child)) { 1195c269f196SRoland McGrath ret = PTR_ERR(child); 1196c269f196SRoland McGrath goto out; 1197c269f196SRoland McGrath } 1198c269f196SRoland McGrath 11993544d72aSTejun Heo if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) { 1200aa9147c9SDenys Vlasenko ret = ptrace_attach(child, request, addr, data); 1201c269f196SRoland McGrath /* 1202c269f196SRoland McGrath * Some architectures need to do book-keeping after 1203c269f196SRoland McGrath * a ptrace attach. 1204c269f196SRoland McGrath */ 1205c269f196SRoland McGrath if (!ret) 1206c269f196SRoland McGrath arch_ptrace_attach(child); 1207c269f196SRoland McGrath goto out_put_task_struct; 1208c269f196SRoland McGrath } 1209c269f196SRoland McGrath 1210fca26f26STejun Heo ret = ptrace_check_attach(child, request == PTRACE_KILL || 1211fca26f26STejun Heo request == PTRACE_INTERRUPT); 12129899d11fSOleg Nesterov if (!ret) { 1213c269f196SRoland McGrath ret = compat_arch_ptrace(child, request, addr, data); 12149899d11fSOleg Nesterov if (ret || request != PTRACE_DETACH) 12159899d11fSOleg Nesterov ptrace_unfreeze_traced(child); 12169899d11fSOleg Nesterov } 1217c269f196SRoland McGrath 1218c269f196SRoland McGrath out_put_task_struct: 1219c269f196SRoland McGrath put_task_struct(child); 1220c269f196SRoland McGrath out: 1221c269f196SRoland McGrath return ret; 1222c269f196SRoland McGrath } 122396b8936aSChristoph Hellwig #endif /* CONFIG_COMPAT */ 1224bf26c018SFrederic Weisbecker 1225bf26c018SFrederic Weisbecker #ifdef CONFIG_HAVE_HW_BREAKPOINT 1226bf26c018SFrederic Weisbecker int ptrace_get_breakpoints(struct task_struct *tsk) 1227bf26c018SFrederic Weisbecker { 1228bf26c018SFrederic Weisbecker if (atomic_inc_not_zero(&tsk->ptrace_bp_refcnt)) 1229bf26c018SFrederic Weisbecker return 0; 1230bf26c018SFrederic Weisbecker 1231bf26c018SFrederic Weisbecker return -1; 1232bf26c018SFrederic Weisbecker } 1233bf26c018SFrederic Weisbecker 1234bf26c018SFrederic Weisbecker void ptrace_put_breakpoints(struct task_struct *tsk) 1235bf26c018SFrederic Weisbecker { 1236bf26c018SFrederic Weisbecker if (atomic_dec_and_test(&tsk->ptrace_bp_refcnt)) 1237bf26c018SFrederic Weisbecker flush_ptrace_hw_breakpoint(tsk); 1238bf26c018SFrederic Weisbecker } 1239bf26c018SFrederic Weisbecker #endif /* CONFIG_HAVE_HW_BREAKPOINT */ 1240