nfc: llcp: fix socket list self-loop on connect of a linked socket
Subsystems (4)
PATCH v2 1/1 nfc: llcp: fix socket list self-loop on connect of a linked socket
llcp_sock_connect() only rejects LLCP_CONNECTED and LLCP_CONNECTING, so a socket that is still linked in local->sockets can be linked a second time into local->connecting_sockets. That covers bound and listening sockets, and sockets that a peer moved to LLCP_CLOSED with DISC or DM.
Both lists use sk->sk_node. When the peer answers with CC, nfc_llcp_recv_cc() removes the node from the connecting list and links it back into local->sockets, where the stale bind-time entry makes the node point to itself. Every later sk_for_each() over local->sockets then spins forever with the read lock held, which pins the CPU and stalls RCU, and the socket is never freed.
Reject the connection while sk_node is still linked. A socket has to be closed before it can be connected.
Reproduced on v7.4-rc3 as uid 1000: a soft lockup / RCU stall in nfc_llcp_sock_from_sn() while walking local->sockets.
This issue was discovered by Tencent CodeBuddy Security.
---
v1 -> v2: reject any socket that is still hashed (sk_hashed) instead of only
LLCP_BOUND, covering bind()+listen()+connect() and sockets left linked after DISC/DM.
net/nfc/llcp_sock.c | 9 +++++++++ 1 file changed, 9 insertions(+)
net/nfc/llcp_sock.c +9 -0
@@ -714,6 +714,15 @@ static int llcp_sock_connect(struct socket *sock, struct sockaddr_unsized *_addr ret = -EINPROGRESS ; goto error; } +/* A socket that is still linked in local->sockets must not be linked +* into local->connecting_sockets as well: both lists use sk_node, and +* the CC handler links the node back into local->sockets, which turns +* the stale entry into a self-loop. +*/ +if (sk_hashed (sk)) { + ret = -EISCONN ; + goto error; + } dev =nfc_get_device (addr->dev_idx);if (dev ==NULL ) {
Signature · 1 line
2.43.7
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
Critical: 0 · High: 1 (1) · Medium: 0 · Low: 1
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [High] Incomplete fix: the same sk_node double-link, and the same endless loop over local->sockets, can still be reached through…
- [Low] The new check in llcp_sock_connect() (net/nfc/llcp_sock.c:722-725) returns -EISCONN for any hashed socket,…
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but it is the same shared-sk_node bug class, in the same function, and it is still…
--
nfc: llcp: fix socket list self-loop on connect of a linked socket
This patch makes llcp_sock_connect() reject a socket whose sk_node is still linked, so that the node is never on both local->sockets and local->connecting_sockets. Before this, nfc_llcp_recv_cc() could link the node back into local->sockets and make it point to itself.
Quoted 7 lines
net/nfc/llcp_sock.c +0
-0@@ -714,6 +714,15 @@ static int llcp_sock_connect(struct socket *sock, struct sockaddr_unsized *_addr ret = -EINPROGRESS ; goto error;
net/nfc/llcp_sock.c static int llcp_sock_connect(struct socket *sock, struct sockaddr_unsized *_addr · line 725
Quoted 10 lines
net/nfc/llcp_sock.c +9
-0} +/* A socket that is still linked in local->sockets must not be linked +* into local->connecting_sockets as well: both lists use sk_node, and +* the CC handler links the node back into local->sockets, which turns +* the stale entry into a self-loop. +*/ +if (sk_hashed (sk)) { + ret = -EISCONN ; + goto error; + }
[Severity: High]
The commit message lists "sockets that a peer moved to LLCP_CLOSED with DISC or DM" as one of the problem states. Can those sockets still get their sk_node linked twice through llcp_sock_bind()?
nfc_llcp_recv_disc() moves a connected socket to LLCP_CLOSED but leaves it linked in local->sockets:
net/nfc/llcp_core.c:nfc_llcp_recv_disc() {
...
if (sk->sk_state == LLCP_CONNECTED) {
nfc_put_device(local->dev);
sk->sk_state = LLCP_CLOSED;
sk->sk_state_change(sk);
}
...
}For any reason other than LLCP_DM_NOBOUND or LLCP_DM_REJ, nfc_llcp_recv_dm() looks the socket up on local->sockets through nfc_llcp_sock_get(). Since connecting is false in that case, the socket stays linked:
net/nfc/llcp_core.c:nfc_llcp_recv_dm() {
...
if (connecting)
nfc_llcp_sock_unlink(&local->connecting_sockets, sk);
sk->sk_err = ENXIO;
sk->sk_state = LLCP_CLOSED;
...
}That applies to connected sockets, accepted children, and bound or listening sockets whose ssap/dsap match the DM.
llcp_sock_bind() only checks the state:
if (sk->sk_state != LLCP_CLOSED) {
ret = -EBADFD;
goto error;
}nfc_llcp_sock_from_sn() skips CLOSED sockets. With a non-WKS service name, nfc_llcp_get_sdp_ssap() therefore returns LLCP_SDP_UNBOUND, and bind() reaches:
nfc_llcp_sock_link(&local->sockets, sk);
nfc_llcp_sock_link()->sk_add_node()->hlist_add_head() runs on a node that is already in the list. If the node was first, it now points to itself. Otherwise the list gets a cycle.
Wouldn't every later sk_for_each() over local->sockets, such as nfc_llcp_sock_get() in the RX worker or nfc_llcp_sock_from_sn(), then spin forever with the read lock held? sk_add_node() also takes an extra sock_hold() that is never released.
Should llcp_sock_bind() get the same sk_hashed() check? Alternatively, the DISC and DM handlers could unlink the socket from local->sockets.
[Severity: Low]
This returns -EISCONN for any hashed socket, not just LLCP_CONNECTED ones. That includes LLCP_BOUND and LLCP_LISTEN sockets, and sockets a peer left in LLCP_CLOSED with DISC or DM. Is -EISCONN the intended errno in those cases?
In the usual non-blocking connect pattern, a repeated connect() that returns EISCONN means the socket is connected. After a CC and then a default-reason DM, connect() would no longer report the pending ENXIO in sk_err. The application would be told the socket is connected after the link has gone away.
TCP's __inet_stream_connect() also returns -EISCONN for some non-connected states, so this may be deliberate. Still, the other wrong-state checks in this file (llcp_sock_bind(), llcp_sock_listen(), llcp_sock_accept()) return -EBADFD. The commit message also doesn't mention this change to the errno userspace sees.
[Severity: High]
This is a pre-existing issue and was not introduced by this patch. It is the same shared sk_node problem in the same function, further down in the sock_unlink error path of llcp_sock_connect(). Can that path unlink the socket from the wrong list and drop the nfc_dev reference twice?
sock_wait_state() releases the socket lock while it sleeps:
release_sock(sk); timeo = schedule_timeout(timeo); lock_sock(sk);
While the lock is released, the RX worker can handle a CC. nfc_llcp_recv_cc() then moves sk to local->sockets and sets LLCP_CONNECTED:
nfc_llcp_sock_unlink(&local->connecting_sockets, sk); nfc_llcp_sock_link(&local->sockets, sk);
Before the waiter takes the lock again, the worker can also handle a DISC or a default-reason DM for the same socket. nfc_llcp_recv_agf() handles aggregated sub-PDUs one after another in a single worker run, so one AGF frame carrying CC plus DISC would be enough.
After a DM, sock_wait_state() returns -ENXIO. After a DISC, sk_err is not set, so the waiter sleeps until a signal arrives and returns -ERESTARTSYS or -EINTR. In both cases connect() then runs:
if (ret && ret != -EINPROGRESS) goto sock_unlink; ... sock_unlink: nfc_llcp_sock_unlink(&local->connecting_sockets, sk);
This takes connecting_sockets.lock, but sk_del_node_init() removes the node from local->sockets. Isn't local->sockets then being modified without local->sockets.lock, racing with its other writers and readers?
In the DISC case, nfc_llcp_recv_disc() has already called nfc_put_device(local->dev) for the connected state. The error path then falls through to:
put_dev:
nfc_put_device(dev);
Could this underflow the nfc_dev refcount and eventually free the nfc_dev while it is still in use?
The same path also frees service_name, puts the ssap, and clears llcp_sock->local and llcp_sock->dev on a socket the peer had already connected.
Should this error path use sk_state, checked under lock_sock(), to decide which list to unlink from and whether to drop the device reference?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120075908.4064155-1-bsdhenrymartin%40gmail.com