Commit 56325d9f authored by Eric Dumazet's avatar Eric Dumazet Committed by Willy Tarreau

tcp: take care of truncations done by sk_filter()

commit ac6e7800 upstream.

With syzkaller help, Marco Grassi found a bug in TCP stack,
crashing in tcp_collapse()

Root cause is that sk_filter() can truncate the incoming skb,
but TCP stack was not really expecting this to happen.
It probably was expecting a simple DROP or ACCEPT behavior.

We first need to make sure no part of TCP header could be removed.
Then we need to adjust TCP_SKB_CB(skb)->end_seq

Many thanks to syzkaller team and Marco for giving us a reproducer.
Signed-off-by: default avatarEric Dumazet <edumazet@google.com>
Reported-by: default avatarMarco Grassi <marco.gra@gmail.com>
Reported-by: default avatarVladis Dronov <vdronov@redhat.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
Signed-off-by: default avatarWilly Tarreau <w@1wt.eu>
parent d318f82f
...@@ -36,7 +36,11 @@ static inline unsigned int sk_filter_len(const struct sk_filter *fp) ...@@ -36,7 +36,11 @@ static inline unsigned int sk_filter_len(const struct sk_filter *fp)
return fp->len * sizeof(struct sock_filter) + sizeof(*fp); return fp->len * sizeof(struct sock_filter) + sizeof(*fp);
} }
extern int sk_filter(struct sock *sk, struct sk_buff *skb); int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap);
static inline int sk_filter(struct sock *sk, struct sk_buff *skb)
{
return sk_filter_trim_cap(sk, skb, 1);
}
extern unsigned int sk_run_filter(const struct sk_buff *skb, extern unsigned int sk_run_filter(const struct sk_buff *skb,
const struct sock_filter *filter); const struct sock_filter *filter);
extern int sk_unattached_filter_create(struct sk_filter **pfp, extern int sk_unattached_filter_create(struct sk_filter **pfp,
......
...@@ -1029,6 +1029,7 @@ static inline void tcp_prequeue_init(struct tcp_sock *tp) ...@@ -1029,6 +1029,7 @@ static inline void tcp_prequeue_init(struct tcp_sock *tp)
} }
extern bool tcp_prequeue(struct sock *sk, struct sk_buff *skb); extern bool tcp_prequeue(struct sock *sk, struct sk_buff *skb);
int tcp_filter(struct sock *sk, struct sk_buff *skb);
#undef STATE_TRACE #undef STATE_TRACE
......
...@@ -67,9 +67,10 @@ static inline void *load_pointer(const struct sk_buff *skb, int k, ...@@ -67,9 +67,10 @@ static inline void *load_pointer(const struct sk_buff *skb, int k,
} }
/** /**
* sk_filter - run a packet through a socket filter * sk_filter_trim_cap - run a packet through a socket filter
* @sk: sock associated with &sk_buff * @sk: sock associated with &sk_buff
* @skb: buffer to filter * @skb: buffer to filter
* @cap: limit on how short the eBPF program may trim the packet
* *
* Run the filter code and then cut skb->data to correct size returned by * Run the filter code and then cut skb->data to correct size returned by
* sk_run_filter. If pkt_len is 0 we toss packet. If skb->len is smaller * sk_run_filter. If pkt_len is 0 we toss packet. If skb->len is smaller
...@@ -78,7 +79,7 @@ static inline void *load_pointer(const struct sk_buff *skb, int k, ...@@ -78,7 +79,7 @@ static inline void *load_pointer(const struct sk_buff *skb, int k,
* be accepted or -EPERM if the packet should be tossed. * be accepted or -EPERM if the packet should be tossed.
* *
*/ */
int sk_filter(struct sock *sk, struct sk_buff *skb) int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap)
{ {
int err; int err;
struct sk_filter *filter; struct sk_filter *filter;
...@@ -99,14 +100,13 @@ int sk_filter(struct sock *sk, struct sk_buff *skb) ...@@ -99,14 +100,13 @@ int sk_filter(struct sock *sk, struct sk_buff *skb)
filter = rcu_dereference(sk->sk_filter); filter = rcu_dereference(sk->sk_filter);
if (filter) { if (filter) {
unsigned int pkt_len = SK_RUN_FILTER(filter, skb); unsigned int pkt_len = SK_RUN_FILTER(filter, skb);
err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM;
err = pkt_len ? pskb_trim(skb, pkt_len) : -EPERM;
} }
rcu_read_unlock(); rcu_read_unlock();
return err; return err;
} }
EXPORT_SYMBOL(sk_filter); EXPORT_SYMBOL(sk_filter_trim_cap);
/** /**
* sk_run_filter - run a filter on a socket * sk_run_filter - run a filter on a socket
......
...@@ -1959,6 +1959,21 @@ bool tcp_prequeue(struct sock *sk, struct sk_buff *skb) ...@@ -1959,6 +1959,21 @@ bool tcp_prequeue(struct sock *sk, struct sk_buff *skb)
} }
EXPORT_SYMBOL(tcp_prequeue); EXPORT_SYMBOL(tcp_prequeue);
int tcp_filter(struct sock *sk, struct sk_buff *skb)
{
struct tcphdr *th = (struct tcphdr *)skb->data;
unsigned int eaten = skb->len;
int err;
err = sk_filter_trim_cap(sk, skb, th->doff * 4);
if (!err) {
eaten -= skb->len;
TCP_SKB_CB(skb)->end_seq -= eaten;
}
return err;
}
EXPORT_SYMBOL(tcp_filter);
/* /*
* From tcp_input.c * From tcp_input.c
*/ */
...@@ -2021,8 +2036,10 @@ int tcp_v4_rcv(struct sk_buff *skb) ...@@ -2021,8 +2036,10 @@ int tcp_v4_rcv(struct sk_buff *skb)
goto discard_and_relse; goto discard_and_relse;
nf_reset(skb); nf_reset(skb);
if (sk_filter(sk, skb)) if (tcp_filter(sk, skb))
goto discard_and_relse; goto discard_and_relse;
th = (const struct tcphdr *)skb->data;
iph = ip_hdr(skb);
skb->dev = NULL; skb->dev = NULL;
......
...@@ -1330,7 +1330,7 @@ static int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb) ...@@ -1330,7 +1330,7 @@ static int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb)
goto discard; goto discard;
#endif #endif
if (sk_filter(sk, skb)) if (tcp_filter(sk, skb))
goto discard; goto discard;
/* /*
...@@ -1501,8 +1501,10 @@ static int tcp_v6_rcv(struct sk_buff *skb) ...@@ -1501,8 +1501,10 @@ static int tcp_v6_rcv(struct sk_buff *skb)
if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
goto discard_and_relse; goto discard_and_relse;
if (sk_filter(sk, skb)) if (tcp_filter(sk, skb))
goto discard_and_relse; goto discard_and_relse;
th = (const struct tcphdr *)skb->data;
hdr = ipv6_hdr(skb);
skb->dev = NULL; skb->dev = NULL;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment