Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] vsock: inconsistent loopback source CID check in
  vsock_dgram_get_transport()
- [Medium] vsock: missing VMADDR_CID_ANY check in
  vsock_dgram_resolve_cid()
- [Medium] vsock: per-packet global vsock_register_mutex lock on
  datagram hot paths
--

commit 21473b8a230a0146483d0435292761949197fc7c
Author: Sergio Lopez <[email protected]>

vsock: allow datagrams to use directional transports

This commit enables AF_VSOCK datagram sockets to route packets across
directional (G2H/H2G) and loopback transports while retaining VMCI as a
fallback endpoint owner and unifying datagram receive queueing in the core.

Link: https://lore.kernel.org/all/[email protected]/
Link: https://lore.kernel.org/all/[email protected]/

> diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
> index aef5389eeca2c..1880ff8ed9ca3 100644
> --- a/net/vmw_vsock/af_vsock.c
> +++ b/net/vmw_vsock/af_vsock.c

[ ... ]

> @@ -724,6 +802,113 @@ int vsock_assign_transport(struct vsock_sock *vsk, 
> struct vsock_sock *psk)
>  }
>  EXPORT_SYMBOL_GPL(vsock_assign_transport);
>  
> +/**
> + * vsock_dgram_get_transport - select a transport for a datagram destination
> + * @vsk: socket whose datagram state will be used
> + * @remote_addr: destination address
> + * @vmci_self: wildcard destination resolved to the VMCI owner's local CID
> + *
> + * The caller must hold the socket lock and release the returned transport's
> + * module reference after use. A datagram fallback owns its socket's binding,
> + * but a directional transport takes precedence when it can reach the peer.
> + * Parent fallback sets VMADDR_FLAG_TO_HOST to preserve that route on 
> connect.
> + *
> + * Return: a referenced transport or an ERR_PTR on failure.
> + */
> +const struct vsock_transport *
> +vsock_dgram_get_transport(struct vsock_sock *vsk,
> +                       struct sockaddr_vm *remote_addr, bool vmci_self)
> +{

[ ... ]

> +     if (!transport)
> +             goto out;
> +     if (owner->dgram_transport_match &&
> +         !owner->dgram_transport_match(transport)) {
> +             err = -ESOCKTNOSUPPORT;
> +             goto out;
> +     }
> +     /* Loopback source addresses must route replies back to loopback. */
> +     if (shared && transport == transport_local &&
> +         vsk->local_addr.svm_cid != VMADDR_CID_ANY &&
> +         !vsock_use_local_transport(vsk->local_addr.svm_cid)) {
> +             err = -EADDRNOTAVAIL;
> +             goto out;
> +     }

[Severity: Medium]
Does this loopback source check miss the VMCI exclusions that
vsock_select_transport() applies?

For a VMCI-owned datagram socket in global mode, vsock_select_transport()
routes both owner->get_local_cid() and VMADDR_CID_HOST away from
transport_local even when vsock_use_local_transport() returns true.
Checking only vsock_use_local_transport(vsk->local_addr.svm_cid) here lets
a socket explicitly bound to VMADDR_CID_HOST (when no active G2H CID is
present) or to VMCI's local CID (when VMCI is registered as transport_g2h)
send or connect over transport_local, but replies to that source CID and
connected-peer verification in virtio_transport_dgram_source_allow()
resolve to vmci_transport instead of transport_local, causing connected
loopback receivers to drop the packets.

[ ... ]

> @@ -1461,6 +1660,31 @@ static int vsock_read_skb(struct sock *sk, 
> skb_read_actor_t read_actor)
>       return vsk->transport->read_skb(vsk, read_actor);
>  }
>  
> +static int vsock_dgram_resolve_cid(struct vsock_sock *vsk,
> +                                struct sockaddr_vm *remote_addr,
> +                                bool *vmci_self)
> +{
> +     const struct vsock_transport *owner = vsk->transport;
> +
> +     if (remote_addr->svm_cid != VMADDR_CID_ANY)
> +             return 0;
> +
> +     mutex_lock(&vsock_register_mutex);
> +     if (owner->dgram_fallback && vsock_net_mode_global(vsk) &&
> +         vsk->local_addr.svm_cid == owner->get_local_cid()) {
> +             remote_addr->svm_cid = vsk->local_addr.svm_cid;
> +             *vmci_self = true;
> +     } else if (transport_local || owner->dgram_transport_match) {
> +             remote_addr->svm_cid = VMADDR_CID_LOCAL;
> +     } else {
> +             remote_addr->svm_cid = owner->get_local_cid();
> +             *vmci_self = owner->dgram_fallback;
> +     }
> +     mutex_unlock(&vsock_register_mutex);
> +
> +     return remote_addr->svm_cid == VMADDR_CID_ANY ? -EADDRNOTAVAIL : 0;
> +}

