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