tracing/probes: Fixes several bugs
Masami Hiramatsu (Google)
maintainercover tracing/probes: Fixes several bugs
Hi,
Here are a patches to fix some bugs in the probe events and eprobe. These are found during cleaning up the code. I made these as a split series because those could be backported. Let me send another series for cleaning up probe event code.
Thank you,
---
Masami Hiramatsu (Google) (4):
tracing/probes: Avoid temporary buffer truncation in trace_probe_match_command_args() tracing/probes: Prevent out-of-bounds write in __trace_probe_log_err() tracing/probes: Fix potential underflow in LEN_OR_ZERO macro tracing/eprobe: Fix exact system name matching in eprobe_dyn_event_match()
kernel/trace/trace_eprobe.c | 3 ++- kernel/trace/trace_probe.c | 13 +++++++------ 2 files changed, 9 insertions(+), 7 deletions(-)
--
Masami Hiramatsu (Google) <[email protected]>
Masami Hiramatsu (Google)
maintainerPATCH 1/4 tracing/probes: Avoid temporary buffer truncation in trace_probe_match_command_args()
From: Masami Hiramatsu (Google) <[email protected]>
In trace_probe_match_command_args(), a stack buffer buf[MAX_ARGSTR_LEN + 1] (256 bytes) is used to format "<name>=<comm>". However, since name can be up to 32 bytes (MAX_ARG_NAME_LEN) and comm up to 255 bytes (MAX_ARGSTR_LEN), the formatted string can exceed 256 bytes and get truncated by snprintf(), causing spurious argument matching failures.
Instead of formatting into a temporary buffer on stack, compare the argument name, the '=' delimiter, and the comm expression directly.
--- kernel/trace/trace_probe.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
kernel/trace/trace_probe.c +5 -4
@@ -2338,16 +2338,17 @@ int trace_probe_compare_arg_type(struct trace_probe *a, struct trace_probe *b)bool trace_probe_match_command_args (struct trace_probe *tp,int argc,const char **argv) { -char buf[MAX_ARGSTR_LEN +1 ];int i;if (tp->nr_args < argc)return false;for (i =0 ; i < argc; i++) { -snprintf (buf,sizeof (buf),"%s=%s" , - tp->args[i].name, tp->args[i].comm); -if (strcmp (buf, argv[i])) +int len =strlen (tp->args[i].name); + +if (strncmp (argv[i], tp->args[i].name, len) || + argv[i][len] !='=' || +strcmp (argv[i] + len +1 , tp->args[i].comm))return false; }return true;
Masami Hiramatsu (Google)
maintainerPATCH 2/4 tracing/probes: Prevent out-of-bounds write in __trace_probe_log_err()
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
If trace_probe_log.argc is 0 in __trace_probe_log_err(), the loop constructing the command string will not execute and p will remain equal to command. Writing to *(p - 1) will cause an out-of-bounds access before command. This should not happen, but better to be treated.
Reject if trace_probe_log.argc is 0.
--- kernel/trace/trace_probe.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/trace/trace_probe.c +1 -1
@@ -188,7 +188,7 @@ void __trace_probe_log_err(int offset, int err_type)lockdep_assert_held (&dyn_event_ops_mutex); -if (!trace_probe_log.argv) +if (!trace_probe_log.argv || !trace_probe_log.argc)return ;/* Recalculate the length and allocate buffer */
Masami Hiramatsu (Google)
maintainerPATCH 3/4 tracing/probes: Fix potential underflow in LEN_OR_ZERO macro
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
In __set_print_fmt(), LEN_OR_ZERO is defined as (len ? len - pos : 0). If len is non-zero but smaller than pos, len - pos evaluates to a negative integer. When passed as a size argument to snprintf(), this negative value is cast to a large unsigned size_t, bypassing buffer size limits.
Ensure len > pos before subtracting to avoid integer underflow.
--- kernel/trace/trace_probe.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/trace/trace_probe.c +1 -1
@@ -2013,7 +2013,7 @@ int traceprobe_update_arg(struct probe_arg *arg) }/* When len=0, we just calculate the needed length */ -#define LEN_OR_ZERO (len ? len - pos : 0) +#define LEN_OR_ZERO (len > pos ? len - pos : 0)static int __set_print_fmt (struct trace_probe *tp,char *buf,int len,enum probe_print_type ptype) {
Masami Hiramatsu (Google)
maintainerPATCH 4/4 tracing/eprobe: Fix exact system name matching in eprobe_dyn_event_match()
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
eprobe_dyn_event_match() checks if the target event system in argv[0] matches ep->event_system using strncmp(ep->event_system, argv[0], len). However, if ep->event_system is longer than len (e.g. "eprobes" vs "ep/event"), strncmp() still returns 0 because the first len characters match.
Check that ep->event_system[len] is '\0' to ensure exact system name matching.
--- kernel/trace/trace_eprobe.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/trace/trace_eprobe.c +2 -1
@@ -172,7 +172,8 @@ static bool eprobe_dyn_event_match(const char *system, const char *event,if (!slash)return false; -if (strncmp (ep->event_system, argv[0 ], slash - argv[0 ])) +if (strncmp (ep->event_system, argv[0 ], slash - argv[0 ]) || + ep->event_system[slash - argv[0 ]] !='\0' )return false;if (strcmp (ep->event_name, slash +1 ))return false;