Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp4820413imu; Wed, 19 Dec 2018 00:28:11 -0800 (PST) X-Google-Smtp-Source: AFSGD/VI56HYfu+ukvzOVfHi12uCl4xl1jFh6afm8Tybp29c5DdvBkccNHFEbBt3IexTMCsFEkMy X-Received: by 2002:a62:99dd:: with SMTP id t90mr19519493pfk.179.1545208091540; Wed, 19 Dec 2018 00:28:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545208091; cv=none; d=google.com; s=arc-20160816; b=WT89qzYZEoPidW6FBzlbrwMgi0K9S6BBpDIzezTMxtDCuJcLsPTLlXkYMx2PJSv28Q I72Vv8wP7/2ssgRLKO02hmxo8RDqUlheF8ycTCQr2EVWQ6vpl3YI+GLIByIOBpwAVQVj ZNtbAeQH52m4vnMkQjpiWhC2Wh68iKf7WWnLpQ5eqhEP7/PBTiPBCtcLV7x+sTUOTMLV Fo/PVnzxUfsNCd2qwuNZVA1zR8JwwHSAj6hMC2DbU4uTDSVSuwns+umaXfRlG2NsS+1x QWXaC/TExTG8q+KPAwf2Mkm0WUjuoSJp28h1pkiB3ZIxIm8pmOVZklgG46NycLm8vQ4U lBDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=ZsMAzGPl/bWaNlvOQVmg7CakjLzeaIJ4W3cGdhy3Q+U=; b=L5stn6y/ndI9QgllorJOoCtoQ6PyOgPDFrZRy2vrjOHahvJPAdXJNfmhPblw45F2PN BKtLMmeIhBVNokZ/CcgUnDrX3Y0F5X+S9Vvk60aLGfqDf9rGixx8NSbszYn/momxVkzN bCoMJ2obULqAo3o1YPG5CNPUyL942Lo+RAFCr6NohWS2RKx/EZeW7hLRtZKwEmh8Ji3t LOs8CVgFgvCxEwOb1mGcg/KswZ+eI4YBYz0ZnnvIHe3EK1iBCZbQ/IDQgbyAuqktjVCj gmqW391CaDthiAoHEWYXV0TEviJOleRxqiiTcsWOR4mGoEUUUo3HLuoaceT4MfBjgaUf ciXA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r8si15067417pgr.252.2018.12.19.00.27.55; Wed, 19 Dec 2018 00:28:11 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727997AbeLSHW6 (ORCPT + 99 others); Wed, 19 Dec 2018 02:22:58 -0500 Received: from 178.115.242.59.static.drei.at ([178.115.242.59]:56945 "EHLO mail.osadl.at" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725294AbeLSHW6 (ORCPT ); Wed, 19 Dec 2018 02:22:58 -0500 Received: by mail.osadl.at (Postfix, from userid 1001) id 7B8655C2288; Wed, 19 Dec 2018 08:22:50 +0100 (CET) Date: Wed, 19 Dec 2018 08:22:50 +0100 From: Nicholas Mc Guire To: Joe Perches Cc: Nicholas Mc Guire , Steve French , linux-cifs@vger.kernel.org, samba-technical@lists.samba.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] cifs: check kzalloc return Message-ID: <20181219072250.GA18501@osadl.at> References: <1545150439-26055-1-git-send-email-hofrat@osadl.org> <4db484684a6e9b096c7fdf57673fd208d1c4005a.camel@perches.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4db484684a6e9b096c7fdf57673fd208d1c4005a.camel@perches.com> User-Agent: Mutt/1.5.23 (2014-03-12) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Dec 18, 2018 at 09:33:58AM -0800, Joe Perches wrote: > On Tue, 2018-12-18 at 17:27 +0100, Nicholas Mc Guire wrote: > > kzalloc can return NULL so a check is needed. While there is a > > check for ret_buf there is no check for the allocation of > > ret_buf->crfid.fid - this check is thus added. Both call-sites > > of tconInfoAlloc() check for NULL return of tconInfoAlloc() > > so returning NULL on failure of kzalloc() here seems appropriate. > > As the kzalloc() is the only thing here that can fail it is > > moved to the beginning so as not to initialize other resources > > on failure of kzalloc. > > > > Signed-off-by: Nicholas Mc Guire > > Fixes: 3d4ef9a15343 ("smb3: fix redundant opens on root") > > --- > > > > Problem located with an experimental coccinelle script > > > > While at it make checkpatch happy by using *ret_buf->crfid.fid > > rather than struct cifs_fid. > > > > Patch was compile tested with: x86_64_defconfig + CIFS=m > > (with some unrelated smatch warnings and some pending cocci fixes) > > > > Patch is against v4.20-rc7 (localversion-next is next-20181218) > [] > > diff --git a/fs/cifs/misc.c b/fs/cifs/misc.c > [] > > @@ -113,6 +113,13 @@ tconInfoAlloc(void) > > struct cifs_tcon *ret_buf; > > ret_buf = kzalloc(sizeof(struct cifs_tcon), GFP_KERNEL); > > if (ret_buf) { > > + ret_buf->crfid.fid = kzalloc(sizeof(*ret_buf->crfid.fid), > > + GFP_KERNEL); > > + if (!ret_buf->crfid.fid) { > > + kfree(ret_buf); > > + return NULL; > > + } > > + > > atomic_inc(&tconInfoAllocCount); > > ret_buf->tidStatus = CifsNew; > > ++ret_buf->tc_count; > > @@ -120,8 +127,6 @@ tconInfoAlloc(void) > > INIT_LIST_HEAD(&ret_buf->tcon_list); > > spin_lock_init(&ret_buf->open_file_lock); > > mutex_init(&ret_buf->crfid.fid_mutex); > > - ret_buf->crfid.fid = kzalloc(sizeof(struct cifs_fid), > > - GFP_KERNEL); > > spin_lock_init(&ret_buf->stat_lock); > > atomic_set(&ret_buf->num_local_opens, 0); > > atomic_set(&ret_buf->num_remote_opens, 0); > > Perhaps use a more common style by returning early on the > first possible failure too so the block can be unindented. > yup the restructured cleanup would be the better way to go rather than making this two patches I guess it would be the best to just skip the intermediate kzalloc only cleanup - atleast I see little value in that intermediat step - so could you take care of the kzalloc() in your refactoring please ? thx! hofrat > Maybe as a separate cleanup patch. > --- > fs/cifs/misc.c | 34 ++++++++++++++++++++-------------- > 1 file changed, 20 insertions(+), 14 deletions(-) > > diff --git a/fs/cifs/misc.c b/fs/cifs/misc.c > index 113980dba4d8..bee203055b30 100644 > --- a/fs/cifs/misc.c > +++ b/fs/cifs/misc.c > @@ -111,21 +111,27 @@ struct cifs_tcon * > tconInfoAlloc(void) > { > struct cifs_tcon *ret_buf; > - ret_buf = kzalloc(sizeof(struct cifs_tcon), GFP_KERNEL); > - if (ret_buf) { > - atomic_inc(&tconInfoAllocCount); > - ret_buf->tidStatus = CifsNew; > - ++ret_buf->tc_count; > - INIT_LIST_HEAD(&ret_buf->openFileList); > - INIT_LIST_HEAD(&ret_buf->tcon_list); > - spin_lock_init(&ret_buf->open_file_lock); > - mutex_init(&ret_buf->crfid.fid_mutex); > - ret_buf->crfid.fid = kzalloc(sizeof(struct cifs_fid), > - GFP_KERNEL); > - spin_lock_init(&ret_buf->stat_lock); > - atomic_set(&ret_buf->num_local_opens, 0); > - atomic_set(&ret_buf->num_remote_opens, 0); > + > + ret_buf = kzalloc(sizeof(*ret_buf), GFP_KERNEL); > + if (!ret_buf) > + return NULL; > + ret_buf->crfid.fid = kzalloc(sizeof(*ret_buf->crfid.fid), GFP_KERNEL); > + if (!ret_buf->crfid.fid) { > + kfree(ret_buf); > + return NULL; > } > + > + atomic_inc(&tconInfoAllocCount); > + ret_buf->tidStatus = CifsNew; > + ++ret_buf->tc_count; > + INIT_LIST_HEAD(&ret_buf->openFileList); > + INIT_LIST_HEAD(&ret_buf->tcon_list); > + spin_lock_init(&ret_buf->open_file_lock); > + mutex_init(&ret_buf->crfid.fid_mutex); > + spin_lock_init(&ret_buf->stat_lock); > + atomic_set(&ret_buf->num_local_opens, 0); > + atomic_set(&ret_buf->num_remote_opens, 0); > + > return ret_buf; > } > >