Commit Graph

480 Commits (cd2a5ec84f4523222ec8afe4f0674251d6de57cf)

Author SHA1 Message Date
Victor Julien cd2a5ec84f packet: move action functions to packet files 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 b06c0579f5 stream: fix reachable assertion
Fix `Flow::thread_id` not always getting properly set up, leading to
a reachable assertion.

Bug #4582.
3 years ago
Juliana Fajardini 9d9bc04886 stream/tcp: typo fix 3 years ago
Victor Julien e250ef6402 debug: remove empty header 3 years ago
Juliana Fajardini aa5bb2c329 stream: add exception policy for midstream flows
This allows to set a midstream-policy that can:
- fail closed (stream.midstream-policy=drop-flow)
- fail open (stream.midstream-policy=pass-flow)
- bypass stream (stream.midstream-policy=bypass)
- do nothing (default behavior)

Usage and behavior:

If stream.midstream-policy is set then if Suricata identifies a midstream flow
it will apply the corresponding action associated with the policy.

No setting means Suricata will not apply such policies, either inspecting the
flow (if stream.midstream=true) or ignoring it stream.midstream=false.

Task #5468
3 years ago
Juliana Fajardini 1774ff18a6 decode: make PacketDrop use action as parameter
A Packet may be dropped due to several different reasons. This change
adds action as a parameter, so we can update the packet action when we
drop it, instead of setting it to drop.

Related to
Bug #5458
3 years ago
Scott Jordan 7eaf1688b5 stream: fix StreamTcpSegmentForSession missing segments
Bugfix, segment traversal was being initialized at root node, but
should have been started at the min node. Bug resulted in captures
missing segments left of root node.
3 years ago
Philippe Antoine 02f2602dde src: rework includes as per cppclean 3 years ago
Victor Julien ad14e71efe stream: suppress exception policy debug message 3 years ago
Victor Julien 1594e41b06 stream: remove unused TCP_LISTEN
Keep the values the same so we might be able to bring it back
w/o issues.
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 b0993d6fd8 flow: add various flow counters
Add flow.end state counters

Add active TCP sessions counter

Add flow.active counter

Add flow.total counter

Ticket: #1478.
3 years ago
Michael Tremer f50af12068 stream: tcp: Handle retransmitted SYN with TSval
For connections that use TCP timestamps for which the first SYN packet
does not reach the server, any replies to retransmitted SYNs will be
tropped.

This is happening in StateSynSentValidateTimestamp, where the timestamp
value in a SYN-ACK packet must match the one from the SYN packet.
However, since the server never received the first SYN packet, it will
respond with an updated timestamp from any of the following SYN packets.

The timestamp value inside suricata is not being updated at any time
which should happen. This patch fixes that problem.

Bug: #4376.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
3 years ago
Victor Julien df2e408d96 detect/threshold: fix offline time handling issue
Due to the TIMEVAL_DIFF_SEC calculating the delta into an unsigned
integer, it would underflow to a high positive value leading to
and incorrect result if the packet timestamp was below the timestamp
for the threshold entry. In normal conditions this shouldn't happen,
but in offline mode each thread has its own concept of time which
might differ significantly based on the pcap. In this case the
overflow would be very common.

Changing it to a signed value calculation triggered fuzz undefined
behavior if the packet timeval was very high, so this patch takes a
new approach where it no longer calculates a diff but sets up the
"seconds" value we compare against as a timeval itself, and uses
that to compare.

Fixes: 9fafc1031c ("time: Add TIMEVAL_EARLIER and TIMEVAL_DIFF_SEC macros.")
Fixes: 82dc61f4c3 ("detect/threshold: Refactor threshold calculation to handle by_rule and by_both.")

Uses add `timeradd` specific version where available.

Bug: #5386.
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 0035673208 eve/drop: log drop reason
Ticket: #5202.
3 years ago
Victor Julien 55fb2d5d2d stream/tests: remove bad test; update failing test to new behavior 3 years ago
Victor Julien f34845858c stream: remove now unused tcp.insert_list_fail counter 3 years ago
Victor Julien cc4e8f0936 stream: detect spurious retransmissions early 3 years ago
Jason Ish adda8801d8 conf: remove ConfGetValue
All uses of ConfGetValue are satisfied by ConfGet
3 years ago
Scott Jordan 6cfc3343e7 log/pcap: dump segments of both sides of tcp session.
This patch updates tcp segment dumping to dump segments
from both sides of the session in order when capturing
alerts and tags.
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 7b592076ff stream: minor code cleanups suggested by cppcheck 4 years ago
Victor Julien c40df43609 stream: improve flow end payload logging
Use all available data, including un-ACK'd, when in flow timeout
mode.

