blob: 47b57b497d5529e570591e97a0f041e0d55d842e [file] [log] [blame]
Jeff Dikeba180fd2007-10-16 01:27:00 -07001/*
2 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include "linux/audit.h"
Jeff Dikeba180fd2007-10-16 01:27:00 -07007#include "linux/ptrace.h"
8#include "linux/sched.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include "asm/uaccess.h"
Jeff Dikeba180fd2007-10-16 01:27:00 -070010#ifdef CONFIG_PROC_MM
11#include "proc_mm.h"
12#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "skas_ptrace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Bodo Stroesser82c1c112005-05-06 21:30:46 -070015static inline void set_singlestepping(struct task_struct *child, int on)
16{
Jeff Dikeba180fd2007-10-16 01:27:00 -070017 if (on)
18 child->ptrace |= PT_DTRACE;
19 else
20 child->ptrace &= ~PT_DTRACE;
21 child->thread.singlestep_syscall = 0;
Bodo Stroesser82c1c112005-05-06 21:30:46 -070022
23#ifdef SUBARCH_SET_SINGLESTEPPING
Jeff Dikeba180fd2007-10-16 01:27:00 -070024 SUBARCH_SET_SINGLESTEPPING(child, on);
Bodo Stroesser82c1c112005-05-06 21:30:46 -070025#endif
Jeff Dikeba9950c2005-05-20 13:59:07 -070026}
Bodo Stroesser82c1c112005-05-06 21:30:46 -070027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028/*
29 * Called by kernel/ptrace.c when detaching..
30 */
31void ptrace_disable(struct task_struct *child)
Jeff Dikeba180fd2007-10-16 01:27:00 -070032{
33 set_singlestepping(child,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034}
35
Bodo Stroesser82c1c112005-05-06 21:30:46 -070036extern int peek_user(struct task_struct * child, long addr, long data);
37extern int poke_user(struct task_struct * child, long addr, long data);
38
Christoph Hellwig481bed42005-11-07 00:59:47 -080039long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 int i, ret;
Al Viro4d338e12006-03-31 02:30:15 -080042 unsigned long __user *p = (void __user *)(unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 switch (request) {
Jeff Dikeba180fd2007-10-16 01:27:00 -070045 /* read word at location addr. */
46 case PTRACE_PEEKTEXT:
Alexey Dobriyan76647322007-07-17 04:03:43 -070047 case PTRACE_PEEKDATA:
48 ret = generic_ptrace_peekdata(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51 /* read the word at location addr in the USER area. */
Jeff Dikeba180fd2007-10-16 01:27:00 -070052 case PTRACE_PEEKUSR:
53 ret = peek_user(child, addr, data);
54 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Jeff Dikeba180fd2007-10-16 01:27:00 -070056 /* write the word at location addr. */
57 case PTRACE_POKETEXT:
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 case PTRACE_POKEDATA:
Alexey Dobriyanf284ce72007-07-17 04:03:44 -070059 ret = generic_ptrace_pokedata(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 break;
61
Jeff Dikeba180fd2007-10-16 01:27:00 -070062 /* write the word at location addr in the USER area */
63 case PTRACE_POKEUSR:
64 ret = poke_user(child, addr, data);
65 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Jeff Dikeba180fd2007-10-16 01:27:00 -070067 /* continue and stop at next (return from) syscall */
68 case PTRACE_SYSCALL:
69 /* restart after signal. */
70 case PTRACE_CONT: {
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 ret = -EIO;
Jesper Juhl7ed20e12005-05-01 08:59:14 -070072 if (!valid_signal(data))
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 break;
74
Jeff Dikeba180fd2007-10-16 01:27:00 -070075 set_singlestepping(child, 0);
76 if (request == PTRACE_SYSCALL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
Jeff Dikeba180fd2007-10-16 01:27:00 -070078 else clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 child->exit_code = data;
80 wake_up_process(child);
81 ret = 0;
82 break;
83 }
84
85/*
Jeff Dikeba180fd2007-10-16 01:27:00 -070086 * make the child exit. Best I can do is send it a sigkill.
87 * perhaps it should be put in the status that it wants to
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * exit.
89 */
90 case PTRACE_KILL: {
91 ret = 0;
92 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
93 break;
94
Jeff Dikeba180fd2007-10-16 01:27:00 -070095 set_singlestepping(child, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 child->exit_code = SIGKILL;
97 wake_up_process(child);
98 break;
99 }
100
101 case PTRACE_SINGLESTEP: { /* set the trap flag. */
102 ret = -EIO;
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700103 if (!valid_signal(data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 break;
105 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700106 set_singlestepping(child, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 child->exit_code = data;
108 /* give it a chance to run. */
109 wake_up_process(child);
110 ret = 0;
111 break;
112 }
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#ifdef PTRACE_GETREGS
115 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
Al Viro4d338e12006-03-31 02:30:15 -0800116 if (!access_ok(VERIFY_WRITE, p, MAX_REG_OFFSET)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 ret = -EIO;
118 break;
119 }
120 for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
Al Viro4d338e12006-03-31 02:30:15 -0800121 __put_user(getreg(child, i), p);
122 p++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
124 ret = 0;
125 break;
126 }
127#endif
128#ifdef PTRACE_SETREGS
129 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
130 unsigned long tmp = 0;
Al Viro4d338e12006-03-31 02:30:15 -0800131 if (!access_ok(VERIFY_READ, p, MAX_REG_OFFSET)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 ret = -EIO;
133 break;
134 }
135 for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
Al Viro4d338e12006-03-31 02:30:15 -0800136 __get_user(tmp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 putreg(child, i, tmp);
Al Viro4d338e12006-03-31 02:30:15 -0800138 p++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 }
140 ret = 0;
141 break;
142 }
143#endif
144#ifdef PTRACE_GETFPREGS
145 case PTRACE_GETFPREGS: /* Get the child FPU state. */
Jeff Dikee8012b52007-10-16 01:27:16 -0700146 ret = get_fpregs((struct user_i387_struct __user *) data,
147 child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 break;
149#endif
150#ifdef PTRACE_SETFPREGS
151 case PTRACE_SETFPREGS: /* Set the child FPU state. */
Jeff Dikee8012b52007-10-16 01:27:16 -0700152 ret = set_fpregs((struct user_i387_struct __user *) data,
153 child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 break;
155#endif
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800156 case PTRACE_GET_THREAD_AREA:
157 ret = ptrace_get_thread_area(child, addr,
158 (struct user_desc __user *) data);
159 break;
160
161 case PTRACE_SET_THREAD_AREA:
162 ret = ptrace_set_thread_area(child, addr,
163 (struct user_desc __user *) data);
164 break;
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 case PTRACE_FAULTINFO: {
Jeff Dikeba180fd2007-10-16 01:27:00 -0700167 /*
168 * Take the info from thread->arch->faultinfo,
Al Viro4d338e12006-03-31 02:30:15 -0800169 * but transfer max. sizeof(struct ptrace_faultinfo).
170 * On i386, ptrace_faultinfo is smaller!
171 */
172 ret = copy_to_user(p, &child->thread.arch.faultinfo,
173 sizeof(struct ptrace_faultinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 break;
175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Bodo Stroesserc5784552005-05-05 16:15:31 -0700177#ifdef PTRACE_LDT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 case PTRACE_LDT: {
179 struct ptrace_ldt ldt;
180
Jeff Dikeba180fd2007-10-16 01:27:00 -0700181 if (copy_from_user(&ldt, p, sizeof(ldt))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 ret = -EIO;
183 break;
184 }
185
Jeff Dikeba180fd2007-10-16 01:27:00 -0700186 /*
187 * This one is confusing, so just punt and return -EIO for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 * now
189 */
190 ret = -EIO;
191 break;
192 }
Bodo Stroesserc5784552005-05-05 16:15:31 -0700193#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194#ifdef CONFIG_PROC_MM
195 case PTRACE_SWITCH_MM: {
196 struct mm_struct *old = child->mm;
197 struct mm_struct *new = proc_mm_get_mm(data);
198
Jeff Dikeba180fd2007-10-16 01:27:00 -0700199 if (IS_ERR(new)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 ret = PTR_ERR(new);
201 break;
202 }
203
204 atomic_inc(&new->mm_users);
205 child->mm = new;
206 child->active_mm = new;
207 mmput(old);
208 ret = 0;
209 break;
210 }
211#endif
Jeff Dike6e6d74c2007-02-10 01:44:30 -0800212#ifdef PTRACE_ARCH_PRCTL
Jeff Dikeba180fd2007-10-16 01:27:00 -0700213 case PTRACE_ARCH_PRCTL:
214 /* XXX Calls ptrace on the host - needs some SMP thinking */
215 ret = arch_prctl(child, data, (void *) addr);
216 break;
Jeff Dike6e6d74c2007-02-10 01:44:30 -0800217#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 default:
219 ret = ptrace_request(child, request, addr, data);
Jeff Dikee8012b52007-10-16 01:27:16 -0700220 if (ret == -EIO)
221 ret = subarch_ptrace(child, request, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 break;
223 }
Christoph Hellwig481bed42005-11-07 00:59:47 -0800224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return ret;
226}
227
Jeff Dike77bf4402007-10-16 01:26:58 -0700228void send_sigtrap(struct task_struct *tsk, struct uml_pt_regs *regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 int error_code)
230{
231 struct siginfo info;
232
233 memset(&info, 0, sizeof(info));
234 info.si_signo = SIGTRAP;
235 info.si_code = TRAP_BRKPT;
236
237 /* User-mode eip? */
238 info.si_addr = UPT_IS_USER(regs) ? (void __user *) UPT_IP(regs) : NULL;
239
Simon Arlottb60745b2007-10-20 01:23:03 +0200240 /* Send us the fake SIGTRAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 force_sig_info(SIGTRAP, &info, tsk);
242}
243
Jeff Dikeba180fd2007-10-16 01:27:00 -0700244/*
245 * XXX Check PT_DTRACE vs TIF_SINGLESTEP for singlestepping check and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 * PT_PTRACED vs TIF_SYSCALL_TRACE for syscall tracing check
247 */
Jeff Dike77bf4402007-10-16 01:26:58 -0700248void syscall_trace(struct uml_pt_regs *regs, int entryexit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 int is_singlestep = (current->ptrace & PT_DTRACE) && entryexit;
251 int tracesysgood;
252
253 if (unlikely(current->audit_context)) {
254 if (!entryexit)
Al Viro5411be52006-03-29 20:23:36 -0500255 audit_syscall_entry(HOST_AUDIT_ARCH,
Jeff Dike79d20b12005-05-03 07:54:51 +0100256 UPT_SYSCALL_NR(regs),
257 UPT_SYSCALL_ARG1(regs),
258 UPT_SYSCALL_ARG2(regs),
259 UPT_SYSCALL_ARG3(regs),
260 UPT_SYSCALL_ARG4(regs));
Al Viro5411be52006-03-29 20:23:36 -0500261 else audit_syscall_exit(AUDITSC_RESULT(UPT_SYSCALL_RET(regs)),
Jeff Dikeba180fd2007-10-16 01:27:00 -0700262 UPT_SYSCALL_RET(regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
264
265 /* Fake a debug trap */
266 if (is_singlestep)
267 send_sigtrap(current, regs, 0);
268
269 if (!test_thread_flag(TIF_SYSCALL_TRACE))
270 return;
271
272 if (!(current->ptrace & PT_PTRACED))
273 return;
274
Jeff Dikeba180fd2007-10-16 01:27:00 -0700275 /*
276 * the 0x80 provides a way for the tracing parent to distinguish
277 * between a syscall stop and SIGTRAP delivery
278 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 tracesysgood = (current->ptrace & PT_TRACESYSGOOD);
280 ptrace_notify(SIGTRAP | (tracesysgood ? 0x80 : 0));
281
282 if (entryexit) /* force do_signal() --> is_syscall() */
283 set_thread_flag(TIF_SIGPENDING);
284
Jeff Dikeba180fd2007-10-16 01:27:00 -0700285 /*
286 * this isn't the same as continuing with a signal, but it will do
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 * for normal use. strace only continues with a signal if the
288 * stopping signal is not SIGTRAP. -brl
289 */
290 if (current->exit_code) {
291 send_sig(current->exit_code, current, 1);
292 current->exit_code = 0;
293 }
294}