sys_process.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. /* $OpenBSD: sys_process.c,v 1.67 2015/01/20 19:43:21 kettenis Exp $ */
  2. /* $NetBSD: sys_process.c,v 1.55 1996/05/15 06:17:47 tls Exp $ */
  3. /*-
  4. * Copyright (c) 1994 Christopher G. Demetriou. All rights reserved.
  5. * Copyright (c) 1982, 1986, 1989, 1993
  6. * The Regents of the University of California. All rights reserved.
  7. * (c) UNIX System Laboratories, Inc.
  8. * All or some portions of this file are derived from material licensed
  9. * to the University of California by American Telephone and Telegraph
  10. * Co. or Unix System Laboratories, Inc. and are reproduced herein with
  11. * the permission of UNIX System Laboratories, Inc.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. * 1. Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. * 3. Neither the name of the University nor the names of its contributors
  22. * may be used to endorse or promote products derived from this software
  23. * without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  26. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35. * SUCH DAMAGE.
  36. *
  37. * from: @(#)sys_process.c 8.1 (Berkeley) 6/10/93
  38. */
  39. /*
  40. * References:
  41. * (1) Bach's "The Design of the UNIX Operating System",
  42. * (2) sys/miscfs/procfs from UCB's 4.4BSD-Lite distribution,
  43. * (3) the "4.4BSD Programmer's Reference Manual" published
  44. * by USENIX and O'Reilly & Associates.
  45. * The 4.4BSD PRM does a reasonably good job of documenting what the various
  46. * ptrace() requests should actually do, and its text is quoted several times
  47. * in this file.
  48. */
  49. #include <sys/param.h>
  50. #include <sys/systm.h>
  51. #include <sys/exec.h>
  52. #include <sys/proc.h>
  53. #include <sys/signalvar.h>
  54. #include <sys/errno.h>
  55. #include <sys/malloc.h>
  56. #include <sys/ptrace.h>
  57. #include <sys/uio.h>
  58. #include <sys/sched.h>
  59. #include <sys/mount.h>
  60. #include <sys/syscallargs.h>
  61. #include <uvm/uvm_extern.h>
  62. #include <machine/reg.h>
  63. int process_auxv_offset(struct proc *, struct proc *, struct uio *);
  64. #ifdef PTRACE
  65. int global_ptrace; /* permit tracing of not children */
  66. /*
  67. * Process debugging system call.
  68. */
  69. int
  70. sys_ptrace(struct proc *p, void *v, register_t *retval)
  71. {
  72. struct sys_ptrace_args /* {
  73. syscallarg(int) req;
  74. syscallarg(pid_t) pid;
  75. syscallarg(caddr_t) addr;
  76. syscallarg(int) data;
  77. } */ *uap = v;
  78. struct proc *t; /* target thread */
  79. struct process *tr; /* target process */
  80. struct uio uio;
  81. struct iovec iov;
  82. struct ptrace_io_desc piod;
  83. struct ptrace_event pe;
  84. struct ptrace_thread_state pts;
  85. struct reg *regs;
  86. #if defined (PT_SETFPREGS) || defined (PT_GETFPREGS)
  87. struct fpreg *fpregs;
  88. #endif
  89. #if defined (PT_SETXMMREGS) || defined (PT_GETXMMREGS)
  90. struct xmmregs *xmmregs;
  91. #endif
  92. #ifdef PT_WCOOKIE
  93. register_t wcookie;
  94. #endif
  95. int error, write;
  96. int temp;
  97. int req = SCARG(uap, req);
  98. int s;
  99. /* "A foolish consistency..." XXX */
  100. switch (req) {
  101. case PT_TRACE_ME:
  102. t = p;
  103. break;
  104. /* calls that only operate on the PID */
  105. case PT_READ_I:
  106. case PT_READ_D:
  107. case PT_WRITE_I:
  108. case PT_WRITE_D:
  109. case PT_KILL:
  110. case PT_ATTACH:
  111. case PT_IO:
  112. case PT_SET_EVENT_MASK:
  113. case PT_GET_EVENT_MASK:
  114. case PT_GET_PROCESS_STATE:
  115. case PT_GET_THREAD_FIRST:
  116. case PT_GET_THREAD_NEXT:
  117. default:
  118. /* Find the process we're supposed to be operating on. */
  119. if ((t = pfind(SCARG(uap, pid))) == NULL)
  120. return (ESRCH);
  121. if (t->p_flag & P_THREAD)
  122. return (ESRCH);
  123. break;
  124. /* calls that accept a PID or a thread ID */
  125. case PT_CONTINUE:
  126. case PT_DETACH:
  127. #ifdef PT_STEP
  128. case PT_STEP:
  129. #endif
  130. case PT_GETREGS:
  131. case PT_SETREGS:
  132. #ifdef PT_GETFPREGS
  133. case PT_GETFPREGS:
  134. #endif
  135. #ifdef PT_SETFPREGS
  136. case PT_SETFPREGS:
  137. #endif
  138. #ifdef PT_GETXMMREGS
  139. case PT_GETXMMREGS:
  140. #endif
  141. #ifdef PT_SETXMMREGS
  142. case PT_SETXMMREGS:
  143. #endif
  144. if (SCARG(uap, pid) > THREAD_PID_OFFSET) {
  145. t = pfind(SCARG(uap, pid) - THREAD_PID_OFFSET);
  146. if (t == NULL)
  147. return (ESRCH);
  148. } else {
  149. if ((t = pfind(SCARG(uap, pid))) == NULL)
  150. return (ESRCH);
  151. if (t->p_flag & P_THREAD)
  152. return (ESRCH);
  153. }
  154. break;
  155. }
  156. tr = t->p_p;
  157. if ((tr->ps_flags & PS_INEXEC) != 0)
  158. return (EAGAIN);
  159. /* Make sure we can operate on it. */
  160. switch (req) {
  161. case PT_TRACE_ME:
  162. /* Saying that you're being traced is always legal. */
  163. break;
  164. case PT_ATTACH:
  165. /*
  166. * You can't attach to a process if:
  167. * (1) it's the process that's doing the attaching,
  168. */
  169. if (tr == p->p_p)
  170. return (EINVAL);
  171. /*
  172. * (2) it's a system process
  173. */
  174. if (ISSET(tr->ps_flags, PS_SYSTEM))
  175. return (EPERM);
  176. /*
  177. * (3) it's already being traced, or
  178. */
  179. if (ISSET(tr->ps_flags, PS_TRACED))
  180. return (EBUSY);
  181. /*
  182. * (4) it's not owned by you, or the last exec
  183. * gave us setuid/setgid privs (unless
  184. * you're root), or...
  185. *
  186. * [Note: once PS_SUGID or PS_SUGIDEXEC gets set in
  187. * execve(), they stay set until the process does
  188. * another execve(). Hence this prevents a setuid
  189. * process which revokes its special privileges using
  190. * setuid() from being traced. This is good security.]
  191. */
  192. if ((tr->ps_ucred->cr_ruid != p->p_ucred->cr_ruid ||
  193. ISSET(tr->ps_flags, PS_SUGIDEXEC | PS_SUGID)) &&
  194. (error = suser(p, 0)) != 0)
  195. return (error);
  196. /*
  197. * (4.5) it's not a child of the tracing process.
  198. */
  199. if (global_ptrace == 0 && !inferior(tr, p->p_p) &&
  200. (error = suser(p, 0)) != 0)
  201. return (error);
  202. /*
  203. * (5) ...it's init, which controls the security level
  204. * of the entire system, and the system was not
  205. * compiled with permanently insecure mode turned
  206. * on.
  207. */
  208. if ((tr->ps_pid == 1) && (securelevel > -1))
  209. return (EPERM);
  210. /*
  211. * (6) it's an ancestor of the current process and
  212. * not init (because that would create a loop in
  213. * the process graph).
  214. */
  215. if (tr->ps_pid != 1 && inferior(p->p_p, tr))
  216. return (EINVAL);
  217. break;
  218. case PT_READ_I:
  219. case PT_READ_D:
  220. case PT_WRITE_I:
  221. case PT_WRITE_D:
  222. case PT_IO:
  223. case PT_CONTINUE:
  224. case PT_KILL:
  225. case PT_DETACH:
  226. #ifdef PT_STEP
  227. case PT_STEP:
  228. #endif
  229. case PT_SET_EVENT_MASK:
  230. case PT_GET_EVENT_MASK:
  231. case PT_GET_PROCESS_STATE:
  232. case PT_GETREGS:
  233. case PT_SETREGS:
  234. #ifdef PT_GETFPREGS
  235. case PT_GETFPREGS:
  236. #endif
  237. #ifdef PT_SETFPREGS
  238. case PT_SETFPREGS:
  239. #endif
  240. #ifdef PT_GETXMMREGS
  241. case PT_GETXMMREGS:
  242. #endif
  243. #ifdef PT_SETXMMREGS
  244. case PT_SETXMMREGS:
  245. #endif
  246. #ifdef PT_WCOOKIE
  247. case PT_WCOOKIE:
  248. #endif
  249. /*
  250. * You can't do what you want to the process if:
  251. * (1) It's not being traced at all,
  252. */
  253. if (!ISSET(tr->ps_flags, PS_TRACED))
  254. return (EPERM);
  255. /*
  256. * (2) it's not being traced by _you_, or
  257. */
  258. if (tr->ps_pptr != p->p_p)
  259. return (EBUSY);
  260. /*
  261. * (3) it's not currently stopped.
  262. */
  263. if (t->p_stat != SSTOP || !ISSET(tr->ps_flags, PS_WAITED))
  264. return (EBUSY);
  265. break;
  266. case PT_GET_THREAD_FIRST:
  267. case PT_GET_THREAD_NEXT:
  268. /*
  269. * You can't do what you want to the process if:
  270. * (1) It's not being traced at all,
  271. */
  272. if (!ISSET(tr->ps_flags, PS_TRACED))
  273. return (EPERM);
  274. /*
  275. * (2) it's not being traced by _you_, or
  276. */
  277. if (tr->ps_pptr != p->p_p)
  278. return (EBUSY);
  279. /*
  280. * Do the work here because the request isn't actually
  281. * associated with 't'
  282. */
  283. if (SCARG(uap, data) != sizeof(pts))
  284. return (EINVAL);
  285. if (req == PT_GET_THREAD_NEXT) {
  286. error = copyin(SCARG(uap, addr), &pts, sizeof(pts));
  287. if (error)
  288. return (error);
  289. t = pfind(pts.pts_tid - THREAD_PID_OFFSET);
  290. if (t == NULL || ISSET(t->p_flag, P_WEXIT))
  291. return (ESRCH);
  292. if (t->p_p != tr)
  293. return (EINVAL);
  294. t = TAILQ_NEXT(t, p_thr_link);
  295. } else {
  296. t = TAILQ_FIRST(&tr->ps_threads);
  297. }
  298. if (t == NULL)
  299. pts.pts_tid = -1;
  300. else
  301. pts.pts_tid = t->p_pid + THREAD_PID_OFFSET;
  302. return (copyout(&pts, SCARG(uap, addr), sizeof(pts)));
  303. default: /* It was not a legal request. */
  304. return (EINVAL);
  305. }
  306. /* Do single-step fixup if needed. */
  307. FIX_SSTEP(t);
  308. /* Now do the operation. */
  309. write = 0;
  310. *retval = 0;
  311. switch (req) {
  312. case PT_TRACE_ME:
  313. /* Just set the trace flag. */
  314. atomic_setbits_int(&tr->ps_flags, PS_TRACED);
  315. tr->ps_oppid = tr->ps_pptr->ps_pid;
  316. if (tr->ps_ptstat == NULL)
  317. tr->ps_ptstat = malloc(sizeof(*tr->ps_ptstat),
  318. M_SUBPROC, M_WAITOK);
  319. memset(tr->ps_ptstat, 0, sizeof(*tr->ps_ptstat));
  320. return (0);
  321. case PT_WRITE_I: /* XXX no separate I and D spaces */
  322. case PT_WRITE_D:
  323. write = 1;
  324. temp = SCARG(uap, data);
  325. case PT_READ_I: /* XXX no separate I and D spaces */
  326. case PT_READ_D:
  327. /* write = 0 done above. */
  328. iov.iov_base = (caddr_t)&temp;
  329. iov.iov_len = sizeof(int);
  330. uio.uio_iov = &iov;
  331. uio.uio_iovcnt = 1;
  332. uio.uio_offset = (off_t)(vaddr_t)SCARG(uap, addr);
  333. uio.uio_resid = sizeof(int);
  334. uio.uio_segflg = UIO_SYSSPACE;
  335. uio.uio_rw = write ? UIO_WRITE : UIO_READ;
  336. uio.uio_procp = p;
  337. error = process_domem(p, t, &uio, write ? PT_WRITE_I :
  338. PT_READ_I);
  339. if (write == 0)
  340. *retval = temp;
  341. return (error);
  342. case PT_IO:
  343. error = copyin(SCARG(uap, addr), &piod, sizeof(piod));
  344. if (error)
  345. return (error);
  346. iov.iov_base = piod.piod_addr;
  347. iov.iov_len = piod.piod_len;
  348. uio.uio_iov = &iov;
  349. uio.uio_iovcnt = 1;
  350. uio.uio_offset = (off_t)(vaddr_t)piod.piod_offs;
  351. uio.uio_resid = piod.piod_len;
  352. uio.uio_segflg = UIO_USERSPACE;
  353. uio.uio_procp = p;
  354. switch (piod.piod_op) {
  355. case PIOD_READ_I:
  356. req = PT_READ_I;
  357. uio.uio_rw = UIO_READ;
  358. break;
  359. case PIOD_READ_D:
  360. req = PT_READ_D;
  361. uio.uio_rw = UIO_READ;
  362. break;
  363. case PIOD_WRITE_I:
  364. req = PT_WRITE_I;
  365. uio.uio_rw = UIO_WRITE;
  366. break;
  367. case PIOD_WRITE_D:
  368. req = PT_WRITE_D;
  369. uio.uio_rw = UIO_WRITE;
  370. break;
  371. case PIOD_READ_AUXV:
  372. req = PT_READ_D;
  373. uio.uio_rw = UIO_READ;
  374. temp = tr->ps_emul->e_arglen * sizeof(char *);
  375. if (uio.uio_offset > temp)
  376. return (EIO);
  377. if (uio.uio_resid > temp - uio.uio_offset)
  378. uio.uio_resid = temp - uio.uio_offset;
  379. piod.piod_len = iov.iov_len = uio.uio_resid;
  380. error = process_auxv_offset(p, t, &uio);
  381. if (error)
  382. return (error);
  383. break;
  384. default:
  385. return (EINVAL);
  386. }
  387. error = process_domem(p, t, &uio, req);
  388. piod.piod_len -= uio.uio_resid;
  389. (void) copyout(&piod, SCARG(uap, addr), sizeof(piod));
  390. return (error);
  391. #ifdef PT_STEP
  392. case PT_STEP:
  393. /*
  394. * From the 4.4BSD PRM:
  395. * "Execution continues as in request PT_CONTINUE; however
  396. * as soon as possible after execution of at least one
  397. * instruction, execution stops again. [ ... ]"
  398. */
  399. #endif
  400. case PT_CONTINUE:
  401. /*
  402. * From the 4.4BSD PRM:
  403. * "The data argument is taken as a signal number and the
  404. * child's execution continues at location addr as if it
  405. * incurred that signal. Normally the signal number will
  406. * be either 0 to indicate that the signal that caused the
  407. * stop should be ignored, or that value fetched out of
  408. * the process's image indicating which signal caused
  409. * the stop. If addr is (int *)1 then execution continues
  410. * from where it stopped."
  411. */
  412. if (SCARG(uap, pid) < THREAD_PID_OFFSET && tr->ps_single)
  413. t = tr->ps_single;
  414. /* Check that the data is a valid signal number or zero. */
  415. if (SCARG(uap, data) < 0 || SCARG(uap, data) >= NSIG)
  416. return (EINVAL);
  417. /* If the address parameter is not (int *)1, set the pc. */
  418. if ((int *)SCARG(uap, addr) != (int *)1)
  419. if ((error = process_set_pc(t, SCARG(uap, addr))) != 0)
  420. goto relebad;
  421. #ifdef PT_STEP
  422. /*
  423. * Arrange for a single-step, if that's requested and possible.
  424. */
  425. error = process_sstep(t, req == PT_STEP);
  426. if (error)
  427. goto relebad;
  428. #endif
  429. goto sendsig;
  430. case PT_DETACH:
  431. /*
  432. * From the 4.4BSD PRM:
  433. * "The data argument is taken as a signal number and the
  434. * child's execution continues at location addr as if it
  435. * incurred that signal. Normally the signal number will
  436. * be either 0 to indicate that the signal that caused the
  437. * stop should be ignored, or that value fetched out of
  438. * the process's image indicating which signal caused
  439. * the stop. If addr is (int *)1 then execution continues
  440. * from where it stopped."
  441. */
  442. if (SCARG(uap, pid) < THREAD_PID_OFFSET && tr->ps_single)
  443. t = tr->ps_single;
  444. /* Check that the data is a valid signal number or zero. */
  445. if (SCARG(uap, data) < 0 || SCARG(uap, data) >= NSIG)
  446. return (EINVAL);
  447. #ifdef PT_STEP
  448. /*
  449. * Arrange for a single-step, if that's requested and possible.
  450. */
  451. error = process_sstep(t, req == PT_STEP);
  452. if (error)
  453. goto relebad;
  454. #endif
  455. /* give process back to original parent or init */
  456. if (tr->ps_oppid != tr->ps_pptr->ps_pid) {
  457. struct process *ppr;
  458. ppr = prfind(tr->ps_oppid);
  459. proc_reparent(tr, ppr ? ppr : initprocess);
  460. }
  461. /* not being traced any more */
  462. tr->ps_oppid = 0;
  463. atomic_clearbits_int(&tr->ps_flags, PS_TRACED|PS_WAITED);
  464. sendsig:
  465. memset(tr->ps_ptstat, 0, sizeof(*tr->ps_ptstat));
  466. /* Finally, deliver the requested signal (or none). */
  467. if (t->p_stat == SSTOP) {
  468. t->p_xstat = SCARG(uap, data);
  469. SCHED_LOCK(s);
  470. setrunnable(t);
  471. SCHED_UNLOCK(s);
  472. } else {
  473. if (SCARG(uap, data) != 0)
  474. psignal(t, SCARG(uap, data));
  475. }
  476. return (0);
  477. relebad:
  478. return (error);
  479. case PT_KILL:
  480. if (SCARG(uap, pid) < THREAD_PID_OFFSET && tr->ps_single)
  481. t = tr->ps_single;
  482. /* just send the process a KILL signal. */
  483. SCARG(uap, data) = SIGKILL;
  484. goto sendsig; /* in PT_CONTINUE, above. */
  485. case PT_ATTACH:
  486. /*
  487. * As was done in procfs:
  488. * Go ahead and set the trace flag.
  489. * Save the old parent (it's reset in
  490. * _DETACH, and also in kern_exit.c:wait4()
  491. * Reparent the process so that the tracing
  492. * proc gets to see all the action.
  493. * Stop the target.
  494. */
  495. atomic_setbits_int(&tr->ps_flags, PS_TRACED);
  496. tr->ps_oppid = tr->ps_pptr->ps_pid;
  497. if (tr->ps_pptr != p->p_p)
  498. proc_reparent(tr, p->p_p);
  499. if (tr->ps_ptstat == NULL)
  500. tr->ps_ptstat = malloc(sizeof(*tr->ps_ptstat),
  501. M_SUBPROC, M_WAITOK);
  502. SCARG(uap, data) = SIGSTOP;
  503. goto sendsig;
  504. case PT_GET_EVENT_MASK:
  505. if (SCARG(uap, data) != sizeof(pe))
  506. return (EINVAL);
  507. memset(&pe, 0, sizeof(pe));
  508. pe.pe_set_event = tr->ps_ptmask;
  509. return (copyout(&pe, SCARG(uap, addr), sizeof(pe)));
  510. case PT_SET_EVENT_MASK:
  511. if (SCARG(uap, data) != sizeof(pe))
  512. return (EINVAL);
  513. if ((error = copyin(SCARG(uap, addr), &pe, sizeof(pe))))
  514. return (error);
  515. tr->ps_ptmask = pe.pe_set_event;
  516. return (0);
  517. case PT_GET_PROCESS_STATE:
  518. if (SCARG(uap, data) != sizeof(*tr->ps_ptstat))
  519. return (EINVAL);
  520. if (tr->ps_single)
  521. tr->ps_ptstat->pe_tid =
  522. tr->ps_single->p_pid + THREAD_PID_OFFSET;
  523. return (copyout(tr->ps_ptstat, SCARG(uap, addr),
  524. sizeof(*tr->ps_ptstat)));
  525. case PT_SETREGS:
  526. KASSERT((p->p_flag & P_SYSTEM) == 0);
  527. if ((error = process_checkioperm(p, tr)) != 0)
  528. return (error);
  529. regs = malloc(sizeof(*regs), M_TEMP, M_WAITOK);
  530. error = copyin(SCARG(uap, addr), regs, sizeof(*regs));
  531. if (error == 0) {
  532. error = process_write_regs(t, regs);
  533. }
  534. free(regs, M_TEMP, sizeof(*regs));
  535. return (error);
  536. case PT_GETREGS:
  537. KASSERT((p->p_flag & P_SYSTEM) == 0);
  538. if ((error = process_checkioperm(p, tr)) != 0)
  539. return (error);
  540. regs = malloc(sizeof(*regs), M_TEMP, M_WAITOK);
  541. error = process_read_regs(t, regs);
  542. if (error == 0)
  543. error = copyout(regs,
  544. SCARG(uap, addr), sizeof (*regs));
  545. free(regs, M_TEMP, sizeof(*regs));
  546. return (error);
  547. #ifdef PT_SETFPREGS
  548. case PT_SETFPREGS:
  549. KASSERT((p->p_flag & P_SYSTEM) == 0);
  550. if ((error = process_checkioperm(p, tr)) != 0)
  551. return (error);
  552. fpregs = malloc(sizeof(*fpregs), M_TEMP, M_WAITOK);
  553. error = copyin(SCARG(uap, addr), fpregs, sizeof(*fpregs));
  554. if (error == 0) {
  555. error = process_write_fpregs(t, fpregs);
  556. }
  557. free(fpregs, M_TEMP, sizeof(*fpregs));
  558. return (error);
  559. #endif
  560. #ifdef PT_GETFPREGS
  561. case PT_GETFPREGS:
  562. KASSERT((p->p_flag & P_SYSTEM) == 0);
  563. if ((error = process_checkioperm(p, tr)) != 0)
  564. return (error);
  565. fpregs = malloc(sizeof(*fpregs), M_TEMP, M_WAITOK);
  566. error = process_read_fpregs(t, fpregs);
  567. if (error == 0)
  568. error = copyout(fpregs,
  569. SCARG(uap, addr), sizeof(*fpregs));
  570. free(fpregs, M_TEMP, sizeof(*fpregs));
  571. return (error);
  572. #endif
  573. #ifdef PT_SETXMMREGS
  574. case PT_SETXMMREGS:
  575. KASSERT((p->p_flag & P_SYSTEM) == 0);
  576. if ((error = process_checkioperm(p, tr)) != 0)
  577. return (error);
  578. xmmregs = malloc(sizeof(*xmmregs), M_TEMP, M_WAITOK);
  579. error = copyin(SCARG(uap, addr), xmmregs, sizeof(*xmmregs));
  580. if (error == 0) {
  581. error = process_write_xmmregs(t, xmmregs);
  582. }
  583. free(xmmregs, M_TEMP, sizeof(*xmmregs));
  584. return (error);
  585. #endif
  586. #ifdef PT_GETXMMREGS
  587. case PT_GETXMMREGS:
  588. KASSERT((p->p_flag & P_SYSTEM) == 0);
  589. if ((error = process_checkioperm(p, tr)) != 0)
  590. return (error);
  591. xmmregs = malloc(sizeof(*xmmregs), M_TEMP, M_WAITOK);
  592. error = process_read_xmmregs(t, xmmregs);
  593. if (error == 0)
  594. error = copyout(xmmregs,
  595. SCARG(uap, addr), sizeof(*xmmregs));
  596. free(xmmregs, M_TEMP, sizeof(*xmmregs));
  597. return (error);
  598. #endif
  599. #ifdef PT_WCOOKIE
  600. case PT_WCOOKIE:
  601. wcookie = process_get_wcookie (t);
  602. return (copyout(&wcookie, SCARG(uap, addr),
  603. sizeof (register_t)));
  604. #endif
  605. }
  606. #ifdef DIAGNOSTIC
  607. panic("ptrace: impossible");
  608. #endif
  609. return 0;
  610. }
  611. #endif /* PTRACE */
  612. /*
  613. * Check if a process is allowed to fiddle with the memory of another.
  614. *
  615. * p = tracer
  616. * tr = tracee
  617. *
  618. * 1. You can't attach to a process not owned by you or one that has raised
  619. * its privileges.
  620. * 1a. ...unless you are root.
  621. *
  622. * 2. init is always off-limits because it can control the securelevel.
  623. * 2a. ...unless securelevel is permanently set to insecure.
  624. *
  625. * 3. Processes that are in the process of doing an exec() are always
  626. * off-limits because of the can of worms they are. Just wait a
  627. * second.
  628. */
  629. int
  630. process_checkioperm(struct proc *p, struct process *tr)
  631. {
  632. int error;
  633. if ((tr->ps_ucred->cr_ruid != p->p_ucred->cr_ruid ||
  634. ISSET(tr->ps_flags, PS_SUGIDEXEC | PS_SUGID)) &&
  635. (error = suser(p, 0)) != 0)
  636. return (error);
  637. if ((tr->ps_pid == 1) && (securelevel > -1))
  638. return (EPERM);
  639. if (tr->ps_flags & PS_INEXEC)
  640. return (EAGAIN);
  641. return (0);
  642. }
  643. int
  644. process_domem(struct proc *curp, struct proc *p, struct uio *uio, int req)
  645. {
  646. struct vmspace *vm;
  647. int error;
  648. vaddr_t addr;
  649. vsize_t len;
  650. len = uio->uio_resid;
  651. if (len == 0)
  652. return (0);
  653. if ((error = process_checkioperm(curp, p->p_p)) != 0)
  654. return (error);
  655. /* XXXCDC: how should locking work here? */
  656. if ((p->p_p->ps_flags & PS_EXITING) || (p->p_vmspace->vm_refcnt < 1))
  657. return(EFAULT);
  658. addr = uio->uio_offset;
  659. vm = p->p_vmspace;
  660. vm->vm_refcnt++;
  661. error = uvm_io(&vm->vm_map, uio,
  662. (req == PT_WRITE_I) ? UVM_IO_FIXPROT : 0);
  663. uvmspace_free(vm);
  664. if (error == 0 && req == PT_WRITE_I)
  665. pmap_proc_iflush(p, addr, len);
  666. return (error);
  667. }
  668. #ifdef PTRACE
  669. int
  670. process_auxv_offset(struct proc *curp, struct proc *p, struct uio *uiop)
  671. {
  672. struct process *pr = p->p_p;
  673. struct ps_strings pss;
  674. struct iovec iov;
  675. struct uio uio;
  676. int error;
  677. iov.iov_base = &pss;
  678. iov.iov_len = sizeof(pss);
  679. uio.uio_iov = &iov;
  680. uio.uio_iovcnt = 1;
  681. uio.uio_offset = (off_t)pr->ps_strings;
  682. uio.uio_resid = sizeof(pss);
  683. uio.uio_segflg = UIO_SYSSPACE;
  684. uio.uio_rw = UIO_READ;
  685. uio.uio_procp = curp;
  686. if ((error = uvm_io(&p->p_vmspace->vm_map, &uio, 0)) != 0)
  687. return (error);
  688. if (pss.ps_envstr == NULL)
  689. return (EIO);
  690. uiop->uio_offset += (off_t)(vaddr_t)(pss.ps_envstr + pss.ps_nenvstr + 1);
  691. #ifdef MACHINE_STACK_GROWS_UP
  692. if (uiop->uio_offset < (off_t)pr->ps_strings)
  693. return (EIO);
  694. #else
  695. if (uiop->uio_offset > (off_t)pr->ps_strings)
  696. return (EIO);
  697. if ((uiop->uio_offset + uiop->uio_resid) > (off_t)pr->ps_strings)
  698. uiop->uio_resid = (off_t)pr->ps_strings - uiop->uio_offset;
  699. #endif
  700. return (0);
  701. }
  702. #endif