From mboxrd@z Thu Jan 1 00:00:00 1970 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 lists.ozlabs.org (lists.ozlabs.org [112.213.38.117]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 92D5ECC6B0E for ; Thu, 2 Apr 2026 07:26:35 +0000 (UTC) Received: from boromir.ozlabs.org (localhost [127.0.0.1]) by lists.ozlabs.org (Postfix) with ESMTP id 4fmYLS3Yfsz2ydq; Thu, 02 Apr 2026 18:26:08 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; arc=none smtp.remote-ip=113.46.200.223 ARC-Seal: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1775114768; cv=none; b=WB390cVvSUtI8/vnXpfYaMSI+198BMuRazLb62gnvzbpoF1IcbVbvx5JV4R9GmE67MBbAO+Om9GiseHlBvTMTmopRK7l1YpAHzhfYQl4pPdqCUy9XgWyf+hPZ6oknjmXFHITZlJ0o9YoOT/crGHLtY91j5/bQQoFCz8+MX6NX0EtFcKinMeCnrnKknxpe/XeEt1Tj6in3amyP9kK4P6F3vQ2xt8qKXW3mKcvpPzkJCTLTov+/a/MQrQ/yv+iX0XOeJ8Kae+cVqEgnOJyBfVQsJH0viWzj3ATqnYvZBoFbkpO0Y5Ja9KrTikOVhGPkgbgsmBud4YzV5vnibK5rMFAMA== ARC-Message-Signature: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1775114768; c=relaxed/relaxed; bh=sd1rfkCQX0roc8NJSy9Xy3WAJ3YSwR10tehKBLMeGJM=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Upg9T9fgGGrmHkHPTLWuYISMb4PoiQzU6wd84iZmtppDDhfGk1kIHBzbUSweP5h7hAvC828Q3KK9oX6SCT+nsOdLyKuMUMB6Shl3GPUzdHEF5fSizWJzvXiMdBP4Rj14KRiIX/54lsgp8EW2+FAAFS2/1JQXReS2d3QxuM5dr0BPYubBkHpPA9xP5vySzCTlqf2gVVUpYlk5rJFa/Ut56zuCKmcadpHGF6nv7CILO6dkZJ3vLca89wZFmgqUm1nkxZh/AULOoLho85C2BCQVpwk45Cc2jIvHYKFI6N33mhlr2m8WeOGI/UqlSegq/kIhABmQACWBJPGu0PYp0l6fWQ== ARC-Authentication-Results: i=1; lists.ozlabs.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; dkim=pass (1024-bit key; unprotected) header.d=huawei.com header.i=@huawei.com header.a=rsa-sha256 header.s=dkim header.b=54xcYUdu; dkim-atps=neutral; spf=pass (client-ip=113.46.200.223; helo=canpmsgout08.his.huawei.com; envelope-from=ruanjinjie@huawei.com; receiver=lists.ozlabs.org) smtp.mailfrom=huawei.com Authentication-Results: lists.ozlabs.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: lists.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=huawei.com header.i=@huawei.com header.a=rsa-sha256 header.s=dkim header.b=54xcYUdu; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=huawei.com (client-ip=113.46.200.223; helo=canpmsgout08.his.huawei.com; envelope-from=ruanjinjie@huawei.com; receiver=lists.ozlabs.org) Received: from canpmsgout08.his.huawei.com (canpmsgout08.his.huawei.com [113.46.200.223]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4fmYLR4L28z2yYK for ; Thu, 02 Apr 2026 18:26:07 +1100 (AEDT) dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=sd1rfkCQX0roc8NJSy9Xy3WAJ3YSwR10tehKBLMeGJM=; b=54xcYUduJo0UMWYr/pt0k0Np2njxzVf31Sm1XJs4s+8NBJyfujKnT9Yq64qKgE+bNo/t3cxN6 aFYtruhAdT+Fghs1MDmCQdvh4RUvSrL77tLPc5dDCVcz4Gi4KWEDN3Q27oDNvVf/LZqqnoAk5Z4 +C2/qr++3q+V2Pz2Q1xKxf0= Received: from mail.maildlp.com (unknown [172.19.162.92]) by canpmsgout08.his.huawei.com (SkyGuard) with ESMTPS id 4fmYCB3W3WzmVD8; Thu, 2 Apr 2026 15:19:50 +0800 (CST) Received: from dggpemf500011.china.huawei.com (unknown [7.185.36.131]) by mail.maildlp.com (Postfix) with ESMTPS id 4E8284056C; Thu, 2 Apr 2026 15:26:02 +0800 (CST) Received: from huawei.com (10.90.53.73) by dggpemf500011.china.huawei.com (7.185.36.131) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Thu, 2 Apr 2026 15:25:59 +0800 From: Jinjie Ruan To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , CC: Subject: [PATCH v12 06/15] LoongArch: kexec: Fix potential buffer overflow in prepare_elf_headers() Date: Thu, 2 Apr 2026 15:26:52 +0800 Message-ID: <20260402072701.628293-7-ruanjinjie@huawei.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260402072701.628293-1-ruanjinjie@huawei.com> References: <20260402072701.628293-1-ruanjinjie@huawei.com> X-Mailing-List: linuxppc-dev@lists.ozlabs.org List-Id: List-Help: List-Owner: List-Post: List-Archive: , List-Subscribe: , , List-Unsubscribe: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [10.90.53.73] X-ClientProxiedBy: kwepems100002.china.huawei.com (7.221.188.206) To dggpemf500011.china.huawei.com (7.185.36.131) There is a race condition between the kexec_load() system call (crash kernel loading path) and memory hotplug operations that can lead to buffer overflow and potential kernel crash. During prepare_elf_headers(), the following steps occur: 1. The first for_each_mem_range() queries current System RAM memory ranges 2. Allocates buffer based on queried count 3. The 2st for_each_mem_range() populates ranges from memblock If memory hotplug occurs between step 1 and step 3, the number of ranges can increase, causing out-of-bounds write when populating cmem->ranges[]. This happens because kexec_load() uses kexec_trylock (atomic_t) while memory hotplug uses device_hotplug_lock (mutex), so they don't serialize with each other. Just add bounds checking to prevent out-of-bounds access. Cc: Youling Tang Cc: Huacai Chen Fixes: 1bcca8620a91 ("LoongArch: Add crash dump support for kexec_file") Signed-off-by: Jinjie Ruan --- arch/loongarch/kernel/machine_kexec_file.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/arch/loongarch/kernel/machine_kexec_file.c b/arch/loongarch/kernel/machine_kexec_file.c index 5584b798ba46..167392c1da33 100644 --- a/arch/loongarch/kernel/machine_kexec_file.c +++ b/arch/loongarch/kernel/machine_kexec_file.c @@ -75,6 +75,11 @@ static int prepare_elf_headers(void **addr, unsigned long *sz) cmem->max_nr_ranges = nr_ranges; cmem->nr_ranges = 0; for_each_mem_range(i, &start, &end) { + if (cmem->nr_ranges >= cmem->max_nr_ranges) { + ret = -ENOMEM; + goto out; + } + cmem->ranges[cmem->nr_ranges].start = start; cmem->ranges[cmem->nr_ranges].end = end - 1; cmem->nr_ranges++; -- 2.34.1