nfc: llcp: Fix race condition in accept_queue lifecycle
Subsystems (3)
PATCH 1/1 nfc: llcp: Fix race condition in accept_queue lifecycle
nfc_llcp_socket_release() walks the local sockets list and the listener socket's accept_queue under bh_lock_sock(). However, bh_lock_sock() does not provide mutual exclusion against process-context lock_sock() held by nfc_llcp_accept_dequeue() during accept(). Since socket_release() runs in process context (workqueue or kref cleanup), it never checks sock_owned_by_user(), allowing both paths to manipulate the same child socket concurrently. This leads to a double sock_put() (Use-After-Free) or a NULL pointer dereference of child->parent in nfc_llcp_accept_unlink().
Fix this by converting nfc_llcp_socket_release() to use lock_sock(). Since it is called with the local sockets write-lock held (which disables preemption, preventing sleep), repeatedly pop the head socket from the list under the write-lock using a new helper nfc_llcp_sock_list_pop(), then safely acquire lock_sock() on it after dropping the list lock. A sock_hold() is taken under the list lock to keep the socket alive across the sleep and balanced with a sock_put() after release.
Additionally, add a check for a non-NULL parent in nfc_llcp_accept_unlink() to ensure idempotency and prevent NULL pointer dereference if the unlink races with dequeue.
--- net/nfc/llcp_core.c | 47 +++++++++++++++++++++++++-------------------- net/nfc/llcp_sock.c | 13 +++++++------ 2 files changed, 33 insertions(+), 27 deletions(-)
net/nfc/llcp_core.c +26 -21
@@ -63,21 +63,33 @@ static void nfc_llcp_socket_purge(struct nfc_llcp_sock *sock) } } +static struct sock *nfc_llcp_sock_list_pop (struct llcp_sock_list *l) +{ +struct sock *sk; + +write_lock (&l->lock); + sk =sk_head (&l->head); +if (sk) { +sock_hold (sk); +sk_del_node_init (sk); + } +write_unlock (&l->lock); + +return sk; +} +static void nfc_llcp_socket_release (struct nfc_llcp_local *local,bool device,int err) {struct sock *sk; -struct hlist_node *tmp;struct nfc_llcp_sock *llcp_sock;skb_queue_purge (&local->tx_queue); -write_lock (&local->sockets.lock); - -sk_for_each_safe (sk, tmp, &local->sockets.head) { +while ((sk =nfc_llcp_sock_list_pop (&local->sockets))) { llcp_sock =nfc_llcp_sock (sk); -bh_lock_sock (sk); +lock_sock (sk);nfc_llcp_socket_purge (llcp_sock);
@@ -92,7 +104,8 @@ static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool device, &llcp_sock->accept_queue, accept_queue) { accept_sk = &lsk->sk; -bh_lock_sock (accept_sk); +lock_sock_nested (accept_sk, +SINGLE_DEPTH_NESTING );nfc_llcp_accept_unlink (accept_sk);
@@ -101,7 +114,7 @@ static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool device, accept_sk->sk_state =LLCP_CLOSED ; accept_sk->sk_state_change (sk); -bh_unlock_sock (accept_sk); +release_sock (accept_sk); } }
@@ -110,23 +123,18 @@ static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool device, sk->sk_state =LLCP_CLOSED ; sk->sk_state_change (sk); -bh_unlock_sock (sk); - -sk_del_node_init (sk); +release_sock (sk); +sock_put (sk); } -write_unlock (&local->sockets.lock); -/* If we still have a device, we keep the RAW sockets alive */ if (device == true)return ; -write_lock (&local->raw_sockets.lock); - -sk_for_each_safe (sk, tmp, &local->raw_sockets.head) { +while ((sk =nfc_llcp_sock_list_pop (&local->raw_sockets))) { llcp_sock =nfc_llcp_sock (sk); -bh_lock_sock (sk); +lock_sock (sk);nfc_llcp_socket_purge (llcp_sock);
@@ -135,12 +143,9 @@ static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool device, sk->sk_state =LLCP_CLOSED ; sk->sk_state_change (sk); -bh_unlock_sock (sk); - -sk_del_node_init (sk); +release_sock (sk); +sock_put (sk); } - -write_unlock (&local->raw_sockets.lock); }static struct nfc_llcp_local *nfc_llcp_local_get (struct nfc_llcp_local *local)
net/nfc/llcp_sock.c +7 -6
@@ -384,11 +384,12 @@ void nfc_llcp_accept_unlink(struct sock *sk)pr_debug ("state %d\n" , sk->sk_state); -list_del_init (&llcp_sock->accept_queue); -sk_acceptq_removed (llcp_sock->parent); - llcp_sock->parent =NULL ; - -sock_put (sk); +if (llcp_sock->parent) { +list_del_init (&llcp_sock->accept_queue); +sk_acceptq_removed (llcp_sock->parent); + llcp_sock->parent =NULL ; +sock_put (sk); + } }void nfc_llcp_accept_enqueue (struct sock *parent,struct sock *sk)
@@ -622,7 +623,7 @@ static int llcp_sock_release(struct socket *sock)list_for_each_entry_safe (lsk, n, &llcp_sock->accept_queue, accept_queue) { accept_sk = &lsk->sk; -lock_sock (accept_sk); +lock_sock_nested (accept_sk,SINGLE_DEPTH_NESTING );nfc_llcp_send_disconnect (lsk);nfc_llcp_accept_unlink (accept_sk);
Signature · 1 line
2.54.0.823.g6e5bcc1fc9-goog
[INTENTIONAL TOP POST]
This one has been knocking around for a very long time.
Would you like me to [RESEND] it, or does everyone still have it?
Quoted 167 lines · Lee Jones
nfc_llcp_socket_release()walks the local sockets list and the listener socket'saccept_queueunderbh_lock_sock(). However,bh_lock_sock()does not provide mutual exclusion against process-contextlock_sock()held bynfc_llcp_accept_dequeue()duringaccept(). Sincesocket_release()runs in process context (workqueue or kref cleanup), it never checkssock_owned_by_user(), allowing both paths to manipulate the same child socket concurrently. This leads to a doublesock_put()(Use-After-Free) or a NULL pointer dereference ofchild->parentinnfc_llcp_accept_unlink().Fix this by converting
nfc_llcp_socket_release()to uselock_sock(). Since it is called with the local sockets write-lock held (which disables preemption, preventing sleep), repeatedly pop the head socket from the list under the write-lock using a new helpernfc_llcp_sock_list_pop(), then safely acquirelock_sock()on it after dropping the list lock. Asock_hold()is taken under the list lock to keep the socket alive across the sleep and balanced with asock_put()after release.Additionally, add a check for a non-NULL parent in
nfc_llcp_accept_unlink()to ensure idempotency and prevent NULL pointer dereference if the unlink races with dequeue.Fixes 50b78b2a6500 ("NFC: Fix sleeping in atomic when releasing socket")Signed-off-by Lee Jones <[email protected]>--- net/nfc/llcp_core.c | 47 +++++++++++++++++++++++++-------------------- net/nfc/llcp_sock.c | 13 +++++++------ 2 files changed, 33 insertions(+), 27 deletions(-)net/nfc/llcp_core.c +26
-21@@ -63,21 +63,33 @@ static void nfc_llcp_socket_purge(struct nfc_llcp_sock *sock) } } +static struct sock *nfc_llcp_sock_list_pop (struct llcp_sock_list *l) +{ +struct sock *sk; + +write_lock (&l->lock); + sk =sk_head (&l->head); +if (sk) { +sock_hold (sk); +sk_del_node_init (sk); + } +write_unlock (&l->lock); + +return sk; +} +static void nfc_llcp_socket_release (struct nfc_llcp_local *local,bool device,int err) {struct sock *sk; -struct hlist_node *tmp;struct nfc_llcp_sock *llcp_sock;skb_queue_purge (&local->tx_queue); -write_lock (&local->sockets.lock); - -sk_for_each_safe (sk, tmp, &local->sockets.head) { +while ((sk =nfc_llcp_sock_list_pop (&local->sockets))) { llcp_sock =nfc_llcp_sock (sk); -bh_lock_sock (sk); +lock_sock (sk);nfc_llcp_socket_purge (llcp_sock);@@ -92,7 +104,8 @@ static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool device, &llcp_sock->accept_queue, accept_queue) { accept_sk = &lsk->sk; -bh_lock_sock (accept_sk); +lock_sock_nested (accept_sk, +SINGLE_DEPTH_NESTING );nfc_llcp_accept_unlink (accept_sk);@@ -101,7 +114,7 @@ static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool device, accept_sk->sk_state =LLCP_CLOSED ; accept_sk->sk_state_change (sk); -bh_unlock_sock (accept_sk); +release_sock (accept_sk); } }@@ -110,23 +123,18 @@ static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool device, sk->sk_state =LLCP_CLOSED ; sk->sk_state_change (sk); -bh_unlock_sock (sk); - -sk_del_node_init (sk); +release_sock (sk); +sock_put (sk); } -write_unlock (&local->sockets.lock); -/* If we still have a device, we keep the RAW sockets alive */ if (device == true)return ; -write_lock (&local->raw_sockets.lock); - -sk_for_each_safe (sk, tmp, &local->raw_sockets.head) { +while ((sk =nfc_llcp_sock_list_pop (&local->raw_sockets))) { llcp_sock =nfc_llcp_sock (sk); -bh_lock_sock (sk); +lock_sock (sk);nfc_llcp_socket_purge (llcp_sock);@@ -135,12 +143,9 @@ static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool device, sk->sk_state =LLCP_CLOSED ; sk->sk_state_change (sk); -bh_unlock_sock (sk); - -sk_del_node_init (sk); +release_sock (sk); +sock_put (sk); } - -write_unlock (&local->raw_sockets.lock); }static struct nfc_llcp_local *nfc_llcp_local_get (struct nfc_llcp_local *local)net/nfc/llcp_sock.c +7
-6@@ -384,11 +384,12 @@ void nfc_llcp_accept_unlink(struct sock *sk)pr_debug ("state %d\n" , sk->sk_state); -list_del_init (&llcp_sock->accept_queue); -sk_acceptq_removed (llcp_sock->parent); - llcp_sock->parent =NULL ; - -sock_put (sk); +if (llcp_sock->parent) { +list_del_init (&llcp_sock->accept_queue); +sk_acceptq_removed (llcp_sock->parent); + llcp_sock->parent =NULL ; +sock_put (sk); + } }void nfc_llcp_accept_enqueue (struct sock *parent,struct sock *sk)@@ -622,7 +623,7 @@ static int llcp_sock_release(struct socket *sock)list_for_each_entry_safe (lsk, n, &llcp_sock->accept_queue, accept_queue) { accept_sk = &lsk->sk; -lock_sock (accept_sk); +lock_sock_nested (accept_sk,SINGLE_DEPTH_NESTING );nfc_llcp_send_disconnect (lsk);nfc_llcp_accept_unlink (accept_sk);Signature · 1 line
2.54.0.823.g6e5bcc1fc9-goog
Signature · 1 line
Lee Jones
David Heidelberg
maintainerQuoted 6 lines · Lee Jones
[INTENTIONAL TOP POST]
This one has been knocking around for a very long time.
Would you like me to [RESEND] it, or does everyone still have it?
Hello Lee,
sorry, I've must missed the patch. Could you check the Sashiko comments (below)?
Feel free to ignore the pre-existing issue, the main complaint is about the description.
Thank you
David
commit 74abfc44e401a556abde5003d20ce7cf50ee958d
Author: Lee Jones <[email protected]>
nfc: llcp: Fix race condition in accept_queue lifecycle
This commit attempts to fix a race condition between nfc_llcp_socket_release() and nfc_llcp_accept_dequeue() by replacing bh_lock_sock() with lock_sock() and lock_sock_nested(). It also introduces a socket popping helper and adds a parent check in nfc_llcp_accept_unlink().
Quoted 5 lines
nfc_llcp_socket_release()walks the local sockets list and the listener socket'saccept_queueunderbh_lock_sock(). However,bh_lock_sock()does not provide mutual exclusion against process-contextlock_sock()held bynfc_llcp_accept_dequeue()duringaccept(). Sincesocket_release()runs in process context (workqueue or kref cleanup), it never checks
Is this description accurate?
nfc_llcp_data_received() routinely runs from softirq/interrupt context via URB completion handlers like pn533_recv_response(). When it drops the last reference via nfc_llcp_local_put(), this triggers local_cleanup() and nfc_llcp_socket_release().
Could this lead to a sleeping in atomic bug since lock_sock() acquires a mutex?
Quoted 3 lines
sock_owned_by_user(), allowing both paths to manipulate the same child socket concurrently. This leads to a doublesock_put()(Use-After-Free) or a NULL pointer dereference ofchild->parentinnfc_llcp_accept_unlink().
[ ... ]
Quoted 8 lines
net/nfc/llcp_core.c +0
-0@@ -63,21 +63,33 @@ static void nfc_llcp_socket_purge(struct nfc_llcp_sock *sock) } }
[ ... ]
net/nfc/llcp_core.c static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool device, · line 107
Quoted 7 lines
net/nfc/llcp_core.c +2
-2@@ -92,7 +104,8 @@ static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool device, -sk_for_each_safe (sk, tmp, &local->sockets.head) { +while ((sk =nfc_llcp_sock_list_pop (&local->sockets))) { llcp_sock =nfc_llcp_sock (sk); -bh_lock_sock (sk); +lock_sock (sk);
Since nfc_llcp_socket_release() can be invoked from softirq teardown paths, does calling lock_sock() here cause a sleeping in atomic bug because it acquires a mutex?
[ ... ]
net/nfc/llcp_core.c static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool device, · line 108
Quoted 6 lines
net/nfc/llcp_core.c +2
-1&llcp_sock->accept_queue, accept_queue) { accept_sk = &lsk->sk; -bh_lock_sock (accept_sk); +lock_sock_nested (accept_sk, +SINGLE_DEPTH_NESTING );
Does this nested lock_sock_nested() also cause a sleep in atomic context when nfc_llcp_socket_release() is invoked from softirq teardown paths?
Quoted 1 line
nfc_llcp_accept_unlink(accept_sk);
This isn't a bug introduced by this patch, but does this leak the initial sk_alloc reference count?
Sockets created for incoming connections receive an initial sk_alloc reference. When the listener is closed, unaccepted child sockets are unlinked here and their list references are dropped.
Since they were never accepted by userspace, sock_release() is never called. Do these unaccepted sockets permanently leak?
[ ... ]
Quoted 5 lines
net/nfc/llcp_core.c +0
-1@@ -135,12 +143,9 @@ static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool device, sk->sk_state =LLCP_CLOSED ; sk->sk_state_change (sk); -bh_unlock_sock (sk);
net/nfc/llcp_core.c static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool device, · line 151
Quoted 10 lines
net/nfc/llcp_core.c +2
-4- -sk_del_node_init (sk); +release_sock (sk); +sock_put (sk); } - -write_unlock (&local->raw_sockets.lock); }static struct nfc_llcp_local *nfc_llcp_local_get (struct nfc_llcp_local *local)
This isn't a bug introduced by this patch, but is it possible for a dying object to be resurrected here?
If local_release() runs local_cleanup(), it purges sockets via nfc_llcp_socket_release() and then waits for rx_work via cancel_work_sync().
If rx_work executes concurrently, it calls nfc_llcp_local_get() which uses kref_get() without checking for zero. Can rx_work then allocate and link a new socket just before local is freed, leading to a Use-After-Free?
Quoted 4 lines
net/nfc/llcp_sock.c +0
-0
[ ... ]
net/nfc/llcp_sock.c static int llcp_sock_release(struct socket *sock) · line 629
Quoted 9 lines
net/nfc/llcp_sock.c +1
-1@@ -622,7 +623,7 @@ static int llcp_sock_release(struct socket *sock)list_for_each_entry_safe (lsk, n, &llcp_sock->accept_queue, accept_queue) { accept_sk = &lsk->sk; -lock_sock (accept_sk); +lock_sock_nested (accept_sk,SINGLE_DEPTH_NESTING );nfc_llcp_send_disconnect (lsk);nfc_llcp_accept_unlink (accept_sk);
This isn't a bug introduced by this patch, but as mentioned above, does unlinking unaccepted child sockets here permanently leak their initial reference count?
This isn't a bug introduced by this patch, but can the blocking loop in llcp_sock_accept() hang indefinitely?
If the listener socket is shut down or released concurrently, the thread wakes up but doesn't check sk->sk_state:
llcp_sock_accept() {
...
while (!(new_sk = nfc_llcp_accept_dequeue(sk, newsock))) {
set_current_state(TASK_INTERRUPTIBLE);
...
release_sock(sk);
timeo = schedule_timeout(timeo);
lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
}
...
}Will it just go back to sleep waiting for a closed socket if no new socket was dequeued?