Skip to content

Commit ac6e780

Browse files
Eric Dumazetdavem330
Eric Dumazet
authored andcommitted
tcp: take care of truncations done by sk_filter()
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: Eric Dumazet <edumazet@google.com> Reported-by: Marco Grassi <marco.gra@gmail.com> Reported-by: Vladis Dronov <vdronov@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 969447f commit ac6e780

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

Diff for: include/net/tcp.h

+1
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,7 @@ static inline void tcp_prequeue_init(struct tcp_sock *tp)
12201220

12211221
bool tcp_prequeue(struct sock *sk, struct sk_buff *skb);
12221222
bool tcp_add_backlog(struct sock *sk, struct sk_buff *skb);
1223+
int tcp_filter(struct sock *sk, struct sk_buff *skb);
12231224

12241225
#undef STATE_TRACE
12251226

Diff for: net/ipv4/tcp_ipv4.c

+18-1
Original file line numberDiff line numberDiff line change
@@ -1564,6 +1564,21 @@ bool tcp_add_backlog(struct sock *sk, struct sk_buff *skb)
15641564
}
15651565
EXPORT_SYMBOL(tcp_add_backlog);
15661566

1567+
int tcp_filter(struct sock *sk, struct sk_buff *skb)
1568+
{
1569+
struct tcphdr *th = (struct tcphdr *)skb->data;
1570+
unsigned int eaten = skb->len;
1571+
int err;
1572+
1573+
err = sk_filter_trim_cap(sk, skb, th->doff * 4);
1574+
if (!err) {
1575+
eaten -= skb->len;
1576+
TCP_SKB_CB(skb)->end_seq -= eaten;
1577+
}
1578+
return err;
1579+
}
1580+
EXPORT_SYMBOL(tcp_filter);
1581+
15671582
/*
15681583
* From tcp_input.c
15691584
*/
@@ -1676,8 +1691,10 @@ int tcp_v4_rcv(struct sk_buff *skb)
16761691

16771692
nf_reset(skb);
16781693

1679-
if (sk_filter(sk, skb))
1694+
if (tcp_filter(sk, skb))
16801695
goto discard_and_relse;
1696+
th = (const struct tcphdr *)skb->data;
1697+
iph = ip_hdr(skb);
16811698

16821699
skb->dev = NULL;
16831700

Diff for: net/ipv6/tcp_ipv6.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -1229,7 +1229,7 @@ static int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb)
12291229
if (skb->protocol == htons(ETH_P_IP))
12301230
return tcp_v4_do_rcv(sk, skb);
12311231

1232-
if (sk_filter(sk, skb))
1232+
if (tcp_filter(sk, skb))
12331233
goto discard;
12341234

12351235
/*
@@ -1457,8 +1457,10 @@ static int tcp_v6_rcv(struct sk_buff *skb)
14571457
if (tcp_v6_inbound_md5_hash(sk, skb))
14581458
goto discard_and_relse;
14591459

1460-
if (sk_filter(sk, skb))
1460+
if (tcp_filter(sk, skb))
14611461
goto discard_and_relse;
1462+
th = (const struct tcphdr *)skb->data;
1463+
hdr = ipv6_hdr(skb);
14621464

14631465
skb->dev = NULL;
14641466

0 commit comments

Comments
 (0)