More MP work.
[dragonfly.git] / sys / kern / kern_prot.c
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1990, 1991, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the University of
21  *      California, Berkeley and its contributors.
22  * 4. 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  *      @(#)kern_prot.c 8.6 (Berkeley) 1/21/94
39  * $FreeBSD: src/sys/kern/kern_prot.c,v 1.53.2.9 2002/03/09 05:20:26 dd Exp $
40  * $DragonFly: src/sys/kern/kern_prot.c,v 1.24 2006/05/26 00:33:09 dillon Exp $
41  */
42
43 /*
44  * System calls related to processes and protection
45  */
46
47 #include "opt_compat.h"
48
49 #include <sys/param.h>
50 #include <sys/acct.h>
51 #include <sys/systm.h>
52 #include <sys/sysproto.h>
53 #include <sys/kernel.h>
54 #include <sys/proc.h>
55 #include <sys/malloc.h>
56 #include <sys/pioctl.h>
57 #include <sys/resourcevar.h>
58 #include <sys/jail.h>
59 #include <sys/lockf.h>
60 #include <sys/spinlock.h>
61
62 #include <sys/thread2.h>
63 #include <sys/spinlock2.h>
64
65 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
66
67 /*
68  * NOT MP SAFE due to p_pptr access
69  */
70 /* ARGSUSED */
71 int
72 getpid(struct getpid_args *uap)
73 {
74         struct proc *p = curproc;
75
76         uap->sysmsg_fds[0] = p->p_pid;
77 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
78         uap->sysmsg_fds[1] = p->p_pptr->p_pid;
79 #endif
80         return (0);
81 }
82
83 /* ARGSUSED */
84 int
85 getppid(struct getppid_args *uap)
86 {
87         struct proc *p = curproc;
88
89         uap->sysmsg_result = p->p_pptr->p_pid;
90         return (0);
91 }
92
93 /* 
94  * Get process group ID; note that POSIX getpgrp takes no parameter 
95  *
96  * MP SAFE
97  */
98 int
99 getpgrp(struct getpgrp_args *uap)
100 {
101         struct proc *p = curproc;
102
103         uap->sysmsg_result = p->p_pgrp->pg_id;
104         return (0);
105 }
106
107 /*
108  * Get an arbitary pid's process group id 
109  */
110 int
111 getpgid(struct getpgid_args *uap)
112 {
113         struct proc *p = curproc;
114         struct proc *pt;
115
116         pt = p;
117         if (uap->pid == 0)
118                 goto found;
119
120         if ((pt = pfind(uap->pid)) == 0)
121                 return ESRCH;
122 found:
123         uap->sysmsg_result = pt->p_pgrp->pg_id;
124         return 0;
125 }
126
127 /*
128  * Get an arbitary pid's session id.
129  */
130 int
131 getsid(struct getsid_args *uap)
132 {
133         struct proc *p = curproc;
134         struct proc *pt;
135
136         pt = p;
137         if (uap->pid == 0)
138                 goto found;
139
140         if ((pt = pfind(uap->pid)) == 0)
141                 return ESRCH;
142 found:
143         uap->sysmsg_result = pt->p_session->s_sid;
144         return 0;
145 }
146
147
148 /*
149  * getuid() - MP SAFE
150  */
151 /* ARGSUSED */
152 int
153 getuid(struct getuid_args *uap)
154 {
155         struct proc *p = curproc;
156
157         uap->sysmsg_fds[0] = p->p_ucred->cr_ruid;
158 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
159         uap->sysmsg_fds[1] = p->p_ucred->cr_uid;
160 #endif
161         return (0);
162 }
163
164 /*
165  * geteuid() - MP SAFE
166  */
167 /* ARGSUSED */
168 int
169 geteuid(struct geteuid_args *uap)
170 {
171         struct proc *p = curproc;
172
173         uap->sysmsg_result = p->p_ucred->cr_uid;
174         return (0);
175 }
176
177 /*
178  * getgid() - MP SAFE
179  */
180 /* ARGSUSED */
181 int
182 getgid(struct getgid_args *uap)
183 {
184         struct proc *p = curproc;
185
186         uap->sysmsg_fds[0] = p->p_ucred->cr_rgid;
187 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
188         uap->sysmsg_fds[1] = p->p_ucred->cr_groups[0];
189 #endif
190         return (0);
191 }
192
193 /*
194  * Get effective group ID.  The "egid" is groups[0], and could be obtained
195  * via getgroups.  This syscall exists because it is somewhat painful to do
196  * correctly in a library function.
197  *
198  * MP SAFE
199  */
200 /* ARGSUSED */
201 int
202 getegid(struct getegid_args *uap)
203 {
204         struct proc *p = curproc;
205
206         uap->sysmsg_result = p->p_ucred->cr_groups[0];
207         return (0);
208 }
209
210 int
211 getgroups(struct getgroups_args *uap)
212 {
213         struct proc *p = curproc;
214         struct ucred *cr;
215         u_int ngrp;
216         int error;
217
218         if (p == NULL)                          /* API enforcement */
219                 return(EPERM);
220         cr = p->p_ucred;
221
222         if ((ngrp = uap->gidsetsize) == 0) {
223                 uap->sysmsg_result = cr->cr_ngroups;
224                 return (0);
225         }
226         if (ngrp < cr->cr_ngroups)
227                 return (EINVAL);
228         ngrp = cr->cr_ngroups;
229         if ((error = copyout((caddr_t)cr->cr_groups,
230             (caddr_t)uap->gidset, ngrp * sizeof(gid_t))))
231                 return (error);
232         uap->sysmsg_result = ngrp;
233         return (0);
234 }
235
236 /* ARGSUSED */
237 int
238 setsid(struct setsid_args *uap)
239 {
240         struct proc *p = curproc;
241
242         if (p->p_pgid == p->p_pid || pgfind(p->p_pid)) {
243                 return (EPERM);
244         } else {
245                 (void)enterpgrp(p, p->p_pid, 1);
246                 uap->sysmsg_result = p->p_pid;
247                 return (0);
248         }
249 }
250
251 /*
252  * set process group (setpgid/old setpgrp)
253  *
254  * caller does setpgid(targpid, targpgid)
255  *
256  * pid must be caller or child of caller (ESRCH)
257  * if a child
258  *      pid must be in same session (EPERM)
259  *      pid can't have done an exec (EACCES)
260  * if pgid != pid
261  *      there must exist some pid in same session having pgid (EPERM)
262  * pid must not be session leader (EPERM)
263  */
264 /* ARGSUSED */
265 int
266 setpgid(struct setpgid_args *uap)
267 {
268         struct proc *curp = curproc;
269         struct proc *targp;             /* target process */
270         struct pgrp *pgrp;              /* target pgrp */
271
272         if (uap->pgid < 0)
273                 return (EINVAL);
274         if (uap->pid != 0 && uap->pid != curp->p_pid) {
275                 if ((targp = pfind(uap->pid)) == 0 || !inferior(targp))
276                         return (ESRCH);
277                 if (targp->p_pgrp == NULL ||  targp->p_session != curp->p_session)
278                         return (EPERM);
279                 if (targp->p_flag & P_EXEC)
280                         return (EACCES);
281         } else
282                 targp = curp;
283         if (SESS_LEADER(targp))
284                 return (EPERM);
285         if (uap->pgid == 0)
286                 uap->pgid = targp->p_pid;
287         else if (uap->pgid != targp->p_pid)
288                 if ((pgrp = pgfind(uap->pgid)) == 0 ||
289                     pgrp->pg_session != curp->p_session)
290                         return (EPERM);
291         return (enterpgrp(targp, uap->pgid, 0));
292 }
293
294 /*
295  * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
296  * compatible.  It says that setting the uid/gid to euid/egid is a special
297  * case of "appropriate privilege".  Once the rules are expanded out, this
298  * basically means that setuid(nnn) sets all three id's, in all permitted
299  * cases unless _POSIX_SAVED_IDS is enabled.  In that case, setuid(getuid())
300  * does not set the saved id - this is dangerous for traditional BSD
301  * programs.  For this reason, we *really* do not want to set
302  * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
303  */
304 #define POSIX_APPENDIX_B_4_2_2
305
306 /* ARGSUSED */
307 int
308 setuid(struct setuid_args *uap)
309 {
310         struct proc *p = curproc;
311         struct ucred *cr;
312         uid_t uid;
313         int error;
314
315         if (p == NULL)                          /* API enforcement */
316                 return(EPERM);
317         cr = p->p_ucred;
318
319         /*
320          * See if we have "permission" by POSIX 1003.1 rules.
321          *
322          * Note that setuid(geteuid()) is a special case of 
323          * "appropriate privileges" in appendix B.4.2.2.  We need
324          * to use this clause to be compatible with traditional BSD
325          * semantics.  Basically, it means that "setuid(xx)" sets all
326          * three id's (assuming you have privs).
327          *
328          * Notes on the logic.  We do things in three steps.
329          * 1: We determine if the euid is going to change, and do EPERM
330          *    right away.  We unconditionally change the euid later if this
331          *    test is satisfied, simplifying that part of the logic.
332          * 2: We determine if the real and/or saved uid's are going to
333          *    change.  Determined by compile options.
334          * 3: Change euid last. (after tests in #2 for "appropriate privs")
335          */
336         uid = uap->uid;
337         if (uid != cr->cr_ruid &&               /* allow setuid(getuid()) */
338 #ifdef _POSIX_SAVED_IDS
339             uid != crc->cr_svuid &&             /* allow setuid(saved gid) */
340 #endif
341 #ifdef POSIX_APPENDIX_B_4_2_2   /* Use BSD-compat clause from B.4.2.2 */
342             uid != cr->cr_uid &&        /* allow setuid(geteuid()) */
343 #endif
344             (error = suser_cred(cr, PRISON_ROOT)))
345                 return (error);
346
347 #ifdef _POSIX_SAVED_IDS
348         /*
349          * Do we have "appropriate privileges" (are we root or uid == euid)
350          * If so, we are changing the real uid and/or saved uid.
351          */
352         if (
353 #ifdef POSIX_APPENDIX_B_4_2_2   /* Use the clause from B.4.2.2 */
354             uid == cr->cr_uid ||
355 #endif
356             suser_cred(cr, PRISON_ROOT) == 0) /* we are using privs */
357 #endif
358         {
359                 /*
360                  * Set the real uid and transfer proc count to new user.
361                  */
362                 if (uid != cr->cr_ruid) {
363                         change_ruid(uid);
364                         setsugid();
365                 }
366                 /*
367                  * Set saved uid
368                  *
369                  * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
370                  * the security of seteuid() depends on it.  B.4.2.2 says it
371                  * is important that we should do this.
372                  */
373                 if (cr->cr_svuid != uid) {
374                         cr = cratom(&p->p_ucred);
375                         cr->cr_svuid = uid;
376                         setsugid();
377                 }
378         }
379
380         /*
381          * In all permitted cases, we are changing the euid.
382          * Copy credentials so other references do not see our changes.
383          */
384         if (cr->cr_uid != uid) {
385                 change_euid(uid);
386                 setsugid();
387         }
388         return (0);
389 }
390
391 /* ARGSUSED */
392 int
393 seteuid(struct seteuid_args *uap)
394 {
395         struct proc *p = curproc;
396         struct ucred *cr;
397         uid_t euid;
398         int error;
399
400         if (p == NULL)                          /* API enforcement */
401                 return(EPERM);
402
403         cr = p->p_ucred;
404         euid = uap->euid;
405         if (euid != cr->cr_ruid &&              /* allow seteuid(getuid()) */
406             euid != cr->cr_svuid &&             /* allow seteuid(saved uid) */
407             (error = suser_cred(cr, PRISON_ROOT)))
408                 return (error);
409         /*
410          * Everything's okay, do it.  Copy credentials so other references do
411          * not see our changes.
412          */
413         if (cr->cr_uid != euid) {
414                 change_euid(euid);
415                 setsugid();
416         }
417         return (0);
418 }
419
420 /* ARGSUSED */
421 int
422 setgid(struct setgid_args *uap)
423 {
424         struct proc *p = curproc;
425         struct ucred *cr;
426         gid_t gid;
427         int error;
428
429         if (p == NULL)                          /* API enforcement */
430                 return(EPERM);
431         cr = p->p_ucred;
432
433         /*
434          * See if we have "permission" by POSIX 1003.1 rules.
435          *
436          * Note that setgid(getegid()) is a special case of
437          * "appropriate privileges" in appendix B.4.2.2.  We need
438          * to use this clause to be compatible with traditional BSD
439          * semantics.  Basically, it means that "setgid(xx)" sets all
440          * three id's (assuming you have privs).
441          *
442          * For notes on the logic here, see setuid() above.
443          */
444         gid = uap->gid;
445         if (gid != cr->cr_rgid &&               /* allow setgid(getgid()) */
446 #ifdef _POSIX_SAVED_IDS
447             gid != cr->cr_svgid &&              /* allow setgid(saved gid) */
448 #endif
449 #ifdef POSIX_APPENDIX_B_4_2_2   /* Use BSD-compat clause from B.4.2.2 */
450             gid != cr->cr_groups[0] && /* allow setgid(getegid()) */
451 #endif
452             (error = suser_cred(cr, PRISON_ROOT)))
453                 return (error);
454
455 #ifdef _POSIX_SAVED_IDS
456         /*
457          * Do we have "appropriate privileges" (are we root or gid == egid)
458          * If so, we are changing the real uid and saved gid.
459          */
460         if (
461 #ifdef POSIX_APPENDIX_B_4_2_2   /* use the clause from B.4.2.2 */
462             gid == cr->cr_groups[0] ||
463 #endif
464             suser_cred(cr, PRISON_ROOT) == 0) /* we are using privs */
465 #endif
466         {
467                 /*
468                  * Set real gid
469                  */
470                 if (cr->cr_rgid != gid) {
471                         cr = cratom(&p->p_ucred);
472                         cr->cr_rgid = gid;
473                         setsugid();
474                 }
475                 /*
476                  * Set saved gid
477                  *
478                  * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
479                  * the security of setegid() depends on it.  B.4.2.2 says it
480                  * is important that we should do this.
481                  */
482                 if (cr->cr_svgid != gid) {
483                         cr = cratom(&p->p_ucred);
484                         cr->cr_svgid = gid;
485                         setsugid();
486                 }
487         }
488         /*
489          * In all cases permitted cases, we are changing the egid.
490          * Copy credentials so other references do not see our changes.
491          */
492         if (cr->cr_groups[0] != gid) {
493                 cr = cratom(&p->p_ucred);
494                 cr->cr_groups[0] = gid;
495                 setsugid();
496         }
497         return (0);
498 }
499
500 /* ARGSUSED */
501 int
502 setegid(struct setegid_args *uap)
503 {
504         struct proc *p = curproc;
505         struct ucred *cr;
506         gid_t egid;
507         int error;
508
509         if (p == NULL)                          /* API enforcement */
510                 return(EPERM);
511         cr = p->p_ucred;
512
513         egid = uap->egid;
514         if (egid != cr->cr_rgid &&              /* allow setegid(getgid()) */
515             egid != cr->cr_svgid &&             /* allow setegid(saved gid) */
516             (error = suser_cred(cr, PRISON_ROOT)))
517                 return (error);
518         if (cr->cr_groups[0] != egid) {
519                 cr = cratom(&p->p_ucred);
520                 cr->cr_groups[0] = egid;
521                 setsugid();
522         }
523         return (0);
524 }
525
526 /* ARGSUSED */
527 int
528 setgroups(struct setgroups_args *uap)
529 {
530         struct proc *p = curproc;
531         struct ucred *cr;
532         u_int ngrp;
533         int error;
534
535         if (p == NULL)                          /* API enforcement */
536                 return(EPERM);
537         cr = p->p_ucred;
538
539         if ((error = suser_cred(cr, PRISON_ROOT)))
540                 return (error);
541         ngrp = uap->gidsetsize;
542         if (ngrp > NGROUPS)
543                 return (EINVAL);
544         /*
545          * XXX A little bit lazy here.  We could test if anything has
546          * changed before cratom() and setting P_SUGID.
547          */
548         cr = cratom(&p->p_ucred);
549         if (ngrp < 1) {
550                 /*
551                  * setgroups(0, NULL) is a legitimate way of clearing the
552                  * groups vector on non-BSD systems (which generally do not
553                  * have the egid in the groups[0]).  We risk security holes
554                  * when running non-BSD software if we do not do the same.
555                  */
556                 cr->cr_ngroups = 1;
557         } else {
558                 if ((error = copyin((caddr_t)uap->gidset,
559                     (caddr_t)cr->cr_groups, ngrp * sizeof(gid_t))))
560                         return (error);
561                 cr->cr_ngroups = ngrp;
562         }
563         setsugid();
564         return (0);
565 }
566
567 /* ARGSUSED */
568 int
569 setreuid(struct setreuid_args *uap)
570 {
571         struct proc *p = curproc;
572         struct ucred *cr;
573         uid_t ruid, euid;
574         int error;
575
576         if (p == NULL)                          /* API enforcement */
577                 return(EPERM);
578         cr = p->p_ucred;
579
580         ruid = uap->ruid;
581         euid = uap->euid;
582         if (((ruid != (uid_t)-1 && ruid != cr->cr_ruid && ruid != cr->cr_svuid) ||
583              (euid != (uid_t)-1 && euid != cr->cr_uid &&
584              euid != cr->cr_ruid && euid != cr->cr_svuid)) &&
585             (error = suser_cred(cr, PRISON_ROOT)) != 0)
586                 return (error);
587
588         if (euid != (uid_t)-1 && cr->cr_uid != euid) {
589                 change_euid(euid);
590                 setsugid();
591         }
592         if (ruid != (uid_t)-1 && cr->cr_ruid != ruid) {
593                 change_ruid(ruid);
594                 setsugid();
595         }
596         if ((ruid != (uid_t)-1 || cr->cr_uid != cr->cr_ruid) &&
597             cr->cr_svuid != cr->cr_uid) {
598                 cr = cratom(&p->p_ucred);
599                 cr->cr_svuid = cr->cr_uid;
600                 setsugid();
601         }
602         return (0);
603 }
604
605 /* ARGSUSED */
606 int
607 setregid(struct setregid_args *uap)
608 {
609         struct proc *p = curproc;
610         struct ucred *cr;
611         gid_t rgid, egid;
612         int error;
613
614         if (p == NULL)                          /* API enforcement */
615                 return(EPERM);
616         cr = p->p_ucred;
617
618         rgid = uap->rgid;
619         egid = uap->egid;
620         if (((rgid != (gid_t)-1 && rgid != cr->cr_rgid && rgid != cr->cr_svgid) ||
621              (egid != (gid_t)-1 && egid != cr->cr_groups[0] &&
622              egid != cr->cr_rgid && egid != cr->cr_svgid)) &&
623             (error = suser_cred(cr, PRISON_ROOT)) != 0)
624                 return (error);
625
626         if (egid != (gid_t)-1 && cr->cr_groups[0] != egid) {
627                 cr = cratom(&p->p_ucred);
628                 cr->cr_groups[0] = egid;
629                 setsugid();
630         }
631         if (rgid != (gid_t)-1 && cr->cr_rgid != rgid) {
632                 cr = cratom(&p->p_ucred);
633                 cr->cr_rgid = rgid;
634                 setsugid();
635         }
636         if ((rgid != (gid_t)-1 || cr->cr_groups[0] != cr->cr_rgid) &&
637             cr->cr_svgid != cr->cr_groups[0]) {
638                 cr = cratom(&p->p_ucred);
639                 cr->cr_svgid = cr->cr_groups[0];
640                 setsugid();
641         }
642         return (0);
643 }
644
645 /*
646  * setresuid(ruid, euid, suid) is like setreuid except control over the
647  * saved uid is explicit.
648  */
649
650 /* ARGSUSED */
651 int
652 setresuid(struct setresuid_args *uap)
653 {
654         struct proc *p = curproc;
655         struct ucred *cr;
656         uid_t ruid, euid, suid;
657         int error;
658
659         cr = p->p_ucred;
660         ruid = uap->ruid;
661         euid = uap->euid;
662         suid = uap->suid;
663         if (((ruid != (uid_t)-1 && ruid != cr->cr_ruid && ruid != cr->cr_svuid &&
664               ruid != cr->cr_uid) ||
665              (euid != (uid_t)-1 && euid != cr->cr_ruid && euid != cr->cr_svuid &&
666               euid != cr->cr_uid) ||
667              (suid != (uid_t)-1 && suid != cr->cr_ruid && suid != cr->cr_svuid &&
668               suid != cr->cr_uid)) &&
669             (error = suser_cred(cr, PRISON_ROOT)) != 0)
670                 return (error);
671         if (euid != (uid_t)-1 && cr->cr_uid != euid) {
672                 change_euid(euid);
673                 setsugid();
674         }
675         if (ruid != (uid_t)-1 && cr->cr_ruid != ruid) {
676                 change_ruid(ruid);
677                 setsugid();
678         }
679         if (suid != (uid_t)-1 && cr->cr_svuid != suid) {
680                 cr = cratom(&p->p_ucred);
681                 cr->cr_svuid = suid;
682                 setsugid();
683         }
684         return (0);
685 }
686
687 /*
688  * setresgid(rgid, egid, sgid) is like setregid except control over the
689  * saved gid is explicit.
690  */
691
692 /* ARGSUSED */
693 int
694 setresgid(struct setresgid_args *uap)
695 {
696         struct proc *p = curproc;
697         struct ucred *cr;
698         gid_t rgid, egid, sgid;
699         int error;
700
701         cr = p->p_ucred;
702         rgid = uap->rgid;
703         egid = uap->egid;
704         sgid = uap->sgid;
705         if (((rgid != (gid_t)-1 && rgid != cr->cr_rgid && rgid != cr->cr_svgid &&
706               rgid != cr->cr_groups[0]) ||
707              (egid != (gid_t)-1 && egid != cr->cr_rgid && egid != cr->cr_svgid &&
708               egid != cr->cr_groups[0]) ||
709              (sgid != (gid_t)-1 && sgid != cr->cr_rgid && sgid != cr->cr_svgid &&
710               sgid != cr->cr_groups[0])) &&
711             (error = suser_cred(cr, PRISON_ROOT)) != 0)
712                 return (error);
713
714         if (egid != (gid_t)-1 && cr->cr_groups[0] != egid) {
715                 cr = cratom(&p->p_ucred);
716                 cr->cr_groups[0] = egid;
717                 setsugid();
718         }
719         if (rgid != (gid_t)-1 && cr->cr_rgid != rgid) {
720                 cr = cratom(&p->p_ucred);
721                 cr->cr_rgid = rgid;
722                 setsugid();
723         }
724         if (sgid != (gid_t)-1 && cr->cr_svgid != sgid) {
725                 cr = cratom(&p->p_ucred);
726                 cr->cr_svgid = sgid;
727                 setsugid();
728         }
729         return (0);
730 }
731
732 /* ARGSUSED */
733 int
734 getresuid(struct getresuid_args *uap)
735 {
736         struct proc *p = curproc;
737         struct ucred *cr = p->p_ucred;
738         int error1 = 0, error2 = 0, error3 = 0;
739
740         if (uap->ruid)
741                 error1 = copyout((caddr_t)&cr->cr_ruid,
742                     (caddr_t)uap->ruid, sizeof(cr->cr_ruid));
743         if (uap->euid)
744                 error2 = copyout((caddr_t)&cr->cr_uid,
745                     (caddr_t)uap->euid, sizeof(cr->cr_uid));
746         if (uap->suid)
747                 error3 = copyout((caddr_t)&cr->cr_svuid,
748                     (caddr_t)uap->suid, sizeof(cr->cr_svuid));
749         return error1 ? error1 : (error2 ? error2 : error3);
750 }
751
752 /* ARGSUSED */
753 int
754 getresgid(struct getresgid_args *uap)
755 {
756         struct proc *p = curproc;
757         struct ucred *cr = p->p_ucred;
758         int error1 = 0, error2 = 0, error3 = 0;
759
760         if (uap->rgid)
761                 error1 = copyout((caddr_t)&cr->cr_rgid,
762                     (caddr_t)uap->rgid, sizeof(cr->cr_rgid));
763         if (uap->egid)
764                 error2 = copyout((caddr_t)&cr->cr_groups[0],
765                     (caddr_t)uap->egid, sizeof(cr->cr_groups[0]));
766         if (uap->sgid)
767                 error3 = copyout((caddr_t)&cr->cr_svgid,
768                     (caddr_t)uap->sgid, sizeof(cr->cr_svgid));
769         return error1 ? error1 : (error2 ? error2 : error3);
770 }
771
772
773 /* ARGSUSED */
774 int
775 issetugid(struct issetugid_args *uap)
776 {
777         struct proc *p = curproc;
778         /*
779          * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
780          * we use P_SUGID because we consider changing the owners as
781          * "tainting" as well.
782          * This is significant for procs that start as root and "become"
783          * a user without an exec - programs cannot know *everything*
784          * that libc *might* have put in their data segment.
785          */
786         uap->sysmsg_result = (p->p_flag & P_SUGID) ? 1 : 0;
787         return (0);
788 }
789
790 /*
791  * Check if gid is a member of the group set.
792  */
793 int
794 groupmember(gid_t gid, struct ucred *cred)
795 {
796         gid_t *gp;
797         gid_t *egp;
798
799         egp = &(cred->cr_groups[cred->cr_ngroups]);
800         for (gp = cred->cr_groups; gp < egp; gp++) {
801                 if (*gp == gid)
802                         return (1);
803         }
804         return (0);
805 }
806
807 /*
808  * Test whether the specified credentials imply "super-user"
809  * privilege; if so, and we have accounting info, set the flag
810  * indicating use of super-powers.  A kernel thread without a process
811  * context is assumed to have super user capabilities.  In situations
812  * where the caller always expect a cred to exist, the cred should be
813  * passed separately and suser_cred()should be used instead of suser().
814  *
815  * Returns 0 or error.
816  */
817 int
818 suser(struct thread *td)
819 {
820         struct proc *p = td->td_proc;
821
822         if (p != NULL) {
823                 return suser_cred(p->p_ucred, 0);
824         } else {
825                 return (0);
826         }
827 }
828
829 /*
830  * A non-null credential is expected unless NULL_CRED_OKAY is set.
831  */
832 int
833 suser_cred(struct ucred *cred, int flag)
834 {
835         KASSERT(cred != NULL || flag & NULL_CRED_OKAY,
836                 ("suser_cred: NULL cred!"));
837
838         if (cred == NULL) {
839                 if (flag & NULL_CRED_OKAY)
840                         return (0);
841                 else
842                         return (EPERM);
843         }
844         if (cred->cr_uid != 0) 
845                 return (EPERM);
846         if (cred->cr_prison && !(flag & PRISON_ROOT))
847                 return (EPERM);
848         /* NOTE: accounting for suser access (p_acflag/ASU) removed */
849         return (0);
850 }
851
852 /*
853  * Return zero if p1 can fondle p2, return errno (EPERM/ESRCH) otherwise.
854  */
855 int
856 p_trespass(struct ucred *cr1, struct ucred *cr2)
857 {
858         if (cr1 == cr2)
859                 return (0);
860         if (!PRISON_CHECK(cr1, cr2))
861                 return (ESRCH);
862         if (cr1->cr_ruid == cr2->cr_ruid)
863                 return (0);
864         if (cr1->cr_uid == cr2->cr_ruid)
865                 return (0);
866         if (cr1->cr_ruid == cr2->cr_uid)
867                 return (0);
868         if (cr1->cr_uid == cr2->cr_uid)
869                 return (0);
870         if (suser_cred(cr1, PRISON_ROOT) == 0)
871                 return (0);
872         return (EPERM);
873 }
874
875 /*
876  * MPSAFE
877  */
878 static __inline void
879 _crinit(struct ucred *cr)
880 {
881         bzero(cr, sizeof(*cr));
882         cr->cr_ref = 1;
883         spin_init(&cr->cr_spin);
884 }
885
886 /*
887  * MPSAFE
888  */
889 void
890 crinit(struct ucred *cr)
891 {
892         _crinit(cr);
893 }
894
895 /*
896  * Allocate a zeroed cred structure.
897  *
898  * MPSAFE
899  */
900 struct ucred *
901 crget(void)
902 {
903         struct ucred *cr;
904
905         MALLOC(cr, struct ucred *, sizeof(*cr), M_CRED, M_WAITOK);
906         _crinit(cr);
907         return (cr);
908 }
909
910 /*
911  * Claim another reference to a ucred structure.  Can be used with special
912  * creds.
913  *
914  * It must be possible to call this routine with spinlocks held, meaning
915  * that this routine itself cannot obtain a spinlock.
916  *
917  * MPSAFE
918  */
919 struct ucred *
920 crhold(struct ucred *cr)
921 {
922         if (cr != NOCRED && cr != FSCRED)
923                 atomic_add_int(&cr->cr_ref, 1);
924         return(cr);
925 }
926
927 /*
928  * Drop a reference from the cred structure, free it if the reference count
929  * reaches 0. 
930  *
931  * NOTE: because we used atomic_add_int() above, without a spinlock, we
932  * must also use atomic_subtract_int() below.  A spinlock is required
933  * in crfree() to handle multiple callers racing the refcount to 0.
934  *
935  * MPALMOSTSAFE - acquires mplock on 1->0 transition of ref count
936  */
937 void
938 crfree(struct ucred *cr)
939 {
940         if (cr->cr_ref <= 0)
941                 panic("Freeing already free credential! %p", cr);
942         spin_lock_wr(&cr->cr_spin);
943         atomic_subtract_int(&cr->cr_ref, 1);
944         if (cr->cr_ref == 0) {
945                 spin_unlock_wr(&cr->cr_spin);
946                 /*
947                  * Some callers of crget(), such as nfs_statfs(),
948                  * allocate a temporary credential, but don't
949                  * allocate a uidinfo structure.
950                  */
951                 get_mplock();
952                 if (cr->cr_uidinfo != NULL) {
953                         uidrop(cr->cr_uidinfo);
954                         cr->cr_uidinfo = NULL;
955                 }
956                 if (cr->cr_ruidinfo != NULL) {
957                         uidrop(cr->cr_ruidinfo);
958                         cr->cr_ruidinfo = NULL;
959                 }
960
961                 /*
962                  * Destroy empty prisons
963                  */
964                 if (jailed(cr))
965                         prison_free(cr->cr_prison);
966                 cr->cr_prison = NULL;   /* safety */
967
968                 FREE((caddr_t)cr, M_CRED);
969                 rel_mplock();
970         } else {
971                 spin_unlock_wr(&cr->cr_spin);
972         }
973 }
974
975 /*
976  * Atomize a cred structure so it can be modified without polluting
977  * other references to it.
978  */
979 struct ucred *
980 cratom(struct ucred **pcr)
981 {
982         struct ucred *oldcr;
983         struct ucred *newcr;
984
985         oldcr = *pcr;
986         if (oldcr->cr_ref == 1)
987                 return (oldcr);
988         newcr = crget();
989         *newcr = *oldcr;
990         if (newcr->cr_uidinfo)
991                 uihold(newcr->cr_uidinfo);
992         if (newcr->cr_ruidinfo)
993                 uihold(newcr->cr_ruidinfo);
994         if (jailed(newcr))
995                 prison_hold(newcr->cr_prison);
996         newcr->cr_ref = 1;
997         crfree(oldcr);
998         *pcr = newcr;
999         return (newcr);
1000 }
1001
1002 #if 0   /* no longer used but keep around for a little while */
1003 /*
1004  * Copy cred structure to a new one and free the old one.
1005  */
1006 struct ucred *
1007 crcopy(struct ucred *cr)
1008 {
1009         struct ucred *newcr;
1010
1011         if (cr->cr_ref == 1)
1012                 return (cr);
1013         newcr = crget();
1014         *newcr = *cr;
1015         if (newcr->cr_uidinfo)
1016                 uihold(newcr->cr_uidinfo);
1017         if (newcr->cr_ruidinfo)
1018                 uihold(newcr->cr_ruidinfo);
1019         if (jailed(newcr))
1020                 prison_hold(newcr->cr_prison);
1021         newcr->cr_ref = 1;
1022         crfree(cr);
1023         return (newcr);
1024 }
1025 #endif
1026
1027 /*
1028  * Dup cred struct to a new held one.
1029  */
1030 struct ucred *
1031 crdup(struct ucred *cr)
1032 {
1033         struct ucred *newcr;
1034
1035         newcr = crget();
1036         *newcr = *cr;
1037         if (newcr->cr_uidinfo)
1038                 uihold(newcr->cr_uidinfo);
1039         if (newcr->cr_ruidinfo)
1040                 uihold(newcr->cr_ruidinfo);
1041         if (jailed(newcr))
1042                 prison_hold(newcr->cr_prison);
1043         newcr->cr_ref = 1;
1044         return (newcr);
1045 }
1046
1047 /*
1048  * Fill in a struct xucred based on a struct ucred.
1049  */
1050 void
1051 cru2x(struct ucred *cr, struct xucred *xcr)
1052 {
1053
1054         bzero(xcr, sizeof(*xcr));
1055         xcr->cr_version = XUCRED_VERSION;
1056         xcr->cr_uid = cr->cr_uid;
1057         xcr->cr_ngroups = cr->cr_ngroups;
1058         bcopy(cr->cr_groups, xcr->cr_groups, sizeof(cr->cr_groups));
1059 }
1060
1061 /*
1062  * Get login name, if available.
1063  */
1064 /* ARGSUSED */
1065 int
1066 getlogin(struct getlogin_args *uap)
1067 {
1068         struct proc *p = curproc;
1069
1070         if (uap->namelen > MAXLOGNAME)
1071                 uap->namelen = MAXLOGNAME;
1072         return (copyout((caddr_t) p->p_pgrp->pg_session->s_login,
1073             (caddr_t) uap->namebuf, uap->namelen));
1074 }
1075
1076 /*
1077  * Set login name.
1078  */
1079 /* ARGSUSED */
1080 int
1081 setlogin(struct setlogin_args *uap)
1082 {
1083         struct proc *p = curproc;
1084         int error;
1085         char logintmp[MAXLOGNAME];
1086
1087         KKASSERT(p != NULL);
1088         if ((error = suser_cred(p->p_ucred, PRISON_ROOT)))
1089                 return (error);
1090         error = copyinstr((caddr_t) uap->namebuf, (caddr_t) logintmp,
1091             sizeof(logintmp), (size_t *)0);
1092         if (error == ENAMETOOLONG)
1093                 error = EINVAL;
1094         else if (!error)
1095                 (void) memcpy(p->p_pgrp->pg_session->s_login, logintmp,
1096                     sizeof(logintmp));
1097         return (error);
1098 }
1099
1100 void
1101 setsugid(void)
1102 {
1103         struct proc *p = curproc;
1104
1105         KKASSERT(p != NULL);
1106         p->p_flag |= P_SUGID;
1107         if (!(p->p_pfsflags & PF_ISUGID))
1108                 p->p_stops = 0;
1109 }
1110
1111 /*
1112  * Helper function to change the effective uid of a process
1113  */
1114 void
1115 change_euid(uid_t euid)
1116 {
1117         struct  proc *p = curproc;
1118         struct  ucred *cr;
1119
1120         KKASSERT(p != NULL);
1121         lf_count_adjust(p, 0);
1122         cr = cratom(&p->p_ucred);
1123         cr->cr_uid = euid;
1124         uireplace(&cr->cr_uidinfo, uifind(euid));
1125         lf_count_adjust(p, 1);
1126 }
1127
1128 /*
1129  * Helper function to change the real uid of a process
1130  *
1131  * The per-uid process count for this process is transfered from
1132  * the old uid to the new uid.
1133  */
1134 void
1135 change_ruid(uid_t ruid)
1136 {
1137         struct  proc *p = curproc;
1138         struct  ucred *cr;
1139
1140         KKASSERT(p != NULL);
1141
1142         cr = cratom(&p->p_ucred);
1143         (void)chgproccnt(cr->cr_ruidinfo, -1, 0);
1144         /* It is assumed that pcred is not shared between processes */
1145         cr->cr_ruid = ruid;
1146         uireplace(&cr->cr_ruidinfo, uifind(ruid));
1147         (void)chgproccnt(cr->cr_ruidinfo, 1, 0);
1148 }