virtio-net: Add validation for used length

Started by Xie Yongji on May 25, 2021 · 6 messages from 3 authors · Last reply May 26, 2021

Subsystems (3)

Maintainers (9)

Show 5 more
STALE2015d

open the first message on its own page

Message details and related content
This adds validation for used length (might come
from an untrusted device) to avoid data corruption
or loss.

Signed-off-by: Xie Yongji <redacted>
---
 drivers/net/virtio_net.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index c4711e23af88..2dcdc1a3c7e8 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -668,6 +668,13 @@ static struct sk_buff *receive_small(struct net_device *dev,
 		void *orig_data;
 		u32 act;
 
+		if (unlikely(len > GOOD_PACKET_LEN)) {
+			pr_debug("%s: rx error: len %u exceeds max size %lu\n",
+				 dev->name, len, GOOD_PACKET_LEN);
+			dev->stats.rx_length_errors++;
+			goto err_xdp;
+		}
+
 		if (unlikely(hdr->hdr.gso_type))
 			goto err_xdp;
 
@@ -739,6 +746,14 @@ static struct sk_buff *receive_small(struct net_device *dev,
 	}
 	rcu_read_unlock();
 
+	if (unlikely(len > GOOD_PACKET_LEN)) {
+		pr_debug("%s: rx error: len %u exceeds max size %lu\n",
+			 dev->name, len, GOOD_PACKET_LEN);
+		dev->stats.rx_length_errors++;
+		put_page(page);
+		return NULL;
+	}
+
 	skb = build_skb(buf, buflen);
 	if (!skb) {
 		put_page(page);
@@ -822,6 +837,13 @@ static struct sk_buff *receive_mergeable(struct net_device *dev,
 		void *data;
 		u32 act;
 
+		if (unlikely(len > truesize)) {
+			pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
+				 dev->name, len, (unsigned long)ctx);
+			dev->stats.rx_length_errors++;
+			goto err_xdp;
+		}
+
 		/* Transient failure which in theory could occur if
 		 * in-flight packets from before XDP was enabled reach
 		 * the receive path after XDP is loaded.
-- 
2.11.0
Message details and related content
在 2021/5/25 下午12:58, Xie Yongji 写道:
quoted hunk
This adds validation for used length (might come
from an untrusted device) to avoid data corruption
or loss.

Signed-off-by: Xie Yongji <redacted>
---
  drivers/net/virtio_net.c | 22 ++++++++++++++++++++++
  1 file changed, 22 insertions(+)
diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index c4711e23af88..2dcdc1a3c7e8 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -668,6 +668,13 @@ static struct sk_buff *receive_small(struct net_device *dev,
  		void *orig_data;
  		u32 act;
  
+		if (unlikely(len > GOOD_PACKET_LEN)) {
+			pr_debug("%s: rx error: len %u exceeds max size %lu\n",
+				 dev->name, len, GOOD_PACKET_LEN);
+			dev->stats.rx_length_errors++;
+			goto err_xdp;
+		}

Need to count vi->hdr_len here?

quoted hunk
+
  		if (unlikely(hdr->hdr.gso_type))
  			goto err_xdp;
  
@@ -739,6 +746,14 @@ static struct sk_buff *receive_small(struct net_device *dev,
  	}
  	rcu_read_unlock();
  
+	if (unlikely(len > GOOD_PACKET_LEN)) {
+		pr_debug("%s: rx error: len %u exceeds max size %lu\n",
+			 dev->name, len, GOOD_PACKET_LEN);
+		dev->stats.rx_length_errors++;
+		put_page(page);
+		return NULL;
+	}
+
  	skb = build_skb(buf, buflen);
  	if (!skb) {
  		put_page(page);
@@ -822,6 +837,13 @@ static struct sk_buff *receive_mergeable(struct net_device *dev,
  		void *data;
  		u32 act;
  
+		if (unlikely(len > truesize)) {
+			pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
+				 dev->name, len, (unsigned long)ctx);
+			dev->stats.rx_length_errors++;
+			goto err_xdp;
+		}

There's a similar check after the XDP, let's simply move it here?

And do we need similar check in receive_big()?

Thanks

quoted hunk
+
  		/* Transient failure which in theory could occur if
  		 * in-flight packets from before XDP was enabled reach
  		 * the receive path after XDP is loaded.
Message details and related content
On Tue, May 25, 2021 at 2:30 PM Jason Wang [off-list ref] wrote:

在 2021/5/25 下午12:58, Xie Yongji 写道:
quoted
This adds validation for used length (might come
from an untrusted device) to avoid data corruption
or loss.

Signed-off-by: Xie Yongji <redacted>
---
  drivers/net/virtio_net.c | 22 ++++++++++++++++++++++
  1 file changed, 22 insertions(+)
diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index c4711e23af88..2dcdc1a3c7e8 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -668,6 +668,13 @@ static struct sk_buff *receive_small(struct net_device *dev,
              void *orig_data;
              u32 act;

+             if (unlikely(len > GOOD_PACKET_LEN)) {
+                     pr_debug("%s: rx error: len %u exceeds max size %lu\n",
+                              dev->name, len, GOOD_PACKET_LEN);
+                     dev->stats.rx_length_errors++;
+                     goto err_xdp;
+             }

Need to count vi->hdr_len here?
We did len -= vi->hdr_len before.
quoted
+
              if (unlikely(hdr->hdr.gso_type))
                      goto err_xdp;
@@ -739,6 +746,14 @@ static struct sk_buff *receive_small(struct net_device *dev,
      }
      rcu_read_unlock();

+     if (unlikely(len > GOOD_PACKET_LEN)) {
+             pr_debug("%s: rx error: len %u exceeds max size %lu\n",
+                      dev->name, len, GOOD_PACKET_LEN);
+             dev->stats.rx_length_errors++;
+             put_page(page);
+             return NULL;
+     }
+
      skb = build_skb(buf, buflen);
      if (!skb) {
              put_page(page);
@@ -822,6 +837,13 @@ static struct sk_buff *receive_mergeable(struct net_device *dev,
              void *data;
              u32 act;

+             if (unlikely(len > truesize)) {
+                     pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
+                              dev->name, len, (unsigned long)ctx);
+                     dev->stats.rx_length_errors++;
+                     goto err_xdp;
+             }

There's a similar check after the XDP, let's simply move it here?
Do we still need that in non-XDP cases?
And do we need similar check in receive_big()?
It seems that the check in page_to_skb() can do similar things.

Thanks,
Yongji
Message details and related content
Hi Xie,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on linus/master]
[also build test WARNING on v5.13-rc3 next-20210525]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Xie-Yongji/virtio-net-Add-validation-for-used-length/20210525-130449
base:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git a050a6d2b7e80ca52b2f4141eaf3420d201b72b3
config: x86_64-randconfig-a005-20210525 (attached as .config)
compiler: clang version 13.0.0 (https://github.com/llvm/llvm-project 99155e913e9bad5f7f8a247f8bb3a3ff3da74af1)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # install x86_64 cross compiling tool for clang build
        # apt-get install binutils-x86-64-linux-gnu
        # https://github.com/0day-ci/linux/commit/9cacb0e306acf93325699401dcae01f77505f017
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Xie-Yongji/virtio-net-Add-validation-for-used-length/20210525-130449
        git checkout 9cacb0e306acf93325699401dcae01f77505f017
        # save the attached .config to linux build tree
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=x86_64 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <redacted>

All warnings (new ones prefixed by >>):
quoted
drivers/net/virtio_net.c:740:22: warning: format specifies type 'unsigned long' but the argument has type 'int' [-Wformat]
                                    dev->name, len, GOOD_PACKET_LEN);
                                                    ^~~~~~~~~~~~~~~
   include/linux/printk.h:424:26: note: expanded from macro 'pr_debug'
           dynamic_pr_debug(fmt, ##__VA_ARGS__)
                            ~~~    ^~~~~~~~~~~
   include/linux/dynamic_debug.h:163:22: note: expanded from macro 'dynamic_pr_debug'
                              pr_fmt(fmt), ##__VA_ARGS__)
                                     ~~~     ^~~~~~~~~~~
   include/linux/dynamic_debug.h:152:56: note: expanded from macro '_dynamic_func_call'
           __dynamic_func_call(__UNIQUE_ID(ddebug), fmt, func, ##__VA_ARGS__)
                                                                 ^~~~~~~~~~~
   include/linux/dynamic_debug.h:134:15: note: expanded from macro '__dynamic_func_call'
                   func(&id, ##__VA_ARGS__);               \
                               ^~~~~~~~~~~
   drivers/net/virtio_net.c:35:25: note: expanded from macro 'GOOD_PACKET_LEN'
   #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   drivers/net/virtio_net.c:820:21: warning: format specifies type 'unsigned long' but the argument has type 'int' [-Wformat]
                            dev->name, len, GOOD_PACKET_LEN);
                                            ^~~~~~~~~~~~~~~
   include/linux/printk.h:424:26: note: expanded from macro 'pr_debug'
           dynamic_pr_debug(fmt, ##__VA_ARGS__)
                            ~~~    ^~~~~~~~~~~
   include/linux/dynamic_debug.h:163:22: note: expanded from macro 'dynamic_pr_debug'
                              pr_fmt(fmt), ##__VA_ARGS__)
                                     ~~~     ^~~~~~~~~~~
   include/linux/dynamic_debug.h:152:56: note: expanded from macro '_dynamic_func_call'
           __dynamic_func_call(__UNIQUE_ID(ddebug), fmt, func, ##__VA_ARGS__)
                                                                 ^~~~~~~~~~~
   include/linux/dynamic_debug.h:134:15: note: expanded from macro '__dynamic_func_call'
                   func(&id, ##__VA_ARGS__);               \
                               ^~~~~~~~~~~
   drivers/net/virtio_net.c:35:25: note: expanded from macro 'GOOD_PACKET_LEN'
   #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   2 warnings generated.


vim +740 drivers/net/virtio_net.c

153 more lines

Message details and related content
在 2021/5/25 下午4:45, Yongji Xie 写道:
On Tue, May 25, 2021 at 2:30 PM Jason Wang [off-list ref] wrote:
quoted
在 2021/5/25 下午12:58, Xie Yongji 写道:
quoted
This adds validation for used length (might come
from an untrusted device) to avoid data corruption
or loss.

Signed-off-by: Xie Yongji <redacted>
---
   drivers/net/virtio_net.c | 22 ++++++++++++++++++++++
   1 file changed, 22 insertions(+)
diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index c4711e23af88..2dcdc1a3c7e8 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -668,6 +668,13 @@ static struct sk_buff *receive_small(struct net_device *dev,
               void *orig_data;
               u32 act;

+             if (unlikely(len > GOOD_PACKET_LEN)) {
+                     pr_debug("%s: rx error: len %u exceeds max size %lu\n",
+                              dev->name, len, GOOD_PACKET_LEN);
+                     dev->stats.rx_length_errors++;
+                     goto err_xdp;
+             }
Need to count vi->hdr_len here?
We did len -= vi->hdr_len before.

Right.

quoted
quoted
+
               if (unlikely(hdr->hdr.gso_type))
                       goto err_xdp;
@@ -739,6 +746,14 @@ static struct sk_buff *receive_small(struct net_device *dev,
       }
       rcu_read_unlock();

+     if (unlikely(len > GOOD_PACKET_LEN)) {
+             pr_debug("%s: rx error: len %u exceeds max size %lu\n",
+                      dev->name, len, GOOD_PACKET_LEN);
+             dev->stats.rx_length_errors++;
+             put_page(page);
+             return NULL;
+     }
+
       skb = build_skb(buf, buflen);
       if (!skb) {
               put_page(page);
@@ -822,6 +837,13 @@ static struct sk_buff *receive_mergeable(struct net_device *dev,
               void *data;
               u32 act;

+             if (unlikely(len > truesize)) {
+                     pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
+                              dev->name, len, (unsigned long)ctx);
+                     dev->stats.rx_length_errors++;
+                     goto err_xdp;
+             }
There's a similar check after the XDP, let's simply move it here?
Do we still need that in non-XDP cases?

I meant we check once for both XDP and non-XDP if we do it before if (xdp_prog)

quoted
And do we need similar check in receive_big()?
It seems that the check in page_to_skb() can do similar things.

Right.

Thanks

Thanks,
Yongji
Message details and related content
On Wed, May 26, 2021 at 3:52 PM Jason Wang [off-list ref] wrote:

在 2021/5/25 下午4:45, Yongji Xie 写道:
quoted
On Tue, May 25, 2021 at 2:30 PM Jason Wang [off-list ref] wrote:
quoted
在 2021/5/25 下午12:58, Xie Yongji 写道:
quoted
This adds validation for used length (might come
from an untrusted device) to avoid data corruption
or loss.

Signed-off-by: Xie Yongji <redacted>
---
   drivers/net/virtio_net.c | 22 ++++++++++++++++++++++
   1 file changed, 22 insertions(+)
diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index c4711e23af88..2dcdc1a3c7e8 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -668,6 +668,13 @@ static struct sk_buff *receive_small(struct net_device *dev,
               void *orig_data;
               u32 act;

+             if (unlikely(len > GOOD_PACKET_LEN)) {
+                     pr_debug("%s: rx error: len %u exceeds max size %lu\n",
+                              dev->name, len, GOOD_PACKET_LEN);
+                     dev->stats.rx_length_errors++;
+                     goto err_xdp;
+             }
Need to count vi->hdr_len here?
We did len -= vi->hdr_len before.

Right.

quoted
quoted
quoted
+

36 more quoted lines

Will do it in v2.

Thanks,
Yongji
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help