Skip to content

Commit 097fd00

Browse files
chleroypetrpavlu
authored andcommitted
module: Split module_enable_rodata_ro()
module_enable_rodata_ro() is called twice, once before module init to set rodata sections readonly and once after module init to set rodata_after_init section readonly. The second time, only the rodata_after_init section needs to be set to read-only, no need to re-apply it to already set rodata. Split module_enable_rodata_ro() in two. Signed-off-by: Christophe Leroy <[email protected]> Tested-by: Daniel Gomez <[email protected]> Reviewed-by: Luis Chamberlain <[email protected]> Link: https://lore.kernel.org/r/e3b6ff0df7eac281c58bb02cecaeb377215daff3.1733427536.git.christophe.leroy@csgroup.eu Signed-off-by: Petr Pavlu <[email protected]>
1 parent b83815a commit 097fd00

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

kernel/module/internal.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,8 @@ static inline struct module *mod_find(unsigned long addr, struct mod_tree_root *
327327
}
328328
#endif /* CONFIG_MODULES_TREE_LOOKUP */
329329

330-
int module_enable_rodata_ro(const struct module *mod, bool after_init);
330+
int module_enable_rodata_ro(const struct module *mod);
331+
int module_enable_rodata_ro_after_init(const struct module *mod);
331332
int module_enable_data_nx(const struct module *mod);
332333
int module_enable_text_rox(const struct module *mod);
333334
int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,

kernel/module/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2953,7 +2953,7 @@ static noinline int do_init_module(struct module *mod)
29532953
/* Switch to core kallsyms now init is done: kallsyms may be walking! */
29542954
rcu_assign_pointer(mod->kallsyms, &mod->core_kallsyms);
29552955
#endif
2956-
ret = module_enable_rodata_ro(mod, true);
2956+
ret = module_enable_rodata_ro_after_init(mod);
29572957
if (ret)
29582958
goto fail_mutex_unlock;
29592959
mod_tree_remove_init(mod);
@@ -3123,7 +3123,7 @@ static int complete_formation(struct module *mod, struct load_info *info)
31233123
module_bug_finalize(info->hdr, info->sechdrs, mod);
31243124
module_cfi_finalize(info->hdr, info->sechdrs, mod);
31253125

3126-
err = module_enable_rodata_ro(mod, false);
3126+
err = module_enable_rodata_ro(mod);
31273127
if (err)
31283128
goto out_strict_rwx;
31293129
err = module_enable_data_nx(mod);

kernel/module/strict_rwx.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ int module_enable_text_rox(const struct module *mod)
4747
return 0;
4848
}
4949

50-
int module_enable_rodata_ro(const struct module *mod, bool after_init)
50+
int module_enable_rodata_ro(const struct module *mod)
5151
{
5252
int ret;
5353

@@ -61,12 +61,17 @@ int module_enable_rodata_ro(const struct module *mod, bool after_init)
6161
if (ret)
6262
return ret;
6363

64-
if (after_init)
65-
return module_set_memory(mod, MOD_RO_AFTER_INIT, set_memory_ro);
66-
6764
return 0;
6865
}
6966

67+
int module_enable_rodata_ro_after_init(const struct module *mod)
68+
{
69+
if (!IS_ENABLED(CONFIG_STRICT_MODULE_RWX) || !rodata_enabled)
70+
return 0;
71+
72+
return module_set_memory(mod, MOD_RO_AFTER_INIT, set_memory_ro);
73+
}
74+
7075
int module_enable_data_nx(const struct module *mod)
7176
{
7277
if (!IS_ENABLED(CONFIG_STRICT_MODULE_RWX))

0 commit comments

Comments
 (0)