Linux bluetooth development
 help / color / mirror / Atom feed
From: Zhao Li <enderaoelyther@gmail.com>
To: Hang Nan <2122295973@qq.com>
Cc: linux-bluetooth@vger.kernel.org,
	Marcel Holtmann <marcel@holtmann.org>,
	Luiz Augusto von Dentz <luiz.dentz@gmail.com>,
	Luiz Augusto von Dentz <luiz.von.dentz@intel.com>,
	Pauli Virtanen <pav@iki.fi>, Cen Zhang <zzzccc427@gmail.com>
Subject: Re: [PATCH v2] Bluetooth: RFCOMM: hold the listener across child teardown
Date: Mon, 16 Nov 2026 03:19:08 +0800	[thread overview]
Message-ID: <20261115191908.3646-1-enderaoelyther@gmail.com> (raw)
In-Reply-To: <tencent_D6C2C07E8FD8FC290CA9F9AB237DAE4CE505@qq.com>

Hi Hang,

On Sat, Oct 24, 2026 at 08:22:17PM +0800, Hang Nan wrote:
> before the unlink and drop it after the notification.  Lock order is
> unchanged, the child lock keeps cleanup_listen() out until then.

[...]

>  		if (d->state == BT_CLOSED) {
>  			sock_set_flag(sk, SOCK_ZAPPED);
> +			/* keep the listener alive across the unlink */
> +			sock_hold(parent);
>  			bt_accept_unlink(sk);
> +			parent->sk_data_ready(parent);
> +			sock_put(parent);

Whenever this sock_hold(parent) actually prevents the use-after-free --
i.e. cleanup_listen() sees an empty accept queue after
bt_accept_unlink(sk) and the listener close drops its own reference
first -- this sock_put(parent) becomes the final put and runs
rfcomm_sock_destruct(parent) right here, while both lock_sock(sk) and
the child's rfcomm_dlc_lock(d) are still held.

Unlike l2cap_sock_destruct() and iso_sock_destruct(), which only drop a
refcount, rfcomm_sock_destruct() acquires the socket's own DLC lock
(even a listening RFCOMM socket carries a DLC allocated in
rfcomm_sock_alloc()). Running it at this point has two lockdep problems:

1. The listener's DLC mutex is a different instance from the child's,
   but both are initialized in rfcomm_dlc_alloc() with the same lockdep
   class, creating a nested same-class mutex acquisition.
2. rfcomm_sk_state_change() enters under rfcomm_dlc_lock(d), as its
   __must_hold(&d->lock) annotation states, and then takes lock_sock(sk),
   so taking the listener's rfcomm_dlc_lock() before release_sock(sk)
   also inverts the d->lock -> sk_lock order.

I caught this by reading the code rather than hitting a lockdep splat in
QEMU (the window between bt_accept_unlink() and sock_put() in v2 is tiny
without an injected delay, so the final put never landed there in my
runs), and I have not established an actual two-DLC deadlock for (1).
Still, existing code already drops the child's DLC lock after
release_sock(sk) around rfcomm_sock_kill(), with a comment saying
rfcomm_sock_destruct() would otherwise dead lock.

Dropping the listener reference there avoids both issues:

	if (parent && sock_flag(sk, SOCK_ZAPPED)) {
		rfcomm_dlc_unlock(d);
		rfcomm_sock_kill(sk);
		sock_put(parent);
		rfcomm_dlc_lock(d);
	}

The reference remains balanced: a child with a non-NULL parent is still
queued when the hold is taken under lock_sock(sk), and this callback
sets SOCK_ZAPPED before unlinking it. If d->state != BT_CLOSED, any
concurrent close of the child must go through rfcomm_dlc_close(), which
waits for the rfcomm_lock() the caller holds, before it can set
SOCK_ZAPPED.

In my QEMU/KASAN reproducer on commit 6b4b543fa187 (stalling krfcommd
with an inbound packet flood while closing the listener), the base
kernel hit the listener use-after-free in both boots, while dropping the
reference in the SOCK_ZAPPED block above survived two boots of 826 and
831 rounds cleanly.

Thanks,
Zhao

      parent reply	other threads:[~2026-11-15 19:19 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-24 12:22 [PATCH v2] Bluetooth: RFCOMM: hold the listener across child teardown Hang Nan
2026-10-24 15:24 ` [v2] " bluez.test.bot
2026-11-15 19:19 ` Zhao Li [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261115191908.3646-1-enderaoelyther@gmail.com \
    --to=enderaoelyther@gmail.com \
    --cc=2122295973@qq.com \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=luiz.dentz@gmail.com \
    --cc=luiz.von.dentz@intel.com \
    --cc=marcel@holtmann.org \
    --cc=pav@iki.fi \
    --cc=zzzccc427@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox