From b18ed3888b35a15f019500cc5b5a6bd5059e4706 Mon Sep 17 00:00:00 2001 From: BFE Engineering Date: Fri, 2 Oct 2026 11:01:40 -0600 Subject: [PATCH] relays: refuse a WARDEN_GPIO_ROOT too long for PATH_MAX Sysfs paths were built in fixed 80/96-byte stack buffers with unchecked snprintf, so a root longer than about 70 bytes was truncated and both relays silently read as unavailable. Each operation now formats all of its paths up front into PATH_MAX buffers and checks every snprintf return. A root whose longest path does not fit is refused with a stderr line and the call reports unavailable, off or no-op; nothing is ever truncated. The export argument comes from a constant table instead of runtime formatting. Regression tests: a 200-byte root through the fake io (full path reaches the backend), the exact PATH_MAX boundary, one byte over (refused and logged on available, get and set, io untouched), and a real scratch tree under a root over 200 bytes. MC/DC stays at 100% (50/50 conditions). Fixes #10 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01441BzdGcvQmXkiN2qDjfZg --- docs/architecture.md | 2 +- docs/workflows/relay-drive.md | 6 +- drivers/README.md | 2 +- drivers/relays/relays.c | 88 +++++++++++++------- drivers/relays/relays.h | 4 + drivers/relays/test/test_relays.c | 131 ++++++++++++++++++++++++++++-- 6 files changed, 196 insertions(+), 37 deletions(-) diff --git a/docs/architecture.md b/docs/architecture.md index 58f0802..ab93f75 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -82,7 +82,7 @@ tiered: behind a small injectable seam, mock its world, build `-fcondition-coverage`, enforce via the shared `drivers/enforce-mcdc.sh` (gcc-14 `gcov --conditions`) in the CI `mcdc` job (the proven flare-edge - `tests/uboot-ab` method). **Done:** `relays.c` (40/40 conditions) and + `tests/uboot-ab` method). **Done:** `relays.c` (50/50 conditions) and `freshness.c` (66/66). **Next in:** the modbus master and the RGA dispatch, with the ADR-0005 unification (`drivers/README.md`). - **Tier 2, near-mainline small drivers -> branch coverage + fault diff --git a/docs/workflows/relay-drive.md b/docs/workflows/relay-drive.md index 6f2bdef..3961bf7 100644 --- a/docs/workflows/relay-drive.md +++ b/docs/workflows/relay-drive.md @@ -2,13 +2,15 @@ > **Outcome tested:** A relay is exported transparently and driven without disturbing a held contact. -**Coverage**: relays.c: 40/40 conditions, 100% MC/DC (CI-enforced) +**Coverage**: relays.c: 50/50 conditions, 100% MC/DC (CI-enforced) ```mermaid flowchart TD A[warden_relay_set idx,on] --> B{idx < COUNT?} B -- no --> Z[no-op] - B -- yes --> C{exported?} + B -- yes --> P{all sysfs paths fit PATH_MAX?} + P -- no --> Z3[log to stderr, no-op] + P -- yes --> C{exported?} C -- no --> D[write export] --> E{exported now?} E -- no --> Z2[give up] E -- yes --> F diff --git a/drivers/README.md b/drivers/README.md index 17bc967..5dcc018 100644 --- a/drivers/README.md +++ b/drivers/README.md @@ -12,7 +12,7 @@ Self-contained logic with a clean seam, measured to **100% MC/DC** (gcc-14 | Driver | Seam | MC/DC | |---|---|---| -| `relays/` | `relay_io` vtable (sysfs backend + in-memory fake) | 40/40 conditions, 100% | +| `relays/` | `relay_io` vtable (sysfs backend + in-memory fake) | 50/50 conditions, 100% | | `freshness/` | produce/render callbacks (the "no stale numbers" guard) | 66/66 conditions, 100% | **Adding a Tier-1 driver:** copy `.{c,h}` here, put the hardware/OS calls behind diff --git a/drivers/relays/relays.c b/drivers/relays/relays.c index 924b7be..98a3da0 100644 --- a/drivers/relays/relays.c +++ b/drivers/relays/relays.c @@ -1,5 +1,6 @@ #include "relays.h" +#include #include #include #include @@ -8,6 +9,8 @@ /* GPIO1_A0 and GPIO1_A1; gpiochip1 base is 32. See relays.h. */ static const int s_gpio[WARDEN_RELAY_COUNT] = { 32, 33 }; static const char * const s_name[WARDEN_RELAY_COUNT] = { "Relay 1", "Relay 2" }; +/* The same numbers as the strings written to `export`, so no runtime formatting. */ +static const char * const s_gpio_str[WARDEN_RELAY_COUNT] = { "32", "33" }; /* ---- default sysfs backend (Tier-2 plumbing; covered by integration tests) ---- */ @@ -64,11 +67,46 @@ const char * warden_relay_name(uint32_t idx) return idx < WARDEN_RELAY_COUNT ? s_name[idx] : "-"; } -static bool exported(int gpio) +/** + * Every sysfs path one relay operation can touch. Each holds PATH_MAX bytes, so + * any root the kernel would accept fits. + */ +struct relay_paths { + char gpio[PATH_MAX]; /* /gpioN */ + char export_[PATH_MAX]; /* /export */ + char direction[PATH_MAX]; /* /gpioN/direction */ + char value[PATH_MAX]; /* /gpioN/value */ +}; + +/** + * Format the paths for relay @p idx up front. A `$WARDEN_GPIO_ROOT` too long for + * any of them is refused with a log line, never truncated: a cut-short path + * would silently report both relays unavailable, or name a different node. + */ +static bool relay_paths(uint32_t idx, struct relay_paths * rp) { - char p[80]; - snprintf(p, sizeof(p), "%s/gpio%d", gpio_root(), gpio); - return s_io->exists(p); + const char * root = gpio_root(); + int gpio = s_gpio[idx]; + const int n[] = { + snprintf(rp->gpio, sizeof(rp->gpio), "%s/gpio%d", root, gpio), + snprintf(rp->export_, sizeof(rp->export_), "%s/export", root), + snprintf(rp->direction, sizeof(rp->direction), "%s/gpio%d/direction", root, gpio), + snprintf(rp->value, sizeof(rp->value), "%s/gpio%d/value", root, gpio), + }; + for(size_t i = 0; i < sizeof(n) / sizeof(n[0]); i++) { + /* The unsigned cast also maps an encoding error (n < 0) to "does not fit". */ + if((unsigned)n[i] >= PATH_MAX) { + fprintf(stderr, "relays: WARDEN_GPIO_ROOT is too long (%zu bytes): sysfs paths " + "under it exceed PATH_MAX (%d), refusing it\n", strlen(root), PATH_MAX); + return false; + } + } + return true; +} + +static bool exported(const struct relay_paths * rp) +{ + return s_io->exists(rp->gpio); } /** @@ -77,28 +115,21 @@ static bool exported(int gpio) * relay the first time this page is opened; reading the current level first and * writing it straight back makes the export transparent. */ -static bool ensure_ready(int gpio) +static bool ensure_ready(uint32_t idx, const struct relay_paths * rp) { - if(!exported(gpio)) { - char ex[80], n[16]; - snprintf(ex, sizeof(ex), "%s/export", gpio_root()); - snprintf(n, sizeof(n), "%d", gpio); - s_io->write(ex, n); - if(!exported(gpio)) return false; + if(!exported(rp)) { + s_io->write(rp->export_, s_gpio_str[idx]); + if(!exported(rp)) return false; } - char p[96]; - snprintf(p, sizeof(p), "%s/gpio%d/direction", gpio_root(), gpio); char dir[16] = ""; - if(!s_io->read_line(p, dir, sizeof(dir))) return false; + if(!s_io->read_line(rp->direction, dir, sizeof(dir))) return false; if(strncmp(dir, "out", 3) != 0) { /* Preserve the level across the switch to output. */ - char vp[96]; int cur; - snprintf(vp, sizeof(vp), "%s/gpio%d/value", gpio_root(), gpio); - if(!s_io->read_int(vp, &cur)) cur = 0; - s_io->write(p, cur ? "high" : "low"); + if(!s_io->read_int(rp->value, &cur)) cur = 0; + s_io->write(rp->direction, cur ? "high" : "low"); } return true; } @@ -106,27 +137,28 @@ static bool ensure_ready(int gpio) bool warden_relay_available(uint32_t idx) { if(idx >= WARDEN_RELAY_COUNT) return false; - return ensure_ready(s_gpio[idx]); + struct relay_paths rp; + if(!relay_paths(idx, &rp)) return false; + return ensure_ready(idx, &rp); } bool warden_relay_get(uint32_t idx) { if(idx >= WARDEN_RELAY_COUNT) return false; - if(!exported(s_gpio[idx])) return false; + struct relay_paths rp; + if(!relay_paths(idx, &rp)) return false; + if(!exported(&rp)) return false; - char p[96]; int v; - snprintf(p, sizeof(p), "%s/gpio%d/value", gpio_root(), s_gpio[idx]); - if(!s_io->read_int(p, &v)) v = 0; + if(!s_io->read_int(rp.value, &v)) v = 0; return v != 0; } void warden_relay_set(uint32_t idx, bool on) { if(idx >= WARDEN_RELAY_COUNT) return; - if(!ensure_ready(s_gpio[idx])) return; - - char p[96]; - snprintf(p, sizeof(p), "%s/gpio%d/value", gpio_root(), s_gpio[idx]); - s_io->write(p, on ? "1" : "0"); + struct relay_paths rp; + if(!relay_paths(idx, &rp)) return; + if(!ensure_ready(idx, &rp)) return; + s_io->write(rp.value, on ? "1" : "0"); } diff --git a/drivers/relays/relays.h b/drivers/relays/relays.h index a21987f..21eb1a7 100644 --- a/drivers/relays/relays.h +++ b/drivers/relays/relays.h @@ -16,6 +16,10 @@ * the decision logic runs and is measured to 100% MC/DC on the host, and the * gpio root is `$WARDEN_GPIO_ROOT`-overridable so the real backend can run * against a scratch tree in an integration test. + * + * Paths are built in PATH_MAX buffers. A root too long for its longest path + * ("/gpioNN/direction" within PATH_MAX) is refused with a stderr line, and + * every call then reports unavailable / off / no-op; it is never truncated. */ #ifndef WARDEN_RELAYS_H #define WARDEN_RELAYS_H diff --git a/drivers/relays/test/test_relays.c b/drivers/relays/test/test_relays.c index 78cb2cb..98118c7 100644 --- a/drivers/relays/test/test_relays.c +++ b/drivers/relays/test/test_relays.c @@ -9,6 +9,8 @@ #include "../relays.h" #include +#include +#include #include #include #include @@ -30,11 +32,12 @@ static struct { const char *read_line_val; bool read_int_ok; int read_int_val; - char last_write_path[160]; + char last_write_path[PATH_MAX]; char last_write_val[32]; + int exists_calls; } fk; -static bool fk_exists(const char *p) { (void)p; return fk.exists_ret; } +static bool fk_exists(const char *p) { (void)p; fk.exists_calls++; return fk.exists_ret; } static void fk_write(const char *p, const char *v) { snprintf(fk.last_write_path, sizeof fk.last_write_path, "%s", p); snprintf(fk.last_write_val, sizeof fk.last_write_val, "%s", v); @@ -56,6 +59,104 @@ static const struct relay_io FAKE = { fk_exists, fk_write, fk_read_line, fk_read static void fk_reset(void) { memset(&fk, 0, sizeof fk); } +/* ---- stderr capture: proves a refusal is logged, not silent ---- */ + +static char g_err[1024]; +static int g_saved_stderr = -1; +static char g_err_path[] = "/tmp/warden-relays-stderr-XXXXXX"; + +static void err_begin(void) +{ + int fd = mkstemp(g_err_path); + assert(fd >= 0); + fflush(stderr); + g_saved_stderr = dup(2); + dup2(fd, 2); + close(fd); +} + +static void err_end(void) +{ + fflush(stderr); + dup2(g_saved_stderr, 2); + close(g_saved_stderr); + FILE *f = fopen(g_err_path, "r"); + size_t n = f ? fread(g_err, 1, sizeof g_err - 1, f) : 0; + g_err[n] = '\0'; + if(f) fclose(f); + unlink(g_err_path); + memcpy(g_err_path + strlen(g_err_path) - 6, "XXXXXX", 6); +} + +/* A root of exactly @p len bytes ("/" then 'a's); it need not exist for fake io. */ +static char g_long[PATH_MAX + 64]; +static const char *long_root(size_t len) +{ + assert(len >= 1 && len < sizeof g_long); + g_long[0] = '/'; + memset(g_long + 1, 'a', len - 1); + g_long[len] = '\0'; + return g_long; +} + +/* Root-length bounds (issue #10). The longest path is "/gpio32/direction", + * so a root of PATH_MAX - 1 - strlen("/gpio32/direction") bytes is the longest + * that fits. One byte more must be refused loudly, never truncated. */ +static void long_root_unit_tests(void) +{ + const size_t max_root = PATH_MAX - 1 - strlen("/gpio32/direction"); + + /* Well past the old 80/96-byte buffers but inside PATH_MAX: works, and the + * io sees the full, untruncated path. */ + fk_reset(); fk.exists_ret = true; fk.read_line_ok = true; fk.read_line_val = "out\n"; + setenv("WARDEN_GPIO_ROOT", long_root(200), 1); + warden_relay_set(0, true); + EXPECT(strcmp(fk.last_write_val, "1") == 0); + EXPECT(strlen(fk.last_write_path) == 200 + strlen("/gpio32/value")); + EXPECT(strncmp(fk.last_write_path, g_long, 200) == 0); + EXPECT(strcmp(fk.last_write_path + 200, "/gpio32/value") == 0); + + /* Exactly at the bound: still accepted. */ + fk_reset(); fk.exists_ret = true; fk.read_line_ok = true; fk.read_line_val = "out\n"; + setenv("WARDEN_GPIO_ROOT", long_root(max_root), 1); + EXPECT(warden_relay_available(0) == true); + + /* One byte over: "/gpio32" still fits, the direction path does not. + * Every entry point refuses, logs, and never touches the io. */ + setenv("WARDEN_GPIO_ROOT", long_root(max_root + 1), 1); + fk_reset(); fk.exists_ret = true; fk.read_line_ok = true; fk.read_line_val = "out\n"; + fk.read_int_ok = true; fk.read_int_val = 1; + err_begin(); + EXPECT(warden_relay_available(0) == false); + err_end(); + EXPECT(strstr(g_err, "WARDEN_GPIO_ROOT is too long") != NULL); + EXPECT(fk.exists_calls == 0); + + err_begin(); + EXPECT(warden_relay_get(0) == false); + err_end(); + EXPECT(strstr(g_err, "WARDEN_GPIO_ROOT is too long") != NULL); + EXPECT(fk.exists_calls == 0); + + err_begin(); + warden_relay_set(1, true); + err_end(); + EXPECT(strstr(g_err, "WARDEN_GPIO_ROOT is too long") != NULL); + EXPECT(fk.last_write_path[0] == '\0'); + EXPECT(fk.exists_calls == 0); + + /* A root longer than PATH_MAX itself: refused the same way. */ + setenv("WARDEN_GPIO_ROOT", long_root(PATH_MAX + 10), 1); + fk_reset(); fk.exists_ret = true; + err_begin(); + EXPECT(warden_relay_available(1) == false); + err_end(); + EXPECT(strstr(g_err, "WARDEN_GPIO_ROOT is too long") != NULL); + EXPECT(fk.exists_calls == 0); + + unsetenv("WARDEN_GPIO_ROOT"); +} + static void unit_tests(void) { warden_relay__set_io(&FAKE); @@ -142,18 +243,20 @@ static void unit_tests(void) fk_reset(); fk.exists_ret = true; fk.read_line_ok = true; fk.read_line_val = "out\n"; warden_relay_set(0, false); EXPECT(strcmp(fk.last_write_val, "0") == 0); + + long_root_unit_tests(); } /* ---------------- layer 2: real sysfs backend over a scratch tree ---------------- */ -static char g_root[128]; +static char g_root[PATH_MAX]; static void wr(const char *rel, const char *content) { - char p[256]; snprintf(p, sizeof p, "%s/%s", g_root, rel); + char p[PATH_MAX + 64]; snprintf(p, sizeof p, "%s/%s", g_root, rel); FILE *f = fopen(p, "w"); if(f) { fputs(content, f); fclose(f); } } static void mkgpio(int n) { - char p[256]; snprintf(p, sizeof p, "%s/gpio%d", g_root, n); mkdir(p, 0777); + char p[PATH_MAX + 64]; snprintf(p, sizeof p, "%s/gpio%d", g_root, n); mkdir(p, 0777); } static void integration_tests(void) @@ -207,6 +310,24 @@ static void integration_tests(void) wr("gpio33/direction", "in\n"); wr("gpio33/value", "1\n"); EXPECT(warden_relay_available(1) == true); /* reads value=1, writes direction "high" */ + + /* Issue #10: a real scratch tree under a root far longer than the old 80/96-byte + * buffers (the simulator's case). Both relays must be available and switchable. */ + for(int i = 0; i < 5; i++) { + size_t l = strlen(g_root); + snprintf(g_root + l, sizeof g_root - l, "/%s", "long-scratch-directory-component-for-issue-10"); + EXPECT(mkdir(g_root, 0777) == 0); + } + EXPECT(strlen(g_root) > 200); + setenv("WARDEN_GPIO_ROOT", g_root, 1); + mkgpio(32); mkgpio(33); + wr("gpio32/direction", "out\n"); wr("gpio32/value", "0\n"); + wr("gpio33/direction", "out\n"); wr("gpio33/value", "0\n"); + EXPECT(warden_relay_available(0) == true); + EXPECT(warden_relay_available(1) == true); + warden_relay_set(1, true); + EXPECT(warden_relay_get(1) == true); + EXPECT(warden_relay_get(0) == false); } int main(void) -- 2.54.0