Skip to contentSkip to conversation

read vnet_hdr_sz once

PATCH In review Willem de Bruijn 3 participants 6 messages Feb 3, 2017 – Feb 7, 2017

Subsystems (3)
lore
Share & tools
lore

Willem de Bruijn

author maintainer
#0 lore raw

cover read vnet_hdr_sz once

From: Willem de Bruijn <[email protected]>

Tuntap devices allow concurrent use and update of field vnet_hdr_sz. Read the field once to avoid TOCTOU.

Willem de Bruijn (2):
tun: read vnet_hdr_sz once
macvtap: read vnet_hdr_size once

 drivers/net/macvtap.c |  4 ++--
 drivers/net/tun.c     | 10 ++++++----
 2 files changed, 8 insertions(+), 6 deletions(-)
Signature · 1 line
2.11.0.483.g087da7b7c-goog
author maintainer
#1 lore raw

PATCH 1/2 tun: read vnet_hdr_sz once

From: Willem de Bruijn <[email protected]>

When IFF_VNET_HDR is enabled, a virtio_net header must precede data. Data length is verified to be greater than or equal to expected header length tun->vnet_hdr_sz before copying.

Read this value once and cache locally, as it can be updated between the test and use (TOCTOU).

---
 drivers/net/tun.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/net/tun.c +6 -4
index 2cd10b26b650..bfabe180053e 100644
@@ -1170,9 +1170,11 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
 	}
 
 	if (tun->flags & IFF_VNET_HDR) {
-		if (len < tun->vnet_hdr_sz)
+		int vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
+
+		if (len < vnet_hdr_sz)
 			return -EINVAL;
-		len -= tun->vnet_hdr_sz;
+		len -= vnet_hdr_sz;
 
 		if (!copy_from_iter_full(&gso, sizeof(gso), from))
 			return -EFAULT;
@@ -1183,7 +1185,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
 
 		if (tun16_to_cpu(tun, gso.hdr_len) > len)
 			return -EINVAL;
-		iov_iter_advance(from, tun->vnet_hdr_sz - sizeof(gso));
+		iov_iter_advance(from, vnet_hdr_sz - sizeof(gso));
 	}
 
 	if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) {
@@ -1335,7 +1337,7 @@ static ssize_t tun_put_user(struct tun_struct *tun,
 		vlan_hlen = VLAN_HLEN;
 
 	if (tun->flags & IFF_VNET_HDR)
-		vnet_hdr_sz = tun->vnet_hdr_sz;
+		vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
 
 	total = skb->len + vlan_hlen + vnet_hdr_sz;
 
Signature · 1 line
2.11.0.483.g087da7b7c-goog
Acked-by on v1 1/2
#2 lore raw

Re: [PATCH net 1/2] tun: read vnet_hdr_sz once

Quoted 6 lines · Willem de Bruijn

From: Willem de Bruijn <[email protected]>

When IFF_VNET_HDR is enabled, a virtio_net header must precede data. Data length is verified to be greater than or equal to expected header length tun->vnet_hdr_sz before copying.

Quoted 7 lines

Read this value once and cache locally, as it can be updated between the test and use (TOCTOU).

---

author maintainer
#3 lore raw

PATCH 2/2 macvtap: read vnet_hdr_size once

In reply to Willem de Bruijn

From: Willem de Bruijn <[email protected]>

When IFF_VNET_HDR is enabled, a virtio_net header must precede data. Data length is verified to be greater than or equal to expected header length tun->vnet_hdr_sz before copying.

Macvtap functions read the value once, but unless READ_ONCE is used, the compiler may ignore this and read multiple times. Enforce a single read and locally cached value to avoid updates between test and use.

---
 drivers/net/macvtap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/macvtap.c +2 -2
index 402618565838..c27011bbe30c 100644
@@ -681,7 +681,7 @@ static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m,
 	size_t linear;
 
 	if (q->flags & IFF_VNET_HDR) {
-		vnet_hdr_len = q->vnet_hdr_sz;
+		vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
 
 		err = -EINVAL;
 		if (len < vnet_hdr_len)
@@ -820,7 +820,7 @@ static ssize_t macvtap_put_user(struct macvtap_queue *q,
 
 	if (q->flags & IFF_VNET_HDR) {
 		struct virtio_net_hdr vnet_hdr;
-		vnet_hdr_len = q->vnet_hdr_sz;
+		vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
 		if (iov_iter_count(iter) < vnet_hdr_len)
 			return -EINVAL;
 
Signature · 1 line
2.11.0.483.g087da7b7c-goog
Acked-by on v1 2/2
#4 lore raw

Re: [PATCH net 2/2] macvtap: read vnet_hdr_size once

Quoted 6 lines · Willem de Bruijn

From: Willem de Bruijn <[email protected]>

When IFF_VNET_HDR is enabled, a virtio_net header must precede data. Data length is verified to be greater than or equal to expected header length tun->vnet_hdr_sz before copying.

Quoted 7 lines

Macvtap functions read the value once, but unless READ_ONCE is used, the compiler may ignore this and read multiple times. Enforce a single read and locally cached value to avoid updates between test and use.

---

maintainer
#5 lore raw
In reply to Willem de Bruijn

From: Willem de Bruijn <[email protected]>
Date: Fri, 3 Feb 2017 18:20:47 -0500

Quoted 2 lines

Tuntap devices allow concurrent use and update of field vnet_hdr_sz. Read the field once to avoid TOCTOU.

Series applied and queued up for -stable, thanks.