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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)kern_prot.c 8.6 (Berkeley) 1/21/94
39 * $FreeBSD: src/sys/kern/kern_prot.c,v 1.53.2.9 2002/03/09 05:20:26 dd Exp $
40 * $DragonFly: src/sys/kern/kern_prot.c,v 1.5 2003/06/26 02:17:45 dillon Exp $
44 * System calls related to processes and protection
47 #include "opt_compat.h"
49 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/sysproto.h>
53 #include <sys/kernel.h>
55 #include <sys/malloc.h>
56 #include <sys/pioctl.h>
57 #include <sys/resourcevar.h>
60 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
62 #ifndef _SYS_SYSPROTO_H_
69 * NOT MP SAFE due to p_pptr access
73 getpid(struct getpid_args *uap)
75 struct proc *p = curproc;
77 p->p_retval[0] = p->p_pid;
78 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
79 p->p_retval[1] = p->p_pptr->p_pid;
84 #ifndef _SYS_SYSPROTO_H_
91 getppid(struct getppid_args *uap)
93 struct proc *p = curproc;
95 p->p_retval[0] = p->p_pptr->p_pid;
100 * Get process group ID; note that POSIX getpgrp takes no parameter
104 #ifndef _SYS_SYSPROTO_H_
105 struct getpgrp_args {
111 getpgrp(struct getpgrp_args *uap)
113 struct proc *p = curproc;
115 p->p_retval[0] = p->p_pgrp->pg_id;
119 /* Get an arbitary pid's process group id */
120 #ifndef _SYS_SYSPROTO_H_
121 struct getpgid_args {
127 getpgid(struct getpgid_args *uap)
129 struct proc *p = curproc;
136 if ((pt = pfind(uap->pid)) == 0)
139 p->p_retval[0] = pt->p_pgrp->pg_id;
144 * Get an arbitary pid's session id.
146 #ifndef _SYS_SYSPROTO_H_
153 getsid(struct getsid_args *uap)
155 struct proc *p = curproc;
162 if ((pt = pfind(uap->pid)) == 0)
165 p->p_retval[0] = pt->p_session->s_sid;
173 #ifndef _SYS_SYSPROTO_H_
181 getuid(struct getuid_args *uap)
183 struct proc *p = curproc;
185 p->p_retval[0] = p->p_ucred->cr_ruid;
186 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
187 p->p_retval[1] = p->p_ucred->cr_uid;
193 * geteuid() - MP SAFE
195 #ifndef _SYS_SYSPROTO_H_
196 struct geteuid_args {
203 geteuid(struct geteuid_args *uap)
205 struct proc *p = curproc;
207 p->p_retval[0] = p->p_ucred->cr_uid;
214 #ifndef _SYS_SYSPROTO_H_
222 getgid(struct getgid_args *uap)
224 struct proc *p = curproc;
226 p->p_retval[0] = p->p_ucred->cr_rgid;
227 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
228 p->p_retval[1] = p->p_ucred->cr_groups[0];
234 * Get effective group ID. The "egid" is groups[0], and could be obtained
235 * via getgroups. This syscall exists because it is somewhat painful to do
236 * correctly in a library function.
238 #ifndef _SYS_SYSPROTO_H_
239 struct getegid_args {
246 getegid(struct getegid_args *uap)
248 struct proc *p = curproc;
250 p->p_retval[0] = p->p_ucred->cr_groups[0];
254 #ifndef _SYS_SYSPROTO_H_
255 struct getgroups_args {
261 getgroups(struct getgroups_args *uap)
263 struct proc *p = curproc;
268 if (p == NULL) /* API enforcement */
272 if ((ngrp = uap->gidsetsize) == 0) {
273 p->p_retval[0] = cr->cr_ngroups;
276 if (ngrp < cr->cr_ngroups)
278 ngrp = cr->cr_ngroups;
279 if ((error = copyout((caddr_t)cr->cr_groups,
280 (caddr_t)uap->gidset, ngrp * sizeof(gid_t))))
282 p->p_retval[0] = ngrp;
286 #ifndef _SYS_SYSPROTO_H_
294 setsid(struct setsid_args *uap)
296 struct proc *p = curproc;
298 if (p->p_pgid == p->p_pid || pgfind(p->p_pid)) {
301 (void)enterpgrp(p, p->p_pid, 1);
302 p->p_retval[0] = p->p_pid;
308 * set process group (setpgid/old setpgrp)
310 * caller does setpgid(targpid, targpgid)
312 * pid must be caller or child of caller (ESRCH)
314 * pid must be in same session (EPERM)
315 * pid can't have done an exec (EACCES)
317 * there must exist some pid in same session having pgid (EPERM)
318 * pid must not be session leader (EPERM)
320 #ifndef _SYS_SYSPROTO_H_
321 struct setpgid_args {
322 int pid; /* target process id */
323 int pgid; /* target pgrp id */
328 setpgid(struct setpgid_args *uap)
330 struct proc *curp = curproc;
331 struct proc *targp; /* target process */
332 struct pgrp *pgrp; /* target pgrp */
336 if (uap->pid != 0 && uap->pid != curp->p_pid) {
337 if ((targp = pfind(uap->pid)) == 0 || !inferior(targp))
339 if (targp->p_pgrp == NULL || targp->p_session != curp->p_session)
341 if (targp->p_flag & P_EXEC)
345 if (SESS_LEADER(targp))
348 uap->pgid = targp->p_pid;
349 else if (uap->pgid != targp->p_pid)
350 if ((pgrp = pgfind(uap->pgid)) == 0 ||
351 pgrp->pg_session != curp->p_session)
353 return (enterpgrp(targp, uap->pgid, 0));
357 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
358 * compatable. It says that setting the uid/gid to euid/egid is a special
359 * case of "appropriate privilege". Once the rules are expanded out, this
360 * basically means that setuid(nnn) sets all three id's, in all permitted
361 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
362 * does not set the saved id - this is dangerous for traditional BSD
363 * programs. For this reason, we *really* do not want to set
364 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
366 #define POSIX_APPENDIX_B_4_2_2
368 #ifndef _SYS_SYSPROTO_H_
375 setuid(struct setuid_args *uap)
377 struct proc *p = curproc;
382 if (p == NULL) /* API enforcement */
387 * See if we have "permission" by POSIX 1003.1 rules.
389 * Note that setuid(geteuid()) is a special case of
390 * "appropriate privileges" in appendix B.4.2.2. We need
391 * to use this clause to be compatable with traditional BSD
392 * semantics. Basically, it means that "setuid(xx)" sets all
393 * three id's (assuming you have privs).
395 * Notes on the logic. We do things in three steps.
396 * 1: We determine if the euid is going to change, and do EPERM
397 * right away. We unconditionally change the euid later if this
398 * test is satisfied, simplifying that part of the logic.
399 * 2: We determine if the real and/or saved uid's are going to
400 * change. Determined by compile options.
401 * 3: Change euid last. (after tests in #2 for "appropriate privs")
404 if (uid != cr->cr_ruid && /* allow setuid(getuid()) */
405 #ifdef _POSIX_SAVED_IDS
406 uid != crc->cr_svuid && /* allow setuid(saved gid) */
408 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
409 uid != cr->cr_uid && /* allow setuid(geteuid()) */
411 (error = suser_cred(cr, PRISON_ROOT)))
414 #ifdef _POSIX_SAVED_IDS
416 * Do we have "appropriate privileges" (are we root or uid == euid)
417 * If so, we are changing the real uid and/or saved uid.
420 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
423 suser_cred(cr, PRISON_ROOT) == 0) /* we are using privs */
427 * Set the real uid and transfer proc count to new user.
429 if (uid != cr->cr_ruid) {
436 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
437 * the security of seteuid() depends on it. B.4.2.2 says it
438 * is important that we should do this.
440 if (cr->cr_svuid != uid) {
447 * In all permitted cases, we are changing the euid.
448 * Copy credentials so other references do not see our changes.
450 if (cr->cr_uid != uid) {
457 #ifndef _SYS_SYSPROTO_H_
458 struct seteuid_args {
464 seteuid(struct seteuid_args *uap)
466 struct proc *p = curproc;
471 if (p == NULL) /* API enforcement */
476 if (euid != cr->cr_ruid && /* allow seteuid(getuid()) */
477 euid != cr->cr_svuid && /* allow seteuid(saved uid) */
478 (error = suser_cred(cr, PRISON_ROOT)))
481 * Everything's okay, do it. Copy credentials so other references do
482 * not see our changes.
484 if (cr->cr_uid != euid) {
491 #ifndef _SYS_SYSPROTO_H_
498 setgid(struct setgid_args *uap)
500 struct proc *p = curproc;
505 if (p == NULL) /* API enforcement */
510 * See if we have "permission" by POSIX 1003.1 rules.
512 * Note that setgid(getegid()) is a special case of
513 * "appropriate privileges" in appendix B.4.2.2. We need
514 * to use this clause to be compatable with traditional BSD
515 * semantics. Basically, it means that "setgid(xx)" sets all
516 * three id's (assuming you have privs).
518 * For notes on the logic here, see setuid() above.
521 if (gid != cr->cr_rgid && /* allow setgid(getgid()) */
522 #ifdef _POSIX_SAVED_IDS
523 gid != cr->cr_svgid && /* allow setgid(saved gid) */
525 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
526 gid != cr->cr_groups[0] && /* allow setgid(getegid()) */
528 (error = suser_cred(cr, PRISON_ROOT)))
531 #ifdef _POSIX_SAVED_IDS
533 * Do we have "appropriate privileges" (are we root or gid == egid)
534 * If so, we are changing the real uid and saved gid.
537 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
538 gid == cr->cr_groups[0] ||
540 suser_cred(cr, PRISON_ROOT) == 0) /* we are using privs */
546 if (cr->cr_rgid != gid) {
553 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
554 * the security of setegid() depends on it. B.4.2.2 says it
555 * is important that we should do this.
557 if (cr->cr_svgid != gid) {
563 * In all cases permitted cases, we are changing the egid.
564 * Copy credentials so other references do not see our changes.
566 if (cr->cr_groups[0] != gid) {
567 cr = cratom(&p->p_ucred);
568 cr->cr_groups[0] = gid;
574 #ifndef _SYS_SYSPROTO_H_
575 struct setegid_args {
581 setegid(struct setegid_args *uap)
583 struct proc *p = curproc;
588 if (p == NULL) /* API enforcement */
593 if (egid != cr->cr_rgid && /* allow setegid(getgid()) */
594 egid != cr->cr_svgid && /* allow setegid(saved gid) */
595 (error = suser_cred(cr, PRISON_ROOT)))
597 if (cr->cr_groups[0] != egid) {
598 cr = cratom(&p->p_ucred);
599 cr->cr_groups[0] = egid;
605 #ifndef _SYS_SYSPROTO_H_
606 struct setgroups_args {
613 setgroups(struct setgroups_args *uap)
615 struct proc *p = curproc;
620 if (p == NULL) /* API enforcement */
624 if ((error = suser_cred(cr, PRISON_ROOT)))
626 ngrp = uap->gidsetsize;
630 * XXX A little bit lazy here. We could test if anything has
631 * changed before cratom() and setting P_SUGID.
633 cr = cratom(&p->p_ucred);
636 * setgroups(0, NULL) is a legitimate way of clearing the
637 * groups vector on non-BSD systems (which generally do not
638 * have the egid in the groups[0]). We risk security holes
639 * when running non-BSD software if we do not do the same.
643 if ((error = copyin((caddr_t)uap->gidset,
644 (caddr_t)cr->cr_groups, ngrp * sizeof(gid_t))))
646 cr->cr_ngroups = ngrp;
652 #ifndef _SYS_SYSPROTO_H_
653 struct setreuid_args {
660 setreuid(struct setreuid_args *uap)
662 struct proc *p = curproc;
667 if (p == NULL) /* API enforcement */
673 if (((ruid != (uid_t)-1 && ruid != cr->cr_ruid && ruid != cr->cr_svuid) ||
674 (euid != (uid_t)-1 && euid != cr->cr_uid &&
675 euid != cr->cr_ruid && euid != cr->cr_svuid)) &&
676 (error = suser_cred(cr, PRISON_ROOT)) != 0)
679 if (euid != (uid_t)-1 && cr->cr_uid != euid) {
683 if (ruid != (uid_t)-1 && cr->cr_ruid != ruid) {
687 if ((ruid != (uid_t)-1 || cr->cr_uid != cr->cr_ruid) &&
688 cr->cr_svuid != cr->cr_uid) {
689 cr = cratom(&p->p_ucred);
690 cr->cr_svuid = cr->cr_uid;
696 #ifndef _SYS_SYSPROTO_H_
697 struct setregid_args {
704 setregid(struct setregid_args *uap)
706 struct proc *p = curproc;
711 if (p == NULL) /* API enforcement */
717 if (((rgid != (gid_t)-1 && rgid != cr->cr_rgid && rgid != cr->cr_svgid) ||
718 (egid != (gid_t)-1 && egid != cr->cr_groups[0] &&
719 egid != cr->cr_rgid && egid != cr->cr_svgid)) &&
720 (error = suser_cred(cr, PRISON_ROOT)) != 0)
723 if (egid != (gid_t)-1 && cr->cr_groups[0] != egid) {
724 cr = cratom(&p->p_ucred);
725 cr->cr_groups[0] = egid;
728 if (rgid != (gid_t)-1 && cr->cr_rgid != rgid) {
729 cr = cratom(&p->p_ucred);
733 if ((rgid != (gid_t)-1 || cr->cr_groups[0] != cr->cr_rgid) &&
734 cr->cr_svgid != cr->cr_groups[0]) {
735 cr = cratom(&p->p_ucred);
736 cr->cr_svgid = cr->cr_groups[0];
743 * setresuid(ruid, euid, suid) is like setreuid except control over the
744 * saved uid is explicit.
747 #ifndef _SYS_SYSPROTO_H_
748 struct setresuid_args {
756 setresuid(struct setresuid_args *uap)
758 struct proc *p = curproc;
760 uid_t ruid, euid, suid;
767 if (((ruid != (uid_t)-1 && ruid != cr->cr_ruid && ruid != cr->cr_svuid &&
768 ruid != cr->cr_uid) ||
769 (euid != (uid_t)-1 && euid != cr->cr_ruid && euid != cr->cr_svuid &&
770 euid != cr->cr_uid) ||
771 (suid != (uid_t)-1 && suid != cr->cr_ruid && suid != cr->cr_svuid &&
772 suid != cr->cr_uid)) &&
773 (error = suser_cred(cr, PRISON_ROOT)) != 0)
775 if (euid != (uid_t)-1 && cr->cr_uid != euid) {
779 if (ruid != (uid_t)-1 && cr->cr_ruid != ruid) {
783 if (suid != (uid_t)-1 && cr->cr_svuid != suid) {
784 cr = cratom(&p->p_ucred);
792 * setresgid(rgid, egid, sgid) is like setregid except control over the
793 * saved gid is explicit.
796 #ifndef _SYS_SYSPROTO_H_
797 struct setresgid_args {
805 setresgid(struct setresgid_args *uap)
807 struct proc *p = curproc;
809 gid_t rgid, egid, sgid;
816 if (((rgid != (gid_t)-1 && rgid != cr->cr_rgid && rgid != cr->cr_svgid &&
817 rgid != cr->cr_groups[0]) ||
818 (egid != (gid_t)-1 && egid != cr->cr_rgid && egid != cr->cr_svgid &&
819 egid != cr->cr_groups[0]) ||
820 (sgid != (gid_t)-1 && sgid != cr->cr_rgid && sgid != cr->cr_svgid &&
821 sgid != cr->cr_groups[0])) &&
822 (error = suser_cred(cr, PRISON_ROOT)) != 0)
825 if (egid != (gid_t)-1 && cr->cr_groups[0] != egid) {
826 cr = cratom(&p->p_ucred);
827 cr->cr_groups[0] = egid;
830 if (rgid != (gid_t)-1 && cr->cr_rgid != rgid) {
831 cr = cratom(&p->p_ucred);
835 if (sgid != (gid_t)-1 && cr->cr_svgid != sgid) {
836 cr = cratom(&p->p_ucred);
843 #ifndef _SYS_SYSPROTO_H_
844 struct getresuid_args {
852 getresuid(struct getresuid_args *uap)
854 struct proc *p = curproc;
855 struct ucred *cr = p->p_ucred;
856 int error1 = 0, error2 = 0, error3 = 0;
859 error1 = copyout((caddr_t)&cr->cr_ruid,
860 (caddr_t)uap->ruid, sizeof(cr->cr_ruid));
862 error2 = copyout((caddr_t)&cr->cr_uid,
863 (caddr_t)uap->euid, sizeof(cr->cr_uid));
865 error3 = copyout((caddr_t)&cr->cr_svuid,
866 (caddr_t)uap->suid, sizeof(cr->cr_svuid));
867 return error1 ? error1 : (error2 ? error2 : error3);
870 #ifndef _SYS_SYSPROTO_H_
871 struct getresgid_args {
879 getresgid(struct getresgid_args *uap)
881 struct proc *p = curproc;
882 struct ucred *cr = p->p_ucred;
883 int error1 = 0, error2 = 0, error3 = 0;
886 error1 = copyout((caddr_t)&cr->cr_rgid,
887 (caddr_t)uap->rgid, sizeof(cr->cr_rgid));
889 error2 = copyout((caddr_t)&cr->cr_groups[0],
890 (caddr_t)uap->egid, sizeof(cr->cr_groups[0]));
892 error3 = copyout((caddr_t)&cr->cr_svgid,
893 (caddr_t)uap->sgid, sizeof(cr->cr_svgid));
894 return error1 ? error1 : (error2 ? error2 : error3);
898 #ifndef _SYS_SYSPROTO_H_
899 struct issetugid_args {
905 issetugid(struct issetugid_args *uap)
907 struct proc *p = curproc;
909 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
910 * we use P_SUGID because we consider changing the owners as
911 * "tainting" as well.
912 * This is significant for procs that start as root and "become"
913 * a user without an exec - programs cannot know *everything*
914 * that libc *might* have put in their data segment.
916 p->p_retval[0] = (p->p_flag & P_SUGID) ? 1 : 0;
921 * Check if gid is a member of the group set.
924 groupmember(gid_t gid, struct ucred *cred)
929 egp = &(cred->cr_groups[cred->cr_ngroups]);
930 for (gp = cred->cr_groups; gp < egp; gp++)
937 * Test whether the specified credentials imply "super-user"
938 * privilege; if so, and we have accounting info, set the flag
939 * indicating use of super-powers. A kernel thread without a process
940 * context is assumed to have super user capabilities. In situations
941 * where the caller always expect a cred to exist, the cred should be
942 * passed separately and suser_cred()should be used instead of suser().
944 * Returns 0 or error.
947 suser(struct thread *td)
949 struct proc *p = td->td_proc;
952 return suser_cred(p->p_ucred, 0);
959 suser_cred(struct ucred *cred, int flag)
961 KASSERT(cred != NULL, ("suser_cred: NULL cred!"));
963 if (cred->cr_uid != 0)
965 if (cred->cr_prison && !(flag & PRISON_ROOT))
967 /* NOTE: accounting for suser access (p_acflag/ASU) removed */
972 * Return zero if p1 can fondle p2, return errno (EPERM/ESRCH) otherwise.
975 p_trespass(struct ucred *cr1, struct ucred *cr2)
979 if (!PRISON_CHECK(cr1, cr2))
981 if (cr1->cr_ruid == cr2->cr_ruid)
983 if (cr1->cr_uid == cr2->cr_ruid)
985 if (cr1->cr_ruid == cr2->cr_uid)
987 if (cr1->cr_uid == cr2->cr_uid)
989 if (suser_cred(cr1, PRISON_ROOT) == 0)
995 * Allocate a zeroed cred structure.
1000 register struct ucred *cr;
1002 MALLOC(cr, struct ucred *, sizeof(*cr), M_CRED, M_WAITOK);
1003 bzero((caddr_t)cr, sizeof(*cr));
1009 * Claim another reference to a ucred structure. Can be used with special
1013 crhold(struct ucred *cr)
1015 if (cr != NOCRED && cr != FSCRED)
1021 * Free a cred structure.
1022 * Throws away space when ref count gets to 0.
1025 crfree(struct ucred *cr)
1027 if (cr->cr_ref == 0)
1028 panic("Freeing already free credential! %p", cr);
1030 if (--cr->cr_ref == 0) {
1032 * Some callers of crget(), such as nfs_statfs(),
1033 * allocate a temporary credential, but don't
1034 * allocate a uidinfo structure.
1036 if (cr->cr_uidinfo != NULL)
1037 uifree(cr->cr_uidinfo);
1038 if (cr->cr_ruidinfo != NULL)
1039 uifree(cr->cr_ruidinfo);
1042 * Destroy empty prisons
1044 if (cr->cr_prison && !--cr->cr_prison->pr_ref) {
1045 if (cr->cr_prison->pr_linux != NULL)
1046 FREE(cr->cr_prison->pr_linux, M_PRISON);
1047 FREE(cr->cr_prison, M_PRISON);
1049 cr->cr_prison = NULL; /* safety */
1051 FREE((caddr_t)cr, M_CRED);
1056 * Atomize a cred structure so it can be modified without polluting
1057 * other references to it.
1060 cratom(struct ucred **pcr)
1062 struct ucred *oldcr;
1063 struct ucred *newcr;
1066 if (oldcr->cr_ref == 1)
1070 if (newcr->cr_uidinfo)
1071 uihold(newcr->cr_uidinfo);
1072 if (newcr->cr_ruidinfo)
1073 uihold(newcr->cr_ruidinfo);
1074 if (newcr->cr_prison)
1075 ++newcr->cr_prison->pr_ref;
1082 #if 0 /* no longer used but keep around for a little while */
1084 * Copy cred structure to a new one and free the old one.
1087 crcopy(struct ucred *cr)
1089 struct ucred *newcr;
1091 if (cr->cr_ref == 1)
1095 if (newcr->cr_uidinfo)
1096 uihold(newcr->cr_uidinfo);
1097 if (newcr->cr_ruidinfo)
1098 uihold(newcr->cr_ruidinfo);
1099 if (newcr->cr_prison)
1100 ++newcr->cr_prison->pr_ref;
1108 * Dup cred struct to a new held one.
1114 struct ucred *newcr;
1118 if (newcr->cr_uidinfo)
1119 uihold(newcr->cr_uidinfo);
1120 if (newcr->cr_ruidinfo)
1121 uihold(newcr->cr_ruidinfo);
1122 if (newcr->cr_prison)
1123 ++newcr->cr_prison->pr_ref;
1129 * Fill in a struct xucred based on a struct ucred.
1137 bzero(xcr, sizeof(*xcr));
1138 xcr->cr_version = XUCRED_VERSION;
1139 xcr->cr_uid = cr->cr_uid;
1140 xcr->cr_ngroups = cr->cr_ngroups;
1141 bcopy(cr->cr_groups, xcr->cr_groups, sizeof(cr->cr_groups));
1145 * Get login name, if available.
1147 #ifndef _SYS_SYSPROTO_H_
1148 struct getlogin_args {
1155 getlogin(struct getlogin_args *uap)
1157 struct proc *p = curproc;
1159 if (uap->namelen > MAXLOGNAME)
1160 uap->namelen = MAXLOGNAME;
1161 return (copyout((caddr_t) p->p_pgrp->pg_session->s_login,
1162 (caddr_t) uap->namebuf, uap->namelen));
1168 #ifndef _SYS_SYSPROTO_H_
1169 struct setlogin_args {
1175 setlogin(struct setlogin_args *uap)
1177 struct proc *p = curproc;
1179 char logintmp[MAXLOGNAME];
1181 KKASSERT(p != NULL);
1182 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)))
1184 error = copyinstr((caddr_t) uap->namebuf, (caddr_t) logintmp,
1185 sizeof(logintmp), (size_t *)0);
1186 if (error == ENAMETOOLONG)
1189 (void) memcpy(p->p_pgrp->pg_session->s_login, logintmp,
1197 struct proc *p = curproc;
1199 KKASSERT(p != NULL);
1200 p->p_flag |= P_SUGID;
1201 if (!(p->p_pfsflags & PF_ISUGID))
1206 * Helper function to change the effective uid of a process
1209 change_euid(uid_t euid)
1211 struct proc *p = curproc;
1213 struct uidinfo *uip;
1215 KKASSERT(p != NULL);
1217 cr = cratom(&p->p_ucred);
1218 uip = cr->cr_uidinfo;
1220 cr->cr_uidinfo = uifind(euid);
1225 * Helper function to change the real uid of a process
1227 * The per-uid process count for this process is transfered from
1228 * the old uid to the new uid.
1231 change_ruid(uid_t ruid)
1233 struct proc *p = curproc;
1235 struct uidinfo *uip;
1237 KKASSERT(p != NULL);
1239 cr = cratom(&p->p_ucred);
1240 (void)chgproccnt(cr->cr_ruidinfo, -1, 0);
1241 uip = cr->cr_ruidinfo;
1242 /* It is assumed that pcred is not shared between processes */
1244 cr->cr_ruidinfo = uifind(ruid);
1245 (void)chgproccnt(cr->cr_ruidinfo, 1, 0);