Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp2670208ybt; Mon, 22 Jun 2020 04:17:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxnLY46UIB5yLHxD0u8D/ArUlNor1MLXjRUgHYM3M6miEBXS64uw+S9wRuM+RKbbOI7VMdf X-Received: by 2002:a50:d753:: with SMTP id i19mr12158197edj.9.1592824664455; Mon, 22 Jun 2020 04:17:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592824664; cv=none; d=google.com; s=arc-20160816; b=nX8VbtNiM5gJAuG7i7G5MVIx84rj/aEq8eNy+zaxX0zreJCy30pMPkGYIh+Pgz9Lsc wXISgIjg6bBNhoOVoWjt8C+lpsTPtJ/IUVNI0vk57I1p1a0kXgAFreeSKuXf63QL9qh/ 3f2NMLfCToXDIlibBju6dof0MKL2QLtIyOOsSzmAxGgvnoz8SLRFp9zkCRHld/lfK4yy jS14O3aEcx/ielfAJbPCYSE9pFi2egi9H0VW7QGhg8CFYWwuhhCF0PMVNMjJl7CN1DpZ aW+Vlflpdl9n+/aeM5uT97yHfpbNJw+DlJCoIePQebMdZ+PD4dgeThuN7TiZzyKnnBoa /0cg== 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 :message-id:date:subject:cc:to:from; bh=P65EumAraxLIGvprUhJyGl3BmrPbIvSxSkJTtcEn3rY=; b=Uf7rfwo0xHcvX1l7z9HrstTOu/AhjSXmFV92XlLpRR1/UC00Gof+B4DyElt/I8YSl+ 5MdYirbrxOzUC5hdVfypuMogiJqYLZHPfZ9lZXQj5qpy4yAAdftLFYB2ac0G/Q/e/iOL 3dhZqZ8yoJtsl/XdNShh4Kr06/K6xAKt3QJqlI992ir1Z4e80XWw8zFkzAcUjQa/30XE ZzElxn0t+zL9uSBZFkwR87aW9S2PqFXin3b+5sHlHL9NZBkU1MtbtLfVWmNa4RLPNwG3 vey6x9ulST/48xNYhVT3CKUwI59v1CMhd5xcOxvvBg7NvIRzgNmZIVpisOQrxJkzXY/t dMRA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ba16si9154925edb.52.2020.06.22.04.17.22; Mon, 22 Jun 2020 04:17:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727892AbgFVLNH (ORCPT + 99 others); Mon, 22 Jun 2020 07:13:07 -0400 Received: from szxga06-in.huawei.com ([45.249.212.32]:42694 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727822AbgFVLNH (ORCPT ); Mon, 22 Jun 2020 07:13:07 -0400 Received: from DGGEMS402-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id CB557B8189E77C3B414F; Mon, 22 Jun 2020 19:13:05 +0800 (CST) Received: from huawei.com (10.90.53.225) by DGGEMS402-HUB.china.huawei.com (10.3.19.202) with Microsoft SMTP Server id 14.3.487.0; Mon, 22 Jun 2020 19:12:56 +0800 From: Zheng Bin To: , , , , , , CC: , Subject: [PATCH v3] nbd: Fix memory leak in nbd_add_socket Date: Mon, 22 Jun 2020 19:20:01 +0800 Message-ID: <20200622112001.105047-1-zhengbin13@huawei.com> X-Mailer: git-send-email 2.26.0.106.g9fadedd MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.90.53.225] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When adding first socket to nbd, if nsock's allocation failed, the data structure member "config->socks" was reallocated, but the data structure member "config->num_connections" was not updated. A memory leak will occur then because the function "nbd_config_put" will free "config->socks" only when "config->num_connections" is not zero. Fixes: 03bf73c315ed ("nbd: prevent memory leak") Signed-off-by: Zheng Bin --- v1->v2: improve change description v2->v3: fix some code style issues, improve change description, thanks to Markus for the review. drivers/block/nbd.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c index 43cff01a5a67..ce7e9f223b20 100644 --- a/drivers/block/nbd.c +++ b/drivers/block/nbd.c @@ -1033,25 +1033,26 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg, test_bit(NBD_RT_BOUND, &config->runtime_flags))) { dev_err(disk_to_dev(nbd->disk), "Device being setup by another task"); - sockfd_put(sock); - return -EBUSY; + err = -EBUSY; + goto put_socket; + } + + nsock = kzalloc(sizeof(*nsock), GFP_KERNEL); + if (!nsock) { + err = -ENOMEM; + goto put_socket; } socks = krealloc(config->socks, (config->num_connections + 1) * sizeof(struct nbd_sock *), GFP_KERNEL); if (!socks) { - sockfd_put(sock); - return -ENOMEM; + kfree(nsock); + err = -ENOMEM; + goto put_socket; } config->socks = socks; - nsock = kzalloc(sizeof(struct nbd_sock), GFP_KERNEL); - if (!nsock) { - sockfd_put(sock); - return -ENOMEM; - } - nsock->fallback_index = -1; nsock->dead = false; mutex_init(&nsock->tx_lock); @@ -1063,6 +1064,10 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg, atomic_inc(&config->live_connections); return 0; + +put_socket: + sockfd_put(sock); + return err; } static int nbd_reconnect_socket(struct nbd_device *nbd, unsigned long arg) -- 2.26.0.106.g9fadedd