hv_sock: Add validation for untrusted Hyper-V values
authorAndrea Parri (Microsoft) <parri.andrea@gmail.com>
Thu, 28 Apr 2022 14:51:05 +0000 (16:51 +0200)
committerWei Liu <wei.liu@kernel.org>
Thu, 28 Apr 2022 15:01:14 +0000 (15:01 +0000)
For additional robustness in the face of Hyper-V errors or malicious
behavior, validate all values that originate from packets that Hyper-V
has sent to the guest in the host-to-guest ring buffer.  Ensure that
invalid values cannot cause data being copied out of the bounds of the
source buffer in hvs_stream_dequeue().

Signed-off-by: Andrea Parri (Microsoft) <parri.andrea@gmail.com>
Reviewed-by: Michael Kelley <mikelley@microsoft.com>
Reviewed-by: Stefano Garzarella <sgarzare@redhat.com>
Link: https://lore.kernel.org/r/20220428145107.7878-4-parri.andrea@gmail.com
Signed-off-by: Wei Liu <wei.liu@kernel.org>
include/linux/hyperv.h
net/vmw_vsock/hyperv_transport.c

index 460a716f47486f92369a32c778261b031cd1ddf5..a01c9fd0a3348ed4559f3924055ef58e3b22026f 100644 (file)
@@ -1696,6 +1696,11 @@ static inline u32 hv_pkt_datalen(const struct vmpacket_descriptor *desc)
        return (desc->len8 << 3) - (desc->offset8 << 3);
 }
 
+/* Get packet length associated with descriptor */
+static inline u32 hv_pkt_len(const struct vmpacket_descriptor *desc)
+{
+       return desc->len8 << 3;
+}
 
 struct vmpacket_descriptor *
 hv_pkt_iter_first_raw(struct vmbus_channel *channel);
index 8c37d07017fc4cee44a21c546277bd58c3b93589..fd98229e3db30988b983761b9c809ce059d6d9b2 100644 (file)
@@ -577,12 +577,18 @@ static bool hvs_dgram_allow(u32 cid, u32 port)
 static int hvs_update_recv_data(struct hvsock *hvs)
 {
        struct hvs_recv_buf *recv_buf;
-       u32 payload_len;
+       u32 pkt_len, payload_len;
+
+       pkt_len = hv_pkt_len(hvs->recv_desc);
+
+       if (pkt_len < HVS_HEADER_LEN)
+               return -EIO;
 
        recv_buf = (struct hvs_recv_buf *)(hvs->recv_desc + 1);
        payload_len = recv_buf->hdr.data_size;
 
-       if (payload_len > HVS_MTU_SIZE)
+       if (payload_len > pkt_len - HVS_HEADER_LEN ||
+           payload_len > HVS_MTU_SIZE)
                return -EIO;
 
        if (payload_len == 0)