aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64/ia32/ptrace32.c
blob: b98b6d2462f6b43fb8b6ecd7f07a50c6b00cb277 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/* 
 * 32bit ptrace for x86-64.
 *
 * Copyright 2001,2002 Andi Kleen, SuSE Labs.
 * Some parts copied from arch/i386/kernel/ptrace.c. See that file for earlier 
 * copyright.
 * 
 * This allows to access 64bit processes too; but there is no way to see the extended 
 * register contents.
 *
 * $Id: ptrace32.c,v 1.16 2003/03/14 16:06:35 ak Exp $
 */ 

#include <linux/kernel.h>
#include <linux/stddef.h>
#include <linux/sched.h>
#include <linux/syscalls.h>
#include <linux/unistd.h>
#include <linux/mm.h>
#include <linux/ptrace.h>
#include <asm/ptrace.h>
#include <asm/compat.h>
#include <asm/uaccess.h>
#include <asm/user32.h>
#include <asm/user.h>
#include <asm/errno.h>
#include <asm/debugreg.h>
#include <asm/i387.h>
#include <asm/fpu32.h>

/* determines which flags the user has access to. */
/* 1 = access 0 = no access */
#define FLAG_MASK 0x44dd5UL

#define R32(l,q) \
	case offsetof(struct user32, regs.l): stack[offsetof(struct pt_regs, q)/8] = val; break

static int putreg32(struct task_struct *child, unsigned regno, u32 val)
{
	int i;
	__u64 *stack = (__u64 *)(child->thread.rsp0 - sizeof(struct pt_regs)); 

	switch (regno) {
	case offsetof(struct user32, regs.fs):
		if (val && (val & 3) != 3) return -EIO; 
		child->thread.fs = val & 0xffff; 
		break;
	case offsetof(struct user32, regs.gs):
		if (val && (val & 3) != 3) return -EIO; 
		child->thread.gs = val & 0xffff;
		break;
	case offsetof(struct user32, regs.ds):
		if (val && (val & 3) != 3) return -EIO; 
		child->thread.ds = val & 0xffff;
		break;
	case offsetof(struct user32, regs.es):
		child->thread.es = val & 0xffff;
		break;
	case offsetof(struct user32, regs.ss): 
		if ((val & 3) != 3) return -EIO;
        	stack[offsetof(struct pt_regs, ss)/8] = val & 0xffff;
		break;
	case offsetof(struct user32, regs.cs): 
		if ((val & 3) != 3) return -EIO;
		stack[offsetof(struct pt_regs, cs)/8] = val & 0xffff;
		break;

	R32(ebx, rbx); 
	R32(ecx, rcx);
	R32(edx, rdx);
	R32(edi, rdi);
	R32(esi, rsi);
	R32(ebp, rbp);
	R32(eax, rax);
	R32(orig_eax, orig_rax);
	R32(eip, rip);
	R32(esp, rsp);

	case offsetof(struct user32, regs.eflags): {
		__u64 *flags = &stack[offsetof(struct pt_regs, eflags)/8];
		val &= FLAG_MASK;
		*flags = val | (*flags & ~FLAG_MASK);
		break;
	}

	case offsetof(struct user32, u_debugreg[4]): 
	case offsetof(struct user32, u_debugreg[5]):
		return -EIO;

	case offsetof(struct user32, u_debugreg[0]):
		child->thread.debugreg0 = val;
		break;

	case offsetof(struct user32, u_debugreg[1]):
		child->thread.debugreg1 = val;
		break;

	case offsetof(struct user32, u_debugreg[2]):
		child->thread.debugreg2 = val;
		break;

	case offsetof(struct user32, u_debugreg[3]):
		child->thread.debugreg3 = val;
		break;

	case offsetof(struct user32, u_debugreg[6]):
		child->thread.debugreg6 = val;
		break; 

	case offsetof(struct user32, u_debugreg[7]):
		val &= ~DR_CONTROL_RESERVED;
		/* See arch/i386/kernel/ptrace.c for an explanation of
		 * this awkward check.*/
		for(i=0; i<4; i++)
			if ((0x5454 >> ((val >> (16 + 4*i)) & 0xf)) & 1)
			       return -EIO;
		child->thread.debugreg7 = val; 
		break; 
		    
	default:
		if (regno > sizeof(struct user32) || (regno & 3))
			return -EIO;
	       
		/* Other dummy fields in the virtual user structure are ignored */ 
		break; 		
	}
	return 0;
}

