Skip to content

stream-tcp-inline: align overlap rewrite with pruned segment head - v1 - #16744

Open
victorjulien wants to merge 1 commit into
OISF:mainfrom
victorjulien:issue-gl2647/v1
Open

victorjulien wants to merge 1 commit into
OISF:mainfrom
victorjulien:issue-gl2647/v1

Conversation

@victorjulien

Copy link
Copy Markdown
Member

Ticket: https://redmine.openinfosecfoundation.org/issues/9463

StreamTcpInlineSegmentCompare/ReplacePacket derive the retransmit/
segment overlap from the segment's original seq. Once the stream head
has slid past that head, the stored data starts at base_seq and every
offset is shifted. In IPS mode a retransmit at or after base_seq that
overlaps such a segment is handled here (DoHandleDataOverlap): the
compare clamps internally and reports the data as different, but
ReplacePacket copies from base_seq - seg->seq bytes too far into the
stored data, so the forwarded payload differs from the inspected
stream. A packet that overlaps the pruned head but ends at or before
base_seq also wraps the range arithmetic; that pre-base shape is dropped
by the spurious-retransmission check before the overlap code
(src/stream-tcp.c:3398-3404), but the segment API should not depend on
that.

Clamp tseq/seg_seq to base_seq in both compare and replace before
deriving the overlap: in stored-data coordinates a non-overlap means no
shared data and the range cannot wrap. Fast path is kept only for
unpruned segments; replace guards empty segments; the stale BUG_ON is
dropped.

Unit tests (StreamTcpInlineTest09/10) slide the buffer, insert a
pruned-head segment and drive both paths directly: a retransmit at
base_seq+1 (the reachable shape; pre-fix this copies [6,8) instead of
[4,6)), a retransmit at the segment head, and a packet ending before
base_seq (pre-fix the range wraps). StreamTcpInlineTest09 fails on unfixed main.

SV_BRANCH=OISF/suricata-verify#3579

StreamTcpInlineSegmentReplacePacket and StreamTcpInlineSegmentCompare
derive the overlap from the segment's original seq. Once the stream head
has slid past it, the stored data starts at base_seq and is shorter, so
the derived offsets are shifted. ReplacePacket then reads from
base_seq - seg->seq bytes too far into the stored data: in IPS mode a
retransmit at or after base_seq that overlaps such a segment (handled by
DoHandleDataOverlap) is rewritten from the wrong stream offsets, so the
forwarded payload differs from the inspected stream. The compare clamps
internally, but its no-overlap gates also use the unclamped segment end,
and for a packet ending at or before base_seq the clamped start and
unclamped end make `range` wrap. Only a debug-only guard covered that.

Clamp the segment seq to base_seq in both functions before deriving the
overlap, and apply the no-overlap gates in the clamped coordinates so
`range` cannot wrap and straddling retransmits are not skipped. The
exact-match fast path now only applies to unpruned segments.
ReplacePacket also returns early when the segment carries no data, and
the BUG_ON claiming a segment can never sit entirely before base_seq is
dropped (it can while in use; the NULL data check handles it).

Unit tests cover a pruned-head segment: an aligned rewrite, a partial
rewrite at the segment head, a retransmit at base_seq+1 (the shape
reachable from the packet path; pre-fix the copy reads too far into the
stored data), and a packet overlapping the pruned head while ending
before base_seq (pre-fix `range` wraps).

Ticket: OISF#9463

@suricata-review-bot suricata-review-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI-generated review.

Verdict: approve

No high- or medium-severity findings.

Automated at commit c8807231462a using Claude Code with claude-opus-5-5/xhigh. Run: 21848d35d999f082.

@codecov

codecov Bot commented Nov 26, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.59155% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 83.95%. Comparing base (1f762c1) to head (c880723).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #16744      +/-   ##
==========================================
- Coverage   83.97%   83.95%   -0.02%     
==========================================
  Files        1008     1008              
  Lines      286491   286542      +51     
==========================================
  Hits       240577   240577              
- Misses      45914    45965      +51     
Flag Coverage Δ
fuzzcorpus 61.62% <50.00%> (-0.01%) ⬇️
livemode 25.92% <0.00%> (-0.05%) ⬇️
netns 26.30% <0.00%> (-0.03%) ⬇️
pcap 46.12% <0.00%> (-0.07%) ⬇️
suricata-verify 69.02% <90.00%> (-0.03%) ⬇️
unittests 59.19% <98.59%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@suricata-qa

Copy link
Copy Markdown

Information: QA ran without warnings.

Pipeline = 36135

@victorjulien
victorjulien marked this pull request as ready for review November 26, 2026 14:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants