Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

module: Factor out elf_validity_cache_index_sym

Centralize symbol table detection and property validation.

Signed-off-by: Matthew Maurer <mmaurer@google.com>
Reviewed-by: Sami Tolvanen <samitolvanen@google.com>
Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

authored by

Matthew Maurer and committed by
Luis Chamberlain
9bd4982c 0be41a93

+44 -29
+44 -29
kernel/module/main.c
··· 1980 1980 return 0; 1981 1981 } 1982 1982 1983 + /** 1984 + * elf_validity_cache_index_sym() - Validate and cache symtab index 1985 + * @info: Load info to cache symtab index in. 1986 + * Must have &load_info->sechdrs and &load_info->secstrings populated. 1987 + * 1988 + * Checks that there is exactly one symbol table, then caches its index in 1989 + * &load_info->index.sym. 1990 + * 1991 + * Return: %0 if valid, %-ENOEXEC on failure. 1992 + */ 1993 + static int elf_validity_cache_index_sym(struct load_info *info) 1994 + { 1995 + unsigned int sym_idx; 1996 + unsigned int num_sym_secs = 0; 1997 + int i; 1998 + 1999 + for (i = 1; i < info->hdr->e_shnum; i++) { 2000 + if (info->sechdrs[i].sh_type == SHT_SYMTAB) { 2001 + num_sym_secs++; 2002 + sym_idx = i; 2003 + } 2004 + } 2005 + 2006 + if (num_sym_secs != 1) { 2007 + pr_warn("%s: module has no symbols (stripped?)\n", 2008 + info->name ?: "(missing .modinfo section or name field)"); 2009 + return -ENOEXEC; 2010 + } 2011 + 2012 + info->index.sym = sym_idx; 2013 + 2014 + return 0; 2015 + } 1983 2016 1984 2017 /* 1985 2018 * Check userspace passed ELF module against our expectations, and cache ··· 2036 2003 */ 2037 2004 static int elf_validity_cache_copy(struct load_info *info, int flags) 2038 2005 { 2039 - unsigned int i; 2040 - Elf_Shdr *shdr; 2041 2006 int err; 2042 - unsigned int num_sym_secs = 0, sym_idx; 2007 + int str_idx; 2043 2008 2044 2009 err = elf_validity_cache_sechdrs(info); 2045 2010 if (err < 0) ··· 2051 2020 err = elf_validity_cache_index_mod(info); 2052 2021 if (err < 0) 2053 2022 return err; 2023 + err = elf_validity_cache_index_sym(info); 2024 + if (err < 0) 2025 + return err; 2054 2026 2055 - for (i = 1; i < info->hdr->e_shnum; i++) { 2056 - shdr = &info->sechdrs[i]; 2057 - if (shdr->sh_type == SHT_SYMTAB) { 2058 - if (shdr->sh_link == SHN_UNDEF 2059 - || shdr->sh_link >= info->hdr->e_shnum) { 2060 - pr_err("Invalid ELF sh_link!=SHN_UNDEF(%d) or (sh_link(%d) >= hdr->e_shnum(%d)\n", 2061 - shdr->sh_link, shdr->sh_link, 2062 - info->hdr->e_shnum); 2063 - goto no_exec; 2064 - } 2065 - num_sym_secs++; 2066 - sym_idx = i; 2067 - } 2027 + str_idx = info->sechdrs[info->index.sym].sh_link; 2028 + if (str_idx == SHN_UNDEF || str_idx >= info->hdr->e_shnum) { 2029 + pr_err("Invalid ELF sh_link!=SHN_UNDEF(%d) or (sh_link(%d) >= hdr->e_shnum(%d)\n", 2030 + str_idx, str_idx, info->hdr->e_shnum); 2031 + return -ENOEXEC; 2068 2032 } 2069 2033 2070 - if (num_sym_secs != 1) { 2071 - pr_warn("%s: module has no symbols (stripped?)\n", 2072 - info->name ?: "(missing .modinfo section or name field)"); 2073 - goto no_exec; 2074 - } 2075 - 2076 - /* Sets internal symbols and strings. */ 2077 - info->index.sym = sym_idx; 2078 - shdr = &info->sechdrs[sym_idx]; 2079 - info->index.str = shdr->sh_link; 2034 + /* Sets internal strings. */ 2035 + info->index.str = str_idx; 2080 2036 info->strtab = (char *)info->hdr + info->sechdrs[info->index.str].sh_offset; 2081 2037 2082 2038 /* This is temporary: point mod into copy of data. */ ··· 2084 2066 info->index.pcpu = find_pcpusec(info); 2085 2067 2086 2068 return 0; 2087 - 2088 - no_exec: 2089 - return -ENOEXEC; 2090 2069 } 2091 2070 2092 2071 #define COPY_CHUNK_SIZE (16*PAGE_SIZE)