From nobody Sat Apr 4 01:49:51 2026 Received: from cstnet.cn (smtp21.cstnet.cn [159.226.251.21]) (using TLSv1.2 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D5806173 for ; Sat, 21 Mar 2026 11:34:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.226.251.21 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774092881; cv=none; b=nC7/hYtu34ssvuRRfqpfONIfo3304hTxN7EoQFyaklY8sIMZXHyr31cCioFtU5Ivx8w5Ey5fiegot0ELxttl/WDE65wJfWzwOc7EgeOp6R3G2OATkXvCkjCRG1VYqv9WA5cmF+wK0qTxJpeidCXkVaSK48LIipGPB0+Kz+Gcp2Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774092881; c=relaxed/simple; bh=pQzYmVjP4QJx2WNXAPFqE3mKDWXloCLIklXv1VNUNHk=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=OUH/EqEU0Jfd6/DxmY1SA3wB7kh+TvXvsHUdm6qUPxEaJ+4k87GKkLSoY53dKIWnRREtwmxcK6MIs+WqzK+Dz11ITjH279f1xISrPo0HNyM4Edt7qVW7ZBXXQWcT9XdhBaIjuXUWI4riWqsbKJDC2D8x0jIOjX09+qVml8BYCp0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=iscas.ac.cn; spf=pass smtp.mailfrom=iscas.ac.cn; arc=none smtp.client-ip=159.226.251.21 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=iscas.ac.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=iscas.ac.cn Received: from yzs (unknown [115.192.255.131]) by APP-01 (Coremail) with SMTP id qwCowAB3IW1Bgr5ptGi1Cg--.11653S2; Sat, 21 Mar 2026 19:34:25 +0800 (CST) From: Zishun Yi To: Paul Walmsley , Palmer Dabbelt , Albert Ou Cc: Alexandre Ghiti , Kees Cook , Will Pierce , Maxim Kochetkov , Samuel Holland , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Zishun Yi Subject: [PATCH] riscv: Check the return value of reloc handlers Date: Sat, 21 Mar 2026 19:34:19 +0800 Message-ID: <20260321113419.34437-1-vulab@iscas.ac.cn> X-Mailer: git-send-email 2.51.2 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-CM-TRANSID: qwCowAB3IW1Bgr5ptGi1Cg--.11653S2 X-Coremail-Antispam: 1UD129KBjvJXoWxJF4UZF1ktr43GrWrXr18Krg_yoW5JFWDpF ZxG34ayFWkCw12gF48t3Wvgr4fCa1kWr47WFZ0gw4SywnxCr1rt345tw1FqFZ3Wr929FyU Xr1j9F1UCw4UArDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUU9j14x267AKxVW8JVW5JwAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2ocxC64kIII0Yj41l84x0c7CEw4AK67xGY2AK02 1l84ACjcxK6xIIjxv20xvE14v26r4j6ryUM28EF7xvwVC0I7IYx2IY6xkF7I0E14v26r4j 6F4UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v26rxl6s 0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xII jxv20xvE14v26r126r1DMcIj6I8E87Iv67AKxVW8JVWxJwAm72CE4IkC6x0Yz7v_Jr0_Gr 1lF7xvr2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7M4IIrI8v6xkF7I0E8cxa n2IY04v7MxkF7I0En4kS14v26r1q6r43MxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4 AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE 17CEb7AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMI IF0xvE2Ix0cI8IcVCY1x0267AKxVW8JVWxJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4l IxAIcVC2z280aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvf C2KfnxnUUI43ZEXa7VUbo5l5UUUUU== X-CM-SenderInfo: pyxotu46lvutnvoduhdfq/1tbiBwkNA2m9-DP1mAAAs2 Content-Type: text/plain; charset="utf-8" Currently, process_accumulated_relocations() ignores the return values from both reloc_handler() and accumulate_handler(). As a result, the kernel will proceed to load the module with corrupted or incomplete sections, which can lead to unpredictable behavior or kernel panics. So we need to check the return values of the handlers to propagate the error, and fall back to the cleanup mode. Fixes: 8fd6c5142395 ("riscv: Add remaining module relocations") Signed-off-by: Zishun Yi --- arch/riscv/kernel/module.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/arch/riscv/kernel/module.c b/arch/riscv/kernel/module.c index 1961135689db..b6512fa9aca1 100644 --- a/arch/riscv/kernel/module.c +++ b/arch/riscv/kernel/module.c @@ -594,7 +594,7 @@ static const struct relocation_handlers reloc_handlers[= ] =3D { /* 192-255 nonstandard ABI extensions */ }; =20 -static void +static int process_accumulated_relocations(struct module *me, struct hlist_head **relocation_hashtable, struct list_head *used_buckets_list) @@ -625,6 +625,7 @@ process_accumulated_relocations(struct module *me, int curr_type; void *location; long buffer; + int res, error =3D 0; =20 list_for_each_entry_safe(bucket_iter, bucket_iter_tmp, used_buckets_list, head) { @@ -637,18 +638,27 @@ process_accumulated_relocations(struct module *me, &rel_head_iter->rel_entry, head) { curr_type =3D rel_entry_iter->type; - reloc_handlers[curr_type].reloc_handler( - me, &buffer, rel_entry_iter->value); + if (!error) { + res =3D reloc_handlers[curr_type].reloc_handler( + me, &buffer, rel_entry_iter->value); + if (res) + error =3D res; + } kfree(rel_entry_iter); } - reloc_handlers[curr_type].accumulate_handler( - me, location, buffer); + if (!error) { + res =3D reloc_handlers[curr_type].accumulate_handler( + me, location, buffer); + if (res) + error =3D res; + } kfree(rel_head_iter); } kfree(bucket_iter); } =20 kvfree(*relocation_hashtable); + return error; } =20 static int add_relocation_to_accumulate(struct module *me, int type, @@ -886,10 +896,8 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *= strtab, return res; } =20 - process_accumulated_relocations(me, &relocation_hashtable, + return process_accumulated_relocations(me, &relocation_hashtable, &used_buckets_list); - - return 0; } =20 int module_finalize(const Elf_Ehdr *hdr, --=20 2.51.2