read vnet_hdr_sz once
Subsystems (3)
Willem de Bruijn
maintainercover 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
Willem de Bruijn
maintainerPATCH 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
@@ -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
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_HDRis enabled, avirtio_netheader must precede data. Data length is verified to be greater than or equal to expected header lengthtun->vnet_hdr_szbefore copying.
Quoted 7 lines
Read this value once and cache locally, as it can be updated between the test and use (TOCTOU).
Signed-off-by Willem de Bruijn <[email protected]>Reported-by Dmitry Vyukov <[email protected]>CC Eric Dumazet <[email protected]>---
Willem de Bruijn
maintainerPATCH 2/2 macvtap: read vnet_hdr_size once
In reply to Willem de BruijnFrom: 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
@@ -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
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_HDRis enabled, avirtio_netheader must precede data. Data length is verified to be greater than or equal to expected header lengthtun->vnet_hdr_szbefore copying.
Quoted 7 lines
Macvtap functions read the value once, but unless
READ_ONCEis 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.Signed-off-by Willem de Bruijn <[email protected]>Suggested-by Eric Dumazet <[email protected]>---
David Miller
maintainerFrom: 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.