From owner-freebsd-current@FreeBSD.ORG Sat Mar 21 01:51:58 2015 Return-Path: Delivered-To: freebsd-current@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 96226253; Sat, 21 Mar 2015 01:51:58 +0000 (UTC) Received: from kib.kiev.ua (kib.kiev.ua [IPv6:2001:470:d5e7:1::1]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 37E46D8A; Sat, 21 Mar 2015 01:51:58 +0000 (UTC) Received: from tom.home (kostik@localhost [127.0.0.1]) by kib.kiev.ua (8.14.9/8.14.9) with ESMTP id t2L1ppKv022487 (version=TLSv1/SSLv3 cipher=DHE-RSA-CAMELLIA256-SHA bits=256 verify=NO); Sat, 21 Mar 2015 03:51:51 +0200 (EET) (envelope-from kostikbel@gmail.com) DKIM-Filter: OpenDKIM Filter v2.9.2 kib.kiev.ua t2L1ppKv022487 Received: (from kostik@localhost) by tom.home (8.14.9/8.14.9/Submit) id t2L1ppLc022486; Sat, 21 Mar 2015 03:51:51 +0200 (EET) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: tom.home: kostik set sender to kostikbel@gmail.com using -f Date: Sat, 21 Mar 2015 03:51:51 +0200 From: Konstantin Belousov To: Mateusz Guzik Subject: Re: [PATCH 1/3] fork: assign refed credentials earlier Message-ID: <20150321015151.GF2379@kib.kiev.ua> References: <20150320122125.GP2379@kib.kiev.ua> <1426899640-6599-1-git-send-email-mjguzik@gmail.com> <1426899640-6599-2-git-send-email-mjguzik@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1426899640-6599-2-git-send-email-mjguzik@gmail.com> User-Agent: Mutt/1.5.23 (2014-03-12) X-Spam-Status: No, score=-2.0 required=5.0 tests=ALL_TRUSTED,BAYES_00, DKIM_ADSP_CUSTOM_MED,FREEMAIL_FROM,NML_ADSP_CUSTOM_MED autolearn=no autolearn_force=no version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on tom.home Cc: freebsd-current@freebsd.org, jenkins-admin@freebsd.org, Mateusz Guzik X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 21 Mar 2015 01:51:58 -0000 On Sat, Mar 21, 2015 at 02:00:38AM +0100, Mateusz Guzik wrote: > From: Mateusz Guzik > > Prior to this change the kernel would take p1's credentials and assign > them tempororarily to p2. But p1 could change credentials at that time > and in effect give us a use-after-free. In which way could it change the credentials ? The assigned credentials are taken from td_ucred, which, I thought, are guaranteed to be stable for the duration of a syscall. Other two patches look fine. > --- > sys/kern/kern_fork.c | 15 +++++++-------- > 1 file changed, 7 insertions(+), 8 deletions(-) > > diff --git a/sys/kern/kern_fork.c b/sys/kern/kern_fork.c > index ae86fe1..15833fd 100644 > --- a/sys/kern/kern_fork.c > +++ b/sys/kern/kern_fork.c > @@ -410,9 +410,6 @@ do_fork(struct thread *td, int flags, struct proc *p2, struct thread *td2, > bzero(&p2->p_startzero, > __rangeof(struct proc, p_startzero, p_endzero)); > > - crhold(td->td_ucred); > - proc_set_cred(p2, td->td_ucred); > - > /* Tell the prison that we exist. */ > prison_proc_hold(p2->p_ucred->cr_prison); > > @@ -832,7 +829,7 @@ fork1(struct thread *td, int flags, int pages, struct proc **procp, > td2 = thread_alloc(pages); > if (td2 == NULL) { > error = ENOMEM; > - goto fail1; > + goto fail2; > } > proc_linkup(newproc, td2); > } else { > @@ -841,7 +838,7 @@ fork1(struct thread *td, int flags, int pages, struct proc **procp, > vm_thread_dispose(td2); > if (!thread_alloc_stack(td2, pages)) { > error = ENOMEM; > - goto fail1; > + goto fail2; > } > } > } > @@ -850,7 +847,7 @@ fork1(struct thread *td, int flags, int pages, struct proc **procp, > vm2 = vmspace_fork(p1->p_vmspace, &mem_charged); > if (vm2 == NULL) { > error = ENOMEM; > - goto fail1; > + goto fail2; > } > if (!swap_reserve(mem_charged)) { > /* > @@ -861,7 +858,7 @@ fork1(struct thread *td, int flags, int pages, struct proc **procp, > */ > swap_reserve_force(mem_charged); > error = ENOMEM; > - goto fail1; > + goto fail2; > } > } else > vm2 = NULL; > @@ -870,7 +867,7 @@ fork1(struct thread *td, int flags, int pages, struct proc **procp, > * XXX: This is ugly; when we copy resource usage, we need to bump > * per-cred resource counters. > */ > - proc_set_cred(newproc, p1->p_ucred); > + proc_set_cred(newproc, crhold(td->td_ucred)); > > /* > * Initialize resource accounting for the child process. > @@ -946,6 +943,8 @@ fail: > #endif > racct_proc_exit(newproc); > fail1: > + crfree(proc_set_cred(newproc, NULL)); > +fail2: > if (vm2 != NULL) > vmspace_free(vm2); > uma_zfree(proc_zone, newproc); > -- > 2.3.2