diff --git a/strata_control/private_files.py b/strata_control/private_files.py index 318fee9a..b272d1bb 100644 --- a/strata_control/private_files.py +++ b/strata_control/private_files.py @@ -372,8 +372,11 @@ def adopt_user_defaults() -> bool: ok_owner = bool(_advapi.SetTokenInformation(token, _TOKEN_OWNER, ctypes.byref(_Owner(psid)), ctypes.sizeof(_Owner))) descriptor = ctypes.c_void_p() + # A token default DACL also applies to processes, not just files. FA is + # FILE_ALL_ACCESS (0x1f01ff), which omits process query/set-information. + # GA is mapped to the full rights of the object being created. if not _advapi.ConvertStringSecurityDescriptorToSecurityDescriptorW( - f"D:(A;;FA;;;{sid})(A;;FA;;;SY)(A;;FA;;;BA)", _SDDL_REVISION_1, ctypes.byref(descriptor), None): + f"D:(A;;GA;;;{sid})(A;;GA;;;SY)(A;;GA;;;BA)", _SDDL_REVISION_1, ctypes.byref(descriptor), None): return False present, defaulted, dacl = wintypes.BOOL(), wintypes.BOOL(), ctypes.c_void_p() if not _advapi.GetSecurityDescriptorDacl(descriptor, ctypes.byref(present), ctypes.byref(dacl), diff --git a/tests/terminal/test_default_object_access.py b/tests/terminal/test_default_object_access.py new file mode 100644 index 00000000..b2f9ae70 --- /dev/null +++ b/tests/terminal/test_default_object_access.py @@ -0,0 +1,54 @@ +"""Windows token defaults must permit identifying children, not just opening files.""" +import json +import os +from pathlib import Path +import subprocess +import sys +import tempfile +import unittest + + +ROOT = Path(__file__).resolve().parents[2] +PROBE = r""" +import json, subprocess, sys +from pathlib import Path +from strata_control import private_files, procs + +assert private_files.adopt_user_defaults() +child = subprocess.Popen( + [sys.executable, "-c", "import time; print('ready', flush=True); time.sleep(30)"], + stdout=subprocess.PIPE, text=True, +) +try: + assert child.stdout.readline().strip() == "ready" + assert child.poll() is None + token = procs.start_token(child.pid) + assert token is not None, "live child became unreadable after adopting user defaults" + recorded = procs.write_record(child.pid, "engine") + assert recorded == token, "child identity could not be recorded" + print(json.dumps({"identified": True, "recorded": True}), flush=True) +finally: + child.kill() + child.wait(timeout=10) + child.stdout.close() +""" + + +@unittest.skipUnless(os.name == "nt", "Windows token default DACL") +class DefaultObjectAccess(unittest.TestCase): + def test_adopting_user_defaults_preserves_child_identity(self): + # Token defaults affect every subsequent object creation in this process. + # Exercise them in a disposable helper, not in the test runner itself. + with tempfile.TemporaryDirectory(prefix="strata-default-access-") as folder: + env = dict(os.environ, LOCALAPPDATA=folder, PYTHONPATH=str(ROOT)) + result = subprocess.run( + [sys.executable, "-c", PROBE], cwd=ROOT, env=env, + stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, timeout=25, + creationflags=getattr(subprocess, "CREATE_NO_WINDOW", 0), + ) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(json.loads(result.stdout), {"identified": True, "recorded": True}) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/terminal/test_private_files.py b/tests/terminal/test_private_files.py index 6da7d197..df80af0d 100644 --- a/tests/terminal/test_private_files.py +++ b/tests/terminal/test_private_files.py @@ -163,6 +163,37 @@ class ElevatedRunLockoutTests(unittest.TestCase): path.write_text("{}") self.assertEqual(path.read_text(), "{}") + def test_default_dacl_allows_reopening_child_process_for_qos(self): + import subprocess + import sys + # Isolate token changes and keep this test independent of the test runner's defaults. + script = ''' +import ctypes +from ctypes import wintypes +import subprocess +import sys +from strata_control import private_files as pf +from serve import winjob +assert pf.adopt_user_defaults() +k32 = ctypes.WinDLL("kernel32", use_last_error=True) +k32.OpenProcess.argtypes = (wintypes.DWORD, wintypes.BOOL, wintypes.DWORD) +k32.OpenProcess.restype = wintypes.HANDLE +k32.CloseHandle.argtypes = (wintypes.HANDLE,) +child = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(30)"]) +try: + handle = k32.OpenProcess(0x1000 | 0x0200, False, child.pid) + assert handle, "OpenProcess failed: %s" % ctypes.get_last_error() + try: + assert winjob.no_throttle(handle), "Setting child QoS failed" + finally: + k32.CloseHandle(handle) +finally: + child.terminate() + child.wait() +''' + result = subprocess.run([sys.executable, "-c", script], capture_output=True, text=True, timeout=40) + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + def test_only_the_users_own_folders_are_ever_repaired(self): self.assertFalse(pf.repair_access(os.environ.get("SystemRoot", "C:\Windows") + "\System32\drivers"))