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