From nobody Fri Jun 12 09:44:22 2026 Received: from cstnet.cn (smtp25.cstnet.cn [159.226.251.25]) (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 6C2282836A6 for ; Sat, 9 May 2026 11:51:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.226.251.25 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778327501; cv=none; b=lkOxDFZnKAecYgBPWehvzRqXRTKY+oPBmLdAPAWTvreBesrFvwSSRbRZWSfssYxjbeCAKg7D/MEbMu8o+4XH5OQ8ZHCfPbq8+PunR8aZIxT7hNxKkA78rOTw6t99YWgTNbnPATffbdG3dNtMCyaljuW8g87L50nRNMIDUnwkpb4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778327501; c=relaxed/simple; bh=x6CK3RdYJpp/cQS6/DuKJhzYKxxf5K5n6gsF+G2ArhE=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=P0CkfX+3yADpo5FAZ6uK6/OkcxO2EDct8DA7eA4Z5HMg9utAc6tfeGtrlcEK4HeAOZy/A8cYvTBcbIhlg7kXGqIvnEMkZtBlybgxbLs9Oxz1FPwEuAM+XK4o+PO4mixnRBmQydkNBEBDgQE/kEHXkcA5snqHVp6rzxlNa0a1ym8= 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.25 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.199.218.204]) by APP-05 (Coremail) with SMTP id zQCowABX6QylH_9pIgPLDw--.3436S2; Sat, 09 May 2026 19:51:01 +0800 (CST) From: Zishun Yi To: Paul Walmsley , Palmer Dabbelt , Albert Ou Cc: Alexandre Ghiti , Kees Cook , Charlie Jenkins , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Zishun Yi Subject: [PATCH v2] riscv: Fix memory leak in apply_relocate_add Date: Sat, 9 May 2026 19:51:00 +0800 Message-ID: <20260509115100.1879716-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: zQCowABX6QylH_9pIgPLDw--.3436S2 X-Coremail-Antispam: 1UD129KBjvJXoWxGr17Xw18CF4fKry8CF18Grg_yoW5ur4UpF 13G34SyrWfGr1xtrnIq3WkWw4fGa1kWr47Z3y29wsaywsxA3WFq343KF1FqF95Xryvgr45 WFWj93WUCw4UAFDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUkl14x267AKxVW8JVW5JwAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2ocxC64kIII0Yj41l84x0c7CEw4AK67xGY2AK02 1l84ACjcxK6xIIjxv20xvE14v26r1I6r4UM28EF7xvwVC0I7IYx2IY6xkF7I0E14v26r4j 6F4UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v26rxl6s 0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xII jxv20xvE14v26r106r15McIj6I8E87Iv67AKxVW8JVWxJwAm72CE4IkC6x0Yz7v_Jr0_Gr 1lF7xvr2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7MxkF7I0En4kS14v26r1q 6r43MxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAFwI 0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUtVW8ZwCIc40Y 0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x0267AKxV W8JVWxJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Jr0_Gr1l IxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7VUbHa0JUUUU U== X-CM-SenderInfo: pyxotu46lvutnvoduhdfq/1tbiDAcCA2n+2sLWYgAAsx Content-Type: text/plain; charset="utf-8" The error path in apply_relocate_add() forgets to clean up the hashtable allocated at the beginning. But we should not use process_accumulated_relocations() as a cleanup function because some entries may be half-finished, and we cannot write incorrect values back to memory. So, add cleanup_accumulated_relocations() for freeing all memory allocated in the error paths. Fixes: 8fd6c5142395 ("riscv: Add remaining module relocations") Assisted-by: Gemini:gemini-3.1-pro Signed-off-by: Zishun Yi --- Changes in v2: - Added 'Assisted-by' tag. arch/riscv/kernel/module.c | 44 +++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/arch/riscv/kernel/module.c b/arch/riscv/kernel/module.c index 1961135689db..5ac3f2cd55bc 100644 --- a/arch/riscv/kernel/module.c +++ b/arch/riscv/kernel/module.c @@ -594,6 +594,38 @@ static const struct relocation_handlers reloc_handlers= [] =3D { /* 192-255 nonstandard ABI extensions */ }; =20 +static void +cleanup_accumulated_relocations(struct hlist_head **relocation_hashtable, + struct list_head *used_buckets_list) +{ + /* + * Clean up accumulated relocations without applying them. + */ + struct used_bucket *bucket_iter; + struct used_bucket *bucket_iter_tmp; + struct relocation_head *rel_head_iter; + struct hlist_node *rel_head_iter_tmp; + struct relocation_entry *rel_entry_iter; + struct relocation_entry *rel_entry_iter_tmp; + + list_for_each_entry_safe(bucket_iter, bucket_iter_tmp, + used_buckets_list, head) { + hlist_for_each_entry_safe(rel_head_iter, rel_head_iter_tmp, + bucket_iter->bucket, node) { + list_for_each_entry_safe(rel_entry_iter, + rel_entry_iter_tmp, + &rel_head_iter->rel_entry, + head) { + kfree(rel_entry_iter); + } + kfree(rel_head_iter); + } + kfree(bucket_iter); + } + + kvfree(*relocation_hashtable); +} + static void process_accumulated_relocations(struct module *me, struct hlist_head **relocation_hashtable, @@ -802,6 +834,8 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *s= trtab, continue; pr_warn("%s: Unknown symbol %s\n", me->name, strtab + sym->st_name); + cleanup_accumulated_relocations(&relocation_hashtable, + &used_buckets_list); return -ENOENT; } =20 @@ -815,6 +849,8 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *s= trtab, if (!handler) { pr_err("%s: Unknown relocation type %u\n", me->name, type); + cleanup_accumulated_relocations(&relocation_hashtable, + &used_buckets_list); return -EINVAL; } =20 @@ -868,6 +904,8 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *s= trtab, pr_err( "%s: Can not find HI20 relocation information\n", me->name); + cleanup_accumulated_relocations(&relocation_hashtable, + &used_buckets_list); return -EINVAL; } =20 @@ -882,8 +920,12 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *= strtab, &used_buckets_list); else res =3D handler(me, location, v); - if (res) + + if (res) { + cleanup_accumulated_relocations(&relocation_hashtable, + &used_buckets_list); return res; + } } =20 process_accumulated_relocations(me, &relocation_hashtable, --=20 2.51.2