This source file includes following definitions.
- sys_ptrace
- process_checkioperm
- process_domem
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 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/proc.h>
55 #include <sys/signalvar.h>
56 #include <sys/errno.h>
57 #include <sys/malloc.h>
58 #include <sys/ptrace.h>
59 #include <sys/uio.h>
60 #include <sys/user.h>
61 #include <sys/sched.h>
62
63 #include <sys/mount.h>
64 #include <sys/syscallargs.h>
65
66 #include <uvm/uvm_extern.h>
67
68 #include <machine/reg.h>
69
70 #ifdef PTRACE
71
72
73
74 int
75 sys_ptrace(struct proc *p, void *v, register_t *retval)
76 {
77 struct sys_ptrace_args
78
79
80
81
82 *uap = v;
83 struct proc *t;
84 struct uio uio;
85 struct iovec iov;
86 struct ptrace_io_desc piod;
87 struct ptrace_event pe;
88 struct reg *regs;
89 #if defined (PT_SETFPREGS) || defined (PT_GETFPREGS)
90 struct fpreg *fpregs;
91 #endif
92 #if defined (PT_SETXMMREGS) || defined (PT_GETXMMREGS)
93 struct xmmregs *xmmregs;
94 #endif
95 #ifdef PT_WCOOKIE
96 register_t wcookie;
97 #endif
98 int error, write;
99 int temp;
100 int req;
101 int s;
102
103
104 if (SCARG(uap, req) == PT_TRACE_ME)
105 t = p;
106 else {
107
108
109 if ((t = pfind(SCARG(uap, pid))) == NULL)
110 return (ESRCH);
111 }
112
113 if ((t->p_flag & P_INEXEC) != 0)
114 return (EAGAIN);
115
116
117 switch (SCARG(uap, req)) {
118 case PT_TRACE_ME:
119
120 break;
121
122 case PT_ATTACH:
123
124
125
126
127 if (t->p_pid == p->p_pid)
128 return (EINVAL);
129
130
131
132
133 if (ISSET(t->p_flag, P_SYSTEM))
134 return (EPERM);
135
136
137
138
139 if (ISSET(t->p_flag, P_TRACED))
140 return (EBUSY);
141
142
143
144
145
146
147
148
149
150
151
152
153 if ((t->p_cred->p_ruid != p->p_cred->p_ruid ||
154 ISSET(t->p_flag, P_SUGIDEXEC) ||
155 ISSET(t->p_flag, P_SUGID)) &&
156 (error = suser(p, 0)) != 0)
157 return (error);
158
159
160
161
162
163
164
165 if ((t->p_pid == 1) && (securelevel > -1))
166 return (EPERM);
167 break;
168
169 case PT_READ_I:
170 case PT_READ_D:
171 case PT_WRITE_I:
172 case PT_WRITE_D:
173 case PT_IO:
174 case PT_CONTINUE:
175 case PT_KILL:
176 case PT_DETACH:
177 #ifdef PT_STEP
178 case PT_STEP:
179 #endif
180 case PT_SET_EVENT_MASK:
181 case PT_GET_EVENT_MASK:
182 case PT_GET_PROCESS_STATE:
183 case PT_GETREGS:
184 case PT_SETREGS:
185 #ifdef PT_GETFPREGS
186 case PT_GETFPREGS:
187 #endif
188 #ifdef PT_SETFPREGS
189 case PT_SETFPREGS:
190 #endif
191 #ifdef PT_GETXMMREGS
192 case PT_GETXMMREGS:
193 #endif
194 #ifdef PT_SETXMMREGS
195 case PT_SETXMMREGS:
196 #endif
197 #ifdef PT_WCOOKIE
198 case PT_WCOOKIE:
199 #endif
200
201
202
203
204 if (!ISSET(t->p_flag, P_TRACED))
205 return (EPERM);
206
207
208
209
210 if (t->p_pptr != p)
211 return (EBUSY);
212
213
214
215
216 if (t->p_stat != SSTOP || !ISSET(t->p_flag, P_WAITED))
217 return (EBUSY);
218 break;
219
220 default:
221 return (EINVAL);
222 }
223
224
225 FIX_SSTEP(t);
226
227
228 write = 0;
229 *retval = 0;
230
231 switch (SCARG(uap, req)) {
232 case PT_TRACE_ME:
233
234 atomic_setbits_int(&t->p_flag, P_TRACED);
235 t->p_oppid = t->p_pptr->p_pid;
236 if (t->p_ptstat == NULL)
237 t->p_ptstat = malloc(sizeof(*t->p_ptstat),
238 M_SUBPROC, M_WAITOK);
239 bzero(t->p_ptstat, sizeof(*t->p_ptstat));
240 return (0);
241
242 case PT_WRITE_I:
243 case PT_WRITE_D:
244 write = 1;
245 temp = SCARG(uap, data);
246 case PT_READ_I:
247 case PT_READ_D:
248
249 iov.iov_base = (caddr_t)&temp;
250 iov.iov_len = sizeof(int);
251 uio.uio_iov = &iov;
252 uio.uio_iovcnt = 1;
253 uio.uio_offset = (off_t)(long)SCARG(uap, addr);
254 uio.uio_resid = sizeof(int);
255 uio.uio_segflg = UIO_SYSSPACE;
256 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
257 uio.uio_procp = p;
258 error = process_domem(p, t, &uio, write ? PT_WRITE_I :
259 PT_READ_I);
260 if (write == 0)
261 *retval = temp;
262 return (error);
263 case PT_IO:
264 error = copyin(SCARG(uap, addr), &piod, sizeof(piod));
265 if (error)
266 return (error);
267 iov.iov_base = piod.piod_addr;
268 iov.iov_len = piod.piod_len;
269 uio.uio_iov = &iov;
270 uio.uio_iovcnt = 1;
271 uio.uio_offset = (off_t)(long)piod.piod_offs;
272 uio.uio_resid = piod.piod_len;
273 uio.uio_segflg = UIO_USERSPACE;
274 uio.uio_procp = p;
275 switch (piod.piod_op) {
276 case PIOD_READ_I:
277 req = PT_READ_I;
278 uio.uio_rw = UIO_READ;
279 break;
280 case PIOD_READ_D:
281 req = PT_READ_D;
282 uio.uio_rw = UIO_READ;
283 break;
284 case PIOD_WRITE_I:
285 req = PT_WRITE_I;
286 uio.uio_rw = UIO_WRITE;
287 break;
288 case PIOD_WRITE_D:
289 req = PT_WRITE_D;
290 uio.uio_rw = UIO_WRITE;
291 break;
292 default:
293 return (EINVAL);
294 }
295 error = process_domem(p, t, &uio, req);
296 piod.piod_len -= uio.uio_resid;
297 (void) copyout(&piod, SCARG(uap, addr), sizeof(piod));
298 return (error);
299 #ifdef PT_STEP
300 case PT_STEP:
301
302
303
304
305
306
307 #endif
308 case PT_CONTINUE:
309
310
311
312
313
314
315
316
317
318
319
320
321
322 if (SCARG(uap, data) < 0 || SCARG(uap, data) >= NSIG)
323 return (EINVAL);
324
325
326 if ((int *)SCARG(uap, addr) != (int *)1)
327 if ((error = process_set_pc(t, SCARG(uap, addr))) != 0)
328 goto relebad;
329
330 #ifdef PT_STEP
331
332
333
334 error = process_sstep(t, SCARG(uap, req) == PT_STEP);
335 if (error)
336 goto relebad;
337 #endif
338 goto sendsig;
339
340 case PT_DETACH:
341
342
343
344
345
346
347
348
349
350
351
352
353
354 if (SCARG(uap, data) < 0 || SCARG(uap, data) >= NSIG)
355 return (EINVAL);
356
357 #ifdef PT_STEP
358
359
360
361 error = process_sstep(t, SCARG(uap, req) == PT_STEP);
362 if (error)
363 goto relebad;
364 #endif
365
366
367 if (t->p_oppid != t->p_pptr->p_pid) {
368 struct proc *pp;
369
370 pp = pfind(t->p_oppid);
371 proc_reparent(t, pp ? pp : initproc);
372 }
373
374
375 t->p_oppid = 0;
376 atomic_clearbits_int(&t->p_flag, P_TRACED|P_WAITED);
377
378 sendsig:
379 bzero(t->p_ptstat, sizeof(*t->p_ptstat));
380
381
382 if (t->p_stat == SSTOP) {
383 t->p_xstat = SCARG(uap, data);
384 SCHED_LOCK(s);
385 setrunnable(t);
386 SCHED_UNLOCK(s);
387 } else {
388 if (SCARG(uap, data) != 0)
389 psignal(t, SCARG(uap, data));
390 }
391 return (0);
392
393 relebad:
394 return (error);
395
396 case PT_KILL:
397
398 SCARG(uap, data) = SIGKILL;
399 goto sendsig;
400
401 case PT_ATTACH:
402
403
404
405
406
407
408
409
410
411 atomic_setbits_int(&t->p_flag, P_TRACED);
412 t->p_oppid = t->p_pptr->p_pid;
413 if (t->p_pptr != p)
414 proc_reparent(t, p);
415 if (t->p_ptstat == NULL)
416 t->p_ptstat = malloc(sizeof(*t->p_ptstat),
417 M_SUBPROC, M_WAITOK);
418 SCARG(uap, data) = SIGSTOP;
419 goto sendsig;
420
421 case PT_GET_EVENT_MASK:
422 if (SCARG(uap, data) != sizeof(pe))
423 return (EINVAL);
424 bzero(&pe, sizeof(pe));
425 pe.pe_set_event = t->p_ptmask;
426 return (copyout(&pe, SCARG(uap, addr), sizeof(pe)));
427 case PT_SET_EVENT_MASK:
428 if (SCARG(uap, data) != sizeof(pe))
429 return (EINVAL);
430 if ((error = copyin(SCARG(uap, addr), &pe, sizeof(pe))))
431 return (error);
432 t->p_ptmask = pe.pe_set_event;
433 return (0);
434
435 case PT_GET_PROCESS_STATE:
436 if (SCARG(uap, data) != sizeof(*t->p_ptstat))
437 return (EINVAL);
438 return (copyout(t->p_ptstat, SCARG(uap, addr),
439 sizeof(*t->p_ptstat)));
440
441 case PT_SETREGS:
442 KASSERT((p->p_flag & P_SYSTEM) == 0);
443 if ((error = process_checkioperm(p, t)) != 0)
444 return (error);
445
446 regs = malloc(sizeof(*regs), M_TEMP, M_WAITOK);
447 error = copyin(SCARG(uap, addr), regs, sizeof(*regs));
448 if (error == 0) {
449 error = process_write_regs(t, regs);
450 }
451 free(regs, M_TEMP);
452 return (error);
453 case PT_GETREGS:
454 KASSERT((p->p_flag & P_SYSTEM) == 0);
455 if ((error = process_checkioperm(p, t)) != 0)
456 return (error);
457
458 regs = malloc(sizeof(*regs), M_TEMP, M_WAITOK);
459 error = process_read_regs(t, regs);
460 if (error == 0)
461 error = copyout(regs,
462 SCARG(uap, addr), sizeof (*regs));
463 free(regs, M_TEMP);
464 return (error);
465 #ifdef PT_SETFPREGS
466 case PT_SETFPREGS:
467 KASSERT((p->p_flag & P_SYSTEM) == 0);
468 if ((error = process_checkioperm(p, t)) != 0)
469 return (error);
470
471 fpregs = malloc(sizeof(*fpregs), M_TEMP, M_WAITOK);
472 error = copyin(SCARG(uap, addr), fpregs, sizeof(*fpregs));
473 if (error == 0) {
474 error = process_write_fpregs(t, fpregs);
475 }
476 free(fpregs, M_TEMP);
477 return (error);
478 #endif
479 #ifdef PT_GETFPREGS
480 case PT_GETFPREGS:
481 KASSERT((p->p_flag & P_SYSTEM) == 0);
482 if ((error = process_checkioperm(p, t)) != 0)
483 return (error);
484
485 fpregs = malloc(sizeof(*fpregs), M_TEMP, M_WAITOK);
486 error = process_read_fpregs(t, fpregs);
487 if (error == 0)
488 error = copyout(fpregs,
489 SCARG(uap, addr), sizeof(*fpregs));
490 free(fpregs, M_TEMP);
491 return (error);
492 #endif
493 #ifdef PT_SETXMMREGS
494 case PT_SETXMMREGS:
495 KASSERT((p->p_flag & P_SYSTEM) == 0);
496 if ((error = process_checkioperm(p, t)) != 0)
497 return (error);
498
499 xmmregs = malloc(sizeof(*xmmregs), M_TEMP, M_WAITOK);
500 error = copyin(SCARG(uap, addr), xmmregs, sizeof(*xmmregs));
501 if (error == 0) {
502 error = process_write_xmmregs(t, xmmregs);
503 }
504 free(xmmregs, M_TEMP);
505 return (error);
506 #endif
507 #ifdef PT_GETXMMREGS
508 case PT_GETXMMREGS:
509 KASSERT((p->p_flag & P_SYSTEM) == 0);
510 if ((error = process_checkioperm(p, t)) != 0)
511 return (error);
512
513 xmmregs = malloc(sizeof(*xmmregs), M_TEMP, M_WAITOK);
514 error = process_read_xmmregs(t, xmmregs);
515 if (error == 0)
516 error = copyout(xmmregs,
517 SCARG(uap, addr), sizeof(*xmmregs));
518 free(xmmregs, M_TEMP);
519 return (error);
520 #endif
521 #ifdef PT_WCOOKIE
522 case PT_WCOOKIE:
523 wcookie = process_get_wcookie (t);
524 return (copyout(&wcookie, SCARG(uap, addr),
525 sizeof (register_t)));
526 #endif
527 }
528
529 #ifdef DIAGNOSTIC
530 panic("ptrace: impossible");
531 #endif
532 return 0;
533 }
534 #endif
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553 int
554 process_checkioperm(struct proc *p, struct proc *t)
555 {
556 int error;
557
558 if ((t->p_cred->p_ruid != p->p_cred->p_ruid ||
559 ISSET(t->p_flag, P_SUGIDEXEC) ||
560 ISSET(t->p_flag, P_SUGID)) &&
561 (error = suser(p, 0)) != 0)
562 return (error);
563
564 if ((t->p_pid == 1) && (securelevel > -1))
565 return (EPERM);
566
567 if (t->p_flag & P_INEXEC)
568 return (EAGAIN);
569
570 return (0);
571 }
572
573 int
574 process_domem(struct proc *curp, struct proc *p, struct uio *uio, int req)
575 {
576 int error;
577 vaddr_t addr;
578 vsize_t len;
579
580 len = uio->uio_resid;
581 if (len == 0)
582 return (0);
583
584 if ((error = process_checkioperm(curp, p)) != 0)
585 return (error);
586
587
588 if ((p->p_flag & P_WEXIT) || (p->p_vmspace->vm_refcnt < 1))
589 return(EFAULT);
590 addr = uio->uio_offset;
591 p->p_vmspace->vm_refcnt++;
592 error = uvm_io(&p->p_vmspace->vm_map, uio,
593 (req == PT_WRITE_I) ? UVM_IO_FIXPROT : 0);
594 uvmspace_free(p->p_vmspace);
595
596 if (error == 0 && req == PT_WRITE_I)
597 pmap_proc_iflush(p, addr, len);
598
599 return (error);
600 }