Skip to content

fix null pointer dereferences found by malloc-error-check cocci - #15529

Closed
kenifor wants to merge 5 commits into
OISF:mainfrom
kenifor:fix-redis-stream-format-alloc
Closed

fix null pointer dereferences found by malloc-error-check cocci#15529
kenifor wants to merge 5 commits into
OISF:mainfrom
kenifor:fix-redis-stream-format-alloc

Conversation

@kenifor

@kenifor kenifor commented Jun 3, 2026

Copy link
Copy Markdown

Summary

  • Fixed broken regex alternation in qa/coccinelle/malloc-error-check.cocci: identifier func =~ "(SCMalloc|...)" uses PCRE-style syntax, but Coccinelle uses OCaml Str where | and () are literal — so @malloced never matched anything. Replaced with SCMalloc\|SCStrdup\|... syntax.
  • Fixed all unchecked allocations surfaced by the now-working script:
    • src/util-log-redis.c: guard SCCalloc result for stream_format
    • src/decode.c: replace DEBUG_VALIDATE_BUG_ON with FatalError in PacketAlertCreate (debug-only check was a no-op in production)
    • src/detect-engine-alert.c: guard SCStrdup result before use
    • src/detect-flowbits.c: check SCRealloc result before overwriting the original pointer
    • src/detect-reference.c: guard both SCStrdup calls with goto error
    • src/util-mpm-hs.c: remove extra parentheses causing false negative; add FatalError in SCHSConfigInit

Full src/*.c scan is clean after these changes.

This is a continuation of #15423 (closed per the GitHub PR workflow policy after branch was updated).

Denis Balashov added 4 commits May 20, 2026 12:07
When Redis output is configured in stream/xadd mode with a positive
stream-maxlen, SCConfLogOpenRedis() allocates redis_setup.stream_format
and immediately passes it to snprintf().

If SCCalloc() fails, snprintf() receives a NULL destination pointer and
the process can crash during Redis output initialization. Handle this
unrecoverable setup failure with FatalError(), matching the surrounding
Redis initialization error handling.

Ticket: 8588
…unction args

The @istested rule's '... when != x' only excluded reassignments of x,
not statements that passed x as a function argument. Add
'when != callee(..., x, ...)' so that any use of the allocated pointer
as a function argument before a NULL check is no longer treated as
tested and is correctly flagged.
The identifier regex used PCRE-style '(A|B)' syntax, but Coccinelle
uses OCaml Str where '|' is a literal character. Replace with 'A\|B'
so the pattern correctly matches all SC*alloc functions.
- decode: replace DEBUG_VALIDATE_BUG_ON with FatalError in PacketAlertCreate
- detect-engine-alert: guard SCStrdup result before use
- detect-flowbits: check SCRealloc result before overwriting original pointer
- detect-reference: guard SCStrdup results in DetectReferenceParse
- util-mpm-hs: fix false cocci negative (remove extra parens), add
  FatalError in SCHSConfigInit
@kenifor
kenifor requested review from a team and victorjulien as code owners June 3, 2026 10:35
@victorjulien

Copy link
Copy Markdown
Member

These are nice finds. Can you split the last commit "fix null pointer dereferences found by malloc-error-check cocci" to have a commit per logical unit, like mpm/hs: ... and detect/alert: ....

@github-actions

github-actions Bot commented Jun 3, 2026

Copy link
Copy Markdown

NOTE: This PR may contain new authors.

@codecov

codecov Bot commented Jun 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 53.84615% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.84%. Comparing base (bb4e79c) to head (207594e).
⚠️ Report is 208 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #15529      +/-   ##
==========================================
+ Coverage   82.65%   82.84%   +0.18%     
==========================================
  Files         996      999       +3     
  Lines      271109   272618    +1509     
==========================================
+ Hits       224076   225841    +1765     
+ Misses      47033    46777     -256     
Flag Coverage Δ
fuzzcorpus 61.47% <30.00%> (+0.44%) ⬆️
livemode 18.40% <10.00%> (+0.02%) ⬆️
netns 22.78% <10.00%> (+0.16%) ⬆️
pcap 45.16% <20.00%> (-0.06%) ⬇️
suricata-verify 66.54% <50.00%> (+0.14%) ⬆️
unittests 58.48% <30.76%> (-0.08%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@victorjulien

Copy link
Copy Markdown
Member

@catenacyber remaining warning:
Structure malloced at /__w/suricata/suricata/src/tests/fuzz/fuzz_decodebase64.c:21 but error is not checked.

Do you think this needs to be addressed? Or should we exclude the fuzz targets?

Comment thread src/decode.c Outdated
PacketAlert *pa_array = SCCalloc(packet_alert_max, sizeof(PacketAlert));
DEBUG_VALIDATE_BUG_ON(pa_array == NULL);
if (unlikely(pa_array == NULL)) {
FatalError("Failed to allocate packet alert array");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cannot have FatalError in the packet path

Is the good fix to have this allocated at the same time than the Packet structure ?

PacketAlertCreate is called from PacketInit which is on the packet
allocation path. Using FatalError there is inappropriate — on allocation
failure the packet should be dropped, not the process terminated.

Make PacketInit return bool and propagate the NULL result from
PacketAlertCreate up through PacketGetFromAlloc, which already returns
NULL to signal allocation failure to its callers.

Update the four UNITTESTS-only helpers in defrag.c accordingly.
@catenacyber catenacyber added the needs rebase Needs rebase to main label Jun 4, 2026

@catenacyber catenacyber left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will need a rebase

@catenacyber

Copy link
Copy Markdown
Contributor

Replaced by #15552

@catenacyber catenacyber closed this Jun 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs rebase Needs rebase to main

Development

Successfully merging this pull request may close these issues.

3 participants