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> |
1da177e4 LT |
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> | |
17 | #include <linux/smp_lock.h> | |
18 | #include <linux/ptrace.h> | |
19 | #include <linux/security.h> | |
7ed20e1a | 20 | #include <linux/signal.h> |
a5cb013d | 21 | #include <linux/audit.h> |
b488893a | 22 | #include <linux/pid_namespace.h> |
f17d30a8 | 23 | #include <linux/syscalls.h> |
1da177e4 LT |
24 | |
25 | #include <asm/pgtable.h> | |
26 | #include <asm/uaccess.h> | |
27 | ||
bf53de90 MM |
28 | |
29 | /* | |
30 | * Initialize a new task whose father had been ptraced. | |
31 | * | |
32 | * Called from copy_process(). | |
33 | */ | |
34 | void ptrace_fork(struct task_struct *child, unsigned long clone_flags) | |
35 | { | |
36 | arch_ptrace_fork(child, clone_flags); | |
37 | } | |
38 | ||
1da177e4 LT |
39 | /* |
40 | * ptrace a task: make the debugger its new parent and | |
41 | * move it to the ptrace list. | |
42 | * | |
43 | * Must be called with the tasklist lock write-held. | |
44 | */ | |
36c8b586 | 45 | void __ptrace_link(struct task_struct *child, struct task_struct *new_parent) |
1da177e4 | 46 | { |
f470021a RM |
47 | BUG_ON(!list_empty(&child->ptrace_entry)); |
48 | list_add(&child->ptrace_entry, &new_parent->ptraced); | |
1da177e4 | 49 | child->parent = new_parent; |
1da177e4 LT |
50 | } |
51 | ||
52 | /* | |
53 | * Turn a tracing stop into a normal stop now, since with no tracer there | |
54 | * would be no way to wake it up with SIGCONT or SIGKILL. If there was a | |
55 | * signal sent that would resume the child, but didn't because it was in | |
56 | * TASK_TRACED, resume it now. | |
57 | * Requires that irqs be disabled. | |
58 | */ | |
b747c8c1 | 59 | static void ptrace_untrace(struct task_struct *child) |
1da177e4 LT |
60 | { |
61 | spin_lock(&child->sighand->siglock); | |
6618a3e2 | 62 | if (task_is_traced(child)) { |
1da177e4 | 63 | if (child->signal->flags & SIGNAL_STOP_STOPPED) { |
d9ae90ac | 64 | __set_task_state(child, TASK_STOPPED); |
1da177e4 LT |
65 | } else { |
66 | signal_wake_up(child, 1); | |
67 | } | |
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 | */ | |
36c8b586 | 78 | void __ptrace_unlink(struct task_struct *child) |
1da177e4 | 79 | { |
5ecfbae0 ON |
80 | BUG_ON(!child->ptrace); |
81 | ||
1da177e4 | 82 | child->ptrace = 0; |
f470021a RM |
83 | child->parent = child->real_parent; |
84 | list_del_init(&child->ptrace_entry); | |
1da177e4 | 85 | |
bf53de90 | 86 | arch_ptrace_untrace(child); |
6618a3e2 | 87 | if (task_is_traced(child)) |
e57a5059 | 88 | ptrace_untrace(child); |
1da177e4 LT |
89 | } |
90 | ||
91 | /* | |
92 | * Check that we have indeed attached to the thing.. | |
93 | */ | |
94 | int ptrace_check_attach(struct task_struct *child, int kill) | |
95 | { | |
96 | int ret = -ESRCH; | |
97 | ||
98 | /* | |
99 | * We take the read lock around doing both checks to close a | |
100 | * possible race where someone else was tracing our child and | |
101 | * detached between these two checks. After this locked check, | |
102 | * we are sure that this is our traced child and that can only | |
103 | * be changed by us so it's not changing right after this. | |
104 | */ | |
105 | read_lock(&tasklist_lock); | |
c0c0b649 | 106 | if ((child->ptrace & PT_PTRACED) && child->parent == current) { |
1da177e4 | 107 | ret = 0; |
c0c0b649 ON |
108 | /* |
109 | * child->sighand can't be NULL, release_task() | |
110 | * does ptrace_unlink() before __exit_signal(). | |
111 | */ | |
1da177e4 | 112 | spin_lock_irq(&child->sighand->siglock); |
d9ae90ac | 113 | if (task_is_stopped(child)) |
1da177e4 | 114 | child->state = TASK_TRACED; |
d9ae90ac | 115 | else if (!task_is_traced(child) && !kill) |
1da177e4 | 116 | ret = -ESRCH; |
1da177e4 LT |
117 | spin_unlock_irq(&child->sighand->siglock); |
118 | } | |
119 | read_unlock(&tasklist_lock); | |
120 | ||
d9ae90ac | 121 | if (!ret && !kill) |
85ba2d86 | 122 | ret = wait_task_inactive(child, TASK_TRACED) ? 0 : -ESRCH; |
1da177e4 LT |
123 | |
124 | /* All systems go.. */ | |
125 | return ret; | |
126 | } | |
127 | ||
006ebb40 | 128 | int __ptrace_may_access(struct task_struct *task, unsigned int mode) |
ab8d11be | 129 | { |
df26c40e EB |
130 | /* May we inspect the given task? |
131 | * This check is used both for attaching with ptrace | |
132 | * and for allowing access to sensitive information in /proc. | |
133 | * | |
134 | * ptrace_attach denies several cases that /proc allows | |
135 | * because setting up the necessary parent/child relationship | |
136 | * or halting the specified task is impossible. | |
137 | */ | |
138 | int dumpable = 0; | |
139 | /* Don't let security modules deny introspection */ | |
140 | if (task == current) | |
141 | return 0; | |
ab8d11be MS |
142 | if (((current->uid != task->euid) || |
143 | (current->uid != task->suid) || | |
144 | (current->uid != task->uid) || | |
145 | (current->gid != task->egid) || | |
146 | (current->gid != task->sgid) || | |
147 | (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE)) | |
148 | return -EPERM; | |
149 | smp_rmb(); | |
df26c40e | 150 | if (task->mm) |
6c5d5238 | 151 | dumpable = get_dumpable(task->mm); |
df26c40e | 152 | if (!dumpable && !capable(CAP_SYS_PTRACE)) |
ab8d11be MS |
153 | return -EPERM; |
154 | ||
5cd9c58f | 155 | return security_ptrace_may_access(task, mode); |
ab8d11be MS |
156 | } |
157 | ||
006ebb40 | 158 | bool ptrace_may_access(struct task_struct *task, unsigned int mode) |
ab8d11be MS |
159 | { |
160 | int err; | |
161 | task_lock(task); | |
006ebb40 | 162 | err = __ptrace_may_access(task, mode); |
ab8d11be | 163 | task_unlock(task); |
006ebb40 | 164 | return (!err ? true : false); |
ab8d11be MS |
165 | } |
166 | ||
1da177e4 LT |
167 | int ptrace_attach(struct task_struct *task) |
168 | { | |
169 | int retval; | |
6175ecfe | 170 | unsigned long flags; |
f5b40e36 | 171 | |
a5cb013d AV |
172 | audit_ptrace(task); |
173 | ||
1da177e4 | 174 | retval = -EPERM; |
bac0abd6 | 175 | if (same_thread_group(task, current)) |
f5b40e36 LT |
176 | goto out; |
177 | ||
f358166a LT |
178 | repeat: |
179 | /* | |
180 | * Nasty, nasty. | |
181 | * | |
182 | * We want to hold both the task-lock and the | |
183 | * tasklist_lock for writing at the same time. | |
184 | * But that's against the rules (tasklist_lock | |
185 | * is taken for reading by interrupts on other | |
186 | * cpu's that may have task_lock). | |
187 | */ | |
f5b40e36 | 188 | task_lock(task); |
6175ecfe | 189 | if (!write_trylock_irqsave(&tasklist_lock, flags)) { |
f358166a LT |
190 | task_unlock(task); |
191 | do { | |
192 | cpu_relax(); | |
193 | } while (!write_can_lock(&tasklist_lock)); | |
194 | goto repeat; | |
195 | } | |
f5b40e36 | 196 | |
df26c40e EB |
197 | if (!task->mm) |
198 | goto bad; | |
1da177e4 LT |
199 | /* the same process cannot be attached many times */ |
200 | if (task->ptrace & PT_PTRACED) | |
201 | goto bad; | |
006ebb40 | 202 | retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH); |
1da177e4 LT |
203 | if (retval) |
204 | goto bad; | |
205 | ||
206 | /* Go */ | |
6b39c7bf | 207 | task->ptrace |= PT_PTRACED; |
1da177e4 LT |
208 | if (capable(CAP_SYS_PTRACE)) |
209 | task->ptrace |= PT_PTRACE_CAP; | |
1da177e4 | 210 | |
1da177e4 | 211 | __ptrace_link(task, current); |
1da177e4 | 212 | |
33e9fc7d | 213 | send_sig_info(SIGSTOP, SEND_SIG_FORCED, task); |
1da177e4 | 214 | bad: |
6175ecfe | 215 | write_unlock_irqrestore(&tasklist_lock, flags); |
1da177e4 | 216 | task_unlock(task); |
f5b40e36 | 217 | out: |
1da177e4 LT |
218 | return retval; |
219 | } | |
220 | ||
d5f70c00 | 221 | static inline void __ptrace_detach(struct task_struct *child, unsigned int data) |
5ecfbae0 ON |
222 | { |
223 | child->exit_code = data; | |
224 | /* .. re-parent .. */ | |
225 | __ptrace_unlink(child); | |
226 | /* .. and wake it up. */ | |
227 | if (child->exit_state != EXIT_ZOMBIE) | |
228 | wake_up_process(child); | |
229 | } | |
230 | ||
1da177e4 LT |
231 | int ptrace_detach(struct task_struct *child, unsigned int data) |
232 | { | |
7ed20e1a | 233 | if (!valid_signal(data)) |
5ecfbae0 | 234 | return -EIO; |
1da177e4 LT |
235 | |
236 | /* Architecture-specific hardware disable .. */ | |
237 | ptrace_disable(child); | |
7d941432 | 238 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
1da177e4 | 239 | |
1da177e4 | 240 | write_lock_irq(&tasklist_lock); |
d5f70c00 | 241 | /* protect against de_thread()->release_task() */ |
5ecfbae0 ON |
242 | if (child->ptrace) |
243 | __ptrace_detach(child, data); | |
1da177e4 LT |
244 | write_unlock_irq(&tasklist_lock); |
245 | ||
246 | return 0; | |
247 | } | |
248 | ||
1da177e4 LT |
249 | int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len) |
250 | { | |
251 | int copied = 0; | |
252 | ||
253 | while (len > 0) { | |
254 | char buf[128]; | |
255 | int this_len, retval; | |
256 | ||
257 | this_len = (len > sizeof(buf)) ? sizeof(buf) : len; | |
258 | retval = access_process_vm(tsk, src, buf, this_len, 0); | |
259 | if (!retval) { | |
260 | if (copied) | |
261 | break; | |
262 | return -EIO; | |
263 | } | |
264 | if (copy_to_user(dst, buf, retval)) | |
265 | return -EFAULT; | |
266 | copied += retval; | |
267 | src += retval; | |
268 | dst += retval; | |
269 | len -= retval; | |
270 | } | |
271 | return copied; | |
272 | } | |
273 | ||
274 | int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len) | |
275 | { | |
276 | int copied = 0; | |
277 | ||
278 | while (len > 0) { | |
279 | char buf[128]; | |
280 | int this_len, retval; | |
281 | ||
282 | this_len = (len > sizeof(buf)) ? sizeof(buf) : len; | |
283 | if (copy_from_user(buf, src, this_len)) | |
284 | return -EFAULT; | |
285 | retval = access_process_vm(tsk, dst, buf, this_len, 1); | |
286 | if (!retval) { | |
287 | if (copied) | |
288 | break; | |
289 | return -EIO; | |
290 | } | |
291 | copied += retval; | |
292 | src += retval; | |
293 | dst += retval; | |
294 | len -= retval; | |
295 | } | |
296 | return copied; | |
297 | } | |
298 | ||
299 | static int ptrace_setoptions(struct task_struct *child, long data) | |
300 | { | |
301 | child->ptrace &= ~PT_TRACE_MASK; | |
302 | ||
303 | if (data & PTRACE_O_TRACESYSGOOD) | |
304 | child->ptrace |= PT_TRACESYSGOOD; | |
305 | ||
306 | if (data & PTRACE_O_TRACEFORK) | |
307 | child->ptrace |= PT_TRACE_FORK; | |
308 | ||
309 | if (data & PTRACE_O_TRACEVFORK) | |
310 | child->ptrace |= PT_TRACE_VFORK; | |
311 | ||
312 | if (data & PTRACE_O_TRACECLONE) | |
313 | child->ptrace |= PT_TRACE_CLONE; | |
314 | ||
315 | if (data & PTRACE_O_TRACEEXEC) | |
316 | child->ptrace |= PT_TRACE_EXEC; | |
317 | ||
318 | if (data & PTRACE_O_TRACEVFORKDONE) | |
319 | child->ptrace |= PT_TRACE_VFORK_DONE; | |
320 | ||
321 | if (data & PTRACE_O_TRACEEXIT) | |
322 | child->ptrace |= PT_TRACE_EXIT; | |
323 | ||
324 | return (data & ~PTRACE_O_MASK) ? -EINVAL : 0; | |
325 | } | |
326 | ||
e16b2781 | 327 | static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info) |
1da177e4 | 328 | { |
1da177e4 LT |
329 | int error = -ESRCH; |
330 | ||
331 | read_lock(&tasklist_lock); | |
332 | if (likely(child->sighand != NULL)) { | |
333 | error = -EINVAL; | |
334 | spin_lock_irq(&child->sighand->siglock); | |
335 | if (likely(child->last_siginfo != NULL)) { | |
e16b2781 | 336 | *info = *child->last_siginfo; |
1da177e4 LT |
337 | error = 0; |
338 | } | |
339 | spin_unlock_irq(&child->sighand->siglock); | |
340 | } | |
341 | read_unlock(&tasklist_lock); | |
1da177e4 LT |
342 | return error; |
343 | } | |
344 | ||
e16b2781 | 345 | static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info) |
1da177e4 | 346 | { |
1da177e4 LT |
347 | int error = -ESRCH; |
348 | ||
1da177e4 LT |
349 | read_lock(&tasklist_lock); |
350 | if (likely(child->sighand != NULL)) { | |
351 | error = -EINVAL; | |
352 | spin_lock_irq(&child->sighand->siglock); | |
353 | if (likely(child->last_siginfo != NULL)) { | |
e16b2781 | 354 | *child->last_siginfo = *info; |
1da177e4 LT |
355 | error = 0; |
356 | } | |
357 | spin_unlock_irq(&child->sighand->siglock); | |
358 | } | |
359 | read_unlock(&tasklist_lock); | |
360 | return error; | |
361 | } | |
362 | ||
36df29d7 RM |
363 | |
364 | #ifdef PTRACE_SINGLESTEP | |
365 | #define is_singlestep(request) ((request) == PTRACE_SINGLESTEP) | |
366 | #else | |
367 | #define is_singlestep(request) 0 | |
368 | #endif | |
369 | ||
5b88abbf RM |
370 | #ifdef PTRACE_SINGLEBLOCK |
371 | #define is_singleblock(request) ((request) == PTRACE_SINGLEBLOCK) | |
372 | #else | |
373 | #define is_singleblock(request) 0 | |
374 | #endif | |
375 | ||
36df29d7 RM |
376 | #ifdef PTRACE_SYSEMU |
377 | #define is_sysemu_singlestep(request) ((request) == PTRACE_SYSEMU_SINGLESTEP) | |
378 | #else | |
379 | #define is_sysemu_singlestep(request) 0 | |
380 | #endif | |
381 | ||
382 | static int ptrace_resume(struct task_struct *child, long request, long data) | |
383 | { | |
384 | if (!valid_signal(data)) | |
385 | return -EIO; | |
386 | ||
387 | if (request == PTRACE_SYSCALL) | |
388 | set_tsk_thread_flag(child, TIF_SYSCALL_TRACE); | |
389 | else | |
390 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); | |
391 | ||
392 | #ifdef TIF_SYSCALL_EMU | |
393 | if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP) | |
394 | set_tsk_thread_flag(child, TIF_SYSCALL_EMU); | |
395 | else | |
396 | clear_tsk_thread_flag(child, TIF_SYSCALL_EMU); | |
397 | #endif | |
398 | ||
5b88abbf RM |
399 | if (is_singleblock(request)) { |
400 | if (unlikely(!arch_has_block_step())) | |
401 | return -EIO; | |
402 | user_enable_block_step(child); | |
403 | } else if (is_singlestep(request) || is_sysemu_singlestep(request)) { | |
36df29d7 RM |
404 | if (unlikely(!arch_has_single_step())) |
405 | return -EIO; | |
406 | user_enable_single_step(child); | |
407 | } | |
408 | else | |
409 | user_disable_single_step(child); | |
410 | ||
411 | child->exit_code = data; | |
412 | wake_up_process(child); | |
413 | ||
414 | return 0; | |
415 | } | |
416 | ||
1da177e4 LT |
417 | int ptrace_request(struct task_struct *child, long request, |
418 | long addr, long data) | |
419 | { | |
420 | int ret = -EIO; | |
e16b2781 | 421 | siginfo_t siginfo; |
1da177e4 LT |
422 | |
423 | switch (request) { | |
16c3e389 RM |
424 | case PTRACE_PEEKTEXT: |
425 | case PTRACE_PEEKDATA: | |
426 | return generic_ptrace_peekdata(child, addr, data); | |
427 | case PTRACE_POKETEXT: | |
428 | case PTRACE_POKEDATA: | |
429 | return generic_ptrace_pokedata(child, addr, data); | |
430 | ||
1da177e4 LT |
431 | #ifdef PTRACE_OLDSETOPTIONS |
432 | case PTRACE_OLDSETOPTIONS: | |
433 | #endif | |
434 | case PTRACE_SETOPTIONS: | |
435 | ret = ptrace_setoptions(child, data); | |
436 | break; | |
437 | case PTRACE_GETEVENTMSG: | |
438 | ret = put_user(child->ptrace_message, (unsigned long __user *) data); | |
439 | break; | |
e16b2781 | 440 | |
1da177e4 | 441 | case PTRACE_GETSIGINFO: |
e16b2781 RM |
442 | ret = ptrace_getsiginfo(child, &siginfo); |
443 | if (!ret) | |
444 | ret = copy_siginfo_to_user((siginfo_t __user *) data, | |
445 | &siginfo); | |
1da177e4 | 446 | break; |
e16b2781 | 447 | |
1da177e4 | 448 | case PTRACE_SETSIGINFO: |
e16b2781 RM |
449 | if (copy_from_user(&siginfo, (siginfo_t __user *) data, |
450 | sizeof siginfo)) | |
451 | ret = -EFAULT; | |
452 | else | |
453 | ret = ptrace_setsiginfo(child, &siginfo); | |
1da177e4 | 454 | break; |
e16b2781 | 455 | |
1bcf5482 AD |
456 | case PTRACE_DETACH: /* detach a process that was attached. */ |
457 | ret = ptrace_detach(child, data); | |
458 | break; | |
36df29d7 RM |
459 | |
460 | #ifdef PTRACE_SINGLESTEP | |
461 | case PTRACE_SINGLESTEP: | |
462 | #endif | |
5b88abbf RM |
463 | #ifdef PTRACE_SINGLEBLOCK |
464 | case PTRACE_SINGLEBLOCK: | |
465 | #endif | |
36df29d7 RM |
466 | #ifdef PTRACE_SYSEMU |
467 | case PTRACE_SYSEMU: | |
468 | case PTRACE_SYSEMU_SINGLESTEP: | |
469 | #endif | |
470 | case PTRACE_SYSCALL: | |
471 | case PTRACE_CONT: | |
472 | return ptrace_resume(child, request, data); | |
473 | ||
474 | case PTRACE_KILL: | |
475 | if (child->exit_state) /* already dead */ | |
476 | return 0; | |
477 | return ptrace_resume(child, request, SIGKILL); | |
478 | ||
1da177e4 LT |
479 | default: |
480 | break; | |
481 | } | |
482 | ||
483 | return ret; | |
484 | } | |
481bed45 | 485 | |
6b9c7ed8 CH |
486 | /** |
487 | * ptrace_traceme -- helper for PTRACE_TRACEME | |
488 | * | |
489 | * Performs checks and sets PT_PTRACED. | |
490 | * Should be used by all ptrace implementations for PTRACE_TRACEME. | |
491 | */ | |
492 | int ptrace_traceme(void) | |
481bed45 | 493 | { |
f5b40e36 | 494 | int ret = -EPERM; |
481bed45 CH |
495 | |
496 | /* | |
6b9c7ed8 CH |
497 | * Are we already being traced? |
498 | */ | |
f470021a | 499 | repeat: |
f5b40e36 LT |
500 | task_lock(current); |
501 | if (!(current->ptrace & PT_PTRACED)) { | |
f470021a RM |
502 | /* |
503 | * See ptrace_attach() comments about the locking here. | |
504 | */ | |
505 | unsigned long flags; | |
506 | if (!write_trylock_irqsave(&tasklist_lock, flags)) { | |
507 | task_unlock(current); | |
508 | do { | |
509 | cpu_relax(); | |
510 | } while (!write_can_lock(&tasklist_lock)); | |
511 | goto repeat; | |
512 | } | |
513 | ||
5cd9c58f | 514 | ret = security_ptrace_traceme(current->parent); |
f470021a | 515 | |
f5b40e36 LT |
516 | /* |
517 | * Set the ptrace bit in the process ptrace flags. | |
f470021a | 518 | * Then link us on our parent's ptraced list. |
f5b40e36 | 519 | */ |
f470021a | 520 | if (!ret) { |
f5b40e36 | 521 | current->ptrace |= PT_PTRACED; |
f470021a RM |
522 | __ptrace_link(current, current->real_parent); |
523 | } | |
524 | ||
525 | write_unlock_irqrestore(&tasklist_lock, flags); | |
f5b40e36 LT |
526 | } |
527 | task_unlock(current); | |
528 | return ret; | |
6b9c7ed8 | 529 | } |
481bed45 | 530 | |
6b9c7ed8 CH |
531 | /** |
532 | * ptrace_get_task_struct -- grab a task struct reference for ptrace | |
533 | * @pid: process id to grab a task_struct reference of | |
534 | * | |
535 | * This function is a helper for ptrace implementations. It checks | |
536 | * permissions and then grabs a task struct for use of the actual | |
537 | * ptrace implementation. | |
538 | * | |
539 | * Returns the task_struct for @pid or an ERR_PTR() on failure. | |
540 | */ | |
541 | struct task_struct *ptrace_get_task_struct(pid_t pid) | |
542 | { | |
543 | struct task_struct *child; | |
481bed45 | 544 | |
481bed45 | 545 | read_lock(&tasklist_lock); |
228ebcbe | 546 | child = find_task_by_vpid(pid); |
481bed45 CH |
547 | if (child) |
548 | get_task_struct(child); | |
f400e198 | 549 | |
481bed45 CH |
550 | read_unlock(&tasklist_lock); |
551 | if (!child) | |
6b9c7ed8 CH |
552 | return ERR_PTR(-ESRCH); |
553 | return child; | |
481bed45 CH |
554 | } |
555 | ||
0ac15559 CH |
556 | #ifndef arch_ptrace_attach |
557 | #define arch_ptrace_attach(child) do { } while (0) | |
558 | #endif | |
559 | ||
481bed45 CH |
560 | asmlinkage long sys_ptrace(long request, long pid, long addr, long data) |
561 | { | |
562 | struct task_struct *child; | |
563 | long ret; | |
564 | ||
565 | /* | |
566 | * This lock_kernel fixes a subtle race with suid exec | |
567 | */ | |
568 | lock_kernel(); | |
6b9c7ed8 CH |
569 | if (request == PTRACE_TRACEME) { |
570 | ret = ptrace_traceme(); | |
6ea6dd93 HS |
571 | if (!ret) |
572 | arch_ptrace_attach(current); | |
481bed45 | 573 | goto out; |
6b9c7ed8 CH |
574 | } |
575 | ||
576 | child = ptrace_get_task_struct(pid); | |
577 | if (IS_ERR(child)) { | |
578 | ret = PTR_ERR(child); | |
579 | goto out; | |
580 | } | |
481bed45 CH |
581 | |
582 | if (request == PTRACE_ATTACH) { | |
583 | ret = ptrace_attach(child); | |
0ac15559 CH |
584 | /* |
585 | * Some architectures need to do book-keeping after | |
586 | * a ptrace attach. | |
587 | */ | |
588 | if (!ret) | |
589 | arch_ptrace_attach(child); | |
005f18df | 590 | goto out_put_task_struct; |
481bed45 CH |
591 | } |
592 | ||
593 | ret = ptrace_check_attach(child, request == PTRACE_KILL); | |
594 | if (ret < 0) | |
595 | goto out_put_task_struct; | |
596 | ||
597 | ret = arch_ptrace(child, request, addr, data); | |
598 | if (ret < 0) | |
599 | goto out_put_task_struct; | |
600 | ||
601 | out_put_task_struct: | |
602 | put_task_struct(child); | |
603 | out: | |
604 | unlock_kernel(); | |
605 | return ret; | |
606 | } | |
76647323 AD |
607 | |
608 | int generic_ptrace_peekdata(struct task_struct *tsk, long addr, long data) | |
609 | { | |
610 | unsigned long tmp; | |
611 | int copied; | |
612 | ||
613 | copied = access_process_vm(tsk, addr, &tmp, sizeof(tmp), 0); | |
614 | if (copied != sizeof(tmp)) | |
615 | return -EIO; | |
616 | return put_user(tmp, (unsigned long __user *)data); | |
617 | } | |
f284ce72 AD |
618 | |
619 | int generic_ptrace_pokedata(struct task_struct *tsk, long addr, long data) | |
620 | { | |
621 | int copied; | |
622 | ||
623 | copied = access_process_vm(tsk, addr, &data, sizeof(data), 1); | |
624 | return (copied == sizeof(data)) ? 0 : -EIO; | |
625 | } | |
032d82d9 | 626 | |
96b8936a | 627 | #if defined CONFIG_COMPAT |
032d82d9 RM |
628 | #include <linux/compat.h> |
629 | ||
630 | int compat_ptrace_request(struct task_struct *child, compat_long_t request, | |
631 | compat_ulong_t addr, compat_ulong_t data) | |
632 | { | |
633 | compat_ulong_t __user *datap = compat_ptr(data); | |
634 | compat_ulong_t word; | |
e16b2781 | 635 | siginfo_t siginfo; |
032d82d9 RM |
636 | int ret; |
637 | ||
638 | switch (request) { | |
639 | case PTRACE_PEEKTEXT: | |
640 | case PTRACE_PEEKDATA: | |
641 | ret = access_process_vm(child, addr, &word, sizeof(word), 0); | |
642 | if (ret != sizeof(word)) | |
643 | ret = -EIO; | |
644 | else | |
645 | ret = put_user(word, datap); | |
646 | break; | |
647 | ||
648 | case PTRACE_POKETEXT: | |
649 | case PTRACE_POKEDATA: | |
650 | ret = access_process_vm(child, addr, &data, sizeof(data), 1); | |
651 | ret = (ret != sizeof(data) ? -EIO : 0); | |
652 | break; | |
653 | ||
654 | case PTRACE_GETEVENTMSG: | |
655 | ret = put_user((compat_ulong_t) child->ptrace_message, datap); | |
656 | break; | |
657 | ||
e16b2781 RM |
658 | case PTRACE_GETSIGINFO: |
659 | ret = ptrace_getsiginfo(child, &siginfo); | |
660 | if (!ret) | |
661 | ret = copy_siginfo_to_user32( | |
662 | (struct compat_siginfo __user *) datap, | |
663 | &siginfo); | |
664 | break; | |
665 | ||
666 | case PTRACE_SETSIGINFO: | |
667 | memset(&siginfo, 0, sizeof siginfo); | |
668 | if (copy_siginfo_from_user32( | |
669 | &siginfo, (struct compat_siginfo __user *) datap)) | |
670 | ret = -EFAULT; | |
671 | else | |
672 | ret = ptrace_setsiginfo(child, &siginfo); | |
673 | break; | |
674 | ||
032d82d9 RM |
675 | default: |
676 | ret = ptrace_request(child, request, addr, data); | |
677 | } | |
678 | ||
679 | return ret; | |
680 | } | |
c269f196 | 681 | |
c269f196 RM |
682 | asmlinkage long compat_sys_ptrace(compat_long_t request, compat_long_t pid, |
683 | compat_long_t addr, compat_long_t data) | |
684 | { | |
685 | struct task_struct *child; | |
686 | long ret; | |
687 | ||
688 | /* | |
689 | * This lock_kernel fixes a subtle race with suid exec | |
690 | */ | |
691 | lock_kernel(); | |
692 | if (request == PTRACE_TRACEME) { | |
693 | ret = ptrace_traceme(); | |
694 | goto out; | |
695 | } | |
696 | ||
697 | child = ptrace_get_task_struct(pid); | |
698 | if (IS_ERR(child)) { | |
699 | ret = PTR_ERR(child); | |
700 | goto out; | |
701 | } | |
702 | ||
703 | if (request == PTRACE_ATTACH) { | |
704 | ret = ptrace_attach(child); | |
705 | /* | |
706 | * Some architectures need to do book-keeping after | |
707 | * a ptrace attach. | |
708 | */ | |
709 | if (!ret) | |
710 | arch_ptrace_attach(child); | |
711 | goto out_put_task_struct; | |
712 | } | |
713 | ||
714 | ret = ptrace_check_attach(child, request == PTRACE_KILL); | |
715 | if (!ret) | |
716 | ret = compat_arch_ptrace(child, request, addr, data); | |
717 | ||
718 | out_put_task_struct: | |
719 | put_task_struct(child); | |
720 | out: | |
721 | unlock_kernel(); | |
722 | return ret; | |
723 | } | |
96b8936a | 724 | #endif /* CONFIG_COMPAT */ |