ptrace.c 6.8 KB
Newer Older
J
Jeff Dike 已提交
1 2
/*
 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
L
Linus Torvalds 已提交
3 4 5 6
 * Licensed under the GPL
 */

#include "linux/audit.h"
J
Jeff Dike 已提交
7 8 9
#include "linux/ptrace.h"
#include "linux/sched.h"
#include "asm/uaccess.h"
L
Linus Torvalds 已提交
10
#ifdef CONFIG_PROC_MM
J
Jeff Dike 已提交
11
#include "proc_mm.h"
L
Linus Torvalds 已提交
12 13 14
#endif
#include "skas_ptrace.h"

15 16
static inline void set_singlestepping(struct task_struct *child, int on)
{
J
Jeff Dike 已提交
17 18 19 20 21
	if (on)
		child->ptrace |= PT_DTRACE;
	else
		child->ptrace &= ~PT_DTRACE;
	child->thread.singlestep_syscall = 0;
22 23

#ifdef SUBARCH_SET_SINGLESTEPPING
J
Jeff Dike 已提交
24
	SUBARCH_SET_SINGLESTEPPING(child, on);
25
#endif
26
}
27

L
Linus Torvalds 已提交
28 29 30 31
/*
 * Called by kernel/ptrace.c when detaching..
 */
void ptrace_disable(struct task_struct *child)
J
Jeff Dike 已提交
32 33
{
	set_singlestepping(child,0);
L
Linus Torvalds 已提交
34 35
}

36 37 38
extern int peek_user(struct task_struct * child, long addr, long data);
extern int poke_user(struct task_struct * child, long addr, long data);

39
long arch_ptrace(struct task_struct *child, long request, long addr, long data)
L
Linus Torvalds 已提交
40 41
{
	int i, ret;
A
Al Viro 已提交
42
	unsigned long __user *p = (void __user *)(unsigned long)data;
L
Linus Torvalds 已提交
43 44

	switch (request) {
J
Jeff Dike 已提交
45 46
	/* read word at location addr. */
	case PTRACE_PEEKTEXT:
A
Alexey Dobriyan 已提交
47 48
	case PTRACE_PEEKDATA:
		ret = generic_ptrace_peekdata(child, addr, data);
L
Linus Torvalds 已提交
49 50 51
		break;

	/* read the word at location addr in the USER area. */
J
Jeff Dike 已提交
52 53 54
	case PTRACE_PEEKUSR:
		ret = peek_user(child, addr, data);
		break;
L
Linus Torvalds 已提交
55

J
Jeff Dike 已提交
56 57
	/* write the word at location addr. */
	case PTRACE_POKETEXT:
L
Linus Torvalds 已提交
58
	case PTRACE_POKEDATA:
A
Alexey Dobriyan 已提交
59
		ret = generic_ptrace_pokedata(child, addr, data);
L
Linus Torvalds 已提交
60 61
		break;

J
Jeff Dike 已提交
62 63 64 65
	/* write the word at location addr in the USER area */
	case PTRACE_POKEUSR:
		ret = poke_user(child, addr, data);
		break;
L
Linus Torvalds 已提交
66

67 68 69 70 71
	case PTRACE_SYSEMU:
	case PTRACE_SYSEMU_SINGLESTEP:
		ret = -EIO;
		break;

J
Jeff Dike 已提交
72 73 74 75
	/* continue and stop at next (return from) syscall */
	case PTRACE_SYSCALL:
	/* restart after signal. */
	case PTRACE_CONT: {
L
Linus Torvalds 已提交
76
		ret = -EIO;
77
		if (!valid_signal(data))
L
Linus Torvalds 已提交
78 79
			break;

J
Jeff Dike 已提交
80 81
		set_singlestepping(child, 0);
		if (request == PTRACE_SYSCALL)
L
Linus Torvalds 已提交
82
			set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
J
Jeff Dike 已提交
83
		else clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
L
Linus Torvalds 已提交
84 85 86 87 88 89 90
		child->exit_code = data;
		wake_up_process(child);
		ret = 0;
		break;
	}

/*
J
Jeff Dike 已提交
91 92
 * make the child exit.  Best I can do is send it a sigkill.
 * perhaps it should be put in the status that it wants to
L
Linus Torvalds 已提交
93 94 95 96 97 98 99
 * exit.
 */
	case PTRACE_KILL: {
		ret = 0;
		if (child->exit_state == EXIT_ZOMBIE)	/* already dead */
			break;

J
Jeff Dike 已提交
100
		set_singlestepping(child, 0);
L
Linus Torvalds 已提交
101 102 103 104 105 106 107
		child->exit_code = SIGKILL;
		wake_up_process(child);
		break;
	}

	case PTRACE_SINGLESTEP: {  /* set the trap flag. */
		ret = -EIO;
108
		if (!valid_signal(data))
L
Linus Torvalds 已提交
109 110
			break;
		clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
J
Jeff Dike 已提交
111
		set_singlestepping(child, 1);
L
Linus Torvalds 已提交
112 113 114 115 116 117 118 119 120
		child->exit_code = data;
		/* give it a chance to run. */
		wake_up_process(child);
		ret = 0;
		break;
	}

#ifdef PTRACE_GETREGS
	case PTRACE_GETREGS: { /* Get all gp regs from the child. */
A
Al Viro 已提交
121
		if (!access_ok(VERIFY_WRITE, p, MAX_REG_OFFSET)) {
L
Linus Torvalds 已提交
122 123 124 125
			ret = -EIO;
			break;
		}
		for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
A
Al Viro 已提交
126 127
			__put_user(getreg(child, i), p);
			p++;
L
Linus Torvalds 已提交
128 129 130 131 132 133 134 135
		}
		ret = 0;
		break;
	}
#endif
#ifdef PTRACE_SETREGS
	case PTRACE_SETREGS: { /* Set all gp regs in the child. */
		unsigned long tmp = 0;
A
Al Viro 已提交
136
		if (!access_ok(VERIFY_READ, p, MAX_REG_OFFSET)) {
L
Linus Torvalds 已提交
137 138 139 140
			ret = -EIO;
			break;
		}
		for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
A
Al Viro 已提交
141
			__get_user(tmp, p);
L
Linus Torvalds 已提交
142
			putreg(child, i, tmp);
A
Al Viro 已提交
143
			p++;
L
Linus Torvalds 已提交
144 145 146 147 148 149 150
		}
		ret = 0;
		break;
	}
