Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

rxrpc: Fix recv-recv race of completed call

If a call receives an event (such as incoming data), the call gets placed
on the socket's queue and a thread in recvmsg can be awakened to go and
process it. Once the thread has picked up the call off of the queue,
further events will cause it to be requeued, and once the socket lock is
dropped (recvmsg uses call->user_mutex to allow the socket to be used in
parallel), a second thread can come in and its recvmsg can pop the call off
the socket queue again.

In such a case, the first thread will be receiving stuff from the call and
the second thread will be blocked on call->user_mutex. The first thread
can, at this point, process both the event that it picked call for and the
event that the second thread picked the call for and may see the call
terminate - in which case the call will be "released", decoupling the call
from the user call ID assigned to it (RXRPC_USER_CALL_ID in the control
message).

The first thread will return okay, but then the second thread will wake up
holding the user_mutex and, if it sees that the call has been released by
the first thread, it will BUG thusly:

kernel BUG at net/rxrpc/recvmsg.c:474!

Fix this by just dequeuing the call and ignoring it if it is seen to be
already released. We can't tell userspace about it anyway as the user call
ID has become stale.

Fixes: 248f219cb8bc ("rxrpc: Rewrite the data and ack handling code")
Reported-by: Junvyyang, Tencent Zhuque Lab <zhuque@tencent.com>
Signed-off-by: David Howells <dhowells@redhat.com>
Reviewed-by: Jeffrey Altman <jaltman@auristor.com>
cc: LePremierHomme <kwqcheii@proton.me>
cc: Marc Dionne <marc.dionne@auristor.com>
cc: Simon Horman <horms@kernel.org>
cc: linux-afs@lists.infradead.org
Link: https://patch.msgid.link/20250717074350.3767366-3-dhowells@redhat.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

David Howells and committed by
Jakub Kicinski
962fb1f6 e4d28783

+21 -2
+3
include/trace/events/rxrpc.h
··· 330 330 EM(rxrpc_call_put_userid, "PUT user-id ") \ 331 331 EM(rxrpc_call_see_accept, "SEE accept ") \ 332 332 EM(rxrpc_call_see_activate_client, "SEE act-clnt") \ 333 + EM(rxrpc_call_see_already_released, "SEE alrdy-rl") \ 333 334 EM(rxrpc_call_see_connect_failed, "SEE con-fail") \ 334 335 EM(rxrpc_call_see_connected, "SEE connect ") \ 335 336 EM(rxrpc_call_see_conn_abort, "SEE conn-abt") \ 337 + EM(rxrpc_call_see_discard, "SEE discard ") \ 336 338 EM(rxrpc_call_see_disconnected, "SEE disconn ") \ 337 339 EM(rxrpc_call_see_distribute_error, "SEE dist-err") \ 338 340 EM(rxrpc_call_see_input, "SEE input ") \ 341 + EM(rxrpc_call_see_recvmsg, "SEE recvmsg ") \ 339 342 EM(rxrpc_call_see_release, "SEE release ") \ 340 343 EM(rxrpc_call_see_userid_exists, "SEE u-exists") \ 341 344 EM(rxrpc_call_see_waiting_call, "SEE q-conn ") \
+1
net/rxrpc/call_accept.c
··· 219 219 tail = b->call_backlog_tail; 220 220 while (CIRC_CNT(head, tail, size) > 0) { 221 221 struct rxrpc_call *call = b->call_backlog[tail]; 222 + rxrpc_see_call(call, rxrpc_call_see_discard); 222 223 rcu_assign_pointer(call->socket, rx); 223 224 if (rx->app_ops && 224 225 rx->app_ops->discard_new_call) {
+17 -2
net/rxrpc/recvmsg.c
··· 447 447 goto try_again; 448 448 } 449 449 450 + rxrpc_see_call(call, rxrpc_call_see_recvmsg); 451 + if (test_bit(RXRPC_CALL_RELEASED, &call->flags)) { 452 + rxrpc_see_call(call, rxrpc_call_see_already_released); 453 + list_del_init(&call->recvmsg_link); 454 + spin_unlock_irq(&rx->recvmsg_lock); 455 + release_sock(&rx->sk); 456 + trace_rxrpc_recvmsg(call->debug_id, rxrpc_recvmsg_unqueue, 0); 457 + rxrpc_put_call(call, rxrpc_call_put_recvmsg); 458 + goto try_again; 459 + } 450 460 if (!(flags & MSG_PEEK)) 451 461 list_del_init(&call->recvmsg_link); 452 462 else ··· 480 470 481 471 release_sock(&rx->sk); 482 472 483 - if (test_bit(RXRPC_CALL_RELEASED, &call->flags)) 484 - BUG(); 473 + if (test_bit(RXRPC_CALL_RELEASED, &call->flags)) { 474 + rxrpc_see_call(call, rxrpc_call_see_already_released); 475 + mutex_unlock(&call->user_mutex); 476 + if (!(flags & MSG_PEEK)) 477 + rxrpc_put_call(call, rxrpc_call_put_recvmsg); 478 + goto try_again; 479 + } 485 480 486 481 ret = rxrpc_recvmsg_user_id(call, msg, flags); 487 482 if (ret < 0)