File: patch-linux-2.6.35-utrace-ptrace3-vmulti

package info (click to toggle)
kernel-patch-viewos 0.20120115-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,236 kB
  • sloc: sh: 625; makefile: 85
file content (432 lines) | stat: -rw-r--r-- 12,582 bytes parent folder | download | duplicates (2)
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
diff -Naur linux-2.6.35.utrace.vm2/include/linux/mm.h linux-2.6.35.utrace.vm3/include/linux/mm.h
--- linux-2.6.35.utrace.vm2/include/linux/mm.h	2010-08-11 22:06:36.000000000 +0200
+++ linux-2.6.35.utrace.vm3/include/linux/mm.h	2010-08-11 22:10:24.000000000 +0200
@@ -838,7 +838,10 @@
 #endif
 
 extern int make_pages_present(unsigned long addr, unsigned long end);
-extern int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write);
+extern int access_process_vm(struct task_struct *tsk, unsigned long addr,
+		void *buf, int len, int write);
+extern int access_process_vm_user(struct task_struct *tsk, unsigned long addr,
+		char __user *ubuf, int len, int write, int string);
 
 int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
 			unsigned long start, int nr_pages, int write, int force,
diff -Naur linux-2.6.35.utrace.vm2/include/linux/ptrace.h linux-2.6.35.utrace.vm3/include/linux/ptrace.h
--- linux-2.6.35.utrace.vm2/include/linux/ptrace.h	2010-08-11 22:06:36.000000000 +0200
+++ linux-2.6.35.utrace.vm3/include/linux/ptrace.h	2010-08-11 22:10:24.000000000 +0200
@@ -47,6 +47,18 @@
 #define PTRACE_GETREGSET	0x4204
 #define PTRACE_SETREGSET	0x4205
 
+#define PTRACE_MULTI            0x4300
+#define PTRACE_PEEKCHARDATA     0x4301
+#define PTRACE_POKECHARDATA     0x4302
+#define PTRACE_PEEKSTRINGDATA   0x4303
+
+struct ptrace_multi {
+	long request;
+	long addr;
+	void *localaddr;
+	long length;
+};
+
 /* options set using PTRACE_SETOPTIONS */
 #define PTRACE_O_TRACESYSGOOD	0x00000001
 #define PTRACE_O_TRACEFORK	0x00000002
@@ -109,11 +121,12 @@
 #include <linux/compiler.h>		/* For unlikely.  */
 #include <linux/sched.h>		/* For struct task_struct.  */
 
+#include <linux/mm.h>   /* For access_vm*.  */
+#include <linux/uaccess.h>    /* For *_ACCESS */
+
 extern void ptrace_notify_stop(struct task_struct *tracee);
 extern long arch_ptrace(struct task_struct *child, long request, long addr, long data);
 extern int ptrace_traceme(void);
-extern int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len);
-extern int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len);
 extern int ptrace_attach(struct task_struct *tsk);
 extern bool __ptrace_detach(struct task_struct *tracer, struct task_struct *tracee);
 extern int ptrace_detach(struct task_struct *, unsigned int);
@@ -132,6 +145,30 @@
 /* Returns true on success, false on denial. */
 extern bool ptrace_may_access(struct task_struct *task, unsigned int mode);
 
