]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
util/streaming-buffer: fix regions intersection
authorPhilippe Antoine <pantoine@oisf.net>
Thu, 21 Nov 2024 13:55:32 +0000 (14:55 +0100)
committerVictor Julien <vjulien@oisf.net>
Wed, 11 Dec 2024 05:49:35 +0000 (06:49 +0100)
This was not a problem for current callers in Suricata,
as RegionsIntersect is only called through StreamingBufferInsertAt
which is only used by TCP...

And TCP uses default region gap = 256kb, and only calls
StreamingBufferInsertAt with a u16, so TCP never inserts a new
data that will strictly contain an existing region augmented
with region gap, which was the only case where RegionsIntersect
returned the wrong result, which could later lead to a
buffer overflow.

Ticket: 7393

src/util-streaming-buffer.c

index 3b56d8e0d652b4c2eb69f4eade137351f4ebaaea..aea6934634dea448b8491e0de545855b7be21cf4 100644 (file)
@@ -137,17 +137,14 @@ static inline bool RegionsIntersect(const StreamingBufferConfig *cfg,
     SCLogDebug("r %p: %" PRIu64 "/%" PRIu64 " - adjusted %" PRIu64 "/%" PRIu64, r, r->stream_offset,
             r->stream_offset + r->buf_size, reg_o, reg_re);
     /* check if data range intersects with region range */
-    if (offset >= reg_o && offset <= reg_re) {
-        SCLogDebug("r %p is in-scope", r);
-        return true;
-    }
-    if (re >= reg_o && re <= reg_re) {
-        SCLogDebug("r %p is in-scope: %" PRIu64 " >= %" PRIu64 " && %" PRIu64 " <= %" PRIu64, r, re,
-                reg_o, re, reg_re);
-        return true;
-    }
-    SCLogDebug("r %p is out of scope: %" PRIu64 "/%" PRIu64, r, offset, re);
-    return false;
+    /* [offset:re] and [reg_o:reg_re] do not intersect if and only if
+     * re < reg_o or if reg_re < offset (one segment is strictly before the other)
+     * trusting that offset<=re and reg_o<=reg_re
+     */
+    if (re < reg_o || reg_re < offset) {
+        return false;
+    }
+    return true;
 }
 
 /** \internal