56#include <sys/kernel.h>
58#include <sys/loginclass.h>
59#include <sys/malloc.h>
61#include <sys/ptrace.h>
62#include <sys/refcount.h>
66#include <sys/sysent.h>
67#include <sys/sysproto.h>
71#include <sys/resourcevar.h>
72#include <sys/socket.h>
73#include <sys/socketvar.h>
74#include <sys/syscallsubr.h>
75#include <sys/sysctl.h>
79 "Kernel support for interfaces necessary for regression testing (SECURITY RISK!)");
82#include <security/audit/audit.h>
83#include <security/mac/mac_framework.h>
87SYSCTL_NODE(_security, OID_AUTO, bsd, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
88 "BSD security policy");
94#ifndef _SYS_SYSPROTO_H_
103 struct proc *p = td->td_proc;
105 td->td_retval[0] = p->p_pid;
106#if defined(COMPAT_43)
107 if (SV_PROC_FLAG(p, SV_AOUT))
113#ifndef _SYS_SYSPROTO_H_
130 struct proc *p = td->td_proc;
138#ifndef _SYS_SYSPROTO_H_
146 struct proc *p = td->td_proc;
149 td->td_retval[0] = p->p_pgrp->pg_id;
155#ifndef _SYS_SYSPROTO_H_
179 td->td_retval[0] = p->p_pgrp->pg_id;
187#ifndef _SYS_SYSPROTO_H_
218 td->td_retval[0] = p->p_session->s_sid;
223#ifndef _SYS_SYSPROTO_H_
233 td->td_retval[0] = td->td_ucred->cr_ruid;
234#if defined(COMPAT_43)
235 td->td_retval[1] = td->td_ucred->cr_uid;
240#ifndef _SYS_SYSPROTO_H_
250 td->td_retval[0] = td->td_ucred->cr_uid;
254#ifndef _SYS_SYSPROTO_H_
264 td->td_retval[0] = td->td_ucred->cr_rgid;
265#if defined(COMPAT_43)
266 td->td_retval[1] = td->td_ucred->cr_groups[0];
276#ifndef _SYS_SYSPROTO_H_
286 td->td_retval[0] = td->td_ucred->cr_groups[0];
290#ifndef _SYS_SYSPROTO_H_
303 ngrp = cred->cr_ngroups;
312 error = copyout(cred->cr_groups, uap->
gidset, ngrp *
sizeof(gid_t));
314 td->td_retval[0] = ngrp;
318#ifndef _SYS_SYSPROTO_H_
329 struct proc *p = td->td_proc;
330 struct pgrp *newpgrp;
331 struct session *newsess;
336 newpgrp = uma_zalloc(
pgrp_zone, M_WAITOK);
337 newsess =
malloc(
sizeof(
struct session), M_SESSION, M_WAITOK | M_ZERO);
341 if (p->p_pgid == p->p_pid || (pgrp =
pgfind(p->p_pid)) != NULL) {
346 (void)
enterpgrp(p, p->p_pid, newpgrp, newsess);
347 td->td_retval[0] = p->p_pid;
355 free(newsess, M_SESSION);
373#ifndef _SYS_SYSPROTO_H_
383 struct proc *curp = td->td_proc;
387 struct pgrp *newpgrp;
394 newpgrp = uma_zalloc(
pgrp_zone, M_WAITOK);
397 if (uap->
pid != 0 && uap->
pid != curp->p_pid) {
398 if ((targp =
pfind(uap->
pid)) == NULL) {
407 if ((error =
p_cansee(td, targp))) {
411 if (targp->p_pgrp == NULL ||
412 targp->p_session != curp->p_session) {
417 if (targp->p_flag & P_EXEC) {
425 if (SESS_LEADER(targp)) {
430 uap->
pgid = targp->p_pid;
432 if (uap->
pgid == targp->p_pid) {
440 if (pgrp == targp->p_pgrp) {
444 if (pgrp->pg_id != targp->p_pid &&
445 pgrp->pg_session != curp->p_session) {
455 KASSERT((error == 0) || (newpgrp != NULL),
456 (
"setpgid failed and newpgrp is NULL"));
471#define POSIX_APPENDIX_B_4_2_2
473#ifndef _SYS_SYSPROTO_H_
482 struct proc *p = td->td_proc;
483 struct ucred *newcred, *oldcred;
499 error = mac_cred_check_setuid(oldcred, uid);
521 if (uid != oldcred->cr_ruid &&
522#ifdef _POSIX_SAVED_IDS
523 uid != oldcred->cr_svuid &&
526 uid != oldcred->cr_uid &&
531#ifdef _POSIX_SAVED_IDS
538 uid == oldcred->cr_uid ||
547 if (uid != oldcred->cr_ruid) {
558 if (uid != oldcred->cr_svuid) {
567 if (uid != oldcred->cr_uid) {
573 racct_proc_ucred_changed(p, oldcred, newcred);
578 rctl_proc_ucred_changed(p, newcred);
592#ifndef _SYS_SYSPROTO_H_
601 struct proc *p = td->td_proc;
602 struct ucred *newcred, *oldcred;
604 struct uidinfo *euip;
608 AUDIT_ARG_EUID(euid);
618 error = mac_cred_check_seteuid(oldcred, euid);
623 if (euid != oldcred->cr_ruid &&
624 euid != oldcred->cr_svuid &&
631 if (oldcred->cr_uid != euid) {
648#ifndef _SYS_SYSPROTO_H_
657 struct proc *p = td->td_proc;
658 struct ucred *newcred, *oldcred;
669 error = mac_cred_check_setgid(oldcred, gid);
685 if (gid != oldcred->cr_rgid &&
686#ifdef _POSIX_SAVED_IDS
687 gid != oldcred->cr_svgid &&
690 gid != oldcred->cr_groups[0] &&
695#ifdef _POSIX_SAVED_IDS
702 gid == oldcred->cr_groups[0] ||
711 if (oldcred->cr_rgid != gid) {
722 if (oldcred->cr_svgid != gid) {
731 if (oldcred->cr_groups[0] != gid) {
746#ifndef _SYS_SYSPROTO_H_
755 struct proc *p = td->td_proc;
756 struct ucred *newcred, *oldcred;
761 AUDIT_ARG_EGID(egid);
767 error = mac_cred_check_setegid(oldcred, egid);
772 if (egid != oldcred->cr_rgid &&
773 egid != oldcred->cr_svgid &&
777 if (oldcred->cr_groups[0] != egid) {
792#ifndef _SYS_SYSPROTO_H_
802 gid_t smallgroups[XU_NGROUPS];
804 int gidsetsize, error;
807 if (gidsetsize >
ngroups_max + 1 || gidsetsize < 0)
810 if (gidsetsize > XU_NGROUPS)
811 groups =
malloc(gidsetsize *
sizeof(gid_t), M_TEMP, M_WAITOK);
813 groups = smallgroups;
815 error = copyin(uap->
gidset, groups, gidsetsize *
sizeof(gid_t));
819 if (gidsetsize > XU_NGROUPS)
820 free(groups, M_TEMP);
827 struct proc *p = td->td_proc;
828 struct ucred *newcred, *oldcred;
832 AUDIT_ARG_GROUPSET(groups, ngrp);
839 error = mac_cred_check_setgroups(oldcred, ngrp, groups);
855 newcred->cr_ngroups = 1;
871#ifndef _SYS_SYSPROTO_H_
881 struct proc *p = td->td_proc;
882 struct ucred *newcred, *oldcred;
884 struct uidinfo *euip, *ruip;
889 AUDIT_ARG_EUID(euid);
890 AUDIT_ARG_RUID(ruid);
898 error = mac_cred_check_setreuid(oldcred, ruid, euid);
903 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
904 ruid != oldcred->cr_svuid) ||
905 (euid != (uid_t)-1 && euid != oldcred->cr_uid &&
906 euid != oldcred->cr_ruid && euid != oldcred->cr_svuid)) &&
910 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
914 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
918 if ((ruid != (uid_t)-1 || newcred->cr_uid != newcred->cr_ruid) &&
919 newcred->cr_svuid != newcred->cr_uid) {
925 racct_proc_ucred_changed(p, oldcred, newcred);
930 rctl_proc_ucred_changed(p, newcred);
946#ifndef _SYS_SYSPROTO_H_
956 struct proc *p = td->td_proc;
957 struct ucred *newcred, *oldcred;
963 AUDIT_ARG_EGID(egid);
964 AUDIT_ARG_RGID(rgid);
970 error = mac_cred_check_setregid(oldcred, rgid, egid);
975 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
976 rgid != oldcred->cr_svgid) ||
977 (egid != (gid_t)-1 && egid != oldcred->cr_groups[0] &&
978 egid != oldcred->cr_rgid && egid != oldcred->cr_svgid)) &&
982 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
986 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
990 if ((rgid != (gid_t)-1 || newcred->cr_groups[0] != newcred->cr_rgid) &&
991 newcred->cr_svgid != newcred->cr_groups[0]) {
1010#ifndef _SYS_SYSPROTO_H_
1021 struct proc *p = td->td_proc;
1022 struct ucred *newcred, *oldcred;
1023 uid_t euid, ruid, suid;
1024 struct uidinfo *euip, *ruip;
1030 AUDIT_ARG_EUID(euid);
1031 AUDIT_ARG_RUID(ruid);
1032 AUDIT_ARG_SUID(suid);
1040 error = mac_cred_check_setresuid(oldcred, ruid, euid, suid);
1045 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
1046 ruid != oldcred->cr_svuid &&
1047 ruid != oldcred->cr_uid) ||
1048 (euid != (uid_t)-1 && euid != oldcred->cr_ruid &&
1049 euid != oldcred->cr_svuid &&
1050 euid != oldcred->cr_uid) ||
1051 (suid != (uid_t)-1 && suid != oldcred->cr_ruid &&
1052 suid != oldcred->cr_svuid &&
1053 suid != oldcred->cr_uid)) &&
1057 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
1061 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
1065 if (suid != (uid_t)-1 && oldcred->cr_svuid != suid) {
1071 racct_proc_ucred_changed(p, oldcred, newcred);
1076 rctl_proc_ucred_changed(p, newcred);
1097#ifndef _SYS_SYSPROTO_H_
1108 struct proc *p = td->td_proc;
1109 struct ucred *newcred, *oldcred;
1110 gid_t egid, rgid, sgid;
1116 AUDIT_ARG_EGID(egid);
1117 AUDIT_ARG_RGID(rgid);
1118 AUDIT_ARG_SGID(sgid);
1124 error = mac_cred_check_setresgid(oldcred, rgid, egid, sgid);
1129 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
1130 rgid != oldcred->cr_svgid &&
1131 rgid != oldcred->cr_groups[0]) ||
1132 (egid != (gid_t)-1 && egid != oldcred->cr_rgid &&
1133 egid != oldcred->cr_svgid &&
1134 egid != oldcred->cr_groups[0]) ||
1135 (sgid != (gid_t)-1 && sgid != oldcred->cr_rgid &&
1136 sgid != oldcred->cr_svgid &&
1137 sgid != oldcred->cr_groups[0])) &&
1141 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
1145 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
1149 if (sgid != (gid_t)-1 && oldcred->cr_svgid != sgid) {
1164#ifndef _SYS_SYSPROTO_H_
1176 int error1 = 0, error2 = 0, error3 = 0;
1178 cred = td->td_ucred;
1180 error1 = copyout(&cred->cr_ruid,
1181 uap->
ruid,
sizeof(cred->cr_ruid));
1183 error2 = copyout(&cred->cr_uid,
1184 uap->
euid,
sizeof(cred->cr_uid));
1186 error3 = copyout(&cred->cr_svuid,
1187 uap->
suid,
sizeof(cred->cr_svuid));
1188 return (error1 ? error1 : error2 ? error2 : error3);
1191#ifndef _SYS_SYSPROTO_H_
1203 int error1 = 0, error2 = 0, error3 = 0;
1205 cred = td->td_ucred;
1207 error1 = copyout(&cred->cr_rgid,
1208 uap->
rgid,
sizeof(cred->cr_rgid));
1210 error2 = copyout(&cred->cr_groups[0],
1211 uap->
egid,
sizeof(cred->cr_groups[0]));
1213 error3 = copyout(&cred->cr_svgid,
1214 uap->
sgid,
sizeof(cred->cr_svgid));
1215 return (error1 ? error1 : error2 ? error2 : error3);
1218#ifndef _SYS_SYSPROTO_H_
1227 struct proc *p = td->td_proc;
1237 td->td_retval[0] = (p->p_flag & P_SUGID) ? 1 : 0;
1248 switch (uap->flag) {
1251 p->p_flag &= ~P_SUGID;
1256 p->p_flag |= P_SUGID;
1278 if (cred->cr_groups[0] == gid)
1287 h = cred->cr_ngroups;
1289 m = l + ((h - l) / 2);
1290 if (cred->cr_groups[m] < gid)
1295 if ((l < cred->cr_ngroups) && (cred->cr_groups[l] == gid))
1318 return (cr->cr_prison->pr_securelevel >
level ? EPERM : 0);
1325 return (cr->cr_prison->pr_securelevel >=
level ? EPERM : 0);
1337 "Unprivileged processes may see subjects/objects with different real uid");
1367 "Unprivileged processes may see subjects/objects with different real gid");
1384 for (i = 0; i < u1->cr_ngroups; i++) {
1409 "Unprivileged processes may see subjects/objects with different jail ids");
1422 if (u1->cr_uid == 0)
1424 return (!
see_jail_proc && u1->cr_prison != u2->cr_prison ? ESRCH : 0);
1442 if ((error = mac_cred_check_visible(u1, u2)))
1465 KASSERT(td == curthread, (
"%s: td not curthread", __func__));
1466 PROC_LOCK_ASSERT(p, MA_OWNED);
1468 if (td->td_proc == p)
1470 return (
cr_cansee(td->td_ucred, p->p_ucred));
1486 "sending certain signals to processes whose credentials have changed");
1498 PROC_LOCK_ASSERT(proc, MA_OWNED);
1507 if ((error = mac_proc_check_signal(cred, proc, signum)))
1551 if (cred->cr_ruid != proc->p_ucred->cr_ruid &&
1552 cred->cr_ruid != proc->p_ucred->cr_svuid &&
1553 cred->cr_uid != proc->p_ucred->cr_ruid &&
1554 cred->cr_uid != proc->p_ucred->cr_svuid) {
1575 KASSERT(td == curthread, (
"%s: td not curthread", __func__));
1576 PROC_LOCK_ASSERT(p, MA_OWNED);
1577 if (td->td_proc == p)
1586 if (signum == SIGCONT && td->td_proc->p_session == p->p_session)
1597 if (td->td_proc->p_leader != NULL && signum >= SIGTHR &&
1598 signum < SIGTHR + 4 && td->td_proc->p_leader == p->p_leader)
1617 KASSERT(td == curthread, (
"%s: td not curthread", __func__));
1618 PROC_LOCK_ASSERT(p, MA_OWNED);
1619 if (td->td_proc == p)
1624 if ((error = mac_proc_check_sched(td->td_ucred, p)))
1631 if (td->td_ucred->cr_ruid != p->p_ucred->cr_ruid &&
1632 td->td_ucred->cr_uid != p->p_ucred->cr_ruid) {
1649 val =
prison_allow(req->td->td_ucred, PR_ALLOW_UNPRIV_DEBUG);
1651 if (error != 0 || req->newptr == NULL)
1653 if (val != 0 && val != 1)
1668 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_SECURE |
1670 "Unprivileged processes may use process debugging facilities");
1683 int credentialchanged, error, grpsubset, i, uidsubset;
1685 KASSERT(td == curthread, (
"%s: td not curthread", __func__));
1686 PROC_LOCK_ASSERT(p, MA_OWNED);
1687 if (td->td_proc == p)
1689 if ((error =
priv_check(td, PRIV_DEBUG_UNPRIV)))
1694 if ((error = mac_proc_check_debug(td->td_ucred, p)))
1707 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
1708 if (!
groupmember(p->p_ucred->cr_groups[i], td->td_ucred)) {
1713 grpsubset = grpsubset &&
1721 uidsubset = (td->td_ucred->cr_uid == p->p_ucred->cr_uid &&
1722 td->td_ucred->cr_uid == p->p_ucred->cr_svuid &&
1723 td->td_ucred->cr_uid == p->p_ucred->cr_ruid);
1728 credentialchanged = (p->p_flag & P_SUGID);
1735 if (!grpsubset || !uidsubset) {
1741 if (credentialchanged) {
1761 if ((p->p_flag & P_INEXEC) != 0)
1765 if ((p->p_flag2 & P2_NOTRACE) != 0) {
1787 error = mac_socket_check_visible(cred, so);
1813 KASSERT(td == curthread, (
"%s: td not curthread", __func__));
1814 PROC_LOCK_ASSERT(p, MA_OWNED);
1818 if ((error = mac_proc_check_wait(td->td_ucred, p)))
1857 mtx_lock(&cr->cr_mtx);
1858 KASSERT(cr->cr_users > 0, (
"%s: users %d not > 0 on cred %p",
1859 __func__, cr->cr_users, cr));
1862 mtx_unlock(&cr->cr_mtx);
1866static struct ucred *
1869 struct ucred *cr, *crold;
1871 MPASS(td->td_realucred == td->td_ucred);
1872 cr = td->td_realucred;
1873 mtx_lock(&cr->cr_mtx);
1874 cr->cr_ref += td->td_ucredref;
1875 td->td_ucredref = 0;
1876 KASSERT(cr->cr_users > 0, (
"%s: users %d not > 0 on cred %p",
1877 __func__, cr->cr_users, cr));
1879 if (cr->cr_users == 0) {
1880 KASSERT(cr->cr_ref > 0, (
"%s: ref %d not > 0 on cred %p",
1881 __func__, cr->cr_ref, cr));
1887 mtx_unlock(&cr->cr_mtx);
1888 td->td_realucred = NULL;
1896 KASSERT(users > 0, (
"%s: passed users %d not > 0 ; cred %p",
1897 __func__, users, cr));
1898 mtx_lock(&cr->cr_mtx);
1899 KASSERT(cr->cr_users >= users, (
"%s: users %d not > %d on cred %p",
1900 __func__, cr->cr_users, users, cr));
1901 cr->cr_users -= users;
1903 cr->cr_ref -= users;
1904 if (cr->cr_users > 0) {
1905 mtx_unlock(&cr->cr_mtx);
1908 KASSERT(cr->cr_ref >= 0, (
"%s: ref %d not >= 0 on cred %p",
1909 __func__, cr->cr_ref, cr));
1910 if (cr->cr_ref > 0) {
1911 mtx_unlock(&cr->cr_mtx);
1932 struct ucred *crnew, *crold;
1936 PROC_LOCK_ASSERT(p, MA_OWNED);
1938 MPASS(td->td_realucred == td->td_ucred);
1939 if (td->td_realucred == p->p_ucred)
1944 td->td_realucred = crnew;
1945 td->td_ucred = td->td_realucred;
1957 MPASS(td->td_realucred != NULL);
1958 MPASS(td->td_realucred == td->td_ucred);
1959 MPASS(TD_GET_STATE(td) == TDS_INACTIVE);
1960 cr = td->td_realucred;
1961 KASSERT(cr->cr_users > 0, (
"%s: users %d not > 0 on cred %p",
1962 __func__, cr->cr_users, cr));
1963 if (crb->cred != cr) {
1964 if (crb->users > 0) {
1965 MPASS(crb->cred != NULL);
1973 crb->ref += td->td_ucredref;
1974 td->td_ucredref = 0;
1975 td->td_realucred = NULL;
1982 MPASS(crb->cred != NULL);
1983 MPASS(crb->users > 0);
1995 cr =
malloc(
sizeof(*cr), M_CRED, M_WAITOK | M_ZERO);
1996 mtx_init(&cr->cr_mtx,
"cred", NULL, MTX_DEF);
1999 audit_cred_init(cr);
2004 cr->cr_groups = cr->cr_smallgroups;
2006 sizeof(cr->cr_smallgroups) /
sizeof(cr->cr_smallgroups[0]);
2019 if (__predict_true(td->td_realucred == cr)) {
2020 KASSERT(cr->cr_users > 0, (
"%s: users %d not > 0 on cred %p",
2021 __func__, cr->cr_users, cr));
2025 mtx_lock(&cr->cr_mtx);
2027 mtx_unlock(&cr->cr_mtx);
2040 if (__predict_true(td->td_realucred == cr)) {
2041 KASSERT(cr->cr_users > 0, (
"%s: users %d not > 0 on cred %p",
2042 __func__, cr->cr_users, cr));
2046 mtx_lock(&cr->cr_mtx);
2047 KASSERT(cr->cr_users >= 0, (
"%s: users %d not >= 0 on cred %p",
2048 __func__, cr->cr_users, cr));
2050 if (cr->cr_users > 0) {
2051 mtx_unlock(&cr->cr_mtx);
2054 KASSERT(cr->cr_ref >= 0, (
"%s: ref %d not >= 0 on cred %p",
2055 __func__, cr->cr_ref, cr));
2056 if (cr->cr_ref > 0) {
2057 mtx_unlock(&cr->cr_mtx);
2067 KASSERT(cr->cr_users == 0, (
"%s: users %d not == 0 on cred %p",
2068 __func__, cr->cr_users, cr));
2069 KASSERT(cr->cr_ref == 0, (
"%s: ref %d not == 0 on cred %p",
2070 __func__, cr->cr_ref, cr));
2076 if (cr->cr_uidinfo != NULL)
2078 if (cr->cr_ruidinfo != NULL)
2080 if (cr->cr_prison != NULL)
2082 if (cr->cr_loginclass != NULL)
2085 audit_cred_destroy(cr);
2088 mac_cred_destroy(cr);
2090 mtx_destroy(&cr->cr_mtx);
2091 if (cr->cr_groups != cr->cr_smallgroups)
2092 free(cr->cr_groups, M_CRED);
2103 KASSERT(dest->cr_ref == 1, (
"crcopy of shared ucred"));
2104 bcopy(&
src->cr_startcopy, &dest->cr_startcopy,
2105 (
unsigned)((caddr_t)&
src->cr_endcopy -
2106 (caddr_t)&
src->cr_startcopy));
2108 uihold(dest->cr_uidinfo);
2109 uihold(dest->cr_ruidinfo);
2111 loginclass_hold(dest->cr_loginclass);
2113 audit_cred_copy(
src, dest);
2116 mac_cred_copy(
src, dest);
2126 struct ucred *newcr;
2137cru2x(
struct ucred *cr,
struct xucred *xcr)
2141 bzero(xcr,
sizeof(*xcr));
2142 xcr->cr_version = XUCRED_VERSION;
2143 xcr->cr_uid = cr->cr_uid;
2145 ngroups = MIN(cr->cr_ngroups, XU_NGROUPS);
2146 xcr->cr_ngroups = ngroups;
2147 bcopy(cr->cr_groups, xcr->cr_groups,
2148 ngroups *
sizeof(*cr->cr_groups));
2155 cru2x(td->td_ucred, xcr);
2156 xcr->cr_pid = td->td_proc->p_pid;
2187 PROC_LOCK_ASSERT(p, MA_OWNED);
2188 KASSERT(newcred->cr_users == 0, (
"%s: users %d not 0 on cred %p",
2189 __func__, newcred->cr_users, newcred));
2190 mtx_lock(&cr->cr_mtx);
2191 KASSERT(cr->cr_users > 0, (
"%s: users %d not > 0 on cred %p",
2192 __func__, cr->cr_users, cr));
2194 mtx_unlock(&cr->cr_mtx);
2195 p->p_ucred = newcred;
2196 newcred->cr_users = 1;
2205 MPASS(p->p_state == PRS_ZOMBIE || p->p_state == PRS_NEW);
2208 KASSERT(cr->cr_users > 0, (
"%s: users %d not > 0 on cred %p",
2209 __func__, cr->cr_users, cr));
2210 mtx_lock(&cr->cr_mtx);
2212 if (cr->cr_users == 0)
2213 KASSERT(cr->cr_ref > 0, (
"%s: ref %d not > 0 on cred %p",
2214 __func__, cr->cr_ref, cr));
2215 mtx_unlock(&cr->cr_mtx);
2222 struct ucred *oldcred;
2225 PROC_LOCK_ASSERT(p, MA_OWNED);
2227 oldcred = p->p_ucred;
2228 while (cr->cr_agroups < oldcred->cr_agroups) {
2229 groups = oldcred->cr_agroups;
2233 oldcred = p->p_ucred;
2249 if (n <= cr->cr_agroups)
2261 if ( n < PAGE_SIZE /
sizeof(gid_t) ) {
2262 if (cr->cr_agroups == 0)
2263 cnt = MAX(1, MINALLOCSIZE /
sizeof(gid_t));
2265 cnt = cr->cr_agroups * 2;
2270 cnt = roundup2(n, PAGE_SIZE /
sizeof(gid_t));
2273 if (cr->cr_groups != cr->cr_smallgroups)
2274 free(cr->cr_groups, M_CRED);
2276 cr->cr_groups =
malloc(cnt *
sizeof(gid_t), M_CRED, M_WAITOK | M_ZERO);
2277 cr->cr_agroups = cnt;
2293 KASSERT(cr->cr_agroups >= ngrp, (
"cr_ngroups is too small"));
2295 bcopy(groups, cr->cr_groups, ngrp *
sizeof(gid_t));
2296 cr->cr_ngroups = ngrp;
2306 for (i = 2; i < ngrp; i++) {
2307 g = cr->cr_groups[i];
2308 for (j = i-1; j >= 1 && g < cr->cr_groups[j]; j--)
2309 cr->cr_groups[j + 1] = cr->cr_groups[j];
2310 cr->cr_groups[j + 1] = g;
2332#ifndef _SYS_SYSPROTO_H_
2342 char login[MAXLOGNAME];
2343 struct proc *p = td->td_proc;
2346 if (uap->
namelen > MAXLOGNAME)
2349 SESS_LOCK(p->p_session);
2350 len = strlcpy(login, p->p_session->s_login, uap->
namelen) + 1;
2351 SESS_UNLOCK(p->p_session);
2355 return (copyout(login, uap->
namebuf, len));
2361#ifndef _SYS_SYSPROTO_H_
2370 struct proc *p = td->td_proc;
2372 char logintmp[MAXLOGNAME];
2374 CTASSERT(
sizeof(p->p_session->s_login) >=
sizeof(logintmp));
2379 error = copyinstr(uap->
namebuf, logintmp,
sizeof(logintmp), NULL);
2381 if (error == ENAMETOOLONG)
2385 AUDIT_ARG_LOGIN(logintmp);
2387 SESS_LOCK(p->p_session);
2388 strcpy(p->p_session->s_login, logintmp);
2389 SESS_UNLOCK(p->p_session);
2398 PROC_LOCK_ASSERT(p, MA_OWNED);
2399 p->p_flag |= P_SUGID;
2412 newcred->cr_uid = euip->ui_uid;
2414 uifree(newcred->cr_uidinfo);
2415 newcred->cr_uidinfo = euip;
2428 newcred->cr_groups[0] = egid;
2443 (void)
chgproccnt(newcred->cr_ruidinfo, -1, 0);
2444 newcred->cr_ruid = ruip->ui_uid;
2446 uifree(newcred->cr_ruidinfo);
2447 newcred->cr_ruidinfo = ruip;
2448 (void)
chgproccnt(newcred->cr_ruidinfo, 1, 0);
2461 newcred->cr_rgid = rgid;
2474 newcred->cr_svuid = svuid;
2487 newcred->cr_svgid = svgid;
2493 "Deny ptrace(2) use by returning ENOSYS");
const struct cf_level * level
CTASSERT(MAXSHELLCMDLEN >=MAXINTERP+3)
FEATURE(kdtrace_hooks, "Kernel DTrace hooks which are required to load DTrace kernel modules")
void prison_hold(struct prison *pr)
void prison_set_allow(struct ucred *cred, unsigned flag, int enable)
int prison_check(struct ucred *cred1, struct ucred *cred2)
int prison_allow(struct ucred *cred, unsigned flag)
void prison_free(struct prison *pr)
void loginclass_free(struct loginclass *lc)
void *() malloc(size_t size, struct malloc_type *mtp, int flags)
void free(void *addr, struct malloc_type *mtp)
int priv_check_cred(struct ucred *cred, int priv)
int priv_check(struct thread *td, int priv)
int inferior(struct proc *p)
struct sx __exclusive_cache_line proctree_lock
int enterthispgrp(struct proc *p, struct pgrp *pgrp)
struct proc * pfind(pid_t pid)
int enterpgrp(struct proc *p, pid_t pgid, struct pgrp *pgrp, struct session *sess)
struct pgrp * pgfind(pid_t pgid)
int p_canwait(struct thread *td, struct proc *p)
struct ucred * crget(void)
struct ucred * crdup(struct ucred *cr)
void setsugid(struct proc *p)
void change_egid(struct ucred *newcred, gid_t egid)
void change_euid(struct ucred *newcred, struct uidinfo *euip)
void cru2x(struct ucred *cr, struct xucred *xcr)
void crcopy(struct ucred *dest, struct ucred *src)
void change_svuid(struct ucred *newcred, uid_t svuid)
int kern_setgroups(struct thread *td, u_int ngrp, gid_t *groups)
static int see_other_uids
int p_cansched(struct thread *td, struct proc *p)
int sys_setpgid(struct thread *td, struct setpgid_args *uap)
int sys_getppid(struct thread *td, struct getppid_args *uap)
static int sysctl_unprivileged_proc_debug(SYSCTL_HANDLER_ARGS)
static MALLOC_DEFINE(M_CRED, "cred", "credentials")
void proc_unset_cred(struct proc *p)
static struct ucred * crunuse(struct thread *td)
static void crfree_final(struct ucred *cr)
#define POSIX_APPENDIX_B_4_2_2
int p_cansee(struct thread *td, struct proc *p)
SYSCTL_NODE(_security, OID_AUTO, bsd, CTLFLAG_RW|CTLFLAG_MPSAFE, 0, "BSD security policy")
int sys_getpid(struct thread *td, struct getpid_args *uap)
static void crsetgroups_locked(struct ucred *cr, int ngrp, gid_t *groups)
static int conservative_signals
struct ucred * crcowget(struct ucred *cr)
int sys_issetugid(struct thread *td, struct issetugid_args *uap)
static void crunusebatch(struct ucred *cr, int users, int ref)
int sys_setgroups(struct thread *td, struct setgroups_args *uap)
SYSCTL_BOOL(_security_bsd, OID_AUTO, allow_ptrace, CTLFLAG_RWTUN, &allow_ptrace, 0, "Deny ptrace(2) use by returning ENOSYS")
void change_svgid(struct ucred *newcred, gid_t svgid)
int cr_canseeotheruids(struct ucred *u1, struct ucred *u2)
int p_candebug(struct thread *td, struct proc *p)
void proc_set_cred(struct proc *p, struct ucred *newcred)
int sys_setresgid(struct thread *td, struct setresgid_args *uap)
int cr_canseesocket(struct ucred *cred, struct socket *so)
int sys_getpgrp(struct thread *td, struct getpgrp_args *uap)
int sys_setlogin(struct thread *td, struct setlogin_args *uap)
int sys_getegid(struct thread *td, struct getegid_args *uap)
SYSCTL_PROC(_security_bsd, OID_AUTO, unprivileged_proc_debug, CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_PRISON|CTLFLAG_SECURE|CTLFLAG_MPSAFE, 0, 0, sysctl_unprivileged_proc_debug, "I", "Unprivileged processes may use process debugging facilities")
int sys_setuid(struct thread *td, struct setuid_args *uap)
int sys___setugid(struct thread *td, struct __setugid_args *uap)
int cr_cansignal(struct ucred *cred, struct proc *proc, int signum)
void credbatch_add(struct credbatch *crb, struct thread *td)
int securelevel_gt(struct ucred *cr, int level)
int groupmember(gid_t gid, struct ucred *cred)
int sys_geteuid(struct thread *td, struct geteuid_args *uap)
int sys_getsid(struct thread *td, struct getsid_args *uap)
int kern_getppid(struct thread *td)
int sys_getpgid(struct thread *td, struct getpgid_args *uap)
int sys_seteuid(struct thread *td, struct seteuid_args *uap)
int sys_getuid(struct thread *td, struct getuid_args *uap)
int sys_getgid(struct thread *td, struct getgid_args *uap)
int sys_setsid(struct thread *td, struct setsid_args *uap)
int sys_setreuid(struct thread *td, struct setreuid_args *uap)
void proc_set_cred_init(struct proc *p, struct ucred *newcred)
void credbatch_final(struct credbatch *crb)
SYSCTL_INT(_security_bsd, OID_AUTO, see_other_uids, CTLFLAG_RW, &see_other_uids, 0, "Unprivileged processes may see subjects/objects with different real uid")
struct ucred * crcopysafe(struct proc *p, struct ucred *cr)
int sys_setresuid(struct thread *td, struct setresuid_args *uap)
struct ucred * crcowsync(void)
void change_rgid(struct ucred *newcred, gid_t rgid)
void crextend(struct ucred *cr, int n)
struct ucred * crhold(struct ucred *cr)
int kern_getsid(struct thread *td, pid_t pid)
void change_ruid(struct ucred *newcred, struct uidinfo *ruip)
int sys_getresgid(struct thread *td, struct getresgid_args *uap)
void crfree(struct ucred *cr)
void cru2xt(struct thread *td, struct xucred *xcr)
int sys_getresuid(struct thread *td, struct getresuid_args *uap)
int cr_canseejailproc(struct ucred *u1, struct ucred *u2)
int sys_setregid(struct thread *td, struct setregid_args *uap)
int sys_setgid(struct thread *td, struct setgid_args *uap)
int cr_cansee(struct ucred *u1, struct ucred *u2)
int sys_getlogin(struct thread *td, struct getlogin_args *uap)
int sys_setegid(struct thread *td, struct setegid_args *uap)
void crcowfree(struct thread *td)
int p_cansignal(struct thread *td, struct proc *p, int signum)
int securelevel_ge(struct ucred *cr, int level)
int cr_canseeothergids(struct ucred *u1, struct ucred *u2)
int sys_getgroups(struct thread *td, struct getgroups_args *uap)
static int see_other_gids
void crsetgroups(struct ucred *cr, int ngrp, gid_t *groups)
void uifree(struct uidinfo *uip)
void uihold(struct uidinfo *uip)
struct uidinfo * uifind(uid_t uid)
int chgproccnt(struct uidinfo *uip, int diff, rlim_t max)
int sysctl_handle_int(SYSCTL_HANDLER_ARGS)
struct intr_irqsrc ** src