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 25EAECC6B11 for ; Thu, 2 Apr 2026 07:26:28 +0000 (UTC) Received: from boromir.ozlabs.org (localhost [127.0.0.1]) by lists.ozlabs.org (Postfix) with ESMTP id 4fmYLL2DXqz2yZc; Thu, 02 Apr 2026 18:26:02 +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=1775114762; cv=none; b=i+joPWKDqY8iMLPJlwFDTiz6Fgo3Cs6NMpqsVt9p9KJMuwcG8B66UCXBtCx+7Gm1xbqvsUV1GimaMRc0KBL/o+facWNtrxMcJFx6tDKoVPLRB+RH8dWbpk5FuC9G4tQlQRqCnvsfsXmh3XycPB6Gh1UTJGxPrivRYQFiSNxrGLQsP1oSy0ArVz2LgwTs09K1rxRCxPKYJw2oPkXdOzzXoAsVhbPwrW6LHkQTCWKDeyCNu5Ww9ddwdrHJnvi6JleObMys5qYvk8ZpHEXkPMdfwVBP/J05g+ouxXt+zIxjxZYzgpC8yp6pJC1V1vylok+UTAhfGYrHVb5d2u+FguPxYA== ARC-Message-Signature: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1775114762; c=relaxed/relaxed; bh=2CVL6M7h4vY4kI4KzhS6vFtDcU8tP4DeufZv1p45yx8=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=nWqNwcpT1+hwhOmHuxbAxSpRr32hpoCuq92v2i0akghbHTMaRDUxTsNmgA6XA902g595+v5nSw+7eObIZuGhXAVMA2nwkVa3nZKvV6vvurMEEti6DgTP5OE6MJ/daV45mPVxmPcBuT/NsPlTc1CEqsEmuJXRlT2b7V6LwFQ1TGU1NpJR2fpXBJN/ZhECbl45b2+tOsMuxWu8Bjmcq+ENkvIALQiNdFlAHReW+lf4c6BlVN5xcnVARzvNmhrdeBQiC9uKVJlz/e9qc1MTzK65lKTO8UUgyXfxDYrm37+n7E7I7fZrNpTFEgvxpWvpCD7MAofKGi53TZQ5QgB9XBBkNw== 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=Hh6dkgZQ; 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=Hh6dkgZQ; 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 4fmYLK39Mpz2yYK for ; Thu, 02 Apr 2026 18:26:01 +1100 (AEDT) dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=2CVL6M7h4vY4kI4KzhS6vFtDcU8tP4DeufZv1p45yx8=; b=Hh6dkgZQcTT1fQKxm1gP5TRn0Ugnr1hO9wW6ndbdksU/Dokcvl9DqG9mj5IhUFYQyRr6MBv6I quSBZOA4Eb8bx6CLRE0Gc/xp9qPblaGRburMfLWy5Dff6nsQC2yFAn386Ocl3CwKCnHWmkBVHAs ZqkM3+yoksIFhlU8Cv8y86k= Received: from mail.maildlp.com (unknown [172.19.163.15]) by canpmsgout08.his.huawei.com (SkyGuard) with ESMTPS id 4fmYC71jnzzmVD8; Thu, 2 Apr 2026 15:19:47 +0800 (CST) Received: from dggpemf500011.china.huawei.com (unknown [7.185.36.131]) by mail.maildlp.com (Postfix) with ESMTPS id 10FCD40571; Thu, 2 Apr 2026 15:25:59 +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:55 +0800 From: Jinjie Ruan To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , CC: Subject: [PATCH v12 05/15] riscv: kexec_file: Fix potential buffer overflow in prepare_elf_headers() Date: Thu, 2 Apr 2026 15:26:51 +0800 Message-ID: <20260402072701.628293-6-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. get_nr_ram_ranges_callback() queries current System RAM memory ranges 2. Allocates buffer based on queried count 3. prepare_elf64_ram_headers_callback() 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 in prepare_elf64_ram_headers_callback() to prevent out-of-bounds (OOB) access. Fixes: 8acea455fafa ("RISC-V: Support for kexec_file on panic") Reviewed-by: Guo Ren Signed-off-by: Jinjie Ruan --- arch/riscv/kernel/machine_kexec_file.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/arch/riscv/kernel/machine_kexec_file.c b/arch/riscv/kernel/machine_kexec_file.c index 3f7766057cac..773a1cba8ba0 100644 --- a/arch/riscv/kernel/machine_kexec_file.c +++ b/arch/riscv/kernel/machine_kexec_file.c @@ -48,6 +48,9 @@ static int prepare_elf64_ram_headers_callback(struct resource *res, void *arg) { struct crash_mem *cmem = arg; + if (cmem->nr_ranges >= cmem->max_nr_ranges) + return -ENOMEM; + cmem->ranges[cmem->nr_ranges].start = res->start; cmem->ranges[cmem->nr_ranges].end = res->end; cmem->nr_ranges++; -- 2.34.1