Skip to content

Commit 32a08c1

Browse files
chleroymcgrof
authored andcommitted
module: Move module_enable_x() and frob_text() in strict_rwx.c
Move module_enable_x() together with module_enable_nx() and module_enable_ro(). Those three functions are going together, they are all used to set up the correct page flags on the different sections. As module_enable_x() is used independently of CONFIG_STRICT_MODULE_RWX, build strict_rwx.c all the time and use IS_ENABLED(CONFIG_STRICT_MODULE_RWX) when relevant. Signed-off-by: Christophe Leroy <[email protected]> Signed-off-by: Luis Chamberlain <[email protected]>
1 parent 0597579 commit 32a08c1

File tree

4 files changed

+50
-53
lines changed

4 files changed

+50
-53
lines changed

kernel/module/Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77
# and produce insane amounts of uninteresting coverage.
88
KCOV_INSTRUMENT_module.o := n
99

10-
obj-y += main.o
10+
obj-y += main.o strict_rwx.o
1111
obj-$(CONFIG_MODULE_DECOMPRESS) += decompress.o
1212
obj-$(CONFIG_MODULE_SIG) += signing.o
1313
obj-$(CONFIG_LIVEPATCH) += livepatch.o
1414
obj-$(CONFIG_MODULES_TREE_LOOKUP) += tree_lookup.o
15-
obj-$(CONFIG_STRICT_MODULE_RWX) += strict_rwx.o
1615
obj-$(CONFIG_DEBUG_KMEMLEAK) += debug_kmemleak.o
1716
obj-$(CONFIG_KALLSYMS) += kallsyms.o
1817
obj-$(CONFIG_PROC_FS) += procfs.o

kernel/module/internal.h

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -175,25 +175,12 @@ static inline struct module *mod_find(unsigned long addr)
175175
}
176176
#endif /* CONFIG_MODULES_TREE_LOOKUP */
177177

178-
void frob_text(const struct module_layout *layout, int (*set_memory)(unsigned long start,
179-
int num_pages));
180-
181-
#ifdef CONFIG_STRICT_MODULE_RWX
182178
void module_enable_ro(const struct module *mod, bool after_init);
183179
void module_enable_nx(const struct module *mod);
180+
void module_enable_x(const struct module *mod);
184181
int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
185182
char *secstrings, struct module *mod);
186183

187-
#else /* !CONFIG_STRICT_MODULE_RWX */
188-
static inline void module_enable_nx(const struct module *mod) { }
189-
static inline void module_enable_ro(const struct module *mod, bool after_init) {}
190-
static inline int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
191-
char *secstrings, struct module *mod)
192-
{
193-
return 0;
194-
}
195-
#endif /* CONFIG_STRICT_MODULE_RWX */
196-
197184
#ifdef CONFIG_MODULE_SIG
198185
int module_sig_check(struct load_info *info, int flags);
199186
#else /* !CONFIG_MODULE_SIG */

kernel/module/main.c

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,43 +1124,6 @@ resolve_symbol_wait(struct module *mod,
11241124
return ksym;
11251125
}
11261126

