From nobody Thu Apr 2 15:37:25 2026 Received: from canpmsgout09.his.huawei.com (canpmsgout09.his.huawei.com [113.46.200.224]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5D2C8388396; Thu, 2 Apr 2026 07:26:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=113.46.200.224 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775114768; cv=none; b=pJueUgpPXDE4rLBMqC9h2RmM+/dqloYara4iCfjVjRZf0JMGQmw5PUMi8u7V/QiyeV/vbqunnaqfqcSoIpdGP1fgKRQ9e1y0V8FVfGbi5kkP7ymd4Bjxi1VnxAW3mczfvcmPzUWaYVqXcahgAPe2ZxI//B7LWlHnVaa0qFwHRAs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775114768; c=relaxed/simple; bh=RlpWDDeLpWKT8hTmhedF503Wm/VkSJwpxbTeDK/75M4=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=YuxoOaymMffJa50KXZo1DTOYyFceGpQ2JS5hY2YYD5afvXgdlHHKll5A/rhmj/Vf83jCI+cTgey2dyIPyyFlbSrKc5+dOhX9t/X94nfcviSzSv4TjpA5jG/Ctu3KHweCHvSA99ot07eAXIQxVF4jriU9fujykRjstKzlWfVt/dQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b=54xcYUdu; arc=none smtp.client-ip=113.46.200.224 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b="54xcYUdu" 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 canpmsgout09.his.huawei.com (SkyGuard) with ESMTPS id 4fmYCD5G1Rz1cyPx; Thu, 2 Apr 2026 15:19:52 +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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: kwepems100002.china.huawei.com (7.221.188.206) To dggpemf500011.china.huawei.com (7.185.36.131) Content-Type: text/plain; charset="utf-8" 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/ke= rnel/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 lon= g *sz) cmem->max_nr_ranges =3D nr_ranges; cmem->nr_ranges =3D 0; for_each_mem_range(i, &start, &end) { + if (cmem->nr_ranges >=3D cmem->max_nr_ranges) { + ret =3D -ENOMEM; + goto out; + } + cmem->ranges[cmem->nr_ranges].start =3D start; cmem->ranges[cmem->nr_ranges].end =3D end - 1; cmem->nr_ranges++; --=20 2.34.1