]> git.ipfire.org Git - thirdparty/suricata.git/log
thirdparty/suricata.git
3 years agomime: fix and cleanup tests 7592/head
Victor Julien [Tue, 28 Jun 2022 05:46:54 +0000 (07:46 +0200)] 
mime: fix and cleanup tests

Line count check was failing after recent delim handling updates.

(cherry picked from commit 074cfb5c68e76642f35f78c10e56761eb8010f5f)

3 years agomime: fix corner case
Victor Julien [Tue, 28 Jun 2022 13:29:25 +0000 (15:29 +0200)] 
mime: fix corner case

Fix a corner case where a base64 sequence including a space was followed
by a newline in the input data.

(cherry picked from commit d81582c4a2b5b9cd6000c8637a7c6bc9b8f4dfd6)

3 years agomime: add base64 related debug messages
Victor Julien [Tue, 28 Jun 2022 13:29:11 +0000 (15:29 +0200)] 
mime: add base64 related debug messages

(cherry picked from commit 5805ed47f51edbb2319f687cfa4fe503d6f0f0e3)

3 years agomime: improved empty line handling
Victor Julien [Tue, 28 Jun 2022 11:07:37 +0000 (13:07 +0200)] 
mime: improved empty line handling

Make sure a new body is not set up on empty lines unless it is
a body that is not encoded as base64/quoted printable.

(cherry picked from commit 41c2c1ed5a8a1f71e4cab02bb28567da9171332d)

3 years agomime: properly pass full lines to non-decoded body
Victor Julien [Mon, 27 Jun 2022 18:15:16 +0000 (20:15 +0200)] 
mime: properly pass full lines to non-decoded body

Use actual delim count and make sure we also pass on empty lines
(so delim(s) only).

(cherry picked from commit b82b8825e79bd43901720813b672a9ff5c7bf120)

3 years agomime/base64: fix final data not getting processed
Victor Julien [Mon, 27 Jun 2022 14:20:02 +0000 (16:20 +0200)] 
mime/base64: fix final data not getting processed

If the last data of the body was not a multple of 4 and not padded
to be a multiple of 4, it would not be processed.

(cherry picked from commit 0d6ab727c55cbda04404ec44c5aa6582f3922478)

3 years agomime: remove unused 'linerem' logic
Victor Julien [Sun, 26 Jun 2022 14:25:36 +0000 (16:25 +0200)] 
mime: remove unused 'linerem' logic

(cherry picked from commit 0871029d17d23a7875af074d76965675c31599f0)

3 years agosmtp/mime: fix parsing edge case
Victor Julien [Sun, 26 Jun 2022 00:29:34 +0000 (02:29 +0200)] 
smtp/mime: fix parsing edge case

Correctly track "remaining" bytes after partial base64 decoding.

Add comment clarifications and debug validation checks.

(cherry picked from commit 5953a7d2ebd20be2a9f578fae66face4e172b678)

3 years agosmtp: skip preprocessing for mime headers
Victor Julien [Sat, 25 Jun 2022 11:01:37 +0000 (13:01 +0200)] 
smtp: skip preprocessing for mime headers

Mime parser doesn't expect partial lines, which preprocessing can
provide. Add a check to let mime headers be handled by regular line
parsing.

(cherry picked from commit a38f2f2a52822f150fff0d5e17e9f62582d6558c)

3 years agosmtp: handle buffered data in pre processing
Victor Julien [Sat, 18 Jun 2022 08:33:51 +0000 (10:33 +0200)] 
smtp: handle buffered data in pre processing

If we're in DATA mode we know that the buffered data is:

1. an incomplete line
2. all 'DATA'

So lets consume it and enter our normal loop afterwards.

Make sure not to pass on incomplete (possible) mime boundaries or
end of data markers.

3 years agosmtp: remove defunct check for line with single LF
Victor Julien [Sun, 5 Jun 2022 16:23:55 +0000 (18:23 +0200)] 
smtp: remove defunct check for line with single LF

Don't fix it as DATA processing needs all the bytes.

(cherry picked from commit 96bb67f474242936c029e57be3d4da1365150943)

3 years agosmtp: turn assertions in to debug asserts
Victor Julien [Sat, 4 Jun 2022 06:49:41 +0000 (08:49 +0200)] 
smtp: turn assertions in to debug asserts

(cherry picked from commit 77fae275efb54221d9cf3badd25df38eb1ea0b86)

3 years agosmtp: simplify preprocess loop
Victor Julien [Sat, 4 Jun 2022 06:49:25 +0000 (08:49 +0200)] 
smtp: simplify preprocess loop

(cherry picked from commit 3a631085bb853d6d42c5218235cd9a581a3e106c)

3 years agosmtp: treat CR as a line terminator
Shivani Bhardwaj [Fri, 29 Apr 2022 08:51:40 +0000 (14:21 +0530)] 
smtp: treat CR as a line terminator

The ideal line terminator for an SMTP line is <CRLF>. But, given that
bare LF is still allowed by many systems despite the prohibition by
standards, we have to consider that. In order to simplify things, we
consider bare CR as line terminators as well while updating the
delimiter parameter correctly if they were to be followed by a LF
immediately or as a part of next fragment.

This takes care of some edge cases that made base64 decoder error out
because unexpected data was sent to it at times.

Ticket: 5316
(cherry picked from commit 1e3282f36343c59a18d8e342d5e78aa3018b8edd)

3 years agodetect/tests: improve detection entry 7559/head
Victor Julien [Fri, 10 Sep 2021 11:42:15 +0000 (13:42 +0200)] 
detect/tests: improve detection entry

Lots of tests still use SigMatchSignatures as their main detection
entry function, which bypassed some logic. Make it match main logic
more closely.

(cherry picked from commit e36b9b89a1be5b45b6e071248a17208b617b0c71)

3 years agotests: fix drop test; cleanup
Victor Julien [Fri, 10 Sep 2021 11:43:26 +0000 (13:43 +0200)] 
tests: fix drop test; cleanup

SigTestDropFlow04 was incorrectly expecting an alert in the packet
following a "drop" packet. The first drop is applied to the flow, so
it should lead to the 2nd packet being dropped before inspection is
run.

Clean up the test as well.

(cherry picked from commit 3874d08015f36b79b32e25dbecc4b245d314f269)

3 years agodetect: enforce flow drops earlier 7558/head
Victor Julien [Fri, 10 Sep 2021 10:35:13 +0000 (12:35 +0200)] 
detect: enforce flow drops earlier

Enforcing flow drops is now done earlier in the detection engine and
moved out of the IP-only engine where it didn't belong.

