hex/src/codesign.rs
Dax 92bcf45aa3
verify the hex microphone driver's signature before installing it (#2767)
* verify the Hex Microphone driver's signature before installing it

The driver install copied whatever bundle validated: the app's own copy,
or, when that was missing, scripts/build-hex-microphone-driver.sh's
output in ~/Library/Caches/hex-mic, a user-writable path. Nothing checked
the code signature, so an admin-approved install could plant a driver
from outside Hex's trust anchor into the HAL folder.

A published build (stable or dev channel) now checks the chosen bundle
with codesign --verify --deep --strict -R (src/codesign.rs) against
app/driver-requirement.txt: anchor apple generic, identifier
anomaly.hex.microphone.driver, Developer ID, and Anomaly's team
5NZ4Q7NXJ4. The check runs in the install worker before the admin prompt
and reports through DriverActionState::Failed.

dev_build::is_local_build is the gate: debug, test, HEX_LOCAL_BUILD,
HEX_TEST_BUILD, or an unmarked 0.0.0-dev checkout. It is narrower than
is_dev_build, whose dev-channel builds are published and signed with the
same Developer ID, so a dev-channel driver is verified too.

The candidate selection is a pure function: a bundle copy that is present
but broken fails the install instead of falling through to the cache, and
only local builds consider ~/Library/Caches/hex-mic at all, so an ad-hoc
or other-team signed driver still installs there.

The updater is untouched.

* read the bundled driver version without checking its signature

status() asks for the bundled driver's version on the UI thread (the macOS
host's platform poll), and the version read went through
bundled_driver_path(), which spawns codesign in a shipped build. The
version read now shares the install's candidate order and cache policy but
never verifies; only the install worker, before the admin prompt, checks
the signature.

Also says exactly what the requirement test pins (the driver's identifier
and the team ID the updater's requirement uses), and lists every kind of
local build the cache fallback covers in the feature map.

* driver install: re-check the signature as root and refuse symlinked copies

Addresses the second review of #2767.

The user-side codesign check ran before the admin prompt, so a bundle
swapped while the prompt was up was copied anyway. The requirement now
travels to osascript as its own argv item (empty for local builds) and
the root shell checks the staged copy with codesign --verify before it
replaces the driver or restarts coreaudiod. The copy refuses a symlinked
or non-directory source, drops ACLs and quarantine flags, and leaves the
staging copy root:wheel with no group or other write.

Candidate inspection uses symlink_metadata: a symlink (dangling included)
or a non-directory fails the install instead of reading as missing, and
only a real Hex.app's Contents folder counts as the running bundle. The
user home comes from app_paths.

is_local_build no longer treats a hand-stamped 0.0.0-dev-<N> build (what
scripts/release-app.sh makes, signed and notarized) as local; only
build.rs's -dev-local marker is local. is_dev_build is unchanged.

nix/package.nix's fileset now carries the two app/*-requirement.txt files
the Linux build and tests include, and the codesign helper compiles on
macOS only.
2026-11-24 16:58:01 -05:00

31 lines
1.3 KiB
Rust

//! The `/usr/bin/codesign` check the Hex Microphone driver install makes
//! before it asks for administrator rights: the driver bundle must satisfy
//! `app/driver-requirement.txt`, a designated requirement that pins its
//! identifier, Anomaly's Developer ID anchor, and the team. The updater keeps
//! its own copy of the same `codesign` invocation.
use std::path::Path;
use std::process::{Command, Stdio};
/// Verifies `bundle` against `requirement` with
/// `codesign --verify --deep --strict -R`, so nested code and every resource
/// must match too. `Ok` means the signature satisfies the requirement; `Err`
/// carries codesign's own complaint, or why it could not run.
pub fn verify(bundle: &Path, requirement: &str) -> Result<(), String> {
let output = Command::new("/usr/bin/codesign")
.args(["--verify", "--deep", "--strict"])
.arg(format!("-R={}", requirement.trim()))
.arg(bundle)
.stdin(Stdio::null())
.output()
.map_err(|error| format!("could not run codesign: {error}"))?;
if output.status.success() {
return Ok(());
}
let detail = String::from_utf8_lossy(&output.stderr);
let detail = detail.trim();
if detail.is_empty() {
return Err(format!("codesign did not accept it ({})", output.status));
}
Err(detail.to_owned())
}