BPF Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH bpf] bpf: lwtunnel: fix reroute supplying invalid dst
@ 2019-10-09  8:31 Jiri Benc
  2019-10-12 16:56 ` Alexei Starovoitov
  0 siblings, 1 reply; 4+ messages in thread
From: Jiri Benc @ 2019-10-09  8:31 UTC (permalink / raw)
  To: bpf, netdev; +Cc: Peter Oskolkov

The dst in bpf_input() has lwtstate field set. As it is of the
LWTUNNEL_ENCAP_BPF type, lwtstate->data is struct bpf_lwt. When the bpf
program returns BPF_LWT_REROUTE, ip_route_input_noref is directly called on
this skb. This causes invalid memory access, as ip_route_input_slow calls
skb_tunnel_info(skb) that expects the dst->lwstate->data to be
struct ip_tunnel_info. This results to struct bpf_lwt being accessed as
struct ip_tunnel_info.

Drop the dst before calling the IP route input functions (both for IPv4 and
IPv6).

Reported by KASAN.

Fixes: 3bd0b15281af ("bpf: add handling of BPF_LWT_REROUTE to lwt_bpf.c")
Cc: Peter Oskolkov <posk@google.com>
Signed-off-by: Jiri Benc <jbenc@redhat.com>
---
 net/core/lwt_bpf.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/net/core/lwt_bpf.c b/net/core/lwt_bpf.c
index f93785e5833c..74cfb8b5ab33 100644
--- a/net/core/lwt_bpf.c
+++ b/net/core/lwt_bpf.c
@@ -88,11 +88,16 @@ static int bpf_lwt_input_reroute(struct sk_buff *skb)
 	int err = -EINVAL;
 
 	if (skb->protocol == htons(ETH_P_IP)) {
+		struct net_device *dev = skb_dst(skb)->dev;
 		struct iphdr *iph = ip_hdr(skb);
 
+		dev_hold(dev);
+		skb_dst_drop(skb);
 		err = ip_route_input_noref(skb, iph->daddr, iph->saddr,
-					   iph->tos, skb_dst(skb)->dev);
+					   iph->tos, dev);
+		dev_put(dev);
 	} else if (skb->protocol == htons(ETH_P_IPV6)) {
+		skb_dst_drop(skb);
 		err = ipv6_stub->ipv6_route_input(skb);
 	} else {
 		err = -EAFNOSUPPORT;
-- 
2.18.1


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

* Re: [PATCH bpf] bpf: lwtunnel: fix reroute supplying invalid dst
  2019-10-09  8:31 [PATCH bpf] bpf: lwtunnel: fix reroute supplying invalid dst Jiri Benc
@ 2019-10-12 16:56 ` Alexei Starovoitov
  2019-10-14 17:39   ` Peter Oskolkov
  0 siblings, 1 reply; 4+ messages in thread
From: Alexei Starovoitov @ 2019-10-12 16:56 UTC (permalink / raw)
  To: Jiri Benc
  Cc: bpf, Network Development, Peter Oskolkov, Stanislav Fomichev,
	Eric Dumazet

On Wed, Oct 9, 2019 at 1:31 AM Jiri Benc <jbenc@redhat.com> wrote:
>
> The dst in bpf_input() has lwtstate field set. As it is of the
> LWTUNNEL_ENCAP_BPF type, lwtstate->data is struct bpf_lwt. When the bpf
> program returns BPF_LWT_REROUTE, ip_route_input_noref is directly called on
> this skb. This causes invalid memory access, as ip_route_input_slow calls
> skb_tunnel_info(skb) that expects the dst->lwstate->data to be
> struct ip_tunnel_info. This results to struct bpf_lwt being accessed as
> struct ip_tunnel_info.
>
> Drop the dst before calling the IP route input functions (both for IPv4 and
> IPv6).
>
> Reported by KASAN.
>
> Fixes: 3bd0b15281af ("bpf: add handling of BPF_LWT_REROUTE to lwt_bpf.c")
> Cc: Peter Oskolkov <posk@google.com>
> Signed-off-by: Jiri Benc <jbenc@redhat.com>

Peter and other google folks,
please review.

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

* Re: [PATCH bpf] bpf: lwtunnel: fix reroute supplying invalid dst
  2019-10-12 16:56 ` Alexei Starovoitov
@ 2019-10-14 17:39   ` Peter Oskolkov
  2019-10-14 18:44     ` Alexei Starovoitov
  0 siblings, 1 reply; 4+ messages in thread
