Fix memory-safety and correctness bugs surfaced by Coverity audit (part 4) - #22270
Conversation
Coverity CID 410102 (TAINTED_SCALAR): copy_and_convert_key() cast PCRE2 name-table bytes to unsigned instead of unsigned char, which can turn non-ASCII UTF bytes in named groups into huge indexes on signed-char builds. Cast through unsigned char before indexing journal_key_characters_map.
Coverity CID 410217 (TAINTED_SCALAR): logfmt escape parsing advanced past the terminating NUL when a key or value ended with a lone backslash. Treat trailing backslashes as literal characters so the parser stays in-bounds without changing valid escape handling.
Coverity CID 439982 (STRING_OVERFLOW): reject AF_UNIX socket paths that do not fit in sun_path before the nofork spawn server stores or uses them. The nofork connect and bind paths now use bounded copies after that validation, fixing both copies of the same root cause.
Fail with an explicit error message when the formatted socket path would not fit in the local buffer. This catches the truncation case before the AF_UNIX length check and produces a more actionable log line.
There was a problem hiding this comment.
No issues found across 6 files
Confidence score: 5/5
- Automated review surfaced no issues in the provided summaries.
- No files require special attention.
Architecture diagram
sequenceDiagram
participant LogSrc as Log Source
participant L2J as log2journal (logfmt/PCRE2)
participant Map as journal_key_characters_map
participant Parent as Parent Process
participant SpawnSrv as Spawn Server (nofork)
participant OS as OS (Filesystem/Sockets)
Note over LogSrc, Map: Log Parsing Flow (Memory Safety Fixes)
LogSrc->>L2J: Provide log line (e.g. key=value\)
L2J->>L2J: logftm_parse_value()
opt NEW: Trailing backslash detected
L2J->>L2J: Treat '\' as literal char (prevent OOB read)
end
L2J->>Map: Lookup character index
Note right of Map: CHANGED: Indexing now uses (unsigned char)<br/>to prevent sign-extension/large offsets.
Map-->>L2J: Validated Journal Key char
Note over Parent, OS: Spawn Server Lifecycle (Validation Fixes)
Parent->>SpawnSrv: spawn_server_create(name)
SpawnSrv->>SpawnSrv: Generate socket path (snprintf)
alt NEW: Path exceeds buffer or AF_UNIX limit
SpawnSrv-->>Parent: Return NULL (Log truncation/length error)
else Path is valid
SpawnSrv->>OS: unlink(path)
SpawnSrv->>OS: NEW: spawn_server_set_unix_socket_path()
Note right of OS: Uses bounded strncpyz & length checks
SpawnSrv->>OS: socket(AF_UNIX) & bind()
SpawnSrv-->>Parent: Server Handle
end
Parent->>SpawnSrv: connect_to_spawn_server(path)
SpawnSrv->>SpawnSrv: NEW: validate path length < sun_path
alt NEW: Invalid Path Length
SpawnSrv-->>Parent: Return -1 (ENAMETOOLONG)
else Valid
SpawnSrv->>OS: connect(sock, addr)
SpawnSrv-->>Parent: Connection FD
end
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Addresses memory-safety and correctness findings from a Coverity audit by hardening AF_UNIX socket path handling in the nofork spawn server and fixing log2journal parsing edge-cases (including trailing backslashes), plus adding a regression test.
Changes:
- Add AF_UNIX socket path length validation and bounded copies for spawn server connect/listen and socket path generation.
- Fix logfmt parsing to treat trailing backslashes as literals (avoid out-of-bounds reads) and tighten quote handling.
- Prevent out-of-range indexing in PCRE2 key conversion by casting input bytes to
unsigned char; add a logfmt regression test case.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/libnetdata/spawn_server/spawn_server_nofork.c | Introduces bounded AF_UNIX socket-path handling and improved path-generation error handling. |
| src/collectors/log2journal/tests.d/logfmt-trailing-backslash.yaml | Adds a new logfmt test case descriptor. |
| src/collectors/log2journal/tests.d/logfmt-trailing-backslash.input | Adds input fixture covering trailing backslash behavior. |
| src/collectors/log2journal/tests.d/logfmt-trailing-backslash.output | Adds expected output fixture for trailing backslash behavior. |
| src/collectors/log2journal/log2journal-pcre2.c | Avoids invalid indexing by casting key bytes to unsigned char. |
| src/collectors/log2journal/log2journal-logfmt.c | Fixes edge-case parsing to keep reads in-bounds for trailing backslashes in keys/values. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
thiagoftsm
left a comment
There was a problem hiding this comment.
PR is running as expected. No issues found during runtime. LGTM!
…rt 4) (#22270) * log2journal: fix utf named-group key indexing Coverity CID 410102 (TAINTED_SCALAR): copy_and_convert_key() cast PCRE2 name-table bytes to unsigned instead of unsigned char, which can turn non-ASCII UTF bytes in named groups into huge indexes on signed-char builds. Cast through unsigned char before indexing journal_key_characters_map. * log2journal: handle trailing logfmt escapes safely Coverity CID 410217 (TAINTED_SCALAR): logfmt escape parsing advanced past the terminating NUL when a key or value ended with a lone backslash. Treat trailing backslashes as literal characters so the parser stays in-bounds without changing valid escape handling. * spawn_server: reject oversized unix socket paths Coverity CID 439982 (STRING_OVERFLOW): reject AF_UNIX socket paths that do not fit in sun_path before the nofork spawn server stores or uses them. The nofork connect and bind paths now use bounded copies after that validation, fixing both copies of the same root cause. * spawn_server: detect snprintf truncation of socket path Fail with an explicit error message when the formatted socket path would not fit in the local buffer. This catches the truncation case before the AF_UNIX length check and produces a more actionable log line. * Fix log message --------- Co-authored-by: Costa Tsaousis <costa@netdata.cloud> (cherry picked from commit d3139a8)



Summary
Summary by cubic
Fixes memory-safety and correctness issues flagged by Coverity in log2journal and the nofork spawn server, preventing out-of-bounds reads and rejecting invalid AF_UNIX socket paths. Adds a test for trailing backslashes in logfmt and improves error logs.
journal_key_characters_mapwith(unsigned char)to avoid large indexes on non-ASCII bytes.sun_path; detect and fail onsnprintftruncation; fix an error log message.logfmt-trailing-backslashcase to lock behavior.Written for commit 023dc8f. Summary will update on new commits.