Netdev List
 help / color / mirror / Atom feed
* [PATCH net] nfc: llcp: fix double put of nfc_dev on interrupted connect
@ 2026-11-20  8:13 Henry Martin
  0 siblings, 0 replies; only message in thread
From: Henry Martin @ 2026-11-20  8:13 UTC (permalink / raw)
  To: david, davem, edumazet, kuba, pabeni, horms
  Cc: oe-linux-nfc, netdev, Henry Martin, stable

llcp_sock_connect() takes an nfc_dev reference with nfc_get_device()
and always drops it on the error path when sock_wait_state() is
interrupted.

While the socket waits for the peer, nfc_llcp_recv_cc() can already
have claimed it: the CC handler moves the socket from
connecting_sockets to local->sockets and the reference now belongs to
the socket. It is dropped exactly once, by nfc_llcp_recv_disc() or by
llcp_sock_destruct().

If the interrupted error path still unwinds that reference, the
nfc_dev kref is put a second time. Once the refcount underflows, the
device is released while it is still registered and in use:

e.g. on v7.4-rc3 (KASAN build):

  KASAN: slab-use-after-free in klist_next
  Read of size 8
  CPU: 2 UID: 1000
  Read: klist_next <- class_find_device <- nfc_get_device <-
        llcp_sock_connect <- __sys_connect
  Allocated by nfc_allocate_device <- nfcsim_device_new
  Freed by kfree <- device_release <- kobject_put <-
       llcp_sock_connect <- __sys_connect

The freed device is later used by nfc_llcp_tx_work(), which trips a
KASAN use-after-free write in mutex_lock().

Check whether the CC handler already claimed the socket - it is the
only path that links a connecting socket into local->sockets - and
leave the references with the socket in that case.

This issue was discovered by Tencent CodeBuddy Security.

Fixes: ff353d86a92e ("NFC: LLCP connect must wait for a CC frame")
Cc: stable@vger.kernel.org
Signed-off-by: Henry Martin <bsdhenrymartin@gmail.com>
---
 net/nfc/llcp_sock.c | 50 ++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 49 insertions(+), 1 deletion(-)

diff --git a/net/nfc/llcp_sock.c b/net/nfc/llcp_sock.c
index 1e5ee4bcde68..22b2f65e984a 100644
--- a/net/nfc/llcp_sock.c
+++ b/net/nfc/llcp_sock.c
@@ -683,6 +683,36 @@ static int llcp_sock_release(struct socket *sock)
 	return err;
 }
 
+/**
+ * llcp_sock_on_local_sockets() - check if a socket was claimed by a CC
+ * @local: the LLCP local the socket connected on
+ * @sk: the socket to look up
+ *
+ * nfc_llcp_recv_cc() moves a connecting socket to local->sockets when the
+ * peer answers with a CC frame.  Only that path links a connecting socket
+ * into local->sockets, so membership doubles as "the peer claimed the
+ * socket".  The caller must hold lock_sock().
+ *
+ * Return: true if @sk is linked in local->sockets.
+ */
+static bool llcp_sock_on_local_sockets(struct nfc_llcp_local *local,
+				       struct sock *sk)
+{
+	struct sock *s;
+	bool found = false;
+
+	read_lock(&local->sockets.lock);
+	sk_for_each(s, &local->sockets.head) {
+		if (s == sk) {
+			found = true;
+			break;
+		}
+	}
+	read_unlock(&local->sockets.lock);
+
+	return found;
+}
+
 static int llcp_sock_connect(struct socket *sock, struct sockaddr_unsized *_addr,
 			     int len, int flags)
 {
@@ -781,9 +811,27 @@ static int llcp_sock_connect(struct socket *sock, struct sockaddr_unsized *_addr
 
 	ret = sock_wait_state(sk, LLCP_CONNECTED,
 			      sock_sndtimeo(sk, flags & O_NONBLOCK));
-	if (ret && ret != -EINPROGRESS)
+	if (ret) {
+		if (ret == -EINPROGRESS)
+			goto out;
+
+		/* If the CC frame was processed while we slept, recv_cc()
+		 * moved the socket to local->sockets and the dev/local
+		 * references now belong to the socket.  They are dropped by
+		 * nfc_llcp_recv_disc() or llcp_sock_destruct(); unwinding
+		 * them on this error path would put the nfc_dev reference
+		 * obtained above a second time.
+		 */
+		if (llcp_sock_on_local_sockets(local, sk)) {
+			if (sk->sk_state == LLCP_CONNECTED)
+				ret = 0;
+			goto out;
+		}
+
 		goto sock_unlink;
+	}
 
+out:
 	release_sock(sk);
 
 	return ret;
-- 
2.43.7


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-20  8:14 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20  8:13 [PATCH net] nfc: llcp: fix double put of nfc_dev on interrupted connect Henry Martin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox