virtio-net: Verify page list size before fitting into skb
Subsystems (4)
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
@@ -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
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
@@ -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
David Miller
maintainerFrom: 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
Cc Rusty Russell <[email protected]>Cc "Michael S. Tsirkin" <[email protected]>Signed-off-by Sasha Levin <[email protected]>
Michael, tell me what's happening with these two virtio-net bug fixes.
Thanks.
Michael S. Tsirkin
maintainerRe: [PATCH v2 2/2] virtio-net: Prevent NULL dereference
In reply to Sasha LevinQuoted 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.
Cc Rusty Russell <[email protected]>Cc "Michael S. Tsirkin" <[email protected]>Signed-off-by Sasha Levin <[email protected]>--- drivers/net/virtio_net.c | 12 +++++++++++- 1 files changed, 11 insertions(+), 1 deletions(-)drivers/net/virtio_net.c +0
-0@@ -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
Michael S. Tsirkin
maintainerQuoted 17 lines · David Miller
From: Sasha Levin <[email protected]>
Date: Wed, 28 Sep 2011 17:40:54 +0300Quoted 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
Cc Rusty Russell <[email protected]>Cc "Michael S. Tsirkin" <[email protected]>Signed-off-by Sasha Levin <[email protected]>
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
Michael S. Tsirkin
maintainerQuoted 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.
Cc Rusty Russell <[email protected]>Cc "Michael S. Tsirkin" <[email protected]>Signed-off-by Sasha Levin <[email protected]>--- drivers/net/virtio_net.c | 13 +++++++++++++ 1 files changed, 13 insertions(+), 0 deletions(-)drivers/net/virtio_net.c +3
-0@@ -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
-0while (len) {set_skb_frag (skb, page, offset, &len); page = (struct page *)page->private;Signature · 1 line
1.7.6.1
Michael S. Tsirkin
maintainerRe: [PATCH v2 2/2] virtio-net: Prevent NULL dereference
In reply to Sasha LevinQuoted 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.
Cc Rusty Russell <[email protected]>Cc "Michael S. Tsirkin" <[email protected]>Signed-off-by Sasha Levin <[email protected]>--- drivers/net/virtio_net.c | 12 +++++++++++- 1 files changed, 11 insertions(+), 1 deletions(-)drivers/net/virtio_net.c +0
-0
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
-0set_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
Re: [PATCH v2 2/2] virtio-net: Prevent NULL dereference
In reply to Michael S. TsirkinQuoted 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.
Cc Rusty Russell <[email protected]>Cc "Michael S. Tsirkin" <[email protected]>Signed-off-by Sasha Levin <[email protected]>--- drivers/net/virtio_net.c | 12 +++++++++++- 1 files changed, 11 insertions(+), 1 deletions(-)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) {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.
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.
Cc Rusty Russell <[email protected]>Cc "Michael S. Tsirkin" <[email protected]>Signed-off-by Sasha Levin <[email protected]>--- drivers/net/virtio_net.c | 13 +++++++++++++ 1 files changed, 13 insertions(+), 0 deletions(-)drivers/net/virtio_net.c +3
-0@@ -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_mergeabledoes
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.
Michael S. Tsirkin
maintainerQuoted 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.
Cc Rusty Russell <[email protected]>Cc "Michael S. Tsirkin" <[email protected]>Signed-off-by Sasha Levin <[email protected]>--- drivers/net/virtio_net.c | 13 +++++++++++++ 1 files changed, 13 insertions(+), 0 deletions(-)drivers/net/virtio_net.c +3
-0@@ -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_mergeabledoespr_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_errorshere as well?
this is all debugging tool for devices/drivers, right? so maybe not worth the noise.
Quoted 3 lines
Signature · 1 line
Sasha.
Michael S. Tsirkin
maintainerRe: [PATCH v2 2/2] virtio-net: Prevent NULL dereference
In reply to Sasha LevinQuoted 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.
Cc Rusty Russell <[email protected]>Cc "Michael S. Tsirkin" <[email protected]>Signed-off-by Sasha Levin <[email protected]>--- drivers/net/virtio_net.c | 12 +++++++++++- 1 files changed, 11 insertions(+), 1 deletions(-)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) {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.
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
Cc Rusty Russell <[email protected]>Cc "Michael S. Tsirkin" <[email protected]>Signed-off-by Sasha Levin <[email protected]>
Applied to net-next
David Miller
maintainerRe: [PATCH v2 2/2] virtio-net: Prevent NULL dereference
In reply to Sasha LevinFrom: 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.
Cc Rusty Russell <[email protected]>Cc "Michael S. Tsirkin" <[email protected]>Signed-off-by Sasha Levin <[email protected]>
Waiting for a respin of this patch with clarified comments.
Re: [PATCH v2 2/2] virtio-net: Prevent NULL dereference
In reply to Michael S. TsirkinQuoted 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)toreceive_mergeable, maybe add a common function if we need to avoid duplication.
Signature · 1 line
Sasha.