From: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
To: Ingo Molnar <mingo@kernel.org>
Cc: linux-arch@vger.kernel.org, Rusty Russell <rusty@rustcorp.com.au>,
Ananth N Mavinakayanahalli <ananth@in.ibm.com>,
Sandeepa Prabhu <sandeepa.prabhu@linaro.org>,
x86@kernel.org, lkml <linux-kernel@vger.kernel.org>,
"Steven Rostedt (Red Hat)" <rostedt@goodmis.org>,
virtualization@lists.linux-foundation.org,
Rob Landley <rob@landley.net>,
systemtap@sourceware.org, "David S. Miller" <davem@davemloft.net>
Subject: [PATCH -tip v3 04/23] kprobes: Support blacklist functions in module
Date: Wed, 20 Nov 2013 04:21:59 +0000 [thread overview]
Message-ID: <20131120042159.15296.80748.stgit@kbuild-fedora.novalocal> (raw)
In-Reply-To: <20131120042148.15296.88360.stgit@kbuild-fedora.novalocal>
To blacklist the functions in a module (e.g. user-defined
kprobe handler and the functions invoked from it), expand
blacklist support for modules.
With this change, users can use NOKPROBE_SYMBOL() macro in
their own modules.
Changes from previous:
- Fix the type of kprobe_blacklist_seq_stop()
Signed-off-by: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Ananth N Mavinakayanahalli <ananth@in.ibm.com>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Rob Landley <rob@landley.net>
Cc: Rusty Russell <rusty@rustcorp.com.au>
---
Documentation/kprobes.txt | 8 ++++++++
include/linux/module.h | 5 +++++
kernel/kprobes.c | 44 +++++++++++++++++++++++++++++++++++++++++---
kernel/module.c | 6 ++++++
4 files changed, 60 insertions(+), 3 deletions(-)
diff --git a/Documentation/kprobes.txt b/Documentation/kprobes.txt
index 7062631..c6634b3 100644
--- a/Documentation/kprobes.txt
+++ b/Documentation/kprobes.txt
@@ -512,6 +512,14 @@ int enable_jprobe(struct jprobe *jp);
Enables *probe which has been disabled by disable_*probe(). You must specify
the probe which has been registered.
+4.9 NOKPROBE_SYMBOL()
+
+#include <linux/kprobes.h>
+NOKPROBE_SYMBOL(FUNCTION);
+
+Protects given FUNCTION from other kprobes. This is useful for handler
+functions and functions called from the handlers.
+
5. Kprobes Features and Limitations
Kprobes allows multiple probes at the same address. Currently,
diff --git a/include/linux/module.h b/include/linux/module.h
index 05f2447..acb682b 100644
--- a/include/linux/module.h
+++ b/include/linux/module.h
@@ -16,6 +16,7 @@
#include <linux/kobject.h>
#include <linux/moduleparam.h>
#include <linux/tracepoint.h>
+#include <linux/kprobes.h>
#include <linux/export.h>
#include <linux/percpu.h>
@@ -360,6 +361,10 @@ struct module
unsigned int num_ftrace_callsites;
unsigned long *ftrace_callsites;
#endif
+#ifdef CONFIG_KPROBES
+ struct kprobe_blackpoint **kprobe_blacklist;
+ unsigned int num_kprobe_blacklist;
+#endif
#ifdef CONFIG_MODULE_UNLOAD
/* What modules depend on me? */
diff --git a/kernel/kprobes.c b/kernel/kprobes.c
index d34744e..eb9b938 100644
--- a/kernel/kprobes.c
+++ b/kernel/kprobes.c
@@ -88,6 +88,7 @@ static raw_spinlock_t *kretprobe_table_lock_ptr(unsigned long hash)
/* Blacklist -- list of struct kprobe_blackpoint */
static LIST_HEAD(kprobe_blacklist);
+static DEFINE_MUTEX(kprobe_blacklist_mutex);
#ifdef __ARCH_WANT_KPROBES_INSN_SLOT
/*
@@ -1420,6 +1421,7 @@ static __kprobes int check_kprobe_address_safe(struct kprobe *p,
#endif
}
+ mutex_lock(&kprobe_blacklist_mutex);
jump_label_lock();
preempt_disable();
@@ -1457,6 +1459,7 @@ static __kprobes int check_kprobe_address_safe(struct kprobe *p,
out:
preempt_enable();
jump_label_unlock();
+ mutex_unlock(&kprobe_blacklist_mutex);
return ret;
}
@@ -2011,6 +2014,11 @@ void __kprobes dump_kprobe(struct kprobe *kp)
kp->symbol_name, kp->addr, kp->offset);
}
+static void populate_kprobe_blacklist(struct kprobe_blackpoint **start,
+ struct kprobe_blackpoint **end);
+static void shrink_kprobe_blacklist(struct kprobe_blackpoint **start,
+ struct kprobe_blackpoint **end);
+
/* Module notifier call back, checking kprobes on the module */
static int __kprobes kprobes_module_callback(struct notifier_block *nb,
unsigned long val, void *data)
@@ -2021,6 +2029,16 @@ static int __kprobes kprobes_module_callback(struct notifier_block *nb,
unsigned int i;
int checkcore = (val == MODULE_STATE_GOING);
+ /* Add/remove module blacklist */
+ if (val == MODULE_STATE_COMING)
+ populate_kprobe_blacklist(mod->kprobe_blacklist,
+ mod->kprobe_blacklist +
+ mod->num_kprobe_blacklist);
+ else if (val == MODULE_STATE_GOING)
+ shrink_kprobe_blacklist(mod->kprobe_blacklist,
+ mod->kprobe_blacklist +
+ mod->num_kprobe_blacklist);
+
if (val != MODULE_STATE_GOING && val != MODULE_STATE_LIVE)
return NOTIFY_DONE;
@@ -2054,6 +2072,18 @@ static struct notifier_block kprobe_module_nb = {
.priority = 0
};
+/* Shrink the blacklist */
+static void shrink_kprobe_blacklist(struct kprobe_blackpoint **start,
+ struct kprobe_blackpoint **end)
+{
+ struct kprobe_blackpoint **iter;
+
+ mutex_lock(&kprobe_blacklist_mutex);
+ for (iter = start; (unsigned long)iter < (unsigned long)end; iter++)
+ list_del(&(*iter)->list);
+ mutex_unlock(&kprobe_blacklist_mutex);
+}
+
/*
* Lookup and populate the kprobe_blacklist.
*
@@ -2062,14 +2092,15 @@ static struct notifier_block kprobe_module_nb = {
* since a kprobe need not necessarily be at the beginning
* of a function.
*/
-static void __init populate_kprobe_blacklist(struct kprobe_blackpoint **start,
- struct kprobe_blackpoint **end)
+static void populate_kprobe_blacklist(struct kprobe_blackpoint **start,
+ struct kprobe_blackpoint **end)
{
struct kprobe_blackpoint **iter, *bp;
unsigned long offset = 0, size = 0;
char *modname, namebuf[128];
const char *symbol_name;
+ mutex_lock(&kprobe_blacklist_mutex);
for (iter = start; (unsigned long)iter < (unsigned long)end; iter++) {
bp = *iter;
symbol_name = kallsyms_lookup(bp->start_addr,
@@ -2081,6 +2112,7 @@ static void __init populate_kprobe_blacklist(struct kprobe_blackpoint **start,
INIT_LIST_HEAD(&bp->list);
list_add_tail(&bp->list, &kprobe_blacklist);
}
+ mutex_unlock(&kprobe_blacklist_mutex);
}
extern struct kprobe_blackpoint *__start_kprobe_blacklist[];
@@ -2231,6 +2263,7 @@ static const struct file_operations debugfs_kprobes_operations = {
/* kprobes/blacklist -- shows which functions can not be probed */
static void *kprobe_blacklist_seq_start(struct seq_file *m, loff_t *pos)
{
+ mutex_lock(&kprobe_blacklist_mutex);
return seq_list_start(&kprobe_blacklist, *pos);
}
@@ -2239,6 +2272,11 @@ static void *kprobe_blacklist_seq_next(struct seq_file *m, void *v, loff_t *pos)
return seq_list_next(v, &kprobe_blacklist, pos);
}
+static void kprobe_blacklist_seq_stop(struct seq_file *m, void *v)
+{
+ mutex_unlock(&kprobe_blacklist_mutex);
+}
+
static int kprobe_blacklist_seq_show(struct seq_file *m, void *v)
{
struct kprobe_blackpoint *bp =
@@ -2252,7 +2290,7 @@ static int kprobe_blacklist_seq_show(struct seq_file *m, void *v)
static const struct seq_operations kprobe_blacklist_seq_ops = {
.start = kprobe_blacklist_seq_start,
.next = kprobe_blacklist_seq_next,
- .stop = kprobe_seq_stop, /* Reuse void function */
+ .stop = kprobe_blacklist_seq_stop,
.show = kprobe_blacklist_seq_show,
};
diff --git a/kernel/module.c b/kernel/module.c
index dc58274..4cc844c 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -58,6 +58,7 @@
#include <linux/percpu.h>
#include <linux/kmemleak.h>
#include <linux/jump_label.h>
+#include <linux/kprobes.h>
#include <linux/pfn.h>
#include <linux/bsearch.h>
#include <linux/fips.h>
@@ -2796,6 +2797,11 @@ static void find_module_sections(struct module *mod, struct load_info *info)
sizeof(*mod->ftrace_callsites),
&mod->num_ftrace_callsites);
#endif
+#ifdef CONFIG_KPROBES
+ mod->kprobe_blacklist = section_objs(info, "_kprobe_blacklist",
+ sizeof(*mod->kprobe_blacklist),
+ &mod->num_kprobe_blacklist);
+#endif
mod->extable = section_objs(info, "__ex_table",
sizeof(*mod->extable), &mod->num_exentries);
next prev parent reply other threads:[~2013-11-20 4:22 UTC|newest]
Thread overview: 69+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-11-20 4:21 [PATCH -tip v3 00/23] kprobes: introduce NOKPROBE_SYMBOL() and general cleaning of kprobe blacklist Masami Hiramatsu
2013-11-20 4:21 ` Masami Hiramatsu
2013-11-20 4:21 ` [PATCH -tip v3 01/23] kprobes: Prohibit probing on .entry.text code Masami Hiramatsu
2013-11-20 4:21 ` Masami Hiramatsu
2013-11-20 4:21 ` [PATCH -tip v3 02/23] kprobes: Introduce NOKPROBE_SYMBOL() macro for blacklist Masami Hiramatsu
2013-11-20 4:21 ` Masami Hiramatsu
2013-11-27 13:32 ` Ingo Molnar
2013-11-27 13:32 ` Ingo Molnar
2013-11-28 7:56 ` Masami Hiramatsu
2013-11-20 4:21 ` [PATCH -tip v3 03/23] kprobes: Show blacklist entries via debugfs Masami Hiramatsu
2013-11-20 4:21 ` Masami Hiramatsu [this message]
2013-11-20 4:22 ` [PATCH -tip v3 05/23] kprobes: Use NOKPROBE_SYMBOL() in sample modules Masami Hiramatsu
2013-11-20 4:22 ` Masami Hiramatsu
2013-11-20 4:22 ` [PATCH -tip v3 06/23] kprobes/x86: Allow probe on some kprobe preparation functions Masami Hiramatsu
2013-11-20 4:22 ` Masami Hiramatsu
2013-11-20 4:22 ` [PATCH -tip v3 07/23] kprobes/x86: Use NOKPROBE_SYMBOL instead of __kprobes Masami Hiramatsu
2013-11-20 4:22 ` Masami Hiramatsu
2013-11-20 4:22 ` [PATCH -tip v3 08/23] kprobes: Allow probe on some kprobe functions Masami Hiramatsu
2013-11-20 4:22 ` [PATCH -tip v3 09/23] kprobes: Use NOKPROBE_SYMBOL macro instead of __kprobes Masami Hiramatsu
2013-11-20 4:22 ` Masami Hiramatsu
2013-11-20 4:22 ` [PATCH -tip v3 10/23] ftrace/kprobes: Allow probing on some preparation functions Masami Hiramatsu
2013-11-20 4:22 ` Masami Hiramatsu
2013-11-20 4:22 ` [PATCH -tip v3 11/23] ftrace/kprobes: Use NOKPROBE_SYMBOL macro in ftrace Masami Hiramatsu
2013-11-20 4:22 ` Masami Hiramatsu
2013-11-20 4:22 ` [PATCH -tip v3 12/23] x86/hw_breakpoint: Use NOKPROBE_SYMBOL macro in hw_breakpoint Masami Hiramatsu
2013-11-20 4:22 ` [PATCH -tip v3 13/23] x86/trap: Use NOKPROBE_SYMBOL macro in trap.c Masami Hiramatsu
2013-11-20 4:22 ` Masami Hiramatsu
2013-11-22 21:21 ` Andi Kleen
2013-11-23 12:11 ` Masami Hiramatsu
2013-11-27 1:38 ` Masami Hiramatsu
2013-11-27 1:38 ` Masami Hiramatsu
2013-11-20 4:22 ` [PATCH -tip v3 14/23] x86/fault: Use NOKPROBE_SYMBOL macro in fault.c Masami Hiramatsu
2013-11-20 4:22 ` [PATCH -tip v3 15/23] x86/alternative: Use NOKPROBE_SYMBOL macro in alternative.c Masami Hiramatsu
2013-11-20 4:22 ` Masami Hiramatsu
2013-11-20 4:22 ` [PATCH -tip v3 16/23] x86/nmi: Use NOKPROBE_SYMBOL macro for nmi handlers Masami Hiramatsu
2013-11-20 4:22 ` [PATCH -tip v3 17/23] x86/kvm: Use NOKPROBE_SYMBOL macro in kvm.c Masami Hiramatsu
2013-11-20 4:22 ` Masami Hiramatsu
2013-11-20 4:22 ` [PATCH -tip v3 18/23] x86/dumpstack: Use NOKPROBE_SYMBOL macro in dumpstack.c Masami Hiramatsu
2013-11-20 4:22 ` Masami Hiramatsu
2013-11-21 11:30 ` Ingo Molnar
2013-11-21 11:30 ` Ingo Molnar
2013-11-22 2:08 ` Masami Hiramatsu
2013-11-20 4:22 ` [PATCH -tip v3 19/23] [BUGFIX] kprobes/x86: Prohibit probing on debug_stack_* Masami Hiramatsu
2013-11-20 4:22 ` Masami Hiramatsu
2013-11-20 4:22 ` [PATCH -tip v3 20/23] [BUGFIX] kprobes: Prohibit probing on func_ptr_is_kernel_text Masami Hiramatsu
2013-11-20 4:22 ` [PATCH -tip v3 21/23] notifier: Use NOKPROBE_SYMBOL macro in notifier Masami Hiramatsu
2013-11-20 4:22 ` Masami Hiramatsu
2013-11-20 4:22 ` [PATCH -tip v3 22/23] sched: Use NOKPROBE_SYMBOL macro in sched Masami Hiramatsu
2013-11-20 4:22 ` Masami Hiramatsu
2013-11-20 4:22 ` [PATCH -tip v3 23/23] kprobes/x86: Use kprobe_blacklist for .kprobes.text and .entry.text Masami Hiramatsu
2013-11-20 4:22 ` Masami Hiramatsu
2013-11-20 14:26 ` [PATCH -tip v3 00/23] kprobes: introduce NOKPROBE_SYMBOL() and general cleaning of kprobe blacklist Frank Ch. Eigler
2013-11-20 15:38 ` Ingo Molnar
2013-11-20 15:38 ` Ingo Molnar
2013-11-20 17:36 ` Frank Ch. Eigler
2013-11-20 17:56 ` Steven Rostedt
2013-11-20 18:09 ` Josh Stone
2013-11-21 2:14 ` Masami Hiramatsu
2013-11-21 2:14 ` Masami Hiramatsu
2013-11-21 7:29 ` Ingo Molnar
2013-11-22 2:35 ` Masami Hiramatsu
2013-11-22 2:35 ` Masami Hiramatsu
2013-11-22 11:46 ` Masami Hiramatsu
2013-11-27 13:30 ` Ingo Molnar
2013-11-27 13:30 ` Ingo Molnar
2013-11-28 10:43 ` Masami Hiramatsu
2013-11-30 13:46 ` Ingo Molnar
2013-12-01 2:16 ` Masami Hiramatsu
2013-12-01 2:16 ` Masami Hiramatsu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20131120042159.15296.80748.stgit@kbuild-fedora.novalocal \
--to=masami.hiramatsu.pt@hitachi.com \
--cc=ananth@in.ibm.com \
--cc=davem@davemloft.net \
--cc=linux-arch@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=rob@landley.net \
--cc=rostedt@goodmis.org \
--cc=rusty@rustcorp.com.au \
--cc=sandeepa.prabhu@linaro.org \
--cc=systemtap@sourceware.org \
--cc=virtualization@lists.linux-foundation.org \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).