Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp5183678imu; Wed, 19 Dec 2018 07:01:43 -0800 (PST) X-Google-Smtp-Source: AFSGD/WOBAKeEWx6iaoSuG6y9Lg/6CipnKGjowlmj2/hSl4gJ8kVy4UOAC5dGJWTHfOPUCs52eke X-Received: by 2002:a63:585c:: with SMTP id i28mr19918705pgm.178.1545231703302; Wed, 19 Dec 2018 07:01:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545231703; cv=none; d=google.com; s=arc-20160816; b=wxFcip31xYdd/kAAm5plMIqR6FX8RQIFZ/UEwOnAX562oF6rvS0HZrl/HYvKGE12BO D+9G0WvTTMmMVcGd6NrXwfI4wIDYMyRwA+sMIAB7P9AAvI8sjJq8S3rhz49Kt/ce5Fo5 +I2UghRCGkNp24QlplNtePUY2g1QR+m86CKIpNm176u0cyI+xQS1yuyadk0Arc9L2BBc Uwq6mPYnuA4V71nBiQfw8ewDcU1EWOAknMmR4ltk/b95YNNneC+j8/EEVZ2a3pcjopEB i2Fs5gq3WXUjigUoUCkQJpiMt8ch764atLX0Iaw8oGwnxmfMQrF91uo2Vnjfh8H1cqGH 9ALA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from; bh=+zUqkcTs8USoOOss1BPyh+XWcRjbEK2XjZ2YSxuhfQ4=; b=swbXsHCoR2LDv+LoqXyKd8hwJct0MhrhQ8nueakBzbO9TXwgNyODSIi8ex0rGqiZ22 Wljd2Z5cYmjtnErieeGnmmRXCREOVjtUSjxSWQQPxdJZpUgycVwOWkYgop1+BBU55y+/ /ZIbLhoTsU0b8GRLHAcLmx3jaR6BOVFMMeMfo05aoae3wFqqjLDys6rKP3aq/XCp6/kV ROMDBI6me/zpYEeIvN59Gz+5JtZ/C4T/GhCm5Eo3qmGVE02wOiuKI34tOa+oNC161wQ7 vMvHDbHXTb1NNXSuRLVf0CHr2nkS/VI4vRSaKAh78tZZ/8IjVoL2tbtBZEtctqE2lcWr nw/w== 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 19si16091485pgp.186.2018.12.19.07.01.24; Wed, 19 Dec 2018 07:01:43 -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 S1728610AbeLSMFH (ORCPT + 99 others); Wed, 19 Dec 2018 07:05:07 -0500 Received: from www.osadl.org ([62.245.132.105]:37965 "EHLO www.osadl.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727962AbeLSMFH (ORCPT ); Wed, 19 Dec 2018 07:05:07 -0500 Received: from debian01.hofrr.at (178.115.242.59.static.drei.at [178.115.242.59]) by www.osadl.org (8.13.8/8.13.8/OSADL-2007092901) with ESMTP id wBJC1gHi020816; Wed, 19 Dec 2018 13:01:42 +0100 From: Nicholas Mc Guire To: Steve French Cc: Joe Perches , linux-cifs@vger.kernel.org, samba-technical@lists.samba.org, linux-kernel@vger.kernel.org, Nicholas Mc Guire Subject: [PATCH] cifs: handle allocation failures early Date: Wed, 19 Dec 2018 12:58:17 +0100 Message-Id: <1545220697-29183-1-git-send-email-hofrat@osadl.org> X-Mailer: git-send-email 2.1.4 X-Spam-Status: No, score=-1.9 required=6.0 tests=BAYES_00 autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on www.osadl.org Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org kzalloc can return NULL so a check is needed. Further move the allocation checks before the remaining initialization and bail out without initialization if allocation failed returning NULL. Signed-off-by: Joe Perches Signed-off-by: Nicholas Mc Guire Fixes: 3d4ef9a15343 ("smb3: fix redundant opens on root") --- Missing kzalloc check located with an experimental coccinelle script While at it make checkpatch happy by using *ret_buf->crfid.fid rather than struct cifs_fid and use a more common style by returning early on the first possible failure too so the block can be unindented. Patch was compile tested with: x86_64_defconfig + CIFS=m (with an unrelated smatch warnings and some pending cocci fixes) Patch is against v4.20-rc7 (localversion-next is next-20181219) fs/cifs/misc.c | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/fs/cifs/misc.c b/fs/cifs/misc.c index 113980d..2d4b5fb 100644 --- a/fs/cifs/misc.c +++ b/fs/cifs/misc.c @@ -111,21 +111,26 @@ 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; } -- 2.1.4