Security #9160
closeddefrag: bsd policy only trims the immediate right neighbour
b5effe7f8e8a51155353277a749e317e985ce2e9
de91327f3450daf737e89d84cb64003be148d7a1
Description
- Summary
In src/defrag.c, DefragInsertFrag() handles overlaps for DEFRAG_POLICY_BSD
in two places (the new fragment starts at or after the start of the
previous one, and the new fragment starts before it). Both do the same
thing to the fragments to the right of the new one:
```c
if ((next != NULL) && (frag_end > next->offset)) {
next->ltrim = frag_end - next->offset;
}
goto insert;
```
Only the single `next` fragment is trimmed and then we jump to `insert`. If
the new fragment also covers the fragments after `next`, those are left
untouched. Defrag4Reassemble() / Defrag6Reassemble() later copy the
fragments in tree order, so an untrimmed later fragment is written over the
bytes of the new fragment. With BSD policy the new fragment should have won
those bytes, so Suricata reassembles a different datagram than a BSD host
does.
- Confirmed present in
- oisf/main @ 7ad9874c050188cc3cf4c7eafdf90a277f534fb7
- Impact
A remote attacker who can send IP fragments through the sensor can make
Suricata reassemble a different payload than a host using the BSD policy.
For example, fragments at offsets 0, 16 and 32 (16 bytes each, all MF=1),
then a 40 byte fragment at offset 8 carrying the real payload, then a last
fragment at offset 48. The BSD policy keeps the earlier data for bytes 8-15
and takes the new fragment for bytes 16-47. Suricata trims only the
fragment at offset 16, so the fragment at offset 32 is left intact and its
benign data overwrites bytes 32-47 of the new fragment during reassembly.
Any signature that depends on the content or offsets in the reassembled
datagram can be evaded this way. The same code is used for IPv4 and IPv6.
- Preconditions
the destination is handled with the BSD policy (the default_policy in
defrag.c is BSD, or host-os-policy maps the target to bsd) and the attacker
can send raw fragments past the sensor.
- Proposed fix
Walk forward from `next` and trim every fragment the new one overlaps,
marking fully covered ones to be skipped, in both BSD branches:
```c
for (Frag *n = next; n != NULL && frag_end > n->offset;
n = IP_FRAGMENTS_RB_NEXT(n)) {
if (frag_end >= n->offset + n->data_len)
n->skip = 1;
else
n->ltrim = frag_end - n->offset;
}
```
- Validation status
For issue 1 we traced the fragment sequence above through
DefragInsertFrag() and the reassembly code by hand, both with and without
the change.
- Attachments
- `0001-defrag-bsd-trim-all-overlapped-fragments.patch` (issue 1)
- `regression_test_snippet_defrag.c` (issue 1, four tests, IPv4 and IPv6)
- Suggested severity
Our view, we'll defer to your assessment:
issue 1 looks MEDIUM (remotely triggerable detection bypass, but only for the BSD policy)
- Credit
Please credit Alexander Stadnikov and CC Luukas Larinkoski; no other individuals need to be named.
Files