Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | ||
c59ede7b | 10 | #include <linux/capability.h> |
9984de1a | 11 | #include <linux/export.h> |
1da177e4 LT |
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> | |
1da177e4 LT |
17 | #include <linux/ptrace.h> |
18 | #include <linux/security.h> | |
7ed20e1a | 19 | #include <linux/signal.h> |
a27bb332 | 20 | #include <linux/uio.h> |
a5cb013d | 21 | #include <linux/audit.h> |
b488893a | 22 | #include <linux/pid_namespace.h> |
3c2a0909 | 23 | #include <linux/user_namespace.h> |
f17d30a8 | 24 | #include <linux/syscalls.h> |
3a709703 | 25 | #include <linux/uaccess.h> |
2225a122 | 26 | #include <linux/regset.h> |
bf26c018 | 27 | #include <linux/hw_breakpoint.h> |
f701e5b7 | 28 | #include <linux/cn_proc.h> |
84c751bd | 29 | #include <linux/compat.h> |
1da177e4 | 30 | |
bf53de90 | 31 | |
62c124ff TH |
32 | static int ptrace_trapping_sleep_fn(void *flags) |
33 | { | |
34 | schedule(); | |
35 | return 0; | |
36 | } | |
37 | ||
1da177e4 LT |
38 | /* |
39 | * ptrace a task: make the debugger its new parent and | |
40 | * move it to the ptrace list. | |
41 | * | |
42 | * Must be called with the tasklist lock write-held. | |
43 | */ | |
36c8b586 | 44 | void __ptrace_link(struct task_struct *child, struct task_struct *new_parent) |
1da177e4 | 45 | { |
f470021a RM |
46 | BUG_ON(!list_empty(&child->ptrace_entry)); |
47 | list_add(&child->ptrace_entry, &new_parent->ptraced); | |
1da177e4 | 48 | child->parent = new_parent; |
1da177e4 | 49 | } |
3a709703 | 50 | |
e3bd058f TH |
51 | /** |
52 | * __ptrace_unlink - unlink ptracee and restore its execution state | |
53 | * @child: ptracee to be unlinked | |
1da177e4 | 54 | * |
0e9f0a4a TH |
55 | * Remove @child from the ptrace list, move it back to the original parent, |
56 | * and restore the execution state so that it conforms to the group stop | |
57 | * state. | |
58 | * | |
59 | * Unlinking can happen via two paths - explicit PTRACE_DETACH or ptracer | |
60 | * exiting. For PTRACE_DETACH, unless the ptracee has been killed between | |
61 | * ptrace_check_attach() and here, it's guaranteed to be in TASK_TRACED. | |
62 | * If the ptracer is exiting, the ptracee can be in any state. | |
63 | * | |
64 | * After detach, the ptracee should be in a state which conforms to the | |
65 | * group stop. If the group is stopped or in the process of stopping, the | |
66 | * ptracee should be put into TASK_STOPPED; otherwise, it should be woken | |
67 | * up from TASK_TRACED. | |
68 | * | |
69 | * If the ptracee is in TASK_TRACED and needs to be moved to TASK_STOPPED, | |
70 | * it goes through TRACED -> RUNNING -> STOPPED transition which is similar | |
71 | * to but in the opposite direction of what happens while attaching to a | |
72 | * stopped task. However, in this direction, the intermediate RUNNING | |
73 | * state is not hidden even from the current ptracer and if it immediately | |
74 | * re-attaches and performs a WNOHANG wait(2), it may fail. | |
e3bd058f TH |
75 | * |
76 | * CONTEXT: | |
77 | * write_lock_irq(tasklist_lock) | |
1da177e4 | 78 | */ |
36c8b586 | 79 | void __ptrace_unlink(struct task_struct *child) |
1da177e4 | 80 | { |
5ecfbae0 ON |
81 | BUG_ON(!child->ptrace); |
82 | ||
1da177e4 | 83 | child->ptrace = 0; |
f470021a RM |
84 | child->parent = child->real_parent; |
85 | list_del_init(&child->ptrace_entry); | |
1da177e4 | 86 | |
1da177e4 | 87 | spin_lock(&child->sighand->siglock); |
0e9f0a4a | 88 | |
73ddff2b TH |
89 | /* |
90 | * Clear all pending traps and TRAPPING. TRAPPING should be | |
91 | * cleared regardless of JOBCTL_STOP_PENDING. Do it explicitly. | |
92 | */ | |
93 | task_clear_jobctl_pending(child, JOBCTL_TRAP_MASK); | |
94 | task_clear_jobctl_trapping(child); | |
95 | ||
0e9f0a4a | 96 | /* |
a8f072c1 | 97 | * Reinstate JOBCTL_STOP_PENDING if group stop is in effect and |
0e9f0a4a TH |
98 | * @child isn't dead. |
99 | */ | |
100 | if (!(child->flags & PF_EXITING) && | |
101 | (child->signal->flags & SIGNAL_STOP_STOPPED || | |
8a88951b | 102 | child->signal->group_stop_count)) { |
a8f072c1 | 103 | child->jobctl |= JOBCTL_STOP_PENDING; |
0e9f0a4a | 104 | |
8a88951b ON |
105 | /* |
106 | * This is only possible if this thread was cloned by the | |
107 | * traced task running in the stopped group, set the signal | |
108 | * for the future reports. | |
109 | * FIXME: we should change ptrace_init_task() to handle this | |
110 | * case. | |
111 | */ | |
112 | if (!(child->jobctl & JOBCTL_STOP_SIGMASK)) | |
113 | child->jobctl |= SIGSTOP; | |
114 | } | |
115 | ||
0e9f0a4a TH |
116 | /* |
117 | * If transition to TASK_STOPPED is pending or in TASK_TRACED, kick | |
118 | * @child in the butt. Note that @resume should be used iff @child | |
119 | * is in TASK_TRACED; otherwise, we might unduly disrupt | |
120 | * TASK_KILLABLE sleeps. | |
121 | */ | |
a8f072c1 | 122 | if (child->jobctl & JOBCTL_STOP_PENDING || task_is_traced(child)) |
910ffdb1 | 123 | ptrace_signal_wake_up(child, true); |
0e9f0a4a | 124 | |
1da177e4 | 125 | spin_unlock(&child->sighand->siglock); |
1da177e4 LT |
126 | } |
127 | ||
9899d11f ON |
128 | /* Ensure that nothing can wake it up, even SIGKILL */ |
129 | static bool ptrace_freeze_traced(struct task_struct *task) | |
130 | { | |
131 | bool ret = false; | |
132 | ||
133 | /* Lockless, nobody but us can set this flag */ | |
134 | if (task->jobctl & JOBCTL_LISTENING) | |
135 | return ret; | |
136 | ||
137 | spin_lock_irq(&task->sighand->siglock); | |
138 | if (task_is_traced(task) && !__fatal_signal_pending(task)) { | |
139 | task->state = __TASK_TRACED; | |
140 | ret = true; | |
141 | } | |
142 | spin_unlock_irq(&task->sighand->siglock); | |
143 | ||
144 | return ret; | |
145 | } | |
146 | ||
147 | static void ptrace_unfreeze_traced(struct task_struct *task) | |
148 | { | |
149 | if (task->state != __TASK_TRACED) | |
150 | return; | |
151 | ||
152 | WARN_ON(!task->ptrace || task->parent != current); | |
153 | ||
154 | spin_lock_irq(&task->sighand->siglock); | |
155 | if (__fatal_signal_pending(task)) | |
156 | wake_up_state(task, __TASK_TRACED); | |
157 | else | |
158 | task->state = TASK_TRACED; | |
159 | spin_unlock_irq(&task->sighand->siglock); | |
160 | } | |
161 | ||
755e276b TH |
162 | /** |
163 | * ptrace_check_attach - check whether ptracee is ready for ptrace operation | |
164 | * @child: ptracee to check for | |
165 | * @ignore_state: don't check whether @child is currently %TASK_TRACED | |
166 | * | |
167 | * Check whether @child is being ptraced by %current and ready for further | |
168 | * ptrace operations. If @ignore_state is %false, @child also should be in | |
169 | * %TASK_TRACED state and on return the child is guaranteed to be traced | |
170 | * and not executing. If @ignore_state is %true, @child can be in any | |
171 | * state. | |
172 | * | |
173 | * CONTEXT: | |
174 | * Grabs and releases tasklist_lock and @child->sighand->siglock. | |
175 | * | |
176 | * RETURNS: | |
177 | * 0 on success, -ESRCH if %child is not ready. | |
1da177e4 | 178 | */ |
edea0d03 | 179 | static int ptrace_check_attach(struct task_struct *child, bool ignore_state) |
1da177e4 LT |
180 | { |
181 | int ret = -ESRCH; | |
182 | ||
183 | /* | |
184 | * We take the read lock around doing both checks to close a | |
185 | * possible race where someone else was tracing our child and | |
186 | * detached between these two checks. After this locked check, | |
187 | * we are sure that this is our traced child and that can only | |
188 | * be changed by us so it's not changing right after this. | |
189 | */ | |
190 | read_lock(&tasklist_lock); | |
9899d11f ON |
191 | if (child->ptrace && child->parent == current) { |
192 | WARN_ON(child->state == __TASK_TRACED); | |
c0c0b649 ON |
193 | /* |
194 | * child->sighand can't be NULL, release_task() | |
195 | * does ptrace_unlink() before __exit_signal(). | |
196 | */ | |
9899d11f | 197 | if (ignore_state || ptrace_freeze_traced(child)) |
321fb561 | 198 | ret = 0; |
1da177e4 LT |
199 | } |
200 | read_unlock(&tasklist_lock); | |
201 | ||
9899d11f ON |
202 | if (!ret && !ignore_state) { |
203 | if (!wait_task_inactive(child, __TASK_TRACED)) { | |
204 | /* | |
205 | * This can only happen if may_ptrace_stop() fails and | |
206 | * ptrace_stop() changes ->state back to TASK_RUNNING, | |
207 | * so we should not worry about leaking __TASK_TRACED. | |
208 | */ | |
209 | WARN_ON(child->state == __TASK_TRACED); | |
210 | ret = -ESRCH; | |
211 | } | |
212 | } | |
1da177e4 | 213 | |
1da177e4 LT |
214 | return ret; |
215 | } | |
216 | ||
3c2a0909 | 217 | static bool ptrace_has_cap(const struct cred *tcred, unsigned int mode) |
69f594a3 | 218 | { |
3c2a0909 S |
219 | struct user_namespace *tns = tcred->user_ns; |
220 | /* When a root-owned process enters a user namespace created by a | |
221 | * malicious user, the user shouldn't be able to execute code under | |
222 | * uid 0 by attaching to the root-owned process via ptrace. | |
223 | * Therefore, similar to the capable_wrt_inode_uidgid() check, | |
224 | * verify that all the uids and gids of the target process are | |
225 | * mapped into a namespace below the current one in which the caller | |
226 | * is capable.+ * No fsuid/fsgid check because __ptrace_may_access doesn't do it | |
227 | * either. | |
228 | */ | |
229 | while ( | |
230 | !kuid_has_mapping(tns, tcred->euid) || | |
231 | !kuid_has_mapping(tns, tcred->suid) || | |
232 | !kuid_has_mapping(tns, tcred->uid) || | |
233 | !kgid_has_mapping(tns, tcred->egid) || | |
234 | !kgid_has_mapping(tns, tcred->sgid) || | |
235 | !kgid_has_mapping(tns, tcred->gid)) { | |
236 | tns = tns->parent; | |
237 | } | |
238 | ||
69f594a3 | 239 | if (mode & PTRACE_MODE_NOAUDIT) |
3c2a0909 | 240 | return has_ns_capability_noaudit(current, tns, CAP_SYS_PTRACE); |
69f594a3 | 241 | else |
3c2a0909 | 242 | return has_ns_capability(current, tns, CAP_SYS_PTRACE); |
69f594a3 EP |
243 | } |
244 | ||
9f99798f TH |
245 | /* Returns 0 on success, -errno on denial. */ |
246 | static int __ptrace_may_access(struct task_struct *task, unsigned int mode) | |
ab8d11be | 247 | { |
c69e8d9c | 248 | const struct cred *cred = current_cred(), *tcred; |
b6dff3ec | 249 | |
df26c40e EB |
250 | /* May we inspect the given task? |
251 | * This check is used both for attaching with ptrace | |
252 | * and for allowing access to sensitive information in /proc. | |
253 | * | |
254 | * ptrace_attach denies several cases that /proc allows | |
255 | * because setting up the necessary parent/child relationship | |
256 | * or halting the specified task is impossible. | |
257 | */ | |
258 | int dumpable = 0; | |
259 | /* Don't let security modules deny introspection */ | |
3c2a0909 | 260 | if (same_thread_group(task, current)) |
df26c40e | 261 | return 0; |
c69e8d9c DH |
262 | rcu_read_lock(); |
263 | tcred = __task_cred(task); | |
5af66203 EB |
264 | if (uid_eq(cred->uid, tcred->euid) && |
265 | uid_eq(cred->uid, tcred->suid) && | |
266 | uid_eq(cred->uid, tcred->uid) && | |
267 | gid_eq(cred->gid, tcred->egid) && | |
268 | gid_eq(cred->gid, tcred->sgid) && | |
269 | gid_eq(cred->gid, tcred->gid)) | |
8409cca7 | 270 | goto ok; |
3c2a0909 | 271 | if (ptrace_has_cap(tcred, mode)) |
8409cca7 SH |
272 | goto ok; |
273 | rcu_read_unlock(); | |
274 | return -EPERM; | |
275 | ok: | |
c69e8d9c | 276 | rcu_read_unlock(); |
ab8d11be | 277 | smp_rmb(); |
df26c40e | 278 | if (task->mm) |
6c5d5238 | 279 | dumpable = get_dumpable(task->mm); |
4c44aaaf | 280 | rcu_read_lock(); |
d389d610 | 281 | if (dumpable != SUID_DUMP_USER && |
3c2a0909 | 282 | !ptrace_has_cap(__task_cred(task), mode)) { |
4c44aaaf | 283 | rcu_read_unlock(); |
ab8d11be | 284 | return -EPERM; |
4c44aaaf EB |
285 | } |
286 | rcu_read_unlock(); | |
ab8d11be | 287 | |
9e48858f | 288 | return security_ptrace_access_check(task, mode); |
ab8d11be MS |
289 | } |
290 | ||
006ebb40 | 291 | bool ptrace_may_access(struct task_struct *task, unsigned int mode) |
ab8d11be MS |
292 | { |
293 | int err; | |
294 | task_lock(task); | |
006ebb40 | 295 | err = __ptrace_may_access(task, mode); |
ab8d11be | 296 | task_unlock(task); |
3a709703 | 297 | return !err; |
ab8d11be MS |
298 | } |
299 | ||
3544d72a | 300 | static int ptrace_attach(struct task_struct *task, long request, |
aa9147c9 | 301 | unsigned long addr, |
3544d72a | 302 | unsigned long flags) |
1da177e4 | 303 | { |
3544d72a | 304 | bool seize = (request == PTRACE_SEIZE); |
1da177e4 | 305 | int retval; |
f5b40e36 | 306 | |
3544d72a | 307 | retval = -EIO; |
aa9147c9 DV |
308 | if (seize) { |
309 | if (addr != 0) | |
310 | goto out; | |
aa9147c9 DV |
311 | if (flags & ~(unsigned long)PTRACE_O_MASK) |
312 | goto out; | |
313 | flags = PT_PTRACED | PT_SEIZED | (flags << PT_OPT_FLAG_SHIFT); | |
314 | } else { | |
315 | flags = PT_PTRACED; | |
316 | } | |
3544d72a | 317 | |
a5cb013d AV |
318 | audit_ptrace(task); |
319 | ||
1da177e4 | 320 | retval = -EPERM; |
b79b7ba9 ON |
321 | if (unlikely(task->flags & PF_KTHREAD)) |
322 | goto out; | |
bac0abd6 | 323 | if (same_thread_group(task, current)) |
f5b40e36 LT |
324 | goto out; |
325 | ||
f2f0b00a ON |
326 | /* |
327 | * Protect exec's credential calculations against our interference; | |
86b6c1f3 | 328 | * SUID, SGID and LSM creds get determined differently |
5e751e99 | 329 | * under ptrace. |
d84f4f99 | 330 | */ |
793285fc | 331 | retval = -ERESTARTNOINTR; |
9b1bf12d | 332 | if (mutex_lock_interruptible(&task->signal->cred_guard_mutex)) |
d84f4f99 | 333 | goto out; |
f5b40e36 | 334 | |
4b105cbb | 335 | task_lock(task); |
006ebb40 | 336 | retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH); |
4b105cbb | 337 | task_unlock(task); |
1da177e4 | 338 | if (retval) |
4b105cbb | 339 | goto unlock_creds; |
1da177e4 | 340 | |
4b105cbb | 341 | write_lock_irq(&tasklist_lock); |
b79b7ba9 ON |
342 | retval = -EPERM; |
343 | if (unlikely(task->exit_state)) | |
4b105cbb | 344 | goto unlock_tasklist; |
f2f0b00a | 345 | if (task->ptrace) |
4b105cbb | 346 | goto unlock_tasklist; |
b79b7ba9 | 347 | |
3544d72a | 348 | if (seize) |
aa9147c9 | 349 | flags |= PT_SEIZED; |
4c44aaaf EB |
350 | rcu_read_lock(); |
351 | if (ns_capable(__task_cred(task)->user_ns, CAP_SYS_PTRACE)) | |
aa9147c9 | 352 | flags |= PT_PTRACE_CAP; |
4c44aaaf | 353 | rcu_read_unlock(); |
aa9147c9 | 354 | task->ptrace = flags; |
1da177e4 | 355 | |
1da177e4 | 356 | __ptrace_link(task, current); |
3544d72a TH |
357 | |
358 | /* SEIZE doesn't trap tracee on attach */ | |
359 | if (!seize) | |
360 | send_sig_info(SIGSTOP, SEND_SIG_FORCED, task); | |
b79b7ba9 | 361 | |
d79fdd6d TH |
362 | spin_lock(&task->sighand->siglock); |
363 | ||
364 | /* | |
73ddff2b | 365 | * If the task is already STOPPED, set JOBCTL_TRAP_STOP and |
d79fdd6d TH |
366 | * TRAPPING, and kick it so that it transits to TRACED. TRAPPING |
367 | * will be cleared if the child completes the transition or any | |
368 | * event which clears the group stop states happens. We'll wait | |
369 | * for the transition to complete before returning from this | |
370 | * function. | |
371 | * | |
372 | * This hides STOPPED -> RUNNING -> TRACED transition from the | |
373 | * attaching thread but a different thread in the same group can | |
374 | * still observe the transient RUNNING state. IOW, if another | |
375 | * thread's WNOHANG wait(2) on the stopped tracee races against | |
376 | * ATTACH, the wait(2) may fail due to the transient RUNNING. | |
377 | * | |
378 | * The following task_is_stopped() test is safe as both transitions | |
379 | * in and out of STOPPED are protected by siglock. | |
380 | */ | |
7dd3db54 | 381 | if (task_is_stopped(task) && |
73ddff2b | 382 | task_set_jobctl_pending(task, JOBCTL_TRAP_STOP | JOBCTL_TRAPPING)) |
910ffdb1 | 383 | signal_wake_up_state(task, __TASK_STOPPED); |
d79fdd6d TH |
384 | |
385 | spin_unlock(&task->sighand->siglock); | |
386 | ||
b79b7ba9 | 387 | retval = 0; |
4b105cbb ON |
388 | unlock_tasklist: |
389 | write_unlock_irq(&tasklist_lock); | |
390 | unlock_creds: | |
9b1bf12d | 391 | mutex_unlock(&task->signal->cred_guard_mutex); |
f5b40e36 | 392 | out: |
f701e5b7 | 393 | if (!retval) { |
62c124ff TH |
394 | wait_on_bit(&task->jobctl, JOBCTL_TRAPPING_BIT, |
395 | ptrace_trapping_sleep_fn, TASK_UNINTERRUPTIBLE); | |
f701e5b7 VZ |
396 | proc_ptrace_connector(task, PTRACE_ATTACH); |
397 | } | |
398 | ||
1da177e4 LT |
399 | return retval; |
400 | } | |
401 | ||
f2f0b00a ON |
402 | /** |
403 | * ptrace_traceme -- helper for PTRACE_TRACEME | |
404 | * | |
405 | * Performs checks and sets PT_PTRACED. | |
406 | * Should be used by all ptrace implementations for PTRACE_TRACEME. | |
407 | */ | |
e3e89cc5 | 408 | static int ptrace_traceme(void) |
f2f0b00a ON |
409 | { |
410 | int ret = -EPERM; | |
411 | ||
4b105cbb ON |
412 | write_lock_irq(&tasklist_lock); |
413 | /* Are we already being traced? */ | |
f2f0b00a | 414 | if (!current->ptrace) { |
f2f0b00a | 415 | ret = security_ptrace_traceme(current->parent); |
f2f0b00a ON |
416 | /* |
417 | * Check PF_EXITING to ensure ->real_parent has not passed | |
418 | * exit_ptrace(). Otherwise we don't report the error but | |
419 | * pretend ->real_parent untraces us right after return. | |
420 | */ | |
421 | if (!ret && !(current->real_parent->flags & PF_EXITING)) { | |
422 | current->ptrace = PT_PTRACED; | |
423 | __ptrace_link(current, current->real_parent); | |
424 | } | |
f2f0b00a | 425 | } |
4b105cbb ON |
426 | write_unlock_irq(&tasklist_lock); |
427 | ||
f2f0b00a ON |
428 | return ret; |
429 | } | |
430 | ||
39c626ae ON |
431 | /* |
432 | * Called with irqs disabled, returns true if childs should reap themselves. | |
433 | */ | |
434 | static int ignoring_children(struct sighand_struct *sigh) | |
435 | { | |
436 | int ret; | |
437 | spin_lock(&sigh->siglock); | |
438 | ret = (sigh->action[SIGCHLD-1].sa.sa_handler == SIG_IGN) || | |
439 | (sigh->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT); | |
440 | spin_unlock(&sigh->siglock); | |
441 | return ret; | |
442 | } | |
443 | ||
444 | /* | |
445 | * Called with tasklist_lock held for writing. | |
446 | * Unlink a traced task, and clean it up if it was a traced zombie. | |
447 | * Return true if it needs to be reaped with release_task(). | |
448 | * (We can't call release_task() here because we already hold tasklist_lock.) | |
449 | * | |
450 | * If it's a zombie, our attachedness prevented normal parent notification | |
451 | * or self-reaping. Do notification now if it would have happened earlier. | |
452 | * If it should reap itself, return true. | |
453 | * | |
a7f0765e ON |
454 | * If it's our own child, there is no notification to do. But if our normal |
455 | * children self-reap, then this child was prevented by ptrace and we must | |
456 | * reap it now, in that case we must also wake up sub-threads sleeping in | |
457 | * do_wait(). | |
39c626ae ON |
458 | */ |
459 | static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p) | |
460 | { | |
9843a1e9 ON |
461 | bool dead; |
462 | ||
39c626ae ON |
463 | __ptrace_unlink(p); |
464 | ||
9843a1e9 ON |
465 | if (p->exit_state != EXIT_ZOMBIE) |
466 | return false; | |
467 | ||
468 | dead = !thread_group_leader(p); | |
469 | ||
470 | if (!dead && thread_group_empty(p)) { | |
471 | if (!same_thread_group(p->real_parent, tracer)) | |
472 | dead = do_notify_parent(p, p->exit_signal); | |
473 | else if (ignoring_children(tracer->sighand)) { | |
474 | __wake_up_parent(p, tracer); | |
9843a1e9 | 475 | dead = true; |
39c626ae ON |
476 | } |
477 | } | |
9843a1e9 ON |
478 | /* Mark it as in the process of being reaped. */ |
479 | if (dead) | |
480 | p->exit_state = EXIT_DEAD; | |
481 | return dead; | |
39c626ae ON |
482 | } |
483 | ||
e3e89cc5 | 484 | static int ptrace_detach(struct task_struct *child, unsigned int data) |
1da177e4 | 485 | { |
39c626ae | 486 | bool dead = false; |
4576145c | 487 | |
7ed20e1a | 488 | if (!valid_signal(data)) |
5ecfbae0 | 489 | return -EIO; |
1da177e4 LT |
490 | |
491 | /* Architecture-specific hardware disable .. */ | |
492 | ptrace_disable(child); | |
7d941432 | 493 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
1da177e4 | 494 | |
95c3eb76 | 495 | write_lock_irq(&tasklist_lock); |
39c626ae ON |
496 | /* |
497 | * This child can be already killed. Make sure de_thread() or | |
498 | * our sub-thread doing do_wait() didn't do release_task() yet. | |
499 | */ | |
95c3eb76 ON |
500 | if (child->ptrace) { |
501 | child->exit_code = data; | |
4576145c | 502 | dead = __ptrace_detach(current, child); |
95c3eb76 | 503 | } |
1da177e4 LT |
504 | write_unlock_irq(&tasklist_lock); |
505 | ||
f701e5b7 | 506 | proc_ptrace_connector(child, PTRACE_DETACH); |
4576145c ON |
507 | if (unlikely(dead)) |
508 | release_task(child); | |
509 | ||
1da177e4 LT |
510 | return 0; |
511 | } | |
512 | ||
39c626ae | 513 | /* |
c7e49c14 ON |
514 | * Detach all tasks we were using ptrace on. Called with tasklist held |
515 | * for writing, and returns with it held too. But note it can release | |
516 | * and reacquire the lock. | |
39c626ae ON |
517 | */ |
518 | void exit_ptrace(struct task_struct *tracer) | |
c4b5ed25 NK |
519 | __releases(&tasklist_lock) |
520 | __acquires(&tasklist_lock) | |
39c626ae ON |
521 | { |
522 | struct task_struct *p, *n; | |
523 | LIST_HEAD(ptrace_dead); | |
524 | ||
c7e49c14 ON |
525 | if (likely(list_empty(&tracer->ptraced))) |
526 | return; | |
527 | ||
39c626ae | 528 | list_for_each_entry_safe(p, n, &tracer->ptraced, ptrace_entry) { |
992fb6e1 ON |
529 | if (unlikely(p->ptrace & PT_EXITKILL)) |
530 | send_sig_info(SIGKILL, SEND_SIG_FORCED, p); | |
531 | ||
39c626ae ON |
532 | if (__ptrace_detach(tracer, p)) |
533 | list_add(&p->ptrace_entry, &ptrace_dead); | |
534 | } | |
39c626ae | 535 | |
c7e49c14 | 536 | write_unlock_irq(&tasklist_lock); |
39c626ae ON |
537 | BUG_ON(!list_empty(&tracer->ptraced)); |
538 | ||
539 | list_for_each_entry_safe(p, n, &ptrace_dead, ptrace_entry) { | |
540 | list_del_init(&p->ptrace_entry); | |
541 | release_task(p); | |
542 | } | |
c7e49c14 ON |
543 | |
544 | write_lock_irq(&tasklist_lock); | |
39c626ae ON |
545 | } |
546 | ||
1da177e4 LT |
547 | int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len) |
548 | { | |
549 | int copied = 0; | |
550 | ||
551 | while (len > 0) { | |
552 | char buf[128]; | |
553 | int this_len, retval; | |
554 | ||
555 | this_len = (len > sizeof(buf)) ? sizeof(buf) : len; | |
556 | retval = access_process_vm(tsk, src, buf, this_len, 0); | |
557 | if (!retval) { | |
558 | if (copied) | |
559 | break; | |
560 | return -EIO; | |
561 | } | |
562 | if (copy_to_user(dst, buf, retval)) | |
563 | return -EFAULT; | |
564 | copied += retval; | |
565 | src += retval; | |
566 | dst += retval; | |
3a709703 | 567 | len -= retval; |
1da177e4 LT |
568 | } |
569 | return copied; | |
570 | } | |
571 | ||
572 | int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len) | |
573 | { | |
574 | int copied = 0; | |
575 | ||
576 | while (len > 0) { | |
577 | char buf[128]; | |
578 | int this_len, retval; | |
579 | ||
580 | this_len = (len > sizeof(buf)) ? sizeof(buf) : len; | |
581 | if (copy_from_user(buf, src, this_len)) | |
582 | return -EFAULT; | |
583 | retval = access_process_vm(tsk, dst, buf, this_len, 1); | |
584 | if (!retval) { | |
585 | if (copied) | |
586 | break; | |
587 | return -EIO; | |
588 | } | |
589 | copied += retval; | |
590 | src += retval; | |
591 | dst += retval; | |
3a709703 | 592 | len -= retval; |
1da177e4 LT |
593 | } |
594 | return copied; | |
595 | } | |
596 | ||
4abf9869 | 597 | static int ptrace_setoptions(struct task_struct *child, unsigned long data) |
1da177e4 | 598 | { |
86b6c1f3 DV |
599 | unsigned flags; |
600 | ||
8c5cf9e5 DV |
601 | if (data & ~(unsigned long)PTRACE_O_MASK) |
602 | return -EINVAL; | |
603 | ||
86b6c1f3 DV |
604 | /* Avoid intermediate state when all opts are cleared */ |
605 | flags = child->ptrace; | |
606 | flags &= ~(PTRACE_O_MASK << PT_OPT_FLAG_SHIFT); | |
607 | flags |= (data << PT_OPT_FLAG_SHIFT); | |
608 | child->ptrace = flags; | |
1da177e4 | 609 | |
8c5cf9e5 | 610 | return 0; |
1da177e4 LT |
611 | } |
612 | ||
e16b2781 | 613 | static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info) |
1da177e4 | 614 | { |
e4961254 | 615 | unsigned long flags; |
1da177e4 LT |
616 | int error = -ESRCH; |
617 | ||
e4961254 | 618 | if (lock_task_sighand(child, &flags)) { |
1da177e4 | 619 | error = -EINVAL; |
1da177e4 | 620 | if (likely(child->last_siginfo != NULL)) { |
e16b2781 | 621 | *info = *child->last_siginfo; |
1da177e4 LT |
622 | error = 0; |
623 | } | |
e4961254 | 624 | unlock_task_sighand(child, &flags); |
1da177e4 | 625 | } |
1da177e4 LT |
626 | return error; |
627 | } | |
628 | ||
e16b2781 | 629 | static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info) |
1da177e4 | 630 | { |
e4961254 | 631 | unsigned long flags; |
1da177e4 LT |
632 | int error = -ESRCH; |
633 | ||
e4961254 | 634 | if (lock_task_sighand(child, &flags)) { |
1da177e4 | 635 | error = -EINVAL; |
1da177e4 | 636 | if (likely(child->last_siginfo != NULL)) { |
e16b2781 | 637 | *child->last_siginfo = *info; |
1da177e4 LT |
638 | error = 0; |
639 | } | |
e4961254 | 640 | unlock_task_sighand(child, &flags); |
1da177e4 | 641 | } |
1da177e4 LT |
642 | return error; |
643 | } | |
644 | ||
84c751bd AV |
645 | static int ptrace_peek_siginfo(struct task_struct *child, |
646 | unsigned long addr, | |
647 | unsigned long data) | |
648 | { | |
649 | struct ptrace_peeksiginfo_args arg; | |
650 | struct sigpending *pending; | |
651 | struct sigqueue *q; | |
652 | int ret, i; | |
653 | ||
654 | ret = copy_from_user(&arg, (void __user *) addr, | |
655 | sizeof(struct ptrace_peeksiginfo_args)); | |
656 | if (ret) | |
657 | return -EFAULT; | |
658 | ||
659 | if (arg.flags & ~PTRACE_PEEKSIGINFO_SHARED) | |
660 | return -EINVAL; /* unknown flags */ | |
661 | ||
662 | if (arg.nr < 0) | |
663 | return -EINVAL; | |
664 | ||
665 | if (arg.flags & PTRACE_PEEKSIGINFO_SHARED) | |
666 | pending = &child->signal->shared_pending; | |
667 | else | |
668 | pending = &child->pending; | |
669 | ||
670 | for (i = 0; i < arg.nr; ) { | |
671 | siginfo_t info; | |
672 | s32 off = arg.off + i; | |
673 | ||
674 | spin_lock_irq(&child->sighand->siglock); | |
675 | list_for_each_entry(q, &pending->list, list) { | |
676 | if (!off--) { | |
677 | copy_siginfo(&info, &q->info); | |
678 | break; | |
679 | } | |
680 | } | |
681 | spin_unlock_irq(&child->sighand->siglock); | |
682 | ||
683 | if (off >= 0) /* beyond the end of the list */ | |
684 | break; | |
685 | ||
686 | #ifdef CONFIG_COMPAT | |
687 | if (unlikely(is_compat_task())) { | |
688 | compat_siginfo_t __user *uinfo = compat_ptr(data); | |
689 | ||
706b23bd MD |
690 | if (copy_siginfo_to_user32(uinfo, &info) || |
691 | __put_user(info.si_code, &uinfo->si_code)) { | |
692 | ret = -EFAULT; | |
693 | break; | |
694 | } | |
695 | ||
84c751bd AV |
696 | } else |
697 | #endif | |
698 | { | |
699 | siginfo_t __user *uinfo = (siginfo_t __user *) data; | |
700 | ||
706b23bd MD |
701 | if (copy_siginfo_to_user(uinfo, &info) || |
702 | __put_user(info.si_code, &uinfo->si_code)) { | |
703 | ret = -EFAULT; | |
704 | break; | |
705 | } | |
84c751bd AV |
706 | } |
707 | ||
708 | data += sizeof(siginfo_t); | |
709 | i++; | |
710 | ||
711 | if (signal_pending(current)) | |
712 | break; | |
713 | ||
714 | cond_resched(); | |
715 | } | |
716 | ||
717 | if (i > 0) | |
718 | return i; | |
719 | ||
720 | return ret; | |
721 | } | |
36df29d7 RM |
722 | |
723 | #ifdef PTRACE_SINGLESTEP | |
724 | #define is_singlestep(request) ((request) == PTRACE_SINGLESTEP) | |
725 | #else | |
726 | #define is_singlestep(request) 0 | |
727 | #endif | |
728 | ||
5b88abbf RM |
729 | #ifdef PTRACE_SINGLEBLOCK |
730 | #define is_singleblock(request) ((request) == PTRACE_SINGLEBLOCK) | |
731 | #else | |
732 | #define is_singleblock(request) 0 | |
733 | #endif | |
734 | ||
36df29d7 RM |
735 | #ifdef PTRACE_SYSEMU |
736 | #define is_sysemu_singlestep(request) ((request) == PTRACE_SYSEMU_SINGLESTEP) | |
737 | #else | |
738 | #define is_sysemu_singlestep(request) 0 | |
739 | #endif | |
740 | ||
4abf9869 NK |
741 | static int ptrace_resume(struct task_struct *child, long request, |
742 | unsigned long data) | |
36df29d7 RM |
743 | { |
744 | if (!valid_signal(data)) | |
745 | return -EIO; | |
746 | ||
747 | if (request == PTRACE_SYSCALL) | |
748 | set_tsk_thread_flag(child, TIF_SYSCALL_TRACE); | |
749 | else | |
750 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); | |
751 | ||
752 | #ifdef TIF_SYSCALL_EMU | |
753 | if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP) | |
754 | set_tsk_thread_flag(child, TIF_SYSCALL_EMU); | |
755 | else | |
756 | clear_tsk_thread_flag(child, TIF_SYSCALL_EMU); | |
757 | #endif | |
758 | ||
5b88abbf RM |
759 | if (is_singleblock(request)) { |
760 | if (unlikely(!arch_has_block_step())) | |
761 | return -EIO; | |
762 | user_enable_block_step(child); | |
763 | } else if (is_singlestep(request) || is_sysemu_singlestep(request)) { | |
36df29d7 RM |
764 | if (unlikely(!arch_has_single_step())) |
765 | return -EIO; | |
766 | user_enable_single_step(child); | |
3a709703 | 767 | } else { |
36df29d7 | 768 | user_disable_single_step(child); |
3a709703 | 769 | } |
36df29d7 RM |
770 | |
771 | child->exit_code = data; | |
0666fb51 | 772 | wake_up_state(child, __TASK_TRACED); |
36df29d7 RM |
773 | |
774 | return 0; | |
775 | } | |
776 | ||
2225a122 SS |
777 | #ifdef CONFIG_HAVE_ARCH_TRACEHOOK |
778 | ||
779 | static const struct user_regset * | |
780 | find_regset(const struct user_regset_view *view, unsigned int type) | |
781 | { | |
782 | const struct user_regset *regset; | |
783 | int n; | |
784 | ||
785 | for (n = 0; n < view->n; ++n) { | |
786 | regset = view->regsets + n; | |
787 | if (regset->core_note_type == type) | |
788 | return regset; | |
789 | } | |
790 | ||
791 | return NULL; | |
792 | } | |
793 | ||
794 | static int ptrace_regset(struct task_struct *task, int req, unsigned int type, | |
795 | struct iovec *kiov) | |
796 | { | |
797 | const struct user_regset_view *view = task_user_regset_view(task); | |
798 | const struct user_regset *regset = find_regset(view, type); | |
799 | int regset_no; | |
800 | ||
801 | if (!regset || (kiov->iov_len % regset->size) != 0) | |
c6a0dd7e | 802 | return -EINVAL; |
2225a122 SS |
803 | |
804 | regset_no = regset - view->regsets; | |
805 | kiov->iov_len = min(kiov->iov_len, | |
806 | (__kernel_size_t) (regset->n * regset->size)); | |
807 | ||
808 | if (req == PTRACE_GETREGSET) | |
809 | return copy_regset_to_user(task, view, regset_no, 0, | |
810 | kiov->iov_len, kiov->iov_base); | |
811 | else | |
812 | return copy_regset_from_user(task, view, regset_no, 0, | |
813 | kiov->iov_len, kiov->iov_base); | |
814 | } | |
815 | ||
e8440c14 JS |
816 | /* |
817 | * This is declared in linux/regset.h and defined in machine-dependent | |
818 | * code. We put the export here, near the primary machine-neutral use, | |
819 | * to ensure no machine forgets it. | |
820 | */ | |
821 | EXPORT_SYMBOL_GPL(task_user_regset_view); | |
2225a122 SS |
822 | #endif |
823 | ||
1da177e4 | 824 | int ptrace_request(struct task_struct *child, long request, |
4abf9869 | 825 | unsigned long addr, unsigned long data) |
1da177e4 | 826 | { |
fca26f26 | 827 | bool seized = child->ptrace & PT_SEIZED; |
1da177e4 | 828 | int ret = -EIO; |
544b2c91 | 829 | siginfo_t siginfo, *si; |
9fed81dc NK |
830 | void __user *datavp = (void __user *) data; |
831 | unsigned long __user *datalp = datavp; | |
fca26f26 | 832 | unsigned long flags; |
1da177e4 LT |
833 | |
834 | switch (request) { | |
16c3e389 RM |
835 | case PTRACE_PEEKTEXT: |
836 | case PTRACE_PEEKDATA: | |
837 | return generic_ptrace_peekdata(child, addr, data); | |
838 | case PTRACE_POKETEXT: | |
839 | case PTRACE_POKEDATA: | |
840 | return generic_ptrace_pokedata(child, addr, data); | |
841 | ||
1da177e4 LT |
842 | #ifdef PTRACE_OLDSETOPTIONS |
843 | case PTRACE_OLDSETOPTIONS: | |
844 | #endif | |
845 | case PTRACE_SETOPTIONS: | |
846 | ret = ptrace_setoptions(child, data); | |
847 | break; | |
848 | case PTRACE_GETEVENTMSG: | |
9fed81dc | 849 | ret = put_user(child->ptrace_message, datalp); |
1da177e4 | 850 | break; |
e16b2781 | 851 | |
84c751bd AV |
852 | case PTRACE_PEEKSIGINFO: |
853 | ret = ptrace_peek_siginfo(child, addr, data); | |
854 | break; | |
855 | ||
1da177e4 | 856 | case PTRACE_GETSIGINFO: |
e16b2781 RM |
857 | ret = ptrace_getsiginfo(child, &siginfo); |
858 | if (!ret) | |
9fed81dc | 859 | ret = copy_siginfo_to_user(datavp, &siginfo); |
1da177e4 | 860 | break; |
e16b2781 | 861 | |
1da177e4 | 862 | case PTRACE_SETSIGINFO: |
9fed81dc | 863 | if (copy_from_user(&siginfo, datavp, sizeof siginfo)) |
e16b2781 RM |
864 | ret = -EFAULT; |
865 | else | |
866 | ret = ptrace_setsiginfo(child, &siginfo); | |
1da177e4 | 867 | break; |
e16b2781 | 868 | |
fca26f26 TH |
869 | case PTRACE_INTERRUPT: |
870 | /* | |
871 | * Stop tracee without any side-effect on signal or job | |
872 | * control. At least one trap is guaranteed to happen | |
873 | * after this request. If @child is already trapped, the | |
874 | * current trap is not disturbed and another trap will | |
875 | * happen after the current trap is ended with PTRACE_CONT. | |
876 | * | |
877 | * The actual trap might not be PTRACE_EVENT_STOP trap but | |
878 | * the pending condition is cleared regardless. | |
879 | */ | |
880 | if (unlikely(!seized || !lock_task_sighand(child, &flags))) | |
881 | break; | |
882 | ||
544b2c91 TH |
883 | /* |
884 | * INTERRUPT doesn't disturb existing trap sans one | |
885 | * exception. If ptracer issued LISTEN for the current | |
886 | * STOP, this INTERRUPT should clear LISTEN and re-trap | |
887 | * tracee into STOP. | |
888 | */ | |
fca26f26 | 889 | if (likely(task_set_jobctl_pending(child, JOBCTL_TRAP_STOP))) |
910ffdb1 | 890 | ptrace_signal_wake_up(child, child->jobctl & JOBCTL_LISTENING); |
544b2c91 TH |
891 | |
892 | unlock_task_sighand(child, &flags); | |
893 | ret = 0; | |
894 | break; | |
895 | ||
896 | case PTRACE_LISTEN: | |
897 | /* | |
898 | * Listen for events. Tracee must be in STOP. It's not | |
899 | * resumed per-se but is not considered to be in TRACED by | |
900 | * wait(2) or ptrace(2). If an async event (e.g. group | |
901 | * stop state change) happens, tracee will enter STOP trap | |
902 | * again. Alternatively, ptracer can issue INTERRUPT to | |
903 | * finish listening and re-trap tracee into STOP. | |
904 | */ | |
905 | if (unlikely(!seized || !lock_task_sighand(child, &flags))) | |
906 | break; | |
907 | ||
908 | si = child->last_siginfo; | |
f9d81f61 ON |
909 | if (likely(si && (si->si_code >> 8) == PTRACE_EVENT_STOP)) { |
910 | child->jobctl |= JOBCTL_LISTENING; | |
911 | /* | |
912 | * If NOTIFY is set, it means event happened between | |
913 | * start of this trap and now. Trigger re-trap. | |
914 | */ | |
915 | if (child->jobctl & JOBCTL_TRAP_NOTIFY) | |
910ffdb1 | 916 | ptrace_signal_wake_up(child, true); |
f9d81f61 ON |
917 | ret = 0; |
918 | } | |
fca26f26 | 919 | unlock_task_sighand(child, &flags); |
fca26f26 TH |
920 | break; |
921 | ||
1bcf5482 AD |
922 | case PTRACE_DETACH: /* detach a process that was attached. */ |
923 | ret = ptrace_detach(child, data); | |
924 | break; | |
36df29d7 | 925 | |
9c1a1259 MF |
926 | #ifdef CONFIG_BINFMT_ELF_FDPIC |
927 | case PTRACE_GETFDPIC: { | |
e0129ef9 | 928 | struct mm_struct *mm = get_task_mm(child); |
9c1a1259 MF |
929 | unsigned long tmp = 0; |
930 | ||
e0129ef9 ON |
931 | ret = -ESRCH; |
932 | if (!mm) | |
933 | break; | |
934 | ||
9c1a1259 MF |
935 | switch (addr) { |
936 | case PTRACE_GETFDPIC_EXEC: | |
e0129ef9 | 937 | tmp = mm->context.exec_fdpic_loadmap; |
9c1a1259 MF |
938 | break; |
939 | case PTRACE_GETFDPIC_INTERP: | |
e0129ef9 | 940 | tmp = mm->context.interp_fdpic_loadmap; |
9c1a1259 MF |
941 | break; |
942 | default: | |
943 | break; | |
944 | } | |
e0129ef9 | 945 | mmput(mm); |
9c1a1259 | 946 | |
9fed81dc | 947 | ret = put_user(tmp, datalp); |
9c1a1259 MF |
948 | break; |
949 | } | |
950 | #endif | |
951 | ||
36df29d7 RM |
952 | #ifdef PTRACE_SINGLESTEP |
953 | case PTRACE_SINGLESTEP: | |
954 | #endif | |
5b88abbf RM |
955 | #ifdef PTRACE_SINGLEBLOCK |
956 | case PTRACE_SINGLEBLOCK: | |
957 | #endif | |
36df29d7 RM |
958 | #ifdef PTRACE_SYSEMU |
959 | case PTRACE_SYSEMU: | |
960 | case PTRACE_SYSEMU_SINGLESTEP: | |
961 | #endif | |
962 | case PTRACE_SYSCALL: | |
963 | case PTRACE_CONT: | |
964 | return ptrace_resume(child, request, data); | |
965 | ||
966 | case PTRACE_KILL: | |
967 | if (child->exit_state) /* already dead */ | |
968 | return 0; | |
969 | return ptrace_resume(child, request, SIGKILL); | |
970 | ||
2225a122 SS |
971 | #ifdef CONFIG_HAVE_ARCH_TRACEHOOK |
972 | case PTRACE_GETREGSET: | |
973 | case PTRACE_SETREGSET: | |
974 | { | |
975 | struct iovec kiov; | |
9fed81dc | 976 | struct iovec __user *uiov = datavp; |
2225a122 SS |
977 | |
978 | if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov))) | |
979 | return -EFAULT; | |
980 | ||
981 | if (__get_user(kiov.iov_base, &uiov->iov_base) || | |
982 | __get_user(kiov.iov_len, &uiov->iov_len)) | |
983 | return -EFAULT; | |
984 | ||
985 | ret = ptrace_regset(child, request, addr, &kiov); | |
986 | if (!ret) | |
987 | ret = __put_user(kiov.iov_len, &uiov->iov_len); | |
988 | break; | |
989 | } | |
990 | #endif | |
1da177e4 LT |
991 | default: |
992 | break; | |
993 | } | |
994 | ||
995 | return ret; | |
996 | } | |
481bed45 | 997 | |
8053bdd5 | 998 | static struct task_struct *ptrace_get_task_struct(pid_t pid) |
6b9c7ed8 CH |
999 | { |
1000 | struct task_struct *child; | |
481bed45 | 1001 | |
8053bdd5 | 1002 | rcu_read_lock(); |
228ebcbe | 1003 | child = find_task_by_vpid(pid); |
481bed45 CH |
1004 | if (child) |
1005 | get_task_struct(child); | |
8053bdd5 | 1006 | rcu_read_unlock(); |
f400e198 | 1007 | |
481bed45 | 1008 | if (!child) |
6b9c7ed8 CH |
1009 | return ERR_PTR(-ESRCH); |
1010 | return child; | |
481bed45 CH |
1011 | } |
1012 | ||
0ac15559 CH |
1013 | #ifndef arch_ptrace_attach |
1014 | #define arch_ptrace_attach(child) do { } while (0) | |
1015 | #endif | |
1016 | ||
4abf9869 NK |
1017 | SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr, |
1018 | unsigned long, data) | |
481bed45 CH |
1019 | { |
1020 | struct task_struct *child; | |
1021 | long ret; | |
1022 | ||
6b9c7ed8 CH |
1023 | if (request == PTRACE_TRACEME) { |
1024 | ret = ptrace_traceme(); | |
6ea6dd93 HS |
1025 | if (!ret) |
1026 | arch_ptrace_attach(current); | |
481bed45 | 1027 | goto out; |
6b9c7ed8 CH |
1028 | } |
1029 | ||
1030 | child = ptrace_get_task_struct(pid); | |
1031 | if (IS_ERR(child)) { | |
1032 | ret = PTR_ERR(child); | |
1033 | goto out; | |
1034 | } | |
481bed45 | 1035 | |
3544d72a | 1036 | if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) { |
aa9147c9 | 1037 | ret = ptrace_attach(child, request, addr, data); |
0ac15559 CH |
1038 | /* |
1039 | * Some architectures need to do book-keeping after | |
1040 | * a ptrace attach. | |
1041 | */ | |
1042 | if (!ret) | |
1043 | arch_ptrace_attach(child); | |
005f18df | 1044 | goto out_put_task_struct; |
481bed45 CH |
1045 | } |
1046 | ||
fca26f26 TH |
1047 | ret = ptrace_check_attach(child, request == PTRACE_KILL || |
1048 | request == PTRACE_INTERRUPT); | |
481bed45 CH |
1049 | if (ret < 0) |
1050 | goto out_put_task_struct; | |
1051 | ||
1052 | ret = arch_ptrace(child, request, addr, data); | |
9899d11f ON |
1053 | if (ret || request != PTRACE_DETACH) |
1054 | ptrace_unfreeze_traced(child); | |
481bed45 CH |
1055 | |
1056 | out_put_task_struct: | |
1057 | put_task_struct(child); | |
1058 | out: | |
481bed45 CH |
1059 | return ret; |
1060 | } | |
76647323 | 1061 | |
4abf9869 NK |
1062 | int generic_ptrace_peekdata(struct task_struct *tsk, unsigned long addr, |
1063 | unsigned long data) | |
76647323 AD |
1064 | { |
1065 | unsigned long tmp; | |
1066 | int copied; | |
1067 | ||
1068 | copied = access_process_vm(tsk, addr, &tmp, sizeof(tmp), 0); | |
1069 | if (copied != sizeof(tmp)) | |
1070 | return -EIO; | |
1071 | return put_user(tmp, (unsigned long __user *)data); | |
1072 | } | |
f284ce72 | 1073 | |
4abf9869 NK |
1074 | int generic_ptrace_pokedata(struct task_struct *tsk, unsigned long addr, |
1075 | unsigned long data) | |
f284ce72 AD |
1076 | { |
1077 | int copied; | |
1078 | ||
1079 | copied = access_process_vm(tsk, addr, &data, sizeof(data), 1); | |
1080 | return (copied == sizeof(data)) ? 0 : -EIO; | |
1081 | } | |
032d82d9 | 1082 | |
96b8936a | 1083 | #if defined CONFIG_COMPAT |
032d82d9 RM |
1084 | #include <linux/compat.h> |
1085 | ||
1086 | int compat_ptrace_request(struct task_struct *child, compat_long_t request, | |
1087 | compat_ulong_t addr, compat_ulong_t data) | |
1088 | { | |
1089 | compat_ulong_t __user *datap = compat_ptr(data); | |
1090 | compat_ulong_t word; | |
e16b2781 | 1091 | siginfo_t siginfo; |
032d82d9 RM |
1092 | int ret; |
1093 | ||
1094 | switch (request) { | |
1095 | case PTRACE_PEEKTEXT: | |
1096 | case PTRACE_PEEKDATA: | |
1097 | ret = access_process_vm(child, addr, &word, sizeof(word), 0); | |
1098 | if (ret != sizeof(word)) | |
1099 | ret = -EIO; | |
1100 | else | |
1101 | ret = put_user(word, datap); | |
1102 | break; | |
1103 | ||
1104 | case PTRACE_POKETEXT: | |
1105 | case PTRACE_POKEDATA: | |
1106 | ret = access_process_vm(child, addr, &data, sizeof(data), 1); | |
1107 | ret = (ret != sizeof(data) ? -EIO : 0); | |
1108 | break; | |
1109 | ||
1110 | case PTRACE_GETEVENTMSG: | |
1111 | ret = put_user((compat_ulong_t) child->ptrace_message, datap); | |
1112 | break; | |
1113 | ||
e16b2781 RM |
1114 | case PTRACE_GETSIGINFO: |
1115 | ret = ptrace_getsiginfo(child, &siginfo); | |
1116 | if (!ret) | |
1117 | ret = copy_siginfo_to_user32( | |
1118 | (struct compat_siginfo __user *) datap, | |
1119 | &siginfo); | |
1120 | break; | |
1121 | ||
1122 | case PTRACE_SETSIGINFO: | |
1123 | memset(&siginfo, 0, sizeof siginfo); | |
1124 | if (copy_siginfo_from_user32( | |
1125 | &siginfo, (struct compat_siginfo __user *) datap)) | |
1126 | ret = -EFAULT; | |
1127 | else | |
1128 | ret = ptrace_setsiginfo(child, &siginfo); | |
1129 | break; | |
2225a122 SS |
1130 | #ifdef CONFIG_HAVE_ARCH_TRACEHOOK |
1131 | case PTRACE_GETREGSET: | |
1132 | case PTRACE_SETREGSET: | |
1133 | { | |
1134 | struct iovec kiov; | |
1135 | struct compat_iovec __user *uiov = | |
1136 | (struct compat_iovec __user *) datap; | |
1137 | compat_uptr_t ptr; | |
1138 | compat_size_t len; | |
1139 | ||
1140 | if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov))) | |
1141 | return -EFAULT; | |
1142 | ||
1143 | if (__get_user(ptr, &uiov->iov_base) || | |
1144 | __get_user(len, &uiov->iov_len)) | |
1145 | return -EFAULT; | |
1146 | ||
1147 | kiov.iov_base = compat_ptr(ptr); | |
1148 | kiov.iov_len = len; | |
1149 | ||
1150 | ret = ptrace_regset(child, request, addr, &kiov); | |
1151 | if (!ret) | |
1152 | ret = __put_user(kiov.iov_len, &uiov->iov_len); | |
1153 | break; | |
1154 | } | |
1155 | #endif | |
e16b2781 | 1156 | |
032d82d9 RM |
1157 | default: |
1158 | ret = ptrace_request(child, request, addr, data); | |
1159 | } | |
1160 | ||
1161 | return ret; | |
1162 | } | |
c269f196 | 1163 | |
c269f196 RM |
1164 | asmlinkage long compat_sys_ptrace(compat_long_t request, compat_long_t pid, |
1165 | compat_long_t addr, compat_long_t data) | |
1166 | { | |
1167 | struct task_struct *child; | |
1168 | long ret; | |
1169 | ||
c269f196 RM |
1170 | if (request == PTRACE_TRACEME) { |
1171 | ret = ptrace_traceme(); | |
1172 | goto out; | |
1173 | } | |
1174 | ||
1175 | child = ptrace_get_task_struct(pid); | |
1176 | if (IS_ERR(child)) { | |
1177 | ret = PTR_ERR(child); | |
1178 | goto out; | |
1179 | } | |
1180 | ||
3544d72a | 1181 | if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) { |
aa9147c9 | 1182 | ret = ptrace_attach(child, request, addr, data); |
c269f196 RM |
1183 | /* |
1184 | * Some architectures need to do book-keeping after | |
1185 | * a ptrace attach. | |
1186 | */ | |
1187 | if (!ret) | |
1188 | arch_ptrace_attach(child); | |
1189 | goto out_put_task_struct; | |
1190 | } | |
1191 | ||
fca26f26 TH |
1192 | ret = ptrace_check_attach(child, request == PTRACE_KILL || |
1193 | request == PTRACE_INTERRUPT); | |
9899d11f | 1194 | if (!ret) { |
c269f196 | 1195 | ret = compat_arch_ptrace(child, request, addr, data); |
9899d11f ON |
1196 | if (ret || request != PTRACE_DETACH) |
1197 | ptrace_unfreeze_traced(child); | |
1198 | } | |
c269f196 RM |
1199 | |
1200 | out_put_task_struct: | |
1201 | put_task_struct(child); | |
1202 | out: | |
c269f196 RM |
1203 | return ret; |
1204 | } | |
96b8936a | 1205 | #endif /* CONFIG_COMPAT */ |
bf26c018 FW |
1206 | |
1207 | #ifdef CONFIG_HAVE_HW_BREAKPOINT | |
1208 | int ptrace_get_breakpoints(struct task_struct *tsk) | |
1209 | { | |
1210 | if (atomic_inc_not_zero(&tsk->ptrace_bp_refcnt)) | |
1211 | return 0; | |
1212 | ||
1213 | return -1; | |
1214 | } | |
1215 | ||
1216 | void ptrace_put_breakpoints(struct task_struct *tsk) | |
1217 | { | |
1218 | if (atomic_dec_and_test(&tsk->ptrace_bp_refcnt)) | |
1219 | flush_ptrace_hw_breakpoint(tsk); | |
1220 | } | |
1221 | #endif /* CONFIG_HAVE_HW_BREAKPOINT */ |