From: Peter Oskolkov @ 2019-10-14 17:39 UTC (permalink / raw)
  To: Alexei Starovoitov
  Cc: Jiri Benc, bpf, Network Development, Peter Oskolkov,
	Stanislav Fomichev, Eric Dumazet

On Sat, Oct 12, 2019 at 9:59 AM Alexei Starovoitov
<alexei.starovoitov@gmail.com> wrote:
>
> On Wed, Oct 9, 2019 at 1:31 AM Jiri Benc <jbenc@redhat.com> wrote:
> >
> > The dst in bpf_input() has lwtstate field set. As it is of the
> > LWTUNNEL_ENCAP_BPF type, lwtstate->data is struct bpf_lwt. When the bpf
> > program returns BPF_LWT_REROUTE, ip_route_input_noref is directly called on
> > this skb. This causes invalid memory access, as ip_route_input_slow calls
> > skb_tunnel_info(skb) that expects the dst->lwstate->data to be
> > struct ip_tunnel_info. This results to struct bpf_lwt being accessed as
> > struct ip_tunnel_info.
> >
> > Drop the dst before calling the IP route input functions (both for IPv4 and
> > IPv6).
> >
> > Reported by KASAN.
> >
> > Fixes: 3bd0b15281af ("bpf: add handling of BPF_LWT_REROUTE to lwt_bpf.c")
> > Cc: Peter Oskolkov <posk@google.com>
> > Signed-off-by: Jiri Benc <jbenc@redhat.com>
>
> Peter and other google folks,
> please review.

selftests/bpf/test_lwt_ip_encap.sh passes. Seems OK.

Acked-by: Peter Oskolkov <posk@google.com>

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

* Re: [PATCH bpf] bpf: lwtunnel: fix reroute supplying invalid dst
  2019-10-14 17:39   ` Peter Oskolkov
@ 2019-10-14 18:44     ` Alexei Starovoitov
  0 siblings, 0 replies; 4+ messages in thread
From: Alexei Starovoitov @ 2019-10-14 18:44 UTC (permalink / raw)
  To: Peter Oskolkov
  Cc: Jiri Benc, bpf, Network Development, Peter Oskolkov,
	Stanislav Fomichev, Eric Dumazet

On Mon, Oct 14, 2019 at 10:39 AM Peter Oskolkov <posk@posk.io> wrote:
>
> On Sat, Oct 12, 2019 at 9:59 AM Alexei Starovoitov
> <alexei.starovoitov@gmail.com> wrote:
> >
> > On Wed, Oct 9, 2019 at 1:31 AM Jiri Benc <jbenc@redhat.com> wrote:
> > >
> > > The dst in bpf_input() has lwtstate field set. As it is of the
> > > LWTUNNEL_ENCAP_BPF type, lwtstate->data is struct bpf_lwt. When the bpf
> > > program returns BPF_LWT_REROUTE, ip_route_input_noref is directly called on
> > > this skb. This causes invalid memory access, as ip_route_input_slow calls
> > > skb_tunnel_info(skb) that expects the dst->lwstate->data to be
> > > struct ip_tunnel_info. This results to struct bpf_lwt being accessed as
> > > struct ip_tunnel_info.
> > >
> > > Drop the dst before calling the IP route input functions (both for IPv4 and
> > > IPv6).
> > >
> > > Reported by KASAN.
> > >
> > > Fixes: 3bd0b15281af ("bpf: add handling of BPF_LWT_REROUTE to lwt_bpf.c")
> > > Cc: Peter Oskolkov <posk@google.com>
> > > Signed-off-by: Jiri Benc <jbenc@redhat.com>
> >
> > Peter and other google folks,
> > please review.
>
> selftests/bpf/test_lwt_ip_encap.sh passes. Seems OK.
>
> Acked-by: Peter Oskolkov <posk@google.com>

Applied to bpf tree. Thanks

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

end of thread, back to index

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-09  8:31 [PATCH bpf] bpf: lwtunnel: fix reroute supplying invalid dst Jiri Benc
2019-10-12 16:56 ` Alexei Starovoitov
2019-10-14 17:39   ` Peter Oskolkov
2019-10-14 18:44     ` Alexei Starovoitov

BPF Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/bpf/0 bpf/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 bpf bpf/ https://lore.kernel.org/bpf \
		bpf@vger.kernel.org bpf@archiver.kernel.org
	public-inbox-index bpf

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.bpf


AGPL code for this site: git clone https://public-inbox.org/ public-inbox