blob: ffc599bd85c322ffee8313e2a2601e076a425e6b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2000 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
5
6#include "linux/sched.h"
7#include "linux/mm.h"
8#include "linux/errno.h"
9#include "linux/smp_lock.h"
10#include "linux/security.h"
11#include "linux/ptrace.h"
12#include "linux/audit.h"
13#ifdef CONFIG_PROC_MM
14#include "linux/proc_mm.h"
15#endif
16#include "asm/ptrace.h"
17#include "asm/uaccess.h"
18#include "kern_util.h"
19#include "skas_ptrace.h"
20#include "sysdep/ptrace.h"
21
22/*
23 * Called by kernel/ptrace.c when detaching..
24 */
25void ptrace_disable(struct task_struct *child)
26{
27 child->ptrace &= ~PT_DTRACE;
28 child->thread.singlestep_syscall = 0;
29}
30
31long sys_ptrace(long request, long pid, long addr, long data)
32{
33 struct task_struct *child;
34 int i, ret;
35
36 lock_kernel();
37 ret = -EPERM;
38 if (request == PTRACE_TRACEME) {
39 /* are we already being traced? */
40 if (current->ptrace & PT_PTRACED)
41 goto out;
42
43 ret = security_ptrace(current->parent, current);
44 if (ret)
45 goto out;
46
47 /* set the ptrace bit in the process flags. */
48 current->ptrace |= PT_PTRACED;
49 ret = 0;
50 goto out;
51 }
52 ret = -ESRCH;
53 read_lock(&tasklist_lock);
54 child = find_task_by_pid(pid);
55 if (child)
56 get_task_struct(child);
57 read_unlock(&tasklist_lock);
58 if (!child)
59 goto out;
60
61 ret = -EPERM;
62 if (pid == 1) /* you may not mess with init */
63 goto out_tsk;
64
65 if (request == PTRACE_ATTACH) {
66 ret = ptrace_attach(child);
67 goto out_tsk;
68 }
69
70 ret = ptrace_check_attach(child, request == PTRACE_KILL);
71 if (ret < 0)
72 goto out_tsk;
73
74 switch (request) {
75 /* when I and D space are separate, these will need to be fixed. */
76 case PTRACE_PEEKTEXT: /* read word at location addr. */
77 case PTRACE_PEEKDATA: {
78 unsigned long tmp;
79 int copied;
80
81 ret = -EIO;
82 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
83 if (copied != sizeof(tmp))
84 break;
85 ret = put_user(tmp, (unsigned long __user *) data);
86 break;
87 }
88
89 /* read the word at location addr in the USER area. */
90 case PTRACE_PEEKUSR: {
91 unsigned long tmp;
92
93 ret = -EIO;
94 if ((addr & 3) || addr < 0)
95 break;
96
97 tmp = 0; /* Default return condition */
98 if(addr < MAX_REG_OFFSET){
99 tmp = getreg(child, addr);
100 }
Paolo 'Blaisorblade' Giarrussof7fe8782005-05-05 16:15:15 -0700101#if defined(CONFIG_UML_X86) && !defined(CONFIG_64BIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 else if((addr >= offsetof(struct user, u_debugreg[0])) &&
103 (addr <= offsetof(struct user, u_debugreg[7]))){
104 addr -= offsetof(struct user, u_debugreg[0]);
105 addr = addr >> 2;
106 tmp = child->thread.arch.debugregs[addr];
107 }
Paolo 'Blaisorblade' Giarrussof7fe8782005-05-05 16:15:15 -0700108#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 ret = put_user(tmp, (unsigned long __user *) data);
110 break;
111 }
112
113 /* when I and D space are separate, this will have to be fixed. */
114 case PTRACE_POKETEXT: /* write the word at location addr. */
115 case PTRACE_POKEDATA:
116 ret = -EIO;
117 if (access_process_vm(child, addr, &data, sizeof(data),
118 1) != sizeof(data))
119 break;
120 ret = 0;
121 break;
122
123 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
124 ret = -EIO;
125 if ((addr & 3) || addr < 0)
126 break;
127
128 if (addr < MAX_REG_OFFSET) {
129 ret = putreg(child, addr, data);
130 break;
131 }
Paolo 'Blaisorblade' Giarrussof7fe8782005-05-05 16:15:15 -0700132#if defined(CONFIG_UML_X86) && !defined(CONFIG_64BIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 else if((addr >= offsetof(struct user, u_debugreg[0])) &&
134 (addr <= offsetof(struct user, u_debugreg[7]))){
135 addr -= offsetof(struct user, u_debugreg[0]);
136 addr = addr >> 2;
137 if((addr == 4) || (addr == 5)) break;
138 child->thread.arch.debugregs[addr] = data;
139 ret = 0;
140 }
141#endif
142
143 break;
144
145 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
146 case PTRACE_CONT: { /* restart after signal. */
147 ret = -EIO;
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700148 if (!valid_signal(data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 break;
150
151 child->ptrace &= ~PT_DTRACE;
152 child->thread.singlestep_syscall = 0;
153 if (request == PTRACE_SYSCALL) {
154 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
155 }
156 else {
157 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
158 }
159 child->exit_code = data;
160 wake_up_process(child);
161 ret = 0;
162 break;
163 }
164
165/*
166 * make the child exit. Best I can do is send it a sigkill.
167 * perhaps it should be put in the status that it wants to
168 * exit.
169 */
170 case PTRACE_KILL: {
171 ret = 0;
172 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
173 break;
174
175 child->ptrace &= ~PT_DTRACE;
176 child->thread.singlestep_syscall = 0;
177 child->exit_code = SIGKILL;
178 wake_up_process(child);
179 break;
180 }
181
182 case PTRACE_SINGLESTEP: { /* set the trap flag. */
183 ret = -EIO;
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700184 if (!valid_signal(data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 break;
186 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
187 child->ptrace |= PT_DTRACE;
188 child->thread.singlestep_syscall = 0;
189 child->exit_code = data;
190 /* give it a chance to run. */
191 wake_up_process(child);
192 ret = 0;
193 break;
194 }
195
196 case PTRACE_DETACH:
197 /* detach a process that was attached. */
198 ret = ptrace_detach(child, data);
199 break;
200
201#ifdef PTRACE_GETREGS
202 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
203 if (!access_ok(VERIFY_WRITE, (unsigned long *)data,
204 MAX_REG_OFFSET)) {
205 ret = -EIO;
206 break;
207 }
208 for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
209 __put_user(getreg(child, i),
210 (unsigned long __user *) data);
211 data += sizeof(long);
212 }
213 ret = 0;
214 break;
215 }
216#endif
217#ifdef PTRACE_SETREGS
218 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
219 unsigned long tmp = 0;
220 if (!access_ok(VERIFY_READ, (unsigned *)data,
221 MAX_REG_OFFSET)) {
222 ret = -EIO;
223 break;
224 }
225 for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
226 __get_user(tmp, (unsigned long __user *) data);
227 putreg(child, i, tmp);
228 data += sizeof(long);
229 }
230 ret = 0;
231 break;
232 }
233#endif
234#ifdef PTRACE_GETFPREGS
235 case PTRACE_GETFPREGS: /* Get the child FPU state. */
236 ret = get_fpregs(data, child);
237 break;
238#endif
239#ifdef PTRACE_SETFPREGS
240 case PTRACE_SETFPREGS: /* Set the child FPU state. */
241 ret = set_fpregs(data, child);
242 break;
243#endif
244#ifdef PTRACE_GETFPXREGS
245 case PTRACE_GETFPXREGS: /* Get the child FPU state. */
246 ret = get_fpxregs(data, child);
247 break;
248#endif
249#ifdef PTRACE_SETFPXREGS
250 case PTRACE_SETFPXREGS: /* Set the child FPU state. */
251 ret = set_fpxregs(data, child);
252 break;
253#endif
254 case PTRACE_FAULTINFO: {
255 struct ptrace_faultinfo fault;
256
257 fault = ((struct ptrace_faultinfo)
258 { .is_write = child->thread.err,
259 .addr = child->thread.cr2 });
260 ret = copy_to_user((unsigned long __user *) data, &fault,
261 sizeof(fault));
262 if(ret)
263 break;
264 break;
265 }
266 case PTRACE_SIGPENDING:
267 ret = copy_to_user((unsigned long __user *) data,
268 &child->pending.signal,
269 sizeof(child->pending.signal));
270 break;
271
272 case PTRACE_LDT: {
273 struct ptrace_ldt ldt;
274
275 if(copy_from_user(&ldt, (unsigned long __user *) data,
276 sizeof(ldt))){
277 ret = -EIO;
278 break;
279 }
280
281 /* This one is confusing, so just punt and return -EIO for
282 * now
283 */
284 ret = -EIO;
285 break;
286 }
287#ifdef CONFIG_PROC_MM
288 case PTRACE_SWITCH_MM: {
289 struct mm_struct *old = child->mm;
290 struct mm_struct *new = proc_mm_get_mm(data);
291
292 if(IS_ERR(new)){
293 ret = PTR_ERR(new);
294 break;
295 }
296
297 atomic_inc(&new->mm_users);
298 child->mm = new;
299 child->active_mm = new;
300 mmput(old);
301 ret = 0;
302 break;
303 }
304#endif
305 default:
306 ret = ptrace_request(child, request, addr, data);
307 break;
308 }
309 out_tsk:
310 put_task_struct(child);
311 out:
312 unlock_kernel();
313 return ret;
314}
315
316void send_sigtrap(struct task_struct *tsk, union uml_pt_regs *regs,
317 int error_code)
318{
319 struct siginfo info;
320
321 memset(&info, 0, sizeof(info));
322 info.si_signo = SIGTRAP;
323 info.si_code = TRAP_BRKPT;
324
325 /* User-mode eip? */
326 info.si_addr = UPT_IS_USER(regs) ? (void __user *) UPT_IP(regs) : NULL;
327
328 /* Send us the fakey SIGTRAP */
329 force_sig_info(SIGTRAP, &info, tsk);
330}
331
332/* XXX Check PT_DTRACE vs TIF_SINGLESTEP for singlestepping check and
333 * PT_PTRACED vs TIF_SYSCALL_TRACE for syscall tracing check
334 */
335void syscall_trace(union uml_pt_regs *regs, int entryexit)
336{
337 int is_singlestep = (current->ptrace & PT_DTRACE) && entryexit;
338 int tracesysgood;
339
340 if (unlikely(current->audit_context)) {
341 if (!entryexit)
Jeff Dike79d20b12005-05-03 07:54:51 +0100342 audit_syscall_entry(current,
343 HOST_AUDIT_ARCH,
344 UPT_SYSCALL_NR(regs),
345 UPT_SYSCALL_ARG1(regs),
346 UPT_SYSCALL_ARG2(regs),
347 UPT_SYSCALL_ARG3(regs),
348 UPT_SYSCALL_ARG4(regs));
349 else {
350 int res = UPT_SYSCALL_RET(regs);
351 audit_syscall_exit(current, AUDITSC_RESULT(res),
352 res);
353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
355
356 /* Fake a debug trap */
357 if (is_singlestep)
358 send_sigtrap(current, regs, 0);
359
360 if (!test_thread_flag(TIF_SYSCALL_TRACE))
361 return;
362
363 if (!(current->ptrace & PT_PTRACED))
364 return;
365
366 /* the 0x80 provides a way for the tracing parent to distinguish
367 between a syscall stop and SIGTRAP delivery */
368 tracesysgood = (current->ptrace & PT_TRACESYSGOOD);
369 ptrace_notify(SIGTRAP | (tracesysgood ? 0x80 : 0));
370
371 if (entryexit) /* force do_signal() --> is_syscall() */
372 set_thread_flag(TIF_SIGPENDING);
373
374 /* this isn't the same as continuing with a signal, but it will do
375 * for normal use. strace only continues with a signal if the
376 * stopping signal is not SIGTRAP. -brl
377 */
378 if (current->exit_code) {
379 send_sig(current->exit_code, current, 1);
380 current->exit_code = 0;
381 }
382}
383
384/*
385 * Overrides for Emacs so that we follow Linus's tabbing style.
386 * Emacs will notice this stuff at the end of the file and automatically
387 * adjust the settings for this buffer only. This must remain at the end
388 * of the file.
389 * ---------------------------------------------------------------------------
390 * Local variables:
391 * c-file-style: "linux"
392 * End:
393 */