Skip to content

Commit ad6561d

Browse files
committed
module: trim exception table on init free.
It's theoretically possible that there are exception table entries which point into the (freed) init text of modules. These could cause future problems if other modules get loaded into that memory and cause an exception as we'd see the wrong fixup. The only case I know of is kvm-intel.ko (when CONFIG_CC_OPTIMIZE_FOR_SIZE=n). Amerigo fixed this long-standing FIXME in the x86 version, but this patch is more general. This implements trim_init_extable(); most archs are simple since they use the standard lib/extable.c sort code. Alpha and IA64 use relative addresses in their fixups, so thier trimming is a slight variation. Sparc32 is unique; it doesn't seem to define ARCH_HAS_SORT_EXTABLE, yet it defines its own sort_extable() which overrides the one in lib. It doesn't sort, so we have to mark deleted entries instead of actually trimming them. Inspired-by: Amerigo Wang <[email protected]> Signed-off-by: Rusty Russell <[email protected]> Cc: [email protected] Cc: [email protected] Cc: [email protected]
1 parent c398df3 commit ad6561d

File tree

7 files changed

+101
-1
lines changed

7 files changed

+101
-1
lines changed

arch/alpha/mm/extable.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,27 @@ void sort_extable(struct exception_table_entry *start,
4848
cmp_ex, swap_ex);
4949
}
5050

51+
#ifdef CONFIG_MODULES
52+
/*
53+
* Any entry referring to the module init will be at the beginning or
54+
* the end.
55+
*/
56+
void trim_init_extable(struct module *m)
57+
{
58+
/*trim the beginning*/
59+
while (m->num_exentries &&
60+
within_module_init(ex_to_addr(&m->extable[0]), m)) {
61+
m->extable++;
62+
m->num_exentries--;
63+
}
64+
/*trim the end*/
65+
while (m->num_exentries &&
66+
within_module_init(ex_to_addr(&m->extable[m->num_exentries-1]),
67+
m))
68+
m->num_exentries--;
69+
}
70+
#endif /* CONFIG_MODULES */
71+
5172
const struct exception_table_entry *
5273
search_extable(const struct exception_table_entry *first,
5374
const struct exception_table_entry *last,

arch/ia64/mm/extable.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,32 @@ void sort_extable (struct exception_table_entry *start,
5353
cmp_ex, swap_ex);
5454
}
5555

56+
static inline unsigned long ex_to_addr(const struct exception_table_entry *x)
57+
{
58+
return (unsigned long)&x->insn + x->insn;
59+
}
60+
61+
#ifdef CONFIG_MODULES
62+
/*
63+
* Any entry referring to the module init will be at the beginning or
64+
* the end.
65+
*/
66+
void trim_init_extable(struct module *m)
67+
{
68+
/*trim the beginning*/
69+
while (m->num_exentries &&
70+
within_module_init(ex_to_addr(&m->extable[0]), m)) {
71+
m->extable++;
72+
m->num_exentries--;
73+
}
74+
/*trim the end*/
75+
while (m->num_exentries &&
76+
within_module_init(ex_to_addr(&m->extable[m->num_exentries-1]),
77+
m))
78+
m->num_exentries--;
79+
}
80+
#endif /* CONFIG_MODULES */
81+
5682
const struct exception_table_entry *
5783
search_extable (const struct exception_table_entry *first,
5884
const struct exception_table_entry *last,

arch/sparc/include/asm/uaccess_32.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
#ifndef __ASSEMBLY__
1919

20+
#define ARCH_HAS_SORT_EXTABLE
21+
#define ARCH_HAS_SEARCH_EXTABLE
22+
2023
/* Sparc is not segmented, however we need to be able to fool access_ok()
2124
* when doing system calls from kernel mode legitimately.
2225
*

arch/sparc/mm/extable.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ search_extable(const struct exception_table_entry *start,
2828
* word 3: last insn address + 4 bytes
2929
* word 4: fixup code address
3030
*
31+
* Deleted entries are encoded as:
32+
* word 1: unused
33+
* word 2: -1
34+
*
3135
* See asm/uaccess.h for more details.
3236
*/
3337

@@ -39,6 +43,10 @@ search_extable(const struct exception_table_entry *start,
3943
continue;
4044
}
4145

46+
/* A deleted entry; see trim_init_extable */
47+
if (walk->fixup == -1)
48+
continue;
49+
4250
if (walk->insn == value)
4351
return walk;
4452
}
@@ -57,6 +65,27 @@ search_extable(const struct exception_table_entry *start,
5765
return NULL;
5866
}
5967

68+
#ifdef CONFIG_MODULES
69+
/* We could memmove them around; easier to mark the trimmed ones. */
70+
void trim_init_extable(struct module *m)
71+
{
72+
unsigned int i;
73+
bool range;
74+
75+
for (i = 0; i < m->num_exentries; i += range ? 2 : 1) {
76+
range = m->extable[i].fixup == 0;
77+
78+
if (within_module_init(m->extable[i].insn, m)) {
79+
m->extable[i].fixup = -1;
80+
if (range)
81+
m->extable[i+1].fixup = -1;
82+
}
83+
if (range)
84+
i++;
85+
}
86+
}
87+
#endif /* CONFIG_MODULES */
88+
6089
/* Special extable search, which handles ranges. Returns fixup */
6190
unsigned long search_extables_range(unsigned long addr, unsigned long *g2)
6291
{

include/linux/module.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ search_extable(const struct exception_table_entry *first,
7777
void sort_extable(struct exception_table_entry *start,
7878
struct exception_table_entry *finish);
7979
void sort_main_extable(void);
80+
void trim_init_extable(struct module *m);
8081

8182
#ifdef MODULE
8283
#define MODULE_GENERIC_TABLE(gtype,name) \

kernel/module.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2455,6 +2455,7 @@ SYSCALL_DEFINE3(init_module, void __user *, umod,
24552455
mutex_lock(&module_mutex);
24562456
/* Drop initial reference. */
24572457
module_put(mod);
2458+
trim_init_extable(mod);
24582459
module_free(mod, mod->module_init);
24592460
mod->module_init = NULL;
24602461
mod->init_size = 0;

lib/extable.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,26 @@ void sort_extable(struct exception_table_entry *start,
3939
sort(start, finish - start, sizeof(struct exception_table_entry),
4040
cmp_ex, NULL);
4141
}
42-
#endif
42+
43+
#ifdef CONFIG_MODULES
44+
/*
45+
* If the exception table is sorted, any referring to the module init
46+
* will be at the beginning or the end.
47+
*/
48+
void trim_init_extable(struct module *m)
49+
{
50+
/*trim the beginning*/
51+
while (m->num_exentries && within_module_init(m->extable[0].insn, m)) {
52+
m->extable++;
53+
m->num_exentries--;
54+
}
55+
/*trim the end*/
56+
while (m->num_exentries &&
57+
within_module_init(m->extable[m->num_exentries-1].insn, m))
58+
m->num_exentries--;
59+
}
60+
#endif /* CONFIG_MODULES */
61+
#endif /* !ARCH_HAS_SORT_EXTABLE */
4362

4463
#ifndef ARCH_HAS_SEARCH_EXTABLE
4564
/*

0 commit comments

Comments
 (0)