Skip to contentSkip to conversation

tracing: Require kallsyms permission for the .function filter modifier

PATCH New JunYoung Lee 2 participants 2 messages Nov 19 Tracing

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tracing: Require kallsyms permission for the .function filter modifier

Hi Steven, Masami,

An unprivileged user can locate the kernel image (KASLR bypass) with PERF_EVENT_IOC_SET_FILTER at the default perf_event_paranoid=2. parse_pred() accepts "<field>.function == <addr>" only if the address resolves in kallsyms, and the perf path has no permission check, so the ioctl result tells whether an address is kernel text. The details and the fix are in the patch below.

Affected (reproduced as uid 65534, KASLR on), since e6745a4da964 (v6.3):
- mainline 4405c18c95d2 (v7.4-rc3-230)
- stable 6.12.118

Example on mainline: a counting event on ftrace:print (field "ip") and 561 probes return image_start=0xffffffffa1000000 and image_end=0xffffffffaae30000. These equal _stext and _end in /proc/kallsyms as read by root; the same user sees zeros there. No tracefs and no capabilities are needed. Not affected:
perf_event_paranoid > 2, or perf_event_open blocked.

A C reproducer is available privately on request.

Testing of the patch on mainline with KASAN: for an unprivileged user every .function form now fails with -EINVAL, and ordinary filters still work. Root keeps .function via perf and tracefs (it is refused under kptr_restrict=2, like kallsyms values). ftrace selftests test.d/filter pass 4/4. checkpatch --strict and W=1 are clean. A 6.12.y backport is available.

A separate issue seen while testing: as root, a .function filter on the perf function event (ftrace:function) crashes the kernel with a NULL dereference in ftrace_function_set_filter_pred(), because pred->regex is not set on the .function path. It needs CAP_PERFMON. I can send details if useful.

Found with AI-assisted code analysis. All results come from test runs.

Thanks,
JunYoung Lee

-- >8 --
"<field>.function == <operand>" resolves the operand to a kernel symbol range. parse_pred() accepts the predicate only if kallsyms_lookup_size_offset() succeeds, so the result of setting the filter tells whether an address lies inside the kernel image (or a module).

perf_event_open() allows any user to open a tracepoint event for counting at the default perf_event_paranoid=2, and PERF_EVENT_IOC_SET_FILTER passes the string to the same parser without a permission check. An unprivileged user can therefore probe addresses one ioctl at a time and recover _stext and _end exactly (about 560 ioctls on x86-64, under two seconds), while /proc/kallsyms shows that user zeros.

Apply the rule that /proc/kallsyms uses, kallsyms_show_value() on the current credentials, before the operand is looked at, so both the numeric and the symbol name form are refused to callers who may not see kernel addresses. Root and CAP_SYSLOG keep the modifier under the default kptr_restrict. With kptr_restrict=2 it is refused to everyone, the same as kallsyms values are hidden from everyone.

Fixes e6745a4da964 ("tracing: Add a way to filter function addresses to function names")
---
 kernel/trace/trace_events_filter.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
