Skip to contentSkip to conversation

virtio-net: Verify page list size before fitting into skb

PATCH Applied Sasha Levin 3 participants 14 messages Sep 28, 2011 – Oct 8, 2011

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

PATCH v2 1/2 virtio-net: Verify page list size before fitting into skb

This patch verifies that the length of a buffer stored in a linked list of pages is small enough to fit into a skb.

If the size is larger than a max size of a skb, it means that we shouldn't go ahead building skbs anyway since we won't be able to send the buffer as the user requested.

---
 drivers/net/virtio_net.c |   13 +++++++++++++
 1 files changed, 13 insertions(+), 0 deletions(-)
drivers/net/virtio_net.c +13 -0
index 0c7321c..bde0dec 100644
@@ -195,6 +195,19 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
 	len -= copy;
 	offset += copy;
 
+	/*
+	 * Verify that we can indeed put this data into a skb.
+	 * This is here to handle cases when the device erroneously
+	 * tries to receive more than is possible. This is usually
+	 * the case of a broken device.
+	 */
+	if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
+		if (net_ratelimit())
+			pr_debug("%s: too much data\n", skb->dev->name);
+		dev_kfree_skb(skb);
+		return NULL;
+	}
+
 	while (len) {
 		set_skb_frag(skb, page, offset, &len);
 		page = (struct page *)page->private;
Signature · 1 line
1.7.6.1
#1 lore raw

PATCH v2 2/2 virtio-net: Prevent NULL dereference

This patch prevents a NULL dereference when the user has passed a length longer than an actual buffer to virtio-net.

---
 drivers/net/virtio_net.c |   12 +++++++++++-
 1 files changed, 11 insertions(+), 1 deletions(-)
drivers/net/virtio_net.c +11 -1
index bde0dec..4a53d2a 100644
@@ -208,12 +208,22 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
 		return NULL;
 	}
 
-	while (len) {
+	while (len && page) {
 		set_skb_frag(skb, page, offset, &len);
 		page = (struct page *)page->private;
 		offset = 0;
 	}
 
+	/*
+	 * This is the case where we ran out of pages in our linked list, but
+	 * supposedly have more data to read.
+	 */	
+	if (len > 0) {
+		pr_debug("%s: missing data to assemble skb\n", skb->dev->name);
+		dev_kfree_skb(skb);
+		return NULL;
+	}
+
 	if (page)
 		give_pages(vi, page);
 
Signature · 1 line
1.7.6.1
#2 lore raw

Re: [PATCH v2 2/2] virtio-net: Prevent NULL dereference

Quoted 21 lines · Sasha Levin

This patch prevents a NULL dereference when the user has passed a length longer than an actual buffer to virtio-net.

---
 drivers/net/virtio_net.c |   12 +++++++++++-
 1 files changed, 11 insertions(+), 1 deletions(-)
drivers/net/virtio_net.c +0 -0
index bde0dec..4a53d2a 100644
@@ -208,12 +208,22 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
 		return NULL;
 	}
 

