All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3] Bluetooth: RFCOMM: hold the listener across child teardown
       [not found] <20261115191908.3646-enderaoelyther@gmail.com>
@ 2026-11-20 12:18 ` Hang Nan
  2026-11-20 13:28   ` [v3] " bluez.test.bot
  2026-11-20 15:10   ` [PATCH v3] " Luiz Augusto von Dentz
  0 siblings, 2 replies; 5+ messages in thread
From: Hang Nan @ 2026-11-20 12:18 UTC (permalink / raw)
  To: linux-bluetooth
  Cc: marcel, luiz.dentz, luiz.von.dentz, pav, zzzccc427,
	enderaoelyther

---
 net/bluetooth/rfcomm/sock.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/net/bluetooth/rfcomm/sock.c b/net/bluetooth/rfcomm/sock.c
index 3fb8b5474948..94a495e86057 100644
--- a/net/bluetooth/rfcomm/sock.c
+++ b/net/bluetooth/rfcomm/sock.c
@@ -66,6 +66,7 @@ static void rfcomm_sk_state_change(struct rfcomm_dlc *d, int err)
 	__must_hold(&d->lock)
 {
 	struct sock *sk = d->owner, *parent;
+	bool held = false;
 
 	if (!sk)
 		return;
@@ -83,6 +84,12 @@ static void rfcomm_sk_state_change(struct rfcomm_dlc *d, int err)
 	if (parent) {
 		if (d->state == BT_CLOSED) {
 			sock_set_flag(sk, SOCK_ZAPPED);
+			/* close() may already have dropped its reference and the
+			 * queue holds none, so keep the listener alive across the
+			 * unlink and the notification.
+			 */
+			sock_hold(parent);
+			held = true;
 			bt_accept_unlink(sk);
 		}
 		parent->sk_data_ready(parent);
@@ -100,6 +107,12 @@ static void rfcomm_sk_state_change(struct rfcomm_dlc *d, int err)
 		 * rfcomm_sock_destruct() will dead lock. */
 		rfcomm_dlc_unlock(d);
 		rfcomm_sock_kill(sk);
+		/* Drop the listener reference with neither the child's socket
+		 * lock nor its DLC lock held: this put can be the final one and
+		 * rfcomm_sock_destruct() takes the listener's own DLC mutex.
+		 */
+		if (held)
+			sock_put(parent);
 		rfcomm_dlc_lock(d);
 	}
 }
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* RE: [v3] Bluetooth: RFCOMM: hold the listener across child teardown
  2026-11-20 12:18 ` [PATCH v3] Bluetooth: RFCOMM: hold the listener across child teardown Hang Nan
@ 2026-11-20 13:28   ` bluez.test.bot
  2026-11-20 15:10   ` [PATCH v3] " Luiz Augusto von Dentz
  1 sibling, 0 replies; 5+ messages in thread
From: bluez.test.bot @ 2026-11-20 13:28 UTC (permalink / raw)
  To: linux-bluetooth, 2122295973

[-- Attachment #1: Type: text/plain, Size: 2019 bytes --]

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list.
This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/series/1217309/

---Test result---

Test Summary:
CheckPatch                    FAIL      0.49 seconds
VerifyFixes                   PASS      0.07 seconds
VerifySignedoff               PASS      0.07 seconds
GitLint                       FAIL      0.20 seconds
SubjectPrefix                 PASS      0.06 seconds
BuildKernel                   PASS      23.27 seconds
CheckAllWarning               PASS      27.65 seconds
CheckSparse                   PASS      29.94 seconds
BuildKernel32                 PASS      24.18 seconds
CheckKernelLLVM               PASS      24.74 seconds
TestRunnerSetup               PASS      559.27 seconds
TestRunner_rfcomm-tester      PASS      6.64 seconds
IncrementalBuild              PASS      23.02 seconds

Details
##############################
Test: CheckPatch - FAIL
Desc: Run checkpatch.pl script
Output:
[v3] Bluetooth: RFCOMM: hold the listener across child teardown
WARNING: Missing commit description - Add an appropriate one

ERROR: Missing Signed-off-by: line(s)

total: 1 errors, 1 warnings, 0 checks, 31 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
      mechanically convert to the typical style using --fix or --fix-inplace.

/home/runner/work/bluetooth-next/bluetooth-next/src/patch/15005421.patch has style problems, please review.

NOTE: Ignored message types: UNKNOWN_COMMIT_ID

NOTE: If any of the errors are false positives, please report
      them to the maintainer, see CHECKPATCH in MAINTAINERS.


##############################
Test: GitLint - FAIL
Desc: Run gitlint
Output:
[v3] Bluetooth: RFCOMM: hold the listener across child teardown

1: CC1 Body does not contain a 'Signed-off-by' line


https://github.com/bluez/bluetooth-next/pull/1168

---
Regards,
Linux Bluetooth


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v3] Bluetooth: RFCOMM: hold the listener across child teardown
  2026-11-20 12:18 ` [PATCH v3] Bluetooth: RFCOMM: hold the listener across child teardown Hang Nan
  2026-11-20 13:28   ` [v3] " bluez.test.bot
