2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* 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
72 * System calls related to processes and protection
75 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/ucred.h>
79 #include <sys/proc_internal.h>
81 #include <sys/kauth.h>
82 #include <sys/timeb.h>
83 #include <sys/times.h>
84 #include <sys/malloc.h>
86 #include <bsm/audit_kernel.h>
88 #include <sys/mount_internal.h>
89 #include <sys/sysproto.h>
90 #include <mach/message.h>
91 #include <mach/host_security.h>
93 #include <kern/host.h>
95 int groupmember(gid_t gid
, kauth_cred_t cred
);
99 extern int prepare_profile_database(int user
);
102 * setprivexec: (dis)allow this process to hold
103 * task, thread, or execption ports of processes about to exec.
106 setprivexec(struct proc
*p
, struct setprivexec_args
*uap
, register_t
*retval
)
108 AUDIT_ARG(value
, uap
->flag
);
109 *retval
= p
->p_debugger
;
110 p
->p_debugger
= (uap
->flag
!= 0);
116 getpid(struct proc
*p
, __unused
struct getpid_args
*uap
, register_t
*retval
)
125 getppid(struct proc
*p
, __unused
struct getppid_args
*uap
, register_t
*retval
)
128 *retval
= p
->p_pptr
->p_pid
;
132 /* Get process group ID; note that POSIX getpgrp takes no parameter */
134 getpgrp(struct proc
*p
, __unused
struct getpgrp_args
*uap
, register_t
*retval
)
137 *retval
= p
->p_pgrp
->pg_id
;
141 /* Get an arbitary pid's process group id */
143 getpgid(struct proc
*p
, struct getpgid_args
*uap
, register_t
*retval
)
151 if ((pt
= pfind(uap
->pid
)) == 0)
154 *retval
= pt
->p_pgrp
->pg_id
;
159 * Get an arbitary pid's session id.
163 getsid(struct proc
*p
, struct getsid_args
*uap
, register_t
*retval
)
171 if ((pt
= pfind(uap
->pid
)) == 0)
174 *retval
= pt
->p_session
->s_sid
;
180 getuid(__unused
struct proc
*p
, __unused
struct getuid_args
*uap
, register_t
*retval
)
183 *retval
= kauth_getruid();
189 geteuid(__unused
struct proc
*p
, __unused
struct geteuid_args
*uap
, register_t
*retval
)
192 *retval
= kauth_getuid();
197 * Return the per-thread override identity.
200 gettid(__unused
struct proc
*p
, struct gettid_args
*uap
, register_t
*retval
)
202 struct uthread
*uthread
= get_bsdthread_info(current_thread());
206 * If this thread is not running with an override identity, we can't
207 * return one to the caller, so return an error instead.
209 if (!(uthread
->uu_flag
& UT_SETUID
))
212 if ((error
= suword(uap
->uidp
, uthread
->uu_ucred
->cr_ruid
)))
214 if ((error
= suword(uap
->gidp
, uthread
->uu_ucred
->cr_rgid
)))
223 getgid(__unused
struct proc
*p
, __unused
struct getgid_args
*uap
, register_t
*retval
)
226 *retval
= kauth_getrgid();
231 * Get effective group ID. The "egid" is groups[0], and could be obtained
232 * via getgroups. This syscall exists because it is somewhat painful to do
233 * correctly in a library function.
237 getegid(struct proc
*p
, __unused
struct getegid_args
*uap
, register_t
*retval
)
240 *retval
= kauth_getgid();
245 getgroups(__unused
struct proc
*p
, struct getgroups_args
*uap
, register_t
*retval
)
251 /* grab reference while we muck around with the credential */
252 cred
= kauth_cred_get_with_ref();
254 if ((ngrp
= uap
->gidsetsize
) == 0) {
255 *retval
= cred
->cr_ngroups
;
256 kauth_cred_rele(cred
);
259 if (ngrp
< cred
->cr_ngroups
) {
260 kauth_cred_rele(cred
);
263 ngrp
= cred
->cr_ngroups
;
264 if ((error
= copyout((caddr_t
)cred
->cr_groups
,
266 ngrp
* sizeof(gid_t
)))) {
267 kauth_cred_rele(cred
);
270 kauth_cred_rele(cred
);
276 * Return the per-thread/per-process supplementary groups list.
278 #warning XXX implement
280 getsgroups(__unused
struct proc
*p
, __unused
struct getsgroups_args
*uap
, __unused register_t
*retval
)
287 * Return the per-thread/per-process whiteout groups list.
289 #warning XXX implement
291 getwgroups(__unused
struct proc
*p
, __unused
struct getwgroups_args
*uap
, __unused register_t
*retval
)
299 setsid(struct proc
*p
, __unused
struct setsid_args
*uap
, register_t
*retval
)
302 if (p
->p_pgid
== p
->p_pid
|| pgfind(p
->p_pid
) || p
->p_flag
& P_INVFORK
) {
305 (void)enterpgrp(p
, p
->p_pid
, 1);
312 * set process group (setpgid/old setpgrp)
314 * caller does setpgid(targpid, targpgid)
316 * pid must be caller or child of caller (ESRCH)
318 * pid must be in same session (EPERM)
319 * pid can't have done an exec (EACCES)
320 * ig pgid is -ve return EINVAL (as per SUV spec)
322 * there must exist some pid in same session having pgid (EPERM)
323 * pid must not be session leader (EPERM)
327 setpgid(struct proc
*curp
, register struct setpgid_args
*uap
, __unused register_t
*retval
)
329 register struct proc
*targp
; /* target process */
330 register struct pgrp
*pgrp
; /* target pgrp */
332 if (uap
->pid
!= 0 && uap
->pid
!= curp
->p_pid
) {
333 if ((targp
= pfind(uap
->pid
)) == 0 || !inferior(targp
))
335 if (targp
->p_session
!= curp
->p_session
)
337 if (targp
->p_flag
& P_EXEC
)
341 if (SESS_LEADER(targp
))
346 uap
->pgid
= targp
->p_pid
;
347 else if (uap
->pgid
!= targp
->p_pid
)
348 if ((pgrp
= pgfind(uap
->pgid
)) == 0 ||
349 pgrp
->pg_session
!= curp
->p_session
)
351 return (enterpgrp(targp
, uap
->pgid
, 0));
355 issetugid(struct proc
*p
, __unused
struct issetugid_args
*uap
, register_t
*retval
)
358 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
359 * we use P_SUGID because we consider changing the owners as
360 * "tainting" as well.
361 * This is significant for procs that start as root and "become"
362 * a user without an exec - programs cannot know *everything*
363 * that libc *might* have put in their data segment.
366 *retval
= (p
->p_flag
& P_SUGID
) ? 1 : 0;
372 setuid(struct proc
*p
, struct setuid_args
*uap
, __unused register_t
*retval
)
376 kauth_cred_t my_cred
, my_new_cred
;
379 AUDIT_ARG(uid
, uid
, 0, 0, 0);
380 if (uid
!= p
->p_ucred
->cr_ruid
&&
381 (error
= suser(p
->p_ucred
, &p
->p_acflag
)))
384 * Everything's okay, do it.
385 * Transfer proc count to new user.
386 * Copy credentials so other references do not see our changes.
389 /* prepare app access profile files */
390 prepare_profile_database(uap
->uid
);
391 (void)chgproccnt(kauth_getruid(), -1);
392 (void)chgproccnt(uid
, 1);
394 /* get current credential and take a reference while we muck with it */
396 my_cred
= kauth_cred_proc_ref(p
);
399 * set the credential with new info. If there is no change we get back
400 * the same credential we passed in.
402 my_new_cred
= kauth_cred_setuid(my_cred
, uid
);
403 if (my_cred
!= my_new_cred
) {
405 /* need to protect for a race where another thread also changed
406 * the credential after we took our reference. If p_ucred has
407 * changed then we should restart this again with the new cred.
409 if (p
->p_ucred
!= my_cred
) {
411 kauth_cred_rele(my_cred
);
412 kauth_cred_rele(my_new_cred
);
416 p
->p_ucred
= my_new_cred
;
417 p
->p_flag
|= P_SUGID
;
420 /* drop our extra reference */
421 kauth_cred_rele(my_cred
);
425 set_security_token(p
);
431 seteuid(struct proc
*p
, struct seteuid_args
*uap
, __unused register_t
*retval
)
435 kauth_cred_t my_cred
, my_new_cred
;
438 AUDIT_ARG(uid
, 0, euid
, 0, 0);
439 if (euid
!= p
->p_ucred
->cr_ruid
&& euid
!= p
->p_ucred
->cr_svuid
&&
440 (error
= suser(p
->p_ucred
, &p
->p_acflag
)))
443 * Everything's okay, do it. Copy credentials so other references do
444 * not see our changes. get current credential and take a reference
445 * while we muck with it
448 my_cred
= kauth_cred_proc_ref(p
);
451 * set the credential with new info. If there is no change we get back
452 * the same credential we passed in.
454 my_new_cred
= kauth_cred_seteuid(p
->p_ucred
, euid
);
456 if (my_cred
!= my_new_cred
) {
458 /* need to protect for a race where another thread also changed
459 * the credential after we took our reference. If p_ucred has
460 * changed then we should restart this again with the new cred.
462 if (p
->p_ucred
!= my_cred
) {
464 kauth_cred_rele(my_cred
);
465 kauth_cred_rele(my_new_cred
);
469 p
->p_ucred
= my_new_cred
;
470 p
->p_flag
|= P_SUGID
;
473 /* drop our extra reference */
474 kauth_cred_rele(my_cred
);
478 set_security_token(p
);
484 setgid(struct proc
*p
, struct setgid_args
*uap
, __unused register_t
*retval
)
488 kauth_cred_t my_cred
, my_new_cred
;
491 AUDIT_ARG(gid
, gid
, 0, 0, 0);
492 if (gid
!= p
->p_ucred
->cr_rgid
&& (error
= suser(p
->p_ucred
, &p
->p_acflag
)))
495 /* get current credential and take a reference while we muck with it */
497 my_cred
= kauth_cred_proc_ref(p
);
500 * set the credential with new info. If there is no change we get back
501 * the same credential we passed in.
503 my_new_cred
= kauth_cred_setgid(p
->p_ucred
, gid
);
504 if (my_cred
!= my_new_cred
) {
506 /* need to protect for a race where another thread also changed
507 * the credential after we took our reference. If p_ucred has
508 * changed then we should restart this again with the new cred.
510 if (p
->p_ucred
!= my_cred
) {
512 kauth_cred_rele(my_cred
);
513 kauth_cred_rele(my_new_cred
);
517 p
->p_ucred
= my_new_cred
;
518 p
->p_flag
|= P_SUGID
;
521 /* drop our extra reference */
522 kauth_cred_rele(my_cred
);
526 set_security_token(p
);
532 setegid(struct proc
*p
, struct setegid_args
*uap
, __unused register_t
*retval
)
536 kauth_cred_t my_cred
, my_new_cred
;
539 AUDIT_ARG(gid
, 0, egid
, 0, 0);
540 if (egid
!= p
->p_ucred
->cr_rgid
&& egid
!= p
->p_ucred
->cr_svgid
&&
541 (error
= suser(p
->p_ucred
, &p
->p_acflag
)))
544 /* get current credential and take a reference while we muck with it */
546 my_cred
= kauth_cred_proc_ref(p
);
549 * set the credential with new info. If there is no change we get back
550 * the same credential we passed in.
552 my_new_cred
= kauth_cred_setegid(p
->p_ucred
, egid
);
553 if (my_cred
!= my_new_cred
) {
555 /* need to protect for a race where another thread also changed
556 * the credential after we took our reference. If p_ucred has
557 * changed then we should restart this again with the new cred.
559 if (p
->p_ucred
!= my_cred
) {
561 kauth_cred_rele(my_cred
);
562 kauth_cred_rele(my_new_cred
);
566 p
->p_ucred
= my_new_cred
;
567 p
->p_flag
|= P_SUGID
;
570 /* drop our extra reference */
571 kauth_cred_rele(my_cred
);
575 set_security_token(p
);
580 * Set the per-thread override identity. The first parameter can be the
581 * current real UID, KAUTH_UID_NONE, or, if the caller is priviledged, it
582 * can be any UID. If it is KAUTH_UID_NONE, then as a special case, this
583 * means "revert to the per process credential"; otherwise, if permitted,
584 * it changes the effective, real, and saved UIDs and GIDs for the current
585 * thread to the requested UID and single GID, and clears all other GIDs.
588 settid(struct proc
*p
, struct settid_args
*uap
, __unused register_t
*retval
)
591 struct uthread
*uthread
= get_bsdthread_info(current_thread());
597 AUDIT_ARG(uid
, uid
, gid
, gid
, 0);
599 if (suser(p
->p_ucred
, &p
->p_acflag
) != 0) {
603 if (uid
== KAUTH_UID_NONE
) {
605 /* must already be assuming another identity in order to revert back */
606 if ((uthread
->uu_flag
& UT_SETUID
) == 0)
609 /* revert to delayed binding of process credential */
610 uc
= kauth_cred_proc_ref(p
);
611 kauth_cred_rele(uthread
->uu_ucred
);
612 uthread
->uu_ucred
= uc
;
613 uthread
->uu_flag
&= ~UT_SETUID
;
615 kauth_cred_t my_cred
, my_new_cred
;
617 /* cannot already be assuming another identity */
618 if ((uthread
->uu_flag
& UT_SETUID
) != 0) {
623 * get a new credential instance from the old if this one changes else
624 * kauth_cred_setuidgid returns the same credential. we take an extra
625 * reference on the current credential while we muck wit it here.
627 kauth_cred_ref(uthread
->uu_ucred
);
628 my_cred
= uthread
->uu_ucred
;
629 my_new_cred
= kauth_cred_setuidgid(my_cred
, uid
, gid
);
630 if (my_cred
!= my_new_cred
)
631 uthread
->uu_ucred
= my_new_cred
;
632 uthread
->uu_flag
|= UT_SETUID
;
634 /* drop our extra reference */
635 kauth_cred_rele(my_cred
);
638 * XXX should potentially set per thread security token (there is
640 * XXX it is unclear whether P_SUGID should be st at this point;
641 * XXX in theory, it is being deprecated.
647 * Set the per-thread override identity. Use this system call for a thread to
648 * assume the identity of another process or to revert back to normal identity
649 * of the current process.
650 * When the "assume" argument is non zero the current thread will assume the
651 * identity of the process represented by the pid argument.
652 * When the assume argument is zero we revert back to our normal identity.
655 settid_with_pid(struct proc
*p
, struct settid_with_pid_args
*uap
, __unused register_t
*retval
)
658 struct uthread
*uthread
= get_bsdthread_info(current_thread());
659 kauth_cred_t my_cred
, my_target_cred
, my_new_cred
;
661 AUDIT_ARG(pid
, uap
->pid
);
662 AUDIT_ARG(value
, uap
->assume
);
664 if (suser(p
->p_ucred
, &p
->p_acflag
) != 0) {
669 * XXX should potentially set per thread security token (there is
671 * XXX it is unclear whether P_SUGID should be st at this point;
672 * XXX in theory, it is being deprecated.
676 * assume argument tells us to assume the identity of the process with the
677 * id passed in the pid argument.
679 if (uap
->assume
!= 0) {
680 /* can't do this if we have already assumed an identity */
681 if ((uthread
->uu_flag
& UT_SETUID
) != 0)
684 target_proc
= pfind(uap
->pid
);
685 /* can't assume the identity of the kernel process */
686 if (target_proc
== NULL
|| target_proc
== kernproc
) {
691 * take a reference on the credential used in our target process then use
692 * it as the identity for our current thread.
694 kauth_cred_ref(uthread
->uu_ucred
);
695 my_cred
= uthread
->uu_ucred
;
696 my_target_cred
= kauth_cred_proc_ref(target_proc
);
697 my_new_cred
= kauth_cred_setuidgid(my_cred
, my_target_cred
->cr_uid
, my_target_cred
->cr_gid
);
698 if (my_cred
!= my_new_cred
)
699 uthread
->uu_ucred
= my_new_cred
;
701 uthread
->uu_flag
|= UT_SETUID
;
703 /* drop our extra references */
704 kauth_cred_rele(my_cred
);
705 kauth_cred_rele(my_target_cred
);
710 /* we are reverting back to normal mode of operation where delayed binding
711 * of the process credential sets the credential in the thread (uu_ucred)
713 if ((uthread
->uu_flag
& UT_SETUID
) == 0)
716 /* revert to delayed binding of process credential */
717 my_new_cred
= kauth_cred_proc_ref(p
);
718 kauth_cred_rele(uthread
->uu_ucred
);
719 uthread
->uu_ucred
= my_new_cred
;
720 uthread
->uu_flag
&= ~UT_SETUID
;
727 setgroups1(struct proc
*p
, u_int gidsetsize
, user_addr_t gidset
, uid_t gmuid
, __unused register_t
*retval
)
730 gid_t newgroups
[NGROUPS
] = { 0 };
732 kauth_cred_t my_cred
, my_new_cred
;
733 struct uthread
*uthread
= get_bsdthread_info(current_thread());
735 if ((error
= suser(p
->p_ucred
, &p
->p_acflag
)))
745 error
= copyin(gidset
,
746 (caddr_t
)newgroups
, ngrp
* sizeof(gid_t
));
752 if ((uthread
->uu_flag
& UT_SETUID
) != 0) {
754 * If this thread is under an assumed identity, set the
755 * supplementary grouplist on the thread credential instead
756 * of the process one. If we were the only reference holder,
757 * the credential is updated in place, otherwise, our reference
758 * is dropped and we get back a different cred with a reference
759 * already held on it. Because this is per-thread, we don't
760 * need the referencing/locking/retry required for per-process.
762 * Hack: this opts into memberd to avoid needing to use a per
763 * thread credential initgroups() instead of setgroups() in
764 * AFP server to address <rdar://4561060>
766 my_cred
= uthread
->uu_ucred
;
767 uthread
->uu_ucred
= kauth_cred_setgroups(my_cred
, &newgroups
[0], ngrp
, my_cred
->cr_gmuid
);
771 * get current credential and take a reference while we muck
775 my_cred
= kauth_cred_proc_ref(p
);
778 * set the credential with new info. If there is no
779 * change we get back the same credential we passed in.
781 my_new_cred
= kauth_cred_setgroups(my_cred
, &newgroups
[0], ngrp
, gmuid
);
782 if (my_cred
!= my_new_cred
) {
785 * need to protect for a race where another
786 * thread also changed the credential after we
787 * took our reference. If p_ucred has
788 * changed then we should restart this again
791 if (p
->p_ucred
!= my_cred
) {
793 kauth_cred_rele(my_cred
);
794 kauth_cred_rele(my_new_cred
);
798 p
->p_ucred
= my_new_cred
;
799 p
->p_flag
|= P_SUGID
;
802 /* drop our extra reference */
803 kauth_cred_rele(my_cred
);
807 AUDIT_ARG(groupset
, p
->p_ucred
->cr_groups
, ngrp
);
808 set_security_token(p
);
815 initgroups(struct proc
*p
, struct initgroups_args
*uap
, __unused register_t
*retval
)
817 return(setgroups1(p
, uap
->gidsetsize
, uap
->gidset
, uap
->gmuid
, retval
));
821 setgroups(struct proc
*p
, struct setgroups_args
*uap
, __unused register_t
*retval
)
823 return(setgroups1(p
, uap
->gidsetsize
, uap
->gidset
, KAUTH_UID_NONE
, retval
));
827 * Set the per-thread/per-process supplementary groups list.
829 #warning XXX implement
831 setsgroups(__unused
struct proc
*p
, __unused
struct setsgroups_args
*uap
, __unused register_t
*retval
)
837 * Set the per-thread/per-process whiteout groups list.
839 #warning XXX implement
841 setwgroups(__unused
struct proc
*p
, __unused
struct setwgroups_args
*uap
, __unused register_t
*retval
)
847 * Check if gid is a member of the group set.
849 * XXX This interface is going away
852 groupmember(gid_t gid
, kauth_cred_t cred
)
856 if (kauth_cred_ismember_gid(cred
, gid
, &is_member
) == 0 && is_member
)
862 * Test whether the specified credentials imply "super-user"
863 * privilege; if so, and we have accounting info, set the flag
864 * indicating use of super-powers.
865 * Returns 0 or error.
867 * XXX This interface is going away
870 suser(kauth_cred_t cred
, u_short
*acflag
)
873 if (cred
== NOCRED
|| cred
== FSCRED
)
876 if (kauth_cred_getuid(cred
) == 0) {
887 struct proc
*p
= current_proc();
892 return (suser(p
->p_ucred
, &p
->p_acflag
) == 0);
898 struct proc
*p
= current_proc();
903 return (suser(p
->p_ucred
, &p
->p_acflag
) == 0 ||
904 p
->p_ucred
->cr_ruid
== 0 || p
->p_ucred
->cr_svuid
== 0);
908 * Get login name, if available.
912 getlogin(struct proc
*p
, struct getlogin_args
*uap
, __unused register_t
*retval
)
915 if (uap
->namelen
> sizeof (p
->p_pgrp
->pg_session
->s_login
))
916 uap
->namelen
= sizeof (p
->p_pgrp
->pg_session
->s_login
);
917 return (copyout((caddr_t
) p
->p_pgrp
->pg_session
->s_login
,
918 uap
->namebuf
, uap
->namelen
));
926 setlogin(struct proc
*p
, struct setlogin_args
*uap
, __unused register_t
*retval
)
931 if ((error
= suser(p
->p_ucred
, &p
->p_acflag
)))
934 error
= copyinstr(uap
->namebuf
,
935 (caddr_t
) p
->p_pgrp
->pg_session
->s_login
,
936 sizeof (p
->p_pgrp
->pg_session
->s_login
) - 1, (size_t *)&dummy
);
938 AUDIT_ARG(text
, p
->p_pgrp
->pg_session
->s_login
);
939 else if (error
== ENAMETOOLONG
)
945 /* Set the secrity token of the task with current euid and eguid */
947 * XXX This needs to change to give the task a reference and/or an opaque
951 set_security_token(struct proc
* p
)
953 security_token_t sec_token
;
954 audit_token_t audit_token
;
957 * Don't allow a vfork child to override the parent's token settings
958 * (since they share a task). Instead, the child will just have to
959 * suffer along using the parent's token until the exec(). It's all
960 * undefined behavior anyway, right?
962 if (p
->task
== current_task()) {
964 uthread
= (uthread_t
)get_bsdthread_info(current_thread());
965 if (uthread
->uu_flag
& UT_VFORK
)
969 /* XXX mach_init doesn't have a p_ucred when it calls this function */
970 if (p
->p_ucred
!= NOCRED
&& p
->p_ucred
!= FSCRED
) {
971 sec_token
.val
[0] = kauth_cred_getuid(p
->p_ucred
);
972 sec_token
.val
[1] = p
->p_ucred
->cr_gid
;
974 sec_token
.val
[0] = 0;
975 sec_token
.val
[1] = 0;
979 * The current layout of the Mach audit token explicitly
980 * adds these fields. But nobody should rely on such
981 * a literal representation. Instead, the BSM library
982 * provides a function to convert an audit token into
983 * a BSM subject. Use of that mechanism will isolate
984 * the user of the trailer from future representation
987 audit_token
.val
[0] = p
->p_ucred
->cr_au
.ai_auid
;
988 audit_token
.val
[1] = p
->p_ucred
->cr_uid
;
989 audit_token
.val
[2] = p
->p_ucred
->cr_gid
;
990 audit_token
.val
[3] = p
->p_ucred
->cr_ruid
;
991 audit_token
.val
[4] = p
->p_ucred
->cr_rgid
;
992 audit_token
.val
[5] = p
->p_pid
;
993 audit_token
.val
[6] = p
->p_ucred
->cr_au
.ai_asid
;
994 audit_token
.val
[7] = p
->p_ucred
->cr_au
.ai_termid
.port
;
996 return (host_security_set_task_token(host_security_self(),
1000 (sec_token
.val
[0]) ?
1002 host_priv_self()) != KERN_SUCCESS
);
1007 * Fill in a struct xucred based on a kauth_cred_t.
1011 cru2x(kauth_cred_t cr
, struct xucred
*xcr
)
1014 bzero(xcr
, sizeof(*xcr
));
1015 xcr
->cr_version
= XUCRED_VERSION
;
1016 xcr
->cr_uid
= kauth_cred_getuid(cr
);
1017 xcr
->cr_ngroups
= cr
->cr_ngroups
;
1018 bcopy(cr
->cr_groups
, xcr
->cr_groups
, sizeof(xcr
->cr_groups
));