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> 221da177e4SLinus Torvalds 231da177e4SLinus Torvalds #include <asm/pgtable.h> 241da177e4SLinus Torvalds #include <asm/uaccess.h> 251da177e4SLinus Torvalds 261da177e4SLinus Torvalds /* 271da177e4SLinus Torvalds * ptrace a task: make the debugger its new parent and 281da177e4SLinus Torvalds * move it to the ptrace list. 291da177e4SLinus Torvalds * 301da177e4SLinus Torvalds * Must be called with the tasklist lock write-held. 311da177e4SLinus Torvalds */ 3236c8b586SIngo Molnar void __ptrace_link(struct task_struct *child, struct task_struct *new_parent) 331da177e4SLinus Torvalds { 34524223caSEric Sesterhenn BUG_ON(!list_empty(&child->ptrace_list)); 351da177e4SLinus Torvalds if (child->parent == new_parent) 361da177e4SLinus Torvalds return; 371da177e4SLinus Torvalds list_add(&child->ptrace_list, &child->parent->ptrace_children); 389b678eceSOleg Nesterov remove_parent(child); 391da177e4SLinus Torvalds child->parent = new_parent; 409b678eceSOleg Nesterov add_parent(child); 411da177e4SLinus Torvalds } 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds /* 441da177e4SLinus Torvalds * Turn a tracing stop into a normal stop now, since with no tracer there 451da177e4SLinus Torvalds * would be no way to wake it up with SIGCONT or SIGKILL. If there was a 461da177e4SLinus Torvalds * signal sent that would resume the child, but didn't because it was in 471da177e4SLinus Torvalds * TASK_TRACED, resume it now. 481da177e4SLinus Torvalds * Requires that irqs be disabled. 491da177e4SLinus Torvalds */ 5036c8b586SIngo Molnar void ptrace_untrace(struct task_struct *child) 511da177e4SLinus Torvalds { 521da177e4SLinus Torvalds spin_lock(&child->sighand->siglock); 531da177e4SLinus Torvalds if (child->state == TASK_TRACED) { 541da177e4SLinus Torvalds if (child->signal->flags & SIGNAL_STOP_STOPPED) { 551da177e4SLinus Torvalds child->state = TASK_STOPPED; 561da177e4SLinus Torvalds } else { 571da177e4SLinus Torvalds signal_wake_up(child, 1); 581da177e4SLinus Torvalds } 591da177e4SLinus Torvalds } 601da177e4SLinus Torvalds spin_unlock(&child->sighand->siglock); 611da177e4SLinus Torvalds } 621da177e4SLinus Torvalds 631da177e4SLinus Torvalds /* 641da177e4SLinus Torvalds * unptrace a task: move it back to its original parent and 651da177e4SLinus Torvalds * remove it from the ptrace list. 661da177e4SLinus Torvalds * 671da177e4SLinus Torvalds * Must be called with the tasklist lock write-held. 681da177e4SLinus Torvalds */ 6936c8b586SIngo Molnar void __ptrace_unlink(struct task_struct *child) 701da177e4SLinus Torvalds { 715ecfbae0SOleg Nesterov BUG_ON(!child->ptrace); 725ecfbae0SOleg Nesterov 731da177e4SLinus Torvalds child->ptrace = 0; 741da177e4SLinus Torvalds if (!list_empty(&child->ptrace_list)) { 751da177e4SLinus Torvalds list_del_init(&child->ptrace_list); 769b678eceSOleg Nesterov remove_parent(child); 771da177e4SLinus Torvalds child->parent = child->real_parent; 789b678eceSOleg Nesterov add_parent(child); 791da177e4SLinus Torvalds } 801da177e4SLinus Torvalds 81e57a5059SRoland McGrath if (child->state == TASK_TRACED) 821da177e4SLinus Torvalds ptrace_untrace(child); 831da177e4SLinus Torvalds } 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds /* 861da177e4SLinus Torvalds * Check that we have indeed attached to the thing.. 871da177e4SLinus Torvalds */ 881da177e4SLinus Torvalds int ptrace_check_attach(struct task_struct *child, int kill) 891da177e4SLinus Torvalds { 901da177e4SLinus Torvalds int ret = -ESRCH; 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds /* 931da177e4SLinus Torvalds * We take the read lock around doing both checks to close a 941da177e4SLinus Torvalds * possible race where someone else was tracing our child and 951da177e4SLinus Torvalds * detached between these two checks. After this locked check, 961da177e4SLinus Torvalds * we are sure that this is our traced child and that can only 971da177e4SLinus Torvalds * be changed by us so it's not changing right after this. 981da177e4SLinus Torvalds */ 991da177e4SLinus Torvalds read_lock(&tasklist_lock); 1001da177e4SLinus Torvalds if ((child->ptrace & PT_PTRACED) && child->parent == current && 1011da177e4SLinus Torvalds (!(child->ptrace & PT_ATTACHED) || child->real_parent != current) 1021da177e4SLinus Torvalds && child->signal != NULL) { 1031da177e4SLinus Torvalds ret = 0; 1041da177e4SLinus Torvalds spin_lock_irq(&child->sighand->siglock); 1051da177e4SLinus Torvalds if (child->state == TASK_STOPPED) { 1061da177e4SLinus Torvalds child->state = TASK_TRACED; 1071da177e4SLinus Torvalds } else if (child->state != TASK_TRACED && !kill) { 1081da177e4SLinus Torvalds ret = -ESRCH; 1091da177e4SLinus Torvalds } 1101da177e4SLinus Torvalds spin_unlock_irq(&child->sighand->siglock); 1111da177e4SLinus Torvalds } 1121da177e4SLinus Torvalds read_unlock(&tasklist_lock); 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds if (!ret && !kill) { 1151da177e4SLinus Torvalds wait_task_inactive(child); 1161da177e4SLinus Torvalds } 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds /* All systems go.. */ 1191da177e4SLinus Torvalds return ret; 1201da177e4SLinus Torvalds } 1211da177e4SLinus Torvalds 122ab8d11beSMiklos Szeredi static int may_attach(struct task_struct *task) 123ab8d11beSMiklos Szeredi { 124df26c40eSEric W. Biederman /* May we inspect the given task? 125df26c40eSEric W. Biederman * This check is used both for attaching with ptrace 126df26c40eSEric W. Biederman * and for allowing access to sensitive information in /proc. 127df26c40eSEric W. Biederman * 128df26c40eSEric W. Biederman * ptrace_attach denies several cases that /proc allows 129df26c40eSEric W. Biederman * because setting up the necessary parent/child relationship 130df26c40eSEric W. Biederman * or halting the specified task is impossible. 131df26c40eSEric W. Biederman */ 132df26c40eSEric W. Biederman int dumpable = 0; 133df26c40eSEric W. Biederman /* Don't let security modules deny introspection */ 134df26c40eSEric W. Biederman if (task == current) 135df26c40eSEric W. Biederman return 0; 136ab8d11beSMiklos Szeredi if (((current->uid != task->euid) || 137ab8d11beSMiklos Szeredi (current->uid != task->suid) || 138ab8d11beSMiklos Szeredi (current->uid != task->uid) || 139ab8d11beSMiklos Szeredi (current->gid != task->egid) || 140ab8d11beSMiklos Szeredi (current->gid != task->sgid) || 141ab8d11beSMiklos Szeredi (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE)) 142ab8d11beSMiklos Szeredi return -EPERM; 143ab8d11beSMiklos Szeredi smp_rmb(); 144df26c40eSEric W. Biederman if (task->mm) 145df26c40eSEric W. Biederman dumpable = task->mm->dumpable; 146df26c40eSEric W. Biederman if (!dumpable && !capable(CAP_SYS_PTRACE)) 147ab8d11beSMiklos Szeredi return -EPERM; 148ab8d11beSMiklos Szeredi 149ab8d11beSMiklos Szeredi return security_ptrace(current, task); 150ab8d11beSMiklos Szeredi } 151ab8d11beSMiklos Szeredi 152ab8d11beSMiklos Szeredi int ptrace_may_attach(struct task_struct *task) 153ab8d11beSMiklos Szeredi { 154ab8d11beSMiklos Szeredi int err; 155ab8d11beSMiklos Szeredi task_lock(task); 156ab8d11beSMiklos Szeredi err = may_attach(task); 157ab8d11beSMiklos Szeredi task_unlock(task); 158ab8d11beSMiklos Szeredi return !err; 159ab8d11beSMiklos Szeredi } 160ab8d11beSMiklos Szeredi 1611da177e4SLinus Torvalds int ptrace_attach(struct task_struct *task) 1621da177e4SLinus Torvalds { 1631da177e4SLinus Torvalds int retval; 1646175ecfeSSripathi Kodi unsigned long flags; 165f5b40e36SLinus Torvalds 166a5cb013dSAl Viro audit_ptrace(task); 167a5cb013dSAl Viro 1681da177e4SLinus Torvalds retval = -EPERM; 1691da177e4SLinus Torvalds if (task->pid <= 1) 170f5b40e36SLinus Torvalds goto out; 17128d838ccSLinus Torvalds if (task->tgid == current->tgid) 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; 198ab8d11beSMiklos Szeredi retval = may_attach(task); 1991da177e4SLinus Torvalds if (retval) 2001da177e4SLinus Torvalds goto bad; 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds /* Go */ 2031da177e4SLinus Torvalds task->ptrace |= PT_PTRACED | ((task->real_parent != current) 2041da177e4SLinus Torvalds ? PT_ATTACHED : 0); 2051da177e4SLinus Torvalds if (capable(CAP_SYS_PTRACE)) 2061da177e4SLinus Torvalds task->ptrace |= PT_PTRACE_CAP; 2071da177e4SLinus Torvalds 2081da177e4SLinus Torvalds __ptrace_link(task, current); 2091da177e4SLinus Torvalds 2101da177e4SLinus Torvalds force_sig_specific(SIGSTOP, task); 2111da177e4SLinus Torvalds 2121da177e4SLinus Torvalds bad: 2136175ecfeSSripathi Kodi write_unlock_irqrestore(&tasklist_lock, flags); 2141da177e4SLinus Torvalds task_unlock(task); 215f5b40e36SLinus Torvalds out: 2161da177e4SLinus Torvalds return retval; 2171da177e4SLinus Torvalds } 2181da177e4SLinus Torvalds 219d5f70c00SOleg Nesterov static inline void __ptrace_detach(struct task_struct *child, unsigned int data) 2205ecfbae0SOleg Nesterov { 2215ecfbae0SOleg Nesterov child->exit_code = data; 2225ecfbae0SOleg Nesterov /* .. re-parent .. */ 2235ecfbae0SOleg Nesterov __ptrace_unlink(child); 2245ecfbae0SOleg Nesterov /* .. and wake it up. */ 2255ecfbae0SOleg Nesterov if (child->exit_state != EXIT_ZOMBIE) 2265ecfbae0SOleg Nesterov wake_up_process(child); 2275ecfbae0SOleg Nesterov } 2285ecfbae0SOleg Nesterov 2291da177e4SLinus Torvalds int ptrace_detach(struct task_struct *child, unsigned int data) 2301da177e4SLinus Torvalds { 2317ed20e1aSJesper Juhl if (!valid_signal(data)) 2321da177e4SLinus Torvalds return -EIO; 2331da177e4SLinus Torvalds 2341da177e4SLinus Torvalds /* Architecture-specific hardware disable .. */ 2351da177e4SLinus Torvalds ptrace_disable(child); 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds write_lock_irq(&tasklist_lock); 238d5f70c00SOleg Nesterov /* protect against de_thread()->release_task() */ 2395ecfbae0SOleg Nesterov if (child->ptrace) 2405ecfbae0SOleg Nesterov __ptrace_detach(child, data); 2411da177e4SLinus Torvalds write_unlock_irq(&tasklist_lock); 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds return 0; 2441da177e4SLinus Torvalds } 2451da177e4SLinus Torvalds 2461da177e4SLinus Torvalds int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len) 2471da177e4SLinus Torvalds { 2481da177e4SLinus Torvalds int copied = 0; 2491da177e4SLinus Torvalds 2501da177e4SLinus Torvalds while (len > 0) { 2511da177e4SLinus Torvalds char buf[128]; 2521da177e4SLinus Torvalds int this_len, retval; 2531da177e4SLinus Torvalds 2541da177e4SLinus Torvalds this_len = (len > sizeof(buf)) ? sizeof(buf) : len; 2551da177e4SLinus Torvalds retval = access_process_vm(tsk, src, buf, this_len, 0); 2561da177e4SLinus Torvalds if (!retval) { 2571da177e4SLinus Torvalds if (copied) 2581da177e4SLinus Torvalds break; 2591da177e4SLinus Torvalds return -EIO; 2601da177e4SLinus Torvalds } 2611da177e4SLinus Torvalds if (copy_to_user(dst, buf, retval)) 2621da177e4SLinus Torvalds return -EFAULT; 2631da177e4SLinus Torvalds copied += retval; 2641da177e4SLinus Torvalds src += retval; 2651da177e4SLinus Torvalds dst += retval; 2661da177e4SLinus Torvalds len -= retval; 2671da177e4SLinus Torvalds } 2681da177e4SLinus Torvalds return copied; 2691da177e4SLinus Torvalds } 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len) 2721da177e4SLinus Torvalds { 2731da177e4SLinus Torvalds int copied = 0; 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds while (len > 0) { 2761da177e4SLinus Torvalds char buf[128]; 2771da177e4SLinus Torvalds int this_len, retval; 2781da177e4SLinus Torvalds 2791da177e4SLinus Torvalds this_len = (len > sizeof(buf)) ? sizeof(buf) : len; 2801da177e4SLinus Torvalds if (copy_from_user(buf, src, this_len)) 2811da177e4SLinus Torvalds return -EFAULT; 2821da177e4SLinus Torvalds retval = access_process_vm(tsk, dst, buf, this_len, 1); 2831da177e4SLinus Torvalds if (!retval) { 2841da177e4SLinus Torvalds if (copied) 2851da177e4SLinus Torvalds break; 2861da177e4SLinus Torvalds return -EIO; 2871da177e4SLinus Torvalds } 2881da177e4SLinus Torvalds copied += retval; 2891da177e4SLinus Torvalds src += retval; 2901da177e4SLinus Torvalds dst += retval; 2911da177e4SLinus Torvalds len -= retval; 2921da177e4SLinus Torvalds } 2931da177e4SLinus Torvalds return copied; 2941da177e4SLinus Torvalds } 2951da177e4SLinus Torvalds 2961da177e4SLinus Torvalds static int ptrace_setoptions(struct task_struct *child, long data) 2971da177e4SLinus Torvalds { 2981da177e4SLinus Torvalds child->ptrace &= ~PT_TRACE_MASK; 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds if (data & PTRACE_O_TRACESYSGOOD) 3011da177e4SLinus Torvalds child->ptrace |= PT_TRACESYSGOOD; 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds if (data & PTRACE_O_TRACEFORK) 3041da177e4SLinus Torvalds child->ptrace |= PT_TRACE_FORK; 3051da177e4SLinus Torvalds 3061da177e4SLinus Torvalds if (data & PTRACE_O_TRACEVFORK) 3071da177e4SLinus Torvalds child->ptrace |= PT_TRACE_VFORK; 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds if (data & PTRACE_O_TRACECLONE) 3101da177e4SLinus Torvalds child->ptrace |= PT_TRACE_CLONE; 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds if (data & PTRACE_O_TRACEEXEC) 3131da177e4SLinus Torvalds child->ptrace |= PT_TRACE_EXEC; 3141da177e4SLinus Torvalds 3151da177e4SLinus Torvalds if (data & PTRACE_O_TRACEVFORKDONE) 3161da177e4SLinus Torvalds child->ptrace |= PT_TRACE_VFORK_DONE; 3171da177e4SLinus Torvalds 3181da177e4SLinus Torvalds if (data & PTRACE_O_TRACEEXIT) 3191da177e4SLinus Torvalds child->ptrace |= PT_TRACE_EXIT; 3201da177e4SLinus Torvalds 3211da177e4SLinus Torvalds return (data & ~PTRACE_O_MASK) ? -EINVAL : 0; 3221da177e4SLinus Torvalds } 3231da177e4SLinus Torvalds 3241da177e4SLinus Torvalds static int ptrace_getsiginfo(struct task_struct *child, siginfo_t __user * data) 3251da177e4SLinus Torvalds { 3261da177e4SLinus Torvalds siginfo_t lastinfo; 3271da177e4SLinus Torvalds int error = -ESRCH; 3281da177e4SLinus Torvalds 3291da177e4SLinus Torvalds read_lock(&tasklist_lock); 3301da177e4SLinus Torvalds if (likely(child->sighand != NULL)) { 3311da177e4SLinus Torvalds error = -EINVAL; 3321da177e4SLinus Torvalds spin_lock_irq(&child->sighand->siglock); 3331da177e4SLinus Torvalds if (likely(child->last_siginfo != NULL)) { 3341da177e4SLinus Torvalds lastinfo = *child->last_siginfo; 3351da177e4SLinus Torvalds error = 0; 3361da177e4SLinus Torvalds } 3371da177e4SLinus Torvalds spin_unlock_irq(&child->sighand->siglock); 3381da177e4SLinus Torvalds } 3391da177e4SLinus Torvalds read_unlock(&tasklist_lock); 3401da177e4SLinus Torvalds if (!error) 3411da177e4SLinus Torvalds return copy_siginfo_to_user(data, &lastinfo); 3421da177e4SLinus Torvalds return error; 3431da177e4SLinus Torvalds } 3441da177e4SLinus Torvalds 3451da177e4SLinus Torvalds static int ptrace_setsiginfo(struct task_struct *child, siginfo_t __user * data) 3461da177e4SLinus Torvalds { 3471da177e4SLinus Torvalds siginfo_t newinfo; 3481da177e4SLinus Torvalds int error = -ESRCH; 3491da177e4SLinus Torvalds 3501da177e4SLinus Torvalds if (copy_from_user(&newinfo, data, sizeof (siginfo_t))) 3511da177e4SLinus Torvalds return -EFAULT; 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds read_lock(&tasklist_lock); 3541da177e4SLinus Torvalds if (likely(child->sighand != NULL)) { 3551da177e4SLinus Torvalds error = -EINVAL; 3561da177e4SLinus Torvalds spin_lock_irq(&child->sighand->siglock); 3571da177e4SLinus Torvalds if (likely(child->last_siginfo != NULL)) { 3581da177e4SLinus Torvalds *child->last_siginfo = newinfo; 3591da177e4SLinus Torvalds error = 0; 3601da177e4SLinus Torvalds } 3611da177e4SLinus Torvalds spin_unlock_irq(&child->sighand->siglock); 3621da177e4SLinus Torvalds } 3631da177e4SLinus Torvalds read_unlock(&tasklist_lock); 3641da177e4SLinus Torvalds return error; 3651da177e4SLinus Torvalds } 3661da177e4SLinus Torvalds 3671da177e4SLinus Torvalds int ptrace_request(struct task_struct *child, long request, 3681da177e4SLinus Torvalds long addr, long data) 3691da177e4SLinus Torvalds { 3701da177e4SLinus Torvalds int ret = -EIO; 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds switch (request) { 3731da177e4SLinus Torvalds #ifdef PTRACE_OLDSETOPTIONS 3741da177e4SLinus Torvalds case PTRACE_OLDSETOPTIONS: 3751da177e4SLinus Torvalds #endif 3761da177e4SLinus Torvalds case PTRACE_SETOPTIONS: 3771da177e4SLinus Torvalds ret = ptrace_setoptions(child, data); 3781da177e4SLinus Torvalds break; 3791da177e4SLinus Torvalds case PTRACE_GETEVENTMSG: 3801da177e4SLinus Torvalds ret = put_user(child->ptrace_message, (unsigned long __user *) data); 3811da177e4SLinus Torvalds break; 3821da177e4SLinus Torvalds case PTRACE_GETSIGINFO: 3831da177e4SLinus Torvalds ret = ptrace_getsiginfo(child, (siginfo_t __user *) data); 3841da177e4SLinus Torvalds break; 3851da177e4SLinus Torvalds case PTRACE_SETSIGINFO: 3861da177e4SLinus Torvalds ret = ptrace_setsiginfo(child, (siginfo_t __user *) data); 3871da177e4SLinus Torvalds break; 3881da177e4SLinus Torvalds default: 3891da177e4SLinus Torvalds break; 3901da177e4SLinus Torvalds } 3911da177e4SLinus Torvalds 3921da177e4SLinus Torvalds return ret; 3931da177e4SLinus Torvalds } 394481bed45SChristoph Hellwig 3956b9c7ed8SChristoph Hellwig /** 3966b9c7ed8SChristoph Hellwig * ptrace_traceme -- helper for PTRACE_TRACEME 3976b9c7ed8SChristoph Hellwig * 3986b9c7ed8SChristoph Hellwig * Performs checks and sets PT_PTRACED. 3996b9c7ed8SChristoph Hellwig * Should be used by all ptrace implementations for PTRACE_TRACEME. 4006b9c7ed8SChristoph Hellwig */ 4016b9c7ed8SChristoph Hellwig int ptrace_traceme(void) 402481bed45SChristoph Hellwig { 403f5b40e36SLinus Torvalds int ret = -EPERM; 404481bed45SChristoph Hellwig 405481bed45SChristoph Hellwig /* 406481bed45SChristoph Hellwig * Are we already being traced? 407481bed45SChristoph Hellwig */ 408f5b40e36SLinus Torvalds task_lock(current); 409f5b40e36SLinus Torvalds if (!(current->ptrace & PT_PTRACED)) { 410481bed45SChristoph Hellwig ret = security_ptrace(current->parent, current); 411481bed45SChristoph Hellwig /* 412481bed45SChristoph Hellwig * Set the ptrace bit in the process ptrace flags. 413481bed45SChristoph Hellwig */ 414f5b40e36SLinus Torvalds if (!ret) 415481bed45SChristoph Hellwig current->ptrace |= PT_PTRACED; 416f5b40e36SLinus Torvalds } 417f5b40e36SLinus Torvalds task_unlock(current); 418f5b40e36SLinus Torvalds return ret; 419481bed45SChristoph Hellwig } 420481bed45SChristoph Hellwig 4216b9c7ed8SChristoph Hellwig /** 4226b9c7ed8SChristoph Hellwig * ptrace_get_task_struct -- grab a task struct reference for ptrace 4236b9c7ed8SChristoph Hellwig * @pid: process id to grab a task_struct reference of 4246b9c7ed8SChristoph Hellwig * 4256b9c7ed8SChristoph Hellwig * This function is a helper for ptrace implementations. It checks 4266b9c7ed8SChristoph Hellwig * permissions and then grabs a task struct for use of the actual 4276b9c7ed8SChristoph Hellwig * ptrace implementation. 4286b9c7ed8SChristoph Hellwig * 4296b9c7ed8SChristoph Hellwig * Returns the task_struct for @pid or an ERR_PTR() on failure. 4306b9c7ed8SChristoph Hellwig */ 4316b9c7ed8SChristoph Hellwig struct task_struct *ptrace_get_task_struct(pid_t pid) 4326b9c7ed8SChristoph Hellwig { 4336b9c7ed8SChristoph Hellwig struct task_struct *child; 4346b9c7ed8SChristoph Hellwig 435481bed45SChristoph Hellwig /* 4366b9c7ed8SChristoph Hellwig * Tracing init is not allowed. 437481bed45SChristoph Hellwig */ 438481bed45SChristoph Hellwig if (pid == 1) 4396b9c7ed8SChristoph Hellwig return ERR_PTR(-EPERM); 440481bed45SChristoph Hellwig 441481bed45SChristoph Hellwig read_lock(&tasklist_lock); 442481bed45SChristoph Hellwig child = find_task_by_pid(pid); 443481bed45SChristoph Hellwig if (child) 444481bed45SChristoph Hellwig get_task_struct(child); 445f400e198SSukadev Bhattiprolu 446481bed45SChristoph Hellwig read_unlock(&tasklist_lock); 447481bed45SChristoph Hellwig if (!child) 4486b9c7ed8SChristoph Hellwig return ERR_PTR(-ESRCH); 4496b9c7ed8SChristoph Hellwig return child; 450481bed45SChristoph Hellwig } 451481bed45SChristoph Hellwig 4526b9c7ed8SChristoph Hellwig #ifndef __ARCH_SYS_PTRACE 453481bed45SChristoph Hellwig asmlinkage long sys_ptrace(long request, long pid, long addr, long data) 454481bed45SChristoph Hellwig { 455481bed45SChristoph Hellwig struct task_struct *child; 456481bed45SChristoph Hellwig long ret; 457481bed45SChristoph Hellwig 458481bed45SChristoph Hellwig /* 459481bed45SChristoph Hellwig * This lock_kernel fixes a subtle race with suid exec 460481bed45SChristoph Hellwig */ 461481bed45SChristoph Hellwig lock_kernel(); 4626b9c7ed8SChristoph Hellwig if (request == PTRACE_TRACEME) { 4636b9c7ed8SChristoph Hellwig ret = ptrace_traceme(); 464481bed45SChristoph Hellwig goto out; 4656b9c7ed8SChristoph Hellwig } 4666b9c7ed8SChristoph Hellwig 4676b9c7ed8SChristoph Hellwig child = ptrace_get_task_struct(pid); 4686b9c7ed8SChristoph Hellwig if (IS_ERR(child)) { 4696b9c7ed8SChristoph Hellwig ret = PTR_ERR(child); 4706b9c7ed8SChristoph Hellwig goto out; 4716b9c7ed8SChristoph Hellwig } 472481bed45SChristoph Hellwig 473481bed45SChristoph Hellwig if (request == PTRACE_ATTACH) { 474481bed45SChristoph Hellwig ret = ptrace_attach(child); 475005f18dfSChristoph Hellwig goto out_put_task_struct; 476481bed45SChristoph Hellwig } 477481bed45SChristoph Hellwig 478481bed45SChristoph Hellwig ret = ptrace_check_attach(child, request == PTRACE_KILL); 479481bed45SChristoph Hellwig if (ret < 0) 480481bed45SChristoph Hellwig goto out_put_task_struct; 481481bed45SChristoph Hellwig 482481bed45SChristoph Hellwig ret = arch_ptrace(child, request, addr, data); 483481bed45SChristoph Hellwig if (ret < 0) 484481bed45SChristoph Hellwig goto out_put_task_struct; 485481bed45SChristoph Hellwig 486481bed45SChristoph Hellwig out_put_task_struct: 487481bed45SChristoph Hellwig put_task_struct(child); 488481bed45SChristoph Hellwig out: 489481bed45SChristoph Hellwig unlock_kernel(); 490481bed45SChristoph Hellwig return ret; 491481bed45SChristoph Hellwig } 492481bed45SChristoph Hellwig #endif /* __ARCH_SYS_PTRACE */ 49376647323SAlexey Dobriyan 49476647323SAlexey Dobriyan int generic_ptrace_peekdata(struct task_struct *tsk, long addr, long data) 49576647323SAlexey Dobriyan { 49676647323SAlexey Dobriyan unsigned long tmp; 49776647323SAlexey Dobriyan int copied; 49876647323SAlexey Dobriyan 49976647323SAlexey Dobriyan copied = access_process_vm(tsk, addr, &tmp, sizeof(tmp), 0); 50076647323SAlexey Dobriyan if (copied != sizeof(tmp)) 50176647323SAlexey Dobriyan return -EIO; 50276647323SAlexey Dobriyan return put_user(tmp, (unsigned long __user *)data); 50376647323SAlexey Dobriyan } 504f284ce72SAlexey Dobriyan 505f284ce72SAlexey Dobriyan int generic_ptrace_pokedata(struct task_struct *tsk, long addr, long data) 506f284ce72SAlexey Dobriyan { 507f284ce72SAlexey Dobriyan int copied; 508f284ce72SAlexey Dobriyan 509f284ce72SAlexey Dobriyan copied = access_process_vm(tsk, addr, &data, sizeof(data), 1); 510f284ce72SAlexey Dobriyan return (copied == sizeof(data)) ? 0 : -EIO; 511f284ce72SAlexey Dobriyan } 512