Skip to content

Commit 1e6769b

Browse files
mhiramatKAGA-KOKO
authored andcommitted
kprobes: Support __kprobes blacklist in modules
Support __kprobes attribute for blacklist functions in modules. The __kprobes attribute functions are stored in .kprobes.text section. Signed-off-by: Masami Hiramatsu <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Reviewed-by: Alexandre Chartre <[email protected]> Acked-by: Peter Zijlstra <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 4fdd888 commit 1e6769b

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

include/linux/module.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,10 @@ struct module {
489489
unsigned int num_ftrace_callsites;
490490
unsigned long *ftrace_callsites;
491491
#endif
492+
#ifdef CONFIG_KPROBES
493+
void *kprobes_text_start;
494+
unsigned int kprobes_text_size;
495+
#endif
492496

493497
#ifdef CONFIG_LIVEPATCH
494498
bool klp; /* Is this a livepatch module? */

kernel/kprobes.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2179,6 +2179,19 @@ int kprobe_add_area_blacklist(unsigned long start, unsigned long end)
21792179
return 0;
21802180
}
21812181

2182+
/* Remove all symbols in given area from kprobe blacklist */
2183+
static void kprobe_remove_area_blacklist(unsigned long start, unsigned long end)
2184+
{
2185+
struct kprobe_blacklist_entry *ent, *n;
2186+
2187+
list_for_each_entry_safe(ent, n, &kprobe_blacklist, list) {
2188+
if (ent->start_addr < start || ent->start_addr >= end)
2189+
continue;
2190+
list_del(&ent->list);
2191+
kfree(ent);
2192+
}
2193+
}
2194+
21822195
int __init __weak arch_populate_kprobe_blacklist(void)
21832196
{
21842197
return 0;
@@ -2215,6 +2228,28 @@ static int __init populate_kprobe_blacklist(unsigned long *start,
22152228
return ret ? : arch_populate_kprobe_blacklist();
22162229
}
22172230

2231+
static void add_module_kprobe_blacklist(struct module *mod)
2232+
{
2233+
unsigned long start, end;
2234+
2235+
start = (unsigned long)mod->kprobes_text_start;
2236+
if (start) {
2237+
end = start + mod->kprobes_text_size;
2238+
kprobe_add_area_blacklist(start, end);
2239+
}
2240+
}
2241+
2242+
static void remove_module_kprobe_blacklist(struct module *mod)
2243+
{
2244+
unsigned long start, end;
2245+
2246+
start = (unsigned long)mod->kprobes_text_start;
2247+
if (start) {
2248+
end = start + mod->kprobes_text_size;
2249+
kprobe_remove_area_blacklist(start, end);
2250+
}
2251+
}
2252+
22182253
/* Module notifier call back, checking kprobes on the module */
22192254
static int kprobes_module_callback(struct notifier_block *nb,
22202255
unsigned long val, void *data)
@@ -2225,6 +2260,11 @@ static int kprobes_module_callback(struct notifier_block *nb,
22252260
unsigned int i;
22262261
int checkcore = (val == MODULE_STATE_GOING);
22272262

2263+
if (val == MODULE_STATE_COMING) {
2264+
mutex_lock(&kprobe_mutex);
2265+
add_module_kprobe_blacklist(mod);
2266+
mutex_unlock(&kprobe_mutex);
2267+
}
22282268
if (val != MODULE_STATE_GOING && val != MODULE_STATE_LIVE)
22292269
return NOTIFY_DONE;
22302270

@@ -2255,6 +2295,8 @@ static int kprobes_module_callback(struct notifier_block *nb,
22552295
kill_kprobe(p);
22562296
}
22572297
}
2298+
if (val == MODULE_STATE_GOING)
2299+
remove_module_kprobe_blacklist(mod);
22582300
mutex_unlock(&kprobe_mutex);
22592301
return NOTIFY_DONE;
22602302
}

kernel/module.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3193,6 +3193,10 @@ static int find_module_sections(struct module *mod, struct load_info *info)
31933193
mod->ei_funcs = section_objs(info, "_error_injection_whitelist",
31943194
sizeof(*mod->ei_funcs),
31953195
&mod->num_ei_funcs);
3196+
#endif
3197+
#ifdef CONFIG_KPROBES
3198+
mod->kprobes_text_start = section_objs(info, ".kprobes.text", 1,
3199+
&mod->kprobes_text_size);
31963200
#endif
31973201
mod->extable = section_objs(info, "__ex_table",
31983202
sizeof(*mod->extable), &mod->num_exentries);

0 commit comments

Comments
 (0)