From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.48 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796035007; b=qf/am2iNndAn5J/WCuD69VjKK47rSIJNsAtiJQ09dUFjdQxPYz9x8Dn7MzyJ0Ws5IJ6X uetsT74UX6aiu4Zl7rxvMB5MFuK9jIjtUbquHBLiKPwdsUMDa10A2kXpLTDw+CkOMkFeK THPRHDBJXegYqNg1wTeDb3xACH7k/LbOpEYkOR9bIV2UIFyzIxFhaGgaNI7JzDVfjwjq4 eJMEmZne6cVNEvGZMQpmyjcBa17Gnv7HGPswjr95tEMOYuFSFhHxILwcfHOwvMK2FjSPH oAKiE+7Wl3AWTw1SaE9fs0Ahnl//wbpDp8rhNXw5g8Eak2HF6GfBU+7Kb02R2Rd3VTg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796035007; 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:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=H+5YZaMqDZm69NErbtY60LeeI0bTfuEy9AeUv+lDJu0=; b=y2/QlJtuwxgfchxBvh6FzzWuWttsFcgKs8G4cXac1mq9+to1pRnbt0PgpgrATULo2DMv Mx78WdqMDiz0t04XuKE5ONSgeMww900UQqgJ4i7LQ3lAztfShiy/PrXjT73cJi4Km1yZQ oCCp65cjwIrxejjdusFSA+tIj29XpNcnxuPlMQ8lQrykVcsxryPCHRPKC0x9NblX+4Y9T xOR8pP3MzuQGUXWqhRnsN/flWEiuuSGQrw9phMMetpVyKDYE/P3p7IHQa5YCMOywb0OGs 7iLPZv5MQwRe0lBa3eCLp4gsv+q3tOC/yHsAhP5RvGYN5vt7GZmoOLwjupcmy+ae+KA== 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=X0q1LerU; arc=none smtp.remote-ip=209.85.221.48 Received: from mail-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) (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 9E876606A32A for ; Mon, 30 Nov 2026 10:36:46 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 9E876606A32A 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=X0q1LerU Received: by mail-wr1-f48.google.com with SMTP id ffacd0b85a97d-4ac109a8a12so6117804f8f.3 for ; Mon, 30 Nov 2026 02:36:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796035005; x=1796639805; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=H+5YZaMqDZm69NErbtY60LeeI0bTfuEy9AeUv+lDJu0=; b=X0q1LerUStj5qbii2MVHr8KRa1tTPhLo3Uag6mHCM663TX/Sdy72HWbrbcKmOFbn9L LTjbVykjhENcqyTQ0wFmJdRURvf86c/lNpZUN05n6Vauim792jRmdxuN52D6WZtUxDDL ztRsMrgAqzibe6CmrkfZbLbGXGoQZYJb6463TfjiQMy+fKBetgXYw31Uj0Zh61nSdyJz aMSd+UNrpQsS1PGfium91xP/pI7tFpEGPylJdP/Ikrv8hJNVINAd4u9i2rP8wJ1obUq2 kCQfiI3Otr+Yq2n+jVTEMMozemyCD6PshNStZZD7r/HWGC7o6UdgQAE6pdtMbqXPervj TcNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796035005; x=1796639805; h=content-transfer-encoding:mime-version:references:in-reply-to :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=H+5YZaMqDZm69NErbtY60LeeI0bTfuEy9AeUv+lDJu0=; b=OMk4g+2waIFsjg3jgvcIH7jnFw0nl4Ec3T+nrnZ0b25Ap1sUBfW19jqVT554DG+vNz hDJ/YTq9nZqFW8vmRQDn3y4DA4QnA+tdc1DHHjYCWVk8QSrjtPSuNuhTL7AtFDk1F0wX UnsPnvnnA5IK5ZPSG2AMg4x/F64Q0ErUYgqYeY5e2LjdumihJSOnrKnA9t0TQPJNqBhL /NRYtxM5kqa7Q2owhp4O0J0sjnO+38Q0SHx0UWWtAHpWZkMbbvcHMYmol/Z0sN76n4DW 8VAFQkOKHl51IY6S7RWxGfhbq5QMtAE1iK3+l1OX5tlFucHgPbid3Vs1r1ktyBqmx4PI +5Jg== X-Gm-Message-State: APuU4Z8AtmNCfxz97IrnkxzswmQaszHBDMetXifkDXJRN0mj70iqRVV+ rb2vvswmUpguOIJO7wvCTmVUlE7FAydy6ZkVCvm3UTj/M3wgiagZsCoD X-Gm-Gg: AZFdTZehgIQXj7hpfI7PhiF4rhtcR032HIOSp3kQ/OvjduZ/I7dOFi2cYsSqI7ZzPuJ W5hX11HCWt/j7x9zUaIUN/Lmi9Krn+badpajiyPE1sQ8NRA4N4kAU1uFTJwisNPyPKMECt+hVFo u2SiMRdKJP54dE5ZbZVm8vzcizL5CJB+Qb2lZSsRQz6VeotyA9AjcQYRwLX3hQdYqhH2NJJfGcM fhBj/WD88gf1URKeu4T5Wylg76NQbXObBProc5A1h6Xu6JwaDUqfG/2w38YlGaphZ2vbBRSospN c6GZJZ1taNTG6+E++06hOV1zC6B/gdWTGjU+MvACMWMwjYyT8hXBzYfBlPjIhTi4Y4B3TAWkUWc NVZzhawRFrbGvz4reqRIh4pGpYs93SLDKF0/AVjUP0+R0ynQi4WowENX3x2StxFFNdSWuCTgtsR z3o0kqCxt+0aJUrGF5gXy7uaUNy/5Z82WOBGZ750v7vqcxo4Ff4du5cMsX23whysGiik0L4K1v+ GiFN5LdDr0u8eNrqCXHQp9PnKsAE57fpdDkDC7625o5S+DlwIX1M/yBouF047pZxquAT2rSoFwU hkF9FPSN0ZNYzkDqvsX1K9cj3ZghTYZorzLLx9rXHW62ljm0wcZ4DUIXpKUKStW8pgdrPPoqxHi ymPlKe874OjEKe2BAWKAwjkOxzQc+QxexKhhuVy/XsEcXp0NK X-Received: by 2002:a05:6000:4908:b0:4bc:38c7:aad6 with SMTP id ffacd0b85a97d-4be43dbd129mr20135225f8f.36.1796035004842; Mon, 30 Nov 2026 02:36:44 -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 ffacd0b85a97d-4ab69bf630esm65461778f8f.16.2026.11.30.02.36.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 02:36:44 -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] Bluetooth: RFCOMM: Serialize tty unparenting with hci_conn_del_sysfs() Date: Mon, 30 Nov 2026 11:35:29 +0100 Message-ID: <20261130103529.65147-1-loreferro01@gmail.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261117185737.3758255-17-luiz.dentz@gmail.com> References: <20261117185737.3758255-17-luiz.dentz@gmail.com> 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 --- 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 | 39 ++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/net/bluetooth/rfcomm/tty.c b/net/bluetooth/rfcomm/tty.c index 745cf1e4bb..98ae59a7a4 100644 --- a/net/bluetooth/rfcomm/tty.c +++ b/net/bluetooth/rfcomm/tty.c @@ -74,6 +74,37 @@ 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); + + if (hdev) { + hci_dev_unlock(hdev); + hci_dev_put(hdev); + } +} + static void rfcomm_dev_destruct(struct tty_port *port) { struct rfcomm_dev *dev = container_of(port, struct rfcomm_dev, port); @@ -89,8 +120,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 +160,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.54.0