xref: /freebsd/sys/kern/kern_prot.c (revision 09e8dea79366f1e5b3a73e8a271b26e4b6bf2e6a)
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  * Copyright (c) 2000-2001 Robert N. M. Watson.  All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the University of
22  *	California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  *	@(#)kern_prot.c	8.6 (Berkeley) 1/21/94
40  * $FreeBSD$
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/systm.h>
51 #include <sys/acct.h>
52 #include <sys/kernel.h>
53 #include <sys/lock.h>
54 #include <sys/malloc.h>
55 #include <sys/mutex.h>
56 #include <sys/sx.h>
57 #include <sys/proc.h>
58 #include <sys/sysproto.h>
59 #include <sys/jail.h>
60 #include <sys/pioctl.h>
61 #include <sys/resourcevar.h>
62 #include <sys/socket.h>
63 #include <sys/socketvar.h>
64 #include <sys/sysctl.h>
65 
66 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
67 
68 SYSCTL_DECL(_security);
69 SYSCTL_NODE(_security, OID_AUTO, bsd, CTLFLAG_RW, 0,
70     "BSD security policy");
71 
72 #ifndef _SYS_SYSPROTO_H_
73 struct getpid_args {
74 	int	dummy;
75 };
76 #endif
77 /*
78  * MPSAFE
79  */
80 /* ARGSUSED */
81 int
82 getpid(struct thread *td, struct getpid_args *uap)
83 {
84 	struct proc *p = td->td_proc;
85 	int s;
86 
87 	s = mtx_lock_giant(kern_giant_proc);
88 	td->td_retval[0] = p->p_pid;
89 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
90 	PROC_LOCK(p);
91 	td->td_retval[1] = p->p_pptr->p_pid;
92 	PROC_UNLOCK(p);
93 #endif
94 	mtx_unlock_giant(s);
95 	return (0);
96 }
97 
98 #ifndef _SYS_SYSPROTO_H_
99 struct getppid_args {
100         int     dummy;
101 };
102 #endif
103 /*
104  * MPSAFE
105  */
106 /* ARGSUSED */
107 int
108 getppid(struct thread *td, struct getppid_args *uap)
109 {
110 	struct proc *p = td->td_proc;
111 	int s;
112 
113 	s = mtx_lock_giant(kern_giant_proc);
114 	PROC_LOCK(p);
115 	td->td_retval[0] = p->p_pptr->p_pid;
116 	PROC_UNLOCK(p);
117 	mtx_unlock_giant(s);
118 	return (0);
119 }
120 
121 /*
122  * Get process group ID; note that POSIX getpgrp takes no parameter.
123  */
124 #ifndef _SYS_SYSPROTO_H_
125 struct getpgrp_args {
126         int     dummy;
127 };
128 #endif
129 /*
130  * MPSAFE
131  */
132 int
133 getpgrp(struct thread *td, struct getpgrp_args *uap)
134 {
135 	struct proc *p = td->td_proc;
136 	int s;
137 
138 	s = mtx_lock_giant(kern_giant_proc);
139 	PROC_LOCK(p);
140 	td->td_retval[0] = p->p_pgrp->pg_id;
141 	PROC_UNLOCK(p);
142 	mtx_unlock_giant(s);
143 	return (0);
144 }
145 
146 /* Get an arbitary pid's process group id */
147 #ifndef _SYS_SYSPROTO_H_
148 struct getpgid_args {
149 	pid_t	pid;
150 };
151 #endif
152 /*
153  * MPSAFE
154  */
155 int
156 getpgid(struct thread *td, struct getpgid_args *uap)
157 {
158 	struct proc *p = td->td_proc;
159 	struct proc *pt;
160 	int error;
161 
162 	mtx_lock(&Giant);
163 	error = 0;
164 	if (uap->pid == 0) {
165 		PROC_LOCK(p);
166 		td->td_retval[0] = p->p_pgrp->pg_id;
167 		PROC_UNLOCK(p);
168 	} else if ((pt = pfind(uap->pid)) == NULL)
169 		error = ESRCH;
170 	else {
171 		error = p_cansee(td, pt);
172 		if (error == 0)
173 			td->td_retval[0] = pt->p_pgrp->pg_id;
174 		PROC_UNLOCK(pt);
175 	}
176 	mtx_unlock(&Giant);
177 	return (error);
178 }
179 
180 /*
181  * Get an arbitary pid's session id.
182  */
183 #ifndef _SYS_SYSPROTO_H_
184 struct getsid_args {
185 	pid_t	pid;
186 };
187 #endif
188 /*
189  * MPSAFE
190  */
191 int
192 getsid(struct thread *td, struct getsid_args *uap)
193 {
194 	struct proc *p = td->td_proc;
195 	struct proc *pt;
196 	int error;
197 
198 	mtx_lock(&Giant);
199 	error = 0;
200 	if (uap->pid == 0) {
201 		PROC_LOCK(p);
202 		td->td_retval[0] = p->p_session->s_sid;
203 		PROC_UNLOCK(p);
204 	} else if ((pt = pfind(uap->pid)) == NULL)
205 		error = ESRCH;
206 	else {
207 		error = p_cansee(td, pt);
208 		if (error == 0)
209 			td->td_retval[0] = pt->p_session->s_sid;
210 		PROC_UNLOCK(pt);
211 	}
212 	mtx_unlock(&Giant);
213 	return (error);
214 }
215 
216 #ifndef _SYS_SYSPROTO_H_
217 struct getuid_args {
218         int     dummy;
219 };
220 #endif
221 /*
222  * MPSAFE
223  */
224 /* ARGSUSED */
225 int
226 getuid(struct thread *td, struct getuid_args *uap)
227 {
228 
229 	td->td_retval[0] = td->td_ucred->cr_ruid;
230 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
231 	td->td_retval[1] = td->td_ucred->cr_uid;
232 #endif
233 	return (0);
234 }
235 
236 #ifndef _SYS_SYSPROTO_H_
237 struct geteuid_args {
238         int     dummy;
239 };
240 #endif
241 /*
242  * MPSAFE
243  */
244 /* ARGSUSED */
245 int
246 geteuid(struct thread *td, struct geteuid_args *uap)
247 {
248 
249 	td->td_retval[0] = td->td_ucred->cr_uid;
250 	return (0);
251 }
252 
253 #ifndef _SYS_SYSPROTO_H_
254 struct getgid_args {
255         int     dummy;
256 };
257 #endif
258 /*
259  * MPSAFE
260  */
261 /* ARGSUSED */
262 int
263 getgid(struct thread *td, struct getgid_args *uap)
264 {
265 
266 	td->td_retval[0] = td->td_ucred->cr_rgid;
267 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
268 	td->td_retval[1] = td->td_ucred->cr_groups[0];
269 #endif
270 	return (0);
271 }
272 
273 /*
274  * Get effective group ID.  The "egid" is groups[0], and could be obtained
275  * via getgroups.  This syscall exists because it is somewhat painful to do
276  * correctly in a library function.
277  */
278 #ifndef _SYS_SYSPROTO_H_
279 struct getegid_args {
280         int     dummy;
281 };
282 #endif
283 /*
284  * MPSAFE
285  */
286 /* ARGSUSED */
287 int
288 getegid(struct thread *td, struct getegid_args *uap)
289 {
290 
291 	td->td_retval[0] = td->td_ucred->cr_groups[0];
292 	return (0);
293 }
294 
295 #ifndef _SYS_SYSPROTO_H_
296 struct getgroups_args {
297 	u_int	gidsetsize;
298 	gid_t	*gidset;
299 };
300 #endif
301 /*
302  * MPSAFE
303  */
304 int
305 getgroups(struct thread *td, register struct getgroups_args *uap)
306 {
307 	struct ucred *cred;
308 	u_int ngrp;
309 	int error;
310 
311 	cred = td->td_ucred;
312 	if ((ngrp = uap->gidsetsize) == 0) {
313 		td->td_retval[0] = cred->cr_ngroups;
314 		return (0);
315 	}
316 	if (ngrp < cred->cr_ngroups)
317 		return (EINVAL);
318 	ngrp = cred->cr_ngroups;
319 	error = copyout((caddr_t)cred->cr_groups, (caddr_t)uap->gidset,
320 	    ngrp * sizeof(gid_t));
321 	if (error == 0)
322 		td->td_retval[0] = ngrp;
323 	return (error);
324 }
325 
326 #ifndef _SYS_SYSPROTO_H_
327 struct setsid_args {
328         int     dummy;
329 };
330 #endif
331 /*
332  * MPSAFE
333  */
334 /* ARGSUSED */
335 int
336 setsid(register struct thread *td, struct setsid_args *uap)
337 {
338 	struct pgrp *pgrp;
339 	int error;
340 	struct proc *p = td->td_proc;
341 	struct pgrp *newpgrp;
342 	struct session *newsess;
343 
344 	error = 0;
345 	pgrp = NULL;
346 
347 	MALLOC(newpgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
348 	MALLOC(newsess, struct session *, sizeof(struct session), M_SESSION, M_WAITOK | M_ZERO);
349 
350 	sx_xlock(&proctree_lock);
351 
352 	if (p->p_pgid == p->p_pid || (pgrp = pgfind(p->p_pid)) != NULL) {
353 		if (pgrp != NULL)
354 			PGRP_UNLOCK(pgrp);
355 		error = EPERM;
356 	} else {
357 		(void)enterpgrp(p, p->p_pid, newpgrp, newsess);
358 		td->td_retval[0] = p->p_pid;
359 		newpgrp = NULL;
360 		newsess = NULL;
361 	}
362 
363 	sx_xunlock(&proctree_lock);
364 
365 	if (newpgrp != NULL)
366 		FREE(newpgrp, M_PGRP);
367 	if (newsess != NULL)
368 		FREE(newsess, M_SESSION);
369 
370 	return (error);
371 }
372 
373 /*
374  * set process group (setpgid/old setpgrp)
375  *
376  * caller does setpgid(targpid, targpgid)
377  *
378  * pid must be caller or child of caller (ESRCH)
379  * if a child
380  *	pid must be in same session (EPERM)
381  *	pid can't have done an exec (EACCES)
382  * if pgid != pid
383  * 	there must exist some pid in same session having pgid (EPERM)
384  * pid must not be session leader (EPERM)
385  */
386 #ifndef _SYS_SYSPROTO_H_
387 struct setpgid_args {
388 	int	pid;		/* target process id */
389 	int	pgid;		/* target pgrp id */
390 };
391 #endif
392 /*
393  * MPSAFE
394  */
395 /* ARGSUSED */
396 int
397 setpgid(struct thread *td, register struct setpgid_args *uap)
398 {
399 	struct proc *curp = td->td_proc;
400 	register struct proc *targp;	/* target process */
401 	register struct pgrp *pgrp;	/* target pgrp */
402 	int error;
403 	struct pgrp *newpgrp;
404 
405 	if (uap->pgid < 0)
406 		return (EINVAL);
407 
408 	error = 0;
409 
410 	MALLOC(newpgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
411 
412 	sx_xlock(&proctree_lock);
413 	if (uap->pid != 0 && uap->pid != curp->p_pid) {
414 		if ((targp = pfind(uap->pid)) == NULL) {
415 			if (targp)
416 				PROC_UNLOCK(targp);
417 			error = ESRCH;
418 			goto done;
419 		}
420 		if (!inferior(targp)) {
421 			PROC_UNLOCK(targp);
422 			error = ESRCH;
423 			goto done;
424 		}
425 		if ((error = p_cansee(curthread, targp))) {
426 			PROC_UNLOCK(targp);
427 			goto done;
428 		}
429 		if (targp->p_pgrp == NULL ||
430 		    targp->p_session != curp->p_session) {
431 			PROC_UNLOCK(targp);
432 			error = EPERM;
433 			goto done;
434 		}
435 		if (targp->p_flag & P_EXEC) {
436 			PROC_UNLOCK(targp);
437 			error = EACCES;
438 			goto done;
439 		}
440 		PROC_UNLOCK(targp);
441 	} else
442 		targp = curp;
443 	if (SESS_LEADER(targp)) {
444 		error = EPERM;
445 		goto done;
446 	}
447 	if (uap->pgid == 0)
448 		uap->pgid = targp->p_pid;
449 	if (uap->pgid == targp->p_pid) {
450 		if (targp->p_pgid == uap->pgid)
451 			goto done;
452 		error = enterpgrp(targp, uap->pgid, newpgrp, NULL);
453 		if (error == 0)
454 			newpgrp = NULL;
455 	} else {
456 		if ((pgrp = pgfind(uap->pgid)) == NULL ||
457 		    pgrp->pg_session != curp->p_session) {
458 			if (pgrp != NULL)
459 				PGRP_UNLOCK(pgrp);
460 			error = EPERM;
461 			goto done;
462 		}
463 		if (pgrp == targp->p_pgrp) {
464 			PGRP_UNLOCK(pgrp);
465 			goto done;
466 		}
467 		PGRP_UNLOCK(pgrp);
468 		error = enterthispgrp(targp, pgrp);
469 	}
470 done:
471 	sx_xunlock(&proctree_lock);
472 	KASSERT((error == 0) || (newpgrp != NULL),
473 	    ("setpgid failed and newpgrp is NULL"));
474 	if (newpgrp != NULL)
475 		FREE(newpgrp, M_PGRP);
476 	return (error);
477 }
478 
479 /*
480  * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
481  * compatible.  It says that setting the uid/gid to euid/egid is a special
482  * case of "appropriate privilege".  Once the rules are expanded out, this
483  * basically means that setuid(nnn) sets all three id's, in all permitted
484  * cases unless _POSIX_SAVED_IDS is enabled.  In that case, setuid(getuid())
485  * does not set the saved id - this is dangerous for traditional BSD
486  * programs.  For this reason, we *really* do not want to set
487  * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
488  */
489 #define POSIX_APPENDIX_B_4_2_2
490 
491 #ifndef _SYS_SYSPROTO_H_
492 struct setuid_args {
493 	uid_t	uid;
494 };
495 #endif
496 /*
497  * MPSAFE
498  */
499 /* ARGSUSED */
500 int
501 setuid(struct thread *td, struct setuid_args *uap)
502 {
503 	struct proc *p = td->td_proc;
504 	struct ucred *newcred, *oldcred;
505 	uid_t uid;
506 	struct uidinfo *uip;
507 	int error;
508 
509 	mtx_lock(&Giant);
510 	uid = uap->uid;
511 	newcred = crget();
512 	uip = uifind(uid);
513 	PROC_LOCK(p);
514 	oldcred = p->p_ucred;
515 
516 	/*
517 	 * See if we have "permission" by POSIX 1003.1 rules.
518 	 *
519 	 * Note that setuid(geteuid()) is a special case of
520 	 * "appropriate privileges" in appendix B.4.2.2.  We need
521 	 * to use this clause to be compatible with traditional BSD
522 	 * semantics.  Basically, it means that "setuid(xx)" sets all
523 	 * three id's (assuming you have privs).
524 	 *
525 	 * Notes on the logic.  We do things in three steps.
526 	 * 1: We determine if the euid is going to change, and do EPERM
527 	 *    right away.  We unconditionally change the euid later if this
528 	 *    test is satisfied, simplifying that part of the logic.
529 	 * 2: We determine if the real and/or saved uids are going to
530 	 *    change.  Determined by compile options.
531 	 * 3: Change euid last. (after tests in #2 for "appropriate privs")
532 	 */
533 	if (uid != oldcred->cr_ruid &&		/* allow setuid(getuid()) */
534 #ifdef _POSIX_SAVED_IDS
535 	    uid != oldcred->cr_svuid &&		/* allow setuid(saved gid) */
536 #endif
537 #ifdef POSIX_APPENDIX_B_4_2_2	/* Use BSD-compat clause from B.4.2.2 */
538 	    uid != oldcred->cr_uid &&		/* allow setuid(geteuid()) */
539 #endif
540 	    (error = suser_cred(oldcred, PRISON_ROOT)) != 0) {
541 		PROC_UNLOCK(p);
542 		uifree(uip);
543 		crfree(newcred);
544 		mtx_unlock(&Giant);
545 		return (error);
546 	}
547 
548 	/*
549 	 * Copy credentials so other references do not see our changes.
550 	 */
551 	crcopy(newcred, oldcred);
552 #ifdef _POSIX_SAVED_IDS
553 	/*
554 	 * Do we have "appropriate privileges" (are we root or uid == euid)
555 	 * If so, we are changing the real uid and/or saved uid.
556 	 */
557 	if (
558 #ifdef POSIX_APPENDIX_B_4_2_2	/* Use the clause from B.4.2.2 */
559 	    uid == oldcred->cr_uid ||
560 #endif
561 	    suser_cred(oldcred, PRISON_ROOT) == 0) /* we are using privs */
562 #endif
563 	{
564 		/*
565 		 * Set the real uid and transfer proc count to new user.
566 		 */
567 		if (uid != oldcred->cr_ruid) {
568 			change_ruid(newcred, uip);
569 			setsugid(p);
570 		}
571 		/*
572 		 * Set saved uid
573 		 *
574 		 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
575 		 * the security of seteuid() depends on it.  B.4.2.2 says it
576 		 * is important that we should do this.
577 		 */
578 		if (uid != oldcred->cr_svuid) {
579 			change_svuid(newcred, uid);
580 			setsugid(p);
581 		}
582 	}
583 
584 	/*
585 	 * In all permitted cases, we are changing the euid.
586 	 */
587 	if (uid != oldcred->cr_uid) {
588 		change_euid(newcred, uip);
589 		setsugid(p);
590 	}
591 	p->p_ucred = newcred;
592 	PROC_UNLOCK(p);
593 	uifree(uip);
594 	crfree(oldcred);
595 	mtx_unlock(&Giant);
596 	return (0);
597 }
598 
599 #ifndef _SYS_SYSPROTO_H_
600 struct seteuid_args {
601 	uid_t	euid;
602 };
603 #endif
604 /*
605  * MPSAFE
606  */
607 /* ARGSUSED */
608 int
609 seteuid(struct thread *td, struct seteuid_args *uap)
610 {
611 	struct proc *p = td->td_proc;
612 	struct ucred *newcred, *oldcred;
613 	uid_t euid;
614 	struct uidinfo *euip;
615 	int error;
616 
617 	euid = uap->euid;
618 	mtx_lock(&Giant);
619 	newcred = crget();
620 	euip = uifind(euid);
621 	PROC_LOCK(p);
622 	oldcred = p->p_ucred;
623 	if (euid != oldcred->cr_ruid &&		/* allow seteuid(getuid()) */
624 	    euid != oldcred->cr_svuid &&	/* allow seteuid(saved uid) */
625 	    (error = suser_cred(oldcred, PRISON_ROOT)) != 0) {
626 		PROC_UNLOCK(p);
627 		uifree(euip);
628 		crfree(newcred);
629 		mtx_unlock(&Giant);
630 		return (error);
631 	}
632 	/*
633 	 * Everything's okay, do it.  Copy credentials so other references do
634 	 * not see our changes.
635 	 */
636 	crcopy(newcred, oldcred);
637 	if (oldcred->cr_uid != euid) {
638 		change_euid(newcred, euip);
639 		setsugid(p);
640 	}
641 	p->p_ucred = newcred;
642 	PROC_UNLOCK(p);
643 	uifree(euip);
644 	crfree(oldcred);
645 	mtx_unlock(&Giant);
646 	return (0);
647 }
648 
649 #ifndef _SYS_SYSPROTO_H_
650 struct setgid_args {
651 	gid_t	gid;
652 };
653 #endif
654 /*
655  * MPSAFE
656  */
657 /* ARGSUSED */
658 int
659 setgid(struct thread *td, struct setgid_args *uap)
660 {
661 	struct proc *p = td->td_proc;
662 	struct ucred *newcred, *oldcred;
663 	gid_t gid;
664 	int error;
665 
666 	gid = uap->gid;
667 	mtx_lock(&Giant);
668 	newcred = crget();
669 	PROC_LOCK(p);
670 	oldcred = p->p_ucred;
671 
672 	/*
673 	 * See if we have "permission" by POSIX 1003.1 rules.
674 	 *
675 	 * Note that setgid(getegid()) is a special case of
676 	 * "appropriate privileges" in appendix B.4.2.2.  We need
677 	 * to use this clause to be compatible with traditional BSD
678 	 * semantics.  Basically, it means that "setgid(xx)" sets all
679 	 * three id's (assuming you have privs).
680 	 *
681 	 * For notes on the logic here, see setuid() above.
682 	 */
683 	if (gid != oldcred->cr_rgid &&		/* allow setgid(getgid()) */
684 #ifdef _POSIX_SAVED_IDS
685 	    gid != oldcred->cr_svgid &&		/* allow setgid(saved gid) */
686 #endif
687 #ifdef POSIX_APPENDIX_B_4_2_2	/* Use BSD-compat clause from B.4.2.2 */
688 	    gid != oldcred->cr_groups[0] && /* allow setgid(getegid()) */
689 #endif
690 	    (error = suser_cred(oldcred, PRISON_ROOT)) != 0) {
691 		PROC_UNLOCK(p);
692 		crfree(newcred);
693 		mtx_unlock(&Giant);
694 		return (error);
695 	}
696 
697 	crcopy(newcred, oldcred);
698 #ifdef _POSIX_SAVED_IDS
699 	/*
700 	 * Do we have "appropriate privileges" (are we root or gid == egid)
701 	 * If so, we are changing the real uid and saved gid.
702 	 */
703 	if (
704 #ifdef POSIX_APPENDIX_B_4_2_2	/* use the clause from B.4.2.2 */
705 	    gid == oldcred->cr_groups[0] ||
706 #endif
707 	    suser_cred(oldcred, PRISON_ROOT) == 0) /* we are using privs */
708 #endif
709 	{
710 		/*
711 		 * Set real gid
712 		 */
713 		if (oldcred->cr_rgid != gid) {
714 			change_rgid(newcred, gid);
715 			setsugid(p);
716 		}
717 		/*
718 		 * Set saved gid
719 		 *
720 		 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
721 		 * the security of setegid() depends on it.  B.4.2.2 says it
722 		 * is important that we should do this.
723 		 */
724 		if (oldcred->cr_svgid != gid) {
725 			change_svgid(newcred, gid);
726 			setsugid(p);
727 		}
728 	}
729 	/*
730 	 * In all cases permitted cases, we are changing the egid.
731 	 * Copy credentials so other references do not see our changes.
732 	 */
733 	if (oldcred->cr_groups[0] != gid) {
734 		change_egid(newcred, gid);
735 		setsugid(p);
736 	}
737 	p->p_ucred = newcred;
738 	PROC_UNLOCK(p);
739 	crfree(oldcred);
740 	mtx_unlock(&Giant);
741 	return (0);
742 }
743 
744 #ifndef _SYS_SYSPROTO_H_
745 struct setegid_args {
746 	gid_t	egid;
747 };
748 #endif
749 /*
750  * MPSAFE
751  */
752 /* ARGSUSED */
753 int
754 setegid(struct thread *td, struct setegid_args *uap)
755 {
756 	struct proc *p = td->td_proc;
757 	struct ucred *newcred, *oldcred;
758 	gid_t egid;
759 	int error;
760 
761 	egid = uap->egid;
762 	mtx_lock(&Giant);
763 	newcred = crget();
764 	PROC_LOCK(p);
765 	oldcred = p->p_ucred;
766 	if (egid != oldcred->cr_rgid &&		/* allow setegid(getgid()) */
767 	    egid != oldcred->cr_svgid &&	/* allow setegid(saved gid) */
768 	    (error = suser_cred(oldcred, PRISON_ROOT)) != 0) {
769 		PROC_UNLOCK(p);
770 		crfree(newcred);
771 		mtx_unlock(&Giant);
772 		return (error);
773 	}
774 	crcopy(newcred, oldcred);
775 	if (oldcred->cr_groups[0] != egid) {
776 		change_egid(newcred, egid);
777 		setsugid(p);
778 	}
779 	p->p_ucred = newcred;
780 	PROC_UNLOCK(p);
781 	crfree(oldcred);
782 	mtx_unlock(&Giant);
783 	return (0);
784 }
785 
786 #ifndef _SYS_SYSPROTO_H_
787 struct setgroups_args {
788 	u_int	gidsetsize;
789 	gid_t	*gidset;
790 };
791 #endif
792 /*
793  * MPSAFE
794  */
795 /* ARGSUSED */
796 int
797 setgroups(struct thread *td, struct setgroups_args *uap)
798 {
799 	struct proc *p = td->td_proc;
800 	struct ucred *newcred, *tempcred, *oldcred;
801 	u_int ngrp;
802 	int error;
803 
804 	ngrp = uap->gidsetsize;
805 	if (ngrp > NGROUPS)
806 		return (EINVAL);
807 	mtx_lock(&Giant);
808 	tempcred = crget();
809 	error = copyin((caddr_t)uap->gidset, (caddr_t)tempcred->cr_groups,
810 	    ngrp * sizeof(gid_t));
811 	if (error != 0) {
812 		crfree(tempcred);
813 		mtx_unlock(&Giant);
814 		return (error);
815 	}
816 	newcred = crget();
817 	PROC_LOCK(p);
818 	oldcred = p->p_ucred;
819 	error = suser_cred(oldcred, PRISON_ROOT);
820 	if (error) {
821 		PROC_UNLOCK(p);
822 		crfree(newcred);
823 		crfree(tempcred);
824 		mtx_unlock(&Giant);
825 		return (error);
826 	}
827 
828 	/*
829 	 * XXX A little bit lazy here.  We could test if anything has
830 	 * changed before crcopy() and setting P_SUGID.
831 	 */
832 	crcopy(newcred, oldcred);
833 	if (ngrp < 1) {
834 		/*
835 		 * setgroups(0, NULL) is a legitimate way of clearing the
836 		 * groups vector on non-BSD systems (which generally do not
837 		 * have the egid in the groups[0]).  We risk security holes
838 		 * when running non-BSD software if we do not do the same.
839 		 */
840 		newcred->cr_ngroups = 1;
841 	} else {
842 		bcopy(tempcred->cr_groups, newcred->cr_groups,
843 		    ngrp * sizeof(gid_t));
844 		newcred->cr_ngroups = ngrp;
845 	}
846 	setsugid(p);
847 	p->p_ucred = newcred;
848 	PROC_UNLOCK(p);
849 	crfree(tempcred);
850 	crfree(oldcred);
851 	mtx_unlock(&Giant);
852 	return (0);
853 }
854 
855 #ifndef _SYS_SYSPROTO_H_
856 struct setreuid_args {
857 	uid_t	ruid;
858 	uid_t	euid;
859 };
860 #endif
861 /*
862  * MPSAFE
863  */
864 /* ARGSUSED */
865 int
866 setreuid(register struct thread *td, struct setreuid_args *uap)
867 {
868 	struct proc *p = td->td_proc;
869 	struct ucred *newcred, *oldcred;
870 	uid_t euid, ruid;
871 	struct uidinfo *euip, *ruip;
872 	int error;
873 
874 	euid = uap->euid;
875 	ruid = uap->ruid;
876 	mtx_lock(&Giant);
877 	newcred = crget();
878 	euip = uifind(euid);
879 	ruip = uifind(ruid);
880 	PROC_LOCK(p);
881 	oldcred = p->p_ucred;
882 	if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
883 	      ruid != oldcred->cr_svuid) ||
884 	     (euid != (uid_t)-1 && euid != oldcred->cr_uid &&
885 	      euid != oldcred->cr_ruid && euid != oldcred->cr_svuid)) &&
886 	    (error = suser_cred(oldcred, PRISON_ROOT)) != 0) {
887 		PROC_UNLOCK(p);
888 		uifree(ruip);
889 		uifree(euip);
890 		crfree(newcred);
891 		mtx_unlock(&Giant);
892 		return (error);
893 	}
894 	crcopy(newcred, oldcred);
895 	if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
896 		change_euid(newcred, euip);
897 		setsugid(p);
898 	}
899 	if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
900 		change_ruid(newcred, ruip);
901 		setsugid(p);
902 	}
903 	if ((ruid != (uid_t)-1 || newcred->cr_uid != newcred->cr_ruid) &&
904 	    newcred->cr_svuid != newcred->cr_uid) {
905 		change_svuid(newcred, newcred->cr_uid);
906 		setsugid(p);
907 	}
908 	p->p_ucred = newcred;
909 	PROC_UNLOCK(p);
910 	uifree(ruip);
911 	uifree(euip);
912 	crfree(oldcred);
913 	mtx_unlock(&Giant);
914 	return (0);
915 }
916 
917 #ifndef _SYS_SYSPROTO_H_
918 struct setregid_args {
919 	gid_t	rgid;
920 	gid_t	egid;
921 };
922 #endif
923 /*
924  * MPSAFE
925  */
926 /* ARGSUSED */
927 int
928 setregid(register struct thread *td, struct setregid_args *uap)
929 {
930 	struct proc *p = td->td_proc;
931 	struct ucred *newcred, *oldcred;
932 	gid_t egid, rgid;
933 	int error;
934 
935 	egid = uap->egid;
936 	rgid = uap->rgid;
937 	mtx_lock(&Giant);
938 	newcred = crget();
939 	PROC_LOCK(p);
940 	oldcred = p->p_ucred;
941 	if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
942 	    rgid != oldcred->cr_svgid) ||
943 	     (egid != (gid_t)-1 && egid != oldcred->cr_groups[0] &&
944 	     egid != oldcred->cr_rgid && egid != oldcred->cr_svgid)) &&
945 	    (error = suser_cred(oldcred, PRISON_ROOT)) != 0) {
946 		PROC_UNLOCK(p);
947 		crfree(newcred);
948 		mtx_unlock(&Giant);
949 		return (error);
950 	}
951 
952 	crcopy(newcred, oldcred);
953 	if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
954 		change_egid(newcred, egid);
955 		setsugid(p);
956 	}
957 	if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
958 		change_rgid(newcred, rgid);
959 		setsugid(p);
960 	}
961 	if ((rgid != (gid_t)-1 || newcred->cr_groups[0] != newcred->cr_rgid) &&
962 	    newcred->cr_svgid != newcred->cr_groups[0]) {
963 		change_svgid(newcred, newcred->cr_groups[0]);
964 		setsugid(p);
965 	}
966 	p->p_ucred = newcred;
967 	PROC_UNLOCK(p);
968 	crfree(oldcred);
969 	mtx_unlock(&Giant);
970 	return (0);
971 }
972 
973 /*
974  * setresuid(ruid, euid, suid) is like setreuid except control over the
975  * saved uid is explicit.
976  */
977 
978 #ifndef _SYS_SYSPROTO_H_
979 struct setresuid_args {
980 	uid_t	ruid;
981 	uid_t	euid;
982 	uid_t	suid;
983 };
984 #endif
985 /*
986  * MPSAFE
987  */
988 /* ARGSUSED */
989 int
990 setresuid(register struct thread *td, struct setresuid_args *uap)
991 {
992 	struct proc *p = td->td_proc;
993 	struct ucred *newcred, *oldcred;
994 	uid_t euid, ruid, suid;
995 	struct uidinfo *euip, *ruip;
996 	int error;
997 
998 	euid = uap->euid;
999 	ruid = uap->ruid;
1000 	suid = uap->suid;
1001 	mtx_lock(&Giant);
1002 	newcred = crget();
1003 	euip = uifind(euid);
1004 	ruip = uifind(ruid);
1005 	PROC_LOCK(p);
1006 	oldcred = p->p_ucred;
1007 	if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
1008 	     ruid != oldcred->cr_svuid &&
1009 	      ruid != oldcred->cr_uid) ||
1010 	     (euid != (uid_t)-1 && euid != oldcred->cr_ruid &&
1011 	    euid != oldcred->cr_svuid &&
1012 	      euid != oldcred->cr_uid) ||
1013 	     (suid != (uid_t)-1 && suid != oldcred->cr_ruid &&
1014 	    suid != oldcred->cr_svuid &&
1015 	      suid != oldcred->cr_uid)) &&
1016 	    (error = suser_cred(oldcred, PRISON_ROOT)) != 0) {
1017 		PROC_UNLOCK(p);
1018 		uifree(ruip);
1019 		uifree(euip);
1020 		crfree(newcred);
1021 		mtx_unlock(&Giant);
1022 		return (error);
1023 	}
1024 
1025 	crcopy(newcred, oldcred);
1026 	if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
1027 		change_euid(newcred, euip);
1028 		setsugid(p);
1029 	}
1030 	if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
1031 		change_ruid(newcred, ruip);
1032 		setsugid(p);
1033 	}
1034 	if (suid != (uid_t)-1 && oldcred->cr_svuid != suid) {
1035 		change_svuid(newcred, suid);
1036 		setsugid(p);
1037 	}
1038 	p->p_ucred = newcred;
1039 	PROC_UNLOCK(p);
1040 	uifree(ruip);
1041 	uifree(euip);
1042 	crfree(oldcred);
1043 	mtx_unlock(&Giant);
1044 	return (0);
1045 }
1046 
1047 /*
1048  * setresgid(rgid, egid, sgid) is like setregid except control over the
1049  * saved gid is explicit.
1050  */
1051 
1052 #ifndef _SYS_SYSPROTO_H_
1053 struct setresgid_args {
1054 	gid_t	rgid;
1055 	gid_t	egid;
1056 	gid_t	sgid;
1057 };
1058 #endif
1059 /*
1060  * MPSAFE
1061  */
1062 /* ARGSUSED */
1063 int
1064 setresgid(register struct thread *td, struct setresgid_args *uap)
1065 {
1066 	struct proc *p = td->td_proc;
1067 	struct ucred *newcred, *oldcred;
1068 	gid_t egid, rgid, sgid;
1069 	int error;
1070 
1071 	egid = uap->egid;
1072 	rgid = uap->rgid;
1073 	sgid = uap->sgid;
1074 	mtx_lock(&Giant);
1075 	newcred = crget();
1076 	PROC_LOCK(p);
1077 	oldcred = p->p_ucred;
1078 	if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
1079 	      rgid != oldcred->cr_svgid &&
1080 	      rgid != oldcred->cr_groups[0]) ||
1081 	     (egid != (gid_t)-1 && egid != oldcred->cr_rgid &&
1082 	      egid != oldcred->cr_svgid &&
1083 	      egid != oldcred->cr_groups[0]) ||
1084 	     (sgid != (gid_t)-1 && sgid != oldcred->cr_rgid &&
1085 	      sgid != oldcred->cr_svgid &&
1086 	      sgid != oldcred->cr_groups[0])) &&
1087 	    (error = suser_cred(oldcred, PRISON_ROOT)) != 0) {
1088 		PROC_UNLOCK(p);
1089 		crfree(newcred);
1090 		mtx_unlock(&Giant);
1091 		return (error);
1092 	}
1093 
1094 	crcopy(newcred, oldcred);
1095 	if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
1096 		change_egid(newcred, egid);
1097 		setsugid(p);
1098 	}
1099 	if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
1100 		change_rgid(newcred, rgid);
1101 		setsugid(p);
1102 	}
1103 	if (sgid != (gid_t)-1 && oldcred->cr_svgid != sgid) {
1104 		change_svgid(newcred, sgid);
1105 		setsugid(p);
1106 	}
1107 	p->p_ucred = newcred;
1108 	PROC_UNLOCK(p);
1109 	crfree(oldcred);
1110 	mtx_unlock(&Giant);
1111 	return (0);
1112 }
1113 
1114 #ifndef _SYS_SYSPROTO_H_
1115 struct getresuid_args {
1116 	uid_t	*ruid;
1117 	uid_t	*euid;
1118 	uid_t	*suid;
1119 };
1120 #endif
1121 /*
1122  * MPSAFE
1123  */
1124 /* ARGSUSED */
1125 int
1126 getresuid(register struct thread *td, struct getresuid_args *uap)
1127 {
1128 	struct ucred *cred;
1129 	int error1 = 0, error2 = 0, error3 = 0;
1130 
1131 	cred = td->td_ucred;
1132 	if (uap->ruid)
1133 		error1 = copyout((caddr_t)&cred->cr_ruid,
1134 		    (caddr_t)uap->ruid, sizeof(cred->cr_ruid));
1135 	if (uap->euid)
1136 		error2 = copyout((caddr_t)&cred->cr_uid,
1137 		    (caddr_t)uap->euid, sizeof(cred->cr_uid));
1138 	if (uap->suid)
1139 		error3 = copyout((caddr_t)&cred->cr_svuid,
1140 		    (caddr_t)uap->suid, sizeof(cred->cr_svuid));
1141 	return (error1 ? error1 : error2 ? error2 : error3);
1142 }
1143 
1144 #ifndef _SYS_SYSPROTO_H_
1145 struct getresgid_args {
1146 	gid_t	*rgid;
1147 	gid_t	*egid;
1148 	gid_t	*sgid;
1149 };
1150 #endif
1151 /*
1152  * MPSAFE
1153  */
1154 /* ARGSUSED */
1155 int
1156 getresgid(register struct thread *td, struct getresgid_args *uap)
1157 {
1158 	struct ucred *cred;
1159 	int error1 = 0, error2 = 0, error3 = 0;
1160 
1161 	cred = td->td_ucred;
1162 	if (uap->rgid)
1163 		error1 = copyout((caddr_t)&cred->cr_rgid,
1164 		    (caddr_t)uap->rgid, sizeof(cred->cr_rgid));
1165 	if (uap->egid)
1166 		error2 = copyout((caddr_t)&cred->cr_groups[0],
1167 		    (caddr_t)uap->egid, sizeof(cred->cr_groups[0]));
1168 	if (uap->sgid)
1169 		error3 = copyout((caddr_t)&cred->cr_svgid,
1170 		    (caddr_t)uap->sgid, sizeof(cred->cr_svgid));
1171 	return (error1 ? error1 : error2 ? error2 : error3);
1172 }
1173 
1174 #ifndef _SYS_SYSPROTO_H_
1175 struct issetugid_args {
1176 	int dummy;
1177 };
1178 #endif
1179 /*
1180  * NOT MPSAFE?
1181  */
1182 /* ARGSUSED */
1183 int
1184 issetugid(register struct thread *td, struct issetugid_args *uap)
1185 {
1186 	struct proc *p = td->td_proc;
1187 
1188 	/*
1189 	 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
1190 	 * we use P_SUGID because we consider changing the owners as
1191 	 * "tainting" as well.
1192 	 * This is significant for procs that start as root and "become"
1193 	 * a user without an exec - programs cannot know *everything*
1194 	 * that libc *might* have put in their data segment.
1195 	 */
1196 	PROC_LOCK(p);
1197 	td->td_retval[0] = (p->p_flag & P_SUGID) ? 1 : 0;
1198 	PROC_UNLOCK(p);
1199 	return (0);
1200 }
1201 
1202 /*
1203  * MPSAFE
1204  */
1205 int
1206 __setugid(struct thread *td, struct __setugid_args *uap)
1207 {
1208 #ifdef REGRESSION
1209 	struct proc *p;
1210 
1211 	p = td->td_proc;
1212 	switch (uap->flag) {
1213 	case 0:
1214 		mtx_lock(&Giant);
1215 		PROC_LOCK(p);
1216 		p->p_flag &= ~P_SUGID;
1217 		PROC_UNLOCK(p);
1218 		mtx_unlock(&Giant);
1219 		return (0);
1220 	case 1:
1221 		mtx_lock(&Giant);
1222 		PROC_LOCK(p);
1223 		p->p_flag |= P_SUGID;
1224 		PROC_UNLOCK(p);
1225 		mtx_unlock(&Giant);
1226 		return (0);
1227 	default:
1228 		return (EINVAL);
1229 	}
1230 #else /* !REGRESSION */
1231 
1232 	return (ENOSYS);
1233 #endif /* REGRESSION */
1234 }
1235 
1236 /*
1237  * Check if gid is a member of the group set.
1238  *
1239  * MPSAFE (cred must be held)
1240  */
1241 int
1242 groupmember(gid_t gid, struct ucred *cred)
1243 {
1244 	register gid_t *gp;
1245 	gid_t *egp;
1246 
1247 	egp = &(cred->cr_groups[cred->cr_ngroups]);
1248 	for (gp = cred->cr_groups; gp < egp; gp++)
1249 		if (*gp == gid)
1250 			return (1);
1251 	return (0);
1252 }
1253 
1254 /*
1255  * `suser_enabled' (which can be set by the security.suser_enabled
1256  * sysctl) determines whether the system 'super-user' policy is in effect.
1257  * If it is nonzero, an effective uid of 0 connotes special privilege,
1258  * overriding many mandatory and discretionary protections.  If it is zero,
1259  * uid 0 is offered no special privilege in the kernel security policy.
1260  * Setting it to zero may seriously impact the functionality of many
1261  * existing userland programs, and should not be done without careful
1262  * consideration of the consequences.
1263  */
1264 int	suser_enabled = 1;
1265 SYSCTL_INT(_security_bsd, OID_AUTO, suser_enabled, CTLFLAG_RW,
1266     &suser_enabled, 0, "processes with uid 0 have privilege");
1267 TUNABLE_INT("security.bsd.suser_enabled", &suser_enabled);
1268 
1269 /*
1270  * Test whether the specified credentials imply "super-user" privilege.
1271  * Return 0 or EPERM.  The flag argument is currently used only to
1272  * specify jail interaction.
1273  */
1274 int
1275 suser_cred(struct ucred *cred, int flag)
1276 {
1277 
1278 	if (!suser_enabled)
1279 		return (EPERM);
1280 	if (cred->cr_uid != 0)
1281 		return (EPERM);
1282 	if (jailed(cred) && !(flag & PRISON_ROOT))
1283 		return (EPERM);
1284 	return (0);
1285 }
1286 
1287 /*
1288  * Shortcut to hide contents of struct td and struct proc from the
1289  * caller, promoting binary compatibility.
1290  */
1291 int
1292 suser(struct thread *td)
1293 {
1294 
1295 	return (suser_cred(td->td_ucred, 0));
1296 }
1297 
1298 /*
1299  * Test the active securelevel against a given level.  securelevel_gt()
1300  * implements (securelevel > level).  securelevel_ge() implements
1301  * (securelevel >= level).  Note that the logic is inverted -- these
1302  * functions return EPERM on "success" and 0 on "failure".
1303  *
1304  * MPSAFE
1305  */
1306 int
1307 securelevel_gt(struct ucred *cr, int level)
1308 {
1309 	int active_securelevel;
1310 
1311 	active_securelevel = securelevel;
1312 	KASSERT(cr != NULL, ("securelevel_gt: null cr"));
1313 	if (cr->cr_prison != NULL) {
1314 		mtx_lock(&cr->cr_prison->pr_mtx);
1315 		active_securelevel = imax(cr->cr_prison->pr_securelevel,
1316 		    active_securelevel);
1317 		mtx_unlock(&cr->cr_prison->pr_mtx);
1318 	}
1319 	return (active_securelevel > level ? EPERM : 0);
1320 }
1321 
1322 int
1323 securelevel_ge(struct ucred *cr, int level)
1324 {
1325 	int active_securelevel;
1326 
1327 	active_securelevel = securelevel;
1328 	KASSERT(cr != NULL, ("securelevel_ge: null cr"));
1329 	if (cr->cr_prison != NULL) {
1330 		mtx_lock(&cr->cr_prison->pr_mtx);
1331 		active_securelevel = imax(cr->cr_prison->pr_securelevel,
1332 		    active_securelevel);
1333 		mtx_unlock(&cr->cr_prison->pr_mtx);
1334 	}
1335 	return (active_securelevel >= level ? EPERM : 0);
1336 }
1337 
1338 /*
1339  * 'see_other_uids' determines whether or not visibility of processes
1340  * and sockets with credentials holding different real uids is possible
1341  * using a variety of system MIBs.
1342  * XXX: data declarations should be together near the beginning of the file.
1343  */
1344 static int	see_other_uids = 1;
1345 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_uids, CTLFLAG_RW,
1346     &see_other_uids, 0,
1347     "Unprivileged processes may see subjects/objects with different real uid");
1348 
1349 /*-
1350  * Determine if u1 "can see" the subject specified by u2, according to the
1351  * 'see_other_uids' policy.
1352  * Returns: 0 for permitted, ESRCH otherwise
1353  * Locks: none
1354  * References: *u1 and *u2 must not change during the call
1355  *             u1 may equal u2, in which case only one reference is required
1356  */
1357 static int
1358 cr_seeotheruids(struct ucred *u1, struct ucred *u2)
1359 {
1360 
1361 	if (!see_other_uids && u1->cr_ruid != u2->cr_ruid) {
1362 		if (suser_cred(u1, PRISON_ROOT) != 0)
1363 			return (ESRCH);
1364 	}
1365 	return (0);
1366 }
1367 
1368 /*-
1369  * Determine if u1 "can see" the subject specified by u2.
1370  * Returns: 0 for permitted, an errno value otherwise
1371  * Locks: none
1372  * References: *u1 and *u2 must not change during the call
1373  *             u1 may equal u2, in which case only one reference is required
1374  */
1375 int
1376 cr_cansee(struct ucred *u1, struct ucred *u2)
1377 {
1378 	int error;
1379 
1380 	if ((error = prison_check(u1, u2)))
1381 		return (error);
1382 	if ((error = cr_seeotheruids(u1, u2)))
1383 		return (error);
1384 	return (0);
1385 }
1386 
1387 /*-
1388  * Determine if td "can see" the subject specified by p.
1389  * Returns: 0 for permitted, an errno value otherwise
1390  * Locks: Sufficient locks to protect p->p_ucred must be held.  td really
1391  *        should be curthread.
1392  * References: td and p must be valid for the lifetime of the call
1393  */
1394 int
1395 p_cansee(struct thread *td, struct proc *p)
1396 {
1397 
1398 	/* Wrap cr_cansee() for all functionality. */
1399 	KASSERT(td == curthread, ("%s: td not curthread", __func__));
1400 	PROC_LOCK_ASSERT(p, MA_OWNED);
1401 	return (cr_cansee(td->td_ucred, p->p_ucred));
1402 }
1403 
1404 /*-
1405  * Determine whether cred may deliver the specified signal to proc.
1406  * Returns: 0 for permitted, an errno value otherwise.
1407  * Locks: A lock must be held for proc.
1408  * References: cred and proc must be valid for the lifetime of the call.
1409  */
1410 int
1411 cr_cansignal(struct ucred *cred, struct proc *proc, int signum)
1412 {
1413 	int error;
1414 
1415 	PROC_LOCK_ASSERT(proc, MA_OWNED);
1416 	/*
1417 	 * Jail semantics limit the scope of signalling to proc in the
1418 	 * same jail as cred, if cred is in jail.
1419 	 */
1420 	error = prison_check(cred, proc->p_ucred);
1421 	if (error)
1422 		return (error);
1423 	error = cr_seeotheruids(cred, proc->p_ucred);
1424 	if (error)
1425 		return (error);
1426 
1427 	/*
1428 	 * UNIX signal semantics depend on the status of the P_SUGID
1429 	 * bit on the target process.  If the bit is set, then additional
1430 	 * restrictions are placed on the set of available signals.
1431 	 */
1432 	if (proc->p_flag & P_SUGID) {
1433 		switch (signum) {
1434 		case 0:
1435 		case SIGKILL:
1436 		case SIGINT:
1437 		case SIGTERM:
1438 		case SIGSTOP:
1439 		case SIGTTIN:
1440 		case SIGTTOU:
1441 		case SIGTSTP:
1442 		case SIGHUP:
1443 		case SIGUSR1:
1444 		case SIGUSR2:
1445 			/*
1446 			 * Generally, permit job and terminal control
1447 			 * signals.
1448 			 */
1449 			break;
1450 		default:
1451 			/* Not permitted without privilege. */
1452 			error = suser_cred(cred, PRISON_ROOT);
1453 			if (error)
1454 				return (error);
1455 		}
1456 	}
1457 
1458 	/*
1459 	 * Generally, the target credential's ruid or svuid must match the
1460 	 * subject credential's ruid or euid.
1461 	 */
1462 	if (cred->cr_ruid != proc->p_ucred->cr_ruid &&
1463 	    cred->cr_ruid != proc->p_ucred->cr_svuid &&
1464 	    cred->cr_uid != proc->p_ucred->cr_ruid &&
1465 	    cred->cr_uid != proc->p_ucred->cr_svuid) {
1466 		/* Not permitted without privilege. */
1467 		error = suser_cred(cred, PRISON_ROOT);
1468 		if (error)
1469 			return (error);
1470 	}
1471 
1472 	return (0);
1473 }
1474 
1475 
1476 /*-
1477  * Determine whether td may deliver the specified signal to p.
1478  * Returns: 0 for permitted, an errno value otherwise
1479  * Locks: Sufficient locks to protect various components of td and p
1480  *        must be held.  td must be curthread, and a lock must be
1481  *        held for p.
1482  * References: td and p must be valid for the lifetime of the call
1483  */
1484 int
1485 p_cansignal(struct thread *td, struct proc *p, int signum)
1486 {
1487 
1488 	KASSERT(td == curthread, ("%s: td not curthread", __func__));
1489 	PROC_LOCK_ASSERT(p, MA_OWNED);
1490 	if (td->td_proc == p)
1491 		return (0);
1492 
1493 	/*
1494 	 * UNIX signalling semantics require that processes in the same
1495 	 * session always be able to deliver SIGCONT to one another,
1496 	 * overriding the remaining protections.
1497 	 */
1498 	/* XXX: This will require an additional lock of some sort. */
1499 	if (signum == SIGCONT && td->td_proc->p_session == p->p_session)
1500 		return (0);
1501 
1502 	return (cr_cansignal(td->td_ucred, p, signum));
1503 }
1504 
1505 /*-
1506  * Determine whether td may reschedule p.
1507  * Returns: 0 for permitted, an errno value otherwise
1508  * Locks: Sufficient locks to protect various components of td and p
1509  *        must be held.  td must be curthread, and a lock must
1510  *        be held for p.
1511  * References: td and p must be valid for the lifetime of the call
1512  */
1513 int
1514 p_cansched(struct thread *td, struct proc *p)
1515 {
1516 	int error;
1517 
1518 	KASSERT(td == curthread, ("%s: td not curthread", __func__));
1519 	PROC_LOCK_ASSERT(p, MA_OWNED);
1520 	if (td->td_proc == p)
1521 		return (0);
1522 	if ((error = prison_check(td->td_ucred, p->p_ucred)))
1523 		return (error);
1524 	if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1525 		return (error);
1526 	if (td->td_ucred->cr_ruid == p->p_ucred->cr_ruid)
1527 		return (0);
1528 	if (td->td_ucred->cr_uid == p->p_ucred->cr_ruid)
1529 		return (0);
1530 	if (suser_cred(td->td_ucred, PRISON_ROOT) == 0)
1531 		return (0);
1532 
1533 #ifdef CAPABILITIES
1534 	if (!cap_check(NULL, td, CAP_SYS_NICE, PRISON_ROOT))
1535 		return (0);
1536 #endif
1537 
1538 	return (EPERM);
1539 }
1540 
1541 /*
1542  * The 'unprivileged_proc_debug' flag may be used to disable a variety of
1543  * unprivileged inter-process debugging services, including some procfs
1544  * functionality, ptrace(), and ktrace().  In the past, inter-process
1545  * debugging has been involved in a variety of security problems, and sites
1546  * not requiring the service might choose to disable it when hardening
1547  * systems.
1548  *
1549  * XXX: Should modifying and reading this variable require locking?
1550  * XXX: data declarations should be together near the beginning of the file.
1551  */
1552 static int	unprivileged_proc_debug = 1;
1553 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_proc_debug, CTLFLAG_RW,
1554     &unprivileged_proc_debug, 0,
1555     "Unprivileged processes may use process debugging facilities");
1556 
1557 /*-
1558  * Determine whether td may debug p.
1559  * Returns: 0 for permitted, an errno value otherwise
1560  * Locks: Sufficient locks to protect various components of td and p
1561  *        must be held.  td must be curthread, and a lock must
1562  *        be held for p.
1563  * References: td and p must be valid for the lifetime of the call
1564  */
1565 int
1566 p_candebug(struct thread *td, struct proc *p)
1567 {
1568 	int credentialchanged, error, grpsubset, i, uidsubset;
1569 
1570 	KASSERT(td == curthread, ("%s: td not curthread", __func__));
1571 	PROC_LOCK_ASSERT(p, MA_OWNED);
1572 	if (!unprivileged_proc_debug) {
1573 		error = suser_cred(td->td_ucred, PRISON_ROOT);
1574 		if (error)
1575 			return (error);
1576 	}
1577 	if (td->td_proc == p)
1578 		return (0);
1579 	if ((error = prison_check(td->td_ucred, p->p_ucred)))
1580 		return (error);
1581 	if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1582 		return (error);
1583 
1584 	/*
1585 	 * Is p's group set a subset of td's effective group set?  This
1586 	 * includes p's egid, group access list, rgid, and svgid.
1587 	 */
1588 	grpsubset = 1;
1589 	for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
1590 		if (!groupmember(p->p_ucred->cr_groups[i], td->td_ucred)) {
1591 			grpsubset = 0;
1592 			break;
1593 		}
1594 	}
1595 	grpsubset = grpsubset &&
1596 	    groupmember(p->p_ucred->cr_rgid, td->td_ucred) &&
1597 	    groupmember(p->p_ucred->cr_svgid, td->td_ucred);
1598 
1599 	/*
1600 	 * Are the uids present in p's credential equal to td's
1601 	 * effective uid?  This includes p's euid, svuid, and ruid.
1602 	 */
1603 	uidsubset = (td->td_ucred->cr_uid == p->p_ucred->cr_uid &&
1604 	    td->td_ucred->cr_uid == p->p_ucred->cr_svuid &&
1605 	    td->td_ucred->cr_uid == p->p_ucred->cr_ruid);
1606 
1607 	/*
1608 	 * Has the credential of the process changed since the last exec()?
1609 	 */
1610 	credentialchanged = (p->p_flag & P_SUGID);
1611 
1612 	/*
1613 	 * If p's gids aren't a subset, or the uids aren't a subset,
1614 	 * or the credential has changed, require appropriate privilege
1615 	 * for td to debug p.  For POSIX.1e capabilities, this will
1616 	 * require CAP_SYS_PTRACE.
1617 	 */
1618 	if (!grpsubset || !uidsubset || credentialchanged) {
1619 		error = suser_cred(td->td_ucred, PRISON_ROOT);
1620 		if (error)
1621 			return (error);
1622 	}
1623 
1624 	/* Can't trace init when securelevel > 0. */
1625 	if (p == initproc) {
1626 		error = securelevel_gt(td->td_ucred, 0);
1627 		if (error)
1628 			return (error);
1629 	}
1630 
1631 	/*
1632 	 * Can't trace a process that's currently exec'ing.
1633 	 * XXX: Note, this is not a security policy decision, it's a
1634 	 * basic correctness/functionality decision.  Therefore, this check
1635 	 * should be moved to the caller's of p_candebug().
1636 	 */
1637 	if ((p->p_flag & P_INEXEC) != 0)
1638 		return (EAGAIN);
1639 
1640 	return (0);
1641 }
1642 
1643 /*-
1644  * Determine whether the subject represented by cred can "see" a socket.
1645  * Returns: 0 for permitted, ENOENT otherwise.
1646  */
1647 int
1648 cr_canseesocket(struct ucred *cred, struct socket *so)
1649 {
1650 	int error;
1651 
1652 	error = prison_check(cred, so->so_cred);
1653 	if (error)
1654 		return (ENOENT);
1655 	if (cr_seeotheruids(cred, so->so_cred))
1656 		return (ENOENT);
1657 #ifdef MAC
1658 	/* XXX: error = mac_cred_check_seesocket() here. */
1659 #endif
1660 
1661 	return (0);
1662 }
1663 
1664 /*
1665  * Allocate a zeroed cred structure.
1666  */
1667 struct ucred *
1668 crget(void)
1669 {
1670 	register struct ucred *cr;
1671 
1672 	MALLOC(cr, struct ucred *, sizeof(*cr), M_CRED, M_WAITOK | M_ZERO);
1673 	cr->cr_ref = 1;
1674 	cr->cr_mtxp = mtx_pool_find(cr);
1675 	return (cr);
1676 }
1677 
1678 /*
1679  * Claim another reference to a ucred structure.
1680  */
1681 struct ucred *
1682 crhold(struct ucred *cr)
1683 {
1684 
1685 	mtx_lock(cr->cr_mtxp);
1686 	cr->cr_ref++;
1687 	mtx_unlock(cr->cr_mtxp);
1688 	return (cr);
1689 }
1690 
1691 /*
1692  * Free a cred structure.
1693  * Throws away space when ref count gets to 0.
1694  */
1695 void
1696 crfree(struct ucred *cr)
1697 {
1698 	struct mtx *mtxp = cr->cr_mtxp;
1699 
1700 	mtx_lock(mtxp);
1701 	KASSERT(cr->cr_ref > 0, ("bad ucred refcount: %d", cr->cr_ref));
1702 	if (--cr->cr_ref == 0) {
1703 		/*
1704 		 * Some callers of crget(), such as nfs_statfs(),
1705 		 * allocate a temporary credential, but don't
1706 		 * allocate a uidinfo structure.
1707 		 */
1708 		mtx_unlock(mtxp);
1709 		mtx_lock(&Giant);
1710 		if (cr->cr_uidinfo != NULL)
1711 			uifree(cr->cr_uidinfo);
1712 		if (cr->cr_ruidinfo != NULL)
1713 			uifree(cr->cr_ruidinfo);
1714 		/*
1715 		 * Free a prison, if any.
1716 		 */
1717 		if (jailed(cr))
1718 			prison_free(cr->cr_prison);
1719 		FREE((caddr_t)cr, M_CRED);
1720 		mtx_unlock(&Giant);
1721 	} else {
1722 		mtx_unlock(mtxp);
1723 	}
1724 }
1725 
1726 /*
1727  * Check to see if this ucred is shared.
1728  */
1729 int
1730 crshared(struct ucred *cr)
1731 {
1732 	int shared;
1733 
1734 	mtx_lock(cr->cr_mtxp);
1735 	shared = (cr->cr_ref > 1);
1736 	mtx_unlock(cr->cr_mtxp);
1737 	return (shared);
1738 }
1739 
1740 /*
1741  * Copy a ucred's contents from a template.  Does not block.
1742  */
1743 void
1744 crcopy(struct ucred *dest, struct ucred *src)
1745 {
1746 
1747 	KASSERT(crshared(dest) == 0, ("crcopy of shared ucred"));
1748 	bcopy(&src->cr_startcopy, &dest->cr_startcopy,
1749 	    (unsigned)((caddr_t)&src->cr_endcopy -
1750 		(caddr_t)&src->cr_startcopy));
1751 	uihold(dest->cr_uidinfo);
1752 	uihold(dest->cr_ruidinfo);
1753 	if (jailed(dest))
1754 		prison_hold(dest->cr_prison);
1755 }
1756 
1757 /*
1758  * Dup cred struct to a new held one.
1759  */
1760 struct ucred *
1761 crdup(struct ucred *cr)
1762 {
1763 	struct ucred *newcr;
1764 
1765 	newcr = crget();
1766 	crcopy(newcr, cr);
1767 	return (newcr);
1768 }
1769 
1770 /*
1771  * Fill in a struct xucred based on a struct ucred.
1772  */
1773 void
1774 cru2x(struct ucred *cr, struct xucred *xcr)
1775 {
1776 
1777 	bzero(xcr, sizeof(*xcr));
1778 	xcr->cr_version = XUCRED_VERSION;
1779 	xcr->cr_uid = cr->cr_uid;
1780 	xcr->cr_ngroups = cr->cr_ngroups;
1781 	bcopy(cr->cr_groups, xcr->cr_groups, sizeof(cr->cr_groups));
1782 }
1783 
1784 /*
1785  * small routine to swap a thread's current ucred for the correct one
1786  * taken from the process.
1787  */
1788 void
1789 cred_update_thread(struct thread *td)
1790 {
1791 	struct proc *p;
1792 	struct ucred *cred;
1793 
1794 	p = td->td_proc;
1795 	cred = td->td_ucred;
1796 	mtx_lock(&Giant);
1797 	PROC_LOCK(p);
1798 	td->td_ucred = crhold(p->p_ucred);
1799 	PROC_UNLOCK(p);
1800 	if (cred != NULL)
1801 		crfree(cred);
1802 	mtx_unlock(&Giant);
1803 }
1804 
1805 /*
1806  * Get login name, if available.
1807  */
1808 #ifndef _SYS_SYSPROTO_H_
1809 struct getlogin_args {
1810 	char	*namebuf;
1811 	u_int	namelen;
1812 };
1813 #endif
1814 /*
1815  * MPSAFE
1816  */
1817 /* ARGSUSED */
1818 int
1819 getlogin(struct thread *td, struct getlogin_args *uap)
1820 {
1821 	int error;
1822 	char login[MAXLOGNAME];
1823 	struct proc *p = td->td_proc;
1824 
1825 	if (uap->namelen > MAXLOGNAME)
1826 		uap->namelen = MAXLOGNAME;
1827 	PROC_LOCK(p);
1828 	SESS_LOCK(p->p_session);
1829 	bcopy(p->p_session->s_login, login, uap->namelen);
1830 	SESS_UNLOCK(p->p_session);
1831 	PROC_UNLOCK(p);
1832 	error = copyout((caddr_t) login, (caddr_t) uap->namebuf, uap->namelen);
1833 	return(error);
1834 }
1835 
1836 /*
1837  * Set login name.
1838  */
1839 #ifndef _SYS_SYSPROTO_H_
1840 struct setlogin_args {
1841 	char	*namebuf;
1842 };
1843 #endif
1844 /*
1845  * MPSAFE
1846  */
1847 /* ARGSUSED */
1848 int
1849 setlogin(struct thread *td, struct setlogin_args *uap)
1850 {
1851 	struct proc *p = td->td_proc;
1852 	int error;
1853 	char logintmp[MAXLOGNAME];
1854 
1855 	error = suser_cred(td->td_ucred, PRISON_ROOT);
1856 	if (error)
1857 		return (error);
1858 	error = copyinstr((caddr_t) uap->namebuf, (caddr_t) logintmp,
1859 	    sizeof(logintmp), (size_t *)0);
1860 	if (error == ENAMETOOLONG)
1861 		error = EINVAL;
1862 	else if (!error) {
1863 		PROC_LOCK(p);
1864 		SESS_LOCK(p->p_session);
1865 		(void) memcpy(p->p_session->s_login, logintmp,
1866 		    sizeof(logintmp));
1867 		SESS_UNLOCK(p->p_session);
1868 		PROC_UNLOCK(p);
1869 	}
1870 	return (error);
1871 }
1872 
1873 void
1874 setsugid(struct proc *p)
1875 {
1876 
1877 	PROC_LOCK_ASSERT(p, MA_OWNED);
1878 	p->p_flag |= P_SUGID;
1879 	if (!(p->p_pfsflags & PF_ISUGID))
1880 		p->p_stops = 0;
1881 }
1882 
1883 /*-
1884  * Change a process's effective uid.
1885  * Side effects: newcred->cr_uid and newcred->cr_uidinfo will be modified.
1886  * References: newcred must be an exclusive credential reference for the
1887  *             duration of the call.
1888  */
1889 void
1890 change_euid(struct ucred *newcred, struct uidinfo *euip)
1891 {
1892 
1893 	newcred->cr_uid = euip->ui_uid;
1894 	uihold(euip);
1895 	uifree(newcred->cr_uidinfo);
1896 	newcred->cr_uidinfo = euip;
1897 }
1898 
1899 /*-
1900  * Change a process's effective gid.
1901  * Side effects: newcred->cr_gid will be modified.
1902  * References: newcred must be an exclusive credential reference for the
1903  *             duration of the call.
1904  */
1905 void
1906 change_egid(struct ucred *newcred, gid_t egid)
1907 {
1908 
1909 	newcred->cr_groups[0] = egid;
1910 }
1911 
1912 /*-
1913  * Change a process's real uid.
1914  * Side effects: newcred->cr_ruid will be updated, newcred->cr_ruidinfo
1915  *               will be updated, and the old and new cr_ruidinfo proc
1916  *               counts will be updated.
1917  * References: newcred must be an exclusive credential reference for the
1918  *             duration of the call.
1919  */
1920 void
1921 change_ruid(struct ucred *newcred, struct uidinfo *ruip)
1922 {
1923 
1924 	(void)chgproccnt(newcred->cr_ruidinfo, -1, 0);
1925 	newcred->cr_ruid = ruip->ui_uid;
1926 	uihold(ruip);
1927 	uifree(newcred->cr_ruidinfo);
1928 	newcred->cr_ruidinfo = ruip;
1929 	(void)chgproccnt(newcred->cr_ruidinfo, 1, 0);
1930 }
1931 
1932 /*-
1933  * Change a process's real gid.
1934  * Side effects: newcred->cr_rgid will be updated.
1935  * References: newcred must be an exclusive credential reference for the
1936  *             duration of the call.
1937  */
1938 void
1939 change_rgid(struct ucred *newcred, gid_t rgid)
1940 {
1941 
1942 	newcred->cr_rgid = rgid;
1943 }
1944 
1945 /*-
1946  * Change a process's saved uid.
1947  * Side effects: newcred->cr_svuid will be updated.
1948  * References: newcred must be an exclusive credential reference for the
1949  *             duration of the call.
1950  */
1951 void
1952 change_svuid(struct ucred *newcred, uid_t svuid)
1953 {
1954 
1955 	newcred->cr_svuid = svuid;
1956 }
1957 
1958 /*-
1959  * Change a process's saved gid.
1960  * Side effects: newcred->cr_svgid will be updated.
1961  * References: newcred must be an exclusive credential reference for the
1962  *             duration of the call.
1963  */
1964 void
1965 change_svgid(struct ucred *newcred, gid_t svgid)
1966 {
1967 
1968 	newcred->cr_svgid = svgid;
1969 }
1970