1 /* $OpenBSD: sys_process.c,v 1.102 2024/10/08 12:02:24 claudio Exp $ */
2 /* $NetBSD: sys_process.c,v 1.55 1996/05/15 06:17:47 tls Exp $ */
3
4 /*-
5 * Copyright (c) 1994 Christopher G. Demetriou. All rights reserved.
6 * Copyright (c) 1982, 1986, 1989, 1993
7 * The Regents of the University of California. All rights reserved.
8 * (c) UNIX System Laboratories, Inc.
9 * All or some portions of this file are derived from material licensed
10 * to the University of California by American Telephone and Telegraph
11 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
12 * the permission of UNIX System Laboratories, Inc.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * from: @(#)sys_process.c 8.1 (Berkeley) 6/10/93
39 */
40
41 /*
42 * References:
43 * (1) Bach's "The Design of the UNIX Operating System",
44 * (2) sys/miscfs/procfs from UCB's 4.4BSD-Lite distribution,
45 * (3) the "4.4BSD Programmer's Reference Manual" published
46 * by USENIX and O'Reilly & Associates.
47 * The 4.4BSD PRM does a reasonably good job of documenting what the various
48 * ptrace() requests should actually do, and its text is quoted several times
49 * in this file.
50 */
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/exec.h>
55 #include <sys/proc.h>
56 #include <sys/signalvar.h>
57 #include <sys/errno.h>
58 #include <sys/malloc.h>
59 #include <sys/ptrace.h>
60 #include <sys/uio.h>
61 #include <sys/sched.h>
62 #include <sys/exec_elf.h>
63
64 #include <sys/mount.h>
65 #include <sys/syscallargs.h>
66
67 #include <uvm/uvm_extern.h>
68
69 #include <machine/reg.h>
70
71 #ifdef PTRACE
72
73 static inline int process_checktracestate(struct process *_curpr,
74 struct process *_tr, struct proc *_t);
75 static inline struct process *process_tprfind(pid_t _tpid, struct proc **_tp);
76
77 int ptrace_ctrl(struct proc *, int, pid_t, caddr_t, int);
78 int ptrace_ustate(struct proc *, int, pid_t, void *, int, register_t *);
79 int ptrace_kstate(struct proc *, int, pid_t, void *);
80
81 int global_ptrace; /* permit tracing of not children */
82
83
84 /*
85 * Process debugging system call.
86 */
87 int
sys_ptrace(struct proc * p,void * v,register_t * retval)88 sys_ptrace(struct proc *p, void *v, register_t *retval)
89 {
90 struct sys_ptrace_args /* {
91 syscallarg(int) req;
92 syscallarg(pid_t) pid;
93 syscallarg(caddr_t) addr;
94 syscallarg(int) data;
95 } */ *uap = v;
96 int req = SCARG(uap, req);
97 pid_t pid = SCARG(uap, pid);
98 caddr_t uaddr = SCARG(uap, addr); /* userspace */
99 void *kaddr = NULL; /* kernelspace */
100 int data = SCARG(uap, data);
101 union {
102 struct ptrace_thread_state u_pts;
103 struct ptrace_io_desc u_piod;
104 struct ptrace_event u_pe;
105 struct ptrace_state u_ps;
106 register_t u_wcookie;
107 register_t u_pacmask[2];
108 } u;
109 int size = 0;
110 enum { NONE, IN, IN_ALLOC, OUT, OUT_ALLOC, IN_OUT } mode;
111 int kstate = 0;
112 int error;
113
114 *retval = 0;
115
116 /* Figure out what sort of copyin/out operations we'll do */
117 switch (req) {
118 case PT_TRACE_ME:
119 case PT_CONTINUE:
120 case PT_KILL:
121 case PT_ATTACH:
122 case PT_DETACH:
123 #ifdef PT_STEP
124 case PT_STEP:
125 #endif
126 /* control operations do no copyin/out; dispatch directly */
127 return ptrace_ctrl(p, req, pid, uaddr, data);
128
129 case PT_READ_I:
130 case PT_READ_D:
131 case PT_WRITE_I:
132 case PT_WRITE_D:
133 mode = NONE;
134 break;
135 case PT_IO:
136 mode = IN_OUT;
137 size = sizeof u.u_piod;
138 data = size; /* suppress the data == size check */
139 break;
140 case PT_GET_THREAD_FIRST:
141 mode = OUT;
142 size = sizeof u.u_pts;
143 kstate = 1;
144 break;
145 case PT_GET_THREAD_NEXT:
146 mode = IN_OUT;
147 size = sizeof u.u_pts;
148 kstate = 1;
149 break;
150 case PT_GET_EVENT_MASK:
151 mode = OUT;
152 size = sizeof u.u_pe;
153 kstate = 1;
154 break;
155 case PT_SET_EVENT_MASK:
156 mode = IN;
157 size = sizeof u.u_pe;
158 kstate = 1;
159 break;
160 case PT_GET_PROCESS_STATE:
161 mode = OUT;
162 size = sizeof u.u_ps;
163 kstate = 1;
164 break;
165 case PT_GETREGS:
166 mode = OUT_ALLOC;
167 size = sizeof(struct reg);
168 break;
169 case PT_SETREGS:
170 mode = IN_ALLOC;
171 size = sizeof(struct reg);
172 break;
173 #ifdef PT_GETFPREGS
174 case PT_GETFPREGS:
175 mode = OUT_ALLOC;
176 size = sizeof(struct fpreg);
177 break;
178 #endif
179 #ifdef PT_SETFPREGS
180 case PT_SETFPREGS:
181 mode = IN_ALLOC;
182 size = sizeof(struct fpreg);
183 break;
184 #endif
185 #ifdef PT_GETXMMREGS
186 case PT_GETXMMREGS:
187 mode = OUT_ALLOC;
188 size = sizeof(struct xmmregs);
189 break;
190 #endif
191 #ifdef PT_SETXMMREGS
192 case PT_SETXMMREGS:
193 mode = IN_ALLOC;
194 size = sizeof(struct xmmregs);
195 break;
196 #endif
197 #ifdef PT_WCOOKIE
198 case PT_WCOOKIE:
199 mode = OUT;
200 size = sizeof u.u_wcookie;
201 data = size; /* suppress the data == size check */
202 break;
203 #endif
204 #ifdef PT_PACMASK
205 case PT_PACMASK:
206 mode = OUT;
207 size = sizeof u.u_pacmask;
208 break;
209 #endif
210 default:
211 return EINVAL;
212 }
213
214
215 /* Now do any copyin()s and allocations in a consistent manner */
216 switch (mode) {
217 case NONE:
218 kaddr = uaddr;
219 break;
220 case IN:
221 case IN_OUT:
222 case OUT:
223 KASSERT(size <= sizeof u);
224 if (data != size)
225 return EINVAL;
226 if (mode == OUT)
227 memset(&u, 0, size);
228 else { /* IN or IN_OUT */
229 if ((error = copyin(uaddr, &u, size)))
230 return error;
231 }
232 kaddr = &u;
233 break;
234 case IN_ALLOC:
235 kaddr = malloc(size, M_TEMP, M_WAITOK);
236 if ((error = copyin(uaddr, kaddr, size))) {
237 free(kaddr, M_TEMP, size);
238 return error;
239 }
240 break;
241 case OUT_ALLOC:
242 kaddr = malloc(size, M_TEMP, M_WAITOK | M_ZERO);
243 break;
244 }
245
246 if (kstate)
247 error = ptrace_kstate(p, req, pid, kaddr);
248 else
249 error = ptrace_ustate(p, req, pid, kaddr, data, retval);
250
251 /* Do any copyout()s and frees */
252 if (error == 0) {
253 switch (mode) {
254 case NONE:
255 case IN:
256 case IN_ALLOC:
257 break;
258 case IN_OUT:
259 case OUT:
260 error = copyout(&u, uaddr, size);
261 if (req == PT_IO) {
262 /* historically, errors here are ignored */
263 error = 0;
264 }
265 break;
266 case OUT_ALLOC:
267 error = copyout(kaddr, uaddr, size);
268 break;
269 }
270 }
271
272 if (mode == IN_ALLOC || mode == OUT_ALLOC)
273 free(kaddr, M_TEMP, size);
274 return error;
275 }
276
277 /*
278 * ptrace control requests: attach, detach, continue, kill, single-step, etc
279 */
280 int
ptrace_ctrl(struct proc * p,int req,pid_t pid,caddr_t addr,int data)281 ptrace_ctrl(struct proc *p, int req, pid_t pid, caddr_t addr, int data)
282 {
283 struct proc *t; /* target thread */
284 struct process *tr; /* target process */
285 int error = 0;
286
287 switch (req) {
288 case PT_TRACE_ME:
289 /* Just set the trace flag. */
290 tr = p->p_p;
291 mtx_enter(&tr->ps_mtx);
292 if (ISSET(tr->ps_flags, PS_TRACED)) {
293 mtx_leave(&tr->ps_mtx);
294 return EBUSY;
295 }
296 atomic_setbits_int(&tr->ps_flags, PS_TRACED);
297 tr->ps_opptr = tr->ps_pptr;
298 mtx_leave(&tr->ps_mtx);
299 if (tr->ps_ptstat == NULL)
300 tr->ps_ptstat = malloc(sizeof(*tr->ps_ptstat),
301 M_SUBPROC, M_WAITOK);
302 memset(tr->ps_ptstat, 0, sizeof(*tr->ps_ptstat));
303 return 0;
304
305 /* calls that only operate on the PID */
306 case PT_KILL:
307 case PT_ATTACH:
308 case PT_DETACH:
309 /* Find the process we're supposed to be operating on. */
310 if ((tr = prfind(pid)) == NULL) {
311 error = ESRCH;
312 goto fail;
313 }
314 t = TAILQ_FIRST(&tr->ps_threads);
315 break;
316
317 /* calls that accept a PID or a thread ID */
318 case PT_CONTINUE:
319 #ifdef PT_STEP
320 case PT_STEP:
321 #endif
322 if ((tr = process_tprfind(pid, &t)) == NULL) {
323 error = ESRCH;
324 goto fail;
325 }
326 break;
327 }
328
329 /* Check permissions/state */
330 if (req != PT_ATTACH) {
331 /* Check that the data is a valid signal number or zero. */
332 if (req != PT_KILL && (data < 0 || data >= NSIG)) {
333 error = EINVAL;
334 goto fail;
335 }
336
337 /* Most operations require the target to already be traced */
338 if ((error = process_checktracestate(p->p_p, tr, t)))
339 goto fail;
340
341 /* Do single-step fixup if needed. */
342 FIX_SSTEP(t);
343 } else {
344 /*
345 * PT_ATTACH is the opposite; you can't attach to a process if:
346 * (1) it's the process that's doing the attaching,
347 */
348 if (tr == p->p_p) {
349 error = EINVAL;
350 goto fail;
351 }
352
353 /*
354 * (2) it's a system process
355 */
356 if (ISSET(tr->ps_flags, PS_SYSTEM)) {
357 error = EPERM;
358 goto fail;
359 }
360
361 /*
362 * (3) it's already being traced, or
363 */
364 if (ISSET(tr->ps_flags, PS_TRACED)) {
365 error = EBUSY;
366 goto fail;
367 }
368
369 /*
370 * (4) it's in the middle of execve(2)
371 */
372 if (ISSET(tr->ps_flags, PS_INEXEC)) {
373 error = EAGAIN;
374 goto fail;
375 }
376
377 /*
378 * (5) it's not owned by you, or the last exec
379 * gave us setuid/setgid privs (unless
380 * you're root), or...
381 *
382 * [Note: once PS_SUGID or PS_SUGIDEXEC gets set in
383 * execve(), they stay set until the process does
384 * another execve(). Hence this prevents a setuid
385 * process which revokes its special privileges using
386 * setuid() from being traced. This is good security.]
387 */
388 if ((tr->ps_ucred->cr_ruid != p->p_ucred->cr_ruid ||
389 ISSET(tr->ps_flags, PS_SUGIDEXEC | PS_SUGID)) &&
390 (error = suser(p)) != 0)
391 goto fail;
392
393 /*
394 * (5.5) it's not a child of the tracing process.
395 */
396 if (global_ptrace == 0 && !inferior(tr, p->p_p) &&
397 (error = suser(p)) != 0)
398 goto fail;
399
400 /*
401 * (6) ...it's init, which controls the security level
402 * of the entire system, and the system was not
403 * compiled with permanently insecure mode turned
404 * on.
405 */
406 if ((tr->ps_pid == 1) && (securelevel > -1)) {
407 error = EPERM;
408 goto fail;
409 }
410
411 /*
412 * (7) it's an ancestor of the current process and
413 * not init (because that would create a loop in
414 * the process graph).
415 */
416 if (tr->ps_pid != 1 && inferior(p->p_p, tr)) {
417 error = EINVAL;
418 goto fail;
419 }
420 }
421
422 switch (req) {
423
424 #ifdef PT_STEP
425 case PT_STEP:
426 /*
427 * From the 4.4BSD PRM:
428 * "Execution continues as in request PT_CONTINUE; however
429 * as soon as possible after execution of at least one
430 * instruction, execution stops again. [ ... ]"
431 */
432 #endif
433 case PT_CONTINUE:
434 /*
435 * From the 4.4BSD PRM:
436 * "The data argument is taken as a signal number and the
437 * child's execution continues at location addr as if it
438 * incurred that signal. Normally the signal number will
439 * be either 0 to indicate that the signal that caused the
440 * stop should be ignored, or that value fetched out of
441 * the process's image indicating which signal caused
442 * the stop. If addr is (int *)1 then execution continues
443 * from where it stopped."
444 */
445
446 if (pid < THREAD_PID_OFFSET && tr->ps_single)
447 t = tr->ps_single;
448 else if (t == tr->ps_single)
449 atomic_setbits_int(&t->p_flag, P_TRACESINGLE);
450 else {
451 error = EINVAL;
452 goto fail;
453 }
454
455
456 /* If the address parameter is not (int *)1, set the pc. */
457 if ((int *)addr != (int *)1)
458 if ((error = process_set_pc(t, addr)) != 0)
459 goto fail;
460
461 #ifdef PT_STEP
462 /*
463 * Arrange for a single-step, if that's requested and possible.
464 */
465 error = process_sstep(t, req == PT_STEP);
466 if (error)
467 goto fail;
468 #endif
469 goto sendsig;
470
471 case PT_DETACH:
472 /*
473 * From the 4.4BSD PRM:
474 * "The data argument is taken as a signal number and the
475 * child's execution continues at location addr as if it
476 * incurred that signal. Normally the signal number will
477 * be either 0 to indicate that the signal that caused the
478 * stop should be ignored, or that value fetched out of
479 * the process's image indicating which signal caused
480 * the stop. If addr is (int *)1 then execution continues
481 * from where it stopped."
482 */
483
484 if (pid < THREAD_PID_OFFSET && tr->ps_single)
485 t = tr->ps_single;
486
487 #ifdef PT_STEP
488 /*
489 * Stop single stepping.
490 */
491 error = process_sstep(t, 0);
492 if (error)
493 goto fail;
494 #endif
495
496 mtx_enter(&tr->ps_mtx);
497 process_untrace(tr);
498 atomic_clearbits_int(&tr->ps_flags, PS_WAITED);
499 mtx_leave(&tr->ps_mtx);
500
501 sendsig:
502 memset(tr->ps_ptstat, 0, sizeof(*tr->ps_ptstat));
503
504 /* Finally, deliver the requested signal (or none). */
505 if (t->p_stat == SSTOP) {
506 tr->ps_xsig = data;
507 SCHED_LOCK();
508 unsleep(t);
509 setrunnable(t);
510 SCHED_UNLOCK();
511 } else {
512 if (data != 0)
513 psignal(t, data);
514 }
515 break;
516
517 case PT_KILL:
518 if (pid < THREAD_PID_OFFSET && tr->ps_single)
519 t = tr->ps_single;
520
521 /* just send the process a KILL signal. */
522 data = SIGKILL;
523 goto sendsig; /* in PT_CONTINUE, above. */
524
525 case PT_ATTACH:
526 /*
527 * As was done in procfs:
528 * Go ahead and set the trace flag.
529 * Save the old parent (it's reset in
530 * _DETACH, and also in kern_exit.c:wait4()
531 * Reparent the process so that the tracing
532 * proc gets to see all the action.
533 * Stop the target.
534 */
535 mtx_enter(&tr->ps_mtx);
536 atomic_setbits_int(&tr->ps_flags, PS_TRACED);
537 tr->ps_opptr = tr->ps_pptr;
538 process_reparent(tr, p->p_p);
539 mtx_leave(&tr->ps_mtx);
540 if (tr->ps_ptstat == NULL)
541 tr->ps_ptstat = malloc(sizeof(*tr->ps_ptstat),
542 M_SUBPROC, M_WAITOK);
543 data = SIGSTOP;
544 goto sendsig;
545 default:
546 KASSERTMSG(0, "%s: unhandled request %d", __func__, req);
547 break;
548 }
549
550 fail:
551 return error;
552 }
553
554 /*
555 * ptrace kernel-state requests: thread list, event mask, process state
556 */
557 int
ptrace_kstate(struct proc * p,int req,pid_t pid,void * addr)558 ptrace_kstate(struct proc *p, int req, pid_t pid, void *addr)
559 {
560 struct process *tr; /* target process */
561 struct ptrace_event *pe = addr;
562 int error;
563
564 KASSERT((p->p_flag & P_SYSTEM) == 0);
565
566 /* Find the process we're supposed to be operating on. */
567 if ((tr = prfind(pid)) == NULL)
568 return ESRCH;
569
570 if ((error = process_checktracestate(p->p_p, tr, NULL)))
571 return error;
572
573 switch (req) {
574 case PT_GET_THREAD_FIRST:
575 case PT_GET_THREAD_NEXT:
576 {
577 struct ptrace_thread_state *pts = addr;
578 struct proc *t;
579
580 if (req == PT_GET_THREAD_NEXT) {
581 t = tfind_user(pts->pts_tid, tr);
582 if (t == NULL || ISSET(t->p_flag, P_WEXIT))
583 return ESRCH;
584 t = TAILQ_NEXT(t, p_thr_link);
585 } else {
586 t = TAILQ_FIRST(&tr->ps_threads);
587 }
588
589 if (t == NULL)
590 pts->pts_tid = -1;
591 else
592 pts->pts_tid = t->p_tid + THREAD_PID_OFFSET;
593 return 0;
594 }
595 }
596
597 switch (req) {
598 case PT_GET_EVENT_MASK:
599 pe->pe_set_event = tr->ps_ptmask;
600 break;
601 case PT_SET_EVENT_MASK:
602 tr->ps_ptmask = pe->pe_set_event;
603 break;
604 case PT_GET_PROCESS_STATE:
605 if (tr->ps_single)
606 tr->ps_ptstat->pe_tid =
607 tr->ps_single->p_tid + THREAD_PID_OFFSET;
608 memcpy(addr, tr->ps_ptstat, sizeof *tr->ps_ptstat);
609 break;
610 default:
611 KASSERTMSG(0, "%s: unhandled request %d", __func__, req);
612 break;
613 }
614
615 return 0;
616 }
617
618 /*
619 * ptrace user-state requests: memory access, registers, stack cookie
620 */
621 int
ptrace_ustate(struct proc * p,int req,pid_t pid,void * addr,int data,register_t * retval)622 ptrace_ustate(struct proc *p, int req, pid_t pid, void *addr, int data,
623 register_t *retval)
624 {
625 struct proc *t; /* target thread */
626 struct process *tr; /* target process */
627 struct uio uio;
628 struct iovec iov;
629 int error, write;
630 int temp = 0;
631
632 KASSERT((p->p_flag & P_SYSTEM) == 0);
633
634 /* Accept either PID or TID */
635 if ((tr = process_tprfind(pid, &t)) == NULL)
636 return ESRCH;
637
638 if ((error = process_checktracestate(p->p_p, tr, t)))
639 return error;
640
641 FIX_SSTEP(t);
642
643 /* Now do the operation. */
644 write = 0;
645
646 if ((error = process_checkioperm(p, tr)) != 0)
647 return error;
648
649 switch (req) {
650 case PT_WRITE_I: /* XXX no separate I and D spaces */
651 case PT_WRITE_D:
652 write = 1;
653 temp = data;
654 case PT_READ_I: /* XXX no separate I and D spaces */
655 case PT_READ_D:
656 /* write = 0 done above. */
657 iov.iov_base = (caddr_t)&temp;
658 iov.iov_len = sizeof(int);
659 uio.uio_iov = &iov;
660 uio.uio_iovcnt = 1;
661 uio.uio_offset = (off_t)(vaddr_t)addr;
662 uio.uio_resid = sizeof(int);
663 uio.uio_segflg = UIO_SYSSPACE;
664 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
665 uio.uio_procp = p;
666 error = process_domem(p, tr, &uio, write ? PT_WRITE_I :
667 PT_READ_I);
668 if (write == 0)
669 *retval = temp;
670 return error;
671
672 case PT_IO:
673 {
674 struct ptrace_io_desc *piod = addr;
675
676 iov.iov_base = piod->piod_addr;
677 iov.iov_len = piod->piod_len;
678 uio.uio_iov = &iov;
679 uio.uio_iovcnt = 1;
680 uio.uio_offset = (off_t)(vaddr_t)piod->piod_offs;
681 uio.uio_resid = piod->piod_len;
682 uio.uio_segflg = UIO_USERSPACE;
683 uio.uio_procp = p;
684 switch (piod->piod_op) {
685 case PIOD_READ_I:
686 req = PT_READ_I;
687 uio.uio_rw = UIO_READ;
688 break;
689 case PIOD_READ_D:
690 req = PT_READ_D;
691 uio.uio_rw = UIO_READ;
692 break;
693 case PIOD_WRITE_I:
694 req = PT_WRITE_I;
695 uio.uio_rw = UIO_WRITE;
696 break;
697 case PIOD_WRITE_D:
698 req = PT_WRITE_D;
699 uio.uio_rw = UIO_WRITE;
700 break;
701 case PIOD_READ_AUXV:
702 req = PT_READ_D;
703 uio.uio_rw = UIO_READ;
704 temp = ELF_AUX_WORDS * sizeof(char *);
705 if (uio.uio_offset > temp)
706 return EIO;
707 if (uio.uio_resid > temp - uio.uio_offset)
708 uio.uio_resid = temp - uio.uio_offset;
709 piod->piod_len = iov.iov_len = uio.uio_resid;
710 uio.uio_offset += tr->ps_auxinfo;
711 #ifdef MACHINE_STACK_GROWS_UP
712 if (uio.uio_offset < (off_t)tr->ps_strings)
713 return EIO;
714 #else
715 if (uio.uio_offset > (off_t)tr->ps_strings)
716 return EIO;
717 if ((uio.uio_offset + uio.uio_resid) >
718 (off_t)tr->ps_strings)
719 uio.uio_resid = (off_t)tr->ps_strings -
720 uio.uio_offset;
721 #endif
722 break;
723 default:
724 return EINVAL;
725 }
726 error = process_domem(p, tr, &uio, req);
727 piod->piod_len -= uio.uio_resid;
728 return error;
729 }
730
731 case PT_SETREGS:
732 return process_write_regs(t, addr);
733 case PT_GETREGS:
734 return process_read_regs(t, addr);
735
736 #ifdef PT_SETFPREGS
737 case PT_SETFPREGS:
738 return process_write_fpregs(t, addr);
739 #endif
740 #ifdef PT_SETFPREGS
741 case PT_GETFPREGS:
742 return process_read_fpregs(t, addr);
743 #endif
744 #ifdef PT_SETXMMREGS
745 case PT_SETXMMREGS:
746 return process_write_xmmregs(t, addr);
747 #endif
748 #ifdef PT_SETXMMREGS
749 case PT_GETXMMREGS:
750 return process_read_xmmregs(t, addr);
751 #endif
752 #ifdef PT_WCOOKIE
753 case PT_WCOOKIE:
754 *(register_t *)addr = process_get_wcookie(t);
755 return 0;
756 #endif
757 #ifdef PT_PACMASK
758 case PT_PACMASK:
759 ((register_t *)addr)[0] = process_get_pacmask(t);
760 ((register_t *)addr)[1] = process_get_pacmask(t);
761 return 0;
762 #endif
763 default:
764 KASSERTMSG(0, "%s: unhandled request %d", __func__, req);
765 break;
766 }
767
768 return 0;
769 }
770
771
772 /*
773 * Helper for doing "it could be a PID or TID" lookup. On failure
774 * returns NULL; on success returns the selected process and sets *tp
775 * to an appropriate thread in that process.
776 */
777 static inline struct process *
process_tprfind(pid_t tpid,struct proc ** tp)778 process_tprfind(pid_t tpid, struct proc **tp)
779 {
780 if (tpid > THREAD_PID_OFFSET) {
781 struct proc *t = tfind(tpid - THREAD_PID_OFFSET);
782
783 if (t == NULL)
784 return NULL;
785 *tp = t;
786 return t->p_p;
787 } else {
788 struct process *tr = prfind(tpid);
789
790 if (tr == NULL)
791 return NULL;
792 *tp = TAILQ_FIRST(&tr->ps_threads);
793 return tr;
794 }
795 }
796
797
798 /*
799 * Check whether 'tr' is currently traced by 'curpr' and in a state
800 * to be manipulated. If 't' is supplied then it must be stopped and
801 * waited for.
802 */
803 static inline int
process_checktracestate(struct process * curpr,struct process * tr,struct proc * t)804 process_checktracestate(struct process *curpr, struct process *tr,
805 struct proc *t)
806 {
807 /*
808 * You can't do what you want to the process if:
809 * (1) It's not being traced at all,
810 */
811 if (!ISSET(tr->ps_flags, PS_TRACED))
812 return EPERM;
813
814 /*
815 * (2) it's not being traced by _you_, or
816 */
817 if (tr->ps_pptr != curpr)
818 return EBUSY;
819
820 /*
821 * (3) it's in the middle of execve(2)
822 */
823 if (ISSET(tr->ps_flags, PS_INEXEC))
824 return EAGAIN;
825
826 /*
827 * (4) if a thread was specified and it's not currently stopped.
828 */
829 if (t != NULL &&
830 (t->p_stat != SSTOP || !ISSET(tr->ps_flags, PS_WAITED)))
831 return EBUSY;
832
833 return 0;
834 }
835
836 #endif /* PTRACE */
837
838 /*
839 * Check if a process is allowed to fiddle with the memory of another.
840 *
841 * p = tracer
842 * tr = tracee
843 *
844 * 1. You can't attach to a process not owned by you or one that has raised
845 * its privileges.
846 * 1a. ...unless you are root.
847 *
848 * 2. init is always off-limits because it can control the securelevel.
849 * 2a. ...unless securelevel is permanently set to insecure.
850 *
851 * 3. Processes that are in the process of doing an exec() are always
852 * off-limits because of the can of worms they are. Just wait a
853 * second.
854 */
855 int
process_checkioperm(struct proc * p,struct process * tr)856 process_checkioperm(struct proc *p, struct process *tr)
857 {
858 int error;
859
860 if ((tr->ps_ucred->cr_ruid != p->p_ucred->cr_ruid ||
861 ISSET(tr->ps_flags, PS_SUGIDEXEC | PS_SUGID)) &&
862 (error = suser(p)) != 0)
863 return (error);
864
865 if ((tr->ps_pid == 1) && (securelevel > -1))
866 return (EPERM);
867
868 if (ISSET(tr->ps_flags, PS_INEXEC))
869 return (EAGAIN);
870
871 return (0);
872 }
873
874 int
process_domem(struct proc * curp,struct process * tr,struct uio * uio,int req)875 process_domem(struct proc *curp, struct process *tr, struct uio *uio, int req)
876 {
877 struct vmspace *vm;
878 int error;
879 vaddr_t addr;
880 vsize_t len;
881
882 len = uio->uio_resid;
883 if (len == 0)
884 return 0;
885
886 if ((error = process_checkioperm(curp, tr)) != 0)
887 return error;
888
889 vm = tr->ps_vmspace;
890 if ((tr->ps_flags & PS_EXITING) || (vm->vm_refcnt < 1))
891 return EFAULT;
892 addr = uio->uio_offset;
893
894 uvmspace_addref(vm);
895
896 error = uvm_io(&vm->vm_map, uio, UVM_IO_FIXPROT);
897
898 uvmspace_free(vm);
899
900 if (error == 0 && req == PT_WRITE_I)
901 pmap_proc_iflush(tr, addr, len);
902
903 return error;
904 }
905