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> 111da177e4SLinus Torvalds #include <linux/module.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> 20a5cb013dSAl Viro #include <linux/audit.h> 21b488893aSPavel Emelyanov #include <linux/pid_namespace.h> 22f17d30a8SAdrian Bunk #include <linux/syscalls.h> 233a709703SRoland McGrath #include <linux/uaccess.h> 242225a122SSuresh Siddha #include <linux/regset.h> 25bf26c018SFrederic Weisbecker #include <linux/hw_breakpoint.h> 261da177e4SLinus Torvalds 27bf53de90SMarkus Metzger 2862c124ffSTejun Heo static int ptrace_trapping_sleep_fn(void *flags) 2962c124ffSTejun Heo { 3062c124ffSTejun Heo schedule(); 3162c124ffSTejun Heo return 0; 3262c124ffSTejun Heo } 3362c124ffSTejun Heo 34bf53de90SMarkus Metzger /* 351da177e4SLinus Torvalds * ptrace a task: make the debugger its new parent and 361da177e4SLinus Torvalds * move it to the ptrace list. 371da177e4SLinus Torvalds * 381da177e4SLinus Torvalds * Must be called with the tasklist lock write-held. 391da177e4SLinus Torvalds */ 4036c8b586SIngo Molnar void __ptrace_link(struct task_struct *child, struct task_struct *new_parent) 411da177e4SLinus Torvalds { 42f470021aSRoland McGrath BUG_ON(!list_empty(&child->ptrace_entry)); 43f470021aSRoland McGrath list_add(&child->ptrace_entry, &new_parent->ptraced); 441da177e4SLinus Torvalds child->parent = new_parent; 451da177e4SLinus Torvalds } 461da177e4SLinus Torvalds 47e3bd058fSTejun Heo /** 48e3bd058fSTejun Heo * __ptrace_unlink - unlink ptracee and restore its execution state 49e3bd058fSTejun Heo * @child: ptracee to be unlinked 501da177e4SLinus Torvalds * 510e9f0a4aSTejun Heo * Remove @child from the ptrace list, move it back to the original parent, 520e9f0a4aSTejun Heo * and restore the execution state so that it conforms to the group stop 530e9f0a4aSTejun Heo * state. 540e9f0a4aSTejun Heo * 550e9f0a4aSTejun Heo * Unlinking can happen via two paths - explicit PTRACE_DETACH or ptracer 560e9f0a4aSTejun Heo * exiting. For PTRACE_DETACH, unless the ptracee has been killed between 570e9f0a4aSTejun Heo * ptrace_check_attach() and here, it's guaranteed to be in TASK_TRACED. 580e9f0a4aSTejun Heo * If the ptracer is exiting, the ptracee can be in any state. 590e9f0a4aSTejun Heo * 600e9f0a4aSTejun Heo * After detach, the ptracee should be in a state which conforms to the 610e9f0a4aSTejun Heo * group stop. If the group is stopped or in the process of stopping, the 620e9f0a4aSTejun Heo * ptracee should be put into TASK_STOPPED; otherwise, it should be woken 630e9f0a4aSTejun Heo * up from TASK_TRACED. 640e9f0a4aSTejun Heo * 650e9f0a4aSTejun Heo * If the ptracee is in TASK_TRACED and needs to be moved to TASK_STOPPED, 660e9f0a4aSTejun Heo * it goes through TRACED -> RUNNING -> STOPPED transition which is similar 670e9f0a4aSTejun Heo * to but in the opposite direction of what happens while attaching to a 680e9f0a4aSTejun Heo * stopped task. However, in this direction, the intermediate RUNNING 690e9f0a4aSTejun Heo * state is not hidden even from the current ptracer and if it immediately 700e9f0a4aSTejun Heo * re-attaches and performs a WNOHANG wait(2), it may fail. 71e3bd058fSTejun Heo * 72e3bd058fSTejun Heo * CONTEXT: 73e3bd058fSTejun Heo * write_lock_irq(tasklist_lock) 741da177e4SLinus Torvalds */ 7536c8b586SIngo Molnar void __ptrace_unlink(struct task_struct *child) 761da177e4SLinus Torvalds { 775ecfbae0SOleg Nesterov BUG_ON(!child->ptrace); 785ecfbae0SOleg Nesterov 791da177e4SLinus Torvalds child->ptrace = 0; 801da177e4SLinus Torvalds child->parent = child->real_parent; 81f470021aSRoland McGrath list_del_init(&child->ptrace_entry); 821da177e4SLinus Torvalds 831da177e4SLinus Torvalds spin_lock(&child->sighand->siglock); 840e9f0a4aSTejun Heo 851da177e4SLinus Torvalds /* 8673ddff2bSTejun Heo * Clear all pending traps and TRAPPING. TRAPPING should be 8773ddff2bSTejun Heo * cleared regardless of JOBCTL_STOP_PENDING. Do it explicitly. 8873ddff2bSTejun Heo */ 8973ddff2bSTejun Heo task_clear_jobctl_pending(child, JOBCTL_TRAP_MASK); 9073ddff2bSTejun Heo task_clear_jobctl_trapping(child); 9173ddff2bSTejun Heo 9273ddff2bSTejun Heo /* 93a8f072c1STejun Heo * Reinstate JOBCTL_STOP_PENDING if group stop is in effect and 940e9f0a4aSTejun Heo * @child isn't dead. 951da177e4SLinus Torvalds */ 960e9f0a4aSTejun Heo if (!(child->flags & PF_EXITING) && 970e9f0a4aSTejun Heo (child->signal->flags & SIGNAL_STOP_STOPPED || 980e9f0a4aSTejun Heo child->signal->group_stop_count)) 99a8f072c1STejun Heo child->jobctl |= JOBCTL_STOP_PENDING; 1000e9f0a4aSTejun Heo 1010e9f0a4aSTejun Heo /* 1020e9f0a4aSTejun Heo * If transition to TASK_STOPPED is pending or in TASK_TRACED, kick 1030e9f0a4aSTejun Heo * @child in the butt. Note that @resume should be used iff @child 1040e9f0a4aSTejun Heo * is in TASK_TRACED; otherwise, we might unduly disrupt 1050e9f0a4aSTejun Heo * TASK_KILLABLE sleeps. 1060e9f0a4aSTejun Heo */ 107a8f072c1STejun Heo if (child->jobctl & JOBCTL_STOP_PENDING || task_is_traced(child)) 1080e9f0a4aSTejun Heo signal_wake_up(child, task_is_traced(child)); 1090e9f0a4aSTejun Heo 1101da177e4SLinus Torvalds spin_unlock(&child->sighand->siglock); 1111da177e4SLinus Torvalds } 1121da177e4SLinus Torvalds 113755e276bSTejun Heo /** 114755e276bSTejun Heo * ptrace_check_attach - check whether ptracee is ready for ptrace operation 115755e276bSTejun Heo * @child: ptracee to check for 116755e276bSTejun Heo * @ignore_state: don't check whether @child is currently %TASK_TRACED 117755e276bSTejun Heo * 118755e276bSTejun Heo * Check whether @child is being ptraced by %current and ready for further 119755e276bSTejun Heo * ptrace operations. If @ignore_state is %false, @child also should be in 120755e276bSTejun Heo * %TASK_TRACED state and on return the child is guaranteed to be traced 121755e276bSTejun Heo * and not executing. If @ignore_state is %true, @child can be in any 122755e276bSTejun Heo * state. 123755e276bSTejun Heo * 124755e276bSTejun Heo * CONTEXT: 125755e276bSTejun Heo * Grabs and releases tasklist_lock and @child->sighand->siglock. 126755e276bSTejun Heo * 127755e276bSTejun Heo * RETURNS: 128755e276bSTejun Heo * 0 on success, -ESRCH if %child is not ready. 1291da177e4SLinus Torvalds */ 130755e276bSTejun Heo int ptrace_check_attach(struct task_struct *child, bool ignore_state) 1311da177e4SLinus Torvalds { 1321da177e4SLinus Torvalds int ret = -ESRCH; 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds /* 1351da177e4SLinus Torvalds * We take the read lock around doing both checks to close a 1361da177e4SLinus Torvalds * possible race where someone else was tracing our child and 1371da177e4SLinus Torvalds * detached between these two checks. After this locked check, 1381da177e4SLinus Torvalds * we are sure that this is our traced child and that can only 1391da177e4SLinus Torvalds * be changed by us so it's not changing right after this. 1401da177e4SLinus Torvalds */ 1411da177e4SLinus Torvalds read_lock(&tasklist_lock); 142c0c0b649SOleg Nesterov if ((child->ptrace & PT_PTRACED) && child->parent == current) { 143c0c0b649SOleg Nesterov /* 144c0c0b649SOleg Nesterov * child->sighand can't be NULL, release_task() 145c0c0b649SOleg Nesterov * does ptrace_unlink() before __exit_signal(). 146c0c0b649SOleg Nesterov */ 1471da177e4SLinus Torvalds spin_lock_irq(&child->sighand->siglock); 148321fb561SOleg Nesterov WARN_ON_ONCE(task_is_stopped(child)); 149755e276bSTejun Heo if (task_is_traced(child) || ignore_state) 150321fb561SOleg Nesterov ret = 0; 1511da177e4SLinus Torvalds spin_unlock_irq(&child->sighand->siglock); 1521da177e4SLinus Torvalds } 1531da177e4SLinus Torvalds read_unlock(&tasklist_lock); 1541da177e4SLinus Torvalds 155755e276bSTejun Heo if (!ret && !ignore_state) 15685ba2d86SRoland McGrath ret = wait_task_inactive(child, TASK_TRACED) ? 0 : -ESRCH; 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds /* All systems go.. */ 1591da177e4SLinus Torvalds return ret; 1601da177e4SLinus Torvalds } 1611da177e4SLinus Torvalds 162006ebb40SStephen Smalley int __ptrace_may_access(struct task_struct *task, unsigned int mode) 163ab8d11beSMiklos Szeredi { 164c69e8d9cSDavid Howells const struct cred *cred = current_cred(), *tcred; 165b6dff3ecSDavid Howells 166df26c40eSEric W. Biederman /* May we inspect the given task? 167df26c40eSEric W. Biederman * This check is used both for attaching with ptrace 168df26c40eSEric W. Biederman * and for allowing access to sensitive information in /proc. 169df26c40eSEric W. Biederman * 170df26c40eSEric W. Biederman * ptrace_attach denies several cases that /proc allows 171df26c40eSEric W. Biederman * because setting up the necessary parent/child relationship 172df26c40eSEric W. Biederman * or halting the specified task is impossible. 173df26c40eSEric W. Biederman */ 174df26c40eSEric W. Biederman int dumpable = 0; 175df26c40eSEric W. Biederman /* Don't let security modules deny introspection */ 176df26c40eSEric W. Biederman if (task == current) 177df26c40eSEric W. Biederman return 0; 178c69e8d9cSDavid Howells rcu_read_lock(); 179c69e8d9cSDavid Howells tcred = __task_cred(task); 1808409cca7SSerge E. Hallyn if (cred->user->user_ns == tcred->user->user_ns && 1818409cca7SSerge E. Hallyn (cred->uid == tcred->euid && 1828409cca7SSerge E. Hallyn cred->uid == tcred->suid && 1838409cca7SSerge E. Hallyn cred->uid == tcred->uid && 1848409cca7SSerge E. Hallyn cred->gid == tcred->egid && 1858409cca7SSerge E. Hallyn cred->gid == tcred->sgid && 1868409cca7SSerge E. Hallyn cred->gid == tcred->gid)) 1878409cca7SSerge E. Hallyn goto ok; 1888409cca7SSerge E. Hallyn if (ns_capable(tcred->user->user_ns, CAP_SYS_PTRACE)) 1898409cca7SSerge E. Hallyn goto ok; 190c69e8d9cSDavid Howells rcu_read_unlock(); 191ab8d11beSMiklos Szeredi return -EPERM; 1928409cca7SSerge E. Hallyn ok: 193c69e8d9cSDavid Howells rcu_read_unlock(); 194ab8d11beSMiklos Szeredi smp_rmb(); 195df26c40eSEric W. Biederman if (task->mm) 1966c5d5238SKawai, Hidehiro dumpable = get_dumpable(task->mm); 1978409cca7SSerge E. Hallyn if (!dumpable && !task_ns_capable(task, CAP_SYS_PTRACE)) 198ab8d11beSMiklos Szeredi return -EPERM; 199ab8d11beSMiklos Szeredi 2009e48858fSIngo Molnar return security_ptrace_access_check(task, mode); 201ab8d11beSMiklos Szeredi } 202ab8d11beSMiklos Szeredi 203006ebb40SStephen Smalley bool ptrace_may_access(struct task_struct *task, unsigned int mode) 204ab8d11beSMiklos Szeredi { 205ab8d11beSMiklos Szeredi int err; 206ab8d11beSMiklos Szeredi task_lock(task); 207006ebb40SStephen Smalley err = __ptrace_may_access(task, mode); 208ab8d11beSMiklos Szeredi task_unlock(task); 2093a709703SRoland McGrath return !err; 210ab8d11beSMiklos Szeredi } 211ab8d11beSMiklos Szeredi 2123544d72aSTejun Heo static int ptrace_attach(struct task_struct *task, long request, 2133544d72aSTejun Heo unsigned long flags) 2141da177e4SLinus Torvalds { 2153544d72aSTejun Heo bool seize = (request == PTRACE_SEIZE); 2161da177e4SLinus Torvalds int retval; 217f5b40e36SLinus Torvalds 2183544d72aSTejun Heo /* 2193544d72aSTejun Heo * SEIZE will enable new ptrace behaviors which will be implemented 2203544d72aSTejun Heo * gradually. SEIZE_DEVEL is used to prevent applications 2213544d72aSTejun Heo * expecting full SEIZE behaviors trapping on kernel commits which 2223544d72aSTejun Heo * are still in the process of implementing them. 2233544d72aSTejun Heo * 2243544d72aSTejun Heo * Only test programs for new ptrace behaviors being implemented 2253544d72aSTejun Heo * should set SEIZE_DEVEL. If unset, SEIZE will fail with -EIO. 2263544d72aSTejun Heo * 2273544d72aSTejun Heo * Once SEIZE behaviors are completely implemented, this flag and 2283544d72aSTejun Heo * the following test will be removed. 2293544d72aSTejun Heo */ 2303544d72aSTejun Heo retval = -EIO; 2313544d72aSTejun Heo if (seize && !(flags & PTRACE_SEIZE_DEVEL)) 2323544d72aSTejun Heo goto out; 2333544d72aSTejun Heo 234a5cb013dSAl Viro audit_ptrace(task); 235a5cb013dSAl Viro 2361da177e4SLinus Torvalds retval = -EPERM; 237b79b7ba9SOleg Nesterov if (unlikely(task->flags & PF_KTHREAD)) 238b79b7ba9SOleg Nesterov goto out; 239bac0abd6SPavel Emelyanov if (same_thread_group(task, current)) 240f5b40e36SLinus Torvalds goto out; 241f5b40e36SLinus Torvalds 242f2f0b00aSOleg Nesterov /* 243f2f0b00aSOleg Nesterov * Protect exec's credential calculations against our interference; 2445e751e99SDavid Howells * interference; SUID, SGID and LSM creds get determined differently 2455e751e99SDavid Howells * under ptrace. 246d84f4f99SDavid Howells */ 247793285fcSOleg Nesterov retval = -ERESTARTNOINTR; 2489b1bf12dSKOSAKI Motohiro if (mutex_lock_interruptible(&task->signal->cred_guard_mutex)) 249d84f4f99SDavid Howells goto out; 2504b105cbbSOleg Nesterov 251f5b40e36SLinus Torvalds task_lock(task); 252006ebb40SStephen Smalley retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH); 2534b105cbbSOleg Nesterov task_unlock(task); 2541da177e4SLinus Torvalds if (retval) 2554b105cbbSOleg Nesterov goto unlock_creds; 2561da177e4SLinus Torvalds 2574b105cbbSOleg Nesterov write_lock_irq(&tasklist_lock); 258b79b7ba9SOleg Nesterov retval = -EPERM; 259b79b7ba9SOleg Nesterov if (unlikely(task->exit_state)) 2604b105cbbSOleg Nesterov goto unlock_tasklist; 261f2f0b00aSOleg Nesterov if (task->ptrace) 2624b105cbbSOleg Nesterov goto unlock_tasklist; 263b79b7ba9SOleg Nesterov 264f2f0b00aSOleg Nesterov task->ptrace = PT_PTRACED; 2653544d72aSTejun Heo if (seize) 2663544d72aSTejun Heo task->ptrace |= PT_SEIZED; 2678409cca7SSerge E. Hallyn if (task_ns_capable(task, CAP_SYS_PTRACE)) 2681da177e4SLinus Torvalds task->ptrace |= PT_PTRACE_CAP; 2691da177e4SLinus Torvalds 2701da177e4SLinus Torvalds __ptrace_link(task, current); 2713544d72aSTejun Heo 2723544d72aSTejun Heo /* SEIZE doesn't trap tracee on attach */ 2733544d72aSTejun Heo if (!seize) 27433e9fc7dSOleg Nesterov send_sig_info(SIGSTOP, SEND_SIG_FORCED, task); 275b79b7ba9SOleg Nesterov 276d79fdd6dSTejun Heo spin_lock(&task->sighand->siglock); 277d79fdd6dSTejun Heo 278d79fdd6dSTejun Heo /* 27973ddff2bSTejun Heo * If the task is already STOPPED, set JOBCTL_TRAP_STOP and 280d79fdd6dSTejun Heo * TRAPPING, and kick it so that it transits to TRACED. TRAPPING 281d79fdd6dSTejun Heo * will be cleared if the child completes the transition or any 282d79fdd6dSTejun Heo * event which clears the group stop states happens. We'll wait 283d79fdd6dSTejun Heo * for the transition to complete before returning from this 284d79fdd6dSTejun Heo * function. 285d79fdd6dSTejun Heo * 286d79fdd6dSTejun Heo * This hides STOPPED -> RUNNING -> TRACED transition from the 287d79fdd6dSTejun Heo * attaching thread but a different thread in the same group can 288d79fdd6dSTejun Heo * still observe the transient RUNNING state. IOW, if another 289d79fdd6dSTejun Heo * thread's WNOHANG wait(2) on the stopped tracee races against 290d79fdd6dSTejun Heo * ATTACH, the wait(2) may fail due to the transient RUNNING. 291d79fdd6dSTejun Heo * 292d79fdd6dSTejun Heo * The following task_is_stopped() test is safe as both transitions 293d79fdd6dSTejun Heo * in and out of STOPPED are protected by siglock. 294d79fdd6dSTejun Heo */ 2957dd3db54STejun Heo if (task_is_stopped(task) && 29673ddff2bSTejun Heo task_set_jobctl_pending(task, JOBCTL_TRAP_STOP | JOBCTL_TRAPPING)) 297d79fdd6dSTejun Heo signal_wake_up(task, 1); 298d79fdd6dSTejun Heo 299d79fdd6dSTejun Heo spin_unlock(&task->sighand->siglock); 300d79fdd6dSTejun Heo 301b79b7ba9SOleg Nesterov retval = 0; 3024b105cbbSOleg Nesterov unlock_tasklist: 3034b105cbbSOleg Nesterov write_unlock_irq(&tasklist_lock); 3044b105cbbSOleg Nesterov unlock_creds: 3059b1bf12dSKOSAKI Motohiro mutex_unlock(&task->signal->cred_guard_mutex); 306f5b40e36SLinus Torvalds out: 3070b1007c3STejun Heo if (!retval) 30862c124ffSTejun Heo wait_on_bit(&task->jobctl, JOBCTL_TRAPPING_BIT, 30962c124ffSTejun Heo ptrace_trapping_sleep_fn, TASK_UNINTERRUPTIBLE); 3101da177e4SLinus Torvalds return retval; 3111da177e4SLinus Torvalds } 3121da177e4SLinus Torvalds 313f2f0b00aSOleg Nesterov /** 314f2f0b00aSOleg Nesterov * ptrace_traceme -- helper for PTRACE_TRACEME 315f2f0b00aSOleg Nesterov * 316f2f0b00aSOleg Nesterov * Performs checks and sets PT_PTRACED. 317f2f0b00aSOleg Nesterov * Should be used by all ptrace implementations for PTRACE_TRACEME. 318f2f0b00aSOleg Nesterov */ 319e3e89cc5SLinus Torvalds static int ptrace_traceme(void) 320f2f0b00aSOleg Nesterov { 321f2f0b00aSOleg Nesterov int ret = -EPERM; 322f2f0b00aSOleg Nesterov 3234b105cbbSOleg Nesterov write_lock_irq(&tasklist_lock); 3244b105cbbSOleg Nesterov /* Are we already being traced? */ 325f2f0b00aSOleg Nesterov if (!current->ptrace) { 326f2f0b00aSOleg Nesterov ret = security_ptrace_traceme(current->parent); 327f2f0b00aSOleg Nesterov /* 328f2f0b00aSOleg Nesterov * Check PF_EXITING to ensure ->real_parent has not passed 329f2f0b00aSOleg Nesterov * exit_ptrace(). Otherwise we don't report the error but 330f2f0b00aSOleg Nesterov * pretend ->real_parent untraces us right after return. 331f2f0b00aSOleg Nesterov */ 332f2f0b00aSOleg Nesterov if (!ret && !(current->real_parent->flags & PF_EXITING)) { 333f2f0b00aSOleg Nesterov current->ptrace = PT_PTRACED; 334f2f0b00aSOleg Nesterov __ptrace_link(current, current->real_parent); 335f2f0b00aSOleg Nesterov } 336f2f0b00aSOleg Nesterov } 3374b105cbbSOleg Nesterov write_unlock_irq(&tasklist_lock); 3384b105cbbSOleg Nesterov 339f2f0b00aSOleg Nesterov return ret; 340f2f0b00aSOleg Nesterov } 341f2f0b00aSOleg Nesterov 34239c626aeSOleg Nesterov /* 34339c626aeSOleg Nesterov * Called with irqs disabled, returns true if childs should reap themselves. 34439c626aeSOleg Nesterov */ 34539c626aeSOleg Nesterov static int ignoring_children(struct sighand_struct *sigh) 34639c626aeSOleg Nesterov { 34739c626aeSOleg Nesterov int ret; 34839c626aeSOleg Nesterov spin_lock(&sigh->siglock); 34939c626aeSOleg Nesterov ret = (sigh->action[SIGCHLD-1].sa.sa_handler == SIG_IGN) || 35039c626aeSOleg Nesterov (sigh->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT); 35139c626aeSOleg Nesterov spin_unlock(&sigh->siglock); 35239c626aeSOleg Nesterov return ret; 35339c626aeSOleg Nesterov } 35439c626aeSOleg Nesterov 35539c626aeSOleg Nesterov /* 35639c626aeSOleg Nesterov * Called with tasklist_lock held for writing. 35739c626aeSOleg Nesterov * Unlink a traced task, and clean it up if it was a traced zombie. 35839c626aeSOleg Nesterov * Return true if it needs to be reaped with release_task(). 35939c626aeSOleg Nesterov * (We can't call release_task() here because we already hold tasklist_lock.) 36039c626aeSOleg Nesterov * 36139c626aeSOleg Nesterov * If it's a zombie, our attachedness prevented normal parent notification 36239c626aeSOleg Nesterov * or self-reaping. Do notification now if it would have happened earlier. 36339c626aeSOleg Nesterov * If it should reap itself, return true. 36439c626aeSOleg Nesterov * 365a7f0765eSOleg Nesterov * If it's our own child, there is no notification to do. But if our normal 366a7f0765eSOleg Nesterov * children self-reap, then this child was prevented by ptrace and we must 367a7f0765eSOleg Nesterov * reap it now, in that case we must also wake up sub-threads sleeping in 368a7f0765eSOleg Nesterov * do_wait(). 36939c626aeSOleg Nesterov */ 37039c626aeSOleg Nesterov static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p) 37139c626aeSOleg Nesterov { 37239c626aeSOleg Nesterov __ptrace_unlink(p); 37339c626aeSOleg Nesterov 37439c626aeSOleg Nesterov if (p->exit_state == EXIT_ZOMBIE) { 37539c626aeSOleg Nesterov if (!task_detached(p) && thread_group_empty(p)) { 37639c626aeSOleg Nesterov if (!same_thread_group(p->real_parent, tracer)) 37739c626aeSOleg Nesterov do_notify_parent(p, p->exit_signal); 378a7f0765eSOleg Nesterov else if (ignoring_children(tracer->sighand)) { 379a7f0765eSOleg Nesterov __wake_up_parent(p, tracer); 38039c626aeSOleg Nesterov p->exit_signal = -1; 38139c626aeSOleg Nesterov } 382a7f0765eSOleg Nesterov } 38339c626aeSOleg Nesterov if (task_detached(p)) { 38439c626aeSOleg Nesterov /* Mark it as in the process of being reaped. */ 38539c626aeSOleg Nesterov p->exit_state = EXIT_DEAD; 38639c626aeSOleg Nesterov return true; 38739c626aeSOleg Nesterov } 38839c626aeSOleg Nesterov } 38939c626aeSOleg Nesterov 39039c626aeSOleg Nesterov return false; 39139c626aeSOleg Nesterov } 39239c626aeSOleg Nesterov 393e3e89cc5SLinus Torvalds static int ptrace_detach(struct task_struct *child, unsigned int data) 3941da177e4SLinus Torvalds { 39539c626aeSOleg Nesterov bool dead = false; 3964576145cSOleg Nesterov 3977ed20e1aSJesper Juhl if (!valid_signal(data)) 3981da177e4SLinus Torvalds return -EIO; 3991da177e4SLinus Torvalds 4001da177e4SLinus Torvalds /* Architecture-specific hardware disable .. */ 4011da177e4SLinus Torvalds ptrace_disable(child); 4027d941432SRoland McGrath clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); 4031da177e4SLinus Torvalds 40495c3eb76SOleg Nesterov write_lock_irq(&tasklist_lock); 40539c626aeSOleg Nesterov /* 40639c626aeSOleg Nesterov * This child can be already killed. Make sure de_thread() or 40739c626aeSOleg Nesterov * our sub-thread doing do_wait() didn't do release_task() yet. 40839c626aeSOleg Nesterov */ 40995c3eb76SOleg Nesterov if (child->ptrace) { 41095c3eb76SOleg Nesterov child->exit_code = data; 4114576145cSOleg Nesterov dead = __ptrace_detach(current, child); 41295c3eb76SOleg Nesterov } 4131da177e4SLinus Torvalds write_unlock_irq(&tasklist_lock); 4141da177e4SLinus Torvalds 4154576145cSOleg Nesterov if (unlikely(dead)) 4164576145cSOleg Nesterov release_task(child); 4174576145cSOleg Nesterov 4181da177e4SLinus Torvalds return 0; 4191da177e4SLinus Torvalds } 4201da177e4SLinus Torvalds 42139c626aeSOleg Nesterov /* 422c7e49c14SOleg Nesterov * Detach all tasks we were using ptrace on. Called with tasklist held 423c7e49c14SOleg Nesterov * for writing, and returns with it held too. But note it can release 424c7e49c14SOleg Nesterov * and reacquire the lock. 42539c626aeSOleg Nesterov */ 42639c626aeSOleg Nesterov void exit_ptrace(struct task_struct *tracer) 427c4b5ed25SNamhyung Kim __releases(&tasklist_lock) 428c4b5ed25SNamhyung Kim __acquires(&tasklist_lock) 42939c626aeSOleg Nesterov { 43039c626aeSOleg Nesterov struct task_struct *p, *n; 43139c626aeSOleg Nesterov LIST_HEAD(ptrace_dead); 43239c626aeSOleg Nesterov 433c7e49c14SOleg Nesterov if (likely(list_empty(&tracer->ptraced))) 434c7e49c14SOleg Nesterov return; 435c7e49c14SOleg Nesterov 43639c626aeSOleg Nesterov list_for_each_entry_safe(p, n, &tracer->ptraced, ptrace_entry) { 43739c626aeSOleg Nesterov if (__ptrace_detach(tracer, p)) 43839c626aeSOleg Nesterov list_add(&p->ptrace_entry, &ptrace_dead); 43939c626aeSOleg Nesterov } 44039c626aeSOleg Nesterov 441c7e49c14SOleg Nesterov write_unlock_irq(&tasklist_lock); 44239c626aeSOleg Nesterov BUG_ON(!list_empty(&tracer->ptraced)); 44339c626aeSOleg Nesterov 44439c626aeSOleg Nesterov list_for_each_entry_safe(p, n, &ptrace_dead, ptrace_entry) { 44539c626aeSOleg Nesterov list_del_init(&p->ptrace_entry); 44639c626aeSOleg Nesterov release_task(p); 44739c626aeSOleg Nesterov } 448c7e49c14SOleg Nesterov 449c7e49c14SOleg Nesterov write_lock_irq(&tasklist_lock); 45039c626aeSOleg Nesterov } 45139c626aeSOleg Nesterov 4521da177e4SLinus Torvalds int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len) 4531da177e4SLinus Torvalds { 4541da177e4SLinus Torvalds int copied = 0; 4551da177e4SLinus Torvalds 4561da177e4SLinus Torvalds while (len > 0) { 4571da177e4SLinus Torvalds char buf[128]; 4581da177e4SLinus Torvalds int this_len, retval; 4591da177e4SLinus Torvalds 4601da177e4SLinus Torvalds this_len = (len > sizeof(buf)) ? sizeof(buf) : len; 4611da177e4SLinus Torvalds retval = access_process_vm(tsk, src, buf, this_len, 0); 4621da177e4SLinus Torvalds if (!retval) { 4631da177e4SLinus Torvalds if (copied) 4641da177e4SLinus Torvalds break; 4651da177e4SLinus Torvalds return -EIO; 4661da177e4SLinus Torvalds } 4671da177e4SLinus Torvalds if (copy_to_user(dst, buf, retval)) 4681da177e4SLinus Torvalds return -EFAULT; 4691da177e4SLinus Torvalds copied += retval; 4701da177e4SLinus Torvalds src += retval; 4711da177e4SLinus Torvalds dst += retval; 4721da177e4SLinus Torvalds len -= retval; 4731da177e4SLinus Torvalds } 4741da177e4SLinus Torvalds return copied; 4751da177e4SLinus Torvalds } 4761da177e4SLinus Torvalds 4771da177e4SLinus Torvalds int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len) 4781da177e4SLinus Torvalds { 4791da177e4SLinus Torvalds int copied = 0; 4801da177e4SLinus Torvalds 4811da177e4SLinus Torvalds while (len > 0) { 4821da177e4SLinus Torvalds char buf[128]; 4831da177e4SLinus Torvalds int this_len, retval; 4841da177e4SLinus Torvalds 4851da177e4SLinus Torvalds this_len = (len > sizeof(buf)) ? sizeof(buf) : len; 4861da177e4SLinus Torvalds if (copy_from_user(buf, src, this_len)) 4871da177e4SLinus Torvalds return -EFAULT; 4881da177e4SLinus Torvalds retval = access_process_vm(tsk, dst, buf, this_len, 1); 4891da177e4SLinus Torvalds if (!retval) { 4901da177e4SLinus Torvalds if (copied) 4911da177e4SLinus Torvalds break; 4921da177e4SLinus Torvalds return -EIO; 4931da177e4SLinus Torvalds } 4941da177e4SLinus Torvalds copied += retval; 4951da177e4SLinus Torvalds src += retval; 4961da177e4SLinus Torvalds dst += retval; 4971da177e4SLinus Torvalds len -= retval; 4981da177e4SLinus Torvalds } 4991da177e4SLinus Torvalds return copied; 5001da177e4SLinus Torvalds } 5011da177e4SLinus Torvalds 5024abf9869SNamhyung Kim static int ptrace_setoptions(struct task_struct *child, unsigned long data) 5031da177e4SLinus Torvalds { 5041da177e4SLinus Torvalds child->ptrace &= ~PT_TRACE_MASK; 5051da177e4SLinus Torvalds 5061da177e4SLinus Torvalds if (data & PTRACE_O_TRACESYSGOOD) 5071da177e4SLinus Torvalds child->ptrace |= PT_TRACESYSGOOD; 5081da177e4SLinus Torvalds 5091da177e4SLinus Torvalds if (data & PTRACE_O_TRACEFORK) 5101da177e4SLinus Torvalds child->ptrace |= PT_TRACE_FORK; 5111da177e4SLinus Torvalds 5121da177e4SLinus Torvalds if (data & PTRACE_O_TRACEVFORK) 5131da177e4SLinus Torvalds child->ptrace |= PT_TRACE_VFORK; 5141da177e4SLinus Torvalds 5151da177e4SLinus Torvalds if (data & PTRACE_O_TRACECLONE) 5161da177e4SLinus Torvalds child->ptrace |= PT_TRACE_CLONE; 5171da177e4SLinus Torvalds 5181da177e4SLinus Torvalds if (data & PTRACE_O_TRACEEXEC) 5191da177e4SLinus Torvalds child->ptrace |= PT_TRACE_EXEC; 5201da177e4SLinus Torvalds 5211da177e4SLinus Torvalds if (data & PTRACE_O_TRACEVFORKDONE) 5221da177e4SLinus Torvalds child->ptrace |= PT_TRACE_VFORK_DONE; 5231da177e4SLinus Torvalds 5241da177e4SLinus Torvalds if (data & PTRACE_O_TRACEEXIT) 5251da177e4SLinus Torvalds child->ptrace |= PT_TRACE_EXIT; 5261da177e4SLinus Torvalds 5271da177e4SLinus Torvalds return (data & ~PTRACE_O_MASK) ? -EINVAL : 0; 5281da177e4SLinus Torvalds } 5291da177e4SLinus Torvalds 530e16b2781SRoland McGrath static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info) 5311da177e4SLinus Torvalds { 532e4961254SOleg Nesterov unsigned long flags; 5331da177e4SLinus Torvalds int error = -ESRCH; 5341da177e4SLinus Torvalds 535e4961254SOleg Nesterov if (lock_task_sighand(child, &flags)) { 5361da177e4SLinus Torvalds error = -EINVAL; 5371da177e4SLinus Torvalds if (likely(child->last_siginfo != NULL)) { 538e16b2781SRoland McGrath *info = *child->last_siginfo; 5391da177e4SLinus Torvalds error = 0; 5401da177e4SLinus Torvalds } 541e4961254SOleg Nesterov unlock_task_sighand(child, &flags); 5421da177e4SLinus Torvalds } 5431da177e4SLinus Torvalds return error; 5441da177e4SLinus Torvalds } 5451da177e4SLinus Torvalds 546e16b2781SRoland McGrath static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info) 5471da177e4SLinus Torvalds { 548e4961254SOleg Nesterov unsigned long flags; 5491da177e4SLinus Torvalds int error = -ESRCH; 5501da177e4SLinus Torvalds 551e4961254SOleg Nesterov if (lock_task_sighand(child, &flags)) { 5521da177e4SLinus Torvalds error = -EINVAL; 5531da177e4SLinus Torvalds if (likely(child->last_siginfo != NULL)) { 554e16b2781SRoland McGrath *child->last_siginfo = *info; 5551da177e4SLinus Torvalds error = 0; 5561da177e4SLinus Torvalds } 557e4961254SOleg Nesterov unlock_task_sighand(child, &flags); 5581da177e4SLinus Torvalds } 5591da177e4SLinus Torvalds return error; 5601da177e4SLinus Torvalds } 5611da177e4SLinus Torvalds 56236df29d7SRoland McGrath 56336df29d7SRoland McGrath #ifdef PTRACE_SINGLESTEP 56436df29d7SRoland McGrath #define is_singlestep(request) ((request) == PTRACE_SINGLESTEP) 56536df29d7SRoland McGrath #else 56636df29d7SRoland McGrath #define is_singlestep(request) 0 56736df29d7SRoland McGrath #endif 56836df29d7SRoland McGrath 5695b88abbfSRoland McGrath #ifdef PTRACE_SINGLEBLOCK 5705b88abbfSRoland McGrath #define is_singleblock(request) ((request) == PTRACE_SINGLEBLOCK) 5715b88abbfSRoland McGrath #else 5725b88abbfSRoland McGrath #define is_singleblock(request) 0 5735b88abbfSRoland McGrath #endif 5745b88abbfSRoland McGrath 57536df29d7SRoland McGrath #ifdef PTRACE_SYSEMU 57636df29d7SRoland McGrath #define is_sysemu_singlestep(request) ((request) == PTRACE_SYSEMU_SINGLESTEP) 57736df29d7SRoland McGrath #else 57836df29d7SRoland McGrath #define is_sysemu_singlestep(request) 0 57936df29d7SRoland McGrath #endif 58036df29d7SRoland McGrath 5814abf9869SNamhyung Kim static int ptrace_resume(struct task_struct *child, long request, 5824abf9869SNamhyung Kim unsigned long data) 58336df29d7SRoland McGrath { 58436df29d7SRoland McGrath if (!valid_signal(data)) 58536df29d7SRoland McGrath return -EIO; 58636df29d7SRoland McGrath 58736df29d7SRoland McGrath if (request == PTRACE_SYSCALL) 58836df29d7SRoland McGrath set_tsk_thread_flag(child, TIF_SYSCALL_TRACE); 58936df29d7SRoland McGrath else 59036df29d7SRoland McGrath clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); 59136df29d7SRoland McGrath 59236df29d7SRoland McGrath #ifdef TIF_SYSCALL_EMU 59336df29d7SRoland McGrath if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP) 59436df29d7SRoland McGrath set_tsk_thread_flag(child, TIF_SYSCALL_EMU); 59536df29d7SRoland McGrath else 59636df29d7SRoland McGrath clear_tsk_thread_flag(child, TIF_SYSCALL_EMU); 59736df29d7SRoland McGrath #endif 59836df29d7SRoland McGrath 5995b88abbfSRoland McGrath if (is_singleblock(request)) { 6005b88abbfSRoland McGrath if (unlikely(!arch_has_block_step())) 6015b88abbfSRoland McGrath return -EIO; 6025b88abbfSRoland McGrath user_enable_block_step(child); 6035b88abbfSRoland McGrath } else if (is_singlestep(request) || is_sysemu_singlestep(request)) { 60436df29d7SRoland McGrath if (unlikely(!arch_has_single_step())) 60536df29d7SRoland McGrath return -EIO; 60636df29d7SRoland McGrath user_enable_single_step(child); 6073a709703SRoland McGrath } else { 60836df29d7SRoland McGrath user_disable_single_step(child); 6093a709703SRoland McGrath } 61036df29d7SRoland McGrath 61136df29d7SRoland McGrath child->exit_code = data; 6120666fb51SOleg Nesterov wake_up_state(child, __TASK_TRACED); 61336df29d7SRoland McGrath 61436df29d7SRoland McGrath return 0; 61536df29d7SRoland McGrath } 61636df29d7SRoland McGrath 6172225a122SSuresh Siddha #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 6182225a122SSuresh Siddha 6192225a122SSuresh Siddha static const struct user_regset * 6202225a122SSuresh Siddha find_regset(const struct user_regset_view *view, unsigned int type) 6212225a122SSuresh Siddha { 6222225a122SSuresh Siddha const struct user_regset *regset; 6232225a122SSuresh Siddha int n; 6242225a122SSuresh Siddha 6252225a122SSuresh Siddha for (n = 0; n < view->n; ++n) { 6262225a122SSuresh Siddha regset = view->regsets + n; 6272225a122SSuresh Siddha if (regset->core_note_type == type) 6282225a122SSuresh Siddha return regset; 6292225a122SSuresh Siddha } 6302225a122SSuresh Siddha 6312225a122SSuresh Siddha return NULL; 6322225a122SSuresh Siddha } 6332225a122SSuresh Siddha 6342225a122SSuresh Siddha static int ptrace_regset(struct task_struct *task, int req, unsigned int type, 6352225a122SSuresh Siddha struct iovec *kiov) 6362225a122SSuresh Siddha { 6372225a122SSuresh Siddha const struct user_regset_view *view = task_user_regset_view(task); 6382225a122SSuresh Siddha const struct user_regset *regset = find_regset(view, type); 6392225a122SSuresh Siddha int regset_no; 6402225a122SSuresh Siddha 6412225a122SSuresh Siddha if (!regset || (kiov->iov_len % regset->size) != 0) 642c6a0dd7eSSuresh Siddha return -EINVAL; 6432225a122SSuresh Siddha 6442225a122SSuresh Siddha regset_no = regset - view->regsets; 6452225a122SSuresh Siddha kiov->iov_len = min(kiov->iov_len, 6462225a122SSuresh Siddha (__kernel_size_t) (regset->n * regset->size)); 6472225a122SSuresh Siddha 6482225a122SSuresh Siddha if (req == PTRACE_GETREGSET) 6492225a122SSuresh Siddha return copy_regset_to_user(task, view, regset_no, 0, 6502225a122SSuresh Siddha kiov->iov_len, kiov->iov_base); 6512225a122SSuresh Siddha else 6522225a122SSuresh Siddha return copy_regset_from_user(task, view, regset_no, 0, 6532225a122SSuresh Siddha kiov->iov_len, kiov->iov_base); 6542225a122SSuresh Siddha } 6552225a122SSuresh Siddha 6562225a122SSuresh Siddha #endif 6572225a122SSuresh Siddha 6581da177e4SLinus Torvalds int ptrace_request(struct task_struct *child, long request, 6594abf9869SNamhyung Kim unsigned long addr, unsigned long data) 6601da177e4SLinus Torvalds { 661fca26f26STejun Heo bool seized = child->ptrace & PT_SEIZED; 6621da177e4SLinus Torvalds int ret = -EIO; 663e16b2781SRoland McGrath siginfo_t siginfo; 6649fed81dcSNamhyung Kim void __user *datavp = (void __user *) data; 6659fed81dcSNamhyung Kim unsigned long __user *datalp = datavp; 666fca26f26STejun Heo unsigned long flags; 6671da177e4SLinus Torvalds 6681da177e4SLinus Torvalds switch (request) { 66916c3e389SRoland McGrath case PTRACE_PEEKTEXT: 67016c3e389SRoland McGrath case PTRACE_PEEKDATA: 67116c3e389SRoland McGrath return generic_ptrace_peekdata(child, addr, data); 67216c3e389SRoland McGrath case PTRACE_POKETEXT: 67316c3e389SRoland McGrath case PTRACE_POKEDATA: 67416c3e389SRoland McGrath return generic_ptrace_pokedata(child, addr, data); 67516c3e389SRoland McGrath 6761da177e4SLinus Torvalds #ifdef PTRACE_OLDSETOPTIONS 6771da177e4SLinus Torvalds case PTRACE_OLDSETOPTIONS: 6781da177e4SLinus Torvalds #endif 6791da177e4SLinus Torvalds case PTRACE_SETOPTIONS: 6801da177e4SLinus Torvalds ret = ptrace_setoptions(child, data); 6811da177e4SLinus Torvalds break; 6821da177e4SLinus Torvalds case PTRACE_GETEVENTMSG: 6839fed81dcSNamhyung Kim ret = put_user(child->ptrace_message, datalp); 6841da177e4SLinus Torvalds break; 685e16b2781SRoland McGrath 6861da177e4SLinus Torvalds case PTRACE_GETSIGINFO: 687e16b2781SRoland McGrath ret = ptrace_getsiginfo(child, &siginfo); 688e16b2781SRoland McGrath if (!ret) 6899fed81dcSNamhyung Kim ret = copy_siginfo_to_user(datavp, &siginfo); 6901da177e4SLinus Torvalds break; 691e16b2781SRoland McGrath 6921da177e4SLinus Torvalds case PTRACE_SETSIGINFO: 6939fed81dcSNamhyung Kim if (copy_from_user(&siginfo, datavp, sizeof siginfo)) 694e16b2781SRoland McGrath ret = -EFAULT; 695e16b2781SRoland McGrath else 696e16b2781SRoland McGrath ret = ptrace_setsiginfo(child, &siginfo); 6971da177e4SLinus Torvalds break; 698e16b2781SRoland McGrath 699fca26f26STejun Heo case PTRACE_INTERRUPT: 700fca26f26STejun Heo /* 701fca26f26STejun Heo * Stop tracee without any side-effect on signal or job 702fca26f26STejun Heo * control. At least one trap is guaranteed to happen 703fca26f26STejun Heo * after this request. If @child is already trapped, the 704fca26f26STejun Heo * current trap is not disturbed and another trap will 705fca26f26STejun Heo * happen after the current trap is ended with PTRACE_CONT. 706fca26f26STejun Heo * 707fca26f26STejun Heo * The actual trap might not be PTRACE_EVENT_STOP trap but 708fca26f26STejun Heo * the pending condition is cleared regardless. 709fca26f26STejun Heo */ 710fca26f26STejun Heo if (unlikely(!seized || !lock_task_sighand(child, &flags))) 711fca26f26STejun Heo break; 712fca26f26STejun Heo 713fca26f26STejun Heo if (likely(task_set_jobctl_pending(child, JOBCTL_TRAP_STOP))) 714fca26f26STejun Heo signal_wake_up(child, 0); 715fca26f26STejun Heo 716fca26f26STejun Heo unlock_task_sighand(child, &flags); 717fca26f26STejun Heo ret = 0; 718fca26f26STejun Heo break; 719fca26f26STejun Heo 7201bcf5482SAlexey Dobriyan case PTRACE_DETACH: /* detach a process that was attached. */ 7211bcf5482SAlexey Dobriyan ret = ptrace_detach(child, data); 7221bcf5482SAlexey Dobriyan break; 72336df29d7SRoland McGrath 7249c1a1259SMike Frysinger #ifdef CONFIG_BINFMT_ELF_FDPIC 7259c1a1259SMike Frysinger case PTRACE_GETFDPIC: { 726e0129ef9SOleg Nesterov struct mm_struct *mm = get_task_mm(child); 7279c1a1259SMike Frysinger unsigned long tmp = 0; 7289c1a1259SMike Frysinger 729e0129ef9SOleg Nesterov ret = -ESRCH; 730e0129ef9SOleg Nesterov if (!mm) 731e0129ef9SOleg Nesterov break; 732e0129ef9SOleg Nesterov 7339c1a1259SMike Frysinger switch (addr) { 7349c1a1259SMike Frysinger case PTRACE_GETFDPIC_EXEC: 735e0129ef9SOleg Nesterov tmp = mm->context.exec_fdpic_loadmap; 7369c1a1259SMike Frysinger break; 7379c1a1259SMike Frysinger case PTRACE_GETFDPIC_INTERP: 738e0129ef9SOleg Nesterov tmp = mm->context.interp_fdpic_loadmap; 7399c1a1259SMike Frysinger break; 7409c1a1259SMike Frysinger default: 7419c1a1259SMike Frysinger break; 7429c1a1259SMike Frysinger } 743e0129ef9SOleg Nesterov mmput(mm); 7449c1a1259SMike Frysinger 7459fed81dcSNamhyung Kim ret = put_user(tmp, datalp); 7469c1a1259SMike Frysinger break; 7479c1a1259SMike Frysinger } 7489c1a1259SMike Frysinger #endif 7499c1a1259SMike Frysinger 75036df29d7SRoland McGrath #ifdef PTRACE_SINGLESTEP 75136df29d7SRoland McGrath case PTRACE_SINGLESTEP: 75236df29d7SRoland McGrath #endif 7535b88abbfSRoland McGrath #ifdef PTRACE_SINGLEBLOCK 7545b88abbfSRoland McGrath case PTRACE_SINGLEBLOCK: 7555b88abbfSRoland McGrath #endif 75636df29d7SRoland McGrath #ifdef PTRACE_SYSEMU 75736df29d7SRoland McGrath case PTRACE_SYSEMU: 75836df29d7SRoland McGrath case PTRACE_SYSEMU_SINGLESTEP: 75936df29d7SRoland McGrath #endif 76036df29d7SRoland McGrath case PTRACE_SYSCALL: 76136df29d7SRoland McGrath case PTRACE_CONT: 76236df29d7SRoland McGrath return ptrace_resume(child, request, data); 76336df29d7SRoland McGrath 76436df29d7SRoland McGrath case PTRACE_KILL: 76536df29d7SRoland McGrath if (child->exit_state) /* already dead */ 76636df29d7SRoland McGrath return 0; 76736df29d7SRoland McGrath return ptrace_resume(child, request, SIGKILL); 76836df29d7SRoland McGrath 7692225a122SSuresh Siddha #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 7702225a122SSuresh Siddha case PTRACE_GETREGSET: 7712225a122SSuresh Siddha case PTRACE_SETREGSET: 7722225a122SSuresh Siddha { 7732225a122SSuresh Siddha struct iovec kiov; 7749fed81dcSNamhyung Kim struct iovec __user *uiov = datavp; 7752225a122SSuresh Siddha 7762225a122SSuresh Siddha if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov))) 7772225a122SSuresh Siddha return -EFAULT; 7782225a122SSuresh Siddha 7792225a122SSuresh Siddha if (__get_user(kiov.iov_base, &uiov->iov_base) || 7802225a122SSuresh Siddha __get_user(kiov.iov_len, &uiov->iov_len)) 7812225a122SSuresh Siddha return -EFAULT; 7822225a122SSuresh Siddha 7832225a122SSuresh Siddha ret = ptrace_regset(child, request, addr, &kiov); 7842225a122SSuresh Siddha if (!ret) 7852225a122SSuresh Siddha ret = __put_user(kiov.iov_len, &uiov->iov_len); 7862225a122SSuresh Siddha break; 7872225a122SSuresh Siddha } 7882225a122SSuresh Siddha #endif 7891da177e4SLinus Torvalds default: 7901da177e4SLinus Torvalds break; 7911da177e4SLinus Torvalds } 7921da177e4SLinus Torvalds 7931da177e4SLinus Torvalds return ret; 7941da177e4SLinus Torvalds } 795481bed45SChristoph Hellwig 7968053bdd5SOleg Nesterov static struct task_struct *ptrace_get_task_struct(pid_t pid) 7976b9c7ed8SChristoph Hellwig { 7986b9c7ed8SChristoph Hellwig struct task_struct *child; 7996b9c7ed8SChristoph Hellwig 8008053bdd5SOleg Nesterov rcu_read_lock(); 801228ebcbeSPavel Emelyanov child = find_task_by_vpid(pid); 802481bed45SChristoph Hellwig if (child) 803481bed45SChristoph Hellwig get_task_struct(child); 8048053bdd5SOleg Nesterov rcu_read_unlock(); 805f400e198SSukadev Bhattiprolu 806481bed45SChristoph Hellwig if (!child) 8076b9c7ed8SChristoph Hellwig return ERR_PTR(-ESRCH); 8086b9c7ed8SChristoph Hellwig return child; 809481bed45SChristoph Hellwig } 810481bed45SChristoph Hellwig 8110ac15559SChristoph Hellwig #ifndef arch_ptrace_attach 8120ac15559SChristoph Hellwig #define arch_ptrace_attach(child) do { } while (0) 8130ac15559SChristoph Hellwig #endif 8140ac15559SChristoph Hellwig 8154abf9869SNamhyung Kim SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr, 8164abf9869SNamhyung Kim unsigned long, data) 817481bed45SChristoph Hellwig { 818481bed45SChristoph Hellwig struct task_struct *child; 819481bed45SChristoph Hellwig long ret; 820481bed45SChristoph Hellwig 8216b9c7ed8SChristoph Hellwig if (request == PTRACE_TRACEME) { 8226b9c7ed8SChristoph Hellwig ret = ptrace_traceme(); 8236ea6dd93SHaavard Skinnemoen if (!ret) 8246ea6dd93SHaavard Skinnemoen arch_ptrace_attach(current); 825481bed45SChristoph Hellwig goto out; 8266b9c7ed8SChristoph Hellwig } 8276b9c7ed8SChristoph Hellwig 8286b9c7ed8SChristoph Hellwig child = ptrace_get_task_struct(pid); 8296b9c7ed8SChristoph Hellwig if (IS_ERR(child)) { 8306b9c7ed8SChristoph Hellwig ret = PTR_ERR(child); 8316b9c7ed8SChristoph Hellwig goto out; 8326b9c7ed8SChristoph Hellwig } 833481bed45SChristoph Hellwig 8343544d72aSTejun Heo if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) { 8353544d72aSTejun Heo ret = ptrace_attach(child, request, data); 8360ac15559SChristoph Hellwig /* 8370ac15559SChristoph Hellwig * Some architectures need to do book-keeping after 8380ac15559SChristoph Hellwig * a ptrace attach. 8390ac15559SChristoph Hellwig */ 8400ac15559SChristoph Hellwig if (!ret) 8410ac15559SChristoph Hellwig arch_ptrace_attach(child); 842005f18dfSChristoph Hellwig goto out_put_task_struct; 843481bed45SChristoph Hellwig } 844481bed45SChristoph Hellwig 845fca26f26STejun Heo ret = ptrace_check_attach(child, request == PTRACE_KILL || 846fca26f26STejun Heo request == PTRACE_INTERRUPT); 847481bed45SChristoph Hellwig if (ret < 0) 848481bed45SChristoph Hellwig goto out_put_task_struct; 849481bed45SChristoph Hellwig 850481bed45SChristoph Hellwig ret = arch_ptrace(child, request, addr, data); 851481bed45SChristoph Hellwig 852481bed45SChristoph Hellwig out_put_task_struct: 853481bed45SChristoph Hellwig put_task_struct(child); 854481bed45SChristoph Hellwig out: 855481bed45SChristoph Hellwig return ret; 856481bed45SChristoph Hellwig } 85776647323SAlexey Dobriyan 8584abf9869SNamhyung Kim int generic_ptrace_peekdata(struct task_struct *tsk, unsigned long addr, 8594abf9869SNamhyung Kim unsigned long data) 86076647323SAlexey Dobriyan { 86176647323SAlexey Dobriyan unsigned long tmp; 86276647323SAlexey Dobriyan int copied; 86376647323SAlexey Dobriyan 86476647323SAlexey Dobriyan copied = access_process_vm(tsk, addr, &tmp, sizeof(tmp), 0); 86576647323SAlexey Dobriyan if (copied != sizeof(tmp)) 86676647323SAlexey Dobriyan return -EIO; 86776647323SAlexey Dobriyan return put_user(tmp, (unsigned long __user *)data); 86876647323SAlexey Dobriyan } 869f284ce72SAlexey Dobriyan 8704abf9869SNamhyung Kim int generic_ptrace_pokedata(struct task_struct *tsk, unsigned long addr, 8714abf9869SNamhyung Kim unsigned long data) 872f284ce72SAlexey Dobriyan { 873f284ce72SAlexey Dobriyan int copied; 874f284ce72SAlexey Dobriyan 875f284ce72SAlexey Dobriyan copied = access_process_vm(tsk, addr, &data, sizeof(data), 1); 876f284ce72SAlexey Dobriyan return (copied == sizeof(data)) ? 0 : -EIO; 877f284ce72SAlexey Dobriyan } 878032d82d9SRoland McGrath 87996b8936aSChristoph Hellwig #if defined CONFIG_COMPAT 880032d82d9SRoland McGrath #include <linux/compat.h> 881032d82d9SRoland McGrath 882032d82d9SRoland McGrath int compat_ptrace_request(struct task_struct *child, compat_long_t request, 883032d82d9SRoland McGrath compat_ulong_t addr, compat_ulong_t data) 884032d82d9SRoland McGrath { 885032d82d9SRoland McGrath compat_ulong_t __user *datap = compat_ptr(data); 886032d82d9SRoland McGrath compat_ulong_t word; 887e16b2781SRoland McGrath siginfo_t siginfo; 888032d82d9SRoland McGrath int ret; 889032d82d9SRoland McGrath 890032d82d9SRoland McGrath switch (request) { 891032d82d9SRoland McGrath case PTRACE_PEEKTEXT: 892032d82d9SRoland McGrath case PTRACE_PEEKDATA: 893032d82d9SRoland McGrath ret = access_process_vm(child, addr, &word, sizeof(word), 0); 894032d82d9SRoland McGrath if (ret != sizeof(word)) 895032d82d9SRoland McGrath ret = -EIO; 896032d82d9SRoland McGrath else 897032d82d9SRoland McGrath ret = put_user(word, datap); 898032d82d9SRoland McGrath break; 899032d82d9SRoland McGrath 900032d82d9SRoland McGrath case PTRACE_POKETEXT: 901032d82d9SRoland McGrath case PTRACE_POKEDATA: 902032d82d9SRoland McGrath ret = access_process_vm(child, addr, &data, sizeof(data), 1); 903032d82d9SRoland McGrath ret = (ret != sizeof(data) ? -EIO : 0); 904032d82d9SRoland McGrath break; 905032d82d9SRoland McGrath 906032d82d9SRoland McGrath case PTRACE_GETEVENTMSG: 907032d82d9SRoland McGrath ret = put_user((compat_ulong_t) child->ptrace_message, datap); 908032d82d9SRoland McGrath break; 909032d82d9SRoland McGrath 910e16b2781SRoland McGrath case PTRACE_GETSIGINFO: 911e16b2781SRoland McGrath ret = ptrace_getsiginfo(child, &siginfo); 912e16b2781SRoland McGrath if (!ret) 913e16b2781SRoland McGrath ret = copy_siginfo_to_user32( 914e16b2781SRoland McGrath (struct compat_siginfo __user *) datap, 915e16b2781SRoland McGrath &siginfo); 916e16b2781SRoland McGrath break; 917e16b2781SRoland McGrath 918e16b2781SRoland McGrath case PTRACE_SETSIGINFO: 919e16b2781SRoland McGrath memset(&siginfo, 0, sizeof siginfo); 920e16b2781SRoland McGrath if (copy_siginfo_from_user32( 921e16b2781SRoland McGrath &siginfo, (struct compat_siginfo __user *) datap)) 922e16b2781SRoland McGrath ret = -EFAULT; 923e16b2781SRoland McGrath else 924e16b2781SRoland McGrath ret = ptrace_setsiginfo(child, &siginfo); 925e16b2781SRoland McGrath break; 9262225a122SSuresh Siddha #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 9272225a122SSuresh Siddha case PTRACE_GETREGSET: 9282225a122SSuresh Siddha case PTRACE_SETREGSET: 9292225a122SSuresh Siddha { 9302225a122SSuresh Siddha struct iovec kiov; 9312225a122SSuresh Siddha struct compat_iovec __user *uiov = 9322225a122SSuresh Siddha (struct compat_iovec __user *) datap; 9332225a122SSuresh Siddha compat_uptr_t ptr; 9342225a122SSuresh Siddha compat_size_t len; 9352225a122SSuresh Siddha 9362225a122SSuresh Siddha if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov))) 9372225a122SSuresh Siddha return -EFAULT; 9382225a122SSuresh Siddha 9392225a122SSuresh Siddha if (__get_user(ptr, &uiov->iov_base) || 9402225a122SSuresh Siddha __get_user(len, &uiov->iov_len)) 9412225a122SSuresh Siddha return -EFAULT; 9422225a122SSuresh Siddha 9432225a122SSuresh Siddha kiov.iov_base = compat_ptr(ptr); 9442225a122SSuresh Siddha kiov.iov_len = len; 9452225a122SSuresh Siddha 9462225a122SSuresh Siddha ret = ptrace_regset(child, request, addr, &kiov); 9472225a122SSuresh Siddha if (!ret) 9482225a122SSuresh Siddha ret = __put_user(kiov.iov_len, &uiov->iov_len); 9492225a122SSuresh Siddha break; 9502225a122SSuresh Siddha } 9512225a122SSuresh Siddha #endif 952e16b2781SRoland McGrath 953032d82d9SRoland McGrath default: 954032d82d9SRoland McGrath ret = ptrace_request(child, request, addr, data); 955032d82d9SRoland McGrath } 956032d82d9SRoland McGrath 957032d82d9SRoland McGrath return ret; 958032d82d9SRoland McGrath } 959c269f196SRoland McGrath 960c269f196SRoland McGrath asmlinkage long compat_sys_ptrace(compat_long_t request, compat_long_t pid, 961c269f196SRoland McGrath compat_long_t addr, compat_long_t data) 962c269f196SRoland McGrath { 963c269f196SRoland McGrath struct task_struct *child; 964c269f196SRoland McGrath long ret; 965c269f196SRoland McGrath 966c269f196SRoland McGrath if (request == PTRACE_TRACEME) { 967c269f196SRoland McGrath ret = ptrace_traceme(); 968c269f196SRoland McGrath goto out; 969c269f196SRoland McGrath } 970c269f196SRoland McGrath 971c269f196SRoland McGrath child = ptrace_get_task_struct(pid); 972c269f196SRoland McGrath if (IS_ERR(child)) { 973c269f196SRoland McGrath ret = PTR_ERR(child); 974c269f196SRoland McGrath goto out; 975c269f196SRoland McGrath } 976c269f196SRoland McGrath 9773544d72aSTejun Heo if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) { 9783544d72aSTejun Heo ret = ptrace_attach(child, request, data); 979c269f196SRoland McGrath /* 980c269f196SRoland McGrath * Some architectures need to do book-keeping after 981c269f196SRoland McGrath * a ptrace attach. 982c269f196SRoland McGrath */ 983c269f196SRoland McGrath if (!ret) 984c269f196SRoland McGrath arch_ptrace_attach(child); 985c269f196SRoland McGrath goto out_put_task_struct; 986c269f196SRoland McGrath } 987c269f196SRoland McGrath 988fca26f26STejun Heo ret = ptrace_check_attach(child, request == PTRACE_KILL || 989fca26f26STejun Heo request == PTRACE_INTERRUPT); 990c269f196SRoland McGrath if (!ret) 991c269f196SRoland McGrath ret = compat_arch_ptrace(child, request, addr, data); 992c269f196SRoland McGrath 993c269f196SRoland McGrath out_put_task_struct: 994c269f196SRoland McGrath put_task_struct(child); 995c269f196SRoland McGrath out: 996c269f196SRoland McGrath return ret; 997c269f196SRoland McGrath } 99896b8936aSChristoph Hellwig #endif /* CONFIG_COMPAT */ 999bf26c018SFrederic Weisbecker 1000bf26c018SFrederic Weisbecker #ifdef CONFIG_HAVE_HW_BREAKPOINT 1001bf26c018SFrederic Weisbecker int ptrace_get_breakpoints(struct task_struct *tsk) 1002bf26c018SFrederic Weisbecker { 1003bf26c018SFrederic Weisbecker if (atomic_inc_not_zero(&tsk->ptrace_bp_refcnt)) 1004bf26c018SFrederic Weisbecker return 0; 1005bf26c018SFrederic Weisbecker 1006bf26c018SFrederic Weisbecker return -1; 1007bf26c018SFrederic Weisbecker } 1008bf26c018SFrederic Weisbecker 1009bf26c018SFrederic Weisbecker void ptrace_put_breakpoints(struct task_struct *tsk) 1010bf26c018SFrederic Weisbecker { 1011bf26c018SFrederic Weisbecker if (atomic_dec_and_test(&tsk->ptrace_bp_refcnt)) 1012bf26c018SFrederic Weisbecker flush_ptrace_hw_breakpoint(tsk); 1013bf26c018SFrederic Weisbecker } 1014bf26c018SFrederic Weisbecker #endif /* CONFIG_HAVE_HW_BREAKPOINT */ 1015