Fix OOB stack read in send_ipc_command due to misuse of snprintf return value#234
Merged
pgaskin merged 1 commit intoJun 12, 2026
Merged
Conversation
…rn value snprintf(3) returns the number of bytes it *would* have written had the buffer been large enough, not the number actually written. When the formatted command exceeds 255 bytes the buffer is silently truncated, but packet_len still holds the full would-be length. send_packet then passes (packet_len + 1) to send_in_full, which calls send(2) with a length larger than buf, causing an out-of-bounds stack read. Fix by checking for truncation (packet_len >= sizeof(buf)) and returning KFMON_IPC_SEND_FAILURE instead of proceeding with the corrupted length. This turns a silent garbled-command failure into a clear, returnable error.
Collaborator
|
Did a bit of archeology, best as I can think, I probably recycled that from KFMon code itself, which can afford to skip the overflow check, because it only deals with either string literals, or known fixed-length buffers that are much smaller than the chosen buffer size (which also happens to be 4K in KFMon). TL;DR: Yeah, definitely a boo-boo ;). |
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
snprintf(3)returns the number of bytes it would have written had the buffer been large enough — not the number actually written. Insend_ipc_command, the return value is used directly as the send length:When the formatted command exceeds 255 bytes,
bufis silently truncated butpacket_lenstill holds the full would-be length.send_in_fullthen callssend(2)with alenlarger thanbuf, causing an out-of-bounds read of the stack frame and sending garbage bytes (stack padding, saved registers, return address) to kfmon over the IPC socket.The practical effect is a confusing silent failure: kfmon receives a garbled command and rejects it, with no useful error surfaced to the user.
Fix
Check for truncation (
packet_len >= sizeof(buf)) and returnKFMON_IPC_SEND_FAILUREearly, turning the silent misbehaviour into a clear, returnable error.