Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp2110332rdh; Tue, 26 Sep 2023 12:46:00 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGSCO1xMY9F/r6OQAZCkA3/b01B2kosKzPdHQ7Cur++MzgBeoaTi/W+pqHtlgNjbR6ONxUD X-Received: by 2002:a17:90a:e293:b0:274:9823:b481 with SMTP id d19-20020a17090ae29300b002749823b481mr10297450pjz.9.1695757560588; Tue, 26 Sep 2023 12:46:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695757560; cv=none; d=google.com; s=arc-20160816; b=fLuywoTRgNM5bCE0D3r1Tj8clvGHbH39sM2Fs9BgGl3ljM1Ij80/mG7v1yjP+4IlxB eAIeU0Fkw7z+pGRWQTietD4M27XnepOk4kv9ARjoO0DnMcyZPy9SB+bHEXj8MI5ZrL3Z H16kRd5S6mKRw30EpeRpIRnwd+L+eKMDhJfaOCJauG/9SGBHoyq8sk75MMg+cQxThqsw 1FM4T98jjjBj4ZDuSCJgDcEa/Z/ObHLxQP8b91r1AB5dIuBVNcA0UsuMSEt7MUSZ6sZ3 DUkKfvi6KoA6oBbeLL0J4Ftj24eUf4CmHNhPSvEKgKX9O2doE1+hrnhEaGB5hFtvB/X0 zBbg== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=fynpeHl+ssbG6nfsCVb+MLvLR9eCy4mOBEN6MwbZLdc=; fh=mLwpXbRC66JNOEw8jncRnJ6BTHfhXYIQRe4j+M4FwDE=; b=LJw5sv2Hl9TZ9xJUtDPgMYwSWZmFzc5V81pOwSOWSAoCnk8EB0KJPCSaHljI1zrx9P fuVIkQSe9XIXe7eMau23Fmokg0AqLaMrV1ET/RWxCtOl7YMRsrQ+L58z3qm5VeXD6nx6 iMkHorPiLmd4C/tEQ8ZCBG8Gxa1fbi0M5XLQfEwGhAFANcsQzuHKlxN6VYIru9kKPRaf F+MkDk+PcKn8oOWm5JLwjK8Y/EjkP11MaKO4Jyg3UH4CNl7UHSizfoF4HDLuYvlLptSM WG04IV5gnZZkiJZ4KwyNZ6Nq+u/UbBdNm1YtcxElu1hhfl4bA71EXOhMDxhVF0dR9o8D sSWA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=XpPhiYMx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id l6-20020a17090a49c600b002776a60ea68si4353940pjm.1.2023.09.26.12.45.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Sep 2023 12:46:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=XpPhiYMx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 614E7813CDA2; Tue, 26 Sep 2023 11:45:52 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235577AbjIZSpx (ORCPT + 99 others); Tue, 26 Sep 2023 14:45:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46384 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235449AbjIZSpu (ORCPT ); Tue, 26 Sep 2023 14:45:50 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D7ED5EB; Tue, 26 Sep 2023 11:45:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1695753943; x=1727289943; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=LryE28J099aJFwLkOHr91UPQ19Da3H3VkYKesB9Y1vo=; b=XpPhiYMxUz2nJUcaeyDQ+/znyXVFw7Cu0vQXyPkQl1Ou5IG4CW1XU2RD OqdkkzPuWYK7Lsn2apQDeJJ20X7PyOl8QI/kDiZ6I00J4TP06Y4FSWSQ4 UCtvz1LMnab8NJZYTtE0EhBzfMmnfFJyPsETqv4CkL4Kzvz71zfAaayu6 63qMmvJSuCRuyZuxaqX5N+fTFf1DKhnTg0niME1lAbzDgtvH31XwJYJUA 6QY/tcQ/8uK/qxSaw57yhIwt1p+JPm/j7ECNClokQ3z7hp31CUuTfIXrT XUuyBu9zotCHHCqDZXlCEU65O9d5/08oASMNS5oyHkp4sWVie/FcqRHtN g==; X-IronPort-AV: E=McAfee;i="6600,9927,10845"; a="378920265" X-IronPort-AV: E=Sophos;i="6.03,178,1694761200"; d="scan'208";a="378920265" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Sep 2023 11:45:34 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10845"; a="752279638" X-IronPort-AV: E=Sophos;i="6.03,178,1694761200"; d="scan'208";a="752279638" Received: from powerlab.fi.intel.com ([10.237.71.25]) by fmsmga007-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Sep 2023 11:45:31 -0700 From: Michal Wilczynski To: linux-acpi@vger.kernel.org, linux-kernel@vger.kernel.org, nvdimm@lists.linux.dev Cc: rafael.j.wysocki@intel.com, andriy.shevchenko@intel.com, lenb@kernel.org, dan.j.williams@intel.com, vishal.l.verma@intel.com, ira.weiny@intel.com, Michal Wilczynski , Andy Shevchenko Subject: [PATCH v1 1/2] ACPI: NFIT: Fix memory leak, and local use of devm_*() Date: Tue, 26 Sep 2023 21:45:19 +0300 Message-ID: <20230926184520.2239723-2-michal.wilczynski@intel.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230926184520.2239723-1-michal.wilczynski@intel.com> References: <20230926184520.2239723-1-michal.wilczynski@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 26 Sep 2023 11:45:52 -0700 (PDT) devm_*() family of functions purpose is managing memory attached to a device. So in general it should only be used for allocations that should last for the whole lifecycle of the device. This is not the case for acpi_nfit_init_interleave_set(). There are two allocations that are only used locally in this function. What's more - if the function exits on error path memory is never freed. It's still attached to dev and would be freed on device detach, so this leak could be called a 'local leak'. Fix this by switching from devm_kcalloc() to kcalloc(), and adding proper rollback. Fixes: eaf961536e16 ("libnvdimm, nfit: add interleave-set state-tracking infrastructure") Reported-by: Andy Shevchenko Signed-off-by: Michal Wilczynski --- drivers/acpi/nfit/core.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/drivers/acpi/nfit/core.c b/drivers/acpi/nfit/core.c index f0e6738ae3c9..78f0f855c4de 100644 --- a/drivers/acpi/nfit/core.c +++ b/drivers/acpi/nfit/core.c @@ -2262,6 +2262,7 @@ static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc, u16 nr = ndr_desc->num_mappings; struct nfit_set_info2 *info2; struct nfit_set_info *info; + int err = 0; int i; nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL); @@ -2269,13 +2270,15 @@ static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc, return -ENOMEM; import_guid(&nd_set->type_guid, spa->range_guid); - info = devm_kcalloc(dev, nr, sizeof(*info), GFP_KERNEL); + info = kcalloc(nr, sizeof(*info), GFP_KERNEL); if (!info) return -ENOMEM; - info2 = devm_kcalloc(dev, nr, sizeof(*info2), GFP_KERNEL); - if (!info2) - return -ENOMEM; + info2 = kcalloc(nr, sizeof(*info2), GFP_KERNEL); + if (!info2) { + err = -ENOMEM; + goto free_info; + } for (i = 0; i < nr; i++) { struct nd_mapping_desc *mapping = &ndr_desc->mapping[i]; @@ -2289,7 +2292,8 @@ static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc, if (!memdev || !nfit_mem->dcr) { dev_err(dev, "%s: failed to find DCR\n", __func__); - return -ENODEV; + err = -ENODEV; + goto free_info2; } map->region_offset = memdev->region_offset; @@ -2337,10 +2341,13 @@ static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc, } ndr_desc->nd_set = nd_set; - devm_kfree(dev, info); - devm_kfree(dev, info2); - return 0; +free_info2: + kfree(info2); +free_info: + kfree(info); + + return err; } static int ars_get_cap(struct acpi_nfit_desc *acpi_desc, -- 2.41.0