@ 2026-11-20 15:10   ` Luiz Augusto von Dentz
  2026-11-20 15:15     ` [PATCH v4] " Hang Nan
  1 sibling, 1 reply; 5+ messages in thread
From: Luiz Augusto von Dentz @ 2026-11-20 15:10 UTC (permalink / raw)
  To: Hang Nan
  Cc: linux-bluetooth, marcel, luiz.von.dentz, pav, zzzccc427,
	enderaoelyther

Hi Hang,

On Fri, Nov 20, 2026 at 7:18 AM Hang Nan <2122295973@qq.com> wrote:

I need a proper explanation for why these changes are needed, what you
are trying to fix with backtraces, etc. Also, your Signed-off-by and
Fixes tags are needed here.

> ---
>  net/bluetooth/rfcomm/sock.c | 13 +++++++++++++
>  1 file changed, 13 insertions(+)
>
> diff --git a/net/bluetooth/rfcomm/sock.c b/net/bluetooth/rfcomm/sock.c
> index 3fb8b5474948..94a495e86057 100644
> --- a/net/bluetooth/rfcomm/sock.c
> +++ b/net/bluetooth/rfcomm/sock.c
> @@ -66,6 +66,7 @@ static void rfcomm_sk_state_change(struct rfcomm_dlc *d, int err)
>         __must_hold(&d->lock)
>  {
>         struct sock *sk = d->owner, *parent;
> +       bool held = false;
>
>         if (!sk)
>                 return;
> @@ -83,6 +84,12 @@ static void rfcomm_sk_state_change(struct rfcomm_dlc *d, int err)
>         if (parent) {
>                 if (d->state == BT_CLOSED) {
>                         sock_set_flag(sk, SOCK_ZAPPED);
> +                       /* close() may already have dropped its reference and the
> +                        * queue holds none, so keep the listener alive across the
> +                        * unlink and the notification.
> +                        */
> +                       sock_hold(parent);
> +                       held = true;
>                         bt_accept_unlink(sk);
>                 }
>                 parent->sk_data_ready(parent);
> @@ -100,6 +107,12 @@ static void rfcomm_sk_state_change(struct rfcomm_dlc *d, int err)
>                  * rfcomm_sock_destruct() will dead lock. */
>                 rfcomm_dlc_unlock(d);
>                 rfcomm_sock_kill(sk);
> +               /* Drop the listener reference with neither the child's socket
> +                * lock nor its DLC lock held: this put can be the final one and
> +                * rfcomm_sock_destruct() takes the listener's own DLC mutex.
> +                */
> +               if (held)
> +                       sock_put(parent);
>                 rfcomm_dlc_lock(d);
>         }
>  }
> --
> 2.47.3
>


