From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BE59D1A76A0 for ; Wed, 25 Sep 2024 23:39:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307575; cv=none; b=cVvrjvAg7a9wDMBp1+/qvIjyQHRThvxnyosU82TsxesBqDOoDJEZ0BzpiSvcgxk9YEEubDYdbc+rIaHX5PPBj5y6BCJmfGt0PFoS4K5OkR8tJmcCuUgj9x0HeIhVGmNAZU0axWg3sYSeS/OxgCIDC5+sN6GuXXleQPXlHRJWhwU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307575; c=relaxed/simple; bh=oOvwo7YW3AUcfbqrf+A9OVLdTk2yYzaOPGripVgZWf8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=V7TcAjdQhc2XhH/xRhbGzqhB7bC2jI+J/1qXLPYGjdbYB/l9gXKC3nbOJ5hIBW8i5G2iNesgnnCps+uRXyKSz1C+8lGBhkeeeIZed3q7KPmwN+rRFZ4x1zQsrjoTEXuyjdIzKFXHNpsZJgACR9EL0pwK/WSLbC1TsHgsOW+qj2Y= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ergjOfhz; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ergjOfhz" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e0b3d35ccfbso598177276.3 for ; Wed, 25 Sep 2024 16:39:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727307573; x=1727912373; darn=lists.linux.dev; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=XKJyYEevVucNlB3/71Ku7Qx+VTTwNT+eh7LmoeHsrUQ=; b=ergjOfhz686spqKhSIcARabwHVD0zVHZucGPoknx9TnwYq6WJUWl5uca4KIQbPqmxA iHcX2nIy/ujRbYofnpAUxOPMXjM68cth3aoSWTZvgs9vMOPug6uJx5pdjopWEAjO5/xJ f154Qbxu4Bn0JBYEHql5RV8bsZXJ7RzObKWYA+McJObNJ9ddh07H5tY+oHr59kJZC1kN pxoEJHg0m+OYHxV4RREWlV4TaDZiA05CwRWnVafTrk/r5wkfjdN4EAw61szWlRGpP8fG B+AqvxMBkCH4lNdvFPmjvm1XB45XoFchnk3u3XCRmukrbKcq/uf9Px5LEf8DcAx9Fq1s iRkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727307573; x=1727912373; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=XKJyYEevVucNlB3/71Ku7Qx+VTTwNT+eh7LmoeHsrUQ=; b=DSFebJHKcWPZ3Ls+UOJXhEWfauiPJlcMIY0Lu9Od0MNLNKsjM0MJpcrBCbPF4iVMuU aTpEUHT9Idvn+1SS9O2umvhXHkYb9vFQa5Ml/BcVQiyvc4+E9KWiScG1+2+W4Tr2MZKy laMDs4LKGtBPFwNpFerGNm0rXyE0x+x3g6p2Dsyipd9ebzzMFSIXmLCC+WP8odnPUtOn ZJPcgOgBE9WeOJ7z2w8zPsjrSbcUb7FjgXE5yTmtP1dWtO9pcS2rLfE8DBRema2InqmS ECQ/QVBb9C6sw2qUwhEOQWemfUxAxilKbER35AfwVQcNakI2u0WPXewPjzSa1iidktj4 DuWA== X-Forwarded-Encrypted: i=1; AJvYcCXYDcUwSaP/sM6Qsjt0B0pCTSsB41srOYVlhzor4naWVVQT8x724gwDVDJUULdL/4luc4xi3A==@lists.linux.dev X-Gm-Message-State: AOJu0YxleBpwlyFWPZXr7KfdbubFWib/uleu01BGewJ6k9pRMXfHKnDx Cbb/WE/6QyTtkDoMqb6/+Y2C03fN1pQf8RAhYAXDdHboEip/g1NFaR7Cx9gwoMtt3IOBblyfHKu eA09++A== X-Google-Smtp-Source: AGHT+IGx+nve+AoQTJhC0039Mrm5L1Mmot8nzzFqz6EcNfCPXpgrANZ2tGjsmltzJQkDur51XUIFhuHCnZ0h X-Received: from anyblade.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1791]) (user=mmaurer job=sendgmr) by 2002:a25:780e:0:b0:e16:6e0a:bb0b with SMTP id 3f1490d57ef6-e24d44cad51mr4787276.0.1727307572609; Wed, 25 Sep 2024 16:39:32 -0700 (PDT) Date: Wed, 25 Sep 2024 23:38:20 +0000 In-Reply-To: <20240925233854.90072-1-mmaurer@google.com> Precedence: bulk X-Mailing-List: asahi@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925233854.90072-1-mmaurer@google.com> X-Mailer: git-send-email 2.46.1.824.gd892dcdcdd-goog Message-ID: <20240925233854.90072-6-mmaurer@google.com> Subject: [PATCH v5 05/16] module: Factor out elf_validity_cache_index_info From: Matthew Maurer To: masahiroy@kernel.org, ndesaulniers@google.com, ojeda@kernel.org, gary@garyguo.net, mcgrof@kernel.org, Alex Gaynor Cc: rust-for-linux@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, neal@gompa.dev, marcan@marcan.st, j@jannau.net, asahi@lists.linux.dev, linux-modules@vger.kernel.org, Matthew Maurer , Petr Pavlu , Sami Tolvanen , Daniel Gomez , Boqun Feng , "=?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?=" , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Content-Type: text/plain; charset="UTF-8" Centralize .modinfo detection and property validation. Signed-off-by: Matthew Maurer --- 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 6a9159afca02..511d645ac577 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) { @@ -1854,6 +1886,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. @@ -1880,13 +1945,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; @@ -1912,24 +1979,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.46.1.824.gd892dcdcdd-goog