Skip to contentSkip to conversation

hv_netvsc: Add validation for untrusted Hyper-V values

PATCH Superseded Andres Beltran 4 participants 4 messages Jul 28, 2020 – Aug 14, 2020

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hv_netvsc: Add validation for untrusted Hyper-V values

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 indexing off the end of an array, or subvert an existing validation via integer overflow. Ensure that outgoing packets do not have any leftover guest memory that has not been zeroed out.

---
 drivers/net/hyperv/hyperv_net.h   |  4 ++
 drivers/net/hyperv/netvsc.c       | 99 +++++++++++++++++++++++++++----
 drivers/net/hyperv/netvsc_drv.c   |  7 +++
 drivers/net/hyperv/rndis_filter.c | 73 ++++++++++++++++++++---
 4 files changed, 163 insertions(+), 20 deletions(-)
drivers/net/hyperv/hyperv_net.h +4 -0
index f43b614f2345..7df5943fa46f 100644
@@ -860,6 +860,10 @@ static inline u32 netvsc_rqstor_size(unsigned long ringbytes)
 	       ringbytes / NETVSC_MIN_IN_MSG_SIZE;
 }
 
+#define NETVSC_XFER_HEADER_SIZE(rng_cnt) \
+		(offsetof(struct vmtransfer_page_packet_header, ranges) + \
+		(rng_cnt) * sizeof(struct vmtransfer_page_range))
+
 struct multi_send_data {
 	struct sk_buff *skb; /* skb containing the pkt */
 	struct hv_netvsc_packet *pkt; /* netvsc pkt pending */
drivers/net/hyperv/netvsc.c +86 -13

203 lines not on this page · raw message

drivers/net/hyperv/netvsc_drv.c +7 -0
index 6267f706e8ee..aa0268495229 100644
@@ -724,6 +724,13 @@ void netvsc_linkstatus_callback(struct net_device *net,
 	struct netvsc_reconfig *event;
 	unsigned long flags;
 
+	/* Ensure the packet is big enough to access its fields */
+	if (resp->msg_len - RNDIS_HEADER_SIZE < sizeof(struct rndis_indicate_status)) {
+		netdev_err(net, "invalid rndis_indicate_status packet, len: %u\n",
+			   resp->msg_len);
+		return;
+	}
+
 	/* Update the physical link speed when changing to another vSwitch */
 	if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) {
 		u32 speed;
drivers/net/hyperv/rndis_filter.c +66 -7

124 lines not on this page · raw message

Signature · 1 line
2.25.1
maintainer
#1 lore raw

From: Andres Beltran <[email protected]>
Date: Tue, 28 Jul 2020 18:53:21 -0400

Quoted 8 lines

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 indexing off the end of an array, or subvert an existing validation via integer overflow. Ensure that outgoing packets do not have any leftover guest memory that has not been zeroed out.

Quoted 4 lines

I need hyperv maintainer reviews before I will apply this.

Thank you.

maintainer
#2 lore raw
In reply to Andres Beltran
Quoted 99 lines

-----Original Message-----
From: Andres Beltran <[email protected]>
Sent: Tuesday, July 28, 2020 6:53 PM
To: KY Srinivasan <[email protected]>; Haiyang Zhang <[email protected]>; Stephen Hemminger <[email protected]>; [email protected]
Cc: [email protected]; [email protected]; Michael Kelley <[email protected]>; [email protected]; Saruhan Karademir <[email protected]>; Andres Beltran <[email protected]>; David S . Miller <[email protected]>; Jakub Kicinski <[email protected]>; [email protected]
Subject: [PATCH] hv_netvsc: Add validation for untrusted Hyper-V values

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 indexing off the end of an array, or subvert an existing validation via integer overflow. Ensure that outgoing packets do not have any leftover guest memory that has not been zeroed out.

---
 drivers/net/hyperv/hyperv_net.h   |  4 ++
 drivers/net/hyperv/netvsc.c       | 99 +++++++++++++++++++++++++++----
 drivers/net/hyperv/netvsc_drv.c   |  7 +++
 drivers/net/hyperv/rndis_filter.c | 73 ++++++++++++++++++++---
 4 files changed, 163 insertions(+), 20 deletions(-)
drivers/net/hyperv/hyperv_net.h +4 -0
index f43b614f2345..7df5943fa46f 100644
@@ -860,6 +860,10 @@ static inline u32 netvsc_rqstor_size(unsigned long
 ringbytes)
 	       ringbytes / NETVSC_MIN_IN_MSG_SIZE;
 }
 
+#define NETVSC_XFER_HEADER_SIZE(rng_cnt) \
+		(offsetof(struct vmtransfer_page_packet_header, ranges) + \
+		(rng_cnt) * sizeof(struct vmtransfer_page_range))
+
 struct multi_send_data {
 	struct sk_buff *skb; /* skb containing the pkt */
drivers/net/hyperv/hyperv_net.h +0 -0
 	struct hv_netvsc_packet *pkt; /* netvsc pkt pending */
drivers/net/hyperv/netvsc.c +2 -0
index 79b907a29433..7aa5276a1f36 100644
@@ -398,6 +398,15 @@ static int netvsc_init_buf(struct hv_device *device,
 	net_device->recv_section_size = resp->sections[0].sub_alloc_size;
 	net_device->recv_section_cnt = resp->sections[0].num_sub_allocs;
 
+	/* Ensure buffer will not overflow */
+	if (net_device->recv_section_size < NETVSC_MTU_MIN ||

(u64)net_device->recv_section_size *

drivers/net/hyperv/netvsc.c +13 -0
+	    (u64)net_device->recv_section_cnt > (u64)buf_size) {
+		netdev_err(ndev, "invalid recv_section_size %u\n",
+			   net_device->recv_section_size);
+		ret = -EINVAL;
+		goto cleanup;
+	}
+
 	/* Setup receive completion ring.
 	 * Add 1 to the recv_section_cnt because at least one entry in a
 	 * ring buffer has to be empty.
@@ -479,6 +488,12 @@ static int netvsc_init_buf(struct hv_device *device,
 	/* Parse the response */
 	net_device->send_section_size = init_packet->msg.
 				v1_msg.send_send_buf_complete.section_size;
+	if (net_device->send_section_size < NETVSC_MTU_MIN) {
+		netdev_err(ndev, "invalid send_section_size %u\n",
+			   net_device->send_section_size);
+		ret = -EINVAL;
+		goto cleanup;
+	}
 
 	/* Section count is simply the size divided by the section size. */
 	net_device->send_section_cnt = buf_size / net_device-
Quoted 1 line

send_section_size;

drivers/net/hyperv/netvsc.c +7 -0
@@ -770,12 +785,24 @@ static void netvsc_send_completion(struct
 net_device *ndev,
 				   int budget)
 {
 	const struct nvsp_message *nvsp_packet = hv_pkt_data(desc);
+	u32 msglen = hv_pkt_datalen(desc);
+
+	/* Ensure packet is big enough to read header fields */
+	if (msglen < sizeof(struct nvsp_message_header)) {
+		netdev_err(ndev, "nvsp_message length too small: %u\n",
 msglen);
+		return;
+	}
 

drivers/net/hyperv/netvsc.c static void netvsc_send_completion(struct · line 807

Quoted 10 lines
drivers/net/hyperv/netvsc.c +5 -0
 	switch (nvsp_packet->hdr.msg_type) {
 	case NVSP_MSG_TYPE_INIT_COMPLETE:
 	case NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE:
 	case NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE:
 	case NVSP_MSG5_TYPE_SUBCHANNEL:
+		if (msglen < sizeof(struct nvsp_message)) {
+			netdev_err(ndev, "nvsp_msg5 length too small: %u\n",
+				   msglen);
+			return;
+		}

struct nvsp_message includes all message types, so its length is the longest type, The messages from older host version are not necessarily reaching the sizeof(struct nvsp_message).

Testing on both new and older hosts are recommended, in case I didn't find out all issues like this one.

Thanks,
- Haiyang

#3 lore raw

Hi Haiyang,

[I'm resuming this work by Andres. Sorry for the delay.]

Quoted 6 lines
Quoted 6 lines
drivers/net/hyperv/netvsc.c +1 -0
 	switch (nvsp_packet->hdr.msg_type) {
 	case NVSP_MSG_TYPE_INIT_COMPLETE:
 	case NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE:
 	case NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE:
 	case NVSP_MSG5_TYPE_SUBCHANNEL:
+		if (msglen < sizeof(struct nvsp_message)) {
Quoted 8 lines

drivers/net/hyperv/netvsc.c static void netvsc_send_completion(struct net_device *ndev, · line 805

Quoted 4 lines
drivers/net/hyperv/netvsc.c +4 -0
+			netdev_err(ndev, "nvsp_msg5 length too small: %u\n",
+				   msglen);
+			return;
+		}

struct nvsp_message includes all message types, so its length is the longest type, The messages from older host version are not necessarily reaching the sizeof(struct nvsp_message).

I split the check above into several checks, one for each "case", using (what I understand are) the corresponding structures/sizeofs...

Quoted 3 lines

Testing on both new and older hosts are recommended, in case I didn't find out all issues like this one.

Sure, will do.

Thanks,
Andrea