xref: /linux/arch/um/kernel/ptrace.c (revision 36ca1195ad7f760a6af3814cb002bd3a3d4b4db1)
1 /*
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 static inline void set_singlestepping(struct task_struct *child, int on)
23 {
24         if (on)
25                 child->ptrace |= PT_DTRACE;
26         else
27                 child->ptrace &= ~PT_DTRACE;
28         child->thread.singlestep_syscall = 0;
29 
30 #ifdef SUBARCH_SET_SINGLESTEPPING
31         SUBARCH_SET_SINGLESTEPPING(child, on);
32 #endif
33 }
34 
35 /*
36  * Called by kernel/ptrace.c when detaching..
37  */
38 void ptrace_disable(struct task_struct *child)
39 {
40         set_singlestepping(child,0);
41 }
42 
43 extern int peek_user(struct task_struct * child, long addr, long data);
44 extern int poke_user(struct task_struct * child, long addr, long data);
45 
46 long sys_ptrace(long request, long pid, long addr, long data)
47 {
48 	struct task_struct *child;
49 	int i, ret;
50 
51 	lock_kernel();
52 	ret = -EPERM;
53 	if (request == PTRACE_TRACEME) {
54 		/* are we already being traced? */
55 		if (current->ptrace & PT_PTRACED)
56 			goto out;
57 
58 		ret = security_ptrace(current->parent, current);
59 		if (ret)
60  			goto out;
61 
62 		/* set the ptrace bit in the process flags. */
63 		current->ptrace |= PT_PTRACED;
64 		ret = 0;
65 		goto out;
66 	}
67 	ret = -ESRCH;
68 	read_lock(&tasklist_lock);
69 	child = find_task_by_pid(pid);
70 	if (child)
71 		get_task_struct(child);
72 	read_unlock(&tasklist_lock);
73 	if (!child)
74 		goto out;
75 
76 	ret = -EPERM;
77 	if (pid == 1)		/* you may not mess with init */
78 		goto out_tsk;
79 
80 	if (request == PTRACE_ATTACH) {
81 		ret = ptrace_attach(child);
82 		goto out_tsk;
83 	}
84 
85 #ifdef SUBACH_PTRACE_SPECIAL
86         SUBARCH_PTRACE_SPECIAL(child,request,addr,data);
87 #endif
88 
89 	ret = ptrace_check_attach(child, request == PTRACE_KILL);
90 	if (ret < 0)
91 		goto out_tsk;
92 
93 	switch (request) {
94 		/* when I and D space are separate, these will need to be fixed. */
95 	case PTRACE_PEEKTEXT: /* read word at location addr. */
96 	case PTRACE_PEEKDATA: {
97 		unsigned long tmp;
98 		int copied;
99 
100 		ret = -EIO;
101 		copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
102 		if (copied != sizeof(tmp))
103 			break;
104 		ret = put_user(tmp, (unsigned long __user *) data);
105 		break;
106 	}
107 
108 	/* read the word at location addr in the USER area. */
109         case PTRACE_PEEKUSR:
110                 ret = peek_user(child, addr, data);
111                 break;
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 = poke_user(child, addr, data);
125                 break;
126 
127 	case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
128 	case PTRACE_CONT: { /* restart after signal. */
129 		ret = -EIO;
130 		if (!valid_signal(data))
131 			break;
132 
133                 set_singlestepping(child, 0);
134 		if (request == PTRACE_SYSCALL) {
135 			set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
136 		}
137 		else {
138 			clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
139 		}
140 		child->exit_code = data;
141 		wake_up_process(child);
142 		ret = 0;
143 		break;
144 	}
145 
146 /*
147  * make the child exit.  Best I can do is send it a sigkill.
148  * perhaps it should be put in the status that it wants to
149  * exit.
150  */
151 	case PTRACE_KILL: {
152 		ret = 0;
153 		if (child->exit_state == EXIT_ZOMBIE)	/* already dead */
154 			break;
155 
156                 set_singlestepping(child, 0);
157 		child->exit_code = SIGKILL;
158 		wake_up_process(child);
159 		break;
160 	}
161 
162 	case PTRACE_SINGLESTEP: {  /* set the trap flag. */
163 		ret = -EIO;
164 		if (!valid_signal(data))
165 			break;
166 		clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
167                 set_singlestepping(child, 1);
168 		child->exit_code = data;
169 		/* give it a chance to run. */
170 		wake_up_process(child);
171 		ret = 0;
172 		break;
173 	}
174 
175 	case PTRACE_DETACH:
176 		/* detach a process that was attached. */
177 		ret = ptrace_detach(child, data);
178  		break;
179 
180 #ifdef PTRACE_GETREGS
181 	case PTRACE_GETREGS: { /* Get all gp regs from the child. */
182 	  	if (!access_ok(VERIFY_WRITE, (unsigned long *)data,
183 			       MAX_REG_OFFSET)) {
184 			ret = -EIO;
185 			break;
186 		}
187 		for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
188 			__put_user(getreg(child, i),
189 				   (unsigned long __user *) data);
190 			data += sizeof(long);
191 		}
192 		ret = 0;
193 		break;
194 	}
195 #endif
196 #ifdef PTRACE_SETREGS
197 	case PTRACE_SETREGS: { /* Set all gp regs in the child. */
198 		unsigned long tmp = 0;
199 	  	if (!access_ok(VERIFY_READ, (unsigned *)data,
200 			       MAX_REG_OFFSET)) {
201 			ret = -EIO;
202 			break;
203 		}
204 		for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
205 			__get_user(tmp, (unsigned long __user *) data);
206 			putreg(child, i, tmp);
207 			data += sizeof(long);
208 		}
209 		ret = 0;
210 		break;
211 	}
212 #endif
213 #ifdef PTRACE_GETFPREGS
214 	case PTRACE_GETFPREGS: /* Get the child FPU state. */
215 		ret = get_fpregs(data, child);
216 		break;
217 #endif
218 #ifdef PTRACE_SETFPREGS
219 	case PTRACE_SETFPREGS: /* Set the child FPU state. */
220 	        ret = set_fpregs(data, child);
221 		break;
222 #endif
223 #ifdef PTRACE_GETFPXREGS
224 	case PTRACE_GETFPXREGS: /* Get the child FPU state. */
225 		ret = get_fpxregs(data, child);
226 		break;
227 #endif
228 #ifdef PTRACE_SETFPXREGS
229 	case PTRACE_SETFPXREGS: /* Set the child FPU state. */
230 		ret = set_fpxregs(data, child);
231 		break;
232 #endif
233 	case PTRACE_FAULTINFO: {
234                 /* Take the info from thread->arch->faultinfo,
235                  * but transfer max. sizeof(struct ptrace_faultinfo).
236                  * On i386, ptrace_faultinfo is smaller!
237                  */
238                 ret = copy_to_user((unsigned long __user *) data,
239                                    &child->thread.arch.faultinfo,
240                                    sizeof(struct ptrace_faultinfo));
241 		if(ret)
242 			break;
243 		break;
244 	}
245 
246 #ifdef PTRACE_LDT
247 	case PTRACE_LDT: {
248 		struct ptrace_ldt ldt;
249 
250 		if(copy_from_user(&ldt, (unsigned long __user *) data,
251 				  sizeof(ldt))){
252 			ret = -EIO;
253 			break;
254 		}
255 
256 		/* This one is confusing, so just punt and return -EIO for
257 		 * now
258 		 */
259 		ret = -EIO;
260 		break;
261 	}
262 #endif
263 #ifdef CONFIG_PROC_MM
264 	case PTRACE_SWITCH_MM: {
265 		struct mm_struct *old = child->mm;
266 		struct mm_struct *new = proc_mm_get_mm(data);
267 
268 		if(IS_ERR(new)){
269 			ret = PTR_ERR(new);
270 			break;
271 		}
272 
273 		atomic_inc(&new->mm_users);
274 		child->mm = new;
275 		child->active_mm = new;
276 		mmput(old);
277 		ret = 0;
278 		break;
279 	}
280 #endif
281 	default:
282 		ret = ptrace_request(child, request, addr, data);
283 		break;
284 	}
285  out_tsk:
286 	put_task_struct(child);
287  out:
288 	unlock_kernel();
289 	return ret;
290 }
291 
292 void send_sigtrap(struct task_struct *tsk, union uml_pt_regs *regs,
293 		  int error_code)
294 {
295 	struct siginfo info;
296 
297 	memset(&info, 0, sizeof(info));
298 	info.si_signo = SIGTRAP;
299 	info.si_code = TRAP_BRKPT;
300 
301 	/* User-mode eip? */
302 	info.si_addr = UPT_IS_USER(regs) ? (void __user *) UPT_IP(regs) : NULL;
303 
304 	/* Send us the fakey SIGTRAP */
305 	force_sig_info(SIGTRAP, &info, tsk);
306 }
307 
308 /* XXX Check PT_DTRACE vs TIF_SINGLESTEP for singlestepping check and
309  * PT_PTRACED vs TIF_SYSCALL_TRACE for syscall tracing check
310  */
311 void syscall_trace(union uml_pt_regs *regs, int entryexit)
312 {
313 	int is_singlestep = (current->ptrace & PT_DTRACE) && entryexit;
314 	int tracesysgood;
315 
316 	if (unlikely(current->audit_context)) {
317 		if (!entryexit)
318 			audit_syscall_entry(current,
319                                             HOST_AUDIT_ARCH,
320 					    UPT_SYSCALL_NR(regs),
321 					    UPT_SYSCALL_ARG1(regs),
322 					    UPT_SYSCALL_ARG2(regs),
323 					    UPT_SYSCALL_ARG3(regs),
324 					    UPT_SYSCALL_ARG4(regs));
325 		else audit_syscall_exit(current,
326                                         AUDITSC_RESULT(UPT_SYSCALL_RET(regs)),
327                                         UPT_SYSCALL_RET(regs));
328 	}
329 
330 	/* Fake a debug trap */
331 	if (is_singlestep)
332 		send_sigtrap(current, regs, 0);
333 
334 	if (!test_thread_flag(TIF_SYSCALL_TRACE))
335 		return;
336 
337 	if (!(current->ptrace & PT_PTRACED))
338 		return;
339 
340 	/* the 0x80 provides a way for the tracing parent to distinguish
341 	   between a syscall stop and SIGTRAP delivery */
342 	tracesysgood = (current->ptrace & PT_TRACESYSGOOD);
343 	ptrace_notify(SIGTRAP | (tracesysgood ? 0x80 : 0));
344 
345 	if (entryexit) /* force do_signal() --> is_syscall() */
346 		set_thread_flag(TIF_SIGPENDING);
347 
348 	/* this isn't the same as continuing with a signal, but it will do
349 	 * for normal use.  strace only continues with a signal if the
350 	 * stopping signal is not SIGTRAP.  -brl
351 	 */
352 	if (current->exit_code) {
353 		send_sig(current->exit_code, current, 1);
354 		current->exit_code = 0;
355 	}
356 }
357