2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved
32 * Copyright (c) 1982, 1986, 1989, 1990, 1991, 1993
33 * The Regents of the University of California. All rights reserved.
34 * (c) UNIX System Laboratories, Inc.
35 * All or some portions of this file are derived from material licensed
36 * to the University of California by American Telephone and Telegraph
37 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
38 * the permission of UNIX System Laboratories, Inc.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the University of
51 * California, Berkeley and its contributors.
52 * 4. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * @(#)kern_prot.c 8.9 (Berkeley) 2/14/95
71 * NOTICE: This file was modified by McAfee Research in 2004 to introduce
72 * support for mandatory and extensible security protections. This notice
73 * is included in support of clause 2.2 (b) of the Apple Public License,
77 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
78 * support for mandatory and extensible security protections. This notice
79 * is included in support of clause 2.2 (b) of the Apple Public License,
85 * System calls related to processes and protection
88 #include <sys/param.h>
90 #include <sys/systm.h>
91 #include <sys/ucred.h>
92 #include <sys/proc_internal.h>
94 #include <sys/kauth.h>
95 #include <sys/timeb.h>
96 #include <sys/times.h>
97 #include <sys/malloc.h>
99 #include <security/audit/audit.h>
102 #include <sys/lctx.h>
106 #include <security/mac_framework.h>
108 #include <secuity/mac_mach_internal.h>
112 #include <sys/mount_internal.h>
113 #include <sys/sysproto.h>
114 #include <mach/message.h>
115 #include <mach/host_security.h>
117 #include <kern/host.h>
118 #include <kern/task.h> /* for current_task() */
119 #include <kern/assert.h>
123 * Credential debugging; we can track entry into a function that might
124 * change a credential, and we can track actual credential changes that
127 * Note: Does *NOT* currently include per-thread credential changes
129 * We don't use kauth_cred_print() in current debugging, but it
130 * can be used if needed when debugging is active.
133 #define DEBUG_CRED_ENTER printf
134 #define DEBUG_CRED_CHANGE printf
135 extern void kauth_cred_print(kauth_cred_t cred
);
136 #else /* !DEBUG_CRED */
137 #define DEBUG_CRED_ENTER(fmt, ...) do {} while (0)
138 #define DEBUG_CRED_CHANGE(fmt, ...) do {} while (0)
139 #endif /* !DEBUG_CRED */
146 * Description: (dis)allow this process to hold task, thread, or execption
147 * ports of processes about to exec.
149 * Parameters: uap->flag New value for flag
151 * Returns: int Previous value of flag
153 * XXX: Belongs in kern_proc.c
156 setprivexec(proc_t p
, struct setprivexec_args
*uap
, int32_t *retval
)
158 AUDIT_ARG(value32
, uap
->flag
);
159 *retval
= p
->p_debugger
;
160 p
->p_debugger
= (uap
->flag
!= 0);
168 * Description: get the process ID
172 * Returns: pid_t Current process ID
174 * XXX: Belongs in kern_proc.c
177 getpid(proc_t p
, __unused
struct getpid_args
*uap
, int32_t *retval
)
188 * Description: get the parent process ID
192 * Returns: pid_t Parent process ID
194 * XXX: Belongs in kern_proc.c
197 getppid(proc_t p
, __unused
struct getppid_args
*uap
, int32_t *retval
)
208 * Description: get the process group ID of the calling process
212 * Returns: pid_t Process group ID
214 * XXX: Belongs in kern_proc.c
217 getpgrp(proc_t p
, __unused
struct getpgrp_args
*uap
, int32_t *retval
)
220 *retval
= p
->p_pgrpid
;
228 * Description: Get an arbitary pid's process group id
230 * Parameters: uap->pid The target pid
233 * ESRCH No such process
235 * Notes: We are permitted to return EPERM in the case that the target
236 * process is not in the same session as the calling process,
237 * which could be a security consideration
239 * XXX: Belongs in kern_proc.c
242 getpgid(proc_t p
, struct getpgid_args
*uap
, int32_t *retval
)
251 if ((pt
= proc_find(uap
->pid
)) == 0)
255 *retval
= pt
->p_pgrpid
;
265 * Description: Get an arbitary pid's session leaders process group ID
267 * Parameters: uap->pid The target pid
270 * ESRCH No such process
272 * Notes: We are permitted to return EPERM in the case that the target
273 * process is not in the same session as the calling process,
274 * which could be a security consideration
276 * XXX: Belongs in kern_proc.c
279 getsid(proc_t p
, struct getsid_args
*uap
, int32_t *retval
)
283 struct session
* sessp
;
289 if ((pt
= proc_find(uap
->pid
)) == 0)
293 sessp
= proc_session(pt
);
294 *retval
= sessp
->s_sid
;
306 * Description: get real user ID for caller
310 * Returns: uid_t The real uid of the caller
313 getuid(__unused proc_t p
, __unused
struct getuid_args
*uap
, int32_t *retval
)
316 *retval
= kauth_getruid();
324 * Description: get effective user ID for caller
328 * Returns: uid_t The effective uid of the caller
331 geteuid(__unused proc_t p
, __unused
struct geteuid_args
*uap
, int32_t *retval
)
334 *retval
= kauth_getuid();
342 * Description: Return the per-thread override identity.
344 * Parameters: uap->uidp Address of uid_t to get uid
345 * uap->gidp Address of gid_t to get gid
348 * ESRCH No per thread identity active
351 gettid(__unused proc_t p
, struct gettid_args
*uap
, int32_t *retval
)
353 struct uthread
*uthread
= get_bsdthread_info(current_thread());
357 * If this thread is not running with an override identity, we can't
358 * return one to the caller, so return an error instead.
360 if (!(uthread
->uu_flag
& UT_SETUID
))
363 if ((error
= suword(uap
->uidp
, uthread
->uu_ucred
->cr_ruid
)))
365 if ((error
= suword(uap
->gidp
, uthread
->uu_ucred
->cr_rgid
)))
376 * Description: get the real group ID for the calling process
380 * Returns: gid_t The real gid of the caller
383 getgid(__unused proc_t p
, __unused
struct getgid_args
*uap
, int32_t *retval
)
386 *retval
= kauth_getrgid();
394 * Description: get the effective group ID for the calling process
398 * Returns: gid_t The effective gid of the caller
400 * Notes: As an implementation detail, the effective gid is stored as
401 * the first element of the supplementary group list.
403 * This could be implemented in Libc instead because of the above
407 getegid(__unused proc_t p
, __unused
struct getegid_args
*uap
, int32_t *retval
)
410 *retval
= kauth_getgid();
418 * Description: get the list of supplementary groups for the calling process
420 * Parameters: uap->gidsetsize # of gid_t's in user buffer
421 * uap->gidset Pointer to user buffer
424 * EINVAL User buffer too small
425 * copyout:EFAULT User buffer invalid
430 * Notes: The caller may specify a 0 value for gidsetsize, and we will
431 * then return how large a buffer is required (in gid_t's) to
432 * contain the answer at the time of the call. Otherwise, we
433 * return the number of gid_t's catually copied to user space.
435 * When called with a 0 gidsetsize from a multithreaded program,
436 * there is no guarantee that another thread may not change the
437 * number of supplementary groups, and therefore a subsequent
438 * call could still fail, unless the maximum possible buffer
439 * size is supplied by the user.
441 * As an implementation detail, the effective gid is stored as
442 * the first element of the supplementary group list, and will
443 * be returned by this call.
446 getgroups(__unused proc_t p
, struct getgroups_args
*uap
, int32_t *retval
)
452 /* grab reference while we muck around with the credential */
453 cred
= kauth_cred_get_with_ref();
455 if ((ngrp
= uap
->gidsetsize
) == 0) {
456 *retval
= cred
->cr_ngroups
;
457 kauth_cred_unref(&cred
);
460 if (ngrp
< cred
->cr_ngroups
) {
461 kauth_cred_unref(&cred
);
464 ngrp
= cred
->cr_ngroups
;
465 if ((error
= copyout((caddr_t
)cred
->cr_groups
,
467 ngrp
* sizeof(gid_t
)))) {
468 kauth_cred_unref(&cred
);
471 kauth_cred_unref(&cred
);
478 * Return the per-thread/per-process supplementary groups list.
480 * XXX implement getsgroups
485 getsgroups(__unused proc_t p
, __unused
struct getsgroups_args
*uap
, __unused
int32_t *retval
)
491 * Return the per-thread/per-process whiteout groups list.
493 * XXX implement getwgroups
498 getwgroups(__unused proc_t p
, __unused
struct getwgroups_args
*uap
, __unused
int32_t *retval
)
507 * Description: Create a new session and set the process group ID to the
513 * EPERM Permission denied
515 * Notes: If the calling process is not the process group leader; there
516 * is no existing process group with its ID, and we are not
517 * currently in vfork, then this function will create a new
518 * session, a new process group, and put the caller in the
519 * process group (as the sole member) and make it the session
520 * leader (as the sole process in the session).
522 * The existing controlling tty (if any) will be dissociated
523 * from the process, and the next non-O_NOCTTY open of a tty
524 * will establish a new controlling tty.
526 * XXX: Belongs in kern_proc.c
529 setsid(proc_t p
, __unused
struct setsid_args
*uap
, int32_t *retval
)
531 struct pgrp
* pg
= PGRP_NULL
;
533 if (p
->p_pgrpid
== p
->p_pid
|| (pg
= pgfind(p
->p_pid
)) || p
->p_lflag
& P_LINVFORK
) {
538 /* enter pgrp works with its own pgrp refcount */
539 (void)enterpgrp(p
, p
->p_pid
, 1);
549 * Description: set process group ID for job control
551 * Parameters: uap->pid Process to change
552 * uap->pgid Process group to join or create
555 * ESRCH pid is not the caller or a child of
557 * enterpgrp:ESRCH No such process
558 * EACCES Permission denied due to exec
559 * EINVAL Invalid argument
560 * EPERM The target process is not in the same
561 * session as the calling process
562 * EPERM The target process is a session leader
563 * EPERM pid and pgid are not the same, and
564 * there is no process in the calling
565 * process whose process group ID matches
568 * Notes: This function will cause the target process to either join
569 * an existing process process group, or create a new process
570 * group in the session of the calling process. It cannot be
571 * used to change the process group ID of a process which is
572 * already a session leader.
574 * If the target pid is 0, the pid of the calling process is
575 * substituted as the new target; if pgid is 0, the target pid
576 * is used as the target process group ID.
578 * Legacy: This system call entry point is also used to implement the
579 * legacy library routine setpgrp(), which under POSIX
581 * XXX: Belongs in kern_proc.c
584 setpgid(proc_t curp
, register struct setpgid_args
*uap
, __unused
int32_t *retval
)
586 proc_t targp
= PROC_NULL
; /* target process */
587 struct pgrp
*pg
= PGRP_NULL
; /* target pgrp */
591 struct session
* curp_sessp
= SESSION_NULL
;
592 struct session
* targp_sessp
= SESSION_NULL
;
594 curp_sessp
= proc_session(curp
);
596 if (uap
->pid
!= 0 && uap
->pid
!= curp
->p_pid
) {
597 if ((targp
= proc_find(uap
->pid
)) == 0 || !inferior(targp
)) {
598 if (targp
!= PROC_NULL
)
604 targp_sessp
= proc_session(targp
);
605 if (targp_sessp
!= curp_sessp
) {
609 if (targp
->p_flag
& P_EXEC
) {
615 targp_sessp
= proc_session(targp
);
618 if (SESS_LEADER(targp
, targp_sessp
)) {
622 if (targp_sessp
!= SESSION_NULL
) {
623 session_rele(targp_sessp
);
624 targp_sessp
= SESSION_NULL
;
632 uap
->pgid
= targp
->p_pid
;
633 else if (uap
->pgid
!= targp
->p_pid
) {
634 if ((pg
= pgfind(uap
->pgid
)) == 0){
638 samesess
= (pg
->pg_session
!= curp_sessp
);
645 error
= enterpgrp(targp
, uap
->pgid
, 0);
647 if (targp_sessp
!= SESSION_NULL
)
648 session_rele(targp_sessp
);
649 if (curp_sessp
!= SESSION_NULL
)
650 session_rele(curp_sessp
);
660 * Description: Is current process tainted by uid or gid changes system call
664 * Returns: 0 Not tainted
667 * Notes: A process is considered tainted if it was created as a retult
668 * of an execve call from an imnage that had either the SUID or
669 * SGID bit set on the executable, or if it has changed any of its
670 * real, effective, or saved user or group IDs since beginning
674 issetugid(proc_t p
, __unused
struct issetugid_args
*uap
, int32_t *retval
)
677 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
678 * we use P_SUGID because we consider changing the owners as
679 * "tainting" as well.
680 * This is significant for procs that start as root and "become"
681 * a user without an exec - programs cannot know *everything*
682 * that libc *might* have put in their data segment.
685 *retval
= (p
->p_flag
& P_SUGID
) ? 1 : 0;
693 * Description: Set user ID system call
695 * Parameters: uap->uid uid to set
698 * suser:EPERM Permission denied
700 * Notes: If called by a privileged process, this function will set the
701 * real, effective, and saved uid to the requested value.
703 * If called from an unprivileged process, but uid is equal to the
704 * real or saved uid, then the effective uid will be set to the
705 * requested value, but the real and saved uid will not change.
707 * If the credential is changed as a result of this call, then we
708 * flag the process as having set privilege since the last exec.
711 setuid(proc_t p
, struct setuid_args
*uap
, __unused
int32_t *retval
)
714 uid_t svuid
= KAUTH_UID_NONE
;
715 uid_t ruid
= KAUTH_UID_NONE
;
716 uid_t gmuid
= KAUTH_UID_NONE
;
718 kauth_cred_t my_cred
, my_new_cred
;
723 my_cred
= kauth_cred_proc_ref(p
);
725 DEBUG_CRED_ENTER("setuid (%d/%d): %p %d\n", p
->p_pid
, (p
->p_pptr
? p
->p_pptr
->p_pid
: 0), my_cred
, uap
->uid
);
728 if (uid
!= my_cred
->cr_ruid
&& /* allow setuid(getuid()) */
729 uid
!= my_cred
->cr_svuid
&& /* allow setuid(saved uid) */
730 (error
= suser(my_cred
, &p
->p_acflag
))) {
731 kauth_cred_unref(&my_cred
);
735 * Everything's okay, do it.
739 * If we are priviledged, then set the saved and real UID too;
740 * otherwise, just set the effective UID
742 if (suser(my_cred
, &p
->p_acflag
) == 0) {
746 * Transfer proc count to new user.
747 * chgproccnt uses list lock for protection
749 (void)chgproccnt(uid
, 1);
750 (void)chgproccnt(my_cred
->cr_ruid
, -1);
753 /* get current credential and take a reference while we muck with it */
756 * Only set the gmuid if the current cred has not opt'ed out;
757 * this normally only happens when calling setgroups() instead
758 * of initgroups() to set an explicit group list, or one of the
759 * other group manipulation functions is invoked and results in
760 * a dislocation (i.e. the credential group membership changes
761 * to something other than the default list for the user, as
762 * in entering a group or leaving an exclusion group).
764 if (!(my_cred
->cr_flags
& CRF_NOMEMBERD
))
768 * Set the credential with new info. If there is no change,
769 * we get back the same credential we passed in; if there is
770 * a change, we drop the reference on the credential we
771 * passed in. The subsequent compare is safe, because it is
772 * a pointer compare rather than a contents compare.
774 my_new_cred
= kauth_cred_setresuid(my_cred
, ruid
, uid
, svuid
, gmuid
);
775 if (my_cred
!= my_new_cred
) {
777 DEBUG_CRED_CHANGE("setuid CH(%d): %p/0x%08x -> %p/0x%08x\n", p
->p_pid
, my_cred
, my_cred
->cr_flags
, my_new_cred
, my_new_cred
->cr_flags
);
781 * We need to protect for a race where another thread
782 * also changed the credential after we took our
783 * reference. If p_ucred has changed then we should
784 * restart this again with the new cred.
786 if (p
->p_ucred
!= my_cred
) {
788 kauth_cred_unref(&my_new_cred
);
789 my_cred
= kauth_cred_proc_ref(p
);
793 p
->p_ucred
= my_new_cred
;
794 OSBitOrAtomic(P_SUGID
, &p
->p_flag
);
799 /* Drop old proc reference or our extra reference */
800 kauth_cred_unref(&my_cred
);
802 set_security_token(p
);
810 * Description: Set effective user ID system call
812 * Parameters: uap->euid effective uid to set
815 * suser:EPERM Permission denied
817 * Notes: If called by a privileged process, or called from an
818 * unprivileged process but euid is equal to the real or saved
819 * uid, then the effective uid will be set to the requested
820 * value, but the real and saved uid will not change.
822 * If the credential is changed as a result of this call, then we
823 * flag the process as having set privilege since the last exec.
826 seteuid(proc_t p
, struct seteuid_args
*uap
, __unused
int32_t *retval
)
830 kauth_cred_t my_cred
, my_new_cred
;
832 DEBUG_CRED_ENTER("seteuid: %d\n", uap
->euid
);
835 AUDIT_ARG(euid
, euid
);
837 my_cred
= kauth_cred_proc_ref(p
);
839 if (euid
!= my_cred
->cr_ruid
&& euid
!= my_cred
->cr_svuid
&&
840 (error
= suser(my_cred
, &p
->p_acflag
))) {
841 kauth_cred_unref(&my_cred
);
846 * Everything's okay, do it. Copy credentials so other references do
847 * not see our changes. get current credential and take a reference
848 * while we muck with it
852 * Set the credential with new info. If there is no change,
853 * we get back the same credential we passed in; if there is
854 * a change, we drop the reference on the credential we
855 * passed in. The subsequent compare is safe, because it is
856 * a pointer compare rather than a contents compare.
858 my_new_cred
= kauth_cred_setresuid(my_cred
, KAUTH_UID_NONE
, euid
, KAUTH_UID_NONE
, my_cred
->cr_gmuid
);
860 if (my_cred
!= my_new_cred
) {
862 DEBUG_CRED_CHANGE("seteuid CH(%d): %p/0x%08x -> %p/0x%08x\n", p
->p_pid
, my_cred
, my_cred
->cr_flags
, my_new_cred
, my_new_cred
->cr_flags
);
866 * We need to protect for a race where another thread
867 * also changed the credential after we took our
868 * reference. If p_ucred has changed then we
869 * should restart this again with the new cred.
871 if (p
->p_ucred
!= my_cred
) {
873 kauth_cred_unref(&my_new_cred
);
874 my_cred
= kauth_cred_proc_ref(p
);
878 p
->p_ucred
= my_new_cred
;
879 OSBitOrAtomic(P_SUGID
, &p
->p_flag
);
884 /* drop old proc reference or our extra reference */
885 kauth_cred_unref(&my_cred
);
887 set_security_token(p
);
895 * Description: Set real and effective user ID system call
897 * Parameters: uap->ruid real uid to set
898 * uap->euid effective uid to set
901 * suser:EPERM Permission denied
903 * Notes: A value of -1 is a special case indicating that the uid for
904 * which that value is specified not be changed. If both values
905 * are specified as -1, no action is taken.
907 * If called by a privileged process, the real and effective uid
908 * will be set to the new value(s) specified.
910 * If called from an unprivileged process, the real uid may be
911 * set to the current value of the real uid, or to the current
912 * value of the saved uid. The effective uid may be set to the
913 * current value of any of the effective, real, or saved uid.
915 * If the newly requested real uid or effective uid does not
916 * match the saved uid, then set the saved uid to the new
917 * effective uid (potentially unrecoverably dropping saved
920 * If the credential is changed as a result of this call, then we
921 * flag the process as having set privilege since the last exec.
924 setreuid(proc_t p
, struct setreuid_args
*uap
, __unused
int32_t *retval
)
928 kauth_cred_t my_cred
, my_new_cred
;
930 DEBUG_CRED_ENTER("setreuid %d %d\n", uap
->ruid
, uap
->euid
);
934 if (ruid
== (uid_t
)-1)
935 ruid
= KAUTH_UID_NONE
;
936 if (euid
== (uid_t
)-1)
937 euid
= KAUTH_UID_NONE
;
938 AUDIT_ARG(euid
, euid
);
939 AUDIT_ARG(ruid
, ruid
);
941 my_cred
= kauth_cred_proc_ref(p
);
943 if (((ruid
!= KAUTH_UID_NONE
&& /* allow no change of ruid */
944 ruid
!= my_cred
->cr_ruid
&& /* allow ruid = ruid */
945 ruid
!= my_cred
->cr_uid
&& /* allow ruid = euid */
946 ruid
!= my_cred
->cr_svuid
) || /* allow ruid = svuid */
947 (euid
!= KAUTH_UID_NONE
&& /* allow no change of euid */
948 euid
!= my_cred
->cr_uid
&& /* allow euid = euid */
949 euid
!= my_cred
->cr_ruid
&& /* allow euid = ruid */
950 euid
!= my_cred
->cr_svuid
)) && /* allow euid = svui */
951 (error
= suser(my_cred
, &p
->p_acflag
))) { /* allow root user any */
952 kauth_cred_unref(&my_cred
);
957 * Everything's okay, do it. Copy credentials so other references do
958 * not see our changes. get current credential and take a reference
959 * while we muck with it
964 uid_t svuid
= KAUTH_UID_NONE
;
966 new_euid
= my_cred
->cr_uid
;
967 new_ruid
= my_cred
->cr_ruid
;
970 * Set the credential with new info. If there is no change,
971 * we get back the same credential we passed in; if there is
972 * a change, we drop the reference on the credential we
973 * passed in. The subsequent compare is safe, because it is
974 * a pointer compare rather than a contents compare.
976 if (euid
== KAUTH_UID_NONE
&& my_cred
->cr_uid
!= euid
) {
977 /* changing the effective UID */
979 OSBitOrAtomic(P_SUGID
, &p
->p_flag
);
981 if (ruid
!= KAUTH_UID_NONE
&& my_cred
->cr_ruid
!= ruid
) {
982 /* changing the real UID; must do user accounting */
983 /* chgproccnt uses list lock for protection */
984 (void)chgproccnt(ruid
, 1);
985 (void)chgproccnt(my_cred
->cr_ruid
, -1);
987 OSBitOrAtomic(P_SUGID
, &p
->p_flag
);
990 * If the newly requested real uid or effective uid does
991 * not match the saved uid, then set the saved uid to the
992 * new effective uid. We are protected from escalation
993 * by the prechecking.
995 if (my_cred
->cr_svuid
!= uap
->ruid
&&
996 my_cred
->cr_svuid
!= uap
->euid
) {
998 OSBitOrAtomic(P_SUGID
, &p
->p_flag
);
1001 my_new_cred
= kauth_cred_setresuid(my_cred
, ruid
, euid
, svuid
, my_cred
->cr_gmuid
);
1003 if (my_cred
!= my_new_cred
) {
1005 DEBUG_CRED_CHANGE("setreuid CH(%d): %p/0x%08x -> %p/0x%08x\n", p
->p_pid
, my_cred
, my_cred
->cr_flags
, my_new_cred
, my_new_cred
->cr_flags
);
1009 * We need to protect for a race where another thread
1010 * also changed the credential after we took our
1011 * reference. If p_ucred has changed then we should
1012 * restart this again with the new cred.
1014 if (p
->p_ucred
!= my_cred
) {
1016 kauth_cred_unref(&my_new_cred
);
1017 my_cred
= kauth_cred_proc_ref(p
);
1021 p
->p_ucred
= my_new_cred
;
1022 OSBitOrAtomic(P_SUGID
, &p
->p_flag
); /* XXX redundant? */
1027 /* drop old proc reference or our extra reference */
1028 kauth_cred_unref(&my_cred
);
1030 set_security_token(p
);
1038 * Description: Set group ID system call
1040 * Parameters: uap->gid gid to set
1042 * Returns: 0 Success
1043 * suser:EPERM Permission denied
1045 * Notes: If called by a privileged process, this function will set the
1046 * real, effective, and saved gid to the requested value.
1048 * If called from an unprivileged process, but gid is equal to the
1049 * real or saved gid, then the effective gid will be set to the
1050 * requested value, but the real and saved gid will not change.
1052 * If the credential is changed as a result of this call, then we
1053 * flag the process as having set privilege since the last exec.
1055 * As an implementation detail, the effective gid is stored as
1056 * the first element of the supplementary group list, and
1057 * therefore the effective group list may be reordered to keep
1058 * the supplementary group list unchanged.
1061 setgid(proc_t p
, struct setgid_args
*uap
, __unused
int32_t *retval
)
1064 gid_t rgid
= KAUTH_GID_NONE
;
1065 gid_t svgid
= KAUTH_GID_NONE
;
1067 kauth_cred_t my_cred
, my_new_cred
;
1069 DEBUG_CRED_ENTER("setgid(%d/%d): %d\n", p
->p_pid
, (p
->p_pptr
? p
->p_pptr
->p_pid
: 0), uap
->gid
);
1072 AUDIT_ARG(gid
, gid
);
1074 my_cred
= kauth_cred_proc_ref(p
);
1076 if (gid
!= my_cred
->cr_rgid
&& /* allow setgid(getgid()) */
1077 gid
!= my_cred
->cr_svgid
&& /* allow setgid(saved gid) */
1078 (error
= suser(my_cred
, &p
->p_acflag
))) {
1079 kauth_cred_unref(&my_cred
);
1084 * If we are priviledged, then set the saved and real GID too;
1085 * otherwise, just set the effective GID
1087 if (suser(my_cred
, &p
->p_acflag
) == 0) {
1092 /* get current credential and take a reference while we muck with it */
1096 * Set the credential with new info. If there is no change,
1097 * we get back the same credential we passed in; if there is
1098 * a change, we drop the reference on the credential we
1099 * passed in. The subsequent compare is safe, because it is
1100 * a pointer compare rather than a contents compare.
1102 my_new_cred
= kauth_cred_setresgid(my_cred
, rgid
, gid
, svgid
);
1103 if (my_cred
!= my_new_cred
) {
1105 DEBUG_CRED_CHANGE("setgid(CH)%d: %p/0x%08x->%p/0x%08x\n", p
->p_pid
, my_cred
, my_cred
->cr_flags
, my_new_cred
, my_new_cred
->cr_flags
);
1109 * We need to protect for a race where another thread
1110 * also changed the credential after we took our
1111 * reference. If p_ucred has changed then we
1112 * should restart this again with the new cred.
1114 if (p
->p_ucred
!= my_cred
) {
1116 kauth_cred_unref(&my_new_cred
);
1118 my_cred
= kauth_cred_proc_ref(p
);
1121 p
->p_ucred
= my_new_cred
;
1122 OSBitOrAtomic(P_SUGID
, &p
->p_flag
);
1127 /* Drop old proc reference or our extra reference */
1128 kauth_cred_unref(&my_cred
);
1130 set_security_token(p
);
1138 * Description: Set effective group ID system call
1140 * Parameters: uap->egid effective gid to set
1142 * Returns: 0 Success
1145 * Notes: If called by a privileged process, or called from an
1146 * unprivileged process but egid is equal to the real or saved
1147 * gid, then the effective gid will be set to the requested
1148 * value, but the real and saved gid will not change.
1150 * If the credential is changed as a result of this call, then we
1151 * flag the process as having set privilege since the last exec.
1153 * As an implementation detail, the effective gid is stored as
1154 * the first element of the supplementary group list, and
1155 * therefore the effective group list may be reordered to keep
1156 * the supplementary group list unchanged.
1159 setegid(proc_t p
, struct setegid_args
*uap
, __unused
int32_t *retval
)
1163 kauth_cred_t my_cred
, my_new_cred
;
1165 DEBUG_CRED_ENTER("setegid %d\n", uap
->egid
);
1168 AUDIT_ARG(egid
, egid
);
1170 my_cred
= kauth_cred_proc_ref(p
);
1172 if (egid
!= my_cred
->cr_rgid
&&
1173 egid
!= my_cred
->cr_svgid
&&
1174 (error
= suser(my_cred
, &p
->p_acflag
))) {
1175 kauth_cred_unref(&my_cred
);
1179 /* get current credential and take a reference while we muck with it */
1182 * Set the credential with new info. If there is no change,
1183 * we get back the same credential we passed in; if there is
1184 * a change, we drop the reference on the credential we
1185 * passed in. The subsequent compare is safe, because it is
1186 * a pointer compare rather than a contents compare.
1188 my_new_cred
= kauth_cred_setresgid(my_cred
, KAUTH_GID_NONE
, egid
, KAUTH_GID_NONE
);
1189 if (my_cred
!= my_new_cred
) {
1191 DEBUG_CRED_CHANGE("setegid(CH)%d: %p/0x%08x->%p/0x%08x\n", p
->p_pid
, my_cred
, my_cred
->cr_flags
, my_new_cred
, my_new_cred
->cr_flags
);
1195 * We need to protect for a race where another thread
1196 * also changed the credential after we took our
1197 * reference. If p_ucred has changed then we
1198 * should restart this again with the new cred.
1200 if (p
->p_ucred
!= my_cred
) {
1202 kauth_cred_unref(&my_new_cred
);
1204 my_cred
= kauth_cred_proc_ref(p
);
1207 p
->p_ucred
= my_new_cred
;
1208 OSBitOrAtomic(P_SUGID
, &p
->p_flag
);
1214 /* Drop old proc reference or our extra reference */
1215 kauth_cred_unref(&my_cred
);
1217 set_security_token(p
);
1224 * Description: Set real and effective group ID system call
1226 * Parameters: uap->rgid real gid to set
1227 * uap->egid effective gid to set
1229 * Returns: 0 Success
1230 * suser:EPERM Permission denied
1232 * Notes: A value of -1 is a special case indicating that the gid for
1233 * which that value is specified not be changed. If both values
1234 * are specified as -1, no action is taken.
1236 * If called by a privileged process, the real and effective gid
1237 * will be set to the new value(s) specified.
1239 * If called from an unprivileged process, the real gid may be
1240 * set to the current value of the real gid, or to the current
1241 * value of the saved gid. The effective gid may be set to the
1242 * current value of any of the effective, real, or saved gid.
1244 * If the new real and effective gid will not be equal, or the
1245 * new real or effective gid is not the same as the saved gid,
1246 * then the saved gid will be updated to reflect the new
1247 * effective gid (potentially unrecoverably dropping saved
1250 * If the credential is changed as a result of this call, then we
1251 * flag the process as having set privilege since the last exec.
1253 * As an implementation detail, the effective gid is stored as
1254 * the first element of the supplementary group list, and
1255 * therefore the effective group list may be reordered to keep
1256 * the supplementary group list unchanged.
1259 setregid(proc_t p
, struct setregid_args
*uap
, __unused
int32_t *retval
)
1263 kauth_cred_t my_cred
, my_new_cred
;
1265 DEBUG_CRED_ENTER("setregid %d %d\n", uap
->rgid
, uap
->egid
);
1270 if (rgid
== (uid_t
)-1)
1271 rgid
= KAUTH_GID_NONE
;
1272 if (egid
== (uid_t
)-1)
1273 egid
= KAUTH_GID_NONE
;
1274 AUDIT_ARG(egid
, egid
);
1275 AUDIT_ARG(rgid
, rgid
);
1277 my_cred
= kauth_cred_proc_ref(p
);
1279 if (((rgid
!= KAUTH_UID_NONE
&& /* allow no change of rgid */
1280 rgid
!= my_cred
->cr_rgid
&& /* allow rgid = rgid */
1281 rgid
!= my_cred
->cr_gid
&& /* allow rgid = egid */
1282 rgid
!= my_cred
->cr_svgid
) || /* allow rgid = svgid */
1283 (egid
!= KAUTH_UID_NONE
&& /* allow no change of egid */
1284 egid
!= my_cred
->cr_groups
[0] && /* allow no change of egid */
1285 egid
!= my_cred
->cr_gid
&& /* allow egid = egid */
1286 egid
!= my_cred
->cr_rgid
&& /* allow egid = rgid */
1287 egid
!= my_cred
->cr_svgid
)) && /* allow egid = svgid */
1288 (error
= suser(my_cred
, &p
->p_acflag
))) { /* allow root user any */
1289 kauth_cred_unref(&my_cred
);
1293 /* get current credential and take a reference while we muck with it */
1295 uid_t new_egid
= my_cred
->cr_gid
;
1296 uid_t new_rgid
= my_cred
->cr_rgid
;
1297 uid_t svgid
= KAUTH_UID_NONE
;
1301 * Set the credential with new info. If there is no change,
1302 * we get back the same credential we passed in; if there is
1303 * a change, we drop the reference on the credential we
1304 * passed in. The subsequent compare is safe, because it is
1305 * a pointer compare rather than a contents compare.
1307 if (egid
== KAUTH_UID_NONE
&& my_cred
->cr_groups
[0] != egid
) {
1308 /* changing the effective GID */
1310 OSBitOrAtomic(P_SUGID
, &p
->p_flag
);
1312 if (rgid
!= KAUTH_UID_NONE
&& my_cred
->cr_rgid
!= rgid
) {
1313 /* changing the real GID */
1315 OSBitOrAtomic(P_SUGID
, &p
->p_flag
);
1318 * If the newly requested real gid or effective gid does
1319 * not match the saved gid, then set the saved gid to the
1320 * new effective gid. We are protected from escalation
1321 * by the prechecking.
1323 if (my_cred
->cr_svgid
!= uap
->rgid
&&
1324 my_cred
->cr_svgid
!= uap
->egid
) {
1326 OSBitOrAtomic(P_SUGID
, &p
->p_flag
);
1329 my_new_cred
= kauth_cred_setresgid(my_cred
, rgid
, egid
, svgid
);
1330 if (my_cred
!= my_new_cred
) {
1332 DEBUG_CRED_CHANGE("setregid(CH)%d: %p/0x%08x->%p/0x%08x\n", p
->p_pid
, my_cred
, my_cred
->cr_flags
, my_new_cred
, my_new_cred
->cr_flags
);
1335 /* need to protect for a race where another thread
1336 * also changed the credential after we took our
1337 * reference. If p_ucred has changed then we
1338 * should restart this again with the new cred.
1340 if (p
->p_ucred
!= my_cred
) {
1342 kauth_cred_unref(&my_new_cred
);
1344 my_cred
= kauth_cred_proc_ref(p
);
1347 p
->p_ucred
= my_new_cred
;
1348 OSBitOrAtomic(P_SUGID
, &p
->p_flag
); /* XXX redundant? */
1353 /* Drop old proc reference or our extra reference */
1354 kauth_cred_unref(&my_cred
);
1356 set_security_token(p
);
1362 * Set the per-thread override identity. The first parameter can be the
1363 * current real UID, KAUTH_UID_NONE, or, if the caller is priviledged, it
1364 * can be any UID. If it is KAUTH_UID_NONE, then as a special case, this
1365 * means "revert to the per process credential"; otherwise, if permitted,
1366 * it changes the effective, real, and saved UIDs and GIDs for the current
1367 * thread to the requested UID and single GID, and clears all other GIDs.
1370 settid(proc_t p
, struct settid_args
*uap
, __unused
int32_t *retval
)
1373 struct uthread
*uthread
= get_bsdthread_info(current_thread());
1379 AUDIT_ARG(uid
, uid
);
1380 AUDIT_ARG(gid
, gid
);
1382 if (proc_suser(p
) != 0)
1385 if (uid
== KAUTH_UID_NONE
) {
1387 /* must already be assuming another identity in order to revert back */
1388 if ((uthread
->uu_flag
& UT_SETUID
) == 0)
1391 /* revert to delayed binding of process credential */
1392 uc
= kauth_cred_proc_ref(p
);
1393 kauth_cred_unref(&uthread
->uu_ucred
);
1394 uthread
->uu_ucred
= uc
;
1395 uthread
->uu_flag
&= ~UT_SETUID
;
1397 kauth_cred_t my_cred
, my_new_cred
;
1399 /* cannot already be assuming another identity */
1400 if ((uthread
->uu_flag
& UT_SETUID
) != 0) {
1405 * Get a new credential instance from the old if this one
1406 * changes; otherwise kauth_cred_setuidgid() returns the
1407 * same credential. We take an extra reference on the
1408 * current credential while we muck with it, so we can do
1409 * the post-compare for changes by pointer.
1411 kauth_cred_ref(uthread
->uu_ucred
);
1412 my_cred
= uthread
->uu_ucred
;
1413 my_new_cred
= kauth_cred_setuidgid(my_cred
, uid
, gid
);
1414 if (my_cred
!= my_new_cred
)
1415 uthread
->uu_ucred
= my_new_cred
;
1416 uthread
->uu_flag
|= UT_SETUID
;
1418 /* Drop old uthread reference or our extra reference */
1419 kauth_cred_unref(&my_cred
);
1422 * XXX should potentially set per thread security token (there is
1424 * XXX it is unclear whether P_SUGID should be st at this point;
1425 * XXX in theory, it is being deprecated.
1432 * Set the per-thread override identity. Use this system call for a thread to
1433 * assume the identity of another process or to revert back to normal identity
1434 * of the current process.
1436 * When the "assume" argument is non zero the current thread will assume the
1437 * identity of the process represented by the pid argument.
1439 * When the assume argument is zero we revert back to our normal identity.
1442 settid_with_pid(proc_t p
, struct settid_with_pid_args
*uap
, __unused
int32_t *retval
)
1445 struct uthread
*uthread
= get_bsdthread_info(current_thread());
1446 kauth_cred_t my_cred
, my_target_cred
, my_new_cred
;
1448 AUDIT_ARG(pid
, uap
->pid
);
1449 AUDIT_ARG(value32
, uap
->assume
);
1451 if (proc_suser(p
) != 0) {
1456 * XXX should potentially set per thread security token (there is
1458 * XXX it is unclear whether P_SUGID should be st at this point;
1459 * XXX in theory, it is being deprecated.
1463 * assume argument tells us to assume the identity of the process with the
1464 * id passed in the pid argument.
1466 if (uap
->assume
!= 0) {
1467 /* can't do this if we have already assumed an identity */
1468 if ((uthread
->uu_flag
& UT_SETUID
) != 0)
1471 target_proc
= proc_find(uap
->pid
);
1472 /* can't assume the identity of the kernel process */
1473 if (target_proc
== NULL
|| target_proc
== kernproc
) {
1474 if (target_proc
!= NULL
)
1475 proc_rele(target_proc
);
1480 * Take a reference on the credential used in our target
1481 * process then use it as the identity for our current
1482 * thread. We take an extra reference on the current
1483 * credential while we muck with it, so we can do the
1484 * post-compare for changes by pointer.
1486 * The post-compare is needed for the case that our process
1487 * credential has been changed to be identical to our thread
1488 * credential following our assumption of a per-thread one,
1489 * since the credential cache will maintain a unique instance.
1491 kauth_cred_ref(uthread
->uu_ucred
);
1492 my_cred
= uthread
->uu_ucred
;
1493 my_target_cred
= kauth_cred_proc_ref(target_proc
);
1494 my_new_cred
= kauth_cred_setuidgid(my_cred
, my_target_cred
->cr_uid
, my_target_cred
->cr_gid
);
1495 if (my_cred
!= my_new_cred
)
1496 uthread
->uu_ucred
= my_new_cred
;
1498 uthread
->uu_flag
|= UT_SETUID
;
1500 /* Drop old uthread reference or our extra reference */
1501 proc_rele(target_proc
);
1502 kauth_cred_unref(&my_cred
);
1503 kauth_cred_unref(&my_target_cred
);
1509 * Otherwise, we are reverting back to normal mode of operation where
1510 * delayed binding of the process credential sets the credential in
1511 * the thread (uu_ucred)
1513 if ((uthread
->uu_flag
& UT_SETUID
) == 0)
1516 /* revert to delayed binding of process credential */
1517 my_new_cred
= kauth_cred_proc_ref(p
);
1518 kauth_cred_unref(&uthread
->uu_ucred
);
1519 uthread
->uu_ucred
= my_new_cred
;
1520 uthread
->uu_flag
&= ~UT_SETUID
;
1529 * Description: Internal implementation for both the setgroups and initgroups
1532 * Parameters: gidsetsize Number of groups in set
1533 * gidset Pointer to group list
1534 * gmuid Base gid (initgroups only!)
1536 * Returns: 0 Success
1537 * suser:EPERM Permision denied
1538 * EINVAL Invalid gidsetsize value
1539 * copyin:EFAULT Bad gidset or gidsetsize is
1542 * Notes: When called from a thread running under an assumed per-thread
1543 * identity, this function will operate against the per-thread
1544 * credential, rather than against the process credential. In
1545 * this specific case, the process credential is verified to
1546 * still be privileged at the time of the call, rather than the
1547 * per-thread credential for this operation to be permitted.
1549 * This effectively means that setgroups/initigroups calls in
1550 * a thread running a per-thread credential should occur *after*
1551 * the settid call that created it, not before (unlike setuid,
1552 * which must be called after, since it will result in privilege
1555 * When called normally (i.e. no per-thread assumed identity),
1556 * the per process credential is updated per POSIX.
1558 * If the credential is changed as a result of this call, then we
1559 * flag the process as having set privilege since the last exec.
1562 setgroups1(proc_t p
, u_int gidsetsize
, user_addr_t gidset
, uid_t gmuid
, __unused
int32_t *retval
)
1565 gid_t newgroups
[NGROUPS
] = { 0 };
1567 kauth_cred_t my_cred
, my_new_cred
;
1568 struct uthread
*uthread
= get_bsdthread_info(current_thread());
1570 DEBUG_CRED_ENTER("setgroups1 (%d/%d): %d 0x%016x %d\n", p
->p_pid
, (p
->p_pptr
? p
->p_pptr
->p_pid
: 0), gidsetsize
, gidset
, gmuid
);
1579 error
= copyin(gidset
,
1580 (caddr_t
)newgroups
, ngrp
* sizeof(gid_t
));
1586 my_cred
= kauth_cred_proc_ref(p
);
1587 if ((error
= suser(my_cred
, &p
->p_acflag
))) {
1588 kauth_cred_unref(&my_cred
);
1592 if ((uthread
->uu_flag
& UT_SETUID
) != 0) {
1594 int my_cred_flags
= uthread
->uu_ucred
->cr_flags
;
1595 #endif /* DEBUG_CRED */
1596 kauth_cred_unref(&my_cred
);
1599 * If this thread is under an assumed identity, set the
1600 * supplementary grouplist on the thread credential instead
1601 * of the process one. If we were the only reference holder,
1602 * the credential is updated in place, otherwise, our reference
1603 * is dropped and we get back a different cred with a reference
1604 * already held on it. Because this is per-thread, we don't
1605 * need the referencing/locking/retry required for per-process.
1607 my_cred
= uthread
->uu_ucred
;
1608 uthread
->uu_ucred
= kauth_cred_setgroups(my_cred
, &newgroups
[0], ngrp
, gmuid
);
1610 if (my_cred
!= uthread
->uu_ucred
) {
1611 DEBUG_CRED_CHANGE("setgroups1(CH)%d: %p/0x%08x->%p/0x%08x\n", p
->p_pid
, my_cred
, my_cred_flags
, uthread
->uu_ucred
, uthread
->uu_ucred
->cr_flags
);
1613 #endif /* DEBUG_CRED */
1617 * get current credential and take a reference while we muck
1622 * Set the credential with new info. If there is no
1623 * change, we get back the same credential we passed
1624 * in; if there is a change, we drop the reference on
1625 * the credential we passed in. The subsequent
1626 * compare is safe, because it is a pointer compare
1627 * rather than a contents compare.
1629 my_new_cred
= kauth_cred_setgroups(my_cred
, &newgroups
[0], ngrp
, gmuid
);
1630 if (my_cred
!= my_new_cred
) {
1632 DEBUG_CRED_CHANGE("setgroups1(CH)%d: %p/0x%08x->%p/0x%08x\n", p
->p_pid
, my_cred
, my_cred
->cr_flags
, my_new_cred
, my_new_cred
->cr_flags
);
1636 * We need to protect for a race where another
1637 * thread also changed the credential after we
1638 * took our reference. If p_ucred has
1639 * changed then we should restart this again
1640 * with the new cred.
1642 if (p
->p_ucred
!= my_cred
) {
1644 kauth_cred_unref(&my_new_cred
);
1645 my_cred
= kauth_cred_proc_ref(p
);
1649 p
->p_ucred
= my_new_cred
;
1650 OSBitOrAtomic(P_SUGID
, &p
->p_flag
);
1655 /* Drop old proc reference or our extra reference */
1656 AUDIT_ARG(groupset
, my_cred
->cr_groups
, ngrp
);
1657 kauth_cred_unref(&my_cred
);
1660 set_security_token(p
);
1670 * Description: Initialize the default supplementary groups list and set the
1671 * gmuid for use by the external group resolver (if any)
1673 * Parameters: uap->gidsetsize Number of groups in set
1674 * uap->gidset Pointer to group list
1675 * uap->gmuid Base gid
1677 * Returns: 0 Success
1678 * setgroups1:EPERM Permision denied
1679 * setgroups1:EINVAL Invalid gidsetsize value
1680 * setgroups1:EFAULT Bad gidset or gidsetsize is
1682 * Notes: This function opts *IN* to memberd participation
1684 * The normal purpose of this function is for a privileged
1685 * process to indicate supplementary groups and identity for
1686 * participation in extended group membership resolution prior
1687 * to dropping privilege by assuming a specific user identity.
1689 * It is the first half of the primary mechanism whereby user
1690 * identity is established to the system by programs such as
1691 * /usr/bin/login. The second half is the drop of uid privilege
1692 * for a specific uid corresponding to the user.
1694 * See also: setgroups1()
1697 initgroups(proc_t p
, struct initgroups_args
*uap
, __unused
int32_t *retval
)
1699 DEBUG_CRED_ENTER("initgroups\n");
1701 return(setgroups1(p
, uap
->gidsetsize
, uap
->gidset
, uap
->gmuid
, retval
));
1708 * Description: Initialize the default supplementary groups list
1710 * Parameters: gidsetsize Number of groups in set
1711 * gidset Pointer to group list
1713 * Returns: 0 Success
1714 * setgroups1:EPERM Permision denied
1715 * setgroups1:EINVAL Invalid gidsetsize value
1716 * setgroups1:EFAULT Bad gidset or gidsetsize is
1718 * Notes: This functions opts *OUT* of memberd participation.
1720 * This function exists for compatibility with POSIX. Most user
1721 * programs should use initgroups() instead to ensure correct
1722 * participation in group membership resolution when utilizing
1723 * a directory service for authentication.
1725 * It is identical to an initgroups() call with a gmuid argument
1726 * of KAUTH_UID_NONE.
1728 * See also: setgroups1()
1731 setgroups(proc_t p
, struct setgroups_args
*uap
, __unused
int32_t *retval
)
1733 DEBUG_CRED_ENTER("setgroups\n");
1735 return(setgroups1(p
, uap
->gidsetsize
, uap
->gidset
, KAUTH_UID_NONE
, retval
));
1740 * Set the per-thread/per-process supplementary groups list.
1742 * XXX implement setsgroups
1747 setsgroups(__unused proc_t p
, __unused
struct setsgroups_args
*uap
, __unused
int32_t *retval
)
1753 * Set the per-thread/per-process whiteout groups list.
1755 * XXX implement setwgroups
1760 setwgroups(__unused proc_t p
, __unused
struct setwgroups_args
*uap
, __unused
int32_t *retval
)
1767 * Check if gid is a member of the group set.
1769 * XXX This interface is going away; use kauth_cred_ismember_gid() directly
1773 groupmember(gid_t gid
, kauth_cred_t cred
)
1777 if (kauth_cred_ismember_gid(cred
, gid
, &is_member
) == 0 && is_member
)
1784 * Test whether the specified credentials imply "super-user"
1785 * privilege; if so, and we have accounting info, set the flag
1786 * indicating use of super-powers.
1787 * Returns 0 or error.
1789 * XXX This interface is going away; use kauth_cred_issuser() directly
1792 * Note: This interface exists to implement the "has used privilege"
1793 * bit (ASU) in the p_acflags field of the process, which is
1794 * only externalized via private sysctl and in process accounting
1795 * records. The flag is technically not required in either case.
1798 suser(kauth_cred_t cred
, u_short
*acflag
)
1801 if (!IS_VALID_CRED(cred
))
1804 if (kauth_cred_getuid(cred
) == 0) {
1814 * XXX This interface is going away; use kauth_cred_issuser() directly
1820 proc_t p
= current_proc();
1825 return (proc_suser(p
) == 0);
1830 * XXX This interface is going away; use kauth_cred_issuser() directly
1836 proc_t p
= current_proc();
1837 kauth_cred_t my_cred
;
1843 my_cred
= kauth_cred_proc_ref(p
);
1845 err
= (suser(my_cred
, &p
->p_acflag
) == 0 ||
1846 my_cred
->cr_ruid
== 0 || my_cred
->cr_svuid
== 0);
1847 kauth_cred_unref(&my_cred
);
1855 * Description: Get login name, if available.
1857 * Parameters: uap->namebuf User buffer for return
1858 * uap->namelen User buffer length
1860 * Returns: 0 Success
1863 * Notes: Intended to obtain a string containing the user name of the
1864 * user associated with the controlling terminal for the calling
1867 * Not very useful on modern systems, due to inherent length
1868 * limitations for the static array in the session structure
1869 * which is used to store the login name.
1871 * Permitted to return NULL
1873 * XXX: Belongs in kern_proc.c
1876 getlogin(proc_t p
, struct getlogin_args
*uap
, __unused
int32_t *retval
)
1878 char buffer
[MAXLOGNAME
+1];
1879 struct session
* sessp
;
1881 bzero(buffer
, MAXLOGNAME
+1);
1883 sessp
= proc_session(p
);
1885 if (uap
->namelen
> MAXLOGNAME
)
1886 uap
->namelen
= MAXLOGNAME
;
1888 if(sessp
!= SESSION_NULL
) {
1889 session_lock(sessp
);
1890 bcopy( sessp
->s_login
, buffer
, uap
->namelen
);
1891 session_unlock(sessp
);
1893 session_rele(sessp
);
1895 return (copyout((caddr_t
)buffer
, uap
->namebuf
, uap
->namelen
));
1902 * Description: Set login name.
1904 * Parameters: uap->namebuf User buffer containing name
1906 * Returns: 0 Success
1907 * suser:EPERM Permission denied
1908 * copyinstr:EFAULT User buffer invalid
1909 * copyinstr:EINVAL Supplied name was too long
1911 * Notes: This is a utility system call to support getlogin().
1913 * XXX: Belongs in kern_proc.c
1916 setlogin(proc_t p
, struct setlogin_args
*uap
, __unused
int32_t *retval
)
1920 char buffer
[MAXLOGNAME
+1];
1921 struct session
* sessp
;
1923 if ((error
= proc_suser(p
)))
1926 bzero(&buffer
[0], MAXLOGNAME
+1);
1929 error
= copyinstr(uap
->namebuf
,
1930 (caddr_t
) &buffer
[0],
1931 MAXLOGNAME
- 1, (size_t *)&dummy
);
1933 sessp
= proc_session(p
);
1935 if (sessp
!= SESSION_NULL
) {
1936 session_lock(sessp
);
1937 bcopy(buffer
, sessp
->s_login
, MAXLOGNAME
);
1938 session_unlock(sessp
);
1939 session_rele(sessp
);
1944 AUDIT_ARG(text
, buffer
);
1945 } else if (error
== ENAMETOOLONG
)
1951 /* Set the secrity token of the task with current euid and eguid */
1953 * XXX This needs to change to give the task a reference and/or an opaque
1957 set_security_token(proc_t p
)
1959 security_token_t sec_token
;
1960 audit_token_t audit_token
;
1961 kauth_cred_t my_cred
;
1962 host_priv_t host_priv
;
1965 * Don't allow a vfork child to override the parent's token settings
1966 * (since they share a task). Instead, the child will just have to
1967 * suffer along using the parent's token until the exec(). It's all
1968 * undefined behavior anyway, right?
1970 if (p
->task
== current_task()) {
1972 uthread
= (uthread_t
)get_bsdthread_info(current_thread());
1973 if (uthread
->uu_flag
& UT_VFORK
)
1977 my_cred
= kauth_cred_proc_ref(p
);
1978 /* XXX mach_init doesn't have a p_ucred when it calls this function */
1979 if (IS_VALID_CRED(my_cred
)) {
1980 sec_token
.val
[0] = kauth_cred_getuid(my_cred
);
1981 sec_token
.val
[1] = my_cred
->cr_gid
;
1983 sec_token
.val
[0] = 0;
1984 sec_token
.val
[1] = 0;
1988 * The current layout of the Mach audit token explicitly
1989 * adds these fields. But nobody should rely on such
1990 * a literal representation. Instead, the BSM library
1991 * provides a function to convert an audit token into
1992 * a BSM subject. Use of that mechanism will isolate
1993 * the user of the trailer from future representation
1996 audit_token
.val
[0] = my_cred
->cr_audit
.as_aia_p
->ai_auid
;
1997 audit_token
.val
[1] = my_cred
->cr_uid
;
1998 audit_token
.val
[2] = my_cred
->cr_gid
;
1999 audit_token
.val
[3] = my_cred
->cr_ruid
;
2000 audit_token
.val
[4] = my_cred
->cr_rgid
;
2001 audit_token
.val
[5] = p
->p_pid
;
2002 audit_token
.val
[6] = my_cred
->cr_audit
.as_aia_p
->ai_asid
;
2003 audit_token
.val
[7] = p
->p_idversion
;
2005 #if CONFIG_MACF_MACH
2006 mac_task_label_update_cred(my_cred
, p
->task
);
2009 host_priv
= (sec_token
.val
[0]) ? HOST_PRIV_NULL
: host_priv_self();
2011 if (host_priv
!= HOST_PRIV_NULL
&& mac_system_check_host_priv(my_cred
))
2012 host_priv
= HOST_PRIV_NULL
;
2014 kauth_cred_unref(&my_cred
);
2016 return (host_security_set_task_token(host_security_self(),
2020 host_priv
) != KERN_SUCCESS
);
2025 * Fill in a struct xucred based on a kauth_cred_t.
2029 cru2x(kauth_cred_t cr
, struct xucred
*xcr
)
2032 bzero(xcr
, sizeof(*xcr
));
2033 xcr
->cr_version
= XUCRED_VERSION
;
2034 xcr
->cr_uid
= kauth_cred_getuid(cr
);
2035 xcr
->cr_ngroups
= cr
->cr_ngroups
;
2036 bcopy(cr
->cr_groups
, xcr
->cr_groups
, sizeof(xcr
->cr_groups
));
2042 * Set Login Context ID
2045 * MPSAFE - assignment of (visible) process to context protected by ALLLCTX_LOCK,
2046 * LCTX by its own locks.
2049 setlcid(proc_t p0
, struct setlcid_args
*uap
, __unused
int32_t *retval
)
2056 AUDIT_ARG(pid
, uap
->pid
);
2057 AUDIT_ARG(value32
, uap
->lcid
);
2058 if (uap
->pid
== LCID_PROC_SELF
) { /* Create/Join/Leave */
2060 } else { /* Adopt/Orphan */
2061 p
= proc_find(uap
->pid
);
2068 error
= mac_proc_check_setlcid(p0
, p
, uap
->pid
, uap
->lcid
);
2073 switch (uap
->lcid
) {
2077 /* Only root may Leave/Orphan. */
2083 /* Process not in login context. */
2084 if (p
->p_lctx
== NULL
) {
2096 /* Create only valid for self! */
2097 if (uap
->pid
!= LCID_PROC_SELF
) {
2102 /* Already in a login context. */
2103 if (p
->p_lctx
!= NULL
) {
2121 /* Only root may Join/Adopt. */
2127 l
= lcfind(uap
->lcid
);
2138 enterlctx(p
, l
, (uap
->lcid
== LCID_CREATE
) ? 1 : 0);
2148 * Get Login Context ID
2151 * MPSAFE - membership of (visible) process in a login context
2152 * protected by the all-context lock.
2155 getlcid(proc_t p0
, struct getlcid_args
*uap
, int32_t *retval
)
2161 AUDIT_ARG(pid
, uap
->pid
);
2162 if (uap
->pid
== LCID_PROC_SELF
) {
2165 p
= proc_find(uap
->pid
);
2172 error
= mac_proc_check_getlcid(p0
, p
, uap
->pid
);
2177 if (p
->p_lctx
== NULL
) {
2182 *retval
= p
->p_lctx
->lc_id
;
2192 setlcid(proc_t p0
, struct setlcid_args
*uap
, int32_t *retval
)
2199 getlcid(proc_t p0
, struct getlcid_args
*uap
, int32_t *retval
)