The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Jaskaran Singh <jsingh@cloudlinux.com>
To: Marcel Holtmann <marcel@holtmann.org>,
	Luiz Augusto von Dentz <luiz.dentz@gmail.com>
Cc: linux-bluetooth@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] Bluetooth: RFCOMM: add the listening session under rfcomm_mutex
Date: Wed, 18 Nov 2026 09:49:11 +0000	[thread overview]
Message-ID: <20261118094911.277-1-jsingh@cloudlinux.com> (raw)

Sessions are added to session_list by rfcomm_dlc_open() and by
rfcomm_accept_connection(), and removed by rfcomm_session_close(), all
with rfcomm_mutex held.  The listening session is the exception:
krfcommd adds it in rfcomm_add_listener() without taking the mutex.

That relies on krfcommd adding its listener before anything else can
reach session_list.  rfcomm_init() starts krfcommd with kthread_run(),
which only wakes the new thread, and then goes on to
rfcomm_init_sockets().  Once bt_sock_register() has made
BTPROTO_RFCOMM available, a connect() can reach rfcomm_dlc_open() and
add a session while krfcommd is adding the listener. The two
list_add() calls on session_list then race and corrupt the list:

  list_add corruption. next->prev should be prev (ffffffffc0f12540), but was ffff8cdd86b43700. (next=ffff8cdd84444f80).
  ------------[ cut here ]------------
  kernel BUG at lib/list_debug.c:29!
  Oops: invalid opcode: 0000 [#1] SMP PTI
  CPU: 1 UID: 0 PID: 7188 Comm: krfcommd Kdump: loaded Not tainted 7.4.0-rc2-vanilla #2 PREEMPT(lazy)
  RIP: 0010:__list_add_valid_or_report+0x6d/0xa0
  Call Trace:
   <TASK>
   rfcomm_session_add+0xa5/0x110 [rfcomm]
   rfcomm_run+0x145/0x1bb0 [rfcomm]
   kthread+0xe9/0x120
   ret_from_fork+0x2bc/0x350
   ret_from_fork_asm+0x1a/0x30
   </TASK>

The above stack trace was reproduced on a rfcomm module with an added
msleep(200) before the rfcomm_add_listener() call.

Add the listening session with rfcomm_mutex held, like the other
sessions.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Jaskaran Singh <jsingh@cloudlinux.com>
---
The trace above is from mainline v7.4-rc2 (Debian trixie's kernel
config).  rfcomm.ko was rebuilt in-tree with one debug change,
msleep(200) before rfcomm_add_listener() in rfcomm_run(), so that the
listener is added after rfcomm_init_sockets() has made BTPROTO_RFCOMM
reachable. The corruption above hit on the 9th module load. 

Without any added delay it was not observed (0 hits in ~158,000 module
loads).

The race was found and reproduced with the help of an AI coding
assistant (Claude). Reproducer available on request.


 net/bluetooth/rfcomm/core.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/net/bluetooth/rfcomm/core.c b/net/bluetooth/rfcomm/core.c
index 8a4205347c4a..2d35121224a0 100644
--- a/net/bluetooth/rfcomm/core.c
+++ b/net/bluetooth/rfcomm/core.c
@@ -2219,7 +2219,9 @@ static int rfcomm_add_listener(bdaddr_t *ba)
 	}
 
 	/* Add listening session */
+	rfcomm_lock();
 	s = rfcomm_session_add(sock, BT_LISTEN);
+	rfcomm_unlock();
 	if (!s) {
 		err = -ENOMEM;
 		goto failed;

base-commit: 3c5963d23d64972385786e0be47be16d89ff4434
-- 
2.43.7


                 reply	other threads:[~2026-11-18  9:49 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261118094911.277-1-jsingh@cloudlinux.com \
    --to=jsingh@cloudlinux.com \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luiz.dentz@gmail.com \
    --cc=marcel@holtmann.org \
    /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