From patchwork Sun Oct 8 08:02:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Zhuo, Qiuxu" X-Patchwork-Id: 13412639 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 1C470E95A67 for ; Sun, 8 Oct 2023 08:03:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229988AbjJHIDo (ORCPT ); Sun, 8 Oct 2023 04:03:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56388 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229945AbjJHIDo (ORCPT ); Sun, 8 Oct 2023 04:03:44 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A352AB9; Sun, 8 Oct 2023 01:03: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=1696752223; x=1728288223; h=from:to:cc:subject:date:message-id; bh=M+CZyx661hVrmPY+4+i7MhwylTG6WH1acuVWa4jyhKE=; b=HvhE+kXpSDnt+SGB4d+PBaA/LXhQazCLyy74vWKzl98GN4ajBVa7IP8a /9ZlRyFQkBnih5USFy8ZobqczCEkAk51Bq9877aqlmIZGJnKq6CRrdVV9 xFZ2lAkG9vga+8Jfn+BVZppz5dIyCrvspVTRFOKxR8zmzFu0SoYKQh0kk M+VJEV/pFnxtuth8FMVLud9G/ZS5jglyo1wi/O/33uBltDujF3GLVCxgs ESDZUVqm10M37Nt47nN2SxS2lO9MIsY31RIJf27NKYv2/BavQroBQwce9 GwZdYspJnm4xpZ+a2cXjoIAPox7C6j7hWSxBjpknC2dya60yhW+RXPOlX Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10856"; a="383863996" X-IronPort-AV: E=Sophos;i="6.03,207,1694761200"; d="scan'208";a="383863996" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Oct 2023 01:03:43 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10856"; a="876474391" X-IronPort-AV: E=Sophos;i="6.03,207,1694761200"; d="scan'208";a="876474391" Received: from qiuxu-clx.sh.intel.com ([10.239.53.109]) by orsmga004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Oct 2023 01:03:39 -0700 From: Qiuxu Zhuo To: Borislav Petkov , Tony Luck Cc: Qiuxu Zhuo , Lili Li , James Morse , Mauro Carvalho Chehab , Robert Richter , linux-edac@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/3] EDAC/igen6: Fix slab-use-after-free in igen6_unregister_mci() Date: Sun, 8 Oct 2023 16:02:29 +0800 Message-Id: <20231008080231.51917-1-qiuxu.zhuo@intel.com> X-Mailer: git-send-email 2.17.1 Precedence: bulk List-ID: X-Mailing-List: linux-edac@vger.kernel.org When unloading the igen6_edac driver, the EDAC core wrongly kfreed 'pvt_info,' which was private data and managed by the igen6_edac driver. This resulted in a slab-use-after-free bug. Fix it by adding a flag to indicate whether 'pvt_info' is managed by the EDAC core. The EDAC core will only kfree 'pvt_info' when the flag is set to true. BUG: KASAN: slab-use-after-free in igen6_unregister_mcis+0x74/0x1f0 [igen6_edac] Read of size 8 at addr ffff88810c10a350 by task modprobe/5137 Call Trace: dump_stack_lvl+0x4c/0x70 print_report+0xcf/0x620 ? __virt_addr_valid+0xfc/0x180 ? kasan_complete_mode_report_info+0x80/0x210 ? igen6_unregister_mcis+0x74/0x1f0 [igen6_edac] kasan_report+0xbf/0x100 ? igen6_unregister_mcis+0x74/0x1f0 [igen6_edac] __asan_load8+0x82/0xb0 igen6_unregister_mcis+0x74/0x1f0 [igen6_edac] igen6_remove+0x97/0xc0 [igen6_edac] ... Allocated by task 578: kasan_save_stack+0x2a/0x50 kasan_set_track+0x29/0x40 kasan_save_alloc_info+0x1f/0x30 __kasan_kmalloc+0x88/0xa0 kmalloc_trace+0x4e/0xb0 igen6_probe+0xe5/0x1400 [igen6_edac] local_pci_probe+0x89/0xf0 pci_device_probe+0x18e/0x450 ... Freed by task 5137: kasan_save_stack+0x2a/0x50 kasan_set_track+0x29/0x40 kasan_save_free_info+0x32/0x50 __kasan_slab_free+0x113/0x1b0 slab_free_freelist_hook+0xb1/0x190 __kmem_cache_free+0x15d/0x280 kfree+0x7d/0x120 mci_release+0x24a/0x280 device_release+0x64/0x110 kobject_put+0xfd/0x260 put_device+0x17/0x30 edac_mc_free+0x43/0x50 igen6_unregister_mcis+0x18f/0x1f0 [igen6_edac] igen6_remove+0x97/0xc0 [igen6_edac] pci_device_remove+0x6a/0x100 device_remove+0x6f/0xb0 Fixes: 0bbb265f7089 ("EDAC/mc: Get rid of silly one-shot struct allocation in edac_mc_alloc()") Co-developed-by: Lili Li Signed-off-by: Lili Li Tested-by: Lili Li Signed-off-by: Qiuxu Zhuo --- drivers/edac/edac_mc.c | 19 +++++++++++++++---- include/linux/edac.h | 5 +++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/drivers/edac/edac_mc.c b/drivers/edac/edac_mc.c index 6faeb2ab3960..6a68b0225130 100644 --- a/drivers/edac/edac_mc.c +++ b/drivers/edac/edac_mc.c @@ -201,7 +201,14 @@ static void mci_release(struct device *dev) } kfree(mci->csrows); } - kfree(mci->pvt_info); + + /* + * if !pvt_managed_by_edac_core, the resource, e.g. memory or MMIO-mapped + * registers pointed by pvt_info is managed by the EDAC driver. The EDAC + * core shouldn't kfree it. + */ + if (mci->pvt_managed_by_edac_core) + kfree(mci->pvt_info); kfree(mci->layers); kfree(mci); } @@ -369,9 +376,13 @@ struct mem_ctl_info *edac_mc_alloc(unsigned int mc_num, if (!mci->layers) goto error; - mci->pvt_info = kzalloc(sz_pvt, GFP_KERNEL); - if (!mci->pvt_info) - goto error; + if (sz_pvt) { + mci->pvt_info = kzalloc(sz_pvt, GFP_KERNEL); + if (!mci->pvt_info) + goto error; + + mci->pvt_managed_by_edac_core = true; + } mci->dev.release = mci_release; device_initialize(&mci->dev); diff --git a/include/linux/edac.h b/include/linux/edac.h index fa4bda2a70f6..6f9f4893ba77 100644 --- a/include/linux/edac.h +++ b/include/linux/edac.h @@ -567,6 +567,11 @@ struct mem_ctl_info { const char *ctl_name; const char *dev_name; void *pvt_info; + /* + * Indicate whether the resource pointed by pvt_info is managed by + * the EDAC core. + */ + bool pvt_managed_by_edac_core; unsigned long start_time; /* mci load start time (in jiffies) */ /* From patchwork Sun Oct 8 08:02:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Zhuo, Qiuxu" X-Patchwork-Id: 13412641 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 B7A23E95A67 for ; Sun, 8 Oct 2023 08:05:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231193AbjJHIFz (ORCPT ); Sun, 8 Oct 2023 04:05:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34112 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229945AbjJHIFz (ORCPT ); Sun, 8 Oct 2023 04:05:55 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DBC59C5; Sun, 8 Oct 2023 01:05:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1696752353; x=1728288353; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=SNNpZq9DeJkCOqcsWszbC19a6ENGnr7eTy1pzxaaE98=; b=g7I8NfMYDBbLtuFdc43hckw4fRiOablrAPEZ8hyyX2fNfJSIoBSaK7Hi /PzNvkhxePjq1/G48qhthTQt/bfgcdrZYot7JBiLyItENSLZoCvdblyyP rwzafGPg0n+IKj0rNvK2vWxes7vb+1Cty0aJEjuaMlitxctglD9qNEkGS 4OE4SC1pHDyHfnU1g66+Ggr4Gtnv+CEb8blwRajfI4vhVijJoAWPeGHAV 3KZTu5sQHPSEf1408OQLY8tQvapEL1WiExxMBsDHFmdczBhX4LfjOkuxg R2oO8gadBK394prlLv5r+8XWPS17GmiQHckgLcYi0dZiBxQu+2S0P59zZ A==; X-IronPort-AV: E=McAfee;i="6600,9927,10856"; a="383864087" X-IronPort-AV: E=Sophos;i="6.03,207,1694761200"; d="scan'208";a="383864087" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Oct 2023 01:05:53 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10856"; a="876474855" X-IronPort-AV: E=Sophos;i="6.03,207,1694761200"; d="scan'208";a="876474855" Received: from qiuxu-clx.sh.intel.com ([10.239.53.109]) by orsmga004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Oct 2023 01:05:50 -0700 From: Qiuxu Zhuo To: Borislav Petkov , Tony Luck Cc: Lili Li , James Morse , Mauro Carvalho Chehab , Robert Richter , Qiuxu Zhuo , linux-edac@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/3] EDAC/device: Fix potential slab-use-after-free Date: Sun, 8 Oct 2023 16:02:30 +0800 Message-Id: <20231008080231.51917-2-qiuxu.zhuo@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20231008080231.51917-1-qiuxu.zhuo@intel.com> References: <20231008080231.51917-1-qiuxu.zhuo@intel.com> Precedence: bulk List-ID: X-Mailing-List: linux-edac@vger.kernel.org From: Lili Li If the EDAC driver invokes edac_device_alloc_ctl_info() with pvt_sz=0, the EDAC driver itself may manage 'pvt_info' and the EDAC core shouldn't kfree it when unloading the EDAC driver. Otherwise it may lead to a slab-use-after-free bug. Fix the potential bug by adding a flag to indicate whether 'pvt_info' is managed by the EDAC core. The EDAC core will only kfree 'pvt_info' when the flag is set to true. Fixes: 9fb9ce392aae ("EDAC/device: Get rid of the silly one-shot memory allocation in edac_device_alloc_ctl_info()") Suggested-by: Qiuxu Zhuo Signed-off-by: Lili Li --- drivers/edac/edac_device.c | 1 + drivers/edac/edac_device.h | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/drivers/edac/edac_device.c b/drivers/edac/edac_device.c index 0689e1510721..b990431df2eb 100644 --- a/drivers/edac/edac_device.c +++ b/drivers/edac/edac_device.c @@ -100,6 +100,7 @@ edac_device_alloc_ctl_info(unsigned pvt_sz, char *dev_name, unsigned nr_instance goto free; dev_ctl->pvt_info = pvt; + dev_ctl->pvt_managed_by_edac_core = true; } dev_ctl->dev_idx = device_index; diff --git a/drivers/edac/edac_device.h b/drivers/edac/edac_device.h index 3f44e6b9d387..cc6a364a4b83 100644 --- a/drivers/edac/edac_device.h +++ b/drivers/edac/edac_device.h @@ -197,6 +197,11 @@ struct edac_device_ctl_info { const char *dev_name; /* pci/platform/etc... name */ void *pvt_info; /* pointer to 'private driver' info */ + /* + * Indicate whether the resource pointed by pvt_info is managed by + * the EDAC core. + */ + bool pvt_managed_by_edac_core; unsigned long start_time; /* edac_device load start time (jiffies) */ @@ -355,7 +360,8 @@ extern const char *edac_layer_name[]; static inline void __edac_device_free_ctl_info(struct edac_device_ctl_info *ci) { if (ci) { - kfree(ci->pvt_info); + if (ci->pvt_managed_by_edac_core) + kfree(ci->pvt_info); kfree(ci->attribs); kfree(ci->blocks); kfree(ci->instances); From patchwork Sun Oct 8 08:02:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Zhuo, Qiuxu" X-Patchwork-Id: 13412642 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 A4C80E95A67 for ; Sun, 8 Oct 2023 08:06:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234215AbjJHIGn (ORCPT ); Sun, 8 Oct 2023 04:06:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37670 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234247AbjJHIGm (ORCPT ); Sun, 8 Oct 2023 04:06:42 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE7FEF0; Sun, 8 Oct 2023 01:06:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1696752397; x=1728288397; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=8S2MqqTgJdBHZ66P8fTZI04cW1lUao8eGVoF7GOEfAc=; b=Ug5sNrMQsoMSAp5DTIYlz2v7Ow9txHGQma4KJYJXv/DQjfHhiWoqvrP2 DMbhPmLRY37DJZA5x2+zyw1JU+ZhRZoxwQpXAs2lQbBhIsulU2re4ufDY ngN62LQ2qhDFidUXLQ+VFQRuP9rwIC8I8bFPR+rRWo4yLGFTnGqrFVhj3 Ea2TVj9Ar1KZGF+kJOMctrK0WIgeizzN6WZYFOJx7cF3c1jgT8van7Ls/ 1WyssNo8ix2lzaY19t93Dq7nSMLVrPitoXh71nh2dc/ItYSCZeXvyo8L3 Pv1J9TMnC6Xb9lu1zPoQzW19JahUlRyfBLS+tiL7GLupYJFD4sEB5NgFN Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10856"; a="383864116" X-IronPort-AV: E=Sophos;i="6.03,207,1694761200"; d="scan'208";a="383864116" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Oct 2023 01:06:37 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10856"; a="876475017" X-IronPort-AV: E=Sophos;i="6.03,207,1694761200"; d="scan'208";a="876475017" Received: from qiuxu-clx.sh.intel.com ([10.239.53.109]) by orsmga004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Oct 2023 01:06:34 -0700 From: Qiuxu Zhuo To: Borislav Petkov , Tony Luck Cc: Lili Li , James Morse , Mauro Carvalho Chehab , Robert Richter , Qiuxu Zhuo , linux-edac@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 3/3] EDAC/pci: Fix a potential memory leak Date: Sun, 8 Oct 2023 16:02:31 +0800 Message-Id: <20231008080231.51917-3-qiuxu.zhuo@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20231008080231.51917-1-qiuxu.zhuo@intel.com> References: <20231008080231.51917-1-qiuxu.zhuo@intel.com> Precedence: bulk List-ID: X-Mailing-List: linux-edac@vger.kernel.org From: Lili Li The EDAC PCI core misses kfreeing 'pvt_info' which may result in a memory leak. Fix it by adding a flag to indicate whether 'pvt_info' is allocated by the EDAC PCI core and kfreeing 'pvt_info' by the EDAC PCI core when the flag is set to true. Fixes: fb8cd45ca39b ("EDAC/pci: Get rid of the silly one-shot memory allocation in edac_pci_alloc_ctl_info()") Suggested-by: Qiuxu Zhuo Signed-off-by: Lili Li --- drivers/edac/edac_pci.c | 1 + drivers/edac/edac_pci.h | 5 +++++ drivers/edac/edac_pci_sysfs.c | 2 ++ 3 files changed, 8 insertions(+) diff --git a/drivers/edac/edac_pci.c b/drivers/edac/edac_pci.c index 64c142aecca7..7c9d1d9115c4 100644 --- a/drivers/edac/edac_pci.c +++ b/drivers/edac/edac_pci.c @@ -40,6 +40,7 @@ struct edac_pci_ctl_info *edac_pci_alloc_ctl_info(unsigned int sz_pvt, pci->pvt_info = kzalloc(sz_pvt, GFP_KERNEL); if (!pci->pvt_info) goto free; + pci->pvt_managed_by_edac_core = true; } pci->op_state = OP_ALLOC; diff --git a/drivers/edac/edac_pci.h b/drivers/edac/edac_pci.h index 5175f5724cfa..27fecf6bfafc 100644 --- a/drivers/edac/edac_pci.h +++ b/drivers/edac/edac_pci.h @@ -69,6 +69,11 @@ struct edac_pci_ctl_info { const char *dev_name; /* pci/platform/etc... name */ void *pvt_info; /* pointer to 'private driver' info */ + /* + * Indicate whether the resource pointed by pvt_info is managed by + * EDAC core + */ + bool pvt_managed_by_edac_core; unsigned long start_time; /* edac_pci load start time (jiffies) */ diff --git a/drivers/edac/edac_pci_sysfs.c b/drivers/edac/edac_pci_sysfs.c index 287cc51dbc86..520fb2fa9411 100644 --- a/drivers/edac/edac_pci_sysfs.c +++ b/drivers/edac/edac_pci_sysfs.c @@ -83,6 +83,8 @@ static void edac_pci_instance_release(struct kobject *kobj) /* decrement reference count on top main kobj */ kobject_put(edac_pci_top_main_kobj); + if (pci->pvt_managed_by_edac_core) + kfree(pci->pvt_info); kfree(pci); /* Free the control struct */ }