blob: 484509948ee96696183f2d4ef535c98a3d93c10e [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
Christoph Hellwig1bd09502010-03-10 15:22:56 -080015
16
17void user_enable_single_step(struct task_struct *child)
Bodo Stroesser82c1c112005-05-06 21:30:46 -070018{
Christoph Hellwig1bd09502010-03-10 15:22:56 -080019 child->ptrace |= PT_DTRACE;
Jeff Dikeba180fd2007-10-16 01:27:00 -070020 child->thread.singlestep_syscall = 0;
Bodo Stroesser82c1c112005-05-06 21:30:46 -070021
22#ifdef SUBARCH_SET_SINGLESTEPPING
Christoph Hellwig1bd09502010-03-10 15:22:56 -080023 SUBARCH_SET_SINGLESTEPPING(child, 1);
24#endif
25}
26
27void user_disable_single_step(struct task_struct *child)
28{
29 child->ptrace &= ~PT_DTRACE;
30 child->thread.singlestep_syscall = 0;
31
32#ifdef SUBARCH_SET_SINGLESTEPPING
33 SUBARCH_SET_SINGLESTEPPING(child, 0);
Bodo Stroesser82c1c112005-05-06 21:30:46 -070034#endif
Jeff Dikeba9950c2005-05-20 13:59:07 -070035}
Bodo Stroesser82c1c112005-05-06 21:30:46 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/*
38 * Called by kernel/ptrace.c when detaching..
39 */
40void ptrace_disable(struct task_struct *child)
Jeff Dikeba180fd2007-10-16 01:27:00 -070041{
Christoph Hellwig1bd09502010-03-10 15:22:56 -080042 user_disable_single_step(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043}
44
Bodo Stroesser82c1c112005-05-06 21:30:46 -070045extern int peek_user(struct task_struct * child, long addr, long data);
46extern int poke_user(struct task_struct * child, long addr, long data);
47
Christoph Hellwig481bed42005-11-07 00:59:47 -080048long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 int i, ret;
Al Viro4d338e12006-03-31 02:30:15 -080051 unsigned long __user *p = (void __user *)(unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 switch (request) {
Jeff Dikeba180fd2007-10-16 01:27:00 -070054 /* read word at location addr. */
55 case PTRACE_PEEKTEXT:
Alexey Dobriyan76647322007-07-17 04:03:43 -070056 case PTRACE_PEEKDATA:
57 ret = generic_ptrace_peekdata(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60 /* read the word at location addr in the USER area. */
Jeff Dikeba180fd2007-10-16 01:27:00 -070061 case PTRACE_PEEKUSR:
62 ret = peek_user(child, addr, data);
63 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Jeff Dikeba180fd2007-10-16 01:27:00 -070065 /* write the word at location addr. */
66 case PTRACE_POKETEXT:
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 case PTRACE_POKEDATA:
Alexey Dobriyanf284ce72007-07-17 04:03:44 -070068 ret = generic_ptrace_pokedata(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 break;
70
Jeff Dikeba180fd2007-10-16 01:27:00 -070071 /* write the word at location addr in the USER area */
72 case PTRACE_POKEUSR:
73 ret = poke_user(child, addr, data);
74 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Renzo Davoli86d6f2b2009-03-12 14:31:23 -070076 case PTRACE_SYSEMU:
77 case PTRACE_SYSEMU_SINGLESTEP:
78 ret = -EIO;
79 break;
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#ifdef PTRACE_GETREGS
82 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
Al Viro4d338e12006-03-31 02:30:15 -080083 if (!access_ok(VERIFY_WRITE, p, MAX_REG_OFFSET)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 ret = -EIO;
85 break;
86 }
87 for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
Al Viro4d338e12006-03-31 02:30:15 -080088 __put_user(getreg(child, i), p);
89 p++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 }
91 ret = 0;
92 break;
93 }
94#endif
95#ifdef PTRACE_SETREGS
96 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
97 unsigned long tmp = 0;
Al Viro4d338e12006-03-31 02:30:15 -080098 if (!access_ok(VERIFY_READ, p, MAX_REG_OFFSET)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 ret = -EIO;
100 break;
101 }
102 for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
Al Viro4d338e12006-03-31 02:30:15 -0800103 __get_user(tmp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 putreg(child, i, tmp);
Al Viro4d338e12006-03-31 02:30:15 -0800105 p++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
107 ret = 0;
108 break;
109 }
110#endif
111#ifdef PTRACE_GETFPREGS
112 case PTRACE_GETFPREGS: /* Get the child FPU state. */
Jeff Dikee8012b52007-10-16 01:27:16 -0700113 ret = get_fpregs((struct user_i387_struct __user *) data,
114 child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 break;
116#endif
117#ifdef PTRACE_SETFPREGS
118 case PTRACE_SETFPREGS: /* Set the child FPU state. */
Jeff Dikee8012b52007-10-16 01:27:16 -0700119 ret = set_fpregs((struct user_i387_struct __user *) data,
120 child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 break;
122#endif
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800123 case PTRACE_GET_THREAD_AREA:
124 ret = ptrace_get_thread_area(child, addr,
125 (struct user_desc __user *) data);
126 break;
127
128 case PTRACE_SET_THREAD_AREA:
129 ret = ptrace_set_thread_area(child, addr,
130 (struct user_desc __user *) data);
131 break;
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 case PTRACE_FAULTINFO: {
Jeff Dikeba180fd2007-10-16 01:27:00 -0700134 /*
135 * Take the info from thread->arch->faultinfo,
Al Viro4d338e12006-03-31 02:30:15 -0800136 * but transfer max. sizeof(struct ptrace_faultinfo).
137 * On i386, ptrace_faultinfo is smaller!
138 */
139 ret = copy_to_user(p, &child->thread.arch.faultinfo,
140 sizeof(struct ptrace_faultinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 break;
142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Bodo Stroesserc5784552005-05-05 16:15:31 -0700144#ifdef PTRACE_LDT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 case PTRACE_LDT: {
146 struct ptrace_ldt ldt;
147
Jeff Dikeba180fd2007-10-16 01:27:00 -0700148 if (copy_from_user(&ldt, p, sizeof(ldt))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 ret = -EIO;
150 break;
151 }
152
Jeff Dikeba180fd2007-10-16 01:27:00 -0700153 /*
154 * This one is confusing, so just punt and return -EIO for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 * now
156 */
157 ret = -EIO;
158 break;
159 }
Bodo Stroesserc5784552005-05-05 16:15:31 -0700160#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161#ifdef CONFIG_PROC_MM
162 case PTRACE_SWITCH_MM: {
163 struct mm_struct *old = child->mm;
164 struct mm_struct *new = proc_mm_get_mm(data);
165
Jeff Dikeba180fd2007-10-16 01:27:00 -0700166 if (IS_ERR(new)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 ret = PTR_ERR(new);
168 break;
169 }
170
171 atomic_inc(&new->mm_users);
172 child->mm = new;
173 child->active_mm = new;
174 mmput(old);
175 ret = 0;
176 break;
177 }
178#endif
Jeff Dike6e6d74c2007-02-10 01:44:30 -0800179#ifdef PTRACE_ARCH_PRCTL
Jeff Dikeba180fd2007-10-16 01:27:00 -0700180 case PTRACE_ARCH_PRCTL:
181 /* XXX Calls ptrace on the host - needs some SMP thinking */
182 ret = arch_prctl(child, data, (void *) addr);
183 break;
Jeff Dike6e6d74c2007-02-10 01:44:30 -0800184#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 default:
186 ret = ptrace_request(child, request, addr, data);
Jeff Dikee8012b52007-10-16 01:27:16 -0700187 if (ret == -EIO)
188 ret = subarch_ptrace(child, request, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 break;
190 }
Christoph Hellwig481bed42005-11-07 00:59:47 -0800191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return ret;
193}
194
WANG Cong99764fa2008-07-23 21:28:49 -0700195static void send_sigtrap(struct task_struct *tsk, struct uml_pt_regs *regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 int error_code)
197{
198 struct siginfo info;
199
200 memset(&info, 0, sizeof(info));
201 info.si_signo = SIGTRAP;
202 info.si_code = TRAP_BRKPT;
203
204 /* User-mode eip? */
205 info.si_addr = UPT_IS_USER(regs) ? (void __user *) UPT_IP(regs) : NULL;
206
Simon Arlottb60745b2007-10-20 01:23:03 +0200207 /* Send us the fake SIGTRAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 force_sig_info(SIGTRAP, &info, tsk);
209}
210
Jeff Dikeba180fd2007-10-16 01:27:00 -0700211/*
212 * XXX Check PT_DTRACE vs TIF_SINGLESTEP for singlestepping check and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 * PT_PTRACED vs TIF_SYSCALL_TRACE for syscall tracing check
214 */
Jeff Dike77bf4402007-10-16 01:26:58 -0700215void syscall_trace(struct uml_pt_regs *regs, int entryexit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 int is_singlestep = (current->ptrace & PT_DTRACE) && entryexit;
218 int tracesysgood;
219
220 if (unlikely(current->audit_context)) {
221 if (!entryexit)
Al Viro5411be52006-03-29 20:23:36 -0500222 audit_syscall_entry(HOST_AUDIT_ARCH,
Jeff Dike79d20b12005-05-03 07:54:51 +0100223 UPT_SYSCALL_NR(regs),
224 UPT_SYSCALL_ARG1(regs),
225 UPT_SYSCALL_ARG2(regs),
226 UPT_SYSCALL_ARG3(regs),
227 UPT_SYSCALL_ARG4(regs));
Al Viro5411be52006-03-29 20:23:36 -0500228 else audit_syscall_exit(AUDITSC_RESULT(UPT_SYSCALL_RET(regs)),
Jeff Dikeba180fd2007-10-16 01:27:00 -0700229 UPT_SYSCALL_RET(regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
231
232 /* Fake a debug trap */
233 if (is_singlestep)
234 send_sigtrap(current, regs, 0);
235
236 if (!test_thread_flag(TIF_SYSCALL_TRACE))
237 return;
238
239 if (!(current->ptrace & PT_PTRACED))
240 return;
241
Jeff Dikeba180fd2007-10-16 01:27:00 -0700242 /*
243 * the 0x80 provides a way for the tracing parent to distinguish
244 * between a syscall stop and SIGTRAP delivery
245 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 tracesysgood = (current->ptrace & PT_TRACESYSGOOD);
247 ptrace_notify(SIGTRAP | (tracesysgood ? 0x80 : 0));
248
249 if (entryexit) /* force do_signal() --> is_syscall() */
250 set_thread_flag(TIF_SIGPENDING);
251
Jeff Dikeba180fd2007-10-16 01:27:00 -0700252 /*
253 * this isn't the same as continuing with a signal, but it will do
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 * for normal use. strace only continues with a signal if the
255 * stopping signal is not SIGTRAP. -brl
256 */
257 if (current->exit_code) {
258 send_sig(current->exit_code, current, 1);
259 current->exit_code = 0;
260 }
261}