blob: 0c1ef188b282321c49f9d2b388702acd8d46a7cb [file] [log] [blame]
From 116a285c2f4a91aaf1c6b3f7cba3645c27d0d53d Mon Sep 17 00:00:00 2001
From: Will Deacon <>
Date: Fri, 3 Apr 2020 16:13:21 +0100
Subject: [PATCH] tun: Don't put_page() for all negative return values from XDP
commit bee348907d19d654e8524d3a946dcd25b693aa7e upstream.
When an XDP program is installed, tun_build_skb() grabs a reference to
the current page fragment page if the program returns XDP_REDIRECT or
XDP_TX. However, since tun_xdp_act() passes through negative return
values from the XDP program, it is possible to trigger the error path by
mistake and accidentally drop a reference to the fragments page without
taking one, leading to a spurious free. This is believed to be the cause
of some KASAN use-after-free reports from syzbot [1], although without a
reproducer it is not possible to confirm whether this patch fixes the
Ensure that we only drop a reference to the fragments page if the XDP
transmit or redirect operations actually fail.
Cc: "David S. Miller" <>
Cc: Alexei Starovoitov <>
Cc: Daniel Borkmann <>
CC: Eric Dumazet <>
Acked-by: Jason Wang <>
Fixes: 8ae1aff0b331 ("tuntap: split out XDP logic")
Signed-off-by: Will Deacon <>
Signed-off-by: David S. Miller <>
Signed-off-by: Paul Gortmaker <>
diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index bead09f37ebd..96be656e2eae 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c
@@ -1716,8 +1716,12 @@ static struct sk_buff *tun_build_skb(struct tun_struct *tun,
alloc_frag->offset += buflen;
err = tun_xdp_act(tun, xdp_prog, &xdp, act);
- if (err < 0)
- goto err_xdp;
+ if (err < 0) {
+ if (act == XDP_REDIRECT || act == XDP_TX)
+ put_page(alloc_frag->page);
+ goto out;
+ }
if (err == XDP_REDIRECT)
if (err != XDP_PASS)
@@ -1731,8 +1735,6 @@ static struct sk_buff *tun_build_skb(struct tun_struct *tun,
return __tun_build_skb(tfile, alloc_frag, buf, buflen, len, pad);
- put_page(alloc_frag->page);