+static inline int ptrace_readdata(struct task_struct *tsk, unsigned long src,
+		char __user *dst, int len)
+{
+	if (!access_ok(VERIFY_WRITE, dst, len))
+		return -EIO;
+	return access_process_vm_user(tsk, src, dst, len, 0, 0);
+}
+
+static inline int ptrace_readstringdata(struct task_struct *tsk, unsigned long src,
+		char __user *dst, int len)
+{
+	if (!access_ok(VERIFY_WRITE, dst, len))
+		return -EIO;
+	return access_process_vm_user(tsk, src, dst, len, 0, 1);
+}
+
+static inline int ptrace_writedata(struct task_struct *tsk, char __user *src,
+		unsigned long dst, int len)
+{
+	if (!access_ok(VERIFY_READ, dst, len))
+		return -EIO;
+	return access_process_vm_user(tsk, dst, src, len, 1, 0);
+}
+
 static inline int ptrace_reparented(struct task_struct *child)
 {
 	return child->real_parent != child->parent;
diff -Naur linux-2.6.35.utrace.vm2/kernel/ptrace.c linux-2.6.35.utrace.vm3/kernel/ptrace.c
--- linux-2.6.35.utrace.vm2/kernel/ptrace.c	2010-08-11 22:06:36.000000000 +0200
+++ linux-2.6.35.utrace.vm3/kernel/ptrace.c	2010-08-11 22:11:39.000000000 +0200
@@ -2,6 +2,7 @@
  * linux/kernel/ptrace.c
  *
  * (C) Copyright 1999 Linus Torvalds
+ * PTRACE_MULTI support 2009 Renzo Davoli
  *
  * Common interfaces for "ptrace()" which we do not want
  * to continually duplicate across every architecture.
@@ -121,56 +122,6 @@
 	return false;
 }
 
-int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
-{
-	int copied = 0;
-
-	while (len > 0) {
-		char buf[128];
-		int this_len, retval;
-
-		this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
-		retval = access_process_vm(tsk, src, buf, this_len, 0);
-		if (!retval) {
-			if (copied)
-				break;
-			return -EIO;
-		}
-		if (copy_to_user(dst, buf, retval))
-			return -EFAULT;
-		copied += retval;
-		src += retval;
-		dst += retval;
-		len -= retval;
-	}
-	return copied;
-}
-
-int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
-{
-	int copied = 0;
-
-	while (len > 0) {
-		char buf[128];
-		int this_len, retval;
-
-		this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
-		if (copy_from_user(buf, src, this_len))
-			return -EFAULT;
-		retval = access_process_vm(tsk, dst, buf, this_len, 1);
-		if (!retval) {
-			if (copied)
-				break;
-			return -EIO;
-		}
-		copied += retval;
-		src += retval;
-		dst += retval;
-		len -= retval;
-	}
-	return copied;
-}
-
 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
 
 static const struct user_regset *
@@ -231,6 +182,58 @@
 #define arch_ptrace_attach(child)	do { } while (0)
 #endif
 
+static int multi_ptrace(struct task_struct *child, long request, long addr,
+		long size)
+{
+	int i, ret;
+	long j;
+	ret = 0;
+	if (!access_ok(VERIFY_READ, addr, size*sizeof(struct ptrace_multi))) {
+		ret = -EIO;
+		goto out_multi_ptrace;
+	}
+	for (i = 0; i < size && ret == 0; i++,
+			addr += sizeof(struct ptrace_multi)) {
+		unsigned long len;
+		struct ptrace_multi __user pm ;
+		if (unlikely(__copy_from_user(&pm, (struct ptrace_multi __user *)addr,
+					sizeof(struct ptrace_multi)) != 0))
+			continue;
+		len = pm.length;
+		switch (pm.request) {
+		case PTRACE_PEEKTEXT:
+		case PTRACE_PEEKDATA:
+		case PTRACE_PEEKUSR:
+		case PTRACE_POKETEXT:
+		case PTRACE_POKEDATA:
+		case PTRACE_POKEUSR:
+			if (len <= 0)
+				len = 1;
+			for (j = 0; j < len && ret == 0; j++)
+				ret = arch_ptrace(child, pm.request,
+					(long) (pm.addr) + j * sizeof(long),
+					(long) (pm.localaddr) + j * sizeof(long));
+			break;
+		case PTRACE_PEEKCHARDATA:
+			ret = ptrace_readdata(child, pm.addr, pm.localaddr, len);
+			break;
+		case PTRACE_POKECHARDATA:
+			ret = ptrace_writedata(child, pm.localaddr, pm.addr, len);
+			break;
+		case PTRACE_PEEKSTRINGDATA:
+			ret = ptrace_readstringdata(child, pm.addr, pm.localaddr, len);
+			break;
+		default:
+			ret = arch_ptrace(child, pm.request, (long) (pm.addr),
+					(long) (pm.localaddr));
+			break;
+		}
+	}
+out_multi_ptrace:
+	return ret;
+}
+
+
 SYSCALL_DEFINE4(ptrace, long, request, long, pid, long, addr, long, data)
 {
 	struct task_struct *child;
@@ -264,11 +267,14 @@
 	if (ret < 0)
 		goto out_put_task_struct;
 
-	ret = arch_ptrace(child, request, addr, data);
+	if (request == PTRACE_MULTI)
+		ret = multi_ptrace(child, request, addr, data);
+	else
+		ret = arch_ptrace(child, request, addr, data);
 
- out_put_task_struct:
+out_put_task_struct:
 	put_task_struct(child);
- out:
+out:
 	return ret;
 }
 
