apex-twin 0.27.0: rotate the audit log under flock
audit.log grew without a bound. APEX_AUDIT_MAX_BYTES (default 10 MiB, 0 disables) rotates it to audit.log.1 when the next line would cross the limit: the size is checked, then re-checked under flock(LOCK_EX) on audit.log.lock, so only one proxy child renames. Lines are still one write() each, so a line never mixes with another writer. A child with an already-open fd keeps writing to the renamed file, which is still a valid log; new writers open the new file. audit_log reads the current file only. --check and capabilities report audit_max_bytes. Tests with a 300-byte cap and 12 audited calls: audit.log.1 exists, every non-empty line in both files parses as JSON, audit_log still answers, and APEX_AUDIT_MAX_BYTES=0 produces no .1. Red proof: disabling the rotation call fails the rotation and rotated-file checks. 323 checks, coverage 77.1%, release-check green including distcheck.
This commit is contained in:
1 parent
0e86929ee0
commit
f44df202ba
9 files changed
+124
-14
No files matched your search
+15
-1
@@ -8,7 +8,21 @@ fields, and state from an older version is read by a newer one.
|
||||
There are no git tags. The hash next to a version is the commit that introduced
|
||||
it; the current entry is the commit that adds this section.
|
||||
|
||||
## 0.26.0 — 2026-11-21 — this change
|
||||
## 0.27.0 — 2026-11-21 — this change
|
||||
|
||||
- `audit.log` rotates to `audit.log.1` when it reaches `APEX_AUDIT_MAX_BYTES`
|
||||
(default 10 MiB, `0` disables). The size is checked again under
|
||||
`flock(LOCK_EX)` so only one proxy child renames; lines stay one `write()`.
|
||||
`audit_log` reads the current file only; back up both.
|
||||
- `--check` and `capabilities` report `audit_max_bytes`.
|
||||
- Tests: 12 audited calls with a 300-byte cap produce a `.1` file, and every
|
||||
line in both files parses as JSON; `0` produces no `.1`. Red proof: disabling
|
||||
the rotation call makes the rotation and rotated-file checks fail.
|
||||
|
||||
## 0.26.0 — 2026-11-21 — 97c79a7
|
||||
|
||||
- Harness follow-ups in this release window: stdio input is fed from a temp file
|
||||
and a clean-exit-empty run is retried once, loudly (69f941a, 0e86929).
|
||||
|
||||
- `finding_add` deduplicates: the same target and title (case-insensitive,
|
||||
whitespace runs collapsed, ends trimmed) returns the existing id with
|
||||
|
||||
@@ -30,7 +30,7 @@ separate, explicitly armed switch.
|
||||
|
||||
```sh
|
||||
make # apex-twin, ~239 kB static-ish binary, zero warnings under -Werror
|
||||
make test # gate audit + 317 end-to-end checks, no mocks
|
||||
make test # gate audit + 323 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 gate-audit # static audit: every target-taking tool calls the gate
|
||||
@@ -309,7 +309,7 @@ bounding set without `CAP_NET_RAW` (connect scans need none).
|
||||
|
||||
`--check` answers the operational questions without starting a server:
|
||||
`rewrite_rules`, `webhook_configured` (a boolean; the URL is never printed),
|
||||
`proxy_max_children` and the loaded scope, alongside the version and tool count.
|
||||
`proxy_max_children`, `audit_max_bytes` and the loaded scope, alongside the version and tool count.
|
||||
`capabilities` returns the same fields to the model.
|
||||
|
||||
## The radio
|
||||
@@ -343,6 +343,7 @@ APEX_SERIAL=/dev/ttyACM0 APEX_BAUD=115200
|
||||
| `APEX_WEBHOOK_MIN_SEVERITY` | `critical` | floor for the webhook |
|
||||
| `APEX_REWRITE` | `$APEX_HOME/rewrite.json` | proxy rewrite rules (SIGHUP reloads) |
|
||||
| `APEX_PROXY_MAX_CHILDREN` | `64` | concurrent proxy connections before 503 |
|
||||
| `APEX_AUDIT_MAX_BYTES` | `10485760` | rotate `audit.log` to `.1`; `0` disables |
|
||||
|
||||
## Files it writes
|
||||
|
||||
@@ -350,6 +351,7 @@ APEX_SERIAL=/dev/ttyACM0 APEX_BAUD=115200
|
||||
$APEX_HOME/
|
||||
scope.json the rules of engagement
|
||||
audit.log one JSON line per decision, append-only
|
||||
audit.log.1 previous audit file after size rotation
|
||||
findings/f0001.json one finding per file
|
||||
evidence/<ts>-<label>.bin raw proof, 0600
|
||||
reports/report-<ts>.md rendered findings, 0600, never overwritten
|
||||
@@ -370,7 +372,7 @@ finding and the next report no longer mentions it.
|
||||
make test
|
||||
```
|
||||
|
||||
317 checks, real binary, real DNS, real files written; no mocks. The suite was
|
||||
323 checks, real binary, real DNS, real files written; no mocks. The suite was
|
||||
seen red on purpose: with the "no scope" branch disabled by `if (0 && ...)`,
|
||||
three checks fail, because they assert the *reason* and not merely that an error
|
||||
came back — a gate that falls through to the next deny branch looks identical
|
||||
@@ -401,7 +403,7 @@ 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 317 checks pass with no ASan or UBSan report,
|
||||
`-fsanitize=address,undefined`. All 323 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
|
||||
@@ -460,7 +462,7 @@ example.com checks; `make test` is the fast loop.
|
||||
|
||||
`make coverage` builds `apex-twin-cov` with `-O0 --coverage`, runs the suite and
|
||||
prints per-file line coverage (`tests/coverage.py`, gcov only, no extra tools).
|
||||
The first measured pass was 55.9%; the gap tests brought it to **77.0%** —
|
||||
The first measured pass was 55.9%; the gap tests brought it to **77.1%** —
|
||||
`tools_kb.c` 100%, `tools_traffic.c` 81.8%, `tools_web.c` 71.6%,
|
||||
`tools_recon.c` 68.0%. A PTY-based Marauder simulator then raised
|
||||
`tools_radio.c` from 31.7% to 76.2% without hardware: firmware `info`/`help`,
|
||||
@@ -472,7 +474,7 @@ the copied scope reason is now bounded with a precision, not left to snprintf.
|
||||
|
||||
## Status
|
||||
|
||||
0.26.0. Built and run: 45 tools, 317 checks, 340 fuzz cases and 77.0% line
|
||||
0.27.0. Built and run: 45 tools, 323 checks, 340 fuzz cases and 77.1% line
|
||||
coverage. 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
|
||||
|
||||
+4
-1
@@ -66,7 +66,9 @@ rewrite.json proxy rules, read at start and on SIGHUP (optional)
|
||||
```
|
||||
|
||||
`audit.log` is written **before** an action, so a process that dies mid-scan
|
||||
still left its intent on disk. Findings, assets and reports are written beside
|
||||
still left its intent on disk. When it reaches `APEX_AUDIT_MAX_BYTES` (default
|
||||
10 MiB, `0` disables) it is renamed to `audit.log.1` under `flock`; one previous
|
||||
file is kept. Back up both. `audit_log` reads the current file only. Findings, assets and reports are written beside
|
||||
their target and renamed; a reader never sees half a file.
|
||||
|
||||
## Scope lifecycle
|
||||
@@ -198,6 +200,7 @@ of that file — back up first.
|
||||
| Default tool timeout | 60 s (hard cap 900 s) |
|
||||
| Radio scan | 3–300 s, always ends with `stopscan` |
|
||||
| Concurrent proxy connections | `APEX_PROXY_MAX_CHILDREN`, default 64, 503 beyond |
|
||||
| Audit file size | `APEX_AUDIT_MAX_BYTES`, default 10 MiB, rotates to `.1` |
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
|
||||
+3
-2
@@ -357,11 +357,12 @@ int main(int argc, char **argv)
|
||||
apex_scope_json(&b);
|
||||
printf("{\"version\":\"%s\",\"tools\":%lu,\"home\":\"%s\","
|
||||
"\"rewrite_rules\":%d,\"webhook_configured\":%s,"
|
||||
"\"proxy_max_children\":%d,\"scope\":%s}\n", APEX_VERSION,
|
||||
"\"proxy_max_children\":%d,\"audit_max_bytes\":%d,"
|
||||
"\"scope\":%s}\n", APEX_VERSION,
|
||||
(unsigned long)g_srv.ntools, g_cfg.home,
|
||||
apex_rewrite_count(),
|
||||
g_cfg.webhook[0] != '\0' ? "true" : "false",
|
||||
g_cfg.proxy_max_children, b.p);
|
||||
g_cfg.proxy_max_children, g_cfg.audit_max_bytes, b.p);
|
||||
mcp_buf_free(&b);
|
||||
return 0;
|
||||
}
|
||||
|
||||
+2
-1
@@ -15,7 +15,7 @@
|
||||
|
||||
#include "mcp.h"
|
||||
|
||||
#define APEX_VERSION "0.26.0"
|
||||
#define APEX_VERSION "0.27.0"
|
||||
#define APEX_MAX_OUT (1u << 20)
|
||||
#define APEX_PATH_MAX 512
|
||||
#define APEX_PATH_FULL 1024
|
||||
@@ -75,6 +75,7 @@ typedef struct {
|
||||
int baud;
|
||||
int default_timeout_ms;
|
||||
int max_timeout_ms;
|
||||
int audit_max_bytes;
|
||||
size_t max_output;
|
||||
int dry_run;
|
||||
} apex_cfg;
|
||||
|
||||
@@ -165,11 +165,15 @@ void apex_config_init(void)
|
||||
g_cfg.baud = 115200;
|
||||
g_cfg.default_timeout_ms = 60000;
|
||||
g_cfg.max_timeout_ms = 900000;
|
||||
g_cfg.audit_max_bytes = 10 * 1024 * 1024;
|
||||
g_cfg.max_output = APEX_MAX_OUT;
|
||||
g_cfg.dry_run = 0;
|
||||
|
||||
env_int("APEX_DEFAULT_TIMEOUT_MS", &g_cfg.default_timeout_ms);
|
||||
env_int("APEX_MAX_TIMEOUT_MS", &g_cfg.max_timeout_ms);
|
||||
env_int("APEX_AUDIT_MAX_BYTES", &g_cfg.audit_max_bytes);
|
||||
if (g_cfg.audit_max_bytes < 0)
|
||||
g_cfg.audit_max_bytes = 0;
|
||||
env_int("APEX_BAUD", &g_cfg.baud);
|
||||
g_cfg.proxy_max_children = 64;
|
||||
env_int("APEX_PROXY_MAX_CHILDREN", &g_cfg.proxy_max_children);
|
||||
|
||||
+40
@@ -20,6 +20,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include <sys/file.h>
|
||||
#include <sys/stat.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
@@ -44,11 +45,50 @@ void apex_json_str(mcp_buf *b, const char *s)
|
||||
mcp_buf_puts(b, "\"");
|
||||
}
|
||||
|
||||
/*
|
||||
* Rotate audit.log to audit.log.1 once it reaches the configured size. Children
|
||||
* from the proxy share the file, so the size check is repeated under flock:
|
||||
* only one of them renames, the others see the fresh file. A writer that
|
||||
* already has an fd open keeps writing to the renamed file, which is still a
|
||||
* valid log; new writers open the new file.
|
||||
*/
|
||||
static void rotate_audit(void)
|
||||
{
|
||||
struct stat st;
|
||||
char lock_path[APEX_PATH_FULL];
|
||||
char old_path[APEX_PATH_FULL];
|
||||
int lfd;
|
||||
|
||||
if (g_cfg.audit_max_bytes <= 0)
|
||||
return;
|
||||
if (stat(g_cfg.audit_file, &st) != 0 ||
|
||||
st.st_size < (off_t)g_cfg.audit_max_bytes)
|
||||
return;
|
||||
if (snprintf(lock_path, sizeof(lock_path), "%s.lock",
|
||||
g_cfg.audit_file) >= (int)sizeof(lock_path))
|
||||
return;
|
||||
if (snprintf(old_path, sizeof(old_path), "%s.1",
|
||||
g_cfg.audit_file) >= (int)sizeof(old_path))
|
||||
return;
|
||||
lfd = open(lock_path, O_CREAT | O_RDWR, 0600);
|
||||
if (lfd < 0)
|
||||
return;
|
||||
if (flock(lfd, LOCK_EX) == 0) {
|
||||
if (stat(g_cfg.audit_file, &st) == 0 &&
|
||||
st.st_size >= (off_t)g_cfg.audit_max_bytes)
|
||||
(void)rename(g_cfg.audit_file, old_path);
|
||||
(void)flock(lfd, LOCK_UN);
|
||||
}
|
||||
close(lfd);
|
||||
}
|
||||
|
||||
static void append_line(const char *path, const char *line, size_t len)
|
||||
{
|
||||
int fd;
|
||||
char *buf;
|
||||
|
||||
if (path != NULL && strcmp(path, g_cfg.audit_file) == 0)
|
||||
rotate_audit();
|
||||
/*
|
||||
* One write call: the proxy records from several children, and a line
|
||||
* split into two writes (text plus newline) lets another process slip
|
||||
|
||||
+2
-2
@@ -954,10 +954,10 @@ static void t_capabilities(mcp_conn *c, const mcp_json *args, void *user)
|
||||
mcp_buf_puts(&b, g_scope.loaded ? "true" : "false");
|
||||
(void)apex_rewrite_load();
|
||||
mcp_buf_printf(&b, ",\"rewrite_rules\":%d,\"webhook_configured\":%s,"
|
||||
"\"proxy_max_children\":%d}",
|
||||
"\"proxy_max_children\":%d,\"audit_max_bytes\":%d}",
|
||||
apex_rewrite_count(),
|
||||
g_cfg.webhook[0] != '\0' ? "true" : "false",
|
||||
g_cfg.proxy_max_children);
|
||||
g_cfg.proxy_max_children, g_cfg.audit_max_bytes);
|
||||
mcp_reply_json(c, b.p);
|
||||
mcp_buf_free(&b);
|
||||
}
|
||||
|
||||
+46
-1
@@ -1991,6 +1991,8 @@ check("--check reports webhook_configured=false",
|
||||
chk["webhook_configured"] is False, chk)
|
||||
check("--check reports the proxy child limit",
|
||||
chk["proxy_max_children"] == 64, chk)
|
||||
check("--check reports the audit rotation size",
|
||||
chk["audit_max_bytes"] == 10485760, chk)
|
||||
chk_hook, out_hook = check_json({"APEX_WEBHOOK": "http://127.0.0.1:9/hook"})
|
||||
check("--check reports the webhook", chk_hook["webhook_configured"] is True,
|
||||
chk_hook)
|
||||
@@ -2001,7 +2003,8 @@ res, _ = run([call(1, "capabilities")], home=ops_home,
|
||||
caps = json.loads(text(res[0]))
|
||||
check("capabilities reports the operational state",
|
||||
caps["rewrite_rules"] == 2 and caps["webhook_configured"] is True and
|
||||
caps["proxy_max_children"] == 64, caps)
|
||||
caps["proxy_max_children"] == 64 and
|
||||
caps["audit_max_bytes"] == 10485760, caps)
|
||||
check("capabilities does not leak the webhook URL",
|
||||
"127.0.0.1:9" not in text(res[0]), text(res[0])[:200])
|
||||
with open(os.path.join(ops_home, "rewrite.json"), "w") as f:
|
||||
@@ -2011,6 +2014,48 @@ check("--check survives an invalid rewrite file", chk_bad["rewrite_rules"] == 0,
|
||||
chk_bad)
|
||||
shutil.rmtree(ops_home, ignore_errors=True)
|
||||
|
||||
print("\n== audit rotation ==")
|
||||
|
||||
|
||||
def audit_lines(path):
|
||||
try:
|
||||
with open(path) as f:
|
||||
return [json.loads(line) for line in f if line.strip()]
|
||||
except (OSError, ValueError):
|
||||
return None
|
||||
|
||||
|
||||
rot_home = tempfile.mkdtemp(prefix="apex-rot-")
|
||||
write_scope(rot_home, {"label": "rot", "expires": "2099-12-31T23:59:59Z",
|
||||
"allow_ip": ["192.0.2.0/24"], "allow_port": ["80"]})
|
||||
rot_calls = [call(i + 1, "scope_check", target="192.0.2.5", port=80)
|
||||
for i in range(12)]
|
||||
run(rot_calls, home=rot_home, extra_env={"APEX_AUDIT_MAX_BYTES": "300"})
|
||||
check("the audit log rotates at the configured size",
|
||||
os.path.isfile(os.path.join(rot_home, "audit.log.1")),
|
||||
os.listdir(rot_home))
|
||||
current = audit_lines(os.path.join(rot_home, "audit.log"))
|
||||
rotated = audit_lines(os.path.join(rot_home, "audit.log.1"))
|
||||
check("the current audit file has only complete JSON lines",
|
||||
current is not None and len(current) > 0, current)
|
||||
check("the rotated audit file has only complete JSON lines",
|
||||
rotated is not None and len(rotated) > 0, rotated)
|
||||
res, _ = run([call(1, "audit_log", limit=100)], home=rot_home)
|
||||
check("audit_log still returns valid entries after rotation",
|
||||
not err(res[0]) and isinstance(json.loads(text(res[0])), list),
|
||||
text(res[0])[:160])
|
||||
shutil.rmtree(rot_home, ignore_errors=True)
|
||||
|
||||
nohome = tempfile.mkdtemp(prefix="apex-norot-")
|
||||
write_scope(nohome, {"label": "norot", "expires": "2099-12-31T23:59:59Z",
|
||||
"allow_ip": ["192.0.2.0/24"], "allow_port": ["80"]})
|
||||
run(rot_calls, home=nohome, extra_env={"APEX_AUDIT_MAX_BYTES": "0"})
|
||||
check("rotation is disabled with APEX_AUDIT_MAX_BYTES=0",
|
||||
os.path.isfile(os.path.join(nohome, "audit.log")) and
|
||||
not os.path.exists(os.path.join(nohome, "audit.log.1")),
|
||||
os.listdir(nohome))
|
||||
shutil.rmtree(nohome, ignore_errors=True)
|
||||
|
||||
print("\n== summary ==")
|
||||
print("%d passed, %d failed" % (passes, len(fails)))
|
||||
if fails:
|
||||
|
||||
Reference in new issue
Block a user