Skip to content

Commit b022f0c

Browse files
eiffel-flmhiramat
authored andcommitted
tracing/kprobes: Return EADDRNOTAVAIL when func matches several symbols
When a kprobe is attached to a function that's name is not unique (is static and shares the name with other functions in the kernel), the kprobe is attached to the first function it finds. This is a bug as the function that it is attaching to is not necessarily the one that the user wants to attach to. Instead of blindly picking a function to attach to what is ambiguous, error with EADDRNOTAVAIL to let the user know that this function is not unique, and that the user must use another unique function with an address offset to get to the function they want to attach to. Link: https://lore.kernel.org/all/[email protected]/ Cc: [email protected] Fixes: 413d37d ("tracing: Add kprobe-based event tracer") Suggested-by: Masami Hiramatsu <[email protected]> Signed-off-by: Francis Laniel <[email protected]> Link: https://lore.kernel.org/lkml/[email protected]/ Acked-by: Masami Hiramatsu (Google) <[email protected]> Signed-off-by: Masami Hiramatsu (Google) <[email protected]>
1 parent 700b2b4 commit b022f0c

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

kernel/trace/trace_kprobe.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,25 @@ static struct notifier_block trace_kprobe_module_nb = {
705705
.priority = 1 /* Invoked after kprobe module callback */
706706
};
707707

708+
static int count_symbols(void *data, unsigned long unused)
709+
{
710+
unsigned int *count = data;
711+
712+
(*count)++;
713+
714+
return 0;
715+
}
716+
717+
static unsigned int number_of_same_symbols(char *func_name)
718+
{
719+
unsigned int count;
720+
721+
count = 0;
722+
kallsyms_on_each_match_symbol(count_symbols, func_name, &count);
723+
724+
return count;
725+
}
726+
708727
static int __trace_kprobe_create(int argc, const char *argv[])
709728
{
710729
/*
@@ -836,6 +855,31 @@ static int __trace_kprobe_create(int argc, const char *argv[])
836855
}
837856
}
838857

858+
if (symbol && !strchr(symbol, ':')) {
859+
unsigned int count;
860+
861+
count = number_of_same_symbols(symbol);
862+
if (count > 1) {
863+
/*
864+
* Users should use ADDR to remove the ambiguity of
865+
* using KSYM only.
866+
*/
867+
trace_probe_log_err(0, NON_UNIQ_SYMBOL);
868+
ret = -EADDRNOTAVAIL;
869+
870+
goto error;
871+
} else if (count == 0) {
872+
/*
873+
* We can return ENOENT earlier than when register the
874+
* kprobe.
875+
*/
876+
trace_probe_log_err(0, BAD_PROBE_ADDR);
877+
ret = -ENOENT;
878+
879+
goto error;
880+
}
881+
}
882+
839883
trace_probe_log_set_index(0);
840884
if (event) {
841885
ret = traceprobe_parse_event_name(&event, &group, gbuf,
@@ -1695,6 +1739,7 @@ static int unregister_kprobe_event(struct trace_kprobe *tk)
16951739
}
16961740

16971741
#ifdef CONFIG_PERF_EVENTS
1742+
16981743
/* create a trace_kprobe, but don't add it to global lists */
16991744
struct trace_event_call *
17001745
create_local_trace_kprobe(char *func, void *addr, unsigned long offs,
@@ -1705,6 +1750,24 @@ create_local_trace_kprobe(char *func, void *addr, unsigned long offs,
17051750
int ret;
17061751
char *event;
17071752

1753+
if (func) {
1754+
unsigned int count;
1755+
1756+
count = number_of_same_symbols(func);
1757+
if (count > 1)
1758+
/*
1759+
* Users should use addr to remove the ambiguity of
1760+
* using func only.
1761+
*/
1762+
return ERR_PTR(-EADDRNOTAVAIL);
1763+
else if (count == 0)
1764+
/*
1765+
* We can return ENOENT earlier than when register the
1766+
* kprobe.
1767+
*/
1768+
return ERR_PTR(-ENOENT);
1769+
}
1770+
17081771
/*
17091772
* local trace_kprobes are not added to dyn_event, so they are never
17101773
* searched in find_trace_kprobe(). Therefore, there is no concern of

kernel/trace/trace_probe.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ extern int traceprobe_define_arg_fields(struct trace_event_call *event_call,
450450
C(BAD_MAXACT, "Invalid maxactive number"), \
451451
C(MAXACT_TOO_BIG, "Maxactive is too big"), \
452452
C(BAD_PROBE_ADDR, "Invalid probed address or symbol"), \
453+
C(NON_UNIQ_SYMBOL, "The symbol is not unique"), \
453454
C(BAD_RETPROBE, "Retprobe address must be an function entry"), \
454455
C(NO_TRACEPOINT, "Tracepoint is not found"), \
455456
C(BAD_ADDR_SUFFIX, "Invalid probed address suffix"), \

0 commit comments

Comments
 (0)