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.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)kern_prot.c 8.6 (Berkeley) 1/21/94
35 * $FreeBSD: src/sys/kern/kern_prot.c,v 1.53.2.9 2002/03/09 05:20:26 dd Exp $
39 * System calls related to processes and protection
42 #include "opt_compat.h"
44 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/sysproto.h>
48 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/pioctl.h>
54 #include <sys/resourcevar.h>
56 #include <sys/lockf.h>
57 #include <sys/spinlock.h>
59 #include <sys/thread2.h>
60 #include <sys/spinlock2.h>
62 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
65 sys_getpid(struct getpid_args *uap)
67 struct proc *p = curproc;
69 uap->sysmsg_fds[0] = p->p_pid;
70 #if defined(COMPAT_43)
71 lwkt_gettoken(&proc_token);
72 uap->sysmsg_fds[1] = p->p_pptr->p_pid;
73 lwkt_reltoken(&proc_token);
79 sys_getppid(struct getppid_args *uap)
81 struct proc *p = curproc;
83 lwkt_gettoken(&proc_token);
84 uap->sysmsg_result = p->p_pptr->p_pid;
85 lwkt_reltoken(&proc_token);
94 sys_lwp_gettid(struct lwp_gettid_args *uap)
96 struct lwp *lp = curthread->td_lwp;
97 uap->sysmsg_result = lp->lwp_tid;
102 * Get process group ID; note that POSIX getpgrp takes no parameter
107 sys_getpgrp(struct getpgrp_args *uap)
109 struct proc *p = curproc;
111 uap->sysmsg_result = p->p_pgrp->pg_id;
116 * Get an arbitrary pid's process group id
119 sys_getpgid(struct getpgid_args *uap)
121 struct proc *p = curproc;
131 pt = pfind(uap->pid);
135 /* XXX MPSAFE on pgrp? */
137 uap->sysmsg_result = pt->p_pgrp->pg_id;
144 * Get an arbitrary pid's session id.
147 sys_getsid(struct getsid_args *uap)
149 struct proc *p = curproc;
159 pt = pfind(uap->pid);
164 uap->sysmsg_result = pt->p_session->s_sid;
177 sys_getuid(struct getuid_args *uap)
179 struct ucred *cred = curthread->td_ucred;
181 uap->sysmsg_fds[0] = cred->cr_ruid;
182 #if defined(COMPAT_43)
183 uap->sysmsg_fds[1] = cred->cr_uid;
194 sys_geteuid(struct geteuid_args *uap)
196 struct ucred *cred = curthread->td_ucred;
198 uap->sysmsg_result = cred->cr_uid;
208 sys_getgid(struct getgid_args *uap)
210 struct ucred *cred = curthread->td_ucred;
212 uap->sysmsg_fds[0] = cred->cr_rgid;
213 #if defined(COMPAT_43)
214 uap->sysmsg_fds[1] = cred->cr_groups[0];
220 * Get effective group ID. The "egid" is groups[0], and could be obtained
221 * via getgroups. This syscall exists because it is somewhat painful to do
222 * correctly in a library function.
227 sys_getegid(struct getegid_args *uap)
229 struct ucred *cred = curthread->td_ucred;
231 uap->sysmsg_result = cred->cr_groups[0];
239 sys_getgroups(struct getgroups_args *uap)
245 cr = curthread->td_ucred;
246 if ((ngrp = uap->gidsetsize) == 0) {
247 uap->sysmsg_result = cr->cr_ngroups;
250 if (ngrp < cr->cr_ngroups)
252 ngrp = cr->cr_ngroups;
253 error = copyout((caddr_t)cr->cr_groups,
254 (caddr_t)uap->gidset, ngrp * sizeof(gid_t));
256 uap->sysmsg_result = ngrp;
261 sys_setsid(struct setsid_args *uap)
263 struct proc *p = curproc;
264 struct pgrp *pg = NULL;
267 lwkt_gettoken(&p->p_token);
268 if (p->p_pgid == p->p_pid || (pg = pgfind(p->p_pid)) != NULL) {
273 enterpgrp(p, p->p_pid, 1);
274 uap->sysmsg_result = p->p_pid;
277 lwkt_reltoken(&p->p_token);
282 * set process group (setpgid/old setpgrp)
284 * caller does setpgid(targpid, targpgid)
286 * pid must be caller or child of caller (ESRCH)
288 * pid must be in same session (EPERM)
289 * pid can't have done an exec (EACCES)
291 * there must exist some pid in same session having pgid (EPERM)
292 * pid must not be session leader (EPERM)
295 sys_setpgid(struct setpgid_args *uap)
297 struct proc *curp = curproc;
298 struct proc *targp; /* target process */
299 struct pgrp *pgrp = NULL; /* target pgrp */
305 if (uap->pid != 0 && uap->pid != curp->p_pid) {
306 if ((targp = pfind(uap->pid)) == NULL || !inferior(targp)) {
313 lwkt_gettoken(&targp->p_token);
314 /* targp now referenced and its token is held */
316 if (targp->p_pgrp == NULL ||
317 targp->p_session != curp->p_session) {
321 if (targp->p_flags & P_EXEC) {
328 lwkt_gettoken(&targp->p_token);
330 if (SESS_LEADER(targp)) {
334 if (uap->pgid == 0) {
335 uap->pgid = targp->p_pid;
336 } else if (uap->pgid != targp->p_pid) {
337 if ((pgrp = pgfind(uap->pgid)) == NULL ||
338 pgrp->pg_session != curp->p_session) {
343 error = enterpgrp(targp, uap->pgid, 0);
348 lwkt_reltoken(&targp->p_token);
355 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
356 * compatible. It says that setting the uid/gid to euid/egid is a special
357 * case of "appropriate privilege". Once the rules are expanded out, this
358 * basically means that setuid(nnn) sets all three id's, in all permitted
359 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
360 * does not set the saved id - this is dangerous for traditional BSD
361 * programs. For this reason, we *really* do not want to set
362 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
364 #define POSIX_APPENDIX_B_4_2_2
367 sys_setuid(struct setuid_args *uap)
369 struct proc *p = curproc;
374 lwkt_gettoken(&proc_token);
378 * See if we have "permission" by POSIX 1003.1 rules.
380 * Note that setuid(geteuid()) is a special case of
381 * "appropriate privileges" in appendix B.4.2.2. We need
382 * to use this clause to be compatible with traditional BSD
383 * semantics. Basically, it means that "setuid(xx)" sets all
384 * three id's (assuming you have privs).
386 * Notes on the logic. We do things in three steps.
387 * 1: We determine if the euid is going to change, and do EPERM
388 * right away. We unconditionally change the euid later if this
389 * test is satisfied, simplifying that part of the logic.
390 * 2: We determine if the real and/or saved uid's are going to
391 * change. Determined by compile options.
392 * 3: Change euid last. (after tests in #2 for "appropriate privs")
395 if (uid != cr->cr_ruid && /* allow setuid(getuid()) */
396 #ifdef _POSIX_SAVED_IDS
397 uid != crc->cr_svuid && /* allow setuid(saved gid) */
399 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
400 uid != cr->cr_uid && /* allow setuid(geteuid()) */
402 (error = priv_check_cred(cr, PRIV_CRED_SETUID, 0)))
405 #ifdef _POSIX_SAVED_IDS
407 * Do we have "appropriate privileges" (are we root or uid == euid)
408 * If so, we are changing the real uid and/or saved uid.
411 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
414 priv_check_cred(cr, PRIV_CRED_SETUID, 0) == 0) /* we are using privs */
418 * Set the real uid and transfer proc count to new user.
420 if (uid != cr->cr_ruid) {
421 cr = change_ruid(uid);
427 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
428 * the security of seteuid() depends on it. B.4.2.2 says it
429 * is important that we should do this.
431 if (cr->cr_svuid != uid) {
432 cr = cratom(&p->p_ucred);
439 * In all permitted cases, we are changing the euid.
440 * Copy credentials so other references do not see our changes.
442 if (cr->cr_uid != uid) {
448 lwkt_reltoken(&proc_token);
453 sys_seteuid(struct seteuid_args *uap)
455 struct proc *p = curproc;
460 lwkt_gettoken(&proc_token);
463 if (euid != cr->cr_ruid && /* allow seteuid(getuid()) */
464 euid != cr->cr_svuid && /* allow seteuid(saved uid) */
465 (error = priv_check_cred(cr, PRIV_CRED_SETEUID, 0))) {
466 lwkt_reltoken(&proc_token);
471 * Everything's okay, do it. Copy credentials so other references do
472 * not see our changes.
474 if (cr->cr_uid != euid) {
478 lwkt_reltoken(&proc_token);
483 sys_setgid(struct setgid_args *uap)
485 struct proc *p = curproc;
490 lwkt_gettoken(&proc_token);
494 * See if we have "permission" by POSIX 1003.1 rules.
496 * Note that setgid(getegid()) is a special case of
497 * "appropriate privileges" in appendix B.4.2.2. We need
498 * to use this clause to be compatible with traditional BSD
499 * semantics. Basically, it means that "setgid(xx)" sets all
500 * three id's (assuming you have privs).
502 * For notes on the logic here, see setuid() above.
505 if (gid != cr->cr_rgid && /* allow setgid(getgid()) */
506 #ifdef _POSIX_SAVED_IDS
507 gid != cr->cr_svgid && /* allow setgid(saved gid) */
509 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
510 gid != cr->cr_groups[0] && /* allow setgid(getegid()) */
512 (error = priv_check_cred(cr, PRIV_CRED_SETGID, 0))) {
516 #ifdef _POSIX_SAVED_IDS
518 * Do we have "appropriate privileges" (are we root or gid == egid)
519 * If so, we are changing the real uid and saved gid.
522 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
523 gid == cr->cr_groups[0] ||
525 priv_check_cred(cr, PRIV_CRED_SETGID, 0) == 0) /* we are using privs */
531 if (cr->cr_rgid != gid) {
532 cr = cratom(&p->p_ucred);
539 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
540 * the security of setegid() depends on it. B.4.2.2 says it
541 * is important that we should do this.
543 if (cr->cr_svgid != gid) {
544 cr = cratom(&p->p_ucred);
550 * In all cases permitted cases, we are changing the egid.
551 * Copy credentials so other references do not see our changes.
553 if (cr->cr_groups[0] != gid) {
554 cr = cratom(&p->p_ucred);
555 cr->cr_groups[0] = gid;
560 lwkt_reltoken(&proc_token);
565 sys_setegid(struct setegid_args *uap)
567 struct proc *p = curproc;
572 lwkt_gettoken(&proc_token);
575 if (egid != cr->cr_rgid && /* allow setegid(getgid()) */
576 egid != cr->cr_svgid && /* allow setegid(saved gid) */
577 (error = priv_check_cred(cr, PRIV_CRED_SETEGID, 0))) {
580 if (cr->cr_groups[0] != egid) {
581 cr = cratom(&p->p_ucred);
582 cr->cr_groups[0] = egid;
587 lwkt_reltoken(&proc_token);
592 sys_setgroups(struct setgroups_args *uap)
594 struct proc *p = curproc;
599 lwkt_gettoken(&proc_token);
602 if ((error = priv_check_cred(cr, PRIV_CRED_SETGROUPS, 0)))
604 ngrp = uap->gidsetsize;
605 if (ngrp > NGROUPS) {
610 * XXX A little bit lazy here. We could test if anything has
611 * changed before cratom() and setting P_SUGID.
613 cr = cratom(&p->p_ucred);
616 * setgroups(0, NULL) is a legitimate way of clearing the
617 * groups vector on non-BSD systems (which generally do not
618 * have the egid in the groups[0]). We risk security holes
619 * when running non-BSD software if we do not do the same.
623 error = copyin(uap->gidset, cr->cr_groups,
624 ngrp * sizeof(gid_t));
627 cr->cr_ngroups = ngrp;
632 lwkt_reltoken(&proc_token);
637 sys_setreuid(struct setreuid_args *uap)
639 struct proc *p = curproc;
644 lwkt_gettoken(&proc_token);
649 if (((ruid != (uid_t)-1 && ruid != cr->cr_ruid && ruid != cr->cr_svuid) ||
650 (euid != (uid_t)-1 && euid != cr->cr_uid &&
651 euid != cr->cr_ruid && euid != cr->cr_svuid)) &&
652 (error = priv_check_cred(cr, PRIV_CRED_SETREUID, 0)) != 0) {
656 if (euid != (uid_t)-1 && cr->cr_uid != euid) {
657 cr = change_euid(euid);
660 if (ruid != (uid_t)-1 && cr->cr_ruid != ruid) {
661 cr = change_ruid(ruid);
664 if ((ruid != (uid_t)-1 || cr->cr_uid != cr->cr_ruid) &&
665 cr->cr_svuid != cr->cr_uid) {
666 cr = cratom(&p->p_ucred);
667 cr->cr_svuid = cr->cr_uid;
672 lwkt_reltoken(&proc_token);
677 sys_setregid(struct setregid_args *uap)
679 struct proc *p = curproc;
684 lwkt_gettoken(&proc_token);
689 if (((rgid != (gid_t)-1 && rgid != cr->cr_rgid && rgid != cr->cr_svgid) ||
690 (egid != (gid_t)-1 && egid != cr->cr_groups[0] &&
691 egid != cr->cr_rgid && egid != cr->cr_svgid)) &&
692 (error = priv_check_cred(cr, PRIV_CRED_SETREGID, 0)) != 0) {
696 if (egid != (gid_t)-1 && cr->cr_groups[0] != egid) {
697 cr = cratom(&p->p_ucred);
698 cr->cr_groups[0] = egid;
701 if (rgid != (gid_t)-1 && cr->cr_rgid != rgid) {
702 cr = cratom(&p->p_ucred);
706 if ((rgid != (gid_t)-1 || cr->cr_groups[0] != cr->cr_rgid) &&
707 cr->cr_svgid != cr->cr_groups[0]) {
708 cr = cratom(&p->p_ucred);
709 cr->cr_svgid = cr->cr_groups[0];
714 lwkt_reltoken(&proc_token);
719 * setresuid(ruid, euid, suid) is like setreuid except control over the
720 * saved uid is explicit.
723 sys_setresuid(struct setresuid_args *uap)
725 struct proc *p = curproc;
727 uid_t ruid, euid, suid;
730 lwkt_gettoken(&proc_token);
736 if (((ruid != (uid_t)-1 && ruid != cr->cr_ruid && ruid != cr->cr_svuid &&
737 ruid != cr->cr_uid) ||
738 (euid != (uid_t)-1 && euid != cr->cr_ruid && euid != cr->cr_svuid &&
739 euid != cr->cr_uid) ||
740 (suid != (uid_t)-1 && suid != cr->cr_ruid && suid != cr->cr_svuid &&
741 suid != cr->cr_uid)) &&
742 (error = priv_check_cred(cr, PRIV_CRED_SETRESUID, 0)) != 0) {
745 if (euid != (uid_t)-1 && cr->cr_uid != euid) {
746 cr = change_euid(euid);
749 if (ruid != (uid_t)-1 && cr->cr_ruid != ruid) {
750 cr = change_ruid(ruid);
753 if (suid != (uid_t)-1 && cr->cr_svuid != suid) {
754 cr = cratom(&p->p_ucred);
760 lwkt_reltoken(&proc_token);
765 * setresgid(rgid, egid, sgid) is like setregid except control over the
766 * saved gid is explicit.
769 sys_setresgid(struct setresgid_args *uap)
771 struct proc *p = curproc;
773 gid_t rgid, egid, sgid;
776 lwkt_gettoken(&proc_token);
781 if (((rgid != (gid_t)-1 && rgid != cr->cr_rgid && rgid != cr->cr_svgid &&
782 rgid != cr->cr_groups[0]) ||
783 (egid != (gid_t)-1 && egid != cr->cr_rgid && egid != cr->cr_svgid &&
784 egid != cr->cr_groups[0]) ||
785 (sgid != (gid_t)-1 && sgid != cr->cr_rgid && sgid != cr->cr_svgid &&
786 sgid != cr->cr_groups[0])) &&
787 (error = priv_check_cred(cr, PRIV_CRED_SETRESGID, 0)) != 0) {
791 if (egid != (gid_t)-1 && cr->cr_groups[0] != egid) {
792 cr = cratom(&p->p_ucred);
793 cr->cr_groups[0] = egid;
796 if (rgid != (gid_t)-1 && cr->cr_rgid != rgid) {
797 cr = cratom(&p->p_ucred);
801 if (sgid != (gid_t)-1 && cr->cr_svgid != sgid) {
802 cr = cratom(&p->p_ucred);
808 lwkt_reltoken(&proc_token);
813 sys_getresuid(struct getresuid_args *uap)
815 struct proc *p = curproc;
817 int error1 = 0, error2 = 0, error3 = 0;
819 lwkt_gettoken(&proc_token);
822 error1 = copyout((caddr_t)&cr->cr_ruid,
823 (caddr_t)uap->ruid, sizeof(cr->cr_ruid));
825 error2 = copyout((caddr_t)&cr->cr_uid,
826 (caddr_t)uap->euid, sizeof(cr->cr_uid));
828 error3 = copyout((caddr_t)&cr->cr_svuid,
829 (caddr_t)uap->suid, sizeof(cr->cr_svuid));
830 lwkt_reltoken(&proc_token);
831 return error1 ? error1 : (error2 ? error2 : error3);
838 sys_getresgid(struct getresgid_args *uap)
841 int error1 = 0, error2 = 0, error3 = 0;
843 cr = curthread->td_ucred;
845 error1 = copyout(&cr->cr_rgid, uap->rgid,
846 sizeof(cr->cr_rgid));
848 error2 = copyout(&cr->cr_groups[0], uap->egid,
849 sizeof(cr->cr_groups[0]));
851 error3 = copyout(&cr->cr_svgid, uap->sgid,
852 sizeof(cr->cr_svgid));
853 return error1 ? error1 : (error2 ? error2 : error3);
858 * NOTE: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
859 * we use P_SUGID because we consider changing the owners as
860 * "tainting" as well.
861 * This is significant for procs that start as root and "become"
862 * a user without an exec - programs cannot know *everything*
863 * that libc *might* have put in their data segment.
868 sys_issetugid(struct issetugid_args *uap)
870 uap->sysmsg_result = (curproc->p_flags & P_SUGID) ? 1 : 0;
875 * Check if gid is a member of the group set.
878 groupmember(gid_t gid, struct ucred *cred)
883 egp = &(cred->cr_groups[cred->cr_ngroups]);
884 for (gp = cred->cr_groups; gp < egp; gp++) {
892 * Test whether the specified credentials have the privilege
895 * A kernel thread without a process context is assumed to have
896 * the privilege in question. In situations where the caller always
897 * expect a cred to exist, the cred should be passed separately and
898 * priv_check_cred() should be used instead of priv_check().
900 * Returns 0 or error.
905 priv_check(struct thread *td, int priv)
907 if (td->td_lwp != NULL)
908 return priv_check_cred(td->td_ucred, priv, 0);
913 * Check a credential for privilege.
915 * A non-null credential is expected unless NULL_CRED_OKAY is set.
920 priv_check_cred(struct ucred *cred, int priv, int flags)
924 KASSERT(PRIV_VALID(priv), ("priv_check_cred: invalid privilege"));
926 KASSERT(cred != NULL || flags & NULL_CRED_OKAY,
927 ("priv_check_cred: NULL cred!"));
930 if (flags & NULL_CRED_OKAY)
935 if (cred->cr_uid != 0)
938 error = prison_priv_check(cred, priv);
942 /* NOTE: accounting for suser access (p_acflag/ASU) removed */
947 * Return zero if p1 can fondle p2, return errno (EPERM/ESRCH) otherwise.
950 p_trespass(struct ucred *cr1, struct ucred *cr2)
954 if (!PRISON_CHECK(cr1, cr2))
956 if (cr1->cr_ruid == cr2->cr_ruid)
958 if (cr1->cr_uid == cr2->cr_ruid)
960 if (cr1->cr_ruid == cr2->cr_uid)
962 if (cr1->cr_uid == cr2->cr_uid)
964 if (priv_check_cred(cr1, PRIV_PROC_TRESPASS, 0) == 0)
973 _crinit(struct ucred *cr)
982 crinit(struct ucred *cr)
984 bzero(cr, sizeof(*cr));
989 * Allocate a zeroed cred structure.
998 cr = kmalloc(sizeof(*cr), M_CRED, M_WAITOK|M_ZERO);
1004 * Claim another reference to a ucred structure. Can be used with special
1007 * It must be possible to call this routine with spinlocks held, meaning
1008 * that this routine itself cannot obtain a spinlock.
1013 crhold(struct ucred *cr)
1015 if (cr != NOCRED && cr != FSCRED)
1016 atomic_add_int(&cr->cr_ref, 1);
1021 * Drop a reference from the cred structure, free it if the reference count
1024 * NOTE: because we used atomic_add_int() above, without a spinlock, we
1025 * must also use atomic_subtract_int() below. A spinlock is required
1026 * in crfree() to handle multiple callers racing the refcount to 0.
1031 crfree(struct ucred *cr)
1033 if (cr->cr_ref <= 0)
1034 panic("Freeing already free credential! %p", cr);
1035 if (atomic_fetchadd_int(&cr->cr_ref, -1) == 1) {
1037 * Some callers of crget(), such as nfs_statfs(),
1038 * allocate a temporary credential, but don't
1039 * allocate a uidinfo structure.
1041 if (cr->cr_uidinfo != NULL) {
1042 uidrop(cr->cr_uidinfo);
1043 cr->cr_uidinfo = NULL;
1045 if (cr->cr_ruidinfo != NULL) {
1046 uidrop(cr->cr_ruidinfo);
1047 cr->cr_ruidinfo = NULL;
1051 * Destroy empty prisons
1054 prison_free(cr->cr_prison);
1055 cr->cr_prison = NULL; /* safety */
1057 kfree((caddr_t)cr, M_CRED);
1062 * Atomize a cred structure so it can be modified without polluting
1063 * other references to it.
1065 * MPSAFE (however, *pcr must be stable)
1068 cratom(struct ucred **pcr)
1070 struct ucred *oldcr;
1071 struct ucred *newcr;
1074 if (oldcr->cr_ref == 1)
1078 if (newcr->cr_uidinfo)
1079 uihold(newcr->cr_uidinfo);
1080 if (newcr->cr_ruidinfo)
1081 uihold(newcr->cr_ruidinfo);
1083 prison_hold(newcr->cr_prison);
1090 #if 0 /* no longer used but keep around for a little while */
1092 * Copy cred structure to a new one and free the old one.
1094 * MPSAFE (*cr must be stable)
1097 crcopy(struct ucred *cr)
1099 struct ucred *newcr;
1101 if (cr->cr_ref == 1)
1105 if (newcr->cr_uidinfo)
1106 uihold(newcr->cr_uidinfo);
1107 if (newcr->cr_ruidinfo)
1108 uihold(newcr->cr_ruidinfo);
1110 prison_hold(newcr->cr_prison);
1118 * Dup cred struct to a new held one.
1121 crdup(struct ucred *cr)
1123 struct ucred *newcr;
1127 if (newcr->cr_uidinfo)
1128 uihold(newcr->cr_uidinfo);
1129 if (newcr->cr_ruidinfo)
1130 uihold(newcr->cr_ruidinfo);
1132 prison_hold(newcr->cr_prison);
1138 * Fill in a struct xucred based on a struct ucred.
1141 cru2x(struct ucred *cr, struct xucred *xcr)
1144 bzero(xcr, sizeof(*xcr));
1145 xcr->cr_version = XUCRED_VERSION;
1146 xcr->cr_uid = cr->cr_uid;
1147 xcr->cr_ngroups = cr->cr_ngroups;
1148 bcopy(cr->cr_groups, xcr->cr_groups, sizeof(cr->cr_groups));
1152 * Get login name, if available.
1155 sys_getlogin(struct getlogin_args *uap)
1157 struct proc *p = curproc;
1158 char buf[MAXLOGNAME];
1161 if (uap->namelen > MAXLOGNAME) /* namelen is unsigned */
1162 uap->namelen = MAXLOGNAME;
1163 bzero(buf, sizeof(buf));
1164 lwkt_gettoken(&proc_token);
1165 bcopy(p->p_pgrp->pg_session->s_login, buf, uap->namelen);
1166 lwkt_reltoken(&proc_token);
1168 error = copyout(buf, uap->namebuf, uap->namelen);
1176 sys_setlogin(struct setlogin_args *uap)
1178 struct thread *td = curthread;
1181 char buf[MAXLOGNAME];
1184 cred = td->td_ucred;
1187 if ((error = priv_check_cred(cred, PRIV_PROC_SETLOGIN, 0)))
1189 bzero(buf, sizeof(buf));
1190 error = copyinstr(uap->namebuf, buf, sizeof(buf), NULL);
1191 if (error == ENAMETOOLONG)
1194 lwkt_gettoken(&proc_token);
1195 memcpy(p->p_pgrp->pg_session->s_login, buf, sizeof(buf));
1196 lwkt_reltoken(&proc_token);
1204 struct proc *p = curproc;
1206 KKASSERT(p != NULL);
1207 lwkt_gettoken(&p->p_token);
1208 p->p_flags |= P_SUGID;
1209 if (!(p->p_pfsflags & PF_ISUGID))
1211 lwkt_reltoken(&p->p_token);
1215 * Helper function to change the effective uid of a process
1218 change_euid(uid_t euid)
1220 struct proc *p = curproc;
1223 KKASSERT(p != NULL);
1224 lf_count_adjust(p, 0);
1225 cr = cratom(&p->p_ucred);
1227 uireplace(&cr->cr_uidinfo, uifind(euid));
1228 lf_count_adjust(p, 1);
1233 * Helper function to change the real uid of a process
1235 * The per-uid process count for this process is transfered from
1236 * the old uid to the new uid.
1239 change_ruid(uid_t ruid)
1241 struct proc *p = curproc;
1244 KKASSERT(p != NULL);
1246 cr = cratom(&p->p_ucred);
1247 chgproccnt(cr->cr_ruidinfo, -1, 0);
1249 uireplace(&cr->cr_ruidinfo, uifind(ruid));
1250 chgproccnt(cr->cr_ruidinfo, 1, 0);