@@ -1946,7 +1946,6 @@ static void mod_sysfs_teardown(struct module *mod)
19461946 mod_sysfs_fini (mod );
19471947}
19481948
1949- #ifdef CONFIG_ARCH_HAS_STRICT_MODULE_RWX
19501949/*
19511950 * LKM RO/NX protection: protect module's text/ro-data
19521951 * from modification and any data from execution.
@@ -1960,6 +1959,14 @@ static void mod_sysfs_teardown(struct module *mod)
19601959 *
19611960 * These values are always page-aligned (as is base)
19621961 */
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
19631970static void frob_text (const struct module_layout * layout ,
19641971 int (* set_memory )(unsigned long start , int num_pages ))
19651972{
@@ -1969,6 +1976,15 @@ static void frob_text(const struct module_layout *layout,
19691976 layout -> text_size >> PAGE_SHIFT );
19701977}
19711978
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+
19721988#ifdef CONFIG_STRICT_MODULE_RWX
19731989static void frob_rodata (const struct module_layout * layout ,
19741990 int (* set_memory )(unsigned long start , int num_pages ))
@@ -2026,20 +2042,29 @@ static void module_enable_nx(const struct module *mod)
20262042 frob_writable_data (& mod -> init_layout , set_memory_nx );
20272043}
20282044
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+
20292059#else /* !CONFIG_STRICT_MODULE_RWX */
20302060static void module_enable_nx (const struct module * mod ) { }
20312061static void module_enable_ro (const struct module * mod , bool after_init ) {}
2032- #endif /* CONFIG_STRICT_MODULE_RWX */
2033- static void module_enable_x ( const struct module * mod )
2062+ static int module_enforce_rwx_sections ( Elf_Ehdr * hdr , Elf_Shdr * sechdrs ,
2063+ char * secstrings , struct module * mod )
20342064{
2035- frob_text (& mod -> core_layout , set_memory_x );
2036- frob_text (& mod -> init_layout , set_memory_x );
2065+ return 0 ;
20372066}
2038- #else /* !CONFIG_ARCH_HAS_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-
2067+ #endif /* CONFIG_STRICT_MODULE_RWX */
20432068
20442069#ifdef CONFIG_LIVEPATCH
20452070/*
@@ -3385,6 +3410,11 @@ static struct module *layout_and_allocate(struct load_info *info, int flags)
33853410 if (err < 0 )
33863411 return ERR_PTR (err );
33873412
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+
33883418 /* We will do a special allocation for per-cpu sections later. */
33893419 info -> sechdrs [info -> index .pcpu ].sh_flags &= ~(unsigned long )SHF_ALLOC ;
33903420
0 commit comments