Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/kernel/ptrace.c |
| 3 | * |
| 4 | * (C) Copyright 1999 Linus Torvalds |
| 5 | * |
| 6 | * Common interfaces for "ptrace()" which we do not want |
| 7 | * to continually duplicate across every architecture. |
| 8 | */ |
| 9 | |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 10 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/module.h> |
| 12 | #include <linux/sched.h> |
| 13 | #include <linux/errno.h> |
| 14 | #include <linux/mm.h> |
| 15 | #include <linux/highmem.h> |
| 16 | #include <linux/pagemap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/ptrace.h> |
| 18 | #include <linux/security.h> |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 19 | #include <linux/signal.h> |
Al Viro | a5cb013 | 2007-03-20 13:58:35 -0400 | [diff] [blame] | 20 | #include <linux/audit.h> |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 21 | #include <linux/pid_namespace.h> |
Adrian Bunk | f17d30a | 2008-02-06 01:36:44 -0800 | [diff] [blame] | 22 | #include <linux/syscalls.h> |
Roland McGrath | 3a70970 | 2009-04-07 23:21:06 -0700 | [diff] [blame] | 23 | #include <linux/uaccess.h> |
Suresh Siddha | 2225a12 | 2010-02-11 11:51:00 -0800 | [diff] [blame] | 24 | #include <linux/regset.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Markus Metzger | bf53de9 | 2008-12-19 15:10:24 +0100 | [diff] [blame] | 26 | |
| 27 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | * ptrace a task: make the debugger its new parent and |
| 29 | * move it to the ptrace list. |
| 30 | * |
| 31 | * Must be called with the tasklist lock write-held. |
| 32 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 33 | void __ptrace_link(struct task_struct *child, struct task_struct *new_parent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | { |
Roland McGrath | f470021 | 2008-03-24 18:36:23 -0700 | [diff] [blame] | 35 | BUG_ON(!list_empty(&child->ptrace_entry)); |
| 36 | list_add(&child->ptrace_entry, &new_parent->ptraced); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | child->parent = new_parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | } |
Roland McGrath | 3a70970 | 2009-04-07 23:21:06 -0700 | [diff] [blame] | 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | /* |
| 41 | * Turn a tracing stop into a normal stop now, since with no tracer there |
| 42 | * would be no way to wake it up with SIGCONT or SIGKILL. If there was a |
| 43 | * signal sent that would resume the child, but didn't because it was in |
| 44 | * TASK_TRACED, resume it now. |
| 45 | * Requires that irqs be disabled. |
| 46 | */ |
Adrian Bunk | b747c8c | 2008-10-18 20:28:21 -0700 | [diff] [blame] | 47 | static void ptrace_untrace(struct task_struct *child) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | { |
| 49 | spin_lock(&child->sighand->siglock); |
Matthew Wilcox | 6618a3e | 2007-12-06 11:06:16 -0500 | [diff] [blame] | 50 | if (task_is_traced(child)) { |
Oleg Nesterov | 1ee1184 | 2009-04-02 16:58:23 -0700 | [diff] [blame] | 51 | /* |
Tejun Heo | d79fdd6 | 2011-03-23 10:37:00 +0100 | [diff] [blame^] | 52 | * If group stop is completed or in progress, it should |
| 53 | * participate in the group stop. Set GROUP_STOP_PENDING |
| 54 | * before kicking it. |
| 55 | * |
| 56 | * This involves TRACED -> RUNNING -> STOPPED transition |
| 57 | * which is similar to but in the opposite direction of |
| 58 | * what happens while attaching to a stopped task. |
| 59 | * However, in this direction, the intermediate RUNNING |
| 60 | * state is not hidden even from the current ptracer and if |
| 61 | * it immediately re-attaches and performs a WNOHANG |
| 62 | * wait(2), it may fail. |
Oleg Nesterov | 1ee1184 | 2009-04-02 16:58:23 -0700 | [diff] [blame] | 63 | */ |
| 64 | if (child->signal->flags & SIGNAL_STOP_STOPPED || |
| 65 | child->signal->group_stop_count) |
Tejun Heo | d79fdd6 | 2011-03-23 10:37:00 +0100 | [diff] [blame^] | 66 | child->group_stop |= GROUP_STOP_PENDING; |
| 67 | signal_wake_up(child, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | } |
| 69 | spin_unlock(&child->sighand->siglock); |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | * unptrace a task: move it back to its original parent and |
| 74 | * remove it from the ptrace list. |
| 75 | * |
| 76 | * Must be called with the tasklist lock write-held. |
| 77 | */ |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 78 | void __ptrace_unlink(struct task_struct *child) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | { |
Oleg Nesterov | 5ecfbae | 2006-02-15 22:50:10 +0300 | [diff] [blame] | 80 | BUG_ON(!child->ptrace); |
| 81 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | child->ptrace = 0; |
Roland McGrath | f470021 | 2008-03-24 18:36:23 -0700 | [diff] [blame] | 83 | child->parent = child->real_parent; |
| 84 | list_del_init(&child->ptrace_entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
Matthew Wilcox | 6618a3e | 2007-12-06 11:06:16 -0500 | [diff] [blame] | 86 | if (task_is_traced(child)) |
Roland McGrath | e57a505 | 2006-04-12 16:30:20 -0700 | [diff] [blame] | 87 | ptrace_untrace(child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /* |
| 91 | * Check that we have indeed attached to the thing.. |
| 92 | */ |
| 93 | int ptrace_check_attach(struct task_struct *child, int kill) |
| 94 | { |
| 95 | int ret = -ESRCH; |
| 96 | |
| 97 | /* |
| 98 | * We take the read lock around doing both checks to close a |
| 99 | * possible race where someone else was tracing our child and |
| 100 | * detached between these two checks. After this locked check, |
| 101 | * we are sure that this is our traced child and that can only |
| 102 | * be changed by us so it's not changing right after this. |
| 103 | */ |
| 104 | read_lock(&tasklist_lock); |
Oleg Nesterov | c0c0b64 | 2008-02-08 04:19:00 -0800 | [diff] [blame] | 105 | if ((child->ptrace & PT_PTRACED) && child->parent == current) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | ret = 0; |
Oleg Nesterov | c0c0b64 | 2008-02-08 04:19:00 -0800 | [diff] [blame] | 107 | /* |
| 108 | * child->sighand can't be NULL, release_task() |
| 109 | * does ptrace_unlink() before __exit_signal(). |
| 110 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | spin_lock_irq(&child->sighand->siglock); |
Oleg Nesterov | d9ae90a | 2008-02-06 01:36:13 -0800 | [diff] [blame] | 112 | if (task_is_stopped(child)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | child->state = TASK_TRACED; |
Oleg Nesterov | d9ae90a | 2008-02-06 01:36:13 -0800 | [diff] [blame] | 114 | else if (!task_is_traced(child) && !kill) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | ret = -ESRCH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | spin_unlock_irq(&child->sighand->siglock); |
| 117 | } |
| 118 | read_unlock(&tasklist_lock); |
| 119 | |
Oleg Nesterov | d9ae90a | 2008-02-06 01:36:13 -0800 | [diff] [blame] | 120 | if (!ret && !kill) |
Roland McGrath | 85ba2d8 | 2008-07-25 19:45:58 -0700 | [diff] [blame] | 121 | ret = wait_task_inactive(child, TASK_TRACED) ? 0 : -ESRCH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
| 123 | /* All systems go.. */ |
| 124 | return ret; |
| 125 | } |
| 126 | |
Stephen Smalley | 006ebb4 | 2008-05-19 08:32:49 -0400 | [diff] [blame] | 127 | int __ptrace_may_access(struct task_struct *task, unsigned int mode) |
Miklos Szeredi | ab8d11b | 2005-09-06 15:18:24 -0700 | [diff] [blame] | 128 | { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 129 | const struct cred *cred = current_cred(), *tcred; |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 130 | |
Eric W. Biederman | df26c40 | 2006-06-26 00:25:59 -0700 | [diff] [blame] | 131 | /* May we inspect the given task? |
| 132 | * This check is used both for attaching with ptrace |
| 133 | * and for allowing access to sensitive information in /proc. |
| 134 | * |
| 135 | * ptrace_attach denies several cases that /proc allows |
| 136 | * because setting up the necessary parent/child relationship |
| 137 | * or halting the specified task is impossible. |
| 138 | */ |
| 139 | int dumpable = 0; |
| 140 | /* Don't let security modules deny introspection */ |
| 141 | if (task == current) |
| 142 | return 0; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 143 | rcu_read_lock(); |
| 144 | tcred = __task_cred(task); |
| 145 | if ((cred->uid != tcred->euid || |
| 146 | cred->uid != tcred->suid || |
| 147 | cred->uid != tcred->uid || |
| 148 | cred->gid != tcred->egid || |
| 149 | cred->gid != tcred->sgid || |
| 150 | cred->gid != tcred->gid) && |
| 151 | !capable(CAP_SYS_PTRACE)) { |
| 152 | rcu_read_unlock(); |
Miklos Szeredi | ab8d11b | 2005-09-06 15:18:24 -0700 | [diff] [blame] | 153 | return -EPERM; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 154 | } |
| 155 | rcu_read_unlock(); |
Miklos Szeredi | ab8d11b | 2005-09-06 15:18:24 -0700 | [diff] [blame] | 156 | smp_rmb(); |
Eric W. Biederman | df26c40 | 2006-06-26 00:25:59 -0700 | [diff] [blame] | 157 | if (task->mm) |
Kawai, Hidehiro | 6c5d523 | 2007-07-19 01:48:27 -0700 | [diff] [blame] | 158 | dumpable = get_dumpable(task->mm); |
Eric W. Biederman | df26c40 | 2006-06-26 00:25:59 -0700 | [diff] [blame] | 159 | if (!dumpable && !capable(CAP_SYS_PTRACE)) |
Miklos Szeredi | ab8d11b | 2005-09-06 15:18:24 -0700 | [diff] [blame] | 160 | return -EPERM; |
| 161 | |
Ingo Molnar | 9e48858 | 2009-05-07 19:26:19 +1000 | [diff] [blame] | 162 | return security_ptrace_access_check(task, mode); |
Miklos Szeredi | ab8d11b | 2005-09-06 15:18:24 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Stephen Smalley | 006ebb4 | 2008-05-19 08:32:49 -0400 | [diff] [blame] | 165 | bool ptrace_may_access(struct task_struct *task, unsigned int mode) |
Miklos Szeredi | ab8d11b | 2005-09-06 15:18:24 -0700 | [diff] [blame] | 166 | { |
| 167 | int err; |
| 168 | task_lock(task); |
Stephen Smalley | 006ebb4 | 2008-05-19 08:32:49 -0400 | [diff] [blame] | 169 | err = __ptrace_may_access(task, mode); |
Miklos Szeredi | ab8d11b | 2005-09-06 15:18:24 -0700 | [diff] [blame] | 170 | task_unlock(task); |
Roland McGrath | 3a70970 | 2009-04-07 23:21:06 -0700 | [diff] [blame] | 171 | return !err; |
Miklos Szeredi | ab8d11b | 2005-09-06 15:18:24 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Linus Torvalds | e3e89cc | 2011-03-04 09:23:30 -0800 | [diff] [blame] | 174 | static int ptrace_attach(struct task_struct *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | { |
Tejun Heo | d79fdd6 | 2011-03-23 10:37:00 +0100 | [diff] [blame^] | 176 | bool wait_trap = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | int retval; |
Linus Torvalds | f5b40e3 | 2006-05-07 10:49:33 -0700 | [diff] [blame] | 178 | |
Al Viro | a5cb013 | 2007-03-20 13:58:35 -0400 | [diff] [blame] | 179 | audit_ptrace(task); |
| 180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | retval = -EPERM; |
Oleg Nesterov | b79b7ba | 2009-06-17 16:27:31 -0700 | [diff] [blame] | 182 | if (unlikely(task->flags & PF_KTHREAD)) |
| 183 | goto out; |
Pavel Emelyanov | bac0abd | 2007-10-18 23:40:18 -0700 | [diff] [blame] | 184 | if (same_thread_group(task, current)) |
Linus Torvalds | f5b40e3 | 2006-05-07 10:49:33 -0700 | [diff] [blame] | 185 | goto out; |
| 186 | |
Oleg Nesterov | f2f0b00 | 2009-06-17 16:27:32 -0700 | [diff] [blame] | 187 | /* |
| 188 | * Protect exec's credential calculations against our interference; |
David Howells | 5e751e9 | 2009-05-08 13:55:22 +0100 | [diff] [blame] | 189 | * interference; SUID, SGID and LSM creds get determined differently |
| 190 | * under ptrace. |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 191 | */ |
Oleg Nesterov | 793285f | 2009-07-05 12:08:26 -0700 | [diff] [blame] | 192 | retval = -ERESTARTNOINTR; |
KOSAKI Motohiro | 9b1bf12 | 2010-10-27 15:34:08 -0700 | [diff] [blame] | 193 | if (mutex_lock_interruptible(&task->signal->cred_guard_mutex)) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 194 | goto out; |
Oleg Nesterov | 4b105cb | 2009-06-17 16:27:33 -0700 | [diff] [blame] | 195 | |
Linus Torvalds | f5b40e3 | 2006-05-07 10:49:33 -0700 | [diff] [blame] | 196 | task_lock(task); |
Stephen Smalley | 006ebb4 | 2008-05-19 08:32:49 -0400 | [diff] [blame] | 197 | retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH); |
Oleg Nesterov | 4b105cb | 2009-06-17 16:27:33 -0700 | [diff] [blame] | 198 | task_unlock(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | if (retval) |
Oleg Nesterov | 4b105cb | 2009-06-17 16:27:33 -0700 | [diff] [blame] | 200 | goto unlock_creds; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
Oleg Nesterov | 4b105cb | 2009-06-17 16:27:33 -0700 | [diff] [blame] | 202 | write_lock_irq(&tasklist_lock); |
Oleg Nesterov | b79b7ba | 2009-06-17 16:27:31 -0700 | [diff] [blame] | 203 | retval = -EPERM; |
| 204 | if (unlikely(task->exit_state)) |
Oleg Nesterov | 4b105cb | 2009-06-17 16:27:33 -0700 | [diff] [blame] | 205 | goto unlock_tasklist; |
Oleg Nesterov | f2f0b00 | 2009-06-17 16:27:32 -0700 | [diff] [blame] | 206 | if (task->ptrace) |
Oleg Nesterov | 4b105cb | 2009-06-17 16:27:33 -0700 | [diff] [blame] | 207 | goto unlock_tasklist; |
Oleg Nesterov | b79b7ba | 2009-06-17 16:27:31 -0700 | [diff] [blame] | 208 | |
Oleg Nesterov | f2f0b00 | 2009-06-17 16:27:32 -0700 | [diff] [blame] | 209 | task->ptrace = PT_PTRACED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | if (capable(CAP_SYS_PTRACE)) |
| 211 | task->ptrace |= PT_PTRACE_CAP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | __ptrace_link(task, current); |
Oleg Nesterov | 33e9fc7 | 2008-04-30 00:53:14 -0700 | [diff] [blame] | 214 | send_sig_info(SIGSTOP, SEND_SIG_FORCED, task); |
Oleg Nesterov | b79b7ba | 2009-06-17 16:27:31 -0700 | [diff] [blame] | 215 | |
Tejun Heo | d79fdd6 | 2011-03-23 10:37:00 +0100 | [diff] [blame^] | 216 | spin_lock(&task->sighand->siglock); |
| 217 | |
| 218 | /* |
| 219 | * If the task is already STOPPED, set GROUP_STOP_PENDING and |
| 220 | * TRAPPING, and kick it so that it transits to TRACED. TRAPPING |
| 221 | * will be cleared if the child completes the transition or any |
| 222 | * event which clears the group stop states happens. We'll wait |
| 223 | * for the transition to complete before returning from this |
| 224 | * function. |
| 225 | * |
| 226 | * This hides STOPPED -> RUNNING -> TRACED transition from the |
| 227 | * attaching thread but a different thread in the same group can |
| 228 | * still observe the transient RUNNING state. IOW, if another |
| 229 | * thread's WNOHANG wait(2) on the stopped tracee races against |
| 230 | * ATTACH, the wait(2) may fail due to the transient RUNNING. |
| 231 | * |
| 232 | * The following task_is_stopped() test is safe as both transitions |
| 233 | * in and out of STOPPED are protected by siglock. |
| 234 | */ |
| 235 | if (task_is_stopped(task)) { |
| 236 | task->group_stop |= GROUP_STOP_PENDING | GROUP_STOP_TRAPPING; |
| 237 | signal_wake_up(task, 1); |
| 238 | wait_trap = true; |
| 239 | } |
| 240 | |
| 241 | spin_unlock(&task->sighand->siglock); |
| 242 | |
Oleg Nesterov | b79b7ba | 2009-06-17 16:27:31 -0700 | [diff] [blame] | 243 | retval = 0; |
Oleg Nesterov | 4b105cb | 2009-06-17 16:27:33 -0700 | [diff] [blame] | 244 | unlock_tasklist: |
| 245 | write_unlock_irq(&tasklist_lock); |
| 246 | unlock_creds: |
KOSAKI Motohiro | 9b1bf12 | 2010-10-27 15:34:08 -0700 | [diff] [blame] | 247 | mutex_unlock(&task->signal->cred_guard_mutex); |
Linus Torvalds | f5b40e3 | 2006-05-07 10:49:33 -0700 | [diff] [blame] | 248 | out: |
Tejun Heo | d79fdd6 | 2011-03-23 10:37:00 +0100 | [diff] [blame^] | 249 | if (wait_trap) |
| 250 | wait_event(current->signal->wait_chldexit, |
| 251 | !(task->group_stop & GROUP_STOP_TRAPPING)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | return retval; |
| 253 | } |
| 254 | |
Oleg Nesterov | f2f0b00 | 2009-06-17 16:27:32 -0700 | [diff] [blame] | 255 | /** |
| 256 | * ptrace_traceme -- helper for PTRACE_TRACEME |
| 257 | * |
| 258 | * Performs checks and sets PT_PTRACED. |
| 259 | * Should be used by all ptrace implementations for PTRACE_TRACEME. |
| 260 | */ |
Linus Torvalds | e3e89cc | 2011-03-04 09:23:30 -0800 | [diff] [blame] | 261 | static int ptrace_traceme(void) |
Oleg Nesterov | f2f0b00 | 2009-06-17 16:27:32 -0700 | [diff] [blame] | 262 | { |
| 263 | int ret = -EPERM; |
| 264 | |
Oleg Nesterov | 4b105cb | 2009-06-17 16:27:33 -0700 | [diff] [blame] | 265 | write_lock_irq(&tasklist_lock); |
| 266 | /* Are we already being traced? */ |
Oleg Nesterov | f2f0b00 | 2009-06-17 16:27:32 -0700 | [diff] [blame] | 267 | if (!current->ptrace) { |
Oleg Nesterov | f2f0b00 | 2009-06-17 16:27:32 -0700 | [diff] [blame] | 268 | ret = security_ptrace_traceme(current->parent); |
Oleg Nesterov | f2f0b00 | 2009-06-17 16:27:32 -0700 | [diff] [blame] | 269 | /* |
| 270 | * Check PF_EXITING to ensure ->real_parent has not passed |
| 271 | * exit_ptrace(). Otherwise we don't report the error but |
| 272 | * pretend ->real_parent untraces us right after return. |
| 273 | */ |
| 274 | if (!ret && !(current->real_parent->flags & PF_EXITING)) { |
| 275 | current->ptrace = PT_PTRACED; |
| 276 | __ptrace_link(current, current->real_parent); |
| 277 | } |
Oleg Nesterov | f2f0b00 | 2009-06-17 16:27:32 -0700 | [diff] [blame] | 278 | } |
Oleg Nesterov | 4b105cb | 2009-06-17 16:27:33 -0700 | [diff] [blame] | 279 | write_unlock_irq(&tasklist_lock); |
| 280 | |
Oleg Nesterov | f2f0b00 | 2009-06-17 16:27:32 -0700 | [diff] [blame] | 281 | return ret; |
| 282 | } |
| 283 | |
Oleg Nesterov | 39c626a | 2009-04-02 16:58:18 -0700 | [diff] [blame] | 284 | /* |
| 285 | * Called with irqs disabled, returns true if childs should reap themselves. |
| 286 | */ |
| 287 | static int ignoring_children(struct sighand_struct *sigh) |
| 288 | { |
| 289 | int ret; |
| 290 | spin_lock(&sigh->siglock); |
| 291 | ret = (sigh->action[SIGCHLD-1].sa.sa_handler == SIG_IGN) || |
| 292 | (sigh->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT); |
| 293 | spin_unlock(&sigh->siglock); |
| 294 | return ret; |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | * Called with tasklist_lock held for writing. |
| 299 | * Unlink a traced task, and clean it up if it was a traced zombie. |
| 300 | * Return true if it needs to be reaped with release_task(). |
| 301 | * (We can't call release_task() here because we already hold tasklist_lock.) |
| 302 | * |
| 303 | * If it's a zombie, our attachedness prevented normal parent notification |
| 304 | * or self-reaping. Do notification now if it would have happened earlier. |
| 305 | * If it should reap itself, return true. |
| 306 | * |
Oleg Nesterov | a7f0765 | 2009-09-23 15:56:44 -0700 | [diff] [blame] | 307 | * If it's our own child, there is no notification to do. But if our normal |
| 308 | * children self-reap, then this child was prevented by ptrace and we must |
| 309 | * reap it now, in that case we must also wake up sub-threads sleeping in |
| 310 | * do_wait(). |
Oleg Nesterov | 39c626a | 2009-04-02 16:58:18 -0700 | [diff] [blame] | 311 | */ |
| 312 | static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p) |
| 313 | { |
| 314 | __ptrace_unlink(p); |
| 315 | |
| 316 | if (p->exit_state == EXIT_ZOMBIE) { |
| 317 | if (!task_detached(p) && thread_group_empty(p)) { |
| 318 | if (!same_thread_group(p->real_parent, tracer)) |
| 319 | do_notify_parent(p, p->exit_signal); |
Oleg Nesterov | a7f0765 | 2009-09-23 15:56:44 -0700 | [diff] [blame] | 320 | else if (ignoring_children(tracer->sighand)) { |
| 321 | __wake_up_parent(p, tracer); |
Oleg Nesterov | 39c626a | 2009-04-02 16:58:18 -0700 | [diff] [blame] | 322 | p->exit_signal = -1; |
Oleg Nesterov | a7f0765 | 2009-09-23 15:56:44 -0700 | [diff] [blame] | 323 | } |
Oleg Nesterov | 39c626a | 2009-04-02 16:58:18 -0700 | [diff] [blame] | 324 | } |
| 325 | if (task_detached(p)) { |
| 326 | /* Mark it as in the process of being reaped. */ |
| 327 | p->exit_state = EXIT_DEAD; |
| 328 | return true; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | return false; |
| 333 | } |
| 334 | |
Linus Torvalds | e3e89cc | 2011-03-04 09:23:30 -0800 | [diff] [blame] | 335 | static int ptrace_detach(struct task_struct *child, unsigned int data) |
Oleg Nesterov | 5ecfbae | 2006-02-15 22:50:10 +0300 | [diff] [blame] | 336 | { |
Oleg Nesterov | 39c626a | 2009-04-02 16:58:18 -0700 | [diff] [blame] | 337 | bool dead = false; |
Oleg Nesterov | 4576145 | 2009-04-02 16:58:14 -0700 | [diff] [blame] | 338 | |
Oleg Nesterov | 5ecfbae | 2006-02-15 22:50:10 +0300 | [diff] [blame] | 339 | if (!valid_signal(data)) |
| 340 | return -EIO; |
| 341 | |
| 342 | /* Architecture-specific hardware disable .. */ |
| 343 | ptrace_disable(child); |
Roland McGrath | 7d94143 | 2007-09-05 03:05:56 -0700 | [diff] [blame] | 344 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
Oleg Nesterov | 5ecfbae | 2006-02-15 22:50:10 +0300 | [diff] [blame] | 345 | |
Oleg Nesterov | 95c3eb7 | 2009-04-02 16:58:11 -0700 | [diff] [blame] | 346 | write_lock_irq(&tasklist_lock); |
Oleg Nesterov | 39c626a | 2009-04-02 16:58:18 -0700 | [diff] [blame] | 347 | /* |
| 348 | * This child can be already killed. Make sure de_thread() or |
| 349 | * our sub-thread doing do_wait() didn't do release_task() yet. |
| 350 | */ |
Oleg Nesterov | 95c3eb7 | 2009-04-02 16:58:11 -0700 | [diff] [blame] | 351 | if (child->ptrace) { |
| 352 | child->exit_code = data; |
Oleg Nesterov | 4576145 | 2009-04-02 16:58:14 -0700 | [diff] [blame] | 353 | dead = __ptrace_detach(current, child); |
Oleg Nesterov | 95c3eb7 | 2009-04-02 16:58:11 -0700 | [diff] [blame] | 354 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | write_unlock_irq(&tasklist_lock); |
| 356 | |
Oleg Nesterov | 4576145 | 2009-04-02 16:58:14 -0700 | [diff] [blame] | 357 | if (unlikely(dead)) |
| 358 | release_task(child); |
| 359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | return 0; |
| 361 | } |
| 362 | |
Oleg Nesterov | 39c626a | 2009-04-02 16:58:18 -0700 | [diff] [blame] | 363 | /* |
Oleg Nesterov | c7e49c1 | 2010-08-10 18:03:07 -0700 | [diff] [blame] | 364 | * Detach all tasks we were using ptrace on. Called with tasklist held |
| 365 | * for writing, and returns with it held too. But note it can release |
| 366 | * and reacquire the lock. |
Oleg Nesterov | 39c626a | 2009-04-02 16:58:18 -0700 | [diff] [blame] | 367 | */ |
| 368 | void exit_ptrace(struct task_struct *tracer) |
Namhyung Kim | c4b5ed2 | 2010-10-27 15:33:44 -0700 | [diff] [blame] | 369 | __releases(&tasklist_lock) |
| 370 | __acquires(&tasklist_lock) |
Oleg Nesterov | 39c626a | 2009-04-02 16:58:18 -0700 | [diff] [blame] | 371 | { |
| 372 | struct task_struct *p, *n; |
| 373 | LIST_HEAD(ptrace_dead); |
| 374 | |
Oleg Nesterov | c7e49c1 | 2010-08-10 18:03:07 -0700 | [diff] [blame] | 375 | if (likely(list_empty(&tracer->ptraced))) |
| 376 | return; |
| 377 | |
Oleg Nesterov | 39c626a | 2009-04-02 16:58:18 -0700 | [diff] [blame] | 378 | list_for_each_entry_safe(p, n, &tracer->ptraced, ptrace_entry) { |
| 379 | if (__ptrace_detach(tracer, p)) |
| 380 | list_add(&p->ptrace_entry, &ptrace_dead); |
| 381 | } |
Oleg Nesterov | 39c626a | 2009-04-02 16:58:18 -0700 | [diff] [blame] | 382 | |
Oleg Nesterov | c7e49c1 | 2010-08-10 18:03:07 -0700 | [diff] [blame] | 383 | write_unlock_irq(&tasklist_lock); |
Oleg Nesterov | 39c626a | 2009-04-02 16:58:18 -0700 | [diff] [blame] | 384 | BUG_ON(!list_empty(&tracer->ptraced)); |
| 385 | |
| 386 | list_for_each_entry_safe(p, n, &ptrace_dead, ptrace_entry) { |
| 387 | list_del_init(&p->ptrace_entry); |
| 388 | release_task(p); |
| 389 | } |
Oleg Nesterov | c7e49c1 | 2010-08-10 18:03:07 -0700 | [diff] [blame] | 390 | |
| 391 | write_lock_irq(&tasklist_lock); |
Oleg Nesterov | 39c626a | 2009-04-02 16:58:18 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len) |
| 395 | { |
| 396 | int copied = 0; |
| 397 | |
| 398 | while (len > 0) { |
| 399 | char buf[128]; |
| 400 | int this_len, retval; |
| 401 | |
| 402 | this_len = (len > sizeof(buf)) ? sizeof(buf) : len; |
| 403 | retval = access_process_vm(tsk, src, buf, this_len, 0); |
| 404 | if (!retval) { |
| 405 | if (copied) |
| 406 | break; |
| 407 | return -EIO; |
| 408 | } |
| 409 | if (copy_to_user(dst, buf, retval)) |
| 410 | return -EFAULT; |
| 411 | copied += retval; |
| 412 | src += retval; |
| 413 | dst += retval; |
Roland McGrath | 3a70970 | 2009-04-07 23:21:06 -0700 | [diff] [blame] | 414 | len -= retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | } |
| 416 | return copied; |
| 417 | } |
| 418 | |
| 419 | int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len) |
| 420 | { |
| 421 | int copied = 0; |
| 422 | |
| 423 | while (len > 0) { |
| 424 | char buf[128]; |
| 425 | int this_len, retval; |
| 426 | |
| 427 | this_len = (len > sizeof(buf)) ? sizeof(buf) : len; |
| 428 | if (copy_from_user(buf, src, this_len)) |
| 429 | return -EFAULT; |
| 430 | retval = access_process_vm(tsk, dst, buf, this_len, 1); |
| 431 | if (!retval) { |
| 432 | if (copied) |
| 433 | break; |
| 434 | return -EIO; |
| 435 | } |
| 436 | copied += retval; |
| 437 | src += retval; |
| 438 | dst += retval; |
Roland McGrath | 3a70970 | 2009-04-07 23:21:06 -0700 | [diff] [blame] | 439 | len -= retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | } |
| 441 | return copied; |
| 442 | } |
| 443 | |
Namhyung Kim | 4abf986 | 2010-10-27 15:33:45 -0700 | [diff] [blame] | 444 | static int ptrace_setoptions(struct task_struct *child, unsigned long data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | { |
| 446 | child->ptrace &= ~PT_TRACE_MASK; |
| 447 | |
| 448 | if (data & PTRACE_O_TRACESYSGOOD) |
| 449 | child->ptrace |= PT_TRACESYSGOOD; |
| 450 | |
| 451 | if (data & PTRACE_O_TRACEFORK) |
| 452 | child->ptrace |= PT_TRACE_FORK; |
| 453 | |
| 454 | if (data & PTRACE_O_TRACEVFORK) |
| 455 | child->ptrace |= PT_TRACE_VFORK; |
| 456 | |
| 457 | if (data & PTRACE_O_TRACECLONE) |
| 458 | child->ptrace |= PT_TRACE_CLONE; |
| 459 | |
| 460 | if (data & PTRACE_O_TRACEEXEC) |
| 461 | child->ptrace |= PT_TRACE_EXEC; |
| 462 | |
| 463 | if (data & PTRACE_O_TRACEVFORKDONE) |
| 464 | child->ptrace |= PT_TRACE_VFORK_DONE; |
| 465 | |
| 466 | if (data & PTRACE_O_TRACEEXIT) |
| 467 | child->ptrace |= PT_TRACE_EXIT; |
| 468 | |
| 469 | return (data & ~PTRACE_O_MASK) ? -EINVAL : 0; |
| 470 | } |
| 471 | |
Roland McGrath | e16b278 | 2008-04-20 13:10:12 -0700 | [diff] [blame] | 472 | static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | { |
Oleg Nesterov | e496125 | 2009-06-17 16:27:36 -0700 | [diff] [blame] | 474 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | int error = -ESRCH; |
| 476 | |
Oleg Nesterov | e496125 | 2009-06-17 16:27:36 -0700 | [diff] [blame] | 477 | if (lock_task_sighand(child, &flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | error = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | if (likely(child->last_siginfo != NULL)) { |
Roland McGrath | e16b278 | 2008-04-20 13:10:12 -0700 | [diff] [blame] | 480 | *info = *child->last_siginfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | error = 0; |
| 482 | } |
Oleg Nesterov | e496125 | 2009-06-17 16:27:36 -0700 | [diff] [blame] | 483 | unlock_task_sighand(child, &flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | return error; |
| 486 | } |
| 487 | |
Roland McGrath | e16b278 | 2008-04-20 13:10:12 -0700 | [diff] [blame] | 488 | static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | { |
Oleg Nesterov | e496125 | 2009-06-17 16:27:36 -0700 | [diff] [blame] | 490 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | int error = -ESRCH; |
| 492 | |
Oleg Nesterov | e496125 | 2009-06-17 16:27:36 -0700 | [diff] [blame] | 493 | if (lock_task_sighand(child, &flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | error = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | if (likely(child->last_siginfo != NULL)) { |
Roland McGrath | e16b278 | 2008-04-20 13:10:12 -0700 | [diff] [blame] | 496 | *child->last_siginfo = *info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | error = 0; |
| 498 | } |
Oleg Nesterov | e496125 | 2009-06-17 16:27:36 -0700 | [diff] [blame] | 499 | unlock_task_sighand(child, &flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | return error; |
| 502 | } |
| 503 | |
Roland McGrath | 36df29d | 2008-01-30 13:30:51 +0100 | [diff] [blame] | 504 | |
| 505 | #ifdef PTRACE_SINGLESTEP |
| 506 | #define is_singlestep(request) ((request) == PTRACE_SINGLESTEP) |
| 507 | #else |
| 508 | #define is_singlestep(request) 0 |
| 509 | #endif |
| 510 | |
Roland McGrath | 5b88abb | 2008-01-30 13:30:53 +0100 | [diff] [blame] | 511 | #ifdef PTRACE_SINGLEBLOCK |
| 512 | #define is_singleblock(request) ((request) == PTRACE_SINGLEBLOCK) |
| 513 | #else |
| 514 | #define is_singleblock(request) 0 |
| 515 | #endif |
| 516 | |
Roland McGrath | 36df29d | 2008-01-30 13:30:51 +0100 | [diff] [blame] | 517 | #ifdef PTRACE_SYSEMU |
| 518 | #define is_sysemu_singlestep(request) ((request) == PTRACE_SYSEMU_SINGLESTEP) |
| 519 | #else |
| 520 | #define is_sysemu_singlestep(request) 0 |
| 521 | #endif |
| 522 | |
Namhyung Kim | 4abf986 | 2010-10-27 15:33:45 -0700 | [diff] [blame] | 523 | static int ptrace_resume(struct task_struct *child, long request, |
| 524 | unsigned long data) |
Roland McGrath | 36df29d | 2008-01-30 13:30:51 +0100 | [diff] [blame] | 525 | { |
| 526 | if (!valid_signal(data)) |
| 527 | return -EIO; |
| 528 | |
| 529 | if (request == PTRACE_SYSCALL) |
| 530 | set_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
| 531 | else |
| 532 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
| 533 | |
| 534 | #ifdef TIF_SYSCALL_EMU |
| 535 | if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP) |
| 536 | set_tsk_thread_flag(child, TIF_SYSCALL_EMU); |
| 537 | else |
| 538 | clear_tsk_thread_flag(child, TIF_SYSCALL_EMU); |
| 539 | #endif |
| 540 | |
Roland McGrath | 5b88abb | 2008-01-30 13:30:53 +0100 | [diff] [blame] | 541 | if (is_singleblock(request)) { |
| 542 | if (unlikely(!arch_has_block_step())) |
| 543 | return -EIO; |
| 544 | user_enable_block_step(child); |
| 545 | } else if (is_singlestep(request) || is_sysemu_singlestep(request)) { |
Roland McGrath | 36df29d | 2008-01-30 13:30:51 +0100 | [diff] [blame] | 546 | if (unlikely(!arch_has_single_step())) |
| 547 | return -EIO; |
| 548 | user_enable_single_step(child); |
Roland McGrath | 3a70970 | 2009-04-07 23:21:06 -0700 | [diff] [blame] | 549 | } else { |
Roland McGrath | 36df29d | 2008-01-30 13:30:51 +0100 | [diff] [blame] | 550 | user_disable_single_step(child); |
Roland McGrath | 3a70970 | 2009-04-07 23:21:06 -0700 | [diff] [blame] | 551 | } |
Roland McGrath | 36df29d | 2008-01-30 13:30:51 +0100 | [diff] [blame] | 552 | |
| 553 | child->exit_code = data; |
| 554 | wake_up_process(child); |
| 555 | |
| 556 | return 0; |
| 557 | } |
| 558 | |
Suresh Siddha | 2225a12 | 2010-02-11 11:51:00 -0800 | [diff] [blame] | 559 | #ifdef CONFIG_HAVE_ARCH_TRACEHOOK |
| 560 | |
| 561 | static const struct user_regset * |
| 562 | find_regset(const struct user_regset_view *view, unsigned int type) |
| 563 | { |
| 564 | const struct user_regset *regset; |
| 565 | int n; |
| 566 | |
| 567 | for (n = 0; n < view->n; ++n) { |
| 568 | regset = view->regsets + n; |
| 569 | if (regset->core_note_type == type) |
| 570 | return regset; |
| 571 | } |
| 572 | |
| 573 | return NULL; |
| 574 | } |
| 575 | |
| 576 | static int ptrace_regset(struct task_struct *task, int req, unsigned int type, |
| 577 | struct iovec *kiov) |
| 578 | { |
| 579 | const struct user_regset_view *view = task_user_regset_view(task); |
| 580 | const struct user_regset *regset = find_regset(view, type); |
| 581 | int regset_no; |
| 582 | |
| 583 | if (!regset || (kiov->iov_len % regset->size) != 0) |
Suresh Siddha | c6a0dd7 | 2010-02-22 14:51:32 -0800 | [diff] [blame] | 584 | return -EINVAL; |
Suresh Siddha | 2225a12 | 2010-02-11 11:51:00 -0800 | [diff] [blame] | 585 | |
| 586 | regset_no = regset - view->regsets; |
| 587 | kiov->iov_len = min(kiov->iov_len, |
| 588 | (__kernel_size_t) (regset->n * regset->size)); |
| 589 | |
| 590 | if (req == PTRACE_GETREGSET) |
| 591 | return copy_regset_to_user(task, view, regset_no, 0, |
| 592 | kiov->iov_len, kiov->iov_base); |
| 593 | else |
| 594 | return copy_regset_from_user(task, view, regset_no, 0, |
| 595 | kiov->iov_len, kiov->iov_base); |
| 596 | } |
| 597 | |
| 598 | #endif |
| 599 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | int ptrace_request(struct task_struct *child, long request, |
Namhyung Kim | 4abf986 | 2010-10-27 15:33:45 -0700 | [diff] [blame] | 601 | unsigned long addr, unsigned long data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | { |
| 603 | int ret = -EIO; |
Roland McGrath | e16b278 | 2008-04-20 13:10:12 -0700 | [diff] [blame] | 604 | siginfo_t siginfo; |
Namhyung Kim | 9fed81d | 2010-10-27 15:33:46 -0700 | [diff] [blame] | 605 | void __user *datavp = (void __user *) data; |
| 606 | unsigned long __user *datalp = datavp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | |
| 608 | switch (request) { |
Roland McGrath | 16c3e38 | 2008-01-30 13:31:47 +0100 | [diff] [blame] | 609 | case PTRACE_PEEKTEXT: |
| 610 | case PTRACE_PEEKDATA: |
| 611 | return generic_ptrace_peekdata(child, addr, data); |
| 612 | case PTRACE_POKETEXT: |
| 613 | case PTRACE_POKEDATA: |
| 614 | return generic_ptrace_pokedata(child, addr, data); |
| 615 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | #ifdef PTRACE_OLDSETOPTIONS |
| 617 | case PTRACE_OLDSETOPTIONS: |
| 618 | #endif |
| 619 | case PTRACE_SETOPTIONS: |
| 620 | ret = ptrace_setoptions(child, data); |
| 621 | break; |
| 622 | case PTRACE_GETEVENTMSG: |
Namhyung Kim | 9fed81d | 2010-10-27 15:33:46 -0700 | [diff] [blame] | 623 | ret = put_user(child->ptrace_message, datalp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | break; |
Roland McGrath | e16b278 | 2008-04-20 13:10:12 -0700 | [diff] [blame] | 625 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | case PTRACE_GETSIGINFO: |
Roland McGrath | e16b278 | 2008-04-20 13:10:12 -0700 | [diff] [blame] | 627 | ret = ptrace_getsiginfo(child, &siginfo); |
| 628 | if (!ret) |
Namhyung Kim | 9fed81d | 2010-10-27 15:33:46 -0700 | [diff] [blame] | 629 | ret = copy_siginfo_to_user(datavp, &siginfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | break; |
Roland McGrath | e16b278 | 2008-04-20 13:10:12 -0700 | [diff] [blame] | 631 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | case PTRACE_SETSIGINFO: |
Namhyung Kim | 9fed81d | 2010-10-27 15:33:46 -0700 | [diff] [blame] | 633 | if (copy_from_user(&siginfo, datavp, sizeof siginfo)) |
Roland McGrath | e16b278 | 2008-04-20 13:10:12 -0700 | [diff] [blame] | 634 | ret = -EFAULT; |
| 635 | else |
| 636 | ret = ptrace_setsiginfo(child, &siginfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | break; |
Roland McGrath | e16b278 | 2008-04-20 13:10:12 -0700 | [diff] [blame] | 638 | |
Alexey Dobriyan | 1bcf548 | 2007-10-16 01:23:45 -0700 | [diff] [blame] | 639 | case PTRACE_DETACH: /* detach a process that was attached. */ |
| 640 | ret = ptrace_detach(child, data); |
| 641 | break; |
Roland McGrath | 36df29d | 2008-01-30 13:30:51 +0100 | [diff] [blame] | 642 | |
Mike Frysinger | 9c1a125 | 2010-05-26 14:42:52 -0700 | [diff] [blame] | 643 | #ifdef CONFIG_BINFMT_ELF_FDPIC |
| 644 | case PTRACE_GETFDPIC: { |
Oleg Nesterov | e0129ef | 2010-05-26 14:42:53 -0700 | [diff] [blame] | 645 | struct mm_struct *mm = get_task_mm(child); |
Mike Frysinger | 9c1a125 | 2010-05-26 14:42:52 -0700 | [diff] [blame] | 646 | unsigned long tmp = 0; |
| 647 | |
Oleg Nesterov | e0129ef | 2010-05-26 14:42:53 -0700 | [diff] [blame] | 648 | ret = -ESRCH; |
| 649 | if (!mm) |
| 650 | break; |
| 651 | |
Mike Frysinger | 9c1a125 | 2010-05-26 14:42:52 -0700 | [diff] [blame] | 652 | switch (addr) { |
| 653 | case PTRACE_GETFDPIC_EXEC: |
Oleg Nesterov | e0129ef | 2010-05-26 14:42:53 -0700 | [diff] [blame] | 654 | tmp = mm->context.exec_fdpic_loadmap; |
Mike Frysinger | 9c1a125 | 2010-05-26 14:42:52 -0700 | [diff] [blame] | 655 | break; |
| 656 | case PTRACE_GETFDPIC_INTERP: |
Oleg Nesterov | e0129ef | 2010-05-26 14:42:53 -0700 | [diff] [blame] | 657 | tmp = mm->context.interp_fdpic_loadmap; |
Mike Frysinger | 9c1a125 | 2010-05-26 14:42:52 -0700 | [diff] [blame] | 658 | break; |
| 659 | default: |
| 660 | break; |
| 661 | } |
Oleg Nesterov | e0129ef | 2010-05-26 14:42:53 -0700 | [diff] [blame] | 662 | mmput(mm); |
Mike Frysinger | 9c1a125 | 2010-05-26 14:42:52 -0700 | [diff] [blame] | 663 | |
Namhyung Kim | 9fed81d | 2010-10-27 15:33:46 -0700 | [diff] [blame] | 664 | ret = put_user(tmp, datalp); |
Mike Frysinger | 9c1a125 | 2010-05-26 14:42:52 -0700 | [diff] [blame] | 665 | break; |
| 666 | } |
| 667 | #endif |
| 668 | |
Roland McGrath | 36df29d | 2008-01-30 13:30:51 +0100 | [diff] [blame] | 669 | #ifdef PTRACE_SINGLESTEP |
| 670 | case PTRACE_SINGLESTEP: |
| 671 | #endif |
Roland McGrath | 5b88abb | 2008-01-30 13:30:53 +0100 | [diff] [blame] | 672 | #ifdef PTRACE_SINGLEBLOCK |
| 673 | case PTRACE_SINGLEBLOCK: |
| 674 | #endif |
Roland McGrath | 36df29d | 2008-01-30 13:30:51 +0100 | [diff] [blame] | 675 | #ifdef PTRACE_SYSEMU |
| 676 | case PTRACE_SYSEMU: |
| 677 | case PTRACE_SYSEMU_SINGLESTEP: |
| 678 | #endif |
| 679 | case PTRACE_SYSCALL: |
| 680 | case PTRACE_CONT: |
| 681 | return ptrace_resume(child, request, data); |
| 682 | |
| 683 | case PTRACE_KILL: |
| 684 | if (child->exit_state) /* already dead */ |
| 685 | return 0; |
| 686 | return ptrace_resume(child, request, SIGKILL); |
| 687 | |
Suresh Siddha | 2225a12 | 2010-02-11 11:51:00 -0800 | [diff] [blame] | 688 | #ifdef CONFIG_HAVE_ARCH_TRACEHOOK |
| 689 | case PTRACE_GETREGSET: |
| 690 | case PTRACE_SETREGSET: |
| 691 | { |
| 692 | struct iovec kiov; |
Namhyung Kim | 9fed81d | 2010-10-27 15:33:46 -0700 | [diff] [blame] | 693 | struct iovec __user *uiov = datavp; |
Suresh Siddha | 2225a12 | 2010-02-11 11:51:00 -0800 | [diff] [blame] | 694 | |
| 695 | if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov))) |
| 696 | return -EFAULT; |
| 697 | |
| 698 | if (__get_user(kiov.iov_base, &uiov->iov_base) || |
| 699 | __get_user(kiov.iov_len, &uiov->iov_len)) |
| 700 | return -EFAULT; |
| 701 | |
| 702 | ret = ptrace_regset(child, request, addr, &kiov); |
| 703 | if (!ret) |
| 704 | ret = __put_user(kiov.iov_len, &uiov->iov_len); |
| 705 | break; |
| 706 | } |
| 707 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | default: |
| 709 | break; |
| 710 | } |
| 711 | |
| 712 | return ret; |
| 713 | } |
Christoph Hellwig | 481bed4 | 2005-11-07 00:59:47 -0800 | [diff] [blame] | 714 | |
Oleg Nesterov | 8053bdd | 2009-06-17 16:27:34 -0700 | [diff] [blame] | 715 | static struct task_struct *ptrace_get_task_struct(pid_t pid) |
Christoph Hellwig | 6b9c7ed | 2006-01-08 01:02:33 -0800 | [diff] [blame] | 716 | { |
| 717 | struct task_struct *child; |
Christoph Hellwig | 481bed4 | 2005-11-07 00:59:47 -0800 | [diff] [blame] | 718 | |
Oleg Nesterov | 8053bdd | 2009-06-17 16:27:34 -0700 | [diff] [blame] | 719 | rcu_read_lock(); |
Pavel Emelyanov | 228ebcb | 2007-10-18 23:40:16 -0700 | [diff] [blame] | 720 | child = find_task_by_vpid(pid); |
Christoph Hellwig | 481bed4 | 2005-11-07 00:59:47 -0800 | [diff] [blame] | 721 | if (child) |
| 722 | get_task_struct(child); |
Oleg Nesterov | 8053bdd | 2009-06-17 16:27:34 -0700 | [diff] [blame] | 723 | rcu_read_unlock(); |
Sukadev Bhattiprolu | f400e19 | 2006-09-29 02:00:07 -0700 | [diff] [blame] | 724 | |
Christoph Hellwig | 481bed4 | 2005-11-07 00:59:47 -0800 | [diff] [blame] | 725 | if (!child) |
Christoph Hellwig | 6b9c7ed | 2006-01-08 01:02:33 -0800 | [diff] [blame] | 726 | return ERR_PTR(-ESRCH); |
| 727 | return child; |
Christoph Hellwig | 481bed4 | 2005-11-07 00:59:47 -0800 | [diff] [blame] | 728 | } |
| 729 | |
Christoph Hellwig | 0ac1555 | 2007-10-16 01:26:37 -0700 | [diff] [blame] | 730 | #ifndef arch_ptrace_attach |
| 731 | #define arch_ptrace_attach(child) do { } while (0) |
| 732 | #endif |
| 733 | |
Namhyung Kim | 4abf986 | 2010-10-27 15:33:45 -0700 | [diff] [blame] | 734 | SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr, |
| 735 | unsigned long, data) |
Christoph Hellwig | 481bed4 | 2005-11-07 00:59:47 -0800 | [diff] [blame] | 736 | { |
| 737 | struct task_struct *child; |
| 738 | long ret; |
| 739 | |
Christoph Hellwig | 6b9c7ed | 2006-01-08 01:02:33 -0800 | [diff] [blame] | 740 | if (request == PTRACE_TRACEME) { |
| 741 | ret = ptrace_traceme(); |
Haavard Skinnemoen | 6ea6dd9 | 2007-11-27 13:02:40 +0100 | [diff] [blame] | 742 | if (!ret) |
| 743 | arch_ptrace_attach(current); |
Christoph Hellwig | 481bed4 | 2005-11-07 00:59:47 -0800 | [diff] [blame] | 744 | goto out; |
Christoph Hellwig | 6b9c7ed | 2006-01-08 01:02:33 -0800 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | child = ptrace_get_task_struct(pid); |
| 748 | if (IS_ERR(child)) { |
| 749 | ret = PTR_ERR(child); |
| 750 | goto out; |
| 751 | } |
Christoph Hellwig | 481bed4 | 2005-11-07 00:59:47 -0800 | [diff] [blame] | 752 | |
| 753 | if (request == PTRACE_ATTACH) { |
| 754 | ret = ptrace_attach(child); |
Christoph Hellwig | 0ac1555 | 2007-10-16 01:26:37 -0700 | [diff] [blame] | 755 | /* |
| 756 | * Some architectures need to do book-keeping after |
| 757 | * a ptrace attach. |
| 758 | */ |
| 759 | if (!ret) |
| 760 | arch_ptrace_attach(child); |
Christoph Hellwig | 005f18d | 2005-11-13 16:06:33 -0800 | [diff] [blame] | 761 | goto out_put_task_struct; |
Christoph Hellwig | 481bed4 | 2005-11-07 00:59:47 -0800 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | ret = ptrace_check_attach(child, request == PTRACE_KILL); |
| 765 | if (ret < 0) |
| 766 | goto out_put_task_struct; |
| 767 | |
| 768 | ret = arch_ptrace(child, request, addr, data); |
Christoph Hellwig | 481bed4 | 2005-11-07 00:59:47 -0800 | [diff] [blame] | 769 | |
| 770 | out_put_task_struct: |
| 771 | put_task_struct(child); |
| 772 | out: |
Christoph Hellwig | 481bed4 | 2005-11-07 00:59:47 -0800 | [diff] [blame] | 773 | return ret; |
| 774 | } |
Alexey Dobriyan | 7664732 | 2007-07-17 04:03:43 -0700 | [diff] [blame] | 775 | |
Namhyung Kim | 4abf986 | 2010-10-27 15:33:45 -0700 | [diff] [blame] | 776 | int generic_ptrace_peekdata(struct task_struct *tsk, unsigned long addr, |
| 777 | unsigned long data) |
Alexey Dobriyan | 7664732 | 2007-07-17 04:03:43 -0700 | [diff] [blame] | 778 | { |
| 779 | unsigned long tmp; |
| 780 | int copied; |
| 781 | |
| 782 | copied = access_process_vm(tsk, addr, &tmp, sizeof(tmp), 0); |
| 783 | if (copied != sizeof(tmp)) |
| 784 | return -EIO; |
| 785 | return put_user(tmp, (unsigned long __user *)data); |
| 786 | } |
Alexey Dobriyan | f284ce7 | 2007-07-17 04:03:44 -0700 | [diff] [blame] | 787 | |
Namhyung Kim | 4abf986 | 2010-10-27 15:33:45 -0700 | [diff] [blame] | 788 | int generic_ptrace_pokedata(struct task_struct *tsk, unsigned long addr, |
| 789 | unsigned long data) |
Alexey Dobriyan | f284ce7 | 2007-07-17 04:03:44 -0700 | [diff] [blame] | 790 | { |
| 791 | int copied; |
| 792 | |
| 793 | copied = access_process_vm(tsk, addr, &data, sizeof(data), 1); |
| 794 | return (copied == sizeof(data)) ? 0 : -EIO; |
| 795 | } |
Roland McGrath | 032d82d | 2008-01-30 13:31:47 +0100 | [diff] [blame] | 796 | |
Christoph Hellwig | 96b8936 | 2008-11-25 08:10:03 +0100 | [diff] [blame] | 797 | #if defined CONFIG_COMPAT |
Roland McGrath | 032d82d | 2008-01-30 13:31:47 +0100 | [diff] [blame] | 798 | #include <linux/compat.h> |
| 799 | |
| 800 | int compat_ptrace_request(struct task_struct *child, compat_long_t request, |
| 801 | compat_ulong_t addr, compat_ulong_t data) |
| 802 | { |
| 803 | compat_ulong_t __user *datap = compat_ptr(data); |
| 804 | compat_ulong_t word; |
Roland McGrath | e16b278 | 2008-04-20 13:10:12 -0700 | [diff] [blame] | 805 | siginfo_t siginfo; |
Roland McGrath | 032d82d | 2008-01-30 13:31:47 +0100 | [diff] [blame] | 806 | int ret; |
| 807 | |
| 808 | switch (request) { |
| 809 | case PTRACE_PEEKTEXT: |
| 810 | case PTRACE_PEEKDATA: |
| 811 | ret = access_process_vm(child, addr, &word, sizeof(word), 0); |
| 812 | if (ret != sizeof(word)) |
| 813 | ret = -EIO; |
| 814 | else |
| 815 | ret = put_user(word, datap); |
| 816 | break; |
| 817 | |
| 818 | case PTRACE_POKETEXT: |
| 819 | case PTRACE_POKEDATA: |
| 820 | ret = access_process_vm(child, addr, &data, sizeof(data), 1); |
| 821 | ret = (ret != sizeof(data) ? -EIO : 0); |
| 822 | break; |
| 823 | |
| 824 | case PTRACE_GETEVENTMSG: |
| 825 | ret = put_user((compat_ulong_t) child->ptrace_message, datap); |
| 826 | break; |
| 827 | |
Roland McGrath | e16b278 | 2008-04-20 13:10:12 -0700 | [diff] [blame] | 828 | case PTRACE_GETSIGINFO: |
| 829 | ret = ptrace_getsiginfo(child, &siginfo); |
| 830 | if (!ret) |
| 831 | ret = copy_siginfo_to_user32( |
| 832 | (struct compat_siginfo __user *) datap, |
| 833 | &siginfo); |
| 834 | break; |
| 835 | |
| 836 | case PTRACE_SETSIGINFO: |
| 837 | memset(&siginfo, 0, sizeof siginfo); |
| 838 | if (copy_siginfo_from_user32( |
| 839 | &siginfo, (struct compat_siginfo __user *) datap)) |
| 840 | ret = -EFAULT; |
| 841 | else |
| 842 | ret = ptrace_setsiginfo(child, &siginfo); |
| 843 | break; |
Suresh Siddha | 2225a12 | 2010-02-11 11:51:00 -0800 | [diff] [blame] | 844 | #ifdef CONFIG_HAVE_ARCH_TRACEHOOK |
| 845 | case PTRACE_GETREGSET: |
| 846 | case PTRACE_SETREGSET: |
| 847 | { |
| 848 | struct iovec kiov; |
| 849 | struct compat_iovec __user *uiov = |
| 850 | (struct compat_iovec __user *) datap; |
| 851 | compat_uptr_t ptr; |
| 852 | compat_size_t len; |
| 853 | |
| 854 | if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov))) |
| 855 | return -EFAULT; |
| 856 | |
| 857 | if (__get_user(ptr, &uiov->iov_base) || |
| 858 | __get_user(len, &uiov->iov_len)) |
| 859 | return -EFAULT; |
| 860 | |
| 861 | kiov.iov_base = compat_ptr(ptr); |
| 862 | kiov.iov_len = len; |
| 863 | |
| 864 | ret = ptrace_regset(child, request, addr, &kiov); |
| 865 | if (!ret) |
| 866 | ret = __put_user(kiov.iov_len, &uiov->iov_len); |
| 867 | break; |
| 868 | } |
| 869 | #endif |
Roland McGrath | e16b278 | 2008-04-20 13:10:12 -0700 | [diff] [blame] | 870 | |
Roland McGrath | 032d82d | 2008-01-30 13:31:47 +0100 | [diff] [blame] | 871 | default: |
| 872 | ret = ptrace_request(child, request, addr, data); |
| 873 | } |
| 874 | |
| 875 | return ret; |
| 876 | } |
Roland McGrath | c269f19 | 2008-01-30 13:31:48 +0100 | [diff] [blame] | 877 | |
Roland McGrath | c269f19 | 2008-01-30 13:31:48 +0100 | [diff] [blame] | 878 | asmlinkage long compat_sys_ptrace(compat_long_t request, compat_long_t pid, |
| 879 | compat_long_t addr, compat_long_t data) |
| 880 | { |
| 881 | struct task_struct *child; |
| 882 | long ret; |
| 883 | |
Roland McGrath | c269f19 | 2008-01-30 13:31:48 +0100 | [diff] [blame] | 884 | if (request == PTRACE_TRACEME) { |
| 885 | ret = ptrace_traceme(); |
| 886 | goto out; |
| 887 | } |
| 888 | |
| 889 | child = ptrace_get_task_struct(pid); |
| 890 | if (IS_ERR(child)) { |
| 891 | ret = PTR_ERR(child); |
| 892 | goto out; |
| 893 | } |
| 894 | |
| 895 | if (request == PTRACE_ATTACH) { |
| 896 | ret = ptrace_attach(child); |
| 897 | /* |
| 898 | * Some architectures need to do book-keeping after |
| 899 | * a ptrace attach. |
| 900 | */ |
| 901 | if (!ret) |
| 902 | arch_ptrace_attach(child); |
| 903 | goto out_put_task_struct; |
| 904 | } |
| 905 | |
| 906 | ret = ptrace_check_attach(child, request == PTRACE_KILL); |
| 907 | if (!ret) |
| 908 | ret = compat_arch_ptrace(child, request, addr, data); |
| 909 | |
| 910 | out_put_task_struct: |
| 911 | put_task_struct(child); |
| 912 | out: |
Roland McGrath | c269f19 | 2008-01-30 13:31:48 +0100 | [diff] [blame] | 913 | return ret; |
| 914 | } |
Christoph Hellwig | 96b8936 | 2008-11-25 08:10:03 +0100 | [diff] [blame] | 915 | #endif /* CONFIG_COMPAT */ |