NFC on Linux
 help / color / mirror / Atom feed
* [PATCH net v2 0/2] nfc: rawsock: fix write-queue race and sk_destruct deadlock
@ 2026-12-04  6:06 Bin Luo via B4 Relay
  2026-12-04  6:06 ` [PATCH net v2 1/2] nfc: rawsock: serialize SEND_SHUTDOWN and write queue teardown Bin Luo via B4 Relay
  2026-12-04  6:06 ` [PATCH net v2 2/2] nfc: rawsock: move nfc_deactivate_target() to rawsock_release() Bin Luo via B4 Relay
  0 siblings, 2 replies; 3+ messages in thread
From: Bin Luo via B4 Relay @ 2026-12-04  6:06 UTC (permalink / raw)
  To: David Heidelberg, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Simon Horman
  Cc: oe-linux-nfc, netdev, linux-kernel

This series fixes two teardown issues in net/nfc/rawsock.c:

1. Patch 1/2 serializes SEND_SHUTDOWN updates/checks, sk_write_queue
   purging, and tx_work scheduling under sk->sk_write_queue.lock,
   closing the residual window left after commit d793458c45df ("nfc:
   rawsock: cancel tx_work before socket teardown") where an in-flight
   completion could re-queue tx_work during release, and guarding
   against NULL skb dereference in rawsock_tx_work().

2. Patch 2/2 moves nfc_deactivate_target() out of rawsock_destruct()
   into rawsock_release() (process context), preventing a 30-second
   self-deadlock on ndev->rx_wq when rawsock_data_exchange_complete()
   drops the final socket reference from nci_rx_work().

Changes in v2:
- Resend via b4 web relay (v1 had broken diff lines from Gmail wrapping)
  and send both dependent rawsock patches together as a threaded series.

Both patches were regression-tested in QEMU KVM with KASAN, LOCKDEP,
and DEBUG_OBJECTS enabled using virtual_ncidev across 3 x 200
iterations with zero timeouts or warnings, and passed
tools/testing/selftests/nci/nci_dev (8/8).

Bin Luo (University of Electronic Science and Technology of China) (2):
  nfc: rawsock: serialize SEND_SHUTDOWN and write queue teardown
  nfc: rawsock: move nfc_deactivate_target() to rawsock_release()

 net/nfc/rawsock.c | 54 +++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 35 insertions(+), 19 deletions(-)
-- 
2.43.0



^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH net v2 1/2] nfc: rawsock: serialize SEND_SHUTDOWN and write queue teardown
  2026-12-04  6:06 [PATCH net v2 0/2] nfc: rawsock: fix write-queue race and sk_destruct deadlock Bin Luo via B4 Relay
@ 2026-12-04  6:06 ` Bin Luo via B4 Relay
  2026-12-04  6:06 ` [PATCH net v2 2/2] nfc: rawsock: move nfc_deactivate_target() to rawsock_release() Bin Luo via B4 Relay
  1 sibling, 0 replies; 3+ messages in thread
From: Bin Luo via B4 Relay @ 2026-12-04  6:06 UTC (permalink / raw)
  To: David Heidelberg, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Simon Horman
  Cc: oe-linux-nfc, netdev, linux-kernel, stable

From: "Bin Luo (University of Electronic Science and Technology of China)" <luob87709@gmail.com>

