Skip to content

Commit 784c2eb

Browse files
mhiramatgregkh
authored andcommitted
tracing/kprobes: Check the probe on unloaded module correctly
[ Upstream commit 59158ec ] Current kprobe event doesn't checks correctly whether the given event is on unloaded module or not. It just checks the event has ":" in the name. That is not enough because if we define a probe on non-exist symbol on loaded module, it allows to define that (with warning message) To ensure it correctly, this searches the module name on loaded module list and only if there is not, it allows to define it. (this event will be available when the target module is loaded) Link: http://lkml.kernel.org/r/153547309528.26502.8300278470528281328.stgit@devbox Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org> Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org> Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 7ecd146 commit 784c2eb

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

kernel/trace/trace_kprobe.c

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,23 @@ static nokprobe_inline bool trace_kprobe_within_module(struct trace_kprobe *tk,
6161
return strncmp(mod->name, name, len) == 0 && name[len] == ':';
6262
}
6363

64-
static nokprobe_inline bool trace_kprobe_is_on_module(struct trace_kprobe *tk)
64+
static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprobe *tk)
6565
{
66-
return !!strchr(trace_kprobe_symbol(tk), ':');
66+
char *p;
67+
bool ret;
68+
69+
if (!tk->symbol)
70+
return false;
71+
p = strchr(tk->symbol, ':');
72+
if (!p)
73+
return true;
74+
*p = '\0';
75+
mutex_lock(&module_mutex);
76+
ret = !!find_module(tk->symbol);
77+
mutex_unlock(&module_mutex);
78+
*p = ':';
79+
80+
return ret;
6781
}
6882

6983
static nokprobe_inline unsigned long trace_kprobe_nhit(struct trace_kprobe *tk)
@@ -554,19 +568,13 @@ static int __register_trace_kprobe(struct trace_kprobe *tk)
554568
else
555569
ret = register_kprobe(&tk->rp.kp);
556570

557-
if (ret == 0)
571+
if (ret == 0) {
558572
tk->tp.flags |= TP_FLAG_REGISTERED;
559-
else {
560-
if (ret == -ENOENT && trace_kprobe_is_on_module(tk)) {
561-
pr_warn("This probe might be able to register after target module is loaded. Continue.\n");
562-
ret = 0;
563-
} else if (ret == -EILSEQ) {
564-
pr_warn("Probing address(0x%p) is not an instruction boundary.\n",
565-
tk->rp.kp.addr);
566-
ret = -EINVAL;
567-
}
573+
} else if (ret == -EILSEQ) {
574+
pr_warn("Probing address(0x%p) is not an instruction boundary.\n",
575+
tk->rp.kp.addr);
576+
ret = -EINVAL;
568577
}
569-
570578
return ret;
571579
}
572580

@@ -629,6 +637,11 @@ static int register_trace_kprobe(struct trace_kprobe *tk)
629637

630638
/* Register k*probe */
631639
ret = __register_trace_kprobe(tk);
640+
if (ret == -ENOENT && !trace_kprobe_module_exist(tk)) {
641+
pr_warn("This probe might be able to register after target module is loaded. Continue.\n");
642+
ret = 0;
643+
}
644+
632645
if (ret < 0)
633646
unregister_kprobe_event(tk);
634647
else

0 commit comments

Comments
 (0)