Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp4137359imu; Tue, 18 Dec 2018 09:36:11 -0800 (PST) X-Google-Smtp-Source: AFSGD/UaY8gYZJZNY3bhA0+zmjhRDio1aOZfsywnmM/R6bqHC5zY8RfvWdrK7LySZSfd278hMfkR X-Received: by 2002:a65:500c:: with SMTP id f12mr15836364pgo.226.1545154571264; Tue, 18 Dec 2018 09:36:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545154570; cv=none; d=google.com; s=arc-20160816; b=Ic0+5k1/AKaAwXH8QJpGmUqa3PILaehtktJJHCBJcToOC+K0qy0g6FfoeQDwgDjT8Q BwSWHQaQFQy6SqT2+0aWNJuleQE+U7D7FlZacc3QUrsiqrL9xCGZ7v6Jysq9G4pjTHr6 GcneJWlovtJlW1RYdH8cB0pEtB3Yl3sqPlrmlfroivZxTmP8JjkJaiTt+N9pIgO12nQj qesjDUBWYXkYyAILqbAeZGLuDYi25l8bAOQuHzOfZxvzfIEeSipGdp60XxZX+LtMqAd1 oPaTTN40pMciIl6kbWRLwJMTFEUl9QmalWungvvTno0fP1mvkNeu3tl1VyiXp5bhWgiR H6Dg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:date:cc:to:from:subject :message-id; bh=FGpQA+IRFhs3FoBFwNy4M6ijgmkrBqQm1H+Se0P2WF4=; b=IsvK/n9217/Z5ImhlOB4YdgvF0j85wRFmiouEFZOJEfnh/YRFzEW1vTuEhX/nWOGgT rIJUKNOm8JQQCUaDGxAncW8FcQv1Wm2J+Dc126mWUhRtRpzzbsCNzCvfpndhT1Gx2NYl j9chNSxAyAE73d1ZiA7e0WNMQJw7p8CQ2kI4AzeNYopOrM8Eu+qroxAh4JxTvtIalmM7 eXtMnDekpzXSKW+/5uSga00l9gGeDQYA8ITbtQweaDP/t2MO0u//pk0O5sP5iT/d3bOD T/n92RH3v1KLSMZivmz7ZCZTtDWhRqXDcZUcK7MWbixu2Ey86Y2uKEJvT+AWWTG5ANkJ BpZw== 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 e13si13362280pfi.271.2018.12.18.09.35.46; Tue, 18 Dec 2018 09:36:10 -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 S1727251AbeLRReD (ORCPT + 99 others); Tue, 18 Dec 2018 12:34:03 -0500 Received: from smtprelay0039.hostedemail.com ([216.40.44.39]:59432 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726839AbeLRReC (ORCPT ); Tue, 18 Dec 2018 12:34:02 -0500 Received: from filter.hostedemail.com (clb03-v110.bra.tucows.net [216.40.38.60]) by smtprelay05.hostedemail.com (Postfix) with ESMTP id 2527118028995; Tue, 18 Dec 2018 17:34:01 +0000 (UTC) X-Session-Marker: 6A6F6540706572636865732E636F6D X-Spam-Summary: 2,0,0,,d41d8cd98f00b204,joe@perches.com,:::::::::,RULES_HIT:41:69:355:379:599:800:960:966:973:982:988:989:1260:1277:1311:1313:1314:1345:1359:1437:1515:1516:1518:1534:1543:1593:1594:1711:1730:1747:1777:1792:2196:2199:2393:2559:2562:2828:2899:3138:3139:3140:3141:3142:3355:3622:3865:3866:3867:3868:3871:3872:3874:4250:4321:4385:5007:7875:7903:8660:8957:10004:10400:10848:11026:11232:11233:11473:11658:11914:12043:12291:12296:12555:12683:12740:12760:12895:12986:13148:13230:13439:13868:14096:14097:14181:14659:14721:21080:21451:21627:30054:30070:30091,0,RBL:47.151.153.53:@perches.com:.lbl8.mailshell.net-62.14.0.100 64.201.201.201,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fn,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:29,LUA_SUMMARY:none X-HE-Tag: blow87_400871a9d6404 X-Filterd-Recvd-Size: 4506 Received: from XPS-9350.home (unknown [47.151.153.53]) (Authenticated sender: joe@perches.com) by omf05.hostedemail.com (Postfix) with ESMTPA; Tue, 18 Dec 2018 17:33:59 +0000 (UTC) Message-ID: <4db484684a6e9b096c7fdf57673fd208d1c4005a.camel@perches.com> Subject: Re: [PATCH] cifs: check kzalloc return From: Joe Perches To: Nicholas Mc Guire , Steve French Cc: linux-cifs@vger.kernel.org, samba-technical@lists.samba.org, linux-kernel@vger.kernel.org Date: Tue, 18 Dec 2018 09:33:58 -0800 In-Reply-To: <1545150439-26055-1-git-send-email-hofrat@osadl.org> References: <1545150439-26055-1-git-send-email-hofrat@osadl.org> Content-Type: text/plain; charset="ISO-8859-1" User-Agent: Evolution 3.30.1-1build1 Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. 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; }