Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp11013452rwd; Thu, 22 Jun 2023 07:42:55 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7014/VraceYxK1Nn04nacq1BqYjgoJ9CdjG2e7XFYyNDiCX/amvUtw5Fqqz1OruZDQYdba X-Received: by 2002:a17:902:c944:b0:1b0:6038:2982 with SMTP id i4-20020a170902c94400b001b060382982mr21407492pla.41.1687444975654; Thu, 22 Jun 2023 07:42:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687444975; cv=none; d=google.com; s=arc-20160816; b=WyRUfypMRx7dCKFa3HmDgfrRLAHCp+s082xk0lwa4hf5mMr5RTC/dzJ2a6lniFvSzZ 0fwRB53tnvvxb6qAkWbS330N6ldUMtxpRY1PLQnzzCVK/V3qUidWKE7/F329gzaJQw5B 3yb7IuVGOFdOecsQGm/r1rSafJXORSoKPfXP6fP4FE/Nekxx0awvRuGRBUWtHM9DpSwe UODQPbtbZ3WQgCKmSpy8HcUSt22zP1shqy4NDCj+VgJIw4LMD6GRdv6duyIwSuJQTx8p sSiL9RlBkqZ2S0x7MdECFRHJ6SvjXJ7icZMY+BM+INI4hWqL/lJo9VcAAiOAb1hXENSx hpVA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=p8uc9jDP/HqwYDyJO1LVfKoKqq8U5V+JWC9dfRUtX/c=; b=rdbAvdFYYlYRE4nGIs/JT9U+iAPa1YeWaj0+h7fIzN/iTFSw1m3oC8zZWnbZysexAs OMBT72tHlb/QEarMgTu1ZzogoNQDfXiBiwYTBXYdmy4DC4xRFdolm5Zwhgzhs+6l8LnW ta4584z0E2KyAl374pqwkz8cPVD+YccMGrDJ7EusHBZNKlNzLsEzxg22m3EpUzY3JFCN NNaXfORg9a6maoUxaxsHdMEs39Ae9xN5kkidopx06xCudv203iy0u1VnFNiKa6kV3x1y UHivj2DbefhUlRbEZHduObpkcTWnp+xWdlGBDKM/xvH2IeqF99TVHYuRZP9uEmJ1xp2g sYmw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y21-20020a170902e19500b001b047410425si2306214pla.503.2023.06.22.07.42.42; Thu, 22 Jun 2023 07:42:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231368AbjFVOiO (ORCPT + 99 others); Thu, 22 Jun 2023 10:38:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44076 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229840AbjFVOiL (ORCPT ); Thu, 22 Jun 2023 10:38:11 -0400 Received: from cstnet.cn (smtp81.cstnet.cn [159.226.251.81]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EB2E9E7E for ; Thu, 22 Jun 2023 07:38:09 -0700 (PDT) Received: from ed3e173716be.home.arpa (unknown [124.16.138.129]) by APP-03 (Coremail) with SMTP id rQCowABHT2fJXJRkDIXGAg--.21888S2; Thu, 22 Jun 2023 22:38:02 +0800 (CST) From: Jiasheng Jiang To: ira.weiny@intel.com, dan.j.williams@intel.com, vishal.l.verma@intel.com, dave.jiang@intel.com, oohall@gmail.com, aneesh.kumar@linux.ibm.com Cc: nvdimm@lists.linux.dev, linux-kernel@vger.kernel.org, Jiasheng Jiang Subject: Re: [PATCH] libnvdimm/of_pmem: Add check and kfree for kstrdup Date: Thu, 22 Jun 2023 22:38:00 +0800 Message-Id: <20230622143800.31779-1-jiasheng@iscas.ac.cn> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: rQCowABHT2fJXJRkDIXGAg--.21888S2 X-Coremail-Antispam: 1UD129KBjvJXoW7WFWDAw1kJF4UKr4xXFy7Jrb_yoW8ArW8p3 y8JF13CrWkJF4a9ws2v3WY9a1Yvw4ktFy8Wa4Dt3srXFn0vr17ZryrJryjgrs3uFy0kw1I yFWUJa1agry5ArDanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUkv14x267AKxVW8JVW5JwAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2ocxC64kIII0Yj41l84x0c7CEw4AK67xGY2AK02 1l84ACjcxK6xIIjxv20xvE14v26r1j6r1xM28EF7xvwVC0I7IYx2IY6xkF7I0E14v26r4j 6F4UM28EF7xvwVC2z280aVAFwI0_Cr0_Gr1UM28EF7xvwVC2z280aVCY1x0267AKxVW8Jr 0_Cr1UM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj 6xIIjxv20xvE14v26r106r15McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr 0_Gr1lF7xvr2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7MxkIecxEwVAFwVW5 XwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E14v26r 1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_Jw0_GFylIxkGc2Ij 64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY6xIIjxv20xvEc7CjxVAFwI0_Gr 0_Cr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r1j6r4UMIIF 0xvEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVjvjDU0xZFpf9x0JUXiSdUUUUU= X-Originating-IP: [124.16.138.129] X-CM-SenderInfo: pmld2xxhqjqxpvfd2hldfou0/ X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_PASS, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jun 21, 2023 at 00:04:36 +0800, Ira Weiny wrote: > Ira Weiny wrote: >> Jiasheng Jiang wrote: >> > Add check for the return value of kstrdup() and return the error >> > if it fails in order to avoid NULL pointer dereference. >> > Moreover, use kfree() in the later error handling in order to avoid >> > memory leak. >> > >> > Fixes: 49bddc73d15c ("libnvdimm/of_pmem: Provide a unique name for bus provider") >> > Signed-off-by: Jiasheng Jiang >> > --- >> > drivers/nvdimm/of_pmem.c | 6 ++++++ >> > 1 file changed, 6 insertions(+) >> > >> > diff --git a/drivers/nvdimm/of_pmem.c b/drivers/nvdimm/of_pmem.c >> > index 10dbdcdfb9ce..fe6edb7e6631 100644 >> > --- a/drivers/nvdimm/of_pmem.c >> > +++ b/drivers/nvdimm/of_pmem.c >> > @@ -31,11 +31,17 @@ static int of_pmem_region_probe(struct platform_device *pdev) >> > return -ENOMEM; >> > >> > priv->bus_desc.provider_name = kstrdup(pdev->name, GFP_KERNEL); >> > + if (!priv->bus_desc.provider_name) { >> > + kfree(priv); >> > + return -ENOMEM; >> > + } >> > + >> > priv->bus_desc.module = THIS_MODULE; >> > priv->bus_desc.of_node = np; >> > >> > priv->bus = bus = nvdimm_bus_register(&pdev->dev, &priv->bus_desc); >> > if (!bus) { >> > + kfree(priv->bus_desc.provider_name); >> >> Nice catch! >> >> However, this free needs to happen in of_pmem_region_remove() as well. > > Looks like the mail from my phone had html in it. Sorry for that. > > This would be better with devm_kstrdup() and then we don't have to worry > about the kfree at all. Looks good. I have submitted a new patch "libnvdimm/of_pmem: Replace kstrdup with devm_kstrdup and add check". Since the titie has been modified, I did not submitted a v2. - Jiasheng Jiang