2020-05-05 14:26:29

by Thomas Gleixner

[permalink] [raw]
Subject: [patch V4 part 1 17/36] kprobes: Support NOKPROBE_SYMBOL() in modules

From: Masami Hiramatsu <[email protected]>

Support NOKPROBE_SYMBOL() in modules. NOKPROBE_SYMBOL() records only symbol
address in "_kprobe_blacklist" section in the module.

Signed-off-by: Masami Hiramatsu <[email protected]>
Signed-off-by: Thomas Gleixner <[email protected]>
Link: https://lkml.kernel.org/r/158523419989.24735.6665260504057165207.stgit@devnote2

---
include/linux/module.h | 2 ++
kernel/kprobes.c | 17 +++++++++++++++++
kernel/module.c | 3 +++
3 files changed, 22 insertions(+)

diff --git a/include/linux/module.h b/include/linux/module.h
index 369c354f9207..1192097c9a69 100644
--- a/include/linux/module.h
+++ b/include/linux/module.h
@@ -492,6 +492,8 @@ struct module {
#ifdef CONFIG_KPROBES
void *kprobes_text_start;
unsigned int kprobes_text_size;
+ unsigned long *kprobe_blacklist;
+ unsigned int num_kprobe_blacklist;
#endif

#ifdef CONFIG_LIVEPATCH
diff --git a/kernel/kprobes.c b/kernel/kprobes.c
index b7549992b9bd..9eb5acf0a9f3 100644
--- a/kernel/kprobes.c
+++ b/kernel/kprobes.c
@@ -2192,6 +2192,11 @@ static void kprobe_remove_area_blacklist(unsigned long start, unsigned long end)
}
}

