Skip to content

Commit 644c8fd

Browse files
committed
fix(mcp): bound the help-list appends instead of trusting snprintf's return
CodeQL flagged two high-severity alerts in the previous commit: the help-list builder accumulated snprintf's return value, which is the length it WOULD have written rather than what it did. Once len passes cap, the next `cap - len` underflows to a huge size_t and the following write lands outside the buffer. That is the same class the Cypher UNWIND list builder was just fixed for in DeusData#1173, where blen advanced past a 2KB buffer the same way. The capacity computed here is in fact sufficient for the current registry, so nothing overflows today -- which makes this the more dangerous shape, not the safer one. The code was correct only by an argument made ten lines away in the cap loop, so renaming a tool or changing the wrap rule would have turned it into an out-of-bounds write with nothing in the function to notice. help_append() returns the bytes ACTUALLY written, clamped to the space left, so `len <= cap - 1` becomes a local invariant that holds no matter what the registry or the wrap column later become. Also adds the <stdarg.h> the variadic helper needs. Verified: builds clean under -Werror; --help still renders all 15 tools in registry order with the same wrapping; mcp + cli suites 437 passed, 0 failed; make -f Makefile.cbm lint-ci clean. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 0e1b453 commit 644c8fd

1 file changed

Lines changed: 32 additions & 4 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ enum {
8989
#include <yyjson/yyjson.h>
9090
#include <ctype.h>
9191
#include <limits.h>
92+
#include <stdarg.h> // va_list, for the bounded help-list appender
9293
#include <stdint.h> // int64_t
9394
#include <stdio.h>
9495
#include <stdlib.h>
@@ -906,6 +907,32 @@ const char *cbm_mcp_tool_name(int index) {
906907
* serves. The list used to be hand-maintained in the help text and drifted
907908
* when check_index_coverage was added (#1361); deriving it here makes that
908909
* divergence impossible. Heap-allocated; caller frees. */
910+
/* Append at out[len] and return the bytes ACTUALLY written.
911+
*
912+
* snprintf returns the length it WOULD have written, so accumulating that value
913+
* lets len run past cap; the next `cap - len` then underflows to a huge size_t
914+
* and the following write lands outside the buffer. CodeQL flagged exactly that
915+
* shape here, and it is the same class #1173 just fixed in the Cypher list
916+
* builder. The capacity computed below does happen to be sufficient today —
917+
* which makes this the more dangerous version, not the safer one: the code is
918+
* correct only by an argument made ten lines away, so renaming a tool or
919+
* changing the wrap rule would turn it into an overflow with nothing to notice.
920+
* Clamping makes `len <= cap - 1` a local invariant no later edit can void. */
921+
static size_t help_append(char *out, size_t cap, size_t len, const char *fmt, ...) {
922+
if (len + 1 >= cap) {
923+
return 0;
924+
}
925+
va_list args;
926+
va_start(args, fmt);
927+
int written = vsnprintf(out + len, cap - len, fmt, args);
928+
va_end(args);
929+
if (written <= 0) {
930+
return 0;
931+
}
932+
size_t room = cap - len - 1;
933+
return (size_t)written > room ? room : (size_t)written;
934+
}
935+
909936
char *cbm_mcp_tools_help_list(void) {
910937
size_t cap = SLEN("Tools:") + 2; /* trailing newline + NUL */
911938
for (int i = 0; i < TOOL_COUNT; i++) {
@@ -915,20 +942,21 @@ char *cbm_mcp_tools_help_list(void) {
915942
if (!out) {
916943
return NULL;
917944
}
918-
size_t len = (size_t)snprintf(out, cap, "Tools:");
945+
size_t len = help_append(out, cap, 0, "Tools:");
919946
size_t col = len;
920947
for (int i = 0; i < TOOL_COUNT; i++) {
921948
const char *sep = (i + 1 < TOOL_COUNT) ? "," : "";
922949
size_t item = SLEN(" ") + strlen(TOOLS[i].name) + strlen(sep);
923950
if (i > 0 && col + item > MCP_HELP_TOOLS_WRAP_COL) {
924-
len += (size_t)snprintf(out + len, cap - len, "\n ");
951+
len += help_append(out, cap, len, "\n ");
925952
col = 1;
926953
}
927-
size_t wrote = (size_t)snprintf(out + len, cap - len, " %s%s", TOOLS[i].name, sep);
954+
size_t wrote = help_append(out, cap, len, " %s%s", TOOLS[i].name, sep);
928955
len += wrote;
929956
col += wrote;
930957
}
931-
snprintf(out + len, cap - len, "\n");
958+
len += help_append(out, cap, len, "\n");
959+
(void)len; /* final length is not needed; the buffer is NUL-terminated */
932960
return out;
933961
}
934962

0 commit comments

Comments
 (0)