Pull module updates from Jessica Yu: - Harden CONFIG_STRICT_MODULE_RWX by rejecting any module that has SHF_WRITE|SHF_EXECINSTR sections - Remove and clean up nested #ifdefs, as it makes code hard to read * tag 'modules-for-v5.8' of git://git.kernel.org/pub/scm/linux/kernel/git/jeyu/linux: module: Harden STRICT_MODULE_RWX module: break nested ARCH_HAS_STRICT_MODULE_RWX and STRICT_MODULE_RWX #ifdefs
Unified · Split
| @@ -1946,7 +1946,6 @@ static void mod_sysfs_teardown(struct module *mod) · 1945 unmodified lines | |||
| 1946 | mod_sysfs_fini(mod); | 1946 | mod_sysfs_fini(mod); |
| 1947 | } | 1947 | } |
| 1948 | 1948 | ||
| 1949 | #ifdef CONFIG_ARCH_HAS_STRICT_MODULE_RWX | ||
| 1950 | /* | 1949 | /* |
| 1951 | * LKM RO/NX protection: protect module's text/ro-data | 1950 | * LKM RO/NX protection: protect module's text/ro-data |
| 1952 | * from modification and any data from execution. | 1951 | * from modification and any data from execution. |
| @@ -1960,6 +1959,14 @@ static void mod_sysfs_teardown(struct module *mod) · 7 unmodified lines | |||
| 1960 | * | 1959 | * |
| 1961 | * These values are always page-aligned (as is base) | 1960 | * These values are always page-aligned (as is base) |
| 1962 | */ | 1961 | */ |
| 1962 | |||
| 1963 | /* | ||
| 1964 | * Since some arches are moving towards PAGE_KERNEL module allocations instead | ||
| 1965 | * of PAGE_KERNEL_EXEC, keep frob_text() and module_enable_x() outside of the | ||
| 1966 | * CONFIG_STRICT_MODULE_RWX block below because they are needed regardless of | ||
| 1967 | * whether we are strict. | ||
| 1968 | */ | ||
| 1969 | #ifdef CONFIG_ARCH_HAS_STRICT_MODULE_RWX | ||
| 1963 | static void frob_text(const struct module_layout *layout, | 1970 | static void frob_text(const struct module_layout *layout, |
| 1964 | int (*set_memory)(unsigned long start, int num_pages)) | 1971 | int (*set_memory)(unsigned long start, int num_pages)) |
| 1965 | { | 1972 | { |
| @@ -1969,6 +1976,15 @@ static void frob_text(const struct module_layout *layout, · 3 unmodified lines | |||
| 1969 | layout->text_size >> PAGE_SHIFT); | 1976 | layout->text_size >> PAGE_SHIFT); |
| 1970 | } | 1977 | } |
| 1971 | 1978 | ||
| 1979 | static void module_enable_x(const struct module *mod) | ||
| 1980 | { | ||
| 1981 | frob_text(&mod->core_layout, set_memory_x); | ||
| 1982 | frob_text(&mod->init_layout, set_memory_x); | ||
| 1983 | } | ||
| 1984 | #else /* !CONFIG_ARCH_HAS_STRICT_MODULE_RWX */ | ||
| 1985 | static void module_enable_x(const struct module *mod) { } | ||
| 1986 | #endif /* CONFIG_ARCH_HAS_STRICT_MODULE_RWX */ | ||
| 1987 | |||
| 1972 | #ifdef CONFIG_STRICT_MODULE_RWX | 1988 | #ifdef CONFIG_STRICT_MODULE_RWX |
| 1973 | static void frob_rodata(const struct module_layout *layout, | 1989 | static void frob_rodata(const struct module_layout *layout, |
| 1974 | int (*set_memory)(unsigned long start, int num_pages)) | 1990 | int (*set_memory)(unsigned long start, int num_pages)) |
| @@ -2026,20 +2042,29 @@ static void module_enable_nx(const struct module *mod) · 51 unmodified lines | |||
| 2026 | frob_writable_data(&mod->init_layout, set_memory_nx); | 2042 | frob_writable_data(&mod->init_layout, set_memory_nx); |
| 2027 | } | 2043 | } |
| 2028 | 2044 | ||
| 2045 | static int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs, | ||
| 2046 | char *secstrings, struct module *mod) | ||
| 2047 | { | ||
| 2048 | const unsigned long shf_wx = SHF_WRITE|SHF_EXECINSTR; | ||
| 2049 | int i; | ||
| 2050 | |||
| 2051 | for (i = 0; i < hdr->e_shnum; i++) { | ||
| 2052 | if ((sechdrs[i].sh_flags & shf_wx) == shf_wx) | ||
| 2053 | return -ENOEXEC; | ||
| 2054 | } | ||
| 2055 | |||
| 2056 | return 0; | ||
| 2057 | } | ||
| 2058 | |||
| 2029 | #else /* !CONFIG_STRICT_MODULE_RWX */ | 2059 | #else /* !CONFIG_STRICT_MODULE_RWX */ |
| 2030 | static void module_enable_nx(const struct module *mod) { } | 2060 | static void module_enable_nx(const struct module *mod) { } |
| 2031 | static void module_enable_ro(const struct module *mod, bool after_init) {} | 2061 | static void module_enable_ro(const struct module *mod, bool after_init) {} |
| 2032 | #endif /* CONFIG_STRICT_MODULE_RWX */ | 2062 | static int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs, |
| 2033 | static void module_enable_x(const struct module *mod) | 2063 | char *secstrings, struct module *mod) |
| 2034 | { | 2064 | { |
| 2035 | frob_text(&mod->core_layout, set_memory_x); | 2065 | return 0; |
| 2036 | frob_text(&mod->init_layout, set_memory_x); | ||
| 2037 | } | 2066 | } |
| 2038 | #else /* !CONFIG_ARCH_HAS_STRICT_MODULE_RWX */ | 2067 | #endif /* CONFIG_STRICT_MODULE_RWX */ |
| 2039 | static void module_enable_nx(const struct module *mod) { } | ||
| 2040 | static void module_enable_x(const struct module *mod) { } | ||
| 2041 | #endif /* CONFIG_ARCH_HAS_STRICT_MODULE_RWX */ | ||
| 2042 | |||
| 2043 | 2068 | ||
| 2044 | #ifdef CONFIG_LIVEPATCH | 2069 | #ifdef CONFIG_LIVEPATCH |
| 2045 | /* | 2070 | /* |
| @@ -3385,6 +3410,11 @@ static struct module *layout_and_allocate(struct load_info *info, int flags) · 1339 unmodified lines | |||
| 3385 | if (err < 0) | 3410 | if (err < 0) |
| 3386 | return ERR_PTR(err); | 3411 | return ERR_PTR(err); |
| 3387 | 3412 | ||
| 3413 | err = module_enforce_rwx_sections(info->hdr, info->sechdrs, | ||
| 3414 | info->secstrings, info->mod); | ||
| 3415 | if (err < 0) | ||
| 3416 | return ERR_PTR(err); | ||
| 3417 | |||
| 3388 | /* We will do a special allocation for per-cpu sections later. */ | 3418 | /* We will do a special allocation for per-cpu sections later. */ |
| 3389 | info->sechdrs[info->index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC; | 3419 | info->sechdrs[info->index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC; |
| 3390 | 3420 | ||