-- 
Luiz Augusto von Dentz

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v4] Bluetooth: RFCOMM: hold the listener across child teardown
  2026-11-20 15:10   ` [PATCH v3] " Luiz Augusto von Dentz
@ 2026-11-20 15:15     ` Hang Nan
  2026-11-20 21:30       ` [v4] " bluez.test.bot
  0 siblings, 1 reply; 5+ messages in thread
From: Hang Nan @ 2026-11-20 15:15 UTC (permalink / raw)
  To: linux-bluetooth
  Cc: marcel, luiz.dentz, luiz.von.dentz, pav, zzzccc427,
	enderaoelyther

Bluetooth: RFCOMM: hold the listener across child teardown

rfcomm_sk_state_change() takes the listener from bt_sk(sk)->parent,
unlinks a closing child from the accept queue and then notifies the
listener through that pointer.  bt_accept_enqueue() retains only the
child, so once bt_accept_unlink() has dropped the child's queue reference
a concurrent close() of the listener can run rfcomm_sock_cleanup_listen(),
find an empty queue, and free the listener:

    krfcommd                             close(listener)
    rfcomm_sk_state_change()
      parent = bt_sk(sk)->parent
      bt_accept_unlink(sk)
                                           cleanup_listen(): queue empty
                                           rfcomm_sock_kill() -> kfree
      parent->sk_data_ready(parent)                 <- use-after-free

KASAN reports it as an 8-byte read 264 bytes into a freed kmalloc-1k
object, i.e. the listener sock:

  BUG: KASAN: slab-use-after-free in rfcomm_sk_state_change+0x2ed/0x320
  Read of size 8 at addr ffff888002716908 by task krfcommd/60
  CPU: 1 PID: 60 Comm: krfcommd Tainted: G    B      PREEMPT(full)
    rfcomm_sk_state_change+0x2ed/0x320
    __rfcomm_dlc_close+0x16a/0x3d0
    rfcomm_run+0x1824/0x4000

The window needs full preemption: the only preemption point is the
spin_unlock_bh() at the tail of bt_accept_unlink().  It was reproduced on
7.2-rc6 with two vhci controllers, one side listening and closing, the
other connecting and dropping the child, with an mdelay(3) inserted
between the unlink and the notification for the test (full report below
the --- line).

Same class as commit 4020751fd0df ("Bluetooth: L2CAP: Hold the listener
while notifying child teardown") and commit b4a00dedc5cc ("Bluetooth: ISO:
Hold the listener during disconnect notification"): take a reference
before the unlink and drop it after the notification.

The reference cannot be dropped where it is taken.  Once
bt_accept_unlink() has run, close() may have released the fd reference, so
the sock_put() can be the final put and runs rfcomm_sock_destruct() ->
rfcomm_dlc_lock(listener->dlc) while the child's socket lock and the
child's DLC mutex are still held.  All DLC mutexes share one lockdep class
(rfcomm_dlc_alloc() does a plain mutex_init()) and this function already
runs under d->lock and takes lock_sock(sk), so that nests a same-class
mutex and inverts the d->lock -> sk_lock order.

Drop it in the existing SOCK_ZAPPED tail instead, where the child's socket
lock has been released and its DLC lock is dropped for
rfcomm_sock_kill() - the same place the code already drops the DLC lock so
that the child's own destructor does not deadlock.  The put is gated on
the flag set where the reference is taken rather than on SOCK_ZAPPED
alone, because __rfcomm_sock_close() sets SOCK_ZAPPED directly for
BT_CONNECT, BT_CONNECT2, BT_CONFIG and BT_CONNECTED.

Cc: stable@vger.kernel.org
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Hang Nan <2122295973@qq.com>
---
v4: add the commit description, Signed-off-by, Fixes tag and the KASAN
    report; v3 went out as a bare diff.
v3: drop the reference in the SOCK_ZAPPED tail and gate it on a flag, so
    that a final put cannot run rfcomm_sock_destruct() under the child's
    sk_lock and d->lock (Zhao Li).
v2: cite the two reference commits as 'commit <sha> ("<title>")', as
    checkpatch.pl requires.

Full report (the mdelay(3) only widens the window; the code path is
unchanged):

BUG: KASAN: slab-use-after-free in rfcomm_sk_state_change+0x2ed/0x320
Read of size 8 at addr ffff888002716908 by task krfcommd/60
CPU: 1 PID: 60 Comm: krfcommd Tainted: G    B      PREEMPT(full)
  rfcomm_sk_state_change+0x2ed/0x320
  __rfcomm_dlc_close+0x16a/0x3d0
  rfcomm_run+0x1824/0x4000
  kthread

Allocated by task 72:
  rfcomm_sock_alloc.constprop.0+0x47/0x240
  rfcomm_sock_create+0xa6/0x2a0
  __sys_socket+0x116/0x1d0

Freed by task 72:
  __sk_destruct+0x499/0x5d0
  rfcomm_sock_release+0x101/0x140
  __sock_release+0xa1/0x260
  __x64_sys_close+0x78/0xd0

The buggy address is located 264 bytes inside of a freed 1024-byte region
(kmalloc-1k).


diff --git a/net/bluetooth/rfcomm/sock.c b/net/bluetooth/rfcomm/sock.c
index 3fb8b5474948..94a495e86057 100644
--- a/net/bluetooth/rfcomm/sock.c
+++ b/net/bluetooth/rfcomm/sock.c
@@ -66,6 +66,7 @@ static void rfcomm_sk_state_change(struct rfcomm_dlc *d, int err)
 	__must_hold(&d->lock)
 {
 	struct sock *sk = d->owner, *parent;
+	bool held = false;
 
 	if (!sk)
 		return;
@@ -83,6 +84,12 @@ static void rfcomm_sk_state_change(struct rfcomm_dlc *d, int err)
 	if (parent) {
 		if (d->state == BT_CLOSED) {
 			sock_set_flag(sk, SOCK_ZAPPED);
+			/* close() may already have dropped its reference and the
+			 * queue holds none, so keep the listener alive across the
+			 * unlink and the notification.
+			 */
+			sock_hold(parent);
+			held = true;
 			bt_accept_unlink(sk);
 		}
 		parent->sk_data_ready(parent);
@@ -100,6 +107,12 @@ static void rfcomm_sk_state_change(struct rfcomm_dlc *d, int err)
 		 * rfcomm_sock_destruct() will dead lock. */
 		rfcomm_dlc_unlock(d);
 		rfcomm_sock_kill(sk);
+		/* Drop the listener reference with neither the child's socket
+		 * lock nor its DLC lock held: this put can be the final one and
+		 * rfcomm_sock_destruct() takes the listener's own DLC mutex.
+		 */
+		if (held)
+			sock_put(parent);
 		rfcomm_dlc_lock(d);
 	}
 }


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* RE: [v4] Bluetooth: RFCOMM: hold the listener across child teardown
  2026-11-20 15:15     ` [PATCH v4] " Hang Nan
