Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp1409343rdb; Wed, 6 Dec 2023 19:44:23 -0800 (PST) X-Google-Smtp-Source: AGHT+IH8fDhgrPPMf10P9P1CcxsInp7NNFb6cdxnrVprtLjh3a7mmcXf+ZKc2T+evvGZOcs4t5wS X-Received: by 2002:a05:6830:4119:b0:6d8:d287:9890 with SMTP id w25-20020a056830411900b006d8d2879890mr2726771ott.46.1701920662873; Wed, 06 Dec 2023 19:44:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701920662; cv=none; d=google.com; s=arc-20160816; b=oY+vwbTL/5fmo50JaUE+cPO6Oj09Owb7kRVx32cV7kHIBWlX7PWPuutpYkaMhPfakO ylSuSBc2mK9/fJVTiGx/soXs0iCfshNWHppwU2nabhgE+wL9FEq5gL7Podo0xgbBT44s ablFnzFfOXzuzF/7CC+Gy9YC6kuaeABqESpNkzXnd6OfDdYWzkWE7ODyL0K2PW5u/CCO VkqHfWSpP0pOItTuRPTRB+nVL8wm4n9hkmucOt23I0dG07uObddArwc10EwowXUmM6vv ajhaMTahnwqqDLMcF6c0OLyngEsHaE2was3u8AIqBlo63el3ORRRO5wphs9NQww9yk9h qwGg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from; bh=EWvZSUgTzm/1Sks/+P5y6cZVNcIhaUQ6nOiz8bPdYs8=; fh=E0YUV2je+kfRe4IpvbpWN+kgQxiPwH4JWpWdOeTsT8M=; b=v58PHuq/LWxYrwMDts8AS40fwP8wPDnnykpanNkzrvKXJT2kAMq1Uc3dRIUMQ+5cjz fR+8wPkAuLmprDDIpjAvYpg+pMYZr8m/XZDOtPaQqiQGPfqmJHppWGUHJT966PPKXOWU GtbbQCHETN/7YQ5NdsG9xig22vIc1abmwjz8DYjhT0iWeanklTLZdeuTDhei8TKoAnbJ EA5gXKlcCoX4Dzl6YbiiC5gmZ24XyUEVakQLRY+j0cZWk0e7igW0iaXqUJe4oO/hcMbl u5YAgfRaJwa3w528ZjP4KYZcNZts7nZ/HmtXbNTHFIvFAvVMNrdixlJNKRLT3SZmE50t 4RBg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id i1-20020a654841000000b005c1b30b1b65si389649pgs.648.2023.12.06.19.44.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 19:44:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id D23A9831D2E6; Wed, 6 Dec 2023 19:44:20 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229455AbjLGDoI (ORCPT + 99 others); Wed, 6 Dec 2023 22:44:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36224 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229447AbjLGDoH (ORCPT ); Wed, 6 Dec 2023 22:44:07 -0500 Received: from zju.edu.cn (spam.zju.edu.cn [61.164.42.155]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 92284D5C for ; Wed, 6 Dec 2023 19:44:11 -0800 (PST) Received: from localhost.localdomain (unknown [10.190.66.146]) by mail-app4 (Coremail) with SMTP id cS_KCgBHTzeBP3FlODFaAA--.40820S4; Thu, 07 Dec 2023 11:44:08 +0800 (CST) From: Dinghao Liu To: dinghao.liu@zju.edu.cn Cc: Vishal Verma , Dan Williams , Dave Jiang , Ira Weiny , nvdimm@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH] nvdimm-btt: fix a potential memleak in btt_freelist_init Date: Thu, 7 Dec 2023 11:43:32 +0800 Message-Id: <20231207034332.24107-1-dinghao.liu@zju.edu.cn> X-Mailer: git-send-email 2.17.1 X-CM-TRANSID: cS_KCgBHTzeBP3FlODFaAA--.40820S4 X-Coremail-Antispam: 1UD129KBjvJXoW7urWrArWxXw4rKF1xXFW7twb_yoW8Ww1DpF s3ArW5Ar4UJF47ur17ZwsrWa43Ca1fG3srGa4Yka4Syr15AFWqvFWSyayavry5Cr48ZFWj kr4ktrWrGF4UAwUanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUkI1xkIjI8I6I8E6xAIw20EY4v20xvaj40_Wr0E3s1l1IIY67AE w4v_Jr0_Jr4l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xvwVC0I7IYx2 IY67AKxVWDJVCq3wA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxVWxJr0_GcWl84ACjcxK6I8E 87Iv67AKxVW0oVCq3wA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_GcCE3s1le2I262IYc4CY6c 8Ij28IcVAaY2xG8wAqx4xG64xvF2IEw4CE5I8CrVC2j2WlYx0E2Ix0cI8IcVAFwI0_Jr0_ Jr4lYx0Ex4A2jsIE14v26r1j6r4UMcvjeVCFs4IE7xkEbVWUJVW8JwACjcxG0xvY0x0EwI xGrwACjI8F5VA0II8E6IAqYI8I648v4I1l42xK82IYc2Ij64vIr41l42xK82IY6x8ErcxF aVAv8VW8uw4UJr1UMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr 4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUAVWUtwCIc40Y0x0EwIxG rwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x0267AKxVWUJVW8Jw CI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Jr0_Gr1lIxAIcVC2 z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7VUbXdbUUUUUU== X-CM-SenderInfo: qrrzjiaqtzq6lmxovvfxof0/1tbiAgsCBmVwRZQp9wAAsX X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Wed, 06 Dec 2023 19:44:20 -0800 (PST) When an error happens in btt_freelist_init(), its caller discover_arenas() will directly free arena, which makes arena->freelist allocated in btt_freelist_init() a leaked memory. Fix this by freeing arena->freelist in all error handling paths of btt_freelist_init(). Fixes: 5212e11fde4d ("nd_btt: atomic sector updates") Signed-off-by: Dinghao Liu --- drivers/nvdimm/btt.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/drivers/nvdimm/btt.c b/drivers/nvdimm/btt.c index d5593b0dc700..d8c4ba8bfdda 100644 --- a/drivers/nvdimm/btt.c +++ b/drivers/nvdimm/btt.c @@ -544,8 +544,10 @@ static int btt_freelist_init(struct arena_info *arena) for (i = 0; i < arena->nfree; i++) { new = btt_log_read(arena, i, &log_new, LOG_NEW_ENT); - if (new < 0) - return new; + if (new < 0) { + ret = new; + goto out_free; + } /* old and new map entries with any flags stripped out */ log_oldmap = ent_lba(le32_to_cpu(log_new.old_map)); @@ -577,7 +579,7 @@ static int btt_freelist_init(struct arena_info *arena) ret = btt_map_read(arena, le32_to_cpu(log_new.lba), &map_entry, NULL, NULL, 0); if (ret) - return ret; + goto out_free; /* * The map_entry from btt_read_map is stripped of any flag bits, @@ -594,11 +596,16 @@ static int btt_freelist_init(struct arena_info *arena) ret = btt_map_write(arena, le32_to_cpu(log_new.lba), le32_to_cpu(log_new.new_map), 0, 0, 0); if (ret) - return ret; + goto out_free; } } return 0; + +out_free: + kfree(arena->freelist); + arena->freelist = NULL; + return ret; } static bool ent_is_padding(struct log_entry *ent) -- 2.17.1