Commit Graph

430 Commits (5c686af149a02f415221556a6c72f6e5f99c5230)

Author SHA1 Message Date
Jeff Lucovsky 193e0ea1a9 memory/alloc: Use SCCalloc instead of malloc/memset 2 years ago
Jeff Lucovsky 2016d68f41 stream/bool: Use bool for StreamTcpInlineMode 2 years ago
Jeff Lucovsky 904f0ddeee stats: Track stream reassembly drops
Issue: 6235
2 years ago
Victor Julien b63374bf5f stream: special handling for RST data
Data on RST packets is not invalid, but also shouldn't be used
in reassembly.

RFC 1122:

  4.2.2.12  RST Segment: RFC-793 Section 3.4

    A TCP SHOULD allow a received RST segment to include data.

    DISCUSSION
        It has been suggested that a RST segment could contain
        ASCII text that encoded and explained the cause of the
        RST.  No standard has yet been established for such
        data.

RST data will be presented to the detection engine per packet,
but will not be part of stream reassembly.

Bug: #6244.
2 years ago
Victor Julien 6b5da30d9d streaming/buffer: set errno in allocators
Add wrappers for the default allocators to set SC_ENOMEM.

The stream reassembly wrappers can set both SC_ENOMEM (alloc failed)
and SC_ELIMIT (memcap reached).
2 years ago
Victor Julien c90f67ac55 app-layer: improve/fix updates logic
In 23323a961f ("app-layer: reduce app cleanup and output-tx calls"), flag
was set per packet updating the app-layer state. However this was missing a
common pattern: in IDS mode most updates are done in the opposite direction
of the traffic due to updates getting triggered by ACK's. This meant that
file store processing might not happen for a long time, or at all. Also,
app layer cleanup might not be called, which includes file pruning.

This patch sets per flow set of flags to indicate app layer is (potentially)
updated. It sets this per direction, based on how the parsers were invoked.
If an ACK triggers an app update, the flow is tagged for the opposite
direction and the next packet in that direction triggers output and cleanup.

Fixes: 23323a961f ("app-layer: reduce app cleanup and output-tx calls")

Bug: #6120.
2 years ago
Jeff Lucovsky 4edd516250 stream/reassemble: Include pool item count msg
Issue: 5563

This commit adds the segment pool item count in the summary DEBUG
message.
2 years ago
Victor Julien 74f5d080e6 stream: fix test code spelling
Thanks to Josh Soref.
2 years ago
Victor Julien 0b15b73c9c stream: minor const cleanup 2 years ago
Victor Julien 5903ca624e stream: simplify raw stream progress catch up
Don't use TCP window, but use last ACK instead.
2 years ago
Victor Julien e61673cbc4 stream: segment insertion error handling cleanup 2 years ago
Victor Julien 5fe2fba184 stream: fix TFO overlap detection with ECN/CWR flags 2 years ago
Victor Julien 65f28b075f stream: turn session bool into flag 3 years ago
Victor Julien 435ca5bbf0 stream: remove unused packetqueue argument 3 years ago
Victor Julien 8ff2543343 stream: add util to get absolute right edge of data 3 years ago
Victor Julien 2cfbefb6c6 streaming: make minimum region gap size configurable 3 years ago
Victor Julien 8e9dac99f3 stream/reassembly: make max-regions configurable; set default 3 years ago
Victor Julien d2001ef94b stream: improve gap tests 3 years ago
Victor Julien 1bb6f44ff0 stream: flow timeout improvement
Check continuous data for app-layer and post gap data as well for
stream inspection.
3 years ago
Victor Julien 1dac2467c5 streaming: implement memory regions
In TCP, large gaps in the data could lead to an extremely poor utilization
of the streaming buffer memory. This was caused by the implementation using
a single continues memory allocation from the "stream offset" to the
current data. If a 100 byte segment was inserted for ISN + 20MiB, we would
allocate 20MiB, even if only 100 bytes were actually used.

This patch addresses the issue by implementing a list of memory regions.
The StreamingBuffer structure holds a static "main" region, which can be
extended in the form of a simple list of regions.

    [ main region ] [ gap ] [ aux region ]
    [ sbb ]                 [ sbb ]

On insert, find the correct region and see if the new data fits. If it
doesn't, see if we can expand the current region, or than we need to add
a new region. If expanding the current region means we overlap or get
too close to the next region, we merge them.

On sliding, we free any regions that slide out of window and consolidate
auxilary regions into main where needed.

Bug: #4580.
3 years ago
Victor Julien 61e47ad6f5 stream: reduce streaming buffer internals use 3 years ago
Victor Julien ef2ed35311 stream: add debug statements 3 years ago
Victor Julien db28d85d01 stream: consider any uninspected data 3 years ago
Victor Julien 1a35801f96 stream: minor constification 3 years ago
Victor Julien ebd8728219 src: fix strict-prototype warnings
Tested on Fedora 37 with clang 15.

