From nobody Fri Nov 29 03:49:19 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 B78BE1AB6E4 for ; Wed, 25 Sep 2024 23:39:36 +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=1727307578; cv=none; b=MBmAb3vIyn4+Tbt1n+afcjmwEsJuCP7ZF05rlKfFLlf+q84GZ3KhzNe8cLAy/d+9Vs5dulvcT1Vctk3L8RLsCjm3lrt/D1KpwU8PhUfUU8fUpXRigAJetxAAVVwDxZWhfH+QeaMfvnFqpDRD+uU99MjurwkUKfWqwwm5Fnk99W4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307578; c=relaxed/simple; bh=h/LAOljVw5E3JGGOaE89sERZTa3pvx6H/SQhACbdpCo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=PXqvmW0iNmR3G+9uZR1mYr3z/kk8pLJ5FFarAJLEXBRvg3L0uG4+HBiUNQeCqSnoN6rBhiVuzJ8axnIaL8hxqGWF2b+8HrA65mOmJKiV6hfNhHWCHjAZ7Bs4QbJKFiyTZVVuAkWHXCkC/hZ8kX8MKfbMK605V04BC3hjxL/eryI= 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=bLzMJc1z; 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="bLzMJc1z" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6e20e4e55bbso9636707b3.0 for ; Wed, 25 Sep 2024 16:39:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727307575; x=1727912375; 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=Q8UL0Q7oSLmAAG+S1LbknTXXfZfG+WAJTpNf/8OfOpQ=; b=bLzMJc1z9tr9FfcPYNuF0FgDcztptHJ1LRBD+erLfNiV2PY2u4HZxOfIeAXGY0EQ/c tdFb1ycVm5t8818XUd5c1lXovrHrUKltTsdKPovlYOU0pKm5sXPPmGwes3ACT/xHfQ+X Y1hFGlLB85haavFgwDwoOCeeFk1BnkyCzcADPdiKdW1ohmGRboABBKPAswFrJQdKpcUk T8f+Lwd+wIFFomvH1yJE8BrP2Nj8DY3B1fJg+8DJP0eYqG6/blOLutMbMv1pM7kDRYqE D/E3li1pycxfJw4Bo/JGXG04spUOrIDbca1MRR8jDLmyEpFtm5eTTsRWRSJfsJZTiS3J dCmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727307575; x=1727912375; 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=Q8UL0Q7oSLmAAG+S1LbknTXXfZfG+WAJTpNf/8OfOpQ=; b=knb46CkfnaZlaVpqZjGw1/Pay6/MPyxV//Msn1r2dXWuUp5GyEgksqh6d7HUUiuVjz pA0lHZ1iP0wsY/Y+n6VBd1d08L0gkQoN6nbS2DaJdEYfsRiQH0JWCdX6MbH7Rk6IHG1I bajPY+F6jNQS4yV/s81z0Ai38pQzyPJKSRtFDCdg0bpLsA36ine5CPSi5iVdGEljis4N 1ewgwzcURKyPHTDh31I01u7Xp+4bcKEq9/TEeMgoQxkKUxvog8GSB6BcLqM5m9MERRjm GkWZEbTDM6qL4iuAxj4Pu7ICCyEIPjZyjD/B1XxQIxpxAJnBqlCz4UEQBH534A6b3ldA KeoA== X-Forwarded-Encrypted: i=1; AJvYcCWVKxfAn833f8A1exHqvbtnxzhdXe9PRWUNLWlbDUwS8P4GX4uNnxZLHR78crHM8ivJo6itCEdT6wwwQ44=@vger.kernel.org X-Gm-Message-State: AOJu0YwoQjdJJLm7D03HCIwzdzRbS52LGTmbtjCLVJr11GwJdYsW0KcD fEi5kYrY+bAIKkvJFO1jF/O/HL6wkqV/ybNZC7Osn9tHLYAI8sy3mQILIbEdHfmc/hrZ5PIQwnc tU+giqw== X-Google-Smtp-Source: AGHT+IG616iIEoJXepMEnFIXh2EvSXgtvB4OvHJFrlY3ghUherNjNwvIMu6DpxK7ywhW+VD5HsliaqgLk/2C X-Received: from anyblade.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1791]) (user=mmaurer job=sendgmr) by 2002:a5b:d43:0:b0:e02:c06f:1db8 with SMTP id 3f1490d57ef6-e24d7b07255mr3692276.4.1727307575061; Wed, 25 Sep 2024 16:39:35 -0700 (PDT) Date: Wed, 25 Sep 2024 23:38:21 +0000 In-Reply-To: <20240925233854.90072-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: <20240925233854.90072-1-mmaurer@google.com> X-Mailer: git-send-email 2.46.1.824.gd892dcdcdd-goog Message-ID: <20240925233854.90072-7-mmaurer@google.com> Subject: [PATCH v5 06/16] module: Factor out elf_validity_cache_index_mod 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Centralize .gnu.linkonce.this_module detection and property validation. Signed-off-by: Matthew Maurer --- kernel/module/main.c | 129 ++++++++++++++++++++++--------------------- 1 file changed, 67 insertions(+), 62 deletions(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index 511d645ac577..ec638187ffcf 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1919,6 +1919,68 @@ static int elf_validity_cache_index_info(struct load= _info *info) return 0; } =20 +/** + * elf_validity_cache_index_mod() - Validates and caches this_module secti= on + * @info: Load info to cache this_module on. + * Must have &load_info->sechdrs and &load_info->secstrings populat= ed + * + * The ".gnu.linkonce.this_module" ELF section is special. It is what modp= ost + * uses to refer to __this_module and let's use rely on THIS_MODULE to poi= nt + * to &__this_module properly. The kernel's modpost declares it on each + * modules's *.mod.c file. If the struct module of the kernel changes a fu= ll + * kernel rebuild is required. + * + * We have a few expectations for this special section, this function + * validates all this for us: + * + * * The section has contents + * * The section is unique + * * We expect the kernel to always have to allocate it: SHF_ALLOC + * * The section size must match the kernel's run time's struct module + * size + * + * If all checks pass, the index will be cached in &load_info->index.mod + * + * Return: %0 on validation success, %-ENOEXEC on failure + */ +static int elf_validity_cache_index_mod(struct load_info *info) +{ + Elf_Shdr *shdr; + int mod_idx; + + mod_idx =3D find_any_unique_sec(info, ".gnu.linkonce.this_module"); + if (mod_idx <=3D 0) { + pr_err("module %s: Exactly one .gnu.linkonce.this_module section must ex= ist.\n", + info->name ?: "(missing .modinfo section or name field)"); + return -ENOEXEC; + } + + shdr =3D &info->sechdrs[mod_idx]; + + if (shdr->sh_type =3D=3D SHT_NOBITS) { + pr_err("module %s: .gnu.linkonce.this_module section must have a size se= t\n", + info->name ?: "(missing .modinfo section or name field)"); + return -ENOEXEC; + } + + if (!(shdr->sh_flags & SHF_ALLOC)) { + pr_err("module %s: .gnu.linkonce.this_module must occupy memory during p= rocess execution\n", + info->name ?: "(missing .modinfo section or name field)"); + return -ENOEXEC; + } + + if (shdr->sh_size !=3D sizeof(struct module)) { + pr_err("module %s: .gnu.linkonce.this_module section size must match the= kernel's built struct module size at run time\n", + info->name ?: "(missing .modinfo section or name field)"); + return -ENOEXEC; + } + + info->index.mod =3D mod_idx; + + return 0; +} + + /* * Check userspace passed ELF module against our expectations, and cache * useful variables for further processing as we go. @@ -1944,7 +2006,6 @@ static int elf_validity_cache_copy(struct load_info *= info, int flags) unsigned int i; Elf_Shdr *shdr; int err; - unsigned int num_mod_secs =3D 0, mod_idx; unsigned int num_sym_secs =3D 0, sym_idx; =20 err =3D elf_validity_cache_sechdrs(info); @@ -1954,16 +2015,15 @@ static int elf_validity_cache_copy(struct load_info= *info, int flags) if (err < 0) return err; err =3D elf_validity_cache_index_info(info); + if (err < 0) + return err; + err =3D elf_validity_cache_index_mod(info); if (err < 0) return err; =20 for (i =3D 1; i < info->hdr->e_shnum; i++) { shdr =3D &info->sechdrs[i]; - switch (shdr->sh_type) { - case SHT_NULL: - case SHT_NOBITS: - continue; - case SHT_SYMTAB: + if (shdr->sh_type =3D=3D SHT_SYMTAB) { if (shdr->sh_link =3D=3D SHN_UNDEF || shdr->sh_link >=3D info->hdr->e_shnum) { pr_err("Invalid ELF sh_link!=3DSHN_UNDEF(%d) or (sh_link(%d) >=3D hdr-= >e_shnum(%d)\n", @@ -1973,14 +2033,6 @@ static int elf_validity_cache_copy(struct load_info = *info, int flags) } num_sym_secs++; sym_idx =3D i; - fallthrough; - default: - if (strcmp(info->secstrings + shdr->sh_name, - ".gnu.linkonce.this_module") =3D=3D 0) { - num_mod_secs++; - mod_idx =3D i; - } - break; } } =20 @@ -1996,55 +2048,8 @@ static int elf_validity_cache_copy(struct load_info = *info, int flags) info->index.str =3D shdr->sh_link; info->strtab =3D (char *)info->hdr + info->sechdrs[info->index.str].sh_of= fset; =20 - /* - * The ".gnu.linkonce.this_module" ELF section is special. It is - * what modpost uses to refer to __this_module and let's use rely - * on THIS_MODULE to point to &__this_module properly. The kernel's - * modpost declares it on each modules's *.mod.c file. If the struct - * module of the kernel changes a full kernel rebuild is required. - * - * We have a few expectaions for this special section, the following - * code validates all this for us: - * - * o Only one section must exist - * o We expect the kernel to always have to allocate it: SHF_ALLOC - * o The section size must match the kernel's run time's struct module - * size - */ - if (num_mod_secs !=3D 1) { - pr_err("module %s: Only one .gnu.linkonce.this_module section must exist= .\n", - info->name ?: "(missing .modinfo section or name field)"); - goto no_exec; - } - - shdr =3D &info->sechdrs[mod_idx]; - - /* - * This is already implied on the switch above, however let's be - * pedantic about it. - */ - if (shdr->sh_type =3D=3D SHT_NOBITS) { - pr_err("module %s: .gnu.linkonce.this_module section must have a size se= t\n", - info->name ?: "(missing .modinfo section or name field)"); - goto no_exec; - } - - if (!(shdr->sh_flags & SHF_ALLOC)) { - pr_err("module %s: .gnu.linkonce.this_module must occupy memory during p= rocess execution\n", - info->name ?: "(missing .modinfo section or name field)"); - goto no_exec; - } - - if (shdr->sh_size !=3D sizeof(struct module)) { - pr_err("module %s: .gnu.linkonce.this_module section size must match the= kernel's built struct module size at run time\n", - info->name ?: "(missing .modinfo section or name field)"); - goto no_exec; - } - - info->index.mod =3D mod_idx; - /* This is temporary: point mod into copy of data. */ - info->mod =3D (void *)info->hdr + shdr->sh_offset; + info->mod =3D (void *)info->hdr + info->sechdrs[info->index.mod].sh_offse= t; =20 /* * If we didn't load the .modinfo 'name' field earlier, fall back to --=20 2.46.1.824.gd892dcdcdd-goog