#undef R32

#define R32(l,q) \
	case offsetof(struct user32, regs.l): *val = stack[offsetof(struct pt_regs, q)/8]; break

static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
{
	__u64 *stack = (__u64 *)(child->thread.rsp0 - sizeof(struct pt_regs)); 

	switch (regno) {
	case offsetof(struct user32, regs.fs):
	        *val = child->thread.fs; 
		break;
	case offsetof(struct user32, regs.gs):
		*val = child->thread.gs;
		break;
	case offsetof(struct user32, regs.ds):
		*val = child->thread.ds;
		break;
	case offsetof(struct user32, regs.es):
		*val = child->thread.es;
		break;

	R32(cs, cs);
	R32(ss, ss);
	R32(ebx, rbx); 
	R32(ecx, rcx);
	R32(edx, rdx);
	R32(edi, rdi);
	R32(esi, rsi);
	R32(ebp, rbp);
	R32(eax, rax);
	R32(orig_eax, orig_rax);
	R32(eip, rip);
	R32(eflags, eflags);
	R32(esp, rsp);

	case offsetof(struct user32, u_debugreg[0]): 
		*val = child->thread.debugreg0; 
		break; 
	case offsetof(struct user32, u_debugreg[1]): 
		*val = child->thread.debugreg1; 
		break; 
	case offsetof(struct user32, u_debugreg[2]): 
		*val = child->thread.debugreg2; 
		break; 
	case offsetof(struct user32, u_debugreg[3]): 
		*val = child->thread.debugreg3; 
		break; 
	case offsetof(struct user32, u_debugreg[6]): 
		*val = child->thread.debugreg6; 
		break; 
	case offsetof(struct user32, u_debugreg[7]): 
		*val = child->thread.debugreg7; 
		break; 
		    
	default:
		if (regno > sizeof(struct user32) || (regno & 3))
			return -EIO;

		/* Other dummy fields in the virtual user structure are ignored */ 
		*val = 0;
		break; 		
	}
	return 0;
}

#undef R32

static struct task_struct *find_target(int request, int pid, int *err)
{ 
	struct task_struct *child;

	*err = -EPERM; 
	if (pid == 1)
		return NULL; 

	*err = -ESRCH;
	read_lock(&tasklist_lock);
	child = find_task_by_pid(pid);
	if (child)
		get_task_struct(child);
	read_unlock(&tasklist_lock);
	if (child) { 
		*err = -EPERM;
		if (child->pid == 1) 
			goto out;
		*err = ptrace_check_attach(child, request == PTRACE_KILL); 
		if (*err < 0) 
			goto out;
		return child; 
	} 
 out:
	if (child)
	put_task_struct(child);
	return NULL; 
	
} 