#endif
#ifdef PTRACE_GETFPREGS
	case PTRACE_GETFPREGS: /* Get the child FPU state. */
J
Jeff Dike 已提交
151 152
		ret = get_fpregs((struct user_i387_struct __user *) data,
				 child);
L
Linus Torvalds 已提交
153 154 155 156
		break;
#endif
#ifdef PTRACE_SETFPREGS
	case PTRACE_SETFPREGS: /* Set the child FPU state. */
J
Jeff Dike 已提交
157 158
	        ret = set_fpregs((struct user_i387_struct __user *) data,
				 child);
L
Linus Torvalds 已提交
159 160
		break;
#endif
161 162 163 164 165 166 167 168 169 170
	case PTRACE_GET_THREAD_AREA:
		ret = ptrace_get_thread_area(child, addr,
					     (struct user_desc __user *) data);
		break;

	case PTRACE_SET_THREAD_AREA:
		ret = ptrace_set_thread_area(child, addr,
					     (struct user_desc __user *) data);
		break;

L
Linus Torvalds 已提交
171
	case PTRACE_FAULTINFO: {
J
Jeff Dike 已提交
172 173
		/*
		 * Take the info from thread->arch->faultinfo,
A
Al Viro 已提交
174 175 176 177 178
		 * but transfer max. sizeof(struct ptrace_faultinfo).
		 * On i386, ptrace_faultinfo is smaller!
		 */
		ret = copy_to_user(p, &child->thread.arch.faultinfo,
				   sizeof(struct ptrace_faultinfo));
L
Linus Torvalds 已提交
179 180 181
		break;
	}

182
#ifdef PTRACE_LDT
L
Linus Torvalds 已提交
183 184 185
	case PTRACE_LDT: {
		struct ptrace_ldt ldt;

J
Jeff Dike 已提交
186
		if (copy_from_user(&ldt, p, sizeof(ldt))) {
L
Linus Torvalds 已提交
187 188 189 190
			ret = -EIO;
			break;
		}

J
Jeff Dike 已提交
191 192
		/*
		 * This one is confusing, so just punt and return -EIO for
L
Linus Torvalds 已提交
193 194 195 196 197
		 * now
		 */
		ret = -EIO;
		break;
	}
198
#endif
L
Linus Torvalds 已提交
199 200 201 202 203
#ifdef CONFIG_PROC_MM
	case PTRACE_SWITCH_MM: {
		struct mm_struct *old = child->mm;
		struct mm_struct *new = proc_mm_get_mm(data);

J
Jeff Dike 已提交
204
		if (IS_ERR(new)) {
L
Linus Torvalds 已提交
205 206 207 208 209 210 211 212 213 214 215
			ret = PTR_ERR(new);
			break;
		}

		atomic_inc(&new->mm_users);
		child->mm = new;
		child->active_mm = new;
		mmput(old);
		ret = 0;
		break;
	}
J
Jeff Dike 已提交
216 217
#endif
#ifdef PTRACE_ARCH_PRCTL
J
Jeff Dike 已提交
218 219 220 221
	case PTRACE_ARCH_PRCTL:
		/* XXX Calls ptrace on the host - needs some SMP thinking */
		ret = arch_prctl(child, data, (void *) addr);
		break;
L
Linus Torvalds 已提交
222 223 224
#endif
	default:
		ret = ptrace_request(child, request, addr, data);
J
Jeff Dike 已提交
225 226
		if (ret == -EIO)
			ret = subarch_ptrace(child, request, addr, data);
L
Linus Torvalds 已提交
227 228
		break;
	}
229

L
Linus Torvalds 已提交
230 231 232
	return ret;
}

W
WANG Cong 已提交
233
static void send_sigtrap(struct task_struct *tsk, struct uml_pt_regs *regs,
L
Linus Torvalds 已提交
234 235 236 237 238 239 240 241 242 243 244
		  int error_code)
{
	struct siginfo info;

	memset(&info, 0, sizeof(info));
	info.si_signo = SIGTRAP;
	info.si_code = TRAP_BRKPT;

	/* User-mode eip? */
	info.si_addr = UPT_IS_USER(regs) ? (void __user *) UPT_IP(regs) : NULL;

S
Simon Arlott 已提交
245
	/* Send us the fake SIGTRAP */
L
Linus Torvalds 已提交
246 247 248
	force_sig_info(SIGTRAP, &info, tsk);
}

J
Jeff Dike 已提交
249 250
/*
 * XXX Check PT_DTRACE vs TIF_SINGLESTEP for singlestepping check and
L
Linus Torvalds 已提交
251 252
 * PT_PTRACED vs TIF_SYSCALL_TRACE for syscall tracing check
 */
253
void syscall_trace(struct uml_pt_regs *regs, int entryexit)
L
Linus Torvalds 已提交
254 255 256 257 258 259
{
	int is_singlestep = (current->ptrace & PT_DTRACE) && entryexit;
	int tracesysgood;

	if (unlikely(current->audit_context)) {
		if (!entryexit)
260
			audit_syscall_entry(HOST_AUDIT_ARCH,
261 262 263 264 265
					    UPT_SYSCALL_NR(regs),
					    UPT_SYSCALL_ARG1(regs),
					    UPT_SYSCALL_ARG2(regs),
					    UPT_SYSCALL_ARG3(regs),
					    UPT_SYSCALL_ARG4(regs));
266
		else audit_syscall_exit(AUDITSC_RESULT(UPT_SYSCALL_RET(regs)),
J
Jeff Dike 已提交
267
					UPT_SYSCALL_RET(regs));
L
Linus Torvalds 已提交
268 269 270 271 272 273 274 275 276 277 278 279
	}

	/* Fake a debug trap */
	if (is_singlestep)
		send_sigtrap(current, regs, 0);

	if (!test_thread_flag(TIF_SYSCALL_TRACE))
		return;

	if (!(current->ptrace & PT_PTRACED))
		return;

J
Jeff Dike 已提交
280 281 282 283
	/*
	 * the 0x80 provides a way for the tracing parent to distinguish
	 * between a syscall stop and SIGTRAP delivery
	 */
L
Linus Torvalds 已提交
284 285 286 287 288 289
	tracesysgood = (current->ptrace & PT_TRACESYSGOOD);
	ptrace_notify(SIGTRAP | (tracesysgood ? 0x80 : 0));

	if (entryexit) /* force do_signal() --> is_syscall() */
		set_thread_flag(TIF_SIGPENDING);

J
Jeff Dike 已提交
290 291
	/*
	 * this isn't the same as continuing with a signal, but it will do
L
Linus Torvalds 已提交
292 293 294 295 296 297 298 299
	 * for normal use.  strace only continues with a signal if the
	 * stopping signal is not SIGTRAP.  -brl
	 */
	if (current->exit_code) {
		send_sig(current->exit_code, current, 1);
		current->exit_code = 0;
	}
}