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] nfc: rawsock: serialize sock->state and dev read in rawsock_sendmsg()
Date: Sun, 06 Dec 2026 11:08:59 -0000 [thread overview]
Message-ID: <20261206110859.377-1-luob87709@gmail.com> (raw)
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
reply other threads:[~2026-12-06 11:09 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20261206110859.377-1-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