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_mod

Centralize .gnu.linkonce.this_module 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
0be41a93 fbc0e4e4

+67 -62
+67 -62
kernel/module/main.c
··· 1919 1919 return 0; 1920 1920 } 1921 1921 1922 + /** 1923 + * elf_validity_cache_index_mod() - Validates and caches this_module section 1924 + * @info: Load info to cache this_module on. 1925 + * Must have &load_info->sechdrs and &load_info->secstrings populated 1926 + * 1927 + * The ".gnu.linkonce.this_module" ELF section is special. It is what modpost 1928 + * uses to refer to __this_module and let's use rely on THIS_MODULE to point 1929 + * to &__this_module properly. The kernel's modpost declares it on each 1930 + * modules's *.mod.c file. If the struct module of the kernel changes a full 1931 + * kernel rebuild is required. 1932 + * 1933 + * We have a few expectations for this special section, this function 1934 + * validates all this for us: 1935 + * 1936 + * * The section has contents 1937 + * * The section is unique 1938 + * * We expect the kernel to always have to allocate it: SHF_ALLOC 1939 + * * The section size must match the kernel's run time's struct module 1940 + * size 1941 + * 1942 + * If all checks pass, the index will be cached in &load_info->index.mod 1943 + * 1944 + * Return: %0 on validation success, %-ENOEXEC on failure 1945 + */ 1946 + static int elf_validity_cache_index_mod(struct load_info *info) 1947 + { 1948 + Elf_Shdr *shdr; 1949 + int mod_idx; 1950 + 1951 + mod_idx = find_any_unique_sec(info, ".gnu.linkonce.this_module"); 1952 + if (mod_idx <= 0) { 1953 + pr_err("module %s: Exactly one .gnu.linkonce.this_module section must exist.\n", 1954 + info->name ?: "(missing .modinfo section or name field)"); 1955 + return -ENOEXEC; 1956 + } 1957 + 1958 + shdr = &info->sechdrs[mod_idx]; 1959 + 1960 + if (shdr->sh_type == SHT_NOBITS) { 1961 + pr_err("module %s: .gnu.linkonce.this_module section must have a size set\n", 1962 + info->name ?: "(missing .modinfo section or name field)"); 1963 + return -ENOEXEC; 1964 + } 1965 + 1966 + if (!(shdr->sh_flags & SHF_ALLOC)) { 1967 + pr_err("module %s: .gnu.linkonce.this_module must occupy memory during process execution\n", 1968 + info->name ?: "(missing .modinfo section or name field)"); 1969 + return -ENOEXEC; 1970 + } 1971 + 1972 + if (shdr->sh_size != sizeof(struct module)) { 1973 + pr_err("module %s: .gnu.linkonce.this_module section size must match the kernel's built struct module size at run time\n", 1974 + info->name ?: "(missing .modinfo section or name field)"); 1975 + return -ENOEXEC; 1976 + } 1977 + 1978 + info->index.mod = mod_idx; 1979 + 1980 + return 0; 1981 + } 1982 + 1983 + 1922 1984 /* 1923 1985 * Check userspace passed ELF module against our expectations, and cache 1924 1986 * useful variables for further processing as we go. ··· 2006 1944 unsigned int i; 2007 1945 Elf_Shdr *shdr; 2008 1946 int err; 2009 - unsigned int num_mod_secs = 0, mod_idx; 2010 1947 unsigned int num_sym_secs = 0, sym_idx; 2011 1948 2012 1949 err = elf_validity_cache_sechdrs(info); ··· 2017 1956 err = elf_validity_cache_index_info(info); 2018 1957 if (err < 0) 2019 1958 return err; 1959 + err = elf_validity_cache_index_mod(info); 1960 + if (err < 0) 1961 + return err; 2020 1962 2021 1963 for (i = 1; i < info->hdr->e_shnum; i++) { 2022 1964 shdr = &info->sechdrs[i]; 2023 - switch (shdr->sh_type) { 2024 - case SHT_NULL: 2025 - case SHT_NOBITS: 2026 - continue; 2027 - case SHT_SYMTAB: 1965 + if (shdr->sh_type == SHT_SYMTAB) { 2028 1966 if (shdr->sh_link == SHN_UNDEF 2029 1967 || shdr->sh_link >= info->hdr->e_shnum) { 2030 1968 pr_err("Invalid ELF sh_link!=SHN_UNDEF(%d) or (sh_link(%d) >= hdr->e_shnum(%d)\n", ··· 2033 1973 } 2034 1974 num_sym_secs++; 2035 1975 sym_idx = i; 2036 - fallthrough; 2037 - default: 2038 - if (strcmp(info->secstrings + shdr->sh_name, 2039 - ".gnu.linkonce.this_module") == 0) { 2040 - num_mod_secs++; 2041 - mod_idx = i; 2042 - } 2043 - break; 2044 1976 } 2045 1977 } 2046 1978 ··· 2048 1996 info->index.str = shdr->sh_link; 2049 1997 info->strtab = (char *)info->hdr + info->sechdrs[info->index.str].sh_offset; 2050 1998 2051 - /* 2052 - * The ".gnu.linkonce.this_module" ELF section is special. It is 2053 - * what modpost uses to refer to __this_module and let's use rely 2054 - * on THIS_MODULE to point to &__this_module properly. The kernel's 2055 - * modpost declares it on each modules's *.mod.c file. If the struct 2056 - * module of the kernel changes a full kernel rebuild is required. 2057 - * 2058 - * We have a few expectaions for this special section, the following 2059 - * code validates all this for us: 2060 - * 2061 - * o Only one section must exist 2062 - * o We expect the kernel to always have to allocate it: SHF_ALLOC 2063 - * o The section size must match the kernel's run time's struct module 2064 - * size 2065 - */ 2066 - if (num_mod_secs != 1) { 2067 - pr_err("module %s: Only one .gnu.linkonce.this_module section must exist.\n", 2068 - info->name ?: "(missing .modinfo section or name field)"); 2069 - goto no_exec; 2070 - } 2071 - 2072 - shdr = &info->sechdrs[mod_idx]; 2073 - 2074 - /* 2075 - * This is already implied on the switch above, however let's be 2076 - * pedantic about it. 2077 - */ 2078 - if (shdr->sh_type == SHT_NOBITS) { 2079 - pr_err("module %s: .gnu.linkonce.this_module section must have a size set\n", 2080 - info->name ?: "(missing .modinfo section or name field)"); 2081 - goto no_exec; 2082 - } 2083 - 2084 - if (!(shdr->sh_flags & SHF_ALLOC)) { 2085 - pr_err("module %s: .gnu.linkonce.this_module must occupy memory during process execution\n", 2086 - info->name ?: "(missing .modinfo section or name field)"); 2087 - goto no_exec; 2088 - } 2089 - 2090 - if (shdr->sh_size != sizeof(struct module)) { 2091 - pr_err("module %s: .gnu.linkonce.this_module section size must match the kernel's built struct module size at run time\n", 2092 - info->name ?: "(missing .modinfo section or name field)"); 2093 - goto no_exec; 2094 - } 2095 - 2096 - info->index.mod = mod_idx; 2097 - 2098 1999 /* This is temporary: point mod into copy of data. */ 2099 - info->mod = (void *)info->hdr + shdr->sh_offset; 2000 + info->mod = (void *)info->hdr + info->sechdrs[info->index.mod].sh_offset; 2100 2001 2101 2002 /* 2102 2003 * If we didn't load the .modinfo 'name' field earlier, fall back to