blob: a5e33f29bbeb7ef71e9a25697e5c8bfd3e664370 [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
Namhyung Kim9b05a692010-10-27 15:33:47 -070045long arch_ptrace(struct task_struct *child, long request,
46 unsigned long addr, unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 int i, ret;
Namhyung Kim9b05a692010-10-27 15:33:47 -070049 unsigned long __user *p = (void __user *)data;
Namhyung Kim0a3d7632010-10-27 15:34:04 -070050 void __user *vp = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 switch (request) {
Jeff Dikeba180fd2007-10-16 01:27:00 -070053 /* read word at location addr. */
54 case PTRACE_PEEKTEXT:
Alexey Dobriyan76647322007-07-17 04:03:43 -070055 case PTRACE_PEEKDATA:
56 ret = generic_ptrace_peekdata(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 /* read the word at location addr in the USER area. */
Jeff Dikeba180fd2007-10-16 01:27:00 -070060 case PTRACE_PEEKUSR:
61 ret = peek_user(child, addr, data);
62 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Jeff Dikeba180fd2007-10-16 01:27:00 -070064 /* write the word at location addr. */
65 case PTRACE_POKETEXT:
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 case PTRACE_POKEDATA:
Alexey Dobriyanf284ce72007-07-17 04:03:44 -070067 ret = generic_ptrace_pokedata(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 break;
69
Jeff Dikeba180fd2007-10-16 01:27:00 -070070 /* write the word at location addr in the USER area */
71 case PTRACE_POKEUSR:
72 ret = poke_user(child, addr, data);
73 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Renzo Davoli86d6f2b2009-03-12 14:31:23 -070075 case PTRACE_SYSEMU:
76 case PTRACE_SYSEMU_SINGLESTEP:
77 ret = -EIO;
78 break;
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#ifdef PTRACE_GETREGS
81 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
Al Viro4d338e12006-03-31 02:30:15 -080082 if (!access_ok(VERIFY_WRITE, p, MAX_REG_OFFSET)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 ret = -EIO;
84 break;
85 }
86 for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
Al Viro4d338e12006-03-31 02:30:15 -080087 __put_user(getreg(child, i), p);
88 p++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 }
90 ret = 0;
91 break;
92 }
93#endif
94#ifdef PTRACE_SETREGS
95 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
96 unsigned long tmp = 0;
Al Viro4d338e12006-03-31 02:30:15 -080097 if (!access_ok(VERIFY_READ, p, MAX_REG_OFFSET)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 ret = -EIO;
99 break;
100 }
101 for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
Al Viro4d338e12006-03-31 02:30:15 -0800102 __get_user(tmp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 putreg(child, i, tmp);
Al Viro4d338e12006-03-31 02:30:15 -0800104 p++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
106 ret = 0;
107 break;
108 }
109#endif
110#ifdef PTRACE_GETFPREGS
111 case PTRACE_GETFPREGS: /* Get the child FPU state. */
Namhyung Kim0a3d7632010-10-27 15:34:04 -0700112 ret = get_fpregs(vp, child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 break;
114#endif
115#ifdef PTRACE_SETFPREGS
116 case PTRACE_SETFPREGS: /* Set the child FPU state. */
Namhyung Kim0a3d7632010-10-27 15:34:04 -0700117 ret = set_fpregs(vp, 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:
Namhyung Kim0a3d7632010-10-27 15:34:04 -0700121 ret = ptrace_get_thread_area(child, addr, vp);
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800122 break;
123
124 case PTRACE_SET_THREAD_AREA:
Namhyung Kim0a3d7632010-10-27 15:34:04 -0700125 ret = ptrace_set_thread_area(child, addr, datavp);
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800126 break;
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 case PTRACE_FAULTINFO: {
Jeff Dikeba180fd2007-10-16 01:27:00 -0700129 /*
130 * Take the info from thread->arch->faultinfo,
Al Viro4d338e12006-03-31 02:30:15 -0800131 * but transfer max. sizeof(struct ptrace_faultinfo).
132 * On i386, ptrace_faultinfo is smaller!
133 */
134 ret = copy_to_user(p, &child->thread.arch.faultinfo,
Namhyung Kim0a3d7632010-10-27 15:34:04 -0700135 sizeof(struct ptrace_faultinfo)) ?
136 -EIO : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 break;
138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Bodo Stroesserc5784552005-05-05 16:15:31 -0700140#ifdef PTRACE_LDT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 case PTRACE_LDT: {
142 struct ptrace_ldt ldt;
143
Jeff Dikeba180fd2007-10-16 01:27:00 -0700144 if (copy_from_user(&ldt, p, sizeof(ldt))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 ret = -EIO;
146 break;
147 }
148
Jeff Dikeba180fd2007-10-16 01:27:00 -0700149 /*
150 * This one is confusing, so just punt and return -EIO for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 * now
152 */
153 ret = -EIO;
154 break;
155 }
Bodo Stroesserc5784552005-05-05 16:15:31 -0700156#endif
Jeff Dike6e6d74c2007-02-10 01:44:30 -0800157#ifdef PTRACE_ARCH_PRCTL
Jeff Dikeba180fd2007-10-16 01:27:00 -0700158 case PTRACE_ARCH_PRCTL:
159 /* XXX Calls ptrace on the host - needs some SMP thinking */
Namhyung Kim0a3d7632010-10-27 15:34:04 -0700160 ret = arch_prctl(child, data, (void __user *) addr);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700161 break;
Jeff Dike6e6d74c2007-02-10 01:44:30 -0800162#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 default:
164 ret = ptrace_request(child, request, addr, data);
Jeff Dikee8012b52007-10-16 01:27:16 -0700165 if (ret == -EIO)
166 ret = subarch_ptrace(child, request, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 break;
168 }
Christoph Hellwig481bed42005-11-07 00:59:47 -0800169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return ret;
171}
172
WANG Cong99764fa2008-07-23 21:28:49 -0700173static void send_sigtrap(struct task_struct *tsk, struct uml_pt_regs *regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 int error_code)
175{
176 struct siginfo info;
177
178 memset(&info, 0, sizeof(info));
179 info.si_signo = SIGTRAP;
180 info.si_code = TRAP_BRKPT;
181
182 /* User-mode eip? */
183 info.si_addr = UPT_IS_USER(regs) ? (void __user *) UPT_IP(regs) : NULL;
184
Simon Arlottb60745b2007-10-20 01:23:03 +0200185 /* Send us the fake SIGTRAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 force_sig_info(SIGTRAP, &info, tsk);
187}
188
Jeff Dikeba180fd2007-10-16 01:27:00 -0700189/*
190 * XXX Check PT_DTRACE vs TIF_SINGLESTEP for singlestepping check and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 * PT_PTRACED vs TIF_SYSCALL_TRACE for syscall tracing check
192 */
Jeff Dike77bf4402007-10-16 01:26:58 -0700193void syscall_trace(struct uml_pt_regs *regs, int entryexit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 int is_singlestep = (current->ptrace & PT_DTRACE) && entryexit;
196 int tracesysgood;
197
198 if (unlikely(current->audit_context)) {
199 if (!entryexit)
Al Viro5411be52006-03-29 20:23:36 -0500200 audit_syscall_entry(HOST_AUDIT_ARCH,
Jeff Dike79d20b12005-05-03 07:54:51 +0100201 UPT_SYSCALL_NR(regs),
202 UPT_SYSCALL_ARG1(regs),
203 UPT_SYSCALL_ARG2(regs),
204 UPT_SYSCALL_ARG3(regs),
205 UPT_SYSCALL_ARG4(regs));
Al Viro5411be52006-03-29 20:23:36 -0500206 else audit_syscall_exit(AUDITSC_RESULT(UPT_SYSCALL_RET(regs)),
Jeff Dikeba180fd2007-10-16 01:27:00 -0700207 UPT_SYSCALL_RET(regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
209
210 /* Fake a debug trap */
211 if (is_singlestep)
212 send_sigtrap(current, regs, 0);
213
214 if (!test_thread_flag(TIF_SYSCALL_TRACE))
215 return;
216
217 if (!(current->ptrace & PT_PTRACED))
218 return;
219
Jeff Dikeba180fd2007-10-16 01:27:00 -0700220 /*
221 * the 0x80 provides a way for the tracing parent to distinguish
222 * between a syscall stop and SIGTRAP delivery
223 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 tracesysgood = (current->ptrace & PT_TRACESYSGOOD);
225 ptrace_notify(SIGTRAP | (tracesysgood ? 0x80 : 0));
226
227 if (entryexit) /* force do_signal() --> is_syscall() */
228 set_thread_flag(TIF_SIGPENDING);
229
Jeff Dikeba180fd2007-10-16 01:27:00 -0700230 /*
231 * this isn't the same as continuing with a signal, but it will do
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 * for normal use. strace only continues with a signal if the
233 * stopping signal is not SIGTRAP. -brl
234 */
235 if (current->exit_code) {
236 send_sig(current->exit_code, current, 1);
237 current->exit_code = 0;
238 }
239}