Skip to content

Commit ef50505

Browse files
chleroymcgrof
authored andcommitted
module: Rework layout alignment to avoid BUG_ON()s
Perform layout alignment verification up front and WARN_ON() and fail module loading instead of crashing the machine. Signed-off-by: Christophe Leroy <[email protected]> Signed-off-by: Luis Chamberlain <[email protected]>
1 parent 32a08c1 commit ef50505

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

kernel/module/internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ void module_enable_nx(const struct module *mod);
180180
void module_enable_x(const struct module *mod);
181181
int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
182182
char *secstrings, struct module *mod);
183+
bool module_check_misalignment(const struct module *mod);
183184

184185
#ifdef CONFIG_MODULE_SIG
185186
int module_sig_check(struct load_info *info, int flags);

kernel/module/main.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2550,6 +2550,9 @@ static int complete_formation(struct module *mod, struct load_info *info)
25502550
/* This relies on module_mutex for list integrity. */
25512551
module_bug_finalize(info->hdr, info->sechdrs, mod);
25522552

2553+
if (module_check_misalignment(mod))
2554+
goto out_misaligned;
2555+
25532556
module_enable_ro(mod, false);
25542557
module_enable_nx(mod);
25552558
module_enable_x(mod);
@@ -2563,6 +2566,8 @@ static int complete_formation(struct module *mod, struct load_info *info)
25632566

25642567
return 0;
25652568

2569+
out_misaligned:
2570+
err = -EINVAL;
25662571
out:
25672572
mutex_unlock(&module_mutex);
25682573
return err;

kernel/module/strict_rwx.c

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,33 +42,42 @@ static void frob_text(const struct module_layout *layout,
4242
static void frob_rodata(const struct module_layout *layout,
4343
int (*set_memory)(unsigned long start, int num_pages))
4444
{
45-
BUG_ON(!PAGE_ALIGNED(layout->base));
46-
BUG_ON(!PAGE_ALIGNED(layout->text_size));
47-
BUG_ON(!PAGE_ALIGNED(layout->ro_size));
4845
set_memory((unsigned long)layout->base + layout->text_size,
4946
(layout->ro_size - layout->text_size) >> PAGE_SHIFT);
5047
}
5148

5249
static void frob_ro_after_init(const struct module_layout *layout,
5350
int (*set_memory)(unsigned long start, int num_pages))
5451
{
55-
BUG_ON(!PAGE_ALIGNED(layout->base));
56-
BUG_ON(!PAGE_ALIGNED(layout->ro_size));
57-
BUG_ON(!PAGE_ALIGNED(layout->ro_after_init_size));
5852
set_memory((unsigned long)layout->base + layout->ro_size,
5953
(layout->ro_after_init_size - layout->ro_size) >> PAGE_SHIFT);
6054
}
6155

6256
static void frob_writable_data(const struct module_layout *layout,
6357
int (*set_memory)(unsigned long start, int num_pages))
6458
{
65-
BUG_ON(!PAGE_ALIGNED(layout->base));
66-
BUG_ON(!PAGE_ALIGNED(layout->ro_after_init_size));
67-
BUG_ON(!PAGE_ALIGNED(layout->size));
6859
set_memory((unsigned long)layout->base + layout->ro_after_init_size,
6960
(layout->size - layout->ro_after_init_size) >> PAGE_SHIFT);
7061
}
7162

63+
static bool layout_check_misalignment(const struct module_layout *layout)
64+
{
65+
return WARN_ON(!PAGE_ALIGNED(layout->base)) ||
66+
WARN_ON(!PAGE_ALIGNED(layout->text_size)) ||
67+
WARN_ON(!PAGE_ALIGNED(layout->ro_size)) ||
68+
WARN_ON(!PAGE_ALIGNED(layout->ro_after_init_size)) ||
69+
WARN_ON(!PAGE_ALIGNED(layout->size));
70+
}
71+
72+
bool module_check_misalignment(const struct module *mod)
73+
{
74+
if (!IS_ENABLED(CONFIG_STRICT_MODULE_RWX))
75+
return false;
76+
77+
return layout_check_misalignment(&mod->core_layout) ||
78+
layout_check_misalignment(&mod->init_layout);
79+
}
80+
7281
void module_enable_x(const struct module *mod)
7382
{
7483
if (!PAGE_ALIGNED(mod->core_layout.base) ||

0 commit comments

Comments
 (0)