mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 18:44:41 +01:00
(cherry picked from commit 4caf45b04a8faa55f191b22c069f58714236689b) Ported for #244 (part 1/6). Internal-only content from the archived commit was dropped: .aiwg records, internal runner-policy routing, and the internal security-docs test. Marked experimental in user docs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
84 lines
2.3 KiB
Python
84 lines
2.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Bind one content-addressed source read-only inside an isolated mount namespace."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import ctypes
|
|
import os
|
|
import re
|
|
from pathlib import Path
|
|
|
|
|
|
_SOURCE_NAME = re.compile(r"^source-[0-9a-f]{64}$")
|
|
_MS_RDONLY = 1
|
|
_MS_NOSUID = 2
|
|
_MS_NODEV = 4
|
|
_MS_NOEXEC = 8
|
|
_MS_REMOUNT = 32
|
|
_MS_BIND = 4096
|
|
_MS_REC = 16384
|
|
_MS_PRIVATE = 1 << 18
|
|
_EXIT_FAILURE = 70
|
|
|
|
|
|
def _mount(
|
|
source: str | None,
|
|
target: str,
|
|
flags: int,
|
|
) -> None:
|
|
library = ctypes.CDLL(None, use_errno=True)
|
|
operation = library.mount
|
|
operation.argtypes = [
|
|
ctypes.c_char_p,
|
|
ctypes.c_char_p,
|
|
ctypes.c_char_p,
|
|
ctypes.c_ulong,
|
|
ctypes.c_void_p,
|
|
]
|
|
operation.restype = ctypes.c_int
|
|
source_bytes = source.encode() if source is not None else None
|
|
if operation(source_bytes, target.encode(), None, flags, None) != 0:
|
|
raise OSError(ctypes.get_errno(), "mount namespace operation failed")
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--source", type=Path, required=True)
|
|
parser.add_argument("command", nargs=argparse.REMAINDER)
|
|
args = parser.parse_args()
|
|
|
|
source = args.source
|
|
command = list(args.command)
|
|
if command[:1] == ["--"]:
|
|
command = command[1:]
|
|
try:
|
|
resolved = source.resolve(strict=True)
|
|
if (
|
|
not source.is_absolute()
|
|
or resolved != source
|
|
or source.is_symlink()
|
|
or not source.is_dir()
|
|
or _SOURCE_NAME.fullmatch(source.name) is None
|
|
or not command
|
|
or not Path(command[0]).is_absolute()
|
|
):
|
|
return _EXIT_FAILURE
|
|
_mount(None, "/", _MS_REC | _MS_PRIVATE)
|
|
# A non-recursive bind deliberately excludes writable nested mounts.
|
|
# The exact artifact is one filesystem tree, not an arbitrary mount graph.
|
|
_mount(str(source), str(source), _MS_BIND)
|
|
_mount(
|
|
None,
|
|
str(source),
|
|
_MS_BIND | _MS_REMOUNT | _MS_RDONLY | _MS_NOSUID | _MS_NODEV | _MS_NOEXEC,
|
|
)
|
|
os.execve(command[0], command, os.environ)
|
|
except (OSError, RuntimeError, ValueError):
|
|
return _EXIT_FAILURE
|
|
return _EXIT_FAILURE
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|