3"""Semantic checks for public startup and live bench capabilities."""
5from __future__
import annotations
16from pathlib
import Path
18import hil_convergence_safety_image_lock_receipts
as image_lock_receipts
19import hil_convergence_safety_policy
as policy
20import hil_convergence_safety_roles
as roles
22from check_shebangs
import (
23 PINNED_INTERPRETER_BOUNDARIES,
24 PRIVILEGED_BODY_CLOSE,
25 PRIVILEGED_BODY_PREFIX,
28CONTEXT_INPUT_FIELD_COUNT = 2
29DEV_DIR = Path(__file__).resolve().parents[1] /
"dev"
30HIL_LIB = Path(__file__).resolve().parents[1] /
"hil/lib"
31for module_dir
in (DEV_DIR, HIL_LIB):
32 sys.path.insert(0, str(module_dir))
34import bench_lock_broker
as broker
35import bench_lock_capability
as capability
36import bench_lock_verify
as lock_verify
37import fleet_transaction_auth
as transaction
40def _lock_verifier_errors(source: str) -> list[str]:
41 """Require nonblocking descriptor classification and its live selftest."""
43 tree = ast.parse(source)
45 return [
"bench lock verifier: invalid Python"]
50 if isinstance(node, ast.FunctionDef)
and node.name ==
"_has_script_digest"
54 expected = ast.parse(
"if not stat.S_ISREG(before.st_mode):\n continue").body[0]
58 else [node
for node
in ast.walk(digest)
if ast.dump(node) == ast.dump(expected)]
60 errors = []
if len(matches) == 1
else [
"bench lock verifier: non-regular fds may block"]
65 if isinstance(node, ast.FunctionDef)
and node.name ==
"run_selftest"
71 for node
in (()
if run
is None else ast.walk(run))
72 if isinstance(node, ast.Call)
73 and isinstance(node.func, ast.Name)
74 and node.func.id ==
"_live_descriptor_selftest"
77 errors.append(
"bench lock verifier: live descriptor selftest is not load-bearing")
81def _canonical_context_inputs(source: str) -> set[str]:
82 """Return exact paths from the image builder's canonical root-input table."""
84 r"(?ms)^ canonical_root_context_inputs\(\) \{\n"
85 r" cat <<'EOF'\n(.*?)^EOF\n \}"
87 matches = pattern.findall(source)
90 rows = [line.split(maxsplit=1)
for line
in matches[0].splitlines()
if line]
91 if any(len(row) != CONTEXT_INPUT_FIELD_COUNT
or not row[0].isdigit()
for row
in rows):
93 return {row[1]
for row
in rows}
96def _context_scope_errors(
97 defaults_source: str, transaction_source: str, image_source: str
99 """Require every asserted provisioning input to enter the exact archive."""
101 defaults = yaml.safe_load(defaults_source)
102 tasks = yaml.safe_load(transaction_source)
103 except yaml.YAMLError:
104 return [
"dev box context: malformed defaults or transaction"]
105 scopes = defaults.get(
"dev_box_context_paths")
if isinstance(defaults, dict)
else None
107 not isinstance(scopes, list)
109 or any(
not isinstance(scope, str)
or not scope
for scope
in scopes)
110 or not isinstance(tasks, list)
112 return [
"dev box context: archive scopes are missing or malformed"]
116 if isinstance(task, dict)
and task.get(
"name") ==
"Assert the staged context arrived"
118 required = matches[0].get(
"loop")
if len(matches) == 1
else None
119 if not isinstance(required, list)
or any(
not isinstance(path, str)
for path
in required):
120 return [
"dev box context: authoritative input census is missing or malformed"]
121 consumed = _canonical_context_inputs(image_source)
123 return [
"dev box context: image root-input authority is missing or malformed"]
125 def covered(path: str) -> bool:
126 return any(path == scope
or path.startswith(scope.rstrip(
"/") +
"/")
for scope
in scopes)
128 errors: list[str] = []
129 missing = sorted(path
for path
in {*required, *consumed}
if not covered(path))
132 "dev box context: required paths escape archive scopes: " +
", ".join(missing)
134 unasserted = sorted(consumed - set(required))
137 "dev box context: consumed root inputs escape the assertion: " +
", ".join(unasserted)
142def _image_lock_tasks(
143 tasks: list[object], names: tuple[str, ...]
144) -> tuple[dict[str, dict[str, object]], list[str]]:
145 """Return each required image-lock task exactly once."""
146 found: dict[str, dict[str, object]] = {}
147 errors: list[str] = []
149 matches = [task
for task
in tasks
if isinstance(task, dict)
and task.get(
"name") == name]
150 if len(matches) == 1:
151 found[name] = matches[0]
153 errors.append(f
"dev box image lock: task is not unique: {name}")
157def _normalized_conditions(task: dict[str, object]) -> set[str] |
None:
158 """Return an assert task's whitespace-normalized conditions."""
159 assertion = task.get(
"ansible.builtin.assert")
160 conditions = assertion.get(
"that")
if isinstance(assertion, dict)
else None
161 if not isinstance(conditions, list)
or not all(isinstance(item, str)
for item
in conditions):
163 return {
" ".join(item.split())
for item
in conditions}
166def _stat_task_is_exact(task: dict[str, object], path: str, register: str) -> bool:
167 """Return whether a stat task pins its path, no-follow behavior, and result."""
168 stat = task.get(
"ansible.builtin.stat")
170 task.get(
"become")
is True
171 and isinstance(stat, dict)
172 and stat.get(
"path") == path
173 and stat.get(
"follow")
is False
174 and task.get(
"register") == register
178def _image_lock_directory_errors(named: dict[str, dict[str, object]]) -> list[str]:
179 """Require refusal, convergence, and post-proof for the managed directory."""
180 errors: list[str] = []
181 path =
"{{ dev_box_image_lock_dir }}"
182 inspect_name =
"Inspect the managed image lock directory without following links"
183 if not _stat_task_is_exact(named[inspect_name], path,
"dev_box_image_lock_dir_before"):
184 errors.append(
"dev box image lock: directory inspection may follow links")
185 refuse_name =
"Refuse an unsafe managed image lock directory"
187 "not dev_box_image_lock_dir_before.stat.exists or "
188 "(dev_box_image_lock_dir_before.stat.isdir and "
189 "not dev_box_image_lock_dir_before.stat.islnk and "
190 "dev_box_image_lock_dir_before.stat.uid == 0 and "
191 "dev_box_image_lock_dir_before.stat.gid "
192 "== (dev_box_image_lock_gid.stdout | int) and "
193 "dev_box_image_lock_dir_before.stat.mode == '0750')"
195 if _normalized_conditions(named[refuse_name]) != required_refusal:
196 errors.append(
"dev box image lock: unsafe directory refusal drifted")
197 expected_directory = {
199 "state":
"directory",
201 "group":
"{{ dev_box_image_lock_gid.stdout }}",
204 create_name =
"Create the managed image lock directory"
205 if named[create_name].get(
"ansible.builtin.file") != expected_directory
or not named[
208 errors.append(
"dev box image lock: directory ownership or mode drifted")
209 post_name =
"Reinspect the converged managed image lock directory"
211 not _stat_task_is_exact(named[post_name], path,
"dev_box_image_lock_dir_after")
212 or named[post_name].get(
"when") !=
"not ansible_check_mode"
214 errors.append(
"dev box image lock: directory post-stat may follow links")
215 proof_name =
"Prove the managed image lock directory identity and permissions"
217 "dev_box_image_lock_dir_after.stat.isdir",
218 "not dev_box_image_lock_dir_after.stat.islnk",
219 "dev_box_image_lock_dir_after.stat.uid == 0",
220 "dev_box_image_lock_dir_after.stat.gid == (dev_box_image_lock_gid.stdout | int)",
221 "dev_box_image_lock_dir_after.stat.mode == '0750'",
224 _normalized_conditions(named[proof_name]) != required_proof
225 or named[proof_name].get(
"when") !=
"not ansible_check_mode"
227 errors.append(
"dev box image lock: directory post-proof drifted")
231def _image_lock_file_errors(named: dict[str, dict[str, object]]) -> list[str]:
232 """Require the stable single-link file authority and post-proof."""
233 errors: list[str] = []
234 lock_path =
"{{ dev_box_image_lock_dir }}/devcontainer-image.lock"
235 inspect_name =
"Inspect the managed image lock file without following links"
236 if not _stat_task_is_exact(named[inspect_name], lock_path,
"dev_box_image_lock_before"):
237 errors.append(
"dev box image lock: file inspection may follow links")
238 refuse_name =
"Refuse an unsafe managed image lock file"
240 "not dev_box_image_lock_before.stat.exists or "
241 "(dev_box_image_lock_before.stat.isreg and "
242 "not dev_box_image_lock_before.stat.islnk and "
243 "dev_box_image_lock_before.stat.nlink == 1 and "
244 "dev_box_image_lock_before.stat.uid == 0 and "
245 "dev_box_image_lock_before.stat.gid "
246 "== (dev_box_image_lock_gid.stdout | int) and "
247 "dev_box_image_lock_before.stat.mode == '0660')"
249 if _normalized_conditions(named[refuse_name]) != required_refusal:
250 errors.append(
"dev box image lock: unsafe file refusal drifted")
256 "group":
"{{ dev_box_image_lock_gid.stdout }}",
258 "access_time":
"preserve",
259 "modification_time":
"preserve",
261 create_name =
"Create the stable managed image lock file"
262 if named[create_name].get(
"ansible.builtin.file") != expected_lock
or not named[
265 errors.append(
"dev box image lock: file ownership, mode, or inode stability drifted")
266 post_name =
"Reinspect the converged managed image lock"
268 not _stat_task_is_exact(named[post_name], lock_path,
"dev_box_image_lock_after")
269 or named[post_name].get(
"when") !=
"not ansible_check_mode"
271 errors.append(
"dev box image lock: file post-stat may follow links")
272 proof_name =
"Prove the managed image lock identity and permissions"
273 required_conditions = {
274 "dev_box_image_lock_after.stat.isreg",
275 "not dev_box_image_lock_after.stat.islnk",
276 "dev_box_image_lock_after.stat.nlink == 1",
277 "dev_box_image_lock_after.stat.uid == 0",
278 "dev_box_image_lock_after.stat.gid == (dev_box_image_lock_gid.stdout | int)",
279 "dev_box_image_lock_after.stat.gid == dev_box_image_lock_dir_after.stat.gid",
280 "dev_box_image_lock_after.stat.mode == '0660'",
283 _normalized_conditions(named[proof_name]) != required_conditions
284 or named[proof_name].get(
"when") !=
"not ansible_check_mode"
286 errors.append(
"dev box image lock: post-convergence identity proof drifted")
290def _image_lock_gid_errors(named: dict[str, dict[str, object]]) -> list[str]:
291 """Require the non-root numeric primary-group authority."""
292 resolve = named[
"Resolve the managed image lock numeric primary group"]
293 command = resolve.get(
"ansible.builtin.command")
294 expected = {
"argv": [
"/usr/bin/id",
"-g",
"--",
"{{ dev_box_user }}"]}
295 errors: list[str] = []
297 resolve.get(
"become")
is not True
298 or command != expected
299 or resolve.get(
"register") !=
"dev_box_image_lock_gid"
300 or resolve.get(
"changed_when")
is not False
301 or resolve.get(
"check_mode")
is not False
303 errors.append(
"dev box image lock: numeric group resolution drifted")
305 "dev_box_image_lock_gid.stdout is regex('^[0-9]+$')",
306 "(dev_box_image_lock_gid.stdout | int) > 0",
308 refusal = named[
"Refuse an unsafe managed image lock numeric primary group"]
309 if _normalized_conditions(refusal) != required:
310 errors.append(
"dev box image lock: unsafe numeric group refusal drifted")
314def _image_lock_marker_errors(named: dict[str, dict[str, object]]) -> list[str]:
315 """Require the root-owned immutable numeric-group marker authority."""
316 marker =
"{{ dev_box_image_lock_dir }}/devcontainer-image.gid"
318 *_image_lock_marker_inspection_errors(named, marker),
319 *_image_lock_marker_convergence_errors(named, marker),
320 *_image_lock_marker_proof_errors(named),
324def _image_lock_marker_inspection_errors(
325 named: dict[str, dict[str, object]], marker: str
327 """Require no-follow inspection and exact refusal of an unsafe marker."""
328 errors: list[str] = []
329 inspect = named[
"Inspect the managed image lock group marker without following links"]
330 inspect_stat = inspect.get(
"ansible.builtin.stat")
332 not _stat_task_is_exact(inspect, marker,
"dev_box_image_lock_gid_marker_before")
333 or not isinstance(inspect_stat, dict)
334 or inspect_stat.get(
"get_checksum")
is not True
335 or inspect_stat.get(
"checksum_algorithm") !=
"sha256"
337 errors.append(
"dev box image lock: group marker inspection may follow links")
339 "not dev_box_image_lock_gid_marker_before.stat.exists or "
340 "(dev_box_image_lock_gid_marker_before.stat.isreg and "
341 "not dev_box_image_lock_gid_marker_before.stat.islnk and "
342 "dev_box_image_lock_gid_marker_before.stat.nlink == 1 and "
343 "dev_box_image_lock_gid_marker_before.stat.uid == 0 and "
344 "dev_box_image_lock_gid_marker_before.stat.gid == 0 and "
345 "dev_box_image_lock_gid_marker_before.stat.mode == '0444' and "
346 "dev_box_image_lock_gid_marker_before.stat.checksum "
347 "== ((dev_box_image_lock_gid.stdout ~ '\\n') | hash('sha256')))"
349 refuse = named[
"Refuse an unsafe managed image lock group marker"]
350 if _normalized_conditions(refuse) != required_refusal:
351 errors.append(
"dev box image lock: unsafe group marker refusal drifted")
355def _image_lock_marker_convergence_errors(
356 named: dict[str, dict[str, object]], marker: str
358 """Require atomic marker convergence and an exact post-stat."""
359 errors: list[str] = []
362 "content":
"{{ dev_box_image_lock_gid.stdout }}\n",
366 "unsafe_writes":
False,
368 create = named[
"Create the managed image lock numeric group marker atomically"]
370 create.get(
"become")
is not True
371 or create.get(
"ansible.builtin.copy") != expected_copy
372 or create.get(
"when")
373 !=
"not ansible_check_mode or dev_box_image_lock_dir_before.stat.exists"
375 errors.append(
"dev box image lock: group marker atomic convergence drifted")
376 post = named[
"Reinspect the converged managed image lock group marker"]
377 post_stat = post.get(
"ansible.builtin.stat")
379 not _stat_task_is_exact(post, marker,
"dev_box_image_lock_gid_marker_after")
380 or not isinstance(post_stat, dict)
381 or post_stat.get(
"get_checksum")
is not True
382 or post_stat.get(
"checksum_algorithm") !=
"sha256"
383 or post.get(
"when") !=
"not ansible_check_mode"
385 errors.append(
"dev box image lock: group marker post-stat drifted")
389def _image_lock_marker_proof_errors(
390 named: dict[str, dict[str, object]],
392 """Require the marker, directory, and lock to share the approved identity."""
394 "dev_box_image_lock_gid_marker_after.stat.isreg",
395 "not dev_box_image_lock_gid_marker_after.stat.islnk",
396 "dev_box_image_lock_gid_marker_after.stat.nlink == 1",
397 "dev_box_image_lock_gid_marker_after.stat.uid == 0",
398 "dev_box_image_lock_gid_marker_after.stat.gid == 0",
399 "dev_box_image_lock_gid_marker_after.stat.mode == '0444'",
400 "dev_box_image_lock_gid_marker_after.stat.checksum "
401 "== ((dev_box_image_lock_gid.stdout ~ '\\n') | hash('sha256'))",
402 "dev_box_image_lock_dir_after.stat.gid == (dev_box_image_lock_gid.stdout | int)",
403 "dev_box_image_lock_after.stat.gid == (dev_box_image_lock_gid.stdout | int)",
405 proof = named[
"Prove the managed image lock group marker identity and content"]
407 _normalized_conditions(proof) != required_proof
408 or proof.get(
"when") !=
"not ansible_check_mode"
410 return [
"dev box image lock: group marker identity proof drifted"]
414def _image_lock_environment_errors(named: dict[str, dict[str, object]]) -> list[str]:
415 """Require both root image commands to consume the managed path explicitly."""
416 expected_environment = {
417 "RA8_CI_IMAGE":
"{{ dev_box_ci_image }}",
418 "RA8_IMAGE_LOCK_DIR":
"{{ dev_box_image_lock_dir }}",
421 "Prove the staleness check itself works, before trusting its verdict",
422 "Build the gate image unless the cached one matches this context",
425 f
"dev box image lock: explicit environment drifted: {name}"
426 for name
in command_names
427 if named[name].get(
"environment") != expected_environment
431def _image_lock_order_errors(tasks: list[object]) -> list[str]:
432 """Require refusal-before-repair and selftest-before-use task ordering."""
435 for index, task
in enumerate(tasks)
436 if isinstance(task, dict)
and isinstance(task.get(
"name"), str)
439 "Refuse an unsafe managed image lock directory",
440 "Refuse an unsafe managed image lock file",
441 "Refuse an unsafe managed image lock group marker",
444 "Create the managed image lock directory",
445 "Create the stable managed image lock file",
446 "Create the managed image lock numeric group marker atomically",
450 "Resolve the managed image lock numeric primary group",
451 "Refuse an unsafe managed image lock numeric primary group",
454 "Refuse an unsafe managed image lock numeric primary group",
457 *((refusal, creation)
for refusal
in refusals
for creation
in creations),
459 "Prove the managed image lock group marker identity and content",
460 "Prove the staleness check itself works, before trusting its verdict",
463 "Prove the staleness check itself works, before trusting its verdict",
464 "Build the gate image unless the cached one matches this context",
468 f
"dev box image lock: required task order drifted: {before} before {after}"
469 for before, after
in required_pairs
470 if positions.get(before, len(tasks)) >= positions.get(after, -1)
474def _image_lock_profile_errors(tasks: list[object]) -> list[str]:
475 """Refuse a profile lock authority now that canonical discovery is exact."""
476 name =
"Set the container runtime and the shared compiler cache for every shell"
477 matches = [task
for task
in tasks
if isinstance(task, dict)
and task.get(
"name") == name]
478 copy = matches[0].get(
"ansible.builtin.copy")
if len(matches) == 1
else None
479 content = copy.get(
"content")
if isinstance(copy, dict)
else None
480 if not isinstance(content, str):
481 return [
"dev box image lock: shell profile task is missing"]
482 if "RA8_IMAGE_LOCK_DIR" in content:
483 return [
"dev box image lock: shell profile is a second lock authority"]
487def _image_lock_task_errors(tasks: list[object]) -> list[str]:
488 """Require exact Ansible ownership, identity, and caller environments."""
490 "Resolve the managed image lock numeric primary group",
491 "Refuse an unsafe managed image lock numeric primary group",
492 "Inspect the managed image lock directory without following links",
493 "Refuse an unsafe managed image lock directory",
494 "Create the managed image lock directory",
495 "Reinspect the converged managed image lock directory",
496 "Prove the managed image lock directory identity and permissions",
497 "Inspect the managed image lock file without following links",
498 "Refuse an unsafe managed image lock file",
499 "Create the stable managed image lock file",
500 "Reinspect the converged managed image lock",
501 "Prove the managed image lock identity and permissions",
502 "Inspect the managed image lock group marker without following links",
503 "Refuse an unsafe managed image lock group marker",
504 "Create the managed image lock numeric group marker atomically",
505 "Reinspect the converged managed image lock group marker",
506 "Prove the managed image lock group marker identity and content",
507 "Prove the staleness check itself works, before trusting its verdict",
508 "Build the gate image unless the cached one matches this context",
510 named, errors = _image_lock_tasks(tasks, names)
514 *_image_lock_gid_errors(named),
515 *_image_lock_directory_errors(named),
516 *_image_lock_file_errors(named),
517 *_image_lock_marker_errors(named),
518 *_image_lock_environment_errors(named),
519 *_image_lock_order_errors(tasks),
520 *_image_lock_profile_errors(tasks),
524def _image_lock_required_source_tokens() -> tuple[str, ...]:
525 """Return the exact production image-lock authority tokens."""
527 'ra8_bound_entry="${RA8_SELFTEST_BOUND_ENTRY-}"',
528 "unset -v RA8_SELFTEST_BOUND_ENTRY",
529 '[[ "${BASH_SOURCE[0]}" =~ ^/proc/self/fd/[0-9]+$ &&',
530 '"$ra8_bound_entry" == /*/devcontainer_image.sh &&',
531 '-f "$ra8_bound_entry" && ! -L "$ra8_bound_entry" &&',
532 '"${BASH_SOURCE[0]}" -ef "$ra8_bound_entry"',
533 '[[ "$ra8_bound_entry" == "$SCRIPT_DIR/devcontainer_image.sh" ]] || {',
534 '"${BASH_SOURCE[0]}" -ef "$main_path"',
535 'CANONICAL_IMAGE_LOCK_DIR="/var/cache/ra8-devcontainer-image-lock"',
536 'IMAGE_LOCK_GROUP_GID=""',
537 "validate_managed_image_lock_group_marker() {",
539 "validate_managed_image_lock_dir() {",
540 "validate_image_lock_file() {",
541 "validate_opened_image_lock() {",
542 "\n resolve_image_lock() {",
543 ' elif [[ -e "$canonical_dir" || -L "$canonical_dir" ||\n'
544 ' -e "$canonical_dir/devcontainer-image.lock" ||\n'
545 ' -L "$canonical_dir/devcontainer-image.lock" ||\n'
546 ' -e "$canonical_dir/devcontainer-image.gid" ||\n'
547 ' -L "$canonical_dir/devcontainer-image.gid" ]]; then',
548 '\n lock_dir="$canonical_dir"\n IMAGE_LOCK_MANAGED=1\n',
549 '[[ "$links" == "1" && "$owner" == "0" && "$group" == "0" && "$mode" == "444" ]]',
550 '[[ "$marker_gid" =~ ^[0-9]+$ && "$marker_gid" != "0" ]]',
551 "if IFS= read -r -n 1 extra <&7; then",
552 'size="$(fd_size 7)"',
553 "((size == ${#marker_gid} + 1)) ||\n"
554 ' die "managed image lock group marker must contain exactly one numeric gid line"',
555 '[[ "$opened" == "$identity" && "$current" == "$opened" ]] ||\n'
556 ' die "managed image lock group marker changed while opening it"',
557 '[[ "$owner" == "0" && "$mode" == "750" ]]',
558 '[[ "$group" == "$IMAGE_LOCK_GROUP_GID" ]] ||\n'
559 ' die "managed image lock directory gid does not match its marker: $group"',
560 '[[ "$group" == "$IMAGE_LOCK_GROUP_GID" ]] ||\n'
561 ' die "managed image lock gid does not match its marker: $group"',
562 'exec 9<"$IMAGE_LOCK_FILE"',
563 'build_locked "$want" forced "" 1',
564 "\n managed_image_lock_preflight\n",
565 'dispatch_image_lock_selftest "$@"',
566 "\n load_image_lock_selftest\n selftest_case_signal_child ",
567 'export IMAGE_LOCK_RECEIPTS_RAW_SHA256="',
568 '"$helper" == "$SCRIPT_DIR/devcontainer_image_lock_receipts.bash" ||',
569 '\n [[ "$IMAGE_LOCK_MANAGED" == "0" ]] ||\n'
570 ' die "flock is required for the managed image lock"',
574def _image_lock_required_case_tokens() -> tuple[str, ...]:
575 """Return the exact receipt-first image-lock loader tokens."""
577 "load_image_lock_selftest() {",
578 'local receipts="$SCRIPT_DIR/devcontainer_image_lock_receipts.bash"',
579 'output="$(/bin/bash -p -- "$receipts" 2>&1)"',
580 '"$output" == "error: devcontainer image lock receipt helper is source-only"',
581 'source_approved_selftest_helper "$receipts" "$IMAGE_LOCK_RECEIPTS_RAW_SHA256"\n'
582 ' source_approved_selftest_helper "$helper" "$IMAGE_LOCK_SELFTEST_RAW_SHA256"',
583 "declare -F expected_image_lock_suite_receipts scenario_receipt_value",
584 "require_controller_cleanup_receipt_file dispatch_image_lock_selftest >/dev/null",
585 '\n dispatch_image_lock_selftest suite "$tmp"\n',
589def _image_lock_script_errors(image_source: str, cases_source: str) -> list[str]:
590 """Require discovery, no-create locking, and force serialization."""
591 required_source = _image_lock_required_source_tokens()
592 required_cases = _image_lock_required_case_tokens()
594 if any(image_source.count(token) != 1
for token
in required_source)
or any(
595 cases_source.count(token) != 1
for token
in required_cases
597 errors.append(
"dev box image lock: fail-closed helper or selftest is not load-bearing")
598 expected_open_validations = 2
599 open_validation =
"\n validate_opened_image_lock 9\n"
600 if image_source.count(open_validation) != expected_open_validations:
601 errors.append(
"dev box image lock: opened inode is not checked before and after flock")
602 if "/var/cache/ra8-tools" in image_source
or "RA8_TOOLS_CACHE_DIR" in image_source:
603 errors.append(
"dev box image lock: unmanaged path still uses the sticky shared cache")
604 if 'exec 9>>"$IMAGE_LOCK_FILE"' in image_source
or 'exec 9>"$IMAGE_LOCK_FILE"' in image_source:
605 errors.append(
"dev box image lock: managed open may recreate the lock")
606 if 'while [[ ! -e "$ready" ]]' in image_source:
607 errors.append(
"dev box image lock: force-contention readiness wait is unbounded")
611def _image_lock_selftest_errors(
617 """Require bounded process-group cleanup and every semantic attack."""
618 required_once = policy.image_lock_selftest_required_tokens()
619 receipt_required_once = policy.image_lock_receipt_required_tokens()
620 cases_required_once = policy.image_lock_cases_required_tokens()
621 signal_required_once = policy.image_lock_signal_required_tokens()
623 '"$SELFTEST_CASE_DIR/ready.status"',
624 '"$SELFTEST_CASE_DIR/done.status"',
629 any(source.count(token) != 1
for token
in required_once)
630 or any(token
not in source
for token
in required_present)
631 or any(receipt_source.count(token) != 1
for token
in receipt_required_once)
632 or any(cases_source.count(token) != 1
for token
in cases_required_once)
633 or any(signal_source.count(token) != 1
for token
in signal_required_once)
635 errors.append(
"dev box image lock: bounded selftest harness is not load-bearing")
637 'kill -KILL "$SELFTEST_WORKER_PID"',
638 'kill -KILL "$controller"',
639 'wait "$SELFTEST_WORKER_PID" || true',
643 or token
in receipt_source
644 or token
in cases_source
645 or token
in signal_source
646 for token
in forbidden
648 errors.append(
"dev box image lock: selftest contains an unbounded wait")
652def _image_lock_authority_errors(inputs: dict[str, str]) -> list[str]:
653 """Require one managed cross-user image lock and a private local default."""
654 defaults_source = inputs[
"dev_defaults"]
655 transaction_entry_source = inputs[
"dev_transaction"]
656 transaction_source = inputs[
"dev_main"]
657 image_source = inputs[
"devcontainer_image"]
658 image_receipt_source = inputs[
"devcontainer_image_lock_receipts"]
659 image_selftest_source = inputs[
"devcontainer_image_lock_selftest"]
660 image_cases_source = inputs[
"devcontainer_image_selftest_cases"]
661 image_signal_source = inputs[
"devcontainer_image_signal_selftest"]
663 "- name: Converge the managed devcontainer image lock authority\n"
664 " ansible.builtin.include_tasks: image_lock.yml\n"
667 defaults = yaml.safe_load(defaults_source)
668 tasks = yaml.safe_load(transaction_source)
669 except yaml.YAMLError:
670 return [
"dev box image lock: malformed defaults or transaction"]
671 if not isinstance(defaults, dict)
or not isinstance(tasks, list):
672 return [
"dev box image lock: defaults or transaction has the wrong shape"]
673 errors = _image_lock_task_errors(tasks)
674 if transaction_entry_source.count(include) != 1:
675 errors.append(
"dev box image lock: transaction include boundary drifted")
676 authority =
"/var/cache/ra8-devcontainer-image-lock"
677 if defaults.get(
"dev_box_image_lock_dir") != authority:
678 errors.append(
"dev box image lock: managed directory authority drifted")
679 if image_source.count(f
'CANONICAL_IMAGE_LOCK_DIR="{authority}"') != 1:
680 errors.append(
"dev box image lock: Ansible and script authorities diverged")
683 *_image_lock_script_errors(image_source, image_cases_source),
684 *_image_lock_selftest_errors(
685 image_selftest_source,
686 image_receipt_source,
690 *image_lock_receipts.errors(
692 image_receipt_source,
693 image_selftest_source,
700def semantic_errors() -> list[str]:
701 """Execute offline selftests for each new authentication boundary."""
703 *capability.run_selftest(),
704 *broker.run_selftest(),
705 *lock_verify.run_selftest(),
706 *transaction.run_selftest(),
708 return [f
"HIL convergence v9 semantic selftest: {failure}" for failure
in failures]
711def _semantic_authority_lines(lines: tuple[str, ...]) -> list[str]:
712 """Normalize wrapper presentation indentation for semantic comparison."""
713 return [line.strip()
for line
in lines]
716def startup_authority_selftest() -> list[str]:
717 """Prove an indented canonical wrapper retains the same semantics."""
718 return roles.startup_authority_selftest(PRIVILEGED_BODY_PREFIX)
721def _public_script_errors(source: str, rel: str, safe_path: str) -> list[str]:
722 """Require sanitation using check_shebangs' authority, never a drifting copy."""
723 expected_preamble = list(PINNED_INTERPRETER_BOUNDARIES[rel])
724 lines = source.splitlines()
725 if lines[: len(expected_preamble)] != expected_preamble:
726 return [f
"{rel}: interpreter boundary is not exact"]
729 for line
in lines[len(expected_preamble) :]
730 if line.strip()
and not line.lstrip().startswith(
"#")
732 prefix = _semantic_authority_lines(PRIVILEGED_BODY_PREFIX)
733 suffix = _semantic_authority_lines(PRIVILEGED_BODY_CLOSE)
734 sanitizer =
"unset PYTHONHOME PYTHONPATH RA8_TOOL_VENV"
735 if rel ==
"scripts/dev/provision_dev_box_toolchain.sh":
736 sanitizer +=
" TMPDIR"
739 "export BASH_ENV=/dev/null ENV=/dev/null PYTHONNOUSERSITE=1",
744 wrapper_ok = executable[: len(prefix)] == prefix
and executable[-len(suffix) :] == suffix
745 body = executable[len(prefix) : -len(suffix)]
if wrapper_ok
else []
746 errors = []
if body[:5] == expected
else [f
"{rel}: startup sanitizer moved"]
747 if any(re.search(
r"(?<![/\.\w-])bash\b", line)
for line
in executable):
748 errors.append(f
"{rel}: child Bash resolves through caller PATH")
752def _installer_errors(inputs: dict[str, str]) -> list[str]:
753 """Check direct public setup and tool-provision entrypoint boundaries."""
755 _public_script_errors(
756 inputs[
"setup_ansible"],
"scripts/dev/setup_ansible.sh",
"/usr/bin:/bin"
758 + _public_script_errors(
759 inputs[
"provision_toolchain"],
760 "scripts/dev/provision_dev_box_toolchain.sh",
761 "/usr/local/bin:/usr/bin:/bin",
763 + _public_script_errors(inputs[
"infra_bootstrap"],
"infra/bootstrap.sh",
"/usr/bin:/bin")
767def _infra_boundary_errors(infra_just: str, infra_sh: str) -> list[str]:
768 """Bind the dependency-free public boundary probe to sanitized infra."""
769 recipe =
"_boundary_selftest:\n {{ infra }} --selftest-boundary"
770 endpoint =
"""if [[ "${1:-}" == --selftest-boundary ]]; then
772 echo "error: infrastructure boundary selftest takes no arguments" >&2
778 '[[ -z "${BASH_ENV:-}" && -z "${ENV:-}" && -z "${PYTHONPATH:-}"'
779 ' && -z "${PYTHONHOME:-}" ]] || {\n'
780 ' echo "error: infrastructure startup environment was not sanitized" >&2\n'
784 if infra_just.count(recipe) != 1:
785 errors.append(
"just/infra.just: public boundary selftest recipe is not exact")
786 if infra_sh.count(endpoint) != 1:
787 errors.append(
"scripts/dev/infra.sh: boundary selftest endpoint is not exact")
788 if infra_sh.count(sanitizer) != 1:
789 errors.append(
"scripts/dev/infra.sh: startup sanitizer is not exact")
790 elif infra_sh.find(endpoint) < infra_sh.find(sanitizer) + len(sanitizer):
791 errors.append(
"scripts/dev/infra.sh: boundary selftest precedes startup sanitation")
795def _hil_shell_errors(raw_sources: str) -> list[str]:
796 """Require fixed privileged Bash at every HIL-owned shell boundary."""
798 sources = json.loads(raw_sources)
799 except json.JSONDecodeError:
800 return [
"HIL shell boundary inventory is malformed"]
801 if not isinstance(sources, dict)
or not sources:
802 return [
"HIL shell boundary inventory is empty"]
803 errors: list[str] = []
804 for path, source
in sources.items():
805 if not isinstance(path, str)
or not isinstance(source, str):
806 errors.append(
"HIL shell boundary inventory entry is malformed")
808 shebangs = [line.strip()
for line
in source.splitlines()
if line.lstrip().startswith(
"#!")]
809 if not shebangs
or any(line !=
"#!/bin/bash -p" for line
in shebangs):
810 errors.append(f
"{path}: shell entry is not fixed privileged Bash")
811 executable = [line
for line
in source.splitlines()
if not line.lstrip().startswith(
"#")]
812 if any(re.search(
r"(?<!/)\bbash\b", line)
for line
in executable):
813 errors.append(f
"{path}: active Bash invocation resolves through caller PATH")
815 path ==
"scripts/ci/monitor.sh"
816 and source.count(
"ExecStart=/bin/bash -p $self daemon") != 1
818 errors.append(
"scripts/ci/monitor.sh: generated service Bash argv is not exact")
822def _write_executable(path: Path, marker: Path) ->
None:
823 """Write one hostile startup executable used only in the temp fixture."""
824 path.parent.mkdir(parents=
True, exist_ok=
True)
825 path.write_text(f
"#!/bin/sh\nprintf x >>'{marker}'\nexit 91\n", encoding=
"utf-8")
829def _hostile_environment(fixture: Path, marker: Path) -> dict[str, str]:
830 """Create one hostile caller environment without executing its payloads."""
831 startup = fixture /
"startup.sh"
832 startup.write_text(f
"printf x >>'{marker}'\n", encoding=
"utf-8")
833 site = fixture /
"site"
835 (site /
"sitecustomize.py").write_text(
836 f
"from pathlib import Path\nPath({str(marker)!r}).write_text('x')\n",
839 fake_bin = fixture /
"bin"
840 for name
in (
"bash",
"dirname",
"python3"):
841 _write_executable(fake_bin / name, marker)
842 fake_venv = fixture /
"venv"
843 for name
in (
"python3",
"ansible-playbook"):
844 _write_executable(fake_venv / f
"bin/{name}", marker)
845 account = pwd.getpwuid(os.getuid())
847 "BASH_ENV": str(startup),
849 "HOME": account.pw_dir,
852 "LOGNAME": account.pw_name,
853 "PATH": f
"{fake_bin}:/usr/bin:/bin",
854 "PYTHONHOME": str(fixture /
"python-home"),
855 "PYTHONPATH": str(site),
856 "RA8_TOOL_VENV": str(fake_venv),
857 "USER": account.pw_name,
861def _run_boundary(argv: list[str], root: Path, environment: dict[str, str]) -> int:
862 """Run one offline public boundary with fixed process controls."""
863 result = subprocess.run(
871 return result.returncode
874def _just_boundary_errors(
875 just: str, root: Path, fixture: Path, marker: Path, environment: dict[str, str]
877 """Exercise the infra and HIL Just public entrypoints offline."""
878 prefix = [str(Path(just).resolve()),
"--justfile", str(root /
"justfile")]
879 hostile_tool_env = dict(environment)
880 clean_tool_env = dict(environment)
881 del clean_tool_env[
"RA8_TOOL_VENV"]
882 hostile_rc = _run_boundary([*prefix,
"infra::list"], root, hostile_tool_env)
884 if marker.exists()
or hostile_rc == 0:
885 errors.append(
"caller-selected tool environment did not fail closed")
886 marker.unlink(missing_ok=
True)
887 infra_rc = _run_boundary([*prefix,
"infra::_boundary_selftest"], root, clean_tool_env)
888 if marker.exists()
or infra_rc != 0:
889 errors.append(
"hostile startup executed at the public infra entry")
890 marker.unlink(missing_ok=
True)
891 hil_rc = _run_boundary(
892 [*prefix,
"hil::preflash_check", str(fixture /
"absent.elf")],
897 errors.append(
"hostile startup executed at the public HIL entry")
899 errors.append(
"offline invalid-image HIL entry unexpectedly succeeded")
903def _installer_boundary_selftest(
904 root: Path, marker: Path, environment: dict[str, str]
906 """Exercise the three direct setup/provision boundaries offline."""
909 "scripts/dev/setup_ansible.sh",
910 "scripts/dev/provision_dev_box_toolchain.sh",
911 "infra/bootstrap.sh",
913 marker.unlink(missing_ok=
True)
914 argv = [
"/bin/bash",
"-p", str(root / relative),
"--selftest-boundary"]
915 if _run_boundary(argv, root, environment) != 0
or marker.exists():
916 errors.append(f
"{relative}: hostile startup boundary failed")
920def public_boundary_selftest(root: Path) -> list[str]:
921 """Run real public entries with every supported startup injection hostile."""
922 just = shutil.which(
"just")
924 return [
"public infra boundary selftest cannot find Just"]
925 with tempfile.TemporaryDirectory(prefix=
"ra8-infra-boundary-")
as raw:
927 marker = fixture /
"executed"
928 environment = _hostile_environment(fixture, marker)
929 return _just_boundary_errors(
930 just, root, fixture, marker, environment
931 ) + _installer_boundary_selftest(root, marker, environment)
934def errors(inputs: dict[str, str]) -> list[str]:
935 """Return structural v9 findings for the supplied source bytes."""
937 _lock_verifier_errors(inputs[
"bench_lock_verify"])
938 + _context_scope_errors(
939 inputs[
"dev_defaults"], inputs[
"dev_main"], inputs[
"devcontainer_image"]
941 + _image_lock_authority_errors(inputs)
942 + roles.pin_and_shell_authority_errors(
943 inputs[
"dev_main"], inputs[
"dockerfile"], inputs[
"root_justfile"]
945 + roles.dev_box_shell_boundary_errors(inputs[
"dev_main"], inputs[
"hil_just"])
946 + _installer_errors(inputs)
947 + _infra_boundary_errors(inputs[
"infra_just"], inputs[
"infra_sh"])
948 + _hil_shell_errors(inputs[
"hil_shells"])