Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions bin/true-check
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Expand Down
36 changes: 36 additions & 0 deletions tests/test_true_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading