xref: /freebsd/sys/kern/kern_prot.c (revision ce4946daa5ce852d28008dac492029500ab2ee95)
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$
40  */
41 
42 /*
43  * System calls related to processes and protection
44  */
45 
46 #include "opt_compat.h"
47 #include "opt_global.h"
48 
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/acct.h>
52 #include <sys/kernel.h>
53 #include <sys/lock.h>
54 #include <sys/mutex.h>
55 #include <sys/proc.h>
56 #include <sys/sysproto.h>
57 #include <sys/malloc.h>
58 #include <sys/pioctl.h>
59 #include <sys/resourcevar.h>
60 #include <sys/sysctl.h>
61 #include <sys/jail.h>
62 
63 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
64 
65 #ifndef _SYS_SYSPROTO_H_
66 struct getpid_args {
67 	int	dummy;
68 };
69 #endif
70 
71 /*
72  * getpid - MP SAFE
73  */
74 
75 /* ARGSUSED */
76 int
77 getpid(p, uap)
78 	struct proc *p;
79 	struct getpid_args *uap;
80 {
81 
82 	p->p_retval[0] = p->p_pid;
83 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
84 	PROC_LOCK(p);
85 	p->p_retval[1] = p->p_pptr->p_pid;
86 	PROC_UNLOCK(p);
87 #endif
88 	return (0);
89 }
90 
91 /*
92  * getppid - MP SAFE
93  */
94 
95 #ifndef _SYS_SYSPROTO_H_
96 struct getppid_args {
97         int     dummy;
98 };
99 #endif
100 /* ARGSUSED */
101 int
102 getppid(p, uap)
103 	struct proc *p;
104 	struct getppid_args *uap;
105 {
106 
107 	PROC_LOCK(p);
108 	p->p_retval[0] = p->p_pptr->p_pid;
109 	PROC_UNLOCK(p);
110 	return (0);
111 }
112 
113 /*
114  * Get process group ID; note that POSIX getpgrp takes no parameter
115  *
116  * MP SAFE
117  */
118 #ifndef _SYS_SYSPROTO_H_
119 struct getpgrp_args {
120         int     dummy;
121 };
122 #endif
123 
124 int
125 getpgrp(p, uap)
126 	struct proc *p;
127 	struct getpgrp_args *uap;
128 {
129 
130 	p->p_retval[0] = p->p_pgrp->pg_id;
131 	return (0);
132 }
133 
134 /* Get an arbitary pid's process group id */
135 #ifndef _SYS_SYSPROTO_H_
136 struct getpgid_args {
137 	pid_t	pid;
138 };
139 #endif
140 
141 int
142 getpgid(p, uap)
143 	struct proc *p;
144 	struct getpgid_args *uap;
145 {
146 	struct proc *pt;
147 	int error;
148 
149 	if (uap->pid == 0)
150 		p->p_retval[0] = p->p_pgrp->pg_id;
151 	else {
152 		if ((pt = pfind(uap->pid)) == NULL)
153 			return ESRCH;
154 		if ((error = p_can(p, pt, P_CAN_SEE, NULL))) {
155 			PROC_UNLOCK(pt);
156 			return (error);
157 		}
158 		p->p_retval[0] = pt->p_pgrp->pg_id;
159 		PROC_UNLOCK(pt);
160 	}
161 	return 0;
162 }
163 
164 /*
165  * Get an arbitary pid's session id.
166  */
167 #ifndef _SYS_SYSPROTO_H_
168 struct getsid_args {
169 	pid_t	pid;
170 };
171 #endif
172 
173 int
174 getsid(p, uap)
175 	struct proc *p;
176 	struct getsid_args *uap;
177 {
178 	struct proc *pt;
179 	int error;
180 
181 	if (uap->pid == 0)
182 		p->p_retval[0] = p->p_session->s_sid;
183 	else {
184 		if ((pt = pfind(uap->pid)) == NULL)
185 			return ESRCH;
186 		if ((error = p_can(p, pt, P_CAN_SEE, NULL))) {
187 			PROC_UNLOCK(pt);
188 			return (error);
189 		}
190 		p->p_retval[0] = pt->p_session->s_sid;
191 		PROC_UNLOCK(pt);
192 	}
193 	return 0;
194 }
195 
196 
197 /*
198  * getuid() - MP SAFE
199  */
200 #ifndef _SYS_SYSPROTO_H_
201 struct getuid_args {
202         int     dummy;
203 };
204 #endif
205 
206 /* ARGSUSED */
207 int
208 getuid(p, uap)
209 	struct proc *p;
210 	struct getuid_args *uap;
211 {
212 
213 	p->p_retval[0] = p->p_cred->p_ruid;
214 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
215 	p->p_retval[1] = p->p_ucred->cr_uid;
216 #endif
217 	return (0);
218 }
219 
220 /*
221  * geteuid() - MP SAFE
222  */
223 #ifndef _SYS_SYSPROTO_H_
224 struct geteuid_args {
225         int     dummy;
226 };
227 #endif
228 
229 /* ARGSUSED */
230 int
231 geteuid(p, uap)
232 	struct proc *p;
233 	struct geteuid_args *uap;
234 {
235 
236 	p->p_retval[0] = p->p_ucred->cr_uid;
237 	return (0);
238 }
239 
240 /*
241  * getgid() - MP SAFE
242  */
243 #ifndef _SYS_SYSPROTO_H_
244 struct getgid_args {
245         int     dummy;
246 };
247 #endif
248 
249 /* ARGSUSED */
250 int
251 getgid(p, uap)
252 	struct proc *p;
253 	struct getgid_args *uap;
254 {
255 
256 	p->p_retval[0] = p->p_cred->p_rgid;
257 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
258 	p->p_retval[1] = p->p_ucred->cr_groups[0];
259 #endif
260 	return (0);
261 }
262 
263 /*
264  * Get effective group ID.  The "egid" is groups[0], and could be obtained
265  * via getgroups.  This syscall exists because it is somewhat painful to do
266  * correctly in a library function.
267  */
268 #ifndef _SYS_SYSPROTO_H_
269 struct getegid_args {
270         int     dummy;
271 };
272 #endif
273 
274 /* ARGSUSED */
275 int
276 getegid(p, uap)
277 	struct proc *p;
278 	struct getegid_args *uap;
279 {
280 
281 	p->p_retval[0] = p->p_ucred->cr_groups[0];
282 	return (0);
283 }
284 
285 #ifndef _SYS_SYSPROTO_H_
286 struct getgroups_args {
287 	u_int	gidsetsize;
288 	gid_t	*gidset;
289 };
290 #endif
291 int
292 getgroups(p, uap)
293 	struct proc *p;
294 	register struct	getgroups_args *uap;
295 {
296 	register struct pcred *pc = p->p_cred;
297 	register u_int ngrp;
298 	int error;
299 
300 	if ((ngrp = uap->gidsetsize) == 0) {
301 		p->p_retval[0] = pc->pc_ucred->cr_ngroups;
302 		return (0);
303 	}
304 	if (ngrp < pc->pc_ucred->cr_ngroups)
305 		return (EINVAL);
306 	ngrp = pc->pc_ucred->cr_ngroups;
307 	if ((error = copyout((caddr_t)pc->pc_ucred->cr_groups,
308 	    (caddr_t)uap->gidset, ngrp * sizeof(gid_t))))
309 		return (error);
310 	p->p_retval[0] = ngrp;
311 	return (0);
312 }
313 
314 #ifndef _SYS_SYSPROTO_H_
315 struct setsid_args {
316         int     dummy;
317 };
318 #endif
319 
320 /* ARGSUSED */
321 int
322 setsid(p, uap)
323 	register struct proc *p;
324 	struct setsid_args *uap;
325 {
326 
327 	if (p->p_pgid == p->p_pid || pgfind(p->p_pid)) {
328 		return (EPERM);
329 	} else {
330 		(void)enterpgrp(p, p->p_pid, 1);
331 		p->p_retval[0] = p->p_pid;
332 		return (0);
333 	}
334 }
335 
336 /*
337  * set process group (setpgid/old setpgrp)
338  *
339  * caller does setpgid(targpid, targpgid)
340  *
341  * pid must be caller or child of caller (ESRCH)
342  * if a child
343  *	pid must be in same session (EPERM)
344  *	pid can't have done an exec (EACCES)
345  * if pgid != pid
346  * 	there must exist some pid in same session having pgid (EPERM)
347  * pid must not be session leader (EPERM)
348  */
349 #ifndef _SYS_SYSPROTO_H_
350 struct setpgid_args {
351 	int	pid;	/* target process id */
352 	int	pgid;	/* target pgrp id */
353 };
354 #endif
355 /* ARGSUSED */
356 int
357 setpgid(curp, uap)
358 	struct proc *curp;
359 	register struct setpgid_args *uap;
360 {
361 	register struct proc *targp;		/* target process */
362 	register struct pgrp *pgrp;		/* target pgrp */
363 	int error;
364 
365 	if (uap->pgid < 0)
366 		return (EINVAL);
367 	if (uap->pid != 0 && uap->pid != curp->p_pid) {
368 		if ((targp = pfind(uap->pid)) == NULL || !inferior(targp)) {
369 			if (targp)
370 				PROC_UNLOCK(targp);
371 			return (ESRCH);
372 		}
373 		if ((error = p_can(curproc, targp, P_CAN_SEE, NULL))) {
374 			PROC_UNLOCK(targp);
375 			return (error);
376 		}
377 		if (targp->p_pgrp == NULL ||
378 		    targp->p_session != curp->p_session) {
379 			PROC_UNLOCK(targp);
380 			return (EPERM);
381 		}
382 		if (targp->p_flag & P_EXEC) {
383 			PROC_UNLOCK(targp);
384 			return (EACCES);
385 		}
386 	} else {
387 		targp = curp;
388 		PROC_LOCK(curp);	/* XXX: not needed */
389 	}
390 	if (SESS_LEADER(targp)) {
391 		PROC_UNLOCK(targp);
392 		return (EPERM);
393 	}
394 	if (uap->pgid == 0)
395 		uap->pgid = targp->p_pid;
396 	else if (uap->pgid != targp->p_pid)
397 		if ((pgrp = pgfind(uap->pgid)) == 0 ||
398 	            pgrp->pg_session != curp->p_session) {
399 			PROC_UNLOCK(targp);
400 			return (EPERM);
401 		}
402 	/* XXX: We should probably hold the lock across enterpgrp. */
403 	PROC_UNLOCK(targp);
404 	return (enterpgrp(targp, uap->pgid, 0));
405 }
406 
407 /*
408  * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
409  * compatible.  It says that setting the uid/gid to euid/egid is a special
410  * case of "appropriate privilege".  Once the rules are expanded out, this
411  * basically means that setuid(nnn) sets all three id's, in all permitted
412  * cases unless _POSIX_SAVED_IDS is enabled.  In that case, setuid(getuid())
413  * does not set the saved id - this is dangerous for traditional BSD
414  * programs.  For this reason, we *really* do not want to set
415  * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
416  */
417 #define POSIX_APPENDIX_B_4_2_2
418 
419 #ifndef _SYS_SYSPROTO_H_
420 struct setuid_args {
421 	uid_t	uid;
422 };
423 #endif
424 /* ARGSUSED */
425 int
426 setuid(p, uap)
427 	struct proc *p;
428 	struct setuid_args *uap;
429 {
430 	register struct pcred *pc = p->p_cred;
431 	register uid_t uid;
432 	int error;
433 
434 	/*
435 	 * See if we have "permission" by POSIX 1003.1 rules.
436 	 *
437 	 * Note that setuid(geteuid()) is a special case of
438 	 * "appropriate privileges" in appendix B.4.2.2.  We need
439 	 * to use this clause to be compatible with traditional BSD
440 	 * semantics.  Basically, it means that "setuid(xx)" sets all
441 	 * three id's (assuming you have privs).
442 	 *
443 	 * Notes on the logic.  We do things in three steps.
444 	 * 1: We determine if the euid is going to change, and do EPERM
445 	 *    right away.  We unconditionally change the euid later if this
446 	 *    test is satisfied, simplifying that part of the logic.
447 	 * 2: We determine if the real and/or saved uid's are going to
448 	 *    change.  Determined by compile options.
449 	 * 3: Change euid last. (after tests in #2 for "appropriate privs")
450 	 */
451 	uid = uap->uid;
452 	if (uid != pc->p_ruid &&		/* allow setuid(getuid()) */
453 #ifdef _POSIX_SAVED_IDS
454 	    uid != pc->p_svuid &&		/* allow setuid(saved gid) */
455 #endif
456 #ifdef POSIX_APPENDIX_B_4_2_2	/* Use BSD-compat clause from B.4.2.2 */
457 	    uid != pc->pc_ucred->cr_uid &&	/* allow setuid(geteuid()) */
458 #endif
459 	    (error = suser_xxx(0, p, PRISON_ROOT)))
460 		return (error);
461 
462 #ifdef _POSIX_SAVED_IDS
463 	/*
464 	 * Do we have "appropriate privileges" (are we root or uid == euid)
465 	 * If so, we are changing the real uid and/or saved uid.
466 	 */
467 	if (
468 #ifdef POSIX_APPENDIX_B_4_2_2	/* Use the clause from B.4.2.2 */
469 	    uid == pc->pc_ucred->cr_uid ||
470 #endif
471 	    suser_xxx(0, p, PRISON_ROOT) == 0) /* we are using privs */
472 #endif
473 	{
474 		/*
475 		 * Set the real uid and transfer proc count to new user.
476 		 */
477 		if (uid != pc->p_ruid) {
478 			change_ruid(p, uid);
479 			setsugid(p);
480 		}
481 		/*
482 		 * Set saved uid
483 		 *
484 		 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
485 		 * the security of seteuid() depends on it.  B.4.2.2 says it
486 		 * is important that we should do this.
487 		 */
488 		if (pc->p_svuid != uid) {
489 			pc->p_svuid = uid;
490 			setsugid(p);
491 		}
492 	}
493 
494 	/*
495 	 * In all permitted cases, we are changing the euid.
496 	 * Copy credentials so other references do not see our changes.
497 	 */
498 	if (pc->pc_ucred->cr_uid != uid) {
499 		change_euid(p, uid);
500 		setsugid(p);
501 	}
502 	return (0);
503 }
504 
505 #ifndef _SYS_SYSPROTO_H_
506 struct seteuid_args {
507 	uid_t	euid;
508 };
509 #endif
510 /* ARGSUSED */
511 int
512 seteuid(p, uap)
513 	struct proc *p;
514 	struct seteuid_args *uap;
515 {
516 	register struct pcred *pc = p->p_cred;
517 	register uid_t euid;
518 	int error;
519 
520 	euid = uap->euid;
521 	if (euid != pc->p_ruid &&		/* allow seteuid(getuid()) */
522 	    euid != pc->p_svuid &&		/* allow seteuid(saved uid) */
523 	    (error = suser_xxx(0, p, PRISON_ROOT)))
524 		return (error);
525 	/*
526 	 * Everything's okay, do it.  Copy credentials so other references do
527 	 * not see our changes.
528 	 */
529 	if (pc->pc_ucred->cr_uid != euid) {
530 		change_euid(p, euid);
531 		setsugid(p);
532 	}
533 	return (0);
534 }
535 
536 #ifndef _SYS_SYSPROTO_H_
537 struct setgid_args {
538 	gid_t	gid;
539 };
540 #endif
541 /* ARGSUSED */
542 int
543 setgid(p, uap)
544 	struct proc *p;
545 	struct setgid_args *uap;
546 {
547 	register struct pcred *pc = p->p_cred;
548 	register gid_t gid;
549 	int error;
550 
551 	/*
552 	 * See if we have "permission" by POSIX 1003.1 rules.
553 	 *
554 	 * Note that setgid(getegid()) is a special case of
555 	 * "appropriate privileges" in appendix B.4.2.2.  We need
556 	 * to use this clause to be compatible with traditional BSD
557 	 * semantics.  Basically, it means that "setgid(xx)" sets all
558 	 * three id's (assuming you have privs).
559 	 *
560 	 * For notes on the logic here, see setuid() above.
561 	 */
562 	gid = uap->gid;
563 	if (gid != pc->p_rgid &&		/* allow setgid(getgid()) */
564 #ifdef _POSIX_SAVED_IDS
565 	    gid != pc->p_svgid &&		/* allow setgid(saved gid) */
566 #endif
567 #ifdef POSIX_APPENDIX_B_4_2_2	/* Use BSD-compat clause from B.4.2.2 */
568 	    gid != pc->pc_ucred->cr_groups[0] && /* allow setgid(getegid()) */
569 #endif
570 	    (error = suser_xxx(0, p, PRISON_ROOT)))
571 		return (error);
572 
573 #ifdef _POSIX_SAVED_IDS
574 	/*
575 	 * Do we have "appropriate privileges" (are we root or gid == egid)
576 	 * If so, we are changing the real uid and saved gid.
577 	 */
578 	if (
579 #ifdef POSIX_APPENDIX_B_4_2_2	/* use the clause from B.4.2.2 */
580 	    gid == pc->pc_ucred->cr_groups[0] ||
581 #endif
582 	    suser_xxx(0, p, PRISON_ROOT) == 0) /* we are using privs */
583 #endif
584 	{
585 		/*
586 		 * Set real gid
587 		 */
588 		if (pc->p_rgid != gid) {
589 			pc->p_rgid = gid;
590 			setsugid(p);
591 		}
592 		/*
593 		 * Set saved gid
594 		 *
595 		 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
596 		 * the security of setegid() depends on it.  B.4.2.2 says it
597 		 * is important that we should do this.
598 		 */
599 		if (pc->p_svgid != gid) {
600 			pc->p_svgid = gid;
601 			setsugid(p);
602 		}
603 	}
604 	/*
605 	 * In all cases permitted cases, we are changing the egid.
606 	 * Copy credentials so other references do not see our changes.
607 	 */
608 	if (pc->pc_ucred->cr_groups[0] != gid) {
609 		pc->pc_ucred = crcopy(pc->pc_ucred);
610 		pc->pc_ucred->cr_groups[0] = gid;
611 		setsugid(p);
612 	}
613 	return (0);
614 }
615 
616 #ifndef _SYS_SYSPROTO_H_
617 struct setegid_args {
618 	gid_t	egid;
619 };
620 #endif
621 /* ARGSUSED */
622 int
623 setegid(p, uap)
624 	struct proc *p;
625 	struct setegid_args *uap;
626 {
627 	register struct pcred *pc = p->p_cred;
628 	register gid_t egid;
629 	int error;
630 
631 	egid = uap->egid;
632 	if (egid != pc->p_rgid &&		/* allow setegid(getgid()) */
633 	    egid != pc->p_svgid &&		/* allow setegid(saved gid) */
634 	    (error = suser_xxx(0, p, PRISON_ROOT)))
635 		return (error);
636 	if (pc->pc_ucred->cr_groups[0] != egid) {
637 		pc->pc_ucred = crcopy(pc->pc_ucred);
638 		pc->pc_ucred->cr_groups[0] = egid;
639 		setsugid(p);
640 	}
641 	return (0);
642 }
643 
644 #ifndef _SYS_SYSPROTO_H_
645 struct setgroups_args {
646 	u_int	gidsetsize;
647 	gid_t	*gidset;
648 };
649 #endif
650 /* ARGSUSED */
651 int
652 setgroups(p, uap)
653 	struct proc *p;
654 	struct setgroups_args *uap;
655 {
656 	register struct pcred *pc = p->p_cred;
657 	register u_int ngrp;
658 	int error;
659 
660 	if ((error = suser_xxx(0, p, PRISON_ROOT)))
661 		return (error);
662 	ngrp = uap->gidsetsize;
663 	if (ngrp > NGROUPS)
664 		return (EINVAL);
665 	/*
666 	 * XXX A little bit lazy here.  We could test if anything has
667 	 * changed before crcopy() and setting P_SUGID.
668 	 */
669 	pc->pc_ucred = crcopy(pc->pc_ucred);
670 	if (ngrp < 1) {
671 		/*
672 		 * setgroups(0, NULL) is a legitimate way of clearing the
673 		 * groups vector on non-BSD systems (which generally do not
674 		 * have the egid in the groups[0]).  We risk security holes
675 		 * when running non-BSD software if we do not do the same.
676 		 */
677 		pc->pc_ucred->cr_ngroups = 1;
678 	} else {
679 		if ((error = copyin((caddr_t)uap->gidset,
680 		    (caddr_t)pc->pc_ucred->cr_groups, ngrp * sizeof(gid_t))))
681 			return (error);
682 		pc->pc_ucred->cr_ngroups = ngrp;
683 	}
684 	setsugid(p);
685 	return (0);
686 }
687 
688 #ifndef _SYS_SYSPROTO_H_
689 struct setreuid_args {
690 	uid_t	ruid;
691 	uid_t	euid;
692 };
693 #endif
694 /* ARGSUSED */
695 int
696 setreuid(p, uap)
697 	register struct proc *p;
698 	struct setreuid_args *uap;
699 {
700 	register struct pcred *pc = p->p_cred;
701 	register uid_t ruid, euid;
702 	int error;
703 
704 	ruid = uap->ruid;
705 	euid = uap->euid;
706 	if (((ruid != (uid_t)-1 && ruid != pc->p_ruid && ruid != pc->p_svuid) ||
707 	     (euid != (uid_t)-1 && euid != pc->pc_ucred->cr_uid &&
708 	     euid != pc->p_ruid && euid != pc->p_svuid)) &&
709 	    (error = suser_xxx(0, p, PRISON_ROOT)) != 0)
710 		return (error);
711 
712 	if (euid != (uid_t)-1 && pc->pc_ucred->cr_uid != euid) {
713 		change_euid(p, euid);
714 		setsugid(p);
715 	}
716 	if (ruid != (uid_t)-1 && pc->p_ruid != ruid) {
717 		change_ruid(p, ruid);
718 		setsugid(p);
719 	}
720 	if ((ruid != (uid_t)-1 || pc->pc_ucred->cr_uid != pc->p_ruid) &&
721 	    pc->p_svuid != pc->pc_ucred->cr_uid) {
722 		pc->p_svuid = pc->pc_ucred->cr_uid;
723 		setsugid(p);
724 	}
725 	return (0);
726 }
727 
728 #ifndef _SYS_SYSPROTO_H_
729 struct setregid_args {
730 	gid_t	rgid;
731 	gid_t	egid;
732 };
733 #endif
734 /* ARGSUSED */
735 int
736 setregid(p, uap)
737 	register struct proc *p;
738 	struct setregid_args *uap;
739 {
740 	register struct pcred *pc = p->p_cred;
741 	register gid_t rgid, egid;
742 	int error;
743 
744 	rgid = uap->rgid;
745 	egid = uap->egid;
746 	if (((rgid != (gid_t)-1 && rgid != pc->p_rgid && rgid != pc->p_svgid) ||
747 	     (egid != (gid_t)-1 && egid != pc->pc_ucred->cr_groups[0] &&
748 	     egid != pc->p_rgid && egid != pc->p_svgid)) &&
749 	    (error = suser_xxx(0, p, PRISON_ROOT)) != 0)
750 		return (error);
751 
752 	if (egid != (gid_t)-1 && pc->pc_ucred->cr_groups[0] != egid) {
753 		pc->pc_ucred = crcopy(pc->pc_ucred);
754 		pc->pc_ucred->cr_groups[0] = egid;
755 		setsugid(p);
756 	}
757 	if (rgid != (gid_t)-1 && pc->p_rgid != rgid) {
758 		pc->p_rgid = rgid;
759 		setsugid(p);
760 	}
761 	if ((rgid != (gid_t)-1 || pc->pc_ucred->cr_groups[0] != pc->p_rgid) &&
762 	    pc->p_svgid != pc->pc_ucred->cr_groups[0]) {
763 		pc->p_svgid = pc->pc_ucred->cr_groups[0];
764 		setsugid(p);
765 	}
766 	return (0);
767 }
768 
769 /*
770  * setresuid(ruid, euid, suid) is like setreuid except control over the
771  * saved uid is explicit.
772  */
773 
774 #ifndef _SYS_SYSPROTO_H_
775 struct setresuid_args {
776 	uid_t	ruid;
777 	uid_t	euid;
778 	uid_t	suid;
779 };
780 #endif
781 /* ARGSUSED */
782 int
783 setresuid(p, uap)
784 	register struct proc *p;
785 	struct setresuid_args *uap;
786 {
787 	register struct pcred *pc = p->p_cred;
788 	register uid_t ruid, euid, suid;
789 	int error;
790 
791 	ruid = uap->ruid;
792 	euid = uap->euid;
793 	suid = uap->suid;
794 	if (((ruid != (uid_t)-1 && ruid != pc->p_ruid && ruid != pc->p_svuid &&
795 	      ruid != pc->pc_ucred->cr_uid) ||
796 	     (euid != (uid_t)-1 && euid != pc->p_ruid && euid != pc->p_svuid &&
797 	      euid != pc->pc_ucred->cr_uid) ||
798 	     (suid != (uid_t)-1 && suid != pc->p_ruid && suid != pc->p_svuid &&
799 	      suid != pc->pc_ucred->cr_uid)) &&
800 	    (error = suser_xxx(0, p, PRISON_ROOT)) != 0)
801 		return (error);
802 	if (euid != (uid_t)-1 && pc->pc_ucred->cr_uid != euid) {
803 		change_euid(p, euid);
804 		setsugid(p);
805 	}
806 	if (ruid != (uid_t)-1 && pc->p_ruid != ruid) {
807 		change_ruid(p, ruid);
808 		setsugid(p);
809 	}
810 	if (suid != (uid_t)-1 && pc->p_svuid != suid) {
811 		pc->p_svuid = suid;
812 		setsugid(p);
813 	}
814 	return (0);
815 }
816 
817 /*
818  * setresgid(rgid, egid, sgid) is like setregid except control over the
819  * saved gid is explicit.
820  */
821 
822 #ifndef _SYS_SYSPROTO_H_
823 struct setresgid_args {
824 	gid_t	rgid;
825 	gid_t	egid;
826 	gid_t	sgid;
827 };
828 #endif
829 /* ARGSUSED */
830 int
831 setresgid(p, uap)
832 	register struct proc *p;
833 	struct setresgid_args *uap;
834 {
835 	register struct pcred *pc = p->p_cred;
836 	register gid_t rgid, egid, sgid;
837 	int error;
838 
839 	rgid = uap->rgid;
840 	egid = uap->egid;
841 	sgid = uap->sgid;
842 	if (((rgid != (gid_t)-1 && rgid != pc->p_rgid && rgid != pc->p_svgid &&
843 	      rgid != pc->pc_ucred->cr_groups[0]) ||
844 	     (egid != (gid_t)-1 && egid != pc->p_rgid && egid != pc->p_svgid &&
845 	      egid != pc->pc_ucred->cr_groups[0]) ||
846 	     (sgid != (gid_t)-1 && sgid != pc->p_rgid && sgid != pc->p_svgid &&
847 	      sgid != pc->pc_ucred->cr_groups[0])) &&
848 	    (error = suser_xxx(0, p, PRISON_ROOT)) != 0)
849 		return (error);
850 
851 	if (egid != (gid_t)-1 && pc->pc_ucred->cr_groups[0] != egid) {
852 		pc->pc_ucred = crcopy(pc->pc_ucred);
853 		pc->pc_ucred->cr_groups[0] = egid;
854 		setsugid(p);
855 	}
856 	if (rgid != (gid_t)-1 && pc->p_rgid != rgid) {
857 		pc->p_rgid = rgid;
858 		setsugid(p);
859 	}
860 	if (sgid != (gid_t)-1 && pc->p_svgid != sgid) {
861 		pc->p_svgid = sgid;
862 		setsugid(p);
863 	}
864 	return (0);
865 }
866 
867 #ifndef _SYS_SYSPROTO_H_
868 struct getresuid_args {
869 	uid_t	*ruid;
870 	uid_t	*euid;
871 	uid_t	*suid;
872 };
873 #endif
874 /* ARGSUSED */
875 int
876 getresuid(p, uap)
877 	register struct proc *p;
878 	struct getresuid_args *uap;
879 {
880 	struct pcred *pc = p->p_cred;
881 	int error1 = 0, error2 = 0, error3 = 0;
882 
883 	if (uap->ruid)
884 		error1 = copyout((caddr_t)&pc->p_ruid,
885 		    (caddr_t)uap->ruid, sizeof(pc->p_ruid));
886 	if (uap->euid)
887 		error2 = copyout((caddr_t)&pc->pc_ucred->cr_uid,
888 		    (caddr_t)uap->euid, sizeof(pc->pc_ucred->cr_uid));
889 	if (uap->suid)
890 		error3 = copyout((caddr_t)&pc->p_svuid,
891 		    (caddr_t)uap->suid, sizeof(pc->p_svuid));
892 	return error1 ? error1 : (error2 ? error2 : error3);
893 }
894 
895 #ifndef _SYS_SYSPROTO_H_
896 struct getresgid_args {
897 	gid_t	*rgid;
898 	gid_t	*egid;
899 	gid_t	*sgid;
900 };
901 #endif
902 /* ARGSUSED */
903 int
904 getresgid(p, uap)
905 	register struct proc *p;
906 	struct getresgid_args *uap;
907 {
908 	struct pcred *pc = p->p_cred;
909 	int error1 = 0, error2 = 0, error3 = 0;
910 
911 	if (uap->rgid)
912 		error1 = copyout((caddr_t)&pc->p_rgid,
913 		    (caddr_t)uap->rgid, sizeof(pc->p_rgid));
914 	if (uap->egid)
915 		error2 = copyout((caddr_t)&pc->pc_ucred->cr_groups[0],
916 		    (caddr_t)uap->egid, sizeof(pc->pc_ucred->cr_groups[0]));
917 	if (uap->sgid)
918 		error3 = copyout((caddr_t)&pc->p_svgid,
919 		    (caddr_t)uap->sgid, sizeof(pc->p_svgid));
920 	return error1 ? error1 : (error2 ? error2 : error3);
921 }
922 
923 
924 #ifndef _SYS_SYSPROTO_H_
925 struct issetugid_args {
926 	int dummy;
927 };
928 #endif
929 /* ARGSUSED */
930 int
931 issetugid(p, uap)
932 	register struct proc *p;
933 	struct issetugid_args *uap;
934 {
935 	/*
936 	 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
937 	 * we use P_SUGID because we consider changing the owners as
938 	 * "tainting" as well.
939 	 * This is significant for procs that start as root and "become"
940 	 * a user without an exec - programs cannot know *everything*
941 	 * that libc *might* have put in their data segment.
942 	 */
943 	p->p_retval[0] = (p->p_flag & P_SUGID) ? 1 : 0;
944 	return (0);
945 }
946 
947 int
948 __setugid(p, uap)
949 	struct proc *p;
950 	struct __setugid_args *uap;
951 {
952 
953 #ifdef REGRESSION
954 	switch (uap->flag) {
955 	case 0:
956 		p->p_flag &= ~P_SUGID;
957 		return (0);
958 	case 1:
959 		p->p_flag |= P_SUGID;
960 		return (0);
961 	default:
962 		return (EINVAL);
963 	}
964 #else /* !REGRESSION */
965 	return (ENOSYS);
966 #endif /* !REGRESSION */
967 }
968 
969 /*
970  * Check if gid is a member of the group set.
971  */
972 int
973 groupmember(gid, cred)
974 	gid_t gid;
975 	register struct ucred *cred;
976 {
977 	register gid_t *gp;
978 	gid_t *egp;
979 
980 	egp = &(cred->cr_groups[cred->cr_ngroups]);
981 	for (gp = cred->cr_groups; gp < egp; gp++)
982 		if (*gp == gid)
983 			return (1);
984 	return (0);
985 }
986 
987 static int suser_permitted = 1;
988 
989 SYSCTL_INT(_kern, OID_AUTO, suser_permitted, CTLFLAG_RW, &suser_permitted, 0,
990     "processes with uid 0 have privilege");
991 
992 /*
993  * Test whether the specified credentials imply "super-user"
994  * privilege; if so, and we have accounting info, set the flag
995  * indicating use of super-powers.
996  * Returns 0 or error.
997  */
998 int
999 suser(p)
1000 	struct proc *p;
1001 {
1002 	return suser_xxx(0, p, 0);
1003 }
1004 
1005 int
1006 suser_xxx(cred, proc, flag)
1007 	struct ucred *cred;
1008 	struct proc *proc;
1009 	int flag;
1010 {
1011 	if (!suser_permitted)
1012 		return (EPERM);
1013 	if (!cred && !proc) {
1014 		printf("suser_xxx(): THINK!\n");
1015 		return (EPERM);
1016 	}
1017 	if (!cred)
1018 		cred = proc->p_ucred;
1019 	if (cred->cr_uid != 0)
1020 		return (EPERM);
1021 	if (jailed(cred) && !(flag & PRISON_ROOT))
1022 		return (EPERM);
1023 	return (0);
1024 }
1025 
1026 /*
1027  * u_cansee(u1, u2): determine if u1 "can see" the subject specified by u2
1028  * Arguments: imutable credentials u1, u2
1029  * Returns: 0 for permitted, an errno value otherwise
1030  * Locks: none
1031  * References: u1 and u2 must be valid for the lifetime of the call
1032  *             u1 may equal u2, in which case only one reference is required
1033  */
1034 int
1035 u_cansee(struct ucred *u1, struct ucred *u2)
1036 {
1037 	int error;
1038 
1039 	if ((error = prison_check(u1, u2)))
1040 		return (error);
1041 	if (!ps_showallprocs && u1->cr_uid != u2->cr_uid) {
1042 		if (suser_xxx(u1, NULL, PRISON_ROOT) != 0)
1043 			return (ESRCH);
1044 	}
1045 	return (0);
1046 }
1047 
1048 static int
1049 p_cansee(struct proc *p1, struct proc *p2, int *privused)
1050 {
1051 
1052 	/* XXX: privused is going away, so don't do that here. */
1053 	if (privused != NULL)
1054 		*privused = 0;
1055 	/* Wrap u_cansee() for all functionality. */
1056 	return (u_cansee(p1->p_ucred, p2->p_ucred));
1057 }
1058 
1059 /*
1060  * Can process p1 send the signal signum to process p2?
1061  */
1062 int
1063 p_cansignal(struct proc *p1, struct proc *p2, int signum)
1064 {
1065 	int	error;
1066 
1067 	if (p1 == p2)
1068 		return (0);
1069 
1070 	/*
1071 	 * Jail semantics limit the scope of signalling to p2 in the same
1072 	 * jail as p1, if p1 is in jail.
1073 	 */
1074 	if ((error = prison_check(p1->p_ucred, p2->p_ucred)))
1075 		return (error);
1076 
1077 	/*
1078 	 * UNIX signalling semantics require that processes in the same
1079 	 * session always be able to deliver SIGCONT to one another,
1080 	 * overriding the remaining protections.
1081 	 */
1082 	if (signum == SIGCONT && p1->p_session == p2->p_session)
1083 		return (0);
1084 
1085 	/*
1086 	 * UNIX uid semantics depend on the status of the P_SUGID
1087 	 * bit on the target process.  If the bit is set, then more
1088 	 * restricted signal sets are permitted.
1089 	 */
1090 	if (p2->p_flag & P_SUGID) {
1091 		switch (signum) {
1092 		case 0:
1093 		case SIGKILL:
1094 		case SIGINT:
1095 		case SIGTERM:
1096 		case SIGSTOP:
1097 		case SIGTTIN:
1098 		case SIGTTOU:
1099 		case SIGTSTP:
1100 		case SIGHUP:
1101 		case SIGUSR1:
1102 		case SIGUSR2:
1103 			break;
1104 		default:
1105 			/* Not permitted, try privilege. */
1106 			error = suser_xxx(NULL, p1, PRISON_ROOT);
1107 			if (error)
1108 				return (error);
1109 		}
1110 	}
1111 
1112 	/*
1113 	 * Generally, the object credential's ruid or svuid must match the
1114 	 * subject credential's ruid or euid.
1115 	 */
1116 	if (p1->p_cred->p_ruid != p2->p_cred->p_ruid &&
1117 	    p1->p_cred->p_ruid != p2->p_cred->p_svuid &&
1118 	    p1->p_ucred->cr_uid != p2->p_cred->p_ruid &&
1119 	    p1->p_ucred->cr_uid != p2->p_cred->p_svuid) {
1120 		/* Not permitted, try privilege. */
1121 		error = suser_xxx(NULL, p1, PRISON_ROOT);
1122 		if (error)
1123 			return (error);
1124 	}
1125 
1126         return (0);
1127 }
1128 
1129 static int
1130 p_cansched(struct proc *p1, struct proc *p2, int *privused)
1131 {
1132 	int error;
1133 
1134 	if (privused != NULL)
1135 		*privused = 0;
1136 
1137 	if (p1 == p2)
1138 		return (0);
1139 
1140 	if ((error = prison_check(p1->p_ucred, p2->p_ucred)))
1141 		return (error);
1142 
1143 	if (p1->p_cred->p_ruid == p2->p_cred->p_ruid)
1144 		return (0);
1145 	if (p1->p_ucred->cr_uid == p2->p_cred->p_ruid)
1146 		return (0);
1147 
1148 	if (!suser_xxx(0, p1, PRISON_ROOT)) {
1149 		if (privused != NULL)
1150 			*privused = 1;
1151 		return (0);
1152 	}
1153 
1154 #ifdef CAPABILITIES
1155 	if (!cap_check_xxx(0, p1, CAP_SYS_NICE, PRISON_ROOT)) {
1156 		if (privused != NULL)
1157 			*privused = 1;
1158 		return (0);
1159 	}
1160 #endif
1161 
1162 	return (EPERM);
1163 }
1164 
1165 static int
1166 p_candebug(struct proc *p1, struct proc *p2, int *privused)
1167 {
1168 	int error;
1169 
1170 	if (privused != NULL)
1171 		*privused = 0;
1172 
1173 	if (p1 == p2)
1174 		return (0);
1175 
1176 	if ((error = prison_check(p1->p_ucred, p2->p_ucred)))
1177 		return (error);
1178 
1179 	/* not owned by you, has done setuid (unless you're root) */
1180 	/* add a CAP_SYS_PTRACE here? */
1181 	if (p1->p_cred->pc_ucred->cr_uid != p2->p_cred->p_ruid ||
1182 	    p1->p_cred->p_ruid != p2->p_cred->p_ruid ||
1183 	    p1->p_cred->p_svuid != p2->p_cred->p_ruid ||
1184 	    p2->p_flag & P_SUGID) {
1185 		if ((error = suser_xxx(0, p1, PRISON_ROOT)))
1186 			return (error);
1187 		if (privused != NULL)
1188 			*privused = 1;
1189 	}
1190 
1191 	/* can't trace init when securelevel > 0 */
1192 	if (securelevel > 0 && p2->p_pid == 1)
1193 		return (EPERM);
1194 
1195 	return (0);
1196 }
1197 
1198 int
1199 p_can(struct proc *p1, struct proc *p2, int operation,
1200     int *privused)
1201 {
1202 
1203 	switch(operation) {
1204 	case P_CAN_SEE:
1205 		return (p_cansee(p1, p2, privused));
1206 
1207 	case P_CAN_SCHED:
1208 		return (p_cansched(p1, p2, privused));
1209 
1210 	case P_CAN_DEBUG:
1211 		return (p_candebug(p1, p2, privused));
1212 
1213 	default:
1214 		panic("p_can: invalid operation");
1215 	}
1216 }
1217 
1218 
1219 /*
1220  * Allocate a zeroed cred structure.
1221  */
1222 struct ucred *
1223 crget()
1224 {
1225 	register struct ucred *cr;
1226 
1227 	MALLOC(cr, struct ucred *, sizeof(*cr), M_CRED, M_WAITOK|M_ZERO);
1228 	cr->cr_ref = 1;
1229 	mtx_init(&cr->cr_mtx, "ucred", MTX_DEF);
1230 	return (cr);
1231 }
1232 
1233 /*
1234  * Claim another reference to a ucred structure
1235  */
1236 void
1237 crhold(cr)
1238 	struct ucred *cr;
1239 {
1240 
1241 	mtx_lock(&cr->cr_mtx);
1242 	cr->cr_ref++;
1243 	mtx_unlock(&(cr)->cr_mtx);
1244 }
1245 
1246 
1247 /*
1248  * Free a cred structure.
1249  * Throws away space when ref count gets to 0.
1250  */
1251 void
1252 crfree(cr)
1253 	struct ucred *cr;
1254 {
1255 
1256 	mtx_lock(&cr->cr_mtx);
1257 	KASSERT(cr->cr_ref > 0, ("bad ucred refcount: %d", cr->cr_ref));
1258 	if (--cr->cr_ref == 0) {
1259 		mtx_destroy(&cr->cr_mtx);
1260 		/*
1261 		 * Some callers of crget(), such as nfs_statfs(),
1262 		 * allocate a temporary credential, but don't
1263 		 * allocate a uidinfo structure.
1264 		 */
1265 		if (cr->cr_uidinfo != NULL)
1266 			uifree(cr->cr_uidinfo);
1267 		/*
1268 		 * Free a prison, if any.
1269 		 */
1270 		if (jailed(cr))
1271 			prison_free(cr->cr_prison);
1272 		FREE((caddr_t)cr, M_CRED);
1273 	} else {
1274 		mtx_unlock(&cr->cr_mtx);
1275 	}
1276 }
1277 
1278 /*
1279  * Copy cred structure to a new one and free the old one.
1280  */
1281 struct ucred *
1282 crcopy(cr)
1283 	struct ucred *cr;
1284 {
1285 	struct ucred *newcr;
1286 
1287 	mtx_lock(&cr->cr_mtx);
1288 	if (cr->cr_ref == 1) {
1289 		mtx_unlock(&cr->cr_mtx);
1290 		return (cr);
1291 	}
1292 	mtx_unlock(&cr->cr_mtx);
1293 	newcr = crdup(cr);
1294 	crfree(cr);
1295 	return (newcr);
1296 }
1297 
1298 /*
1299  * Dup cred struct to a new held one.
1300  */
1301 struct ucred *
1302 crdup(cr)
1303 	struct ucred *cr;
1304 {
1305 	struct ucred *newcr;
1306 
1307 	MALLOC(newcr, struct ucred *, sizeof(*cr), M_CRED, M_WAITOK);
1308 	*newcr = *cr;
1309 	mtx_init(&newcr->cr_mtx, "ucred", MTX_DEF);
1310 	uihold(newcr->cr_uidinfo);
1311 	if (jailed(newcr))
1312 		prison_hold(newcr->cr_prison);
1313 	newcr->cr_ref = 1;
1314 	return (newcr);
1315 }
1316 
1317 /*
1318  * Get login name, if available.
1319  */
1320 #ifndef _SYS_SYSPROTO_H_
1321 struct getlogin_args {
1322 	char	*namebuf;
1323 	u_int	namelen;
1324 };
1325 #endif
1326 /* ARGSUSED */
1327 int
1328 getlogin(p, uap)
1329 	struct proc *p;
1330 	struct getlogin_args *uap;
1331 {
1332 
1333 	if (uap->namelen > MAXLOGNAME)
1334 		uap->namelen = MAXLOGNAME;
1335 	return (copyout((caddr_t) p->p_pgrp->pg_session->s_login,
1336 	    (caddr_t) uap->namebuf, uap->namelen));
1337 }
1338 
1339 /*
1340  * Set login name.
1341  */
1342 #ifndef _SYS_SYSPROTO_H_
1343 struct setlogin_args {
1344 	char	*namebuf;
1345 };
1346 #endif
1347 /* ARGSUSED */
1348 int
1349 setlogin(p, uap)
1350 	struct proc *p;
1351 	struct setlogin_args *uap;
1352 {
1353 	int error;
1354 	char logintmp[MAXLOGNAME];
1355 
1356 	if ((error = suser_xxx(0, p, PRISON_ROOT)))
1357 		return (error);
1358 	error = copyinstr((caddr_t) uap->namebuf, (caddr_t) logintmp,
1359 	    sizeof(logintmp), (size_t *)0);
1360 	if (error == ENAMETOOLONG)
1361 		error = EINVAL;
1362 	else if (!error)
1363 		(void) memcpy(p->p_pgrp->pg_session->s_login, logintmp,
1364 		    sizeof(logintmp));
1365 	return (error);
1366 }
1367 
1368 void
1369 setsugid(p)
1370 	struct proc *p;
1371 {
1372 	p->p_flag |= P_SUGID;
1373 	if (!(p->p_pfsflags & PF_ISUGID))
1374 		p->p_stops = 0;
1375 }
1376 
1377 /*
1378  * Helper function to change the effective uid of a process
1379  */
1380 void
1381 change_euid(p, euid)
1382 	struct	proc *p;
1383 	uid_t	euid;
1384 {
1385 	struct	pcred *pc;
1386 	struct	uidinfo *uip;
1387 
1388 	pc = p->p_cred;
1389 	/*
1390 	 * crcopy is essentially a NOP if ucred has a reference count
1391 	 * of 1, which is true if it has already been copied.
1392 	 */
1393 	pc->pc_ucred = crcopy(pc->pc_ucred);
1394 	uip = pc->pc_ucred->cr_uidinfo;
1395 	pc->pc_ucred->cr_uid = euid;
1396 	pc->pc_ucred->cr_uidinfo = uifind(euid);
1397 	uifree(uip);
1398 }
1399 
1400 /*
1401  * Helper function to change the real uid of a process
1402  *
1403  * The per-uid process count for this process is transfered from
1404  * the old uid to the new uid.
1405  */
1406 void
1407 change_ruid(p, ruid)
1408 	struct	proc *p;
1409 	uid_t	ruid;
1410 {
1411 	struct	pcred *pc;
1412 	struct	uidinfo *uip;
1413 
1414 	pc = p->p_cred;
1415 	(void)chgproccnt(pc->p_uidinfo, -1, 0);
1416 	uip = pc->p_uidinfo;
1417 	/* It is assumed that pcred is not shared between processes */
1418 	pc->p_ruid = ruid;
1419 	pc->p_uidinfo = uifind(ruid);
1420 	(void)chgproccnt(pc->p_uidinfo, 1, 0);
1421 	uifree(uip);
1422 }
1423