From be4e833b919a3387c1b7b9d8b4d3b03e22975bc6 Mon Sep 17 00:00:00 2001 From: lotherk Date: Sat, 21 Nov 2026 07:26:34 +0000 Subject: [PATCH] apex-twin 0.12.0: sanitizer and analyzer pass New build targets, no new features: make asan apex-twin-asan with -fsanitize=address,undefined make test-asan the whole suite against it (APEX_BIN override) make analyze gcc -fanalyzer over every translation unit Results: 239/239 checks pass against the ASan/UBSan binary with no report; a batch of error paths, network captures, report generation and evidence/traffic reads run with LeakSanitizer on: no leaks. make analyze is clean (0 warnings). The analyzer was red first on vendor/libmcp/mcp.h: 'b->p = realloc(b->p, want)' loses the only pointer to the old block when realloc fails. It now grows through a temporary and assigns after the NULL check. That is a real robustness fix, not a suppression. .gitignore covers apex-twin-asan and build-asan/. README documents the targets and the result. --- .gitignore | 2 ++ Makefile | 31 +++++++++++++++++++++++++++++-- README.md | 17 +++++++++++++++-- src/apex.h | 2 +- tests/run.py | 2 +- vendor/libmcp/mcp.h | 9 +++++++-- 6 files changed, 55 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index a763ebc..03adc3d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ apex-twin *.o +apex-twin-asan +build-asan/ diff --git a/Makefile b/Makefile index 1afbe17..2fb9c25 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,9 @@ DOCDIR ?= $(PREFIX)/share/doc/apex-twin UNITDIR ?= $(PREFIX)/lib/systemd/system SRC := $(wildcard src/*.c) OBJ := $(SRC:.c=.o) +ASAN_BIN := apex-twin-asan +ASAN_OBJ := $(SRC:src/%.c=build-asan/%.o) +ASAN_FLAGS := -g -O1 -fsanitize=address,undefined -fno-omit-frame-pointer all: apex-twin @@ -20,6 +23,29 @@ src/%.o: src/%.c src/apex.h $(LIBDIR)/mcp.h test: apex-twin ./tests/run.sh +asan: $(ASAN_BIN) + +test-asan: $(ASAN_BIN) + ASAN_OPTIONS=detect_leaks=0 UBSAN_OPTIONS=halt_on_error=1 \ + APEX_BIN=$(CURDIR)/$(ASAN_BIN) ./tests/run.sh + +build-asan: + mkdir -p build-asan + +build-asan/%.o: src/%.c src/apex.h $(LIBDIR)/mcp.h | build-asan + $(CC) $(CFLAGS) $(ASAN_FLAGS) -I$(LIBDIR) -Isrc -c -o $@ $< + +$(ASAN_BIN): $(ASAN_OBJ) + $(CC) $(CFLAGS) $(ASAN_FLAGS) -o $@ $(ASAN_OBJ) + +# Static analysis, one translation unit at a time. Warnings are expected to be +# read, not piped away; the target fails on a compiler error. +analyze: + @for f in $(SRC); do \ + echo " ANALYZE $$f"; \ + $(CC) $(CFLAGS) -fanalyzer -I$(LIBDIR) -Isrc -c -o /dev/null $$f || exit 1; \ + done + install: apex-twin mkdir -p $(DESTDIR)$(BINDIR) $(DESTDIR)$(DOCDIR) $(DESTDIR)$(UNITDIR) install -m 0755 apex-twin $(DESTDIR)$(BINDIR)/apex-twin @@ -28,6 +54,7 @@ install: apex-twin install -m 0644 systemd/apex-twin-proxy.service $(DESTDIR)$(UNITDIR)/apex-twin-proxy.service clean: - rm -f apex-twin $(OBJ) + rm -f apex-twin $(OBJ) $(ASAN_BIN) + rm -rf build-asan -.PHONY: all test install clean +.PHONY: all test asan test-asan analyze install clean diff --git a/README.md b/README.md index b08bb98..0a1b2f4 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,9 @@ separate, explicitly armed switch. ```sh make # apex-twin, ~239 kB static-ish binary, zero warnings under -Werror make test # 239 end-to-end checks, no mocks +make asan # apex-twin-asan with ASan + UBSan +make test-asan # the whole suite against the sanitizer binary +make analyze # gcc -fanalyzer over every translation unit make install # binary, README and both systemd units (DESTDIR/PREFIX respected) ``` @@ -352,9 +355,18 @@ A port is never assumed: an earlier version of the HTTP test used 18797, which o this host is an SSH forward to another machine, and a foreign server answered `{"serverInfo":{"name":"koder"}}` while the suite was green. +The suite is also run against `apex-twin-asan` (`make test-asan`), built with +`-fsanitize=address,undefined`. All 239 checks pass with no ASan or UBSan report, +and a separate batch of error paths, network captures and report generation was +run with leak detection on: no leaks. `make analyze` runs `gcc -fanalyzer` over +every translation unit; it was red first on `vendor/libmcp/mcp.h`, where +`b->p = realloc(b->p, ...)` loses the only pointer when realloc fails. Growing +through a temporary is correct and the analyzer is now silent. That is the kind +of finding this pass exists for. + ## Status -0.11.0. Built and run: 45 tools, 239 checks. The loop closes: a probe captures +0.12.0. Built and run: 45 tools, 239 checks. The loop closes: a probe captures the exchange as a pair, a finding points at it, `finding_update` gives it a state, an asset records the host it came from, `cors_check`/`cookie_audit`/ `http_options` turn a web answer into a graded lead, `report_markdown` turns @@ -363,7 +375,8 @@ findings and inventory into one Markdown or CSV file, and in front of curl and a browser, with optional rewrite rules that can set or remove a header or block a path, and `APEX_WEBHOOK` wakes someone when the severity floor is crossed. `make install` ships the binary, this README and both -units; `--check` reports the operational state without starting a server. Not yet +units; `--check` reports the operational state without starting a server. The +whole suite also runs under ASan/UBSan, and `make analyze` is clean. Not yet wired into a fleet router. Licence: AGPL-3.0, same as ARTEX. diff --git a/src/apex.h b/src/apex.h index a4236bf..83fdaf0 100644 --- a/src/apex.h +++ b/src/apex.h @@ -15,7 +15,7 @@ #include "mcp.h" -#define APEX_VERSION "0.11.0" +#define APEX_VERSION "0.12.0" #define APEX_MAX_OUT (1u << 20) #define APEX_PATH_MAX 512 #define APEX_PATH_FULL 1024 diff --git a/tests/run.py b/tests/run.py index 5a01d82..27a186a 100755 --- a/tests/run.py +++ b/tests/run.py @@ -20,7 +20,7 @@ import time import urllib.request HERE = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -BIN = os.path.join(HERE, "apex-twin") +BIN = os.environ.get("APEX_BIN") or os.path.join(HERE, "apex-twin") ROOT = tempfile.mkdtemp(prefix="apex-twin-test-") fails = [] passes = 0 diff --git a/vendor/libmcp/mcp.h b/vendor/libmcp/mcp.h index 75dad25..8e4dc44 100644 --- a/vendor/libmcp/mcp.h +++ b/vendor/libmcp/mcp.h @@ -180,14 +180,19 @@ void mcp_buf_add(mcp_buf *b, const char *s, size_t n) { if (b->len + n + 1 > b->cap) { size_t want = b->cap ? b->cap : 256; + char *grown; while (want < b->len + n + 1) want *= 2; - b->p = realloc(b->p, want); - if (b->p == NULL) { + /* grow through a temporary: the old block is either freed by + * realloc or still owned here, and a failed realloc must not + * overwrite the only pointer to it */ + grown = realloc(b->p, want); + if (grown == NULL) { fprintf(stderr, "libmcp: out of memory\n"); exit(1); } + b->p = grown; b->cap = want; } memcpy(b->p + b->len, s, n);