Skip to content

Commit e1edab8

Browse files
wdebruijdavem330
authored andcommitted
tun: read vnet_hdr_sz once
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). Signed-off-by: Willem de Bruijn <[email protected]> Reported-by: Dmitry Vyukov <[email protected]> CC: Eric Dumazet <[email protected]> Acked-by: Eric Dumazet <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent ccf7abb commit e1edab8

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

drivers/net/tun.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,9 +1170,11 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
11701170
}
11711171

11721172
if (tun->flags & IFF_VNET_HDR) {
1173-
if (len < tun->vnet_hdr_sz)
1173+
int vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
1174+
1175+
if (len < vnet_hdr_sz)
11741176
return -EINVAL;
1175-
len -= tun->vnet_hdr_sz;
1177+
len -= vnet_hdr_sz;
11761178

11771179
if (!copy_from_iter_full(&gso, sizeof(gso), from))
11781180
return -EFAULT;
@@ -1183,7 +1185,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
11831185

11841186
if (tun16_to_cpu(tun, gso.hdr_len) > len)
11851187
return -EINVAL;
1186-
iov_iter_advance(from, tun->vnet_hdr_sz - sizeof(gso));
1188+
iov_iter_advance(from, vnet_hdr_sz - sizeof(gso));
11871189
}
11881190

11891191
if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) {
@@ -1335,7 +1337,7 @@ static ssize_t tun_put_user(struct tun_struct *tun,
13351337
vlan_hlen = VLAN_HLEN;
13361338

13371339
if (tun->flags & IFF_VNET_HDR)
1338-
vnet_hdr_sz = tun->vnet_hdr_sz;
1340+
vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
13391341

13401342
total = skb->len + vlan_hlen + vnet_hdr_sz;
13411343

0 commit comments

Comments
 (0)