Commit d793458c45df ("nfc: rawsock: cancel tx_work before socket
teardown") set SEND_SHUTDOWN and called cancel_work_sync(&tx_work)
before rawsock_write_queue_purge() in rawsock_release().

However, two races remain on tx_work and sk_write_queue:

1. rawsock_release() sets SEND_SHUTDOWN without holding
   sk_write_queue.lock and purges sk_write_queue only after
   cancel_work_sync().  Meanwhile, rawsock_data_exchange_complete()
   checks !skb_queue_empty(&sk->sk_write_queue) without checking
   SEND_SHUTDOWN and schedules tx_work without holding a socket
   reference.  If an in-flight completion runs between
   cancel_work_sync() and rawsock_write_queue_purge(), it reschedules
   tx_work while rawsock_release() proceeds to drop the final socket
   reference, leaving tx_work queued on a freed sock.

2. rawsock_tx_work() checks SEND_SHUTDOWN without sk_write_queue.lock
   and then passes skb = skb_dequeue(&sk->sk_write_queue) directly to
   skb_get_kcov_handle(skb) without checking for NULL.  If
   rawsock_report_error() or rawsock_release() purges sk_write_queue in
   between, skb is NULL and skb_get_kcov_handle() dereferences NULL.

Serialize SEND_SHUTDOWN updates/checks, write-queue purging, and
tx_work scheduling under sk->sk_write_queue.lock:

- In rawsock_release(), set SEND_SHUTDOWN and purge sk_write_queue
  under sk_write_queue.lock before calling cancel_work_sync().
- In rawsock_data_exchange_complete() and rawsock_sendmsg(), refuse to
  enqueue or schedule tx_work once SEND_SHUTDOWN is set.
- In rawsock_tx_work(), check SEND_SHUTDOWN and dequeue skb under
  sk_write_queue.lock, returning cleanly if the queue is empty.
- In rawsock_report_error(), update sk_shutdown and purge
  sk_write_queue under sk_write_queue.lock.

Discovered by static analysis and AI-assisted code review.  Confirmed by
inspecting the window between cancel_work_sync() and
rawsock_write_queue_purge() via ftrace without modifying the kernel;
not triggered standalone without delay injection, and regression-tested
in QEMU KVM (KASAN, LOCKDEP, DEBUG_OBJECTS) against virtual_ncidev
teardown loops and tools/testing/selftests/nci/nci_dev (8/8 passed).

Fixes: d793458c45df ("nfc: rawsock: cancel tx_work before socket teardown")
Fixes: 7e8cdc97148c ("nfc: Add KCOV annotations")
Cc: stable@vger.kernel.org
Signed-off-by: Bin Luo (University of Electronic Science and Technology of China) <luob87709@gmail.com>
---
Changes in v2:
- Resend cleanly via b4 web relay (v1 patch was line-wrapped by Gmail)
  and include both dependent rawsock patches as a threaded series.

 net/nfc/rawsock.c | 44 ++++++++++++++++++++++++++++++--------------
 1 file changed, 30 insertions(+), 14 deletions(-)

diff --git a/net/nfc/rawsock.c b/net/nfc/rawsock.c
index f7d7a59..291f509 100644
--- a/net/nfc/rawsock.c
+++ b/net/nfc/rawsock.c
@@ -34,25 +34,25 @@ static void nfc_sock_unlink(struct nfc_sock_list *l, struct sock *sk)
 	write_unlock(&l->lock);
 }
 
-static void rawsock_write_queue_purge(struct sock *sk)
+static void __rawsock_write_queue_purge(struct sock *sk)
 {
 	pr_debug("sk=%p\n", sk);
 
-	spin_lock_bh(&sk->sk_write_queue.lock);
 	__skb_queue_purge(&sk->sk_write_queue);
 	nfc_rawsock(sk)->tx_work_scheduled = false;
-	spin_unlock_bh(&sk->sk_write_queue.lock);
 }
 
 static void rawsock_report_error(struct sock *sk, int err)
 {
 	pr_debug("sk=%p err=%d\n", sk, err);
 
+	spin_lock_bh(&sk->sk_write_queue.lock);
 	sk->sk_shutdown = SHUTDOWN_MASK;
+	__rawsock_write_queue_purge(sk);
+	spin_unlock_bh(&sk->sk_write_queue.lock);
+
 	sk->sk_err = -err;
 	sk_error_report(sk);
-
-	rawsock_write_queue_purge(sk);
 }
 
 static int rawsock_release(struct socket *sock)
@@ -73,9 +73,11 @@ static int rawsock_release(struct socket *sock)
 		 * before the socket is orphaned to avoid a race where
 		 * rawsock_tx_work runs after the NCI device has been freed.
 		 */
