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=1794061160; b=QYw6L74xIkbEyRRQk+K+voSH1kdBVpdfQNPL2pL2YMzPD5PSQBkdpSQz/d6st6J0NMcl CsvkihK4XymZlE4N3KIcXR02sFxLkaRI/OTLJneLJiV09mehuLO6psl4zafAMEH8SOYxl S7UFIwati8rG0Dt8kKAQ3YDx3Y8MFIJM68IwOH8nkU5GPIExCOqTe2WpyewvWxgN5Fkvd 2Z6l6lZdJ61zrHf+TyxydN5PD1xe0aGhMQtKRjEubgOXJV9jawGsN6OGUSJfqkVG4wJsx 6Rh/qhfposvyg6egVGiY5hkt7Yu89RG+R6+qDcj4YUT5gGWXLU87Wwp5F17nA1P0IFg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794061160; 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=9qG6avX9JWsH5N54g7WyEGTX+T7xiOWxyq0CLFT8AbQ=; b=S2QBfFeczNTxzvbh/4JnjjCXBgDAIRDQAHthgFPFf1J3GLSr3gUTycBsU7C+OFa62ry/ ntbAq7+ZA9aH5dLmsHz0MTFgMlAZc84/qVOzeuXEpWoOoN6xNrbnp8N/e+snOkWTc5NZx pBvAIMm3rUg44rHoG7QtCdtGW0ClH6TmMISGSVw8dJMbdjvNVFQ+4FjH8rUno+3my+jIw HM8s3oTHByYZnAu12Ia6xeUryhNR5cyEA9masXGgPReXhfvwKElrUQvq2hrcV2laItkUU tlBMxj/Ow7saehDD0sg7+aHK3nHNcqOXnatfe/0gV7GQ7UO/S2Y0Zt7/N9DIxH7XFRQ== 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=DB3w5iSH; 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 A42BC606D805; Sat, 7 Nov 2026 14:19:19 +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=DB3w5iSH Received: by smtp.kernel.org (Postfix) with ESMTPSA id 512D31F0089F; Sat, 7 Nov 2026 14:19:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1794061159; bh=9qG6avX9JWsH5N54g7WyEGTX+T7xiOWxyq0CLFT8AbQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=DB3w5iSHpkiRvg+4Su/dAawICRbGj6fIjJzLq/CWnKdBS5PRKzBf0ds/lG8zrIKkD pn/Cl6fVKGb7vsK3wzq0YmdJLB9pyz9s5TV9dM5H+I7Gb677+ndnPal8UGaTMlqMkB 2uqHuOS6EVoo/dGgfQrTLu3zXGxzVwf6Cqlf7dC4= 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 0466/1536] module: Factor out elf_validity_ehdr Date: Sat, 7 Nov 2026 08:41:22 -0500 Message-ID: <20261107135010.602342253@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 90f8f312db720dbabec7c6258ef580b50129cc21 ] Factor out verification of the ELF header and document what is checked. 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 | 70 +++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 23 deletions(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index ad58c44fb74fd..33fe360e928ef 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1673,6 +1673,50 @@ static int validate_section_offset(struct load_info *info, Elf_Shdr *shdr) return 0; } +/** + * elf_validity_ehdr() - Checks an ELF header for module validity + * @info: Load info containing the ELF header to check + * + * Checks whether an ELF header could belong to a valid module. Checks: + * + * * ELF header is within the data the user provided + * * ELF magic is present + * * It is relocatable (not final linked, not core file, etc.) + * * The header's machine type matches what the architecture expects. + * * Optional arch-specific hook for other properties + * - module_elf_check_arch() is currently only used by PPC to check + * ELF ABI version, but may be used by others in the future. + * + * Return: %0 if valid, %-ENOEXEC on failure. + */ +static int elf_validity_ehdr(const struct load_info *info) +{ + if (info->len < sizeof(*(info->hdr))) { + pr_err("Invalid ELF header len %lu\n", info->len); + return -ENOEXEC; + } + if (memcmp(info->hdr->e_ident, ELFMAG, SELFMAG) != 0) { + pr_err("Invalid ELF header magic: != %s\n", ELFMAG); + return -ENOEXEC; + } + if (info->hdr->e_type != ET_REL) { + pr_err("Invalid ELF header type: %u != %u\n", + info->hdr->e_type, ET_REL); + return -ENOEXEC; + } + if (!elf_check_arch(info->hdr)) { + pr_err("Invalid architecture in ELF header: %u\n", + info->hdr->e_machine); + return -ENOEXEC; + } + if (!module_elf_check_arch(info->hdr)) { + pr_err("Invalid module architecture in ELF header: %u\n", + info->hdr->e_machine); + return -ENOEXEC; + } + return 0; +} + /* * Check userspace passed ELF module against our expectations, and cache * useful variables for further processing as we go. @@ -1702,30 +1746,10 @@ static int elf_validity_cache_copy(struct load_info *info, int flags) unsigned int num_info_secs = 0, info_idx; unsigned int num_sym_secs = 0, sym_idx; - if (info->len < sizeof(*(info->hdr))) { - pr_err("Invalid ELF header len %lu\n", info->len); - goto no_exec; - } + err = elf_validity_ehdr(info); + if (err < 0) + return err; - if (memcmp(info->hdr->e_ident, ELFMAG, SELFMAG) != 0) { - pr_err("Invalid ELF header magic: != %s\n", ELFMAG); - goto no_exec; - } - if (info->hdr->e_type != ET_REL) { - pr_err("Invalid ELF header type: %u != %u\n", - info->hdr->e_type, ET_REL); - goto no_exec; - } - if (!elf_check_arch(info->hdr)) { - pr_err("Invalid architecture in ELF header: %u\n", - info->hdr->e_machine); - goto no_exec; - } - if (!module_elf_check_arch(info->hdr)) { - pr_err("Invalid module architecture in ELF header: %u\n", - info->hdr->e_machine); - goto no_exec; - } if (info->hdr->e_shentsize != sizeof(Elf_Shdr)) { pr_err("Invalid ELF section header size\n"); goto no_exec; -- 2.53.0