2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1982, 1986, 1989, 1990, 1991, 1993
25 * The Regents of the University of California. All rights reserved.
26 * (c) UNIX System Laboratories, Inc.
27 * All or some portions of this file are derived from material licensed
28 * to the University of California by American Telephone and Telegraph
29 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
30 * the permission of UNIX System Laboratories, Inc.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)kern_prot.c 8.9 (Berkeley) 2/14/95
64 * System calls related to processes and protection
67 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/ucred.h>
72 #include <sys/timeb.h>
73 #include <sys/times.h>
74 #include <sys/malloc.h>
76 #include <sys/mount.h>
77 #include <mach/message.h>
78 #include <kern/host.h>
81 * setprivexec: (dis)allow this process to hold
82 * task, thread, or execption ports of processes about to exec.
84 struct setprivexec_args
{
88 setprivexec(p
, uap
, retval
)
90 register struct setprivexec_args
*uap
;
93 *retval
= p
->p_debugger
;
94 p
->p_debugger
= (uap
->flag
!= 0);
99 getpid(p
, uap
, retval
)
107 retval
[1] = p
->p_pptr
->p_pid
;
113 getppid(p
, uap
, retval
)
119 *retval
= p
->p_pptr
->p_pid
;
123 /* Get process group ID; note that POSIX getpgrp takes no parameter */
124 getpgrp(p
, uap
, retval
)
130 *retval
= p
->p_pgrp
->pg_id
;
135 getuid(p
, uap
, retval
)
141 *retval
= p
->p_cred
->p_ruid
;
143 retval
[1] = p
->p_ucred
->cr_uid
;
149 geteuid(p
, uap
, retval
)
155 *retval
= p
->p_ucred
->cr_uid
;
160 getgid(p
, uap
, retval
)
166 *retval
= p
->p_cred
->p_rgid
;
168 retval
[1] = p
->p_ucred
->cr_groups
[0];
174 * Get effective group ID. The "egid" is groups[0], and could be obtained
175 * via getgroups. This syscall exists because it is somewhat painful to do
176 * correctly in a library function.
179 getegid(p
, uap
, retval
)
185 *retval
= p
->p_ucred
->cr_groups
[0];
189 struct getgroups_args
{
193 getgroups(p
, uap
, retval
)
195 register struct getgroups_args
*uap
;
198 register struct pcred
*pc
= p
->p_cred
;
202 if ((ngrp
= uap
->gidsetsize
) == 0) {
203 *retval
= pc
->pc_ucred
->cr_ngroups
;
206 if (ngrp
< pc
->pc_ucred
->cr_ngroups
)
209 ngrp
= pc
->pc_ucred
->cr_ngroups
;
210 if (error
= copyout((caddr_t
)pc
->pc_ucred
->cr_groups
,
211 (caddr_t
)uap
->gidset
, ngrp
* sizeof(gid_t
))) {
221 setsid(p
, uap
, retval
)
222 register struct proc
*p
;
227 if (p
->p_pgid
== p
->p_pid
|| pgfind(p
->p_pid
)) {
230 (void)enterpgrp(p
, p
->p_pid
, 1);
237 * set process group (setpgid/old setpgrp)
239 * caller does setpgid(targpid, targpgid)
241 * pid must be caller or child of caller (ESRCH)
243 * pid must be in same session (EPERM)
244 * pid can't have done an exec (EACCES)
246 * there must exist some pid in same session having pgid (EPERM)
247 * pid must not be session leader (EPERM)
249 struct setpgid_args
{
254 setpgid(curp
, uap
, retval
)
256 register struct setpgid_args
*uap
;
259 register struct proc
*targp
; /* target process */
260 register struct pgrp
*pgrp
; /* target pgrp */
262 if (uap
->pid
!= 0 && uap
->pid
!= curp
->p_pid
) {
263 if ((targp
= pfind(uap
->pid
)) == 0 || !inferior(targp
))
265 if (targp
->p_session
!= curp
->p_session
)
267 if (targp
->p_flag
& P_EXEC
)
271 if (SESS_LEADER(targp
))
274 uap
->pgid
= targp
->p_pid
;
275 else if (uap
->pgid
!= targp
->p_pid
)
276 if ((pgrp
= pgfind(uap
->pgid
)) == 0 ||
277 pgrp
->pg_session
!= curp
->p_session
)
279 return (enterpgrp(targp
, uap
->pgid
, 0));
286 setuid(p
, uap
, retval
)
288 struct setuid_args
*uap
;
291 register struct pcred
*pc
= p
->p_cred
;
296 if (uid
!= pc
->p_ruid
&&
297 (error
= suser(pc
->pc_ucred
, &p
->p_acflag
)))
300 * Everything's okay, do it.
301 * Transfer proc count to new user.
302 * Copy credentials so other references do not see our changes.
305 (void)chgproccnt(pc
->p_ruid
, -1);
306 (void)chgproccnt(uid
, 1);
307 pc
->pc_ucred
= crcopy(pc
->pc_ucred
);
308 pc
->pc_ucred
->cr_uid
= uid
;
312 set_security_token(p
);
313 p
->p_flag
|= P_SUGID
;
317 struct seteuid_args
{
321 seteuid(p
, uap
, retval
)
323 struct seteuid_args
*uap
;
326 register struct pcred
*pc
= p
->p_cred
;
331 if (euid
!= pc
->p_ruid
&& euid
!= pc
->p_svuid
&&
332 (error
= suser(pc
->pc_ucred
, &p
->p_acflag
)))
335 * Everything's okay, do it. Copy credentials so other references do
336 * not see our changes.
339 pc
->pc_ucred
= crcopy(pc
->pc_ucred
);
340 pc
->pc_ucred
->cr_uid
= euid
;
342 set_security_token(p
);
343 p
->p_flag
|= P_SUGID
;
351 setgid(p
, uap
, retval
)
353 struct setgid_args
*uap
;
356 register struct pcred
*pc
= p
->p_cred
;
361 if (gid
!= pc
->p_rgid
&& (error
= suser(pc
->pc_ucred
, &p
->p_acflag
)))
364 pc
->pc_ucred
= crcopy(pc
->pc_ucred
);
365 pc
->pc_ucred
->cr_groups
[0] = gid
;
367 pc
->p_svgid
= gid
; /* ??? */
369 set_security_token(p
);
370 p
->p_flag
|= P_SUGID
;
374 struct setegid_args
{
378 setegid(p
, uap
, retval
)
380 struct setegid_args
*uap
;
383 register struct pcred
*pc
= p
->p_cred
;
388 if (egid
!= pc
->p_rgid
&& egid
!= pc
->p_svgid
&&
389 (error
= suser(pc
->pc_ucred
, &p
->p_acflag
)))
392 pc
->pc_ucred
= crcopy(pc
->pc_ucred
);
393 pc
->pc_ucred
->cr_groups
[0] = egid
;
395 set_security_token(p
);
396 p
->p_flag
|= P_SUGID
;
400 struct setgroups_args
{
406 setgroups(p
, uap
, retval
)
408 struct setgroups_args
*uap
;
411 register struct pcred
*pc
= p
->p_cred
;
412 struct ucred
*new, *old
;
416 if (error
= suser(pc
->pc_ucred
, &p
->p_acflag
))
418 ngrp
= uap
->gidsetsize
;
419 if (ngrp
< 1 || ngrp
> NGROUPS
)
422 error
= copyin((caddr_t
)uap
->gidset
,
423 (caddr_t
)new->cr_groups
, ngrp
* sizeof(gid_t
));
428 new->cr_ngroups
= ngrp
;
431 new->cr_uid
= old
->cr_uid
;
434 set_security_token(p
);
435 p
->p_flag
|= P_SUGID
;
442 struct osetreuid_args
{
447 osetreuid(p
, uap
, retval
)
448 register struct proc
*p
;
449 struct osetreuid_args
*uap
;
452 struct seteuid_args seuidargs
;
453 struct setuid_args suidargs
;
456 * There are five cases, and we attempt to emulate them in
457 * the following fashion:
458 * -1, -1: return 0. This is correct emulation.
459 * -1, N: call seteuid(N). This is correct emulation.
460 * N, -1: if we called setuid(N), our euid would be changed
461 * to N as well. the theory is that we don't want to
462 * revoke root access yet, so we call seteuid(N)
463 * instead. This is incorrect emulation, but often
464 * suffices enough for binary compatibility.
465 * N, N: call setuid(N). This is correct emulation.
466 * N, M: call setuid(N). This is close to correct emulation.
468 if (uap
->ruid
== (uid_t
)-1) {
469 if (uap
->euid
== (uid_t
)-1)
470 return (0); /* -1, -1 */
471 seuidargs
.euid
= uap
->euid
; /* -1, N */
472 return (seteuid(p
, &seuidargs
, retval
));
474 if (uap
->euid
== (uid_t
)-1) {
475 seuidargs
.euid
= uap
->ruid
; /* N, -1 */
476 return (seteuid(p
, &seuidargs
, retval
));
478 suidargs
.uid
= uap
->ruid
; /* N, N and N, M */
479 return (setuid(p
, &suidargs
, retval
));
482 struct osetregid_args
{
487 osetregid(p
, uap
, retval
)
488 register struct proc
*p
;
489 struct osetregid_args
*uap
;
492 struct setegid_args segidargs
;
493 struct setgid_args sgidargs
;
496 * There are five cases, described above in osetreuid()
498 if (uap
->rgid
== (gid_t
)-1) {
499 if (uap
->egid
== (gid_t
)-1)
500 return (0); /* -1, -1 */
501 segidargs
.egid
= uap
->egid
; /* -1, N */
502 return (setegid(p
, &segidargs
, retval
));
504 if (uap
->egid
== (gid_t
)-1) {
505 segidargs
.egid
= uap
->rgid
; /* N, -1 */
506 return (setegid(p
, &segidargs
, retval
));
508 sgidargs
.gid
= uap
->rgid
; /* N, N and N, M */
509 return (setgid(p
, &sgidargs
, retval
));
511 #endif /* COMPAT_43 */
514 * Check if gid is a member of the group set.
516 groupmember(gid
, cred
)
518 register struct ucred
*cred
;
523 egp
= &(cred
->cr_groups
[cred
->cr_ngroups
]);
524 for (gp
= cred
->cr_groups
; gp
< egp
; gp
++)
531 * Test whether the specified credentials imply "super-user"
532 * privilege; if so, and we have accounting info, set the flag
533 * indicating use of super-powers.
534 * Returns 0 or error.
541 if (cred
== NOCRED
|| cred
== FSCRED
)
544 if (cred
->cr_uid
== 0) {
555 struct proc
*p
= current_proc();
560 return (suser(p
->p_ucred
, &p
->p_acflag
) == 0);
566 struct proc
*p
= current_proc();
571 return (suser(p
->p_ucred
, &p
->p_acflag
) == 0 ||
572 p
->p_cred
->p_ruid
== 0 || p
->p_cred
->p_svuid
== 0);
576 * Allocate a zeroed cred structure.
581 register struct ucred
*cr
;
583 MALLOC_ZONE(cr
, struct ucred
*, sizeof(*cr
), M_CRED
, M_WAITOK
);
584 bzero((caddr_t
)cr
, sizeof(*cr
));
590 * Free a cred structure.
591 * Throws away space when ref count gets to 0.
598 if (cr
== NOCRED
|| cr
== FSCRED
)
601 if (--cr
->cr_ref
== 0)
602 FREE_ZONE((caddr_t
)cr
, sizeof *cr
, M_CRED
);
606 * Copy cred structure to a new one and free the old one.
615 if (cr
== NOCRED
|| cr
== FSCRED
)
628 * Dup cred struct to a new held one.
637 if (cr
== NOCRED
|| cr
== FSCRED
)
647 * Get login name, if available.
649 struct getlogin_args
{
654 getlogin(p
, uap
, retval
)
656 struct getlogin_args
*uap
;
660 if (uap
->namelen
> sizeof (p
->p_pgrp
->pg_session
->s_login
))
661 uap
->namelen
= sizeof (p
->p_pgrp
->pg_session
->s_login
);
662 return (copyout((caddr_t
) p
->p_pgrp
->pg_session
->s_login
,
663 (caddr_t
)uap
->namebuf
, uap
->namelen
));
669 struct setlogin_args
{
673 setlogin(p
, uap
, retval
)
675 struct setlogin_args
*uap
;
681 if (error
= suser(p
->p_ucred
, &p
->p_acflag
))
684 error
= copyinstr((caddr_t
) uap
->namebuf
,
685 (caddr_t
) p
->p_pgrp
->pg_session
->s_login
,
686 sizeof (p
->p_pgrp
->pg_session
->s_login
) - 1, (size_t *)&dummy
);
687 if (error
== ENAMETOOLONG
)
693 /* Set the secrity token of the task with current euid and eguid */
695 set_security_token(struct proc
* p
)
697 security_token_t sec_token
;
699 sec_token
.val
[0] = p
->p_ucred
->cr_uid
;
700 sec_token
.val
[1] = p
->p_ucred
->cr_gid
;
701 (void)host_security_set_task_token(host_security_self(),