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/smp_lock.h> 181da177e4SLinus Torvalds #include <linux/ptrace.h> 191da177e4SLinus Torvalds #include <linux/security.h> 207ed20e1aSJesper Juhl #include <linux/signal.h> 21a5cb013dSAl Viro #include <linux/audit.h> 22b488893aSPavel Emelyanov #include <linux/pid_namespace.h> 23f17d30a8SAdrian Bunk #include <linux/syscalls.h> 241da177e4SLinus Torvalds 251da177e4SLinus Torvalds #include <asm/pgtable.h> 261da177e4SLinus Torvalds #include <asm/uaccess.h> 271da177e4SLinus Torvalds 281da177e4SLinus Torvalds /* 291da177e4SLinus Torvalds * ptrace a task: make the debugger its new parent and 301da177e4SLinus Torvalds * move it to the ptrace list. 311da177e4SLinus Torvalds * 321da177e4SLinus Torvalds * Must be called with the tasklist lock write-held. 331da177e4SLinus Torvalds */ 3436c8b586SIngo Molnar void __ptrace_link(struct task_struct *child, struct task_struct *new_parent) 351da177e4SLinus Torvalds { 36524223caSEric Sesterhenn BUG_ON(!list_empty(&child->ptrace_list)); 371da177e4SLinus Torvalds if (child->parent == new_parent) 381da177e4SLinus Torvalds return; 391da177e4SLinus Torvalds list_add(&child->ptrace_list, &child->parent->ptrace_children); 409b678eceSOleg Nesterov remove_parent(child); 411da177e4SLinus Torvalds child->parent = new_parent; 429b678eceSOleg Nesterov add_parent(child); 431da177e4SLinus Torvalds } 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds /* 461da177e4SLinus Torvalds * Turn a tracing stop into a normal stop now, since with no tracer there 471da177e4SLinus Torvalds * would be no way to wake it up with SIGCONT or SIGKILL. If there was a 481da177e4SLinus Torvalds * signal sent that would resume the child, but didn't because it was in 491da177e4SLinus Torvalds * TASK_TRACED, resume it now. 501da177e4SLinus Torvalds * Requires that irqs be disabled. 511da177e4SLinus Torvalds */ 5236c8b586SIngo Molnar void ptrace_untrace(struct task_struct *child) 531da177e4SLinus Torvalds { 541da177e4SLinus Torvalds spin_lock(&child->sighand->siglock); 556618a3e2SMatthew Wilcox if (task_is_traced(child)) { 561da177e4SLinus Torvalds if (child->signal->flags & SIGNAL_STOP_STOPPED) { 57d9ae90acSOleg Nesterov __set_task_state(child, TASK_STOPPED); 581da177e4SLinus Torvalds } else { 591da177e4SLinus Torvalds signal_wake_up(child, 1); 601da177e4SLinus Torvalds } 611da177e4SLinus Torvalds } 621da177e4SLinus Torvalds spin_unlock(&child->sighand->siglock); 631da177e4SLinus Torvalds } 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds /* 661da177e4SLinus Torvalds * unptrace a task: move it back to its original parent and 671da177e4SLinus Torvalds * remove it from the ptrace list. 681da177e4SLinus Torvalds * 691da177e4SLinus Torvalds * Must be called with the tasklist lock write-held. 701da177e4SLinus Torvalds */ 7136c8b586SIngo Molnar void __ptrace_unlink(struct task_struct *child) 721da177e4SLinus Torvalds { 735ecfbae0SOleg Nesterov BUG_ON(!child->ptrace); 745ecfbae0SOleg Nesterov 751da177e4SLinus Torvalds child->ptrace = 0; 7668cb9478SOleg Nesterov if (ptrace_reparented(child)) { 771da177e4SLinus Torvalds list_del_init(&child->ptrace_list); 789b678eceSOleg Nesterov remove_parent(child); 791da177e4SLinus Torvalds child->parent = child->real_parent; 809b678eceSOleg Nesterov add_parent(child); 811da177e4SLinus Torvalds } 821da177e4SLinus Torvalds 836618a3e2SMatthew Wilcox if (task_is_traced(child)) 841da177e4SLinus Torvalds ptrace_untrace(child); 851da177e4SLinus Torvalds } 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds /* 881da177e4SLinus Torvalds * Check that we have indeed attached to the thing.. 891da177e4SLinus Torvalds */ 901da177e4SLinus Torvalds int ptrace_check_attach(struct task_struct *child, int kill) 911da177e4SLinus Torvalds { 921da177e4SLinus Torvalds int ret = -ESRCH; 931da177e4SLinus Torvalds 941da177e4SLinus Torvalds /* 951da177e4SLinus Torvalds * We take the read lock around doing both checks to close a 961da177e4SLinus Torvalds * possible race where someone else was tracing our child and 971da177e4SLinus Torvalds * detached between these two checks. After this locked check, 981da177e4SLinus Torvalds * we are sure that this is our traced child and that can only 991da177e4SLinus Torvalds * be changed by us so it's not changing right after this. 1001da177e4SLinus Torvalds */ 1011da177e4SLinus Torvalds read_lock(&tasklist_lock); 102c0c0b649SOleg Nesterov if ((child->ptrace & PT_PTRACED) && child->parent == current) { 1031da177e4SLinus Torvalds ret = 0; 104c0c0b649SOleg Nesterov /* 105c0c0b649SOleg Nesterov * child->sighand can't be NULL, release_task() 106c0c0b649SOleg Nesterov * does ptrace_unlink() before __exit_signal(). 107c0c0b649SOleg Nesterov */ 1081da177e4SLinus Torvalds spin_lock_irq(&child->sighand->siglock); 109d9ae90acSOleg Nesterov if (task_is_stopped(child)) 1101da177e4SLinus Torvalds child->state = TASK_TRACED; 111d9ae90acSOleg Nesterov else if (!task_is_traced(child) && !kill) 1121da177e4SLinus Torvalds ret = -ESRCH; 1131da177e4SLinus Torvalds spin_unlock_irq(&child->sighand->siglock); 1141da177e4SLinus Torvalds } 1151da177e4SLinus Torvalds read_unlock(&tasklist_lock); 1161da177e4SLinus Torvalds 117d9ae90acSOleg Nesterov if (!ret && !kill) 1181da177e4SLinus Torvalds wait_task_inactive(child); 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds /* All systems go.. */ 1211da177e4SLinus Torvalds return ret; 1221da177e4SLinus Torvalds } 1231da177e4SLinus Torvalds 124006ebb40SStephen Smalley int __ptrace_may_access(struct task_struct *task, unsigned int mode) 125ab8d11beSMiklos Szeredi { 126df26c40eSEric W. Biederman /* May we inspect the given task? 127df26c40eSEric W. Biederman * This check is used both for attaching with ptrace 128df26c40eSEric W. Biederman * and for allowing access to sensitive information in /proc. 129df26c40eSEric W. Biederman * 130df26c40eSEric W. Biederman * ptrace_attach denies several cases that /proc allows 131df26c40eSEric W. Biederman * because setting up the necessary parent/child relationship 132df26c40eSEric W. Biederman * or halting the specified task is impossible. 133df26c40eSEric W. Biederman */ 134df26c40eSEric W. Biederman int dumpable = 0; 135df26c40eSEric W. Biederman /* Don't let security modules deny introspection */ 136df26c40eSEric W. Biederman if (task == current) 137df26c40eSEric W. Biederman return 0; 138ab8d11beSMiklos Szeredi if (((current->uid != task->euid) || 139ab8d11beSMiklos Szeredi (current->uid != task->suid) || 140ab8d11beSMiklos Szeredi (current->uid != task->uid) || 141ab8d11beSMiklos Szeredi (current->gid != task->egid) || 142ab8d11beSMiklos Szeredi (current->gid != task->sgid) || 143ab8d11beSMiklos Szeredi (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE)) 144ab8d11beSMiklos Szeredi return -EPERM; 145ab8d11beSMiklos Szeredi smp_rmb(); 146df26c40eSEric W. Biederman if (task->mm) 1476c5d5238SKawai, Hidehiro dumpable = get_dumpable(task->mm); 148df26c40eSEric W. Biederman if (!dumpable && !capable(CAP_SYS_PTRACE)) 149ab8d11beSMiklos Szeredi return -EPERM; 150ab8d11beSMiklos Szeredi 151006ebb40SStephen Smalley return security_ptrace(current, task, mode); 152ab8d11beSMiklos Szeredi } 153ab8d11beSMiklos Szeredi 154006ebb40SStephen Smalley bool ptrace_may_access(struct task_struct *task, unsigned int mode) 155ab8d11beSMiklos Szeredi { 156ab8d11beSMiklos Szeredi int err; 157ab8d11beSMiklos Szeredi task_lock(task); 158006ebb40SStephen Smalley err = __ptrace_may_access(task, mode); 159ab8d11beSMiklos Szeredi task_unlock(task); 160006ebb40SStephen Smalley return (!err ? true : false); 161ab8d11beSMiklos Szeredi } 162ab8d11beSMiklos Szeredi 1631da177e4SLinus Torvalds int ptrace_attach(struct task_struct *task) 1641da177e4SLinus Torvalds { 1651da177e4SLinus Torvalds int retval; 1666175ecfeSSripathi Kodi unsigned long flags; 167f5b40e36SLinus Torvalds 168a5cb013dSAl Viro audit_ptrace(task); 169a5cb013dSAl Viro 1701da177e4SLinus Torvalds retval = -EPERM; 171bac0abd6SPavel Emelyanov if (same_thread_group(task, current)) 172f5b40e36SLinus Torvalds goto out; 173f5b40e36SLinus Torvalds 174f358166aSLinus Torvalds repeat: 175f358166aSLinus Torvalds /* 176f358166aSLinus Torvalds * Nasty, nasty. 177f358166aSLinus Torvalds * 178f358166aSLinus Torvalds * We want to hold both the task-lock and the 179f358166aSLinus Torvalds * tasklist_lock for writing at the same time. 180f358166aSLinus Torvalds * But that's against the rules (tasklist_lock 181f358166aSLinus Torvalds * is taken for reading by interrupts on other 182f358166aSLinus Torvalds * cpu's that may have task_lock). 183f358166aSLinus Torvalds */ 184f5b40e36SLinus Torvalds task_lock(task); 1856175ecfeSSripathi Kodi if (!write_trylock_irqsave(&tasklist_lock, flags)) { 186f358166aSLinus Torvalds task_unlock(task); 187f358166aSLinus Torvalds do { 188f358166aSLinus Torvalds cpu_relax(); 189f358166aSLinus Torvalds } while (!write_can_lock(&tasklist_lock)); 190f358166aSLinus Torvalds goto repeat; 191f358166aSLinus Torvalds } 192f5b40e36SLinus Torvalds 193df26c40eSEric W. Biederman if (!task->mm) 194df26c40eSEric W. Biederman goto bad; 1951da177e4SLinus Torvalds /* the same process cannot be attached many times */ 1961da177e4SLinus Torvalds if (task->ptrace & PT_PTRACED) 1971da177e4SLinus Torvalds goto bad; 198006ebb40SStephen Smalley retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH); 1991da177e4SLinus Torvalds if (retval) 2001da177e4SLinus Torvalds goto bad; 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds /* Go */ 2036b39c7bfSOleg Nesterov task->ptrace |= PT_PTRACED; 2041da177e4SLinus Torvalds if (capable(CAP_SYS_PTRACE)) 2051da177e4SLinus Torvalds task->ptrace |= PT_PTRACE_CAP; 2061da177e4SLinus Torvalds 2071da177e4SLinus Torvalds __ptrace_link(task, current); 2081da177e4SLinus Torvalds 20933e9fc7dSOleg Nesterov send_sig_info(SIGSTOP, SEND_SIG_FORCED, task); 2101da177e4SLinus Torvalds bad: 2116175ecfeSSripathi Kodi write_unlock_irqrestore(&tasklist_lock, flags); 2121da177e4SLinus Torvalds task_unlock(task); 213f5b40e36SLinus Torvalds out: 2141da177e4SLinus Torvalds return retval; 2151da177e4SLinus Torvalds } 2161da177e4SLinus Torvalds 217d5f70c00SOleg Nesterov static inline void __ptrace_detach(struct task_struct *child, unsigned int data) 2185ecfbae0SOleg Nesterov { 2195ecfbae0SOleg Nesterov child->exit_code = data; 2205ecfbae0SOleg Nesterov /* .. re-parent .. */ 2215ecfbae0SOleg Nesterov __ptrace_unlink(child); 2225ecfbae0SOleg Nesterov /* .. and wake it up. */ 2235ecfbae0SOleg Nesterov if (child->exit_state != EXIT_ZOMBIE) 2245ecfbae0SOleg Nesterov wake_up_process(child); 2255ecfbae0SOleg Nesterov } 2265ecfbae0SOleg Nesterov 2271da177e4SLinus Torvalds int ptrace_detach(struct task_struct *child, unsigned int data) 2281da177e4SLinus Torvalds { 2297ed20e1aSJesper Juhl if (!valid_signal(data)) 2301da177e4SLinus Torvalds return -EIO; 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds /* Architecture-specific hardware disable .. */ 2331da177e4SLinus Torvalds ptrace_disable(child); 2347d941432SRoland McGrath clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); 2351da177e4SLinus Torvalds 2361da177e4SLinus Torvalds write_lock_irq(&tasklist_lock); 237d5f70c00SOleg Nesterov /* protect against de_thread()->release_task() */ 2385ecfbae0SOleg Nesterov if (child->ptrace) 2395ecfbae0SOleg Nesterov __ptrace_detach(child, data); 2401da177e4SLinus Torvalds write_unlock_irq(&tasklist_lock); 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds return 0; 2431da177e4SLinus Torvalds } 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len) 2461da177e4SLinus Torvalds { 2471da177e4SLinus Torvalds int copied = 0; 2481da177e4SLinus Torvalds 2491da177e4SLinus Torvalds while (len > 0) { 2501da177e4SLinus Torvalds char buf[128]; 2511da177e4SLinus Torvalds int this_len, retval; 2521da177e4SLinus Torvalds 2531da177e4SLinus Torvalds this_len = (len > sizeof(buf)) ? sizeof(buf) : len; 2541da177e4SLinus Torvalds retval = access_process_vm(tsk, src, buf, this_len, 0); 2551da177e4SLinus Torvalds if (!retval) { 2561da177e4SLinus Torvalds if (copied) 2571da177e4SLinus Torvalds break; 2581da177e4SLinus Torvalds return -EIO; 2591da177e4SLinus Torvalds } 2601da177e4SLinus Torvalds if (copy_to_user(dst, buf, retval)) 2611da177e4SLinus Torvalds return -EFAULT; 2621da177e4SLinus Torvalds copied += retval; 2631da177e4SLinus Torvalds src += retval; 2641da177e4SLinus Torvalds dst += retval; 2651da177e4SLinus Torvalds len -= retval; 2661da177e4SLinus Torvalds } 2671da177e4SLinus Torvalds return copied; 2681da177e4SLinus Torvalds } 2691da177e4SLinus Torvalds 2701da177e4SLinus Torvalds int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len) 2711da177e4SLinus Torvalds { 2721da177e4SLinus Torvalds int copied = 0; 2731da177e4SLinus Torvalds 2741da177e4SLinus Torvalds while (len > 0) { 2751da177e4SLinus Torvalds char buf[128]; 2761da177e4SLinus Torvalds int this_len, retval; 2771da177e4SLinus Torvalds 2781da177e4SLinus Torvalds this_len = (len > sizeof(buf)) ? sizeof(buf) : len; 2791da177e4SLinus Torvalds if (copy_from_user(buf, src, this_len)) 2801da177e4SLinus Torvalds return -EFAULT; 2811da177e4SLinus Torvalds retval = access_process_vm(tsk, dst, buf, this_len, 1); 2821da177e4SLinus Torvalds if (!retval) { 2831da177e4SLinus Torvalds if (copied) 2841da177e4SLinus Torvalds break; 2851da177e4SLinus Torvalds return -EIO; 2861da177e4SLinus Torvalds } 2871da177e4SLinus Torvalds copied += retval; 2881da177e4SLinus Torvalds src += retval; 2891da177e4SLinus Torvalds dst += retval; 2901da177e4SLinus Torvalds len -= retval; 2911da177e4SLinus Torvalds } 2921da177e4SLinus Torvalds return copied; 2931da177e4SLinus Torvalds } 2941da177e4SLinus Torvalds 2951da177e4SLinus Torvalds static int ptrace_setoptions(struct task_struct *child, long data) 2961da177e4SLinus Torvalds { 2971da177e4SLinus Torvalds child->ptrace &= ~PT_TRACE_MASK; 2981da177e4SLinus Torvalds 2991da177e4SLinus Torvalds if (data & PTRACE_O_TRACESYSGOOD) 3001da177e4SLinus Torvalds child->ptrace |= PT_TRACESYSGOOD; 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds if (data & PTRACE_O_TRACEFORK) 3031da177e4SLinus Torvalds child->ptrace |= PT_TRACE_FORK; 3041da177e4SLinus Torvalds 3051da177e4SLinus Torvalds if (data & PTRACE_O_TRACEVFORK) 3061da177e4SLinus Torvalds child->ptrace |= PT_TRACE_VFORK; 3071da177e4SLinus Torvalds 3081da177e4SLinus Torvalds if (data & PTRACE_O_TRACECLONE) 3091da177e4SLinus Torvalds child->ptrace |= PT_TRACE_CLONE; 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds if (data & PTRACE_O_TRACEEXEC) 3121da177e4SLinus Torvalds child->ptrace |= PT_TRACE_EXEC; 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds if (data & PTRACE_O_TRACEVFORKDONE) 3151da177e4SLinus Torvalds child->ptrace |= PT_TRACE_VFORK_DONE; 3161da177e4SLinus Torvalds 3171da177e4SLinus Torvalds if (data & PTRACE_O_TRACEEXIT) 3181da177e4SLinus Torvalds child->ptrace |= PT_TRACE_EXIT; 3191da177e4SLinus Torvalds 3201da177e4SLinus Torvalds return (data & ~PTRACE_O_MASK) ? -EINVAL : 0; 3211da177e4SLinus Torvalds } 3221da177e4SLinus Torvalds 323e16b2781SRoland McGrath static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info) 3241da177e4SLinus Torvalds { 3251da177e4SLinus Torvalds int error = -ESRCH; 3261da177e4SLinus Torvalds 3271da177e4SLinus Torvalds read_lock(&tasklist_lock); 3281da177e4SLinus Torvalds if (likely(child->sighand != NULL)) { 3291da177e4SLinus Torvalds error = -EINVAL; 3301da177e4SLinus Torvalds spin_lock_irq(&child->sighand->siglock); 3311da177e4SLinus Torvalds if (likely(child->last_siginfo != NULL)) { 332e16b2781SRoland McGrath *info = *child->last_siginfo; 3331da177e4SLinus Torvalds error = 0; 3341da177e4SLinus Torvalds } 3351da177e4SLinus Torvalds spin_unlock_irq(&child->sighand->siglock); 3361da177e4SLinus Torvalds } 3371da177e4SLinus Torvalds read_unlock(&tasklist_lock); 3381da177e4SLinus Torvalds return error; 3391da177e4SLinus Torvalds } 3401da177e4SLinus Torvalds 341e16b2781SRoland McGrath static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info) 3421da177e4SLinus Torvalds { 3431da177e4SLinus Torvalds int error = -ESRCH; 3441da177e4SLinus Torvalds 3451da177e4SLinus Torvalds read_lock(&tasklist_lock); 3461da177e4SLinus Torvalds if (likely(child->sighand != NULL)) { 3471da177e4SLinus Torvalds error = -EINVAL; 3481da177e4SLinus Torvalds spin_lock_irq(&child->sighand->siglock); 3491da177e4SLinus Torvalds if (likely(child->last_siginfo != NULL)) { 350e16b2781SRoland McGrath *child->last_siginfo = *info; 3511da177e4SLinus Torvalds error = 0; 3521da177e4SLinus Torvalds } 3531da177e4SLinus Torvalds spin_unlock_irq(&child->sighand->siglock); 3541da177e4SLinus Torvalds } 3551da177e4SLinus Torvalds read_unlock(&tasklist_lock); 3561da177e4SLinus Torvalds return error; 3571da177e4SLinus Torvalds } 3581da177e4SLinus Torvalds 35936df29d7SRoland McGrath 36036df29d7SRoland McGrath #ifdef PTRACE_SINGLESTEP 36136df29d7SRoland McGrath #define is_singlestep(request) ((request) == PTRACE_SINGLESTEP) 36236df29d7SRoland McGrath #else 36336df29d7SRoland McGrath #define is_singlestep(request) 0 36436df29d7SRoland McGrath #endif 36536df29d7SRoland McGrath 3665b88abbfSRoland McGrath #ifdef PTRACE_SINGLEBLOCK 3675b88abbfSRoland McGrath #define is_singleblock(request) ((request) == PTRACE_SINGLEBLOCK) 3685b88abbfSRoland McGrath #else 3695b88abbfSRoland McGrath #define is_singleblock(request) 0 3705b88abbfSRoland McGrath #endif 3715b88abbfSRoland McGrath 37236df29d7SRoland McGrath #ifdef PTRACE_SYSEMU 37336df29d7SRoland McGrath #define is_sysemu_singlestep(request) ((request) == PTRACE_SYSEMU_SINGLESTEP) 37436df29d7SRoland McGrath #else 37536df29d7SRoland McGrath #define is_sysemu_singlestep(request) 0 37636df29d7SRoland McGrath #endif 37736df29d7SRoland McGrath 37836df29d7SRoland McGrath static int ptrace_resume(struct task_struct *child, long request, long data) 37936df29d7SRoland McGrath { 38036df29d7SRoland McGrath if (!valid_signal(data)) 38136df29d7SRoland McGrath return -EIO; 38236df29d7SRoland McGrath 38336df29d7SRoland McGrath if (request == PTRACE_SYSCALL) 38436df29d7SRoland McGrath set_tsk_thread_flag(child, TIF_SYSCALL_TRACE); 38536df29d7SRoland McGrath else 38636df29d7SRoland McGrath clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); 38736df29d7SRoland McGrath 38836df29d7SRoland McGrath #ifdef TIF_SYSCALL_EMU 38936df29d7SRoland McGrath if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP) 39036df29d7SRoland McGrath set_tsk_thread_flag(child, TIF_SYSCALL_EMU); 39136df29d7SRoland McGrath else 39236df29d7SRoland McGrath clear_tsk_thread_flag(child, TIF_SYSCALL_EMU); 39336df29d7SRoland McGrath #endif 39436df29d7SRoland McGrath 3955b88abbfSRoland McGrath if (is_singleblock(request)) { 3965b88abbfSRoland McGrath if (unlikely(!arch_has_block_step())) 3975b88abbfSRoland McGrath return -EIO; 3985b88abbfSRoland McGrath user_enable_block_step(child); 3995b88abbfSRoland McGrath } else if (is_singlestep(request) || is_sysemu_singlestep(request)) { 40036df29d7SRoland McGrath if (unlikely(!arch_has_single_step())) 40136df29d7SRoland McGrath return -EIO; 40236df29d7SRoland McGrath user_enable_single_step(child); 40336df29d7SRoland McGrath } 40436df29d7SRoland McGrath else 40536df29d7SRoland McGrath user_disable_single_step(child); 40636df29d7SRoland McGrath 40736df29d7SRoland McGrath child->exit_code = data; 40836df29d7SRoland McGrath wake_up_process(child); 40936df29d7SRoland McGrath 41036df29d7SRoland McGrath return 0; 41136df29d7SRoland McGrath } 41236df29d7SRoland McGrath 4131da177e4SLinus Torvalds int ptrace_request(struct task_struct *child, long request, 4141da177e4SLinus Torvalds long addr, long data) 4151da177e4SLinus Torvalds { 4161da177e4SLinus Torvalds int ret = -EIO; 417e16b2781SRoland McGrath siginfo_t siginfo; 4181da177e4SLinus Torvalds 4191da177e4SLinus Torvalds switch (request) { 42016c3e389SRoland McGrath case PTRACE_PEEKTEXT: 42116c3e389SRoland McGrath case PTRACE_PEEKDATA: 42216c3e389SRoland McGrath return generic_ptrace_peekdata(child, addr, data); 42316c3e389SRoland McGrath case PTRACE_POKETEXT: 42416c3e389SRoland McGrath case PTRACE_POKEDATA: 42516c3e389SRoland McGrath return generic_ptrace_pokedata(child, addr, data); 42616c3e389SRoland McGrath 4271da177e4SLinus Torvalds #ifdef PTRACE_OLDSETOPTIONS 4281da177e4SLinus Torvalds case PTRACE_OLDSETOPTIONS: 4291da177e4SLinus Torvalds #endif 4301da177e4SLinus Torvalds case PTRACE_SETOPTIONS: 4311da177e4SLinus Torvalds ret = ptrace_setoptions(child, data); 4321da177e4SLinus Torvalds break; 4331da177e4SLinus Torvalds case PTRACE_GETEVENTMSG: 4341da177e4SLinus Torvalds ret = put_user(child->ptrace_message, (unsigned long __user *) data); 4351da177e4SLinus Torvalds break; 436e16b2781SRoland McGrath 4371da177e4SLinus Torvalds case PTRACE_GETSIGINFO: 438e16b2781SRoland McGrath ret = ptrace_getsiginfo(child, &siginfo); 439e16b2781SRoland McGrath if (!ret) 440e16b2781SRoland McGrath ret = copy_siginfo_to_user((siginfo_t __user *) data, 441e16b2781SRoland McGrath &siginfo); 4421da177e4SLinus Torvalds break; 443e16b2781SRoland McGrath 4441da177e4SLinus Torvalds case PTRACE_SETSIGINFO: 445e16b2781SRoland McGrath if (copy_from_user(&siginfo, (siginfo_t __user *) data, 446e16b2781SRoland McGrath sizeof siginfo)) 447e16b2781SRoland McGrath ret = -EFAULT; 448e16b2781SRoland McGrath else 449e16b2781SRoland McGrath ret = ptrace_setsiginfo(child, &siginfo); 4501da177e4SLinus Torvalds break; 451e16b2781SRoland McGrath 4521bcf5482SAlexey Dobriyan case PTRACE_DETACH: /* detach a process that was attached. */ 4531bcf5482SAlexey Dobriyan ret = ptrace_detach(child, data); 4541bcf5482SAlexey Dobriyan break; 45536df29d7SRoland McGrath 45636df29d7SRoland McGrath #ifdef PTRACE_SINGLESTEP 45736df29d7SRoland McGrath case PTRACE_SINGLESTEP: 45836df29d7SRoland McGrath #endif 4595b88abbfSRoland McGrath #ifdef PTRACE_SINGLEBLOCK 4605b88abbfSRoland McGrath case PTRACE_SINGLEBLOCK: 4615b88abbfSRoland McGrath #endif 46236df29d7SRoland McGrath #ifdef PTRACE_SYSEMU 46336df29d7SRoland McGrath case PTRACE_SYSEMU: 46436df29d7SRoland McGrath case PTRACE_SYSEMU_SINGLESTEP: 46536df29d7SRoland McGrath #endif 46636df29d7SRoland McGrath case PTRACE_SYSCALL: 46736df29d7SRoland McGrath case PTRACE_CONT: 46836df29d7SRoland McGrath return ptrace_resume(child, request, data); 46936df29d7SRoland McGrath 47036df29d7SRoland McGrath case PTRACE_KILL: 47136df29d7SRoland McGrath if (child->exit_state) /* already dead */ 47236df29d7SRoland McGrath return 0; 47336df29d7SRoland McGrath return ptrace_resume(child, request, SIGKILL); 47436df29d7SRoland McGrath 4751da177e4SLinus Torvalds default: 4761da177e4SLinus Torvalds break; 4771da177e4SLinus Torvalds } 4781da177e4SLinus Torvalds 4791da177e4SLinus Torvalds return ret; 4801da177e4SLinus Torvalds } 481481bed45SChristoph Hellwig 4826b9c7ed8SChristoph Hellwig /** 4836b9c7ed8SChristoph Hellwig * ptrace_traceme -- helper for PTRACE_TRACEME 4846b9c7ed8SChristoph Hellwig * 4856b9c7ed8SChristoph Hellwig * Performs checks and sets PT_PTRACED. 4866b9c7ed8SChristoph Hellwig * Should be used by all ptrace implementations for PTRACE_TRACEME. 4876b9c7ed8SChristoph Hellwig */ 4886b9c7ed8SChristoph Hellwig int ptrace_traceme(void) 489481bed45SChristoph Hellwig { 490f5b40e36SLinus Torvalds int ret = -EPERM; 491481bed45SChristoph Hellwig 492481bed45SChristoph Hellwig /* 493481bed45SChristoph Hellwig * Are we already being traced? 494481bed45SChristoph Hellwig */ 495f5b40e36SLinus Torvalds task_lock(current); 496f5b40e36SLinus Torvalds if (!(current->ptrace & PT_PTRACED)) { 497006ebb40SStephen Smalley ret = security_ptrace(current->parent, current, 498006ebb40SStephen Smalley PTRACE_MODE_ATTACH); 499481bed45SChristoph Hellwig /* 500481bed45SChristoph Hellwig * Set the ptrace bit in the process ptrace flags. 501481bed45SChristoph Hellwig */ 502f5b40e36SLinus Torvalds if (!ret) 503481bed45SChristoph Hellwig current->ptrace |= PT_PTRACED; 504f5b40e36SLinus Torvalds } 505f5b40e36SLinus Torvalds task_unlock(current); 506f5b40e36SLinus Torvalds return ret; 507481bed45SChristoph Hellwig } 508481bed45SChristoph Hellwig 5096b9c7ed8SChristoph Hellwig /** 5106b9c7ed8SChristoph Hellwig * ptrace_get_task_struct -- grab a task struct reference for ptrace 5116b9c7ed8SChristoph Hellwig * @pid: process id to grab a task_struct reference of 5126b9c7ed8SChristoph Hellwig * 5136b9c7ed8SChristoph Hellwig * This function is a helper for ptrace implementations. It checks 5146b9c7ed8SChristoph Hellwig * permissions and then grabs a task struct for use of the actual 5156b9c7ed8SChristoph Hellwig * ptrace implementation. 5166b9c7ed8SChristoph Hellwig * 5176b9c7ed8SChristoph Hellwig * Returns the task_struct for @pid or an ERR_PTR() on failure. 5186b9c7ed8SChristoph Hellwig */ 5196b9c7ed8SChristoph Hellwig struct task_struct *ptrace_get_task_struct(pid_t pid) 5206b9c7ed8SChristoph Hellwig { 5216b9c7ed8SChristoph Hellwig struct task_struct *child; 5226b9c7ed8SChristoph Hellwig 523481bed45SChristoph Hellwig read_lock(&tasklist_lock); 524228ebcbeSPavel Emelyanov child = find_task_by_vpid(pid); 525481bed45SChristoph Hellwig if (child) 526481bed45SChristoph Hellwig get_task_struct(child); 527f400e198SSukadev Bhattiprolu 528481bed45SChristoph Hellwig read_unlock(&tasklist_lock); 529481bed45SChristoph Hellwig if (!child) 5306b9c7ed8SChristoph Hellwig return ERR_PTR(-ESRCH); 5316b9c7ed8SChristoph Hellwig return child; 532481bed45SChristoph Hellwig } 533481bed45SChristoph Hellwig 5340ac15559SChristoph Hellwig #ifndef arch_ptrace_attach 5350ac15559SChristoph Hellwig #define arch_ptrace_attach(child) do { } while (0) 5360ac15559SChristoph Hellwig #endif 5370ac15559SChristoph Hellwig 538481bed45SChristoph Hellwig asmlinkage long sys_ptrace(long request, long pid, long addr, long data) 539481bed45SChristoph Hellwig { 540481bed45SChristoph Hellwig struct task_struct *child; 541481bed45SChristoph Hellwig long ret; 542481bed45SChristoph Hellwig 543481bed45SChristoph Hellwig /* 544481bed45SChristoph Hellwig * This lock_kernel fixes a subtle race with suid exec 545481bed45SChristoph Hellwig */ 546481bed45SChristoph Hellwig lock_kernel(); 5476b9c7ed8SChristoph Hellwig if (request == PTRACE_TRACEME) { 5486b9c7ed8SChristoph Hellwig ret = ptrace_traceme(); 5496ea6dd93SHaavard Skinnemoen if (!ret) 5506ea6dd93SHaavard Skinnemoen arch_ptrace_attach(current); 551481bed45SChristoph Hellwig goto out; 5526b9c7ed8SChristoph Hellwig } 5536b9c7ed8SChristoph Hellwig 5546b9c7ed8SChristoph Hellwig child = ptrace_get_task_struct(pid); 5556b9c7ed8SChristoph Hellwig if (IS_ERR(child)) { 5566b9c7ed8SChristoph Hellwig ret = PTR_ERR(child); 5576b9c7ed8SChristoph Hellwig goto out; 5586b9c7ed8SChristoph Hellwig } 559481bed45SChristoph Hellwig 560481bed45SChristoph Hellwig if (request == PTRACE_ATTACH) { 561481bed45SChristoph Hellwig ret = ptrace_attach(child); 5620ac15559SChristoph Hellwig /* 5630ac15559SChristoph Hellwig * Some architectures need to do book-keeping after 5640ac15559SChristoph Hellwig * a ptrace attach. 5650ac15559SChristoph Hellwig */ 5660ac15559SChristoph Hellwig if (!ret) 5670ac15559SChristoph Hellwig arch_ptrace_attach(child); 568005f18dfSChristoph Hellwig goto out_put_task_struct; 569481bed45SChristoph Hellwig } 570481bed45SChristoph Hellwig 571481bed45SChristoph Hellwig ret = ptrace_check_attach(child, request == PTRACE_KILL); 572481bed45SChristoph Hellwig if (ret < 0) 573481bed45SChristoph Hellwig goto out_put_task_struct; 574481bed45SChristoph Hellwig 575481bed45SChristoph Hellwig ret = arch_ptrace(child, request, addr, data); 576481bed45SChristoph Hellwig if (ret < 0) 577481bed45SChristoph Hellwig goto out_put_task_struct; 578481bed45SChristoph Hellwig 579481bed45SChristoph Hellwig out_put_task_struct: 580481bed45SChristoph Hellwig put_task_struct(child); 581481bed45SChristoph Hellwig out: 582481bed45SChristoph Hellwig unlock_kernel(); 583481bed45SChristoph Hellwig return ret; 584481bed45SChristoph Hellwig } 58576647323SAlexey Dobriyan 58676647323SAlexey Dobriyan int generic_ptrace_peekdata(struct task_struct *tsk, long addr, long data) 58776647323SAlexey Dobriyan { 58876647323SAlexey Dobriyan unsigned long tmp; 58976647323SAlexey Dobriyan int copied; 59076647323SAlexey Dobriyan 59176647323SAlexey Dobriyan copied = access_process_vm(tsk, addr, &tmp, sizeof(tmp), 0); 59276647323SAlexey Dobriyan if (copied != sizeof(tmp)) 59376647323SAlexey Dobriyan return -EIO; 59476647323SAlexey Dobriyan return put_user(tmp, (unsigned long __user *)data); 59576647323SAlexey Dobriyan } 596f284ce72SAlexey Dobriyan 597f284ce72SAlexey Dobriyan int generic_ptrace_pokedata(struct task_struct *tsk, long addr, long data) 598f284ce72SAlexey Dobriyan { 599f284ce72SAlexey Dobriyan int copied; 600f284ce72SAlexey Dobriyan 601f284ce72SAlexey Dobriyan copied = access_process_vm(tsk, addr, &data, sizeof(data), 1); 602f284ce72SAlexey Dobriyan return (copied == sizeof(data)) ? 0 : -EIO; 603f284ce72SAlexey Dobriyan } 604032d82d9SRoland McGrath 6059d04d928SRoland McGrath #if defined CONFIG_COMPAT && defined __ARCH_WANT_COMPAT_SYS_PTRACE 606032d82d9SRoland McGrath #include <linux/compat.h> 607032d82d9SRoland McGrath 608032d82d9SRoland McGrath int compat_ptrace_request(struct task_struct *child, compat_long_t request, 609032d82d9SRoland McGrath compat_ulong_t addr, compat_ulong_t data) 610032d82d9SRoland McGrath { 611032d82d9SRoland McGrath compat_ulong_t __user *datap = compat_ptr(data); 612032d82d9SRoland McGrath compat_ulong_t word; 613e16b2781SRoland McGrath siginfo_t siginfo; 614032d82d9SRoland McGrath int ret; 615032d82d9SRoland McGrath 616032d82d9SRoland McGrath switch (request) { 617032d82d9SRoland McGrath case PTRACE_PEEKTEXT: 618032d82d9SRoland McGrath case PTRACE_PEEKDATA: 619032d82d9SRoland McGrath ret = access_process_vm(child, addr, &word, sizeof(word), 0); 620032d82d9SRoland McGrath if (ret != sizeof(word)) 621032d82d9SRoland McGrath ret = -EIO; 622032d82d9SRoland McGrath else 623032d82d9SRoland McGrath ret = put_user(word, datap); 624032d82d9SRoland McGrath break; 625032d82d9SRoland McGrath 626032d82d9SRoland McGrath case PTRACE_POKETEXT: 627032d82d9SRoland McGrath case PTRACE_POKEDATA: 628032d82d9SRoland McGrath ret = access_process_vm(child, addr, &data, sizeof(data), 1); 629032d82d9SRoland McGrath ret = (ret != sizeof(data) ? -EIO : 0); 630032d82d9SRoland McGrath break; 631032d82d9SRoland McGrath 632032d82d9SRoland McGrath case PTRACE_GETEVENTMSG: 633032d82d9SRoland McGrath ret = put_user((compat_ulong_t) child->ptrace_message, datap); 634032d82d9SRoland McGrath break; 635032d82d9SRoland McGrath 636e16b2781SRoland McGrath case PTRACE_GETSIGINFO: 637e16b2781SRoland McGrath ret = ptrace_getsiginfo(child, &siginfo); 638e16b2781SRoland McGrath if (!ret) 639e16b2781SRoland McGrath ret = copy_siginfo_to_user32( 640e16b2781SRoland McGrath (struct compat_siginfo __user *) datap, 641e16b2781SRoland McGrath &siginfo); 642e16b2781SRoland McGrath break; 643e16b2781SRoland McGrath 644e16b2781SRoland McGrath case PTRACE_SETSIGINFO: 645e16b2781SRoland McGrath memset(&siginfo, 0, sizeof siginfo); 646e16b2781SRoland McGrath if (copy_siginfo_from_user32( 647e16b2781SRoland McGrath &siginfo, (struct compat_siginfo __user *) datap)) 648e16b2781SRoland McGrath ret = -EFAULT; 649e16b2781SRoland McGrath else 650e16b2781SRoland McGrath ret = ptrace_setsiginfo(child, &siginfo); 651e16b2781SRoland McGrath break; 652e16b2781SRoland McGrath 653032d82d9SRoland McGrath default: 654032d82d9SRoland McGrath ret = ptrace_request(child, request, addr, data); 655032d82d9SRoland McGrath } 656032d82d9SRoland McGrath 657032d82d9SRoland McGrath return ret; 658032d82d9SRoland McGrath } 659c269f196SRoland McGrath 660c269f196SRoland McGrath asmlinkage long compat_sys_ptrace(compat_long_t request, compat_long_t pid, 661c269f196SRoland McGrath compat_long_t addr, compat_long_t data) 662c269f196SRoland McGrath { 663c269f196SRoland McGrath struct task_struct *child; 664c269f196SRoland McGrath long ret; 665c269f196SRoland McGrath 666c269f196SRoland McGrath /* 667c269f196SRoland McGrath * This lock_kernel fixes a subtle race with suid exec 668c269f196SRoland McGrath */ 669c269f196SRoland McGrath lock_kernel(); 670c269f196SRoland McGrath if (request == PTRACE_TRACEME) { 671c269f196SRoland McGrath ret = ptrace_traceme(); 672c269f196SRoland McGrath goto out; 673c269f196SRoland McGrath } 674c269f196SRoland McGrath 675c269f196SRoland McGrath child = ptrace_get_task_struct(pid); 676c269f196SRoland McGrath if (IS_ERR(child)) { 677c269f196SRoland McGrath ret = PTR_ERR(child); 678c269f196SRoland McGrath goto out; 679c269f196SRoland McGrath } 680c269f196SRoland McGrath 681c269f196SRoland McGrath if (request == PTRACE_ATTACH) { 682c269f196SRoland McGrath ret = ptrace_attach(child); 683c269f196SRoland McGrath /* 684c269f196SRoland McGrath * Some architectures need to do book-keeping after 685c269f196SRoland McGrath * a ptrace attach. 686c269f196SRoland McGrath */ 687c269f196SRoland McGrath if (!ret) 688c269f196SRoland McGrath arch_ptrace_attach(child); 689c269f196SRoland McGrath goto out_put_task_struct; 690c269f196SRoland McGrath } 691c269f196SRoland McGrath 692c269f196SRoland McGrath ret = ptrace_check_attach(child, request == PTRACE_KILL); 693c269f196SRoland McGrath if (!ret) 694c269f196SRoland McGrath ret = compat_arch_ptrace(child, request, addr, data); 695c269f196SRoland McGrath 696c269f196SRoland McGrath out_put_task_struct: 697c269f196SRoland McGrath put_task_struct(child); 698c269f196SRoland McGrath out: 699c269f196SRoland McGrath unlock_kernel(); 700c269f196SRoland McGrath return ret; 701c269f196SRoland McGrath } 7029d04d928SRoland McGrath #endif /* CONFIG_COMPAT && __ARCH_WANT_COMPAT_SYS_PTRACE */ 703