From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796453080; b=3ZOHUFFiotl/fh9gPVuoo3gsQtBmvJeDhVDcvHOjtU13IwulbmQSwAvqbwpB7BSwBdjs 3Ml/XTrGaUQ3Hq1z6tkYZrKyenFiVu90UKypJKkb+rFaAG6355nSY62fI1kC5EzrlMyeN yF9LkOP59MzSAs4UQlZTGkOHmJDMVstyh8DUH5hV6VCsJwa+9OxTYRlsUuSe1MzsO6j9Y BLWXys4JkkWdC5JGY+0e5vh2qsaxMUvB5akKBI+E/1MMjcy72H57FUT0bsoWJ6k9tKjpH TbHDdqJutQp4X/D3/7OQPyZxYEGMc3yE+lNGpfKCFOfepfyTVPxIB7PhnnycYL17e6w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796453080; h=Received:DKIM-Signature:Subject:From:To:Cc:Date:Message-ID: In-Reply-To:References:X-sashiko-severity:Content-Type: Content-Transfer-Encoding:MIME-Version; bh=y31wlELu94eIb+uUay8m6XPzU6eAv+2fx39OGSRd8WM=; b=rxLeo6/EF4zk8QCsqsRYJQ6+D9+0Lil2o5Glsf235Spk5uzIZTkglM20Lltn6dk6S/IM EYHkp4OsJFBUXsnW4qe9MGH7YPwAXaM3URm1QCnLqBx7Ev/j5jpmUDEP3JKpPP/iDoTTZ JSdkNIily86he+MQhPj01H9kEOz/HnvVnb1MGUPFkStAmc6au2ROr/9CoUQ3YkpNIm/af 2rFDDLLa+tRV7aLa6JnbStiTRwWemUyLTlRNc2CqlORQUGPMrMIOQnex7xzuZ+i6sl2cz lShTiDleJ0an1fKVqeivbeYCvwWTLN7dU3fOQdIADxXRRaDsoxCJ9xNrL1Yat3vyiWg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=FnxSKPxf; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C73C8606A322; Sat, 5 Dec 2026 06:44:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=FnxSKPxf Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0ADD91F0092B; Sat, 5 Dec 2026 06:44:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796453080; bh=y31wlELu94eIb+uUay8m6XPzU6eAv+2fx39OGSRd8WM=; h=Subject:From:To:Cc:Date:In-Reply-To:References; b=FnxSKPxfLcPVv3qG9prrJAnReRIHSkB5OKLmoo91XJ1QZI337zbRkd11th5eQ0VD8 CHo8wYdiBFruNrIL/B/ecPbONZVtDBooBaY0pypeg8TYn8CXr1ocdjOrn4FPbYBE/K EhuWSwa2pcQ2Bn9rWJgLLJPnFGeAkhWtJVsW6m5T1PFxM284qAjxu9NrKLwF8ZGf+Z WARC7Ozt73fkCQ62tmIodlYopWVX1c5/+GCxUC+2BGHgFiCRlR8DLgyiPHLNlf9g0I HKXI4LzvhrhyVz9xS7FeBgKBLl1+POjPNmlKVlL+UVUV/XzODL0UoYt7doJvOZBNDC FPSgUY2dmFomw== Subject: Re: [PATCH net v2 1/2] nfc: rawsock: serialize SEND_SHUTDOWN and write queue teardown 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 Date: Sat, 05 Dec 2026 06:44:39 +0000 Message-ID: <179645307958.3146.4439479121604304072@kernel.org> In-Reply-To: <20261204060651.488-2-luob87709@gmail.com> References: <20261204060651.488-2-luob87709@gmail.com> X-sashiko-severity: Medium Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 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