Fix #3302: owner-only file checks accept the built-in Administrator (SDDL alias LA)
This commit is contained in:
1 parent
bcda2e4b0d
commit
ef5d8ab946
2 files changed
+69
-3
No files matched your search
@@ -95,6 +95,28 @@ def current_user_sid() -> str:
|
||||
return _sid_cache
|
||||
|
||||
|
||||
def _same_sid(sddl_trustee: str, sid: str) -> bool:
|
||||
"""True when an SDDL trustee names ``sid``. SDDL prints well-known accounts as two-letter aliases (the built-in
|
||||
Administrator, RID 500, is ``LA``), so an alias is turned into its string SID before the compare (#3302)."""
|
||||
if sddl_trustee == sid:
|
||||
return True
|
||||
if sddl_trustee.upper().startswith("S-1-") or not WINDOWS:
|
||||
return False
|
||||
pointer = ctypes.c_void_p()
|
||||
if not _advapi.ConvertStringSidToSidW(sddl_trustee, ctypes.byref(pointer)):
|
||||
return False
|
||||
try:
|
||||
text = wintypes.LPWSTR()
|
||||
if not _advapi.ConvertSidToStringSidW(pointer.value, ctypes.byref(text)):
|
||||
return False
|
||||
try:
|
||||
return text.value == sid
|
||||
finally:
|
||||
_kernel.LocalFree(ctypes.cast(text, ctypes.c_void_p))
|
||||
finally:
|
||||
_kernel.LocalFree(pointer)
|
||||
|
||||
|
||||
def restrict_to_current_user(path: str | os.PathLike, *, directory: bool | None = None) -> None:
|
||||
"""Make ``path`` readable and writable by the current user only.
|
||||
|
||||
@@ -171,7 +193,7 @@ def is_private(path: str | os.PathLike) -> bool:
|
||||
if not head.startswith("D:") or "P" not in head[2:] or len(entries) != 1:
|
||||
return False
|
||||
fields = entries[0].split(";")
|
||||
return len(fields) >= 6 and fields[0] == "A" and fields[5] == current_user_sid()
|
||||
return len(fields) >= 6 and fields[0] == "A" and _same_sid(fields[5], current_user_sid())
|
||||
|
||||
|
||||
_PRIVILEGED = {"SY", "BA", "OW", "CO", "CG", "S-1-5-18", "S-1-5-32-544", "S-1-3-4"}
|
||||
@@ -188,10 +210,12 @@ def is_unshared(path: str | os.PathLike) -> bool:
|
||||
if not WINDOWS:
|
||||
return is_private(path)
|
||||
sddl = dacl_sddl(path)
|
||||
allowed = _PRIVILEGED | {current_user_sid()}
|
||||
me = current_user_sid()
|
||||
allowed = _PRIVILEGED | {me}
|
||||
for entry in re.findall(r"\(([^()]*)\)", sddl):
|
||||
fields = entry.split(";")
|
||||
if len(fields) >= 6 and fields[0] in ("A", "OA") and fields[5] not in allowed:
|
||||
if (len(fields) >= 6 and fields[0] in ("A", "OA") and fields[5] not in allowed
|
||||
and not _same_sid(fields[5], me)):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
@@ -65,5 +65,47 @@ class Issue3298MtpDepth(unittest.TestCase):
|
||||
self.assertEqual(files_present({"args": ["--mtp", str(self.root / "gone")]}), [str(self.root / "gone")])
|
||||
|
||||
|
||||
@unittest.skipUnless(sys.platform == "win32", "SDDL aliases exist on Windows only")
|
||||
class Issue3302AdministratorAlias(unittest.TestCase):
|
||||
"""SDDL prints the built-in Administrator (RID 500) as `LA`; the owner-only checks must still accept it."""
|
||||
|
||||
def setUp(self):
|
||||
from unittest import mock
|
||||
from strata_control import private_files as pf
|
||||
self.pf = pf
|
||||
self.mock = mock
|
||||
# the built-in Administrator's string SID on this machine: resolve the alias the same way the fix does
|
||||
import ctypes
|
||||
from ctypes import wintypes
|
||||
pointer = ctypes.c_void_p()
|
||||
pf._advapi.ConvertStringSidToSidW("LA", ctypes.byref(pointer))
|
||||
text = wintypes.LPWSTR()
|
||||
pf._advapi.ConvertSidToStringSidW(pointer.value, ctypes.byref(text))
|
||||
self.admin_sid = text.value
|
||||
self.assertTrue(self.admin_sid.endswith("-500"))
|
||||
|
||||
def test_alias_matches_the_account_it_names(self):
|
||||
self.assertTrue(self.pf._same_sid("LA", self.admin_sid))
|
||||
self.assertTrue(self.pf._same_sid(self.admin_sid, self.admin_sid))
|
||||
self.assertFalse(self.pf._same_sid("LA", "S-1-5-21-1-2-3-1001"))
|
||||
self.assertFalse(self.pf._same_sid("WD", self.admin_sid))
|
||||
self.assertFalse(self.pf._same_sid("S-1-5-21-1-2-3-1001", self.admin_sid))
|
||||
|
||||
def test_private_and_unshared_hold_for_the_built_in_administrator(self):
|
||||
pf = self.pf
|
||||
with self.mock.patch.object(pf, "current_user_sid", return_value=self.admin_sid), \
|
||||
self.mock.patch.object(pf, "dacl_sddl", return_value="D:PAI(A;OICI;FA;;;LA)"):
|
||||
self.assertTrue(pf.is_private("x"))
|
||||
self.assertTrue(pf.is_unshared("x"))
|
||||
with self.mock.patch.object(pf, "current_user_sid", return_value=self.admin_sid), \
|
||||
self.mock.patch.object(pf, "dacl_sddl", return_value="D:PAI(A;OICI;FA;;;S-1-5-21-1-2-3-1001)"):
|
||||
self.assertFalse(pf.is_private("x"))
|
||||
self.assertFalse(pf.is_unshared("x"))
|
||||
with self.mock.patch.object(pf, "current_user_sid", return_value=self.admin_sid), \
|
||||
self.mock.patch.object(pf, "dacl_sddl", return_value="D:PAI(A;OICI;FA;;;LA)(A;OICI;FR;;;WD)"):
|
||||
self.assertFalse(pf.is_private("x"))
|
||||
self.assertFalse(pf.is_unshared("x"))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user