From: Stefano Garzarella <sgarzare@redhat.com>
To: "Bartłomiej Dmitruk" <bartlomiej.dmitruk@isec.pl>
Cc: Bryan Tan <bryan-bt.tan@broadcom.com>,
Vishnu Dasa <vishnu.dasa@broadcom.com>,
bcm-kernel-feedback-list@broadcom.com,
"David S . Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@google.com>,
Jakub Kicinski <kuba@kernel.org>,
Paolo Abeni <pabeni@redhat.com>, Simon Horman <horms@kernel.org>,
"Michael S . Tsirkin" <mst@redhat.com>,
virtualization@lists.linux.dev, netdev@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 1/2] vsock/vmci: make the cached_peer dgram decision race-safe
Date: Tue, 22 Sep 2026 14:47:01 +0200 [thread overview]
Message-ID: <arJ3bKG7vRjMI5Bo@sgarzare-redhat> (raw)
In-Reply-To: <20260919123208.29032-1-bartlomiej.dmitruk@isec.pl>
On Sat, Sep 19, 2026 at 02:31:56PM +0200, Bartłomiej Dmitruk wrote:
>vmci_transport_allow_dgram() cached its result in vsock->cached_peer and
>vsock->cached_peer_allow_dgram with an unsynchronized check-then-set. The
>function runs both in the lockless receive tasklet
>(vmci_transport_recv_dgram_cb(), no socket lock) and in the lock_sock() send
>path; lock_sock() does not exclude bottom halves, so the two contexts race on
>those fields and can return a stale 'allow' for a VMCI_PRIVILEGE_FLAG_RESTRICTED
>peer. It is also a plain data race. The in-code comment claiming the fields
>are never modified outside create/destruct is contradicted by the send path.
>
>Keep the O(1) cache -- it avoids an O(N) vmci_ctx_get() lookup on every
>datagram in the bottom-half receive path -- but pack the peer CID and the
>decision into a single word accessed with READ_ONCE()/WRITE_ONCE(). A race
>then only forces a recompute and can never return a stale allow.
I honestly don't understand this part...
>
>This was found by code inspection; I do not have VMCI hardware to test on
>(compile-tested only).
>
>Fixes: d021c344051a ("VSOCK: Introduce VM Sockets")
>Signed-off-by: Bartłomiej Dmitruk <bartlomiej.dmitruk@isec.pl>
>Assisted-by: Claude (Anthropic)
>---
>v2: keep an O(1) cache made race-safe rather than dropping it entirely; an
> earlier revision removed the cache, which the Sashiko AI review flagged as
> an O(N)-per-datagram fast-path regression. Split out per Stefano
> Garzarella; independent of namespace support.
>v1: https://lore.kernel.org/netdev/20260917220225.56200-1-bartlomiej.dmitruk@isec.pl/
>
>diff --git a/include/net/af_vsock.h b/include/net/af_vsock.h
>index 5549298c1..97968ac53 100644
>--- a/include/net/af_vsock.h
>+++ b/include/net/af_vsock.h
>@@ -39,10 +39,13 @@ struct vsock_sock {
> * modified outsided of socket create or destruct.
> */
> bool trusted;
>- bool cached_peer_allow_dgram; /* Dgram communication allowed to
>- * cached peer?
>- */
>- u32 cached_peer; /* Context ID of last dgram destination check. */
>+ /* Cached dgram access decision for the last peer, packed as
>+ * (cid << 32) | VALID | ALLOW and accessed via READ_ONCE()/
>+ * WRITE_ONCE() so the lockless receive tasklet and the
>+ * lock_sock() send path cannot race to a stale decision.
>+ * See vmci_transport_allow_dgram().
>+ */
>+ u64 cached_peer_access;
> const struct cred *owner;
> /* Rest are SOCK_STREAM only. */
> long connect_timeout;
>diff --git a/net/vmw_vsock/vmci_transport.c b/net/vmw_vsock/vmci_transport.c
>--- a/net/vmw_vsock/vmci_transport.c
>+++ b/net/vmw_vsock/vmci_transport.c
>@@ -524,23 +524,38 @@
> * only if it is trusted as described in vmci_transport_is_trusted.
> */
>
>+/* Packing for vsk->cached_peer_access. */
>+#define VMCI_DGRAM_ACCESS_VALID BIT_ULL(0)
>+#define VMCI_DGRAM_ACCESS_ALLOW BIT_ULL(1)
>+#define VMCI_DGRAM_ACCESS_CID_SHIFT 32
>+
> static bool vmci_transport_allow_dgram(struct vsock_sock *vsock, u32 peer_cid)
> {
>+ u64 access;
>+
> if (VMADDR_CID_HYPERVISOR == peer_cid)
> return true;
>
>- if (vsock->cached_peer != peer_cid) {
>- vsock->cached_peer = peer_cid;
>- if (!vmci_transport_is_trusted(vsock, peer_cid) &&
>- (vmci_context_get_priv_flags(peer_cid) &
>- VMCI_PRIVILEGE_FLAG_RESTRICTED)) {
>- vsock->cached_peer_allow_dgram = false;
>- } else {
>- vsock->cached_peer_allow_dgram = true;
>- }
>- }
>+ /* Cache the trusted/restricted decision for the last peer to avoid the
>+ * O(N) vmci_ctx_get() lookup on every datagram. Read/update it through
>+ * a single word so a race between the lockless receive tasklet and the
>+ * lock_sock() send path only forces a recompute -- it can never
>return a
Is this a real issue?
We have this in the code:
* NOTE: We access the socket struct without holding the lock here.
* This is ok because the field we are interested is never modified
* outside of the create and destruct socket functions.
*/
vsk = vsock_sk(sk);
if (!vmci_transport_allow_dgram(vsk, dg->src.context))
return VMCI_ERROR_NO_ACCESS;
>+ * stale allow for a restricted peer.
>+ */
>+ access = READ_ONCE(vsock->cached_peer_access);
How this will work on 32-bit systems?
Stefano
>+ if ((access & VMCI_DGRAM_ACCESS_VALID) &&
>+ (u32)(access >> VMCI_DGRAM_ACCESS_CID_SHIFT) == peer_cid)
>+ return !!(access & VMCI_DGRAM_ACCESS_ALLOW);
>
>- return vsock->cached_peer_allow_dgram;
>+ access = VMCI_DGRAM_ACCESS_VALID |
>+ ((u64)peer_cid << VMCI_DGRAM_ACCESS_CID_SHIFT);
>+ if (vmci_transport_is_trusted(vsock, peer_cid) ||
>+ !(vmci_context_get_priv_flags(peer_cid) &
>+ VMCI_PRIVILEGE_FLAG_RESTRICTED))
>+ access |= VMCI_DGRAM_ACCESS_ALLOW;
>+
>+ WRITE_ONCE(vsock->cached_peer_access, access);
>+ return !!(access & VMCI_DGRAM_ACCESS_ALLOW);
> }
>
> static int
>
next prev parent reply other threads:[~2026-09-22 12:47 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-19 12:31 Bartłomiej Dmitruk
2026-09-19 12:31 ` [PATCH v2 2/2] vsock/vmci: enforce per-netns mode on the datagram receive path Bartłomiej Dmitruk
2026-09-22 12:49 ` Stefano Garzarella
2026-09-22 17:54 ` Vishnu Dasa
2026-09-23 12:56 ` netdev-bot+sashiko
2026-09-22 12:47 ` Stefano Garzarella [this message]
2026-09-23 12:56 ` [PATCH v2 1/2] vsock/vmci: make the cached_peer dgram decision race-safe 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=arJ3bKG7vRjMI5Bo@sgarzare-redhat \
--to=sgarzare@redhat.com \
--cc=bartlomiej.dmitruk@isec.pl \
--cc=bcm-kernel-feedback-list@broadcom.com \
--cc=bryan-bt.tan@broadcom.com \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mst@redhat.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=virtualization@lists.linux.dev \
--cc=vishnu.dasa@broadcom.com \
/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
all inboxes | Powered by JetHome®