app-layer.c:1055:27: error: a function declaration without a prototype is deprecated in all versions of C [-Werror,-Wstrict-prototypes]
void AppLayerSetupCounters()
                          ^
                           void
app-layer.c:1176:29: error: a function declaration without a prototype is deprecated in all versions of C [-Werror,-Wstrict-prototypes]
void AppLayerDeSetupCounters()
                            ^
                             void
2 errors generated.
3 years ago
Victor Julien b31ffde6f4 output: remove error codes from output 3 years ago
Victor Julien e042cd785e error: use SC_ENOMEM for alloc errors 3 years ago
Victor Julien 8ef95c10a8 pool: use errno style error reporting 3 years ago
Victor Julien 892b7021b2 stream: debug message cleanup 3 years ago
Eric Leblond e6768118da stream: flag TCP streams with gap 3 years ago
Victor Julien 235f369ab9 stream: reduce pool locking overhead
Add thread local cache to avoid locking overhead for ssns and segments.

A thread will return segments/ssns to a local cache first, and if that
is full, to a return queue where the actual return to the pool returns
a batch, to amortize locking overhead.

Adds segment and session pool/cache counters to see where how effective
the cache is.
3 years ago
Victor Julien 23323a961f app-layer: reduce app cleanup and output-tx calls
Track packets that updated the app-layer, and for those run
the transaction housekeeping and output-tx logging loops.

Do the same of end of flow packets.

This skips needless iterations over the transaction stores.
3 years ago
Victor Julien e250ef6402 debug: remove empty header 3 years ago
Jeff Lucovsky e133ab029e stream/event: Trigger stream reassembly event
Issue: 3512

This commit triggers the stream reassembly depth reached event.
3 years ago
Victor Julien 100d821a9f stream: fix GAP check
Gap check would consider a GAP when the current data was in fact
exactly not a gap, but next segment(s) were already available.
3 years ago
Victor Julien 0ebe372607 stream: after missing segments, be liberal on RST
This avoids long lasting inactive flows because in the most likely
case the RST did in fact end the connection. However Suricata may
still consider it to be "established".
3 years ago
Victor Julien 8580499ded exceptions: initial exception-policy implementation
Adds a framework for setting exception policies. These would be called
when the engine reaches some kind of exception condition, like hitting
a memcap or some traffic processing error.

The policy gives control over what should happen next: drop the packet,
drop the packet and flow, bypass, etc.

Implements the policy for:

    stream: If stream session or reassembly memcaps are hit call the
    memcap policy on the packet and flow.

    flow: Apply policy when memcap is reached and no flow could be
    freed up.

    defrag: Apply policy when no tracker could be picked up.

    app-layer: Apply ppolicy if a parser reaches an error state.

All options default to 'ignore', which means the default behavior
is unchanged.

Adds commandline options: add simulation options for exceptions. These
are only exposed if compiled with `--enable-debug`.

Ticket: #5214.
Ticket: #5215.
Ticket: #5216.
Ticket: #5218.
Ticket: #5194.
3 years ago
Victor Julien 55fb2d5d2d stream/tests: remove bad test; update failing test to new behavior 3 years ago
Victor Julien b6cf79901a stream/reassemble: clarify error handling 3 years ago
Victor Julien 84448d3bae tests: remove unnecessary flow locks
Added once to satisfy debug validation, but we don't mix unittests
and debug validation anymore.

    sed -i -E '/.*FLOWLOCK_.*LOCK/d' *.c
3 years ago
Philippe Antoine 5bd19135b0 util: remove malloc from streaming buffer config
as it is unused
3 years ago
Scott Jordan 2bf3172dd1 stream: memcap tracking for TcpSegment alloc 3 years ago
Eric Leblond 3908166f91 stream: count realloc in memcap
TCP memory cap was not taking into account the memory that can
be used by realloc of Packet headers in TCP segments.
3 years ago
Eric Leblond b416a4455c stream: conditionally add packet header to segment
This patch optionally adds packet header to the TCP segment
and update the for each segment function by changing the
callback.

This patch is based on the work by Scott Jordan <scottfgjordan@gmail.com>
3 years ago
Victor Julien 1282b179cc stream/reassembly: reduce scope of variable 3 years ago
Victor Julien 3a7d09edfc detect/frame: get data using stream callback
Inspect only data that has already been consumed by the
app-layer parser. This allows for simpler progress tracking.
3 years ago
Victor Julien ffe036e881 frame: introduce entry for getting stream data for frame 3 years ago
Victor Julien 96bc11d0d0 stream: make raw data handling more generally usable
Move raw detection logic out of main StreamReassembleRawDo() so that
it can be reused for other parts of the engine.

The caller now has to specify a right edge of the data.
3 years ago
Victor Julien afb97d1dee stream: add offset to raw stream callback
This gives the called function to understand where it is in the
stream.
3 years ago
Philippe Antoine 2a22b4ca1f flow: fix integer warnings
Ticket: 4516
3 years ago