Commit Graph

341 Commits (07df1ce6afffb35c0acd25b3b787ce04643306b1)

Author SHA1 Message Date
Victor Julien 0f41cf3d74 debug/validation: check tcp/app-layer data lengths 6 years ago
Victor Julien a742c86741 stream: improve app-layer data retrieval with GAPs
Don't assume that the next block after the sbb head is after the
requested offset.

If the next block was before the offset, the returned data_len
would underflow and return a nonsense value to the app-layer.

Bug #2993.
6 years ago
Victor Julien 77539e08fc stream: in IDS mode, call app-layer at EOF
On stream end call app-layer with empty message in IDS mode.
6 years ago
Victor Julien 6bc7636826 stream: remove unused code
Remove now unused 'pkt_src' type as well.

Remove related unittests.
6 years ago
Victor Julien d19ded6c43 stream: fix progress for min_inspect_depth
Make sure progress don't exceed raw_progress.
6 years ago
Victor Julien 58e48bcb87 debug: make it easier to trace flush logic 6 years ago
Jeff Lucovsky ef327ab194 stream/tcp: correct spelling typos 6 years ago
Victor Julien 5ddfc42b87 stream: fix midstream reverse flow handling
When a TCP session is picked up from the response the flow is
reversed by the protocol detection code.

This would lead to duplicate logging of the response. The reason this
happened was that the per stream app progress tracker was not handled
correctly by the direction reversing code. While the streams were
swapped the stream engine would continue to use a now outdated pointer
to what had become the wrong direction.

This patches fixes this by making the stream a ptr to ptr that can be
updated by the protocol detection as well.

In addition, the progress tracking was cleaned up and the GAP error
handling in this case was improved as well.
6 years ago
Victor Julien 67ba132c10 stream: simplify segment and ssn pool inits 6 years ago
Victor Julien aaf502d383 stream: suppress noisy debug info messages 6 years ago
Victor Julien 2622006faa stream: minor test cleanups 6 years ago
Victor Julien 9948c96c68 stream/ips: set proper payload len for inspection
On mem(cap) presure we fall back to the packet payload. The previous
patch failed to properly set the payload length.
7 years ago
Victor Julien 02ba360903 ips/stream: handle low mem(cap) crash
In low memory or memcap reached conditions a crash could happen in
inline stream detection.

The crash had the following path:

A packet would come in and it's data was added to the stream. Due
to earlier packet loss, the stream buffer uses a stream buffer block
tree to track the data blocks. When trying to add the current packets
block to the tree, the memory limit was reached and the add fails.

A bit later in the pipeline for the same packet, the inline stream
mpm inspection function gets the data to inspect. For inline mode
this is the current packet + stream data before and after the packet,
if available.

The code looking up the packets data in the stream would not
consider the possibility that the stream block returned wasn't
the right one. The tree search returns either the correct or the
next block. In adjusting the returned block to add the extra stream
data it would miscalculate offsets leading to a corrupt pointer to the
data.

This patch more carefully checks the result of the lookup, and
falls back to simply inspecting the packet payload if the lookup
didn't produce the expected result.

Bug 2842.

Reported-by: Ad Schellevis <ad@opnsense.org>
7 years ago
Victor Julien 478caac975 stream: don't assume malformed TCP packets
This deep in the stream engine packets are valid, so don't check
for the tcph header in a packet as it confuses scan-build.

Do add a DEBUG_VALIDATE_BUG_ON so in QA we double check.
7 years ago
Victor Julien 7186ce7b99 stream: introduce min inspect depth logic
Some rules need to inspect both raw stream data and higher level
buffers together. When this higher level buffer is a streaming
buffer itself, the risk of mismatch exists.

This patch allows an app-layer parser to set a 'min inspect depth'.
The value is used by the stream engine to keep at least this
depth worth of data, so that the detection engine can request
all of it for inspection.

For rules that have the SIG_FLAG_FLUSH flag set, data is inspected
not from offset raw_progress, but from raw_progress minus
min_inspect_depth.

At this time this is only used for sigs that have their fast_pattern
in a HTTP body and have raw stream match as well.
7 years ago
Victor Julien b6b9b56e45 stream/segments: keep track of tree right edge
Use this in places where we need to use the outer right
edge of our sequence space.

This way we can avoid walking the tree to find this, which
is a potentially expensive operation.
7 years ago
Victor Julien ea771c69af streaming/sbb: convert RB_MIN to 'head' 7 years ago
Victor Julien 450500e667 streaming: use rbtree for stream blocks
Switch StreamBufferBlocks implementation to use RBTREE instead of
a list. This makes inserts/removals and lookups a lot cheaper if
the number of data gaps is large.

Use separate compare functions for inserts and regular lookups.
Inserts care about the offset, while lookups care about the blocks
right edge as well.
7 years ago
Victor Julien 26b5e1ed13 stream/segments: turn linked list into rbtree
To improve worst case performance turn the segments list into a rbtree.
This greatly improves inserts, lookups and removals if the number of
segments gets very large.

The tree is sorted by the segment sequence number as its primary key.
If 2 segments have the same seq, the payload_len (segment length) is
used. Then the larger segment will be places after the smaller segment.
Exact matches are not added to the tree.
7 years ago
Victor Julien 9266334430 stream: expand GAP detection 7 years ago
Victor Julien c8ecca59f8 stream: minor code cleanup 7 years ago
Victor Julien 2629ca423a stream: improve TCP CLOSED handling
Trigger app layer reassembly in both directions as soon as we've set
the TCP state to closed.

