Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH net v2 1/2] nfc: rawsock: serialize SEND_SHUTDOWN and write queue teardown
       [not found] <20261204060651.488-1-luob87709@gmail.com>
@ 2026-12-04  6:06 ` Bin Luo via B4 Relay
  2026-12-05  6:44   ` netdev-bot+sashiko
  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, 1 reply; 4+ 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] 4+ messages in thread

* [PATCH net v2 2/2] nfc: rawsock: move nfc_deactivate_target() to rawsock_release()
       [not found] <20261204060651.488-1-luob87709@gmail.com>
  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
  2026-12-05  6:44   ` netdev-bot+sashiko
  1 sibling, 1 reply; 4+ 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] 4+ messages in thread

* Re: [PATCH net v2 1/2] nfc: rawsock: serialize SEND_SHUTDOWN and write queue teardown
  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-05  6:44   ` netdev-bot+sashiko
  0 siblings, 0 replies; 4+ messages in thread
From: netdev-bot+sashiko @ 2026-12-05  6:44 UTC (permalink / raw)
  To: luob87709
  Cc: david, davem, edumazet, kuba, pabeni, horms, oe-linux-nfc, netdev,
	linux-kernel, stable

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

Critical: 0 · High: 0 (1) · Medium: 1 · Low: 2

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

- [Medium] nfc: rawsock: rawsock_report_error() wakes waiters before
  setting sk_err
- [Low] The commit message names a trigger for race #2 that the pre-patch
  code cannot hit.
- [Low] nfc: rawsock: wrong Fixes trailer citing KCOV commit instead of
  23b7869c0fd0

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the patch
  edits rawsock_sendmsg() and leaves it in place.

--

nfc: rawsock: serialize SEND_SHUTDOWN and write queue teardown

This patch puts SEND_SHUTDOWN updates and checks, write queue purging,
and tx_work scheduling in net/nfc/rawsock.c under sk->sk_write_queue.lock.
rawsock_tx_work() now handles an empty write queue without dereferencing
a NULL skb. rawsock_sendmsg() now rejects new data with -EPIPE once
SEND_SHUTDOWN is set.

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

[Severity: Low]
Is the rawsock_release() part of this description accurate for the code
before this patch?

Before this patch, rawsock_release() did:

	sk->sk_shutdown |= SEND_SHUTDOWN;
	cancel_work_sync(&nfc_rawsock(sk)->tx_work);
	rawsock_write_queue_purge(sk);

cancel_work_sync() waits for a running tx_work and removes a pending one,
and both happen before the purge. So the purge in rawsock_release() cannot
land between the SEND_SHUTDOWN check and the skb_dequeue() of a running
rawsock_tx_work().

The only way tx_work can run alongside release is the race 1 reschedule
after cancel_work_sync(). In that case the SEND_SHUTDOWN store is ordered
before the requeue: cancel clears PENDING with full barriers, and
queue_work() then does a fully ordered test_and_set_bit. That means
rawsock_tx_work() takes the shutdown/purge branch and never reaches the
dequeue.

The ftrace evidence in the changelog covers only race 1, but race 2 has
its own Fixes: 7e8cdc97148c tag and is headed for stable. Could the
description of race 2 drop the rawsock_release() trigger?

> diff --git a/net/nfc/rawsock.c b/net/nfc/rawsock.c
> index f7d7a599fade7..291f509f07caf 100644
> --- a/net/nfc/rawsock.c
> +++ b/net/nfc/rawsock.c

[ ... ]

> @@ -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;
>  }

[Severity: Medium]
Does moving the purge ahead of the sk_err assignment change what
userspace sees when the device reports an error?

Before this patch, rawsock_report_error() wrote sk->sk_err and called
sk_error_report() before rawsock_write_queue_purge(). Now it sets
sk->sk_shutdown and calls __rawsock_write_queue_purge() first. It only
writes sk->sk_err after dropping sk_write_queue.lock.

The skbs on sk_write_queue come from nfc_alloc_send_skb(), so they
are charged to the socket with sock_wfree() as their destructor.
Freeing them in __skb_queue_purge() runs sock_def_write_space(), which
wakes anything sleeping on the socket once it is writeable again. That
wakeup now happens before sk_err is set:

CPU1 (report_error)                   CPU2 (sendmsg)
                                      sock_alloc_send_pskb()
                                        sock_wait_for_wmem()