1127-
/*
1128-
* LKM RO/NX protection: protect module's text/ro-data
1129-
* from modification and any data from execution.
1130-
*
1131-
* General layout of module is:
1132-
* [text] [read-only-data] [ro-after-init] [writable data]
1133-
* text_size -----^ ^ ^ ^
1134-
* ro_size ------------------------| | |
1135-
* ro_after_init_size -----------------------------| |
1136-
* size -----------------------------------------------------------|
1137-
*
1138-
* These values are always page-aligned (as is base)
1139-
*/
1140-
1141-
/*
1142-
* Since some arches are moving towards PAGE_KERNEL module allocations instead
1143-
* of PAGE_KERNEL_EXEC, keep frob_text() and module_enable_x() outside of the
1144-
* CONFIG_STRICT_MODULE_RWX block below because they are needed regardless of
1145-
* whether we are strict.
1146-
*/
1147-
void frob_text(const struct module_layout *layout,
1148-
int (*set_memory)(unsigned long start, int num_pages))
1149-
{
1150-
set_memory((unsigned long)layout->base,
1151-
PAGE_ALIGN(layout->text_size) >> PAGE_SHIFT);
1152-
}
1153-
1154-
static void module_enable_x(const struct module *mod)
1155-
{
1156-
if (!PAGE_ALIGNED(mod->core_layout.base) ||
1157-
!PAGE_ALIGNED(mod->init_layout.base))
1158-
return;
1159-
1160-
frob_text(&mod->core_layout, set_memory_x);
1161-
frob_text(&mod->init_layout, set_memory_x);
1162-
}
1163-
11641127
void __weak module_memfree(void *module_region)
11651128
{
11661129
/*

kernel/module/strict_rwx.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,34 @@
1111
#include <linux/set_memory.h>
1212
#include "internal.h"
1313

14+
/*
15+
* LKM RO/NX protection: protect module's text/ro-data
16+
* from modification and any data from execution.
17+
*
18+
* General layout of module is:
19+
* [text] [read-only-data] [ro-after-init] [writable data]
20+
* text_size -----^ ^ ^ ^
21+
* ro_size ------------------------| | |
22+
* ro_after_init_size -----------------------------| |
23+
* size -----------------------------------------------------------|
24+
*
25+
* These values are always page-aligned (as is base) when
26+
* CONFIG_STRICT_MODULE_RWX is set.
27+
*/
28+
29+
/*
30+
* Since some arches are moving towards PAGE_KERNEL module allocations instead
31+
* of PAGE_KERNEL_EXEC, keep frob_text() and module_enable_x() independent of
32+
* CONFIG_STRICT_MODULE_RWX because they are needed regardless of whether we
33+
* are strict.
34+
*/
35+
static void frob_text(const struct module_layout *layout,
36+
int (*set_memory)(unsigned long start, int num_pages))
37+
{
38+
set_memory((unsigned long)layout->base,
39+
PAGE_ALIGN(layout->text_size) >> PAGE_SHIFT);
40+
}
41+
1442
static void frob_rodata(const struct module_layout *layout,
1543
int (*set_memory)(unsigned long start, int num_pages))
1644
{
@@ -41,10 +69,24 @@ static void frob_writable_data(const struct module_layout *layout,
4169
(layout->size - layout->ro_after_init_size) >> PAGE_SHIFT);
4270
}
4371

72+
void module_enable_x(const struct module *mod)
73+
{
74+
if (!PAGE_ALIGNED(mod->core_layout.base) ||
75+
!PAGE_ALIGNED(mod->init_layout.base))
76+
return;
77+
78+
frob_text(&mod->core_layout, set_memory_x);
79+
frob_text(&mod->init_layout, set_memory_x);
80+
}
81+
4482
void module_enable_ro(const struct module *mod, bool after_init)
4583
{
84+
if (!IS_ENABLED(CONFIG_STRICT_MODULE_RWX))
85+
return;
86+
#ifdef CONFIG_STRICT_MODULE_RWX
4687
if (!rodata_enabled)
4788
return;
89+
#endif
4890

4991
set_vm_flush_reset_perms(mod->core_layout.base);
5092
set_vm_flush_reset_perms(mod->init_layout.base);
@@ -60,6 +102,9 @@ void module_enable_ro(const struct module *mod, bool after_init)
60102

61103
void module_enable_nx(const struct module *mod)
62104
{
105+
if (!IS_ENABLED(CONFIG_STRICT_MODULE_RWX))
106+
return;
107+
63108
frob_rodata(&mod->core_layout, set_memory_nx);
64109
frob_ro_after_init(&mod->core_layout, set_memory_nx);
65110
frob_writable_data(&mod->core_layout, set_memory_nx);
@@ -73,6 +118,9 @@ int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
73118
const unsigned long shf_wx = SHF_WRITE | SHF_EXECINSTR;
74119
int i;
75120

121+
if (!IS_ENABLED(CONFIG_STRICT_MODULE_RWX))
122+
return 0;
123+
76124
for (i = 0; i < hdr->e_shnum; i++) {
77125
if ((sechdrs[i].sh_flags & shf_wx) == shf_wx) {
78126
pr_err("%s: section %s (index %d) has invalid WRITE|EXEC flags\n",

0 commit comments

Comments
 (0)