From patchwork Sat Jun 3 14:41:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miaohe Lin X-Patchwork-Id: 13266082 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6CB0DC77B7A for ; Sat, 3 Jun 2023 06:50:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232004AbjFCGua (ORCPT ); Sat, 3 Jun 2023 02:50:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40050 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231444AbjFCGu3 (ORCPT ); Sat, 3 Jun 2023 02:50:29 -0400 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BA550E58; Fri, 2 Jun 2023 23:50:27 -0700 (PDT) Received: from canpemm500002.china.huawei.com (unknown [172.30.72.53]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4QY9Rq21RwztQTy; Sat, 3 Jun 2023 14:48:07 +0800 (CST) Received: from huawei.com (10.175.104.170) by canpemm500002.china.huawei.com (7.192.104.244) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sat, 3 Jun 2023 14:50:24 +0800 From: Miaohe Lin To: , CC: , , , , , Subject: [PATCH] EDAC/mc: fix potential memoryleak in edac_mc_alloc() Date: Sat, 3 Jun 2023 22:41:31 +0800 Message-ID: <20230603144131.832804-1-linmiaohe@huawei.com> X-Mailer: git-send-email 2.23.0 MIME-Version: 1.0 X-Originating-IP: [10.175.104.170] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To canpemm500002.china.huawei.com (7.192.104.244) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-edac@vger.kernel.org When fails to allocate memory for layers or pvt_info, _edac_mc_free() will be called to release the resource of edac mc but mci->dev is not even initialized at that time. _edac_mc_free() will fail to release the allocated memory. Fixes: 0bbb265f7089 ("EDAC/mc: Get rid of silly one-shot struct allocation in edac_mc_alloc()") Signed-off-by: Miaohe Lin --- drivers/edac/edac_mc.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/drivers/edac/edac_mc.c b/drivers/edac/edac_mc.c index 6faeb2ab3960..89dc5954d19e 100644 --- a/drivers/edac/edac_mc.c +++ b/drivers/edac/edac_mc.c @@ -364,6 +364,8 @@ struct mem_ctl_info *edac_mc_alloc(unsigned int mc_num, mci = kzalloc(sizeof(struct mem_ctl_info), GFP_KERNEL); if (!mci) return NULL; + mci->dev.release = mci_release; + device_initialize(&mci->dev); mci->layers = kcalloc(n_layers, sizeof(struct edac_mc_layer), GFP_KERNEL); if (!mci->layers) @@ -373,9 +375,6 @@ struct mem_ctl_info *edac_mc_alloc(unsigned int mc_num, if (!mci->pvt_info) goto error; - mci->dev.release = mci_release; - device_initialize(&mci->dev); - /* setup index and various internal pointers */ mci->mc_idx = mc_num; mci->tot_dimms = tot_dimms;