From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-dl2-f12.google.com (mail-dl2-f12.google.com [74.125.229.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4B3F035F5ED for ; Sat, 26 Sep 2026 17:27:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.229.140 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790443652; cv=none; b=K/6kGZixudF2ck6ygzko1A01lxZFc+k2kGinws7F2PGH5DYabpZ1FCcTDUeJGArc9XKqsTXfxfsa/kHK3cmW9/mXGApAVTD787tUzXzZUjAvNE5n3b5Q9Rm//Sxid+a2neZIjaRUg5JlzfLoIHKVKFowSLPQqS5qQXqeJKiYaU4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790443652; c=relaxed/simple; bh=2ZTmtkIFSofC2JVrEOSYkSGryK/4WBfRMxRimqC7VJ4=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=mwny3mo5/qcfDJw8l9ZnyeWPEBH/ymWIWEjHsbdMpQgJtdK99SMf3dKVsNqF+MuIDkcNXzHK0uVqM5hEbGojHvZw0mnBb60MU/zetLtsic1LyDI7nro9Ole/CTahRx61Iu8YZ+GjsiZrYWafDQjDw5orj5MiD50nF/bJeSo8FtQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=U/yE7S3o; arc=none smtp.client-ip=74.125.229.140 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.b="U/yE7S3o" Received: by mail-dl2-f12.google.com with SMTP id a92af1059eb24-141a5d476abso60311c88.2 for ; Sat, 26 Sep 2026 10:27:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1790443650; x=1791048450; 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=BWkCXAv8KL7gS3gupSCkFH2TZ/H32WNAz15njK7x33c=; b=U/yE7S3oayvOj97t1Yo9gN7YzqOxqVAW2SMBXNxVvfzqDFfD07iJ5ToxAdkR7vz0Ym JXM+WSYourTGDdWd20ZKNvokQ368OB0qZJXuOTRsS7JXXPSdjCrCySngN2TUMs1wXvwu +MOMFLuWOKsJz6RIM2iioDCp5JPlwURX46y18gU0+HuM9jZPX7WEsI0ggq32ytAMZNJI GMyrSeH5yt3z9yUEB/b5fhnQb1gQ3dujEUYji3ZAoxrVS3eIqwO1uEEMVMcskzgatQQh cClD+IQMfNo/JdXPYF6ieVtSkOWJpAhkpHr+jLBmPN50R5XMaXIeUyIW/r7hVmDHEaDw BoeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790443650; x=1791048450; 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=BWkCXAv8KL7gS3gupSCkFH2TZ/H32WNAz15njK7x33c=; b=shWENpQB0jD8Z5B2/7bNnzhmx0NP6SeFUtOpOMQYFa6sQiQ5lH3mGgBlb0BvUUMKyc OQHyP4Yyngxdmf+9id156GFgmJAyLXOvOT9/X0mnXKz7cHE2/dZxmeQEoFf+MuCslWl1 ueIV4GsoqqPCGxUQioBRmRv1YRzEKg5XweKfSqCQWE0bLRucX0N5I4UO24Jwk74OiSny GuO5f6bIV0yvXS/31zPZQvptnETYj9jLQR63vU48J5ZubgqI9sl3PPDEzh6A4vIXsEYi LzL23GmX+g5QR7Ga8pj9vWDFV1ftHfr9UCyYv+XzqjQbs8IQuHiFTsFMnTw7/EDhdhfi i9cw== X-Gm-Message-State: AFuF++lpitcyp49q/mGJ0FxrHObRUPAkl4Sz0uHPRGPMg/JwmEX7JAC3 ogPi4++d6Y6qBF6beS3xmsYwusxobsqV7ufIOHnlVpWQHaizWPEmJrjZ X-Gm-Gg: AYBFou1ETJUsGNBzorgcqPXwgOs82tL0qG+P46xwQ7oIfVa9gZoNxVZ5SeNVnkMqjV4 HEvGBJrFb10YgI8HuRPCr98Jg0mcuuB3ZhMdD8n3jyb6ifrrORwVMObacv95UKDNScboGMb/2vX nmIiqJ32gTzsWc8Giys0V0t5eVgMc794u+3TpHyW7FleDf98q065xLHwWppmabHmE/5exXe4b2b GOj8NXxo8/DVsqRWjAqZwKUzJXCQIl58xoNGjV6ueBOT4X+jHrRMN21tJtIdlpr+yPo+EAKbhvj Zz9o6uJq6eFbezEgVFRzdd6St3iub1fc6nw21Lat996/ArYb537Cin0st2dP02KShl9hytjk8Pa Ad6VIgki0EHlJHfPT6MN8+QA2syGRv29NrxdsJxNKPm/8GbjwhjPgZy8GWrYyss2KJhnI/wskNZ h06SKh8gZDekRekT9rhHwFlc0aOFe8AlYKnVyqjl9XytiOQXDXnubp1rXGaE1Q+2BUghsOdtXZG P9PQvYmwHJQZtPy05FHPVj20Cb25WElCjbakQ+BREzHjU6obuttJ5bCX0dGjJnl1e4p7w== X-Received: by 2002:a05:701a:c919:b0:143:3240:89ac with SMTP id a92af1059eb24-146cfdcef79mr6165010c88.2.1790443650232; Sat, 26 Sep 2026 10:27:30 -0700 (PDT) Received: from localhost.localdomain (95.169.12.199.16clouds.com. [95.169.12.199]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-145a7318afcsm13436097c88.0.2026.09.26.10.27.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 26 Sep 2026 10:27:29 -0700 (PDT) From: Chengfeng Ye To: Marcel Holtmann , Luiz Augusto von Dentz , Jakub Kicinski , Mark Brown , Johan Hovold , Chengfeng Ye , Tim Bird , Kees Cook , Peter Hurley Cc: linux-bluetooth@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: [PATCH] Bluetooth: RFCOMM: Fix initial port reference race Date: Sun, 27 Sep 2026 01:27:23 +0800 Message-ID: <20260926172723.2405885-1-nicoyip.dev@gmail.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit For RFCOMM_RELEASE_ONHUP devices, rfcomm_tty_install() and __rfcomm_release_dev() both use RFCOMM_TTY_OWNED to decide who drops the initial tty_port reference. The release path tests the bit separately from the install path setting it, so both can drop that reference. The synchronous hangup does not prevent this race: port->tty is not assigned until tty_port_open(), after installation. The following interleaving is possible: 1. Install and release each obtain a reference with rfcomm_dev_get(). 2. Release observes RFCOMM_TTY_OWNED clear. 3. Install sets RFCOMM_TTY_OWNED and drops the initial reference. 4. Release drops the same initial reference again. 5. TTY cleanup drops its reference and frees the device. 6. Release performs its final tty_port_put() on the freed port. KASAN reported: BUG: KASAN: slab-use-after-free in tty_port_put+0x22/0x190 Write of size 4 at addr ffff8881001d3d5c by task poc/92 Call Trace: tty_port_put+0x22/0x190 rfcomm_dev_ioctl+0x1d4/0x1930 sock_do_ioctl+0x110/0x260 sock_ioctl+0x380/0x590 __x64_sys_ioctl+0x134/0x1c0 Allocated by task 88: rfcomm_dev_ioctl+0x8f6/0x1930 sock_do_ioctl+0x110/0x260 sock_ioctl+0x380/0x590 __x64_sys_ioctl+0x134/0x1c0 Freed by task 65: kfree+0x131/0x3c0 rfcomm_dev_destruct+0x23b/0x2f0 release_one_tty+0xc1/0x370 process_one_work+0x661/0x1090 Use test_and_set_bit() in both paths so that only the caller that changes RFCOMM_TTY_OWNED from clear to set drops the initial reference. Each path keeps its own lookup reference until release or TTY cleanup, preserving the existing callback ordering and error handling. Fixes: 80ea73378af4 ("Bluetooth: Fix unreleased rfcomm_dev reference") Cc: stable@vger.kernel.org Assisted-by: LLM Signed-off-by: Chengfeng Ye --- net/bluetooth/rfcomm/tty.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/net/bluetooth/rfcomm/tty.c b/net/bluetooth/rfcomm/tty.c index b2c1060394e6..dc3cdf614def 100644 --- a/net/bluetooth/rfcomm/tty.c +++ b/net/bluetooth/rfcomm/tty.c @@ -462,7 +462,7 @@ static int __rfcomm_release_dev(void __user *arg) /* Shut down TTY synchronously before freeing rfcomm_dev */ tty_port_tty_vhangup(&dev->port); - if (!test_bit(RFCOMM_TTY_OWNED, &dev->status)) + if (!test_and_set_bit(RFCOMM_TTY_OWNED, &dev->status)) tty_port_put(&dev->port); tty_port_put(&dev->port); @@ -724,10 +724,9 @@ static int rfcomm_tty_install(struct tty_driver *driver, struct tty_struct *tty) * when the last process closes the tty. The behaviour is expected by * userspace. */ - if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) { - set_bit(RFCOMM_TTY_OWNED, &dev->status); + if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags) && + !test_and_set_bit(RFCOMM_TTY_OWNED, &dev->status)) tty_port_put(&dev->port); - } return 0; } -- 2.43.0