|
| 1 | +# SEC-006 — Daemon mode uses world-writable `/tmp/solikv.pid` + `/tmp/solikv.log`, with weak process verification |
| 2 | + |
| 3 | +**Severity:** High |
| 4 | + |
| 5 | +## Location |
| 6 | + |
| 7 | +- `crates/solikv-server/src/main.rs:106` (`pidfile = PathBuf::from("/tmp/solikv.pid")`) |
| 8 | +- `crates/solikv-server/src/main.rs:108-130` (kill-old logic) |
| 9 | +- `crates/solikv-server/src/main.rs:132-138` (`/tmp/solikv.log` opened with `create + append`) |
| 10 | + |
| 11 | +## Issue |
| 12 | + |
| 13 | +Three problems compound: |
| 14 | + |
| 15 | +1. **Symlink attack on the log file.** `OpenOptions::new().create(true).append(true) |
| 16 | + .open("/tmp/solikv.log")` follows existing symlinks. On a multi-user host an |
| 17 | + attacker who controls `/tmp/solikv.log` (it is in a sticky-bit directory but |
| 18 | + they can win the race or pre-place a symlink before solikv first runs) can |
| 19 | + point it at any path the operator can write to (e.g. `/etc/something`, |
| 20 | + `~/.bashrc`). solikv then appends its stdout/stderr there. |
| 21 | + |
| 22 | +2. **PID file race + weak verification.** Lines 113-122 read `/proc/<pid>/cmdline` |
| 23 | + and only check `.contains("solikv")`. Any cmdline containing the substring |
| 24 | + "solikv" passes — including a user's `vim /tmp/solikv-notes`, `grep solikv |
| 25 | + …`, etc. Combined with the predictable `/tmp/solikv.pid` path (which a local |
| 26 | + attacker can pre-write), this lets the attacker get solikv to send `kill` to |
| 27 | + an arbitrary PID belonging to the operator (so long as that process's |
| 28 | + cmdline mentions "solikv"). |
| 29 | + |
| 30 | +3. **Use of `kill(1)` subprocess** for what is a built-in syscall — extra fork, |
| 31 | + extra attack surface, and silently swallows errors with `let _ = ... .output()`. |
| 32 | + |
| 33 | +## Fix |
| 34 | + |
| 35 | +1. Move pidfile and log to `args.dir` (the operator-supplied data directory), |
| 36 | + defaulting to `./data`. Refuse to operate on `/tmp` paths. |
| 37 | +2. Open the log with `OpenOptions::new().custom_flags(libc::O_NOFOLLOW)` (Unix) |
| 38 | + to refuse symlinks; or open with `O_CREAT | O_EXCL` first and fall back to |
| 39 | + append-after-stat-check. |
| 40 | +3. Strengthen the PID check: parse `/proc/<pid>/exe` (a symlink to the binary) |
| 41 | + and compare to `std::env::current_exe()`. `cmdline.contains("solikv")` is |
| 42 | + not a security check. |
| 43 | +4. Replace the `Command::new("kill")` subprocess with `nix::sys::signal::kill` |
| 44 | + or `libc::kill` directly. |
| 45 | +5. After unlinking the stale pidfile, write the new one with `O_EXCL` so two |
| 46 | + concurrent daemon starts cannot both succeed. |
| 47 | + |
| 48 | +## Verification |
| 49 | + |
| 50 | +- Unit (where feasible): given a PID whose `/proc/<pid>/exe` does not equal our |
| 51 | + current exe, the kill path is a no-op. |
| 52 | +- Manual: `ln -s /tmp/victim /tmp/solikv.log`, run `solikv -d`, confirm solikv |
| 53 | + refuses (or creates a new file separate from the symlink target). |
0 commit comments