From owner-freebsd-current@FreeBSD.ORG Sat Mar 21 01:00:48 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 3E97C423; Sat, 21 Mar 2015 01:00:48 +0000 (UTC) Received: from mail-wg0-x235.google.com (mail-wg0-x235.google.com [IPv6:2a00:1450:400c:c00::235]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id BC8568D7; Sat, 21 Mar 2015 01:00:47 +0000 (UTC) Received: by wgra20 with SMTP id a20so102221603wgr.3; Fri, 20 Mar 2015 18:00:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Z2tEfOSHTCkUi4Cm8kzUt47r2yCE6TW86AgnwUNqE88=; b=e2eprLsslYV/9lkpVI4LWGrKrqgRb7AUZ/rhheyf2iCKdBRdSeK+sMIsqI9tPoUQ9J bmCE9QGbph3QWQmZGb4CKj+flciGVv476IQjph2eypwhaw3jI+0ZCPY8wTb7IuKuxF41 5IPfGFBYmbufv4DhfhIHDwHHPx8KyYBGqSygdyaxKuPQddFliFMY2NHX1FIbEDWc+fz5 i+aWYHGFd2vikck19m8hDMLOkgAWU99Z5P/oEmtjBv1sXHk9ZpZ9hbS/nkD6HZ8mxrfv XFP3mNHn6yNcbcPiyl+m1GLUPc6/KNWYHjpd3UBOX9v+JF/NllEFI1gVFT3DC41NbxWF o3ow== X-Received: by 10.180.103.166 with SMTP id fx6mr704038wib.4.1426899646015; Fri, 20 Mar 2015 18:00:46 -0700 (PDT) Received: from localhost.localdomain (ip-89-176-114-84.net.upcbroadband.cz. [89.176.114.84]) by mx.google.com with ESMTPSA id q6sm303207wix.3.2015.03.20.18.00.44 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 20 Mar 2015 18:00:45 -0700 (PDT) From: Mateusz Guzik To: Konstantin Belousov Subject: [PATCH 2/3] cred: add proc_set_cred_init helper Date: Sat, 21 Mar 2015 02:00:39 +0100 Message-Id: <1426899640-6599-3-git-send-email-mjguzik@gmail.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1426899640-6599-1-git-send-email-mjguzik@gmail.com> References: <20150320122125.GP2379@kib.kiev.ua> <1426899640-6599-1-git-send-email-mjguzik@gmail.com> 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:00:48 -0000 From: Mateusz Guzik proc_set_cred_init can be used to set first credentials of a new process. Update proc_set_cred assertions so that it only expects already used processes. This fixes panics where p_ucred of a new process happens to be non-NULL. --- sys/kern/init_main.c | 2 +- sys/kern/kern_fork.c | 2 +- sys/kern/kern_prot.c | 16 ++++++++++++++-- sys/sys/ucred.h | 1 + 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/sys/kern/init_main.c b/sys/kern/init_main.c index 82cf63f..88cd44c 100644 --- a/sys/kern/init_main.c +++ b/sys/kern/init_main.c @@ -515,7 +515,7 @@ proc0_init(void *dummy __unused) newcred->cr_ruidinfo = uifind(0); newcred->cr_prison = &prison0; newcred->cr_loginclass = loginclass_find("default"); - proc_set_cred(p, newcred); + proc_set_cred_init(p, newcred); #ifdef AUDIT audit_cred_kproc0(newcred); #endif diff --git a/sys/kern/kern_fork.c b/sys/kern/kern_fork.c index 15833fd..a3a70b8 100644 --- a/sys/kern/kern_fork.c +++ b/sys/kern/kern_fork.c @@ -867,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, crhold(td->td_ucred)); + proc_set_cred_init(newproc, crhold(td->td_ucred)); /* * Initialize resource accounting for the child process. diff --git a/sys/kern/kern_prot.c b/sys/kern/kern_prot.c index 72c9f65..9c49f71 100644 --- a/sys/kern/kern_prot.c +++ b/sys/kern/kern_prot.c @@ -1954,8 +1954,19 @@ cred_update_thread(struct thread *td) } /* + * Set initial process credentials. + * Callers are responsible for providing the reference for provided credentials. + */ +void +proc_set_cred_init(struct proc *p, struct ucred *newcred) +{ + + p->p_ucred = newcred; +} + +/* * Change process credentials. - * Callers are responsible for providing the reference for current credentials + * Callers are responsible for providing the reference for passed credentials * and for freeing old ones. * * Process has to be locked except when it does not have credentials (as it @@ -1968,9 +1979,10 @@ proc_set_cred(struct proc *p, struct ucred *newcred) { struct ucred *oldcred; + MPASS(p->p_ucred != NULL); if (newcred == NULL) MPASS(p->p_state == PRS_ZOMBIE); - else if (p->p_ucred != NULL) + else PROC_LOCK_ASSERT(p, MA_OWNED); oldcred = p->p_ucred; diff --git a/sys/sys/ucred.h b/sys/sys/ucred.h index 2b42b01..9a45308 100644 --- a/sys/sys/ucred.h +++ b/sys/sys/ucred.h @@ -106,6 +106,7 @@ void crcopy(struct ucred *dest, struct ucred *src); struct ucred *crcopysafe(struct proc *p, struct ucred *cr); struct ucred *crdup(struct ucred *cr); void cred_update_thread(struct thread *td); +void proc_set_cred_init(struct proc *p, struct ucred *cr); struct ucred *proc_set_cred(struct proc *p, struct ucred *cr); void crfree(struct ucred *cr); struct ucred *crget(void); -- 2.3.2