fix: init log msg_buf to avoid uninit read#440
Draft
MarkAtwood wants to merge 1 commit into
Draft
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes an uninitialized stack read (and potential information leak via the iterate callback) in the POSIX file logging backend’s posixLogFile_Iterate() when parsing a log line whose message field is empty.
Changes:
- Zero-initialize
msg_bufso an empty MESSAGE field results inmsg_len == 0instead ofstrlen()reading uninitialized memory.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| char file_buf[256]; | ||
| char func_buf[256]; | ||
| char msg_buf[WOLFHSM_CFG_LOG_MSG_MAX]; | ||
| char msg_buf[WOLFHSM_CFG_LOG_MSG_MAX] = {0}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
In
posixLogFile_Iterate(port/posix/posix_log_file.c),msg_bufis declared uninitialized:The parse format has 6 conversions:
A log line with an empty message (
TS|LEVEL|FILE:LINE|FUNC|\n) makes the final%255[^\n]scanset match zero characters, so sscanf returns 5 andmsg_bufis never written. The guard is onlyparsed >= 5, soentry.msg_len = strlen(msg_buf)then reads uninitialized stack memory andmemcpycopies that many garbage bytes intoentry.msg— undefined behavior and a stale-stack information leak to the callback.Fix
Zero-initialize
msg_bufso an unpopulated message field yields an empty string;strlenreturns 0 andmemcpycopies nothing.Verification
WOLFHSM_CFG_LOGGINGenabled (gcc, EXIT 0,posixLogFile_Iteratesymbol emitted) before and after the change.msg_len; a control line with a real message parsed cleanly (parsed=6, no error). After zero-init, the uninitialized read is gone.Reported by static analysis (Fenrir finding 151).
[fenrir-sweep:held]