FROMGIT: module: Factor out elf_validity_cache_index_sym
Centralize symbol table detection and property validation. Change-Id: I0d570d317fff30923031f5381ab71741fb0b4b2e Signed-off-by: Matthew Maurer <mmaurer@google.com> Reviewed-by: Sami Tolvanen <samitolvanen@google.com> Signed-off-by: Luis Chamberlain <mcgrof@kernel.org> (cherry picked from commit 9bd4982cf7d65f4c9e0793d5a8fda6ad838e8554 kernel/git/modules/linux.git modules-next) Bug: 347787665
This commit is contained in:
committed by
Matthias Männich
parent
ffaca6ab0a
commit
d487c4ca16
+44
-29
@@ -2016,6 +2016,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
|
||||
@@ -2039,10 +2072,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)
|
||||
@@ -2054,34 +2085,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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/* 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. */
|
||||
@@ -2102,9 +2120,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)
|
||||
|
||||
Reference in New Issue
Block a user