+		spin_lock_bh(&sk->sk_write_queue.lock);
 		sk->sk_shutdown |= SEND_SHUTDOWN;
+		__rawsock_write_queue_purge(sk);
+		spin_unlock_bh(&sk->sk_write_queue.lock);
 		cancel_work_sync(&nfc_rawsock(sk)->tx_work);
-		rawsock_write_queue_purge(sk);
 	}
 
 	sock_orphan(sk);
@@ -168,7 +170,8 @@ static void rawsock_data_exchange_complete(void *context, struct sk_buff *skb,
 		goto error_skb;
 
 	spin_lock_bh(&sk->sk_write_queue.lock);
-	if (!skb_queue_empty(&sk->sk_write_queue))
+	if (!(sk->sk_shutdown & SEND_SHUTDOWN) &&
+	    !skb_queue_empty(&sk->sk_write_queue))
 		schedule_work(&nfc_rawsock(sk)->tx_work);
 	else
 		nfc_rawsock(sk)->tx_work_scheduled = false;
@@ -195,12 +198,20 @@ static void rawsock_tx_work(struct work_struct *work)
 
 	pr_debug("sk=%p target_idx=%u\n", sk, target_idx);
 
+	spin_lock_bh(&sk->sk_write_queue.lock);
 	if (sk->sk_shutdown & SEND_SHUTDOWN) {
-		rawsock_write_queue_purge(sk);
+		__rawsock_write_queue_purge(sk);
+		spin_unlock_bh(&sk->sk_write_queue.lock);
 		return;
 	}
 
-	skb = skb_dequeue(&sk->sk_write_queue);
+	skb = __skb_dequeue(&sk->sk_write_queue);
+	if (!skb) {
+		nfc_rawsock(sk)->tx_work_scheduled = false;
+		spin_unlock_bh(&sk->sk_write_queue.lock);
+		return;
+	}
+	spin_unlock_bh(&sk->sk_write_queue.lock);
 	kcov_remote_start_common(skb_get_kcov_handle(skb));
 
 	sock_hold(sk);
@@ -239,14 +250,19 @@ static int rawsock_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
 	}
 
 	spin_lock_bh(&sk->sk_write_queue.lock);
-	__skb_queue_tail(&sk->sk_write_queue, skb);
-	if (!nfc_rawsock(sk)->tx_work_scheduled) {
-		schedule_work(&nfc_rawsock(sk)->tx_work);
-		nfc_rawsock(sk)->tx_work_scheduled = true;
+	if (sk->sk_shutdown & SEND_SHUTDOWN) {
+		rc = -EPIPE;
+		kfree_skb(skb);
+	} else {
+		__skb_queue_tail(&sk->sk_write_queue, skb);
+		if (!nfc_rawsock(sk)->tx_work_scheduled) {
+			schedule_work(&nfc_rawsock(sk)->tx_work);
+			nfc_rawsock(sk)->tx_work_scheduled = true;
+		}
 	}
 	spin_unlock_bh(&sk->sk_write_queue.lock);
 
-	return len;
+	return rc ? : len;
 }
 
 static int rawsock_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
-- 
2.43.0




^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH net v2 2/2] nfc: rawsock: move nfc_deactivate_target() to rawsock_release()
  2026-12-04  6:06 [PATCH net v2 0/2] nfc: rawsock: fix write-queue race and sk_destruct deadlock Bin Luo via B4 Relay
  2026-12-04  6:06 ` [PATCH net v2 1/2] nfc: rawsock: serialize SEND_SHUTDOWN and write queue teardown Bin Luo via B4 Relay
@ 2026-12-04  6:06 ` Bin Luo via B4 Relay
  1 sibling, 0 replies; 3+ messages in thread
From: Bin Luo via B4 Relay @ 2026-12-04  6:06 UTC (permalink / raw)
  To: David Heidelberg, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Simon Horman
  Cc: oe-linux-nfc, netdev, linux-kernel, stable

From: "Bin Luo (University of Electronic Science and Technology of China)" <luob87709@gmail.com>

