Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

ui: bound the HTTP response accumulators against snprintf truncation

Martin Vogelc8f3db2831e8
diff --git a/src/ui/http_server.c b/src/ui/http_server.c
index 6ef994393..55e6bde08 100644
--- a/src/ui/http_server.c
+++ b/src/ui/http_server.c
@@ -42,6 +42,7 @@
#include <errno.h>
#include <math.h>
#include <stdatomic.h>
+#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -389,6 +390,35 @@ void cbm_ui_log_append(const char *line) {
cbm_mutex_unlock(&g_log_mutex);
}
+/* Append a printf-formatted fragment at *pos within a bufsz buffer, never
+ * advancing *pos past bufsz. snprintf returns the length it WOULD have written,
+ * so `pos += snprintf(...)` runs pos past the end on truncation and the next
+ * call computes a wrapped (huge) remaining size and writes out of bounds. This
+ * clamps: on truncation *pos is pinned at bufsz and further appends are no-ops. */
+static void http_appendf(char *buf, size_t bufsz, int *pos, const char *fmt, ...)
+ __attribute__((format(printf, 4, 5)));
+static void http_appendf(char *buf, size_t bufsz, int *pos, const char *fmt, ...) {
+ if (*pos < 0) {
+ return;
+ }
+ if ((size_t)*pos >= bufsz) {
+ *pos = (int)bufsz;
+ return;
+ }
+ va_list ap;
+ va_start(ap, fmt);
+ int n = vsnprintf(buf + *pos, bufsz - (size_t)*pos, fmt, ap);
+ va_end(ap);
+ if (n < 0) {
+ return;
+ }
+ if ((size_t)n >= bufsz - (size_t)*pos) {
+ *pos = (int)bufsz;
+ } else {
+ *pos += n;
+ }
+}
+
/* GET /api/logs?lines=N — returns last N log lines */
static void handle_logs(cbm_http_conn_t *c, const cbm_http_req_t *req) {
char lines_str[16] = {0};
@@ -414,7 +444,7 @@ static void handle_logs(cbm_http_conn_t *c, const cbm_http_req_t *req) {
}
int pos = 0;
- pos += snprintf(buf + pos, buf_size - (size_t)pos, "{\"lines\":[");
+ http_appendf(buf, buf_size, &pos, "{\"lines\":[");
for (int i = 0; i < count; i++) {
int idx = (start + i) % LOG_RING_SIZE;
if (i > 0)
@@ -439,7 +469,7 @@ static void handle_logs(cbm_http_conn_t *c, const cbm_http_req_t *req) {
buf[pos++] = '"';
}
cbm_mutex_unlock(&g_log_mutex);
- pos += snprintf(buf + pos, buf_size - (size_t)pos, "],\"total\":%d}", total);
+ http_appendf(buf, buf_size, &pos, "],\"total\":%d}", total);
cbm_http_replyf(c, 200, g_cors_json, "%s", buf);
free(buf);
@@ -474,10 +504,10 @@ static void handle_processes(cbm_http_conn_t *c) {
user_s = (double)u.QuadPart / 1e7;
sys_s = (double)k.QuadPart / 1e7;
}
- pos += snprintf(buf + pos, sizeof(buf) - (size_t)pos,
- "{\"self_pid\":%d,\"self_rss_mb\":%.1f,"
- "\"self_user_cpu_s\":%.1f,\"self_sys_cpu_s\":%.1f,\"processes\":[]}",
- (int)_getpid(), (double)rss_bytes / (1024.0 * 1024.0), user_s, sys_s);
+ http_appendf(buf, sizeof(buf), &pos,
+ "{\"self_pid\":%d,\"self_rss_mb\":%.1f,"
+ "\"self_user_cpu_s\":%.1f,\"self_sys_cpu_s\":%.1f,\"processes\":[]}",
+ (int)_getpid(), (double)rss_bytes / (1024.0 * 1024.0), user_s, sys_s);
#else
struct rusage ru;
getrusage(RUSAGE_SELF, &ru);
@@ -485,12 +515,12 @@ static void handle_processes(cbm_http_conn_t *c) {
#ifdef __APPLE__
rss_kb /= 1024;
#endif
- pos += snprintf(buf + pos, sizeof(buf) - (size_t)pos,
- "{\"self_pid\":%d,\"self_rss_mb\":%.1f,"
- "\"self_user_cpu_s\":%.1f,\"self_sys_cpu_s\":%.1f,\"processes\":[",
- (int)getpid(), (double)rss_kb / 1024.0,
- (double)ru.ru_utime.tv_sec + (double)ru.ru_utime.tv_usec / 1e6,
- (double)ru.ru_stime.tv_sec + (double)ru.ru_stime.tv_usec / 1e6);
+ http_appendf(buf, sizeof(buf), &pos,
+ "{\"self_pid\":%d,\"self_rss_mb\":%.1f,"
+ "\"self_user_cpu_s\":%.1f,\"self_sys_cpu_s\":%.1f,\"processes\":[",
+ (int)getpid(), (double)rss_kb / 1024.0,
+ (double)ru.ru_utime.tv_sec + (double)ru.ru_utime.tv_usec / 1e6,
+ (double)ru.ru_stime.tv_sec + (double)ru.ru_stime.tv_usec / 1e6);
FILE *fp = popen("LC_ALL=C ps -eo pid,pcpu,rss,etime,comm 2>/dev/null"
" | grep '[c]odebase-memory-mcp'",
@@ -508,11 +538,11 @@ static void handle_processes(cbm_http_conn_t *c) {
if (sscanf(line, "%d %f %ld %63s %255s", &pid, &cpu, &rss, elapsed, comm) >= 4) {
if (proc_count > 0)
buf[pos++] = ',';
- pos += snprintf(buf + pos, sizeof(buf) - (size_t)pos,
- "{\"pid\":%d,\"cpu\":%.1f,\"rss_mb\":%.1f,"
- "\"elapsed\":\"%s\",\"command\":\"%s\",\"is_self\":%s}",
- pid, (double)cpu, (double)rss / 1024.0, elapsed, comm,
- pid == (int)getpid() ? "true" : "false");
+ http_appendf(buf, sizeof(buf), &pos,
+ "{\"pid\":%d,\"cpu\":%.1f,\"rss_mb\":%.1f,"
+ "\"elapsed\":\"%s\",\"command\":\"%s\",\"is_self\":%s}",
+ pid, (double)cpu, (double)rss / 1024.0, elapsed, comm,
+ pid == (int)getpid() ? "true" : "false");
if (pos >= (int)sizeof(buf)) {
pos = (int)sizeof(buf) - 1;
}
@@ -521,7 +551,7 @@ static void handle_processes(cbm_http_conn_t *c) {
}
pclose(fp);
}
- pos += snprintf(buf + pos, sizeof(buf) - (size_t)pos, "]}");
+ http_appendf(buf, sizeof(buf), &pos, "]}");
#endif
cbm_http_replyf(c, 200, g_cors_json, "%s", buf);
@@ -602,7 +632,7 @@ static void handle_process_kill(cbm_http_conn_t *c, const cbm_http_req_t *req) {
#include <dirent.h>
static void append_roots_json(char *buf, size_t bufsz, int *pos) {
- *pos += snprintf(buf + *pos, bufsz - (size_t)*pos, ",\"roots\":[");
+ http_appendf(buf, bufsz, pos, ",\"roots\":[");
#ifdef _WIN32
DWORD drives = GetLogicalDrives();
int count = 0;
@@ -613,12 +643,12 @@ static void append_roots_json(char *buf, size_t bufsz, int *pos) {
if (count++ > 0) {
buf[(*pos)++] = ',';
}
- *pos += snprintf(buf + *pos, bufsz - (size_t)*pos, "\"%c:/\"", 'A' + i);
+ http_appendf(buf, bufsz, pos, "\"%c:/\"", 'A' + i);
}
#else
- *pos += snprintf(buf + *pos, bufsz - (size_t)*pos, "\"/\"");
+ http_appendf(buf, bufsz, pos, "\"/\"");
#endif
- *pos += snprintf(buf + *pos, bufsz - (size_t)*pos, "]");
+ http_appendf(buf, bufsz, pos, "]");
}
/* GET /api/browse?path=/some/dir — list subdirectories for file picker */
@@ -653,7 +683,7 @@ static void handle_browse(cbm_http_conn_t *c, const cbm_http_req_t *req) {
/* Build JSON response */
char buf[32768];
int pos = 0;
- pos += snprintf(buf + pos, sizeof(buf) - (size_t)pos, "{\"path\":\"%s\",\"dirs\":[", path);
+ http_appendf(buf, sizeof(buf), &pos, "{\"path\":\"%s\",\"dirs\":[", path);
struct dirent *ent;
int count = 0;
@@ -674,7 +704,7 @@ static void handle_browse(cbm_http_conn_t *c, const cbm_http_req_t *req) {
{
char esc[512];
cbm_json_escape(esc, (int)sizeof(esc), ent->d_name);
- pos += snprintf(buf + pos, sizeof(buf) - (size_t)pos, "\"%s\"", esc);
+ http_appendf(buf, sizeof(buf), &pos, "\"%s\"", esc);
}
if (pos >= (int)sizeof(buf)) {
pos = (int)sizeof(buf) - 1;
@@ -706,9 +736,9 @@ static void handle_browse(cbm_http_conn_t *c, const cbm_http_req_t *req) {
{
char esc_parent[2048];
cbm_json_escape(esc_parent, (int)sizeof(esc_parent), parent);
- pos += snprintf(buf + pos, sizeof(buf) - (size_t)pos, "],\"parent\":\"%s\"", esc_parent);
+ http_appendf(buf, sizeof(buf), &pos, "],\"parent\":\"%s\"", esc_parent);
append_roots_json(buf, sizeof(buf), &pos);
- pos += snprintf(buf + pos, sizeof(buf) - (size_t)pos, "}");
+ http_appendf(buf, sizeof(buf), &pos, "}");
}
cbm_http_replyf(c, 200, g_cors_json, "%s", buf);
}
@@ -760,8 +790,8 @@ static void handle_adr_get(cbm_http_conn_t *c, const cbm_http_req_t *req) {
buf[pos++] = ch;
}
}
- pos += snprintf(buf + pos, buf_size - (size_t)pos, "\",\"updated_at\":\"%s\"}",
- adr.updated_at ? adr.updated_at : "");
+ http_appendf(buf, buf_size, &pos, "\",\"updated_at\":\"%s\"}",
+ adr.updated_at ? adr.updated_at : "");
cbm_http_replyf(c, 200, g_cors_json, "%s", buf);
free(buf);
} else {
@@ -1178,9 +1208,9 @@ static void handle_index_status(cbm_http_conn_t *c) {
if (pos > 1)
buf[pos++] = ',';
const char *ss = st == 1 ? "indexing" : st == 2 ? "done" : "error";
- pos += snprintf(buf + pos, sizeof(buf) - (size_t)pos,
- "{\"slot\":%d,\"status\":\"%s\",\"path\":\"%s\",\"error\":\"%s\"}", i, ss,
- g_index_jobs[i].root_path, st == 3 ? g_index_jobs[i].error_msg : "");
+ http_appendf(buf, sizeof(buf), &pos,
+ "{\"slot\":%d,\"status\":\"%s\",\"path\":\"%s\",\"error\":\"%s\"}", i, ss,
+ g_index_jobs[i].root_path, st == 3 ? g_index_jobs[i].error_msg : "");
}
buf[pos++] = ']';
buf[pos] = '\0';
diff --git a/tests/test_httpd.c b/tests/test_httpd.c
index 5d4a8a6b1..cceea3628 100644
--- a/tests/test_httpd.c
+++ b/tests/test_httpd.c
@@ -44,6 +44,7 @@ typedef SOCKET th_sock_t;
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/time.h> /* struct timeval for the SO_RCVTIMEO watchdog (#798 follow-up) */
+#include <sys/wait.h> /* fork/waitpid crash-isolation for the browse overflow guard */
#include <unistd.h>
typedef int th_sock_t;
#define th_sock_close close
@@ -1110,9 +1111,67 @@ TEST(ui_server_rejects_non_loopback_host) {
PASS();
}
+/* The directory browser formats readdir() entries into a fixed 32 KB response
+ * buffer. The per-entry loop is clamped, but the trailing "parent"/"roots"
+ * appends were not — once the entries filled the buffer, pos ran past the end
+ * and the next size argument wrapped, writing out of bounds. Fill the buffer
+ * with many long-named subdirectories and browse it in a forked child so an
+ * overflow surfaces as a killing signal (ASan abort) rather than a clean run. */
+TEST(ui_server_browse_wide_dir_no_overflow) {
+#ifdef _WIN32
+ SKIP_PLATFORM("fork crash-isolation is POSIX-only; the clamp is platform-agnostic");
+#else
+ char *dir = th_mktempdir("cbm_browse");
+ if (!dir) {
+ FAIL("mktempdir");
+ }
+ char longname[240];
+ memset(longname, 'a', sizeof(longname) - 1);
+ longname[sizeof(longname) - 1] = '\0';
+ for (int i = 0; i < 250; i++) { /* 250 * ~220 chars overflows the 32 KB buffer */
+ char sub[600];
+ snprintf(sub, sizeof(sub), "%s/%s%03d", dir, longname, i);
+ th_mkdir_p(sub);
+ }
+ fflush(NULL);
+ pid_t pid = fork();
+ if (pid == 0) {
+ th_server_t ts;
+ if (th_server_start(&ts) != 0) {
+ _exit(2);
+ }
+ char req[512];
+ snprintf(req, sizeof(req), "GET /api/browse?path=%s HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n",
+ dir);
+ char *resp = malloc(262144);
+ int n = resp ? th_http(cbm_http_server_port(ts.srv), req, resp, 262144) : 0;
+ int ok = (n > 0 && strstr(resp, "HTTP/1.1 200") != NULL);
+ free(resp);
+ th_server_stop(&ts);
+ _exit(ok ? 0 : 3);
+ }
+ ASSERT_TRUE(pid > 0);
+ int status = 0;
+ (void)waitpid(pid, &status, 0);
+ char rm[600];
+ snprintf(rm, sizeof(rm), "rm -rf '%s'", dir);
+ (void)system(rm);
+ if (WIFSIGNALED(status)) {
+ char m[96];
+ snprintf(m, sizeof(m), "browse killed by signal %d — response buffer overflow",
+ WTERMSIG(status));
+ FAIL(m);
+ }
+ ASSERT_TRUE(WIFEXITED(status));
+ ASSERT_EQ(WEXITSTATUS(status), 0);
+ PASS();
+#endif
+}
+
/* ── Suite ────────────────────────────────────────────────────── */
SUITE(httpd) {
+ RUN_TEST(ui_server_browse_wide_dir_no_overflow);
/* Parser / helpers */
RUN_TEST(httpd_parse_simple_get);
RUN_TEST(httpd_parse_post_with_body_offset);