drivers/net/virtio_net.c static struct sk_buff *page_to_skb(struct virtnet_info *vi, · line 219

Quoted 10 lines
drivers/net/virtio_net.c +4 -1
-	while (len) {
+	while (len && page) {
 		set_skb_frag(skb, page, offset, &len);
 		page = (struct page *)page->private;
 		offset = 0;
 	}
 
+	/*
+	 * This is the case where we ran out of pages in our linked list, but
+	 * supposedly have more data to read.

Again, let's clarify that this only happens with broken devices.

Quoted 12 lines
drivers/net/virtio_net.c +7 -0
+	 */	
+	if (len > 0) {
+		pr_debug("%s: missing data to assemble skb\n", skb->dev->name);
+		dev_kfree_skb(skb);
+		return NULL;
+	}
+
 	if (page)
 		give_pages(vi, page);
 
Signature · 1 line
1.7.6.1
#3 lore raw

Re: [PATCH v2 2/2] virtio-net: Prevent NULL dereference

Quoted 29 lines · Michael S. Tsirkin
Quoted 28 lines · Sasha Levin

This patch prevents a NULL dereference when the user has passed a length longer than an actual buffer to virtio-net.

---
 drivers/net/virtio_net.c |   12 +++++++++++-
 1 files changed, 11 insertions(+), 1 deletions(-)
drivers/net/virtio_net.c +1 -1
index bde0dec..4a53d2a 100644
@@ -208,12 +208,22 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
 		return NULL;
 	}
 
-	while (len) {
+	while (len && page) {
 		set_skb_frag(skb, page, offset, &len);
 		page = (struct page *)page->private;
 		offset = 0;
 	}
 
Quoted 5 lines

drivers/net/virtio_net.c static struct sk_buff *page_to_skb(struct virtnet_info *vi, · line 219

Quoted 3 lines
drivers/net/virtio_net.c +3 -0
+	/*
+	 * This is the case where we ran out of pages in our linked list, but
+	 * supposedly have more data to read.

Again, let's clarify that this only happens with broken devices.

I think that the code within the if() makes it clear that it isn't the regular path.

Signature · 1 line
Sasha.
#4 lore raw

Re: [PATCH v2 2/2] virtio-net: Prevent NULL dereference

Quoted 30 lines · Sasha Levin
Quoted 29 lines · Michael S. Tsirkin
Quoted 28 lines · Sasha Levin

This patch prevents a NULL dereference when the user has passed a length longer than an actual buffer to virtio-net.

---
 drivers/net/virtio_net.c |   12 +++++++++++-
 1 files changed, 11 insertions(+), 1 deletions(-)
drivers/net/virtio_net.c +1 -1
index bde0dec..4a53d2a 100644
@@ -208,12 +208,22 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
 		return NULL;
 	}
 
-	while (len) {
+	while (len && page) {
 		set_skb_frag(skb, page, offset, &len);
 		page = (struct page *)page->private;
 		offset = 0;
 	}
 
Quoted 8 lines
Quoted 5 lines

drivers/net/virtio_net.c static struct sk_buff *page_to_skb(struct virtnet_info *vi, · line 219

Quoted 3 lines
drivers/net/virtio_net.c +3 -0
+	/*
+	 * This is the case where we ran out of pages in our linked list, but
+	 * supposedly have more data to read.

Again, let's clarify that this only happens with broken devices.

I think that the code within the if() makes it clear that it isn't the regular path.

It doesn't make it clear that this never happens in absence of bugs.

Quoted 3 lines
Signature · 1 line
Sasha.
#5 lore raw

Re: [PATCH v2 2/2] virtio-net: Prevent NULL dereference

In reply to Sasha Levin
Quoted 18 lines · Sasha Levin

This patch prevents a NULL dereference when the user has passed a length longer than an actual buffer to virtio-net.

---
 drivers/net/virtio_net.c |   12 +++++++++++-
 1 files changed, 11 insertions(+), 1 deletions(-)
drivers/net/virtio_net.c +0 -0
index bde0dec..4a53d2a 100644

drivers/net/virtio_net.c static struct sk_buff *page_to_skb(struct virtnet_info *vi, · line 211

Quoted 5 lines
drivers/net/virtio_net.c +1 -1
@@ -208,12 +208,22 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
 		return NULL;
 	}
 
-	while (len) {
+	while (len && page) {

I think if (unlikely(!page))

		goto err_page;

would make the logic clearer. But see below

Quoted 8 lines
drivers/net/virtio_net.c +3 -0
 		set_skb_frag(skb, page, offset, &len);
 		page = (struct page *)page->private;
 		offset = 0;
 	}
 
+	/*
+	 * This is the case where we ran out of pages in our linked list, but
+	 * supposedly have more data to read.

drivers/net/virtio_net.c static struct sk_buff *page_to_skb(struct virtnet_info *vi, · line 229

Quoted 10 lines
drivers/net/virtio_net.c +7 -0
+	 */	
+	if (len > 0) {
+		pr_debug("%s: missing data to assemble skb\n", skb->dev->name);
+		dev_kfree_skb(skb);
+		return NULL;
+	}
+
 	if (page)
 		give_pages(vi, page);
 

I thought about this some more. If length was too large host is right now writing into pages that we have freed.
That is very bad, and I don't know what do do with it, really not worth prettifying that IMO, NULL pointer is the least of our worries.

But, with mergeable buffers at least, and that is the main mode anyway, there is always a single page per buf, right?
So I think we should change the code,
and for mergeable buffers we shall only verify that
length <= PAGE_SIZE.

IOW copy a bit of code from page_to_skb(vi, page, len) to receive_mergeable, maybe add a common function if we need to avoid duplication.

Quoted 2 lines
Signature · 1 line
1.7.6.1
#6 lore raw

Re: [PATCH v2 2/2] virtio-net: Prevent NULL dereference

Quoted 11 lines · Michael S. Tsirkin

I thought about this some more. If length was too large host is right now writing into pages that we have freed.
That is very bad, and I don't know what do do with it, really not worth prettifying that IMO, NULL pointer is the least of our worries.

But, with mergeable buffers at least, and that is the main mode anyway, there is always a single page per buf, right?
So I think we should change the code,
and for mergeable buffers we shall only verify that
length <= PAGE_SIZE.

In receive_mergeable() we already verify both 'page' being non-null and length:

	if (len > PAGE_SIZE)
		len = PAGE_SIZE;
Quoted 4 lines

IOW copy a bit of code from page_to_skb(vi, page, len) to receive_mergeable, maybe add a common function if we need to avoid duplication.

Signature · 1 line
Sasha.
maintainer
#7 lore raw

Re: [PATCH v2 2/2] virtio-net: Prevent NULL dereference

In reply to Sasha Levin

From: Sasha Levin <[email protected]>
Date: Wed, 28 Sep 2011 17:40:55 +0300

Quoted 9 lines

This patch prevents a NULL dereference when the user has passed a length longer than an actual buffer to virtio-net.

Waiting for a respin of this patch with clarified comments.

maintainer
#8 lore raw
In reply to Sasha Levin

From: Sasha Levin <[email protected]>
Date: Wed, 28 Sep 2011 17:40:54 +0300

Quoted 7 lines

This patch verifies that the length of a buffer stored in a linked list of pages is small enough to fit into a skb.

If the size is larger than a max size of a skb, it means that we shouldn't go ahead building skbs anyway since we won't be able to send the buffer as the user requested.

Quoted 6 lines

Michael, tell me what's happening with these two virtio-net bug fixes.

Thanks.

#9 lore raw
Quoted 17 lines · David Miller

From: Sasha Levin <[email protected]>
Date: Wed, 28 Sep 2011 17:40:54 +0300

Quoted 7 lines

This patch verifies that the length of a buffer stored in a linked list of pages is small enough to fit into a skb.

If the size is larger than a max size of a skb, it means that we shouldn't go ahead building skbs anyway since we won't be able to send the buffer as the user requested.

Quoted 6 lines
Quoted 3 lines

Michael, tell me what's happening with these two virtio-net bug fixes.

Thanks.

Thanks for the reminder.
They are more enhancements than bugfixes, in that both help debug buggy hypervisors. So net-next material.
Patch 1 is ok patch 2 would benefit from a slightly clearer comment.

Signature · 1 line
MST
#10 lore raw
In reply to Sasha Levin
Quoted 28 lines · Sasha Levin

This patch verifies that the length of a buffer stored in a linked list of pages is small enough to fit into a skb.

If the size is larger than a max size of a skb, it means that we shouldn't go ahead building skbs anyway since we won't be able to send the buffer as the user requested.

---
 drivers/net/virtio_net.c |   13 +++++++++++++
 1 files changed, 13 insertions(+), 0 deletions(-)
drivers/net/virtio_net.c +3 -0
index 0c7321c..bde0dec 100644
@@ -195,6 +195,19 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
 	len -= copy;
 	offset += copy;
 
+	/*
+	 * Verify that we can indeed put this data into a skb.
+	 * This is here to handle cases when the device erroneously

drivers/net/virtio_net.c static struct sk_buff *page_to_skb(struct virtnet_info *vi, · line 210

Quoted 10 lines
drivers/net/virtio_net.c +10 -0
+	 * tries to receive more than is possible. This is usually
+	 * the case of a broken device.
+	 */
+	if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
+		if (net_ratelimit())
+			pr_debug("%s: too much data\n", skb->dev->name);
+		dev_kfree_skb(skb);
+		return NULL;
+	}
+

BTW, receive_mergeable does

                        pr_debug("%s: packet too long\n", skb->dev->name);
                        skb->dev->stats.rx_length_errors++;

which makes sense.

Quoted 5 lines
drivers/net/virtio_net.c +0 -0
 	while (len) {
 		set_skb_frag(skb, page, offset, &len);
 		page = (struct page *)page->private;
Signature · 1 line
1.7.6.1
#11 lore raw
Quoted 41 lines · Michael S. Tsirkin
Quoted 28 lines · Sasha Levin

This patch verifies that the length of a buffer stored in a linked list of pages is small enough to fit into a skb.

If the size is larger than a max size of a skb, it means that we shouldn't go ahead building skbs anyway since we won't be able to send the buffer as the user requested.

---
 drivers/net/virtio_net.c |   13 +++++++++++++
 1 files changed, 13 insertions(+), 0 deletions(-)
drivers/net/virtio_net.c +3 -0
index 0c7321c..bde0dec 100644
@@ -195,6 +195,19 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
 	len -= copy;
 	offset += copy;
 
+	/*
+	 * Verify that we can indeed put this data into a skb.
+	 * This is here to handle cases when the device erroneously

drivers/net/virtio_net.c static struct sk_buff *page_to_skb(struct virtnet_info *vi, · line 210

Quoted 10 lines
drivers/net/virtio_net.c +10 -0
+	 * tries to receive more than is possible. This is usually
+	 * the case of a broken device.
+	 */
+	if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
+		if (net_ratelimit())
+			pr_debug("%s: too much data\n", skb->dev->name);
+		dev_kfree_skb(skb);
+		return NULL;
+	}
+

BTW, receive_mergeable does

Quoted 4 lines
                        pr_debug("%s: packet too long\n", skb->dev->name);
                        skb->dev->stats.rx_length_errors++;

which makes sense.

Do you think we should increase rx_length_errors here as well?

Signature · 1 line
Sasha.
#12 lore raw
Quoted 45 lines · Sasha Levin
Quoted 44 lines · Michael S. Tsirkin
Quoted 28 lines · Sasha Levin

This patch verifies that the length of a buffer stored in a linked list of pages is small enough to fit into a skb.

If the size is larger than a max size of a skb, it means that we shouldn't go ahead building skbs anyway since we won't be able to send the buffer as the user requested.

---
 drivers/net/virtio_net.c |   13 +++++++++++++
 1 files changed, 13 insertions(+), 0 deletions(-)
drivers/net/virtio_net.c +3 -0
index 0c7321c..bde0dec 100644
@@ -195,6 +195,19 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
 	len -= copy;
 	offset += copy;
 
+	/*
+	 * Verify that we can indeed put this data into a skb.
+	 * This is here to handle cases when the device erroneously

drivers/net/virtio_net.c static struct sk_buff *page_to_skb(struct virtnet_info *vi, · line 210

Quoted 10 lines
drivers/net/virtio_net.c +10 -0
+	 * tries to receive more than is possible. This is usually
+	 * the case of a broken device.
+	 */
+	if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
+		if (net_ratelimit())
+			pr_debug("%s: too much data\n", skb->dev->name);
+		dev_kfree_skb(skb);
+		return NULL;
+	}
+

BTW, receive_mergeable does

                        pr_debug("%s: packet too long\n", skb->dev->name);
                        skb->dev->stats.rx_length_errors++;
Quoted 3 lines
Quoted 1 line

which makes sense.

Do you think we should increase rx_length_errors here as well?

this is all debugging tool for devices/drivers, right? so maybe not worth the noise.

Quoted 3 lines
Signature · 1 line
Sasha.
maintainer Applied
#13 lore raw
In reply to Sasha Levin

From: Sasha Levin <[email protected]>
Date: Wed, 28 Sep 2011 17:40:54 +0300

Quoted 7 lines

This patch verifies that the length of a buffer stored in a linked list of pages is small enough to fit into a skb.

If the size is larger than a max size of a skb, it means that we shouldn't go ahead building skbs anyway since we won't be able to send the buffer as the user requested.

Quoted 6 lines

Applied to net-next