blob: e0510496596c9a047f3fd19860716906caf14f69 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include "skas_ptrace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
Christoph Hellwig1bd09502010-03-10 15:22:56 -080012
13
14void user_enable_single_step(struct task_struct *child)
Bodo Stroesser82c1c112005-05-06 21:30:46 -070015{
Christoph Hellwig1bd09502010-03-10 15:22:56 -080016 child->ptrace |= PT_DTRACE;
Jeff Dikeba180fd2007-10-16 01:27:00 -070017 child->thread.singlestep_syscall = 0;
Bodo Stroesser82c1c112005-05-06 21:30:46 -070018
19#ifdef SUBARCH_SET_SINGLESTEPPING
Christoph Hellwig1bd09502010-03-10 15:22:56 -080020 SUBARCH_SET_SINGLESTEPPING(child, 1);
21#endif
22}
23
24void user_disable_single_step(struct task_struct *child)
25{
26 child->ptrace &= ~PT_DTRACE;
27 child->thread.singlestep_syscall = 0;
28
29#ifdef SUBARCH_SET_SINGLESTEPPING
30 SUBARCH_SET_SINGLESTEPPING(child, 0);
Bodo Stroesser82c1c112005-05-06 21:30:46 -070031#endif
Jeff Dikeba9950c2005-05-20 13:59:07 -070032}
Bodo Stroesser82c1c112005-05-06 21:30:46 -070033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/*
35 * Called by kernel/ptrace.c when detaching..
36 */
37void ptrace_disable(struct task_struct *child)
Jeff Dikeba180fd2007-10-16 01:27:00 -070038{
Christoph Hellwig1bd09502010-03-10 15:22:56 -080039 user_disable_single_step(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040}
41
Bodo Stroesser82c1c112005-05-06 21:30:46 -070042extern int peek_user(struct task_struct * child, long addr, long data);
43extern int poke_user(struct task_struct * child, long addr, long data);
44
Christoph Hellwig481bed42005-11-07 00:59:47 -080045long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 int i, ret;
Al Viro4d338e12006-03-31 02:30:15 -080048 unsigned long __user *p = (void __user *)(unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 switch (request) {
Jeff Dikeba180fd2007-10-16 01:27:00 -070051 /* read word at location addr. */
52 case PTRACE_PEEKTEXT:
Alexey Dobriyan76647322007-07-17 04:03:43 -070053 case PTRACE_PEEKDATA:
54 ret = generic_ptrace_peekdata(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57 /* read the word at location addr in the USER area. */
Jeff Dikeba180fd2007-10-16 01:27:00 -070058 case PTRACE_PEEKUSR:
59 ret = peek_user(child, addr, data);
60 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Jeff Dikeba180fd2007-10-16 01:27:00 -070062 /* write the word at location addr. */
63 case PTRACE_POKETEXT:
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 case PTRACE_POKEDATA:
Alexey Dobriyanf284ce72007-07-17 04:03:44 -070065 ret = generic_ptrace_pokedata(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 break;
67
Jeff Dikeba180fd2007-10-16 01:27:00 -070068 /* write the word at location addr in the USER area */
69 case PTRACE_POKEUSR:
70 ret = poke_user(child, addr, data);
71 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Renzo Davoli86d6f2b2009-03-12 14:31:23 -070073 case PTRACE_SYSEMU:
74 case PTRACE_SYSEMU_SINGLESTEP:
75 ret = -EIO;
76 break;
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#ifdef PTRACE_GETREGS
79 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
Al Viro4d338e12006-03-31 02:30:15 -080080 if (!access_ok(VERIFY_WRITE, p, MAX_REG_OFFSET)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 ret = -EIO;
82 break;
83 }
84 for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
Al Viro4d338e12006-03-31 02:30:15 -080085 __put_user(getreg(child, i), p);
86 p++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 }
88 ret = 0;
89 break;
90 }
91#endif
92#ifdef PTRACE_SETREGS
93 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
94 unsigned long tmp = 0;
Al Viro4d338e12006-03-31 02:30:15 -080095 if (!access_ok(VERIFY_READ, p, MAX_REG_OFFSET)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 ret = -EIO;
97 break;
98 }
99 for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
Al Viro4d338e12006-03-31 02:30:15 -0800100 __get_user(tmp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 putreg(child, i, tmp);
Al Viro4d338e12006-03-31 02:30:15 -0800102 p++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
104 ret = 0;
105 break;
106 }
107#endif
108#ifdef PTRACE_GETFPREGS
109 case PTRACE_GETFPREGS: /* Get the child FPU state. */
Jeff Dikee8012b52007-10-16 01:27:16 -0700110 ret = get_fpregs((struct user_i387_struct __user *) data,
111 child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 break;
113#endif
114#ifdef PTRACE_SETFPREGS
115 case PTRACE_SETFPREGS: /* Set the child FPU state. */
Jeff Dikee8012b52007-10-16 01:27:16 -0700116 ret = set_fpregs((struct user_i387_struct __user *) data,
117 child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 break;
119#endif
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800120 case PTRACE_GET_THREAD_AREA:
121 ret = ptrace_get_thread_area(child, addr,
122 (struct user_desc __user *) data);
123 break;
124
125 case PTRACE_SET_THREAD_AREA:
126 ret = ptrace_set_thread_area(child, addr,
127 (struct user_desc __user *) data);
128 break;
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 case PTRACE_FAULTINFO: {
Jeff Dikeba180fd2007-10-16 01:27:00 -0700131 /*
132 * Take the info from thread->arch->faultinfo,
Al Viro4d338e12006-03-31 02:30:15 -0800133 * but transfer max. sizeof(struct ptrace_faultinfo).
134 * On i386, ptrace_faultinfo is smaller!
135 */
136 ret = copy_to_user(p, &child->thread.arch.faultinfo,
137 sizeof(struct ptrace_faultinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 break;
139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Bodo Stroesserc5784552005-05-05 16:15:31 -0700141#ifdef PTRACE_LDT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 case PTRACE_LDT: {
143 struct ptrace_ldt ldt;
144
Jeff Dikeba180fd2007-10-16 01:27:00 -0700145 if (copy_from_user(&ldt, p, sizeof(ldt))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 ret = -EIO;
147 break;
148 }
149
Jeff Dikeba180fd2007-10-16 01:27:00 -0700150 /*
151 * This one is confusing, so just punt and return -EIO for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 * now
153 */
154 ret = -EIO;
155 break;
156 }
Bodo Stroesserc5784552005-05-05 16:15:31 -0700157#endif
Jeff Dike6e6d74c2007-02-10 01:44:30 -0800158#ifdef PTRACE_ARCH_PRCTL
Jeff Dikeba180fd2007-10-16 01:27:00 -0700159 case PTRACE_ARCH_PRCTL:
160 /* XXX Calls ptrace on the host - needs some SMP thinking */
161 ret = arch_prctl(child, data, (void *) addr);
162 break;
Jeff Dike6e6d74c2007-02-10 01:44:30 -0800163#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 default:
165 ret = ptrace_request(child, request, addr, data);
Jeff Dikee8012b52007-10-16 01:27:16 -0700166 if (ret == -EIO)
167 ret = subarch_ptrace(child, request, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 break;
169 }
Christoph Hellwig481bed42005-11-07 00:59:47 -0800170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return ret;
172}
173
WANG Cong99764fa2008-07-23 21:28:49 -0700174static void send_sigtrap(struct task_struct *tsk, struct uml_pt_regs *regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 int error_code)
176{
177 struct siginfo info;
178
179 memset(&info, 0, sizeof(info));
180 info.si_signo = SIGTRAP;
181 info.si_code = TRAP_BRKPT;
182
183 /* User-mode eip? */
184 info.si_addr = UPT_IS_USER(regs) ? (void __user *) UPT_IP(regs) : NULL;
185
Simon Arlottb60745b2007-10-20 01:23:03 +0200186 /* Send us the fake SIGTRAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 force_sig_info(SIGTRAP, &info, tsk);
188}
189
Jeff Dikeba180fd2007-10-16 01:27:00 -0700190/*
191 * XXX Check PT_DTRACE vs TIF_SINGLESTEP for singlestepping check and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 * PT_PTRACED vs TIF_SYSCALL_TRACE for syscall tracing check
193 */
Jeff Dike77bf4402007-10-16 01:26:58 -0700194void syscall_trace(struct uml_pt_regs *regs, int entryexit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 int is_singlestep = (current->ptrace & PT_DTRACE) && entryexit;
197 int tracesysgood;
198
199 if (unlikely(current->audit_context)) {
200 if (!entryexit)
Al Viro5411be52006-03-29 20:23:36 -0500201 audit_syscall_entry(HOST_AUDIT_ARCH,
Jeff Dike79d20b12005-05-03 07:54:51 +0100202 UPT_SYSCALL_NR(regs),
203 UPT_SYSCALL_ARG1(regs),
204 UPT_SYSCALL_ARG2(regs),
205 UPT_SYSCALL_ARG3(regs),
206 UPT_SYSCALL_ARG4(regs));
Al Viro5411be52006-03-29 20:23:36 -0500207 else audit_syscall_exit(AUDITSC_RESULT(UPT_SYSCALL_RET(regs)),
Jeff Dikeba180fd2007-10-16 01:27:00 -0700208 UPT_SYSCALL_RET(regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210
211 /* Fake a debug trap */
212 if (is_singlestep)
213 send_sigtrap(current, regs, 0);
214
215 if (!test_thread_flag(TIF_SYSCALL_TRACE))
216 return;
217
218 if (!(current->ptrace & PT_PTRACED))
219 return;
220
Jeff Dikeba180fd2007-10-16 01:27:00 -0700221 /*
222 * the 0x80 provides a way for the tracing parent to distinguish
223 * between a syscall stop and SIGTRAP delivery
224 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 tracesysgood = (current->ptrace & PT_TRACESYSGOOD);
226 ptrace_notify(SIGTRAP | (tracesysgood ? 0x80 : 0));
227
228 if (entryexit) /* force do_signal() --> is_syscall() */
229 set_thread_flag(TIF_SIGPENDING);
230
Jeff Dikeba180fd2007-10-16 01:27:00 -0700231 /*
232 * this isn't the same as continuing with a signal, but it will do
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 * for normal use. strace only continues with a signal if the
234 * stopping signal is not SIGTRAP. -brl
235 */
236 if (current->exit_code) {
237 send_sig(current->exit_code, current, 1);
238 current->exit_code = 0;
239 }
240}