From: netdev-bot+sashiko@kernel.org
To: luob87709@gmail.com
Cc: david@ixit.cz, davem@davemloft.net, edumazet@kernel.org,
kuba@kernel.org, pabeni@redhat.com, horms@kernel.org,
oe-linux-nfc@lists.linux.dev, netdev@vger.kernel.org,
linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: Re: [PATCH net v2 1/2] nfc: rawsock: serialize SEND_SHUTDOWN and write queue teardown
Date: Sat, 05 Dec 2026 06:44:39 +0000 [thread overview]
Message-ID: <179645307958.3146.4439479121604304072@kernel.org> (raw)
In-Reply-To: <20261204060651.488-2-luob87709@gmail.com>
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
next prev parent reply other threads:[~2026-12-05 6:44 UTC|newest]
Thread overview: 8+ 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 (University of Electronic Science and Technology of China)
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 (University of Electronic Science and Technology of China)
2026-12-05 6:44 ` netdev-bot+sashiko [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
2026-12-04 6:06 ` Bin Luo (University of Electronic Science and Technology of China)
2026-12-05 6:44 ` netdev-bot+sashiko
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=179645307958.3146.4439479121604304072@kernel.org \
--to=netdev-bot+sashiko@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.