From: Matthew Maurer <mmaurer@google.com>
To: mcgrof@kernel.org
Cc: linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org,
samitolvanen@google.com, Matthew Maurer <mmaurer@google.com>,
Petr Pavlu <petr.pavlu@suse.com>,
Daniel Gomez <da.gomez@samsung.com>
Subject: [PATCH 08/12] module: Factor out elf_validity_cache_index_str
Date: Tue, 15 Oct 2024 23:16:42 +0000 [thread overview]
Message-ID: <20241015231651.3851138-9-mmaurer@google.com> (raw)
In-Reply-To: <20241015231651.3851138-1-mmaurer@google.com>
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 <mmaurer@google.com>
---
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 955746649f37..a6bed293d97b 100644
--- a/kernel/module/main.c
+++ b/kernel/module/main.c
@@ -2047,6 +2047,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.
@@ -2070,7 +2095,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)
@@ -2087,16 +2111,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. */
--
2.47.0.rc1.288.g06298d1525-goog
next prev parent reply other threads:[~2024-10-15 23:17 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-15 23:16 [PATCH 00/12] Module Validation Refactor Matthew Maurer
2024-10-15 23:16 ` [PATCH 01/12] module: Take const arg in validate_section_offset Matthew Maurer
2024-10-15 23:16 ` [PATCH 02/12] module: Factor out elf_validity_ehdr Matthew Maurer
2024-10-15 23:16 ` [PATCH 03/12] module: Factor out elf_validity_cache_sechdrs Matthew Maurer
2024-10-15 23:16 ` [PATCH 04/12] module: Factor out elf_validity_cache_secstrings Matthew Maurer
2024-10-15 23:16 ` [PATCH 05/12] module: Factor out elf_validity_cache_index_info Matthew Maurer
2024-10-15 23:16 ` [PATCH 06/12] module: Factor out elf_validity_cache_index_mod Matthew Maurer
2024-10-15 23:16 ` [PATCH 07/12] module: Factor out elf_validity_cache_index_sym Matthew Maurer
2024-10-15 23:16 ` Matthew Maurer [this message]
2024-10-15 23:16 ` [PATCH 09/12] module: Group section index calculations together Matthew Maurer
2024-10-15 23:16 ` [PATCH 10/12] module: Factor out elf_validity_cache_strtab Matthew Maurer
2024-10-15 23:16 ` [PATCH 11/12] module: Additional validation in elf_validity_cache_strtab Matthew Maurer
2024-10-15 23:16 ` [PATCH 12/12] module: Reformat struct for code style Matthew Maurer
2024-10-17 0:44 ` [PATCH 00/12] Module Validation Refactor Sami Tolvanen
2024-10-19 22:10 ` Luis Chamberlain
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20241015231651.3851138-9-mmaurer@google.com \
--to=mmaurer@google.com \
--cc=da.gomez@samsung.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-modules@vger.kernel.org \
--cc=mcgrof@kernel.org \
--cc=petr.pavlu@suse.com \
--cc=samitolvanen@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®