(cherry picked from commit 802c1ffee35250d1ac753aec1343e481b83d854f)

3 years agodetect/threshold: fix offline time handling issue
Victor Julien [Tue, 7 Jun 2022 20:57:39 +0000 (22:57 +0200)] 
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: 9fafc1031c0c ("time: Add TIMEVAL_EARLIER and TIMEVAL_DIFF_SEC macros.")
Fixes: 82dc61f4c3e3 ("detect/threshold: Refactor threshold calculation to handle by_rule and by_both.")
Uses add `timeradd` specific version where available.

Bug: #5386.
(cherry picked from commit df2e408d96d0e37a0599f885dc29fff4011f8899)

3 years agogithub/workflows: add cargo for all Ubuntu jobs 7538/head
Victor Julien [Mon, 13 Jun 2022 15:21:51 +0000 (17:21 +0200)] 
github/workflows: add cargo for all Ubuntu jobs

(cherry picked from commit 7ced8de6c4659f21bb928d70a950f32016037da2)

3 years agosmb/dce_iface: avoid deleting current ifaces from state 7537/head
Eloy Pérez González [Fri, 22 Oct 2021 11:53:39 +0000 (13:53 +0200)] 
smb/dce_iface: avoid deleting current ifaces from state

The smb dce_iface keyword must match for all those dcerpc requests
and responses sent in the context of the given interface. They are
not matching as the current bind interfaces are deleted by any
non bind message.

Ticket: 4767
(cherry picked from commit bff0774767757b0ccab8165e293024fa39d0a952)

3 years agosmb/dce_iface: use DCERPC_TYPE_REQUEST
Eloy Pérez González [Fri, 22 Oct 2021 11:52:14 +0000 (13:52 +0200)] 
smb/dce_iface: use DCERPC_TYPE_REQUEST

The smb dce_iface keyword must match for all those dcerpc requests and
responses sent in the context of the given interface. They are not
matching because in rs_smb_tx_get_dce_iface, x.req_cmd is erroneously
compared with 1. Fix this by comparing with DCERPC_TYPE_REQUEST instead.

Ticket: 4767
(cherry picked from commit 1ae22fd5de3155c356c1191759cb7c066498a93e)

3 years agosmb/dce_opnum: use DCERPC_TYPE_REQUEST
Eloy Pérez González [Fri, 22 Oct 2021 13:00:11 +0000 (15:00 +0200)] 
smb/dce_opnum: use DCERPC_TYPE_REQUEST

The smb dce_opnum keyword doesn't match the dcerpc requests/responses.
This occurs because in the rs_smb_tx_match_dce_opnum function, the
x.req_cmd is matched against the erroneous code 1. Fix this by using
DCERPC_TYPE_REQUEST for the comparison instead.

Ticket: 4767
(cherry picked from commit 8dca3d0416f539f5086f72f69d71cfc418355a2f)

3 years agodetect/dcerpc: apply dcerpc to smb as well
Victor Julien [Tue, 14 Jun 2022 09:41:58 +0000 (11:41 +0200)] 
detect/dcerpc: apply dcerpc to smb as well

So 'alert dcerpc' also matches if the DCERPC is over SMB.

Bug: #5208.

3 years agodetect/content: fix FNs due to bad depth calc
Victor Julien [Thu, 9 Jun 2022 20:25:44 +0000 (22:25 +0200)] 
detect/content: fix FNs due to bad depth calc

When trying to propegate the depth/offset, within/distance chains
a logic error would set too a restrictive depth on a pattern that
followed more than one "unchained" patterns.

Bug: #5162.
(cherry picked from commit 8d20b40cdd3c8e911b0c4b06fb4fdc999b2d5c7c)

3 years agodetect/content: simplify int bounds checking
Victor Julien [Thu, 9 Jun 2022 20:25:08 +0000 (22:25 +0200)] 
detect/content: simplify int bounds checking

Use a macro to validate the ranges for overflows. This removes
the clutter of all the checks and warnings, and also no longer
puts the state machine in an undefined state when hitting such
a condition.

(cherry picked from commit 50d02ebc055ac99db9ea565ed7bd623c357cceb0)

3 years agodetect: fix integer warnings for content
Philippe Antoine [Fri, 3 Jun 2022 13:45:01 +0000 (15:45 +0200)] 
detect: fix integer warnings for content

Ticket: #4516
(cherry picked from commit ac8fb553a1b1e1ed19f33ac29f62123be51c9713)

3 years agodetect: parsing avoiding infinite loop
Philippe Antoine [Sat, 16 Apr 2022 14:51:42 +0000 (16:51 +0200)] 
detect: parsing avoiding infinite loop

by comparing size_t to strlen result
Instead of uint16_t which would loop

Ticket: #5310
(cherry picked from commit ced96a8aadd8a9a4ce3d8d689ac53f7eedbbe8e9)

3 years agostream: tcp: Handle retransmitted SYN with TSval
Michael Tremer [Fri, 11 Mar 2022 11:08:10 +0000 (11:08 +0000)] 
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>
(cherry picked from commit f50af12068d0af529acdaa4e45f0d4acc8c372d1)

3 years agofile: consistently track size of gaps
Victor Julien [Mon, 13 Jun 2022 10:55:52 +0000 (12:55 +0200)] 
file: consistently track size of gaps

Until now only the size of gaps counted in the regular append, not
close and open.

Bug: #5392.
(cherry picked from commit 71ef62bfc5ee8ffe4798467935f4f9386178565e)

3 years agomime/base64: decode cleanups and simplification
Victor Julien [Fri, 3 Jun 2022 15:10:59 +0000 (17:10 +0200)] 
mime/base64: decode cleanups and simplification

Addresses edge case: > 4 bytes at the end of the input with 2 or more
spaces.

Changes length type for remainder processing to allow for much longer
lines, which can happen in practice.

Adds a series of debug validation checks with real error handling
as well, to assist the fuzzer to find more edge cases.

(cherry picked from commit 30e47b21714b5d9039f22df7b406b201bdd15b7e)

3 years agobase64: no special case for nul char
Victor Julien [Sat, 4 Jun 2022 05:09:27 +0000 (07:09 +0200)] 
base64: no special case for nul char

Let it be handled like other invalid input.

(cherry picked from commit 92cd95b4165eef540bc974d50f54a47a6b74503d)

3 years agobase64: make decoder handle decoded data space constraints
Shivani Bhardwaj [Thu, 2 Jun 2022 15:20:07 +0000 (20:50 +0530)] 
base64: make decoder handle decoded data space constraints

So far, it was the job of caller to send the bae64 decoder a perfect
block of data and take care of the destination buffer (decoded data)
size. Now, make it the decoder's job to take care of any space
constraints that the destination buffer may have and return accordingly.

