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 | { |
c69e8d9c | 130 | const struct cred *cred = current_cred(), *tcred; |
b6dff3ec | 131 | |
df26c40e EB |
132 | /* May we inspect the given task? |
133 | * This check is used both for attaching with ptrace | |
134 | * and for allowing access to sensitive information in /proc. | |
135 | * | |
136 | * ptrace_attach denies several cases that /proc allows | |
137 | * because setting up the necessary parent/child relationship | |
138 | * or halting the specified task is impossible. | |
139 | */ | |
140 | int dumpable = 0; | |
141 | /* Don't let security modules deny introspection */ | |
142 | if (task == current) | |
143 | return 0; | |
c69e8d9c DH |
144 | rcu_read_lock(); |
145 | tcred = __task_cred(task); | |
146 | if ((cred->uid != tcred->euid || | |
147 | cred->uid != tcred->suid || | |
148 | cred->uid != tcred->uid || | |
149 | cred->gid != tcred->egid || | |
150 | cred->gid != tcred->sgid || | |
151 | cred->gid != tcred->gid) && | |
152 | !capable(CAP_SYS_PTRACE)) { | |
153 | rcu_read_unlock(); | |
ab8d11be | 154 | return -EPERM; |
c69e8d9c DH |
155 | } |
156 | rcu_read_unlock(); | |
ab8d11be | 157 | smp_rmb(); |
df26c40e | 158 | if (task->mm) |
6c5d5238 | 159 | dumpable = get_dumpable(task->mm); |
df26c40e | 160 | if (!dumpable && !capable(CAP_SYS_PTRACE)) |
ab8d11be MS |
161 | return -EPERM; |
162 | ||
5cd9c58f | 163 | return security_ptrace_may_access(task, mode); |
ab8d11be MS |
164 | } |
165 | ||
006ebb40 | 166 | bool ptrace_may_access(struct task_struct *task, unsigned int mode) |
ab8d11be MS |
167 | { |
168 | int err; | |
169 | task_lock(task); | |
006ebb40 | 170 | err = __ptrace_may_access(task, mode); |
ab8d11be | 171 | task_unlock(task); |
006ebb40 | 172 | return (!err ? true : false); |
ab8d11be MS |
173 | } |
174 | ||
1da177e4 LT |
175 | int ptrace_attach(struct task_struct *task) |
176 | { | |
177 | int retval; | |
6175ecfe | 178 | unsigned long flags; |
f5b40e36 | 179 | |
a5cb013d AV |
180 | audit_ptrace(task); |
181 | ||
1da177e4 | 182 | retval = -EPERM; |
bac0abd6 | 183 | if (same_thread_group(task, current)) |
f5b40e36 LT |
184 | goto out; |
185 | ||
d84f4f99 DH |
186 | /* Protect exec's credential calculations against our interference; |
187 | * SUID, SGID and LSM creds get determined differently under ptrace. | |
188 | */ | |
189 | retval = mutex_lock_interruptible(¤t->cred_exec_mutex); | |
190 | if (retval < 0) | |
191 | goto out; | |
192 | ||
193 | retval = -EPERM; | |
f358166a LT |
194 | repeat: |
195 | /* | |
196 | * Nasty, nasty. | |
197 | * | |
198 | * We want to hold both the task-lock and the | |
199 | * tasklist_lock for writing at the same time. | |
200 | * But that's against the rules (tasklist_lock | |
201 | * is taken for reading by interrupts on other | |
202 | * cpu's that may have task_lock). | |
203 | */ | |
f5b40e36 | 204 | task_lock(task); |
6175ecfe | 205 | if (!write_trylock_irqsave(&tasklist_lock, flags)) { |
f358166a LT |
206 | task_unlock(task); |
207 | do { | |
208 | cpu_relax(); | |
209 | } while (!write_can_lock(&tasklist_lock)); | |
210 | goto repeat; | |
211 | } | |
f5b40e36 | 212 | |
df26c40e EB |
213 | if (!task->mm) |
214 | goto bad; | |
1da177e4 LT |
215 | /* the same process cannot be attached many times */ |
216 | if (task->ptrace & PT_PTRACED) | |
217 | goto bad; | |
006ebb40 | 218 | retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH); |
1da177e4 LT |
219 | if (retval) |
220 | goto bad; | |
221 | ||
222 | /* Go */ | |
6b39c7bf | 223 | task->ptrace |= PT_PTRACED; |
1da177e4 LT |
224 | if (capable(CAP_SYS_PTRACE)) |
225 | task->ptrace |= PT_PTRACE_CAP; | |
1da177e4 | 226 | |
1da177e4 | 227 | __ptrace_link(task, current); |
1da177e4 | 228 | |
33e9fc7d | 229 | send_sig_info(SIGSTOP, SEND_SIG_FORCED, task); |
1da177e4 | 230 | bad: |
6175ecfe | 231 | write_unlock_irqrestore(&tasklist_lock, flags); |
1da177e4 | 232 | task_unlock(task); |
d84f4f99 | 233 | mutex_unlock(¤t->cred_exec_mutex); |
f5b40e36 | 234 | out: |
1da177e4 LT |
235 | return retval; |
236 | } | |
237 | ||
238 | int ptrace_detach(struct task_struct *child, unsigned int data) | |
239 | { | |
4576145c ON |
240 | int dead = 0; |
241 | ||
7ed20e1a | 242 | if (!valid_signal(data)) |
5ecfbae0 | 243 | return -EIO; |
1da177e4 LT |
244 | |
245 | /* Architecture-specific hardware disable .. */ | |
246 | ptrace_disable(child); | |
7d941432 | 247 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
1da177e4 | 248 | |
95c3eb76 | 249 | write_lock_irq(&tasklist_lock); |
4576145c | 250 | /* protect against de_thread()->release_task() */ |
95c3eb76 ON |
251 | if (child->ptrace) { |
252 | child->exit_code = data; | |
253 | ||
4576145c | 254 | dead = __ptrace_detach(current, child); |
95c3eb76 ON |
255 | |
256 | if (!child->exit_state) | |
257 | wake_up_process(child); | |
258 | } | |
1da177e4 LT |
259 | write_unlock_irq(&tasklist_lock); |
260 | ||
4576145c ON |
261 | if (unlikely(dead)) |
262 | release_task(child); | |
263 | ||
1da177e4 LT |
264 | return 0; |
265 | } | |
266 | ||
1da177e4 LT |
267 | int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len) |
268 | { | |
269 | int copied = 0; | |
270 | ||
271 | while (len > 0) { | |
272 | char buf[128]; | |
273 | int this_len, retval; | |
274 | ||
275 | this_len = (len > sizeof(buf)) ? sizeof(buf) : len; | |
276 | retval = access_process_vm(tsk, src, buf, this_len, 0); | |
277 | if (!retval) { | |
278 | if (copied) | |
279 | break; | |
280 | return -EIO; | |
281 | } | |
282 | if (copy_to_user(dst, buf, retval)) | |
283 | return -EFAULT; | |
284 | copied += retval; | |
285 | src += retval; | |
286 | dst += retval; | |
287 | len -= retval; | |
288 | } | |
289 | return copied; | |
290 | } | |
291 | ||
292 | int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len) | |
293 | { | |
294 | int copied = 0; | |
295 | ||
296 | while (len > 0) { | |
297 | char buf[128]; | |
298 | int this_len, retval; | |
299 | ||
300 | this_len = (len > sizeof(buf)) ? sizeof(buf) : len; | |
301 | if (copy_from_user(buf, src, this_len)) | |
302 | return -EFAULT; | |
303 | retval = access_process_vm(tsk, dst, buf, this_len, 1); | |
304 | if (!retval) { | |
305 | if (copied) | |
306 | break; | |
307 | return -EIO; | |
308 | } | |
309 | copied += retval; | |
310 | src += retval; | |
311 | dst += retval; | |
312 | len -= retval; | |
313 | } | |
314 | return copied; | |
315 | } | |
316 | ||
317 | static int ptrace_setoptions(struct task_struct *child, long data) | |
318 | { | |
319 | child->ptrace &= ~PT_TRACE_MASK; | |
320 | ||
321 | if (data & PTRACE_O_TRACESYSGOOD) | |
322 | child->ptrace |= PT_TRACESYSGOOD; | |
323 | ||
324 | if (data & PTRACE_O_TRACEFORK) | |
325 | child->ptrace |= PT_TRACE_FORK; | |
326 | ||
327 | if (data & PTRACE_O_TRACEVFORK) | |
328 | child->ptrace |= PT_TRACE_VFORK; | |
329 | ||
330 | if (data & PTRACE_O_TRACECLONE) | |
331 | child->ptrace |= PT_TRACE_CLONE; | |
332 | ||
333 | if (data & PTRACE_O_TRACEEXEC) | |
334 | child->ptrace |= PT_TRACE_EXEC; | |
335 | ||
336 | if (data & PTRACE_O_TRACEVFORKDONE) | |
337 | child->ptrace |= PT_TRACE_VFORK_DONE; | |
338 | ||
339 | if (data & PTRACE_O_TRACEEXIT) | |
340 | child->ptrace |= PT_TRACE_EXIT; | |
341 | ||
342 | return (data & ~PTRACE_O_MASK) ? -EINVAL : 0; | |
343 | } | |
344 | ||
e16b2781 | 345 | static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info) |
1da177e4 | 346 | { |
1da177e4 LT |
347 | int error = -ESRCH; |
348 | ||
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 | *info = *child->last_siginfo; |
1da177e4 LT |
355 | error = 0; |
356 | } | |
357 | spin_unlock_irq(&child->sighand->siglock); | |
358 | } | |
359 | read_unlock(&tasklist_lock); | |
1da177e4 LT |
360 | return error; |
361 | } | |
362 | ||
e16b2781 | 363 | static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info) |
1da177e4 | 364 | { |
1da177e4 LT |
365 | int error = -ESRCH; |
366 | ||
1da177e4 LT |
367 | read_lock(&tasklist_lock); |
368 | if (likely(child->sighand != NULL)) { | |
369 | error = -EINVAL; | |
370 | spin_lock_irq(&child->sighand->siglock); | |
371 | if (likely(child->last_siginfo != NULL)) { | |
e16b2781 | 372 | *child->last_siginfo = *info; |
1da177e4 LT |
373 | error = 0; |
374 | } | |
375 | spin_unlock_irq(&child->sighand->siglock); | |
376 | } | |
377 | read_unlock(&tasklist_lock); | |
378 | return error; | |
379 | } | |
380 | ||
36df29d7 RM |
381 | |
382 | #ifdef PTRACE_SINGLESTEP | |
383 | #define is_singlestep(request) ((request) == PTRACE_SINGLESTEP) | |
384 | #else | |
385 | #define is_singlestep(request) 0 | |
386 | #endif | |
387 | ||
5b88abbf RM |
388 | #ifdef PTRACE_SINGLEBLOCK |
389 | #define is_singleblock(request) ((request) == PTRACE_SINGLEBLOCK) | |
390 | #else | |
391 | #define is_singleblock(request) 0 | |
392 | #endif | |
393 | ||
36df29d7 RM |
394 | #ifdef PTRACE_SYSEMU |
395 | #define is_sysemu_singlestep(request) ((request) == PTRACE_SYSEMU_SINGLESTEP) | |
396 | #else | |
397 | #define is_sysemu_singlestep(request) 0 | |
398 | #endif | |
399 | ||
400 | static int ptrace_resume(struct task_struct *child, long request, long data) | |
401 | { | |
402 | if (!valid_signal(data)) | |
403 | return -EIO; | |
404 | ||
405 | if (request == PTRACE_SYSCALL) | |
406 | set_tsk_thread_flag(child, TIF_SYSCALL_TRACE); | |
407 | else | |
408 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); | |
409 | ||
410 | #ifdef TIF_SYSCALL_EMU | |
411 | if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP) | |
412 | set_tsk_thread_flag(child, TIF_SYSCALL_EMU); | |
413 | else | |
414 | clear_tsk_thread_flag(child, TIF_SYSCALL_EMU); | |
415 | #endif | |
416 | ||
5b88abbf RM |
417 | if (is_singleblock(request)) { |
418 | if (unlikely(!arch_has_block_step())) | |
419 | return -EIO; | |
420 | user_enable_block_step(child); | |
421 | } else if (is_singlestep(request) || is_sysemu_singlestep(request)) { | |
36df29d7 RM |
422 | if (unlikely(!arch_has_single_step())) |
423 | return -EIO; | |
424 | user_enable_single_step(child); | |
425 | } | |
426 | else | |
427 | user_disable_single_step(child); | |
428 | ||
429 | child->exit_code = data; | |
430 | wake_up_process(child); | |
431 | ||
432 | return 0; | |
433 | } | |
434 | ||
1da177e4 LT |
435 | int ptrace_request(struct task_struct *child, long request, |
436 | long addr, long data) | |
437 | { | |
438 | int ret = -EIO; | |
e16b2781 | 439 | siginfo_t siginfo; |
1da177e4 LT |
440 | |
441 | switch (request) { | |
16c3e389 RM |
442 | case PTRACE_PEEKTEXT: |
443 | case PTRACE_PEEKDATA: | |
444 | return generic_ptrace_peekdata(child, addr, data); | |
445 | case PTRACE_POKETEXT: | |
446 | case PTRACE_POKEDATA: | |
447 | return generic_ptrace_pokedata(child, addr, data); | |
448 | ||
1da177e4 LT |
449 | #ifdef PTRACE_OLDSETOPTIONS |
450 | case PTRACE_OLDSETOPTIONS: | |
451 | #endif | |
452 | case PTRACE_SETOPTIONS: | |
453 | ret = ptrace_setoptions(child, data); | |
454 | break; | |
455 | case PTRACE_GETEVENTMSG: | |
456 | ret = put_user(child->ptrace_message, (unsigned long __user *) data); | |
457 | break; | |
e16b2781 | 458 | |
1da177e4 | 459 | case PTRACE_GETSIGINFO: |
e16b2781 RM |
460 | ret = ptrace_getsiginfo(child, &siginfo); |
461 | if (!ret) | |
462 | ret = copy_siginfo_to_user((siginfo_t __user *) data, | |
463 | &siginfo); | |
1da177e4 | 464 | break; |
e16b2781 | 465 | |
1da177e4 | 466 | case PTRACE_SETSIGINFO: |
e16b2781 RM |
467 | if (copy_from_user(&siginfo, (siginfo_t __user *) data, |
468 | sizeof siginfo)) | |
469 | ret = -EFAULT; | |
470 | else | |
471 | ret = ptrace_setsiginfo(child, &siginfo); | |
1da177e4 | 472 | break; |
e16b2781 | 473 | |
1bcf5482 AD |
474 | case PTRACE_DETACH: /* detach a process that was attached. */ |
475 | ret = ptrace_detach(child, data); | |
476 | break; | |
36df29d7 RM |
477 | |
478 | #ifdef PTRACE_SINGLESTEP | |
479 | case PTRACE_SINGLESTEP: | |
480 | #endif | |
5b88abbf RM |
481 | #ifdef PTRACE_SINGLEBLOCK |
482 | case PTRACE_SINGLEBLOCK: | |
483 | #endif | |
36df29d7 RM |
484 | #ifdef PTRACE_SYSEMU |
485 | case PTRACE_SYSEMU: | |
486 | case PTRACE_SYSEMU_SINGLESTEP: | |
487 | #endif | |
488 | case PTRACE_SYSCALL: | |
489 | case PTRACE_CONT: | |
490 | return ptrace_resume(child, request, data); | |
491 | ||
492 | case PTRACE_KILL: | |
493 | if (child->exit_state) /* already dead */ | |
494 | return 0; | |
495 | return ptrace_resume(child, request, SIGKILL); | |
496 | ||
1da177e4 LT |
497 | default: |
498 | break; | |
499 | } | |
500 | ||
501 | return ret; | |
502 | } | |
481bed45 | 503 | |
6b9c7ed8 CH |
504 | /** |
505 | * ptrace_traceme -- helper for PTRACE_TRACEME | |
506 | * | |
507 | * Performs checks and sets PT_PTRACED. | |
508 | * Should be used by all ptrace implementations for PTRACE_TRACEME. | |
509 | */ | |
510 | int ptrace_traceme(void) | |
481bed45 | 511 | { |
f5b40e36 | 512 | int ret = -EPERM; |
481bed45 CH |
513 | |
514 | /* | |
6b9c7ed8 CH |
515 | * Are we already being traced? |
516 | */ | |
f470021a | 517 | repeat: |
f5b40e36 LT |
518 | task_lock(current); |
519 | if (!(current->ptrace & PT_PTRACED)) { | |
f470021a RM |
520 | /* |
521 | * See ptrace_attach() comments about the locking here. | |
522 | */ | |
523 | unsigned long flags; | |
524 | if (!write_trylock_irqsave(&tasklist_lock, flags)) { | |
525 | task_unlock(current); | |
526 | do { | |
527 | cpu_relax(); | |
528 | } while (!write_can_lock(&tasklist_lock)); | |
529 | goto repeat; | |
530 | } | |
531 | ||
5cd9c58f | 532 | ret = security_ptrace_traceme(current->parent); |
f470021a | 533 | |
f5b40e36 LT |
534 | /* |
535 | * Set the ptrace bit in the process ptrace flags. | |
f470021a | 536 | * Then link us on our parent's ptraced list. |
f5b40e36 | 537 | */ |
f470021a | 538 | if (!ret) { |
f5b40e36 | 539 | current->ptrace |= PT_PTRACED; |
f470021a RM |
540 | __ptrace_link(current, current->real_parent); |
541 | } | |
542 | ||
543 | write_unlock_irqrestore(&tasklist_lock, flags); | |
f5b40e36 LT |
544 | } |
545 | task_unlock(current); | |
546 | return ret; | |
6b9c7ed8 | 547 | } |
481bed45 | 548 | |
6b9c7ed8 CH |
549 | /** |
550 | * ptrace_get_task_struct -- grab a task struct reference for ptrace | |
551 | * @pid: process id to grab a task_struct reference of | |
552 | * | |
553 | * This function is a helper for ptrace implementations. It checks | |
554 | * permissions and then grabs a task struct for use of the actual | |
555 | * ptrace implementation. | |
556 | * | |
557 | * Returns the task_struct for @pid or an ERR_PTR() on failure. | |
558 | */ | |
559 | struct task_struct *ptrace_get_task_struct(pid_t pid) | |
560 | { | |
561 | struct task_struct *child; | |
481bed45 | 562 | |
481bed45 | 563 | read_lock(&tasklist_lock); |
228ebcbe | 564 | child = find_task_by_vpid(pid); |
481bed45 CH |
565 | if (child) |
566 | get_task_struct(child); | |
f400e198 | 567 | |
481bed45 CH |
568 | read_unlock(&tasklist_lock); |
569 | if (!child) | |
6b9c7ed8 CH |
570 | return ERR_PTR(-ESRCH); |
571 | return child; | |
481bed45 CH |
572 | } |
573 | ||
0ac15559 CH |
574 | #ifndef arch_ptrace_attach |
575 | #define arch_ptrace_attach(child) do { } while (0) | |
576 | #endif | |
577 | ||
1e7bfb21 | 578 | SYSCALL_DEFINE4(ptrace, long, request, long, pid, long, addr, long, data) |
481bed45 CH |
579 | { |
580 | struct task_struct *child; | |
581 | long ret; | |
582 | ||
583 | /* | |
584 | * This lock_kernel fixes a subtle race with suid exec | |
585 | */ | |
586 | lock_kernel(); | |
6b9c7ed8 CH |
587 | if (request == PTRACE_TRACEME) { |
588 | ret = ptrace_traceme(); | |
6ea6dd93 HS |
589 | if (!ret) |
590 | arch_ptrace_attach(current); | |
481bed45 | 591 | goto out; |
6b9c7ed8 CH |
592 | } |
593 | ||
594 | child = ptrace_get_task_struct(pid); | |
595 | if (IS_ERR(child)) { | |
596 | ret = PTR_ERR(child); | |
597 | goto out; | |
598 | } | |
481bed45 CH |
599 | |
600 | if (request == PTRACE_ATTACH) { | |
601 | ret = ptrace_attach(child); | |
0ac15559 CH |
602 | /* |
603 | * Some architectures need to do book-keeping after | |
604 | * a ptrace attach. | |
605 | */ | |
606 | if (!ret) | |
607 | arch_ptrace_attach(child); | |
005f18df | 608 | goto out_put_task_struct; |
481bed45 CH |
609 | } |
610 | ||
611 | ret = ptrace_check_attach(child, request == PTRACE_KILL); | |
612 | if (ret < 0) | |
613 | goto out_put_task_struct; | |
614 | ||
615 | ret = arch_ptrace(child, request, addr, data); | |
616 | if (ret < 0) | |
617 | goto out_put_task_struct; | |
618 | ||
619 | out_put_task_struct: | |
620 | put_task_struct(child); | |
621 | out: | |
622 | unlock_kernel(); | |
623 | return ret; | |
624 | } | |
76647323 AD |
625 | |
626 | int generic_ptrace_peekdata(struct task_struct *tsk, long addr, long data) | |
627 | { | |
628 | unsigned long tmp; | |
629 | int copied; | |
630 | ||
631 | copied = access_process_vm(tsk, addr, &tmp, sizeof(tmp), 0); | |
632 | if (copied != sizeof(tmp)) | |
633 | return -EIO; | |
634 | return put_user(tmp, (unsigned long __user *)data); | |
635 | } | |
f284ce72 AD |
636 | |
637 | int generic_ptrace_pokedata(struct task_struct *tsk, long addr, long data) | |
638 | { | |
639 | int copied; | |
640 | ||
641 | copied = access_process_vm(tsk, addr, &data, sizeof(data), 1); | |
642 | return (copied == sizeof(data)) ? 0 : -EIO; | |
643 | } | |
032d82d9 | 644 | |
96b8936a | 645 | #if defined CONFIG_COMPAT |
032d82d9 RM |
646 | #include <linux/compat.h> |
647 | ||
648 | int compat_ptrace_request(struct task_struct *child, compat_long_t request, | |
649 | compat_ulong_t addr, compat_ulong_t data) | |
650 | { | |
651 | compat_ulong_t __user *datap = compat_ptr(data); | |
652 | compat_ulong_t word; | |
e16b2781 | 653 | siginfo_t siginfo; |
032d82d9 RM |
654 | int ret; |
655 | ||
656 | switch (request) { | |
657 | case PTRACE_PEEKTEXT: | |
658 | case PTRACE_PEEKDATA: | |
659 | ret = access_process_vm(child, addr, &word, sizeof(word), 0); | |
660 | if (ret != sizeof(word)) | |
661 | ret = -EIO; | |
662 | else | |
663 | ret = put_user(word, datap); | |
664 | break; | |
665 | ||
666 | case PTRACE_POKETEXT: | |
667 | case PTRACE_POKEDATA: | |
668 | ret = access_process_vm(child, addr, &data, sizeof(data), 1); | |
669 | ret = (ret != sizeof(data) ? -EIO : 0); | |
670 | break; | |
671 | ||
672 | case PTRACE_GETEVENTMSG: | |
673 | ret = put_user((compat_ulong_t) child->ptrace_message, datap); | |
674 | break; | |
675 | ||
e16b2781 RM |
676 | case PTRACE_GETSIGINFO: |
677 | ret = ptrace_getsiginfo(child, &siginfo); | |
678 | if (!ret) | |
679 | ret = copy_siginfo_to_user32( | |
680 | (struct compat_siginfo __user *) datap, | |
681 | &siginfo); | |
682 | break; | |
683 | ||
684 | case PTRACE_SETSIGINFO: | |
685 | memset(&siginfo, 0, sizeof siginfo); | |
686 | if (copy_siginfo_from_user32( | |
687 | &siginfo, (struct compat_siginfo __user *) datap)) | |
688 | ret = -EFAULT; | |
689 | else | |
690 | ret = ptrace_setsiginfo(child, &siginfo); | |
691 | break; | |
692 | ||
032d82d9 RM |
693 | default: |
694 | ret = ptrace_request(child, request, addr, data); | |
695 | } | |
696 | ||
697 | return ret; | |
698 | } | |
c269f196 | 699 | |
c269f196 RM |
700 | asmlinkage long compat_sys_ptrace(compat_long_t request, compat_long_t pid, |
701 | compat_long_t addr, compat_long_t data) | |
702 | { | |
703 | struct task_struct *child; | |
704 | long ret; | |
705 | ||
706 | /* | |
707 | * This lock_kernel fixes a subtle race with suid exec | |
708 | */ | |
709 | lock_kernel(); | |
710 | if (request == PTRACE_TRACEME) { | |
711 | ret = ptrace_traceme(); | |
712 | goto out; | |
713 | } | |
714 | ||
715 | child = ptrace_get_task_struct(pid); | |
716 | if (IS_ERR(child)) { | |
717 | ret = PTR_ERR(child); | |
718 | goto out; | |
719 | } | |
720 | ||
721 | if (request == PTRACE_ATTACH) { | |
722 | ret = ptrace_attach(child); | |
723 | /* | |
724 | * Some architectures need to do book-keeping after | |
725 | * a ptrace attach. | |
726 | */ | |
727 | if (!ret) | |
728 | arch_ptrace_attach(child); | |
729 | goto out_put_task_struct; | |
730 | } | |
731 | ||
732 | ret = ptrace_check_attach(child, request == PTRACE_KILL); | |
733 | if (!ret) | |
734 | ret = compat_arch_ptrace(child, request, addr, data); | |
735 | ||
736 | out_put_task_struct: | |
737 | put_task_struct(child); | |
738 | out: | |
739 | unlock_kernel(); | |
740 | return ret; | |
741 | } | |
96b8936a | 742 | #endif /* CONFIG_COMPAT */ |