Fix memory corruption in mkstr caused by integer overflow#527
Merged
Conversation
The static 'slot' variable in mkstr() was previously a signed 32-bit integer. On high-load systems (e.g., during status data dumps), this counter could overflow, becoming negative. In C, a negative dividend with the modulo operator (slot % 256) results in a negative index. This caused the 'ret' pointer to point to memory addresses BEFORE the actual buffer. In our case, this led to a SIGSEGV because a timestamp string was written directly over the 'contact_list' pointer, which happened to be located in that memory region. Changes: - Changed 'slot' from signed int to unsigned int (uint) to ensure the modulo result is always positive and within buffer bounds. - This ensures that upon reaching UINT_MAX, the counter wraps safely back to zero. Signed-off-by: nook24 <d.ziegler@avendis.com>
sni
approved these changes
Mar 17, 2026
Cast tv_sec to long long and tv_usec to long to ensure consistent behavior across 32-bit and 64-bit architectures and avoid format string warnings/mismatches. Signed-off-by: nook24 <d.ziegler@avendis.com>
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.
We have noticed random segfaults on one of our systems during the creating of
status.dat.Since Naemon 1.5.0,
tv_stris called in the For-loop for each host and service status object, which will increase the counter of slot by one each time. After2.147.483.647the integer will flip into-2.147.483.648and modulo of a negativ dividend will result in a negativ value in C (and PHP, Go JavaScript).When reading
buf[-2147468803]whatever is at this address will be overwritten.This is a stripped down version of #526 which will keep the
tv_strmethod as the issue was within the underlyingmkstr.Some thoughts and discoveries:
I would suggest to replace
tv_strwith a version which will not depend on a rolling buffer like soAlso
fprintfcan do this as wellIn addition,
mkstris not thread-safe which is irrelevant for Naemon, but could be relevant when a broker module with threads is the caller. To resolve this, we could use the__threadkeyword like soIn this case, we have to link
pthreadas well in theMakefile.amFor all binaries
Or only for Naemon