Skip to contentSkip to conversation

hv_netvsc: Add (more) validation for untrusted Hyper-V values

PATCH Applied Andrea Parri (Microsoft) 4 participants 6 messages Jan 14, 2021 – Jan 19, 2021

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 hv_netvsc: Add (more) 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. 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.

Cc "David S. Miller" <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Song Liu <[email protected]>
Cc Yonghong Song <[email protected]>
Cc John Fastabend <[email protected]>
Cc KP Singh <[email protected]>

---
Applies to 5.11-rc3 (and hyperv-next).

Changes since v1 (Juan Vazquez):
- Improve validation in rndis_set_link_state() and rndis_get_ppi()
- Remove memory/skb leak in netvsc_alloc_recv_skb()

 drivers/net/hyperv/netvsc.c       |   3 +-
 drivers/net/hyperv/netvsc_bpf.c   |   6 ++
 drivers/net/hyperv/netvsc_drv.c   |  18 +++-
 drivers/net/hyperv/rndis_filter.c | 171 +++++++++++++++++++-----------
 4 files changed, 136 insertions(+), 62 deletions(-)
drivers/net/hyperv/netvsc.c +2 -1
index 1510a236aa341..d9324961e0d64 100644
@@ -887,6 +887,7 @@ static inline int netvsc_send_pkt(
 	int ret;
 	u32 ring_avail = hv_get_avail_to_write_percent(&out_channel->outbound);
 
+	memset(&nvmsg, 0, sizeof(struct nvsp_message));
 	nvmsg.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT;
 	if (skb)
 		rpkt->channel_type = 0;		/* 0 is RMC_DATA */
@@ -1306,7 +1307,7 @@ static void netvsc_send_table(struct net_device *ndev,
 			 sizeof(union nvsp_6_message_uber);
 
 	/* Boundary check for all versions */
-	if (offset > msglen - count * sizeof(u32)) {
+	if (msglen < count * sizeof(u32) || offset > msglen - count * sizeof(u32)) {
 		netdev_err(ndev, "Received send-table offset too big:%u\n",
 			   offset);
 		return;
drivers/net/hyperv/netvsc_bpf.c +6 -0
index 440486d9c999e..11f0588a88843 100644
@@ -37,6 +37,12 @@ u32 netvsc_run_xdp(struct net_device *ndev, struct netvsc_channel *nvchan,
 	if (!prog)
 		goto out;
 
+	/* Ensure that the below memcpy() won't overflow the page buffer. */
+	if (len > ndev->mtu + ETH_HLEN) {
+		act = XDP_DROP;
+		goto out;
+	}
+
 	/* allocate page buffer for data */
 	page = alloc_page(GFP_ATOMIC);
 	if (!page) {
drivers/net/hyperv/netvsc_drv.c +17 -1
index f32f28311d573..e5501c1a0cbd4 100644
@@ -760,6 +760,16 @@ void netvsc_linkstatus_callback(struct net_device *net,
 	if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) {
 		u32 speed;
 
+		/* Validate status_buf_offset */
+		if (indicate->status_buflen < sizeof(speed) ||
+		    indicate->status_buf_offset < sizeof(*indicate) ||
+		    resp->msg_len - RNDIS_HEADER_SIZE < indicate->status_buf_offset ||
+		    resp->msg_len - RNDIS_HEADER_SIZE - indicate->status_buf_offset
+				< indicate->status_buflen) {
+			netdev_err(net, "invalid rndis_indicate_status packet\n");
+			return;
+		}
+
 		speed = *(u32 *)((void *)indicate
 				 + indicate->status_buf_offset) / 10000;
 		ndev_ctx->speed = speed;
@@ -865,8 +875,14 @@ static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
 	 */
 	if (csum_info && csum_info->receive.ip_checksum_value_invalid &&
 	    csum_info->receive.ip_checksum_succeeded &&
-	    skb->protocol == htons(ETH_P_IP))
+	    skb->protocol == htons(ETH_P_IP)) {
+		/* Check that there is enough space to hold the IP header. */
+		if (skb_headlen(skb) < sizeof(struct iphdr)) {
+			kfree_skb(skb);
+			return NULL;
+		}
 		netvsc_comp_ipcsum(skb);
+	}
 
 	/* Do L4 checksum offload if enabled and present. */
 	if (csum_info && (net->features & NETIF_F_RXCSUM)) {
drivers/net/hyperv/rndis_filter.c +111 -60

252 lines not on this page · raw message

Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 21 lines · On Thu, 14 Jan 2021 21:26:28 +0100 Andrea Parri (Microsoft) wrote:

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. 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.

Cc "David S. Miller" <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Song Liu <[email protected]>
Cc Yonghong Song <[email protected]>
Cc John Fastabend <[email protected]>
Cc KP Singh <[email protected]>
Quoted 2 lines

---
Applies to 5.11-rc3 (and hyperv-next).

So this is for hyperv-next or should we take it via netdev trees?

Quoted 3 lines

Changes since v1 (Juan Vazquez):
- Improve validation in rndis_set_link_state() and rndis_get_ppi()
- Remove memory/skb leak in netvsc_alloc_recv_skb()

#2 lore raw
Quoted 25 lines · Jakub Kicinski
Quoted 21 lines · On Thu, 14 Jan 2021 21:26:28 +0100 Andrea Parri (Microsoft) wrote:

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. 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.

Cc "David S. Miller" <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Song Liu <[email protected]>
Cc Yonghong Song <[email protected]>
Cc John Fastabend <[email protected]>
Cc KP Singh <[email protected]>
Quoted 2 lines

---
Applies to 5.11-rc3 (and hyperv-next).

Quoted 1 line

So this is for hyperv-next or should we take it via netdev trees?

No preference, either way is good for me.

Thanks,
Andrea

maintainer
#3 lore raw
Quoted 26 lines · Andrea Parri
Quoted 25 lines · Jakub Kicinski
Quoted 23 lines · On Thu, 14 Jan 2021 21:26:28 +0100 Andrea Parri (Microsoft) wrote:

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. 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.

Cc "David S. Miller" <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Song Liu <[email protected]>
Cc Yonghong Song <[email protected]>
Cc John Fastabend <[email protected]>
Cc KP Singh <[email protected]>

---
Applies to 5.11-rc3 (and hyperv-next).

Quoted 3 lines
Quoted 1 line

So this is for hyperv-next or should we take it via netdev trees?

No preference, either way is good for me.

To be clear: There is no dependency on any patch in hyperv-next, right?

That's my understanding, but I would like to confirm it.

Wei.

Quoted 3 lines

Thanks,
Andrea

#4 lore raw
Quoted 27 lines · Wei Liu
Quoted 26 lines · Andrea Parri
Quoted 25 lines · Jakub Kicinski
Quoted 23 lines · On Thu, 14 Jan 2021 21:26:28 +0100 Andrea Parri (Microsoft) wrote:

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. 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.

Cc "David S. Miller" <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Song Liu <[email protected]>
Cc Yonghong Song <[email protected]>
Cc John Fastabend <[email protected]>
Cc KP Singh <[email protected]>

---
Applies to 5.11-rc3 (and hyperv-next).

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

So this is for hyperv-next or should we take it via netdev trees?

No preference, either way is good for me.

To be clear: There is no dependency on any patch in hyperv-next, right?

That's my understanding, but I would like to confirm it.

Well, I wrote that this *applies* to hyperv-next... but that's indeed the only 'dependency' I can think of.

Hope this helps.

Thanks,
Andrea

#5 lore raw
In reply to Andrea Parri (Microsoft)
This patch was applied to netdev/net-next.git (refs/heads/master):

Hello:

This patch was applied to netdev/net-next.git (refs/heads/master):

Quoted 8 lines · On Thu, 14 Jan 2021 21:26:28 +0100 you wrote:

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. 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.

[...]

Here is the summary with links:
- [v2] hv_netvsc: Add (more) validation for untrusted Hyper-V values

    https://git.kernel.org/netdev/net-next/c/505e3f00c3f3

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html