Also, handle space characters in base64 encoded data as per RFC 2045.

Update MIME parser accordingly to handle the base64 data.

Ticket: 5315
(cherry picked from commit 5b2761977871a94d559a9ba3b026593bb1ffd68b)

3 years agobase64: add Base64Ecode enum
Shivani Bhardwaj [Fri, 3 Jun 2022 10:26:36 +0000 (15:56 +0530)] 
base64: add Base64Ecode enum

(cherry picked from commit cb01cc6929b6bfdf02f7b3049d7bd110a1465fa4)

3 years agobase64: add Base64Mode enum
Shivani Bhardwaj [Thu, 12 May 2022 18:00:00 +0000 (23:30 +0530)] 
base64: add Base64Mode enum

(cherry picked from commit 9131d1d85715c817a22d2a987f4a01cf42e07757)

3 years agodetect: update copyright years 7474/head
Juliana Fajardini [Thu, 21 Apr 2022 14:51:21 +0000 (11:51 -0300)] 
detect: update copyright years

(cherry picked from commit 192360aa052841f21d22149b049ef94691f9a1f7)

3 years agoassorted: fix low hanging typos
Juliana Fajardini [Thu, 21 Apr 2022 15:47:03 +0000 (12:47 -0300)] 
assorted: fix low hanging typos

(cherry picked from commit 29b5f68bf0770750954780982c46c6fa82eec96b)

3 years agouserguide: explain alert queue behavior and stats
Juliana Fajardini [Wed, 6 Apr 2022 20:06:09 +0000 (17:06 -0300)] 
userguide: explain alert queue behavior and stats

Added sections along packet-alert-max config section explaining
packet alert queue overflow (when Suri reaches packet alert max), when
alerts are discarded etc.

Since from the user perspective it shouldn't matter how we process the
alert queue, the term "replace" is used, even though there's not exactly
a replacing action happening, with the queue bein pre-processed before
being appended to the Packet.

Also described the associated stats and added an explanation on when to
change packet-alert-max.

Task #5178

(cherry picked from commit 1956dc3d5da5dfdc55e0f8304e815b99bd7567e4)

3 years agodetect/stats: log out total of suppressed alerts
Juliana Fajardini [Wed, 20 Apr 2022 16:49:31 +0000 (13:49 -0300)] 
detect/stats: log out total of suppressed alerts

Related to
Task #4943
Task #5179

(cherry picked from commit 877b32c1e42c6edde3fa5f6ab73293e0a4ad27c3)

3 years agodetect/stats: log out total of discarded alerts
Juliana Fajardini [Tue, 5 Apr 2022 19:54:29 +0000 (16:54 -0300)] 
detect/stats: log out total of discarded alerts

Add a counter to our stats log with the total of alerts that have been
discarded due to packet alert queue overflow.

Also included a fix for
Bug #5354

Task #5179

(cherry picked from commit 04eefa5ab8008c06c8c19e56b06774d07bab91c7)

3 years agodetect/alert: move apply-action-flow code to func
Juliana Fajardini [Wed, 27 Apr 2022 22:18:27 +0000 (19:18 -0300)] 
detect/alert: move apply-action-flow code to func

Trying to clean PacketAlertFinalize a bit more.

(cherry picked from commit 9b275d3878643fa27ac4f54d74ba66b51e115459)

3 years agodetect/alert: remove unused functions
Juliana Fajardini [Wed, 20 Apr 2022 20:50:43 +0000 (17:50 -0300)] 
detect/alert: remove unused functions

Since we now only copy the PacketAlerts to the Packet's queue after
processing them, we no longer do packet alert appending from
detect-engine-alert, nor do we remove PacketAlerts from the queue (if
they're discarded by overflow or thresholding, they're not copied to the
final alert queue).

Task #4943

(cherry picked from commit e4e688a9b0add66aa0b97df0f7b1b9e60f68fb90)

3 years agodetect/alert: preprocess then append alert queue
Juliana Fajardini [Tue, 19 Apr 2022 20:43:10 +0000 (17:43 -0300)] 
detect/alert: preprocess then append alert queue

Do all alert queue processing before actually appending
the PacketAlerts to the Packet's alert queue.

Adjusted changes to use macro instead of functions, in cases where the
latter didn't exist in this branch.

Task #4943

(cherry picked from commit faea583d9b34cf8f88b9e40f6e8feed6c4d70758)

3 years agodetect/engine: use alert queue from det_ctx
Juliana Fajardini [Tue, 19 Apr 2022 13:58:22 +0000 (10:58 -0300)] 
detect/engine: use alert queue from det_ctx

Task #4943

(cherry picked from commit 13fd8ced10f3dbbf838462608b6387f4d94cf8e6)

3 years agodetect/alert: add infra for new alert queue
Juliana Fajardini [Mon, 18 Apr 2022 21:14:52 +0000 (18:14 -0300)] 
detect/alert: add infra for new alert queue

Initial work to bring part of the alert queue processing to
DetectEngineThreadCtx.

Task #4943

(cherry picked from commit 88805f03eec038fd67a18599d7d9f037db083941)

3 years agodoc/userguide: explain packet-alert-max config
Juliana Fajardini [Wed, 6 Apr 2022 14:54:52 +0000 (11:54 -0300)] 
doc/userguide: explain packet-alert-max config

Task #4207

(cherry picked from commit 49542d0f1bb35d889f798a50c04f4a257b816695)

3 years agodecode: make packet_alert_max configurable
Juliana Fajardini [Wed, 19 Jan 2022 18:29:39 +0000 (18:29 +0000)] 
decode: make packet_alert_max configurable

The maximum of possible alerts triggered by a unique packet was
hardcoded to 15. With usage of 'noalert' rules, that limit could be
reached somewhat easily. Make that configurable via suricata.yaml.

Conf Bug#4941

Task #4207

(cherry picked from commit 3ace577d5426e2e1afd1cdf0736151022771226c)

3 years agounittests: alloc Packet with PacketGetFromAlloc
Juliana Fajardini [Mon, 2 May 2022 22:22:33 +0000 (19:22 -0300)] 
unittests: alloc Packet with PacketGetFromAlloc

Some unittests used SCMalloc for allocating new Packet the unittests.
While this is valid, it leads to segmentation faults when we move to
dynamic allocation of the maximum alerts allowed to be triggered by a
single packet.

This massive patch uses PacketGetFromAlloc, which initializes a Packet
in such a way that any dynamic allocated structures within will also be
initialized.

Backport: edit a few more files/unittests that were not present in 7.0.x

