X-Git-Url: https://git.saurik.com/apple/xnu.git/blobdiff_plain/9bccf70c0258c7cac2dcb80011b2a964d884c552..4452a7af2eac33dbad800bcc91f2399d62c18f53:/bsd/kern/kern_fork.c diff --git a/bsd/kern/kern_fork.c b/bsd/kern/kern_fork.c index c34b51b31..b4be0c5e5 100644 --- a/bsd/kern/kern_fork.c +++ b/bsd/kern/kern_fork.c @@ -1,23 +1,29 @@ /* - * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved. + * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved. * - * @APPLE_LICENSE_HEADER_START@ + * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * - * The contents of this file constitute Original Code as defined in and - * are subject to the Apple Public Source License Version 1.1 (the - * "License"). You may not use this file except in compliance with the - * License. Please obtain a copy of the License at - * http://www.apple.com/publicsource and read it before using this file. + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. The rights granted to you under the License + * may not be used to create, or enable the creation or redistribution of, + * unlawful or unlicensed copies of an Apple operating system, or to + * circumvent, violate, or enable the circumvention or violation of, any + * terms of an Apple operating system software license agreement. * - * This Original Code and all software distributed under the License are - * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the - * License for the specific language governing rights and limitations - * under the License. + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. * - * @APPLE_LICENSE_HEADER_END@ + * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */ /* @@ -60,29 +66,39 @@ * @(#)kern_fork.c 8.8 (Berkeley) 2/14/95 */ +#include #include #include #include #include #include -#include +#include +#include #include #include -#include -#include +#include +#include #include #if KTRACE #include #endif +#include + #include +#include +#include #include +#include +#include #include -thread_act_t cloneproc(struct proc *, int); +#include // for vm_map_commpage64 + +thread_t cloneproc(struct proc *, int); struct proc * forkproc(struct proc *, int); -thread_act_t procdup(); +thread_t procdup(struct proc *child, struct proc *parent); #define DOFORK 0x1 /* fork() system call */ #define DOVFORK 0x2 /* vfork() system call */ @@ -92,10 +108,7 @@ static int fork1(struct proc *, long, register_t *); * fork system call. */ int -fork(p, uap, retval) - struct proc *p; - void *uap; - register_t *retval; +fork(struct proc *p, __unused void *uap, register_t *retval) { return (fork1(p, (long)DOFORK, retval)); } @@ -104,18 +117,15 @@ fork(p, uap, retval) * vfork system call */ int -vfork(p, uap, retval) - struct proc *p; - void *uap; - register_t *retval; +vfork(struct proc *p, void *uap, register_t *retval) { register struct proc * newproc; register uid_t uid; - thread_act_t cur_act = (thread_act_t)current_act(); + thread_t cur_act = (thread_t)current_thread(); int count; task_t t; uthread_t ut; - + /* * Although process entries are dynamically created, we still keep * a global limit on the maximum number we will create. Don't allow @@ -123,7 +133,7 @@ vfork(p, uap, retval) * exceed the limit. The variable nprocs is the current number of * processes, maxproc is the limit. */ - uid = p->p_cred->p_ruid; + uid = kauth_cred_get()->cr_ruid; if ((nprocs >= maxproc - 1 && uid != 0) || nprocs >= maxproc) { tablefull("proc"); retval[1] = 0; @@ -141,8 +151,9 @@ vfork(p, uap, retval) } ut = (struct uthread *)get_bsdthread_info(cur_act); - if (ut->uu_flag & P_VFORK) { + if (ut->uu_flag & UT_VFORK) { printf("vfork called recursively by %s\n", p->p_comm); + (void)chgproccnt(uid, -1); return (EINVAL); } p->p_flag |= P_VFORK; @@ -151,6 +162,8 @@ vfork(p, uap, retval) /* The newly created process comes with signal lock held */ newproc = (struct proc *)forkproc(p,1); + AUDIT_ARG(pid, newproc->p_pid); + LIST_INSERT_AFTER(p, newproc, p_pglist); newproc->p_pptr = p; newproc->task = p->task; @@ -163,14 +176,20 @@ vfork(p, uap, retval) newproc->p_flag |= P_INVFORK; newproc->p_vforkact = cur_act; - ut->uu_flag |= P_VFORK; + ut->uu_flag |= UT_VFORK; ut->uu_proc = newproc; ut->uu_userstate = (void *)act_thread_csave(); ut->uu_vforkmask = ut->uu_sigmask; + /* temporarily drop thread-set-id state */ + if (ut->uu_flag & UT_SETUID) { + ut->uu_flag |= UT_WASSETUID; + ut->uu_flag &= ~UT_SETUID; + } + thread_set_child(cur_act, newproc->p_pid); - newproc->p_stats->p_start = time; + microtime(&newproc->p_stats->p_start); newproc->p_acflag = AFORK; /* @@ -193,18 +212,10 @@ vfork(p, uap, retval) * Return to parent vfork ehread() */ void -vfork_return(th_act, p, p2, retval) - thread_act_t th_act; - struct proc * p; - struct proc *p2; - register_t *retval; +vfork_return(__unused thread_t th_act, struct proc *p, struct proc *p2, + register_t *retval) { - long flags; - register uid_t uid; - thread_t newth, self = current_thread(); - thread_act_t cur_act = (thread_act_t)current_act(); - int s, count; - task_t t; + thread_t cur_act = (thread_t)current_thread(); uthread_t ut; ut = (struct uthread *)get_bsdthread_info(cur_act); @@ -218,7 +229,12 @@ vfork_return(th_act, p, p2, retval) if (p->p_vforkcnt <=0) p->p_flag &= ~P_VFORK; ut->uu_userstate = 0; - ut->uu_flag &= ~P_VFORK; + ut->uu_flag &= ~UT_VFORK; + /* restore thread-set-id state */ + if (ut->uu_flag & UT_WASSETUID) { + ut->uu_flag |= UT_SETUID; + ut->uu_flag &= UT_WASSETUID; + } ut->uu_proc = 0; ut->uu_sigmask = ut->uu_vforkmask; p2->p_flag &= ~P_INVFORK; @@ -234,27 +250,47 @@ vfork_return(th_act, p, p2, retval) return; } -thread_act_t -procdup( - struct proc *child, - struct proc *parent) +thread_t +procdup(struct proc *child, struct proc *parent) { - thread_act_t thread; + thread_t thread; task_t task; kern_return_t result; - extern task_t kernel_task; if (parent->task == kernel_task) - result = task_create_local(TASK_NULL, FALSE, FALSE, &task); + result = task_create_internal(TASK_NULL, FALSE, FALSE, &task); else - result = task_create_local(parent->task, TRUE, FALSE, &task); + result = task_create_internal(parent->task, TRUE, (parent->p_flag & P_LP64), &task); if (result != KERN_SUCCESS) printf("fork/procdup: task_create failed. Code: 0x%x\n", result); child->task = task; /* task->proc = child; */ set_bsdtask_info(task, child); + if (parent->p_flag & P_LP64) { + task_set_64bit(task, TRUE); + vm_map_set_64bit(get_task_map(task)); + child->p_flag |= P_LP64; + /* LP64todo - clean up this hacked mapping of commpage */ + pmap_map_sharedpage(task, get_map_pmap(get_task_map(task))); + vm_map_commpage64(get_task_map(task)); + } else { + task_set_64bit(task, FALSE); + vm_map_set_32bit(get_task_map(task)); + child->p_flag &= ~P_LP64; +#ifdef __i386__ + /* + * On Intel, the comm page doesn't get mapped automatically + * because it goes beyond the end of the VM map in the current + * 3GB/1GB address space model. + * XXX This explicit mapping will probably become unnecessary + * when we switch to the new 4GB/4GB address space model. + */ + vm_map_commpage32(get_task_map(task)); +#endif /* __i386__ */ + } if (child->p_nice != 0) resetpriority(child); + result = thread_create(task, &thread); if (result != KERN_SUCCESS) printf("fork/procdup: thread_create failed. Code: 0x%x\n", result); @@ -271,9 +307,9 @@ fork1(p1, flags, retval) { register struct proc *p2; register uid_t uid; - thread_act_t newth; - int s, count; - task_t t; + thread_t newth; + int count; + task_t t; /* * Although process entries are dynamically created, we still keep @@ -282,7 +318,7 @@ fork1(p1, flags, retval) * exceed the limit. The variable nprocs is the current number of * processes, maxproc is the limit. */ - uid = p1->p_cred->p_ruid; + uid = kauth_cred_get()->cr_ruid; if ((nprocs >= maxproc - 1 && uid != 0) || nprocs >= maxproc) { tablefull("proc"); retval[1] = 0; @@ -304,12 +340,13 @@ fork1(p1, flags, retval) thread_dup(newth); /* p2 = newth->task->proc; */ p2 = (struct proc *)(get_bsdtask_info(get_threadtask(newth))); + set_security_token(p2); /* propagate change of PID */ + + AUDIT_ARG(pid, p2->p_pid); thread_set_child(newth, p2->p_pid); - s = splhigh(); - p2->p_stats->p_start = time; - splx(s); + microtime(&p2->p_stats->p_start); p2->p_acflag = AFORK; /* @@ -325,10 +362,12 @@ fork1(p1, flags, retval) (void) thread_resume(newth); /* drop the extra references we got during the creation */ - if (t = (task_t)get_threadtask(newth)) { + if ((t = (task_t)get_threadtask(newth)) != NULL) { task_deallocate(t); } - act_deallocate(newth); + thread_deallocate(newth); + + KNOTE(&p1->p_klist, NOTE_FORK | p2->p_pid); while (p2->p_flag & P_PPWAIT) tsleep(p1, PWAIT, "ppwait", 0); @@ -348,13 +387,13 @@ fork1(p1, flags, retval) * lock set. fork() code needs to explicity remove this lock * before signals can be delivered */ -thread_act_t +thread_t cloneproc(p1, lock) register struct proc *p1; register int lock; { register struct proc *p2; - thread_act_t th; + thread_t th; p2 = (struct proc *)forkproc(p1,lock); @@ -383,17 +422,20 @@ forkproc(p1, lock) { register struct proc *p2, *newproc; static int nextpid = 0, pidchecked = 0; - thread_t th; /* Allocate new proc. */ MALLOC_ZONE(newproc, struct proc *, sizeof *newproc, M_PROC, M_WAITOK); - MALLOC_ZONE(newproc->p_cred, struct pcred *, - sizeof *newproc->p_cred, M_SUBPROC, M_WAITOK); + if (newproc == NULL) + panic("forkproc: M_PROC zone exhausted"); MALLOC_ZONE(newproc->p_stats, struct pstats *, sizeof *newproc->p_stats, M_SUBPROC, M_WAITOK); + if (newproc->p_stats == NULL) + panic("forkproc: M_SUBPROC zone exhausted (p_stats)"); MALLOC_ZONE(newproc->p_sigacts, struct sigacts *, sizeof *newproc->p_sigacts, M_SUBPROC, M_WAITOK); + if (newproc->p_sigacts == NULL) + panic("forkproc: M_SUBPROC zone exhausted (p_sigacts)"); /* * Find an unused process ID. We remember a range of unused IDs @@ -448,6 +490,7 @@ again: nprocs++; p2 = newproc; p2->p_stat = SIDL; + p2->p_shutdownstate = 0; p2->p_pid = nextpid; /* @@ -462,26 +505,35 @@ again: p2->vm_shm = (void *)NULL; /* Make sure it is zero */ /* + * Some flags are inherited from the parent. * Duplicate sub-structures as needed. * Increase reference counts on shared objects. * The p_stats and p_sigacts substructs are set in vm_fork. */ - p2->p_flag = P_INMEM; + p2->p_flag = (p1->p_flag & (P_LP64 | P_TRANSLATED | P_AFFINITY)); if (p1->p_flag & P_PROFIL) startprofclock(p2); - bcopy(p1->p_cred, p2->p_cred, sizeof(*p2->p_cred)); - p2->p_cred->p_refcnt = 1; - crhold(p1->p_ucred); - lockinit(&p2->p_cred->pc_lock, PLOCK, "proc cred", 0, 0); + /* + * Note that if the current thread has an assumed identity, this + * credential will be granted to the new process. + */ + p2->p_ucred = kauth_cred_get_with_ref(); + + lck_mtx_init(&p2->p_mlock, proc_lck_grp, proc_lck_attr); + lck_mtx_init(&p2->p_fdmlock, proc_lck_grp, proc_lck_attr); + klist_init(&p2->p_klist); /* bump references to the text vnode */ p2->p_textvp = p1->p_textvp; - if (p2->p_textvp) - VREF(p2->p_textvp); - + if (p2->p_textvp) { + vnode_rele(p2->p_textvp); + } + /* XXX may fail to copy descriptors to child */ p2->p_fd = fdcopy(p1); + if (p1->vm_shm) { - shmfork(p1,p2); + /* XXX may fail to attach shm to child */ + (void)shmfork(p1,p2); } /* * If p_limit is still copy-on-write, bump refcnt, @@ -503,6 +555,8 @@ again: ((caddr_t)&p2->p_stats->pstat_endcopy - (caddr_t)&p2->p_stats->pstat_startcopy)); + bzero(&p2->p_stats->user_p_prof, sizeof(struct user_uprof)); + if (p1->p_sigacts != NULL) (void)memcpy(p2->p_sigacts, p1->p_sigacts, sizeof *p2->p_sigacts); @@ -512,6 +566,8 @@ again: if (p1->p_session->s_ttyvp != NULL && p1->p_flag & P_CONTROLT) p2->p_flag |= P_CONTROLT; + p2->p_argslen = p1->p_argslen; + p2->p_argc = p1->p_argc; p2->p_xstat = 0; p2->p_ru = NULL; @@ -524,10 +580,16 @@ again: p2->sigwait_thread = NULL; p2->exit_thread = NULL; p2->user_stack = p1->user_stack; - p2->p_xxxsigpending = 0; p2->p_vforkcnt = 0; p2->p_vforkact = 0; + p2->p_lflag = 0; + p2->p_ladvflag = 0; + p2->p_internalref = 0; TAILQ_INIT(&p2->p_uthlist); + TAILQ_INIT(&p2->aio_activeq); + TAILQ_INIT(&p2->aio_doneq); + p2->aio_active_count = 0; + p2->aio_done_count = 0; #if KTRACE /* @@ -536,38 +598,50 @@ again: */ if (p1->p_traceflag&KTRFAC_INHERIT) { p2->p_traceflag = p1->p_traceflag; - if ((p2->p_tracep = p1->p_tracep) != NULL) - VREF(p2->p_tracep); + if ((p2->p_tracep = p1->p_tracep) != NULL) { + vnode_ref(p2->p_tracep); + } } #endif return(p2); } +void +proc_lock(proc_t p) +{ + lck_mtx_lock(&p->p_mlock); +} + +void +proc_unlock(proc_t p) +{ + lck_mtx_unlock(&p->p_mlock); +} + #include struct zone *uthread_zone; int uthread_zone_inited = 0; void -uthread_zone_init() +uthread_zone_init(void) { if (!uthread_zone_inited) { uthread_zone = zinit(sizeof(struct uthread), - THREAD_MAX * sizeof(struct uthread), - THREAD_CHUNK * sizeof(struct uthread), - "uthreads"); + THREAD_MAX * sizeof(struct uthread), + THREAD_CHUNK * sizeof(struct uthread), + "uthreads"); uthread_zone_inited = 1; } } void * -uthread_alloc(task_t task, thread_act_t thr_act ) +uthread_alloc(task_t task, thread_t thr_act ) { struct proc *p; struct uthread *uth, *uth_parent; void *ut; - extern task_t kernel_task; boolean_t funnel_state; if (!uthread_zone_inited) @@ -576,22 +650,49 @@ uthread_alloc(task_t task, thread_act_t thr_act ) ut = (void *)zalloc(uthread_zone); bzero(ut, sizeof(struct uthread)); - if (task != kernel_task) { - uth = (struct uthread *)ut; - p = get_bsdtask_info(task); + p = (struct proc *) get_bsdtask_info(task); + uth = (struct uthread *)ut; + /* + * Thread inherits credential from the creating thread, if both + * are in the same task. + * + * If the creating thread has no credential or is from another + * task we can leave the new thread credential NULL. If it needs + * one later, it will be lazily assigned from the task's process. + */ + uth_parent = (struct uthread *)get_bsdthread_info(current_thread()); + if ((task == current_task()) && + (uth_parent != NULL) && + (IS_VALID_CRED(uth_parent->uu_ucred))) { + /* + * XXX The new thread is, in theory, being created in context + * XXX of parent thread, so a direct reference to the parent + * XXX is OK. + */ + kauth_cred_ref(uth_parent->uu_ucred); + uth->uu_ucred = uth_parent->uu_ucred; + /* the credential we just inherited is an assumed credential */ + if (uth_parent->uu_flag & UT_SETUID) + uth->uu_flag |= UT_SETUID; + } else { + uth->uu_ucred = NOCRED; + } + + if (task != kernel_task) { + funnel_state = thread_funnel_set(kernel_flock, TRUE); - uth_parent = (struct uthread *)get_bsdthread_info(current_act()); if (uth_parent) { - if (uth_parent->uu_flag & USAS_OLDMASK) + if (uth_parent->uu_flag & UT_SAS_OLDMASK) uth->uu_sigmask = uth_parent->uu_oldmask; else uth->uu_sigmask = uth_parent->uu_sigmask; } uth->uu_act = thr_act; //signal_lock(p); - if (p) + if (p) { TAILQ_INSERT_TAIL(&p->p_uthlist, uth, uu_list); + } //signal_unlock(p); (void)thread_funnel_set(kernel_flock, funnel_state); } @@ -606,25 +707,37 @@ uthread_free(task_t task, void *uthread, void * bsd_info) struct _select *sel; struct uthread *uth = (struct uthread *)uthread; struct proc * p = (struct proc *)bsd_info; - extern task_t kernel_task; - int size; boolean_t funnel_state; - sel = &uth->uu_state.ss_select; + /* + * Per-thread audit state should never last beyond system + * call return. Since we don't audit the thread creation/ + * removal, the thread state pointer should never be + * non-NULL when we get here. + */ + assert(uth->uu_ar == NULL); + + sel = &uth->uu_select; /* cleanup the select bit space */ if (sel->nbytes) { FREE(sel->ibits, M_TEMP); FREE(sel->obits, M_TEMP); } - if (sel->allocsize && uth->uu_wqsub){ - kfree(uth->uu_wqsub, sel->allocsize); - sel->count = sel->nfcount = 0; + if (sel->allocsize && sel->wqset){ + kfree(sel->wqset, sel->allocsize); + sel->count = 0; sel->allocsize = 0; - uth->uu_wqsub = 0; + sel->wqset = 0; sel->wql = 0; } + if (IS_VALID_CRED(uth->uu_ucred)) { + kauth_cred_t oldcred = uth->uu_ucred; + uth->uu_ucred = NOCRED; + kauth_cred_unref(&oldcred); + } + if ((task != kernel_task) && p) { funnel_state = thread_funnel_set(kernel_flock, TRUE); //signal_lock(p); @@ -633,5 +746,5 @@ uthread_free(task_t task, void *uthread, void * bsd_info) (void)thread_funnel_set(kernel_flock, funnel_state); } /* and free the uthread itself */ - zfree(uthread_zone, (vm_offset_t)uthread); + zfree(uthread_zone, uthread); }