Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp38065189rwd; Wed, 12 Jul 2023 02:28:06 -0700 (PDT) X-Google-Smtp-Source: APBJJlEq3pbK14mx2ZZX6Omr3OZzI9p7ApWBVzJ4s7b/pmQkACHMzc1SB6Ea0ulhPNNmwvpbp05k X-Received: by 2002:a05:6a00:b93:b0:668:82fe:16f1 with SMTP id g19-20020a056a000b9300b0066882fe16f1mr23133139pfj.1.1689154086117; Wed, 12 Jul 2023 02:28:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689154086; cv=none; d=google.com; s=arc-20160816; b=orfQi53XqNs4ibERuhfj20xmZorE5gmzMXUxZpeWf3CcQVqFW0F9hltFfVN6IYTHFg km8EPjtEW+gQNlNpdwCKu5IDPYyGWz/NAePVjdcZWpTBxAFb5Fq3tWR4TpH5E1tJG6vd mMEoiwo9lBsiKJnOjaiE3AEwn4E8vfyy+gJYWCAUkkq3XI44yM0zvSI/7NFw2GanQAwj 8MuA1D6nTcRNKdmjr5viUvRkXEsuoq77P2JTh37jYVmaIGr2IQfaEWpU2hrO4jwWdx8P NFFd7tt3x4LjEq2wjk1oJVPdInN6dVqvgQ5T9wXynlqE2QPVa1SlJvDueN1QabSetxUn rA8g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id; bh=gaiNhIX2zBmKdEw1RCdvmWnzX0EPJch5VYxoShDG7T0=; fh=uIB1GtCTDX2hv/g4wtuR0S8YkCa+1KEtIQ1bMn99WCk=; b=LpHZxrcRiDRQfW95D17ZWhE49Pd9Aecx6KaR5clMI/lurCKsIuIr3KH2y8Vzde+tQJ oCEXvZAm4T2TDSNk8VGMP8bqmqaJEacYDH2zB4IkVLc/9AeUbz0Z7S2xQFS6wYadMooG AUTh7Ke9gi5RgPBLjciTKX2bMgUiyOD3nvBu8R0aSjTAC5wgjnHNuL9Q0N30a5k7fm9Z 4nXpX314aDRgw4X/YKRffcZmKCgVkE92VSNYvz+voqIduVLKtaEtmvOuBlQihKw3B500 uS8UXhoNjZNF5OHU/A4XCOryUGTc+PHRg1xerkdh5Fi+nnpxWW/v1uSsyzKpFU9Srx6g Cmkg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-bluetooth-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 200-20020a6217d1000000b0063b356e36fesi2885217pfx.372.2023.07.12.02.27.44; Wed, 12 Jul 2023 02:28:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth-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-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232441AbjGLJZv (ORCPT + 99 others); Wed, 12 Jul 2023 05:25:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42344 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231472AbjGLJZu (ORCPT ); Wed, 12 Jul 2023 05:25:50 -0400 Received: from mx3.molgen.mpg.de (mx3.molgen.mpg.de [141.14.17.11]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 10DEBE5F; Wed, 12 Jul 2023 02:25:48 -0700 (PDT) Received: from [141.14.220.45] (g45.guest.molgen.mpg.de [141.14.220.45]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: pmenzel) by mx.molgen.mpg.de (Postfix) with ESMTPSA id 472E361E5FE03; Wed, 12 Jul 2023 11:25:21 +0200 (CEST) Message-ID: Date: Wed, 12 Jul 2023 11:25:20 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 Subject: Re: [PATCH v3] Bluetooth: btmtk: Fix null pointer when processing coredump To: =?UTF-8?B?Q2hyaXMgTHUgKOmZuOeomuazkyk=?= Cc: marcel@holtmann.org, linux-kernel@vger.kernel.org, linux-mediatek@lists.infradead.org, linux-bluetooth@vger.kernel.org, johan.hedberg@gmail.com, =?UTF-8?B?U3RldmUgTGVlICjmnY7oppboqqAp?= , Sean Wang , =?UTF-8?B?QWFyb24gSG91ICjkvq/kv4rku7Ap?= , Luiz Augusto von Dentz References: <20230712051857.13812-1-chris.lu@mediatek.com> <0cb29d27-a76f-47f2-86c3-f39ba25e8bc2@molgen.mpg.de> Content-Language: en-US From: Paul Menzel In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_BLOCKED,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-bluetooth@vger.kernel.org Dear Chris, Am 12.07.23 um 10:53 schrieb Chris Lu (陸稚泓): > On Wed, 2023-07-12 at 08:11 +0200, Paul Menzel wrote: >> >> External email : Please do not click links or open attachments until >> you have verified the sender or the content. (It’d be nice if you removed such (automatically added) phrases from your reply.) > Thanks for your review and feedback to Mediatek's Bluetooth driver > code. Thank you for your reply. >> Am 12.07.23 um 07:18 schrieb Chris Lu: >>> There may be a potential null pointer risk if offset value is >>> less than 0 when doing memcmp in btmtk_process_coredump(). >>> Checking offset is valid before doing memcmp. >> >> Use imperative mood: Check offset … >> >>> Signed-off-by: Chris Lu >>> Co-developed-by: Sean Wang >>> Signed-off-by: Sean Wang >>> --- >>> v2: fix typo >>> v3: fix bot checking error >>> --- >>> drivers/bluetooth/btmtk.c | 16 ++++++++-------- >>> 1 file changed, 8 insertions(+), 8 deletions(-) >>> >>> diff --git a/drivers/bluetooth/btmtk.c b/drivers/bluetooth/btmtk.c >>> index 786f775196ae..0f290430ae0e 100644 >>> --- a/drivers/bluetooth/btmtk.c >>> +++ b/drivers/bluetooth/btmtk.c >>> @@ -370,7 +370,7 @@ EXPORT_SYMBOL_GPL(btmtk_register_coredump); >>> int btmtk_process_coredump(struct hci_dev *hdev, struct sk_buff *skb) >>> { >>> struct btmediatek_data *data = hci_get_priv(hdev); >>> -int err; >>> +int err, offset; >>> >>> if (!IS_ENABLED(CONFIG_DEV_COREDUMP)) >>> return 0; >>> @@ -392,15 +392,15 @@ int btmtk_process_coredump(struct hci_dev >> *hdev, struct sk_buff *skb) >>> if (err < 0) >>> break; >>> data->cd_info.cnt++; >>> +offset = skb->len - sizeof(MTK_COREDUMP_END); >> >> For `sizeof()` shouldn’t you use `size_t`? But that is unsigned of >> course. Maybe ssize_t then? > > yes, it's better to use ssize_t or size_t, I'll change declaratins of > offset from int to ssize_t. > >>> >>> /* Mediatek coredump data would be more than MTK_COREDUMP_NUM */ >>> -if (data->cd_info.cnt > MTK_COREDUMP_NUM && >>> - skb->len > sizeof(MTK_COREDUMP_END) && >>> - !memcmp((char *)&skb->data[skb->len - sizeof(MTK_COREDUMP_END)], >>> - MTK_COREDUMP_END, sizeof(MTK_COREDUMP_END) - 1)) { >>> -bt_dev_info(hdev, "Mediatek coredump end"); >>> -hci_devcd_complete(hdev); >>> -} >>> +if (data->cd_info.cnt > MTK_COREDUMP_NUM && offset > 0) >> >> Why not keep it like before, and just add the condition `skb->len < >> sizeof(MTK_COREDUMP_END)`? The compiler is probably going to optimize >> so the value is not calculated twice. > The reason why I send this patch is when I backport devcoredump feature > to specific project with older kernel version, the compiler might not > so optimized that it would cause kernel panic when run into memcmp. > As a result, make sure `skb->len > sizeof(MTK_COREDUMP_END) ` before > doing memcmp part can avoid null pointer issue. > Besides, only in condition 'data->cd_info.cnt > MTK_COREDUMP_NUM && > offset > 0' need to do memcmp to check the end of coredump. Driver do > noting with condition `skb->len < sizeof(MTK_COREDUMP_END) ` that > additional condiction is not really necessary. Just to avoid misunderstandings, my point, to add the comparison and get rid of the variable `offset`. Kind regards, Paul >>> +if (!memcmp((char *)&skb->data[offset], MTK_COREDUMP_END, >>> + sizeof(MTK_COREDUMP_END) - 1)) { >>> +bt_dev_info(hdev, "Mediatek coredump end"); >>> +hci_devcd_complete(hdev); >>> +} >>> >>> break; >>> }