Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

spmi: trace: fix stack-out-of-bound access in SPMI tracing functions

trace_spmi_write_begin() and trace_spmi_read_end() both call
memcpy() with a length of "len + 1". This leads to one extra
byte being read beyond the end of the specified buffer. Fix
this out-of-bound memory access by using a length of "len"
instead.

Here is a KASAN log showing the issue:

BUG: KASAN: stack-out-of-bounds in trace_event_raw_event_spmi_read_end+0x1d0/0x234
Read of size 2 at addr ffffffc0265b7540 by task thermal@2.0-ser/1314
...
Call trace:
dump_backtrace+0x0/0x3e8
show_stack+0x2c/0x3c
dump_stack_lvl+0xdc/0x11c
print_address_description+0x74/0x384
kasan_report+0x188/0x268
kasan_check_range+0x270/0x2b0
memcpy+0x90/0xe8
trace_event_raw_event_spmi_read_end+0x1d0/0x234
spmi_read_cmd+0x294/0x3ac
spmi_ext_register_readl+0x84/0x9c
regmap_spmi_ext_read+0x144/0x1b0 [regmap_spmi]
_regmap_raw_read+0x40c/0x754
regmap_raw_read+0x3a0/0x514
regmap_bulk_read+0x418/0x494
adc5_gen3_poll_wait_hs+0xe8/0x1e0 [qcom_spmi_adc5_gen3]
...
__arm64_sys_read+0x4c/0x60
invoke_syscall+0x80/0x218
el0_svc_common+0xec/0x1c8
...

addr ffffffc0265b7540 is located in stack of task thermal@2.0-ser/1314 at offset 32 in frame:
adc5_gen3_poll_wait_hs+0x0/0x1e0 [qcom_spmi_adc5_gen3]

this frame has 1 object:
[32, 33) 'status'

Memory state around the buggy address:
ffffffc0265b7400: 00 00 00 00 00 00 00 00 00 00 00 00 f1 f1 f1 f1
ffffffc0265b7480: 04 f3 f3 f3 00 00 00 00 00 00 00 00 00 00 00 00
>ffffffc0265b7500: 00 00 00 00 f1 f1 f1 f1 01 f3 f3 f3 00 00 00 00
^
ffffffc0265b7580: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
ffffffc0265b7600: f1 f1 f1 f1 01 f2 07 f2 f2 f2 01 f3 00 00 00 00
==================================================================

Fixes: a9fce374815d ("spmi: add command tracepoints for SPMI")
Cc: stable@vger.kernel.org
Reviewed-by: Stephen Boyd <sboyd@kernel.org>
Acked-by: Steven Rostedt (Google) <rostedt@goodmis.org>
Signed-off-by: David Collins <quic_collinsd@quicinc.com>
Link: https://lore.kernel.org/r/20220627235512.2272783-1-quic_collinsd@quicinc.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

David Collins and committed by
Greg Kroah-Hartman
2af28b24 2a852fbe

+6 -6
+6 -6
include/trace/events/spmi.h
··· 21 21 __field ( u8, sid ) 22 22 __field ( u16, addr ) 23 23 __field ( u8, len ) 24 - __dynamic_array ( u8, buf, len + 1 ) 24 + __dynamic_array ( u8, buf, len ) 25 25 ), 26 26 27 27 TP_fast_assign( 28 28 __entry->opcode = opcode; 29 29 __entry->sid = sid; 30 30 __entry->addr = addr; 31 - __entry->len = len + 1; 32 - memcpy(__get_dynamic_array(buf), buf, len + 1); 31 + __entry->len = len; 32 + memcpy(__get_dynamic_array(buf), buf, len); 33 33 ), 34 34 35 35 TP_printk("opc=%d sid=%02d addr=0x%04x len=%d buf=0x[%*phD]", ··· 92 92 __field ( u16, addr ) 93 93 __field ( int, ret ) 94 94 __field ( u8, len ) 95 - __dynamic_array ( u8, buf, len + 1 ) 95 + __dynamic_array ( u8, buf, len ) 96 96 ), 97 97 98 98 TP_fast_assign( ··· 100 100 __entry->sid = sid; 101 101 __entry->addr = addr; 102 102 __entry->ret = ret; 103 - __entry->len = len + 1; 104 - memcpy(__get_dynamic_array(buf), buf, len + 1); 103 + __entry->len = len; 104 + memcpy(__get_dynamic_array(buf), buf, len); 105 105 ), 106 106 107 107 TP_printk("opc=%d sid=%02d addr=0x%04x ret=%d len=%02d buf=0x[%*phD]",