From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794061166; b=QbWvurToHNYQr6YPvkXzqLokkSsOWwUt3zI669rU+Bw0uqZKwJy/Hn2GKw4DKMmLCJnq XFawqMiE3ndrkMptIdIhoRZjANV7g4co0iOE5Jr6us+gHXwNRpGwYcelMVpXXcm0b6mcL seAL5MyJ3gKmRpw1JBltbH044HX2HJkM4IKmBl4gN1iS714k4QY7Teh3KvWLUN7IZVOJl RJnhWlVAk5+tpYjxy7CQckI+iyWihEmwh5OEjC0oOaMEWgmfEKHGl1BUDszjkslhUYQRF mx7rOtTyZDzQUiWfADUIB/J0uYbU/u0eTmqIRAlbJpx21TxWeAdrCxQbzvDU/HF7urA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794061166; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=L98YkstN6ouvI+FluRhyJQkJ0DC0GBJ9nD7TWGwAIhc=; b=onSm2Xg7O4EA466VAiU/GWXBYNPAZNqAvHQQXHK9cHrEo1l+tV/pVsxHR2Bd7aP4/fPs ZcDikBa5bp/gyvk3Bkj0iX8aU2noAgfGUYUaO/4rzQ9GA+fn6PZYnAXW3rDHAemPn5AMt KqcNW6mrlctDm1vU48Ir3ro/3ZDPA3wOM942GH+Wp3BZiGZdFAn+zM5aTbBCrforezHnV RUXBJ9MNOIac5tINdCvp6SKEcCrexdLbZ35Z3N3V4d41To5kR8pHlMUb0FH8AU+IsYuQW +tkx7mcVLsuYEepc+Hq/oVNkhq3uMqGILa7ywsQJzqTYpjou8P7npNdhz/z/hqMmPfg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=dWCtK8Dx; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B7D66606A33A; Sat, 7 Nov 2026 14:19:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=dWCtK8Dx Received: by smtp.kernel.org (Postfix) with ESMTPSA id 66DB01F0089F; Sat, 7 Nov 2026 14:19:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1794061165; bh=L98YkstN6ouvI+FluRhyJQkJ0DC0GBJ9nD7TWGwAIhc=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=dWCtK8DxN1dpHDB8tOrYJBCLlvivZLAwpypULyAhiSnKvQzUrePQEjNw+1I/DaHiW rzkznHG+DY4dyAxsg0ttUkOp/AoDrP9lZx+lfMwjyVLV87DCYsdkz8eEaJ8Zr+XuLS bL0vcFasyaERl4UkkxHLJj3+VOguibVEuB/Cxb90= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Matthew Maurer , Sami Tolvanen , Luis Chamberlain , Sasha Levin Subject: [PATCH 6.12 0471/1536] module: Factor out elf_validity_cache_index_sym Date: Sat, 7 Nov 2026 08:41:27 -0500 Message-ID: <20261107135010.739580298@linuxfoundation.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261107134957.653319350@linuxfoundation.org> References: <20261107134957.653319350@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.12-stable review patch. If anyone has any objections, please let me know. ------------------ From: Matthew Maurer [ Upstream commit 9bd4982cf7d65f4c9e0793d5a8fda6ad838e8554 ] Centralize symbol table detection and property validation. Signed-off-by: Matthew Maurer Reviewed-by: Sami Tolvanen Signed-off-by: Luis Chamberlain Stable-dep-of: 9a5ff4568932 ("module: validate string table section types") Signed-off-by: Sasha Levin --- kernel/module/main.c | 73 ++++++++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 29 deletions(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index d77bb2cf7a0e6..0861682a36179 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1989,6 +1989,39 @@ static int elf_validity_cache_index_mod(struct load_info *info) return 0; } +/** + * elf_validity_cache_index_sym() - Validate and cache symtab index + * @info: Load info to cache symtab index in. + * Must have &load_info->sechdrs and &load_info->secstrings populated. + * + * Checks that there is exactly one symbol table, then caches its index in + * &load_info->index.sym. + * + * Return: %0 if valid, %-ENOEXEC on failure. + */ +static int elf_validity_cache_index_sym(struct load_info *info) +{ + unsigned int sym_idx; + unsigned int num_sym_secs = 0; + int i; + + for (i = 1; i < info->hdr->e_shnum; i++) { + if (info->sechdrs[i].sh_type == SHT_SYMTAB) { + num_sym_secs++; + sym_idx = i; + } + } + + if (num_sym_secs != 1) { + pr_warn("%s: module has no symbols (stripped?)\n", + info->name ?: "(missing .modinfo section or name field)"); + return -ENOEXEC; + } + + info->index.sym = sym_idx; + + return 0; +} /* * Check userspace passed ELF module against our expectations, and cache @@ -2012,10 +2045,8 @@ static int elf_validity_cache_index_mod(struct load_info *info) */ static int elf_validity_cache_copy(struct load_info *info, int flags) { - unsigned int i; - Elf_Shdr *shdr; int err; - unsigned int num_sym_secs = 0, sym_idx; + int str_idx; err = elf_validity_cache_sechdrs(info); if (err < 0) @@ -2027,34 +2058,21 @@ static int elf_validity_cache_copy(struct load_info *info, int flags) if (err < 0) return err; err = elf_validity_cache_index_mod(info); + if (err < 0) + return err; + err = elf_validity_cache_index_sym(info); if (err < 0) return err; - for (i = 1; i < info->hdr->e_shnum; i++) { - shdr = &info->sechdrs[i]; - if (shdr->sh_type == SHT_SYMTAB) { - if (shdr->sh_link == SHN_UNDEF - || shdr->sh_link >= info->hdr->e_shnum) { - pr_err("Invalid ELF sh_link!=SHN_UNDEF(%d) or (sh_link(%d) >= hdr->e_shnum(%d)\n", - shdr->sh_link, shdr->sh_link, - info->hdr->e_shnum); - goto no_exec; - } - num_sym_secs++; - sym_idx = i; - } - } - - if (num_sym_secs != 1) { - pr_warn("%s: module has no symbols (stripped?)\n", - info->name ?: "(missing .modinfo section or name field)"); - goto no_exec; + str_idx = info->sechdrs[info->index.sym].sh_link; + if (str_idx == SHN_UNDEF || str_idx >= info->hdr->e_shnum) { + pr_err("Invalid ELF sh_link!=SHN_UNDEF(%d) or (sh_link(%d) >= hdr->e_shnum(%d)\n", + str_idx, str_idx, info->hdr->e_shnum); + return -ENOEXEC; } - /* Sets internal symbols and strings. */ - info->index.sym = sym_idx; - shdr = &info->sechdrs[sym_idx]; - info->index.str = shdr->sh_link; + /* Sets internal strings. */ + info->index.str = str_idx; info->strtab = (char *)info->hdr + info->sechdrs[info->index.str].sh_offset; /* This is temporary: point mod into copy of data. */ @@ -2075,9 +2093,6 @@ static int elf_validity_cache_copy(struct load_info *info, int flags) info->index.pcpu = find_pcpusec(info); return 0; - -no_exec: - return -ENOEXEC; } #define COPY_CHUNK_SIZE (16*PAGE_SIZE) -- 2.53.0