Skip to content

Commit d1909c0

Browse files
chleroymcgrof
authored andcommitted
module: Don't ignore errors from set_memory_XX()
set_memory_ro(), set_memory_nx(), set_memory_x() and other helpers can fail and return an error. In that case the memory might not be protected as expected and the module loading has to be aborted to avoid security issues. Check return value of all calls to set_memory_XX() and handle error if any. Add a check to not call set_memory_XX() on NULL pointers as some architectures may not like it allthough numpages is always 0 in that case. This also avoid a useless call to set_vm_flush_reset_perms(). Link: KSPP/linux#7 Signed-off-by: Christophe Leroy <[email protected]> Tested-by: Marek Szyprowski <[email protected]> Reviewed-by: Kees Cook <[email protected]> Signed-off-by: Luis Chamberlain <[email protected]>
1 parent 1572853 commit d1909c0

File tree

3 files changed

+55
-22
lines changed

3 files changed

+55
-22
lines changed

kernel/module/internal.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,9 @@ static inline struct module *mod_find(unsigned long addr, struct mod_tree_root *
322322
}
323323
#endif /* CONFIG_MODULES_TREE_LOOKUP */
324324

325-
void module_enable_rodata_ro(const struct module *mod, bool after_init);
326-
void module_enable_data_nx(const struct module *mod);
327-
void module_enable_text_rox(const struct module *mod);
325+
int module_enable_rodata_ro(const struct module *mod, bool after_init);
326+
int module_enable_data_nx(const struct module *mod);
327+
int module_enable_text_rox(const struct module *mod);
328328
int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
329329
char *secstrings, struct module *mod);
330330

kernel/module/main.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2571,7 +2571,9 @@ static noinline int do_init_module(struct module *mod)
25712571
/* Switch to core kallsyms now init is done: kallsyms may be walking! */
25722572
rcu_assign_pointer(mod->kallsyms, &mod->core_kallsyms);
25732573
#endif
2574-
module_enable_rodata_ro(mod, true);
2574+
ret = module_enable_rodata_ro(mod, true);
2575+
if (ret)
2576+
goto fail_mutex_unlock;
25752577
mod_tree_remove_init(mod);
25762578
module_arch_freeing_init(mod);
25772579
for_class_mod_mem_type(type, init) {
@@ -2609,6 +2611,8 @@ static noinline int do_init_module(struct module *mod)
26092611

26102612
return 0;
26112613

2614+
fail_mutex_unlock:
2615+
mutex_unlock(&module_mutex);
26122616
fail_free_freeinit:
26132617
kfree(freeinit);
26142618
fail:
@@ -2736,9 +2740,15 @@ static int complete_formation(struct module *mod, struct load_info *info)
27362740
module_bug_finalize(info->hdr, info->sechdrs, mod);
27372741
module_cfi_finalize(info->hdr, info->sechdrs, mod);
27382742

2739-
module_enable_rodata_ro(mod, false);
2740-
module_enable_data_nx(mod);
2741-
module_enable_text_rox(mod);
2743+
err = module_enable_rodata_ro(mod, false);
2744+
if (err)
2745+
goto out_strict_rwx;
2746+
err = module_enable_data_nx(mod);
2747+
if (err)
2748+
goto out_strict_rwx;
2749+
err = module_enable_text_rox(mod);
2750+
if (err)
2751+
goto out_strict_rwx;
27422752

27432753
/*
27442754
* Mark state as coming so strong_try_module_get() ignores us,
@@ -2749,6 +2759,8 @@ static int complete_formation(struct module *mod, struct load_info *info)
27492759

27502760
return 0;
27512761

2762+
out_strict_rwx:
2763+
module_bug_cleanup(mod);
27522764
out:
27532765
mutex_unlock(&module_mutex);
27542766
return err;

kernel/module/strict_rwx.c

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111
#include <linux/set_memory.h>
1212
#include "internal.h"
1313

14-
static void module_set_memory(const struct module *mod, enum mod_mem_type type,
15-
int (*set_memory)(unsigned long start, int num_pages))
14+
static int module_set_memory(const struct module *mod, enum mod_mem_type type,
15+
int (*set_memory)(unsigned long start, int num_pages))
1616
{
1717
const struct module_memory *mod_mem = &mod->mem[type];
1818

19+
if (!mod_mem->base)
20+
return 0;
21+
1922
set_vm_flush_reset_perms(mod_mem->base);
20-
set_memory((unsigned long)mod_mem->base, mod_mem->size >> PAGE_SHIFT);
23+
return set_memory((unsigned long)mod_mem->base, mod_mem->size >> PAGE_SHIFT);
2124
}
2225

2326
/*
@@ -26,35 +29,53 @@ static void module_set_memory(const struct module *mod, enum mod_mem_type type,
2629
* CONFIG_STRICT_MODULE_RWX because they are needed regardless of whether we
2730
* are strict.
2831
*/
29-
void module_enable_text_rox(const struct module *mod)
32+
int module_enable_text_rox(const struct module *mod)
3033
{
3134
for_class_mod_mem_type(type, text) {
35+
int ret;
36+
3237
if (IS_ENABLED(CONFIG_STRICT_MODULE_RWX))
33-
module_set_memory(mod, type, set_memory_rox);
38+
ret = module_set_memory(mod, type, set_memory_rox);
3439
else
35-
module_set_memory(mod, type, set_memory_x);
40+
ret = module_set_memory(mod, type, set_memory_x);
41+
if (ret)
42+
return ret;
3643
}
44+
return 0;
3745
}
3846

39-
void module_enable_rodata_ro(const struct module *mod, bool after_init)
47+
int module_enable_rodata_ro(const struct module *mod, bool after_init)
4048
{
49+
int ret;
50+
4151
if (!IS_ENABLED(CONFIG_STRICT_MODULE_RWX) || !rodata_enabled)
42-
return;
52+
return 0;
4353

44-
module_set_memory(mod, MOD_RODATA, set_memory_ro);
45-
module_set_memory(mod, MOD_INIT_RODATA, set_memory_ro);
54+
ret = module_set_memory(mod, MOD_RODATA, set_memory_ro);
55+
if (ret)
56+
return ret;
57+
ret = module_set_memory(mod, MOD_INIT_RODATA, set_memory_ro);
58+
if (ret)
59+
return ret;
4660

4761
if (after_init)
48-
module_set_memory(mod, MOD_RO_AFTER_INIT, set_memory_ro);
62+
return module_set_memory(mod, MOD_RO_AFTER_INIT, set_memory_ro);
63+
64+
return 0;
4965
}
5066

51-
void module_enable_data_nx(const struct module *mod)
67+
int module_enable_data_nx(const struct module *mod)
5268
{
5369
if (!IS_ENABLED(CONFIG_STRICT_MODULE_RWX))
54-
return;
70+
return 0;
5571

56-
for_class_mod_mem_type(type, data)
57-
module_set_memory(mod, type, set_memory_nx);
72+
for_class_mod_mem_type(type, data) {
73+
int ret = module_set_memory(mod, type, set_memory_nx);
74+
75+
if (ret)
76+
return ret;
77+
}
78+
return 0;
5879
}
5980

6081
int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,

0 commit comments

Comments
 (0)