+static void kprobe_remove_ksym_blacklist(unsigned long entry)
+{
+ kprobe_remove_area_blacklist(entry, entry + 1);
+}
+
int __init __weak arch_populate_kprobe_blacklist(void)
{
return 0;
@@ -2231,6 +2236,12 @@ static int __init populate_kprobe_blacklist(unsigned long *start,
static void add_module_kprobe_blacklist(struct module *mod)
{
unsigned long start, end;
+ int i;
+
+ if (mod->kprobe_blacklist) {
+ for (i = 0; i < mod->num_kprobe_blacklist; i++)
+ kprobe_add_ksym_blacklist(mod->kprobe_blacklist[i]);
+ }

start = (unsigned long)mod->kprobes_text_start;
if (start) {
@@ -2242,6 +2253,12 @@ static void add_module_kprobe_blacklist(struct module *mod)
static void remove_module_kprobe_blacklist(struct module *mod)
{
unsigned long start, end;
+ int i;
+
+ if (mod->kprobe_blacklist) {
+ for (i = 0; i < mod->num_kprobe_blacklist; i++)
+ kprobe_remove_ksym_blacklist(mod->kprobe_blacklist[i]);
+ }

start = (unsigned long)mod->kprobes_text_start;
if (start) {
diff --git a/kernel/module.c b/kernel/module.c
index f7712a923d63..7be011dacd8a 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -3197,6 +3197,9 @@ static int find_module_sections(struct module *mod, struct load_info *info)
#ifdef CONFIG_KPROBES
mod->kprobes_text_start = section_objs(info, ".kprobes.text", 1,
&mod->kprobes_text_size);
+ mod->kprobe_blacklist = section_objs(info, "_kprobe_blacklist",
+ sizeof(unsigned long),
+ &mod->num_kprobe_blacklist);
#endif
mod->extable = section_objs(info, "__ex_table",
sizeof(*mod->extable), &mod->num_exentries);



2020-05-06 16:00:24

by Alexandre Chartre

[permalink] [raw]
Subject: Re: [patch V4 part 1 17/36] kprobes: Support NOKPROBE_SYMBOL() in modules


On 5/5/20 3:16 PM, Thomas Gleixner wrote:
> From: Masami Hiramatsu <[email protected]>
>
> Support NOKPROBE_SYMBOL() in modules. NOKPROBE_SYMBOL() records only symbol
> address in "_kprobe_blacklist" section in the module.
>
> Signed-off-by: Masami Hiramatsu <[email protected]>
> Signed-off-by: Thomas Gleixner <[email protected]>
> Link: https://lkml.kernel.org/r/158523419989.24735.6665260504057165207.stgit@devnote2
>
> ---
> include/linux/module.h | 2 ++
> kernel/kprobes.c | 17 +++++++++++++++++
> kernel/module.c | 3 +++
> 3 files changed, 22 insertions(+)

Reviewed-by: Alexandre Chartre <[email protected]>

alex.

> diff --git a/include/linux/module.h b/include/linux/module.h
> index 369c354f9207..1192097c9a69 100644
> --- a/include/linux/module.h
> +++ b/include/linux/module.h
> @@ -492,6 +492,8 @@ struct module {
> #ifdef CONFIG_KPROBES
> void *kprobes_text_start;
> unsigned int kprobes_text_size;
> + unsigned long *kprobe_blacklist;
> + unsigned int num_kprobe_blacklist;
> #endif
>
> #ifdef CONFIG_LIVEPATCH
> diff --git a/kernel/kprobes.c b/kernel/kprobes.c
> index b7549992b9bd..9eb5acf0a9f3 100644
> --- a/kernel/kprobes.c
> +++ b/kernel/kprobes.c
> @@ -2192,6 +2192,11 @@ static void kprobe_remove_area_blacklist(unsigned long start, unsigned long end)
> }
> }
>
> +static void kprobe_remove_ksym_blacklist(unsigned long entry)
> +{
> + kprobe_remove_area_blacklist(entry, entry + 1);
> +}
> +
> int __init __weak arch_populate_kprobe_blacklist(void)
> {
> return 0;
> @@ -2231,6 +2236,12 @@ static int __init populate_kprobe_blacklist(unsigned long *start,
> static void add_module_kprobe_blacklist(struct module *mod)
> {
> unsigned long start, end;
> + int i;
> +
> + if (mod->kprobe_blacklist) {
> + for (i = 0; i < mod->num_kprobe_blacklist; i++)
> + kprobe_add_ksym_blacklist(mod->kprobe_blacklist[i]);
> + }
>
> start = (unsigned long)mod->kprobes_text_start;
> if (start) {
> @@ -2242,6 +2253,12 @@ static void add_module_kprobe_blacklist(struct module *mod)
> static void remove_module_kprobe_blacklist(struct module *mod)
> {
> unsigned long start, end;
> + int i;
> +
> + if (mod->kprobe_blacklist) {
> + for (i = 0; i < mod->num_kprobe_blacklist; i++)
> + kprobe_remove_ksym_blacklist(mod->kprobe_blacklist[i]);
> + }
>
> start = (unsigned long)mod->kprobes_text_start;
> if (start) {
> diff --git a/kernel/module.c b/kernel/module.c
> index f7712a923d63..7be011dacd8a 100644
> --- a/kernel/module.c
> +++ b/kernel/module.c
> @@ -3197,6 +3197,9 @@ static int find_module_sections(struct module *mod, struct load_info *info)
> #ifdef CONFIG_KPROBES
> mod->kprobes_text_start = section_objs(info, ".kprobes.text", 1,
> &mod->kprobes_text_size);
> + mod->kprobe_blacklist = section_objs(info, "_kprobe_blacklist",
> + sizeof(unsigned long),
> + &mod->num_kprobe_blacklist);
> #endif
> mod->extable = section_objs(info, "__ex_table",
> sizeof(*mod->extable), &mod->num_exentries);
>
>

Subject: [tip: core/kprobes] kprobes: Support NOKPROBE_SYMBOL() in modules

The following commit has been merged into the core/kprobes branch of tip:

Commit-ID: 16db6264c93d2d7df9eb8be5d9eb717ab30105fe
Gitweb: https://git.kernel.org/tip/16db6264c93d2d7df9eb8be5d9eb717ab30105fe
Author: Masami Hiramatsu <[email protected]>
AuthorDate: Thu, 26 Mar 2020 23:50:00 +09:00
Committer: Thomas Gleixner <[email protected]>
CommitterDate: Tue, 12 May 2020 17:15:32 +02:00

kprobes: Support NOKPROBE_SYMBOL() in modules

Support NOKPROBE_SYMBOL() in modules. NOKPROBE_SYMBOL() records only symbol
address in "_kprobe_blacklist" section in the module.

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]

---
include/linux/module.h | 2 ++
kernel/kprobes.c | 17 +++++++++++++++++
kernel/module.c | 3 +++
3 files changed, 22 insertions(+)

diff --git a/include/linux/module.h b/include/linux/module.h
index 369c354..1192097 100644
--- a/include/linux/module.h
+++ b/include/linux/module.h
@@ -492,6 +492,8 @@ struct module {
#ifdef CONFIG_KPROBES
void *kprobes_text_start;
unsigned int kprobes_text_size;
+ unsigned long *kprobe_blacklist;
+ unsigned int num_kprobe_blacklist;
#endif

#ifdef CONFIG_LIVEPATCH
diff --git a/kernel/kprobes.c b/kernel/kprobes.c
index b754999..9eb5acf 100644
--- a/kernel/kprobes.c
+++ b/kernel/kprobes.c
@@ -2192,6 +2192,11 @@ static void kprobe_remove_area_blacklist(unsigned long start, unsigned long end)
}
}

+static void kprobe_remove_ksym_blacklist(unsigned long entry)
+{
+ kprobe_remove_area_blacklist(entry, entry + 1);
+}
+
int __init __weak arch_populate_kprobe_blacklist(void)
{
return 0;
@@ -2231,6 +2236,12 @@ static int __init populate_kprobe_blacklist(unsigned long *start,
static void add_module_kprobe_blacklist(struct module *mod)
{
unsigned long start, end;
+ int i;
+
+ if (mod->kprobe_blacklist) {
+ for (i = 0; i < mod->num_kprobe_blacklist; i++)
+ kprobe_add_ksym_blacklist(mod->kprobe_blacklist[i]);
+ }

start = (unsigned long)mod->kprobes_text_start;
if (start) {
@@ -2242,6 +2253,12 @@ static void add_module_kprobe_blacklist(struct module *mod)
static void remove_module_kprobe_blacklist(struct module *mod)
{
unsigned long start, end;
+ int i;
+
+ if (mod->kprobe_blacklist) {
+ for (i = 0; i < mod->num_kprobe_blacklist; i++)
+ kprobe_remove_ksym_blacklist(mod->kprobe_blacklist[i]);
+ }

start = (unsigned long)mod->kprobes_text_start;
if (start) {
diff --git a/kernel/module.c b/kernel/module.c
index 978f3fa..faf7337 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -3197,6 +3197,9 @@ static int find_module_sections(struct module *mod, struct load_info *info)
#ifdef CONFIG_KPROBES
mod->kprobes_text_start = section_objs(info, ".kprobes.text", 1,
&mod->kprobes_text_size);
+ mod->kprobe_blacklist = section_objs(info, "_kprobe_blacklist",
+ sizeof(unsigned long),
+ &mod->num_kprobe_blacklist);
#endif
mod->extable = section_objs(info, "__ex_table",
sizeof(*mod->extable), &mod->num_exentries);