d5b7da2
From 4d6fa57b4dab0d77f4d8e9d9c73d1e63f6fe8fee Mon Sep 17 00:00:00 2001
d5b7da2
From: "Jason A. Donenfeld" <Jason@zx2c4.com>
d5b7da2
Date: Fri, 21 Apr 2017 23:14:48 +0200
d5b7da2
Subject: macsec: avoid heap overflow in skb_to_sgvec
d5b7da2
d5b7da2
While this may appear as a humdrum one line change, it's actually quite
d5b7da2
important. An sk_buff stores data in three places:
d5b7da2
d5b7da2
1. A linear chunk of allocated memory in skb->data. This is the easiest
d5b7da2
   one to work with, but it precludes using scatterdata since the memory
d5b7da2
   must be linear.
d5b7da2
2. The array skb_shinfo(skb)->frags, which is of maximum length
d5b7da2
   MAX_SKB_FRAGS. This is nice for scattergather, since these fragments
d5b7da2
   can point to different pages.
d5b7da2
3. skb_shinfo(skb)->frag_list, which is a pointer to another sk_buff,
d5b7da2
   which in turn can have data in either (1) or (2).
d5b7da2
d5b7da2
The first two are rather easy to deal with, since they're of a fixed
d5b7da2
maximum length, while the third one is not, since there can be
d5b7da2
potentially limitless chains of fragments. Fortunately dealing with
d5b7da2
frag_list is opt-in for drivers, so drivers don't actually have to deal
d5b7da2
with this mess. For whatever reason, macsec decided it wanted pain, and
d5b7da2
so it explicitly specified NETIF_F_FRAGLIST.
d5b7da2
d5b7da2
Because dealing with (1), (2), and (3) is insane, most users of sk_buff
d5b7da2
doing any sort of crypto or paging operation calls a convenient function
d5b7da2
called skb_to_sgvec (which happens to be recursive if (3) is in use!).
d5b7da2
This takes a sk_buff as input, and writes into its output pointer an
d5b7da2
array of scattergather list items. Sometimes people like to declare a
d5b7da2
fixed size scattergather list on the stack; othertimes people like to
d5b7da2
allocate a fixed size scattergather list on the heap. However, if you're
d5b7da2
doing it in a fixed-size fashion, you really shouldn't be using
d5b7da2
NETIF_F_FRAGLIST too (unless you're also ensuring the sk_buff and its
d5b7da2
frag_list children arent't shared and then you check the number of
d5b7da2
fragments in total required.)
d5b7da2
d5b7da2
Macsec specifically does this:
d5b7da2
d5b7da2
        size += sizeof(struct scatterlist) * (MAX_SKB_FRAGS + 1);
d5b7da2
        tmp = kmalloc(size, GFP_ATOMIC);
d5b7da2
        *sg = (struct scatterlist *)(tmp + sg_offset);
d5b7da2
	...
d5b7da2
        sg_init_table(sg, MAX_SKB_FRAGS + 1);
d5b7da2
        skb_to_sgvec(skb, sg, 0, skb->len);
d5b7da2
d5b7da2
Specifying MAX_SKB_FRAGS + 1 is the right answer usually, but not if you're
d5b7da2
using NETIF_F_FRAGLIST, in which case the call to skb_to_sgvec will
d5b7da2
overflow the heap, and disaster ensues.
d5b7da2
d5b7da2
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
d5b7da2
Cc: stable@vger.kernel.org
d5b7da2
Cc: security@kernel.org
d5b7da2
Signed-off-by: David S. Miller <davem@davemloft.net>
d5b7da2
---
d5b7da2
 drivers/net/macsec.c | 2 +-
d5b7da2
 1 file changed, 1 insertion(+), 1 deletion(-)
d5b7da2
d5b7da2
diff --git a/drivers/net/macsec.c b/drivers/net/macsec.c
d5b7da2
index ff0a5ed..dbab05a 100644
d5b7da2
--- a/drivers/net/macsec.c
d5b7da2
+++ b/drivers/net/macsec.c
d5b7da2
@@ -2716,7 +2716,7 @@ static netdev_tx_t macsec_start_xmit(struct sk_buff *skb,
d5b7da2
 }
d5b7da2
 
d5b7da2
 #define MACSEC_FEATURES \
d5b7da2
-	(NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST)
d5b7da2
+	(NETIF_F_SG | NETIF_F_HIGHDMA)
d5b7da2
 static struct lock_class_key macsec_netdev_addr_lock_key;
d5b7da2
 
d5b7da2
 static int macsec_dev_init(struct net_device *dev)
d5b7da2
-- 
d5b7da2
cgit v1.1
d5b7da2