Related to
Task #4207

(cherry picked from commit ccd45345811dfa4c20a5755f7506dd3e81cfdd38)

3 years agorust: fix doc comments that trigger rust warnings 7453/head
Juliana Fajardini [Thu, 26 May 2022 23:28:41 +0000 (20:28 -0300)] 
rust: fix doc comments that trigger rust warnings

Rust generates warnings that are treated as errors for documentation
blocks before `extern` blocks.

(cherry picked from commit 6ccc01a79c07a5e7f8f46796f026ca50e7b7ee66)

3 years agoutil: add unit tests for CIDRFromMask() 7449/head
Sascha Steinbiss [Wed, 27 Apr 2022 21:12:26 +0000 (23:12 +0200)] 
util: add unit tests for CIDRFromMask()

(cherry picked from commit 5ec6f3ba51c449f7588afac0820ad57b1cea5de9)

3 years agodetect: make int CIDRFromMask() work on big endian platforms
Sascha Steinbiss [Wed, 27 Apr 2022 18:09:50 +0000 (20:09 +0200)] 
detect: make int CIDRFromMask() work on big endian platforms

(cherry picked from commit 394356f73c026879672843fbb20b4de68a09d0ac)

3 years agomemcmp: no-simd no case loop can scan forward
Victor Julien [Fri, 6 May 2022 21:52:00 +0000 (23:52 +0200)] 
memcmp: no-simd no case loop can scan forward

(cherry picked from commit f0479987ff6f5321613395a8bf1a5eb38530b977)

3 years agomemcmp: use SCMEMCMP_BYTES everywhere; general cleanups
Victor Julien [Fri, 6 May 2022 16:20:40 +0000 (18:20 +0200)] 
memcmp: use SCMEMCMP_BYTES everywhere; general cleanups

(cherry picked from commit 0fc7ba45aa6c494bdedd5276e7ec7e4e9012facb)

3 years agomemcmp: remove unreachable code from memcmp simd
Victor Julien [Thu, 5 May 2022 05:16:53 +0000 (07:16 +0200)] 
memcmp: remove unreachable code from memcmp simd

cppcheck:

