From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A4C5BC7618B for ; Thu, 25 Jul 2019 09:39:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 74FDF2190F for ; Thu, 25 Jul 2019 09:39:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390624AbfGYJjf convert rfc822-to-8bit (ORCPT ); Thu, 25 Jul 2019 05:39:35 -0400 Received: from coyote.holtmann.net ([212.227.132.17]:34511 "EHLO mail.holtmann.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387533AbfGYJjf (ORCPT ); Thu, 25 Jul 2019 05:39:35 -0400 Received: from marcel-macbook.fritz.box (p5B3D2BA7.dip0.t-ipconnect.de [91.61.43.167]) by mail.holtmann.org (Postfix) with ESMTPSA id 178D5CED29; Thu, 25 Jul 2019 11:48:10 +0200 (CEST) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.11\)) Subject: Re: [PATCH] net: bluetooth: hci_sock: Fix a possible null-pointer dereference in hci_mgmt_cmd() From: Marcel Holtmann In-Reply-To: <20190725092253.15912-1-baijiaju1990@gmail.com> Date: Thu, 25 Jul 2019 11:39:32 +0200 Cc: Johan Hedberg , "David S. Miller" , linux-bluetooth@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: 8BIT Message-Id: References: <20190725092253.15912-1-baijiaju1990@gmail.com> To: Jia-Ju Bai X-Mailer: Apple Mail (2.3445.104.11) Sender: linux-bluetooth-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org Hi Jia-Ju, > In hci_mgmt_cmd(), there is an if statement on line 1570 to check > whether hdev is NULL: > if (hdev && chan->hdev_init) > > When hdev is NULL, it is used on line 1575: > err = handler->func(sk, hdev, cp, len); > > Some called functions of handler->func use hdev, such as: > set_appearance(), add_device() and remove_device() in mgmt.c. > > Thus, a possible null-pointer dereference may occur. > > To fix this bug, hdev is checked before calling handler->func(). > > This bug is found by a static analysis tool STCheck written by us. > > Signed-off-by: Jia-Ju Bai > --- > net/bluetooth/hci_sock.c | 11 ++++++----- > 1 file changed, 6 insertions(+), 5 deletions(-) > > diff --git a/net/bluetooth/hci_sock.c b/net/bluetooth/hci_sock.c > index d32077b28433..18ea1e47ea48 100644 > --- a/net/bluetooth/hci_sock.c > +++ b/net/bluetooth/hci_sock.c > @@ -1570,11 +1570,12 @@ static int hci_mgmt_cmd(struct hci_mgmt_chan *chan, struct sock *sk, > if (hdev && chan->hdev_init) > chan->hdev_init(sk, hdev); > > - cp = buf + sizeof(*hdr); > - > - err = handler->func(sk, hdev, cp, len); > - if (err < 0) > - goto done; > + if (hdev) { > + cp = buf + sizeof(*hdr); > + err = handler->func(sk, hdev, cp, len); > + if (err < 0) > + goto done; > + } > > err = msglen; have you evaluated the statement above: no_hdev = (handler->flags & HCI_MGMT_NO_HDEV); if (no_hdev != !hdev) { err = mgmt_cmd_status(sk, index, opcode, MGMT_STATUS_INVALID_INDEX); goto done; } I think that code is just overly complex and can be simplified, but I doubt you get to the situation where hdev is NULL for any function that requires it. Only the handler->func marked with HCI_MGMT_NO_HDEV will get hdev == NULL and these are not using it. So we might can make this easier code to really check the index != MGMT_INDEX_NONE check above to cover all cases to ensure that hdev is either valid or set to NULL before proceeding any further. And since we have a full set of unit tests in tools/mgmt-tester, I assume we would have had a chance to catch an issue like this. But we can add a test case to it to explicitly call the functions with either MGMT_INDEX_NONE used or not. Regards Marcel