BACKPORT: tcp: detect malicious patterns in tcp_collapse_ofo_queue()

[ Upstream commit 3d4bf93ac12003f9b8e1e2de37fe27983deebdcf ]

In case an attacker feeds tiny packets completely out of order,
tcp_collapse_ofo_queue() might scan the whole rb-tree, performing
expensive copies, but not changing socket memory usage at all.

1) Do not attempt to collapse tiny skbs.
2) Add logic to exit early when too many tiny skbs are detected.

We prefer not doing aggressive collapsing (which copies packets)
for pathological flows, and revert to tcp_prune_ofo_queue() which
will be less expensive.

In the future, we might add the possibility of terminating flows
that are proven to be malicious.

Signed-off-by: Eric Dumazet <>
Acked-by: Soheil Hassas Yeganeh <>
Signed-off-by: David S. Miller <>
Signed-off-by: Greg Kroah-Hartman <>
(cherry picked from commit c3ccdbc625b7e468f38f35ce98350a1c4b87ab7b
 git:// linux-4.4.y)

TEST=tcp connect

Change-Id: I617048bc9237f090d681f7e8c1a370062b291dd3
Signed-off-by: Guenter Roeck <>
Tested-by: Daniel Wang <>
Trybot-Ready: Daniel Wang <>
Commit-Queue: Daniel Wang <>
1 file changed