Ignore machine-check MSRs
[freebsd-src/fkvm-freebsd.git] / sys / kern / kern_prot.c
blobcd0fb170a20fd68f37e9c2d326f0ece4daa04e60
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.
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.
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.
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.
37 * @(#)kern_prot.c 8.6 (Berkeley) 1/21/94
41 * System calls related to processes and protection
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include "opt_compat.h"
48 #include "opt_mac.h"
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/acct.h>
53 #include <sys/kdb.h>
54 #include <sys/kernel.h>
55 #include <sys/lock.h>
56 #include <sys/malloc.h>
57 #include <sys/mutex.h>
58 #include <sys/refcount.h>
59 #include <sys/sx.h>
60 #include <sys/priv.h>
61 #include <sys/proc.h>
62 #include <sys/sysproto.h>
63 #include <sys/jail.h>
64 #include <sys/pioctl.h>
65 #include <sys/resourcevar.h>
66 #include <sys/socket.h>
67 #include <sys/socketvar.h>
68 #include <sys/syscallsubr.h>
69 #include <sys/sysctl.h>
71 #include <security/audit/audit.h>
72 #include <security/mac/mac_framework.h>
74 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
76 SYSCTL_NODE(_security, OID_AUTO, bsd, CTLFLAG_RW, 0, "BSD security policy");
78 #ifndef _SYS_SYSPROTO_H_
79 struct getpid_args {
80 int dummy;
82 #endif
83 /* ARGSUSED */
84 int
85 getpid(struct thread *td, struct getpid_args *uap)
87 struct proc *p = td->td_proc;
89 td->td_retval[0] = p->p_pid;
90 #if defined(COMPAT_43)
91 PROC_LOCK(p);
92 td->td_retval[1] = p->p_pptr->p_pid;
93 PROC_UNLOCK(p);
94 #endif
95 return (0);
98 #ifndef _SYS_SYSPROTO_H_
99 struct getppid_args {
100 int dummy;
102 #endif
103 /* ARGSUSED */
105 getppid(struct thread *td, struct getppid_args *uap)
107 struct proc *p = td->td_proc;
109 PROC_LOCK(p);
110 td->td_retval[0] = p->p_pptr->p_pid;
111 PROC_UNLOCK(p);
112 return (0);
116 * Get process group ID; note that POSIX getpgrp takes no parameter.
118 #ifndef _SYS_SYSPROTO_H_
119 struct getpgrp_args {
120 int dummy;
122 #endif
124 getpgrp(struct thread *td, struct getpgrp_args *uap)
126 struct proc *p = td->td_proc;
128 PROC_LOCK(p);
129 td->td_retval[0] = p->p_pgrp->pg_id;
130 PROC_UNLOCK(p);
131 return (0);
134 /* Get an arbitary pid's process group id */
135 #ifndef _SYS_SYSPROTO_H_
136 struct getpgid_args {
137 pid_t pid;
139 #endif
141 getpgid(struct thread *td, struct getpgid_args *uap)
143 struct proc *p;
144 int error;
146 if (uap->pid == 0) {
147 p = td->td_proc;
148 PROC_LOCK(p);
149 } else {
150 p = pfind(uap->pid);
151 if (p == NULL)
152 return (ESRCH);
153 error = p_cansee(td, p);
154 if (error) {
155 PROC_UNLOCK(p);
156 return (error);
159 td->td_retval[0] = p->p_pgrp->pg_id;
160 PROC_UNLOCK(p);
161 return (0);
165 * Get an arbitary pid's session id.
167 #ifndef _SYS_SYSPROTO_H_
168 struct getsid_args {
169 pid_t pid;
171 #endif
173 getsid(struct thread *td, struct getsid_args *uap)
175 struct proc *p;
176 int error;
178 if (uap->pid == 0) {
179 p = td->td_proc;
180 PROC_LOCK(p);
181 } else {
182 p = pfind(uap->pid);
183 if (p == NULL)
184 return (ESRCH);
185 error = p_cansee(td, p);
186 if (error) {
187 PROC_UNLOCK(p);
188 return (error);
191 td->td_retval[0] = p->p_session->s_sid;
192 PROC_UNLOCK(p);
193 return (0);
196 #ifndef _SYS_SYSPROTO_H_
197 struct getuid_args {
198 int dummy;
200 #endif
201 /* ARGSUSED */
203 getuid(struct thread *td, struct getuid_args *uap)
206 td->td_retval[0] = td->td_ucred->cr_ruid;
207 #if defined(COMPAT_43)
208 td->td_retval[1] = td->td_ucred->cr_uid;
209 #endif
210 return (0);
213 #ifndef _SYS_SYSPROTO_H_
214 struct geteuid_args {
215 int dummy;
217 #endif
218 /* ARGSUSED */
220 geteuid(struct thread *td, struct geteuid_args *uap)
223 td->td_retval[0] = td->td_ucred->cr_uid;
224 return (0);
227 #ifndef _SYS_SYSPROTO_H_
228 struct getgid_args {
229 int dummy;
231 #endif
232 /* ARGSUSED */
234 getgid(struct thread *td, struct getgid_args *uap)
237 td->td_retval[0] = td->td_ucred->cr_rgid;
238 #if defined(COMPAT_43)
239 td->td_retval[1] = td->td_ucred->cr_groups[0];
240 #endif
241 return (0);
245 * Get effective group ID. The "egid" is groups[0], and could be obtained
246 * via getgroups. This syscall exists because it is somewhat painful to do
247 * correctly in a library function.
249 #ifndef _SYS_SYSPROTO_H_
250 struct getegid_args {
251 int dummy;
253 #endif
254 /* ARGSUSED */
256 getegid(struct thread *td, struct getegid_args *uap)
259 td->td_retval[0] = td->td_ucred->cr_groups[0];
260 return (0);
263 #ifndef _SYS_SYSPROTO_H_
264 struct getgroups_args {
265 u_int gidsetsize;
266 gid_t *gidset;
268 #endif
270 getgroups(struct thread *td, register struct getgroups_args *uap)
272 gid_t groups[NGROUPS];
273 u_int ngrp;
274 int error;
276 ngrp = MIN(uap->gidsetsize, NGROUPS);
277 error = kern_getgroups(td, &ngrp, groups);
278 if (error)
279 return (error);
280 if (uap->gidsetsize > 0)
281 error = copyout(groups, uap->gidset, ngrp * sizeof(gid_t));
282 if (error == 0)
283 td->td_retval[0] = ngrp;
284 return (error);
288 kern_getgroups(struct thread *td, u_int *ngrp, gid_t *groups)
290 struct ucred *cred;
292 cred = td->td_ucred;
293 if (*ngrp == 0) {
294 *ngrp = cred->cr_ngroups;
295 return (0);
297 if (*ngrp < cred->cr_ngroups)
298 return (EINVAL);
299 *ngrp = cred->cr_ngroups;
300 bcopy(cred->cr_groups, groups, *ngrp * sizeof(gid_t));
301 return (0);
304 #ifndef _SYS_SYSPROTO_H_
305 struct setsid_args {
306 int dummy;
308 #endif
309 /* ARGSUSED */
311 setsid(register struct thread *td, struct setsid_args *uap)
313 struct pgrp *pgrp;
314 int error;
315 struct proc *p = td->td_proc;
316 struct pgrp *newpgrp;
317 struct session *newsess;
319 error = 0;
320 pgrp = NULL;
322 MALLOC(newpgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
323 MALLOC(newsess, struct session *, sizeof(struct session), M_SESSION, M_WAITOK | M_ZERO);
325 sx_xlock(&proctree_lock);
327 if (p->p_pgid == p->p_pid || (pgrp = pgfind(p->p_pid)) != NULL) {
328 if (pgrp != NULL)
329 PGRP_UNLOCK(pgrp);
330 error = EPERM;
331 } else {
332 (void)enterpgrp(p, p->p_pid, newpgrp, newsess);
333 td->td_retval[0] = p->p_pid;
334 newpgrp = NULL;
335 newsess = NULL;
338 sx_xunlock(&proctree_lock);
340 if (newpgrp != NULL)
341 FREE(newpgrp, M_PGRP);
342 if (newsess != NULL)
343 FREE(newsess, M_SESSION);
345 return (error);
349 * set process group (setpgid/old setpgrp)
351 * caller does setpgid(targpid, targpgid)
353 * pid must be caller or child of caller (ESRCH)
354 * if a child
355 * pid must be in same session (EPERM)
356 * pid can't have done an exec (EACCES)
357 * if pgid != pid
358 * there must exist some pid in same session having pgid (EPERM)
359 * pid must not be session leader (EPERM)
361 #ifndef _SYS_SYSPROTO_H_
362 struct setpgid_args {
363 int pid; /* target process id */
364 int pgid; /* target pgrp id */
366 #endif
367 /* ARGSUSED */
369 setpgid(struct thread *td, register struct setpgid_args *uap)
371 struct proc *curp = td->td_proc;
372 register struct proc *targp; /* target process */
373 register struct pgrp *pgrp; /* target pgrp */
374 int error;
375 struct pgrp *newpgrp;
377 if (uap->pgid < 0)
378 return (EINVAL);
380 error = 0;
382 MALLOC(newpgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
384 sx_xlock(&proctree_lock);
385 if (uap->pid != 0 && uap->pid != curp->p_pid) {
386 if ((targp = pfind(uap->pid)) == NULL) {
387 error = ESRCH;
388 goto done;
390 if (!inferior(targp)) {
391 PROC_UNLOCK(targp);
392 error = ESRCH;
393 goto done;
395 if ((error = p_cansee(td, targp))) {
396 PROC_UNLOCK(targp);
397 goto done;
399 if (targp->p_pgrp == NULL ||
400 targp->p_session != curp->p_session) {
401 PROC_UNLOCK(targp);
402 error = EPERM;
403 goto done;
405 if (targp->p_flag & P_EXEC) {
406 PROC_UNLOCK(targp);
407 error = EACCES;
408 goto done;
410 PROC_UNLOCK(targp);
411 } else
412 targp = curp;
413 if (SESS_LEADER(targp)) {
414 error = EPERM;
415 goto done;
417 if (uap->pgid == 0)
418 uap->pgid = targp->p_pid;
419 if ((pgrp = pgfind(uap->pgid)) == NULL) {
420 if (uap->pgid == targp->p_pid) {
421 error = enterpgrp(targp, uap->pgid, newpgrp,
422 NULL);
423 if (error == 0)
424 newpgrp = NULL;
425 } else
426 error = EPERM;
427 } else {
428 if (pgrp == targp->p_pgrp) {
429 PGRP_UNLOCK(pgrp);
430 goto done;
432 if (pgrp->pg_id != targp->p_pid &&
433 pgrp->pg_session != curp->p_session) {
434 PGRP_UNLOCK(pgrp);
435 error = EPERM;
436 goto done;
438 PGRP_UNLOCK(pgrp);
439 error = enterthispgrp(targp, pgrp);
441 done:
442 sx_xunlock(&proctree_lock);
443 KASSERT((error == 0) || (newpgrp != NULL),
444 ("setpgid failed and newpgrp is NULL"));
445 if (newpgrp != NULL)
446 FREE(newpgrp, M_PGRP);
447 return (error);
451 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
452 * compatible. It says that setting the uid/gid to euid/egid is a special
453 * case of "appropriate privilege". Once the rules are expanded out, this
454 * basically means that setuid(nnn) sets all three id's, in all permitted
455 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
456 * does not set the saved id - this is dangerous for traditional BSD
457 * programs. For this reason, we *really* do not want to set
458 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
460 #define POSIX_APPENDIX_B_4_2_2
462 #ifndef _SYS_SYSPROTO_H_
463 struct setuid_args {
464 uid_t uid;
466 #endif
467 /* ARGSUSED */
469 setuid(struct thread *td, struct setuid_args *uap)
471 struct proc *p = td->td_proc;
472 struct ucred *newcred, *oldcred;
473 uid_t uid;
474 struct uidinfo *uip;
475 int error;
477 uid = uap->uid;
478 AUDIT_ARG(uid, uid);
479 newcred = crget();
480 uip = uifind(uid);
481 PROC_LOCK(p);
482 oldcred = p->p_ucred;
484 #ifdef MAC
485 error = mac_proc_check_setuid(p, oldcred, uid);
486 if (error)
487 goto fail;
488 #endif
491 * See if we have "permission" by POSIX 1003.1 rules.
493 * Note that setuid(geteuid()) is a special case of
494 * "appropriate privileges" in appendix B.4.2.2. We need
495 * to use this clause to be compatible with traditional BSD
496 * semantics. Basically, it means that "setuid(xx)" sets all
497 * three id's (assuming you have privs).
499 * Notes on the logic. We do things in three steps.
500 * 1: We determine if the euid is going to change, and do EPERM
501 * right away. We unconditionally change the euid later if this
502 * test is satisfied, simplifying that part of the logic.
503 * 2: We determine if the real and/or saved uids are going to
504 * change. Determined by compile options.
505 * 3: Change euid last. (after tests in #2 for "appropriate privs")
507 if (uid != oldcred->cr_ruid && /* allow setuid(getuid()) */
508 #ifdef _POSIX_SAVED_IDS
509 uid != oldcred->cr_svuid && /* allow setuid(saved gid) */
510 #endif
511 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
512 uid != oldcred->cr_uid && /* allow setuid(geteuid()) */
513 #endif
514 (error = priv_check_cred(oldcred, PRIV_CRED_SETUID, 0)) != 0)
515 goto fail;
518 * Copy credentials so other references do not see our changes.
520 crcopy(newcred, oldcred);
521 #ifdef _POSIX_SAVED_IDS
523 * Do we have "appropriate privileges" (are we root or uid == euid)
524 * If so, we are changing the real uid and/or saved uid.
526 if (
527 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
528 uid == oldcred->cr_uid ||
529 #endif
530 /* We are using privs. */
531 priv_check_cred(oldcred, PRIV_CRED_SETUID, 0) == 0)
532 #endif
535 * Set the real uid and transfer proc count to new user.
537 if (uid != oldcred->cr_ruid) {
538 change_ruid(newcred, uip);
539 setsugid(p);
542 * Set saved uid
544 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
545 * the security of seteuid() depends on it. B.4.2.2 says it
546 * is important that we should do this.
548 if (uid != oldcred->cr_svuid) {
549 change_svuid(newcred, uid);
550 setsugid(p);
555 * In all permitted cases, we are changing the euid.
557 if (uid != oldcred->cr_uid) {
558 change_euid(newcred, uip);
559 setsugid(p);
561 p->p_ucred = newcred;
562 PROC_UNLOCK(p);
563 uifree(uip);
564 crfree(oldcred);
565 return (0);
567 fail:
568 PROC_UNLOCK(p);
569 uifree(uip);
570 crfree(newcred);
571 return (error);
574 #ifndef _SYS_SYSPROTO_H_
575 struct seteuid_args {
576 uid_t euid;
578 #endif
579 /* ARGSUSED */
581 seteuid(struct thread *td, struct seteuid_args *uap)
583 struct proc *p = td->td_proc;
584 struct ucred *newcred, *oldcred;
585 uid_t euid;
586 struct uidinfo *euip;
587 int error;
589 euid = uap->euid;
590 AUDIT_ARG(euid, euid);
591 newcred = crget();
592 euip = uifind(euid);
593 PROC_LOCK(p);
594 oldcred = p->p_ucred;
596 #ifdef MAC
597 error = mac_proc_check_seteuid(p, oldcred, euid);
598 if (error)
599 goto fail;
600 #endif
602 if (euid != oldcred->cr_ruid && /* allow seteuid(getuid()) */
603 euid != oldcred->cr_svuid && /* allow seteuid(saved uid) */
604 (error = priv_check_cred(oldcred, PRIV_CRED_SETEUID, 0)) != 0)
605 goto fail;
608 * Everything's okay, do it. Copy credentials so other references do
609 * not see our changes.
611 crcopy(newcred, oldcred);
612 if (oldcred->cr_uid != euid) {
613 change_euid(newcred, euip);
614 setsugid(p);
616 p->p_ucred = newcred;
617 PROC_UNLOCK(p);
618 uifree(euip);
619 crfree(oldcred);
620 return (0);
622 fail:
623 PROC_UNLOCK(p);
624 uifree(euip);
625 crfree(newcred);
626 return (error);
629 #ifndef _SYS_SYSPROTO_H_
630 struct setgid_args {
631 gid_t gid;
633 #endif
634 /* ARGSUSED */
636 setgid(struct thread *td, struct setgid_args *uap)
638 struct proc *p = td->td_proc;
639 struct ucred *newcred, *oldcred;
640 gid_t gid;
641 int error;
643 gid = uap->gid;
644 AUDIT_ARG(gid, gid);
645 newcred = crget();
646 PROC_LOCK(p);
647 oldcred = p->p_ucred;
649 #ifdef MAC
650 error = mac_proc_check_setgid(p, oldcred, gid);
651 if (error)
652 goto fail;
653 #endif
656 * See if we have "permission" by POSIX 1003.1 rules.
658 * Note that setgid(getegid()) is a special case of
659 * "appropriate privileges" in appendix B.4.2.2. We need
660 * to use this clause to be compatible with traditional BSD
661 * semantics. Basically, it means that "setgid(xx)" sets all
662 * three id's (assuming you have privs).
664 * For notes on the logic here, see setuid() above.
666 if (gid != oldcred->cr_rgid && /* allow setgid(getgid()) */
667 #ifdef _POSIX_SAVED_IDS
668 gid != oldcred->cr_svgid && /* allow setgid(saved gid) */
669 #endif
670 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
671 gid != oldcred->cr_groups[0] && /* allow setgid(getegid()) */
672 #endif
673 (error = priv_check_cred(oldcred, PRIV_CRED_SETGID, 0)) != 0)
674 goto fail;
676 crcopy(newcred, oldcred);
677 #ifdef _POSIX_SAVED_IDS
679 * Do we have "appropriate privileges" (are we root or gid == egid)
680 * If so, we are changing the real uid and saved gid.
682 if (
683 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
684 gid == oldcred->cr_groups[0] ||
685 #endif
686 /* We are using privs. */
687 priv_check_cred(oldcred, PRIV_CRED_SETGID, 0) == 0)
688 #endif
691 * Set real gid
693 if (oldcred->cr_rgid != gid) {
694 change_rgid(newcred, gid);
695 setsugid(p);
698 * Set saved gid
700 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
701 * the security of setegid() depends on it. B.4.2.2 says it
702 * is important that we should do this.
704 if (oldcred->cr_svgid != gid) {
705 change_svgid(newcred, gid);
706 setsugid(p);
710 * In all cases permitted cases, we are changing the egid.
711 * Copy credentials so other references do not see our changes.
713 if (oldcred->cr_groups[0] != gid) {
714 change_egid(newcred, gid);
715 setsugid(p);
717 p->p_ucred = newcred;
718 PROC_UNLOCK(p);
719 crfree(oldcred);
720 return (0);
722 fail:
723 PROC_UNLOCK(p);
724 crfree(newcred);
725 return (error);
728 #ifndef _SYS_SYSPROTO_H_
729 struct setegid_args {
730 gid_t egid;
732 #endif
733 /* ARGSUSED */
735 setegid(struct thread *td, struct setegid_args *uap)
737 struct proc *p = td->td_proc;
738 struct ucred *newcred, *oldcred;
739 gid_t egid;
740 int error;
742 egid = uap->egid;
743 AUDIT_ARG(egid, egid);
744 newcred = crget();
745 PROC_LOCK(p);
746 oldcred = p->p_ucred;
748 #ifdef MAC
749 error = mac_proc_check_setegid(p, oldcred, egid);
750 if (error)
751 goto fail;
752 #endif
754 if (egid != oldcred->cr_rgid && /* allow setegid(getgid()) */
755 egid != oldcred->cr_svgid && /* allow setegid(saved gid) */
756 (error = priv_check_cred(oldcred, PRIV_CRED_SETEGID, 0)) != 0)
757 goto fail;
759 crcopy(newcred, oldcred);
760 if (oldcred->cr_groups[0] != egid) {
761 change_egid(newcred, egid);
762 setsugid(p);
764 p->p_ucred = newcred;
765 PROC_UNLOCK(p);
766 crfree(oldcred);
767 return (0);
769 fail:
770 PROC_UNLOCK(p);
771 crfree(newcred);
772 return (error);
775 #ifndef _SYS_SYSPROTO_H_
776 struct setgroups_args {
777 u_int gidsetsize;
778 gid_t *gidset;
780 #endif
781 /* ARGSUSED */
783 setgroups(struct thread *td, struct setgroups_args *uap)
785 gid_t groups[NGROUPS];
786 int error;
788 if (uap->gidsetsize > NGROUPS)
789 return (EINVAL);
790 error = copyin(uap->gidset, groups, uap->gidsetsize * sizeof(gid_t));
791 if (error)
792 return (error);
793 return (kern_setgroups(td, uap->gidsetsize, groups));
797 kern_setgroups(struct thread *td, u_int ngrp, gid_t *groups)
799 struct proc *p = td->td_proc;
800 struct ucred *newcred, *oldcred;
801 int error;
803 if (ngrp > NGROUPS)
804 return (EINVAL);
805 AUDIT_ARG(groupset, groups, ngrp);
806 newcred = crget();
807 PROC_LOCK(p);
808 oldcred = p->p_ucred;
810 #ifdef MAC
811 error = mac_proc_check_setgroups(p, oldcred, ngrp, groups);
812 if (error)
813 goto fail;
814 #endif
816 error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0);
817 if (error)
818 goto fail;
821 * XXX A little bit lazy here. We could test if anything has
822 * changed before crcopy() and setting P_SUGID.
824 crcopy(newcred, oldcred);
825 if (ngrp < 1) {
827 * setgroups(0, NULL) is a legitimate way of clearing the
828 * groups vector on non-BSD systems (which generally do not
829 * have the egid in the groups[0]). We risk security holes
830 * when running non-BSD software if we do not do the same.
832 newcred->cr_ngroups = 1;
833 } else {
834 bcopy(groups, newcred->cr_groups, ngrp * sizeof(gid_t));
835 newcred->cr_ngroups = ngrp;
837 setsugid(p);
838 p->p_ucred = newcred;
839 PROC_UNLOCK(p);
840 crfree(oldcred);
841 return (0);
843 fail:
844 PROC_UNLOCK(p);
845 crfree(newcred);
846 return (error);
849 #ifndef _SYS_SYSPROTO_H_
850 struct setreuid_args {
851 uid_t ruid;
852 uid_t euid;
854 #endif
855 /* ARGSUSED */
857 setreuid(register struct thread *td, struct setreuid_args *uap)
859 struct proc *p = td->td_proc;
860 struct ucred *newcred, *oldcred;
861 uid_t euid, ruid;
862 struct uidinfo *euip, *ruip;
863 int error;
865 euid = uap->euid;
866 ruid = uap->ruid;
867 AUDIT_ARG(euid, euid);
868 AUDIT_ARG(ruid, ruid);
869 newcred = crget();
870 euip = uifind(euid);
871 ruip = uifind(ruid);
872 PROC_LOCK(p);
873 oldcred = p->p_ucred;
875 #ifdef MAC
876 error = mac_proc_check_setreuid(p, oldcred, ruid, euid);
877 if (error)
878 goto fail;
879 #endif
881 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
882 ruid != oldcred->cr_svuid) ||
883 (euid != (uid_t)-1 && euid != oldcred->cr_uid &&
884 euid != oldcred->cr_ruid && euid != oldcred->cr_svuid)) &&
885 (error = priv_check_cred(oldcred, PRIV_CRED_SETREUID, 0)) != 0)
886 goto fail;
888 crcopy(newcred, oldcred);
889 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
890 change_euid(newcred, euip);
891 setsugid(p);
893 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
894 change_ruid(newcred, ruip);
895 setsugid(p);
897 if ((ruid != (uid_t)-1 || newcred->cr_uid != newcred->cr_ruid) &&
898 newcred->cr_svuid != newcred->cr_uid) {
899 change_svuid(newcred, newcred->cr_uid);
900 setsugid(p);
902 p->p_ucred = newcred;
903 PROC_UNLOCK(p);
904 uifree(ruip);
905 uifree(euip);
906 crfree(oldcred);
907 return (0);
909 fail:
910 PROC_UNLOCK(p);
911 uifree(ruip);
912 uifree(euip);
913 crfree(newcred);
914 return (error);
917 #ifndef _SYS_SYSPROTO_H_
918 struct setregid_args {
919 gid_t rgid;
920 gid_t egid;
922 #endif
923 /* ARGSUSED */
925 setregid(register struct thread *td, struct setregid_args *uap)
927 struct proc *p = td->td_proc;
928 struct ucred *newcred, *oldcred;
929 gid_t egid, rgid;
930 int error;
932 egid = uap->egid;
933 rgid = uap->rgid;
934 AUDIT_ARG(egid, egid);
935 AUDIT_ARG(rgid, rgid);
936 newcred = crget();
937 PROC_LOCK(p);
938 oldcred = p->p_ucred;
940 #ifdef MAC
941 error = mac_proc_check_setregid(p, oldcred, rgid, egid);
942 if (error)
943 goto fail;
944 #endif
946 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
947 rgid != oldcred->cr_svgid) ||
948 (egid != (gid_t)-1 && egid != oldcred->cr_groups[0] &&
949 egid != oldcred->cr_rgid && egid != oldcred->cr_svgid)) &&
950 (error = priv_check_cred(oldcred, PRIV_CRED_SETREGID, 0)) != 0)
951 goto fail;
953 crcopy(newcred, oldcred);
954 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
955 change_egid(newcred, egid);
956 setsugid(p);
958 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
959 change_rgid(newcred, rgid);
960 setsugid(p);
962 if ((rgid != (gid_t)-1 || newcred->cr_groups[0] != newcred->cr_rgid) &&
963 newcred->cr_svgid != newcred->cr_groups[0]) {
964 change_svgid(newcred, newcred->cr_groups[0]);
965 setsugid(p);
967 p->p_ucred = newcred;
968 PROC_UNLOCK(p);
969 crfree(oldcred);
970 return (0);
972 fail:
973 PROC_UNLOCK(p);
974 crfree(newcred);
975 return (error);
979 * setresuid(ruid, euid, suid) is like setreuid except control over the saved
980 * uid is explicit.
982 #ifndef _SYS_SYSPROTO_H_
983 struct setresuid_args {
984 uid_t ruid;
985 uid_t euid;
986 uid_t suid;
988 #endif
989 /* ARGSUSED */
991 setresuid(register struct thread *td, struct setresuid_args *uap)
993 struct proc *p = td->td_proc;
994 struct ucred *newcred, *oldcred;
995 uid_t euid, ruid, suid;
996 struct uidinfo *euip, *ruip;
997 int error;
999 euid = uap->euid;
1000 ruid = uap->ruid;
1001 suid = uap->suid;
1002 AUDIT_ARG(euid, euid);
1003 AUDIT_ARG(ruid, ruid);
1004 AUDIT_ARG(suid, suid);
1005 newcred = crget();
1006 euip = uifind(euid);
1007 ruip = uifind(ruid);
1008 PROC_LOCK(p);
1009 oldcred = p->p_ucred;
1011 #ifdef MAC
1012 error = mac_proc_check_setresuid(p, oldcred, ruid, euid, suid);
1013 if (error)
1014 goto fail;
1015 #endif
1017 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
1018 ruid != oldcred->cr_svuid &&
1019 ruid != oldcred->cr_uid) ||
1020 (euid != (uid_t)-1 && euid != oldcred->cr_ruid &&
1021 euid != oldcred->cr_svuid &&
1022 euid != oldcred->cr_uid) ||
1023 (suid != (uid_t)-1 && suid != oldcred->cr_ruid &&
1024 suid != oldcred->cr_svuid &&
1025 suid != oldcred->cr_uid)) &&
1026 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESUID, 0)) != 0)
1027 goto fail;
1029 crcopy(newcred, oldcred);
1030 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
1031 change_euid(newcred, euip);
1032 setsugid(p);
1034 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
1035 change_ruid(newcred, ruip);
1036 setsugid(p);
1038 if (suid != (uid_t)-1 && oldcred->cr_svuid != suid) {
1039 change_svuid(newcred, suid);
1040 setsugid(p);
1042 p->p_ucred = newcred;
1043 PROC_UNLOCK(p);
1044 uifree(ruip);
1045 uifree(euip);
1046 crfree(oldcred);
1047 return (0);
1049 fail:
1050 PROC_UNLOCK(p);
1051 uifree(ruip);
1052 uifree(euip);
1053 crfree(newcred);
1054 return (error);
1059 * setresgid(rgid, egid, sgid) is like setregid except control over the saved
1060 * gid is explicit.
1062 #ifndef _SYS_SYSPROTO_H_
1063 struct setresgid_args {
1064 gid_t rgid;
1065 gid_t egid;
1066 gid_t sgid;
1068 #endif
1069 /* ARGSUSED */
1071 setresgid(register struct thread *td, struct setresgid_args *uap)
1073 struct proc *p = td->td_proc;
1074 struct ucred *newcred, *oldcred;
1075 gid_t egid, rgid, sgid;
1076 int error;
1078 egid = uap->egid;
1079 rgid = uap->rgid;
1080 sgid = uap->sgid;
1081 AUDIT_ARG(egid, egid);
1082 AUDIT_ARG(rgid, rgid);
1083 AUDIT_ARG(sgid, sgid);
1084 newcred = crget();
1085 PROC_LOCK(p);
1086 oldcred = p->p_ucred;
1088 #ifdef MAC
1089 error = mac_proc_check_setresgid(p, oldcred, rgid, egid, sgid);
1090 if (error)
1091 goto fail;
1092 #endif
1094 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
1095 rgid != oldcred->cr_svgid &&
1096 rgid != oldcred->cr_groups[0]) ||
1097 (egid != (gid_t)-1 && egid != oldcred->cr_rgid &&
1098 egid != oldcred->cr_svgid &&
1099 egid != oldcred->cr_groups[0]) ||
1100 (sgid != (gid_t)-1 && sgid != oldcred->cr_rgid &&
1101 sgid != oldcred->cr_svgid &&
1102 sgid != oldcred->cr_groups[0])) &&
1103 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESGID, 0)) != 0)
1104 goto fail;
1106 crcopy(newcred, oldcred);
1107 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
1108 change_egid(newcred, egid);
1109 setsugid(p);
1111 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
1112 change_rgid(newcred, rgid);
1113 setsugid(p);
1115 if (sgid != (gid_t)-1 && oldcred->cr_svgid != sgid) {
1116 change_svgid(newcred, sgid);
1117 setsugid(p);
1119 p->p_ucred = newcred;
1120 PROC_UNLOCK(p);
1121 crfree(oldcred);
1122 return (0);
1124 fail:
1125 PROC_UNLOCK(p);
1126 crfree(newcred);
1127 return (error);
1130 #ifndef _SYS_SYSPROTO_H_
1131 struct getresuid_args {
1132 uid_t *ruid;
1133 uid_t *euid;
1134 uid_t *suid;
1136 #endif
1137 /* ARGSUSED */
1139 getresuid(register struct thread *td, struct getresuid_args *uap)
1141 struct ucred *cred;
1142 int error1 = 0, error2 = 0, error3 = 0;
1144 cred = td->td_ucred;
1145 if (uap->ruid)
1146 error1 = copyout(&cred->cr_ruid,
1147 uap->ruid, sizeof(cred->cr_ruid));
1148 if (uap->euid)
1149 error2 = copyout(&cred->cr_uid,
1150 uap->euid, sizeof(cred->cr_uid));
1151 if (uap->suid)
1152 error3 = copyout(&cred->cr_svuid,
1153 uap->suid, sizeof(cred->cr_svuid));
1154 return (error1 ? error1 : error2 ? error2 : error3);
1157 #ifndef _SYS_SYSPROTO_H_
1158 struct getresgid_args {
1159 gid_t *rgid;
1160 gid_t *egid;
1161 gid_t *sgid;
1163 #endif
1164 /* ARGSUSED */
1166 getresgid(register struct thread *td, struct getresgid_args *uap)
1168 struct ucred *cred;
1169 int error1 = 0, error2 = 0, error3 = 0;
1171 cred = td->td_ucred;
1172 if (uap->rgid)
1173 error1 = copyout(&cred->cr_rgid,
1174 uap->rgid, sizeof(cred->cr_rgid));
1175 if (uap->egid)
1176 error2 = copyout(&cred->cr_groups[0],
1177 uap->egid, sizeof(cred->cr_groups[0]));
1178 if (uap->sgid)
1179 error3 = copyout(&cred->cr_svgid,
1180 uap->sgid, sizeof(cred->cr_svgid));
1181 return (error1 ? error1 : error2 ? error2 : error3);
1184 #ifndef _SYS_SYSPROTO_H_
1185 struct issetugid_args {
1186 int dummy;
1188 #endif
1189 /* ARGSUSED */
1191 issetugid(register struct thread *td, struct issetugid_args *uap)
1193 struct proc *p = td->td_proc;
1196 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
1197 * we use P_SUGID because we consider changing the owners as
1198 * "tainting" as well.
1199 * This is significant for procs that start as root and "become"
1200 * a user without an exec - programs cannot know *everything*
1201 * that libc *might* have put in their data segment.
1203 PROC_LOCK(p);
1204 td->td_retval[0] = (p->p_flag & P_SUGID) ? 1 : 0;
1205 PROC_UNLOCK(p);
1206 return (0);
1210 __setugid(struct thread *td, struct __setugid_args *uap)
1212 #ifdef REGRESSION
1213 struct proc *p;
1215 p = td->td_proc;
1216 switch (uap->flag) {
1217 case 0:
1218 PROC_LOCK(p);
1219 p->p_flag &= ~P_SUGID;
1220 PROC_UNLOCK(p);
1221 return (0);
1222 case 1:
1223 PROC_LOCK(p);
1224 p->p_flag |= P_SUGID;
1225 PROC_UNLOCK(p);
1226 return (0);
1227 default:
1228 return (EINVAL);
1230 #else /* !REGRESSION */
1232 return (ENOSYS);
1233 #endif /* REGRESSION */
1237 * Check if gid is a member of the group set.
1240 groupmember(gid_t gid, struct ucred *cred)
1242 register gid_t *gp;
1243 gid_t *egp;
1245 egp = &(cred->cr_groups[cred->cr_ngroups]);
1246 for (gp = cred->cr_groups; gp < egp; gp++)
1247 if (*gp == gid)
1248 return (1);
1249 return (0);
1253 * Test the active securelevel against a given level. securelevel_gt()
1254 * implements (securelevel > level). securelevel_ge() implements
1255 * (securelevel >= level). Note that the logic is inverted -- these
1256 * functions return EPERM on "success" and 0 on "failure".
1258 * XXXRW: Possibly since this has to do with privilege, it should move to
1259 * kern_priv.c.
1262 securelevel_gt(struct ucred *cr, int level)
1264 int active_securelevel;
1266 active_securelevel = securelevel;
1267 KASSERT(cr != NULL, ("securelevel_gt: null cr"));
1268 if (cr->cr_prison != NULL)
1269 active_securelevel = imax(cr->cr_prison->pr_securelevel,
1270 active_securelevel);
1271 return (active_securelevel > level ? EPERM : 0);
1275 securelevel_ge(struct ucred *cr, int level)
1277 int active_securelevel;
1279 active_securelevel = securelevel;
1280 KASSERT(cr != NULL, ("securelevel_ge: null cr"));
1281 if (cr->cr_prison != NULL)
1282 active_securelevel = imax(cr->cr_prison->pr_securelevel,
1283 active_securelevel);
1284 return (active_securelevel >= level ? EPERM : 0);
1288 * 'see_other_uids' determines whether or not visibility of processes
1289 * and sockets with credentials holding different real uids is possible
1290 * using a variety of system MIBs.
1291 * XXX: data declarations should be together near the beginning of the file.
1293 static int see_other_uids = 1;
1294 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_uids, CTLFLAG_RW,
1295 &see_other_uids, 0,
1296 "Unprivileged processes may see subjects/objects with different real uid");
1299 * Determine if u1 "can see" the subject specified by u2, according to the
1300 * 'see_other_uids' policy.
1301 * Returns: 0 for permitted, ESRCH otherwise
1302 * Locks: none
1303 * References: *u1 and *u2 must not change during the call
1304 * u1 may equal u2, in which case only one reference is required
1306 static int
1307 cr_seeotheruids(struct ucred *u1, struct ucred *u2)
1310 if (!see_other_uids && u1->cr_ruid != u2->cr_ruid) {
1311 if (priv_check_cred(u1, PRIV_SEEOTHERUIDS, 0) != 0)
1312 return (ESRCH);
1314 return (0);
1318 * 'see_other_gids' determines whether or not visibility of processes
1319 * and sockets with credentials holding different real gids is possible
1320 * using a variety of system MIBs.
1321 * XXX: data declarations should be together near the beginning of the file.
1323 static int see_other_gids = 1;
1324 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_gids, CTLFLAG_RW,
1325 &see_other_gids, 0,
1326 "Unprivileged processes may see subjects/objects with different real gid");
1329 * Determine if u1 can "see" the subject specified by u2, according to the
1330 * 'see_other_gids' policy.
1331 * Returns: 0 for permitted, ESRCH otherwise
1332 * Locks: none
1333 * References: *u1 and *u2 must not change during the call
1334 * u1 may equal u2, in which case only one reference is required
1336 static int
1337 cr_seeothergids(struct ucred *u1, struct ucred *u2)
1339 int i, match;
1341 if (!see_other_gids) {
1342 match = 0;
1343 for (i = 0; i < u1->cr_ngroups; i++) {
1344 if (groupmember(u1->cr_groups[i], u2))
1345 match = 1;
1346 if (match)
1347 break;
1349 if (!match) {
1350 if (priv_check_cred(u1, PRIV_SEEOTHERGIDS, 0) != 0)
1351 return (ESRCH);
1354 return (0);
1358 * Determine if u1 "can see" the subject specified by u2.
1359 * Returns: 0 for permitted, an errno value otherwise
1360 * Locks: none
1361 * References: *u1 and *u2 must not change during the call
1362 * u1 may equal u2, in which case only one reference is required
1365 cr_cansee(struct ucred *u1, struct ucred *u2)
1367 int error;
1369 if ((error = prison_check(u1, u2)))
1370 return (error);
1371 #ifdef MAC
1372 if ((error = mac_cred_check_visible(u1, u2)))
1373 return (error);
1374 #endif
1375 if ((error = cr_seeotheruids(u1, u2)))
1376 return (error);
1377 if ((error = cr_seeothergids(u1, u2)))
1378 return (error);
1379 return (0);
1383 * Determine if td "can see" the subject specified by p.
1384 * Returns: 0 for permitted, an errno value otherwise
1385 * Locks: Sufficient locks to protect p->p_ucred must be held. td really
1386 * should be curthread.
1387 * References: td and p must be valid for the lifetime of the call
1390 p_cansee(struct thread *td, struct proc *p)
1393 /* Wrap cr_cansee() for all functionality. */
1394 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1395 PROC_LOCK_ASSERT(p, MA_OWNED);
1396 return (cr_cansee(td->td_ucred, p->p_ucred));
1400 * 'conservative_signals' prevents the delivery of a broad class of
1401 * signals by unprivileged processes to processes that have changed their
1402 * credentials since the last invocation of execve(). This can prevent
1403 * the leakage of cached information or retained privileges as a result
1404 * of a common class of signal-related vulnerabilities. However, this
1405 * may interfere with some applications that expect to be able to
1406 * deliver these signals to peer processes after having given up
1407 * privilege.
1409 static int conservative_signals = 1;
1410 SYSCTL_INT(_security_bsd, OID_AUTO, conservative_signals, CTLFLAG_RW,
1411 &conservative_signals, 0, "Unprivileged processes prevented from "
1412 "sending certain signals to processes whose credentials have changed");
1414 * Determine whether cred may deliver the specified signal to proc.
1415 * Returns: 0 for permitted, an errno value otherwise.
1416 * Locks: A lock must be held for proc.
1417 * References: cred and proc must be valid for the lifetime of the call.
1420 cr_cansignal(struct ucred *cred, struct proc *proc, int signum)
1422 int error;
1424 PROC_LOCK_ASSERT(proc, MA_OWNED);
1426 * Jail semantics limit the scope of signalling to proc in the
1427 * same jail as cred, if cred is in jail.
1429 error = prison_check(cred, proc->p_ucred);
1430 if (error)
1431 return (error);
1432 #ifdef MAC
1433 if ((error = mac_proc_check_signal(cred, proc, signum)))
1434 return (error);
1435 #endif
1436 if ((error = cr_seeotheruids(cred, proc->p_ucred)))
1437 return (error);
1438 if ((error = cr_seeothergids(cred, proc->p_ucred)))
1439 return (error);
1442 * UNIX signal semantics depend on the status of the P_SUGID
1443 * bit on the target process. If the bit is set, then additional
1444 * restrictions are placed on the set of available signals.
1446 if (conservative_signals && (proc->p_flag & P_SUGID)) {
1447 switch (signum) {
1448 case 0:
1449 case SIGKILL:
1450 case SIGINT:
1451 case SIGTERM:
1452 case SIGALRM:
1453 case SIGSTOP:
1454 case SIGTTIN:
1455 case SIGTTOU:
1456 case SIGTSTP:
1457 case SIGHUP:
1458 case SIGUSR1:
1459 case SIGUSR2:
1461 * Generally, permit job and terminal control
1462 * signals.
1464 break;
1465 default:
1466 /* Not permitted without privilege. */
1467 error = priv_check_cred(cred, PRIV_SIGNAL_SUGID, 0);
1468 if (error)
1469 return (error);
1474 * Generally, the target credential's ruid or svuid must match the
1475 * subject credential's ruid or euid.
1477 if (cred->cr_ruid != proc->p_ucred->cr_ruid &&
1478 cred->cr_ruid != proc->p_ucred->cr_svuid &&
1479 cred->cr_uid != proc->p_ucred->cr_ruid &&
1480 cred->cr_uid != proc->p_ucred->cr_svuid) {
1481 error = priv_check_cred(cred, PRIV_SIGNAL_DIFFCRED, 0);
1482 if (error)
1483 return (error);
1486 return (0);
1490 * Determine whether td may deliver the specified signal to p.
1491 * Returns: 0 for permitted, an errno value otherwise
1492 * Locks: Sufficient locks to protect various components of td and p
1493 * must be held. td must be curthread, and a lock must be
1494 * held for p.
1495 * References: td and p must be valid for the lifetime of the call
1498 p_cansignal(struct thread *td, struct proc *p, int signum)
1501 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1502 PROC_LOCK_ASSERT(p, MA_OWNED);
1503 if (td->td_proc == p)
1504 return (0);
1507 * UNIX signalling semantics require that processes in the same
1508 * session always be able to deliver SIGCONT to one another,
1509 * overriding the remaining protections.
1511 /* XXX: This will require an additional lock of some sort. */
1512 if (signum == SIGCONT && td->td_proc->p_session == p->p_session)
1513 return (0);
1515 * Some compat layers use SIGTHR and higher signals for
1516 * communication between different kernel threads of the same
1517 * process, so that they expect that it's always possible to
1518 * deliver them, even for suid applications where cr_cansignal() can
1519 * deny such ability for security consideration. It should be
1520 * pretty safe to do since the only way to create two processes
1521 * with the same p_leader is via rfork(2).
1523 if (td->td_proc->p_leader != NULL && signum >= SIGTHR &&
1524 signum < SIGTHR + 4 && td->td_proc->p_leader == p->p_leader)
1525 return (0);
1527 return (cr_cansignal(td->td_ucred, p, signum));
1531 * Determine whether td may reschedule p.
1532 * Returns: 0 for permitted, an errno value otherwise
1533 * Locks: Sufficient locks to protect various components of td and p
1534 * must be held. td must be curthread, and a lock must
1535 * be held for p.
1536 * References: td and p must be valid for the lifetime of the call
1539 p_cansched(struct thread *td, struct proc *p)
1541 int error;
1543 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1544 PROC_LOCK_ASSERT(p, MA_OWNED);
1545 if (td->td_proc == p)
1546 return (0);
1547 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1548 return (error);
1549 #ifdef MAC
1550 if ((error = mac_proc_check_sched(td->td_ucred, p)))
1551 return (error);
1552 #endif
1553 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1554 return (error);
1555 if ((error = cr_seeothergids(td->td_ucred, p->p_ucred)))
1556 return (error);
1557 if (td->td_ucred->cr_ruid != p->p_ucred->cr_ruid &&
1558 td->td_ucred->cr_uid != p->p_ucred->cr_ruid) {
1559 error = priv_check(td, PRIV_SCHED_DIFFCRED);
1560 if (error)
1561 return (error);
1563 return (0);
1567 * The 'unprivileged_proc_debug' flag may be used to disable a variety of
1568 * unprivileged inter-process debugging services, including some procfs
1569 * functionality, ptrace(), and ktrace(). In the past, inter-process
1570 * debugging has been involved in a variety of security problems, and sites
1571 * not requiring the service might choose to disable it when hardening
1572 * systems.
1574 * XXX: Should modifying and reading this variable require locking?
1575 * XXX: data declarations should be together near the beginning of the file.
1577 static int unprivileged_proc_debug = 1;
1578 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_proc_debug, CTLFLAG_RW,
1579 &unprivileged_proc_debug, 0,
1580 "Unprivileged processes may use process debugging facilities");
1583 * Determine whether td may debug p.
1584 * Returns: 0 for permitted, an errno value otherwise
1585 * Locks: Sufficient locks to protect various components of td and p
1586 * must be held. td must be curthread, and a lock must
1587 * be held for p.
1588 * References: td and p must be valid for the lifetime of the call
1591 p_candebug(struct thread *td, struct proc *p)
1593 int credentialchanged, error, grpsubset, i, uidsubset;
1595 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1596 PROC_LOCK_ASSERT(p, MA_OWNED);
1597 if (!unprivileged_proc_debug) {
1598 error = priv_check(td, PRIV_DEBUG_UNPRIV);
1599 if (error)
1600 return (error);
1602 if (td->td_proc == p)
1603 return (0);
1604 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1605 return (error);
1606 #ifdef MAC
1607 if ((error = mac_proc_check_debug(td->td_ucred, p)))
1608 return (error);
1609 #endif
1610 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1611 return (error);
1612 if ((error = cr_seeothergids(td->td_ucred, p->p_ucred)))
1613 return (error);
1616 * Is p's group set a subset of td's effective group set? This
1617 * includes p's egid, group access list, rgid, and svgid.
1619 grpsubset = 1;
1620 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
1621 if (!groupmember(p->p_ucred->cr_groups[i], td->td_ucred)) {
1622 grpsubset = 0;
1623 break;
1626 grpsubset = grpsubset &&
1627 groupmember(p->p_ucred->cr_rgid, td->td_ucred) &&
1628 groupmember(p->p_ucred->cr_svgid, td->td_ucred);
1631 * Are the uids present in p's credential equal to td's
1632 * effective uid? This includes p's euid, svuid, and ruid.
1634 uidsubset = (td->td_ucred->cr_uid == p->p_ucred->cr_uid &&
1635 td->td_ucred->cr_uid == p->p_ucred->cr_svuid &&
1636 td->td_ucred->cr_uid == p->p_ucred->cr_ruid);
1639 * Has the credential of the process changed since the last exec()?
1641 credentialchanged = (p->p_flag & P_SUGID);
1644 * If p's gids aren't a subset, or the uids aren't a subset,
1645 * or the credential has changed, require appropriate privilege
1646 * for td to debug p.
1648 if (!grpsubset || !uidsubset) {
1649 error = priv_check(td, PRIV_DEBUG_DIFFCRED);
1650 if (error)
1651 return (error);
1654 if (credentialchanged) {
1655 error = priv_check(td, PRIV_DEBUG_SUGID);
1656 if (error)
1657 return (error);
1660 /* Can't trace init when securelevel > 0. */
1661 if (p == initproc) {
1662 error = securelevel_gt(td->td_ucred, 0);
1663 if (error)
1664 return (error);
1668 * Can't trace a process that's currently exec'ing.
1670 * XXX: Note, this is not a security policy decision, it's a
1671 * basic correctness/functionality decision. Therefore, this check
1672 * should be moved to the caller's of p_candebug().
1674 if ((p->p_flag & P_INEXEC) != 0)
1675 return (EAGAIN);
1677 return (0);
1681 * Determine whether the subject represented by cred can "see" a socket.
1682 * Returns: 0 for permitted, ENOENT otherwise.
1685 cr_canseesocket(struct ucred *cred, struct socket *so)
1687 int error;
1689 error = prison_check(cred, so->so_cred);
1690 if (error)
1691 return (ENOENT);
1692 #ifdef MAC
1693 SOCK_LOCK(so);
1694 error = mac_socket_check_visible(cred, so);
1695 SOCK_UNLOCK(so);
1696 if (error)
1697 return (error);
1698 #endif
1699 if (cr_seeotheruids(cred, so->so_cred))
1700 return (ENOENT);
1701 if (cr_seeothergids(cred, so->so_cred))
1702 return (ENOENT);
1704 return (0);
1708 * Determine whether td can wait for the exit of p.
1709 * Returns: 0 for permitted, an errno value otherwise
1710 * Locks: Sufficient locks to protect various components of td and p
1711 * must be held. td must be curthread, and a lock must
1712 * be held for p.
1713 * References: td and p must be valid for the lifetime of the call
1717 p_canwait(struct thread *td, struct proc *p)
1719 int error;
1721 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1722 PROC_LOCK_ASSERT(p, MA_OWNED);
1723 if ((error = prison_check(td->td_ucred, p->p_ucred)))
1724 return (error);
1725 #ifdef MAC
1726 if ((error = mac_proc_check_wait(td->td_ucred, p)))
1727 return (error);
1728 #endif
1729 #if 0
1730 /* XXXMAC: This could have odd effects on some shells. */
1731 if ((error = cr_seeotheruids(td->td_ucred, p->p_ucred)))
1732 return (error);
1733 #endif
1735 return (0);
1739 * Allocate a zeroed cred structure.
1741 struct ucred *
1742 crget(void)
1744 register struct ucred *cr;
1746 MALLOC(cr, struct ucred *, sizeof(*cr), M_CRED, M_WAITOK | M_ZERO);
1747 refcount_init(&cr->cr_ref, 1);
1748 #ifdef AUDIT
1749 audit_cred_init(cr);
1750 #endif
1751 #ifdef MAC
1752 mac_cred_init(cr);
1753 #endif
1754 return (cr);
1758 * Claim another reference to a ucred structure.
1760 struct ucred *
1761 crhold(struct ucred *cr)
1764 refcount_acquire(&cr->cr_ref);
1765 return (cr);
1769 * Free a cred structure. Throws away space when ref count gets to 0.
1771 void
1772 crfree(struct ucred *cr)
1775 KASSERT(cr->cr_ref > 0, ("bad ucred refcount: %d", cr->cr_ref));
1776 KASSERT(cr->cr_ref != 0xdeadc0de, ("dangling reference to ucred"));
1777 if (refcount_release(&cr->cr_ref)) {
1779 * Some callers of crget(), such as nfs_statfs(),
1780 * allocate a temporary credential, but don't
1781 * allocate a uidinfo structure.
1783 if (cr->cr_uidinfo != NULL)
1784 uifree(cr->cr_uidinfo);
1785 if (cr->cr_ruidinfo != NULL)
1786 uifree(cr->cr_ruidinfo);
1788 * Free a prison, if any.
1790 if (jailed(cr))
1791 prison_free(cr->cr_prison);
1792 #ifdef AUDIT
1793 audit_cred_destroy(cr);
1794 #endif
1795 #ifdef MAC
1796 mac_cred_destroy(cr);
1797 #endif
1798 FREE(cr, M_CRED);
1803 * Check to see if this ucred is shared.
1806 crshared(struct ucred *cr)
1809 return (cr->cr_ref > 1);
1813 * Copy a ucred's contents from a template. Does not block.
1815 void
1816 crcopy(struct ucred *dest, struct ucred *src)
1819 KASSERT(crshared(dest) == 0, ("crcopy of shared ucred"));
1820 bcopy(&src->cr_startcopy, &dest->cr_startcopy,
1821 (unsigned)((caddr_t)&src->cr_endcopy -
1822 (caddr_t)&src->cr_startcopy));
1823 uihold(dest->cr_uidinfo);
1824 uihold(dest->cr_ruidinfo);
1825 if (jailed(dest))
1826 prison_hold(dest->cr_prison);
1827 #ifdef AUDIT
1828 audit_cred_copy(src, dest);
1829 #endif
1830 #ifdef MAC
1831 mac_cred_copy(src, dest);
1832 #endif
1836 * Dup cred struct to a new held one.
1838 struct ucred *
1839 crdup(struct ucred *cr)
1841 struct ucred *newcr;
1843 newcr = crget();
1844 crcopy(newcr, cr);
1845 return (newcr);
1849 * Fill in a struct xucred based on a struct ucred.
1851 void
1852 cru2x(struct ucred *cr, struct xucred *xcr)
1855 bzero(xcr, sizeof(*xcr));
1856 xcr->cr_version = XUCRED_VERSION;
1857 xcr->cr_uid = cr->cr_uid;
1858 xcr->cr_ngroups = cr->cr_ngroups;
1859 bcopy(cr->cr_groups, xcr->cr_groups, sizeof(cr->cr_groups));
1863 * small routine to swap a thread's current ucred for the correct one taken
1864 * from the process.
1866 void
1867 cred_update_thread(struct thread *td)
1869 struct proc *p;
1870 struct ucred *cred;
1872 p = td->td_proc;
1873 cred = td->td_ucred;
1874 PROC_LOCK(p);
1875 td->td_ucred = crhold(p->p_ucred);
1876 PROC_UNLOCK(p);
1877 if (cred != NULL)
1878 crfree(cred);
1882 * Get login name, if available.
1884 #ifndef _SYS_SYSPROTO_H_
1885 struct getlogin_args {
1886 char *namebuf;
1887 u_int namelen;
1889 #endif
1890 /* ARGSUSED */
1892 getlogin(struct thread *td, struct getlogin_args *uap)
1894 int error;
1895 char login[MAXLOGNAME];
1896 struct proc *p = td->td_proc;
1898 if (uap->namelen > MAXLOGNAME)
1899 uap->namelen = MAXLOGNAME;
1900 PROC_LOCK(p);
1901 SESS_LOCK(p->p_session);
1902 bcopy(p->p_session->s_login, login, uap->namelen);
1903 SESS_UNLOCK(p->p_session);
1904 PROC_UNLOCK(p);
1905 error = copyout(login, uap->namebuf, uap->namelen);
1906 return(error);
1910 * Set login name.
1912 #ifndef _SYS_SYSPROTO_H_
1913 struct setlogin_args {
1914 char *namebuf;
1916 #endif
1917 /* ARGSUSED */
1919 setlogin(struct thread *td, struct setlogin_args *uap)
1921 struct proc *p = td->td_proc;
1922 int error;
1923 char logintmp[MAXLOGNAME];
1925 error = priv_check(td, PRIV_PROC_SETLOGIN);
1926 if (error)
1927 return (error);
1928 error = copyinstr(uap->namebuf, logintmp, sizeof(logintmp), NULL);
1929 if (error == ENAMETOOLONG)
1930 error = EINVAL;
1931 else if (!error) {
1932 PROC_LOCK(p);
1933 SESS_LOCK(p->p_session);
1934 (void) memcpy(p->p_session->s_login, logintmp,
1935 sizeof(logintmp));
1936 SESS_UNLOCK(p->p_session);
1937 PROC_UNLOCK(p);
1939 return (error);
1942 void
1943 setsugid(struct proc *p)
1946 PROC_LOCK_ASSERT(p, MA_OWNED);
1947 p->p_flag |= P_SUGID;
1948 if (!(p->p_pfsflags & PF_ISUGID))
1949 p->p_stops = 0;
1953 * Change a process's effective uid.
1954 * Side effects: newcred->cr_uid and newcred->cr_uidinfo will be modified.
1955 * References: newcred must be an exclusive credential reference for the
1956 * duration of the call.
1958 void
1959 change_euid(struct ucred *newcred, struct uidinfo *euip)
1962 newcred->cr_uid = euip->ui_uid;
1963 uihold(euip);
1964 uifree(newcred->cr_uidinfo);
1965 newcred->cr_uidinfo = euip;
1969 * Change a process's effective gid.
1970 * Side effects: newcred->cr_gid will be modified.
1971 * References: newcred must be an exclusive credential reference for the
1972 * duration of the call.
1974 void
1975 change_egid(struct ucred *newcred, gid_t egid)
1978 newcred->cr_groups[0] = egid;
1982 * Change a process's real uid.
1983 * Side effects: newcred->cr_ruid will be updated, newcred->cr_ruidinfo
1984 * will be updated, and the old and new cr_ruidinfo proc
1985 * counts will be updated.
1986 * References: newcred must be an exclusive credential reference for the
1987 * duration of the call.
1989 void
1990 change_ruid(struct ucred *newcred, struct uidinfo *ruip)
1993 (void)chgproccnt(newcred->cr_ruidinfo, -1, 0);
1994 newcred->cr_ruid = ruip->ui_uid;
1995 uihold(ruip);
1996 uifree(newcred->cr_ruidinfo);
1997 newcred->cr_ruidinfo = ruip;
1998 (void)chgproccnt(newcred->cr_ruidinfo, 1, 0);
2002 * Change a process's real gid.
2003 * Side effects: newcred->cr_rgid will be updated.
2004 * References: newcred must be an exclusive credential reference for the
2005 * duration of the call.
2007 void
2008 change_rgid(struct ucred *newcred, gid_t rgid)
2011 newcred->cr_rgid = rgid;
2015 * Change a process's saved uid.
2016 * Side effects: newcred->cr_svuid will be updated.
2017 * References: newcred must be an exclusive credential reference for the
2018 * duration of the call.
2020 void
2021 change_svuid(struct ucred *newcred, uid_t svuid)
2024 newcred->cr_svuid = svuid;
2028 * Change a process's saved gid.
2029 * Side effects: newcred->cr_svgid will be updated.
2030 * References: newcred must be an exclusive credential reference for the
2031 * duration of the call.
2033 void
2034 change_svgid(struct ucred *newcred, gid_t svgid)
2037 newcred->cr_svgid = svgid;