@@ -294,8 +300,71 @@
 #if defined CONFIG_COMPAT
 #include <linux/compat.h>
 
+struct compat_ptrace_multi {
+	compat_long_t request;
+	compat_ulong_t addr;
+	compat_ulong_t localaddr;
+	compat_ulong_t length;
+};
+
+static int compat_multi_ptrace(struct task_struct *child,
+		compat_long_t request, compat_ulong_t addr, compat_ulong_t size)
+{
+	int i, ret;
+	compat_long_t j;
+	ret = 0;
+	if (!access_ok(VERIFY_READ, addr,
+				size*sizeof(struct compat_ptrace_multi))) {
+		ret = -EIO;
+		goto out_multi_ptrace;
+	}
+	for (i = 0; i < size && ret == 0;
+			i++, addr += sizeof(struct compat_ptrace_multi)) {
+		compat_ulong_t len;
+		struct compat_ptrace_multi __user pm ;
+		if (unlikely(__copy_from_user(&pm,
+						(struct compat_ptrace_multi __user *)
+						(u_long)addr, sizeof(struct compat_ptrace_multi)) != 0))
+			continue;
+		len = pm.length;
+		switch (pm.request) {
+			case PTRACE_PEEKTEXT:
+			case PTRACE_PEEKDATA:
+			case PTRACE_PEEKUSR:
+			case PTRACE_POKETEXT:
+			case PTRACE_POKEDATA:
+			case PTRACE_POKEUSR:
+				if (len <= 0)
+					len = 1;
+				for (j = 0; j < len && ret == 0; j++)
+					ret = compat_arch_ptrace(child, pm.request,
+							(compat_long_t) (pm.addr) + j * sizeof(compat_long_t),
+							(compat_ulong_t) (pm.localaddr) + j * sizeof(compat_long_t));
+				break;
+			case PTRACE_PEEKCHARDATA:
+				ret = ptrace_readdata(child, pm.addr,
+						(char __user *)(long)pm.localaddr, len);
+				break;
+			case PTRACE_POKECHARDATA:
+				ret = ptrace_writedata(child,
+						(char __user *)(long)pm.localaddr, pm.addr, len);
+				break;
+			case PTRACE_PEEKSTRINGDATA:
+				ret = ptrace_readstringdata(child, pm.addr,
+						(char __user *)(long)pm.localaddr, len);
+				break;
+			default:
+				ret = compat_arch_ptrace(child, pm.request,
+						(compat_ulong_t) (pm.addr), (compat_ulong_t) (pm.localaddr));
+				break;
+		}
+	}
+out_multi_ptrace:
+	return ret;
+}
+
 asmlinkage long compat_sys_ptrace(compat_long_t request, compat_long_t pid,
-				  compat_long_t addr, compat_long_t data)
+		compat_long_t addr, compat_long_t data)
 {
 	struct task_struct *child;
 	long ret;
@@ -323,8 +392,12 @@
 	}
 
 	ret = ptrace_check_attach(child, request == PTRACE_KILL);
-	if (!ret)
-		ret = compat_arch_ptrace(child, request, addr, data);
+	if (!ret) {
+		if (request == PTRACE_MULTI)
+			ret = compat_multi_ptrace(child, request, addr, data);
+		else
+			ret = compat_arch_ptrace(child, request, addr, data);
+	}
 
  out_put_task_struct:
 	put_task_struct(child);