Bug: #5276.
4 years ago
Philippe Antoine dca76a45a8 stream-tcp: fix integer warnings
Ticket: 4516
4 years ago
Victor Julien 5618886aa9 stream: remove unused defines 4 years ago
Philippe Antoine 46981ccd98 warning: explicit casts to double 4 years ago
Philippe Antoine 6cb6225b28 tcp: rejects FIN+SYN packets as invalid
Ticket: #4569

If a FIN+SYN packet is sent, the destination may keep the
connection alive instead of starting to close it.
In this case, a later SYN packet will be ignored by the
destination.

Previously, Suricata considered this a session reuse, and thus
used the sequence number of the last SYN packet, instead of
using the one of the live connection, leading to evasion.

This commit errors on FIN+SYN so that they do not get
processed as regular FIN packets.
4 years ago
Victor Julien 50e2b973ee stream/tcp: handle RST with MD5 or AO header
Special handling for RST packets if they have an TCP MD5 or AO header option.
The options hash can't be validated. The end host might be able to validate
it, as it can have a key/password that was communicated out of band.

The sender could use this to move the TCP state to 'CLOSED', leading to
a desync of the TCP session.

This patch builds on top of
843d0b7a10 ("stream: support RST getting lost/ignored")

It flags the receiver as having received an RST and moves the TCP state
into the CLOSED state. It then reverts this if the sender continues to
send traffic. In this case it sets the following event:

    stream-event:suspected_rst_inject;

Bug: #4710.
4 years ago
Juliana Fajardini 2e0d76e6e7 stream-tcp: fix typos, update copyright year 4 years ago
Juliana Fajardini 4839088359 stream-tcp: fix ConfGetBool unchecked call 4 years ago
Victor Julien 843c4b20da stream: check if ACK packet is outdated
Outdated packets are ACK packets w/o data that have an ACK value
lower than our last_ack and also don't have an SACK records that
are new.

This can happen when some packets come in later than others (possibly
due to different paths taken).
4 years ago
Victor Julien 35c2a02eb8 stream: minor debug additions 4 years ago
Victor Julien b08a7b9a66 stream: update memcaps in code to match config 4 years ago
Eric Leblond 2c8c043185 stream/tcp: limit ACK validation
Only limit ACK value validation for packet where the ACK bit is
set.
4 years ago
Eric Leblond 556570f7dd stream/tcp: don't reject on bad ack
Not using a packet for the streaming analysis when a non zero
ACK value and ACK bit was unset was leading to evasion as it was
possible to start a session with a SYN packet with a non zero ACK
value to see the full TCP stream to escape all stream and application
layer detection.

This addresses CVE-2021-35063.

Fixes: fa692df37 ("stream: reject broken ACK packets")

Ticket: #4504.
4 years ago
Eric Leblond 0d81173d6e stream/tcp: update ack handling logic
Only update the ack value of a session for regular packets when
the ACK bit is set.
4 years ago
Victor Julien beb6b1e0d1 packets: more detailed entry debug for detect/stream 4 years ago
Jeff Lucovsky 1eeb96696b general: Cleanup bool usage 5 years ago
Ilya Bakhtin b3b64803e5 stream: TcpStreamCnf.midstream type changed to bool 5 years ago
Victor Julien ee6d792b02 stream: move tests into tests/ 5 years ago
Victor Julien db2dbaaf40 stream/midstream: handle packet loss after SYN/ACK 5 years ago
Victor Julien 895938080f stream/tcp: fix invalid ack events in timewait state 5 years ago
Victor Julien 8aa02c6d15 stream/tcp: fix async mode ACK validation 5 years ago
Victor Julien 34e83b8acf stream: remove GAP flag from stream
This flag was checked in many places, but never set expect in 2 unittests.
5 years ago
Victor Julien c825f83633 stream/tcp: track if ssn has been closed with RST 5 years ago
Victor Julien d8d59ac9b5 stream: minor debug fixup 5 years ago
Shivani Bhardwaj 6f7d8e50c8 src: use FatalError whenever possible
Replaces all patterns of SCLogError() followed by exit() with
FatalError(). Cocci script to do this:

@@
constant C;
constant char[] msg;
@@

- SCLogError(C,
+ FatalError(SC_ERR_FATAL,
  msg);
- exit(EXIT_FAILURE);

Closes redmine ticket 3188.
5 years ago
Victor Julien 1b0319ea98 stream: add state to string funcs 5 years ago