sk->sk_shutdown = SHUTDOWN_MASK
__rawsock_write_queue_purge()
  sock_wfree()
    sock_def_write_space() /* wake */
                                        sock_error(sk) == 0
                                        SEND_SHUTDOWN is set
                                        return -EPIPE
sk->sk_err = -err

The sender gets -EPIPE instead of the NFC error. The real error stays
in sk_err and is returned by some later call. A poller woken the same
way can see EPOLLHUP without EPOLLERR in datagram_poll(). A reader in
__skb_wait_for_more_packets() can see RCV_SHUTDOWN with no error and
return 0 instead of the error.

Could sk->sk_err be written, with WRITE_ONCE(), before taking
sk_write_queue.lock, with sk_error_report() still called after the
unlock? That keeps the write queue serialization this patch adds, and
the error would be visible before the shutdown and the wakeups.

[Severity: Low]
The second Fixes: tag points at the KCOV annotation commit, but is
that where the race 2 problem was introduced?

As far as I can tell, rawsock_tx_work() has done the unlocked
SEND_SHUTDOWN check followed by an unchecked skb_dequeue() since it
was added in 23b7869c0fd0 ("NFC: add the NFC socket raw protocol").
Back then the dequeued skb was handed straight to nfc_data_exchange(),
which passes it to dev->ops->im_transceive() or dev->ops->tm_send().
Those dereference it without a NULL check.

7e8cdc97148c only inserted
kcov_remote_start_common(skb_get_kcov_handle(skb)) in front of
nfc_data_exchange(). On CONFIG_KCOV kernels that moves the NULL
dereference a little earlier, but it does not add the race or remove
a NULL check.

This is headed for stable. Pointing at 7e8cdc97148c could make the
backport tooling skip older LTS trees that have the same bug. Should
the second tag be:

Fixes: 23b7869c0fd0 ("NFC: add the NFC socket raw protocol")

[Severity: High]
This isn't a bug introduced by this patch, but since rawsock_sendmsg() is
being changed anyway: can rawsock_sendmsg() pass a NULL dev to
nfc_alloc_send_skb()?

rawsock_sendmsg() reads dev before the state check. It does this without
lock_sock() and without any ordering:

net/nfc/rawsock.c:rawsock_sendmsg() {
	struct nfc_dev *dev = nfc_rawsock(sk)->dev;
	...
	if (sock->state != SS_CONNECTED)
		return -ENOTCONN;

	skb = nfc_alloc_send_skb(dev, sk, msg->msg_flags, len, &rc);
	...
}

rawsock_connect() writes both fields while holding only lock_sock():

net/nfc/rawsock.c:rawsock_connect() {
	...
	nfc_rawsock(sk)->dev = dev;
	nfc_rawsock(sk)->target_idx = addr->target_idx;
	sock->state = SS_CONNECTED;
	...
}

A SOCK_SEQPACKET raw socket does not need CAP_NET_RAW. With one thread in
connect() and another in sendmsg() on such a socket:

CPU1 (sendmsg)                         CPU2 (connect)
dev = nfc_rawsock(sk)->dev; /* NULL */
                                       nfc_rawsock(sk)->dev = dev;
                                       sock->state = SS_CONNECTED;
sock->state == SS_CONNECTED
nfc_alloc_send_skb(NULL, ...)
  dev->tx_headroom + dev->tx_tailroom  /* NULL dereference */

This can happen if CPU1 is preempted between the two loads, or if a
weakly ordered CPU reorders the accesses. A target has to exist, which
means an admin must have started polling. Once one does, an unprivileged
user can run this race.

Could dev be read after the SS_CONNECTED check with proper acquire/release
ordering, or could those reads be done under lock_sock()?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261204060651.488-1-luob87709%40gmail.com

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

* Re: [PATCH net v2 2/2] nfc: rawsock: move nfc_deactivate_target() to rawsock_release()
  2026-12-04  6:06 ` [PATCH net v2 2/2] nfc: rawsock: move nfc_deactivate_target() to rawsock_release() Bin Luo via B4 Relay
@ 2026-12-05  6:44   ` netdev-bot+sashiko
  0 siblings, 0 replies; 4+ messages in thread
From: netdev-bot+sashiko @ 2026-12-05  6:44 UTC (permalink / raw)
  To: luob87709
  Cc: david, davem, edumazet, kuba, pabeni, horms, oe-linux-nfc, netdev,
	linux-kernel, stable

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

Critical: 1 · High: 0 · Medium: 0 · 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).

- [Critical] This patch moves nfc_deactivate_target() out of
  rawsock_destruct() and into rawsock_release() in net/nfc/rawsock.c.
