NFC on Linux
 help / color / mirror / Atom feed
From: Bin Luo via B4 Relay <devnull+luob87709.gmail.com@kernel.org>
To: David Heidelberg <david@ixit.cz>,
	 "David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	 Jakub Kicinski <kuba@kernel.org>,
	Paolo Abeni <pabeni@redhat.com>,  Simon Horman <horms@kernel.org>
Cc: oe-linux-nfc@lists.linux.dev, netdev@vger.kernel.org,
	 linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: [PATCH net v2 1/2] nfc: rawsock: serialize SEND_SHUTDOWN and write queue teardown
Date: Fri, 04 Dec 2026 14:06:52 +0800	[thread overview]
Message-ID: <20261204060651.488-2-luob87709@gmail.com> (raw)
In-Reply-To: <20261204060651.488-1-luob87709@gmail.com>

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




  reply	other threads:[~2026-12-04  6:06 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2026-12-04  6:06 ` [PATCH net v2 2/2] nfc: rawsock: move nfc_deactivate_target() to rawsock_release() Bin Luo via B4 Relay

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=20261204060651.488-2-luob87709@gmail.com \
    --to=devnull+luob87709.gmail.com@kernel.org \
    --cc=davem@davemloft.net \
    --cc=david@ixit.cz \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luob87709@gmail.com \
    --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