From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.49 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796060201; b=IWXeX49W4dU8xfr52wLBD9gJlYa6/mcu7DNo10kPiQR0e13YR+8Vzud0/zvbfVKESvyq 1EeTm09yVVGwJ8w9nYoA9ejYrVY0hB9F9hBl6oulxpGcAaTBISURZtiUhOclKbL+QElI6 tyDZY3DkfYZKRuQCgfU+92dH4sRC/4yIgY+lGdEHGc4E5/NqDPtvfUbbjepEqca6Ig3Vq xxEWubpKakKstvnfZHGGzDofPN5LPfV0IphkczSgIqsuO/77rDgOPjjH/4nocVBNxsT2o sJeIZ1nazJMmeodSoPdX+IUEv7n1CWsm8zogbzNETk1lzU5K/88ft7jv5wycgCQDpaQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796060201; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:MIME-Version:Content-Transfer-Encoding; bh=6DvxgUnRO4eu2Sf/kU8CQMzSD9VJqjLmnFDONSJwIAU=; b=Z/zCOyor1kPDv6qnVLjD6YzFp/h0mTgUzd0hgqGiIL5sQ7ywzC5mCae65n7rOZzZceAr OSwBaWs5Ze4q9rFkhYJ/4Cy9oJwmHJkdQV6v+v6SThtaUfOP5+9bARNl2wXfGIBJcqSqf f95B0xOfmSwPM+L7wLOgSnkng4TLuRSLHaWGe/9B3LSVQgRYGhs/+PzdKwNIr+qfsn1/o DcjP8vbFlPGbUy8tVdJZ+i9PMFscm5M05R83VRFOpJI/XwbTkYJmpDs9hnGZm814VlG8H UZDoOUtUiNctzwOHhvRj1ZNrSh/C0bJFPCnZ9Q7ulgeJxNc3sKAu5xyl5wum1fCL86g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=V8HfNp0n; arc=none smtp.remote-ip=209.85.128.49 Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 053C8606A320 for ; Mon, 30 Nov 2026 17:36:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 053C8606A320 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=V8HfNp0n Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-4bb606491faso56396405e9.3 for ; Mon, 30 Nov 2026 09:36:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796060199; x=1796664999; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=6DvxgUnRO4eu2Sf/kU8CQMzSD9VJqjLmnFDONSJwIAU=; b=V8HfNp0ntQFMe/Xv49RV5pe3e+7r4Ax/TMiIutWZalIntx6e/XpsPUcHO1jxHHoiV1 TqAiSJyR4Iff3ZuEW+i0moEGCzRucXcAJd7kVYbEN2pcNjp+YJtgIzbJ0sg6kux+z5Lr PmeIxhkYqsifttIgUMLcR35i9AXvdH5aoR34GgaJTrzzj6g8CT0pJc82j9LjVXtUyUp/ zEhDrFrEdncUcqmSU+FMEb1ZbrLfSNnk6jajDCj5zeo7QpRc6gm3YiWjbeizW+oVT87l CdAzpiyjCzWa6WicbE7s925f2VcniIoYzW7iiSkZ4GS/W8kkmGlF/lIrf6/qCOd4Yvbk npfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796060199; x=1796664999; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=6DvxgUnRO4eu2Sf/kU8CQMzSD9VJqjLmnFDONSJwIAU=; b=Ee9MA9/BbxstFc7nkSJA1oTjqwifGTfDzQCbfsGhVVcMG8hIqwTxpogvaAhKGuKCUc rI3UW7R17fuRk7Hu962oysyqb3B/GXUY9U+Y9uVyLvPAM2Wkg4v76qlIUiW5KdXDTyPb eb49O0mlKX07iE/NaDSp5PLKPFDIJvN8FOo/ft1Evaa8nPkY2t41gjfFI/U+JIhcKru6 xph2iEQQ1DWO/G4vxxB6ZRTgXc0a4BphCJCwdavOr28GWQPnOg4wFvdVUVI4m1Uqgwpm WuRgeLWA9mD4s/AFvdZI6qsZ+LKdWLXM2BlK9148dmAafdBvdVPrJwX+lQIR/rH3zt/p JHUg== X-Gm-Message-State: APuU4Z+16GLY1jcRxI4GuJZt/ldlnC8GW7iwi4Q5EbYb6U7++uZqib6y 0tA8jKICHXMVD/tpj8wnPJXrEcjq1r2T0bHvPA72Sx7PSYy2gdnNqRZ0 X-Gm-Gg: AZFdTZfPD46zmCGidhhoUbmSkeAXC4JFS5wf7hNcQBdncp78TOg1CCNTj6b214wB30L JPDCVNy01/dXPxudhZmecdB+9p7lIGQ2NqOwz2S8+TGG5yuhkLET4xhTT//v1xBt7rZXQTFOFOY HO6lDOE2AX7fGCSJzcl7InBz2mktBokM9hOEWUwSdFj6lcJI/FGsk71WrWR/ndTdAl8WkU93lP5 Pndo6n903xRl7Akzb5EyKZ3zHzoRmeseaz/o0U0VRTGvXM3C8x6OmasMEZWI2Zv6mkBr9GP1Poc fS8y5amDbDULOs5i5iXKlOZfLbJuTEquwA/bOOGBD/UL5muCUcQpEtLQeIP7/+YNDNFchWRszqi hJiXsqgeXTzVVUmQ0pyv/qZG+un+v7bhvrhGZBhuUaKO75fzVp2jMNbUzTmseocsezxTGIBN0jw Y88OxCgokzsfUb8+BK6zjYrRkM+5k4VrxLKPi3gH2VaNI2jnx2+FtLWsqXx+qejGGekG+6pw7/G Enw00WXkXp+H675jcsi6bO+Pkv9/D4aTtJwR1FQZ3diMZz48fzm3F9ghPldtH41eskXc7pAftuB +//GUht67iPAa8qyxKf+1ZYj8nyooOTESuYvXnApcKtR7Y1YGQLc1Mez7Xk4RtQXEfE7h6s7Cfh zLhSXCsXwNlA2VLU2i+WdjPHHktM= X-Received: by 2002:a05:600c:5309:b0:4bb:62d5:4ab1 with SMTP id 5b1f17b1804b1-4bb88924a5bmr672847495e9.33.1796060199088; Mon, 30 Nov 2026 09:36:39 -0800 (PST) Received: from fedora.pigeon-monitor.ts.net (host-79-29-251-162.business.telecomitalia.it. [79.29.251.162]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb884865b1sm966167025e9.5.2026.11.30.09.36.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 09:36:38 -0800 (PST) From: Lorenzo Ferrari To: Luiz Augusto von Dentz , Marcel Holtmann Cc: linux-bluetooth@vger.kernel.org, Lorenzo Ferrari , stable@vger.kernel.org Subject: [PATCH v2] Bluetooth: RFCOMM: Serialize tty unparenting with hci_conn_del_sysfs() Date: Mon, 30 Nov 2026 18:35:08 +0100 Message-ID: <20261130173508.78016-1-loreferro01@gmail.com> X-Mailer: git-send-email 2.56.1 X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit An RFCOMM tty device is reparented under the hci_conn device of its peer by rfcomm_reparent_device(), which calls device_move() with hdev->lock held. When the ACL link goes away, hci_conn_del_sysfs(), also under hdev->lock, moves the rfcomm tty children of the connection to no parent. Two other paths touch the tty's parent without that lock: - rfcomm_dev_shutdown() moves the tty to no parent itself; - rfcomm_dev_destruct() unregisters the tty device. device_del() reads dev->parent once on entry and later calls klist_del(&dev->p->knode_parent) for that parent. If hci_conn_del_sysfs() moves the tty while device_del() is between those two points, the node has already been removed from the parent's klist and its klist pointer cleared, and device_del() unlinks it again: RFCOMMRELEASEDEV HCI Disconnection Complete device_del(tty) parent = dev->parent (hci0:1) hci_conn_del_sysfs() device_move(tty, NULL) klist_remove(knode_parent) klist_del(&dev->p->knode_parent) klist_put(): knode_klist() is NULL KASAN: null-ptr-deref in range [0x0000000000000058-0x000000000000005f] RIP: 0010:klist_put+0x47/0x160 Call Trace: device_del+0x1ee/0xc90 device_destroy+0x8a/0xd0 tty_unregister_device+0x79/0x1c0 rfcomm_dev_destruct+0x11a/0x280 rfcomm_dev_ioctl+0x1b7/0x1a70 Commit 9b851b09b392 ("Bluetooth: hci_sysfs: Fix NULL pointer dereference in device_del()") removed the same race for BNEP network devices by limiting hci_conn_del_sysfs() to rfcomm ttys again; for the ttys it is still there. Detach the tty from its parent under hdev->lock, found with the same route lookup as rfcomm_reparent_device(), in rfcomm_dev_shutdown() and before tty_unregister_device() in rfcomm_dev_destruct(). Every move of the tty and its unregistration are then serialized with hci_conn_del_sysfs(): either it has already moved the tty, or the tty is no longer a child of the connection when it looks for it, and device_del() always runs with no parent. Reproduced on bluetooth.git (77bc0f253ff9) with a vhci controller: an L2CAP connect brings up an ACL link, RFCOMMCREATEDEV creates a tty that is reparented under hci0:1, and RFCOMMRELEASEDEV races an HCI Disconnection Complete event. The window is narrow, so it was widened with a 200 ms msleep() in device_del() for rfcomm devices; with it the first round crashes as above. With this patch, with and without the delay, 100 rounds of each ordering, 100 rounds that also open and close the tty (rfcomm_dev_shutdown()) and the syzbot BNEP reproducer ran with no KASAN or lockdep report. The race was analyzed with the help of an AI assistant (Claude); the reproducer and the tests above were run locally. Fixes: acea6852f32b ("[BLUETOOTH]: Move children of connection device to NULL before connection down.") Cc: stable@vger.kernel.org Signed-off-by: Lorenzo Ferrari --- Changes v1 -> v2: - Take and release hdev->lock in the same branch of rfcomm_dev_unparent(). v1 took and released it conditionally, which the clang context analysis build rejects (reported by bluez.test.bot). No functional change. v1: https://lore.kernel.org/linux-bluetooth/20261130103529.65147-1-loreferro01@gmail.com/ This overlaps with "[PATCH v1 16/16] Bluetooth: RFCOMM: Serialize tty device reparenting", which was not applied with the rest of the series. An RFCOMM-local mutex serializes shutdown with reparent, but the tty is also moved by hci_conn_del_sysfs() under hdev->lock and unregistered by rfcomm_dev_destruct() with no lock held. Taking hdev->lock, which reparent and hci_conn_del_sysfs() already hold, covers all of these paths, so this could replace 16/16. I can share the reproducer if useful. net/bluetooth/rfcomm/tty.c | 41 ++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/net/bluetooth/rfcomm/tty.c b/net/bluetooth/rfcomm/tty.c index 745cf1e4bb..407ef72364 100644 --- a/net/bluetooth/rfcomm/tty.c +++ b/net/bluetooth/rfcomm/tty.c @@ -74,6 +74,39 @@ static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig); /* ---- Device functions ---- */ +/* Detach the tty device from the hci_conn device it was reparented under. + * + * hci_conn_del_sysfs() moves the rfcomm tty children of a connection that + * goes away to no parent, under hdev->lock, and rfcomm_reparent_device() + * moves the tty under a connection with the same lock held. Any other move, + * and the unregistration of the tty device, must be serialized with those: + * device_del() reads dev->parent once and later unlinks the device from that + * parent's klist, so a concurrent device_move() makes it unlink a node that + * is no longer on any list. + */ +static void rfcomm_dev_unparent(struct rfcomm_dev *dev) +{ + struct device *tty_dev = READ_ONCE(dev->tty_dev); + struct hci_dev *hdev; + + if (!tty_dev) + return; + + hdev = hci_get_route(&dev->dst, &dev->src, BDADDR_BREDR); + if (hdev) { + hci_dev_lock(hdev); + if (tty_dev->parent) + device_move(tty_dev, NULL, DPM_ORDER_DEV_LAST); + hci_dev_unlock(hdev); + hci_dev_put(hdev); + } else if (tty_dev->parent) { + /* The adapter the tty was reparented through is down, so its + * connections are gone and nothing else moves the tty. + */ + device_move(tty_dev, NULL, DPM_ORDER_DEV_LAST); + } +} + static void rfcomm_dev_destruct(struct tty_port *port) { struct rfcomm_dev *dev = container_of(port, struct rfcomm_dev, port); @@ -89,8 +122,10 @@ static void rfcomm_dev_destruct(struct tty_port *port) rfcomm_dlc_put(dlc); - if (dev->tty_dev) + if (dev->tty_dev) { + rfcomm_dev_unparent(dev); tty_unregister_device(rfcomm_tty_driver, dev->id); + } mutex_lock(&rfcomm_dev_lock); list_del(&dev->list); @@ -127,10 +162,8 @@ static bool rfcomm_dev_carrier_raised(struct tty_port *port) static void rfcomm_dev_shutdown(struct tty_port *port) { struct rfcomm_dev *dev = container_of(port, struct rfcomm_dev, port); - struct device *tty_dev = READ_ONCE(dev->tty_dev); - if (tty_dev && tty_dev->parent) - device_move(tty_dev, NULL, DPM_ORDER_DEV_LAST); + rfcomm_dev_unparent(dev); /* close the dlc */ rfcomm_dlc_close(dev->dlc, 0); -- 2.56.1