@@ -2014,6 +2014,31 @@ static int elf_validity_cache_index_sym(struct load_info *info)
20142014 return 0 ;
20152015}
20162016
2017+ /**
2018+ * elf_validity_cache_index_str() - Validate and cache strtab index
2019+ * @info: Load info to cache strtab index in.
2020+ * Must have &load_info->sechdrs and &load_info->secstrings populated.
2021+ * Must have &load_info->index.sym populated.
2022+ *
2023+ * Looks at the symbol table's associated string table, makes sure it is
2024+ * in-bounds, and caches it.
2025+ *
2026+ * Return: %0 if valid, %-ENOEXEC on failure.
2027+ */
2028+ static int elf_validity_cache_index_str (struct load_info * info )
2029+ {
2030+ unsigned int str_idx = info -> sechdrs [info -> index .sym ].sh_link ;
2031+
2032+ if (str_idx == SHN_UNDEF || str_idx >= info -> hdr -> e_shnum ) {
2033+ pr_err ("Invalid ELF sh_link!=SHN_UNDEF(%d) or (sh_link(%d) >= hdr->e_shnum(%d)\n" ,
2034+ str_idx , str_idx , info -> hdr -> e_shnum );
2035+ return - ENOEXEC ;
2036+ }
2037+
2038+ info -> index .str = str_idx ;
2039+ return 0 ;
2040+ }
2041+
20172042/*
20182043 * Check userspace passed ELF module against our expectations, and cache
20192044 * useful variables for further processing as we go.
@@ -2037,7 +2062,6 @@ static int elf_validity_cache_index_sym(struct load_info *info)
20372062static int elf_validity_cache_copy (struct load_info * info , int flags )
20382063{
20392064 int err ;
2040- int str_idx ;
20412065
20422066 err = elf_validity_cache_sechdrs (info );
20432067 if (err < 0 )
@@ -2054,16 +2078,11 @@ static int elf_validity_cache_copy(struct load_info *info, int flags)
20542078 err = elf_validity_cache_index_sym (info );
20552079 if (err < 0 )
20562080 return err ;
2057-
2058- str_idx = info -> sechdrs [info -> index .sym ].sh_link ;
2059- if (str_idx == SHN_UNDEF || str_idx >= info -> hdr -> e_shnum ) {
2060- pr_err ("Invalid ELF sh_link!=SHN_UNDEF(%d) or (sh_link(%d) >= hdr->e_shnum(%d)\n" ,
2061- str_idx , str_idx , info -> hdr -> e_shnum );
2062- return - ENOEXEC ;
2063- }
2081+ err = elf_validity_cache_index_str (info );
2082+ if (err < 0 )
2083+ return err ;
20642084
20652085 /* Sets internal strings. */
2066- info -> index .str = str_idx ;
20672086 info -> strtab = (char * )info -> hdr + info -> sechdrs [info -> index .str ].sh_offset ;
20682087
20692088 /* This is temporary: point mod into copy of data. */
0 commit comments