@@ -824,14 +897,14 @@
 		ret = ptrace_getsiginfo(child, &siginfo);
 		if (!ret)
 			ret = copy_siginfo_to_user32(
-				(struct compat_siginfo __user *) datap,
-				&siginfo);
+					(struct compat_siginfo __user *) datap,
+					&siginfo);
 		break;
 
 	case PTRACE_SETSIGINFO:
 		memset(&siginfo, 0, sizeof siginfo);
 		if (copy_siginfo_from_user32(
-			    &siginfo, (struct compat_siginfo __user *) datap))
+					&siginfo, (struct compat_siginfo __user *) datap))
 			ret = -EFAULT;
 		else
 			ret = ptrace_setsiginfo(child, &siginfo);
diff -Naur linux-2.6.35.utrace.vm2/mm/memory.c linux-2.6.35.utrace.vm3/mm/memory.c
--- linux-2.6.35.utrace.vm2/mm/memory.c	2010-08-11 22:06:36.000000000 +0200
+++ linux-2.6.35.utrace.vm3/mm/memory.c	2010-08-11 22:10:24.000000000 +0200
@@ -3460,6 +3460,75 @@
 }
 
 /*
+ * Access another process' address space to/from user space
+ * Do not walk the page table directly, use get_user_pages
+ */
+int access_process_vm_user(struct task_struct *tsk, unsigned long addr,
+		char __user *ubuf, int len, int write, int string)
+{
+	struct mm_struct *mm;
+	struct vm_area_struct *vma;
+	struct page *page;
+	char *buf;
+	unsigned long old_addr = addr;
+
+	mm = get_task_mm(tsk);
+	if (!mm)
+		return 0;
+
+	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
+	if (!buf)
+		return 0;
+
+	down_read(&mm->mmap_sem);
+	/* ignore errors, just check how much was sucessfully transfered */
+	while (len) {
+		int bytes, ret, offset;
+		void *maddr;
+
+		ret = get_user_pages(tsk, mm, addr, 1,
+				write, 1, &page, &vma);
+		if (ret <= 0)
+			break;
+
+		bytes = len;
+		offset = addr & (PAGE_SIZE-1);
+		if (bytes > PAGE_SIZE-offset)
+			bytes = PAGE_SIZE-offset;
+
+		maddr = kmap(page);
+		if (write) {
+			__copy_from_user(buf, ubuf, bytes);
+			copy_to_user_page(vma, page, addr,
+					maddr + offset, buf, bytes);
+			if (!PageCompound(page))
+				set_page_dirty_lock(page);
+		} else {
+			copy_from_user_page(vma, page, addr,
+					buf, maddr + offset, bytes);
+			if (string) {
+				for (offset = 0; offset < bytes; offset++)
+					if (buf[offset] == 0)
+						break;
+				if (offset < bytes)
+					bytes = len = offset + 1;
+			}
+			ret = __copy_to_user(ubuf, buf, bytes);
+		}
+		kunmap(page);
+		page_cache_release(page);
+		len -= bytes;
+		ubuf += bytes;
+		addr += bytes;
+	}
+	up_read(&mm->mmap_sem);
+	mmput(mm);
+
+	kfree(buf);
+	return addr - old_addr;
+}
+
+/*
  * Print the name of a VMA.
  */
 void print_vma_addr(char *prefix, unsigned long ip)
diff -Naur linux-2.6.35.utrace.vm2/mm/nommu.c linux-2.6.35.utrace.vm3/mm/nommu.c
--- linux-2.6.35.utrace.vm2/mm/nommu.c	2010-08-11 22:06:36.000000000 +0200
+++ linux-2.6.35.utrace.vm3/mm/nommu.c	2010-08-11 22:10:24.000000000 +0200
@@ -2003,3 +2003,15 @@
 	up_write(&nommu_region_sem);
 	return 0;
 }
+
+/*
+ * Access another process' address space to/from user space
+ * Do not walk the page table directly, use get_user_pages
+ * Unimplemented yet. 
+ */
+int access_process_vm_user(struct task_struct *tsk, unsigned long addr,
+		    char __user *ubuf, int len, int write, int string)
+{
+	return -EIO;
+}
+