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.21 2005/04/20 16:37:09 cpressey 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>
58 #include <sys/thread2.h>
60 #include <sys/lockf.h>
62 static MALLOC_DEFINE(M_CRED
, "cred", "credentials");
65 * NOT MP SAFE due to p_pptr access
69 getpid(struct getpid_args
*uap
)
71 struct proc
*p
= curproc
;
73 uap
->sysmsg_fds
[0] = p
->p_pid
;
74 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
75 uap
->sysmsg_fds
[1] = p
->p_pptr
->p_pid
;
82 getppid(struct getppid_args
*uap
)
84 struct proc
*p
= curproc
;
86 uap
->sysmsg_result
= p
->p_pptr
->p_pid
;
91 * Get process group ID; note that POSIX getpgrp takes no parameter
96 getpgrp(struct getpgrp_args
*uap
)
98 struct proc
*p
= curproc
;
100 uap
->sysmsg_result
= p
->p_pgrp
->pg_id
;
105 * Get an arbitary pid's process group id
108 getpgid(struct getpgid_args
*uap
)
110 struct proc
*p
= curproc
;
117 if ((pt
= pfind(uap
->pid
)) == 0)
120 uap
->sysmsg_result
= pt
->p_pgrp
->pg_id
;
125 * Get an arbitary pid's session id.
128 getsid(struct getsid_args
*uap
)
130 struct proc
*p
= curproc
;
137 if ((pt
= pfind(uap
->pid
)) == 0)
140 uap
->sysmsg_result
= pt
->p_session
->s_sid
;
150 getuid(struct getuid_args
*uap
)
152 struct proc
*p
= curproc
;
154 uap
->sysmsg_fds
[0] = p
->p_ucred
->cr_ruid
;
155 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
156 uap
->sysmsg_fds
[1] = p
->p_ucred
->cr_uid
;
162 * geteuid() - MP SAFE
166 geteuid(struct geteuid_args
*uap
)
168 struct proc
*p
= curproc
;
170 uap
->sysmsg_result
= p
->p_ucred
->cr_uid
;
179 getgid(struct getgid_args
*uap
)
181 struct proc
*p
= curproc
;
183 uap
->sysmsg_fds
[0] = p
->p_ucred
->cr_rgid
;
184 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
185 uap
->sysmsg_fds
[1] = p
->p_ucred
->cr_groups
[0];
191 * Get effective group ID. The "egid" is groups[0], and could be obtained
192 * via getgroups. This syscall exists because it is somewhat painful to do
193 * correctly in a library function.
197 getegid(struct getegid_args
*uap
)
199 struct proc
*p
= curproc
;
201 uap
->sysmsg_result
= p
->p_ucred
->cr_groups
[0];
206 getgroups(struct getgroups_args
*uap
)
208 struct proc
*p
= curproc
;
213 if (p
== NULL
) /* API enforcement */
217 if ((ngrp
= uap
->gidsetsize
) == 0) {
218 uap
->sysmsg_result
= cr
->cr_ngroups
;
221 if (ngrp
< cr
->cr_ngroups
)
223 ngrp
= cr
->cr_ngroups
;
224 if ((error
= copyout((caddr_t
)cr
->cr_groups
,
225 (caddr_t
)uap
->gidset
, ngrp
* sizeof(gid_t
))))
227 uap
->sysmsg_result
= ngrp
;
233 setsid(struct setsid_args
*uap
)
235 struct proc
*p
= curproc
;
237 if (p
->p_pgid
== p
->p_pid
|| pgfind(p
->p_pid
)) {
240 (void)enterpgrp(p
, p
->p_pid
, 1);
241 uap
->sysmsg_result
= p
->p_pid
;
247 * set process group (setpgid/old setpgrp)
249 * caller does setpgid(targpid, targpgid)
251 * pid must be caller or child of caller (ESRCH)
253 * pid must be in same session (EPERM)
254 * pid can't have done an exec (EACCES)
256 * there must exist some pid in same session having pgid (EPERM)
257 * pid must not be session leader (EPERM)
261 setpgid(struct setpgid_args
*uap
)
263 struct proc
*curp
= curproc
;
264 struct proc
*targp
; /* target process */
265 struct pgrp
*pgrp
; /* target pgrp */
269 if (uap
->pid
!= 0 && uap
->pid
!= curp
->p_pid
) {
270 if ((targp
= pfind(uap
->pid
)) == 0 || !inferior(targp
))
272 if (targp
->p_pgrp
== NULL
|| targp
->p_session
!= curp
->p_session
)
274 if (targp
->p_flag
& P_EXEC
)
278 if (SESS_LEADER(targp
))
281 uap
->pgid
= targp
->p_pid
;
282 else if (uap
->pgid
!= targp
->p_pid
)
283 if ((pgrp
= pgfind(uap
->pgid
)) == 0 ||
284 pgrp
->pg_session
!= curp
->p_session
)
286 return (enterpgrp(targp
, uap
->pgid
, 0));
290 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
291 * compatible. It says that setting the uid/gid to euid/egid is a special
292 * case of "appropriate privilege". Once the rules are expanded out, this
293 * basically means that setuid(nnn) sets all three id's, in all permitted
294 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
295 * does not set the saved id - this is dangerous for traditional BSD
296 * programs. For this reason, we *really* do not want to set
297 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
299 #define POSIX_APPENDIX_B_4_2_2
303 setuid(struct setuid_args
*uap
)
305 struct proc
*p
= curproc
;
310 if (p
== NULL
) /* API enforcement */
315 * See if we have "permission" by POSIX 1003.1 rules.
317 * Note that setuid(geteuid()) is a special case of
318 * "appropriate privileges" in appendix B.4.2.2. We need
319 * to use this clause to be compatible with traditional BSD
320 * semantics. Basically, it means that "setuid(xx)" sets all
321 * three id's (assuming you have privs).
323 * Notes on the logic. We do things in three steps.
324 * 1: We determine if the euid is going to change, and do EPERM
325 * right away. We unconditionally change the euid later if this
326 * test is satisfied, simplifying that part of the logic.
327 * 2: We determine if the real and/or saved uid's are going to
328 * change. Determined by compile options.
329 * 3: Change euid last. (after tests in #2 for "appropriate privs")
332 if (uid
!= cr
->cr_ruid
&& /* allow setuid(getuid()) */
333 #ifdef _POSIX_SAVED_IDS
334 uid
!= crc
->cr_svuid
&& /* allow setuid(saved gid) */
336 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
337 uid
!= cr
->cr_uid
&& /* allow setuid(geteuid()) */
339 (error
= suser_cred(cr
, PRISON_ROOT
)))
342 #ifdef _POSIX_SAVED_IDS
344 * Do we have "appropriate privileges" (are we root or uid == euid)
345 * If so, we are changing the real uid and/or saved uid.
348 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
351 suser_cred(cr
, PRISON_ROOT
) == 0) /* we are using privs */
355 * Set the real uid and transfer proc count to new user.
357 if (uid
!= cr
->cr_ruid
) {
364 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
365 * the security of seteuid() depends on it. B.4.2.2 says it
366 * is important that we should do this.
368 if (cr
->cr_svuid
!= uid
) {
369 cr
= cratom(&p
->p_ucred
);
376 * In all permitted cases, we are changing the euid.
377 * Copy credentials so other references do not see our changes.
379 if (cr
->cr_uid
!= uid
) {
388 seteuid(struct seteuid_args
*uap
)
390 struct proc
*p
= curproc
;
395 if (p
== NULL
) /* API enforcement */
400 if (euid
!= cr
->cr_ruid
&& /* allow seteuid(getuid()) */
401 euid
!= cr
->cr_svuid
&& /* allow seteuid(saved uid) */
402 (error
= suser_cred(cr
, PRISON_ROOT
)))
405 * Everything's okay, do it. Copy credentials so other references do
406 * not see our changes.
408 if (cr
->cr_uid
!= euid
) {
417 setgid(struct setgid_args
*uap
)
419 struct proc
*p
= curproc
;
424 if (p
== NULL
) /* API enforcement */
429 * See if we have "permission" by POSIX 1003.1 rules.
431 * Note that setgid(getegid()) is a special case of
432 * "appropriate privileges" in appendix B.4.2.2. We need
433 * to use this clause to be compatible with traditional BSD
434 * semantics. Basically, it means that "setgid(xx)" sets all
435 * three id's (assuming you have privs).
437 * For notes on the logic here, see setuid() above.
440 if (gid
!= cr
->cr_rgid
&& /* allow setgid(getgid()) */
441 #ifdef _POSIX_SAVED_IDS
442 gid
!= cr
->cr_svgid
&& /* allow setgid(saved gid) */
444 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
445 gid
!= cr
->cr_groups
[0] && /* allow setgid(getegid()) */
447 (error
= suser_cred(cr
, PRISON_ROOT
)))
450 #ifdef _POSIX_SAVED_IDS
452 * Do we have "appropriate privileges" (are we root or gid == egid)
453 * If so, we are changing the real uid and saved gid.
456 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
457 gid
== cr
->cr_groups
[0] ||
459 suser_cred(cr
, PRISON_ROOT
) == 0) /* we are using privs */
465 if (cr
->cr_rgid
!= gid
) {
466 cr
= cratom(&p
->p_ucred
);
473 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
474 * the security of setegid() depends on it. B.4.2.2 says it
475 * is important that we should do this.
477 if (cr
->cr_svgid
!= gid
) {
478 cr
= cratom(&p
->p_ucred
);
484 * In all cases permitted cases, we are changing the egid.
485 * Copy credentials so other references do not see our changes.
487 if (cr
->cr_groups
[0] != gid
) {
488 cr
= cratom(&p
->p_ucred
);
489 cr
->cr_groups
[0] = gid
;
497 setegid(struct setegid_args
*uap
)
499 struct proc
*p
= curproc
;
504 if (p
== NULL
) /* API enforcement */
509 if (egid
!= cr
->cr_rgid
&& /* allow setegid(getgid()) */
510 egid
!= cr
->cr_svgid
&& /* allow setegid(saved gid) */
511 (error
= suser_cred(cr
, PRISON_ROOT
)))
513 if (cr
->cr_groups
[0] != egid
) {
514 cr
= cratom(&p
->p_ucred
);
515 cr
->cr_groups
[0] = egid
;
523 setgroups(struct setgroups_args
*uap
)
525 struct proc
*p
= curproc
;
530 if (p
== NULL
) /* API enforcement */
534 if ((error
= suser_cred(cr
, PRISON_ROOT
)))
536 ngrp
= uap
->gidsetsize
;
540 * XXX A little bit lazy here. We could test if anything has
541 * changed before cratom() and setting P_SUGID.
543 cr
= cratom(&p
->p_ucred
);
546 * setgroups(0, NULL) is a legitimate way of clearing the
547 * groups vector on non-BSD systems (which generally do not
548 * have the egid in the groups[0]). We risk security holes
549 * when running non-BSD software if we do not do the same.
553 if ((error
= copyin((caddr_t
)uap
->gidset
,
554 (caddr_t
)cr
->cr_groups
, ngrp
* sizeof(gid_t
))))
556 cr
->cr_ngroups
= ngrp
;
564 setreuid(struct setreuid_args
*uap
)
566 struct proc
*p
= curproc
;
571 if (p
== NULL
) /* API enforcement */
577 if (((ruid
!= (uid_t
)-1 && ruid
!= cr
->cr_ruid
&& ruid
!= cr
->cr_svuid
) ||
578 (euid
!= (uid_t
)-1 && euid
!= cr
->cr_uid
&&
579 euid
!= cr
->cr_ruid
&& euid
!= cr
->cr_svuid
)) &&
580 (error
= suser_cred(cr
, PRISON_ROOT
)) != 0)
583 if (euid
!= (uid_t
)-1 && cr
->cr_uid
!= euid
) {
587 if (ruid
!= (uid_t
)-1 && cr
->cr_ruid
!= ruid
) {
591 if ((ruid
!= (uid_t
)-1 || cr
->cr_uid
!= cr
->cr_ruid
) &&
592 cr
->cr_svuid
!= cr
->cr_uid
) {
593 cr
= cratom(&p
->p_ucred
);
594 cr
->cr_svuid
= cr
->cr_uid
;
602 setregid(struct setregid_args
*uap
)
604 struct proc
*p
= curproc
;
609 if (p
== NULL
) /* API enforcement */
615 if (((rgid
!= (gid_t
)-1 && rgid
!= cr
->cr_rgid
&& rgid
!= cr
->cr_svgid
) ||
616 (egid
!= (gid_t
)-1 && egid
!= cr
->cr_groups
[0] &&
617 egid
!= cr
->cr_rgid
&& egid
!= cr
->cr_svgid
)) &&
618 (error
= suser_cred(cr
, PRISON_ROOT
)) != 0)
621 if (egid
!= (gid_t
)-1 && cr
->cr_groups
[0] != egid
) {
622 cr
= cratom(&p
->p_ucred
);
623 cr
->cr_groups
[0] = egid
;
626 if (rgid
!= (gid_t
)-1 && cr
->cr_rgid
!= rgid
) {
627 cr
= cratom(&p
->p_ucred
);
631 if ((rgid
!= (gid_t
)-1 || cr
->cr_groups
[0] != cr
->cr_rgid
) &&
632 cr
->cr_svgid
!= cr
->cr_groups
[0]) {
633 cr
= cratom(&p
->p_ucred
);
634 cr
->cr_svgid
= cr
->cr_groups
[0];
641 * setresuid(ruid, euid, suid) is like setreuid except control over the
642 * saved uid is explicit.
647 setresuid(struct setresuid_args
*uap
)
649 struct proc
*p
= curproc
;
651 uid_t ruid
, euid
, suid
;
658 if (((ruid
!= (uid_t
)-1 && ruid
!= cr
->cr_ruid
&& ruid
!= cr
->cr_svuid
&&
659 ruid
!= cr
->cr_uid
) ||
660 (euid
!= (uid_t
)-1 && euid
!= cr
->cr_ruid
&& euid
!= cr
->cr_svuid
&&
661 euid
!= cr
->cr_uid
) ||
662 (suid
!= (uid_t
)-1 && suid
!= cr
->cr_ruid
&& suid
!= cr
->cr_svuid
&&
663 suid
!= cr
->cr_uid
)) &&
664 (error
= suser_cred(cr
, PRISON_ROOT
)) != 0)
666 if (euid
!= (uid_t
)-1 && cr
->cr_uid
!= euid
) {
670 if (ruid
!= (uid_t
)-1 && cr
->cr_ruid
!= ruid
) {
674 if (suid
!= (uid_t
)-1 && cr
->cr_svuid
!= suid
) {
675 cr
= cratom(&p
->p_ucred
);
683 * setresgid(rgid, egid, sgid) is like setregid except control over the
684 * saved gid is explicit.
689 setresgid(struct setresgid_args
*uap
)
691 struct proc
*p
= curproc
;
693 gid_t rgid
, egid
, sgid
;
700 if (((rgid
!= (gid_t
)-1 && rgid
!= cr
->cr_rgid
&& rgid
!= cr
->cr_svgid
&&
701 rgid
!= cr
->cr_groups
[0]) ||
702 (egid
!= (gid_t
)-1 && egid
!= cr
->cr_rgid
&& egid
!= cr
->cr_svgid
&&
703 egid
!= cr
->cr_groups
[0]) ||
704 (sgid
!= (gid_t
)-1 && sgid
!= cr
->cr_rgid
&& sgid
!= cr
->cr_svgid
&&
705 sgid
!= cr
->cr_groups
[0])) &&
706 (error
= suser_cred(cr
, PRISON_ROOT
)) != 0)
709 if (egid
!= (gid_t
)-1 && cr
->cr_groups
[0] != egid
) {
710 cr
= cratom(&p
->p_ucred
);
711 cr
->cr_groups
[0] = egid
;
714 if (rgid
!= (gid_t
)-1 && cr
->cr_rgid
!= rgid
) {
715 cr
= cratom(&p
->p_ucred
);
719 if (sgid
!= (gid_t
)-1 && cr
->cr_svgid
!= sgid
) {
720 cr
= cratom(&p
->p_ucred
);
729 getresuid(struct getresuid_args
*uap
)
731 struct proc
*p
= curproc
;
732 struct ucred
*cr
= p
->p_ucred
;
733 int error1
= 0, error2
= 0, error3
= 0;
736 error1
= copyout((caddr_t
)&cr
->cr_ruid
,
737 (caddr_t
)uap
->ruid
, sizeof(cr
->cr_ruid
));
739 error2
= copyout((caddr_t
)&cr
->cr_uid
,
740 (caddr_t
)uap
->euid
, sizeof(cr
->cr_uid
));
742 error3
= copyout((caddr_t
)&cr
->cr_svuid
,
743 (caddr_t
)uap
->suid
, sizeof(cr
->cr_svuid
));
744 return error1
? error1
: (error2
? error2
: error3
);
749 getresgid(struct getresgid_args
*uap
)
751 struct proc
*p
= curproc
;
752 struct ucred
*cr
= p
->p_ucred
;
753 int error1
= 0, error2
= 0, error3
= 0;
756 error1
= copyout((caddr_t
)&cr
->cr_rgid
,
757 (caddr_t
)uap
->rgid
, sizeof(cr
->cr_rgid
));
759 error2
= copyout((caddr_t
)&cr
->cr_groups
[0],
760 (caddr_t
)uap
->egid
, sizeof(cr
->cr_groups
[0]));
762 error3
= copyout((caddr_t
)&cr
->cr_svgid
,
763 (caddr_t
)uap
->sgid
, sizeof(cr
->cr_svgid
));
764 return error1
? error1
: (error2
? error2
: error3
);
770 issetugid(struct issetugid_args
*uap
)
772 struct proc
*p
= curproc
;
774 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
775 * we use P_SUGID because we consider changing the owners as
776 * "tainting" as well.
777 * This is significant for procs that start as root and "become"
778 * a user without an exec - programs cannot know *everything*
779 * that libc *might* have put in their data segment.
781 uap
->sysmsg_result
= (p
->p_flag
& P_SUGID
) ? 1 : 0;
786 * Check if gid is a member of the group set.
789 groupmember(gid_t gid
, struct ucred
*cred
)
794 egp
= &(cred
->cr_groups
[cred
->cr_ngroups
]);
795 for (gp
= cred
->cr_groups
; gp
< egp
; gp
++) {
803 * Test whether the specified credentials imply "super-user"
804 * privilege; if so, and we have accounting info, set the flag
805 * indicating use of super-powers. A kernel thread without a process
806 * context is assumed to have super user capabilities. In situations
807 * where the caller always expect a cred to exist, the cred should be
808 * passed separately and suser_cred()should be used instead of suser().
810 * Returns 0 or error.
813 suser(struct thread
*td
)
815 struct proc
*p
= td
->td_proc
;
818 return suser_cred(p
->p_ucred
, 0);
825 * A non-null credential is expected unless NULL_CRED_OKAY is set.
828 suser_cred(struct ucred
*cred
, int flag
)
830 KASSERT(cred
!= NULL
|| flag
& NULL_CRED_OKAY
,
831 ("suser_cred: NULL cred!"));
834 if (flag
& NULL_CRED_OKAY
)
839 if (cred
->cr_uid
!= 0)
841 if (cred
->cr_prison
&& !(flag
& PRISON_ROOT
))
843 /* NOTE: accounting for suser access (p_acflag/ASU) removed */
848 * Return zero if p1 can fondle p2, return errno (EPERM/ESRCH) otherwise.
851 p_trespass(struct ucred
*cr1
, struct ucred
*cr2
)
855 if (!PRISON_CHECK(cr1
, cr2
))
857 if (cr1
->cr_ruid
== cr2
->cr_ruid
)
859 if (cr1
->cr_uid
== cr2
->cr_ruid
)
861 if (cr1
->cr_ruid
== cr2
->cr_uid
)
863 if (cr1
->cr_uid
== cr2
->cr_uid
)
865 if (suser_cred(cr1
, PRISON_ROOT
) == 0)
871 * Allocate a zeroed cred structure.
878 MALLOC(cr
, struct ucred
*, sizeof(*cr
), M_CRED
, M_WAITOK
);
879 bzero((caddr_t
)cr
, sizeof(*cr
));
885 * Claim another reference to a ucred structure. Can be used with special
889 crhold(struct ucred
*cr
)
891 if (cr
!= NOCRED
&& cr
!= FSCRED
)
897 * Free a cred structure.
898 * Throws away space when ref count gets to 0.
902 crfree(struct ucred
*cr
)
904 /* Protect crfree() as a critical section as there
905 * appears to be a crfree race which can occur on
910 panic("Freeing already free credential! %p", cr
);
912 if (--cr
->cr_ref
== 0) {
914 * Some callers of crget(), such as nfs_statfs(),
915 * allocate a temporary credential, but don't
916 * allocate a uidinfo structure.
918 if (cr
->cr_uidinfo
!= NULL
) {
919 uidrop(cr
->cr_uidinfo
);
920 cr
->cr_uidinfo
= NULL
;
922 if (cr
->cr_ruidinfo
!= NULL
) {
923 uidrop(cr
->cr_ruidinfo
);
924 cr
->cr_ruidinfo
= NULL
;
928 * Destroy empty prisons
931 prison_free(cr
->cr_prison
);
932 cr
->cr_prison
= NULL
; /* safety */
934 FREE((caddr_t
)cr
, M_CRED
);
940 * Atomize a cred structure so it can be modified without polluting
941 * other references to it.
944 cratom(struct ucred
**pcr
)
950 if (oldcr
->cr_ref
== 1)
954 if (newcr
->cr_uidinfo
)
955 uihold(newcr
->cr_uidinfo
);
956 if (newcr
->cr_ruidinfo
)
957 uihold(newcr
->cr_ruidinfo
);
959 prison_hold(newcr
->cr_prison
);
966 #if 0 /* no longer used but keep around for a little while */
968 * Copy cred structure to a new one and free the old one.
971 crcopy(struct ucred
*cr
)
979 if (newcr
->cr_uidinfo
)
980 uihold(newcr
->cr_uidinfo
);
981 if (newcr
->cr_ruidinfo
)
982 uihold(newcr
->cr_ruidinfo
);
984 prison_hold(newcr
->cr_prison
);
992 * Dup cred struct to a new held one.
995 crdup(struct ucred
*cr
)
1001 if (newcr
->cr_uidinfo
)
1002 uihold(newcr
->cr_uidinfo
);
1003 if (newcr
->cr_ruidinfo
)
1004 uihold(newcr
->cr_ruidinfo
);
1006 prison_hold(newcr
->cr_prison
);
1012 * Fill in a struct xucred based on a struct ucred.
1015 cru2x(struct ucred
*cr
, struct xucred
*xcr
)
1018 bzero(xcr
, sizeof(*xcr
));
1019 xcr
->cr_version
= XUCRED_VERSION
;
1020 xcr
->cr_uid
= cr
->cr_uid
;
1021 xcr
->cr_ngroups
= cr
->cr_ngroups
;
1022 bcopy(cr
->cr_groups
, xcr
->cr_groups
, sizeof(cr
->cr_groups
));
1026 * Get login name, if available.
1030 getlogin(struct getlogin_args
*uap
)
1032 struct proc
*p
= curproc
;
1034 if (uap
->namelen
> MAXLOGNAME
)
1035 uap
->namelen
= MAXLOGNAME
;
1036 return (copyout((caddr_t
) p
->p_pgrp
->pg_session
->s_login
,
1037 (caddr_t
) uap
->namebuf
, uap
->namelen
));
1045 setlogin(struct setlogin_args
*uap
)
1047 struct proc
*p
= curproc
;
1049 char logintmp
[MAXLOGNAME
];
1051 KKASSERT(p
!= NULL
);
1052 if ((error
= suser_cred(p
->p_ucred
, PRISON_ROOT
)))
1054 error
= copyinstr((caddr_t
) uap
->namebuf
, (caddr_t
) logintmp
,
1055 sizeof(logintmp
), (size_t *)0);
1056 if (error
== ENAMETOOLONG
)
1059 (void) memcpy(p
->p_pgrp
->pg_session
->s_login
, logintmp
,
1067 struct proc
*p
= curproc
;
1069 KKASSERT(p
!= NULL
);
1070 p
->p_flag
|= P_SUGID
;
1071 if (!(p
->p_pfsflags
& PF_ISUGID
))
1076 * Helper function to change the effective uid of a process
1079 change_euid(uid_t euid
)
1081 struct proc
*p
= curproc
;
1084 KKASSERT(p
!= NULL
);
1085 lf_count_adjust(p
, 0);
1086 cr
= cratom(&p
->p_ucred
);
1088 uireplace(&cr
->cr_uidinfo
, uifind(euid
));
1089 lf_count_adjust(p
, 1);
1093 * Helper function to change the real uid of a process
1095 * The per-uid process count for this process is transfered from
1096 * the old uid to the new uid.
1099 change_ruid(uid_t ruid
)
1101 struct proc
*p
= curproc
;
1104 KKASSERT(p
!= NULL
);
1106 cr
= cratom(&p
->p_ucred
);
1107 (void)chgproccnt(cr
->cr_ruidinfo
, -1, 0);
1108 /* It is assumed that pcred is not shared between processes */
1110 uireplace(&cr
->cr_ruidinfo
, uifind(ruid
));
1111 (void)chgproccnt(cr
->cr_ruidinfo
, 1, 0);