kernel - use new td_ucred in numerous places
[dragonfly.git] / sys / kern / sys_process.c
1 /*
2  * Copyright (c) 1994, Sean Eric Fagan
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Sean Eric Fagan.
16  * 4. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * $FreeBSD: src/sys/kern/sys_process.c,v 1.51.2.6 2003/01/08 03:06:45 kan Exp $
32  * $DragonFly: src/sys/kern/sys_process.c,v 1.30 2007/02/19 01:14:23 corecode Exp $
33  */
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/sysproto.h>
38 #include <sys/proc.h>
39 #include <sys/priv.h>
40 #include <sys/vnode.h>
41 #include <sys/ptrace.h>
42 #include <sys/reg.h>
43 #include <sys/lock.h>
44
45 #include <vm/vm.h>
46 #include <vm/pmap.h>
47 #include <vm/vm_map.h>
48 #include <vm/vm_page.h>
49
50 #include <sys/user.h>
51 #include <vfs/procfs/procfs.h>
52 #include <sys/thread2.h>
53
54 /* use the equivalent procfs code */
55 #if 0
56 static int
57 pread (struct proc *procp, unsigned int addr, unsigned int *retval) {
58         int             rv;
59         vm_map_t        map, tmap;
60         vm_object_t     object;
61         vm_offset_t     kva = 0;
62         int             page_offset;    /* offset into page */
63         vm_offset_t     pageno;         /* page number */
64         vm_map_entry_t  out_entry;
65         vm_prot_t       out_prot;
66         boolean_t       wired;
67         vm_pindex_t     pindex;
68
69         /* Map page into kernel space */
70
71         map = &procp->p_vmspace->vm_map;
72
73         page_offset = addr - trunc_page(addr);
74         pageno = trunc_page(addr);
75
76         tmap = map;
77         rv = vm_map_lookup (&tmap, pageno, VM_PROT_READ, &out_entry,
78                 &object, &pindex, &out_prot, &wired);
79
80         if (rv != KERN_SUCCESS)
81                 return EINVAL;
82
83         vm_map_lookup_done (tmap, out_entry, 0);
84
85         /* Find space in kernel_map for the page we're interested in */
86         rv = vm_map_find (&kernel_map, object, IDX_TO_OFF(pindex),
87                           &kva, PAGE_SIZE, 
88                           0, 
89                           VM_MAPTYPE_NORMAL,
90                           VM_PROT_ALL, VM_PROT_ALL,
91                           0);
92
93         if (!rv) {
94                 vm_object_reference (object);
95
96                 rv = vm_map_wire (&kernel_map, kva, kva + PAGE_SIZE, 0);
97                 if (!rv) {
98                         *retval = 0;
99                         bcopy ((caddr_t)kva + page_offset,
100                                retval, sizeof *retval);
101                 }
102                 vm_map_remove (&kernel_map, kva, kva + PAGE_SIZE);
103         }
104
105         return rv;
106 }
107
108 static int
109 pwrite (struct proc *procp, unsigned int addr, unsigned int datum) {
110         int             rv;
111         vm_map_t        map, tmap;
112         vm_object_t     object;
113         vm_offset_t     kva = 0;
114         int             page_offset;    /* offset into page */
115         vm_offset_t     pageno;         /* page number */
116         vm_map_entry_t  out_entry;
117         vm_prot_t       out_prot;
118         boolean_t       wired;
119         vm_pindex_t     pindex;
120         boolean_t       fix_prot = 0;
121
122         /* Map page into kernel space */
123
124         map = &procp->p_vmspace->vm_map;
125
126         page_offset = addr - trunc_page(addr);
127         pageno = trunc_page(addr);
128
129         /*
130          * Check the permissions for the area we're interested in.
131          */
132
133         if (vm_map_check_protection (map, pageno, pageno + PAGE_SIZE,
134                 VM_PROT_WRITE) == FALSE) {
135                 /*
136                  * If the page was not writable, we make it so.
137                  * XXX It is possible a page may *not* be read/executable,
138                  * if a process changes that!
139                  */
140                 fix_prot = 1;
141                 /* The page isn't writable, so let's try making it so... */
142                 if ((rv = vm_map_protect (map, pageno, pageno + PAGE_SIZE,
143                         VM_PROT_ALL, 0)) != KERN_SUCCESS)
144                   return EFAULT;        /* I guess... */
145         }
146
147         /*
148          * Now we need to get the page.  out_entry, out_prot, wired, and
149          * single_use aren't used.  One would think the vm code would be
150          * a *bit* nicer...  We use tmap because vm_map_lookup() can
151          * change the map argument.
152          */
153
154         tmap = map;
155         rv = vm_map_lookup (&tmap, pageno, VM_PROT_WRITE, &out_entry,
156                 &object, &pindex, &out_prot, &wired);
157         if (rv != KERN_SUCCESS) {
158                 return EINVAL;
159         }
160
161         /*
162          * Okay, we've got the page.  Let's release tmap.
163          */
164
165         vm_map_lookup_done (tmap, out_entry, 0);
166
167         /*
168          * Fault the page in...
169          */
170
171         rv = vm_fault(map, pageno, VM_PROT_WRITE|VM_PROT_READ, FALSE);
172         if (rv != KERN_SUCCESS)
173                 return EFAULT;
174
175         /* Find space in kernel_map for the page we're interested in */
176         rv = vm_map_find (&kernel_map, object, IDX_TO_OFF(pindex),
177                           &kva, PAGE_SIZE,
178                           0,
179                           VM_MAPTYPE_NORMAL,
180                           VM_PROT_ALL, VM_PROT_ALL,
181                           0);
182         if (!rv) {
183                 vm_object_reference (object);
184
185                 rv = vm_map_wire (&kernel_map, kva, kva + PAGE_SIZE, 0);
186                 if (!rv) {
187                   bcopy (&datum, (caddr_t)kva + page_offset, sizeof datum);
188                 }
189                 vm_map_remove (&kernel_map, kva, kva + PAGE_SIZE);
190         }
191
192         if (fix_prot)
193                 vm_map_protect (map, pageno, pageno + PAGE_SIZE,
194                         VM_PROT_READ|VM_PROT_EXECUTE, 0);
195         return rv;
196 }
197 #endif
198
199 /*
200  * Process debugging system call.
201  *
202  * MPALMOSTSAFE
203  */
204 int
205 sys_ptrace(struct ptrace_args *uap)
206 {
207         struct proc *p = curproc;
208
209         /*
210          * XXX this obfuscation is to reduce stack usage, but the register
211          * structs may be too large to put on the stack anyway.
212          */
213         union {
214                 struct ptrace_io_desc piod;
215                 struct dbreg dbreg;
216                 struct fpreg fpreg;
217                 struct reg reg;
218         } r;
219         void *addr;
220         int error = 0;
221
222         addr = &r;
223         switch (uap->req) {
224         case PT_GETREGS:
225         case PT_GETFPREGS:
226 #ifdef PT_GETDBREGS
227         case PT_GETDBREGS:
228 #endif
229                 break;
230         case PT_SETREGS:
231                 error = copyin(uap->addr, &r.reg, sizeof r.reg);
232                 break;
233         case PT_SETFPREGS:
234                 error = copyin(uap->addr, &r.fpreg, sizeof r.fpreg);
235                 break;
236 #ifdef PT_SETDBREGS
237         case PT_SETDBREGS:
238                 error = copyin(uap->addr, &r.dbreg, sizeof r.dbreg);
239                 break;
240 #endif
241         case PT_IO:
242                 error = copyin(uap->addr, &r.piod, sizeof r.piod);
243                 break;
244         default:
245                 addr = uap->addr;
246         }
247         if (error)
248                 return (error);
249
250         get_mplock();
251         error = kern_ptrace(p, uap->req, uap->pid, addr, uap->data,
252                         &uap->sysmsg_result);
253         rel_mplock();
254         if (error)
255                 return (error);
256
257         switch (uap->req) {
258         case PT_IO:
259                 (void)copyout(&r.piod, uap->addr, sizeof r.piod);
260                 break;
261         case PT_GETREGS:
262                 error = copyout(&r.reg, uap->addr, sizeof r.reg);
263                 break;
264         case PT_GETFPREGS:
265                 error = copyout(&r.fpreg, uap->addr, sizeof r.fpreg);
266                 break;
267 #ifdef PT_GETDBREGS
268         case PT_GETDBREGS:
269                 error = copyout(&r.dbreg, uap->addr, sizeof r.dbreg);
270                 break;
271 #endif
272         }
273
274         return (error);
275 }
276
277 int
278 kern_ptrace(struct proc *curp, int req, pid_t pid, void *addr, int data, int *res)
279 {
280         struct proc *p, *pp;
281         struct lwp *lp;
282         struct iovec iov;
283         struct uio uio;
284         struct ptrace_io_desc *piod;
285         int error = 0;
286         int write, tmp;
287
288         write = 0;
289         if (req == PT_TRACE_ME) {
290                 p = curp;
291         } else {
292                 if ((p = pfind(pid)) == NULL)
293                         return ESRCH;
294         }
295         if (!PRISON_CHECK(curp->p_ucred, p->p_ucred))
296                 return (ESRCH);
297
298         /* Can't trace a process that's currently exec'ing. */
299         if ((p->p_flag & P_INEXEC) != 0)
300                 return EAGAIN;
301
302         /*
303          * Permissions check
304          */
305         switch (req) {
306         case PT_TRACE_ME:
307                 /* Always legal. */
308                 break;
309
310         case PT_ATTACH:
311                 /* Self */
312                 if (p->p_pid == curp->p_pid)
313                         return EINVAL;
314
315                 /* Already traced */
316                 if (p->p_flag & P_TRACED)
317                         return EBUSY;
318
319                 if (curp->p_flag & P_TRACED)
320                         for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr)
321                                 if (pp == p)
322                                         return (EINVAL);
323
324                 /* not owned by you, has done setuid (unless you're root) */
325                 if ((p->p_ucred->cr_ruid != curp->p_ucred->cr_ruid) ||
326                      (p->p_flag & P_SUGID)) {
327                         if ((error = priv_check_cred(curp->p_ucred, PRIV_ROOT, 0)) != 0)
328                                 return error;
329                 }
330
331                 /* can't trace init when securelevel > 0 */
332                 if (securelevel > 0 && p->p_pid == 1)
333                         return EPERM;
334
335                 /* OK */
336                 break;
337
338         case PT_READ_I:
339         case PT_READ_D:
340         case PT_WRITE_I:
341         case PT_WRITE_D:
342         case PT_IO:
343         case PT_CONTINUE:
344         case PT_KILL:
345         case PT_STEP:
346         case PT_DETACH:
347 #ifdef PT_GETREGS
348         case PT_GETREGS:
349 #endif
350 #ifdef PT_SETREGS
351         case PT_SETREGS:
352 #endif
353 #ifdef PT_GETFPREGS
354         case PT_GETFPREGS:
355 #endif
356 #ifdef PT_SETFPREGS
357         case PT_SETFPREGS:
358 #endif
359 #ifdef PT_GETDBREGS
360         case PT_GETDBREGS:
361 #endif
362 #ifdef PT_SETDBREGS
363         case PT_SETDBREGS:
364 #endif
365                 /* not being traced... */
366                 if ((p->p_flag & P_TRACED) == 0)
367                         return EPERM;
368
369                 /* not being traced by YOU */
370                 if (p->p_pptr != curp)
371                         return EBUSY;
372
373                 /* not currently stopped */
374                 if (p->p_stat != SSTOP ||
375                     (p->p_flag & P_WAITED) == 0) {
376                         return EBUSY;
377                 }
378
379                 /* OK */
380                 break;
381
382         default:
383                 return EINVAL;
384         }
385
386         /* XXX lwp */
387         lp = FIRST_LWP_IN_PROC(p);
388 #ifdef FIX_SSTEP
389         /*
390          * Single step fixup ala procfs
391          */
392         FIX_SSTEP(lp);
393 #endif
394
395         /*
396          * Actually do the requests
397          */
398
399         *res = 0;
400
401         switch (req) {
402         case PT_TRACE_ME:
403                 /* set my trace flag and "owner" so it can read/write me */
404                 p->p_flag |= P_TRACED;
405                 p->p_oppid = p->p_pptr->p_pid;
406                 return 0;
407
408         case PT_ATTACH:
409                 /* security check done above */
410                 p->p_flag |= P_TRACED;
411                 p->p_oppid = p->p_pptr->p_pid;
412                 if (p->p_pptr != curp)
413                         proc_reparent(p, curp);
414                 data = SIGSTOP;
415                 goto sendsig;   /* in PT_CONTINUE below */
416
417         case PT_STEP:
418         case PT_CONTINUE:
419         case PT_DETACH:
420                 /* Zero means do not send any signal */
421                 if (data < 0 || data > _SIG_MAXSIG)
422                         return EINVAL;
423
424                 LWPHOLD(lp);
425
426                 if (req == PT_STEP) {
427                         if ((error = ptrace_single_step (lp))) {
428                                 LWPRELE(lp);
429                                 return error;
430                         }
431                 }
432
433                 if (addr != (void *)1) {
434                         if ((error = ptrace_set_pc (lp,
435                             (u_long)(uintfptr_t)addr))) {
436                                 LWPRELE(lp);
437                                 return error;
438                         }
439                 }
440                 LWPRELE(lp);
441
442                 if (req == PT_DETACH) {
443                         /* reset process parent */
444                         if (p->p_oppid != p->p_pptr->p_pid) {
445                                 struct proc *pp;
446
447                                 pp = pfind(p->p_oppid);
448                                 proc_reparent(p, pp ? pp : initproc);
449                         }
450
451                         p->p_flag &= ~(P_TRACED | P_WAITED);
452                         p->p_oppid = 0;
453
454                         /* should we send SIGCHLD? */
455                 }
456
457         sendsig:
458                 /*
459                  * Deliver or queue signal.  If the process is stopped
460                  * force it to be SACTIVE again.
461                  */
462                 crit_enter();
463                 if (p->p_stat == SSTOP) {
464                         p->p_xstat = data;
465                         lp->lwp_flag |= LWP_BREAKTSLEEP;
466                         proc_unstop(p);
467                 } else if (data) {
468                         ksignal(p, data);
469                 }
470                 crit_exit();
471                 return 0;
472
473         case PT_WRITE_I:
474         case PT_WRITE_D:
475                 write = 1;
476                 /* fallthrough */
477         case PT_READ_I:
478         case PT_READ_D:
479                 /*
480                  * NOTE! uio_offset represents the offset in the target
481                  * process.  The iov is in the current process (the guy
482                  * making the ptrace call) so uio_td must be the current
483                  * process (though for a SYSSPACE transfer it doesn't
484                  * really matter).
485                  */
486                 tmp = 0;
487                 /* write = 0 set above */
488                 iov.iov_base = write ? (caddr_t)&data : (caddr_t)&tmp;
489                 iov.iov_len = sizeof(int);
490                 uio.uio_iov = &iov;
491                 uio.uio_iovcnt = 1;
492                 uio.uio_offset = (off_t)(uintptr_t)addr;
493                 uio.uio_resid = sizeof(int);
494                 uio.uio_segflg = UIO_SYSSPACE;
495                 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
496                 uio.uio_td = curthread;
497                 error = procfs_domem(curp, lp, NULL, &uio);
498                 if (uio.uio_resid != 0) {
499                         /*
500                          * XXX procfs_domem() doesn't currently return ENOSPC,
501                          * so I think write() can bogusly return 0.
502                          * XXX what happens for short writes?  We don't want
503                          * to write partial data.
504                          * XXX procfs_domem() returns EPERM for other invalid
505                          * addresses.  Convert this to EINVAL.  Does this
506                          * clobber returns of EPERM for other reasons?
507                          */
508                         if (error == 0 || error == ENOSPC || error == EPERM)
509                                 error = EINVAL; /* EOF */
510                 }
511                 if (!write)
512                         *res = tmp;
513                 return (error);
514
515         case PT_IO:
516                 /*
517                  * NOTE! uio_offset represents the offset in the target
518                  * process.  The iov is in the current process (the guy
519                  * making the ptrace call) so uio_td must be the current
520                  * process.
521                  */
522                 piod = addr;
523                 iov.iov_base = piod->piod_addr;
524                 iov.iov_len = piod->piod_len;
525                 uio.uio_iov = &iov;
526                 uio.uio_iovcnt = 1;
527                 uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
528                 uio.uio_resid = piod->piod_len;
529                 uio.uio_segflg = UIO_USERSPACE;
530                 uio.uio_td = curthread;
531                 switch (piod->piod_op) {
532                 case PIOD_READ_D:
533                 case PIOD_READ_I:
534                         uio.uio_rw = UIO_READ;
535                         break;
536                 case PIOD_WRITE_D:
537                 case PIOD_WRITE_I:
538                         uio.uio_rw = UIO_WRITE;
539                         break;
540                 default:
541                         return (EINVAL);
542                 }
543                 error = procfs_domem(curp, lp, NULL, &uio);
544                 piod->piod_len -= uio.uio_resid;
545                 return (error);
546
547         case PT_KILL:
548                 data = SIGKILL;
549                 goto sendsig;   /* in PT_CONTINUE above */
550
551 #ifdef PT_SETREGS
552         case PT_SETREGS:
553                 write = 1;
554                 /* fallthrough */
555 #endif /* PT_SETREGS */
556 #ifdef PT_GETREGS
557         case PT_GETREGS:
558                 /* write = 0 above */
559 #endif /* PT_SETREGS */
560 #if defined(PT_SETREGS) || defined(PT_GETREGS)
561                 if (!procfs_validregs(lp))      /* no P_SYSTEM procs please */
562                         return EINVAL;
563                 else {
564                         iov.iov_base = addr;
565                         iov.iov_len = sizeof(struct reg);
566                         uio.uio_iov = &iov;
567                         uio.uio_iovcnt = 1;
568                         uio.uio_offset = 0;
569                         uio.uio_resid = sizeof(struct reg);
570                         uio.uio_segflg = UIO_SYSSPACE;
571                         uio.uio_rw = write ? UIO_WRITE : UIO_READ;
572                         uio.uio_td = curthread;
573                         return (procfs_doregs(curp, lp, NULL, &uio));
574                 }
575 #endif /* defined(PT_SETREGS) || defined(PT_GETREGS) */
576
577 #ifdef PT_SETFPREGS
578         case PT_SETFPREGS:
579                 write = 1;
580                 /* fallthrough */
581 #endif /* PT_SETFPREGS */
582 #ifdef PT_GETFPREGS
583         case PT_GETFPREGS:
584                 /* write = 0 above */
585 #endif /* PT_SETFPREGS */
586 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
587                 if (!procfs_validfpregs(lp))    /* no P_SYSTEM procs please */
588                         return EINVAL;
589                 else {
590                         iov.iov_base = addr;
591                         iov.iov_len = sizeof(struct fpreg);
592                         uio.uio_iov = &iov;
593                         uio.uio_iovcnt = 1;
594                         uio.uio_offset = 0;
595                         uio.uio_resid = sizeof(struct fpreg);
596                         uio.uio_segflg = UIO_SYSSPACE;
597                         uio.uio_rw = write ? UIO_WRITE : UIO_READ;
598                         uio.uio_td = curthread;
599                         return (procfs_dofpregs(curp, lp, NULL, &uio));
600                 }
601 #endif /* defined(PT_SETFPREGS) || defined(PT_GETFPREGS) */
602
603 #ifdef PT_SETDBREGS
604         case PT_SETDBREGS:
605                 write = 1;
606                 /* fallthrough */
607 #endif /* PT_SETDBREGS */
608 #ifdef PT_GETDBREGS
609         case PT_GETDBREGS:
610                 /* write = 0 above */
611 #endif /* PT_SETDBREGS */
612 #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
613                 if (!procfs_validdbregs(lp))    /* no P_SYSTEM procs please */
614                         return EINVAL;
615                 else {
616                         iov.iov_base = addr;
617                         iov.iov_len = sizeof(struct dbreg);
618                         uio.uio_iov = &iov;
619                         uio.uio_iovcnt = 1;
620                         uio.uio_offset = 0;
621                         uio.uio_resid = sizeof(struct dbreg);
622                         uio.uio_segflg = UIO_SYSSPACE;
623                         uio.uio_rw = write ? UIO_WRITE : UIO_READ;
624                         uio.uio_td = curthread;
625                         return (procfs_dodbregs(curp, lp, NULL, &uio));
626                 }
627 #endif /* defined(PT_SETDBREGS) || defined(PT_GETDBREGS) */
628
629         default:
630                 break;
631         }
632
633         return 0;
634 }
635
636 int
637 trace_req(struct proc *p)
638 {
639         return 1;
640 }
641
642 /*
643  * stopevent()
644  *
645  * Stop a process because of a procfs event.  Stay stopped until p->p_step
646  * is cleared (cleared by PIOCCONT in procfs).
647  *
648  * MPSAFE
649  */
650 void
651 stopevent(struct proc *p, unsigned int event, unsigned int val) 
652 {
653         p->p_step = 1;
654
655         do {
656                 crit_enter();
657                 wakeup(&p->p_stype);    /* Wake up any PIOCWAIT'ing procs */
658                 p->p_xstat = val;
659                 p->p_stype = event;     /* Which event caused the stop? */
660                 tsleep(&p->p_step, 0, "stopevent", 0);
661                 crit_exit();
662         } while (p->p_step);
663 }
664