"""Check the private Linux GCC launcher with a harmless ELF and the patched frontend.""" import argparse import hashlib import json import os from pathlib import Path import select import shutil import subprocess import tempfile NONCE = "146a66737eef9c04ea33c6e05589f701" ROOT = Path(__file__).resolve().parents[2] WITNESS = r""" #define _GNU_SOURCE #include #include #include #include #include #include #include #include int main(int argc, char **argv, char **env) { const char *fixed[] = {"cc1plus", "-quiet", "-std=c++26", "-freflection", "-fsyntax-only", "-nostdinc", "-o", "/dev/null", "-fvls-observe", "-fvls-step", "input.cpp"}; if (argc != 11) return 10; for (int i = 0; i < argc; ++i) if (strcmp(fixed[i], argv[i])) return 11; const char *nonce = getenv("VLS_GCC_NONCE"); if (!nonce || !getenv("LC_ALL") || strcmp("C", getenv("LC_ALL")) || !getenv("PATH") || *getenv("PATH") || getenv("VLS_TEST_PARENT_ONLY")) return 12; int count = 0; while (env[count]) ++count; if (count != 3) return 13; struct stat raw_in, raw_out, err, control, events; if (fstat(0, &raw_in) || fstat(1, &raw_out) || fstat(2, &err) || fstat(3, &control) || fstat(4, &events)) return 14; if (!S_ISCHR(raw_in.st_mode) || raw_in.st_rdev != makedev(1,3) || raw_out.st_rdev != raw_in.st_rdev || !S_ISFIFO(err.st_mode) || !S_ISFIFO(control.st_mode) || !S_ISFIFO(events.st_mode) || control.st_ino == events.st_ino || err.st_ino == events.st_ino) return 15; for (int fd = 5; fd < 32; ++fd) if (fcntl(fd, F_GETFD) >= 0) return 16; struct rlimit core, cpu, memory, files, descriptors; if (getrlimit(RLIMIT_CORE, &core) || getrlimit(RLIMIT_CPU, &cpu) || getrlimit(RLIMIT_FSIZE, &files) || getrlimit(RLIMIT_AS, &memory) || getrlimit(RLIMIT_NOFILE, &descriptors)) return 17; if (core.rlim_max || cpu.rlim_cur > 15 || cpu.rlim_max > 20 || memory.rlim_max > 768ULL * 1024 * 1024 || files.rlim_max > 1024 * 1024 || descriptors.rlim_max > 32) return 18; int self = open("/proc/self/exe", O_RDONLY); int seals = fcntl(self, F_GET_SEALS); int required = F_SEAL_WRITE | F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL; if (seals < 0 || (seals & required) != required) return 19; close(self); dprintf(1, "discarded raw output\n"); dprintf(2, "witness diagnostic\n"); dprintf(4, "%s\n", nonce); char go; if (read(3, &go, 1) != 1 || go != 'g') return 20; dprintf(4, "same sealed executable\n"); return 0; } """ def digest(path): with path.open("rb") as file: return hashlib.file_digest(file, "sha256").hexdigest() def require(ok, message): if not ok: raise ValueError(message) def args(probe, compiler, sha, nonce=NONCE, mode="step"): return [str(probe), "--leaf", str(compiler), sha, nonce, mode] def verify_leaf(probe, compiler, cc, work): source = work / "witness.c" source.write_text(WITNESS) witness = work / "witness" subprocess.run([cc, str(source), "-o", str(witness)], check=True, timeout=30) sha = digest(witness) saved = work / "original-witness" env = {**os.environ, "VLS_TEST_PARENT_ONLY": "must not reach compiler"} with subprocess.Popen(args(probe, witness, sha), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=work, env=env, bufsize=0) as child: try: require(select.select([child.stdout], [], [], 5)[0], "witness did not start") require(child.stdout.readline() == NONCE.encode() + b"\n", "sealed ELF did not verify process state") witness.rename(saved) witness.write_bytes(b"this file cannot be a compiler") out, err = child.communicate(b"g", timeout=5) require((out, err, child.returncode) == (b"same sealed executable\n", b"witness diagnostic\n", 0), f"unexpected witness output {out!r}, {err!r}, {child.returncode}") finally: if child.poll() is None: child.kill() child.wait(timeout=3) print("Verified fixed argv, pipe identity, resource limits, environment and sealed execution after replacement") script = work / "benign.sh" script.write_text("#!/bin/sh\nexit 0\n") script.chmod(0o700) partial = work / "partial" partial.write_bytes(saved.read_bytes()[:-24]) partial.chmod(0o700) link = work / "alias" link.symlink_to(saved) for path, expected, nonce in ((witness, sha, NONCE), (saved, "0" * 64, NONCE), (saved, sha, "x" * 32), (script, digest(script), NONCE), (partial, digest(partial), NONCE), (link, sha, NONCE), (work, sha, NONCE)): result = subprocess.run(args(probe, path, expected, nonce), input=b"", capture_output=True, timeout=5, cwd=work) require(result.returncode == 126 and not result.stdout, f"unexpected admission of {path}: {result}") with open(os.devnull, "rb") as wrong_control: result = subprocess.run(args(probe, saved, sha), stdin=wrong_control, capture_output=True, timeout=5, cwd=work) require(result.returncode == 126 and not result.stdout, "ordinary file accepted as control") print("Eight invalid image, identity and descriptor configurations refused") shutil.copyfile(ROOT / "tests/corpus/frontend/gcc-live.cpp", work / "input.cpp") with subprocess.Popen(args(probe, compiler, digest(compiler), mode="observe"), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=work) as child: try: out, err = child.communicate(timeout=45) finally: if child.poll() is None: child.kill() child.wait(timeout=3) require(child.returncode == 0 and not err, f"real compiler exit {child.returncode}: {err!r}") events = [json.loads(line) for line in out.splitlines()] require(events and events[0]["kind"] == "hello" and events[0]["pid"] == child.pid and events[0]["backend"] == "gcc-16.2.0/vls-eval-1", "compiler-origin handshake failed") require(all(e["nonce"] == NONCE and e["seq"] == i for i, e in enumerate(events)), "invalid event stream") values = [int(e["value"]["bitsHex"], 16) for e in events if e["kind"] == "value" and e["value"]["kind"] == "integer"] require(values == [17, 34, 22, 44] and events[-1]["kind"] == "frontendEnd", f"unexpected returns: {values}") print("Real sealed cc1plus reported returns 17, 34, 22 and 44") if __name__ == "__main__": parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--probe", type=Path, required=True) parser.add_argument("--cc1plus", type=Path, required=True) parser.add_argument("--cc", default="cc") cli = parser.parse_args() with tempfile.TemporaryDirectory(prefix="vls-gcc-leaf-") as folder: verify_leaf(cli.probe.resolve(strict=True), cli.cc1plus.resolve(strict=True), cli.cc, Path(folder))