blob: 100a71cfdabac3200d87e2ac7037847dee28a27e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/ptrace.c
3 *
4 * (C) Copyright 1999 Linus Torvalds
5 *
6 * Common interfaces for "ptrace()" which we do not want
7 * to continually duplicate across every architecture.
8 */
9
Randy.Dunlapc59ede72006-01-11 12:17:46 -080010#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#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>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070020#include <linux/signal.h>
Al Viroa5cb0132007-03-20 13:58:35 -040021#include <linux/audit.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070022#include <linux/pid_namespace.h>
Adrian Bunkf17d30a2008-02-06 01:36:44 -080023#include <linux/syscalls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <asm/pgtable.h>
26#include <asm/uaccess.h>
27
Markus Metzgerbf53de92008-12-19 15:10:24 +010028
29/*
30 * Initialize a new task whose father had been ptraced.
31 *
32 * Called from copy_process().
33 */
34void ptrace_fork(struct task_struct *child, unsigned long clone_flags)
35{
36 arch_ptrace_fork(child, clone_flags);
37}
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/*
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 */
Ingo Molnar36c8b582006-07-03 00:25:41 -070045void __ptrace_link(struct task_struct *child, struct task_struct *new_parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
Roland McGrathf4700212008-03-24 18:36:23 -070047 BUG_ON(!list_empty(&child->ptrace_entry));
48 list_add(&child->ptrace_entry, &new_parent->ptraced);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 child->parent = new_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
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 */
Adrian Bunkb747c8c2008-10-18 20:28:21 -070059static void ptrace_untrace(struct task_struct *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
61 spin_lock(&child->sighand->siglock);
Matthew Wilcox6618a3e2007-12-06 11:06:16 -050062 if (task_is_traced(child)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 if (child->signal->flags & SIGNAL_STOP_STOPPED) {
Oleg Nesterovd9ae90a2008-02-06 01:36:13 -080064 __set_task_state(child, TASK_STOPPED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 } 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 */
Ingo Molnar36c8b582006-07-03 00:25:41 -070078void __ptrace_unlink(struct task_struct *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Oleg Nesterov5ecfbae2006-02-15 22:50:10 +030080 BUG_ON(!child->ptrace);
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 child->ptrace = 0;
Roland McGrathf4700212008-03-24 18:36:23 -070083 child->parent = child->real_parent;
84 list_del_init(&child->ptrace_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Markus Metzgerbf53de92008-12-19 15:10:24 +010086 arch_ptrace_untrace(child);
Matthew Wilcox6618a3e2007-12-06 11:06:16 -050087 if (task_is_traced(child))
Roland McGrathe57a5052006-04-12 16:30:20 -070088 ptrace_untrace(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
91/*
92 * Check that we have indeed attached to the thing..
93 */
94int 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);
Oleg Nesterovc0c0b642008-02-08 04:19:00 -0800106 if ((child->ptrace & PT_PTRACED) && child->parent == current) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 ret = 0;
Oleg Nesterovc0c0b642008-02-08 04:19:00 -0800108 /*
109 * child->sighand can't be NULL, release_task()
110 * does ptrace_unlink() before __exit_signal().
111 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 spin_lock_irq(&child->sighand->siglock);
Oleg Nesterovd9ae90a2008-02-06 01:36:13 -0800113 if (task_is_stopped(child))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 child->state = TASK_TRACED;
Oleg Nesterovd9ae90a2008-02-06 01:36:13 -0800115 else if (!task_is_traced(child) && !kill)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 ret = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 spin_unlock_irq(&child->sighand->siglock);
118 }
119 read_unlock(&tasklist_lock);
120
Oleg Nesterovd9ae90a2008-02-06 01:36:13 -0800121 if (!ret && !kill)
Roland McGrath85ba2d82008-07-25 19:45:58 -0700122 ret = wait_task_inactive(child, TASK_TRACED) ? 0 : -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 /* All systems go.. */
125 return ret;
126}
127
Stephen Smalley006ebb42008-05-19 08:32:49 -0400128int __ptrace_may_access(struct task_struct *task, unsigned int mode)
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700129{
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700130 /* 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;
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700142 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();
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700150 if (task->mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -0700151 dumpable = get_dumpable(task->mm);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700152 if (!dumpable && !capable(CAP_SYS_PTRACE))
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700153 return -EPERM;
154
David Howells5cd9c582008-08-14 11:37:28 +0100155 return security_ptrace_may_access(task, mode);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700156}
157
Stephen Smalley006ebb42008-05-19 08:32:49 -0400158bool ptrace_may_access(struct task_struct *task, unsigned int mode)
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700159{
160 int err;
161 task_lock(task);
Stephen Smalley006ebb42008-05-19 08:32:49 -0400162 err = __ptrace_may_access(task, mode);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700163 task_unlock(task);
Stephen Smalley006ebb42008-05-19 08:32:49 -0400164 return (!err ? true : false);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700165}
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167int ptrace_attach(struct task_struct *task)
168{
169 int retval;
Sripathi Kodi6175ecf2007-07-15 23:39:26 -0700170 unsigned long flags;
Linus Torvaldsf5b40e32006-05-07 10:49:33 -0700171
Al Viroa5cb0132007-03-20 13:58:35 -0400172 audit_ptrace(task);
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 retval = -EPERM;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -0700175 if (same_thread_group(task, current))
Linus Torvaldsf5b40e32006-05-07 10:49:33 -0700176 goto out;
177
Linus Torvaldsf3581662006-05-11 11:08:49 -0700178repeat:
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 */
Linus Torvaldsf5b40e32006-05-07 10:49:33 -0700188 task_lock(task);
Sripathi Kodi6175ecf2007-07-15 23:39:26 -0700189 if (!write_trylock_irqsave(&tasklist_lock, flags)) {
Linus Torvaldsf3581662006-05-11 11:08:49 -0700190 task_unlock(task);
191 do {
192 cpu_relax();
193 } while (!write_can_lock(&tasklist_lock));
194 goto repeat;
195 }
Linus Torvaldsf5b40e32006-05-07 10:49:33 -0700196
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700197 if (!task->mm)
198 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 /* the same process cannot be attached many times */
200 if (task->ptrace & PT_PTRACED)
201 goto bad;
Stephen Smalley006ebb42008-05-19 08:32:49 -0400202 retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 if (retval)
204 goto bad;
205
206 /* Go */
Oleg Nesterov6b39c7b2008-02-08 04:18:58 -0800207 task->ptrace |= PT_PTRACED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (capable(CAP_SYS_PTRACE))
209 task->ptrace |= PT_PTRACE_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 __ptrace_link(task, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Oleg Nesterov33e9fc72008-04-30 00:53:14 -0700213 send_sig_info(SIGSTOP, SEND_SIG_FORCED, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214bad:
Sripathi Kodi6175ecf2007-07-15 23:39:26 -0700215 write_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 task_unlock(task);
Linus Torvaldsf5b40e32006-05-07 10:49:33 -0700217out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return retval;
219}
220
Oleg Nesterovd5f70c02006-06-26 00:26:07 -0700221static inline void __ptrace_detach(struct task_struct *child, unsigned int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 child->exit_code = data;
Oleg Nesterov5ecfbae2006-02-15 22:50:10 +0300224 /* .. re-parent .. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 __ptrace_unlink(child);
226 /* .. and wake it up. */
227 if (child->exit_state != EXIT_ZOMBIE)
228 wake_up_process(child);
Oleg Nesterov5ecfbae2006-02-15 22:50:10 +0300229}
230
231int ptrace_detach(struct task_struct *child, unsigned int data)
232{
233 if (!valid_signal(data))
234 return -EIO;
235
236 /* Architecture-specific hardware disable .. */
237 ptrace_disable(child);
Roland McGrath7d941432007-09-05 03:05:56 -0700238 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
Oleg Nesterov5ecfbae2006-02-15 22:50:10 +0300239
240 write_lock_irq(&tasklist_lock);
Oleg Nesterovd5f70c02006-06-26 00:26:07 -0700241 /* protect against de_thread()->release_task() */
Oleg Nesterov5ecfbae2006-02-15 22:50:10 +0300242 if (child->ptrace)
243 __ptrace_detach(child, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 write_unlock_irq(&tasklist_lock);
245
246 return 0;
247}
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249int 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
274int 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
299static 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
Roland McGrathe16b2782008-04-20 13:10:12 -0700327static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 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)) {
Roland McGrathe16b2782008-04-20 13:10:12 -0700336 *info = *child->last_siginfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 error = 0;
338 }
339 spin_unlock_irq(&child->sighand->siglock);
340 }
341 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 return error;
343}
344
Roland McGrathe16b2782008-04-20 13:10:12 -0700345static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 int error = -ESRCH;
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 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)) {
Roland McGrathe16b2782008-04-20 13:10:12 -0700354 *child->last_siginfo = *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 error = 0;
356 }
357 spin_unlock_irq(&child->sighand->siglock);
358 }
359 read_unlock(&tasklist_lock);
360 return error;
361}
362
Roland McGrath36df29d2008-01-30 13:30:51 +0100363
364#ifdef PTRACE_SINGLESTEP
365#define is_singlestep(request) ((request) == PTRACE_SINGLESTEP)
366#else
367#define is_singlestep(request) 0
368#endif
369
Roland McGrath5b88abb2008-01-30 13:30:53 +0100370#ifdef PTRACE_SINGLEBLOCK
371#define is_singleblock(request) ((request) == PTRACE_SINGLEBLOCK)
372#else
373#define is_singleblock(request) 0
374#endif
375
Roland McGrath36df29d2008-01-30 13:30:51 +0100376#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
382static 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
Roland McGrath5b88abb2008-01-30 13:30:53 +0100399 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)) {
Roland McGrath36df29d2008-01-30 13:30:51 +0100404 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417int ptrace_request(struct task_struct *child, long request,
418 long addr, long data)
419{
420 int ret = -EIO;
Roland McGrathe16b2782008-04-20 13:10:12 -0700421 siginfo_t siginfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 switch (request) {
Roland McGrath16c3e382008-01-30 13:31:47 +0100424 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431#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;
Roland McGrathe16b2782008-04-20 13:10:12 -0700440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 case PTRACE_GETSIGINFO:
Roland McGrathe16b2782008-04-20 13:10:12 -0700442 ret = ptrace_getsiginfo(child, &siginfo);
443 if (!ret)
444 ret = copy_siginfo_to_user((siginfo_t __user *) data,
445 &siginfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 break;
Roland McGrathe16b2782008-04-20 13:10:12 -0700447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 case PTRACE_SETSIGINFO:
Roland McGrathe16b2782008-04-20 13:10:12 -0700449 if (copy_from_user(&siginfo, (siginfo_t __user *) data,
450 sizeof siginfo))
451 ret = -EFAULT;
452 else
453 ret = ptrace_setsiginfo(child, &siginfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 break;
Roland McGrathe16b2782008-04-20 13:10:12 -0700455
Alexey Dobriyan1bcf5482007-10-16 01:23:45 -0700456 case PTRACE_DETACH: /* detach a process that was attached. */
457 ret = ptrace_detach(child, data);
458 break;
Roland McGrath36df29d2008-01-30 13:30:51 +0100459
460#ifdef PTRACE_SINGLESTEP
461 case PTRACE_SINGLESTEP:
462#endif
Roland McGrath5b88abb2008-01-30 13:30:53 +0100463#ifdef PTRACE_SINGLEBLOCK
464 case PTRACE_SINGLEBLOCK:
465#endif
Roland McGrath36df29d2008-01-30 13:30:51 +0100466#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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 default:
480 break;
481 }
482
483 return ret;
484}
Christoph Hellwig481bed42005-11-07 00:59:47 -0800485
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -0800486/**
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 */
492int ptrace_traceme(void)
Christoph Hellwig481bed42005-11-07 00:59:47 -0800493{
Linus Torvaldsf5b40e32006-05-07 10:49:33 -0700494 int ret = -EPERM;
Christoph Hellwig481bed42005-11-07 00:59:47 -0800495
496 /*
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -0800497 * Are we already being traced?
Christoph Hellwig481bed42005-11-07 00:59:47 -0800498 */
Roland McGrathf4700212008-03-24 18:36:23 -0700499repeat:
Linus Torvaldsf5b40e32006-05-07 10:49:33 -0700500 task_lock(current);
501 if (!(current->ptrace & PT_PTRACED)) {
Roland McGrathf4700212008-03-24 18:36:23 -0700502 /*
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
David Howells5cd9c582008-08-14 11:37:28 +0100514 ret = security_ptrace_traceme(current->parent);
Roland McGrathf4700212008-03-24 18:36:23 -0700515
Linus Torvaldsf5b40e32006-05-07 10:49:33 -0700516 /*
517 * Set the ptrace bit in the process ptrace flags.
Roland McGrathf4700212008-03-24 18:36:23 -0700518 * Then link us on our parent's ptraced list.
Linus Torvaldsf5b40e32006-05-07 10:49:33 -0700519 */
Roland McGrathf4700212008-03-24 18:36:23 -0700520 if (!ret) {
Linus Torvaldsf5b40e32006-05-07 10:49:33 -0700521 current->ptrace |= PT_PTRACED;
Roland McGrathf4700212008-03-24 18:36:23 -0700522 __ptrace_link(current, current->real_parent);
523 }
524
525 write_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvaldsf5b40e32006-05-07 10:49:33 -0700526 }
527 task_unlock(current);
528 return ret;
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -0800529}
Christoph Hellwig481bed42005-11-07 00:59:47 -0800530
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -0800531/**
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 */
541struct task_struct *ptrace_get_task_struct(pid_t pid)
542{
543 struct task_struct *child;
Christoph Hellwig481bed42005-11-07 00:59:47 -0800544
Christoph Hellwig481bed42005-11-07 00:59:47 -0800545 read_lock(&tasklist_lock);
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -0700546 child = find_task_by_vpid(pid);
Christoph Hellwig481bed42005-11-07 00:59:47 -0800547 if (child)
548 get_task_struct(child);
Sukadev Bhattiproluf400e192006-09-29 02:00:07 -0700549
Christoph Hellwig481bed42005-11-07 00:59:47 -0800550 read_unlock(&tasklist_lock);
551 if (!child)
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -0800552 return ERR_PTR(-ESRCH);
553 return child;
Christoph Hellwig481bed42005-11-07 00:59:47 -0800554}
555
Christoph Hellwig0ac15552007-10-16 01:26:37 -0700556#ifndef arch_ptrace_attach
557#define arch_ptrace_attach(child) do { } while (0)
558#endif
559
Christoph Hellwig481bed42005-11-07 00:59:47 -0800560asmlinkage 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();
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -0800569 if (request == PTRACE_TRACEME) {
570 ret = ptrace_traceme();
Haavard Skinnemoen6ea6dd92007-11-27 13:02:40 +0100571 if (!ret)
572 arch_ptrace_attach(current);
Christoph Hellwig481bed42005-11-07 00:59:47 -0800573 goto out;
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -0800574 }
575
576 child = ptrace_get_task_struct(pid);
577 if (IS_ERR(child)) {
578 ret = PTR_ERR(child);
579 goto out;
580 }
Christoph Hellwig481bed42005-11-07 00:59:47 -0800581
582 if (request == PTRACE_ATTACH) {
583 ret = ptrace_attach(child);
Christoph Hellwig0ac15552007-10-16 01:26:37 -0700584 /*
585 * Some architectures need to do book-keeping after
586 * a ptrace attach.
587 */
588 if (!ret)
589 arch_ptrace_attach(child);
Christoph Hellwig005f18d2005-11-13 16:06:33 -0800590 goto out_put_task_struct;
Christoph Hellwig481bed42005-11-07 00:59:47 -0800591 }
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}
Alexey Dobriyan76647322007-07-17 04:03:43 -0700607
608int 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}
Alexey Dobriyanf284ce72007-07-17 04:03:44 -0700618
619int 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}
Roland McGrath032d82d2008-01-30 13:31:47 +0100626
Christoph Hellwig96b89362008-11-25 08:10:03 +0100627#if defined CONFIG_COMPAT
Roland McGrath032d82d2008-01-30 13:31:47 +0100628#include <linux/compat.h>
629
630int 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;
Roland McGrathe16b2782008-04-20 13:10:12 -0700635 siginfo_t siginfo;
Roland McGrath032d82d2008-01-30 13:31:47 +0100636 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
Roland McGrathe16b2782008-04-20 13:10:12 -0700658 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
Roland McGrath032d82d2008-01-30 13:31:47 +0100675 default:
676 ret = ptrace_request(child, request, addr, data);
677 }
678
679 return ret;
680}
Roland McGrathc269f192008-01-30 13:31:48 +0100681
Roland McGrathc269f192008-01-30 13:31:48 +0100682asmlinkage 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}
Christoph Hellwig96b89362008-11-25 08:10:03 +0100724#endif /* CONFIG_COMPAT */