]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
module: Factor out elf_validity_cache_index_str
authorMatthew Maurer <mmaurer@google.com>
Tue, 15 Oct 2024 23:16:42 +0000 (23:16 +0000)
committerLuis Chamberlain <mcgrof@kernel.org>
Sat, 19 Oct 2024 21:35:07 +0000 (14:35 -0700)
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>
Reviewed-by: Sami Tolvanen <samitolvanen@google.com>
Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>
kernel/module/main.c

index 6be58b0a6468cd07535aeb01b933bdc5c20cce3b..43140475aac00ea8a7c5c0051db2604d4f221ed4 100644 (file)
@@ -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. */