kernel/trace/trace_events_filter.c +11 -0
index c10951bcfc5f..895ef6d7652a 100644
@@ -118,6 +118,7 @@ struct filter_pred {
 	C(IP_FIELD_ONLY,	"Only 'ip' field is supported for function trace"), \
 	C(INVALID_VALUE,	"Invalid value (did you forget quotes)?"), \
 	C(NO_FUNCTION,		"Function not found"),			\
+	C(FUNCTION_PERM,	"Not permitted to filter on kernel addresses"), \
 	C(GLOB_WITHIN,		"'within' filter cannot take glob expressions"), \
 	C(ERRNO,		"Error"),				\
 	C(NO_FILTER,		"No filter found")
@@ -1743,6 +1744,16 @@ static int parse_pred(const char *str, void *data,
 	pred->op = op;
 
 	if (function) {
+		/*
+		 * Whether the operand is accepted, and which events then
+		 * match, tells where kernel text is. perf lets unprivileged
+		 * users set filters, so apply the /proc/kallsyms rule.
+		 */
+		if (!kallsyms_show_value(current_cred())) {
+			parse_error(pe, FILT_ERR_FUNCTION_PERM, pos + i);
+			goto err_free;
+		}
+
 		/* The field must be the same size as long */
 		if (field->size != sizeof(long)) {
 			parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i);
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] tracing: unstripped cover letter and scissors line in commit message --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] tracing: unstripped cover letter and scissors line in commit message
--

commit 8240d9570c52f6cf2edfd9796cbc32ecb0f181af
Author: JunYoung Lee <[email protected]>

Quoted 64 lines

tracing: Require kallsyms permission for the .function filter modifier

Hi Steven, Masami,

An unprivileged user can locate the kernel image (KASLR bypass) with PERF_EVENT_IOC_SET_FILTER at the default perf_event_paranoid=2. parse_pred() accepts "<field>.function == <addr>" only if the address resolves in kallsyms, and the perf path has no permission check, so the ioctl result tells whether an address is kernel text. The details and the fix are in the patch below.

Affected (reproduced as uid 65534, KASLR on), since e6745a4da964 (v6.3):
- mainline 4405c18c95d2 (v7.4-rc3-230)
- stable 6.12.118

Example on mainline: a counting event on ftrace:print (field "ip") and 561 probes return image_start=0xffffffffa1000000 and image_end=0xffffffffaae30000. These equal _stext and _end in /proc/kallsyms as read by root; the same user sees zeros there. No tracefs and no capabilities are needed. Not affected:
perf_event_paranoid > 2, or perf_event_open blocked.

A C reproducer is available privately on request.

Testing of the patch on mainline with KASAN: for an unprivileged user every .function form now fails with -EINVAL, and ordinary filters still work. Root keeps .function via perf and tracefs (it is refused under kptr_restrict=2, like kallsyms values). ftrace selftests test.d/filter pass 4/4. checkpatch --strict and W=1 are clean. A 6.12.y backport is available.

A separate issue seen while testing: as root, a .function filter on the perf function event (ftrace:function) crashes the kernel with a NULL dereference in ftrace_function_set_filter_pred(), because pred->regex is not set on the .function path. It needs CAP_PERFMON. I can send details if useful.

Found with AI-assisted code analysis. All results come from test runs.

Thanks,
JunYoung Lee

-- >8 --
"<field>.function == <operand>" resolves the operand to a kernel symbol range. parse_pred() accepts the predicate only if kallsyms_lookup_size_offset() succeeds, so the result of setting the filter tells whether an address lies inside the kernel image (or a module).

perf_event_open() allows any user to open a tracepoint event for counting at the default perf_event_paranoid=2, and PERF_EVENT_IOC_SET_FILTER passes the string to the same parser without a permission check. An unprivileged user can therefore probe addresses one ioctl at a time and recover _stext and _end exactly (about 560 ioctls on x86-64, under two seconds), while /proc/kallsyms shows that user zeros.

Apply the rule that /proc/kallsyms uses, kallsyms_show_value() on the current credentials, before the operand is looked at, so both the numeric and the symbol name form are refused to callers who may not see kernel addresses. Root and CAP_SYSLOG keep the modifier under the default kptr_restrict. With kptr_restrict=2 it is refused to everyone, the same as kallsyms values are hidden from everyone.

Quoted 3 lines
Fixes e6745a4da964 ("tracing: Add a way to filter function addresses to function names")

[Severity: Low]
This isn't a bug, but the commit message retains the email greeting, notes for the maintainers, and the "-- >8 --" scissors line above the actual changelog. Transient commentary intended for maintainers belongs below the "---" separator after the Signed-off-by trailer (or should be stripped with "git am --scissors") so it does not end up in permanent git history.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1