diff --git a/bin/true-check b/bin/true-check index 8032ca8..151853f 100755 --- a/bin/true-check +++ b/bin/true-check @@ -875,7 +875,10 @@ def safe(st) -> bool: def open_consumed(create: bool) -> int | None: """consumed.log as an fd: never through a symlink, never when group/other-writable. None if absent.""" import stat - flags = os.O_RDWR | os.O_APPEND | os.O_NOFOLLOW | (os.O_CREAT if create else 0) + # O_NONBLOCK is a defensive portability guard, not a Linux fix: POSIX leaves + # O_RDWR on a FIFO undefined. On Linux the open already returns without + # blocking, and fstat below already refuses anything that is not a plain file. + flags = os.O_RDWR | os.O_APPEND | os.O_NOFOLLOW | os.O_NONBLOCK | (os.O_CREAT if create else 0) try: fd = os.open(consumed_log(), flags, 0o600) except FileNotFoundError: @@ -958,12 +961,24 @@ def gate_pass(subject: dict, gate: str) -> tuple[str | None, str | None]: if not name.endswith(".json"): continue try: - fd = os.open(f, os.O_RDONLY | os.O_NOFOLLOW) + lst = os.lstat(f) + except OSError: + continue # gone + if not stat.S_ISREG(lst.st_mode): + # A FIFO, device, link or dir named *.json: opening a FIFO would block (D11). + why = f"enforced_by {gate}: {name} is not a plain file; skipped" + continue + try: + # O_NONBLOCK: if the entry was swapped for a FIFO after lstat, the open still returns. + fd = os.open(f, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK) except OSError: continue # a link, or gone try: st = os.fstat(fd) - if not stat.S_ISREG(st.st_mode) or not safe(st): + if not stat.S_ISREG(st.st_mode): + why = f"enforced_by {gate}: {name} is not a plain file; skipped" + continue + if not safe(st): continue # a record anyone else could write proves nothing with os.fdopen(os.dup(fd), "rb") as fh: raw = fh.read() diff --git a/tests/test_true_check.py b/tests/test_true_check.py index eaa4000..ae9f9c4 100644 --- a/tests/test_true_check.py +++ b/tests/test_true_check.py @@ -1090,6 +1090,42 @@ def test_d9_consumed_log_link_or_open_is_refused_and_not_followed(self): self.assertEqual(rc, 11) self.assertIn("consumed.log is not a plain file or is writable by others", out["allow_reason"]) + def bounded(self, cmd, subj): + """Run eval or verify with a 20 s cap, so a hang fails the test instead of stalling the suite.""" + sp, rp = self.tmp / "d11-subject.json", self.tmp / "d11-result.json" + sp.write_text(json.dumps(subj)) + args = ["eval", str(sp), "--out", str(rp)] if cmd == "eval" else ["verify", str(rp), str(sp)] + try: + cp = subprocess.run(["python3", str(BIN), *args], text=True, capture_output=True, env=self.env, timeout=20) + except subprocess.TimeoutExpired: + self.fail(f"true-check {cmd} hung on a FIFO in the gate-passes dir (D11)") + return cp.returncode, (json.loads(rp.read_text()) if cmd == "eval" and rp.exists() else None) + + # Paul's cc6d61c probes, D11: a FIFO named *.json must not hang eval or verify + def test_d11_fifo_record_is_skipped_without_hanging(self): + subj = self.approve_row_and_subject() + d = self.tmp / "passes" / "approve-gate" + d.mkdir(parents=True) + d.chmod(0o755) + d.parent.chmod(0o755) + os.mkfifo(d / "0.json", 0o600) + rc, out = self.bounded("eval", subj) + self.assertEqual(rc, 11) + self.assertEqual(out["allow_reason"], "gate row g: enforced_by approve-gate: 0.json is not a plain file; skipped") + self.record(subj, "approve-gate") # pass-*.json sorts after the FIFO + rc, out = self.bounded("eval", subj) + self.assertEqual((rc, out["allowed"]), (0, True), out["allow_reason"]) + self.assertEqual(self.bounded("verify", subj)[0], 0) + self.assertEqual(self.used_lines(), 1) + + def test_d11_fifo_consumed_log_is_refused_without_hanging(self): + subj = self.approve_row_and_subject() + self.record(subj, "approve-gate") + os.mkfifo(self.tmp / "passes" / "consumed.log", 0o600) + rc, out = self.bounded("eval", subj) + self.assertEqual(rc, 11) + self.assertIn("consumed.log is not a plain file", out["allow_reason"]) + def test_record_ts_in_the_future_says_so(self): subj = self.approve_row_and_subject() self.record(subj, "approve-gate", age=-300)