vsock/virtio: validate packet source for connected sockets
Subsystems (6)
PATCH 1/1 vsock/virtio: validate packet source for connected sockets
virtio_transport_recv_pkt() first looks up a socket using the full source and destination tuple. If that misses, it falls back to a bound-socket lookup using only the destination address. The fallback is needed for listening and connecting sockets, but it can also select an established socket that remains in the bound table.
As a result, a packet from an unrelated source can be dispatched to a non-listening socket. In TCP_SYN_SENT, a source-blind RESPONSE marks the selected socket established while retaining its original remote address. Subsequent RW packets can likewise be delivered through the destination-only fallback.
This was reproduced with two capless processes under different UIDs. The attacker discovered the victim tuple through unprivileged AF_VSOCK sock_diag and injected a chosen 16-byte payload into the victim established loopback socket. The legitimate peer received none of those bytes.
After taking the socket lock, verify that packets for non-listening sockets come from the peer stored in remote_addr. Listening sockets continue to accept packets from any source.
--- net/vmw_vsock/virtio_transport_common.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
net/vmw_vsock/virtio_transport_common.c +7 -3
@@ -1822,11 +1822,15 @@ void virtio_transport_recv_pkt(struct virtio_transport *t,lock_sock (sk); -/* Check if sk has been closed or assigned to another transport before -* lock_sock (note: listener sockets are not assigned to any transport) +/* Check if sk has been closed, assigned to another transport, or if the +* packet is from a different peer than the one connected to sk. These +* properties could have changed before lock_sock. Listener sockets are +* not assigned to any transport and accept packets from any peer. */ if (sock_flag (sk,SOCK_DONE ) || - (sk->sk_state !=TCP_LISTEN && vsk->transport != &t->transport)) { + (sk->sk_state !=TCP_LISTEN && + (vsk->transport != &t->transport || + !vsock_addr_equals_addr (&src, &vsk->remote_addr)))) { (void )virtio_transport_reset_no_sock (t, skb, net);release_sock (sk);sock_put (sk);
base-commit: 9006c116dd111d457bf5d074990210f70a4ad2c8
Signature · 1 line
2.54.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] The identical destination-only fallback vulnerability fixed in virtio_transport exists unpatched in vmci_transport.c. --
--- Patch [1]: [PATCH net] vsock/virtio: validate packet source for connected sockets --- commit cb88c0daae92eec924188a4684efd077dd9df9cd
Author: Daehyeon Ko <[email protected]>
Subject: vsock/virtio: validate packet source for connected sockets
This commit fixes a vulnerability in the virtio socket transport where packets from an unrelated source could be incorrectly dispatched to a non-listening socket via a destination-only fallback lookup.
Quoted 4 lines
net/vmw_vsock/virtio_transport_common.c +0
-0
[ ... ]
Quoted 6 lines
net/vmw_vsock/virtio_transport_common.c +0
-2@@ -1822,11 +1822,15 @@ void virtio_transport_recv_pkt(struct virtio_transport *t,lock_sock (sk); -/* Check if sk has been closed or assigned to another transport before -* lock_sock (note: listener sockets are not assigned to any transport)
net/vmw_vsock/virtio_transport_common.c void virtio_transport_recv_pkt(struct virtio_transport *t, · line 1833
Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +7
-1+/* Check if sk has been closed, assigned to another transport, or if the +* packet is from a different peer than the one connected to sk. These +* properties could have changed before lock_sock. Listener sockets are +* not assigned to any transport and accept packets from any peer. */ if (sock_flag (sk,SOCK_DONE ) || - (sk->sk_state !=TCP_LISTEN && vsk->transport != &t->transport)) { + (sk->sk_state !=TCP_LISTEN && + (vsk->transport != &t->transport || + !vsock_addr_equals_addr (&src, &vsk->remote_addr)))) {
[Severity: High]
This isn't a bug introduced by this patch, but does the same vulnerability exist unpatched in the VMCI transport?
Looking at net/vmw_vsock/vmci_transport.c:vmci_transport_recv_stream_cb(), it appears to use the same fallback logic where it can return an established socket if the connected socket lookup misses:
net/vmw_vsock/vmci_transport.c:vmci_transport_recv_stream_cb() {
...
sk = vsock_find_bound_socket(&dst);
...
}The packet is then processed by vmci_transport_recv_pkt_work() which calls vmci_transport_recv_connected() for TCP_ESTABLISHED sockets:
net/vmw_vsock/vmci_transport.c:vmci_transport_recv_pkt_work() {
...
case TCP_ESTABLISHED:
vmci_transport_recv_connected(sk, pkt);
break;
...
}Neither of these functions appears to verify that the packet's source address matches the socket's remote_addr before processing it.
Could this allow an attacker to inject control packets into an established connection and cause a denial of service?
net/vmw_vsock/vmci_transport.c:vmci_transport_recv_connected() {
...
case VMCI_TRANSPORT_PACKET_TYPE_RST:
vsk = vsock_sk(sk);
sock_set_flag(sk, SOCK_DONE);
WRITE_ONCE(vsk->peer_shutdown, SHUTDOWN_MASK);
...
}Should a similar validation be added to vmci_transport_recv_pkt_work() or the VMCI handlers?
Quoted 3 lines
net/vmw_vsock/virtio_transport_common.c +0
-0(void )virtio_transport_reset_no_sock (t, skb, net);release_sock (sk);sock_put (sk);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Stefano Garzarella
maintainerQuoted 12 lines · Daehyeon Ko
virtio_transport_recv_pkt()first looks up a socket using the full source and destination tuple. If that misses, it falls back to a bound-socket lookup using only the destination address. The fallback is needed for listening and connecting sockets, but it can also select an established socket that remains in the bound table.As a result, a packet from an unrelated source can be dispatched to a non-listening socket. In
TCP_SYN_SENT, a source-blind RESPONSE marks the selected socket established while retaining its original remote address. Subsequent RW packets can likewise be delivered through the destination-only fallback.
Quoted 4 lines
This was reproduced with two capless processes under different UIDs. The attacker discovered the victim tuple through unprivileged
AF_VSOCKsock_diagand injected a chosen 16-byte payload into the victim established loopback socket. The legitimate peer received none of those bytes.
I don't understand this, what it means? If the receiver doesn't receive those injected bytes, should be fine, no?
Quoted 4 lines
After taking the socket lock, verify that packets for non-listening sockets come from the peer stored in
remote_addr. Listening sockets continue to accept packets from any source.
IMO this description should be improved, it's quite hard to follow. This is an idea IIUC the issue:
virtio_transport_recv_pkt() looks up sockets in two steps: first by the full source and destination tuple in the connected table, then by destination only in the bound table. The fallback is needed for listening and connecting sockets, but it never validates the source address in the packet header. Since sockets remain in the bound table after connect(), a packet from any source can be dispatched to a non-listening socket.
An unprivileged process can discover the target tuple through AF_VSOCK sock_diag (no capabilities required), send a forged RESPONSE to a TCP_SYN_SENT socket to establish the connection, and then inject arbitrary data through subsequent RW packets, all delivered via the destination-only fallback.
After lock_sock(), verify that the packet source matches the remote_addr stored during connect().
Quoted 13 lines
Fixes 06a8fc78367d ("VSOCK: Introduce virtio_vsock_common.ko")Signed-off-by Daehyeon Ko <[email protected]>---
net/vmw_vsock/virtio_transport_common.c| 10 +++++++---1 file changed, 7 insertions(+), 3 deletions(-)net/vmw_vsock/virtio_transport_common.c +0
-0
net/vmw_vsock/virtio_transport_common.c void virtio_transport_recv_pkt(struct virtio_transport *t, · line 1828
Quoted 9 lines
net/vmw_vsock/virtio_transport_common.c +4
-2@@ -1822,11 +1822,15 @@ void virtio_transport_recv_pkt(struct virtio_transport *t,lock_sock (sk); -/* Check if sk has been closed or assigned to another transport before -* lock_sock (note: listener sockets are not assigned to any transport) +/* Check if sk has been closed, assigned to another transport, or if the +* packet is from a different peer than the one connected to sk. These +* properties could have changed before lock_sock. Listener sockets are +* not assigned to any transport and accept packets from any peer.
Please keeps changes minimal, why adding "These properties could have changed before lock_sock." and changing the phrase related to listener socket?
Also the new part is not clear IMO, we should explain why we are adding this new check, not what we are doing which is clear looking at the code.
IMO we should just add something like this (feel free to change it):
Also verify the packet source: the bound-table fallback lookup matches by destination only, so a non-listening socket can be reached by a packet from an unrelated peer. Drop it unless the source matches the peer the socket is connecting or connected to.
net/vmw_vsock/virtio_transport_common.c void virtio_transport_recv_pkt(struct virtio_transport *t, · line 1833
Quoted 6 lines
net/vmw_vsock/virtio_transport_common.c +3
-1*/ if (sock_flag (sk,SOCK_DONE ) || - (sk->sk_state !=TCP_LISTEN && vsk->transport != &t->transport)) { + (sk->sk_state !=TCP_LISTEN && + (vsk->transport != &t->transport || + !vsock_addr_equals_addr (&src, &vsk->remote_addr)))) {
Do we need to add the same check also in the VMCI transport?
Hyper-V seems different.
Thanks,
Stefano
Quoted 8 lines
net/vmw_vsock/virtio_transport_common.c +0
-0(void )virtio_transport_reset_no_sock (t, skb, net);release_sock (sk);sock_put (sk);base-commit:
9006c116dd111d457bf5d074990210f70a4ad2c8Signature · 1 line
2.54.0
Quoted 38 lines · Daehyeon Ko
virtio_transport_recv_pkt()first looks up a socket using the full source and destination tuple. If that misses, it falls back to a bound-socket lookup using only the destination address. The fallback is needed for listening and connecting sockets, but it can also select an established socket that remains in the bound table.As a result, a packet from an unrelated source can be dispatched to a non-listening socket. In
TCP_SYN_SENT, a source-blind RESPONSE marks the selected socket established while retaining its original remote address. Subsequent RW packets can likewise be delivered through the destination-only fallback.This was reproduced with two capless processes under different UIDs. The attacker discovered the victim tuple through unprivileged
AF_VSOCKsock_diagand injected a chosen 16-byte payload into the victim established loopback socket. The legitimate peer received none of those bytes.After taking the socket lock, verify that packets for non-listening sockets come from the peer stored in
remote_addr. Listening sockets continue to accept packets from any source.Fixes 06a8fc78367d ("VSOCK: Introduce virtio_vsock_common.ko")Signed-off-by Daehyeon Ko <[email protected]>--- net/vmw_vsock/virtio_transport_common.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)net/vmw_vsock/virtio_transport_common.c +0
-2@@ -1822,11 +1822,15 @@ void virtio_transport_recv_pkt(struct virtio_transport *t,lock_sock (sk); -/* Check if sk has been closed or assigned to another transport before -* lock_sock (note: listener sockets are not assigned to any transport)
net/vmw_vsock/virtio_transport_common.c void virtio_transport_recv_pkt(struct virtio_transport *t, · line 1833
Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +7
-1+/* Check if sk has been closed, assigned to another transport, or if the +* packet is from a different peer than the one connected to sk. These +* properties could have changed before lock_sock. Listener sockets are +* not assigned to any transport and accept packets from any peer. */ if (sock_flag (sk,SOCK_DONE ) || - (sk->sk_state !=TCP_LISTEN && vsk->transport != &t->transport)) { + (sk->sk_state !=TCP_LISTEN && + (vsk->transport != &t->transport || + !vsock_addr_equals_addr (&src, &vsk->remote_addr)))) {
Does this equality work for loopback, when the CID may be VMADDR_CID_LOCAL and/or VMADDR_CID_HOST on host or the guest CID in guest?
Best,
Bobby
Quoted 3 lines · Bobby Eshleman
Does this equality work for loopback, when the CID may be
VMADDR_CID_LOCALand/orVMADDR_CID_HOSTon host or the guest CID in guest?
Not in all cases. I tested VMADDR_CID_HOST with no guest-to-host transport, where it is routed over loopback but packets carry VMADDR_CID_LOCAL as their source. The unpatched kernel transferred data in 3/3 runs, while v1 timed out in 3/3 runs.
The v2 candidate requires the source port to match the socket's remote port and accepts VMADDR_CID_LOCAL as the canonical source CID for loopback. It preserved the CID_LOCAL, CID_HOST, and guest-CID loopback cases in my tests, while the original cross-UID injection remained blocked. The selected VSOCK selftests also passed with no KASAN report.
Stefano, to clarify the original description: the victim socket received the exact 16 attacker-chosen bytes; the intended peer-side socket received none. I will use your clearer description and keep the comment minimal in v2.
I also confirmed the analogous VMCI destination-only fallback. With two VMCI contexts, an RST from a mismatched context reset another context's pending connection in 3/3 runs. VMCI also has a notification fast path before the workqueue handler, so I will address it separately with validation in both paths. Hyper-V does not appear to share this packet lookup path.
I will post the virtio v2 as a separate thread.
Thanks,
Daehyeon