In IDS mode, if a toserver packet would close the state, the app layer
would not get updated until the next toclient packet. However, in
detection, the raw stream inspection would already use all available
stream data in detection and move the 'raw stream progress' tracker
forward. When in later (a) packet(s) the app layer was updated and
inspection ran on the app layer, the stream progress was already
moved too far forward. This would lead to signatures that matched
on both stream and app layer to not match.

By triggering the app layer reassembly as soon as the TCP state is
set to closed, the inspection as both the stream and app layer data
available at the same time so these rules can match.

Bug: #2570
Bug: #2554
7 years ago
Victor Julien 5c584c2b74 app-layer/tcp: set STREAM_MIDSTREAM flag 7 years ago
Eric Leblond 1d0727d85f stream-tcp: fix stream depth computation
The stream depth computation was partly done with the stream_config
depth instead of using the value in the TCP session. As a result,
some configuration were resulting in abnormal behavior.

In particular, when stream depth was 0 and the file store depth was
not 0, Suricata was stopping the streaming on the flow as soon as
the filestore was started.

Reported-by: Pascal Delalande <pdl35@free.fr>
7 years ago
Victor Julien 9423f6149f stream: inform app layer of depth reached 8 years ago
Victor Julien 89dc05d4a6 stream/app-layer: fix GAP handling issue
Fix case where data after GAP was processed as in order data by app-layer.
This happened even if protocol parser did not register to accept GAPs.
8 years ago
Giuseppe Longo 80bf728bfe stream-tcp: get/set memcap value
This adds new functions that will be called
through unix-socket and permit to update
and show memcap value.

The memcap value needs to be handled in a
thread safe way, so for this reason it is
declared as atomic var.
8 years ago
Giuseppe Longo 4a75eaf405 stream-tcp-reassemble: declare 'size' as uint64_t
According to all checking memcap functions,
the size passed as argument is declared as uint64_t
except for StreamTcpReassembleCheckMemcap where it's
defined as uint32_t.
8 years ago
Victor Julien 6bea6edec1 stream: minor debug addition 8 years ago
Victor Julien 2577a64e44 stream: improve error handling of ssn/segment pools
With large number of threads the default memcaps lead to pool setup
failures. Make sure these are reported properly so that the user
knows what is going on.

Bug: #2226
8 years ago
Victor Julien e7428b321d profiling: fix app-layer profiling and csv output 8 years ago
Victor Julien afed6fe4a2 cleanup: remove all uint use 8 years ago
Victor Julien b332551dbb stream: suppress GAP notice message 8 years ago
Victor Julien 1fef8569a5 stream: minor output cleanup 8 years ago
Jason Ish c862bbdc4b app-layer: notify parsers of gaps if enabled
A parser can now set a flag that will tell the application
layer that it is capable of handling gaps. If enabled, and a
gap occurs, the app-layer needs to be prepared to accept
input that is NULL with a length, where the length is the
number of bytes lost. It is up to the app-layer to
determine if it can sync up with the input data again.
8 years ago
Victor Julien f4c4ef12c0 stream: introduce optional 'log' progress tracker
For logging streaming TCP data so far the individual segments where
used. However since the last big stream changes, the segments are
no longer the proper place for this. Segments can now have overlaps
etc.

This patch introduces a new tracker. Next to the existing 'app' and
'raw' trackers, the new tracker is 'log'. When the TCP logging is
used, a flag in the config is set and the log tracker is used to
determine how much of the stream window can be moved.
8 years ago
Victor Julien 4217c6839a stream: single GAP check
Move all GAP checks into CheckGap. Remove seg_list based check.
Also remove seg_list == NULL check to make sure the Gap check is
done on an empty list as well.

Improve next_seq < last_ack check, but add data beyond gap check.
8 years ago
Victor Julien ab1200fbd7 compiler: more strict compiler warnings
Set flags by default:

    -Wmissing-prototypes
    -Wmissing-declarations
    -Wstrict-prototypes
    -Wwrite-strings
    -Wcast-align
    -Wbad-function-cast
    -Wformat-security
    -Wno-format-nonliteral
    -Wmissing-format-attribute
    -funsigned-char

Fix minor compiler warnings for these new flags on gcc and clang.
8 years ago
Victor Julien 34f7cb2b55 stream: debug improvements 8 years ago
Victor Julien 606f515fe9 stream: enforce gap earlier in app reassembly 8 years ago
Victor Julien 314516ffe2 stream: don't call app reassembly if disable flag set 8 years ago
Victor Julien 89af036336 stream: app-layer micro optimizations 8 years ago
Victor Julien 2f77302eeb stream: raw reassembly explicit disable raw handling 8 years ago
Victor Julien 04b24cf24e stream: improve needs reassembly code 8 years ago
Victor Julien 55e19bfb89 stream: more aggressive StreamReassembleRawHasDataReady 8 years ago
Victor Julien bf3f3ce6b2 app-layer: change logic of setting 'no reassembly'
Instead of killing all reassembly instantly do things slightly more
gracefully:
1. disable app-layer reassembly immediately
2. flag raw reassembly not to accept new data

This will allow the current data to be inspected still.

After detect as run the raw reassembly will be fully disabled and
thus all reassembly will be as well.
8 years ago
Victor Julien de4f4e23a0 stream: new depth / disable raw logic
Depth reach sets NOREASSEMBLY after detect.

No new raw sets NORAW after detect.
8 years ago
Victor Julien 7c56c9ada0 stream: allow raw reassembly catch up
If raw reassembly falls behind, for example because no raw mpm is
active, then we need to sync up to the app progress if that is
available, or to the generic tcp tracking otherwise.
8 years ago
Victor Julien 89d0267df2 stream: detect stream GAP also during reassembly 8 years ago
Victor Julien 0c1ec17c92 debug-validation: add stream checks 8 years ago