bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC PATCH] tun: Don't put_page() for all negative return values from XDP program
@ 2020-03-30 16:12 Will Deacon
  2020-03-31  2:59 ` Jason Wang
  0 siblings, 1 reply; 3+ messages in thread
From: Will Deacon @ 2020-03-30 16:12 UTC (permalink / raw)
  To: netdev
  Cc: linux-kernel, bpf, kernel-team, Will Deacon, David S. Miller,
	Alexei Starovoitov, Daniel Borkmann, Jason Wang, Eric Dumazet

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
problem.

Ensure that we only drop a reference to the fragments page if the XDP
transmit or redirect operations actually fail.

[1] https://syzkaller.appspot.com/bug?id=e76a6af1be4acd727ff6bbca669833f98cbf5d95

Cc: "David S. Miller" <davem@davemloft.net>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Jason Wang <jasowang@redhat.com>
CC: Eric Dumazet <edumazet@google.com>
Signed-off-by: Will Deacon <will@kernel.org>
---

Sending as RFC because I've not been able to confirm that this fixes anything.

 drivers/net/tun.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index 650c937ed56b..9de9b7d8aedd 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c
@@ -1715,8 +1715,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)
 			xdp_do_flush();
 		if (err != XDP_PASS)
@@ -1730,8 +1734,6 @@ static struct sk_buff *tun_build_skb(struct tun_struct *tun,
 
 	return __tun_build_skb(tfile, alloc_frag, buf, buflen, len, pad);
 
-err_xdp:
-	put_page(alloc_frag->page);
 out:
 	rcu_read_unlock();
 	local_bh_enable();
-- 
2.26.0.rc2.310.g2932bb562d-goog


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [RFC PATCH] tun: Don't put_page() for all negative return values from XDP program
  2020-03-30 16:12 [RFC PATCH] tun: Don't put_page() for all negative return values from XDP program Will Deacon
@ 2020-03-31  2:59 ` Jason Wang
  2020-03-31 12:14   ` Will Deacon
  0 siblings, 1 reply; 3+ messages in thread
From: Jason Wang @ 2020-03-31  2:59 UTC (permalink / raw)
  To: Will Deacon, netdev
  Cc: linux-kernel, bpf, kernel-team, David S. Miller,
	Alexei Starovoitov, Daniel Borkmann, Eric Dumazet


On 2020/3/31 上午12:12, Will Deacon wrote:
> 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
> problem.
>
> Ensure that we only drop a reference to the fragments page if the XDP
> transmit or redirect operations actually fail.
>
> [1] https://syzkaller.appspot.com/bug?id=e76a6af1be4acd727ff6bbca669833f98cbf5d95


I think the patch fixes the issue reported. Since I can see the warn of 
bad page state in put_page().


>
> Cc: "David S. Miller" <davem@davemloft.net>
> Cc: Alexei Starovoitov <ast@kernel.org>
> Cc: Daniel Borkmann <daniel@iogearbox.net>
> Cc: Jason Wang <jasowang@redhat.com>
> CC: Eric Dumazet <edumazet@google.com>
> Signed-off-by: Will Deacon <will@kernel.org>
> ---
>
> Sending as RFC because I've not been able to confirm that this fixes anything.
>
>   drivers/net/tun.c | 10 ++++++----
>   1 file changed, 6 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/net/tun.c b/drivers/net/tun.c
> index 650c937ed56b..9de9b7d8aedd 100644
> --- a/drivers/net/tun.c
> +++ b/drivers/net/tun.c
> @@ -1715,8 +1715,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)
>   			xdp_do_flush();
>   		if (err != XDP_PASS)
> @@ -1730,8 +1734,6 @@ static struct sk_buff *tun_build_skb(struct tun_struct *tun,
>   
>   	return __tun_build_skb(tfile, alloc_frag, buf, buflen, len, pad);
>   
> -err_xdp:
> -	put_page(alloc_frag->page);
>   out:
>   	rcu_read_unlock();
>   	local_bh_enable();


Acked-by: Jason Wang <jasowang@redhat.com>

Thanks



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [RFC PATCH] tun: Don't put_page() for all negative return values from XDP program
  2020-03-31  2:59 ` Jason Wang
@ 2020-03-31 12:14   ` Will Deacon
  0 siblings, 0 replies; 3+ messages in thread
From: Will Deacon @ 2020-03-31 12:14 UTC (permalink / raw)
  To: Jason Wang
  Cc: netdev, linux-kernel, bpf, kernel-team, David S. Miller,
	Alexei Starovoitov, Daniel Borkmann, Eric Dumazet

On Tue, Mar 31, 2020 at 10:59:01AM +0800, Jason Wang wrote:
> On 2020/3/31 上午12:12, Will Deacon wrote:
> > 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
> > problem.
> > 
> > Ensure that we only drop a reference to the fragments page if the XDP
> > transmit or redirect operations actually fail.
> > 
> > [1] https://syzkaller.appspot.com/bug?id=e76a6af1be4acd727ff6bbca669833f98cbf5d95
> 
> 
> I think the patch fixes the issue reported. Since I can see the warn of bad
> page state in put_page().

[...]

> Acked-by: Jason Wang <jasowang@redhat.com>

Thanks, Jason. In which case, I'll add this tag along with:

Fixes: 8ae1aff0b331 ("tuntap: split out XDP logic")

Will

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-03-31 12:14 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-30 16:12 [RFC PATCH] tun: Don't put_page() for all negative return values from XDP program Will Deacon
2020-03-31  2:59 ` Jason Wang
2020-03-31 12:14   ` Will Deacon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).