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 v3 1/2] nfc: rawsock: serialize SEND_SHUTDOWN and write queue teardown
Date: Sun, 06 Dec 2026 10:52:11 -0000 [thread overview]
Message-ID: <20261206105211.379-2-luob87709@gmail.com> (raw)
In-Reply-To: <20261206105211.379-1-luob87709@gmail.com>
From: Bin Luo <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) and nfc_data_exchange() without checking
for NULL. If rawsock_report_error() purges sk_write_queue in
between, skb is NULL and dereferencing it crashes.
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(), record sk->sk_err with WRITE_ONCE() before
acquiring sk_write_queue.lock so any waiters woken by sock_wfree()
during queue purging observe the NFC error rather than -EPIPE, then
call sk_error_report() after unlocking.
Fixes: d793458c45df ("nfc: rawsock: cancel tx_work before socket teardown")
Fixes: 23b7869c0fd0 ("NFC: add the NFC socket raw protocol")
Cc: stable@vger.kernel.org
Signed-off-by: Bin Luo <luob87709@gmail.com>
---
Changes in v3:
- In rawsock_report_error(), write sk->sk_err with WRITE_ONCE() before
acquiring sk_write_queue.lock so waiters woken by sock_wfree() during
write-queue purging see the NFC error instead of -EPIPE.
- Drop the inaccurate rawsock_release() mention from the race 2
description and fix the second Fixes tag to 23b7869c0fd0.
- Normalize From:/Signed-off-by: author metadata.
Changes in v2:
- Resend cleanly via b4 web relay and include both dependent rawsock
patches as a threaded series.
net/nfc/rawsock.c | 46 +++++++++++++++++++++++++++++++---------------
1 file changed, 31 insertions(+), 15 deletions(-)
diff --git a/net/nfc/rawsock.c b/net/nfc/rawsock.c
index f7d7a59..2fa0f05 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);
+ WRITE_ONCE(sk->sk_err, -err);
+ spin_lock_bh(&sk->sk_write_queue.lock);
sk->sk_shutdown = SHUTDOWN_MASK;
- sk->sk_err = -err;
- sk_error_report(sk);
+ __rawsock_write_queue_purge(sk);
+ spin_unlock_bh(&sk->sk_write_queue.lock);
- rawsock_write_queue_purge(sk);
+ sk_error_report(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
next prev parent reply other threads:[~2026-12-06 10:52 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-06 10:52 [PATCH net v3 0/2] nfc: rawsock: fix teardown races and destruct-time deactivation deadlock Bin Luo via B4 Relay
2026-12-06 10:52 ` [PATCH net v3 2/2] nfc: rawsock: avoid synchronous target deactivation in rawsock_destruct() Bin Luo via B4 Relay
2026-12-06 10:52 ` Bin Luo via B4 Relay [this message]
2026-12-06 10:57 ` [PATCH net v3 0/2] nfc: rawsock: fix teardown races and destruct-time deactivation deadlock netdev-bot+sinfo
2026-12-06 11:04 ` bin luo
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=20261206105211.379-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