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