Skip to content

Commit d907a07

Browse files
edumazetgregkh
authored andcommitted
tcp: limit payload size of sacked skbs
commit 3b4929f upstream. Jonathan Looney reported that TCP can trigger the following crash in tcp_shifted_skb() : BUG_ON(tcp_skb_pcount(skb) < pcount); This can happen if the remote peer has advertized the smallest MSS that linux TCP accepts : 48 An skb can hold 17 fragments, and each fragment can hold 32KB on x86, or 64KB on PowerPC. This means that the 16bit witdh of TCP_SKB_CB(skb)->tcp_gso_segs can overflow. Note that tcp_sendmsg() builds skbs with less than 64KB of payload, so this problem needs SACK to be enabled. SACK blocks allow TCP to coalesce multiple skbs in the retransmit queue, thus filling the 17 fragments to maximal capacity. CVE-2019-11477 -- u16 overflow of TCP_SKB_CB(skb)->tcp_gso_segs Fixes: 832d11c ("tcp: Try to restore large SKBs while SACK processing") Signed-off-by: Eric Dumazet <edumazet@google.com> Reported-by: Jonathan Looney <jtl@netflix.com> Acked-by: Neal Cardwell <ncardwell@google.com> Reviewed-by: Tyler Hicks <tyhicks@canonical.com> Cc: Yuchung Cheng <ycheng@google.com> Cc: Bruce Curtis <brucec@netflix.com> Cc: Jonathan Lemon <jonathan.lemon@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 7e1bdd6 commit d907a07

5 files changed

Lines changed: 30 additions & 9 deletions

File tree

‎include/linux/tcp.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,4 +488,8 @@ static inline u16 tcp_mss_clamp(const struct tcp_sock *tp, u16 mss)
488488

489489
return (user_mss && user_mss < mss) ? user_mss : mss;
490490
}
491+
492+
int tcp_skb_shift(struct sk_buff *to, struct sk_buff *from, int pcount,
493+
int shiftlen);
494+
491495
#endif /* _LINUX_TCP_H */

‎include/net/tcp.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ void tcp_time_wait(struct sock *sk, int state, int timeo);
5555

5656
#define MAX_TCP_HEADER (128 + MAX_HEADER)
5757
#define MAX_TCP_OPTION_SPACE 40
58+
#define TCP_MIN_SND_MSS 48
59+
#define TCP_MIN_GSO_SIZE (TCP_MIN_SND_MSS - MAX_TCP_OPTION_SPACE)
5860

5961
/*
6062
* Never offer a window over 32767 without using window scaling. Some

‎net/ipv4/tcp.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3889,6 +3889,7 @@ void __init tcp_init(void)
38893889
unsigned long limit;
38903890
unsigned int i;
38913891

3892+
BUILD_BUG_ON(TCP_MIN_SND_MSS <= MAX_TCP_OPTION_SPACE);
38923893
BUILD_BUG_ON(sizeof(struct tcp_skb_cb) >
38933894
FIELD_SIZEOF(struct sk_buff, cb));
38943895

‎net/ipv4/tcp_input.c‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,7 +1296,7 @@ static bool tcp_shifted_skb(struct sock *sk, struct sk_buff *prev,
12961296
TCP_SKB_CB(skb)->seq += shifted;
12971297

12981298
tcp_skb_pcount_add(prev, pcount);
1299-
BUG_ON(tcp_skb_pcount(skb) < pcount);
1299+
WARN_ON_ONCE(tcp_skb_pcount(skb) < pcount);
13001300
tcp_skb_pcount_add(skb, -pcount);
13011301

13021302
/* When we're adding to gso_segs == 1, gso_size will be zero,
@@ -1362,6 +1362,21 @@ static int skb_can_shift(const struct sk_buff *skb)
13621362
return !skb_headlen(skb) && skb_is_nonlinear(skb);
13631363
}
13641364

1365+
int tcp_skb_shift(struct sk_buff *to, struct sk_buff *from,
1366+
int pcount, int shiftlen)
1367+
{
1368+
/* TCP min gso_size is 8 bytes (TCP_MIN_GSO_SIZE)
1369+
* Since TCP_SKB_CB(skb)->tcp_gso_segs is 16 bits, we need
1370+
* to make sure not storing more than 65535 * 8 bytes per skb,
1371+
* even if current MSS is bigger.
1372+
*/
1373+
if (unlikely(to->len + shiftlen >= 65535 * TCP_MIN_GSO_SIZE))
1374+
return 0;
1375+
if (unlikely(tcp_skb_pcount(to) + pcount > 65535))
1376+
return 0;
1377+
return skb_shift(to, from, shiftlen);
1378+
}
1379+
13651380
/* Try collapsing SACK blocks spanning across multiple skbs to a single
13661381
* skb.
13671382
*/
@@ -1467,7 +1482,7 @@ static struct sk_buff *tcp_shift_skb_data(struct sock *sk, struct sk_buff *skb,
14671482
if (!after(TCP_SKB_CB(skb)->seq + len, tp->snd_una))
14681483
goto fallback;
14691484

1470-
if (!skb_shift(prev, skb, len))
1485+
if (!tcp_skb_shift(prev, skb, pcount, len))
14711486
goto fallback;
14721487
if (!tcp_shifted_skb(sk, prev, skb, state, pcount, len, mss, dup_sack))
14731488
goto out;
@@ -1485,11 +1500,10 @@ static struct sk_buff *tcp_shift_skb_data(struct sock *sk, struct sk_buff *skb,
14851500
goto out;
14861501

14871502
len = skb->len;
1488-
if (skb_shift(prev, skb, len)) {
1489-
pcount += tcp_skb_pcount(skb);
1490-
tcp_shifted_skb(sk, prev, skb, state, tcp_skb_pcount(skb),
1503+
pcount = tcp_skb_pcount(skb);
1504+
if (tcp_skb_shift(prev, skb, pcount, len))
1505+
tcp_shifted_skb(sk, prev, skb, state, pcount,
14911506
len, mss, 0);
1492-
}
14931507

14941508
out:
14951509
return prev;

‎net/ipv4/tcp_output.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,8 +1457,8 @@ static inline int __tcp_mtu_to_mss(struct sock *sk, int pmtu)
14571457
mss_now -= icsk->icsk_ext_hdr_len;
14581458

14591459
/* Then reserve room for full set of TCP options and 8 bytes of data */
1460-
if (mss_now < 48)
1461-
mss_now = 48;
1460+
if (mss_now < TCP_MIN_SND_MSS)
1461+
mss_now = TCP_MIN_SND_MSS;
14621462
return mss_now;
14631463
}
14641464

@@ -2750,7 +2750,7 @@ static bool tcp_collapse_retrans(struct sock *sk, struct sk_buff *skb)
27502750
if (next_skb_size <= skb_availroom(skb))
27512751
skb_copy_bits(next_skb, 0, skb_put(skb, next_skb_size),
27522752
next_skb_size);
2753-
else if (!skb_shift(skb, next_skb, next_skb_size))
2753+
else if (!tcp_skb_shift(skb, next_skb, 1, next_skb_size))
27542754
return false;
27552755
}
27562756
tcp_highest_sack_replace(sk, next_skb, skb);

0 commit comments

Comments
 (0)