asmlinkage long sys32_ptrace(long request, u32 pid, u32 addr, u32 data)
{
	struct task_struct *child;
	struct pt_regs *childregs; 
	void __user *datap = compat_ptr(data);
	int ret;
	__u32 val;

	switch (request) { 
	default:
		return sys_ptrace(request, pid, addr, data); 

	case PTRACE_PEEKTEXT:
	case PTRACE_PEEKDATA:
	case PTRACE_POKEDATA:
	case PTRACE_POKETEXT:
	case PTRACE_POKEUSR:       
	case PTRACE_PEEKUSR:
	case PTRACE_GETREGS:
	case PTRACE_SETREGS:
	case PTRACE_SETFPREGS:
	case PTRACE_GETFPREGS:
	case PTRACE_SETFPXREGS:
	case PTRACE_GETFPXREGS:
	case PTRACE_GETEVENTMSG:
		break;
	} 

	child = find_target(request, pid, &ret);
	if (!child)
		return ret;

	childregs = (struct pt_regs *)(child->thread.rsp0 - sizeof(struct pt_regs)); 

	switch (request) {
	case PTRACE_PEEKDATA:
	case PTRACE_PEEKTEXT:
		ret = 0;
		if (access_process_vm(child, addr, &val, sizeof(u32), 0)!=sizeof(u32))
			ret = -EIO;
		else
			ret = put_user(val, (unsigned int __user *)datap); 
		break; 

	case PTRACE_POKEDATA:
	case PTRACE_POKETEXT:
		ret = 0;
		if (access_process_vm(child, addr, &data, sizeof(u32), 1)!=sizeof(u32))
			ret = -EIO; 
		break;

	case PTRACE_PEEKUSR:
		ret = getreg32(child, addr, &val);
		if (ret == 0)
			ret = put_user(val, (__u32 __user *)datap);
		break;

	case PTRACE_POKEUSR:
		ret = putreg32(child, addr, data);
		break;

	case PTRACE_GETREGS: { /* Get all gp regs from the child. */
		int i;
	  	if (!access_ok(VERIFY_WRITE, datap, 16*4)) {
			ret = -EIO;
			break;
		}
		ret = 0;
		for ( i = 0; i <= 16*4 ; i += sizeof(__u32) ) {
			getreg32(child, i, &val);
			ret |= __put_user(val,(u32 __user *)datap);
			datap += sizeof(u32);
		}
		break;
	}

	case PTRACE_SETREGS: { /* Set all gp regs in the child. */
		unsigned long tmp;
		int i;
	  	if (!access_ok(VERIFY_READ, datap, 16*4)) {
			ret = -EIO;
			break;
		}
		ret = 0; 
		for ( i = 0; i <= 16*4; i += sizeof(u32) ) {
			ret |= __get_user(tmp, (u32 __user *)datap);
			putreg32(child, i, tmp);
			datap += sizeof(u32);
		}
		break;
	}

	case PTRACE_GETFPREGS:
		ret = -EIO; 
		if (!access_ok(VERIFY_READ, compat_ptr(data), 
			       sizeof(struct user_i387_struct)))
			break;
		save_i387_ia32(child, datap, childregs, 1);
		ret = 0; 
			break;

	case PTRACE_SETFPREGS:
		ret = -EIO;
		if (!access_ok(VERIFY_WRITE, datap, 
			       sizeof(struct user_i387_struct)))
			break;
		ret = 0;
		/* don't check EFAULT to be bug-to-bug compatible to i386 */
		restore_i387_ia32(child, datap, 1);
		break;

	case PTRACE_GETFPXREGS: { 
		struct user32_fxsr_struct __user *u = datap;
		init_fpu(child); 
		ret = -EIO;
		if (!access_ok(VERIFY_WRITE, u, sizeof(*u)))
			break;
			ret = -EFAULT;
		if (__copy_to_user(u, &child->thread.i387.fxsave, sizeof(*u)))
			break;
		ret = __put_user(childregs->cs, &u->fcs);
		ret |= __put_user(child->thread.ds, &u->fos); 
		break; 
	} 
	case PTRACE_SETFPXREGS: { 
		struct user32_fxsr_struct __user *u = datap;
		unlazy_fpu(child);
		ret = -EIO;
		if (!access_ok(VERIFY_READ, u, sizeof(*u)))
			break;
		/* no checking to be bug-to-bug compatible with i386 */
		__copy_from_user(&child->thread.i387.fxsave, u, sizeof(*u));
		set_stopped_child_used_math(child);
		child->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
		ret = 0; 
		break;
	}

	case PTRACE_GETEVENTMSG:
		ret = put_user(child->ptrace_message,(unsigned int __user *)compat_ptr(data));
		break;

	default:
		ret = -EINVAL;
		break;
	}

	put_task_struct(child);
	return ret;
}