rawsock_destruct() currently calls nfc_deactivate_target(), which
acquires device_lock(&dev->dev) and, for NCI devices, invokes
nci_request(ndev, nci_rf_deactivate_req, ..., NCI_RF_DEACTIVATE_TIMEOUT)
to synchronously wait up to 30 seconds for an RF_DEACTIVATE_RSP/NTF
exchange processed on ndev->rx_wq.

When rawsock_release() drops its reference while an nfc_data_exchange()
is still in flight, rawsock_data_exchange_complete() drops the final
socket reference directly from ndev->rx_wq (inside nci_rx_work()).
That invokes rawsock_destruct() -> nfc_deactivate_target() ->
nci_request() on ndev->rx_wq itself, causing ndev->rx_wq to block for
the full 30-second NCI_RF_DEACTIVATE_TIMEOUT waiting on a response that
only ndev->rx_wq can process:

  nci: __nci_request: wait_for_completion_interruptible_timeout failed 0

Move nfc_deactivate_target() into rawsock_release() (process context,
after cancelling tx_work) so deactivation never runs inside sk_destruct
from an asynchronous completion context.  Keep nfc_put_device() in
rawsock_destruct() so the nfc_dev reference stays valid until any
in-flight completion drops its socket reference.

Discovered by AI-assisted code review and reproduced in QEMU KVM on an
unmodified kernel using /dev/virtual_nci without delay injection (an
AF_NFC SOCK_SEQPACKET socket sends one frame and closes concurrently
with the NCI data response).  Tested against the reproducer across
3 x 200 iterations in QEMU KVM (KASAN, LOCKDEP, DEBUG_OBJECTS) with zero
timeouts, and against tools/testing/selftests/nci/nci_dev (8/8 passed).

Fixes: 23b7869c0fd0 ("NFC: add the NFC socket raw protocol")
Cc: stable@vger.kernel.org
Signed-off-by: Bin Luo (University of Electronic Science and Technology of China) <luob87709@gmail.com>
---
Changes in v2:
- Resend cleanly via b4 web relay (v1 patch was line-wrapped by Gmail)
  and include both dependent rawsock patches as a threaded series.

 net/nfc/rawsock.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/net/nfc/rawsock.c b/net/nfc/rawsock.c
index 291f509..e2a9a2b 100644
--- a/net/nfc/rawsock.c
+++ b/net/nfc/rawsock.c
@@ -78,6 +78,10 @@ static int rawsock_release(struct socket *sock)
 		__rawsock_write_queue_purge(sk);
 		spin_unlock_bh(&sk->sk_write_queue.lock);
 		cancel_work_sync(&nfc_rawsock(sk)->tx_work);
+
+		nfc_deactivate_target(nfc_rawsock(sk)->dev,
+				      nfc_rawsock(sk)->target_idx,
+				      NFC_TARGET_MODE_IDLE);
 	}
 
 	sock_orphan(sk);
@@ -332,12 +336,8 @@ static void rawsock_destruct(struct sock *sk)
 {
 	pr_debug("sk=%p\n", sk);
 
-	if (sk->sk_state == TCP_ESTABLISHED) {
-		nfc_deactivate_target(nfc_rawsock(sk)->dev,
-				      nfc_rawsock(sk)->target_idx,
-				      NFC_TARGET_MODE_IDLE);
+	if (sk->sk_state == TCP_ESTABLISHED)
 		nfc_put_device(nfc_rawsock(sk)->dev);
-	}
 
 	skb_queue_purge(&sk->sk_receive_queue);
 
-- 
2.43.0




^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-04  6:06 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04  6:06 [PATCH net v2 0/2] nfc: rawsock: fix write-queue race and sk_destruct deadlock Bin Luo via B4 Relay
2026-12-04  6:06 ` [PATCH net v2 1/2] nfc: rawsock: serialize SEND_SHUTDOWN and write queue teardown Bin Luo via B4 Relay
2026-12-04  6:06 ` [PATCH net v2 2/2] nfc: rawsock: move nfc_deactivate_target() to rawsock_release() Bin Luo via B4 Relay

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