Commit 2104960
authored
fix(security): P1-1 + P1-3 — reject flag-shaped names in systemctl + fail2ban calls (#41)
* fix(security): reject systemctl unit names that look like flags
The HTTP handler for POST /api/v1/services/{name}/{action} validated the
service name with a per-character whitelist that allowed "-" anywhere,
including position 0. A name like "--all", "--no-block", or "-h" passed
validation and reached exec.Command("systemctl", action, name), where
systemctl treats it as a flag instead of a unit. Discrete-arg exec.Command
prevents shell injection but not flag injection — the kernel doesn't know
which argv slot is "the flags" and which is "the operand."
Concrete impact: a caller authenticated to the agent could pass action=start
and service=--all, producing `systemctl start --all`, which systemd
interprets as "start every available unit." Lower-privilege flags are
similar — `--no-block` (don't wait), `--state=...` (filter), etc.
Fix:
- New validUnitName regex (`^[a-zA-Z0-9][a-zA-Z0-9._@-]{0,255}$`) anchors
the first character to alphanumeric so flag-shaped names are rejected
at the HTTP boundary. Subsequent characters allow the systemd unit
charset; total length unchanged (256 cap).
- ControlService passes "--" before the unit name in exec.Command as
defense-in-depth.
Tests pin down both the legitimate-name pass list (sshd.service, haproxy,
getty@tty1.service, etc.) and the attacker-controlled reject list (--all,
-h, --no-block, .leading-dot, names with whitespace/semicolons/$()).
P1-1 from the 2026-05 security audit.
* fix(security): validate fail2ban jail names before exec; add `--` separator
getJailStats called `fail2ban-client status <jail>` with the jail name
flowing in from getJails(), which parses `fail2ban-client status` output.
The only flow today is internal: agent → fail2ban-client → parse →
fail2ban-client. There is no HTTP path that lets a caller specify a jail
name directly, so this is defense-in-depth rather than a closed exploit.
Two scenarios are still worth defending against:
1. A future code path that exposes a jail-name parameter to HTTP input
(e.g. "force fail2ban-client to refresh jail X"), where the same
flag-injection class as P1-1 (systemctl) would re-emerge.
2. A malicious or malformed fail2ban configuration that produces a
weird jail name. fail2ban itself accepts jail names that include
characters our parser passes through unchecked, so a jail named
"--help" on a misconfigured host would crash the agent's collection
loop or worse.
Fix:
- New validJailName regex (`^[a-zA-Z0-9][a-zA-Z0-9_-]{0,63}$`) anchors
the first character to alphanumeric. getJailStats rejects mismatches
with an explicit error rather than passing them through.
- exec.Command passes "--" before the jail name.
Tests cover both legitimate jail names (sshd, haproxy-http, my_jail)
and the flag-injection class (--help, -h) plus the usual injection
characters (newlines, semicolons, $()).
P1-3 from the 2026-05 security audit.1 parent 6a834fe commit 2104960
5 files changed
Lines changed: 130 additions & 12 deletions
File tree
- gearbox-agent/internal/gears
- metrics
- security
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
231 | 231 | | |
232 | 232 | | |
233 | 233 | | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
234 | 239 | | |
235 | | - | |
236 | | - | |
| 240 | + | |
237 | 241 | | |
238 | 242 | | |
239 | 243 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
347 | 348 | | |
348 | 349 | | |
349 | 350 | | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
16 | 25 | | |
17 | 26 | | |
18 | 27 | | |
| |||
267 | 276 | | |
268 | 277 | | |
269 | 278 | | |
270 | | - | |
271 | | - | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
272 | 287 | | |
273 | 288 | | |
274 | 289 | | |
275 | | - | |
276 | | - | |
277 | | - | |
278 | | - | |
279 | | - | |
280 | | - | |
281 | | - | |
282 | 290 | | |
283 | 291 | | |
284 | 292 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
22 | 36 | | |
23 | 37 | | |
24 | 38 | | |
| |||
142 | 156 | | |
143 | 157 | | |
144 | 158 | | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
145 | 166 | | |
146 | 167 | | |
147 | 168 | | |
148 | | - | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
149 | 174 | | |
150 | 175 | | |
151 | 176 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
104 | 105 | | |
105 | 106 | | |
106 | 107 | | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
0 commit comments