Skip to content

Commit 5c3a7db

Browse files
Peter ZijlstraJessica Yu
authored andcommitted
module: Harden STRICT_MODULE_RWX
We're very close to enforcing W^X memory, refuse to load modules that violate this principle per construction. [jeyu: move module_enforce_rwx_sections under STRICT_MODULE_RWX as per discussion] Link: http://lore.kernel.org/r/[email protected] Acked-by: Kees Cook <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Signed-off-by: Jessica Yu <[email protected]>
1 parent db991af commit 5c3a7db

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

kernel/module.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2052,9 +2052,28 @@ static void module_enable_nx(const struct module *mod)
20522052
frob_writable_data(&mod->init_layout, set_memory_nx);
20532053
}
20542054

2055+
static int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
2056+
char *secstrings, struct module *mod)
2057+
{
2058+
const unsigned long shf_wx = SHF_WRITE|SHF_EXECINSTR;
2059+
int i;
2060+
2061+
for (i = 0; i < hdr->e_shnum; i++) {
2062+
if ((sechdrs[i].sh_flags & shf_wx) == shf_wx)
2063+
return -ENOEXEC;
2064+
}
2065+
2066+
return 0;
2067+
}
2068+
20552069
#else /* !CONFIG_STRICT_MODULE_RWX */
20562070
/* module_{enable,disable}_ro() stubs are in module.h */
20572071
static void module_enable_nx(const struct module *mod) { }
2072+
static int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
2073+
char *secstrings, struct module *mod)
2074+
{
2075+
return 0;
2076+
}
20582077
#endif /* CONFIG_STRICT_MODULE_RWX */
20592078

20602079
#ifdef CONFIG_LIVEPATCH
@@ -3385,6 +3404,11 @@ static struct module *layout_and_allocate(struct load_info *info, int flags)
33853404
if (err < 0)
33863405
return ERR_PTR(err);
33873406

3407+
err = module_enforce_rwx_sections(info->hdr, info->sechdrs,
3408+
info->secstrings, info->mod);
3409+
if (err < 0)
3410+
return ERR_PTR(err);
3411+
33883412
/* We will do a special allocation for per-cpu sections later. */
33893413
info->sechdrs[info->index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC;
33903414

0 commit comments

Comments
 (0)