Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp950437imw; Fri, 8 Jul 2022 15:06:36 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vlF7ILYGuUefPr+Zr1RdzTicR0c2Sf0TpqwWVWeWIAfdBoWMXw1rguA0h/22NkEKla0yjT X-Received: by 2002:a17:907:270e:b0:72a:b476:95a with SMTP id w14-20020a170907270e00b0072ab476095amr5855324ejk.648.1657317996129; Fri, 08 Jul 2022 15:06:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657317996; cv=none; d=google.com; s=arc-20160816; b=N0HNyVRne6SbFsbBKWPKr+BrMr0AWwexjbG4PmFS0F3vBpKA9sbmn8my7n0dXcWYel FvLtuJ2I6uKeLSGsgOuGItz8dMqZl7wTGGLPOcTffpCZu6rwEa7hn/WXCQRzy6FkwzD5 pYabu45hUBDuPh0bggJjtjY9aXFH0YfVO4/n3kxhGkpIVUXFq+s5Hx3iyKZ8PopQ8cXO 1C7n6LPOR4kRk27RVVPiAlkV9jcHvaSdxDdapquLMueg0/ESgRc2GibheyvZdkzuhdLo pTcpA0uLmH8CJH8CnB/dpWP2vYrNaWUmhu3HMKaz9cYCRl6A5efAsMXXbwsO4rExGd9c bizQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date; bh=stDdaNx1pw7QSM5OWq3oYo7VNi+qZA/uVFCEw34CB+8=; b=WK0Pog+FaBqG8pNqOtWeY6Ez+0eIDigvw6ayR5SIh84bohOWyBKq0jmQ2MGK8IscyX XPHpy2HFoNsdG4G6joChYemtBIrxBeD9yS48wygravUeWH6PArkHc+npGwmRNG9xGoMO CfXhK6e8umok92eqrrCK0rQhKX8TyP9ijqUIPYWq/h+/HIZde/aqyy7Hw7YY4eyYsOHT hzdgKfIWD/9uM0SYE+jRRrN8yYpMarUaMOD1b9OAyHv5xUc5X0KSTsunS05IOoMKg5RZ uDDo71KWaZ4GTsdT2zjMxPgGGVk7VmgzbpEBTDaJsWGqjzu19SUyQbzp3pEqvaqHY8nC 3qag== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gn41-20020a1709070d2900b00727c6da65fasi28162394ejc.259.2022.07.08.15.06.10; Fri, 08 Jul 2022 15:06:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239139AbiGHWED (ORCPT + 99 others); Fri, 8 Jul 2022 18:04:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35336 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229749AbiGHWEC (ORCPT ); Fri, 8 Jul 2022 18:04:02 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 695B2192BA; Fri, 8 Jul 2022 15:04:01 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 1E334B8296F; Fri, 8 Jul 2022 22:04:00 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BCBB7C341C0; Fri, 8 Jul 2022 22:03:57 +0000 (UTC) Date: Fri, 8 Jul 2022 18:03:56 -0400 From: Steven Rostedt To: David Collins Cc: Stephen Boyd , Ingo Molnar , , , Ankit Gupta , "Gilad Avidov" , Greg Kroah-Hartman , Subject: Re: [PATCH] spmi: trace: fix stack-out-of-bound access in SPMI tracing functions Message-ID: <20220708180356.449203f9@gandalf.local.home> In-Reply-To: <20220627235512.2272783-1-quic_collinsd@quicinc.com> References: <20220627235512.2272783-1-quic_collinsd@quicinc.com> X-Mailer: Claws Mail 3.17.8 (GTK+ 2.24.33; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-6.7 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_HI,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 27 Jun 2022 16:55:12 -0700 David Collins wrote: > 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 > Signed-off-by: David Collins > --- > include/trace/events/spmi.h | 12 ++++++------ > 1 file changed, 6 insertions(+), 6 deletions(-) > > diff --git a/include/trace/events/spmi.h b/include/trace/events/spmi.h > index 8b60efe18ba6..a6819fd85cdf 100644 > --- a/include/trace/events/spmi.h > +++ b/include/trace/events/spmi.h > @@ -21,15 +21,15 @@ TRACE_EVENT(spmi_write_begin, > __field ( u8, sid ) > __field ( u16, addr ) > __field ( u8, len ) > - __dynamic_array ( u8, buf, len + 1 ) > + __dynamic_array ( u8, buf, len ) > ), > > TP_fast_assign( > __entry->opcode = opcode; > __entry->sid = sid; > __entry->addr = addr; > - __entry->len = len + 1; > - memcpy(__get_dynamic_array(buf), buf, len + 1); > + __entry->len = len; > + memcpy(__get_dynamic_array(buf), buf, len); > ), > > TP_printk("opc=%d sid=%02d addr=0x%04x len=%d buf=0x[%*phD]", > @@ -92,7 +92,7 @@ TRACE_EVENT(spmi_read_end, > __field ( u16, addr ) > __field ( int, ret ) > __field ( u8, len ) > - __dynamic_array ( u8, buf, len + 1 ) > + __dynamic_array ( u8, buf, len ) > ), > > TP_fast_assign( > @@ -100,8 +100,8 @@ TRACE_EVENT(spmi_read_end, > __entry->sid = sid; > __entry->addr = addr; > __entry->ret = ret; > - __entry->len = len + 1; > - memcpy(__get_dynamic_array(buf), buf, len + 1); > + __entry->len = len; > + memcpy(__get_dynamic_array(buf), buf, len); > ), Looks legit, Acked-by: Steven Rostedt (Google) -- Steve > > TP_printk("opc=%d sid=%02d addr=0x%04x ret=%d len=%02d buf=0x[%*phD]",