๐Ÿš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #27267

Re: [PATCH net v2 1/2] nfc: rawsock: serialize SEND_SHUTDOWN and write queue teardown

#27267
From: netdev-bot+sashiko
Date: Sat, 05 Dec 2026 06:44
206 lines
15986 bytes
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
Technical Details
Message-ID: <179645307958.3146.4439479121604304072@kernel.org>
Path: mbox-mo1.pugleaf.net!feed-eu-txt.newsdeef.eu!feed-eu-ch.newsdeef.eu!srl.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-377854-linux-kernel-netdev=dewy.ru
References: <20261204060651.488-2-luob87709@gmail.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!feed-eu-txt.newsdeef.eu!feed-eu-ch.newsdeef.eu!srl.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-377854-linux-kernel-netdev=dewy.ru
From: netdev-bot+sashiko@kernel.org
Newsgroups: linux.kernel.netdev
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
Organization: Usenet Network
Message-ID: <179645307958.3146.4439479121604304072@kernel.org>
References: <20261204060651.488-2-luob87709@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="2970231"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:Oj+YF4s1SYNWzpnjXtTbfNFbmjM= sha256:9FjyAlKsyPlyC1qlcIWhAD6dSqD4sxysCfhVQWTEobc=
	sha1:EI2w9YljBZ/jJ0Pm/jWMpbbufvo= sha256:M0e8Iav8UHChM3CrBZ+1uh9Sev7RGiCObvKKcDSW1xo=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id D8897117D86; Sat,  5 Dec 2026 09:45:00 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.541
X-Spam-Status: No, score=-3.541 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.142, DKIM_SIGNED=0.1,
	DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=kernel.org
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id CtU8RnOh7TZE;
	Sat,  5 Dec 2026 09:44:53 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-377854-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
	by mail.dewy.ru (Postfix) with ESMTPS id 1D47B1167B8; Sat,  5 Dec 2026 09:44:44 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sea.lore.kernel.org (Postfix) with ESMTP id D5E9932928; Sat,  5 Dec 2026 06:44:41 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1796453081;
	b=rE/YLeQa3UJGL29v52iZ9Ljpz7mRo/o5ykUH3vnxFcCMDyJXwmCyuYqnt5Im56TT7PCT
	 5VuPJhsCy4ALOS8NARXHcoi1wXQ26nBa3f+7Tlfp/pyefvh0MeY4Hxl/sl3dfU0Fmf01i
	 YyfM6SA6KHPSSIcOmPJkV92xZt+1WCyXRGT3AOVNyAP0kAi7GkswG91ywVSFICOX2DYV8
	 DFJpq4peQsB1r6LCly6Wu2KqulAojy/pTq76nGuGckZbo+pIfWN4sVDejoryD50xJjZzV
	 xlQXg0q97dQSEgOP2TEgCy98Rz+y8KxNU4iaNmEtR/8PbDMR6Etl5IjLULZcsjXqDUw==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796453081;
	h=X-Original-To:Received:Received:DKIM-Signature:Subject:From:To:Cc:
	 Date:Message-ID:In-Reply-To:References:X-sashiko-severity:
	 Content-Type:Content-Transfer-Encoding:X-Mailing-List:List-Id:
	 List-Post:List-Help:List-Subscribe:List-Unsubscribe:List-Owner:
	 Precedence:MIME-Version;
	bh=y31wlELu94eIb+uUay8m6XPzU6eAv+2fx39OGSRd8WM=;
	b=oGQwbB7ghgSOXrKeiucq2eygleQxE/Y6IN1lraCnzWH1WANfEF0izHnhkRb7uWk4o0BH
	 bGVyOr0+grPFhdgjOiAenLc4KXC6SlbzCTRn5aIH5fGhH+BWaKeSTzE6rmVohns0wLqv+
	 471U7n+fPuC++r/MuZNuNH23YmJk2RPoXceMaW7yubrlfNm2Um3mA3HwxQwazL2lDjJvk
	 TBSWhhGL7h2px+iXVjkXefRhBb0ZL179h6HFdExbj1ADvjJZdSiBfqqI2zMis+hu8JoSA
	 GNvTo9tqDOV95A7IBGYa7tYOhfHgAKCjLKQpIo8oKbO0+BL/2Wsp52fc3ErD0J+3aFw==
X-ARC-Authentication-Results: i=2; 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=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id A903A606A337;
	Sat,  5 Dec 2026 06:44:41 +0000 (UTC)
X-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
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18
X-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==
X-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==
X-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
X-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)
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0ADD91F0092B;
	Sat,  5 Dec 2026 06:44:39 +0000 (UTC)
X-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==
X-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
X-In-Reply-To: <20261204060651.488-2-luob87709@gmail.com>
X-sashiko-severity: Medium
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list