[Severity: Medium]
Should the first branch also check that vsk->local_addr.svm_cid !=
VMADDR_CID_ANY?

vmci_transport_init() registers vmci_transport as the datagram fallback at
module load time even when neither VMCI guest nor host personality is
active, in which case owner->get_local_cid() returns VMCI_INVALID_ID
(VMADDR_CID_ANY).  When a wildcard-bound or auto-bound socket calls
connect() or sendmsg() with remote_addr->svm_cid == VMADDR_CID_ANY,
vsk->local_addr.svm_cid == owner->get_local_cid() matches on
VMADDR_CID_ANY, skipping the transport_local branch and failing with
-EADDRNOTAVAIL even when vsock_loopback is loaded.

>  static int vsock_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
>                              size_t len)
>  {

[ ... ]

> @@ -1493,42 +1717,34 @@ static int vsock_dgram_sendmsg(struct socket *sock, 
> struct msghdr *msg,
>       if (msg->msg_name &&
>           vsock_addr_cast(msg->msg_name, msg->msg_namelen,
>                           &remote_addr) == 0) {
> -             /* Ensure this address is of the right type and is a valid
> -              * destination.
> -              */
> -
> -             if (remote_addr->svm_cid == VMADDR_CID_ANY)
> -                     remote_addr->svm_cid = transport->get_local_cid();
> -
> -             if (!vsock_addr_bound(remote_addr)) {
> -                     err = -EINVAL;
> -                     goto out;
> -             }
> +             connected_addr = *remote_addr;
> +             remote_addr = &connected_addr;
>       } else if (sock->state == SS_CONNECTED) {
> -             remote_addr = &vsk->remote_addr;
> -
> -             if (remote_addr->svm_cid == VMADDR_CID_ANY)
> -                     remote_addr->svm_cid = transport->get_local_cid();
> -
> -             /* XXX Should connect() or this function ensure remote_addr is
> -              * bound?
> -              */
> -             if (!vsock_addr_bound(&vsk->remote_addr)) {
> -                     err = -EINVAL;
> -                     goto out;
> -             }
> +             /* A fallback must not pin subsequent sends to the host. */
> +             connected_addr = vsk->remote_addr;
> +             remote_addr = &connected_addr;
> +             vmci_self = vsk->dgram_peer_is_vmci_self;
>       } else {
>               err = -EINVAL;
>               goto out;
>       }
>  
> -     if (!transport->dgram_allow(vsk, remote_addr->svm_cid,
> -                                 remote_addr->svm_port)) {
> +     err = vsock_dgram_resolve_cid(vsk, remote_addr, &vmci_self);
> +     if (err)
> +             goto out;
> +     if (!vsock_addr_bound(remote_addr)) {
>               err = -EINVAL;
>               goto out;
>       }
>  
> -     err = transport->dgram_enqueue(vsk, remote_addr, msg, len);
> +     transport = vsock_dgram_get_transport(vsk, remote_addr, vmci_self);
> +     if (IS_ERR(transport)) {
> +             err = PTR_ERR(transport);
> +             goto out;
> +     }
> +
> +     err = transport->dgram_enqueue(vsk, transport, remote_addr, msg, len);
> +     module_put(transport->module);

[Severity: Medium]
Can we avoid taking vsock_register_mutex and bouncing module refcounts on
every datagram send and connected receive?

vsock_dgram_sendmsg() and virtio_transport_dgram_source_allow() (on every
incoming datagram for a connected socket, while holding the virtqueue lock
and lock_sock()) now call vsock_dgram_get_transport(), which acquires the
global vsock_register_mutex, does try_module_get()/module_put(), and
performs multiple vhost_vsock_get() lookups per packet, serializing
datagram traffic across all sockets system-wide.

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=6

Reply via email to