- [Low] The commit's Author field has no real name: `luob87709@gmail.com
  <luob87709@gmail.com>`.

--

nfc: rawsock: move nfc_deactivate_target() to rawsock_release()

This moves the nfc_deactivate_target() call out of rawsock_destruct() and
into rawsock_release(), where it runs after tx_work has been cancelled.
The goal is to stop the synchronous NCI deactivate request from running
on ndev->rx_wq when an in-flight data exchange drops the last socket
reference. nfc_put_device() stays in rawsock_destruct().

> 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>

[Severity: Low]
This isn't a bug, but the Author field on this commit is only the bare
address "luob87709@gmail.com <luob87709@gmail.com>". It doesn't match the
name in the Signed-off-by line above, and the SoB name also includes an
affiliation string.

The previous patch in this series, "nfc: rawsock: serialize SEND_SHUTDOWN
and write queue teardown", has the same mismatch. checkpatch reports it as
a From:/Signed-off-by: name mismatch. It may come from the b4 web relay
mentioned in the v2 changelog.

Would a "From: Bin Luo <luob87709@gmail.com>" line in the body, plus a SoB
without the affiliation, record the authorship correctly?

> diff --git a/net/nfc/rawsock.c b/net/nfc/rawsock.c
> index 291f509f07caf..e2a9a2b0bf5f9 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);
>  	}

[Severity: Critical]
Can this deactivation now race with a data exchange from this same socket
that is still in flight?

cancel_work_sync() only waits for rawsock_tx_work() to return. It doesn't
wait for the nfc_data_exchange() that rawsock_tx_work() already
submitted, and that exchange holds its own sock_hold() reference.

Before this patch, rawsock_destruct() could only run after
rawsock_data_exchange_complete() dropped that reference. So deactivation
always happened after the driver was done with the response.

On pn533 the two sides look like this:

close() side:
rawsock_release()
  nfc_deactivate_target()
    device_lock(&dev->dev)
    pn533_deactivate_target()
      skb_queue_purge(&dev->resp_q);

dev->wq side, without device_lock:
pn533_wq_cmd_complete()
  pn533_send_async_complete()
    pn533_data_exchange_complete()
      skb_queue_tail(&dev->resp_q, resp);
      ...
      skb = pn533_build_response(dev);

Suppose the purge runs after skb_queue_tail(). Then resp is already freed
and pn533_build_response() finds resp_q empty and returns NULL. After
that, pn533_data_exchange_complete() does:

	skb = pn533_build_response(dev);
	if (!skb) {
		rc = -ENOMEM;
		goto error;
	}
	...
error:
	dev_kfree_skb(resp);

Doesn't that free resp a second time?

Chained (MI) responses can leave several skbs on resp_q. In that case
pn533_build_response() walks the queue without the queue lock:

	skb_queue_walk_safe(&dev->resp_q, tmp, t) {
		memcpy(skb->data + tmp_len, tmp->data, tmp->len);

Could a concurrent purge turn this into a use-after-free read and a
corrupted list walk?

This also looks reachable without privileges now:

  - rawsock_create() checks CAP_NET_RAW only for SOCK_RAW.
  - nfc_sock_create(), rawsock_connect() and rawsock_sendmsg() don't
    check anything.

Before this patch, deactivating pn533 during an in-flight exchange (pn533
has no check_presence op) needed NFC_CMD_DEACTIVATE_TARGET or
NFC_CMD_ACTIVATE_TARGET. Both are GENL_ADMIN_PERM. A second socket can't
do it either, because nfc_activate_target() returns -EBUSY while a target
is active.

The unlocked resp_q handling in pn533 predates this patch. What's new is
the trigger: an unprivileged SOCK_SEQPACKET socket can send one frame and
then call close() while the response is still being processed on
dev->wq.

Could the in-flight exchange be waited for or aborted before
nfc_deactivate_target() is called here? Another option is to avoid the
destruct-time deactivation some other way that doesn't run it alongside
driver completions.

>  
>  	sock_orphan(sk);

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261204060651.488-1-luob87709%40gmail.com

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

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

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20261204060651.488-1-luob87709@gmail.com>
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-05  6:44   ` netdev-bot+sashiko
2026-12-04  6:06 ` [PATCH net v2 2/2] nfc: rawsock: move nfc_deactivate_target() to rawsock_release() Bin Luo via B4 Relay
2026-12-05  6:44   ` netdev-bot+sashiko

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