[RFC PATCH 2/2] kprobes: Allow specifying names for certain blacklisted address ranges

From: Naveen N. Rao
Date: Thu Jun 29 2017 - 10:09:26 EST


This allows us to provide meaningful names for certain address ranges
that are blacklisted such as those with __kprobes annotation, kernel
entry text, and so on. For such ranges, using the symbol/function name
at the start of the range is misleading.

On a powerpc64le system, before patch:
root@ubuntu:/sys/kernel/debug# tail kprobes/blacklist
0xc00000000022d500-0xc00000000022d580 print_type_s64
0xc00000000022d480-0xc00000000022d500 print_type_s32
0xc00000000022d400-0xc00000000022d480 print_type_s16
0xc00000000022d380-0xc00000000022d400 print_type_s8
0xc00000000022d300-0xc00000000022d380 print_type_u64
0xc00000000022d280-0xc00000000022d300 print_type_u32
0xc00000000022d200-0xc00000000022d280 print_type_u16
0xc00000000022d180-0xc00000000022d200 print_type_u8
0xc0000000009ed778-0xc0000000009ed778 __irqentry_text_end
0xc000000000000000-0xc000000000008000 start_first_256B

After patch:
root@ubuntu:/sys/kernel/debug# tail kprobes/blacklist
0xc00000000022d540-0xc00000000022d5c0 print_type_s64
0xc00000000022d4c0-0xc00000000022d540 print_type_s32
0xc00000000022d440-0xc00000000022d4c0 print_type_s16
0xc00000000022d3c0-0xc00000000022d440 print_type_s8
0xc00000000022d340-0xc00000000022d3c0 print_type_u64
0xc00000000022d2c0-0xc00000000022d340 print_type_u32
0xc00000000022d240-0xc00000000022d2c0 print_type_u16
0xc00000000022d1c0-0xc00000000022d240 print_type_u8
0xc0000000009ed7b8-0xc0000000009ed7b8 [__kprobes]
0xc000000000000000-0xc000000000008000 [__exceptions]

Signed-off-by: Naveen N. Rao <naveen.n.rao@xxxxxxxxxxxxxxxxxx>
---
arch/arm64/kernel/probes/kprobes.c | 10 +++++-----
arch/powerpc/kernel/kprobes.c | 4 ++--
arch/x86/kernel/kprobes/core.c | 4 ++--
include/linux/kprobes.h | 3 ++-
kernel/kprobes.c | 17 ++++++++++++-----
5 files changed, 23 insertions(+), 15 deletions(-)

diff --git a/arch/arm64/kernel/probes/kprobes.c b/arch/arm64/kernel/probes/kprobes.c
index 7f453a71b5a8..340325ec97c6 100644
--- a/arch/arm64/kernel/probes/kprobes.c
+++ b/arch/arm64/kernel/probes/kprobes.c
@@ -544,17 +544,17 @@ bool arch_within_kprobe_blacklist(unsigned long addr)
void __init arch_populate_kprobe_blacklist(void)
{
insert_kprobe_blacklist((unsigned long)__kprobes_text_start,
- (unsigned long)__kprobes_text_end);
+ (unsigned long)__kprobes_text_end, "[__kprobes]");
insert_kprobe_blacklist((unsigned long)__entry_text_start,
- (unsigned long)__entry_text_end);
+ (unsigned long)__entry_text_end, "[__entry]");
insert_kprobe_blacklist((unsigned long)__idmap_text_start,
- (unsigned long)__idmap_text_end);
+ (unsigned long)__idmap_text_end, "[__idmap]");

if (!is_kernel_in_hyp_mode()) {
insert_kprobe_blacklist((unsigned long)__hyp_text_start,
- (unsigned long)__hyp_text_end);
+ (unsigned long)__hyp_text_end, "[__hyp]");
insert_kprobe_blacklist((unsigned long)__hyp_idmap_text_start,
- (unsigned long)__hyp_idmap_text_end);
+ (unsigned long)__hyp_idmap_text_end, "[__hyp_idmap]");
}
}

diff --git a/arch/powerpc/kernel/kprobes.c b/arch/powerpc/kernel/kprobes.c
index f707e42b3482..1b873e19d29b 100644
--- a/arch/powerpc/kernel/kprobes.c
+++ b/arch/powerpc/kernel/kprobes.c
@@ -52,9 +52,9 @@ int is_current_kprobe_addr(unsigned long addr)
void __init arch_populate_kprobe_blacklist(void)
{
insert_kprobe_blacklist((unsigned long)__kprobes_text_start,
- (unsigned long)__kprobes_text_end);
+ (unsigned long)__kprobes_text_end, "[__kprobes]");
insert_kprobe_blacklist((unsigned long)_stext,
- (unsigned long)__head_end);
+ (unsigned long)__head_end, "[__exceptions]");
}

kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset)
diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c
index 4a5fc04b3e92..20e71d783bcf 100644
--- a/arch/x86/kernel/kprobes/core.c
+++ b/arch/x86/kernel/kprobes/core.c
@@ -1144,9 +1144,9 @@ NOKPROBE_SYMBOL(longjmp_break_handler);
void __init arch_populate_kprobe_blacklist(void)
{
insert_kprobe_blacklist((unsigned long)__kprobes_text_start,
- (unsigned long)__kprobes_text_end);
+ (unsigned long)__kprobes_text_end, "[__kprobes]");
insert_kprobe_blacklist((unsigned long)__entry_text_start,
- (unsigned long)__entry_text_end);
+ (unsigned long)__entry_text_end, "[__entry]");
}

int __init arch_init_kprobes(void)
diff --git a/include/linux/kprobes.h b/include/linux/kprobes.h
index 6d931ea0604e..05a4f2619cdc 100644
--- a/include/linux/kprobes.h
+++ b/include/linux/kprobes.h
@@ -210,6 +210,7 @@ struct kprobe_blacklist_entry {
struct list_head list;
unsigned long start_addr;
unsigned long end_addr;
+ char *name;
};

#ifdef CONFIG_KPROBES
@@ -271,7 +272,7 @@ extern bool arch_function_offset_within_entry(unsigned long offset);
extern bool function_offset_within_entry(kprobe_opcode_t *addr, const char *sym, unsigned long offset);

extern bool within_kprobe_blacklist(unsigned long addr);
-extern void insert_kprobe_blacklist(unsigned long start, unsigned long end);
+extern void insert_kprobe_blacklist(unsigned long start, unsigned long end, const char *name);
extern void arch_populate_kprobe_blacklist(void);

struct kprobe_insn_cache {
diff --git a/kernel/kprobes.c b/kernel/kprobes.c
index 8ddbee01ce01..fcb1765de49e 100644
--- a/kernel/kprobes.c
+++ b/kernel/kprobes.c
@@ -2125,7 +2125,8 @@ NOKPROBE_SYMBOL(dump_kprobe);
extern unsigned long __start_kprobe_blacklist[];
extern unsigned long __stop_kprobe_blacklist[];

-void __init insert_kprobe_blacklist(unsigned long start, unsigned long end)
+void __init insert_kprobe_blacklist(unsigned long start, unsigned long end,
+ const char *name)
{
struct kprobe_blacklist_entry *ent;

@@ -2137,6 +2138,7 @@ void __init insert_kprobe_blacklist(unsigned long start, unsigned long end)

ent->start_addr = start;
ent->end_addr = end;
+ ent->name = kstrdup(name, GFP_KERNEL);
INIT_LIST_HEAD(&ent->list);
list_add_tail(&ent->list, &kprobe_blacklist);
}
@@ -2145,7 +2147,7 @@ void __weak __init arch_populate_kprobe_blacklist(void)
{
/* The __kprobes marked functions and entry code must not be probed */
insert_kprobe_blacklist((unsigned long)__kprobes_text_start,
- (unsigned long)__kprobes_text_end);
+ (unsigned long)__kprobes_text_end, "[__kprobes]");
}

static void __init populate_kprobe_blacklist(void)
@@ -2165,7 +2167,7 @@ static void __init populate_kprobe_blacklist(void)
continue;
}

- insert_kprobe_blacklist(entry, entry + size);
+ insert_kprobe_blacklist(entry, entry + size, NULL);
}

/* Let architectures add their own entries as well */
@@ -2378,8 +2380,13 @@ static int kprobe_blacklist_seq_show(struct seq_file *m, void *v)
struct kprobe_blacklist_entry *ent =
list_entry(v, struct kprobe_blacklist_entry, list);

- seq_printf(m, "0x%p-0x%p\t%ps\n", (void *)ent->start_addr,
- (void *)ent->end_addr, (void *)ent->start_addr);
+ if (ent->name)
+ seq_printf(m, "0x%p-0x%p\t%s\n", (void *)ent->start_addr,
+ (void *)ent->end_addr, ent->name);
+ else
+ seq_printf(m, "0x%p-0x%p\t%ps\n", (void *)ent->start_addr,
+ (void *)ent->end_addr, (void *)ent->start_addr);
+
return 0;
}

--
2.13.1