Skip to contentSkip to conversation

nfc: llcp: fix double put of nfc_dev on interrupted connect

PATCH New Henry Martin 2 participants 2 messages Nov 20 – Nov 24

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nfc: llcp: fix double put of nfc_dev on interrupted connect

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")
---
 net/nfc/llcp_sock.c | 50 ++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 49 insertions(+), 1 deletion(-)
net/nfc/llcp_sock.c +49 -1
index 1e5ee4bcde68..22b2f65e984a 100644
@@ -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;
Signature · 1 line
2.43.7
#1 lore raw
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider.

Critical: 0 · High: 3 · Medium: 2 · 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] The fix is incomplete.
- [High] The patch adds an nfc_dev refcount leak.
- [High] The new early return in llcp_sock_connect() (socket on local->sockets, state not CONNECTED, ret = -ERESTARTSYS/-EINTR/-ENXIO)…
- [Medium] The commit message gets the failure mechanism wrong and makes false claims about ownership.
- [Medium] The ownership heuristic is also fooled by bind() followed by connect().
- [Low] The comment added in llcp_sock_connect() says the 'dev/local references now belong to the socket.

--

nfc: llcp: fix double put of nfc_dev on interrupted connect

This patch changes the interrupted-wait error path in llcp_sock_connect(). When the socket is found linked on local->sockets, the nfc_dev and local references taken by connect() are no longer unwound. A new helper, llcp_sock_on_local_sockets(), treats list membership as meaning that the CC handler has claimed the socket.

Quoted 5 lines

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().

[Severity: Medium]
Is this description of the failure complete? A CC frame on its own does not seem able to make sock_wait_state() return an error. nfc_llcp_recv_cc() sets LLCP_CONNECTED under lock_sock(). After schedule_timeout(), the loop takes the lock again and checks the state before it reaches signal_pending():

net/nfc/llcp_sock.c:sock_wait_state() {
	while (sk->sk_state != state) {
		...
		if (signal_pending(current)) {
			err = sock_intr_errno(timeo);
			break;
		}

		release_sock(sk);
		timeo = schedule_timeout(timeo);
		lock_sock(sk);
		...
	}
}

So the double put seems to need a CC followed by something that drops the CONNECTED-state dev reference and takes the socket out of LLCP_CONNECTED, such as nfc_llcp_recv_disc() or nfc_llcp_socket_release().

The "dropped exactly once" list also leaves out two functions:

nfc_llcp_socket_release() calls nfc_put_device() for CONNECTED sockets.

nfc_llcp_recv_dm() closes a claimed socket without dropping the reference.

Quoted 3 lines

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.

Is "the only path" accurate? llcp_sock_bind() also links sk into local->sockets and sets LLCP_BOUND, and llcp_sock_connect() does not reject LLCP_BOUND.

The commit message also doesn't explain the new "if (sk->sk_state == LLCP_CONNECTED) ret = 0;" branch. sock_wait_state() only returns an error while CONNECTED if sk_err was already set. Which case is that branch meant to handle?

Quoted 7 lines
net/nfc/llcp_sock.c +0 -0
index 1e5ee4bcde684..22b2f65e984a4 100644
@@ -683,6 +683,36 @@ static int llcp_sock_release(struct socket *sock)
 	return err;
 }

net/nfc/llcp_sock.c static int llcp_sock_release(struct socket *sock) · line 694

Quoted 10 lines
net/nfc/llcp_sock.c +9 -0
 
+/**
+ * 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().

[Severity: Medium]
Can bind() followed by connect() make this helper return true when no CC has arrived?

llcp_sock_bind():

	nfc_llcp_sock_link(&local->sockets, sk);
	...
	sk->sk_state = LLCP_BOUND;

llcp_sock_connect() only rejects LLCP_CONNECTED and LLCP_CONNECTING, so a bound socket goes on to:

	nfc_llcp_sock_link(&local->connecting_sockets, sk);

This links the same sk_node a second time. That hlist double link is older than this patch. But because local->sockets still reaches sk_node, llcp_sock_on_local_sockets() finds sk.

If a signal interrupts sock_wait_state() before any CC arrives, the new code takes goto out. It returns -ERESTARTSYS or -EINTR with the socket still in LLCP_CONNECTING.

The nfc_dev reference taken by connect() is then never dropped, because llcp_sock_destruct() only puts it for LLCP_CONNECTED. connect() also overwrote llcp_sock->local, so the local reference taken by bind() leaks. Before this patch, the error path unwound both references taken by connect().

Quoted 3 lines
net/nfc/llcp_sock.c +3 -0
+ *
+ * Return: true if @sk is linked in local->sockets.
+ */

[ ... ]

