Skip to content

Commit 023cc83

Browse files
committed
Merge tag 'probes-fixes-v6.6-rc6.2' of git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace
Pull probes fixes from Masami Hiramatsu: - kprobe-events: Fix kprobe events to reject if the attached symbol is not unique name because it may not the function which the user want to attach to. (User can attach a probe to such symbol using the nearest unique symbol + offset.) - selftest: Add a testcase to ensure the kprobe event rejects non unique symbol correctly. * tag 'probes-fixes-v6.6-rc6.2' of git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace: selftests/ftrace: Add new test case which checks non unique symbol tracing/kprobes: Return EADDRNOTAVAIL when func matches several symbols
2 parents 4d7b04c + 03b80ff commit 023cc83

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-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"), \
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/sh
2+
# SPDX-License-Identifier: GPL-2.0
3+
# description: Test failure of registering kprobe on non unique symbol
4+
# requires: kprobe_events
5+
6+
SYMBOL='name_show'
7+
8+
# We skip this test on kernel where SYMBOL is unique or does not exist.
9+
if [ "$(grep -c -E "[[:alnum:]]+ t ${SYMBOL}" /proc/kallsyms)" -le '1' ]; then
10+
exit_unsupported
11+
fi
12+
13+
! echo "p:test_non_unique ${SYMBOL}" > kprobe_events

0 commit comments

Comments
 (0)