From patchwork Fri Jun 30 22:33:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Doug Anderson X-Patchwork-Id: 114909 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp10699388vqr; Fri, 30 Jun 2023 15:35:27 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4XDUbh+6rKxA5vkW/CvPlgIkoc/eBFMjyuT2r0VUpB11172vJ+BLKMubK4cu0mpsMVsvMv X-Received: by 2002:a9d:7a96:0:b0:6b7:4af2:492d with SMTP id l22-20020a9d7a96000000b006b74af2492dmr3668038otn.38.1688164527292; Fri, 30 Jun 2023 15:35:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688164527; cv=none; d=google.com; s=arc-20160816; b=P+YM4D/khBPCBRzKjfxwmxw2O4n+xZN/EKGiI9XDfqRdV2jZlsZG8XfIJm28qiPovo QBajNEdvXLiSniSNZwtvoR85hjQ1CC/3codnbWTuKA+mZ3pNIGDOwwt31SUmlgn8IzUH YsXcWoiDmnI7ZIg8eEbohL2uLA2NUsVd4v8nfpGRZm1rGYZkYJ+7Ja5qjr4Nn2VT7aEF h1qouhgNnp6YPnUt885Uz/btR37K2ZowMf+tL5b6BgdDUHUO6uAh+8UbErVS6f9GvBAa 7wyvEvqijRF94yWmf/o1yM3LbgzG46ACtX5OTaeTp5vAY4maHGKx0FyEOX7VaxDOOeGN tTqg== 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=kpBcFGI8EHILNZvJp11Zphal+nl9VNB99C8InAbq4HhaOEWvCJE0BxUQ+rMYM4Fy3C 5DNbdMmcIXAtZsbI8pc6HePZethH/AzkYxK4J7W+U+pBA07vN0FPa5auIOgYon95Cx6g RiLZk7sHltkoz8/adHb9czcr7gvdHdoO0vGPqtRylKMEs/3nE4roZZKavyLEPMMjxlxc Zre/FJSjj4BSQS4LWHGSN5YcouZeUFwJUDWUEAXVEhACa0lpR+LTKMnmx0Z4/u1DD4CU p4iFEqPaWLqj0OoVYAFBKIOMSulwaZZUE1exIJmW1I2HjLqvYIdRxnzZLlXPiH2Itb/4 8nYw== 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-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g32-20020a635220000000b005539ec6a3adsi13438343pgb.703.2023.06.30.15.35.11; Fri, 30 Jun 2023 15:35:27 -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; dkim=pass header.i=@chromium.org header.s=google header.b=ePmdCCgl; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232686AbjF3Wds (ORCPT + 99 others); Fri, 30 Jun 2023 18:33:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57648 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232489AbjF3Wdq (ORCPT ); Fri, 30 Jun 2023 18:33:46 -0400 Received: from mail-oa1-x2b.google.com (mail-oa1-x2b.google.com [IPv6:2001:4860:4864:20::2b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EA8F93C22 for ; Fri, 30 Jun 2023 15:33:45 -0700 (PDT) Received: by mail-oa1-x2b.google.com with SMTP id 586e51a60fabf-1b055511b85so2095902fac.2 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=bilT+HHC/zHNch/hAft+Xqi+7H4KJ40iALmcy6llFRep5MbZ82mlcSI3VIk42CAUkm 0FDCknpwZjRL1iXMdtBDyNr5LWvNlrhgxM8Rg+Xjp6cp0NttbhO2ek2mHtgA1bCqLCuz 1OIE9N95iEGAM5nbMaZ0637ok1Ji9A4rw2Z/s7ZALXSBA2SJMSR2tnYmcR4fiT935AjN iao8JVC1eXpyX536kgz+CTKFZ/RcSBEjbRj2W07oNGRczgRzrkYbNPQCibxV9bdHAOnr WcFYJjTz9ZxDZkU95WtrsLo9CnKjkuApzZkdWPLIXZh1O2LSxIazVAGwHqYaEPJu5FjF ImQg== X-Gm-Message-State: ABy/qLbUXF27CVSVx3R0yHKkgQdrrnGu32VqFi91QjivzgGhAa3mtGG+ ZyPJnidai/UxF3HX+OOijrjloQ== 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 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=unavailable 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1770167640398772644?= X-GMAIL-MSGID: =?utf-8?q?1770168807590302463?= 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; }