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.29 2008/02/16 15:53:39 matthias 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>
56 #include <sys/malloc.h>
57 #include <sys/pioctl.h>
58 #include <sys/resourcevar.h>
60 #include <sys/lockf.h>
61 #include <sys/spinlock.h>
63 #include <sys/thread2.h>
64 #include <sys/spinlock2.h>
66 static MALLOC_DEFINE(M_CRED
, "cred", "credentials");
69 * NOT MP SAFE due to p_pptr access
73 sys_getpid(struct getpid_args
*uap
)
75 struct proc
*p
= curproc
;
77 uap
->sysmsg_fds
[0] = p
->p_pid
;
78 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
79 uap
->sysmsg_fds
[1] = p
->p_pptr
->p_pid
;
86 sys_getppid(struct getppid_args
*uap
)
88 struct proc
*p
= curproc
;
90 uap
->sysmsg_result
= p
->p_pptr
->p_pid
;
96 sys_lwp_gettid(struct lwp_gettid_args
*uap
)
98 struct lwp
*lp
= curthread
->td_lwp
;
100 uap
->sysmsg_result
= lp
->lwp_tid
;
105 * Get process group ID; note that POSIX getpgrp takes no parameter
110 sys_getpgrp(struct getpgrp_args
*uap
)
112 struct proc
*p
= curproc
;
114 uap
->sysmsg_result
= p
->p_pgrp
->pg_id
;
119 * Get an arbitrary pid's process group id
122 sys_getpgid(struct getpgid_args
*uap
)
124 struct proc
*p
= curproc
;
131 if ((pt
= pfind(uap
->pid
)) == 0)
134 uap
->sysmsg_result
= pt
->p_pgrp
->pg_id
;
139 * Get an arbitrary pid's session id.
142 sys_getsid(struct getsid_args
*uap
)
144 struct proc
*p
= curproc
;
151 if ((pt
= pfind(uap
->pid
)) == 0)
154 uap
->sysmsg_result
= pt
->p_session
->s_sid
;
164 sys_getuid(struct getuid_args
*uap
)
166 struct proc
*p
= curproc
;
168 uap
->sysmsg_fds
[0] = p
->p_ucred
->cr_ruid
;
169 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
170 uap
->sysmsg_fds
[1] = p
->p_ucred
->cr_uid
;
176 * geteuid() - MP SAFE
180 sys_geteuid(struct geteuid_args
*uap
)
182 struct proc
*p
= curproc
;
184 uap
->sysmsg_result
= p
->p_ucred
->cr_uid
;
193 sys_getgid(struct getgid_args
*uap
)
195 struct proc
*p
= curproc
;
197 uap
->sysmsg_fds
[0] = p
->p_ucred
->cr_rgid
;
198 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
199 uap
->sysmsg_fds
[1] = p
->p_ucred
->cr_groups
[0];
205 * Get effective group ID. The "egid" is groups[0], and could be obtained
206 * via getgroups. This syscall exists because it is somewhat painful to do
207 * correctly in a library function.
213 sys_getegid(struct getegid_args
*uap
)
215 struct proc
*p
= curproc
;
217 uap
->sysmsg_result
= p
->p_ucred
->cr_groups
[0];
222 sys_getgroups(struct getgroups_args
*uap
)
224 struct proc
*p
= curproc
;
229 if (p
== NULL
) /* API enforcement */
233 if ((ngrp
= uap
->gidsetsize
) == 0) {
234 uap
->sysmsg_result
= cr
->cr_ngroups
;
237 if (ngrp
< cr
->cr_ngroups
)
239 ngrp
= cr
->cr_ngroups
;
240 if ((error
= copyout((caddr_t
)cr
->cr_groups
,
241 (caddr_t
)uap
->gidset
, ngrp
* sizeof(gid_t
))))
243 uap
->sysmsg_result
= ngrp
;
249 sys_setsid(struct setsid_args
*uap
)
251 struct proc
*p
= curproc
;
253 if (p
->p_pgid
== p
->p_pid
|| pgfind(p
->p_pid
)) {
256 (void)enterpgrp(p
, p
->p_pid
, 1);
257 uap
->sysmsg_result
= p
->p_pid
;
263 * set process group (setpgid/old setpgrp)
265 * caller does setpgid(targpid, targpgid)
267 * pid must be caller or child of caller (ESRCH)
269 * pid must be in same session (EPERM)
270 * pid can't have done an exec (EACCES)
272 * there must exist some pid in same session having pgid (EPERM)
273 * pid must not be session leader (EPERM)
277 sys_setpgid(struct setpgid_args
*uap
)
279 struct proc
*curp
= curproc
;
280 struct proc
*targp
; /* target process */
281 struct pgrp
*pgrp
; /* target pgrp */
285 if (uap
->pid
!= 0 && uap
->pid
!= curp
->p_pid
) {
286 if ((targp
= pfind(uap
->pid
)) == 0 || !inferior(targp
))
288 if (targp
->p_pgrp
== NULL
|| targp
->p_session
!= curp
->p_session
)
290 if (targp
->p_flag
& P_EXEC
)
294 if (SESS_LEADER(targp
))
297 uap
->pgid
= targp
->p_pid
;
298 else if (uap
->pgid
!= targp
->p_pid
)
299 if ((pgrp
= pgfind(uap
->pgid
)) == 0 ||
300 pgrp
->pg_session
!= curp
->p_session
)
302 return (enterpgrp(targp
, uap
->pgid
, 0));
306 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
307 * compatible. It says that setting the uid/gid to euid/egid is a special
308 * case of "appropriate privilege". Once the rules are expanded out, this
309 * basically means that setuid(nnn) sets all three id's, in all permitted
310 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
311 * does not set the saved id - this is dangerous for traditional BSD
312 * programs. For this reason, we *really* do not want to set
313 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
315 #define POSIX_APPENDIX_B_4_2_2
319 sys_setuid(struct setuid_args
*uap
)
321 struct proc
*p
= curproc
;
326 if (p
== NULL
) /* API enforcement */
331 * See if we have "permission" by POSIX 1003.1 rules.
333 * Note that setuid(geteuid()) is a special case of
334 * "appropriate privileges" in appendix B.4.2.2. We need
335 * to use this clause to be compatible with traditional BSD
336 * semantics. Basically, it means that "setuid(xx)" sets all
337 * three id's (assuming you have privs).
339 * Notes on the logic. We do things in three steps.
340 * 1: We determine if the euid is going to change, and do EPERM
341 * right away. We unconditionally change the euid later if this
342 * test is satisfied, simplifying that part of the logic.
343 * 2: We determine if the real and/or saved uid's are going to
344 * change. Determined by compile options.
345 * 3: Change euid last. (after tests in #2 for "appropriate privs")
348 if (uid
!= cr
->cr_ruid
&& /* allow setuid(getuid()) */
349 #ifdef _POSIX_SAVED_IDS
350 uid
!= crc
->cr_svuid
&& /* allow setuid(saved gid) */
352 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
353 uid
!= cr
->cr_uid
&& /* allow setuid(geteuid()) */
355 (error
= suser_cred(cr
, PRISON_ROOT
)))
358 #ifdef _POSIX_SAVED_IDS
360 * Do we have "appropriate privileges" (are we root or uid == euid)
361 * If so, we are changing the real uid and/or saved uid.
364 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
367 suser_cred(cr
, PRISON_ROOT
) == 0) /* we are using privs */
371 * Set the real uid and transfer proc count to new user.
373 if (uid
!= cr
->cr_ruid
) {
374 cr
= change_ruid(uid
);
380 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
381 * the security of seteuid() depends on it. B.4.2.2 says it
382 * is important that we should do this.
384 if (cr
->cr_svuid
!= uid
) {
385 cr
= cratom(&p
->p_ucred
);
392 * In all permitted cases, we are changing the euid.
393 * Copy credentials so other references do not see our changes.
395 if (cr
->cr_uid
!= uid
) {
404 sys_seteuid(struct seteuid_args
*uap
)
406 struct proc
*p
= curproc
;
411 if (p
== NULL
) /* API enforcement */
416 if (euid
!= cr
->cr_ruid
&& /* allow seteuid(getuid()) */
417 euid
!= cr
->cr_svuid
&& /* allow seteuid(saved uid) */
418 (error
= suser_cred(cr
, PRISON_ROOT
)))
421 * Everything's okay, do it. Copy credentials so other references do
422 * not see our changes.
424 if (cr
->cr_uid
!= euid
) {
433 sys_setgid(struct setgid_args
*uap
)
435 struct proc
*p
= curproc
;
440 if (p
== NULL
) /* API enforcement */
445 * See if we have "permission" by POSIX 1003.1 rules.
447 * Note that setgid(getegid()) is a special case of
448 * "appropriate privileges" in appendix B.4.2.2. We need
449 * to use this clause to be compatible with traditional BSD
450 * semantics. Basically, it means that "setgid(xx)" sets all
451 * three id's (assuming you have privs).
453 * For notes on the logic here, see setuid() above.
456 if (gid
!= cr
->cr_rgid
&& /* allow setgid(getgid()) */
457 #ifdef _POSIX_SAVED_IDS
458 gid
!= cr
->cr_svgid
&& /* allow setgid(saved gid) */
460 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
461 gid
!= cr
->cr_groups
[0] && /* allow setgid(getegid()) */
463 (error
= suser_cred(cr
, PRISON_ROOT
)))
466 #ifdef _POSIX_SAVED_IDS
468 * Do we have "appropriate privileges" (are we root or gid == egid)
469 * If so, we are changing the real uid and saved gid.
472 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
473 gid
== cr
->cr_groups
[0] ||
475 suser_cred(cr
, PRISON_ROOT
) == 0) /* we are using privs */
481 if (cr
->cr_rgid
!= gid
) {
482 cr
= cratom(&p
->p_ucred
);
489 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
490 * the security of setegid() depends on it. B.4.2.2 says it
491 * is important that we should do this.
493 if (cr
->cr_svgid
!= gid
) {
494 cr
= cratom(&p
->p_ucred
);
500 * In all cases permitted cases, we are changing the egid.
501 * Copy credentials so other references do not see our changes.
503 if (cr
->cr_groups
[0] != gid
) {
504 cr
= cratom(&p
->p_ucred
);
505 cr
->cr_groups
[0] = gid
;
513 sys_setegid(struct setegid_args
*uap
)
515 struct proc
*p
= curproc
;
520 if (p
== NULL
) /* API enforcement */
525 if (egid
!= cr
->cr_rgid
&& /* allow setegid(getgid()) */
526 egid
!= cr
->cr_svgid
&& /* allow setegid(saved gid) */
527 (error
= suser_cred(cr
, PRISON_ROOT
)))
529 if (cr
->cr_groups
[0] != egid
) {
530 cr
= cratom(&p
->p_ucred
);
531 cr
->cr_groups
[0] = egid
;
539 sys_setgroups(struct setgroups_args
*uap
)
541 struct proc
*p
= curproc
;
546 if (p
== NULL
) /* API enforcement */
550 if ((error
= suser_cred(cr
, PRISON_ROOT
)))
552 ngrp
= uap
->gidsetsize
;
556 * XXX A little bit lazy here. We could test if anything has
557 * changed before cratom() and setting P_SUGID.
559 cr
= cratom(&p
->p_ucred
);
562 * setgroups(0, NULL) is a legitimate way of clearing the
563 * groups vector on non-BSD systems (which generally do not
564 * have the egid in the groups[0]). We risk security holes
565 * when running non-BSD software if we do not do the same.
569 if ((error
= copyin((caddr_t
)uap
->gidset
,
570 (caddr_t
)cr
->cr_groups
, ngrp
* sizeof(gid_t
))))
572 cr
->cr_ngroups
= ngrp
;
580 sys_setreuid(struct setreuid_args
*uap
)
582 struct proc
*p
= curproc
;
587 if (p
== NULL
) /* API enforcement */
593 if (((ruid
!= (uid_t
)-1 && ruid
!= cr
->cr_ruid
&& ruid
!= cr
->cr_svuid
) ||
594 (euid
!= (uid_t
)-1 && euid
!= cr
->cr_uid
&&
595 euid
!= cr
->cr_ruid
&& euid
!= cr
->cr_svuid
)) &&
596 (error
= suser_cred(cr
, PRISON_ROOT
)) != 0)
599 if (euid
!= (uid_t
)-1 && cr
->cr_uid
!= euid
) {
600 cr
= change_euid(euid
);
603 if (ruid
!= (uid_t
)-1 && cr
->cr_ruid
!= ruid
) {
604 cr
= change_ruid(ruid
);
607 if ((ruid
!= (uid_t
)-1 || cr
->cr_uid
!= cr
->cr_ruid
) &&
608 cr
->cr_svuid
!= cr
->cr_uid
) {
609 cr
= cratom(&p
->p_ucred
);
610 cr
->cr_svuid
= cr
->cr_uid
;
618 sys_setregid(struct setregid_args
*uap
)
620 struct proc
*p
= curproc
;
625 if (p
== NULL
) /* API enforcement */
631 if (((rgid
!= (gid_t
)-1 && rgid
!= cr
->cr_rgid
&& rgid
!= cr
->cr_svgid
) ||
632 (egid
!= (gid_t
)-1 && egid
!= cr
->cr_groups
[0] &&
633 egid
!= cr
->cr_rgid
&& egid
!= cr
->cr_svgid
)) &&
634 (error
= suser_cred(cr
, PRISON_ROOT
)) != 0)
637 if (egid
!= (gid_t
)-1 && cr
->cr_groups
[0] != egid
) {
638 cr
= cratom(&p
->p_ucred
);
639 cr
->cr_groups
[0] = egid
;
642 if (rgid
!= (gid_t
)-1 && cr
->cr_rgid
!= rgid
) {
643 cr
= cratom(&p
->p_ucred
);
647 if ((rgid
!= (gid_t
)-1 || cr
->cr_groups
[0] != cr
->cr_rgid
) &&
648 cr
->cr_svgid
!= cr
->cr_groups
[0]) {
649 cr
= cratom(&p
->p_ucred
);
650 cr
->cr_svgid
= cr
->cr_groups
[0];
657 * setresuid(ruid, euid, suid) is like setreuid except control over the
658 * saved uid is explicit.
663 sys_setresuid(struct setresuid_args
*uap
)
665 struct proc
*p
= curproc
;
667 uid_t ruid
, euid
, suid
;
674 if (((ruid
!= (uid_t
)-1 && ruid
!= cr
->cr_ruid
&& ruid
!= cr
->cr_svuid
&&
675 ruid
!= cr
->cr_uid
) ||
676 (euid
!= (uid_t
)-1 && euid
!= cr
->cr_ruid
&& euid
!= cr
->cr_svuid
&&
677 euid
!= cr
->cr_uid
) ||
678 (suid
!= (uid_t
)-1 && suid
!= cr
->cr_ruid
&& suid
!= cr
->cr_svuid
&&
679 suid
!= cr
->cr_uid
)) &&
680 (error
= suser_cred(cr
, PRISON_ROOT
)) != 0)
682 if (euid
!= (uid_t
)-1 && cr
->cr_uid
!= euid
) {
683 cr
= change_euid(euid
);
686 if (ruid
!= (uid_t
)-1 && cr
->cr_ruid
!= ruid
) {
687 cr
= change_ruid(ruid
);
690 if (suid
!= (uid_t
)-1 && cr
->cr_svuid
!= suid
) {
691 cr
= cratom(&p
->p_ucred
);
699 * setresgid(rgid, egid, sgid) is like setregid except control over the
700 * saved gid is explicit.
705 sys_setresgid(struct setresgid_args
*uap
)
707 struct proc
*p
= curproc
;
709 gid_t rgid
, egid
, sgid
;
716 if (((rgid
!= (gid_t
)-1 && rgid
!= cr
->cr_rgid
&& rgid
!= cr
->cr_svgid
&&
717 rgid
!= cr
->cr_groups
[0]) ||
718 (egid
!= (gid_t
)-1 && egid
!= cr
->cr_rgid
&& egid
!= cr
->cr_svgid
&&
719 egid
!= cr
->cr_groups
[0]) ||
720 (sgid
!= (gid_t
)-1 && sgid
!= cr
->cr_rgid
&& sgid
!= cr
->cr_svgid
&&
721 sgid
!= cr
->cr_groups
[0])) &&
722 (error
= suser_cred(cr
, PRISON_ROOT
)) != 0)
725 if (egid
!= (gid_t
)-1 && cr
->cr_groups
[0] != egid
) {
726 cr
= cratom(&p
->p_ucred
);
727 cr
->cr_groups
[0] = egid
;
730 if (rgid
!= (gid_t
)-1 && cr
->cr_rgid
!= rgid
) {
731 cr
= cratom(&p
->p_ucred
);
735 if (sgid
!= (gid_t
)-1 && cr
->cr_svgid
!= sgid
) {
736 cr
= cratom(&p
->p_ucred
);
745 sys_getresuid(struct getresuid_args
*uap
)
747 struct proc
*p
= curproc
;
748 struct ucred
*cr
= p
->p_ucred
;
749 int error1
= 0, error2
= 0, error3
= 0;
752 error1
= copyout((caddr_t
)&cr
->cr_ruid
,
753 (caddr_t
)uap
->ruid
, sizeof(cr
->cr_ruid
));
755 error2
= copyout((caddr_t
)&cr
->cr_uid
,
756 (caddr_t
)uap
->euid
, sizeof(cr
->cr_uid
));
758 error3
= copyout((caddr_t
)&cr
->cr_svuid
,
759 (caddr_t
)uap
->suid
, sizeof(cr
->cr_svuid
));
760 return error1
? error1
: (error2
? error2
: error3
);
765 sys_getresgid(struct getresgid_args
*uap
)
767 struct proc
*p
= curproc
;
768 struct ucred
*cr
= p
->p_ucred
;
769 int error1
= 0, error2
= 0, error3
= 0;
772 error1
= copyout((caddr_t
)&cr
->cr_rgid
,
773 (caddr_t
)uap
->rgid
, sizeof(cr
->cr_rgid
));
775 error2
= copyout((caddr_t
)&cr
->cr_groups
[0],
776 (caddr_t
)uap
->egid
, sizeof(cr
->cr_groups
[0]));
778 error3
= copyout((caddr_t
)&cr
->cr_svgid
,
779 (caddr_t
)uap
->sgid
, sizeof(cr
->cr_svgid
));
780 return error1
? error1
: (error2
? error2
: error3
);
786 sys_issetugid(struct issetugid_args
*uap
)
788 struct proc
*p
= curproc
;
790 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
791 * we use P_SUGID because we consider changing the owners as
792 * "tainting" as well.
793 * This is significant for procs that start as root and "become"
794 * a user without an exec - programs cannot know *everything*
795 * that libc *might* have put in their data segment.
797 uap
->sysmsg_result
= (p
->p_flag
& P_SUGID
) ? 1 : 0;
802 * Check if gid is a member of the group set.
805 groupmember(gid_t gid
, struct ucred
*cred
)
810 egp
= &(cred
->cr_groups
[cred
->cr_ngroups
]);
811 for (gp
= cred
->cr_groups
; gp
< egp
; gp
++) {
819 * Test whether the specified credentials imply "super-user"
820 * privilege; if so, and we have accounting info, set the flag
821 * indicating use of super-powers. A kernel thread without a process
822 * context is assumed to have super user capabilities. In situations
823 * where the caller always expect a cred to exist, the cred should be
824 * passed separately and suser_cred()should be used instead of suser().
826 * Returns 0 or error.
829 suser(struct thread
*td
)
831 struct proc
*p
= td
->td_proc
;
834 return suser_cred(p
->p_ucred
, 0);
841 * A non-null credential is expected unless NULL_CRED_OKAY is set.
844 suser_cred(struct ucred
*cred
, int flag
)
846 KASSERT(cred
!= NULL
|| flag
& NULL_CRED_OKAY
,
847 ("suser_cred: NULL cred!"));
850 if (flag
& NULL_CRED_OKAY
)
855 if (cred
->cr_uid
!= 0)
857 if (cred
->cr_prison
&& !(flag
& PRISON_ROOT
))
859 /* NOTE: accounting for suser access (p_acflag/ASU) removed */
864 * Return zero if p1 can fondle p2, return errno (EPERM/ESRCH) otherwise.
867 p_trespass(struct ucred
*cr1
, struct ucred
*cr2
)
871 if (!PRISON_CHECK(cr1
, cr2
))
873 if (cr1
->cr_ruid
== cr2
->cr_ruid
)
875 if (cr1
->cr_uid
== cr2
->cr_ruid
)
877 if (cr1
->cr_ruid
== cr2
->cr_uid
)
879 if (cr1
->cr_uid
== cr2
->cr_uid
)
881 if (suser_cred(cr1
, PRISON_ROOT
) == 0)
890 _crinit(struct ucred
*cr
)
892 bzero(cr
, sizeof(*cr
));
894 spin_init(&cr
->cr_spin
);
901 crinit(struct ucred
*cr
)
907 * Allocate a zeroed cred structure.
916 MALLOC(cr
, struct ucred
*, sizeof(*cr
), M_CRED
, M_WAITOK
);
922 * Claim another reference to a ucred structure. Can be used with special
925 * It must be possible to call this routine with spinlocks held, meaning
926 * that this routine itself cannot obtain a spinlock.
931 crhold(struct ucred
*cr
)
933 if (cr
!= NOCRED
&& cr
!= FSCRED
)
934 atomic_add_int(&cr
->cr_ref
, 1);
939 * Drop a reference from the cred structure, free it if the reference count
942 * NOTE: because we used atomic_add_int() above, without a spinlock, we
943 * must also use atomic_subtract_int() below. A spinlock is required
944 * in crfree() to handle multiple callers racing the refcount to 0.
946 * MPALMOSTSAFE - acquires mplock on 1->0 transition of ref count
949 crfree(struct ucred
*cr
)
952 panic("Freeing already free credential! %p", cr
);
953 spin_lock_wr(&cr
->cr_spin
);
954 atomic_subtract_int(&cr
->cr_ref
, 1);
955 if (cr
->cr_ref
== 0) {
956 spin_unlock_wr(&cr
->cr_spin
);
958 * Some callers of crget(), such as nfs_statfs(),
959 * allocate a temporary credential, but don't
960 * allocate a uidinfo structure.
963 if (cr
->cr_uidinfo
!= NULL
) {
964 uidrop(cr
->cr_uidinfo
);
965 cr
->cr_uidinfo
= NULL
;
967 if (cr
->cr_ruidinfo
!= NULL
) {
968 uidrop(cr
->cr_ruidinfo
);
969 cr
->cr_ruidinfo
= NULL
;
973 * Destroy empty prisons
976 prison_free(cr
->cr_prison
);
977 cr
->cr_prison
= NULL
; /* safety */
979 FREE((caddr_t
)cr
, M_CRED
);
982 spin_unlock_wr(&cr
->cr_spin
);
987 * Atomize a cred structure so it can be modified without polluting
988 * other references to it.
991 cratom(struct ucred
**pcr
)
997 if (oldcr
->cr_ref
== 1)
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
);
1013 #if 0 /* no longer used but keep around for a little while */
1015 * Copy cred structure to a new one and free the old one.
1018 crcopy(struct ucred
*cr
)
1020 struct ucred
*newcr
;
1022 if (cr
->cr_ref
== 1)
1026 if (newcr
->cr_uidinfo
)
1027 uihold(newcr
->cr_uidinfo
);
1028 if (newcr
->cr_ruidinfo
)
1029 uihold(newcr
->cr_ruidinfo
);
1031 prison_hold(newcr
->cr_prison
);
1039 * Dup cred struct to a new held one.
1042 crdup(struct ucred
*cr
)
1044 struct ucred
*newcr
;
1048 if (newcr
->cr_uidinfo
)
1049 uihold(newcr
->cr_uidinfo
);
1050 if (newcr
->cr_ruidinfo
)
1051 uihold(newcr
->cr_ruidinfo
);
1053 prison_hold(newcr
->cr_prison
);
1059 * Fill in a struct xucred based on a struct ucred.
1062 cru2x(struct ucred
*cr
, struct xucred
*xcr
)
1065 bzero(xcr
, sizeof(*xcr
));
1066 xcr
->cr_version
= XUCRED_VERSION
;
1067 xcr
->cr_uid
= cr
->cr_uid
;
1068 xcr
->cr_ngroups
= cr
->cr_ngroups
;
1069 bcopy(cr
->cr_groups
, xcr
->cr_groups
, sizeof(cr
->cr_groups
));
1073 * Get login name, if available.
1077 sys_getlogin(struct getlogin_args
*uap
)
1079 struct proc
*p
= curproc
;
1081 if (uap
->namelen
> MAXLOGNAME
)
1082 uap
->namelen
= MAXLOGNAME
;
1083 return (copyout((caddr_t
) p
->p_pgrp
->pg_session
->s_login
,
1084 (caddr_t
) uap
->namebuf
, uap
->namelen
));
1092 sys_setlogin(struct setlogin_args
*uap
)
1094 struct proc
*p
= curproc
;
1096 char logintmp
[MAXLOGNAME
];
1098 KKASSERT(p
!= NULL
);
1099 if ((error
= suser_cred(p
->p_ucred
, PRISON_ROOT
)))
1101 error
= copyinstr((caddr_t
) uap
->namebuf
, (caddr_t
) logintmp
,
1102 sizeof(logintmp
), (size_t *)0);
1103 if (error
== ENAMETOOLONG
)
1106 (void) memcpy(p
->p_pgrp
->pg_session
->s_login
, logintmp
,
1114 struct proc
*p
= curproc
;
1116 KKASSERT(p
!= NULL
);
1117 p
->p_flag
|= P_SUGID
;
1118 if (!(p
->p_pfsflags
& PF_ISUGID
))
1123 * Helper function to change the effective uid of a process
1126 change_euid(uid_t euid
)
1128 struct proc
*p
= curproc
;
1131 KKASSERT(p
!= NULL
);
1132 lf_count_adjust(p
, 0);
1133 cr
= cratom(&p
->p_ucred
);
1135 uireplace(&cr
->cr_uidinfo
, uifind(euid
));
1136 lf_count_adjust(p
, 1);
1141 * Helper function to change the real uid of a process
1143 * The per-uid process count for this process is transfered from
1144 * the old uid to the new uid.
1147 change_ruid(uid_t ruid
)
1149 struct proc
*p
= curproc
;
1152 KKASSERT(p
!= NULL
);
1154 cr
= cratom(&p
->p_ucred
);
1155 chgproccnt(cr
->cr_ruidinfo
, -1, 0);
1157 uireplace(&cr
->cr_ruidinfo
, uifind(ruid
));
1158 chgproccnt(cr
->cr_ruidinfo
, 1, 0);