From nobody Fri Nov 29 04:46:32 2024 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (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 B04901ABECD for ; Tue, 24 Sep 2024 21:20:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727212844; cv=none; b=QjfQdr1NvxnDvIoVlLZPxxilrzXeWUxy6j548Ahxcn4O9tR4CngJCikwynpnMAt7VHyD+UfZrfKnOLiyfjeTDSYKxFZnGus6hpwCxoc9iTALt/T6mD9qJHlrOUY711drGkPOGbZRVEviyf2bzh1OKGFimzqH3jASDp1M0R4kYvs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727212844; c=relaxed/simple; bh=oOvwo7YW3AUcfbqrf+A9OVLdTk2yYzaOPGripVgZWf8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=FxitM/V2oIl1XDKNZNamnbnkEIYqHmTv7JQAAnuzcaY/y8vx9MxpbOQqz049dqenhPgUipM1x+B+apOIT17C5+2muTrDAMKPCO+eNVcYNHwZhoziOIxSJjipPCANKCOL3fYn5Pt3XyoTmlX8uPsQMaoLvF9dyUwwdZyv4cOHLFI= 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=cK4FBduY; arc=none smtp.client-ip=209.85.128.202 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="cK4FBduY" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6d3e062dbeeso4732637b3.0 for ; Tue, 24 Sep 2024 14:20:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727212841; x=1727817641; darn=vger.kernel.org; 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=cK4FBduY2q/JNMQcZ0tjAjWnPou6Ctck43NZhfUZWJTbx9wqsUmIsIdbRLdkmRoRiD agqdIQ71gt8tVxIiNQzSoPH8FfcuBuUm1cUDzixybN9o3+o+vr2hKvc/Hzu5CYI7CdRB uvmoPmD49i+3WKtdFIx8MbOUTtBDLm22QF01olb3izY156A4bvQD5LWJN7sNZQrKdgAz h6yAdXAHcGutO/W7TkqpeikI1umcLoZMIjOIauKn8KH2hpreL22wPK+AK/gm1xM2L8Cg xZGDMgRi2x9xMIsxB/vcfzBivUASkOGZdc2ARYOEjyq9epZbD7eaH8mqc+7/sdW1xLrg BB2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727212841; x=1727817641; 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=jJ+R1Vbc4j1U/S/WfyzK7nTC2tOq6SXunC/GjCLgI7MOuQJsGbwPW1D+DBiRYBpI1S 9/Ba1dwfwjjXenGxpMRduB/DXZU3y5pGDnIo7AuprGtTaXIcJZp/rQKRpUKYeyKkGrSP qkVk3iWz9NrhgTWV0g4Kw7F1jOPt1jZIv1EO98t01qast3z2HMx6dPNrSym4uOCyX2kK qa6OHPzu3mocAiisCEStQ/FAygCyccG80cCmo3XK+HFyln6bBRE04J2sv8N8hY+l6UxT hrFvbj0WkaKLGa4zG0NyraQZO2fCshgiwi0Q00E6aMR8XN+TXmizxapsnKMx1oT8jUUP xKlQ== X-Forwarded-Encrypted: i=1; AJvYcCUWf38i1N2+QzRieSzUjzUECqv3bLcyjXze8r+NlN7KbGVYv3anHOyYl16GEGirwfs0tqEm8NZljJ1kPIQ=@vger.kernel.org X-Gm-Message-State: AOJu0YxfIEr8VY2pTbiEHG0hwguh1c3SKbDSb0rtdgrtaJo4ho5GhMZH 3PEgpOF5+2R/oLIaqJoeSdRf235LJMRFh19CzLodNZ24rs6O89shvOoS9/Oo4yJXeA+N2i0envF Ywtt6Vw== X-Google-Smtp-Source: AGHT+IEIHoUgWRTaVQTY+4+TEtbudWqV2N5uaDTmBs0ILMQjLIcShuzuxUnbFETJLtyrN6FtMPs3h3vqln4e X-Received: from anyblade.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1791]) (user=mmaurer job=sendgmr) by 2002:a05:690c:3808:b0:6db:d365:19c0 with SMTP id 00721157ae682-6e2089f11c2mr1334437b3.5.1727212841567; Tue, 24 Sep 2024 14:20:41 -0700 (PDT) Date: Tue, 24 Sep 2024 21:19:47 +0000 In-Reply-To: <20240924212024.540574-1-mmaurer@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240924212024.540574-1-mmaurer@google.com> X-Mailer: git-send-email 2.46.1.824.gd892dcdcdd-goog Message-ID: <20240924212024.540574-6-mmaurer@google.com> Subject: [PATCH v4 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, 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 , linux-modules@vger.kernel.org Content-Transfer-Encoding: quoted-printable 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 *i= nfo, const char *name) return 0; } =20 +/** + * 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 *n= ame) +{ + unsigned int idx; + unsigned int count =3D 0; + int i; + + for (i =3D 1; i < info->hdr->e_shnum; i++) { + if (strcmp(info->secstrings + info->sechdrs[i].sh_name, + name) =3D=3D 0) { + count++; + idx =3D i; + } + } + if (count =3D=3D 1) { + return idx; + } else if (count =3D=3D 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; } =20 +/** + * 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 populat= ed + * + * 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 =3D find_any_unique_sec(info, ".modinfo"); + + if (info_idx =3D=3D 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 =3D info_idx; + /* Try to find a name early so we can log errors with a module name */ + info->name =3D 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 =3D 0, mod_idx; - unsigned int num_info_secs =3D 0, info_idx; unsigned int num_sym_secs =3D 0, sym_idx; =20 err =3D elf_validity_cache_sechdrs(info); if (err < 0) return err; err =3D elf_validity_cache_secstrings(info); + if (err < 0) + return err; + err =3D elf_validity_cache_index_info(info); if (err < 0) return err; =20 @@ -1912,24 +1979,11 @@ static int elf_validity_cache_copy(struct load_info= *info, int flags) ".gnu.linkonce.this_module") =3D=3D 0) { num_mod_secs++; mod_idx =3D i; - } else if (strcmp(info->secstrings + shdr->sh_name, - ".modinfo") =3D=3D 0) { - num_info_secs++; - info_idx =3D i; } break; } } =20 - if (num_info_secs > 1) { - pr_err("Only one .modinfo section must exist.\n"); - goto no_exec; - } else if (num_info_secs =3D=3D 1) { - /* Try to find a name early so we can log errors with a module name */ - info->index.info =3D info_idx; - info->name =3D get_modinfo(info, "name"); - } - if (num_sym_secs !=3D 1) { pr_warn("%s: module has no symbols (stripped?)\n", info->name ?: "(missing .modinfo section or name field)"); --=20 2.46.1.824.gd892dcdcdd-goog