Netdev List
 help / color / mirror / Atom feed
* [PATCH net] nfc: rawsock: serialize sock->state and dev read in rawsock_sendmsg()
@ 2026-12-06 11:08 Bin Luo via B4 Relay
  0 siblings, 0 replies; only message in thread
From: Bin Luo via B4 Relay @ 2026-12-06 11:08 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 <luob87709@gmail.com>

rawsock_sendmsg() currently loads dev = nfc_rawsock(sk)->dev before
checking sock->state != SS_CONNECTED, without holding lock_sock(sk) or
using acquire/release ordering.

Meanwhile, rawsock_connect() sets nfc_rawsock(sk)->dev and transitions
sock->state to SS_CONNECTED under lock_sock(sk).  If an unprivileged
SOCK_SEQPACKET socket invokes connect() and sendmsg() concurrently on
two threads, rawsock_sendmsg() can read dev = NULL, then observe
sock->state = SS_CONNECTED and pass NULL into nfc_alloc_send_skb(),
dereferencing dev->tx_headroom and crashing the kernel.

Discovered during Sashiko AI review of a related rawsock patch series
and reproduced live in QEMU KVM on an unmodified kernel using
/dev/virtual_nci without delay injection:

  KASAN: null-ptr-deref in range [0x0000000000000590-0x0000000000000597]
  RIP: 0010:nfc_alloc_send_skb+0x35/0x1b0
  Call Trace:
   rawsock_sendmsg+0x15b/0x4d0
   __sys_sendto+0x36f/0x3c0

Acquire lock_sock(sk) around the sock->state check and dev load in
rawsock_sendmsg() (matching llcp_sock_sendmsg()), releasing the socket
lock before calling nfc_alloc_send_skb().

Verified fixed in QEMU KVM (KASAN, LOCKDEP, DEBUG_OBJECTS) across 400
concurrent connect()/sendmsg() race iterations (0 Oops, 0 warnings) and
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 <luob87709@gmail.com>
---
 net/nfc/rawsock.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/net/nfc/rawsock.c b/net/nfc/rawsock.c
index f7d7a59..61c08c6 100644
--- a/net/nfc/rawsock.c
+++ b/net/nfc/rawsock.c
@@ -216,7 +216,7 @@ static void rawsock_tx_work(struct work_struct *work)
 static int rawsock_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
 {
 	struct sock *sk = sock->sk;
-	struct nfc_dev *dev = nfc_rawsock(sk)->dev;
+	struct nfc_dev *dev;
 	struct sk_buff *skb;
 	int rc;

@@ -225,8 +225,13 @@ static int rawsock_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
 	if (msg->msg_namelen)
 		return -EOPNOTSUPP;

-	if (sock->state != SS_CONNECTED)
+	lock_sock(sk);
+	if (sock->state != SS_CONNECTED) {
+		release_sock(sk);
 		return -ENOTCONN;
+	}
+	dev = nfc_rawsock(sk)->dev;
+	release_sock(sk);

 	skb = nfc_alloc_send_skb(dev, sk, msg->msg_flags, len, &rc);
 	if (skb = NULL)
--
2.43.0




^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-06 11:09 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-06 11:08 [PATCH net] nfc: rawsock: serialize sock->state and dev read in rawsock_sendmsg() Bin Luo via B4 Relay

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