Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp22962090rwd; Fri, 30 Jun 2023 15:34:03 -0700 (PDT) X-Google-Smtp-Source: APBJJlFd61mngEzSsV8us06EwtNTaf6IahrWVMRrg/P9xurragXWfzILkpJuSyXOO2yWQ9HURChn X-Received: by 2002:a92:cac8:0:b0:345:d49a:e4b6 with SMTP id m8-20020a92cac8000000b00345d49ae4b6mr3548286ilq.15.1688164443184; Fri, 30 Jun 2023 15:34:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688164443; cv=none; d=google.com; s=arc-20160816; b=AaM2RPsDg2ncO6auLZyIsvhsjdENxqJ0ggWCy3q7uLBsQXbH5CGiAn4O+D9MzOvcFz iW6of2h70CgnZCbqCS4vv9Tah4LHtb5f0Jt6g1BLzyob9bbLam1ByzQixQ67DV2kGSBs lkKSCjLYTsPD3iKiXTbOp1RqR3QhBKs+XuWM3nJUQjgFYbda2X++yf5Up2ClaU7pP2SL kcOPe38l5SgaKQuLJcFsp1QiaGbzGWyI1xRQIfXySW4ThJHW3HkxpTubuqhnnEpyksEi FMdJjIXQlszwMff5btg3yHQpkysYdCiMT0f7B/JeWDi+xA1cncQCJNGJ72SXE6DvRLYM LMng== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=nq1QcjMnGfIZ/W8d+xE8ssqtAAponfz0QGs18ghKecs=; fh=60tGFC72lEsfjbN6e3AcXrRl0Hn4cz3CZzXi4n3bLI0=; b=iIHHyNRrKyYrlCYp0tEYbsHCbcP/1oKXZYuIPLrIua3ojZJcFmSdAcis9KYYlxoqNA ZQB1zGPadSBEzhfeics6vJZU6sJC+DWzyBJTZJslIXJa50EthQzkYhLeX3f71khNXOsa u8tUdC+1DrH5VmigfqhQGV+PgHh+STJcW6FMl7JAqOd8G+ixte/Te/5yaR+HQh5utc6V Pg2ql0ihLnCFgNkgf+UcYc0/qKM4MXnhOO2QRMyvWrXms6NIkJZW/I198jwqCaXTTKdI a6i+2PNiElMpfdarvZxiOj0mzFSdlHfG9Ttg6n1glTw6QSezgKnQRFdq7vJ9VT0SN9eT h9SA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=ePmdCCgl; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j20-20020a63cf14000000b00557888f1a61si13576687pgg.259.2023.06.30.15.33.50; Fri, 30 Jun 2023 15:34:03 -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; dkim=pass header.i=@chromium.org header.s=google header.b=ePmdCCgl; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232651AbjF3Wdr (ORCPT + 99 others); Fri, 30 Jun 2023 18:33:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57646 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232355AbjF3Wdq (ORCPT ); Fri, 30 Jun 2023 18:33:46 -0400 Received: from mail-oa1-x2f.google.com (mail-oa1-x2f.google.com [IPv6:2001:4860:4864:20::2f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 980773C01 for ; Fri, 30 Jun 2023 15:33:45 -0700 (PDT) Received: by mail-oa1-x2f.google.com with SMTP id 586e51a60fabf-1b03ec2015fso2107256fac.3 for ; Fri, 30 Jun 2023 15:33:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1688164425; x=1690756425; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=nq1QcjMnGfIZ/W8d+xE8ssqtAAponfz0QGs18ghKecs=; b=ePmdCCgl438/fHKG9lmQLCDSCX0oTeNziNiyk4EgYgOQHYihUa9mQvSiGhIgLkH12I eBRWE1x2Ip5ar6Y6JvgY4D1KTJZQd+yBAdpbYu/Rhf7isIn2Jw5qwjf3VTExUuBWbwhC P08HSNG4mlMeeAQE9D9MRInFCXzBXHyGCT+SA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688164425; x=1690756425; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=nq1QcjMnGfIZ/W8d+xE8ssqtAAponfz0QGs18ghKecs=; b=O+pSDbTH4YHnIW7ZPkFRjKlx1x6nAoTSbp0PgNoIcx/JpcoQvjQdspZUqLsBccWg91 JzmFdl47O/1NImJ19E7nivdJdt1eGhvh8Rf3AbLE3WS2IlL5LR/R6QiZTw+/9+jaE4OT MN3dlWvE7UBdgBAYPiHZ3XOiVmc+Mc2rVEHc8FDStDtOftgVTlekWRxxLOVeuAd4oQrW 0HL85izmW+HTWT+oVRtjSnpe8pg3X6IAoVk3QSFEPB9iYwzMkFEhi1ECexJ7PvULf7jS 7aZdh0BRP5DEbpxC+ehFD5B9lRkCGHNy2GvV+QZ4KBi4yBbhVSeGWiwpXqfVNwpHBWgp hv/Q== X-Gm-Message-State: ABy/qLbjQJsqVOaLgANrLypf67cx5B1HsyqscN7WVJlvvfWpkXGs++IB DsaFGDDirC82Pzqx0AnYiFuv5Q== X-Received: by 2002:a05:6870:e92:b0:1a2:8ebd:7d46 with SMTP id mm18-20020a0568700e9200b001a28ebd7d46mr5013095oab.21.1688164424896; Fri, 30 Jun 2023 15:33:44 -0700 (PDT) Received: from tictac2.mtv.corp.google.com ([2620:15c:9d:2:e9c4:8375:7234:e6c2]) by smtp.gmail.com with ESMTPSA id em23-20020a17090b015700b0026356c056cbsm3105864pjb.34.2023.06.30.15.33.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Jun 2023 15:33:44 -0700 (PDT) From: Douglas Anderson To: Marcel Holtmann , Johan Hedberg , Luiz Augusto von Dentz Cc: Stephen Boyd , Douglas Anderson , Manish Mandlik , Miao-chen Chou , linux-bluetooth@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 1/2] Bluetooth: hci_sync: Avoid use-after-free in dbg for hci_remove_adv_monitor() Date: Fri, 30 Jun 2023 15:33:14 -0700 Message-ID: <20230630153315.v3.1.I3b7c8905728f3124576361ca35ed28e37f12f5d1@changeid> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 KASAN reports that there's a use-after-free in hci_remove_adv_monitor(). Trawling through the disassembly, you can see that the complaint is from the access in bt_dev_dbg() under the HCI_ADV_MONITOR_EXT_MSFT case. The problem case happens because msft_remove_monitor() can end up freeing the monitor structure. Specifically: hci_remove_adv_monitor() -> msft_remove_monitor() -> msft_remove_monitor_sync() -> msft_le_cancel_monitor_advertisement_cb() -> hci_free_adv_monitor() Let's fix the problem by just stashing the relevant data when it's still valid. Fixes: 7cf5c2978f23 ("Bluetooth: hci_sync: Refactor remove Adv Monitor") Signed-off-by: Douglas Anderson --- Changes in v3: - Back to v1 where we stash the handle in a local. Changes in v2: - Move the print, don't stash handle in a local. net/bluetooth/hci_core.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c index 48917c68358d..dbb2043a9112 100644 --- a/net/bluetooth/hci_core.c +++ b/net/bluetooth/hci_core.c @@ -1972,6 +1972,7 @@ static int hci_remove_adv_monitor(struct hci_dev *hdev, struct adv_monitor *monitor) { int status = 0; + int handle; switch (hci_get_adv_monitor_offload_ext(hdev)) { case HCI_ADV_MONITOR_EXT_NONE: /* also goes here when powered off */ @@ -1980,9 +1981,10 @@ static int hci_remove_adv_monitor(struct hci_dev *hdev, goto free_monitor; case HCI_ADV_MONITOR_EXT_MSFT: + handle = monitor->handle; status = msft_remove_monitor(hdev, monitor); bt_dev_dbg(hdev, "%s remove monitor %d msft status %d", - hdev->name, monitor->handle, status); + hdev->name, handle, status); break; } -- 2.41.0.255.g8b1d071c50-goog