Skip to content

Commit a4af9d0

Browse files
chleroymcgrof
authored andcommitted
module: pre-test setting ro_after_init data read-only
To be on the safe side, try to set ro_after_init data section readonly at the same time as rodata. If it fails it will likely fail again later so let's cancel module loading while we still can do it. If it doesn't fail, put it back to read-write, continue module loading and cross fingers so that it still works after module init. In practice, if it worked once it will work twice: - On some architecture like powerpc it works on some memory areas and works on others. If you apply it several times to the same area, either it always works or it always fails - On some architecture like ARM, that may apply splitting big pages into smaller ones, that is what can fails, but once it successed the pages will remain split so there's no reason to fail on pass two if it worked on pass one. Then it should in principle never fail so add a WARN_ON_ONCE() to get a big fat warning in case it happens anyway. For systems that sets panic-on-warn, such systems usely care about security and don't want vulnerable systems, so an implied panic is worth it in that case. Signed-off-by: Christophe Leroy <christophe.leroy@csgroup.eu>
1 parent b85ad27 commit a4af9d0

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

kernel/module/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2954,7 +2954,7 @@ static noinline int do_init_module(struct module *mod)
29542954
rcu_assign_pointer(mod->kallsyms, &mod->core_kallsyms);
29552955
#endif
29562956
ret = module_enable_rodata_ro_after_init(mod);
2957-
if (ret)
2957+
if (WARN_ON_ONCE(ret))
29582958
pr_warn("%s: module_enable_rodata_ro_after_init() returned %d, "
29592959
"ro_after_init data might still be writable\n",
29602960
mod->name, ret);

kernel/module/strict_rwx.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ int module_enable_rodata_ro(const struct module *mod)
6161
if (ret)
6262
return ret;
6363

64-
return 0;
64+
ret = module_set_memory(mod, MOD_RO_AFTER_INIT, set_memory_ro);
65+
if (ret)
66+
return ret;
67+
return module_set_memory(mod, MOD_RO_AFTER_INIT, set_memory_rw);
6568
}
6669

6770
int module_enable_rodata_ro_after_init(const struct module *mod)

0 commit comments

Comments
 (0)