Quoted 6 lines
net/nfc/llcp_sock.c +1 -1
@@ -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) {

net/nfc/llcp_sock.c static int llcp_sock_connect(struct socket *sock, struct sockaddr_unsized *_addr · line 824

Quoted 10 lines
net/nfc/llcp_sock.c +10 -0
+		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.
+		 */

[Severity: Low]
Does this comment correctly say who releases these references? nfc_llcp_recv_disc() only drops the dev reference, and never the local kref:

	if (sk->sk_state == LLCP_CONNECTED) {
		nfc_put_device(local->dev);
		...

The local reference is only released through llcp_sock_destruct()->nfc_llcp_sock_free()->nfc_llcp_local_put(). llcp_sock_destruct() drops the dev reference only when sk_state is LLCP_CONNECTED.

nfc_llcp_socket_release() also drops the dev reference for CONNECTED sockets, and the comment doesn't mention it.

net/nfc/llcp_sock.c static int llcp_sock_connect(struct socket *sock, struct sockaddr_unsized *_addr · line 829

Quoted 5 lines
net/nfc/llcp_sock.c +5 -0
+		if (llcp_sock_on_local_sockets(local, sk)) {
+			if (sk->sk_state == LLCP_CONNECTED)
+				ret = 0;
+			goto out;
+		}

[Severity: High]
Does this leak the nfc_dev reference if the peer sends a CC and then a DM?

Say rx_work handles a CC and then a DM whose reason is not LLCP_DM_NOBOUND or LLCP_DM_REJ, before the connecting thread gets lock_sock() back. nfc_llcp_recv_dm() takes the default branch and finds the socket on local->sockets with nfc_llcp_sock_get(). It then does:

	sk->sk_err = ENXIO;
	sk->sk_state = LLCP_CLOSED;
	sk->sk_state_change(sk);

It doesn't call nfc_put_device(), and it leaves sk on local->sockets.

sock_wait_state() then returns -ENXIO from sock_error(). llcp_sock_on_local_sockets() returns true and the state isn't CONNECTED, so we goto out and skip put_dev. Later, llcp_sock_destruct() skips the put as well:

	if (sk->sk_state == LLCP_CONNECTED)
		nfc_put_device(llcp_sock->dev);

Before this patch, this sequence went to sock_unlink and the reference was dropped once. Does the nfc_dev now stay pinned for good?

[Severity: High]
What state is the socket in when this path returns an error with sk_state not CONNECTED?

The socket stays linked on local->sockets in LLCP_CLOSED. It still holds llcp_sock->local, the reserved ssap and the kmemdup'd service_name.

For example, take a CC followed by a DISC with non-zero SAPs. nfc_llcp_recv_disc() puts dev and sets LLCP_CLOSED, but leaves sk on local->sockets. A signal then makes sock_wait_state() return sock_intr_errno(timeo). With the default sk_sndtimeo of MAX_SCHEDULE_TIMEOUT, that is -ERESTARTSYS.

Now suppose the syscall is restarted or userspace retries. The entry checks in llcp_sock_connect() only reject LLCP_CONNECTED and LLCP_CONNECTING, so the second call goes ahead. It overwrites llcp_sock->local, dev, ssap, reserved_ssap and service_name, which leaks the old local reference, the ssap reservation and the service_name buffer. It then does:

	nfc_llcp_sock_link(&local->connecting_sockets, sk);

sk_add_node() calls hlist_add_head() on sk_node without any check, and sk_node is still linked in local->sockets. Can this corrupt both local->sockets and connecting_sockets, and lead to a use-after-free on later list walks?

Before this patch, this error path went through sock_unlink, so a restarted connect() started from a clean socket. A similar double link already exists if userspace reconnects after a normal connect plus DISC. Here, though, a remote peer and an ordinary signal create the state, and the kernel's own syscall restart triggers the corruption.

net/nfc/llcp_sock.c static int llcp_sock_connect(struct socket *sock, struct sockaddr_unsized *_addr · line 832

Quoted 3 lines
net/nfc/llcp_sock.c +2 -0
+
 		goto sock_unlink;
+	}

[Severity: High]
Can the double put still happen through nfc_llcp_socket_release()?

That function takes sockets off local->sockets with nfc_llcp_sock_list_pop(), which only holds the list write lock, not lock_sock():

nfc_llcp_sock_list_pop()

	write_lock(&l->lock);
	sk = sk_head(&l->head);
	if (sk) {
		sock_hold(sk);
		sk_del_node_init(sk);
	}

Then, for each socket it pops:

	if (sk->sk_state == LLCP_CONNECTED)
		nfc_put_device(llcp_sock->dev);
	...
	if (err)
		sk->sk_err = err;
	sk->sk_state = LLCP_CLOSED;

So holding lock_sock() in llcp_sock_connect() doesn't keep list membership stable. That also contradicts "The caller must hold lock_sock()" in the new kernel-doc.

There are two ways to get here while connect() sleeps after the CC was handled:

rx_work: nfc_llcp_recv_disc() with dsap == 0 and ssap == 0
nfc_dep_link_down()
nfc_llcp_mac_is_down()
nfc_llcp_socket_release(local, true, 0)

nfc_llcp_unregister_device()
local_cleanup()
nfc_llcp_socket_release(local, false, ENXIO)

Either way, the socket's dev reference is dropped and sk is no longer on any list. In the unregister case, sock_error() returns -ENXIO and no signal is needed. In the DISC(0,0) case, the state is CLOSED, so the wait continues until a signal returns -ERESTARTSYS or -EINTR.

llcp_sock_on_local_sockets() then returns false, we goto sock_unlink, and put_dev calls nfc_put_device(dev) a second time. Isn't that the same kref underflow and nfc_dev use-after-free that the commit message describes?

Would tracking ownership of the references explicitly work better here than inferring it from list membership?

Quoted 5 lines
net/nfc/llcp_sock.c +1 -0
+out:
 	release_sock(sk);
 
 	return ret;
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120081354.4098856-1-bsdhenrymartin%40gmail.com