trace: Protect trace_iter_expand_format against overflow and ZERO_SIZE_PTR
PATCH 1/1 trace: Protect trace_iter_expand_format against overflow and ZERO_SIZE_PTR
Add a check in trace_iter_expand_format to prevent integer overflow when calculating the new format buffer size, and to handle the case where krealloc returns ZERO_SIZE_PTR. This improves robustness and prevents potential memory corruption or kernel crashes.
--- kernel/trace/trace.c | 4 ++++ kernel/trace/trace_output.c | 2 ++ 2 files changed, 6 insertions(+)
kernel/trace/trace.c +4 -0
@@ -3596,6 +3596,10 @@ char *trace_iter_expand_format(struct trace_iterator *iter)if (!iter->tr || iter->fmt == static_fmt_buf)return NULL ; +/* Protection against overflow and ZERO_SIZE_PTR returned from krealloc */ +if (check_add_overflow (iter->fmt_size,STATIC_FMT_BUF_SIZE , &iter->fmt_size)) +return NULL ; + tmp =krealloc (iter->fmt, iter->fmt_size +STATIC_FMT_BUF_SIZE ,GFP_KERNEL );if (tmp) {
kernel/trace/trace_output.c +2 -0
@@ -979,6 +979,8 @@ static void print_fields(struct trace_iterator *iter, struct trace_event_call *c iter->fmt_size);if (ret <0 )trace_seq_printf (&iter->seq,"(0x%px)" , pos); +else if (ret ==0 ) +trace_seq_printf (&iter->seq,"(0x%px:<NULL>)" , pos);else trace_seq_printf (&iter->seq,"(0x%px:%s)" , pos, iter->fmt);
Signature · 1 line
2.43.0
Steven Rostedt
maintainerQuoted 4 lines · On Mon, 19 May 2025 09:02:40 +0200 Rafal Bilkowski <[email protected]>…
Add a check in
trace_iter_expand_formatto prevent integer overflow when calculating the new format buffer size, and to handle the case where krealloc returnsZERO_SIZE_PTR. This improves robustness and prevents potential memory corruption or kernel crashes.
What exactly is this trying to protect?
Quoted 12 lines
Signed-off-by Rafal Bilkowski <[email protected]>--- kernel/trace/trace.c | 4 ++++ kernel/trace/trace_output.c | 2 ++ 2 files changed, 6 insertions(+)kernel/trace/trace.c +0
-0
kernel/trace/trace.c char *trace_iter_expand_format(struct trace_iterator *iter) · line 3601
Quoted 6 lines
kernel/trace/trace.c +3
-0@@ -3596,6 +3596,10 @@ char *trace_iter_expand_format(struct trace_iterator *iter)if (!iter->tr || iter->fmt == static_fmt_buf)return NULL ; +/* Protection against overflow and ZERO_SIZE_PTR returned from krealloc */ +if (check_add_overflow (iter->fmt_size,STATIC_FMT_BUF_SIZE , &iter->fmt_size)) +return NULL ;
This will *NEVER* happen!
The fmt_size is initialized as STATIC_FMT_BUF_SIZE, and this is the only function that increases it, and that happens *only* if the krealloc() succeeds.
For this to happen, then the krealloc must have allocated something that would allow the format size to be INT_MAX, which krealloc would fail much earlier than that.
Not to mention, the formats can't be more that the sub-buffer size, which is by default 4K and can be at most 64K. Way smaller than INT_MAX.
Quoted 9 lines
kernel/trace/trace.c +1
-0+ tmp =krealloc (iter->fmt, iter->fmt_size +STATIC_FMT_BUF_SIZE ,GFP_KERNEL );if (tmp) {kernel/trace/trace_output.c +0
-0
kernel/trace/trace_output.c static void print_fields(struct trace_iterator *iter, struct trace_event_call *c · line 983
Quoted 5 lines
kernel/trace/trace_output.c +2
-0@@ -979,6 +979,8 @@ static void print_fields(struct trace_iterator *iter, struct trace_event_call *c iter->fmt_size);if (ret <0 )trace_seq_printf (&iter->seq,"(0x%px)" , pos); +else if (ret ==0 ) +trace_seq_printf (&iter->seq,"(0x%px:<NULL>)" , pos);
This part I'm OK with adding.
-- Steve
Quoted 3 lines
kernel/trace/trace_output.c +0
-0else trace_seq_printf (&iter->seq,"(0x%px:%s)" , pos, iter->fmt);