Bluetooth: RFCOMM: add the listening session under rfcomm_mutex
Subsystems (2)
PATCH 1/1 Bluetooth: RFCOMM: add the listening session under rfcomm_mutex
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.
---
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(+)
net/bluetooth/rfcomm/core.c +2 -0
@@ -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
Signature · 1 line
2.43.7