Skip to content

Fix OOB stack read in send_ipc_command due to misuse of snprintf return value#234

Merged
pgaskin merged 1 commit into
pgaskin:masterfrom
craftsoldier:fix/kfmon-send-ipc-truncation
Jun 12, 2026
Merged

Fix OOB stack read in send_ipc_command due to misuse of snprintf return value#234
pgaskin merged 1 commit into
pgaskin:masterfrom
craftsoldier:fix/kfmon-send-ipc-truncation

Conversation

@craftsoldier

Copy link
Copy Markdown
Contributor

Problem

snprintf(3) returns the number of bytes it would have written had the buffer been large enough — not the number actually written. In send_ipc_command, the return value is used directly as the send length:

char buf[256] = { 0 };
int packet_len = snprintf(buf, sizeof(buf), "%s:%s", ipc_cmd, ipc_arg);
return send_packet(data_fd, buf, (size_t)(packet_len + 1));  // bug

When the formatted command exceeds 255 bytes, buf is silently truncated but packet_len still holds the full would-be length. send_in_full then calls send(2) with a len larger than buf, 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 return KFMON_IPC_SEND_FAILURE early, turning the silent misbehaviour into a clear, returnable error.

int packet_len = ipc_arg
    ? snprintf(buf, sizeof(buf), "%s:%s", ipc_cmd, ipc_arg)
    : snprintf(buf, sizeof(buf), "%s", ipc_cmd);
if (packet_len < 0 || (size_t) packet_len >= sizeof(buf)) {
    return KFMON_IPC_SEND_FAILURE;
}

…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.
@craftsoldier craftsoldier requested a review from NiLuJe as a code owner June 11, 2026 13:03
@NiLuJe

NiLuJe commented Jun 11, 2026

Copy link
Copy Markdown
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 ;).

@NiLuJe NiLuJe left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM, thanks!

@pgaskin pgaskin merged commit bc799eb into pgaskin:master Jun 12, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants