Skip to content

Commit f798494

Browse files
committed
module: Add a separate function to mark sections as read-only after init
Move the logic to mark special sections as read-only after module initialization into a separate function, along other related code in strict_rwx.c. Use a table with names of such sections to make it easier to add more. Reviewed-by: Sami Tolvanen <[email protected]> Reviewed-by: Luis Chamberlain <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Petr Pavlu <[email protected]>
1 parent 7bba316 commit f798494

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

kernel/module/internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,8 @@ int module_enable_text_rox(const struct module *mod);
325325
int module_enforce_rwx_sections(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
326326
const char *secstrings,
327327
const struct module *mod);
328+
void module_mark_ro_after_init(const Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
329+
const char *secstrings);
328330

329331
#ifdef CONFIG_MODULE_SIG
330332
int module_sig_check(struct load_info *info, int flags);

kernel/module/main.c

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2768,7 +2768,6 @@ core_param(module_blacklist, module_blacklist, charp, 0400);
27682768
static struct module *layout_and_allocate(struct load_info *info, int flags)
27692769
{
27702770
struct module *mod;
2771-
unsigned int ndx;
27722771
int err;
27732772

27742773
/* Allow arches to frob section contents and sizes. */
@@ -2786,22 +2785,11 @@ static struct module *layout_and_allocate(struct load_info *info, int flags)
27862785
info->sechdrs[info->index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC;
27872786

27882787
/*
2789-
* Mark ro_after_init section with SHF_RO_AFTER_INIT so that
2790-
* layout_sections() can put it in the right place.
2788+
* Mark relevant sections as SHF_RO_AFTER_INIT so layout_sections() can
2789+
* put them in the right place.
27912790
* Note: ro_after_init sections also have SHF_{WRITE,ALLOC} set.
27922791
*/
2793-
ndx = find_sec(info, ".data..ro_after_init");
2794-
if (ndx)
2795-
info->sechdrs[ndx].sh_flags |= SHF_RO_AFTER_INIT;
2796-
/*
2797-
* Mark the __jump_table section as ro_after_init as well: these data
2798-
* structures are never modified, with the exception of entries that
2799-
* refer to code in the __init section, which are annotated as such
2800-
* at module load time.
2801-
*/
2802-
ndx = find_sec(info, "__jump_table");
2803-
if (ndx)
2804-
info->sechdrs[ndx].sh_flags |= SHF_RO_AFTER_INIT;
2792+
module_mark_ro_after_init(info->hdr, info->sechdrs, info->secstrings);
28052793

28062794
/*
28072795
* Determine total sizes, and put offsets in sh_entsize. For now

kernel/module/strict_rwx.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,36 @@ int module_enforce_rwx_sections(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
107107

108108
return 0;
109109
}
110+
111+
static const char *const ro_after_init[] = {
112+
/*
113+
* Section .data..ro_after_init holds data explicitly annotated by
114+
* __ro_after_init.
115+
*/
116+
".data..ro_after_init",
117+
118+
/*
119+
* Section __jump_table holds data structures that are never modified,
120+
* with the exception of entries that refer to code in the __init
121+
* section, which are marked as such at module load time.
122+
*/
123+
"__jump_table",
124+
};
125+
126+
void module_mark_ro_after_init(const Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
127+
const char *secstrings)
128+
{
129+
int i, j;
130+
131+
for (i = 1; i < hdr->e_shnum; i++) {
132+
Elf_Shdr *shdr = &sechdrs[i];
133+
134+
for (j = 0; j < ARRAY_SIZE(ro_after_init); j++) {
135+
if (strcmp(secstrings + shdr->sh_name,
136+
ro_after_init[j]) == 0) {
137+
shdr->sh_flags |= SHF_RO_AFTER_INIT;
138+
break;
139+
}
140+
}
141+
}
142+
}

0 commit comments

Comments
 (0)