From patchwork Wed Sep 25 23:38:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Maurer X-Patchwork-Id: 13812594 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 AE8C91A3BCF for ; Wed, 25 Sep 2024 23:39:24 +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=1727307567; cv=none; b=HZ9jLv0V/m5ySjcPggjyT8LO013jLodK2uaq8mwvYrhqwaiBHbpoIblqA51FzlJB5hssqx5UYUYF3vMG8y6tQLxgqSFsHwNxvwtUyrIg4ZI5z7obIkak7b4QBcEpWNTbujAG3YYDcz/3QmhIuHTi8AESGdU2+de2EcaoTplHsOU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307567; c=relaxed/simple; bh=87UiLghCgImjI67qGqkZVlUAdkIatHILzWkWX7E7o4U=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=R9A+78LpF9qRfIDgdTKsAMOreOz+H7kGMWydggBlCBLEuKwqh5lXtcPLjYX7yacYi59WqbTD1uk/eu1NhGiyGTbKLrPRbwoE5ZrrkoSQZUq0wl+QgUJWOaO3pH5VMIyXd8p+dWrLmo985BQecXQRI9ni4J8XuLrlwXkjfAldLSg= 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=sSv2LwGg; 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="sSv2LwGg" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e17bb508bb9so763248276.2 for ; Wed, 25 Sep 2024 16:39:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727307563; x=1727912363; 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=LyxbaKZI7M7/7zXNo9r/7MnbWQPTGkdTyXhoBixsIfc=; b=sSv2LwGgnR8j+P3liOLGDS+Gd7WpWyGEpltfC2fOq9aHCnwHqKpAttF7d+ujur0gB8 uajSGw9bVI1blAghnlpJjKpoTU8usQLS1/X50XZcXRK2ld2nldpX0shMzOrzMTxdVLIJ o2OFB6nS8m8f30g49HHszNUg4NYHQ7YOfOnn0pR3DzgI2jh4Ex0z/zxC6aNfqKlvgEJB r/zziTXy3Y/X9G/QWZlE+NWZ6N6MwuehcCCRkvO/+09LtjzKlTZoKwgwXnRpuHJO48eN eg7WXZaSbwse0lfJZGVqJ0XJl+5015CPNqjSCTK27vwULOfZQfEkq5wL2ShUhcw7U2j6 u2MQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727307563; x=1727912363; 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=LyxbaKZI7M7/7zXNo9r/7MnbWQPTGkdTyXhoBixsIfc=; b=H0pV3E1UCsZS/GA2Szs9g3VybURbTYLX4x76tdU0NYxcyQV1Vhzhkcwst2jyJ6f7D6 du0S8dvcYBM036xDaip6wWXwo+bZXcrGkscR7y4UR5LkIb/ZMLq9sU9NiYUtw+eIKbCa O0gFKvZNvwViW69YapCPUujnPBFgJ/2LEocApcGhuvyEO7+FH6h8tZU39SIpMD5eEc9k bTNtqt7UtmJBUwozRmfR95txF4accExm62rg8Y+KXePfk4sjOmwZyoxYyakvEiW+BThY lRU8Ep7Yn+AOmqqPJGt7cJOBsRLBZkVuJ6adRWrp7a5GexCSorwT59Fonq7wrcjoA/TY 2z8A== X-Forwarded-Encrypted: i=1; AJvYcCXjeEBUUmYHliPN1Nh1exQejQ4Z3uOhmGlHf/l3LqhdN9w3wNip873qVCalhSrgB+JyabqrsbRqu5KldyE=@vger.kernel.org X-Gm-Message-State: AOJu0Yy4yL/pmS9MxOCFJDXe6vGTd+jKbpGdCBe5LqsKnKfSSVAnwwJ8 d9YmEEPRaPyFdE8pEBLS1O0jKux6M4ke8vkvA0hQs4Z1eAWMfaaKcHpR46VHtrIftKymlf+Auk1 BSlY5vg== X-Google-Smtp-Source: AGHT+IEL4w0/gR8iE4pApg14NOFOUxZBPfsQ63YvAHEvZzW8W2ulmi1Lrysl9VB/6vA6caGgF7tNgGtZN5yi X-Received: from anyblade.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1791]) (user=mmaurer job=sendgmr) by 2002:a05:6902:2e88:b0:e22:5f73:1701 with SMTP id 3f1490d57ef6-e24d44c8e18mr28465276.0.1727307563386; Wed, 25 Sep 2024 16:39:23 -0700 (PDT) Date: Wed, 25 Sep 2024 23:38:16 +0000 In-Reply-To: <20240925233854.90072-1-mmaurer@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@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-2-mmaurer@google.com> Subject: [PATCH v5 01/16] module: Take const arg in validate_section_offset 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_Roy_Baron?= " , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross `validate_section_offset` doesn't modify the info passed in. Make this clear by adjusting the type signature. Signed-off-by: Matthew Maurer --- kernel/module/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index 49b9bca9de12..1a2dd52147ba 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1645,7 +1645,7 @@ bool __weak module_exit_section(const char *name) return strstarts(name, ".exit"); } -static int validate_section_offset(struct load_info *info, Elf_Shdr *shdr) +static int validate_section_offset(const struct load_info *info, Elf_Shdr *shdr) { #if defined(CONFIG_64BIT) unsigned long long secend; From patchwork Wed Sep 25 23:38:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Maurer X-Patchwork-Id: 13812595 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.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 EEA791A42B3 for ; Wed, 25 Sep 2024 23:39:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307568; cv=none; b=XvQ8fdjk8/ZUDMk4vlumIXvZWHAspFsXwlhMQaLVmIBotOtebucuerIKkPXhQl5Ps06tKPTzz6n9ifKaDGn3NwJfahSSp79u/eCwl+9xkv/SzFIiYuTNAEGgsjbaFa5rw6RnADFTWChxUCyjMl4Wbg9BOVLhfaTQ3AwsJ4nacy0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307568; c=relaxed/simple; bh=Mm95RZ0gEd/5ILA/sw+Yul4T3HW7v3bJsG9qmFVzqME=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=N8t2wZ5PHBqWB8OgdqtOAULSFc46R5YvOLA4GAA2g3Tja+DbptFewp4arzowMxpGiJG4N6+t1GAMYtHUfT48oIVJmTUoPqZJlON4wkw8OsE2QA5JdqQ/fJZZe3sGFYTKGpHfnFMQqWGthnE7DW4E5itfqggwy89Q4BWzUuYb25g= 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=tXfD7CXj; arc=none smtp.client-ip=209.85.219.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="tXfD7CXj" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e02b5792baaso693610276.2 for ; Wed, 25 Sep 2024 16:39:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727307566; x=1727912366; 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=+9YZzoCvxvyDdM8B+em9dMKSDuRRuGPZhSev+lAnN9E=; b=tXfD7CXjz4wZB2/ddpedVBun7FwWYkTwT5jkNs4IJgfnwfXiy4LSVv4RX+OAYrv035 2TQoSEG8TmcTr8s9ZfHyXmLleupcr1QzoBnaf2A01lnHXRIRqv13B9fKWD+ikeogU+Yd 32b68d+IYGSxvqZWpCqR5I7z1urq5x1rRfok5oqB7EW+uWRgzNSIAHLCU+kC08OWluoQ VbdgHFtNj2Bx9fJ+0ji+bXWgXIjfnNT6cCrLP1QrpntizC1VuVjlla95u74qLIm3bqk4 lami4hLe5geRmL2CyPcg+1jcas+bA5m66ScVkkqaORCr69Akq0YPJlo30uy5SniGYJ3R BxLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727307566; x=1727912366; 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=+9YZzoCvxvyDdM8B+em9dMKSDuRRuGPZhSev+lAnN9E=; b=YQ0L4XV8E+KN4RvVqNJuJQGY/ewN+hLBQN2d8otr20xsTQoraerv3U76gIGm6holth 5GA8jU5hKSle9Vwq1e0FoUhXrRmdkxLJY+x6tfuOidFlzDB9l72gpFzJwWtCb+p5JU6s 0SglmI3sToAo4XwAjG2kOiLUUlWmYbFVVTooocngVy4cQURAipT8Rb2SWmoXJCqxQf/x CI9SLwXtzgKGjBYPibH+xy7SFIjzauZimDXEa8JO32pZwSLaHz2elrElcdZN4xdiGexq wibW3TDoSI3/Hs9JGXwYT6l5zACx9IoZEP75WGfVS2KXNvx/TPPtTKJamPSYUPJ2jpvg +KHw== X-Forwarded-Encrypted: i=1; AJvYcCVGxlVXamcNCVT5/olSgPUpX0rdRGzx2C0CluVr+rBY7RZp+5UJgXVVh9N34Hjitud+eAWamEoDFulrNRo=@vger.kernel.org X-Gm-Message-State: AOJu0YyNWqQ6Ifp5tZqphYkLJBhK49mElUc/uHTGEL8KwI6THsQ2VDUV /uHy/HGXX/n0a/TWb2CIjbF75Qsl1/h9xEQ0D7LqcPdoUmwAMvuoD7pbIehyMr4xe34JgDpVvlJ 35E+xLw== X-Google-Smtp-Source: AGHT+IGHHE+4KWf9auYKryB2lu9/fM/qXObyfiN1uXRNz038JkA/1/xLeMhKPiONI2CFf2+1SxQefRfFrD41 X-Received: from anyblade.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1791]) (user=mmaurer job=sendgmr) by 2002:a25:7801:0:b0:e0e:89e6:aad4 with SMTP id 3f1490d57ef6-e24d44ca00dmr51018276.0.1727307565822; Wed, 25 Sep 2024 16:39:25 -0700 (PDT) Date: Wed, 25 Sep 2024 23:38:17 +0000 In-Reply-To: <20240925233854.90072-1-mmaurer@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@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-3-mmaurer@google.com> Subject: [PATCH v5 02/16] module: Factor out elf_validity_ehdr 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_Roy_Baron?= " , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Factor out verification of the ELF header and document what is checked. Signed-off-by: Matthew Maurer --- 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 1a2dd52147ba..59c977acfb44 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1664,6 +1664,50 @@ static int validate_section_offset(const 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. @@ -1693,30 +1737,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; From patchwork Wed Sep 25 23:38:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Maurer X-Patchwork-Id: 13812596 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 181AE1A4E7B for ; Wed, 25 Sep 2024 23:39:28 +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=1727307570; cv=none; b=oQAXRlAKVQMYsLSFXc577lnfx70edwNzn0IjiDBMpHq3KOgwhrZ6e7GWo08nlfIDxL+WtaI69Z+Lx6fW0pKFZuB90QV4Uu9gYGga6DsSJdJv3KIh6v5idAQtjJ3VDxHWpc0woi8iFbIqRzeztu16k2kd6gG4Pm/HFpjgyGUUN50= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307570; c=relaxed/simple; bh=WRBs20XgxXhpGUNzmAuEPSHiJfsMkZEkGfwB5C4b2is=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=FWrg7TLmdxMDoYZX5TVjeF5NxJ/mCUOqDJfbbP9ixwy4t3/S2Ttv8BkyJpaiCdf9l4aeSE5gj2OjqcadXrGOOyx7I3x5twcJdYNELVry+taq11M+2wQ+Pso0F/l53YbPxi17UjIKIgOl1b2TebdWM51oafraShfmWbFxIK3cDQ0= 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=sOdzlmeY; 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="sOdzlmeY" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6e20993a03dso7454157b3.2 for ; Wed, 25 Sep 2024 16:39:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727307568; x=1727912368; 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=srrYfI2SZpfKgIsSq8ppkf2FykJu/mFy8hG5tlqb9h4=; b=sOdzlmeY6RY7bpo53PEnQ0YN0/YFAnlXxFf2ZyhVr/9gJ/T37wmWSna16Pm4dszRsH kCAibe8ECqUZozUmaxsJJ7+d2Dm5yeCNVIMbcVZ+mVHroq8zknRXG3i+PN0xMk0cBaAl KtHEwtBf/e0CK81PJDy1xFIO7XAHMNhTiQ2W/9CFH6eGGuFnl7J0a8aKwnum5Tbv3opT H9UqkbO+NemXGkTfLdo2ya2prR7OXRr+P0NBt/iqNlFXgahpRb5JTkG+yUnuAWhTJFTw LSeXIBkWlkFlvXCLqZPkHXV+roqA0hPB8PmXdgVn9JfShCTD4kB4LdafGD5WjtOjCYBu ZXSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727307568; x=1727912368; 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=srrYfI2SZpfKgIsSq8ppkf2FykJu/mFy8hG5tlqb9h4=; b=cR/7c2vkCRkZ+pDrPIP0VS55J1I1e00rgvC1os5asQHEwVAwff7DH8GSx1OnZNYeC0 ssM79qXWZg5Y8S/MBWfFgfo/2t0eSiFjBAbxsG5yONGv/i/1SM9BS+5d64frJ+w2eh7T ZyLcP0mgc0AC6tE1oIkG09jCXv+lgCL2sBcMomzVIOizD8iJJihKUFF1uRvsE5zSNkpa Rr7HS1ygDgL5b8A1WJUcT7wNtvMilTJqiFp88yoI/VWjL8hLO+Tt/a7h2xfgQ42QQgOS LQ8iFWOw1jwPztWrmn+8vdTmHsbOtDZEiuLmIDUPvs1++8AAmi3DwE+h1I15xAni/pV3 lbbQ== X-Forwarded-Encrypted: i=1; AJvYcCWj9vEX77wO5k/AEFXhYh7hmFOb5TTK/nUzyD6KHnJn+8s95tZf+yevjmbs43zMdtdrf/C49+KDeH3hwuk=@vger.kernel.org X-Gm-Message-State: AOJu0Yz5l9mgU+lGzYaxrohsFjJPL39iL729YDBZu48PJ7XvGx+I3K6/ I5c3FxGDscS7KTxAEgkaJlDJF2pAuVk9gQG2/HQh3SJyB/I3k3R2RU954Gq0c51EfvgqXOTNkMc ZeAklAw== X-Google-Smtp-Source: AGHT+IFULUhyTdpe6K1NKJ4iYoAXDGeSnRiSbmRLHEzsKyUqZwAanm3jLnuYHrgujVpMDbLQEh9f4w7q22Vi X-Received: from anyblade.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1791]) (user=mmaurer job=sendgmr) by 2002:a05:690c:c9c:b0:6b0:d571:3540 with SMTP id 00721157ae682-6e21da796e6mr397427b3.6.1727307567956; Wed, 25 Sep 2024 16:39:27 -0700 (PDT) Date: Wed, 25 Sep 2024 23:38:18 +0000 In-Reply-To: <20240925233854.90072-1-mmaurer@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@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-4-mmaurer@google.com> Subject: [PATCH v5 03/16] module: Factor out elf_validity_cache_sechdrs 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_Roy_Baron?= " , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Factor out and document the validation of section headers. Because we now validate all section offsets and lengths before accessing them, we can remove the ad-hoc checks. Signed-off-by: Matthew Maurer --- kernel/module/main.c | 125 ++++++++++++++++++++++++++++--------------- 1 file changed, 82 insertions(+), 43 deletions(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index 59c977acfb44..1f3a07ee59c6 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1708,6 +1708,87 @@ static int elf_validity_ehdr(const struct load_info *info) return 0; } +/** + * elf_validity_cache_sechdrs() - Cache section headers if valid + * @info: Load info to compute section headers from + * + * Checks: + * + * * ELF header is valid (see elf_validity_ehdr()) + * * Section headers are the size we expect + * * Section array fits in the user provided data + * * Section index 0 is NULL + * * Section contents are inbounds + * + * Then updates @info with a &load_info->sechdrs pointer if valid. + * + * Return: %0 if valid, negative error code if validation failed. + */ +static int elf_validity_cache_sechdrs(struct load_info *info) +{ + Elf_Shdr *sechdrs; + Elf_Shdr *shdr; + int i; + int err; + + err = elf_validity_ehdr(info); + if (err < 0) + return err; + + if (info->hdr->e_shentsize != sizeof(Elf_Shdr)) { + pr_err("Invalid ELF section header size\n"); + return -ENOEXEC; + } + + /* + * e_shnum is 16 bits, and sizeof(Elf_Shdr) is + * known and small. So e_shnum * sizeof(Elf_Shdr) + * will not overflow unsigned long on any platform. + */ + if (info->hdr->e_shoff >= info->len + || (info->hdr->e_shnum * sizeof(Elf_Shdr) > + info->len - info->hdr->e_shoff)) { + pr_err("Invalid ELF section header overflow\n"); + return -ENOEXEC; + } + + sechdrs = (void *)info->hdr + info->hdr->e_shoff; + + /* + * The code assumes that section 0 has a length of zero and + * an addr of zero, so check for it. + */ + if (sechdrs[0].sh_type != SHT_NULL + || sechdrs[0].sh_size != 0 + || sechdrs[0].sh_addr != 0) { + pr_err("ELF Spec violation: section 0 type(%d)!=SH_NULL or non-zero len or addr\n", + sechdrs[0].sh_type); + return -ENOEXEC; + } + + /* Validate contents are inbounds */ + for (i = 1; i < info->hdr->e_shnum; i++) { + shdr = &sechdrs[i]; + switch (shdr->sh_type) { + case SHT_NULL: + case SHT_NOBITS: + /* No contents, offset/size don't mean anything */ + continue; + default: + err = validate_section_offset(info, shdr); + if (err < 0) { + pr_err("Invalid ELF section in module (section %u type %u)\n", + i, shdr->sh_type); + return err; + } + } + } + + info->sechdrs = sechdrs; + + return 0; +} + /* * Check userspace passed ELF module against our expectations, and cache * useful variables for further processing as we go. @@ -1737,29 +1818,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; - err = elf_validity_ehdr(info); + err = elf_validity_cache_sechdrs(info); if (err < 0) return err; - if (info->hdr->e_shentsize != sizeof(Elf_Shdr)) { - pr_err("Invalid ELF section header size\n"); - goto no_exec; - } - - /* - * e_shnum is 16 bits, and sizeof(Elf_Shdr) is - * known and small. So e_shnum * sizeof(Elf_Shdr) - * will not overflow unsigned long on any platform. - */ - if (info->hdr->e_shoff >= info->len - || (info->hdr->e_shnum * sizeof(Elf_Shdr) > - info->len - info->hdr->e_shoff)) { - pr_err("Invalid ELF section header overflow\n"); - goto no_exec; - } - - info->sechdrs = (void *)info->hdr + info->hdr->e_shoff; - /* * Verify if the section name table index is valid. */ @@ -1772,11 +1834,6 @@ static int elf_validity_cache_copy(struct load_info *info, int flags) } strhdr = &info->sechdrs[info->hdr->e_shstrndx]; - err = validate_section_offset(info, strhdr); - if (err < 0) { - pr_err("Invalid ELF section hdr(type %u)\n", strhdr->sh_type); - return err; - } /* * The section name table must be NUL-terminated, as required @@ -1793,18 +1850,6 @@ static int elf_validity_cache_copy(struct load_info *info, int flags) goto no_exec; } - /* - * The code assumes that section 0 has a length of zero and - * an addr of zero, so check for it. - */ - if (info->sechdrs[0].sh_type != SHT_NULL - || info->sechdrs[0].sh_size != 0 - || info->sechdrs[0].sh_addr != 0) { - pr_err("ELF Spec violation: section 0 type(%d)!=SH_NULL or non-zero len or addr\n", - info->sechdrs[0].sh_type); - goto no_exec; - } - for (i = 1; i < info->hdr->e_shnum; i++) { shdr = &info->sechdrs[i]; switch (shdr->sh_type) { @@ -1823,12 +1868,6 @@ static int elf_validity_cache_copy(struct load_info *info, int flags) sym_idx = i; fallthrough; default: - err = validate_section_offset(info, shdr); - if (err < 0) { - pr_err("Invalid ELF section in module (section %u type %u)\n", - i, shdr->sh_type); - return err; - } if (strcmp(info->secstrings + shdr->sh_name, ".gnu.linkonce.this_module") == 0) { num_mod_secs++; From patchwork Wed Sep 25 23:38:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Maurer X-Patchwork-Id: 13812597 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 555661A4F1E for ; Wed, 25 Sep 2024 23:39:31 +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=1727307573; cv=none; b=dhsraigfwzs8Mx9GyhS1/UUII3bYpZ6sIaq/DS2hVG3EHbGnzIqIUFRihqGP6a1Et+uS45nCot/H9shVZ1hvTMfIq/kWfZ5HmSQfTi4qo3x65A3Rd+GjaKGs6GynhwPtfDrH00oWZ4diKoO64iL7Xmi7N59Pjqlf+OJaiYBahEM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307573; c=relaxed/simple; bh=iCsuIDpsaG//12LUTpbXOFHaJGeJHbd09pACcn3fot0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=WIvNKUqxMhwW9biRCX+Urkq+0W7khV0/vLeQh6nI7yTjrJV2UPSNNZalGZveH3eySMuU+WiZO9AI7iZkPpdeCVC4sSgR5Q+xOF9h+tJ71qOPBXajaeq9Y4kD7MhyCXUlacP/TPf1q1Bd2vtDO3LePHYMtD115ayBUKCrCQyfXlI= 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=Q08QR+YD; 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="Q08QR+YD" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e25cf4e97aaso27800276.3 for ; Wed, 25 Sep 2024 16:39:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727307570; x=1727912370; 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=vadaxnt9VyshEs2zQXz9EyB1j9KQoYY2nW6Hpjx9NE8=; b=Q08QR+YDROnyZaJUjcY7Me21ezXVpRsUsIEjPwYd/iZNoJr9Xa297mjQyeJkuRvCaI D5iaL7X4NfI1gPcD5IZyOU0VwXpIP5V4ED5MVPiBjihp0vi+IEr+0XQbltQugWA4f2WP 0G/F3f/hItbI/9FLnfrKhC7XkMYIlqp/mCtqgTYiTLjow38lBnuu4xc3raCRBZFE2A1w riZ+uiqUMV4OOmZwMNSAPd3JLngZGn9rJkBTdAlhIXJdp1PRwTa8Gp781SYdxjqzLDNj XDYCuJE+HS0snFX8GBaFcW/kTyADGLdUpGamjfmGo9LEdmfiPQj+pDEgAZYuqBzQ1Q+2 F5eg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727307570; x=1727912370; 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=vadaxnt9VyshEs2zQXz9EyB1j9KQoYY2nW6Hpjx9NE8=; b=iLu7Pf2DlFQrxzOTmnbMuPaoicuYlq+Ti4bcVnGZmwXOoaUeS04MMYiGUVvdNB/s8B mIgUFy6KsMaY5SQeMQrA3c+w/5DihKoqvWSm3uqsa3QwKM/8KeKLnBDn5WfHum8aCD5N oXGH07XjPrOja0tjWLURMMmtkFM4v3bclx5N9rKeXaYPMKbqUM11LGuf8mlvecGWdBKB 4LzYO0UCUjxDB9bhk6/7BGS3trEpS70Cp4X3P+fnUMfd4k/AgQOtSpQKCr6tewS47+WQ tzxzGpbRK7f9gO7VYCfA5LoEDkwdqEBV0NArhxIGiWL+3J3RsqQb9oOP0JiDAVbXEfwQ +Zqg== X-Forwarded-Encrypted: i=1; AJvYcCX6JL69e6lsDKAQq06NWb1mSdTj+a/GJL44dj7LMjYH/9xwuE3KeSqOz5lMZjQyLamGqZipZAPNc9poipw=@vger.kernel.org X-Gm-Message-State: AOJu0YxGOxzGcc61qzEMVPvYHGW68qf9l02bQQPEDVrHq92nHb/mDlVx kd3tojeySEgxBPDE7LttdkT4MDPP5c7HZDwD9ZXm04EA68ExawLaOl94b7KCPtjkLVWePzLqtzx 98Ks8Cw== X-Google-Smtp-Source: AGHT+IGXyiXZNUpD5Qq2WYk35Pvjl1ubLDKQ8ZQZ0AU6QSWocII7bL/lrn6rf8k0qZwiwNOpxGN89htUr/Ws X-Received: from anyblade.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1791]) (user=mmaurer job=sendgmr) by 2002:a05:6902:118f:b0:e24:9584:52d3 with SMTP id 3f1490d57ef6-e24d71625d8mr82056276.2.1727307570290; Wed, 25 Sep 2024 16:39:30 -0700 (PDT) Date: Wed, 25 Sep 2024 23:38:19 +0000 In-Reply-To: <20240925233854.90072-1-mmaurer@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@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-5-mmaurer@google.com> Subject: [PATCH v5 04/16] module: Factor out elf_validity_cache_secstrings 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_Roy_Baron?= " , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Factor out the validation of section names. There are two behavioral changes: 1. Previously, we did not validate non-SHF_ALLOC sections. This may have once been safe, as find_sec skips non-SHF_ALLOC sections, but find_any_sec, which will be used to load BTF if that is enabled, ignores the SHF_ALLOC flag. Since there's no need to support invalid section names, validate all of them, not just SHF_ALLOC sections. 2. Section names were validated *after* accessing them for the purposes of detecting ".modinfo" and ".gnu.linkonce.this_module". They are now checked prior to the access, which could avoid bad accesses with malformed modules. Signed-off-by: Matthew Maurer --- kernel/module/main.c | 106 ++++++++++++++++++++++++++++--------------- 1 file changed, 69 insertions(+), 37 deletions(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index 1f3a07ee59c6..6a9159afca02 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1789,6 +1789,71 @@ static int elf_validity_cache_sechdrs(struct load_info *info) return 0; } +/** + * elf_validity_cache_secstrings() - Caches section names if valid + * @info: Load info to cache section names from. Must have valid sechdrs. + * + * Specifically checks: + * + * * Section name table index is inbounds of section headers + * * Section name table is not empty + * * Section name table is NUL terminated + * * All section name offsets are inbounds of the section + * + * Then updates @info with a &load_info->secstrings pointer if valid. + * + * Return: %0 if valid, negative error code if validation failed. + */ +static int elf_validity_cache_secstrings(struct load_info *info) +{ + Elf_Shdr *strhdr, *shdr; + char *secstrings; + int i; + + /* + * Verify if the section name table index is valid. + */ + if (info->hdr->e_shstrndx == SHN_UNDEF + || info->hdr->e_shstrndx >= info->hdr->e_shnum) { + pr_err("Invalid ELF section name index: %d || e_shstrndx (%d) >= e_shnum (%d)\n", + info->hdr->e_shstrndx, info->hdr->e_shstrndx, + info->hdr->e_shnum); + return -ENOEXEC; + } + + strhdr = &info->sechdrs[info->hdr->e_shstrndx]; + + /* + * The section name table must be NUL-terminated, as required + * by the spec. This makes strcmp and pr_* calls that access + * strings in the section safe. + */ + secstrings = (void *)info->hdr + strhdr->sh_offset; + if (strhdr->sh_size == 0) { + pr_err("empty section name table\n"); + return -ENOEXEC; + } + if (secstrings[strhdr->sh_size - 1] != '\0') { + pr_err("ELF Spec violation: section name table isn't null terminated\n"); + return -ENOEXEC; + } + + for (i = 0; i < info->hdr->e_shnum; i++) { + shdr = &info->sechdrs[i]; + /* SHT_NULL means sh_name has an undefined value */ + if (shdr->sh_type == SHT_NULL) + continue; + if (shdr->sh_name >= strhdr->sh_size) { + pr_err("Invalid ELF section name in module (section %u type %u)\n", + i, shdr->sh_type); + return -ENOEXEC; + } + } + + info->secstrings = secstrings; + return 0; +} + /* * Check userspace passed ELF module against our expectations, and cache * useful variables for further processing as we go. @@ -1812,7 +1877,7 @@ static int elf_validity_cache_sechdrs(struct load_info *info) static int elf_validity_cache_copy(struct load_info *info, int flags) { unsigned int i; - Elf_Shdr *shdr, *strhdr; + Elf_Shdr *shdr; int err; unsigned int num_mod_secs = 0, mod_idx; unsigned int num_info_secs = 0, info_idx; @@ -1821,34 +1886,9 @@ static int elf_validity_cache_copy(struct load_info *info, int flags) err = elf_validity_cache_sechdrs(info); if (err < 0) return err; - - /* - * Verify if the section name table index is valid. - */ - if (info->hdr->e_shstrndx == SHN_UNDEF - || info->hdr->e_shstrndx >= info->hdr->e_shnum) { - pr_err("Invalid ELF section name index: %d || e_shstrndx (%d) >= e_shnum (%d)\n", - info->hdr->e_shstrndx, info->hdr->e_shstrndx, - info->hdr->e_shnum); - goto no_exec; - } - - strhdr = &info->sechdrs[info->hdr->e_shstrndx]; - - /* - * The section name table must be NUL-terminated, as required - * by the spec. This makes strcmp and pr_* calls that access - * strings in the section safe. - */ - info->secstrings = (void *)info->hdr + strhdr->sh_offset; - if (strhdr->sh_size == 0) { - pr_err("empty section name table\n"); - goto no_exec; - } - if (info->secstrings[strhdr->sh_size - 1] != '\0') { - pr_err("ELF Spec violation: section name table isn't null terminated\n"); - goto no_exec; - } + err = elf_validity_cache_secstrings(info); + if (err < 0) + return err; for (i = 1; i < info->hdr->e_shnum; i++) { shdr = &info->sechdrs[i]; @@ -1877,14 +1917,6 @@ static int elf_validity_cache_copy(struct load_info *info, int flags) num_info_secs++; info_idx = i; } - - if (shdr->sh_flags & SHF_ALLOC) { - if (shdr->sh_name >= strhdr->sh_size) { - pr_err("Invalid ELF section name in module (section %u type %u)\n", - i, shdr->sh_type); - return -ENOEXEC; - } - } break; } } From patchwork Wed Sep 25 23:38:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Maurer X-Patchwork-Id: 13812598 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.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 B180E1A727A for ; Wed, 25 Sep 2024 23:39:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307576; cv=none; b=geLDwKFv/OevxoKzd5DKKTlULTB3uvqGVENyTHjkd63np0RbhGvlt7JE7EvM6JnrTUxrBWTYqMbxXiRqmvqEW71PPPo2fBGzxe/uGvLM23+frCwvB36/yOz14vtsyG+CQ64dnDRdsofmCy1FYqP3KOWUiKuFqS2F0VFpgSQpss0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307576; c=relaxed/simple; bh=oOvwo7YW3AUcfbqrf+A9OVLdTk2yYzaOPGripVgZWf8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=A3fDUIpqk3IVlspP8rD+HxAMIPCNVGR4tSbZppqejcyrra5Gmp13MVXu4F2Eep40qwnod+dXUdOrFEu0Hk6oAX6h/kfUpZtbPVvfvy4Ja9SSDKM0uSLuUsegLxPzOo7btjDkKGtnx5MCWkO2lt7oD5e84gmv4Oqzfm0fknI4XT0= 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=m5Z5tSey; arc=none smtp.client-ip=209.85.219.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="m5Z5tSey" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e0353b731b8so587477276.2 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=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=m5Z5tSeyRlsxLy1ersJE+UGxQCyLHbFGDz8D/MbyB/kQa7EYcqRvp/f8uPQukKcrCt EfJ1Aezcx3fQoA6m0su+FwrkOEJYSSEJ9sjwyh81XN3Y497rH+FbhbDjzOEl5RRxJqcp VlypXpgjrZZc7R8C480eVLG4oXVSYm2L+1bi4g0AX+N+4vnBqlNY73Op1ZsJ7ljiUy9q ixYagzg4Gdj+FamIYtiuTGhEiXptWpg5DFWUiQc1qygfowqRpMKtS8WnGuyNST4lx3nZ oxHSLIjZkt0GL4tiNEHelvV57QkjUxh1raAHGuZMGQxxKpuRoMq3uVZFs+7SK92eC2Vp /qxg== 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=kb/1YJTfqPJVBLYbbYxN//K9X+RSZJtJRtfVHCCMqZ4SicM4h0qL1ZZcj9MKcsMG71 8LEBm2WXTLM0R2Kq/8nBdeSBXak5K0lK9559F7BF2irlzgYLskq2K8cVKr5j6QgY6VcD eYNdu0hBzTGYelIy1vSnRIC3TbuY0UQ6IF3dtAy/8edE3ZwZb3cusYXN7kKdt9YwLn8g w4zf+srca6k+g2uoikJnQA8p1UK3jRsK5H54h7sADA6G3DRbSQ7qFZKhjHBmiH3RHuvc PZBiu5q746RJCgu/Cno05pqx3v5ZBI20yLQF7a+cJ2GPsJC2B0Y11d7TGSGtVGOYBZY+ CHzg== X-Forwarded-Encrypted: i=1; AJvYcCUbikWwxNM2T7HadXLXr5rnxyUP2RFr63R+hRXVyHVngFSYdtrx2IFysmflJyhat6PSxVY89wr/rV5eGuM=@vger.kernel.org X-Gm-Message-State: AOJu0YxFVrGW3rFvU+lFI1xTyI8D4WUG7FfpiXBMe0TaYLe1BkVp1SDI ovprxPtPbtBdqB2E6+EY2BvDCkZYVCaHaV5TASzIYSkYpiuRtVQ+cp+BhTIY0C09P5FCwvpt9mA lre93UQ== 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: linux-kbuild@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-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_Roy_Baron?= " , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross 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)"); From patchwork Wed Sep 25 23:38:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Maurer X-Patchwork-Id: 13812599 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.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 6B8051A727A for ; Wed, 25 Sep 2024 23:39:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307578; cv=none; b=Dc2IJy2qodK1WiDCs5kw7ewnfFEXCmW8FT0cNs1zVc9IEcn1ecEsBJlgdbFBXLWIdp1iqPrfP9IkhcARpToM9VTsHyOMI4MqRl6qfUcclkffJl+0kEdBY+RzAYn0P0mbZsHPg5m7zH0QGQ01jKJSA6q0joTQrr6fil6l/BvC+eo= 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.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="bLzMJc1z" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6886cd07673so9464977b3.3 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=kqgDc6yF/i8uSkBT9hFgqm4lbAaptxTgnGYbu7Ez6VPMYSHgnBNazg9QS965LOvspd 3WNyKg45voV/DVDHLaDydkaQz1vqqOR2ST6s7PCIvDrUadKj/sX6HU7S+Bc+z1UPXHVz Z2uA+feGm2RuUiwmXGY21apuAy2WQINMdmRaw3fU9yqKvX0MkLi0/Y01vHIg3TLcYpv+ K5a/cW/esiqqTq4bUN4D4wuP8xsYlKxwVhGT59AEjrQhDWoCn22mQCyosrNVc6+rWU+H ZrtmCZJIIpqMNp3QObXw/7EoUcJUWnYIsWuY+QeqpJkv3OndicRwdQMKZnSGAfQvbVLp 0p/Q== X-Forwarded-Encrypted: i=1; AJvYcCWs8npqMsMgEMlbkjynryVa2t/g1KkpzB2goNMKpAOaG141R6eYNzQpTA7c3jeXU+wMR0in2Q0B/SmHkPI=@vger.kernel.org X-Gm-Message-State: AOJu0Yw5PPEqNUecTEZpSLvwsHcK4OCzcxCLSD0Sb0LYfw/mDHAMtdhf IdaPBXvKQLlJUcvMjt6Dx+w3XV/lHOIL+czA/0JLSBpqMKZh9xhOTqUaPJurU+VJNJfiZfauUGc 4hcid4Q== 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-kbuild@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_Roy_Baron?= " , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross 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; } +/** + * elf_validity_cache_index_mod() - Validates and caches this_module section + * @info: Load info to cache this_module on. + * Must have &load_info->sechdrs and &load_info->secstrings populated + * + * 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 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 = find_any_unique_sec(info, ".gnu.linkonce.this_module"); + if (mod_idx <= 0) { + pr_err("module %s: Exactly one .gnu.linkonce.this_module section must exist.\n", + info->name ?: "(missing .modinfo section or name field)"); + return -ENOEXEC; + } + + shdr = &info->sechdrs[mod_idx]; + + if (shdr->sh_type == SHT_NOBITS) { + pr_err("module %s: .gnu.linkonce.this_module section must have a size set\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 process execution\n", + info->name ?: "(missing .modinfo section or name field)"); + return -ENOEXEC; + } + + if (shdr->sh_size != 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 = 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 = 0, mod_idx; unsigned int num_sym_secs = 0, sym_idx; err = 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 = elf_validity_cache_index_info(info); + if (err < 0) + return err; + err = elf_validity_cache_index_mod(info); if (err < 0) return err; for (i = 1; i < info->hdr->e_shnum; i++) { shdr = &info->sechdrs[i]; - switch (shdr->sh_type) { - case SHT_NULL: - case SHT_NOBITS: - continue; - case SHT_SYMTAB: + if (shdr->sh_type == SHT_SYMTAB) { if (shdr->sh_link == SHN_UNDEF || shdr->sh_link >= info->hdr->e_shnum) { pr_err("Invalid ELF sh_link!=SHN_UNDEF(%d) or (sh_link(%d) >= 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 = i; - fallthrough; - default: - if (strcmp(info->secstrings + shdr->sh_name, - ".gnu.linkonce.this_module") == 0) { - num_mod_secs++; - mod_idx = i; - } - break; } } @@ -1996,55 +2048,8 @@ static int elf_validity_cache_copy(struct load_info *info, int flags) info->index.str = shdr->sh_link; info->strtab = (char *)info->hdr + info->sechdrs[info->index.str].sh_offset; - /* - * 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 != 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 = &info->sechdrs[mod_idx]; - - /* - * This is already implied on the switch above, however let's be - * pedantic about it. - */ - if (shdr->sh_type == SHT_NOBITS) { - pr_err("module %s: .gnu.linkonce.this_module section must have a size set\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 process execution\n", - info->name ?: "(missing .modinfo section or name field)"); - goto no_exec; - } - - if (shdr->sh_size != 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 = mod_idx; - /* This is temporary: point mod into copy of data. */ - info->mod = (void *)info->hdr + shdr->sh_offset; + info->mod = (void *)info->hdr + info->sechdrs[info->index.mod].sh_offset; /* * If we didn't load the .modinfo 'name' field earlier, fall back to From patchwork Wed Sep 25 23:38:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Maurer X-Patchwork-Id: 13812600 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 000B51ABED1 for ; Wed, 25 Sep 2024 23:39:38 +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=1727307580; cv=none; b=rTeEpjDASBVFn5Pv5XuyWeGKWeUfwrGOXdObZ5MYoM3gWDL4ib7eoRLMBMXxc5QMp0ImwYFn+OY9YH5hjq+Mu0HjD/f5YDWH8fC3Z06rRtqXTJWxK0JMDjpXImf4xI8aFXgRwqQE9ueDipGg9XiyHSp8km1YG8UdXsac283p/bE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307580; c=relaxed/simple; bh=hPcayIG4Zzk7TfnQCe5VB/OkeMCLVNaFCb22SLuD7xA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=IJYsfaC/5Y0PjAkMr9biPSe9cVnpztC4mofziEcGz13TeM8g1HlTFS9CiBqmCwY7UJrl6cMTEeZxX0jKpqth5DTz34fk7qZGdSDFMnvwJfrZ51YguQVRBNruBpMnGEry4jK1mtfPZaR0YkZB8c9qU+yS9YtJfH8lz8eUSwkAL/c= 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=wb4Kc+vl; 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="wb4Kc+vl" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e22e472c475so722075276.0 for ; Wed, 25 Sep 2024 16:39:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727307578; x=1727912378; 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=o0kXAOzmEEQHXzTmLkL/vEUOj2egGiISa865No2odHA=; b=wb4Kc+vlm9vspCrEGu31uSdmGCFV4N+qq3ZdgtPNuVg7PQ3AnM/tZOhfz3qmMsSGKR EJ3oKVJaibX0jMt6Z+MELN82Fl4xPXd8nCqPcMLDXQydk4pva9Aw2Atp1OS9iLAiVwbk /FpGLP8QjK2w8po1aocYEVWGjEc/Z9QISVlarljpEcPa5uNqf7A71TvKtxFc0MTg7clF 5kscch70KYzMTVSw25al7YR2T4jsybqoH2sWWp5pBzSEGjSAHdpvqKthwvZ5YcGadoWF s4eLU3/RbMVaF8/7D3L74rtn815TeO0ECl5FTnNu9mxm2o2dt5uKd1HOZkheUoD/Ccmi 0uPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727307578; x=1727912378; 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=o0kXAOzmEEQHXzTmLkL/vEUOj2egGiISa865No2odHA=; b=B+/wMkToKDhVoLHwglR8aTO5pYJ6ufNFKwoIxkOcng1Sykc95YEeClI0pSwVCvF1Gh l8EcMRbvv8JgCgIqqVZ9J0ITey2vI4w2/pvFzs8U7lECUhgoRglzxP8Rm5FmDJqjm1qu AwLFDD9/E3VDp0Ymgq4jxJg3ve9syIgaNCgABWeuJQNJ2+JlkARZk0sSXZXYzP8Etoti YZOKoDYoAR2GJL+mqwtQXhV9M8PofYdgYbwIexZhoar/CDqP60SdSjtfKYy+QqeZEmYA NXB/g+3cBfahcfDy66BqI2TvX/Le2pIw+p5mktHDMRodYbHv+46Co93q6x6GgqzBPjlm y9NA== X-Forwarded-Encrypted: i=1; AJvYcCWCNajnLn2bUY5USne+jFBGNFeujWXsuQIE80PF2M4c/EB2HWh8ucMsvdPoVJpnoN0Eh8O35gRyEvuiBVg=@vger.kernel.org X-Gm-Message-State: AOJu0Yz9hmMqoUadnxqLPijLzkyl5njGgu+l71Nb2PJ3+uu53+aYtXoi ku3Q2YEcDvHeY0EOdCxTPaUsh510+XRPobk2asFXjiBDXltsb6WU1OrKv+CaSqgsSeJFAtxaYox WASfEkQ== X-Google-Smtp-Source: AGHT+IFZUoBFWfBOtauI+q+UpHX4EaqZHRhVqScDHU9VRfOA/W5QmMinhHWYwHFcLrUwulCaskY1L43rYGa0 X-Received: from anyblade.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1791]) (user=mmaurer job=sendgmr) by 2002:a5b:841:0:b0:e20:25f5:a889 with SMTP id 3f1490d57ef6-e24d80d4612mr3980276.5.1727307577820; Wed, 25 Sep 2024 16:39:37 -0700 (PDT) Date: Wed, 25 Sep 2024 23:38:22 +0000 In-Reply-To: <20240925233854.90072-1-mmaurer@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@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-8-mmaurer@google.com> Subject: [PATCH v5 07/16] module: Factor out elf_validity_cache_index_sym 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_Roy_Baron?= " , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Centralize symbol table detection and property validation. Signed-off-by: Matthew Maurer --- kernel/module/main.c | 73 ++++++++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 29 deletions(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index ec638187ffcf..6be58b0a6468 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1980,6 +1980,39 @@ static int elf_validity_cache_index_mod(struct load_info *info) return 0; } +/** + * elf_validity_cache_index_sym() - Validate and cache symtab index + * @info: Load info to cache symtab index in. + * Must have &load_info->sechdrs and &load_info->secstrings populated. + * + * Checks that there is exactly one symbol table, then caches its index in + * &load_info->index.sym. + * + * Return: %0 if valid, %-ENOEXEC on failure. + */ +static int elf_validity_cache_index_sym(struct load_info *info) +{ + unsigned int sym_idx; + unsigned int num_sym_secs = 0; + int i; + + for (i = 1; i < info->hdr->e_shnum; i++) { + if (info->sechdrs[i].sh_type == SHT_SYMTAB) { + num_sym_secs++; + sym_idx = i; + } + } + + if (num_sym_secs != 1) { + pr_warn("%s: module has no symbols (stripped?)\n", + info->name ?: "(missing .modinfo section or name field)"); + return -ENOEXEC; + } + + info->index.sym = sym_idx; + + return 0; +} /* * Check userspace passed ELF module against our expectations, and cache @@ -2003,10 +2036,8 @@ static int elf_validity_cache_index_mod(struct load_info *info) */ static int elf_validity_cache_copy(struct load_info *info, int flags) { - unsigned int i; - Elf_Shdr *shdr; int err; - unsigned int num_sym_secs = 0, sym_idx; + int str_idx; err = elf_validity_cache_sechdrs(info); if (err < 0) @@ -2018,34 +2049,21 @@ static int elf_validity_cache_copy(struct load_info *info, int flags) if (err < 0) return err; err = elf_validity_cache_index_mod(info); + if (err < 0) + return err; + err = elf_validity_cache_index_sym(info); if (err < 0) return err; - for (i = 1; i < info->hdr->e_shnum; i++) { - shdr = &info->sechdrs[i]; - if (shdr->sh_type == SHT_SYMTAB) { - if (shdr->sh_link == SHN_UNDEF - || shdr->sh_link >= info->hdr->e_shnum) { - pr_err("Invalid ELF sh_link!=SHN_UNDEF(%d) or (sh_link(%d) >= hdr->e_shnum(%d)\n", - shdr->sh_link, shdr->sh_link, - info->hdr->e_shnum); - goto no_exec; - } - num_sym_secs++; - sym_idx = i; - } - } - - if (num_sym_secs != 1) { - pr_warn("%s: module has no symbols (stripped?)\n", - info->name ?: "(missing .modinfo section or name field)"); - goto no_exec; + str_idx = info->sechdrs[info->index.sym].sh_link; + if (str_idx == SHN_UNDEF || str_idx >= info->hdr->e_shnum) { + pr_err("Invalid ELF sh_link!=SHN_UNDEF(%d) or (sh_link(%d) >= hdr->e_shnum(%d)\n", + str_idx, str_idx, info->hdr->e_shnum); + return -ENOEXEC; } - /* Sets internal symbols and strings. */ - info->index.sym = sym_idx; - shdr = &info->sechdrs[sym_idx]; - info->index.str = shdr->sh_link; + /* Sets internal strings. */ + info->index.str = str_idx; info->strtab = (char *)info->hdr + info->sechdrs[info->index.str].sh_offset; /* This is temporary: point mod into copy of data. */ @@ -2066,9 +2084,6 @@ static int elf_validity_cache_copy(struct load_info *info, int flags) info->index.pcpu = find_pcpusec(info); return 0; - -no_exec: - return -ENOEXEC; } #define COPY_CHUNK_SIZE (16*PAGE_SIZE) From patchwork Wed Sep 25 23:38:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Maurer X-Patchwork-Id: 13812601 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.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 2DFCA1AC44E for ; Wed, 25 Sep 2024 23:39:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307582; cv=none; b=mM4UHIuXmelB8LS3OOgg30YyKe/wfOAezvgXV4jRfjIyXnJTh9ZUwRC51MDrx4pHXYFxzPONXoQJ5BydPkJoHutPVzuhzlHWvLc/hUjLZ+u+VpqIMzryKHz5ch7m69d474nMF+EZTqtLa8EHx3r03Ignq0UXEm+yZFrcx+RMArw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307582; c=relaxed/simple; bh=wM975yYhhnJbskoT3Xe/Z8RaXLTIAwoZIK7NoqKeDB8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=JQsxk6QtAlHG2MQbo9VpSqCZd1/UtYfYrzLhakv3wKgmSqhzOFvkaFFe9f7t4nT0hVbknWSHU0WMI/w+6H8A+5Ue9CT1e7m/8PuJw9z5SCCRarT6l8WkAd+IGCvlCLgXa27HRVSHW+0yXvfuROAIhu4Mks3ZdlMarFof839T1zI= 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=Ace5t6Cv; arc=none smtp.client-ip=209.85.128.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="Ace5t6Cv" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-690404fd230so8031187b3.3 for ; Wed, 25 Sep 2024 16:39:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727307580; x=1727912380; 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=fOjsxdYdAkInxupUorws184MmwFGmal1AWb/bAcHrOQ=; b=Ace5t6CvyKVH55R5F+8+SFEGTxJt+z7edGdIMLwXZb2veNJnyJiw+inHBKPQefkXcj yfj9g09w66ixqXNGIKfYucTJ/eQohq0K1Zw2pRs/eTkiv0VfbbVbxWuoBf/BqOEqslT5 /ztQBPjaqKWZO0jdXdJa3Wu5L1+NUjStuEM5JR2vT+eTeJuhMuhiklq/b6z3OdsGFcwm dEwg8wI3mrLRBO4tM79Mpzhv34Gs4wka9YKLR6aAjcdY5/2JuRczf900R1uv+5+F6+33 A11AMc3aVg0SeQnHWFLMN4hDviU7z4O6SwauY33tJ1U/86KDOoxW2/k2Gqmtpu/SXwey eadg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727307580; x=1727912380; 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=fOjsxdYdAkInxupUorws184MmwFGmal1AWb/bAcHrOQ=; b=KuQEnQF/V2rRmT4pnY++fsVpMH0c7clMFi8LSlVAAJt9sdtXtwEeqDNr3tWYO+CpmQ iiavh+7CYde7rbF3FO9udX0wwVKB3d31ANpjwbJClH6exEafhkWRTPWAL1/kcRFT1Szn 1BOaonJ5A/KdBBjG39y+eB5Mb30IudGiK67P0tq2R1RiRcKc6WlN6GoNhuWYmPW6oemD J6UJEjXmHk8C93BdImO1V7Bfwi9vEo2LrKSWjmtRAVUy8jjiiySDXcafo49y6zMqym4b OnHewbO3dbjEtz5bZjrof2kP9D+Hua7PlwNLoNGBlYybcau1o6ajBLNbQ6E1j6pb5u2K zFTg== X-Forwarded-Encrypted: i=1; AJvYcCU9WV5+ilZ5CNj2+7nB5i2fO0hbwzlzhFA35KlCzVZZ9hzmG6qRHgX5EwE1IXiVGRS8YwgQIHqQP2oT2P0=@vger.kernel.org X-Gm-Message-State: AOJu0Yy8X6TnqBo5MEvFIihKeM0fdwl5vjgrZOcdQXfMZPn0IoBylZak m1Dt3Sv3I7bkn9gCTSbSYSHx7VkqN0YBDuYWoX8Xer4WYHV7Dwh5FvMbJA78vd3IxswiC+rNS+l anTiBuA== X-Google-Smtp-Source: AGHT+IHdWqR9IrD2hZQX0Xo9yhQJzQmApDf52iNmKjMPjmcrCoxOYUdkaSUs0GEug6EnSPPHsMtafWiB8pKN X-Received: from anyblade.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1791]) (user=mmaurer job=sendgmr) by 2002:a5b:841:0:b0:e24:9af1:43e7 with SMTP id 3f1490d57ef6-e24d7fdf205mr3591276.5.1727307579861; Wed, 25 Sep 2024 16:39:39 -0700 (PDT) Date: Wed, 25 Sep 2024 23:38:23 +0000 In-Reply-To: <20240925233854.90072-1-mmaurer@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@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-9-mmaurer@google.com> Subject: [PATCH v5 08/16] module: Factor out elf_validity_cache_index_str 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_Roy_Baron?= " , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Pull out index validation for the symbol string section. Note that this does not validate the *contents* of the string table, only shape and presence of the section. Signed-off-by: Matthew Maurer --- kernel/module/main.c | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index 6be58b0a6468..43140475aac0 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -2014,6 +2014,31 @@ static int elf_validity_cache_index_sym(struct load_info *info) return 0; } +/** + * elf_validity_cache_index_str() - Validate and cache strtab index + * @info: Load info to cache strtab index in. + * Must have &load_info->sechdrs and &load_info->secstrings populated. + * Must have &load_info->index.sym populated. + * + * Looks at the symbol table's associated string table, makes sure it is + * in-bounds, and caches it. + * + * Return: %0 if valid, %-ENOEXEC on failure. + */ +static int elf_validity_cache_index_str(struct load_info *info) +{ + unsigned int str_idx = info->sechdrs[info->index.sym].sh_link; + + if (str_idx == SHN_UNDEF || str_idx >= info->hdr->e_shnum) { + pr_err("Invalid ELF sh_link!=SHN_UNDEF(%d) or (sh_link(%d) >= hdr->e_shnum(%d)\n", + str_idx, str_idx, info->hdr->e_shnum); + return -ENOEXEC; + } + + info->index.str = str_idx; + return 0; +} + /* * Check userspace passed ELF module against our expectations, and cache * useful variables for further processing as we go. @@ -2037,7 +2062,6 @@ static int elf_validity_cache_index_sym(struct load_info *info) static int elf_validity_cache_copy(struct load_info *info, int flags) { int err; - int str_idx; err = elf_validity_cache_sechdrs(info); if (err < 0) @@ -2054,16 +2078,11 @@ static int elf_validity_cache_copy(struct load_info *info, int flags) err = elf_validity_cache_index_sym(info); if (err < 0) return err; - - str_idx = info->sechdrs[info->index.sym].sh_link; - if (str_idx == SHN_UNDEF || str_idx >= info->hdr->e_shnum) { - pr_err("Invalid ELF sh_link!=SHN_UNDEF(%d) or (sh_link(%d) >= hdr->e_shnum(%d)\n", - str_idx, str_idx, info->hdr->e_shnum); - return -ENOEXEC; - } + err = elf_validity_cache_index_str(info); + if (err < 0) + return err; /* Sets internal strings. */ - info->index.str = str_idx; info->strtab = (char *)info->hdr + info->sechdrs[info->index.str].sh_offset; /* This is temporary: point mod into copy of data. */ From patchwork Wed Sep 25 23:38:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Maurer X-Patchwork-Id: 13812602 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.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 3C6A21AC897 for ; Wed, 25 Sep 2024 23:39:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307584; cv=none; b=enEAa4h4X5EJMUxnnlRKa9i3ClNQ+VXh/RcgH1sQQ+mPv1jsl6F3MU0Ppb5ugniC7oj5Ixu7eNZYRHwQEsh8sw8bXVzgQTKx0h8CGXtvQ9m56A0Gw+i9WZzzreSmBsA7hJfhNWAU3823A6Oc1wQibYopNych3vYrXvyvPmEZbgM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307584; c=relaxed/simple; bh=uwfaf/NAzm+40ipxYEoBkkFYyfKzhKmnZzjM3YJU8lE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ByBUyeWxdFnPegb5qS/btQner/G+MAu1UTjTbHLYRVXjNPJJWhH/xwAGPQ8HDdA/6X07r+oRqRCbMhPNXMfqt9bPR8jbXLNJ0KSRPddxjfigVhSht4VLVT2Yy630N/gYB6oo09Dc5RH+FRXZt2t8m/eckL96wP9O0ugKa9AwK2A= 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=Dnn4lcx6; arc=none smtp.client-ip=209.85.128.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="Dnn4lcx6" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6d683cfa528so5238377b3.0 for ; Wed, 25 Sep 2024 16:39:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727307582; x=1727912382; 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=tc4NXPfplG38lAGlz62WMnJTBJMM1sboCG51BTx1YyI=; b=Dnn4lcx6ETC3b56/fMVL5Sowje5BnLypRNCI6y7V/UppV+ykNC/QI9yXrPLZa4MZDt IiHnuatUNziMF/+SokFMxI0gK60XEbhHCBIUJk6S/fo1t/1cq++ylzYyVRuLE9Nh1qfU v2AibX5noO2NCGTeCsNF7VFL8XgWV4/+kWp8VSXbVFom43FlVRqHkSHr9uKSyEh5DFL5 y2dhTc823caiGsStgcaR3298Pw1aegvPXvVBHk+4CYlTHMqbdoyv1fwD3aIFipfMDUcU 5o31nY4/exqQ5P3Bpj7AO0SDYu6DhamliBw7OMpApYnlI43yFEggoO8VVNhZP9eWf7kJ 9XjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727307582; x=1727912382; 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=tc4NXPfplG38lAGlz62WMnJTBJMM1sboCG51BTx1YyI=; b=VCK/aAL0NrKvAOeBdmy/RY29C3A9SUGdcI5pYI5WePwicNA1pOI9v1d+Xiv+Wpql5u 1rjbeiJNScWgVufKN/Fg4q2PMhdn4OyYn2jS2qpEwRv2cz10HPT/EfPIF7SFewWDMT2U 7inBYWm1j98I8XYJHQ8PEqa1ClOFo1ydUpgNUyM0COxhbGyoDoC1j4Ps1errCKxHKR0T INdvNZtVqhgOHmmlBsS83yWusPuEei3A9J3DVAh3DQIHI4W/FeMfJwM4x2E2AfQWeB7E 6esWX6z84rOUXpyXsYbnhEsDgFoLNGTKyFXT3j5gSH8MSxNNonbuqC+2Hf71Q3uxv2Ba f8dw== X-Forwarded-Encrypted: i=1; AJvYcCW1Cu1F4wyPIpjmYveLD8+NRTAkeGSPIMOwmcjoJoBXH4QNKszQe4CF9APNg4OfEOXobKsWkMyUwHrqfx8=@vger.kernel.org X-Gm-Message-State: AOJu0YzfGtaBPX/FD0u9lWY5Aoadya0FDTkuZzIGRjmSsEOoDYVsZzVX 2JboysjfPdwaIT2ITKIjsrfzV/reosR4Z27DIsZQlaVzQJhoIx9054C9MmMD/rEBcl6LZIAS3xq PjV20dA== X-Google-Smtp-Source: AGHT+IF+swNEdiAvpBogXsgKrBiSvXnXm1t/O+Acxf832i2L6CrfB3YUi3r7S2mXXta4Ea8IRO3aupfjGYBp X-Received: from anyblade.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1791]) (user=mmaurer job=sendgmr) by 2002:a05:6902:1367:b0:e1a:22d5:d9eb with SMTP id 3f1490d57ef6-e24d716247fmr3341276.1.1727307582039; Wed, 25 Sep 2024 16:39:42 -0700 (PDT) Date: Wed, 25 Sep 2024 23:38:24 +0000 In-Reply-To: <20240925233854.90072-1-mmaurer@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@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-10-mmaurer@google.com> Subject: [PATCH v5 09/16] module: Group section index calculations together 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_Roy_Baron?= " , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Group all the index detection together to make the parent function easier to read. Signed-off-by: Matthew Maurer --- kernel/module/main.c | 68 +++++++++++++++++++++++++++++++++----------- 1 file changed, 51 insertions(+), 17 deletions(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index 43140475aac0..e04a228c694a 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -2039,6 +2039,56 @@ static int elf_validity_cache_index_str(struct load_info *info) return 0; } +/** + * elf_validity_cache_index() - Resolve, validate, cache section indices + * @info: Load info to read from and update. + * &load_info->sechdrs and &load_info->secstrings must be populated. + * @flags: Load flags, relevant to suppress version loading, see + * uapi/linux/module.h + * + * Populates &load_info->index, validating as it goes. + * See child functions for per-field validation: + * + * * elf_validity_cache_index_info() + * * elf_validity_cache_index_mod() + * * elf_validity_cache_index_sym() + * * elf_validity_cache_index_str() + * + * If versioning is not suppressed via flags, load the version index from + * a section called "__versions" with no validation. + * + * If CONFIG_SMP is enabled, load the percpu section by name with no + * validation. + * + * Return: 0 on success, negative error code if an index failed validation. + */ +static int elf_validity_cache_index(struct load_info *info, int flags) +{ + int err; + + err = elf_validity_cache_index_info(info); + if (err < 0) + return err; + err = elf_validity_cache_index_mod(info); + if (err < 0) + return err; + err = elf_validity_cache_index_sym(info); + if (err < 0) + return err; + err = elf_validity_cache_index_str(info); + if (err < 0) + return err; + + if (flags & MODULE_INIT_IGNORE_MODVERSIONS) + info->index.vers = 0; /* Pretend no __versions section! */ + else + info->index.vers = find_sec(info, "__versions"); + + info->index.pcpu = find_pcpusec(info); + + return 0; +} + /* * Check userspace passed ELF module against our expectations, and cache * useful variables for further processing as we go. @@ -2069,16 +2119,7 @@ static int elf_validity_cache_copy(struct load_info *info, int flags) err = elf_validity_cache_secstrings(info); if (err < 0) return err; - err = elf_validity_cache_index_info(info); - if (err < 0) - return err; - err = elf_validity_cache_index_mod(info); - if (err < 0) - return err; - err = elf_validity_cache_index_sym(info); - if (err < 0) - return err; - err = elf_validity_cache_index_str(info); + err = elf_validity_cache_index(info, flags); if (err < 0) return err; @@ -2095,13 +2136,6 @@ static int elf_validity_cache_copy(struct load_info *info, int flags) if (!info->name) info->name = info->mod->name; - if (flags & MODULE_INIT_IGNORE_MODVERSIONS) - info->index.vers = 0; /* Pretend no __versions section! */ - else - info->index.vers = find_sec(info, "__versions"); - - info->index.pcpu = find_pcpusec(info); - return 0; } From patchwork Wed Sep 25 23:38:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Maurer X-Patchwork-Id: 13812603 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.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 1FF291ACE1C for ; Wed, 25 Sep 2024 23:39:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307586; cv=none; b=fpvYqclzhv+rh8AfMeAMwOZ7Srn2UUBoqI3ikbBHwccepDNNGIJW14F3G4h9ebrtkXQs1Bgul7LrFJ+esUgB2lRTnZuwbTO3NOdLoGDAvd+6KRyK/uVva7qYcAxHrdg9Ty1IedZYZ81LdPjQF3DIlhRooEK1MtniCyGoT3evl3w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307586; c=relaxed/simple; bh=RFwYTaeqemLtmezDNnI4k0C/4IQdB480nO3xNfeeufc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=aSIpWGrIEin6q8gJqLc6/dP4aygne4dj0LycG3AmjfrYs60KNV2gfafBsQLkVd7tOiboQ/MCmjxb31Cp0wvlop1BzKIT96mWqJNns1ls+SInmoSVnqy+uoxEU00TjA0QqCvNsJ8PBuoXOEqSFYoXItKUTeTIgan93O/GXXiEdd8= 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=MZmAsWA3; arc=none smtp.client-ip=209.85.128.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="MZmAsWA3" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6e0082c1dd0so12723357b3.3 for ; Wed, 25 Sep 2024 16:39:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727307584; x=1727912384; 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=extRuG4D1AdoHKvsMv2fLx/d2D/gEpHsXYLRVzC3Sa0=; b=MZmAsWA3xomFzJ3UUFSEudjq/FEAUJacydlEI1beul+6uGuE6Dk612LIlvkuQsgQxE LKFw1JuiaBXdG1uHQJtonGotcE3vuZBlVdDPI5OlmeijfeydlODQgykSy2MKioYhpxBx tpDeVANLnJsAfhxUabBpeAZO2v32QtQ0ZnkCZwOjiQMrKcSKK7qk+g9nJqLz2wWRoIkn 1thJdq3czEqvSmthv4qaSVD3DLOipzTmOnKwOsYfep36BabfVuSbJIbAk7IsFiWHeb9V bLKfqdSTWXI08j85AFD+OBSQAOnaiR4vQbZCgv8iuHqgKqpIcqasuCk+F8w4TSNp76rw kF9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727307584; x=1727912384; 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=extRuG4D1AdoHKvsMv2fLx/d2D/gEpHsXYLRVzC3Sa0=; b=nQxVkT+NolGuKFOL1VfS7LovQdmytPOGljhLMnbZa8e1EDkCZtrPEQQU22iFE0NY+B TiEj6iN2Te1GeCQzO3i/+99r1e2D1cCskxcePe/KR0nw6BsbiWqyxVw7QJPrTSDZ79hs 3m3/+By0FwLMJg1TyxV0kSoVptcaJltl4fUNcfQopteFergi47HY6uPUDOWfxXheSs7g GL1gBw2WVtb3Yl0o+G87/TaCT3n5wDqjU81bpKq4g2LU2apDOdXM7mWyiK8kPycx1HoM rsBwuCYOdS5BGcXzGohWuM/QlyOFVsrSwubsTD00QPVFAmoU6WV7OwHJfpOAL7IqiweR yXwg== X-Forwarded-Encrypted: i=1; AJvYcCVcak8zmNCOKxjIYsoZP2t9J9UgSikgV5vXGVkn7BTpOjMZyvOk/y4alg00vU0SMe/xKaDJgaIwfAPENRA=@vger.kernel.org X-Gm-Message-State: AOJu0YwxIBrXLJDOnx6R3doV4XgOf9dRo8jQFsWL0UgH6s3wwu9o+My7 kvtZBUpLqPFUe8R/uZt0svEc44QwiGbJNO8CDVYKzVEvNfuGskMvQTjuwSC5jX3/bvOalh/qFxL z3/SuNg== X-Google-Smtp-Source: AGHT+IHi0gSVKxF9796sY6n1cVcgNiz/fsNc836mupUZtqmyvJTlnsLOXeqwbXzuTlIf+2iUj78FZGeorFh0 X-Received: from anyblade.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1791]) (user=mmaurer job=sendgmr) by 2002:a05:690c:2a92:b0:6e2:2600:ed65 with SMTP id 00721157ae682-6e22600f35dmr247467b3.1.1727307584197; Wed, 25 Sep 2024 16:39:44 -0700 (PDT) Date: Wed, 25 Sep 2024 23:38:25 +0000 In-Reply-To: <20240925233854.90072-1-mmaurer@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@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-11-mmaurer@google.com> Subject: [PATCH v5 10/16] module: Factor out elf_validity_cache_strtab 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_Roy_Baron?= " , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross This patch only moves the existing strtab population to a function. Validation comes in a following patch, this is split out to make the new validation checks more clearly separated. Signed-off-by: Matthew Maurer --- kernel/module/main.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index e04a228c694a..c082d5d41a8d 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -2089,6 +2089,23 @@ static int elf_validity_cache_index(struct load_info *info, int flags) return 0; } +/** + * elf_validity_cache_strtab() - Cache symbol string table + * @info: Load info to read from and update. + * Must have &load_info->sechdrs and &load_info->secstrings populated. + * Must have &load_info->index populated. + * + * Return: 0 on success, negative error code if a check failed. + */ +static int elf_validity_cache_strtab(struct load_info *info) +{ + Elf_Shdr *str_shdr = &info->sechdrs[info->index.str]; + char *strtab = (char *)info->hdr + str_shdr->sh_offset; + + info->strtab = strtab; + return 0; +} + /* * Check userspace passed ELF module against our expectations, and cache * useful variables for further processing as we go. @@ -2122,9 +2139,9 @@ static int elf_validity_cache_copy(struct load_info *info, int flags) err = elf_validity_cache_index(info, flags); if (err < 0) return err; - - /* Sets internal strings. */ - info->strtab = (char *)info->hdr + info->sechdrs[info->index.str].sh_offset; + err = elf_validity_cache_strtab(info); + if (err < 0) + return err; /* This is temporary: point mod into copy of data. */ info->mod = (void *)info->hdr + info->sechdrs[info->index.mod].sh_offset; From patchwork Wed Sep 25 23:38:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Maurer X-Patchwork-Id: 13812604 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 5E1C41AD40A for ; Wed, 25 Sep 2024 23:39:47 +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=1727307589; cv=none; b=KzfFkp65/R63CMOLdGO2SDEB711KXEDGaEFqwSEZQWDI2VGx8R4s2Ww7HWsVQTNgOOx+6U/MXn716DSajHV7BeNu/Ci+dpYV/0yWUxhjJ2LJMds+6Hp2ODedr0s64SJVyZMhlRwQx4ticZnOC99men80wmkNW9h5Hz+yetNhk8M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307589; c=relaxed/simple; bh=9QDYzeFqtIgpifJubbDxlmlH7eEjUZaH/2YQ84rdY0c=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=GH9haDa2EUeQTrZ2S+BCe291bOvwrIk5w4Qhz4EQhceUi8ibG2E5+nWWvWAkAuj5nzFOekAWt84jxr17gft3XiB3aKY/tKFVxyGyiFSDGvv07CTKDP27p2xeofQaBv17wFtqk3CCWS12KWeU88FA98/sYYc9m6nCiJLHfjusM/I= 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=OTdjCMgM; 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="OTdjCMgM" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-69a0536b23aso12167227b3.3 for ; Wed, 25 Sep 2024 16:39:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727307586; x=1727912386; 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=6yXvv/vrTLDrEzmPTfvxgBAIug2sA8OdrjoRUJniL2M=; b=OTdjCMgM8YjxDuEHL8ypwKq7l22iOrFWtkQjFufh9dEToAFONT32xfWhlYlYyjYPzh I5yjUlBc5jn4Th5d13BoVfmIoA1V0zqYnXOcpw8JE6CfyloFRCLBJEcsnywsNqhoSqSd l+MXDGR7HPpopbirbgIsdc7HUZpIOWUK+4UXwxNw+ky3BUrtyITuuUucrkU5oFqjY2pe /VvWCgQmo4PptPK5LWwbjMRdZaRDriPq55Ltk721/L1aM9+bL7owhnDXnKTx3/3OnOEn eEXBxTZphjfst/D4skhIKnZka6ofbVS2t+NF37pF6X+HDhksD+a8UgUiSGnPZZ9dtavR fvFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727307586; x=1727912386; 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=6yXvv/vrTLDrEzmPTfvxgBAIug2sA8OdrjoRUJniL2M=; b=dMRqg+nJbohTDGkKN1G95MU5jUOJjUExEgZMJUOtipSR+3d4u/AI8DOFCeO3Ebe1wq FmLc3jwYsZLdlOU+UrucDviZ9Kz92S1hPI9AFBVmrRMkha0CCf7wiYxyCJyjoLc5Eojh 8vmPBPaosO2pzzajd//jAw2gn4BRUauf+8t65OzU661ir7pyc+SWgudvIbxAnnb1SNRg Lb8GIIwxgyNuOrvM/PET6/xhhQpJuq2hodxmZ1mntbslCadVvng/57FwKJWmj+g7jylJ XSZTRfv6B81VpJWNAlT0AxNNjDOmq0oFi6UTaN5E/Iau5a1A0P09ZhJqibXw/Gis9X+O 5iDw== X-Forwarded-Encrypted: i=1; AJvYcCVNYxkaGwAOKl6cVMDwcd/5FrI4sq0zLc9vlLYuPid3WOYSEkt1LxlALtGB+oB3UJIWAyzI52H0e/52M5s=@vger.kernel.org X-Gm-Message-State: AOJu0YzbxQI6jS9u5MKMiHiVFWb8q3PLlwMJvNOSkxSs/4xnnK+SEQLo vbleVFlfbgfYaTBIX2dE8HGSUUeUDFDgCbLZioy4nNsqWdacDTYNyfptWWW94Fj3E2vDDIns0pC MGOkWsw== X-Google-Smtp-Source: AGHT+IFOctLtu2fDpWSwtgdrlWQhzeyg6ur9X7N6yKJb4yvjNshlLawCmOC09mIbp13RxLe82I6Qn8MDC9MI X-Received: from anyblade.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1791]) (user=mmaurer job=sendgmr) by 2002:a05:690c:6583:b0:6dd:bc47:1ce1 with SMTP id 00721157ae682-6e21d0b47e5mr368107b3.0.1727307586448; Wed, 25 Sep 2024 16:39:46 -0700 (PDT) Date: Wed, 25 Sep 2024 23:38:26 +0000 In-Reply-To: <20240925233854.90072-1-mmaurer@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@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-12-mmaurer@google.com> Subject: [PATCH v5 11/16] module: Additional validation in elf_validity_cache_strtab 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_Roy_Baron?= " , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Validate properties of the strtab that are depended on elsewhere, but were previously unchecked: * String table nonempty (offset 0 is valid) * String table has a leading NUL (offset 0 corresponds to "") * String table is NUL terminated (strfoo functions won't run out of the table while reading). * All symbols names are inbounds of the string table. Signed-off-by: Matthew Maurer --- kernel/module/main.c | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index c082d5d41a8d..b40b632f00a6 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -2090,17 +2090,53 @@ static int elf_validity_cache_index(struct load_info *info, int flags) } /** - * elf_validity_cache_strtab() - Cache symbol string table + * elf_validity_cache_strtab() - Validate and cache symbol string table * @info: Load info to read from and update. * Must have &load_info->sechdrs and &load_info->secstrings populated. * Must have &load_info->index populated. * + * Checks: + * + * * The string table is not empty. + * * The string table starts and ends with NUL (required by ELF spec). + * * Every &Elf_Sym->st_name offset in the symbol table is inbounds of the + * string table. + * + * And caches the pointer as &load_info->strtab in @info. + * * Return: 0 on success, negative error code if a check failed. */ static int elf_validity_cache_strtab(struct load_info *info) { Elf_Shdr *str_shdr = &info->sechdrs[info->index.str]; + Elf_Shdr *sym_shdr = &info->sechdrs[info->index.sym]; char *strtab = (char *)info->hdr + str_shdr->sh_offset; + Elf_Sym *syms = (void *)info->hdr + sym_shdr->sh_offset; + int i; + + if (str_shdr->sh_size == 0) { + pr_err("empty symbol string table\n"); + return -ENOEXEC; + } + if (strtab[0] != '\0') { + pr_err("symbol string table missing leading NUL\n"); + return -ENOEXEC; + } + if (strtab[str_shdr->sh_size - 1] != '\0') { + pr_err("symbol string table isn't NUL terminated\n"); + return -ENOEXEC; + } + + /* + * Now that we know strtab is correctly structured, check symbol + * starts are inbounds before they're used later. + */ + for (i = 0; i < sym_shdr->sh_size / sizeof(*syms); i++) { + if (syms[i].st_name >= str_shdr->sh_size) { + pr_err("symbol name out of bounds in string table"); + return -ENOEXEC; + } + } info->strtab = strtab; return 0; From patchwork Wed Sep 25 23:38:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Maurer X-Patchwork-Id: 13812605 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.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 963321AD9D2 for ; Wed, 25 Sep 2024 23:39:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307591; cv=none; b=WHffUv2cfaCNJFAi0ZKi8AiIrIrm6jKGh4XYZZmb+Gec1KttIaKebhl6dWGkittQzJuipE5xiNLj8BsDwy3CRZLPwI7wi0tohTVPoG5WAML6BOMsZZmZVHElXwOwCuPn02htqHr3Nc/yLCKEDKrKixhUjr0uld7WUgM7iG3cPLo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307591; c=relaxed/simple; bh=W7tU7P3GNG+a73WgyQWeP1re2H5Hhpn0LeAD9h0akfE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=oaeHad6jIU/WCp63Bb6Y4ZSVFeWVqF+vtsacgKPEF7imS9as01wd0R7NLzthsBGamz2DdaLzb+9Ek1tZKeu+i/yhGKssJ2lG2jB2HplAbHLcHAcQZ5eWT/kzXI8ZQGMTEGo5pfEz0+jBmbWjrTdVgCBIqnFewDGf3LTr7GUikf4= 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=k0MZOISt; arc=none smtp.client-ip=209.85.128.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="k0MZOISt" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6e21d0bfba7so9662537b3.3 for ; Wed, 25 Sep 2024 16:39:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727307589; x=1727912389; 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=t2yLoLuY3hdxUDQj4FAKHilbBg2KnzEPjHr+p4Rv35M=; b=k0MZOIStxeoZXVUF3SJMKXDcAm5STmIlI3OIg3Fl2vjaRXY/whcZtEC1DIffzUPVX6 GxJ3KS9/LE787QxTPsXtloPhhHp3NsQYs3RJe5sbWQwwukOCmgUHWIuMmTy6+SzNhZ28 +rJjcpI0NA6nsgtPsnPJsqkEz5srRdl3ybWKucSYzXQlmjWUBCeLwu+qZMc3qETB/Rdx TMtlBGwpgZP0CnEe95JKgqO4v1fB9/o1jUJJfc/6yJ8swTnfacnXp5Zs6vdHyBm8kjw5 +H0IZQxdUUNleR8XBBHzJHTuwB3jpKz2XL9dPjFoNhQ3IxlUChMswxwpwSbIyXYzv6Vn LRJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727307589; x=1727912389; 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=t2yLoLuY3hdxUDQj4FAKHilbBg2KnzEPjHr+p4Rv35M=; b=qn8sqkVdUhAzurVSrs7MyjKnQkm6rGKp7ftGtifTm4wPn3NMrHXRw1pFlZQyxfCIHq Kig4QBj6PTyJkHxy8R5J8C9UTShDSsinx+M9LQOQGWh6oIl3et0T4Hgkl+Yj45/YaqZU Bow7ZAyY6NUEVB2GG/M2o5KpxRN3IUIkhS7774NusyiPYV6NOrGIb+PfUSeNiANK5fez /AIoxhcePYGHlWKHttpzcZOY5JyBwB03s3dQQUYorZpjIBnKj9QIsYjsazMNtbEb1Ci/ VOHC2gDWQY5o7+sNi3enmeyglLvGw17AQp9V9kb1jvV7Jmu+NQDGVe1hngDQn2hGWWNZ 7dMw== X-Forwarded-Encrypted: i=1; AJvYcCUR2U9R3/DYgaEImRuQZ5x2FkLAYFTx6RJTMhf0HuO5xjhJ/gvRIXWN5PMQdLXXzAdaI9ixVXdvK6NW5e8=@vger.kernel.org X-Gm-Message-State: AOJu0YzPdKTdBSl2m/L/PDbXYIobdlDCgXP7npMR8d19KO45fKs9Cx8N SH0wV6jNCFlU4EfFrIg3+3YKQk+jqtK2UC/g39NKgO5/K+o3aT4toxUO12jkFDDy765SOVo6e8b tSQMJEA== X-Google-Smtp-Source: AGHT+IGo3KQ6k/CMKWp9luhAJxulkW1/EkZDzA8kfvweM/2fd5g67sil+zl5Hoq7sgFCeukL28HlOifIuPFz X-Received: from anyblade.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1791]) (user=mmaurer job=sendgmr) by 2002:a25:b120:0:b0:e22:624b:aea2 with SMTP id 3f1490d57ef6-e24d46b5e11mr4072276.0.1727307588565; Wed, 25 Sep 2024 16:39:48 -0700 (PDT) Date: Wed, 25 Sep 2024 23:38:27 +0000 In-Reply-To: <20240925233854.90072-1-mmaurer@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@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-13-mmaurer@google.com> Subject: [PATCH v5 12/16] module: Reformat struct for code style 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_Roy_Baron?= " , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Using commas to declare struct members makes adding new members to this struct not as nice with patch management. Signed-off-by: Matthew Maurer --- kernel/module/internal.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/kernel/module/internal.h b/kernel/module/internal.h index 2ebece8a789f..daef2be83902 100644 --- a/kernel/module/internal.h +++ b/kernel/module/internal.h @@ -80,7 +80,12 @@ struct load_info { unsigned int used_pages; #endif struct { - unsigned int sym, str, mod, vers, info, pcpu; + unsigned int sym; + unsigned int str; + unsigned int mod; + unsigned int vers; + unsigned int info; + unsigned int pcpu; } index; }; From patchwork Wed Sep 25 23:38:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Maurer X-Patchwork-Id: 13812606 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 CA39E1A4B8F for ; Wed, 25 Sep 2024 23:39:56 +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=1727307598; cv=none; b=njoLp5D2sUJc7XBrlrVdPJVwn06ezJQwPcN3nhH+hCOO78zxs/zgF4+5kNgz1rAXG01E+Sci7mgjR1USuMSQlAE5+3QQ5wQKNHWnt+YlORlbaAQ98jc6kuROiuxHqKeow+PMrTNpwhpYaHBpbHtqbAwecvEFXVGLivhATBKUd8s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307598; c=relaxed/simple; bh=KqzbIRZaiTcASB5/z7j1pBJp9u/nLEzrmBQfqdwdS+0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=r3xqYZwoacdnKcw/C8OiBD7TQRo63XKSDwvTE3ZhAGhmblIj8XLZQNX5Zk6kZFvWkA5SRFqoGG3geU+d6v9QwxFY2EYn0LNMMEytp6KxgfpXxHuzYO/C7i+KdY+veBQMh5vCVxwJGowu40GDk6uMiQZS8Dav8qtw4t4tniRHp6U= 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=oKP1Z8NY; 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="oKP1Z8NY" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6e230808464so6353447b3.3 for ; Wed, 25 Sep 2024 16:39:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727307596; x=1727912396; 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=c9tQH+085gbT8g6utnUHSGPzzhaq/BvRPx+qqG98o3U=; b=oKP1Z8NYzCW3wdrGLbSd0+z4YaAtfoT+55NhpDw9t6rLvl0jWbL70Dl2B/Kh5tmKYJ G2GupxkGy83tyRGyRSwtyT2FpBXLIt2wWabdNX8ecp8Q2pvr7ur+V5PCfEkE/+QQAfmD doEyyE0OPuqskrD0GERU3wJK2bYsEcRh+wphK+xOCG4PE7A5T8fic6AcJjgLfY1bIffK /CbN1lGPMw30iGanZXxYrQ0ALBD+BIzgPamajUHMsFR/lRfTlwLxJXbYLLPl0Dr0HhEc SppOLafq9h5SiDULiY5MytW1aEBXpqMqaP3MuoJrIfc3QQdNjcXXYfo484YrusBlvFOK t6YA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727307596; x=1727912396; 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=c9tQH+085gbT8g6utnUHSGPzzhaq/BvRPx+qqG98o3U=; b=PECtnsftjQfPrNnVeOQILJkp1w5/a7ZeEddP7DeeSxc3H64UbERG03pSsZP0znIncc PkzYALy9o5i1lpfNeF/GMmeKNVR6yKRypb5NNyK6QJJVNyjsoFLX19ae+X12PyNbG0OU SsDLAfUf6sb6epHbib2hRfbugeVYtYlxDKcA/xF4YjdwyIlfUYLPneKmTsA57U0a6aLX OuLfJeN/R2BbZPD/rNdiu8p1/Vi63ZNWiY1KIvDNn1qLXJhfff1JlbirVyWbVV1rPoj2 WQRU8mrFMPhtfPbcIes4ujhIc2BvMoYKzRRLZSwWQiPZxqnYMtptwgIVI27mPW2uOrGb gHvQ== X-Forwarded-Encrypted: i=1; AJvYcCWY4ozQyoaWSko0wCStWX2uWkuw+cdIEiu8AG89hdsWqUMAag25nA5NLBAUvLySaZL58TMj88taPbpY+m8=@vger.kernel.org X-Gm-Message-State: AOJu0YyW2W3930RYvNNQawgX8Va6KZMCASE9F0tSGoHegEssEeZ5Zl0i 6qmQZSz1yMWaRPNiCQe0z4/BejazQOqw1Gv8uNBFuXQ8wXOX7BmF7MCWofqDogYUstpERbgSfbA gSsfFag== X-Google-Smtp-Source: AGHT+IHf83nSsh8dSI5MT6Lq2qwz18kf/IKNLkJKmhvyxAcTbUnK8QftG83szzLWCAGb7csPDeD/aKPeenOu X-Received: from anyblade.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1791]) (user=mmaurer job=sendgmr) by 2002:a05:6902:118f:b0:e24:9584:52d3 with SMTP id 3f1490d57ef6-e24d71625d8mr82063276.2.1727307595844; Wed, 25 Sep 2024 16:39:55 -0700 (PDT) Date: Wed, 25 Sep 2024 23:38:28 +0000 In-Reply-To: <20240925233854.90072-1-mmaurer@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@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-14-mmaurer@google.com> Subject: [PATCH v5 13/16] export_report: Rehabilitate script From: Matthew Maurer To: masahiroy@kernel.org, ndesaulniers@google.com, ojeda@kernel.org, gary@garyguo.net, mcgrof@kernel.org, Alex Gaynor , Matthew Maurer 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, Boqun Feng , " =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= " , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross * modules.order has .o files when in a build dir, support this * .mod.c source layout has changed, update regexes to match * Add a stage 3, to be more robust against additional .mod.c content Signed-off-by: Matthew Maurer --- scripts/export_report.pl | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/scripts/export_report.pl b/scripts/export_report.pl index feb3d5542a62..dcef915405f3 100755 --- a/scripts/export_report.pl +++ b/scripts/export_report.pl @@ -55,6 +55,7 @@ sub collectcfiles { open my $fh, '< modules.order' or die "cannot open modules.order: $!\n"; while (<$fh>) { s/\.ko$/.mod.c/; + s/\.o$/.mod.c/; push (@file, $_) } close($fh); @@ -120,10 +121,14 @@ foreach my $thismod (@allcfiles) { next; } if ($state == 1) { - $state = 2 if ($_ =~ /__attribute__\(\(section\("__versions"\)\)\)/); + $state = 2 if ($_ =~ /__used __section\("__versions"\)/); next; } if ($state == 2) { + if ( $_ =~ /};/ ) { + $state = 3; + next; + } if ( $_ !~ /0x[0-9a-f]+,/ ) { next; } @@ -133,7 +138,7 @@ foreach my $thismod (@allcfiles) { push(@{$MODULE{$thismod}} , $sym); } } - if ($state != 2) { + if ($state != 3) { warn "WARNING:$thismod is not built with CONFIG_MODVERSIONS enabled\n"; $modversion_warnings++; } From patchwork Wed Sep 25 23:38:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Maurer X-Patchwork-Id: 13812607 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 7CC721A4B8A for ; Wed, 25 Sep 2024 23:40:05 +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=1727307607; cv=none; b=Oa9dZ0o19HsszPrvbJb4s9A4d9CPc8oyIV0i6eJfiPaT9sETlTUk1e47tHIJ/r8mIEV1snVPgHvwvv9qFd06j38zeynDyfpBXbk7SsmpJAu/rUm/F1jhpjdR3e+yCxoWqmyzby4vDgr+S/9nkmuqU6ptz/w43ReqDfklhHzsg0E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307607; c=relaxed/simple; bh=2eETH+bQv7kJed3DaM5YjE+D9ntyBg0gUQUR8ngWEMQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=iXXcn6wymTLx0H8u/6GGjwg2am91SAuJyIFVg8H6d0HOKrO/7mNXuI8UfhNAZPAQqiKTXbjTxRvGSN+ajK2YkS7S7Xh13dS54AJUhF4HyuEAbxPyP3YbfkGEIGHpO1hv+UmVoF812r03kXFK6W5e6jvYOa3ZqbKfpgVKjUiCjKE= 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=2iR2qP/y; 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="2iR2qP/y" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6e230808455so4558197b3.0 for ; Wed, 25 Sep 2024 16:40:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727307604; x=1727912404; 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=qRfAN3nQHQDKwo/EaN0h5CCoEGoDY7NA47N7/1nCHZE=; b=2iR2qP/yDlLLsNZtgkDXkX5S314JbEOa9P2kZKbM6mgkNtfEtDliK2hugHMY6j1YAp f6aZNxF/BoXMQBDKB4V/eAkNl7k/9cHNaDfl6ePmcC9VIPeGQEBALQB0Jjwl4FlHEu4o TXk0dld9TNCnw+Oa1lteSxKiPUrzlUuFBYs/zDih+00Hn4oWFzy60e87JFiw53cvySyy 3d/nahQYvG5yeLQ+0VYhoGQZKRByI97Ratb2NBW61hXWLadpJWF4Rj2XpG82jDkqhol+ xvUWRLO8S58lLA909Uj8HL9/CrorcWykb7lyjneiJOUmMnuqdjFqNelW/Av1ctgxZE4c gkiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727307604; x=1727912404; 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=qRfAN3nQHQDKwo/EaN0h5CCoEGoDY7NA47N7/1nCHZE=; b=fYFfSv6PZzyJiDMoL7TzSp4DN/9kIRWBmR2r/Vu7BWRuZ2cU6RN8QPksgxTKlJqBtm k7/xbV5HGv/sfaQFn1/U6AzZMVYedE0hboMMQspipo9zh/XN1tdkTgmb4FzeXG+lBkLH J3u6rOTFSp9IuhtIpuk7x5FCRYsE10IXxcuvFlcFaLmG2abE+CZe6y6qtL0gdFSAmxxw AN4QEk0ocWKJZLM1t8Comq6O+Vi32C4bMlYgBoNSKET7RTiM7bd5SNQ/NeDAPbM7EvVe 7HMGFhHrST/5lovwezu8p/zyiixO/Tf6OZ7r60rGsfUMa8K47M311KtlLVidD1CwAb2H w90g== X-Forwarded-Encrypted: i=1; AJvYcCVAZWz9+EWHwD73A/mb2wJ5M0/7xOKnuMSz+lX2GzzUMYpHoy0sVqe0Gq8te+vuazlJXwXUyQMoBL5hMBA=@vger.kernel.org X-Gm-Message-State: AOJu0YwSTgdzpG9j0AVRPCNI5XR3EHE2sdHp2r0Hm0Q/qStgs4kakhbz kqzOmpM9kM1C9uSbQENB/4nrrzNLMOlm42Ga+DJzG0P1lKTkHt0FfcYeog/diWs8eW2VeatrCax IL6gxxg== X-Google-Smtp-Source: AGHT+IGmuyyop25FpZ1x0NuUxB8uoOi/RaucBwgtD94Sl8/jMpFz75dImfgJY5lpsN7Qk8pZTSNyBUlZTG5H X-Received: from anyblade.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1791]) (user=mmaurer job=sendgmr) by 2002:a05:690c:20a7:b0:6e2:2c72:3abb with SMTP id 00721157ae682-6e22c723c48mr192977b3.7.1727307603320; Wed, 25 Sep 2024 16:40:03 -0700 (PDT) Date: Wed, 25 Sep 2024 23:38:29 +0000 In-Reply-To: <20240925233854.90072-1-mmaurer@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@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-15-mmaurer@google.com> Subject: [PATCH v5 14/16] modules: Support extended MODVERSIONS info From: Matthew Maurer To: masahiroy@kernel.org, ndesaulniers@google.com, ojeda@kernel.org, gary@garyguo.net, mcgrof@kernel.org, Michael Ellerman , Alex Gaynor , Benjamin Gray , Naveen N Rao , Matthew Maurer 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, Nicholas Piggin , Christophe Leroy , Madhavan Srinivasan , Petr Pavlu , Sami Tolvanen , Daniel Gomez , Boqun Feng , " =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= " , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , linuxppc-dev@lists.ozlabs.org Adds a new format for MODVERSIONS which stores each field in a separate ELF section. This initially adds support for variable length names, but could later be used to add additional fields to MODVERSIONS in a backwards compatible way if needed. Any new fields will be ignored by old user tooling, unlike the current format where user tooling cannot tolerate adjustments to the format (for example making the name field longer). Since PPC munges its version records to strip leading dots, we reproduce the munging for the new format. Other architectures do not appear to have architecture-specific usage of this information. Signed-off-by: Matthew Maurer --- arch/powerpc/kernel/module_64.c | 23 ++++++++- kernel/module/internal.h | 11 ++++ kernel/module/main.c | 92 ++++++++++++++++++++++++++++++--- kernel/module/version.c | 45 ++++++++++++++++ 4 files changed, 161 insertions(+), 10 deletions(-) diff --git a/arch/powerpc/kernel/module_64.c b/arch/powerpc/kernel/module_64.c index e9bab599d0c2..4e7b156dd8b2 100644 --- a/arch/powerpc/kernel/module_64.c +++ b/arch/powerpc/kernel/module_64.c @@ -355,6 +355,23 @@ static void dedotify_versions(struct modversion_info *vers, } } +static void dedotify_ext_version_names(char *str_seq, unsigned long size) +{ + unsigned long out = 0; + unsigned long in; + char last = '\0'; + + for (in = 0; in < size; in++) { + /* Skip one leading dot */ + if (last == '\0' && str_seq[in] == '.') + in++; + last = str_seq[in]; + str_seq[out++] = last; + } + /* Zero the trailing portion of the names table for robustness */ + memset(&str_seq[out], 0, size - out); +} + /* * Undefined symbols which refer to .funcname, hack to funcname. Make .TOC. * seem to be defined (value set later). @@ -424,10 +441,12 @@ int module_frob_arch_sections(Elf64_Ehdr *hdr, me->arch.toc_section = i; if (sechdrs[i].sh_addralign < 8) sechdrs[i].sh_addralign = 8; - } - else if (strcmp(secstrings+sechdrs[i].sh_name,"__versions")==0) + } else if (strcmp(secstrings + sechdrs[i].sh_name, "__versions") == 0) dedotify_versions((void *)hdr + sechdrs[i].sh_offset, sechdrs[i].sh_size); + else if (strcmp(secstrings + sechdrs[i].sh_name, "__version_ext_names") == 0) + dedotify_ext_version_names((void *)hdr + sechdrs[i].sh_offset, + sechdrs[i].sh_size); if (sechdrs[i].sh_type == SHT_SYMTAB) dedotify((void *)hdr + sechdrs[i].sh_offset, diff --git a/kernel/module/internal.h b/kernel/module/internal.h index daef2be83902..59959c21b205 100644 --- a/kernel/module/internal.h +++ b/kernel/module/internal.h @@ -86,6 +86,8 @@ struct load_info { unsigned int vers; unsigned int info; unsigned int pcpu; + unsigned int vers_ext_crc; + unsigned int vers_ext_name; } index; }; @@ -389,6 +391,15 @@ void module_layout(struct module *mod, struct modversion_info *ver, struct kerne struct kernel_symbol *ks, struct tracepoint * const *tp); int check_modstruct_version(const struct load_info *info, struct module *mod); int same_magic(const char *amagic, const char *bmagic, bool has_crcs); +struct modversion_info_ext { + size_t remaining; + const s32 *crc; + const char *name; +}; +void modversion_ext_start(const struct load_info *info, struct modversion_info_ext *ver); +void modversion_ext_advance(struct modversion_info_ext *ver); +#define for_each_modversion_info_ext(ver, info) \ + for (modversion_ext_start(info, &ver); ver.remaining > 0; modversion_ext_advance(&ver)) #else /* !CONFIG_MODVERSIONS */ static inline int check_version(const struct load_info *info, const char *symname, diff --git a/kernel/module/main.c b/kernel/module/main.c index b40b632f00a6..9a9feca344f8 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -2039,6 +2039,82 @@ static int elf_validity_cache_index_str(struct load_info *info) return 0; } +/** + * elf_validity_cache_index_versions() - Validate and cache version indices + * @info: Load info to cache version indices in. + * Must have &load_info->sechdrs and &load_info->secstrings populated. + * @flags: Load flags, relevant to suppress version loading, see + * uapi/linux/module.h + * + * If we're ignoring modversions based on @flags, zero all version indices + * and return validity. Othewrise check: + * + * * If "__version_ext_crcs" is present, "__version_ext_names" is present + * * There is a name present for every crc + * + * Then populate: + * + * * &load_info->index.vers + * * &load_info->index.vers_ext_crc + * * &load_info->index.vers_ext_names + * + * if present. + * + * Return: %0 if valid, %-ENOEXEC on failure. + */ +static int elf_validity_cache_index_versions(struct load_info *info, int flags) +{ + unsigned int vers_ext_crc; + unsigned int vers_ext_name; + size_t crc_count; + size_t remaining_len; + size_t name_size; + char *name; + + /* If modversions were suppressed, pretend we didn't find any */ + if (flags & MODULE_INIT_IGNORE_MODVERSIONS) { + info->index.vers = 0; + info->index.vers_ext_crc = 0; + info->index.vers_ext_name = 0; + return 0; + } + + vers_ext_crc = find_sec(info, "__version_ext_crcs"); + vers_ext_name = find_sec(info, "__version_ext_names"); + + /* If we have one field, we must have the other */ + if (!!vers_ext_crc != !!vers_ext_name) { + pr_err("extended version crc+name presence does not match"); + return -ENOEXEC; + } + + /* + * If we have extended version information, we should have the same + * number of entries in every section. + */ + if (vers_ext_crc) { + crc_count = info->sechdrs[vers_ext_crc].sh_size / sizeof(s32); + name = (void *)info->hdr + + info->sechdrs[vers_ext_name].sh_offset; + remaining_len = info->sechdrs[vers_ext_name].sh_size; + + while (crc_count--) { + name_size = strnlen(name, remaining_len) + 1; + if (name_size > remaining_len) { + pr_err("more extended version crcs than names"); + return -ENOEXEC; + } + remaining_len -= name_size; + name += name_size; + } + } + + info->index.vers = find_sec(info, "__versions"); + info->index.vers_ext_crc = vers_ext_crc; + info->index.vers_ext_name = vers_ext_name; + return 0; +} + /** * elf_validity_cache_index() - Resolve, validate, cache section indices * @info: Load info to read from and update. @@ -2053,9 +2129,7 @@ static int elf_validity_cache_index_str(struct load_info *info) * * elf_validity_cache_index_mod() * * elf_validity_cache_index_sym() * * elf_validity_cache_index_str() - * - * If versioning is not suppressed via flags, load the version index from - * a section called "__versions" with no validation. + * * elf_validity_cache_index_versions() * * If CONFIG_SMP is enabled, load the percpu section by name with no * validation. @@ -2078,11 +2152,9 @@ static int elf_validity_cache_index(struct load_info *info, int flags) err = elf_validity_cache_index_str(info); if (err < 0) return err; - - if (flags & MODULE_INIT_IGNORE_MODVERSIONS) - info->index.vers = 0; /* Pretend no __versions section! */ - else - info->index.vers = find_sec(info, "__versions"); + err = elf_validity_cache_index_versions(info, flags); + if (err < 0) + return err; info->index.pcpu = find_pcpusec(info); @@ -2293,6 +2365,10 @@ static int rewrite_section_headers(struct load_info *info, int flags) /* Track but don't keep modinfo and version sections. */ info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC; + info->sechdrs[info->index.vers_ext_crc].sh_flags &= + ~(unsigned long)SHF_ALLOC; + info->sechdrs[info->index.vers_ext_name].sh_flags &= + ~(unsigned long)SHF_ALLOC; info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC; return 0; diff --git a/kernel/module/version.c b/kernel/module/version.c index 53f43ac5a73e..c246d4087970 100644 --- a/kernel/module/version.c +++ b/kernel/module/version.c @@ -19,11 +19,28 @@ int check_version(const struct load_info *info, unsigned int versindex = info->index.vers; unsigned int i, num_versions; struct modversion_info *versions; + struct modversion_info_ext version_ext; /* Exporting module didn't supply crcs? OK, we're already tainted. */ if (!crc) return 1; + /* If we have extended version info, rely on it */ + if (info->index.vers_ext_crc) { + for_each_modversion_info_ext(version_ext, info) { + if (strcmp(version_ext.name, symname) != 0) + continue; + if (*version_ext.crc == *crc) + return 1; + pr_debug("Found checksum %X vs module %X\n", + *crc, *version_ext.crc); + goto bad_version; + } + pr_warn_once("%s: no extended symbol version for %s\n", + info->name, symname); + return 1; + } + /* No versions at all? modprobe --force does this. */ if (versindex == 0) return try_to_force_load(mod, symname) == 0; @@ -87,6 +104,34 @@ int same_magic(const char *amagic, const char *bmagic, return strcmp(amagic, bmagic) == 0; } +void modversion_ext_start(const struct load_info *info, + struct modversion_info_ext *start) +{ + unsigned int crc_idx = info->index.vers_ext_crc; + unsigned int name_idx = info->index.vers_ext_name; + Elf_Shdr *sechdrs = info->sechdrs; + + /* + * Both of these fields are needed for this to be useful + * Any future fields should be initialized to NULL if absent. + */ + if (crc_idx == 0 || name_idx == 0) { + start->remaining = 0; + return; + } + + start->crc = (const s32 *)sechdrs[crc_idx].sh_addr; + start->name = (const char *)sechdrs[name_idx].sh_addr; + start->remaining = sechdrs[crc_idx].sh_size / sizeof(*start->crc); +} + +void modversion_ext_advance(struct modversion_info_ext *vers) +{ + vers->remaining--; + vers->crc++; + vers->name += strlen(vers->name) + 1; +} + /* * Generate the signature for all relevant module structures here. * If these change, we don't want to try to parse the module. From patchwork Wed Sep 25 23:38:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Maurer X-Patchwork-Id: 13812608 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 C67481A4B8A for ; Wed, 25 Sep 2024 23:40:07 +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=1727307609; cv=none; b=BiiZqVE4hHmjNzhztRk/OzSgpWhuENFY0YY3qJMUn1iRTdWRugWMmZLBCicoP1kF52GplYApnbaJ0YReXSZ0ilssdFMMqwOow3E432XfiSRb6gEKGj4UY09vPn1tN+epLeDDgxOAjUUW7Y+aa/h16z6n/5tTDXUi6DvEYe5cLwY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307609; c=relaxed/simple; bh=HByXTJfGaacs4MV7ivXCvJq3lGnRepYcPD/jPTnE9OU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=kOZtkmHePqPezug1cjtjVbP38SFhpR8OGWiX+TyrNKU8f6VQ9NMTmdugGCDPaBDV2oQA7PlN9GlM5NmaDlmVZFokB2IjCo3C9RyyopTRHltEf5tUNGpZyGZ51vmzJNfUVw01YhCRPS2fCwGnbR+Ar4p1hDevnAqf4XP8ucfRpxE= 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=1SuUZMLC; 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="1SuUZMLC" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e25cf4e97aaso28316276.3 for ; Wed, 25 Sep 2024 16:40:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727307607; x=1727912407; 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=RglsluUoLriSL7HbUF8axxP+liCt7+SgicjK27E/sOo=; b=1SuUZMLCLU82vqpwJ2mBACpsNRBNdC6WVgRnSbUsvKFPxY7L7MPK+yD6KEXXfewbGy 2IlShZajn1rcm4+Adt4FxnGW3f8lAaVwWTOvCgLyz4lO80El+dumIJ5vxIPD6tmYdPmT IMI3YebIeq3Y99RT7LKLtANVjjuWVoW6yoISfA7j4SPLgTKRTU9wL08lQDX9W+vKXrhG Db6PoovDNizNI94JlyD676F7yGDuXQY2UQvVpdGALp+JbBfwrqrSEZMrivm/JTHAtx7t OcWJSv3aWbGd6GN2g4L7o9O40Yc1ANrd8kOlS5e3fr9INjBgQxOPt+Z5QkgtJ8wOA5Xm 7dYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727307607; x=1727912407; 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=RglsluUoLriSL7HbUF8axxP+liCt7+SgicjK27E/sOo=; b=IbhwSp3rh9F/j5m/JijB1uuFMxWTXKB50xZrAewYUsaoELcEeS/aY5TdR+kVea/IiW afHPKFNnmrq4Y/V8HiGuY3kZlMKxOWk1kQDCijr5qebGJVkm5OR5jAoGG0uMUp1H+sSt mKrsa/T4ERtkyC364KrcOwfpXiDOykRkMsc4ebU340baEjH6cYSPyl+y/xnjwFDI0Jl3 Qkfrgb+WBynyj682LoGA5fJOsI9JyAmRBeVg6CRPdRnAhkngDFvK+XP8Fz0P9A5oJKkg TlGhzj4PzsE3KcC8/z9XaL9xS+jrMItsVW24SQabSBMfkz/VYFlknoQgsJdpOV3Ac/8e 1mFA== X-Forwarded-Encrypted: i=1; AJvYcCWsArXNks1+GlNfzLC0X0Cz2q2rA9afD0hAq3YbevzRLR0dgeCypk2WLmZpDsbfeDeLtjcUwyjY1etH2PM=@vger.kernel.org X-Gm-Message-State: AOJu0YxCYpv7zDSRKmajjDx3Ldp93kP21UYXiqVi/hGkhjA5ihTGZFpP eizdGjZ7+DQ0ko2tcJYzQd28l/UwTV9vZLnSKktFoMZXgcaXX3ToCoIAmhAUpCfpq7TH4BZEcav h1Pk4dQ== X-Google-Smtp-Source: AGHT+IGASCvkEn2P1gDzoqsewJkQJHaAHK03+0AmWd/xawb1XFk5R/bQgX0n7dG9j+urvSvhJWBpQ1ZaL5iI X-Received: from anyblade.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1791]) (user=mmaurer job=sendgmr) by 2002:a05:6902:118f:b0:e24:9584:52d3 with SMTP id 3f1490d57ef6-e24d71625d8mr82065276.2.1727307606698; Wed, 25 Sep 2024 16:40:06 -0700 (PDT) Date: Wed, 25 Sep 2024 23:38:30 +0000 In-Reply-To: <20240925233854.90072-1-mmaurer@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@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-16-mmaurer@google.com> Subject: [PATCH v5 15/16] modpost: Produce extended modversion information 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 , Nathan Chancellor , Nicolas Schier , Boqun Feng , " =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= " , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Generate both the existing modversions format and the new extended one when running modpost. We no longer generate an error on long symbols in modpost, as they can now be appropriately encoded in the extended section. These symbols will be skipped in the previous encoding. Signed-off-by: Matthew Maurer --- scripts/mod/modpost.c | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c index 107393a8c48a..f8b7b793d2a2 100644 --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -1840,15 +1840,48 @@ static void add_versions(struct buffer *b, struct module *mod) continue; } if (strlen(s->name) >= MODULE_NAME_LEN) { - error("too long symbol \"%s\" [%s.ko]\n", - s->name, mod->name); - break; + /* this symbol will only be in the extended info */ + continue; } buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name); } buf_printf(b, "};\n"); + + buf_printf(b, "static const s32 ____version_ext_crcs[]\n"); + buf_printf(b, "__used __section(\"__version_ext_crcs\") = {\n"); + list_for_each_entry(s, &mod->unresolved_symbols, list) { + if (!s->module) + continue; + if (!s->crc_valid) { + /* + * We already warned on this when producing the legacy + * modversions table. + */ + continue; + } + buf_printf(b, "\t%#8x,\n", s->crc); + } + buf_printf(b, "};\n"); + + buf_printf(b, "static const char ____version_ext_names[]\n"); + buf_printf(b, "__used __section(\"__version_ext_names\") =\n"); + list_for_each_entry(s, &mod->unresolved_symbols, list) { + if (!s->module) + continue; + if (!s->crc_valid) { + /* + * We already warned on this when producing the legacy + * modversions table. + * We need to skip its name too, as the indexes in + * both tables need to align. + */ + continue; + } + buf_printf(b, "\t\"%s\\0\"\n", s->name); + } + buf_printf(b, ";\n"); } static void add_depends(struct buffer *b, struct module *mod) From patchwork Wed Sep 25 23:38:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Maurer X-Patchwork-Id: 13812609 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.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 6D81B1AED55 for ; Wed, 25 Sep 2024 23:40:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307616; cv=none; b=giiJV5n9NvlwHAEGJYMWo6e+Shz7tF67n04Gn9VrS2q7NCljdktiYWZWRo4dwkitDsol/NG2eAYn4LXg5GvD3i0M8bzezXsMUydXAE7n62IWjlJEkb7t98lTVj7moK3waxsmtJrZ5TnoAc8f0RIW4fkzoGHEV3O+RDnUyV4bLWA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307616; c=relaxed/simple; bh=CxcOGYVl0RHNxURp1Y+1xpXbMOZLvm56O9AP4mZATd8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=fHTqB59ddl84fCZriN42Km7Mk80h7SWUNx+5NOXvJ9mGVTG+l8dZ4nWTKeq/p+U5vxg0LK35fUNfJ87wDPmrJ6aVsiPU3d1zVTsiItYUiKyloEoXzzU7q2dJMutqE8kj1Onh1lCqz/xadwNTI/TEhG3slt0ZdpkSIiLTuTit0DU= 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=aRV+NzUG; arc=none smtp.client-ip=209.85.219.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="aRV+NzUG" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e1159fb161fso624537276.1 for ; Wed, 25 Sep 2024 16:40:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727307614; x=1727912414; 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=WplRKYGF+6Vdtojy3WxnxZnhza95+GyPbQDFLT+DHQA=; b=aRV+NzUGcQL4/ON7uIiCHk8jIbKQL9jQ5QyJkiVpL7vm2kJlCNj8gzPruXe8klTwfl Zi+BHkx2vbPtz78f4TDt5anS2Wb6eEdqIdUyvVzVfcbpCj5vW7Ytkrmb0NHShIm4LFif mq9PTj2fjOJp25iZVq0OpBa/MHbgZ1+rkgh8eMPwdH+Pl/ucPhCprx30plQsooIQxkTK rev4RuTRDiegtTCAXbdUBKvwwXR25AMOo+wD7W/JQYl6j2ObteqKMNwyBCxh9Uddo0yT zi+UVJj3nm7Lnk8rCO571Jzwo1xeSNreuGNM47rKJH6Y/gJqnbLPJFnHp298w9R+Hau8 kKrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727307614; x=1727912414; 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=WplRKYGF+6Vdtojy3WxnxZnhza95+GyPbQDFLT+DHQA=; b=HWk6GlrQdkwrnEMFynIjpbvbu7RKeXqj63O7P9osNHl6e3N2beWkEvu4TmYeerk5C2 c44O25Vzf2WMVzR/HR0L+u83fORl5nCLGzP7BMR09rMpT7il95rHp3zlHxEzrw4dPOqi /mw0yVU9+KPrj0d6vcvsYFRYfP3z2tbyI//iD6XRJVk9mTenOFdNjtdb5BVrqHjsgqXy gk8IBgd5gNr9QNhAyUVj+0dFry5ehxvYckvJfJJ4i6sVVAaB+1ziQtlOjINdDTPYP+wb iUjws6EDG/k2JW00K+86OzUlAskIkXQdTvNDjWJWJgc6BO94/CL6uEh3xC7QF3dRQVKQ pYnA== X-Forwarded-Encrypted: i=1; AJvYcCU7RHy/xtIYpklaKkhS4gytOm+vyfyHbJivAvIY30JrGelpJUQO+IiI4zIepPmYpGUXQNkWT7zDoa1UZ/E=@vger.kernel.org X-Gm-Message-State: AOJu0Yx+Zkf8gNes1CBjISrRwJnkNt+9PPgcihssHatSGVQXi1npM24Z 6YfM9/WviUPar3KHwE9ZHwWc2EoF6yZVd9PvVqGZaV4elPhi6BjY4COMvRnz0BiDcSvyivH3gcm 2FzHugw== X-Google-Smtp-Source: AGHT+IEf40pL03JKKHoArutuPYYFcfXs6+lqlBIsFYP4JGQ4Mtdii+GrGo8PKjLJPO4DM/u/M2ebjWWjQD3t 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-e24d44cad51mr4791276.0.1727307613856; Wed, 25 Sep 2024 16:40:13 -0700 (PDT) Date: Wed, 25 Sep 2024 23:38:31 +0000 In-Reply-To: <20240925233854.90072-1-mmaurer@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@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-17-mmaurer@google.com> Subject: [PATCH v5 16/16] export_report: Use new version info format From: Matthew Maurer To: masahiroy@kernel.org, ndesaulniers@google.com, ojeda@kernel.org, gary@garyguo.net, mcgrof@kernel.org, Alex Gaynor , Matthew Maurer 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, Boqun Feng , " =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= " , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross The new version info format has a superset of symbols in the old format. Since this is a tool for in-tree modules, we don't need to parse the old one with this tool any longer. Signed-off-by: Matthew Maurer --- scripts/export_report.pl | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/scripts/export_report.pl b/scripts/export_report.pl index dcef915405f3..6a37df6f947f 100755 --- a/scripts/export_report.pl +++ b/scripts/export_report.pl @@ -114,31 +114,29 @@ foreach my $thismod (@allcfiles) { } my $state=0; + # State map: + # 0 - Looking for names + # 1 - Scanning names + # 2 - Done while ( <$module> ) { chomp; if ($state == 0) { - $state = 1 if ($_ =~ /static const struct modversion_info/); + $state = 1 if ($_ =~ /__used __section\("__version_ext_names"\)/); next; } if ($state == 1) { - $state = 2 if ($_ =~ /__used __section\("__versions"\)/); - next; - } - if ($state == 2) { - if ( $_ =~ /};/ ) { - $state = 3; - next; - } - if ( $_ !~ /0x[0-9a-f]+,/ ) { + if ( $_ =~ /;/ ) { + $state = 2; next; } - my $sym = (split /([,"])/,)[4]; + $_ =~ /"(.*)\\0"/; + my $sym = $1; my ($module, $value, $symbol, $gpl) = @{$SYMBOL{$sym}}; $SYMBOL{ $sym } = [ $module, $value+1, $symbol, $gpl]; push(@{$MODULE{$thismod}} , $sym); } } - if ($state != 3) { + if ($state != 2) { warn "WARNING:$thismod is not built with CONFIG_MODVERSIONS enabled\n"; $modversion_warnings++; }