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=1794061163; b=sw5Tibda3mswYCLa0LEnDm8iJLCnqt+UNik15glEx+QawS5muEd5in8aoqvMXqWF4Dbv grhZMa8Gd1idJwZ6OROMXZgCvaz6w5zypEZJHDBIocY5HUdXza7pP9k5bxa5KCNUa0ToA gL24/fLwOmfEVv2mZq6IOdoVnqw8DFE4yMN4MJF4se3dQldkcWAQPyB/AdlcjERKBvShJ YUBoT8K+Ywh5CnQKszmbENCNTR8lQO/ysiASkN4+aDn63rOB5bA2MycV3P09ztbsjiHab xXIfmxTeGZFZM0aEAlL2CZFr+Lz/bFgK+r9Bl5v6t9N0VfbaCbmLBOV435ejzAwDD6Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794061163; 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=okK54DtuOiaoi4Ak/OH5gZhbIs8YRHVUvCz/ZGN+NAc=; b=fxiSpqJYOx5XLcqrMJJ9MSbg4n35j+zRQFu/9kTt81STWIVmQ87BkK7lEWzkO2JiJYIP Mukzt4af9WiLQb2rJVz7lBGALiiB54vNuePiDbJA2G0T8P0SzqkxK2jj/nlh3SJ67lUM6 VsaIeC2mhqyuqXNJvsiWHKkA8VGtb3mY5iEIyjUn4aDh6v4+Ae1Jhm4EQ5K7Rz1+kzL4P m2Z5isQVnBzMo2LHY/mCwKqDtsxwaAButxKxj2+xyPByxJrPkQtpn0v2q8YBMv7es12aA 7YwaK7OtbTx4bsXTkQ0cS7gooKXWyvXBY/4ui5cOuBW3ZCeNldxJc3pcz7aoau1NC7g== 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=mo11pnhu; 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 4C432606D805; Sat, 7 Nov 2026 14:19:23 +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=mo11pnhu Received: by smtp.kernel.org (Postfix) with ESMTPSA id EA5C31F0089F; Sat, 7 Nov 2026 14:19:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1794061163; bh=okK54DtuOiaoi4Ak/OH5gZhbIs8YRHVUvCz/ZGN+NAc=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=mo11pnhuFHOnrwxxa6hB/JPZ4RQtS/GHc3BAi35vgmnbP5mmW01f9toVUA4I6IhBd J4DdUGFWC20mDSVx8X3jBTmxQvbltcZ2i16TUg6bhuxO7tLthLM7djSbLipAKa/XA8 Fa0ZXAgmS9F6uuwSZ/+LDJ+IQ92Y+X8mTS1sZ/1c= 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 0469/1536] module: Factor out elf_validity_cache_index_info Date: Sat, 7 Nov 2026 08:41:25 -0500 Message-ID: <20261107135010.687730681@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 fbc0e4e482aac7f2b0d20f0a11f6d5eeda346fda ] Centralize .modinfo 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 | 82 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 68 insertions(+), 14 deletions(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index 6314938422bc5..87ce807015b30 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -195,6 +195,38 @@ static unsigned int find_sec(const struct load_info *info, const char *name) return 0; } +/** + * find_any_unique_sec() - Find a unique section index by name + * @info: Load info for the module to scan + * @name: Name of the section we're looking for + * + * Locates a unique section by name. Ignores SHF_ALLOC. + * + * Return: Section index if found uniquely, zero if absent, negative count + * of total instances if multiple were found. + */ +static int find_any_unique_sec(const struct load_info *info, const char *name) +{ + unsigned int idx; + unsigned int count = 0; + int i; + + for (i = 1; i < info->hdr->e_shnum; i++) { + if (strcmp(info->secstrings + info->sechdrs[i].sh_name, + name) == 0) { + count++; + idx = i; + } + } + if (count == 1) { + return idx; + } else if (count == 0) { + return 0; + } else { + return -count; + } +} + /* Find a module section, or NULL. */ static void *section_addr(const struct load_info *info, const char *name) { @@ -1863,6 +1895,39 @@ static int elf_validity_cache_secstrings(struct load_info *info) return 0; } +/** + * elf_validity_cache_index_info() - Validate and cache modinfo section + * @info: Load info to populate the modinfo index on. + * Must have &load_info->sechdrs and &load_info->secstrings populated + * + * Checks that if there is a .modinfo section, it is unique. + * Then, it caches its index in &load_info->index.info. + * Finally, it tries to populate the name to improve error messages. + * + * Return: %0 if valid, %-ENOEXEC if multiple modinfo sections were found. + */ +static int elf_validity_cache_index_info(struct load_info *info) +{ + int info_idx; + + info_idx = find_any_unique_sec(info, ".modinfo"); + + if (info_idx == 0) + /* Early return, no .modinfo */ + return 0; + + if (info_idx < 0) { + pr_err("Only one .modinfo section must exist.\n"); + return -ENOEXEC; + } + + info->index.info = info_idx; + /* Try to find a name early so we can log errors with a module name */ + info->name = get_modinfo(info, "name"); + + return 0; +} + /* * Check userspace passed ELF module against our expectations, and cache * useful variables for further processing as we go. @@ -1889,13 +1954,15 @@ static int elf_validity_cache_copy(struct load_info *info, int flags) Elf_Shdr *shdr; int err; unsigned int num_mod_secs = 0, mod_idx; - unsigned int num_info_secs = 0, info_idx; unsigned int num_sym_secs = 0, sym_idx; err = elf_validity_cache_sechdrs(info); if (err < 0) return err; err = elf_validity_cache_secstrings(info); + if (err < 0) + return err; + err = elf_validity_cache_index_info(info); if (err < 0) return err; @@ -1921,24 +1988,11 @@ static int elf_validity_cache_copy(struct load_info *info, int flags) ".gnu.linkonce.this_module") == 0) { num_mod_secs++; mod_idx = i; - } else if (strcmp(info->secstrings + shdr->sh_name, - ".modinfo") == 0) { - num_info_secs++; - info_idx = i; } break; } } - if (num_info_secs > 1) { - pr_err("Only one .modinfo section must exist.\n"); - goto no_exec; - } else if (num_info_secs == 1) { - /* Try to find a name early so we can log errors with a module name */ - info->index.info = info_idx; - info->name = get_modinfo(info, "name"); - } - if (num_sym_secs != 1) { pr_warn("%s: module has no symbols (stripped?)\n", info->name ?: "(missing .modinfo section or name field)"); -- 2.53.0