From: Henry Martin <bsdhenrymartin@gmail.com>
To: david@ixit.cz, davem@davemloft.net, edumazet@kernel.org,
kuba@kernel.org, pabeni@redhat.com, horms@kernel.org
Cc: oe-linux-nfc@lists.linux.dev, netdev@vger.kernel.org,
Henry Martin <bsdhenrymartin@gmail.com>,
stable@vger.kernel.org
Subject: [PATCH net] nfc: llcp: fix double put of nfc_dev on interrupted connect
Date: Fri, 20 Nov 2026 16:13:54 +0800 [thread overview]
Message-ID: <20261120081354.4098856-1-bsdhenrymartin@gmail.com> (raw)
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
reply other threads:[~2026-11-20 8:14 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261120081354.4098856-1-bsdhenrymartin@gmail.com \
--to=bsdhenrymartin@gmail.com \
--cc=davem@davemloft.net \
--cc=david@ixit.cz \
--cc=edumazet@kernel.org \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=oe-linux-nfc@lists.linux.dev \
--cc=pabeni@redhat.com \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).