Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.
Python agentagent-osaiai-agentsai-partnerchatopsclaudeclaude-codecodexdevtoolsdiscord-botlark-botllmlocal-firstlong-term-memoryopencodeslack-botvibe-codingvibeywechat
feat(model-hub): keep engine state owner-private on Windows through a shared DACL helper (#2468)
Call White665e090ff07f
diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.ymlindex a9a6d4394..2c3aab307 100644--- a/.github/workflows/lint.yml+++ b/.github/workflows/lint.yml@@ -580,6 +580,13 @@ jobs:- name: Verify Windows Controller control IPCrun: python -m pytest tests/test_control_ipc.py -q+ # Windows reports 0o777/0o666 whatever the access rules are, so the Model+ # Hub's engine state and takeover journal are owner-private only through+ # the DACL helper control IPC shares. Engine start, an API-key Source and+ # the files the engine writes itself all depend on it.+ - name: Verify owner-private Model Hub state+ run: python -m pytest tests/test_private_fs.py tests/test_model_hub_state_private.py -q -ra+# Windows byte-range locks are mandatory and per-handle, so a lock taken# over the bytes of a record makes that record unreadable to every other# handle -- including this process's own. Nothing on Linux can observediff --git a/core/control_ipc.py b/core/control_ipc.pyindex 4f57f06a9..98b7f8c2f 100644--- a/core/control_ipc.py+++ b/core/control_ipc.py@@ -18,6 +18,7 @@ from pathlib import Pathfrom typing import Callable, Iterator, Optionalfrom config import paths+from core.private_fs import PrivatePathError, windows_securityCONTROL_IPC_SCHEMA_VERSION = 1CONTROL_IPC_INSTANCE_HEADER = "X-Avibe-Control-Instance"@@ -51,8 +52,9 @@ class ControlIpcDescriptorError(ValueError):"""Raised when the Windows endpoint descriptor is absent or invalid."""-class ControlIpcSecurityError(PermissionError):- """Raised when a Windows control IPC path is not private to its owner."""+# Raised when a Windows control IPC path is not private to its owner. The+# owner and DACL checks live in the shared owner-private helper.+ControlIpcSecurityError = PrivatePathError@dataclass(frozen=True)@@ -388,7 +390,7 @@ def _load_descriptor_unlocked(target: Path) -> ControlIpcDescriptor:if not stat.S_ISREG(metadata.st_mode):raise ControlIpcDescriptorError(f"control IPC descriptor is not a regular file at {target}")if os.name == "nt":- _windows_security().validate_file_descriptor(fd, target)+ windows_security().validate_file_descriptor(fd, target)if metadata.st_size <= 0 or metadata.st_size > MAX_DESCRIPTOR_BYTES:raise ControlIpcDescriptorError(f"control IPC descriptor size is invalid at {target}")with os.fdopen(fd, "r", encoding="utf-8") as stream:@@ -418,7 +420,7 @@ def write_descriptor_atomic(_ensure_private_directory(target.parent, allow_repair=True)with _descriptor_lock(target, prepare_directory=True):if os.name == "nt":- fd, temporary = _windows_security().create_private_temporary_file(target)+ fd, temporary = windows_security().create_private_temporary_file(target)else:fd, temporary_name = tempfile.mkstemp(prefix=f".{target.name}.",@@ -447,10 +449,10 @@ def write_descriptor_atomic(if os.name == "nt" and target.exists():if target.is_symlink():raise ControlIpcSecurityError(f"control IPC descriptor security is invalid at {target}")- _windows_security().secure_existing_owned_path(target)+ windows_security().secure_existing_owned_path(target)os.replace(temporary, target)if os.name == "nt":- _windows_security().validate_path(target)+ windows_security().validate_path(target)finally:if fd >= 0:os.close(fd)@@ -509,7 +511,7 @@ def _validate_bearer_token(value: object) -> None:def _ensure_private_directory(directory: Path, *, allow_repair: bool = False) -> None:if os.name == "nt":- _windows_security().ensure_private_directory(directory, allow_repair=allow_repair)+ windows_security().ensure_private_directory(directory, allow_repair=allow_repair)returndirectory.mkdir(parents=True, exist_ok=True, mode=0o700)try:@@ -531,7 +533,7 @@ def _descriptor_lock(lock_path = descriptor_path.with_name(f"{descriptor_path.name}.lock")_ensure_private_directory(lock_path.parent, allow_repair=prepare_directory)if os.name == "nt":- fd = _windows_security().open_private_lock_file(+ fd = windows_security().open_private_lock_file(lock_path,allow_repair=prepare_directory,)@@ -569,594 +571,3 @@ def _descriptor_lock(fcntl.flock(fd, fcntl.LOCK_UN)finally:os.close(fd)---class _WindowsSecurity:- """Minimal Win32 owner/DACL operations for control IPC artifacts."""-- _SDDL_REVISION_1 = 1- _SE_FILE_OBJECT = 1- _OWNER_SECURITY_INFORMATION = 0x00000001- _DACL_SECURITY_INFORMATION = 0x00000004- _PROTECTED_DACL_SECURITY_INFORMATION = 0x80000000- _SE_DACL_PRESENT = 0x0004- _SE_DACL_PROTECTED = 0x1000- _TOKEN_QUERY = 0x0008- _TOKEN_USER = 1- _TOKEN_OWNER = 4- _ERROR_INSUFFICIENT_BUFFER = 122- _ERROR_FILE_EXISTS = 80- _ERROR_ALREADY_EXISTS = 183- _GENERIC_READ = 0x80000000- _GENERIC_WRITE = 0x40000000- _FILE_SHARE_READ = 0x00000001- _FILE_SHARE_WRITE = 0x00000002- _FILE_SHARE_DELETE = 0x00000004- _CREATE_NEW = 1- _OPEN_ALWAYS = 4- _FILE_ATTRIBUTE_NORMAL = 0x00000080- _ACL_SIZE_INFORMATION_CLASS = 2-- def __init__(self) -> None:- import ctypes- from ctypes import wintypes-- self.ctypes = ctypes- self.wintypes = wintypes- self.advapi32 = ctypes.WinDLL("advapi32", use_last_error=True)- self.kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)-- class SecurityAttributes(ctypes.Structure):- _fields_ = [- ("nLength", wintypes.DWORD),- ("lpSecurityDescriptor", wintypes.LPVOID),- ("bInheritHandle", wintypes.BOOL),- ]-- class SidAndAttributes(ctypes.Structure):- _fields_ = [- ("Sid", wintypes.LPVOID),- ("Attributes", wintypes.DWORD),- ]-- class TokenUser(ctypes.Structure):- _fields_ = [("User", SidAndAttributes)]-- class TokenOwner(ctypes.Structure):- _fields_ = [("Owner", wintypes.LPVOID)]-- class AclSizeInformation(ctypes.Structure):- _fields_ = [- ("AceCount", wintypes.DWORD),- ("AclBytesInUse", wintypes.DWORD),- ("AclBytesFree", wintypes.DWORD),- ]-- self.SecurityAttributes = SecurityAttributes- self.TokenUser = TokenUser- self.TokenOwner = TokenOwner- self.AclSizeInformation = AclSizeInformation- self._configure_functions()- self.current_user_sid = self._read_current_user_sid()- # Windows stamps newly created objects with the token's *default owner*,- # which is BUILTIN\Administrators for an elevated token rather than the- # token user. Both SIDs therefore name "an object this process made".- self.current_owner_sid = self._read_current_owner_sid()- # The ACEs carry no inheritance flags on purpose. Every object this- # module secures -- the endpoint directory, the descriptor and the lock- # -- is created here with this descriptor applied explicitly, so nothing- # depends on inheriting it. Adding OI/CI would not fix anything and- # would push this policy onto files control IPC does not own; the- # directory boundary in paths.get_runtime_control_ipc_endpoint_path is- # what keeps that set closed.- self.sddl = f"O:{self.current_user_sid}D:P(A;;FA;;;{self.current_user_sid})(A;;FA;;;SY)"-- def _configure_functions(self) -> None:- ctypes = self.ctypes- wintypes = self.wintypes-- self.advapi32.OpenProcessToken.argtypes = [- wintypes.HANDLE,- wintypes.DWORD,- ctypes.POINTER(wintypes.HANDLE),- ]- self.advapi32.OpenProcessToken.restype = wintypes.BOOL- self.advapi32.GetTokenInformation.argtypes = [- wintypes.HANDLE,- ctypes.c_int,- wintypes.LPVOID,- wintypes.DWORD,- ctypes.POINTER(wintypes.DWORD),- ]- self.advapi32.GetTokenInformation.restype = wintypes.BOOL- self.advapi32.ConvertSidToStringSidW.argtypes = [- wintypes.LPVOID,- ctypes.POINTER(wintypes.LPWSTR),- ]- self.advapi32.ConvertSidToStringSidW.restype = wintypes.BOOL- self.advapi32.ConvertStringSecurityDescriptorToSecurityDescriptorW.argtypes = [- wintypes.LPCWSTR,- wintypes.DWORD,- ctypes.POINTER(wintypes.LPVOID),- ctypes.POINTER(wintypes.DWORD),- ]- self.advapi32.ConvertStringSecurityDescriptorToSecurityDescriptorW.restype = wintypes.BOOL- self.advapi32.GetSecurityDescriptorOwner.argtypes = [- wintypes.LPVOID,- ctypes.POINTER(wintypes.LPVOID),- ctypes.POINTER(wintypes.BOOL),- ]- self.advapi32.GetSecurityDescriptorOwner.restype = wintypes.BOOL- self.advapi32.GetSecurityDescriptorDacl.argtypes = [- wintypes.LPVOID,- ctypes.POINTER(wintypes.BOOL),- ctypes.POINTER(wintypes.LPVOID),- ctypes.POINTER(wintypes.BOOL),- ]- self.advapi32.GetSecurityDescriptorDacl.restype = wintypes.BOOL- self.advapi32.GetSecurityDescriptorControl.argtypes = [- wintypes.LPVOID,- ctypes.POINTER(wintypes.WORD),- ctypes.POINTER(wintypes.DWORD),- ]- self.advapi32.GetSecurityDescriptorControl.restype = wintypes.BOOL- self.advapi32.GetSecurityInfo.argtypes = [- wintypes.HANDLE,- ctypes.c_int,- wintypes.DWORD,- ctypes.POINTER(wintypes.LPVOID),- ctypes.POINTER(wintypes.LPVOID),- ctypes.POINTER(wintypes.LPVOID),- ctypes.POINTER(wintypes.LPVOID),- ctypes.POINTER(wintypes.LPVOID),- ]- self.advapi32.GetSecurityInfo.restype = wintypes.DWORD- self.advapi32.GetNamedSecurityInfoW.argtypes = [- wintypes.LPWSTR,- ctypes.c_int,- wintypes.DWORD,- ctypes.POINTER(wintypes.LPVOID),- ctypes.POINTER(wintypes.LPVOID),- ctypes.POINTER(wintypes.LPVOID),- ctypes.POINTER(wintypes.LPVOID),- ctypes.POINTER(wintypes.LPVOID),- ]- self.advapi32.GetNamedSecurityInfoW.restype = wintypes.DWORD- self.advapi32.SetNamedSecurityInfoW.argtypes = [- wintypes.LPWSTR,- ctypes.c_int,- wintypes.DWORD,- wintypes.LPVOID,- wintypes.LPVOID,- wintypes.LPVOID,- wintypes.LPVOID,- ]- self.advapi32.SetNamedSecurityInfoW.restype = wintypes.DWORD- self.advapi32.EqualSid.argtypes = [wintypes.LPVOID, wintypes.LPVOID]- self.advapi32.EqualSid.restype = wintypes.BOOL- self.advapi32.GetAclInformation.argtypes = [- wintypes.LPVOID,- wintypes.LPVOID,- wintypes.DWORD,- ctypes.c_int,- ]- self.advapi32.GetAclInformation.restype = wintypes.BOOL- self.advapi32.GetAce.argtypes = [- wintypes.LPVOID,- wintypes.DWORD,- ctypes.POINTER(wintypes.LPVOID),- ]- self.advapi32.GetAce.restype = wintypes.BOOL-- self.kernel32.GetCurrentProcess.argtypes = []- self.kernel32.GetCurrentProcess.restype = wintypes.HANDLE- self.kernel32.CloseHandle.argtypes = [wintypes.HANDLE]- self.kernel32.CloseHandle.restype = wintypes.BOOL- self.kernel32.LocalFree.argtypes = [wintypes.HLOCAL]- self.kernel32.LocalFree.restype = wintypes.HLOCAL- self.kernel32.CreateDirectoryW.argtypes = [- wintypes.LPCWSTR,- ctypes.POINTER(self.SecurityAttributes),- ]- self.kernel32.CreateDirectoryW.restype = wintypes.BOOL- self.kernel32.CreateFileW.argtypes = [- wintypes.LPCWSTR,- wintypes.DWORD,- wintypes.DWORD,- ctypes.POINTER(self.SecurityAttributes),- wintypes.DWORD,- wintypes.DWORD,- wintypes.HANDLE,- ]- self.kernel32.CreateFileW.restype = wintypes.HANDLE-- def _read_current_user_sid(self) -> str:- return self._read_token_sid(self._TOKEN_USER, self.TokenUser, lambda info: info.User.Sid, "user")-- def _read_current_owner_sid(self) -> str:- return self._read_token_sid(self._TOKEN_OWNER, self.TokenOwner, lambda info: info.Owner, "owner")-- def _read_token_sid(self, information_class: int, structure: type, select, label: str) -> str:- ctypes = self.ctypes- wintypes = self.wintypes- token = wintypes.HANDLE()- if not self.advapi32.OpenProcessToken(- self.kernel32.GetCurrentProcess(),- self._TOKEN_QUERY,- ctypes.byref(token),- ):- self._raise_last_error("cannot open the current process token")- try:- required = wintypes.DWORD()- self.advapi32.GetTokenInformation(- token,- information_class,- None,- 0,- ctypes.byref(required),- )- if ctypes.get_last_error() != self._ERROR_INSUFFICIENT_BUFFER:- self._raise_last_error("cannot size the current process token")- buffer = ctypes.create_string_buffer(required.value)- if not self.advapi32.GetTokenInformation(- token,- information_class,- buffer,- required,- ctypes.byref(required),- ):- self._raise_last_error("cannot read the current process token")- information = ctypes.cast(buffer, ctypes.POINTER(structure)).contents- sid_text = wintypes.LPWSTR()- if not self.advapi32.ConvertSidToStringSidW(- select(information),- ctypes.byref(sid_text),- ):- self._raise_last_error(f"cannot encode the current {label} SID")- try:- value = sid_text.value- if not value:- raise ControlIpcSecurityError(f"the current process token has no {label} SID")- return value- finally:- self.kernel32.LocalFree(ctypes.cast(sid_text, wintypes.HLOCAL))- finally:- self.kernel32.CloseHandle(token)-- @contextmanager- def _security_descriptor(self) -> Iterator[object]:- with self._descriptor_from_sddl(self.sddl) as descriptor:- yield descriptor-- def _self_owner_sddls(self) -> tuple[str, ...]:- """Every SDDL owner this process can legitimately have created a path as.-- An unelevated token owns what it creates as the token user. An elevated- token owns it as the token's default owner, normally- ``BUILTIN\\Administrators``. Both are "us"; anything else is not.- """-- if self.current_owner_sid == self.current_user_sid:- return (self.sddl,)- return (self.sddl, f"O:{self.current_owner_sid}")-- def _owner_is_self(self, owner: object) -> bool:- if not owner:- return False- for sddl in self._self_owner_sddls():- with self._descriptor_from_sddl(sddl) as expected:- if bool(self.advapi32.EqualSid(owner, self._descriptor_owner(expected))):- return True- return False-- @contextmanager- def _descriptor_from_sddl(self, sddl: str) -> Iterator[object]:- ctypes = self.ctypes- wintypes = self.wintypes- descriptor = wintypes.LPVOID()- size = wintypes.DWORD()- if not self.advapi32.ConvertStringSecurityDescriptorToSecurityDescriptorW(- sddl,- self._SDDL_REVISION_1,- ctypes.byref(descriptor),- ctypes.byref(size),- ):- self._raise_last_error("cannot construct the private security descriptor")- try:- yield descriptor- finally:- self.kernel32.LocalFree(descriptor)-- @contextmanager- def _security_attributes(self) -> Iterator[object]:- ctypes = self.ctypes- with self._security_descriptor() as descriptor:- attributes = self.SecurityAttributes(- nLength=ctypes.sizeof(self.SecurityAttributes),- lpSecurityDescriptor=descriptor,- bInheritHandle=False,- )- yield attributes-- def ensure_private_directory(self, directory: Path, *, allow_repair: bool) -> None:- directory = _absolute_path(directory)- if directory.exists():- if not directory.is_dir() or directory.is_symlink():- raise ControlIpcSecurityError(f"control IPC runtime directory security is invalid at {directory}")- if allow_repair:- self.secure_existing_owned_path(directory)- else:- self.validate_path(directory)- return-- directory.parent.mkdir(parents=True, exist_ok=True)- with self._security_attributes() as attributes:- if not self.kernel32.CreateDirectoryW(str(directory), self.ctypes.byref(attributes)):- error = self.ctypes.get_last_error()- if error != self._ERROR_ALREADY_EXISTS:- raise ControlIpcSecurityError(- f"cannot create the control IPC runtime directory at {directory}"- ) from self.ctypes.WinError(error)- self.validate_path(directory)-- def open_private_lock_file(self, path: Path, *, allow_repair: bool) -> int:- if path.exists():- if path.is_symlink() or not path.is_file():- raise ControlIpcSecurityError(f"control IPC lock security is invalid at {path}")- if allow_repair:- self.secure_existing_owned_path(path)- fd = self._create_file(- path,- creation_disposition=self._OPEN_ALWAYS,- share_mode=(self._FILE_SHARE_READ | self._FILE_SHARE_WRITE | self._FILE_SHARE_DELETE),- )- try:- self.validate_file_descriptor(fd, path)- except Exception:- os.close(fd)- raise- return fd-- def create_private_temporary_file(self, target: Path) -> tuple[int, Path]:- for _ in range(128):- temporary = target.with_name(f".{target.name}.{secrets.token_hex(16)}.tmp")- try:- fd = self._create_file(- temporary,- creation_disposition=self._CREATE_NEW,- share_mode=0,- )- except FileExistsError:- continue- try:- self.validate_file_descriptor(fd, temporary)- except Exception:- os.close(fd)- temporary.unlink(missing_ok=True)- raise- return fd, temporary- raise FileExistsError(f"cannot allocate a private descriptor beside {target}")-- def _create_file(- self,- path: Path,- *,- creation_disposition: int,- share_mode: int,- ) -> int:- import msvcrt-- with self._security_attributes() as attributes:- handle = self.kernel32.CreateFileW(- str(path),- self._GENERIC_READ | self._GENERIC_WRITE,- share_mode,- self.ctypes.byref(attributes),- creation_disposition,- self._FILE_ATTRIBUTE_NORMAL,- None,- )- invalid_handle = self.ctypes.c_void_p(-1).value- if handle == invalid_handle:- error = self.ctypes.get_last_error()- if error in {self._ERROR_FILE_EXISTS, self._ERROR_ALREADY_EXISTS}:- raise FileExistsError(error, os.strerror(error), str(path))- raise ControlIpcSecurityError(- f"cannot create the private control IPC file at {path}"- ) from self.ctypes.WinError(error)- try:- return msvcrt.open_osfhandle(- handle,- os.O_RDWR | getattr(os, "O_BINARY", 0),- )- except Exception:- self.kernel32.CloseHandle(handle)- raise-- def secure_existing_owned_path(self, path: Path) -> None:- if not self._named_path_owner_matches(path):- raise ControlIpcSecurityError(f"control IPC path is not owned by the current user at {path}")- with self._security_descriptor() as expected:- expected_dacl = self._descriptor_dacl(expected)- result = self.advapi32.SetNamedSecurityInfoW(- str(path),- self._SE_FILE_OBJECT,- self._DACL_SECURITY_INFORMATION | self._PROTECTED_DACL_SECURITY_INFORMATION,- None,- None,- expected_dacl,- None,- )- if result:- raise ControlIpcSecurityError(- f"cannot secure the control IPC path at {path}"- ) from self.ctypes.WinError(result)- self.validate_path(path)-- def validate_path(self, path: Path) -> None:- ctypes = self.ctypes- owner = self.wintypes.LPVOID()- dacl = self.wintypes.LPVOID()- descriptor = self.wintypes.LPVOID()- result = self.advapi32.GetNamedSecurityInfoW(- str(path),- self._SE_FILE_OBJECT,- self._OWNER_SECURITY_INFORMATION | self._DACL_SECURITY_INFORMATION,- ctypes.byref(owner),- None,- ctypes.byref(dacl),- None,- ctypes.byref(descriptor),- )- if result:- raise ControlIpcSecurityError(f"cannot inspect the control IPC path at {path}") from ctypes.WinError(result)- try:- self._validate_security_descriptor(owner, dacl, descriptor, path)- finally:- self.kernel32.LocalFree(descriptor)-- def validate_file_descriptor(self, fd: int, path: Path) -> None:- import msvcrt-- ctypes = self.ctypes- owner = self.wintypes.LPVOID()- dacl = self.wintypes.LPVOID()- descriptor = self.wintypes.LPVOID()- result = self.advapi32.GetSecurityInfo(- msvcrt.get_osfhandle(fd),- self._SE_FILE_OBJECT,- self._OWNER_SECURITY_INFORMATION | self._DACL_SECURITY_INFORMATION,- ctypes.byref(owner),- None,- ctypes.byref(dacl),- None,- ctypes.byref(descriptor),- )- if result:- raise ControlIpcSecurityError(f"cannot inspect the control IPC file at {path}") from ctypes.WinError(result)- try:- self._validate_security_descriptor(owner, dacl, descriptor, path)- finally:- self.kernel32.LocalFree(descriptor)-- def _named_path_owner_matches(self, path: Path) -> bool:- ctypes = self.ctypes- owner = self.wintypes.LPVOID()- descriptor = self.wintypes.LPVOID()- result = self.advapi32.GetNamedSecurityInfoW(- str(path),- self._SE_FILE_OBJECT,- self._OWNER_SECURITY_INFORMATION,- ctypes.byref(owner),- None,- None,- None,- ctypes.byref(descriptor),- )- if result:- raise ControlIpcSecurityError(f"cannot inspect the control IPC path owner at {path}") from ctypes.WinError(- result- )- try:- return self._owner_is_self(owner)- finally:- self.kernel32.LocalFree(descriptor)-- def _validate_security_descriptor(- self,- owner: object,- dacl: object,- descriptor: object,- path: Path,- ) -> None:- control = self._descriptor_control(descriptor)- owner_is_self = self._owner_is_self(owner)- with self._security_descriptor() as expected:- expected_dacl = self._descriptor_dacl(expected)- valid = (- owner_is_self- and bool(dacl)- and bool(control & self._SE_DACL_PRESENT)- and bool(control & self._SE_DACL_PROTECTED)- and self._acl_signature(dacl) == self._acl_signature(expected_dacl)- )- if not valid:- raise ControlIpcSecurityError(f"control IPC path owner or DACL is invalid at {path}")-- def _descriptor_owner(self, descriptor: object) -> object:- owner = self.wintypes.LPVOID()- defaulted = self.wintypes.BOOL()- if not self.advapi32.GetSecurityDescriptorOwner(- descriptor,- self.ctypes.byref(owner),- self.ctypes.byref(defaulted),- ):- self._raise_last_error("cannot read the private descriptor owner")- if not owner:- raise ControlIpcSecurityError("private control IPC descriptor has no owner")- return owner-- def _descriptor_dacl(self, descriptor: object) -> object:- present = self.wintypes.BOOL()- dacl = self.wintypes.LPVOID()- defaulted = self.wintypes.BOOL()- if not self.advapi32.GetSecurityDescriptorDacl(- descriptor,- self.ctypes.byref(present),- self.ctypes.byref(dacl),- self.ctypes.byref(defaulted),- ):- self._raise_last_error("cannot read the private descriptor DACL")- if not present or not dacl:- raise ControlIpcSecurityError("private control IPC descriptor has no DACL")- return dacl-- def _descriptor_control(self, descriptor: object) -> int:- control = self.wintypes.WORD()- revision = self.wintypes.DWORD()- if not self.advapi32.GetSecurityDescriptorControl(- descriptor,- self.ctypes.byref(control),- self.ctypes.byref(revision),- ):- self._raise_last_error("cannot read control IPC DACL controls")- return control.value-- def _acl_signature(self, acl: object) -> tuple[bytes, ...]:- info = self.AclSizeInformation()- if not self.advapi32.GetAclInformation(- acl,- self.ctypes.byref(info),- self.ctypes.sizeof(info),- self._ACL_SIZE_INFORMATION_CLASS,- ):- self._raise_last_error("cannot inspect a control IPC DACL")- ace_count = int(info.AceCount)- acl_bytes_in_use = int(info.AclBytesInUse)- entries: list[bytes] = []- for index in range(ace_count):- ace = self.wintypes.LPVOID()- if not self.advapi32.GetAce(acl, index, self.ctypes.byref(ace)):- self._raise_last_error("cannot inspect a control IPC DACL entry")- header = self.ctypes.string_at(ace, 4)- size = int.from_bytes(header[2:4], "little")- if size < 4 or size > acl_bytes_in_use:- raise ControlIpcSecurityError("control IPC DACL entry size is invalid")- entries.append(self.ctypes.string_at(ace, size))- return tuple(sorted(entries))-- def _raise_last_error(self, message: str) -> None:- error = self.ctypes.get_last_error()- raise ControlIpcSecurityError(message) from self.ctypes.WinError(error)---_WINDOWS_SECURITY: Optional[_WindowsSecurity] = None---def _windows_security() -> _WindowsSecurity:- global _WINDOWS_SECURITY- if _WINDOWS_SECURITY is None:- if os.name != "nt":- raise RuntimeError("Windows control IPC security is unavailable")- _WINDOWS_SECURITY = _WindowsSecurity()- return _WINDOWS_SECURITYdiff --git a/core/handlers/model_hub/migration_journal.py b/core/handlers/model_hub/migration_journal.pyindex 01bfd68d5..d49222e82 100644--- a/core/handlers/model_hub/migration_journal.py+++ b/core/handlers/model_hub/migration_journal.py@@ -18,6 +18,7 @@ from typing import Anyfrom config.atomic_io import write_atomicfrom config.v2_config import ModelHubConfig+from core import private_fsclass TakeoverStateError(RuntimeError):@@ -34,6 +35,18 @@ def _read_regular(path: Path) -> bytes | None:return path.read_bytes()+def _same_mode(actual: int, recorded: int) -> bool:+ """Whether a file's permission bits are the ones a takeover recorded for it.++ Windows keeps only the read-only attribute, which ``stat`` reports as the+ write bits of 0o666 or 0o444 and ``chmod`` sets from the owner write bit, so+ a recorded mode holds there exactly as far as that bit goes.+ """+ if os.name == "nt":+ return bool(actual & stat.S_IWRITE) == bool(recorded & stat.S_IWRITE)+ return actual == recorded++def _encoded(content: bytes | None) -> str | None:return base64.b64encode(content).decode("ascii") if content is not None else None@@ -160,7 +173,7 @@ class NativeFileEdit:if (content != expectedor (current.st_dev, current.st_ino) != (entry.st_dev, entry.st_ino)- or (mode is not None and stat.S_IMODE(current.st_mode) != mode)+ or (mode is not None and not _same_mode(stat.S_IMODE(current.st_mode), mode))):raise TakeoverStateError("native configuration changed")if sync:@@ -217,17 +230,17 @@ class NativeTakeoverJournal:return frozenset(backends)def _prepare(self) -> None:- self.path.parent.mkdir(parents=True, mode=0o700, exist_ok=True)- mode = self.path.parent.lstat().st_mode- if not stat.S_ISDIR(mode) or stat.S_IMODE(mode) != 0o700:- raise TakeoverStateError("unsafe takeover directory")+ try:+ private_fs.ensure_private_directory(self.path.parent)+ except private_fs.PrivatePathError as exc:+ raise TakeoverStateError("unsafe takeover directory") from excdef load(self) -> dict[str, Any] | None:try:mode = self.path.lstat().st_modeexcept FileNotFoundError:return None- if not stat.S_ISREG(mode) or stat.S_IMODE(mode) != 0o600:+ if not stat.S_ISREG(mode) or not private_fs.is_private(self.path, mode):raise TakeoverStateError("unsafe takeover journal")try:payload = json.loads(self.path.read_bytes())diff --git a/core/private_fs.py b/core/private_fs.pynew file mode 100644index 000000000..8f9b34f85--- /dev/null+++ b/core/private_fs.py@@ -0,0 +1,748 @@+"""Owner-private filesystem paths.++A path is owner-private when only the current user can use it. On POSIX that is+mode 0o700 for a directory and 0o600 for a regular file. Windows keeps no such+modes -- ``chmod`` there only toggles the read-only attribute, and ``stat``+reports 0o777 or 0o666 whatever the access rules are -- so there it means the+path is owned by this process and its DACL grants this user and SYSTEM alone.+"""++from __future__ import annotations++import contextlib+import os+import secrets+import stat+from contextlib import contextmanager+from pathlib import Path+from typing import Iterator, Optional++PRIVATE_DIRECTORY_MODE = 0o700+PRIVATE_FILE_MODE = 0o600+++class PrivatePathError(PermissionError):+ """Raised when a path that must be owner-private is not, or cannot be made so."""+++def ensure_private_directory(path: Path, *, repair: bool = True) -> None:+ """Create ``path`` and any missing parents, and leave ``path`` owner-private.++ Parents keep their defaults. An existing ``path`` is repaired when+ ``repair`` is set and must already be private otherwise. On Windows its+ protected DACL is inheritable, so whatever any process later creates inside+ starts private too.+ """++ if os.name == "nt":+ windows_security().ensure_private_directory(path, allow_repair=repair, inheritable=True)+ return+ path.mkdir(parents=True, mode=PRIVATE_DIRECTORY_MODE, exist_ok=True)+ mode = path.lstat().st_mode+ if not stat.S_ISDIR(mode):+ raise PrivatePathError(f"private directory security is invalid at {path}")+ if repair:+ os.chmod(path, PRIVATE_DIRECTORY_MODE)+ elif not is_private(path, mode):+ raise PrivatePathError(f"private directory permissions are unsafe at {path}")+++def is_private(path: Path, mode: int, *, fd: Optional[int] = None) -> bool:+ """Whether the directory or regular file ``path`` is owner-private.++ ``mode`` is the caller's ``lstat`` of ``path``, or its ``fstat`` of ``fd``;+ with ``fd`` Windows checks the open file rather than what ``path`` names now.+ """++ directory = stat.S_ISDIR(mode)+ if os.name == "nt":+ return windows_security().grants_only_self(path, directory=directory, fd=fd)+ return stat.S_IMODE(mode) == (PRIVATE_DIRECTORY_MODE if directory else PRIVATE_FILE_MODE)+++def make_private_file(path: Path) -> None:+ """Restrict an existing regular file this user owns to owner-only access."""++ if os.name == "nt":+ windows_security().secure_existing_owned_path(path)+ return+ os.chmod(path, PRIVATE_FILE_MODE)+++def _absolute_path(path: Path) -> Path:+ return Path(os.path.abspath(os.fspath(path.expanduser())))+++class WindowsSecurity:+ """Win32 owner and DACL operations behind owner-private paths."""++ _SDDL_REVISION_1 = 1+ _SE_FILE_OBJECT = 1+ _OWNER_SECURITY_INFORMATION = 0x00000001+ _DACL_SECURITY_INFORMATION = 0x00000004+ _PROTECTED_DACL_SECURITY_INFORMATION = 0x80000000+ _SE_DACL_PRESENT = 0x0004+ _SE_DACL_PROTECTED = 0x1000+ _TOKEN_QUERY = 0x0008+ _TOKEN_USER = 1+ _TOKEN_OWNER = 4+ _ERROR_INSUFFICIENT_BUFFER = 122+ _ERROR_FILE_EXISTS = 80+ _ERROR_ALREADY_EXISTS = 183+ _GENERIC_READ = 0x80000000+ _GENERIC_WRITE = 0x40000000+ _FILE_SHARE_READ = 0x00000001+ _FILE_SHARE_WRITE = 0x00000002+ _FILE_SHARE_DELETE = 0x00000004+ _CREATE_NEW = 1+ _OPEN_ALWAYS = 4+ _FILE_ATTRIBUTE_NORMAL = 0x00000080+ _ACL_SIZE_INFORMATION_CLASS = 2+ _ACCESS_ALLOWED_ACE_TYPE = 0+ _ACCESS_DENIED_ACE_TYPE = 1+ _OBJECT_INHERIT_ACE = 0x01+ _CONTAINER_INHERIT_ACE = 0x02++ def __init__(self) -> None:+ import ctypes+ from ctypes import wintypes++ self.ctypes = ctypes+ self.wintypes = wintypes+ self.advapi32 = ctypes.WinDLL("advapi32", use_last_error=True)+ self.kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)++ class SecurityAttributes(ctypes.Structure):+ _fields_ = [+ ("nLength", wintypes.DWORD),+ ("lpSecurityDescriptor", wintypes.LPVOID),+ ("bInheritHandle", wintypes.BOOL),+ ]++ class SidAndAttributes(ctypes.Structure):+ _fields_ = [+ ("Sid", wintypes.LPVOID),+ ("Attributes", wintypes.DWORD),+ ]++ class TokenUser(ctypes.Structure):+ _fields_ = [("User", SidAndAttributes)]++ class TokenOwner(ctypes.Structure):+ _fields_ = [("Owner", wintypes.LPVOID)]++ class AclSizeInformation(ctypes.Structure):+ _fields_ = [+ ("AceCount", wintypes.DWORD),+ ("AclBytesInUse", wintypes.DWORD),+ ("AclBytesFree", wintypes.DWORD),+ ]++ self.SecurityAttributes = SecurityAttributes+ self.TokenUser = TokenUser+ self.TokenOwner = TokenOwner+ self.AclSizeInformation = AclSizeInformation+ self._configure_functions()+ self.current_user_sid = self._read_current_user_sid()+ # Windows stamps newly created objects with the token's *default owner*,+ # which is BUILTIN\Administrators for an elevated token rather than the+ # token user. Both SIDs therefore name "an object this process made".+ self.current_owner_sid = self._read_current_owner_sid()+ # ``sddl`` carries no inheritance flags on purpose. Control IPC creates+ # every object it secures -- the endpoint directory, the descriptor and+ # the lock -- with this descriptor applied explicitly, so nothing+ # depends on inheriting it. Adding OI/CI would not fix anything and+ # would push this policy onto files control IPC does not own; the+ # directory boundary in paths.get_runtime_control_ipc_endpoint_path is+ # what keeps that set closed.+ self.sddl = f"O:{self.current_user_sid}D:P(A;;FA;;;{self.current_user_sid})(A;;FA;;;SY)"+ # A directory that owns its whole subtree carries the same entries as+ # inheritable ones instead, so whatever any process later creates+ # inside it -- including files this process never writes itself --+ # starts private. Windows ignores the umask, so nothing else would.+ self.inheritable_sddl = (+ f"O:{self.current_user_sid}D:P(A;OICI;FA;;;{self.current_user_sid})(A;OICI;FA;;;SY)"+ )++ def _configure_functions(self) -> None:+ ctypes = self.ctypes+ wintypes = self.wintypes++ self.advapi32.OpenProcessToken.argtypes = [+ wintypes.HANDLE,+ wintypes.DWORD,+ ctypes.POINTER(wintypes.HANDLE),+ ]+ self.advapi32.OpenProcessToken.restype = wintypes.BOOL+ self.advapi32.GetTokenInformation.argtypes = [+ wintypes.HANDLE,+ ctypes.c_int,+ wintypes.LPVOID,+ wintypes.DWORD,+ ctypes.POINTER(wintypes.DWORD),+ ]+ self.advapi32.GetTokenInformation.restype = wintypes.BOOL+ self.advapi32.ConvertSidToStringSidW.argtypes = [+ wintypes.LPVOID,+ ctypes.POINTER(wintypes.LPWSTR),+ ]+ self.advapi32.ConvertSidToStringSidW.restype = wintypes.BOOL+ self.advapi32.ConvertStringSecurityDescriptorToSecurityDescriptorW.argtypes = [+ wintypes.LPCWSTR,+ wintypes.DWORD,+ ctypes.POINTER(wintypes.LPVOID),+ ctypes.POINTER(wintypes.DWORD),+ ]+ self.advapi32.ConvertStringSecurityDescriptorToSecurityDescriptorW.restype = wintypes.BOOL+ self.advapi32.GetSecurityDescriptorOwner.argtypes = [+ wintypes.LPVOID,+ ctypes.POINTER(wintypes.LPVOID),+ ctypes.POINTER(wintypes.BOOL),+ ]+ self.advapi32.GetSecurityDescriptorOwner.restype = wintypes.BOOL+ self.advapi32.GetSecurityDescriptorDacl.argtypes = [+ wintypes.LPVOID,+ ctypes.POINTER(wintypes.BOOL),+ ctypes.POINTER(wintypes.LPVOID),+ ctypes.POINTER(wintypes.BOOL),+ ]+ self.advapi32.GetSecurityDescriptorDacl.restype = wintypes.BOOL+ self.advapi32.GetSecurityDescriptorControl.argtypes = [+ wintypes.LPVOID,+ ctypes.POINTER(wintypes.WORD),+ ctypes.POINTER(wintypes.DWORD),+ ]+ self.advapi32.GetSecurityDescriptorControl.restype = wintypes.BOOL+ self.advapi32.GetSecurityInfo.argtypes = [+ wintypes.HANDLE,+ ctypes.c_int,+ wintypes.DWORD,+ ctypes.POINTER(wintypes.LPVOID),+ ctypes.POINTER(wintypes.LPVOID),+ ctypes.POINTER(wintypes.LPVOID),+ ctypes.POINTER(wintypes.LPVOID),+ ctypes.POINTER(wintypes.LPVOID),+ ]+ self.advapi32.GetSecurityInfo.restype = wintypes.DWORD+ self.advapi32.GetNamedSecurityInfoW.argtypes = [+ wintypes.LPWSTR,+ ctypes.c_int,+ wintypes.DWORD,+ ctypes.POINTER(wintypes.LPVOID),+ ctypes.POINTER(wintypes.LPVOID),+ ctypes.POINTER(wintypes.LPVOID),+ ctypes.POINTER(wintypes.LPVOID),+ ctypes.POINTER(wintypes.LPVOID),+ ]+ self.advapi32.GetNamedSecurityInfoW.restype = wintypes.DWORD+ self.advapi32.SetNamedSecurityInfoW.argtypes = [+ wintypes.LPWSTR,+ ctypes.c_int,+ wintypes.DWORD,+ wintypes.LPVOID,+ wintypes.LPVOID,+ wintypes.LPVOID,+ wintypes.LPVOID,+ ]+ self.advapi32.SetNamedSecurityInfoW.restype = wintypes.DWORD+ self.advapi32.EqualSid.argtypes = [wintypes.LPVOID, wintypes.LPVOID]+ self.advapi32.EqualSid.restype = wintypes.BOOL+ self.advapi32.GetAclInformation.argtypes = [+ wintypes.LPVOID,+ wintypes.LPVOID,+ wintypes.DWORD,+ ctypes.c_int,+ ]+ self.advapi32.GetAclInformation.restype = wintypes.BOOL+ self.advapi32.GetAce.argtypes = [+ wintypes.LPVOID,+ wintypes.DWORD,+ ctypes.POINTER(wintypes.LPVOID),+ ]+ self.advapi32.GetAce.restype = wintypes.BOOL++ self.kernel32.GetCurrentProcess.argtypes = []+ self.kernel32.GetCurrentProcess.restype = wintypes.HANDLE+ self.kernel32.CloseHandle.argtypes = [wintypes.HANDLE]+ self.kernel32.CloseHandle.restype = wintypes.BOOL+ self.kernel32.LocalFree.argtypes = [wintypes.HLOCAL]+ self.kernel32.LocalFree.restype = wintypes.HLOCAL+ self.kernel32.CreateDirectoryW.argtypes = [+ wintypes.LPCWSTR,+ ctypes.POINTER(self.SecurityAttributes),+ ]+ self.kernel32.CreateDirectoryW.restype = wintypes.BOOL+ self.kernel32.CreateFileW.argtypes = [+ wintypes.LPCWSTR,+ wintypes.DWORD,+ wintypes.DWORD,+ ctypes.POINTER(self.SecurityAttributes),+ wintypes.DWORD,+ wintypes.DWORD,+ wintypes.HANDLE,+ ]+ self.kernel32.CreateFileW.restype = wintypes.HANDLE++ def _read_current_user_sid(self) -> str:+ return self._read_token_sid(self._TOKEN_USER, self.TokenUser, lambda info: info.User.Sid, "user")++ def _read_current_owner_sid(self) -> str:+ return self._read_token_sid(self._TOKEN_OWNER, self.TokenOwner, lambda info: info.Owner, "owner")++ def _read_token_sid(self, information_class: int, structure: type, select, label: str) -> str:+ ctypes = self.ctypes+ wintypes = self.wintypes+ token = wintypes.HANDLE()+ if not self.advapi32.OpenProcessToken(+ self.kernel32.GetCurrentProcess(),+ self._TOKEN_QUERY,+ ctypes.byref(token),+ ):+ self._raise_last_error("cannot open the current process token")+ try:+ required = wintypes.DWORD()+ self.advapi32.GetTokenInformation(+ token,+ information_class,+ None,+ 0,+ ctypes.byref(required),+ )+ if ctypes.get_last_error() != self._ERROR_INSUFFICIENT_BUFFER:+ self._raise_last_error("cannot size the current process token")+ buffer = ctypes.create_string_buffer(required.value)+ if not self.advapi32.GetTokenInformation(+ token,+ information_class,+ buffer,+ required,+ ctypes.byref(required),+ ):+ self._raise_last_error("cannot read the current process token")+ information = ctypes.cast(buffer, ctypes.POINTER(structure)).contents+ sid_text = wintypes.LPWSTR()+ if not self.advapi32.ConvertSidToStringSidW(+ select(information),+ ctypes.byref(sid_text),+ ):+ self._raise_last_error(f"cannot encode the current {label} SID")+ try:+ value = sid_text.value+ if not value:+ raise PrivatePathError(f"the current process token has no {label} SID")+ return value+ finally:+ self.kernel32.LocalFree(ctypes.cast(sid_text, wintypes.HLOCAL))+ finally:+ self.kernel32.CloseHandle(token)++ @contextmanager+ def _security_descriptor(self, sddl: Optional[str] = None) -> Iterator[object]:+ with self._descriptor_from_sddl(sddl or self.sddl) as descriptor:+ yield descriptor++ def _self_owner_sddls(self) -> tuple[str, ...]:+ """Every SDDL owner this process can legitimately have created a path as.++ An unelevated token owns what it creates as the token user. An elevated+ token owns it as the token's default owner, normally+ ``BUILTIN\\Administrators``. Both are "us"; anything else is not.+ """++ if self.current_owner_sid == self.current_user_sid:+ return (self.sddl,)+ return (self.sddl, f"O:{self.current_owner_sid}")++ def _owner_is_self(self, owner: object) -> bool:+ if not owner:+ return False+ for sddl in self._self_owner_sddls():+ with self._descriptor_from_sddl(sddl) as expected:+ if bool(self.advapi32.EqualSid(owner, self._descriptor_owner(expected))):+ return True+ return False++ @contextmanager+ def _descriptor_from_sddl(self, sddl: str) -> Iterator[object]:+ ctypes = self.ctypes+ wintypes = self.wintypes+ descriptor = wintypes.LPVOID()+ size = wintypes.DWORD()+ if not self.advapi32.ConvertStringSecurityDescriptorToSecurityDescriptorW(+ sddl,+ self._SDDL_REVISION_1,+ ctypes.byref(descriptor),+ ctypes.byref(size),+ ):+ self._raise_last_error("cannot construct the private security descriptor")+ try:+ yield descriptor+ finally:+ self.kernel32.LocalFree(descriptor)++ @contextmanager+ def _security_attributes(self, sddl: Optional[str] = None) -> Iterator[object]:+ ctypes = self.ctypes+ with self._security_descriptor(sddl) as descriptor:+ attributes = self.SecurityAttributes(+ nLength=ctypes.sizeof(self.SecurityAttributes),+ lpSecurityDescriptor=descriptor,+ bInheritHandle=False,+ )+ yield attributes++ def ensure_private_directory(self, directory: Path, *, allow_repair: bool, inheritable: bool = False) -> None:+ sddl = self.inheritable_sddl if inheritable else self.sddl+ directory = _absolute_path(directory)+ if directory.exists():+ if not directory.is_dir() or directory.is_symlink():+ raise PrivatePathError(f"private directory security is invalid at {directory}")+ # Setting an inheritable DACL rewrites the inherited entries of the+ # whole subtree, so a directory that is already private is left as is.+ if allow_repair and not (inheritable and self._path_matches(directory, sddl)):+ self.secure_existing_owned_path(directory, sddl=sddl)+ else:+ self.validate_path(directory, sddl=sddl)+ return++ directory.parent.mkdir(parents=True, exist_ok=True)+ with self._security_attributes(sddl) as attributes:+ if not self.kernel32.CreateDirectoryW(str(directory), self.ctypes.byref(attributes)):+ error = self.ctypes.get_last_error()+ if error != self._ERROR_ALREADY_EXISTS:+ raise PrivatePathError(+ f"cannot create the private directory at {directory}"+ ) from self.ctypes.WinError(error)+ self.validate_path(directory, sddl=sddl)++ def open_private_lock_file(self, path: Path, *, allow_repair: bool) -> int:+ if path.exists():+ if path.is_symlink() or not path.is_file():+ raise PrivatePathError(f"private lock file security is invalid at {path}")+ if allow_repair:+ self.secure_existing_owned_path(path)+ fd = self._create_file(+ path,+ creation_disposition=self._OPEN_ALWAYS,+ share_mode=(self._FILE_SHARE_READ | self._FILE_SHARE_WRITE | self._FILE_SHARE_DELETE),+ )+ try:+ self.validate_file_descriptor(fd, path)+ except Exception:+ os.close(fd)+ raise+ return fd++ def create_private_temporary_file(self, target: Path) -> tuple[int, Path]:+ for _ in range(128):+ temporary = target.with_name(f".{target.name}.{secrets.token_hex(16)}.tmp")+ try:+ fd = self._create_file(+ temporary,+ creation_disposition=self._CREATE_NEW,+ share_mode=0,+ )+ except FileExistsError:+ continue+ try:+ self.validate_file_descriptor(fd, temporary)+ except Exception:+ os.close(fd)+ temporary.unlink(missing_ok=True)+ raise+ return fd, temporary+ raise FileExistsError(f"cannot allocate a private descriptor beside {target}")++ def _create_file(+ self,+ path: Path,+ *,+ creation_disposition: int,+ share_mode: int,+ ) -> int:+ import msvcrt++ with self._security_attributes() as attributes:+ handle = self.kernel32.CreateFileW(+ str(path),+ self._GENERIC_READ | self._GENERIC_WRITE,+ share_mode,+ self.ctypes.byref(attributes),+ creation_disposition,+ self._FILE_ATTRIBUTE_NORMAL,+ None,+ )+ invalid_handle = self.ctypes.c_void_p(-1).value+ if handle == invalid_handle:+ error = self.ctypes.get_last_error()+ if error in {self._ERROR_FILE_EXISTS, self._ERROR_ALREADY_EXISTS}:+ raise FileExistsError(error, os.strerror(error), str(path))+ raise PrivatePathError(+ f"cannot create the private file at {path}"+ ) from self.ctypes.WinError(error)+ try:+ return msvcrt.open_osfhandle(+ handle,+ os.O_RDWR | getattr(os, "O_BINARY", 0),+ )+ except Exception:+ self.kernel32.CloseHandle(handle)+ raise++ def secure_existing_owned_path(self, path: Path, *, sddl: Optional[str] = None) -> None:+ if not self._named_path_owner_matches(path):+ raise PrivatePathError(f"private path is not owned by the current user at {path}")+ with self._security_descriptor(sddl) as expected:+ expected_dacl = self._descriptor_dacl(expected)+ result = self.advapi32.SetNamedSecurityInfoW(+ str(path),+ self._SE_FILE_OBJECT,+ self._DACL_SECURITY_INFORMATION | self._PROTECTED_DACL_SECURITY_INFORMATION,+ None,+ None,+ expected_dacl,+ None,+ )+ if result:+ raise PrivatePathError(+ f"cannot secure the private path at {path}"+ ) from self.ctypes.WinError(result)+ self.validate_path(path, sddl=sddl)++ def validate_path(self, path: Path, *, sddl: Optional[str] = None) -> None:+ with self._named_path_security(path) as (owner, dacl, descriptor):+ self._validate_security_descriptor(owner, dacl, descriptor, path, sddl=sddl)++ def validate_file_descriptor(self, fd: int, path: Path) -> None:+ with self._file_descriptor_security(fd, path) as (owner, dacl, descriptor):+ self._validate_security_descriptor(owner, dacl, descriptor, path)++ def grants_only_self(self, path: Path, *, directory: bool, fd: Optional[int] = None) -> bool:+ """Whether nobody but this user and the machine can open ``path``, inherited entries included.++ What a private directory holds inherits its entries rather than carrying+ a copy, so this checks what the DACL grants, not that it equals one+ descriptor. Besides this user and SYSTEM it accepts the two other+ entries CPython's own ``mkdir(mode=0o700)`` writes on Windows:+ Administrators, who can take ownership of any file anyway, and OWNER+ RIGHTS, which names the owner this already requires to be us. Model Hub+ directories released builds made that way stay private. A directory's+ entries must also be inheritable, or what is created inside it later+ would not start private. With ``fd`` the open file is checked, not+ whatever ``path`` names now.+ """++ reader = (+ self._named_path_security(path) if fd is None else self._file_descriptor_security(fd, path)+ )+ with reader as (owner, dacl, _descriptor):+ if not self._owner_is_self(owner) or not dacl:+ return False+ with contextlib.ExitStack() as stack:+ trusted = [+ self._descriptor_owner(stack.enter_context(self._descriptor_from_sddl(f"O:{sid}")))+ for sid in (self.current_user_sid, "SY", "BA", "S-1-3-4")+ ]+ for ace_type, flags, sid in self._aces(dacl):+ if ace_type == self._ACCESS_DENIED_ACE_TYPE:+ continue+ if ace_type != self._ACCESS_ALLOWED_ACE_TYPE:+ return False+ if not any(self.advapi32.EqualSid(sid, candidate) for candidate in trusted):+ return False+ inherit = self._OBJECT_INHERIT_ACE | self._CONTAINER_INHERIT_ACE+ if directory and flags & inherit != inherit:+ return False+ return True++ def _path_matches(self, path: Path, sddl: str) -> bool:+ try:+ self.validate_path(path, sddl=sddl)+ except PrivatePathError:+ return False+ return True++ @contextmanager+ def _named_path_security(self, path: Path) -> Iterator[tuple[object, object, object]]:+ ctypes = self.ctypes+ owner = self.wintypes.LPVOID()+ dacl = self.wintypes.LPVOID()+ descriptor = self.wintypes.LPVOID()+ result = self.advapi32.GetNamedSecurityInfoW(+ str(path),+ self._SE_FILE_OBJECT,+ self._OWNER_SECURITY_INFORMATION | self._DACL_SECURITY_INFORMATION,+ ctypes.byref(owner),+ None,+ ctypes.byref(dacl),+ None,+ ctypes.byref(descriptor),+ )+ if result:+ raise PrivatePathError(f"cannot inspect the private path at {path}") from ctypes.WinError(result)+ try:+ yield owner, dacl, descriptor+ finally:+ self.kernel32.LocalFree(descriptor)++ @contextmanager+ def _file_descriptor_security(self, fd: int, path: Path) -> Iterator[tuple[object, object, object]]:+ import msvcrt++ ctypes = self.ctypes+ owner = self.wintypes.LPVOID()+ dacl = self.wintypes.LPVOID()+ descriptor = self.wintypes.LPVOID()+ result = self.advapi32.GetSecurityInfo(+ msvcrt.get_osfhandle(fd),+ self._SE_FILE_OBJECT,+ self._OWNER_SECURITY_INFORMATION | self._DACL_SECURITY_INFORMATION,+ ctypes.byref(owner),+ None,+ ctypes.byref(dacl),+ None,+ ctypes.byref(descriptor),+ )+ if result:+ raise PrivatePathError(f"cannot inspect the private file at {path}") from ctypes.WinError(result)+ try:+ yield owner, dacl, descriptor+ finally:+ self.kernel32.LocalFree(descriptor)++ def _named_path_owner_matches(self, path: Path) -> bool:+ ctypes = self.ctypes+ owner = self.wintypes.LPVOID()+ descriptor = self.wintypes.LPVOID()+ result = self.advapi32.GetNamedSecurityInfoW(+ str(path),+ self._SE_FILE_OBJECT,+ self._OWNER_SECURITY_INFORMATION,+ ctypes.byref(owner),+ None,+ None,+ None,+ ctypes.byref(descriptor),+ )+ if result:+ raise PrivatePathError(f"cannot inspect the private path owner at {path}") from ctypes.WinError(result)+ try:+ return self._owner_is_self(owner)+ finally:+ self.kernel32.LocalFree(descriptor)++ def _validate_security_descriptor(+ self,+ owner: object,+ dacl: object,+ descriptor: object,+ path: Path,+ *,+ sddl: Optional[str] = None,+ ) -> None:+ control = self._descriptor_control(descriptor)+ owner_is_self = self._owner_is_self(owner)+ with self._security_descriptor(sddl) as expected:+ expected_dacl = self._descriptor_dacl(expected)+ valid = (+ owner_is_self+ and bool(dacl)+ and bool(control & self._SE_DACL_PRESENT)+ and bool(control & self._SE_DACL_PROTECTED)+ and self._acl_signature(dacl) == self._acl_signature(expected_dacl)+ )+ if not valid:+ raise PrivatePathError(f"private path owner or DACL is invalid at {path}")++ def _descriptor_owner(self, descriptor: object) -> object:+ owner = self.wintypes.LPVOID()+ defaulted = self.wintypes.BOOL()+ if not self.advapi32.GetSecurityDescriptorOwner(+ descriptor,+ self.ctypes.byref(owner),+ self.ctypes.byref(defaulted),+ ):+ self._raise_last_error("cannot read the private descriptor owner")+ if not owner:+ raise PrivatePathError("private security descriptor has no owner")+ return owner++ def _descriptor_dacl(self, descriptor: object) -> object:+ present = self.wintypes.BOOL()+ dacl = self.wintypes.LPVOID()+ defaulted = self.wintypes.BOOL()+ if not self.advapi32.GetSecurityDescriptorDacl(+ descriptor,+ self.ctypes.byref(present),+ self.ctypes.byref(dacl),+ self.ctypes.byref(defaulted),+ ):+ self._raise_last_error("cannot read the private descriptor DACL")+ if not present or not dacl:+ raise PrivatePathError("private security descriptor has no DACL")+ return dacl++ def _descriptor_control(self, descriptor: object) -> int:+ control = self.wintypes.WORD()+ revision = self.wintypes.DWORD()+ if not self.advapi32.GetSecurityDescriptorControl(+ descriptor,+ self.ctypes.byref(control),+ self.ctypes.byref(revision),+ ):+ self._raise_last_error("cannot read private DACL controls")+ return control.value++ def _acl_signature(self, acl: object) -> tuple[bytes, ...]:+ return tuple(sorted(self.ctypes.string_at(ace, size) for ace, size in self._ace_entries(acl)))++ def _aces(self, acl: object) -> Iterator[tuple[int, int, int]]:+ """Yield each entry's type, flags and SID address.++ Only the allowed and denied entry types are decoded; their SID follows+ the 4-byte header and the 4-byte access mask.+ """++ for ace, size in self._ace_entries(acl):+ header = self.ctypes.string_at(ace, 4)+ ace_type, flags = header[0], header[1]+ if ace_type in {self._ACCESS_ALLOWED_ACE_TYPE, self._ACCESS_DENIED_ACE_TYPE} and size < 16:+ raise PrivatePathError("private DACL entry size is invalid")+ yield ace_type, flags, ace.value + 8++ def _ace_entries(self, acl: object) -> Iterator[tuple[object, int]]:+ info = self.AclSizeInformation()+ if not self.advapi32.GetAclInformation(+ acl,+ self.ctypes.byref(info),+ self.ctypes.sizeof(info),+ self._ACL_SIZE_INFORMATION_CLASS,+ ):+ self._raise_last_error("cannot inspect a private DACL")+ ace_count = int(info.AceCount)+ acl_bytes_in_use = int(info.AclBytesInUse)+ for index in range(ace_count):+ ace = self.wintypes.LPVOID()+ if not self.advapi32.GetAce(acl, index, self.ctypes.byref(ace)):+ self._raise_last_error("cannot inspect a private DACL entry")+ header = self.ctypes.string_at(ace, 4)+ size = int.from_bytes(header[2:4], "little")+ if size < 4 or size > acl_bytes_in_use:+ raise PrivatePathError("private DACL entry size is invalid")+ yield ace, size++ def _raise_last_error(self, message: str) -> None:+ error = self.ctypes.get_last_error()+ raise PrivatePathError(message) from self.ctypes.WinError(error)+++_WINDOWS_SECURITY: Optional[WindowsSecurity] = None+++def windows_security() -> WindowsSecurity:+ global _WINDOWS_SECURITY+ if _WINDOWS_SECURITY is None:+ if os.name != "nt":+ raise RuntimeError("Windows private path security is unavailable")+ _WINDOWS_SECURITY = WindowsSecurity()+ return _WINDOWS_SECURITYdiff --git a/tests/test_control_ipc.py b/tests/test_control_ipc.pyindex 6079f81de..09d33cecf 100644--- a/tests/test_control_ipc.py+++ b/tests/test_control_ipc.py@@ -17,7 +17,7 @@ import httpximport pytestfrom config import paths-from core import control_ipc, internal_server, session_turns+from core import control_ipc, internal_server, private_fs, session_turnsfrom vibe import internal_client, model_hub_client@@ -146,7 +146,7 @@ def test_new_windows_hosts_rotate_instance_and_bearer_credentials(tmp_path):def test_windows_control_ipc_artifacts_have_exact_private_security(monkeypatch, tmp_path):target = tmp_path / "runtime" / "control-ipc.json"descriptor = _descriptor()- security = control_ipc._windows_security()+ security = private_fs.windows_security()original_replace = control_ipc.os.replacetemporary_checked = False@@ -277,7 +277,7 @@ def test_windows_descriptor_reader_rejects_widened_dacl_and_writer_repairs_it(tmcontrol_ipc.write_descriptor_atomic(target, successor)assert control_ipc.load_descriptor(target) == successor- control_ipc._windows_security().validate_path(target)+ private_fs.windows_security().validate_path(target)@pytest.mark.skipif(os.name != "nt", reason="requires native Windows ACLs")@@ -299,7 +299,7 @@ def test_windows_descriptor_reader_rejects_widened_lock_and_writer_repairs_it(tmcontrol_ipc.write_descriptor_atomic(target, descriptor)assert control_ipc.load_descriptor(target) == descriptor- control_ipc._windows_security().validate_path(lock_path)+ private_fs.windows_security().validate_path(lock_path)@pytest.mark.skipif(os.name != "nt", reason="requires native Windows ACLs")@@ -320,7 +320,7 @@ def test_windows_descriptor_reader_rejects_widened_runtime_directory(tmp_path):control_ipc.write_descriptor_atomic(target, descriptor)assert control_ipc.load_descriptor(target) == descriptor- control_ipc._windows_security().validate_path(target.parent)+ private_fs.windows_security().validate_path(target.parent)@pytest.mark.skipif(os.name != "nt", reason="requires native Windows ACLs")@@ -331,7 +331,7 @@ def test_windows_descriptor_rejects_non_owner_and_security_api_failure(target = tmp_path / "runtime" / "control-ipc.json"descriptor = _descriptor()control_ipc.write_descriptor_atomic(target, descriptor)- security = control_ipc._windows_security()+ security = private_fs.windows_security()fd = os.open(target, os.O_RDONLY)try:@@ -679,107 +679,3 @@ def test_sse_reconnect_loads_successor_descriptor(monkeypatch, tmp_path):await _stop_server(first_task)asyncio.run(_run())---class _FakeSid:- """A SID pointer stand-in that compares by the SDDL text that produced it."""-- def __init__(self, value: str | None = None) -> None:- self.value = value-- def __bool__(self) -> bool:- return self.value is not None---class _FakeCtypes:- @staticmethod- def byref(cell):- return cell---class _FakeWinTypes:- LPVOID = _FakeSid- BOOL = _FakeSid- DWORD = _FakeSid---def _windows_security_stub(*, user_sid: str, owner_sid: str):- """Drive the real owner-acceptance logic without Windows ctypes bindings.-- ``_WindowsSecurity.__init__`` loads ``advapi32``/``kernel32``, so it cannot- run off Windows. Every method under test reads only instance attributes, so- a bypassed constructor with fake bindings exercises the real decision.- """-- security = object.__new__(control_ipc._WindowsSecurity)- security.ctypes = _FakeCtypes- security.wintypes = _FakeWinTypes- security.current_user_sid = user_sid- security.current_owner_sid = owner_sid- security.sddl = f"O:{user_sid}D:P(A;;FA;;;{user_sid})(A;;FA;;;SY)"-- def _convert(sddl, _revision, descriptor_ref, _size_ref):- descriptor_ref.value = sddl- return 1-- def _descriptor_owner(descriptor, owner_ref, _defaulted_ref):- owner_ref.value = descriptor.value.split("D:", 1)[0][len("O:") :]- return 1-- security.advapi32 = MagicMock(- ConvertStringSecurityDescriptorToSecurityDescriptorW=_convert,- GetSecurityDescriptorOwner=_descriptor_owner,- EqualSid=lambda left, right: left.value == right.value,- )- security.kernel32 = MagicMock(LocalFree=lambda *_args: None)- return security---_USER_SID = "S-1-5-21-1111-2222-3333-1001"-_ADMINISTRATORS_SID = "S-1-5-32-544"-_STRANGER_SID = "S-1-5-21-9999-8888-7777-500"---def test_elevated_token_default_owner_is_accepted_as_self():- """An elevated token stamps BUILTIN\\Administrators on what it creates."""-- security = _windows_security_stub(user_sid=_USER_SID, owner_sid=_ADMINISTRATORS_SID)-- assert security._owner_is_self(_FakeSid(_USER_SID)) is True- assert security._owner_is_self(_FakeSid(_ADMINISTRATORS_SID)) is True- assert security._owner_is_self(_FakeSid(_STRANGER_SID)) is False- assert security._owner_is_self(_FakeSid(None)) is False---def test_administrators_is_not_trusted_when_it_is_not_this_token_owner():- """Acceptance follows the process token, not a hardcoded well-known SID."""-- security = _windows_security_stub(user_sid=_USER_SID, owner_sid=_USER_SID)-- assert security._self_owner_sddls() == (security.sddl,)- assert security._owner_is_self(_FakeSid(_USER_SID)) is True- assert security._owner_is_self(_FakeSid(_ADMINISTRATORS_SID)) is False---def test_relaxed_owner_still_requires_the_protected_private_dacl():- """Only the owner rule moved; the DACL contract is unchanged."""-- security = _windows_security_stub(user_sid=_USER_SID, owner_sid=_ADMINISTRATORS_SID)- security._descriptor_dacl = lambda _descriptor: "private-dacl"- security._acl_signature = lambda acl: (str(acl).encode(),)- control = control_ipc._WindowsSecurity._SE_DACL_PRESENT | control_ipc._WindowsSecurity._SE_DACL_PROTECTED-- security._descriptor_control = lambda _descriptor: control- security._validate_security_descriptor(- _FakeSid(_ADMINISTRATORS_SID), "private-dacl", object(), Path("runtime")- )-- for broken_control, dacl in (- (control_ipc._WindowsSecurity._SE_DACL_PRESENT, "private-dacl"),- (control, "inherited-dacl"),- ):- security._descriptor_control = lambda _descriptor, value=broken_control: value- with pytest.raises(control_ipc.ControlIpcSecurityError, match="owner or DACL"):- security._validate_security_descriptor(- _FakeSid(_ADMINISTRATORS_SID), dacl, object(), Path("runtime")- )diff --git a/tests/test_model_hub_state_private.py b/tests/test_model_hub_state_private.pynew file mode 100644index 000000000..c6f7f49e6--- /dev/null+++ b/tests/test_model_hub_state_private.py@@ -0,0 +1,99 @@+"""Model Hub's engine secrets and takeover journal stay owner-private, Windows included.++POSIX keeps its 0o700/0o600 coverage beside each owner. These cases pin what+Windows adds: modes mean nothing there, so engine start and an API-key Source+depend on the owner-private DACL instead.+"""++from __future__ import annotations++import os+from pathlib import Path++import pytest++from config.v2_config import ModelHubConfig+from core import private_fs+from core.handlers.model_hub.migration_journal import NativeFileEdit, NativeTakeoverJournal+from tests.test_private_fs import restore_windows_inheritance, widen+from vibe.model_hub_runtime.state import EngineStateError, EngineStateStore++windows_only = pytest.mark.skipif(os.name != "nt", reason="requires native Windows ACLs")+++def root(tmp_path):+ try:+ yield tmp_path / "model-hub"+ finally:+ restore_windows_inheritance(tmp_path)+++def _private(path: Path) -> bool:+ return private_fs.is_private(path, path.lstat().st_mode)+++@windows_only+def test_windows_engine_start_and_api_key_source_keep_secrets_private(root):+ store = EngineStateStore(root / "state")+ instance_dir, secrets = store.prepare_instance("install-1")+ # The second start reads the runtime secret back through its private-file check.+ assert store.prepare_instance("install-1") == (instance_dir, secrets)+ reference = store.store_api_key("sk-synthetic-windows", base_url="https://example.invalid/v1")+ assert store.read_api_key(reference) == "sk-synthetic-windows"++ # The engine writes its grants into the auth directory without asking for+ # any access rule; they must pass the audit every engine start runs.+ (store.auth_dir / "codex-synthetic.json").write_text("{}", encoding="utf-8")+ store.audit_auth_permissions()+ for path in (store.root, instance_dir, instance_dir / "runtime-secrets.json", store.auth_dir):+ assert _private(path), path+++@windows_only+def test_windows_widened_secrets_are_refused_and_auth_files_repaired_only_when_enforced(root):+ store = EngineStateStore(root / "state")+ store.prepare_instance("install-1")+ reference = store.store_api_key("sk-synthetic-windows", base_url="https://example.invalid/v1")+ widen(store.root / "credentials" / f"{reference}.json")+ with pytest.raises(EngineStateError, match="credential permissions are unsafe"):+ store.read_api_key(reference)++ grant = store.auth_dir / "codex-synthetic.json"+ grant.write_text("{}", encoding="utf-8")+ widen(grant)+ with pytest.raises(EngineStateError, match="auth credential permissions are unsafe"):+ store.audit_auth_permissions()+ store.audit_auth_permissions(enforce=True)+ assert _private(grant)+++def test_takeover_journal_repairs_its_directory_and_publishes_a_new_native_file(root):+ """A takeover directory an earlier build left non-private is repaired, not refused.++ Released Windows builds created it with the profile's inherited DACL before+ the check that always failed there, so refusing would block every later+ takeover on those hosts.+ """++ journal = NativeTakeoverJournal(root / "native-takeover" / "current.json")+ journal.path.parent.mkdir(parents=True)+ widen(journal.path.parent)+ native = root / "native" / "settings.json"+ native.parent.mkdir()+ edit = NativeFileEdit.plan(native, b'{"model":"hub"}\n')+ journal.save({+ "version": 1, "phase": "prepared", "items": [],+ "backends": ["claude"], "files": [edit.to_payload()],+ "source_ids": [], "credentials": [],+ "previous": ModelHubConfig().to_payload(),+ "updated": ModelHubConfig().to_payload(),+ })+ assert _private(journal.path.parent) and _private(journal.path)++ loaded = NativeFileEdit.from_payload(journal.load()["files"][0])+ loaded.apply()+ loaded.check(applied=True)+ assert native.read_bytes() == b'{"model":"hub"}\n'+ loaded.apply(reverse=True)+ assert not native.exists()diff --git a/tests/test_private_fs.py b/tests/test_private_fs.pynew file mode 100644index 000000000..304a1d63d--- /dev/null+++ b/tests/test_private_fs.py@@ -0,0 +1,222 @@+"""Owner-private paths: the portable rule and the Windows DACL decisions behind it."""++from __future__ import annotations++import os+import subprocess+import sys+from pathlib import Path+from unittest.mock import MagicMock++import pytest++from core import private_fs++windows_only = pytest.mark.skipif(os.name != "nt", reason="requires native Windows ACLs")+++def widen(path: Path) -> None:+ """Let everyone read ``path``: a wider mode on POSIX, an Everyone ACE on Windows."""++ if os.name == "nt":+ subprocess.run(["icacls", str(path), "/grant", "*S-1-1-0:R"], check=True, capture_output=True, text=True)+ else:+ path.chmod(0o755 if path.is_dir() else 0o644)+++def restore_windows_inheritance(root: Path) -> None:+ """Undo protected DACLs so pytest can still delete ``root`` afterwards.++ Teardown only, and deliberately not ``check=True``: a cleanup failure must+ never replace the assertion failure the test exists to report.+ """++ if os.name != "nt":+ return+ for arguments in (["/inheritance:e"], ["/reset", "/T", "/C", "/Q"]):+ subprocess.run(["icacls", str(root), *arguments], capture_output=True, text=True)+++def private_root(tmp_path):+ root = tmp_path / "private"+ try:+ yield root+ finally:+ restore_windows_inheritance(tmp_path)+++def _private(path: Path) -> bool:+ return private_fs.is_private(path, path.lstat().st_mode)+++def test_widened_paths_are_not_private_until_repaired(private_root):+ private_fs.ensure_private_directory(private_root)+ secret = private_root / "secret.json"+ secret.write_text("{}", encoding="utf-8")+ private_fs.make_private_file(secret)+ assert _private(private_root) and _private(secret)++ widen(secret)+ widen(private_root)+ assert not _private(secret)+ assert not _private(private_root)+ with pytest.raises(private_fs.PrivatePathError):+ private_fs.ensure_private_directory(private_root, repair=False)++ private_fs.make_private_file(secret)+ private_fs.ensure_private_directory(private_root)+ assert _private(secret) and _private(private_root)+ private_fs.ensure_private_directory(private_root, repair=False)+++@windows_only+def test_windows_private_directory_makes_what_any_process_creates_inside_private(private_root):+ """Windows ignores the umask, so only inheritance can make the engine's files private.++ The engine writes its OAuth grants with plain file APIs, and Python's+ ``mkstemp`` and ``mkdir`` ask for nothing either. Each must come out+ private from the directory's inheritable entries alone.+ """++ private_fs.ensure_private_directory(private_root)+ nested = private_root / "auth" / "nested"+ os.makedirs(nested)+ created = [private_root / "file.json", private_root / "auth" / "grant.json", nested / "deep.json"]+ for path in created:+ path.write_text("{}", encoding="utf-8")+ child_file = private_root / "from-child-process.json"+ subprocess.run([sys.executable, "-c", "import sys; open(sys.argv[1], 'w').write('{}')", child_file], check=True)++ for path in [private_root / "auth", nested, *created, child_file]:+ assert _private(path), path+ # CPython's own mkdir(mode=0o700) protects a directory for SYSTEM,+ # Administrators and its owner; released builds made Model Hub's auth+ # directory that way, and what lands in it must still count as private.+ legacy = private_root / "legacy"+ legacy.mkdir(mode=0o700)+ (legacy / "grant.json").write_text("{}", encoding="utf-8")+ assert _private(legacy) and _private(legacy / "grant.json")++ descriptor = os.open(created[0], os.O_RDONLY)+ try:+ assert private_fs.is_private(created[0], os.fstat(descriptor).st_mode, fd=descriptor)+ finally:+ os.close(descriptor)+++@windows_only+def test_windows_directory_without_inheritable_entries_is_not_private(private_root):+ """Owner and SYSTEM alone are not enough: what is created inside must start private too."""++ security = private_fs.windows_security()+ security.ensure_private_directory(private_root, allow_repair=True)+ security.validate_path(private_root)+ assert not _private(private_root)++ private_fs.ensure_private_directory(private_root)+ assert _private(private_root)+++class _FakeSid:+ """A SID pointer stand-in that compares by the SDDL text that produced it."""++ def __init__(self, value: str | None = None) -> None:+ self.value = value++ def __bool__(self) -> bool:+ return self.value is not None+++class _FakeCtypes:+ @staticmethod+ def byref(cell):+ return cell+++class _FakeWinTypes:+ LPVOID = _FakeSid+ BOOL = _FakeSid+ DWORD = _FakeSid+++def _windows_security_stub(*, user_sid: str, owner_sid: str):+ """Drive the real owner-acceptance logic without Windows ctypes bindings.++ ``WindowsSecurity.__init__`` loads ``advapi32``/``kernel32``, so it cannot+ run off Windows. Every method under test reads only instance attributes, so+ a bypassed constructor with fake bindings exercises the real decision.+ """++ security = object.__new__(private_fs.WindowsSecurity)+ security.ctypes = _FakeCtypes+ security.wintypes = _FakeWinTypes+ security.current_user_sid = user_sid+ security.current_owner_sid = owner_sid+ security.sddl = f"O:{user_sid}D:P(A;;FA;;;{user_sid})(A;;FA;;;SY)"++ def _convert(sddl, _revision, descriptor_ref, _size_ref):+ descriptor_ref.value = sddl+ return 1++ def _descriptor_owner(descriptor, owner_ref, _defaulted_ref):+ owner_ref.value = descriptor.value.split("D:", 1)[0][len("O:") :]+ return 1++ security.advapi32 = MagicMock(+ ConvertStringSecurityDescriptorToSecurityDescriptorW=_convert,+ GetSecurityDescriptorOwner=_descriptor_owner,+ EqualSid=lambda left, right: left.value == right.value,+ )+ security.kernel32 = MagicMock(LocalFree=lambda *_args: None)+ return security+++_USER_SID = "S-1-5-21-1111-2222-3333-1001"+_ADMINISTRATORS_SID = "S-1-5-32-544"+_STRANGER_SID = "S-1-5-21-9999-8888-7777-500"+++def test_elevated_token_default_owner_is_accepted_as_self():+ """An elevated token stamps BUILTIN\\Administrators on what it creates."""++ security = _windows_security_stub(user_sid=_USER_SID, owner_sid=_ADMINISTRATORS_SID)++ assert security._owner_is_self(_FakeSid(_USER_SID)) is True+ assert security._owner_is_self(_FakeSid(_ADMINISTRATORS_SID)) is True+ assert security._owner_is_self(_FakeSid(_STRANGER_SID)) is False+ assert security._owner_is_self(_FakeSid(None)) is False+++def test_administrators_is_not_trusted_when_it_is_not_this_token_owner():+ """Acceptance follows the process token, not a hardcoded well-known SID."""++ security = _windows_security_stub(user_sid=_USER_SID, owner_sid=_USER_SID)++ assert security._self_owner_sddls() == (security.sddl,)+ assert security._owner_is_self(_FakeSid(_USER_SID)) is True+ assert security._owner_is_self(_FakeSid(_ADMINISTRATORS_SID)) is False+++def test_relaxed_owner_still_requires_the_protected_private_dacl():+ """Only the owner rule moved; the DACL contract is unchanged."""++ security = _windows_security_stub(user_sid=_USER_SID, owner_sid=_ADMINISTRATORS_SID)+ security._descriptor_dacl = lambda _descriptor: "private-dacl"+ security._acl_signature = lambda acl: (str(acl).encode(),)+ control = private_fs.WindowsSecurity._SE_DACL_PRESENT | private_fs.WindowsSecurity._SE_DACL_PROTECTED++ security._descriptor_control = lambda _descriptor: control+ security._validate_security_descriptor(+ _FakeSid(_ADMINISTRATORS_SID), "private-dacl", object(), Path("runtime")+ )++ for broken_control, dacl in (+ (private_fs.WindowsSecurity._SE_DACL_PRESENT, "private-dacl"),+ (control, "inherited-dacl"),+ ):+ security._descriptor_control = lambda _descriptor, value=broken_control: value+ with pytest.raises(private_fs.PrivatePathError, match="owner or DACL"):+ security._validate_security_descriptor(+ _FakeSid(_ADMINISTRATORS_SID), dacl, object(), Path("runtime")+ )diff --git a/vibe/model_hub_runtime/state.py b/vibe/model_hub_runtime/state.pyindex 84b16dba1..af7c29cce 100644--- a/vibe/model_hub_runtime/state.py+++ b/vibe/model_hub_runtime/state.py@@ -16,6 +16,7 @@ from typing import Any, Callable, Sequencefrom config.atomic_io import write_atomicfrom config.v2_config import normalize_model_hub_base_url, validate_model_hub_protocol_vendor+from core import private_fsfrom core.handlers.model_hub.events import contains_credential_materialfrom core.handlers.model_hub.identifiers import model_id_without_credential_addressfrom vibe.model_hub_runtime.api_key_vendors import (@@ -758,7 +759,7 @@ class EngineStateStore:mode = path.lstat().st_modeexcept FileNotFoundError:return None- if not stat.S_ISREG(mode) or stat.S_IMODE(mode) != 0o600:+ if not stat.S_ISREG(mode) or not private_fs.is_private(path, mode):raise EngineStateError("credential permissions are unsafe")payload = self._read_json(path)if payload is None:@@ -791,11 +792,12 @@ class EngineStateStore:for path in (self.root, directory):try:mode = path.lstat().st_mode+ private = stat.S_ISDIR(mode) and private_fs.is_private(path, mode)except FileNotFoundError:return Falseexcept OSError as exc:raise EngineStateError("unable to inspect credential directory") from exc- if not stat.S_ISDIR(mode) or stat.S_IMODE(mode) != 0o700:+ if not private:raise EngineStateError("credential directory permissions are unsafe")return True@@ -814,7 +816,7 @@ class EngineStateStore:return Noneexcept OSError as exc:raise EngineStateError("unable to inspect credential file") from exc- if not stat.S_ISREG(mode) or stat.S_IMODE(mode) != 0o600:+ if not stat.S_ISREG(mode) or not private_fs.is_private(path, mode):raise EngineStateError("credential permissions are unsafe")try:# Do not turn an open/read failure into content corruption. Ref@@ -826,7 +828,7 @@ class EngineStateStore:)with os.fdopen(descriptor, "rb") as handle:mode = os.fstat(handle.fileno()).st_mode- if not stat.S_ISREG(mode) or stat.S_IMODE(mode) != 0o600:+ if not stat.S_ISREG(mode) or not private_fs.is_private(path, mode, fd=handle.fileno()):raise EngineStateError("credential permissions are unsafe")raw = handle.read()except OSError as exc:@@ -868,7 +870,7 @@ class EngineStateStore:credentials_dir = self.root / "credentials"for directory in (self.root, credentials_dir):mode = directory.lstat().st_mode- if not stat.S_ISDIR(mode) or stat.S_IMODE(mode) != 0o700:+ if not stat.S_ISDIR(mode) or not private_fs.is_private(directory, mode):return Falsepayload = json.loads(self._read_private_bytes(credentials_dir / f"{credential_ref}.json",@@ -1106,17 +1108,17 @@ class EngineStateStore:auth_mode = self.auth_dir.lstat().st_modeif not stat.S_ISDIR(auth_mode):raise EngineStateError("engine auth directory is unsafe")- if stat.S_IMODE(self.auth_dir.stat().st_mode) != 0o700:+ if not private_fs.is_private(self.auth_dir, auth_mode):raise EngineStateError("engine auth directory permissions are unsafe")for entry in self.auth_dir.iterdir():mode = entry.lstat().st_modeif not stat.S_ISREG(mode):raise EngineStateError("engine auth directory contains an unsafe entry")- if stat.S_IMODE(mode) == 0o600:+ if private_fs.is_private(entry, mode):continueif not enforce:raise EngineStateError("engine auth credential permissions are unsafe")- entry.chmod(0o600)+ private_fs.make_private_file(entry)def _credential_path(self, credential_ref: str) -> Path:_credential_ref_auth_scheme(credential_ref)@@ -1181,7 +1183,7 @@ class EngineStateStore:result: list[tuple[str, dict[str, Any]]] = []for path in credentials_dir.glob("cred_*.json"):mode = path.lstat().st_mode- if not stat.S_ISREG(mode) or stat.S_IMODE(mode) != 0o600:+ if not stat.S_ISREG(mode) or not private_fs.is_private(path, mode):raise EngineStateError("credential permissions are unsafe")credential_ref = path.stemif path.suffix != ".json" or _CREDENTIAL_REF_RE.fullmatch(credential_ref) is None:@@ -1327,7 +1329,7 @@ class EngineStateStore:# directory durability boundary before cleanup can converge.cls._fsync_directory(path.parent)return False- if not stat.S_ISREG(mode) or stat.S_IMODE(mode) != 0o600:+ if not stat.S_ISREG(mode) or not private_fs.is_private(path, mode):raise EngineStateError("engine state path is unsafe")path.unlink()cls._fsync_directory(path.parent)@@ -1364,7 +1366,7 @@ class EngineStateStore:mode = path.lstat().st_modeexcept FileNotFoundError as exc:raise EngineStateError("OAuth credential is unavailable") from exc- if not stat.S_ISREG(mode) or stat.S_IMODE(mode) != 0o600:+ if not stat.S_ISREG(mode) or not private_fs.is_private(path, mode):raise EngineStateError(message)try:return path.read_bytes()@@ -1450,12 +1452,12 @@ class EngineStateStore:@staticmethoddef _ensure_private_dir(path: Path) -> None:- if path.exists():- if not stat.S_ISDIR(path.lstat().st_mode):- raise EngineStateError("engine state path is unsafe")- else:- path.mkdir(parents=True, mode=0o700)- path.chmod(0o700)+ if path.exists() and not stat.S_ISDIR(path.lstat().st_mode):+ raise EngineStateError("engine state path is unsafe")+ try:+ private_fs.ensure_private_directory(path)+ except private_fs.PrivatePathError as exc:+ raise EngineStateError("engine state path is unsafe") from exc@staticmethoddef _assert_private_file(path: Path, message: str) -> None:@@ -1463,7 +1465,7 @@ class EngineStateStore:mode = path.lstat().st_modeexcept FileNotFoundError:return- if not stat.S_ISREG(mode) or stat.S_IMODE(mode) != 0o600:+ if not stat.S_ISREG(mode) or not private_fs.is_private(path, mode):raise EngineStateError(message)