src/util-memcmp.h:281:18: warning: Identical condition 'len-offset<16', second condition is always false [identicalConditionAfterEarlyExit]
        if (diff < 16) {
                 ^
src/util-memcmp.h:280:24: note: 'diff' is assigned value 'len-offset' here.
        int diff = len - offset;
                       ^
src/util-memcmp.h:269:33: note: If condition 'len-offset<16' is true, the function will return/exit
        if (likely(len - offset < 16)) {
                                ^
src/util-memcmp.h:281:18: note: Testing identical condition 'len-offset<16'
        if (diff < 16) {
                 ^
src/util-memcmp.h:344:18: warning: Identical condition 'len-offset<16', second condition is always false [identicalConditionAfterEarlyExit]
        if (diff < 16) {
                 ^
src/util-memcmp.h:343:24: note: 'diff' is assigned value 'len-offset' here.
        int diff = len - offset;
                       ^
src/util-memcmp.h:318:33: note: If condition 'len-offset<16' is true, the function will return/exit
        if (likely(len - offset < 16)) {
                                ^
src/util-memcmp.h:344:18: note: Testing identical condition 'len-offset<16'
        if (diff < 16) {
                 ^
src/util-memcmp.h:171:18: warning: Identical condition 'len-offset<16', second condition is always false [identicalConditionAfterEarlyExit]
        if (diff < 16) {
                 ^
src/util-memcmp.h:170:24: note: 'diff' is assigned value 'len-offset' here.
        int diff = len - offset;
                       ^
src/util-memcmp.h:159:33: note: If condition 'len-offset<16' is true, the function will return/exit
        if (likely(len - offset < 16)) {
                                ^
src/util-memcmp.h:171:18: note: Testing identical condition 'len-offset<16'
        if (diff < 16) {
                 ^
src/util-memcmp.h:233:18: warning: Identical condition 'len-offset<16', second condition is always false [identicalConditionAfterEarlyExit]
        if (diff < 16) {
                 ^
src/util-memcmp.h:232:24: note: 'diff' is assigned value 'len-offset' here.
        int diff = len - offset;
                       ^
src/util-memcmp.h:208:33: note: If condition 'len-offset<16' is true, the function will return/exit
        if (likely(len - offset < 16)) {
                                ^
src/util-memcmp.h:233:18: note: Testing identical condition 'len-offset<16'
        if (diff < 16) {
                 ^

(cherry picked from commit ca97ed44361fe84cae72fad1807825f149f983eb)

3 years agomemcmp: work around GCC 12+ 'blend' issues
Victor Julien [Fri, 6 May 2022 15:46:40 +0000 (17:46 +0200)] 
memcmp: work around GCC 12+ 'blend' issues

Since GCC 12 the memcmp code using `_mm_blendv_epi8` failed to work.
Inspection of the disassembled objects suggests that it simply omits
the instruction on systems that are not AVX512 capable. On AVX512
it does replace it with VPCMPB logic that appears to work.

Luckily our use of blend is actually uncessary. A simple AND is sufficient.

Bug: #5312.
(cherry picked from commit 87c5d6943709de225d946f5eab8f1d24cdefa491)

3 years agoftp: truncate first segment if over max length 7377/head
Jason Ish [Fri, 22 Apr 2022 18:04:37 +0000 (12:04 -0600)] 
ftp: truncate first segment if over max length

The first segment was not limited to the configured maximum line length
allowing it to be up to 65k. This could result in the next input length
being negative, which while handled properly by the code, did trigger a
debug validation assertion.

The fix is to be consistent and apply the limit to the first segment as
well, which does ensure the input_len could never be less than 0.

Ticket #5281

(cherry picked from commit 9645285dff9eb8313db573d8603162a708736236)

3 years agoja3: fix minor format string warning
Victor Julien [Wed, 27 Apr 2022 09:38:37 +0000 (11:38 +0200)] 
ja3: fix minor format string warning

cppcheck:

src/util-ja3.c:197:28: warning: %d in format string (no. 1) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
        (*buffer)->used += snprintf((*buffer)->data, (*buffer)->size, "%d",
                           ^
src/util-ja3.c:201:28: warning: %d in format string (no. 1) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
        (*buffer)->used += snprintf((*buffer)->data + (*buffer)->used,
                           ^

Bug: #5291.
(cherry picked from commit 1e13f7278585555623ff0be2d3a98476617b1219)

3 years agoaf-packet/v2: use proper type for ring
Victor Julien [Wed, 27 Apr 2022 09:32:22 +0000 (11:32 +0200)] 
af-packet/v2: use proper type for ring

cppcheck:

src/source-af-packet.c:1762:19: warning: Size of pointer 'v2' used instead of size of its data. This is likely to lead to a buffer overflow. You probably intend to write 'sizeof(*v2)'. [pointerSize]
        ptv->ring.v2 = SCMalloc(ptv->req.v2.tp_frame_nr * sizeof (union thdr *));
                  ^
src/source-af-packet.c:1767:26: warning: Size of pointer 'v2' used instead of size of its data. This is likely to lead to a buffer overflow. You probably intend to write 'sizeof(*v2)'. [pointerSize]
        memset(ptv->ring.v2, 0, ptv->req.v2.tp_frame_nr * sizeof (union thdr *));
                         ^

scan-build:

CC       source-af-packet.o
source-af-packet.c:1762:24: warning: Result of 'malloc' is converted to a pointer of type 'char', which is incompatible with sizeof operand type 'union thdr *' [unix.MallocSizeof]
        ptv->ring.v2 = SCMalloc(ptv->req.v2.tp_frame_nr * sizeof (union thdr *));
                       ^~~~~~~~                           ~~~~~~~~~~~~~~~~~~~~~
./util-mem.h:35:18: note: expanded from macro 'SCMalloc'
                 ^~~~~~
1 warning generated.

Bug: #5291.
(cherry picked from commit fedced209dc25443ec5eee22bfab6c99f9f652ab)

3 years agodetect/pcre: assist code analyzer around pointer logic
Victor Julien [Tue, 26 Apr 2022 19:47:37 +0000 (21:47 +0200)] 
detect/pcre: assist code analyzer around pointer logic

cppcheck:

src/detect-pcre.c:381:27: warning: Either the condition 'pcap' is redundant or there is overflow in pointer subtraction. [nullPointerArithmeticRedundantCheck]
            cut_capture = MIN((pcap - regexstr), (fcap - regexstr));
                          ^
src/detect-pcre.c:378:18: note: Assuming that condition 'pcap' is not redundant
        else if (pcap && !fcap)
                 ^
src/detect-pcre.c:381:27: note: Null pointer subtraction
            cut_capture = MIN((pcap - regexstr), (fcap - regexstr));
                          ^

Bug: #5291.
(cherry picked from commit 69b8b48b9422279943c083a24e5baf64e1c4aa94)

3 years agodevice: avoid uninit var warning
Victor Julien [Tue, 26 Apr 2022 19:35:29 +0000 (21:35 +0200)] 
device: avoid uninit var warning

cppcheck:

src/util-device.c:455:17: error: Uninitialized variables: *ndev.dev, *ndev.tenant_id_set, *ndev.id, *ndev.next, *ndev.tenant_id, *ndev.offload_orig [uninitvar]
        *ldev = *ndev;
                ^
src/util-device.c:618:36: note: Calling function 'LiveDeviceForEach', 2nd argument '&ndev' value is <Uninit>
    while(LiveDeviceForEach(&ldev, &ndev)) {
                                   ^
src/util-device.c:455:17: note: Uninitialized variables: *ndev.dev, *ndev.tenant_id_set, *ndev.id, *ndev.next, *ndev.tenant_id, *ndev.offload_orig
        *ldev = *ndev;
                ^

Bug: #5291.
(cherry picked from commit 3bc50df9c3397b9ab06376657c5e8c3467fbc7c5)

3 years agotime: fix warning in timestring creation
Victor Julien [Tue, 26 Apr 2022 19:03:42 +0000 (21:03 +0200)] 
time: fix warning in timestring creation

cppcheck:

src/util-time.c:255:18: warning: Either the condition 'str!=NULL' is redundant or there is possible null pointer dereference: str. [nullPointerRedundantCheck]
        snprintf(str, size, "ts-error");
                 ^
src/util-time.c:252:48: note: Assuming that condition 'str!=NULL' is not redundant
    if (likely(t != NULL && fmt != NULL && str != NULL)) {
                                               ^
src/util-time.c:255:18: note: Null pointer dereference
        snprintf(str, size, "ts-error");
                 ^

Only `t` could possibly be NULL if `localtime_r` fails elsewhere.

Bug: #5291.
(cherry picked from commit 2f48e432cd4465bbb1f42fe7778fee44a5d0aa47)

3 years agodetect/multi-tentancy: minor format string fixes
Victor Julien [Tue, 26 Apr 2022 18:36:36 +0000 (20:36 +0200)] 
detect/multi-tentancy: minor format string fixes

cppcheck:

src/detect-engine.c:3643:5: warning: %d in format string (no. 1) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
    snprintf(prefix, sizeof(prefix), "multi-detect.%d", tenant_id);
    ^
src/detect-engine.c:3707:5: warning: %d in format string (no. 1) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
    snprintf(prefix, sizeof(prefix), "multi-detect.%d.reload.%d", tenant_id, reload_cnt);
    ^
src/detect-engine.c:4086:17: warning: %d in format string (no. 1) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
                snprintf(prefix, sizeof(prefix), "multi-detect.%d", tenant_id);
                ^

Bug: #5291.
(cherry picked from commit 4fcb8740e7b98eee1b5e65bd66045c9440cef8b6)

3 years agoreference: remove useless var reset
Victor Julien [Tue, 26 Apr 2022 18:18:28 +0000 (20:18 +0200)] 
reference: remove useless var reset

cppcheck:

src/util-reference-config.c:179:9: warning: Assignment of function parameter has no effect outside the function. Did you forget dereferencing it? [uselessAssignmentPtrArg]
        fd = NULL;
        ^

Bug: #5291.
(cherry picked from commit 5a0bbb5289cd0fbf4ef50294127cb1018baf82a2)

3 years agompm/ac-ks: address int handling issues
Victor Julien [Tue, 26 Apr 2022 18:14:39 +0000 (20:14 +0200)] 
mpm/ac-ks: address int handling issues

cppcheck:

src/util-mpm-ac-ks.c:1452:5: warning: %d in format string (no. 1) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
    printf("Total states in the state table:    %d\n", ctx->state_count);
    ^
src/util-mpm-ac-ks.c:606:34: error: Signed integer overflow for expression '1<<31'. [integerOverflow]
        encoded_next_state |= (1 << 31);
                                 ^

Bug: #5291.
(cherry picked from commit a8d3cd6eb4f181000256986ea7cf83074a0d2b59)

3 years agoclassification: remove useless clear
Victor Julien [Tue, 26 Apr 2022 18:12:20 +0000 (20:12 +0200)] 
classification: remove useless clear

cppcheck:

src/util-classification-config.c:189:9: warning: Assignment of function parameter has no effect outside the function. Did you forget dereferencing it? [uselessAssignmentPtrArg]
        fd = NULL;
        ^

Bug: #5291.
(cherry picked from commit 9c672a805fe1fe78591ec0f06da56e23c99d0751)

3 years agodetect/content-inspect: code cleanup
Victor Julien [Tue, 26 Apr 2022 18:06:43 +0000 (20:06 +0200)] 
detect/content-inspect: code cleanup

Rearrange code slightly to make it more clear that `found` cannot
be NULL further down the loop.

cppcheck:

src/detect-engine-content-inspection.c:316:50: warning: Either the condition 'found!=NULL' is redundant or there is overflow in pointer subtraction. [nullPointerArithmeticRedundantCheck]
                match_offset = (uint32_t)((found - buffer) + cd->content_len);
                                                 ^
src/detect-engine-content-inspection.c:308:30: note: Assuming that condition 'found!=NULL' is not redundant
            } else if (found != NULL && (cd->flags & DETECT_CONTENT_NEGATED)) {
                             ^
src/detect-engine-content-inspection.c:316:50: note: Null pointer subtraction
                match_offset = (uint32_t)((found - buffer) + cd->content_len);
                                                 ^

Bug: #5291.
(cherry picked from commit 27e9a871d0f7feeafb8fff266b2bb4d97abd39f3)

3 years agodetect/analyzer: minor format string fixes
Victor Julien [Tue, 26 Apr 2022 18:05:51 +0000 (20:05 +0200)] 
detect/analyzer: minor format string fixes

cppcheck flagged this as:

src/detect-engine-analyzer.c:1359:13: warning: %d in format string (no. 1) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
            fprintf(rule_engine_analysis_FD, "    Rule contains %d content options, %d http content options, %d pcre options, and %d pcre options with http modifiers.\n", rule_content, rule_content_http, rule_pcre, rule_pcre_http);
            ^
src/detect-engine-analyzer.c:1359:13: warning: %d in format string (no. 2) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
            fprintf(rule_engine_analysis_FD, "    Rule contains %d content options, %d http content options, %d pcre options, and %d pcre options with http modifiers.\n", rule_content, rule_content_http, rule_pcre, rule_pcre_http);
            ^
src/detect-engine-analyzer.c:1359:13: warning: %d in format string (no. 3) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
            fprintf(rule_engine_analysis_FD, "    Rule contains %d content options, %d http content options, %d pcre options, and %d pcre options with http modifiers.\n", rule_content, rule_content_http, rule_pcre, rule_pcre_http);
            ^
src/detect-engine-analyzer.c:1359:13: warning: %d in format string (no. 4) requires 'int' but the argument type is 'unsigned int'. [invalidPrintfArgType_sint]
            fprintf(rule_engine_analysis_FD, "    Rule contains %d content options, %d http content options, %d pcre options, and %d pcre options with http modifiers.\n", rule_content, rule_content_http, rule_pcre, rule_pcre_http);
            ^

Bug: #5291.
(cherry picked from commit a0847e6c697055b127343cc338029bc69bbc0791)

3 years agodetect/address: remove useless checks
Victor Julien [Tue, 26 Apr 2022 18:04:28 +0000 (20:04 +0200)] 
detect/address: remove useless checks

Cppcheck flagged this:

src/detect-engine-address.c:1035:48: warning: Either the condition 'ghn!=NULL' is redundant or there is possible null pointer dereference: gh. [nullPointerRedundantCheck]
    int r = DetectAddressIsCompleteIPSpaceIPv4(gh->ipv4_head);
                                               ^
src/detect-engine-address.c:1297:17: note: Assuming that condition 'ghn!=NULL' is not redundant
        if (ghn != NULL) {
                ^
src/detect-engine-address.c:1283:44: note: Calling function 'DetectAddressIsCompleteIPSpace', 1st argument 'ghn' value is 0
        if (DetectAddressIsCompleteIPSpace(ghn)) {
                                           ^
src/detect-engine-address.c:1035:48: note: Null pointer dereference
    int r = DetectAddressIsCompleteIPSpaceIPv4(gh->ipv4_head);
                                               ^

Cleanup code could only be reached with non-NULL pointers, so simplify checks.

Bug: #5291.
(cherry picked from commit f8a0f3d9b9f4e1aa758a493e00d38e98a552a0d6)

3 years agodetect/ipv6: remove useless code
Victor Julien [Tue, 26 Apr 2022 18:02:19 +0000 (20:02 +0200)] 
detect/ipv6: remove useless code

Remove useless allocation and free.

Found by cppcheck as a potential issue:

src/detect-engine-address-ipv6.c:385:12: warning: Either the condition 'tmp!=NULL' is redundant or there is possible null pointer dereference: tmp. [nullPointerRedundantCheck]
    memset(tmp,0,sizeof(DetectAddress));
           ^
src/detect-engine-address-ipv6.c:525:13: note: Assuming that condition 'tmp!=NULL' is not redundant
    if (tmp != NULL)
            ^
src/detect-engine-address-ipv6.c:385:12: note: Null pointer dereference
    memset(tmp,0,sizeof(DetectAddress));
           ^

But code turned out not to do anything, so removed.

Bug: #5291.
(cherry picked from commit bad900516133a81afb0a6d3982fa3de5871e6ba7)

3 years agodatasets: fix cppcheck warning
Victor Julien [Tue, 26 Apr 2022 18:01:19 +0000 (20:01 +0200)] 
datasets: fix cppcheck warning

src/datasets.c:107:17: error: Uninitialized variable: hash [uninitvar]
    memcpy(out, hash, outs);
                ^
src/datasets.c:93:26: note: Assuming condition is false
    for (x = 0, i = 0; i < ins; i+=2, x++) {
                         ^
src/datasets.c:107:17: note: Uninitialized variable: hash
    memcpy(out, hash, outs);
                ^

Bug: #5291.
(cherry picked from commit ea2d0ecf08842d52f77d1a88a184a85960999e1e)

3 years agoftp: fix direction of expectation for STOR command
Xiaofan Wang [Thu, 19 Mar 2020 15:50:48 +0000 (23:50 +0800)] 
ftp: fix direction of expectation for STOR command

Fix direction in active mode.

(cherry picked from commit 071f55dcd7892924d193cc1e747bcfa01498202a)

3 years agoftp-data: fix direction for active mode commands
Victor Julien [Sat, 30 Apr 2022 14:54:07 +0000 (16:54 +0200)] 
ftp-data: fix direction for active mode commands

Set correct direction for PORT mode, where the server connects
to the client.

The direction is not also strictly enforced. No data in the wrong
direction will be accepted to setup the file or to be added to the
file after setup.

This also fixes files getting closed twice.

Adds some general cleanups.

Bug: #3542.
(cherry picked from commit 07bf9214513e54e04508c055bb8ed29aa3bce60f)

3 years agodetect: fix rule inspection order 7325/head
Victor Julien [Mon, 25 Apr 2022 16:00:24 +0000 (18:00 +0200)] 
detect: fix rule inspection order

Fix rules from the 'match' list getting added to the tx candidates list
unsorted. In some cases this could lead to the same sid getting inspected
twice leading to a DEBUG_VALIDATION_BUG_ON trigger.

Bug: #5144.
(cherry picked from commit 4bb00964ac22f0f96704cf5befca76d056763142)

3 years agostream: improve flow end payload logging
Victor Julien [Sat, 23 Apr 2022 11:59:34 +0000 (13:59 +0200)] 
stream: improve flow end payload logging

Use all available data, including un-ACK'd, when in flow timeout
mode.

Bug: #5276.
(cherry picked from commit c40df43609becc467e86d756098829b3174c80c7)

3 years agosuricata/init: Remove stack-on-signal code
Jeff Lucovsky [Tue, 26 Apr 2022 19:04:48 +0000 (15:04 -0400)] 
suricata/init: Remove stack-on-signal code

Issue: 5229

This commit removes functionality never intended for the 5.0.x
maintenance branch: stack-on-signal logging

3 years agouserguide: dynamically determine copyright date
Juliana Fajardini [Wed, 9 Feb 2022 19:46:24 +0000 (19:46 +0000)] 
userguide: dynamically determine copyright date

This uses the date of doc generation to determine the copyright date
for the trailing date. Based on Jeff Lucovsky solution.

(cherry picked from commit e0c8dba7ac6eaa757daf80245688f9f2b2496eff)

3 years agoflow-manager: fix off-by-one in flow_hash row allocation
Arne Welzel [Sat, 12 Feb 2022 16:49:07 +0000 (17:49 +0100)] 
flow-manager: fix off-by-one in flow_hash row allocation

The current code doesn't cover all rows when more than one flow manager is
used. It leaves a single row between ftd->max and ftd->min of the next
manager orphaned. As an example:

    hash_size=1000
    flowmgr_number=3
    range=333

    instance  ftd->min  ftd->max
    0         0         333
    1         334       666
    2         667       1000

    Rows not covered: 333, 666

(cherry picked from commit 8ef066318d7c4dd9b6686dbebd621c790828c384)

3 years agoutil/atomic: Fetch and add interface
Jeff Lucovsky [Mon, 25 Apr 2022 12:39:06 +0000 (08:39 -0400)] 
util/atomic: Fetch and add interface

This commit adds an API for an atomic fetch and add function to be
consistent with later branches where SC_ATOMIC_ADD has that
functionality.

3 years agodetect/dataset: fix space condition in rule lang
Shivani Bhardwaj [Fri, 28 Jan 2022 20:17:17 +0000 (01:47 +0530)] 
detect/dataset: fix space condition in rule lang

If there is a space following a keyword that does not expect a value,
the rule fails to load due to improper value evaluation.
e.g. Space after "set" command
alert http any any -> any any (http.user_agent; dataset:set  ,ua-seen,type string,save datasets.csv; sid:1;)

gives error
[ERRCODE: SC_ERR_UNKNOWN_VALUE(129)] - dataset action "" is not supported.

Fix this by handling values correctly for such cases.

(cherry picked from commit 6d2a2a073120906304f70c0e120565eae96e36b8)

3 years agodetect/dataset: cleanup dead code
Shivani Bhardwaj [Fri, 28 Jan 2022 13:43:04 +0000 (19:13 +0530)] 
detect/dataset: cleanup dead code

(cherry picked from commit 7366396011227b5e51ca810d86ebf7d310e98f34)

3 years agostream/unittests: fix failures after last_ack fix
Victor Julien [Fri, 22 Apr 2022 17:33:13 +0000 (19:33 +0200)] 
stream/unittests: fix failures after last_ack fix

Work around many tests not setting up stream completely or correctly.

(cherry picked from commit 3d6e733aa7297f64fe87cc3b59fbbd9a527513fa)

3 years agostream: improve last_ack validation check
Victor Julien [Fri, 22 Apr 2022 16:27:15 +0000 (18:27 +0200)] 
stream: improve last_ack validation check

If a packet after the initialization would come with ACK flag set
but a ACK value of 0, the last_ack tracking could get confused. Fix
this by not checking for 0 but instead checking if the ACK flag
has been seen.

Bug: #4549.
(cherry picked from commit 1f43e1477f3c5781381e75f4bed918cbc6c18dfd)

3 years agoversion: start development towards 5.0.10
Victor Julien [Wed, 27 Apr 2022 09:13:52 +0000 (11:13 +0200)] 
version: start development towards 5.0.10

3 years agorelease: 5.0.9; update changelog; require htp 0.5.40 suricata-5.0.9
Victor Julien [Thu, 21 Apr 2022 08:52:23 +0000 (10:52 +0200)] 
release: 5.0.9; update changelog; require htp 0.5.40

3 years agossl: cert parsing hardening
Jeff Lucovsky [Thu, 10 Mar 2022 14:09:57 +0000 (15:09 +0100)] 
ssl: cert parsing hardening

Limit initial memory allocated for certificates. General parsing
hardening and checking improvements.

Based on commits:
862e84877ff262cd4b8c4b191a8710f94f63fcf7
3ed188e0bcb6f4ae5b6c5eafdd75ce1f8e3d7246

Bug: #5193.

3 years agossl: validation macros
Jeff Lucovsky [Mon, 18 Apr 2022 15:20:58 +0000 (11:20 -0400)] 
ssl: validation macros

Include valiation macros from 3ed188e0bc

3 years agossl: copy data using a safe memcpy wrapper
Victor Julien [Fri, 3 Apr 2020 14:31:00 +0000 (16:31 +0200)] 
ssl: copy data using a safe memcpy wrapper

To avoid future memcpy issues introduce a wrapper and check the
result of it.

When compiled with --enable-debug-validation the wrapper will abort if
the input is wrong.

(cherry picked from commit d1ada2e13c207e0937f8a4818d5731d319f5fa07)

3 years agodoc/userguide: document ftp max-line-length
Jason Ish [Thu, 7 Apr 2022 21:58:58 +0000 (15:58 -0600)] 
doc/userguide: document ftp max-line-length

(cherry picked from commit 7d6bc60abb6eefce3f0d20222b2a843de9ea5a6e)

3 years agoftp: truncate command data that is too long
Jason Ish [Wed, 6 Apr 2022 21:38:35 +0000 (15:38 -0600)] 
ftp: truncate command data that is too long

FTP control commands will be buffered forever until a new line is seen,
this can lead to memory exhaustion in Suricata.

To fix, set an upper bound, 4096 bytes on the size of the command that
is saved in the transaction. The input continues to be parsed to find
the end of the command so the parser can continue to move onto the next
command.

The result is that the command data in the transaction is truncated,
which also shows up in the ftp transaction logs.

This value is configurable with the max-line-length field in the ftp
app-layer.protocols section.

As FTP doesn't have events at this time, add a new fields to eve-log
that specificy if the request, or the response has been truncated.

Ticket #5024

(cherry-picked from commit 433a0b8e3b0bf206069ebb94bc4cb71044572791)

3 years agosmtp: don't pass partial boundary on to mime parser
Victor Julien [Fri, 15 Apr 2022 13:51:10 +0000 (15:51 +0200)] 
smtp: don't pass partial boundary on to mime parser

If the start of a line looks like it might be a mime boundary we
yield to the get line logic if we don't have enough data to be
conclusive.

(cherry picked from commit e7417a8e96fcd23fa9e3b529d7c2bbd7b3efb928)

3 years agomime: allow partial lines as input
Victor Julien [Fri, 15 Apr 2022 13:49:09 +0000 (15:49 +0200)] 
mime: allow partial lines as input

If we get a zero length delim we assume its a partial line and we
won't append CRLF just yet.

(cherry picked from commit 6e800a8548d9d2699589cac6afca3c0fa7613202)

3 years agosmtp: pre process DATA and BDAT commands
Shivani Bhardwaj [Tue, 19 Apr 2022 15:33:24 +0000 (21:03 +0530)] 
smtp: pre process DATA and BDAT commands

The input data received in DATA and BDAT command modes can be huge and
could have important data, like a legit huge email. Therefore, exempt
these from the line buffering limits which were introduced to regulate
the size of lines that we buffer at any point in time.

As a part of this patch, anything that comes under DATA or BDAT is
processed early without buffering as and when it arrives. The ways of
processing remain the same as before.

(cherry picked from commit cf749fd450ca41dc001c2a1c10d8d17500dcedce)

3 years agosmtp: fix indefinite buffering if no LF in line
Shivani Bhardwaj [Tue, 19 Apr 2022 12:05:06 +0000 (17:35 +0530)] 
smtp: fix indefinite buffering if no LF in line

Issue
-----
So far, with the SMTP parser, we would buffer data up until an LF char
was found indicating the end of one line. This would happen in case of
fragmented data where a line might come broken into multiple chunks.
This was problematic if there was a really long line without any LF
character. It would mean that we'd keep buffering data up until we
encounter one such LF char which may be many many bytes of data later.

Fix
---
Fix this issue by setting an upper limit of 4KB on the buffering of
lines. If the limit is reached then we save the data into current line
and process it as if it were a regular request/response up until 4KB
only. Any data after 4KB is discarded up until there is a new LF char in
the received input.

Cases
-----
1. Fragmentation
The limit is enforced for any cases where a line of >= 4KB comes as diff
fragments that are each/some < 4KB.
2. Single too long line
The limit is also enforced for any cases where a single line exceeds the
limit of buffer.

Reported by Victor Julien.
Ticket 5028

3 years agodns: don't parse a full request during probe if not enough data
Jason Ish [Mon, 28 Feb 2022 22:48:34 +0000 (16:48 -0600)] 
dns: don't parse a full request during probe if not enough data

If there is more data than a header, but not enough for a complete DNS
message, the hostname parser could return an error causing the probe to
fail on valid DNS messages.

So only parse the complete message if we have enough input data. This is
reliable for TCP as DNS messages are prefixed, but for UDP its just
going to be the size of the input buffer presented to the parser, so
incomplete could still happen.

Ticket #5034

(cherry picked from commit 27679a12aa4e03e960112f387640419d29780e5a)

3 years agodns: improve probing parser
Philippe Antoine [Tue, 9 Mar 2021 20:00:36 +0000 (21:00 +0100)] 
dns: improve probing parser

Checks opcode is valid
Checks additional_rr do not exceed message length
Better logic for incomplete cases

(cherry picked from commit 9e7ea631b2a067609c500539cd3a7a139f39c3e4)

3 years agodns: better error handling when parsing names
Jason Ish [Tue, 1 Feb 2022 21:44:43 +0000 (15:44 -0600)] 
dns: better error handling when parsing names

The DNS name parser will error out with an error even if the
error is incomplete. Instead of manually generating errors,
use '?' to let the nom error ripple up the error handling chain.

The reason this wasn't done in the first place is this code
predates the ? operator, or we were not aware of it at the time.

This prevents the case where probing fails when there is enough data to
parse the header, but not enough to complete name parser. In such a case
a parse error is returned (instead of incomplete) resulting in the
payload not being detected as DNS.

Ticket #5034

(cherry picked from commit 0623ada24df1da99c72bb8cd4959b2cb0e64ccc2)

3 years agotftp: use destate
Philippe Antoine [Mon, 22 Nov 2021 09:37:33 +0000 (10:37 +0100)] 
tftp: use destate

And avoids memory leaks on it

3 years agodnp3: check Base64Encode return value for logging
Philippe Antoine [Mon, 29 Nov 2021 12:24:12 +0000 (13:24 +0100)] 
dnp3: check Base64Encode return value for logging

So that NULL pointers do not get logged

Ticket: 4849

3 years agodnp3: sync actual code with generated code
Jason Ish [Fri, 1 Apr 2022 18:22:41 +0000 (12:22 -0600)] 
dnp3: sync actual code with generated code

3 years agoutil: right parenthesises for base64 macro
Philippe Antoine [Mon, 29 Nov 2021 12:20:40 +0000 (13:20 +0100)] 
util: right parenthesises for base64 macro

So that BASE64_BUFFER_SIZE(3) == 5 instead of 7

3 years agoutil/crypt: Add macro for max base64encode len
Shivani Bhardwaj [Thu, 3 Dec 2020 11:25:39 +0000 (16:55 +0530)] 
util/crypt: Add macro for max base64encode len

Maximum length of a base64 encoded string can be 33% over the actual
length of the input string. The formula to best cover all the edge cases
is mathematically
(4 * (input_length + 2) / 3) + 1

Add a macro to calculate this for a given input length.

(cherry picked from commit 02942a123a525bb1ed97bba9242f1e08d54f0fb8)