Windows: the token default DACL grants generic rights, so a child process can still be opened after adopt_user_defaults (#3428, #3399)
adopt_user_defaults() set the token's default DACL with FA (FILE_ALL_ACCESS). That default also applies to new process objects, and the file mask has no process query or set-information right, so a child could not be reopened by PID: the HighQoS helper timed out and procs.start_token could not record the child. GA lets Windows map the mask to the object being created; the principals are unchanged. Ported from PR #3428 by kossum (QoS reopen test). Test from PR #3399 by midhatn (a live child is identified and recorded) added; both fail without the change and pass with it. Co-authored-by: MIDHAT NASHAR <35053211+midhatn@users.noreply.github.com>
This commit is contained in:
1 parent
14bef6bb46
commit
e2e99ae54a
3 files changed
+89
-1
No files matched your search
@@ -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),
|
||||
|
||||
@@ -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()
|
||||
@@ -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"))
|
||||
|
||||
|
||||
Reference in new issue
Block a user