From patchwork Thu Dec 12 10:10:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Weikang Guo X-Patchwork-Id: 13904947 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 80382E7717F for ; Thu, 12 Dec 2024 10:10:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B837D6B007B; Thu, 12 Dec 2024 05:10:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B34A36B0082; Thu, 12 Dec 2024 05:10:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9FC8C6B0083; Thu, 12 Dec 2024 05:10:18 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 8298B6B007B for ; Thu, 12 Dec 2024 05:10:18 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 02E6D161776 for ; Thu, 12 Dec 2024 10:10:17 +0000 (UTC) X-FDA: 82885886202.12.EC24F0F Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) by imf13.hostedemail.com (Postfix) with ESMTP id 3BBC220004 for ; Thu, 12 Dec 2024 10:09:52 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=XWSY5SDd; spf=pass (imf13.hostedemail.com: domain of guoweikang.kernel@gmail.com designates 209.85.214.174 as permitted sender) smtp.mailfrom=guoweikang.kernel@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1733998199; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=zZanVRx4WAhit1sZ/hOnh7NH4U6nFyxymYnnlN3ZoUU=; b=HdDrKr/GoOE85RXCYC5/u6dIjFGe0TDtv0XDhNMqQcok/OCXEhwhJXD/puUOKKJyBGL2Eb 6hchc3yJH3rOAszObwjQwiT4eXaTOgreizN3mrHh+B4o/6N1cD/hTjtqq72XaV7NDz/HNm YkC1LJ3nNJ9Fd2CvF0dxmQ+cpzipDiM= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=XWSY5SDd; spf=pass (imf13.hostedemail.com: domain of guoweikang.kernel@gmail.com designates 209.85.214.174 as permitted sender) smtp.mailfrom=guoweikang.kernel@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733998199; a=rsa-sha256; cv=none; b=Pmx4p5HB4Q1hUuNoe4dc2NOFJoXBQ+2lx2Smqw3RsUvcZoVNGE5q//GwKi1DvEDrW2djAo b6QWh9qJOTKavCmvFK4iSQHBYPa89DKND2KPhNz0ktKMvbRpbuuaH0Xj9VjEnHQYFw4hk5 8QXpKwA3RTq39cJvbA5+p3wzMEQcoqc= Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-216281bc30fso4616875ad.0 for ; Thu, 12 Dec 2024 02:10:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733998215; x=1734603015; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=zZanVRx4WAhit1sZ/hOnh7NH4U6nFyxymYnnlN3ZoUU=; b=XWSY5SDdcIO6YQ1fNA2rM3nyGAoY8pfXAfoHTC1yEj44rJEecbC4QNlLv12KdLA/9L D2SnhwFB6GYeWfby5KJUN6jsYQgX+7NlnA4u3wbLIVgnRwwn/B1MEX9GE2tX8YQm/Kav kE3b2TT6ZGc0akmAkN1ZONKb1U0Z848M91BZ707U98hcZFTu2mTn9TkPWcon9vuTVrot R0ynrcx45lFPJYqdf0byp0FNpGPHHcmzrQxzVZ0FuYfB19e9eD3iIYeeHo38FnPUzuBh 21qFP6FjTH+YEh1hj0wlyUeQ4c8+yRmEZKSZetaiKqDxEJ9/FZRahqEab92EPS1GO85A PZwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733998215; x=1734603015; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=zZanVRx4WAhit1sZ/hOnh7NH4U6nFyxymYnnlN3ZoUU=; b=i+SkzzwEnS+zIjL7Sn6POzr0dbufQtjY+uTSaaOQmpM7hvK/YcldwaGIJuVrGgOzsm mbNs14DNE5/95IePUJnZ5w0C1dDtcYoXD4tWR3BNG/Oz2G+Bm2PhhilERB5rormCxlcd 59DS+cXBUlptXfHcNjwvkuHV+bjvInhxtQvXXQUWkh6CGrgQIamvyOlYv3awnonV07AZ cOUR3Yk1o3r17CES7woJW5C9sPO0OauEwjYoba9WoIDC7avKi3GQfMrI45gA9R8XAfKd oGEbdgjIQcESZW/RmFYcDo5GQGUf2glz9doG1wDNs3MuLnwwLBvLn8/vrfrn3wr0ktwv qEbw== X-Forwarded-Encrypted: i=1; AJvYcCWHHE4UNs+YR6toyQyV+Ied4Lvwdqmfhl5n1TghAfNP4XcJOWi29BTAJOv9ystMrgDsG0MoGIkiKg==@kvack.org X-Gm-Message-State: AOJu0Yy+iS23MbSisIaAkygoBNDvfVRvk497AlMuvQ8dbCpF1N6HuqNX 3+4/SkW2KWpOGUDQyW/tgwWaNRDozi+lA/20UGFt4ROcmp5bhpit X-Gm-Gg: ASbGnctPjh3MojgghlSYbVJ0xBWLZaIm5v4THtynuZc1lFMsZo5L/+HmZwmBfKwhLtw RUa8QyuSEadQNh61Fa4zmJzpJVKFVHUET6EyfwO0IkbkHn8+5/+CPJBTEWZ32QU28wQ7cul/mZ2 8j+MRaR5/78Dk9405vA2vfEwLSfjkEtaHrMXUF5yyYuhy94nlIcS647YgH4cBlkUnmlz2MPJiM/ 7GPE1Jr7plybuYzkYwmV7gaxyaPLzvzl1yzri359xGdubSZeKn20HYfDUlJ4U6dAvDvlit+g3eB oG9A X-Google-Smtp-Source: AGHT+IH6gcwPQeo8o7QWFQKnzChC6uQO93gPkmdmDG/SQmzj5FSwJNJHWHb5JoN5L+bUnuXzcp5J7w== X-Received: by 2002:a17:902:d389:b0:215:6816:6345 with SMTP id d9443c01a7336-21778536e08mr75258075ad.16.1733998214774; Thu, 12 Dec 2024 02:10:14 -0800 (PST) Received: from localhost.localdomain ([36.110.106.149]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7fd1570ae4esm10519521a12.43.2024.12.12.02.10.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Dec 2024 02:10:14 -0800 (PST) From: Guo Weikang To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , "Rafael J. Wysocki" , Len Brown , Arnd Bergmann , Andrew Morton , Baoquan He , Ard Biesheuvel , "Jason A. Donenfeld" , Julian Stecklina , "Xin Li (Intel)" , Guo Weikang Cc: Ingo Molnar , Kevin Loughlin , "Rafael J. Wysocki" , linux-kernel@vger.kernel.org, linux-acpi@vger.kernel.org, linux-arch@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH] mm/early_ioremap: Add null pointer checks to prevent NULL-pointer dereference Date: Thu, 12 Dec 2024 18:10:00 +0800 Message-Id: <20241212101004.1544070-1-guoweikang.kernel@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Rspamd-Server: rspam05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5JT9E5hThhht5qfYXLkGEqZ MEBd89vh fsmV8serO4D8WqBcuokrJk9Pty2FeMrDDzJSD33JgvrRlaA6vbiP008fRQT589ATBWCa9ROUpmyGuOI+V+BDgVnnoKoU3IRUDmchtl/LdhizXqnFiYgfkkfT+eHA+PXDYqATYnhVwpuDAe5/D+tA5gvPRJNscez/OZmm2AK898CZnDynN4kYbqjjg+RP3UTV9pV41MUvEX3TE3InB6jxs6YNwdglbD4wAM4Jam7/16ccx97OTD7O/d5Ej8EIt9y73LFJajULDveKpDR1oVM6z2it3xD3AypM//6iY8WEZlgeA7zHVmL7t880ZISv8LUFpfceyybp8U8xgMfWBvJTxJ+0IAsoZ1DRug0WdSFRhrQ1250qzQaVkR4F1Mpwjt5+yQyeuIhYRvT/wedrchRg/3XG/voj/O0B5WUG8KVpypFEMQRg7RvWkh+NOLs96fSPjt0iN5624pLsrCLu3kNAyg4xbq5OcMcYTxTy6fmLwgEw8cRDZBP7zgoIIyKBmt3Va4hmIpjZO1F3gI/SCGwoUmrCMZQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: The early_ioremap interface can fail and return NULL in certain cases. To prevent NULL-pointer dereference crashes, fixed issues in the acpi_extlog and copy_early_mem interfaces, improving robustness when handling early memory. Signed-off-by: Guo Weikang --- arch/x86/kernel/setup.c | 5 ++++- drivers/acpi/acpi_extlog.c | 14 ++++++++++++++ include/asm-generic/early_ioremap.h | 2 +- mm/early_ioremap.c | 8 +++++++- 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c index f1fea506e20f..cebee310e200 100644 --- a/arch/x86/kernel/setup.c +++ b/arch/x86/kernel/setup.c @@ -259,6 +259,7 @@ static void __init relocate_initrd(void) u64 ramdisk_image = get_ramdisk_image(); u64 ramdisk_size = get_ramdisk_size(); u64 area_size = PAGE_ALIGN(ramdisk_size); + int ret = 0; /* We need to move the initrd down into directly mapped mem */ u64 relocated_ramdisk = memblock_phys_alloc_range(area_size, PAGE_SIZE, 0, @@ -272,7 +273,9 @@ static void __init relocate_initrd(void) printk(KERN_INFO "Allocated new RAMDISK: [mem %#010llx-%#010llx]\n", relocated_ramdisk, relocated_ramdisk + ramdisk_size - 1); - copy_from_early_mem((void *)initrd_start, ramdisk_image, ramdisk_size); + ret = copy_from_early_mem((void *)initrd_start, ramdisk_image, ramdisk_size); + if (ret) + panic("Copy RAMDISK failed\n"); printk(KERN_INFO "Move RAMDISK from [mem %#010llx-%#010llx] to" " [mem %#010llx-%#010llx]\n", diff --git a/drivers/acpi/acpi_extlog.c b/drivers/acpi/acpi_extlog.c index ca87a0939135..f7fb7205028d 100644 --- a/drivers/acpi/acpi_extlog.c +++ b/drivers/acpi/acpi_extlog.c @@ -251,6 +251,10 @@ static int __init extlog_init(void) } extlog_l1_hdr = acpi_os_map_iomem(l1_dirbase, l1_hdr_size); + if (!extlog_l1_hdr) { + rc = -ENOMEM; + goto err_release_l1_hdr; + } l1_head = (struct extlog_l1_head *)extlog_l1_hdr; l1_size = l1_head->total_len; l1_percpu_entry = l1_head->entries; @@ -268,6 +272,10 @@ static int __init extlog_init(void) goto err; } extlog_l1_addr = acpi_os_map_iomem(l1_dirbase, l1_size); + if (!extlog_l1_addr) { + rc = -ENOMEM; + goto err_release_l1_dir; + } l1_entry_base = (u64 *)((u8 *)extlog_l1_addr + l1_hdr_size); /* remap elog table */ @@ -279,6 +287,10 @@ static int __init extlog_init(void) goto err_release_l1_dir; } elog_addr = acpi_os_map_iomem(elog_base, elog_size); + if (!elog_addr) { + rc = -ENOMEM; + goto err_release_elog; + } rc = -ENOMEM; /* allocate buffer to save elog record */ @@ -300,6 +312,8 @@ static int __init extlog_init(void) if (extlog_l1_addr) acpi_os_unmap_iomem(extlog_l1_addr, l1_size); release_mem_region(l1_dirbase, l1_size); +err_release_l1_hdr: + release_mem_region(l1_dirbase, l1_hdr_size); err: pr_warn(FW_BUG "Extended error log disabled because of problems parsing f/w tables\n"); return rc; diff --git a/include/asm-generic/early_ioremap.h b/include/asm-generic/early_ioremap.h index 9d0479f50f97..5db59a1efb65 100644 --- a/include/asm-generic/early_ioremap.h +++ b/include/asm-generic/early_ioremap.h @@ -35,7 +35,7 @@ extern void early_ioremap_reset(void); /* * Early copy from unmapped memory to kernel mapped memory. */ -extern void copy_from_early_mem(void *dest, phys_addr_t src, +extern int copy_from_early_mem(void *dest, phys_addr_t src, unsigned long size); #else diff --git a/mm/early_ioremap.c b/mm/early_ioremap.c index ce06b2884789..ff35b84a7b50 100644 --- a/mm/early_ioremap.c +++ b/mm/early_ioremap.c @@ -245,7 +245,10 @@ early_memremap_prot(resource_size_t phys_addr, unsigned long size, #define MAX_MAP_CHUNK (NR_FIX_BTMAPS << PAGE_SHIFT) -void __init copy_from_early_mem(void *dest, phys_addr_t src, unsigned long size) +/* + * If no empty slot, handle that and return -ENOMEM. + */ +int __init copy_from_early_mem(void *dest, phys_addr_t src, unsigned long size) { unsigned long slop, clen; char *p; @@ -256,12 +259,15 @@ void __init copy_from_early_mem(void *dest, phys_addr_t src, unsigned long size) if (clen > MAX_MAP_CHUNK - slop) clen = MAX_MAP_CHUNK - slop; p = early_memremap(src & PAGE_MASK, clen + slop); + if (!p) + return -ENOMEM; memcpy(dest, p + slop, clen); early_memunmap(p, clen + slop); dest += clen; src += clen; size -= clen; } + return 0; } #else /* CONFIG_MMU */