Gcc analyzer/v3 - #15612
Conversation
To assist code analyzers. Gcc -fanalyzer got confused about it. Also test data pointer and length before calling fwrite and check the result better. Use a single atomic for the max open files check.
Code analyzer flagged FrameCopy as a possible source of UB due to
both pointers passed to memcpy being the same.
app-layer-frames.c: In function ‘FrameCopy’:
app-layer-frames.c:236:5: warning: overlapping buffers passed as arguments to ‘memcpy’ [-Wanalyzer-overlapping-buffers]
236 | memcpy(dst, src, sizeof(*dst));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
‘FramePrune’: events 1-8
│
│ 750 | static void FramePrune(Frames *frames, const TcpStream *stream, const bool eof)
│ | ^~~~~~~~~~
│ | |
│ | (1) entry to ‘FramePrune’
│......
│ 766 | for (uint16_t i = 0; i < frames->cnt; i++) {
│ | ~~~~~~~~~~~~~~~
│ | |
│ | (2) following ‘true’ branch... ─>─┐
│ | │
│ | │
│ |┌─────────────────────────────────────────────────────────────┘
│ 767 |│ if (i < FRAMES_STATIC_CNT) {
│ |│ ~
│ |│ |
│ |└──────────>(3) ...to here
│ | (4) following ‘true’ branch (when ‘i <= 2’)... ─>─┐
│ | │
│ | │
│ |┌─────────────────────────────────────────────────────────────┘
│ 768 |│ Frame *frame = &frames->sframes[i];
│ |│ ~~~~~~~~~~~~~~~~~~
│ |│ |
│ |└──────────────────────────────────────────>(5) ...to here
│ 769 | FrameDebug("prune(s)", frames, frame);
│ 770 | if (eof || FrameIsDone(frame, acked)) {
│ | ~
│ | |
│ | (6) following ‘false’ branch... ─>─┐
│ | │
│......
│ | │
│ |┌──────────────────────────────────────────────────┘
│ 779 |│ const uint64_t fle = FrameLeftEdge(stream, frame);
│ |│ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
│ |│ |
│ |└────────────────────────────────────>(7) ...to here
│ | (8) calling ‘FrameLeftEdge’ from ‘FramePrune’
│
└──> ‘FrameLeftEdge’: event 9
│
│ 257 | static inline uint64_t FrameLeftEdge(const TcpStream *stream, const Frame *frame)
│ | ^~~~~~~~~~~~~
│ | |
│ | (9) entry to ‘FrameLeftEdge’
│
‘FrameLeftEdge’: event 10
│
│suricata-common.h:323:27:
│ 323 | #define BUG_ON(x) assert(!(x))
│ | ^~~~~~
│ | |
│ | (10) following ‘false’ branch (when ‘frame_offset <= app_progress’)... ─>─┐
│ | │
util-validate.h:95:36: note: in expansion of macro ‘BUG_ON’
│ 95 | #define DEBUG_VALIDATE_BUG_ON(exp) BUG_ON((exp))
│ | ^~~~~~
app-layer-frames.c:266:5: note: in expansion of macro ‘DEBUG_VALIDATE_BUG_ON’
│ 266 | DEBUG_VALIDATE_BUG_ON(frame_offset > app_progress);
│ | ^~~~~~~~~~~~~~~~~~~~~
│
‘FrameLeftEdge’: event 11
│
│ | │
│ |┌────────────────────────────────────────────────────────────────────────────────────────────────────┘
│ 269 |│ if (frame->len < 0) {
│ |│ ~~~~~^~~~~
│ |│ |
│ |└────────────>(11) ...to here
│
<──────┘
│
‘FramePrune’: events 12-13
│
│ 779 | const uint64_t fle = FrameLeftEdge(stream, frame);
│ | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
│ | |
│ | (12) returning to ‘FramePrune’ from ‘FrameLeftEdge’
│......
│ 783 | FrameCopy(nframe, frame);
│ | ~~~~~~~~~~~~~~~~~~~~~~~~
│ | |
│ | (13) calling ‘FrameCopy’ from ‘FramePrune’
│
└──> ‘FrameCopy’: events 14-15
│
│ 234 | static void FrameCopy(Frame *dst, Frame *src)
│ | ^~~~~~~~~
│ | |
│ | (14) entry to ‘FrameCopy’
│ 235 | {
│ 236 | memcpy(dst, src, sizeof(*dst));
│ | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
│ | |
│ | (15) ⚠️ overlapping buffers passed as arguments to ‘memcpy’
│
In file included from suricata-common.h:129,
from app-layer-frames.c:25:
/usr/include/string.h:47:14: note: the behavior of ‘memcpy’ is undefined for overlapping buffers
47 | extern void *memcpy (void *__restrict __dest, const void *__restrict __src,
| ^~~~~~
Work around TAILQ false positive.
Work around TAILQ false positive.
Addresses a gcc -fanalyzer warning.
Add debug validation statement to assert prev pointer is not NULL.
Helps address a gcc -fanalyzer warning.
Reopen file descriptor for lz4 with the init function. This helps code analyzers understand the handle it's leaked. Improve flow of profiling dumps to avoid analyzer confusion around the file descriptor. Suppress TAILQ related warnings.
By teaching about TAILQ.
The double strchr confused gcc -fanalyzer.
Avoids gcc -fanalyzer warning about out of bounds write to the array.
Help it understand TAILQ.
Pointer can't be NULL, so don't check it. Helps gcc analyzer as well.
Make sure to not run against lua rust crate build, as it's not clean.
Allows to use datasets load
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #15612 +/- ##
==========================================
- Coverage 82.89% 82.87% -0.03%
==========================================
Files 1006 1006
Lines 273487 273528 +41
==========================================
- Hits 226710 226673 -37
- Misses 46777 46855 +78
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
| ff->fd = -1; | ||
| close_file = true; /* not storing it, so need to close */ | ||
| } else if (FileGetMaxOpenFiles() == 0) { | ||
| close_file = true; /* max open files 0 means we immediately close */ |
There was a problem hiding this comment.
We now pass OISF/suricata-verify#3160, previous PR did not.
| } | ||
| fclose(comp->file); | ||
| comp->file = NULL; | ||
| return TM_ECODE_FAILED; |
There was a problem hiding this comment.
IF pcap_dump_fopen happens to fail (a rarity), do we now leak this just opened `comp->pcap_buf_wrapper. It was re-used before, which might actually have been wrong as well.
There was a problem hiding this comment.
Good catch. The error handling for the lz4 is a bit odd in general. Right below this it catches 2 other errors that do not lead to closing of the handles, not sure if that is correct.
|
Information: QA ran without warnings. Pipeline = 32001 |
|
Replaced by #15634 |
Gcc analyzer fixes and CI test.
Replaces #15607, fixing a logic error in the filestore fd handling.