@ 2026-11-20 21:30       ` bluez.test.bot
  0 siblings, 0 replies; 5+ messages in thread
From: bluez.test.bot @ 2026-11-20 21:30 UTC (permalink / raw)
  To: linux-bluetooth, 2122295973

[-- Attachment #1: Type: text/plain, Size: 1073 bytes --]

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list.
This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/series/1217473/

---Test result---

Test Summary:
CheckPatch                    PASS      0.52 seconds
VerifyFixes                   PASS      0.06 seconds
VerifySignedoff               PASS      0.07 seconds
GitLint                       PASS      0.19 seconds
SubjectPrefix                 PASS      0.06 seconds
BuildKernel                   PASS      23.20 seconds
CheckAllWarning               PASS      27.38 seconds
CheckSparse                   PASS      29.28 seconds
BuildKernel32                 PASS      23.44 seconds
CheckKernelLLVM               PASS      23.88 seconds
TestRunnerSetup               PASS      550.85 seconds
TestRunner_rfcomm-tester      PASS      6.88 seconds
IncrementalBuild              PASS      22.55 seconds



https://github.com/bluez/bluetooth-next/pull/1169

---
Regards,
Linux Bluetooth


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-20 21:30 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20261115191908.3646-enderaoelyther@gmail.com>
2026-11-20 12:18 ` [PATCH v3] Bluetooth: RFCOMM: hold the listener across child teardown Hang Nan
2026-11-20 13:28   ` [v3] " bluez.test.bot
2026-11-20 15:10   ` [PATCH v3] " Luiz Augusto von Dentz
2026-11-20 15:15     ` [PATCH v4] " Hang Nan
2026-11-20 21:30       ` [v4] " bluez.test.bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.