Repository navigation
fix(cache): skip chmod when the Windows cache dir is already 0700 #436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lennix1337
wants to merge
3
commits into
redhat-et:main
Choose a base branch
from
lennix1337:fix/win32-cache-dir-acl-skip
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+221
−6
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ### Fixed — Windows: warm runs no longer rewrite the cache directory's ACL | ||
|
|
||
| - The per-user cache directory ladder skips `chmod(0700)` when `lstat` already reports an owner-only directory. On Win32 that `chmod` re-applies a protected DACL with inheritable ACEs, which Windows propagates through every file under the cache: with 38,000 files in the cache a warm run took ~2,024 ms instead of ~10.6 ms (p50 of 3, same revision, only this guard changed). A permissive directory is still repaired. Regression: `test/cache_ladder_windows.py` and the Windows arm of `test/cacheisolationcheck.sh` (#436, thanks @lennix1337). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| """Native Windows cache ACL regression; invoked by cacheisolationcheck.sh. | ||
|
|
||
| Observe NTFS ChangeTime, not timing thresholds: reapplying an unchanged DACL | ||
| changes it and propagates inheritable ACEs through the entire cache tree. | ||
| """ | ||
| import ctypes | ||
| from ctypes import wintypes | ||
| import os | ||
| from pathlib import Path | ||
| import re | ||
| import subprocess | ||
| import sys | ||
| import tempfile | ||
| import unittest | ||
|
|
||
|
|
||
| class FileBasicInfo(ctypes.Structure): | ||
| _fields_ = [(name, ctypes.c_longlong) for name in | ||
| ("creation", "access", "write", "change")] + [("attributes", wintypes.DWORD)] | ||
|
|
||
|
|
||
| def change_time(path): | ||
| kernel = ctypes.WinDLL("kernel32", use_last_error=True) | ||
| kernel.CreateFileW.argtypes = [wintypes.LPCWSTR, wintypes.DWORD, wintypes.DWORD, | ||
| ctypes.c_void_p, wintypes.DWORD, wintypes.DWORD, wintypes.HANDLE] | ||
| kernel.CreateFileW.restype = wintypes.HANDLE | ||
| kernel.GetFileInformationByHandleEx.argtypes = [wintypes.HANDLE, ctypes.c_int, | ||
| ctypes.c_void_p, wintypes.DWORD] | ||
| kernel.GetFileInformationByHandleEx.restype = wintypes.BOOL | ||
| kernel.CloseHandle.argtypes = [wintypes.HANDLE] | ||
| kernel.CloseHandle.restype = wintypes.BOOL | ||
| handle = kernel.CreateFileW(str(path), 0x80, 7, None, 3, 0x02000000, None) | ||
| if handle == wintypes.HANDLE(-1).value: | ||
| raise ctypes.WinError(ctypes.get_last_error()) | ||
| try: | ||
| info = FileBasicInfo() | ||
| if not kernel.GetFileInformationByHandleEx(handle, 0, ctypes.byref(info), ctypes.sizeof(info)): | ||
| raise ctypes.WinError(ctypes.get_last_error()) | ||
| return info.change | ||
| finally: | ||
| if not kernel.CloseHandle(handle): | ||
| raise ctypes.WinError(ctypes.get_last_error()) | ||
|
|
||
|
|
||
| def dacl_sddl(path): | ||
| security = ctypes.WinDLL("advapi32", use_last_error=True) | ||
| kernel = ctypes.WinDLL("kernel32", use_last_error=True) | ||
| security.GetNamedSecurityInfoW.argtypes = [wintypes.LPWSTR, ctypes.c_int, wintypes.DWORD, | ||
| ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, | ||
| ctypes.c_void_p, ctypes.POINTER(ctypes.c_void_p)] | ||
| security.GetNamedSecurityInfoW.restype = wintypes.DWORD | ||
| security.ConvertSecurityDescriptorToStringSecurityDescriptorW.argtypes = [ | ||
| ctypes.c_void_p, wintypes.DWORD, wintypes.DWORD, ctypes.POINTER(wintypes.LPWSTR), ctypes.c_void_p] | ||
| security.ConvertSecurityDescriptorToStringSecurityDescriptorW.restype = wintypes.BOOL | ||
| kernel.LocalFree.argtypes = [ctypes.c_void_p] | ||
| kernel.LocalFree.restype = ctypes.c_void_p | ||
| descriptor = ctypes.c_void_p() | ||
| error = security.GetNamedSecurityInfoW(str(path), 1, 4, None, None, None, None, | ||
| ctypes.byref(descriptor)) | ||
| if error: | ||
| raise ctypes.WinError(error) | ||
| text = wintypes.LPWSTR() | ||
| try: | ||
| if not security.ConvertSecurityDescriptorToStringSecurityDescriptorW( | ||
| descriptor, 1, 4, ctypes.byref(text), None): | ||
| raise ctypes.WinError(ctypes.get_last_error()) | ||
| return text.value | ||
| finally: | ||
| if text: | ||
| kernel.LocalFree(ctypes.cast(text, ctypes.c_void_p)) | ||
| kernel.LocalFree(descriptor) | ||
|
|
||
|
|
||
| # isOwnerClassSid in os_win32.cpp: OWNER RIGHTS, CREATOR OWNER, Administrators, SYSTEM and this user. | ||
| OWNER_CLASS_TRUSTEES = {"OW", "CO", "BA", "SY"} | ||
|
|
||
|
|
||
| def current_user_sid(): | ||
| result = subprocess.run(["whoami.exe", "/user", "/fo", "csv", "/nh"], | ||
| capture_output=True, text=True, check=True, timeout=30) | ||
| return result.stdout.strip().split(",")[-1].strip('"') | ||
|
|
||
|
|
||
| def owner_only(path): | ||
| """Windows counterpart of mode 0700: protected DACL whose allow ACEs name only | ||
| owner-class principals, the same rule os_win32.cpp readOwnerAndMode applies | ||
| (deny ACEs only take access away, so they never make a DACL less private).""" | ||
| sddl = dacl_sddl(path) | ||
| dacl = sddl[sddl.index("D:"):] if "D:" in sddl else "" | ||
| if not dacl.startswith("D:P"): | ||
| return False, sddl | ||
| trustees = OWNER_CLASS_TRUSTEES | {current_user_sid()} | ||
| for ace in re.findall(r"\(([^)]*)\)", dacl): | ||
| fields = ace.split(";") | ||
| if fields[0] == "D": | ||
| continue | ||
| if fields[0] != "A" or fields[-1] not in trustees: | ||
| return False, sddl | ||
| return True, sddl | ||
|
|
||
|
|
||
| class CacheLadderWindows(unittest.TestCase): | ||
| def setUp(self): | ||
| self.temp = tempfile.TemporaryDirectory(prefix="ripwire-acl-") | ||
| self.addCleanup(self.temp.cleanup) | ||
| self.root = Path(self.temp.name) | ||
| self.shared = self.root / "shared" | ||
| self.shared.mkdir() | ||
| self.fixture = self.root / "fixture" | ||
| self.fixture.mkdir() | ||
| (self.fixture / "code.ts").write_text("export function alphaOne() { return 42; }\n") | ||
| self.env = {**os.environ, "TMPDIR": str(self.shared), "RIPWIRE_CACHE_STATS": "1"} | ||
| self.cache = self.shared / "ripwire" | ||
|
|
||
| def query(self): | ||
| result = subprocess.run([BINARY, str(self.fixture), "--grep=alphaOne", "--legend=compact"], | ||
| env=self.env, capture_output=True, text=True, timeout=30, check=True) | ||
| self.assertIn("alphaOne", result.stdout) | ||
| return result | ||
|
|
||
| def prime(self): | ||
| self.query() | ||
| warm = self.query() | ||
| self.assertIn("reparsed=0 reused=1", warm.stderr) | ||
| return warm.stdout | ||
|
|
||
| def test_warm_private_cache_does_not_reapply_acl(self): | ||
| expected = self.prime() | ||
| before = change_time(self.cache) | ||
| result = self.query() | ||
| self.assertEqual(result.stdout, expected) | ||
| self.assertIn("reparsed=0 reused=1", result.stderr) | ||
| self.assertEqual(change_time(self.cache), before, | ||
| "warm cache rewrote directory metadata (inheritable ACL propagation)") | ||
|
|
||
| def test_permissive_cache_is_repaired_once(self): | ||
| expected = self.prime() | ||
| subprocess.run(["icacls.exe", str(self.cache), "/grant", "*S-1-1-0:(OI)(CI)RX"], | ||
| capture_output=True, text=True, check=True, timeout=30) | ||
| before = change_time(self.cache) | ||
| self.assertEqual(self.query().stdout, expected) | ||
| self.assertNotEqual(change_time(self.cache), before, "permissive ACL was not repaired") | ||
| repaired = dacl_sddl(self.cache) | ||
| self.assertTrue(repaired.startswith("D:P"), repaired) | ||
| self.assertNotIn(";;;WD)", repaired, "Everyone still has cache access") | ||
| stable = change_time(self.cache) | ||
| self.query() | ||
| self.assertEqual(change_time(self.cache), stable) | ||
|
|
||
| def test_junction_cache_is_refused_without_touching_target(self): | ||
| target = self.root / "target" | ||
| target.mkdir() | ||
| # No command shell: a temp path holding cmd.exe metacharacters (&, ^) stays one argument. | ||
| import _winapi # Windows-only CPython module; the platform guard runs before the tests | ||
| _winapi.CreateJunction(str(target), str(self.cache)) | ||
| self.addCleanup(self.cache.rmdir) | ||
| before = (change_time(target), dacl_sddl(target)) | ||
| result = self.query() | ||
| self.assertIn("reparsed=1 reused=0", result.stderr) | ||
| self.assertEqual((change_time(target), dacl_sddl(target)), before) | ||
| self.assertEqual(list(target.iterdir()), []) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| if sys.platform != "win32": | ||
| raise SystemExit("This regression requires native Windows Python") | ||
| if len(sys.argv) == 3 and sys.argv[1] == "--owner-only": | ||
| private, sddl = owner_only(sys.argv[2]) | ||
| print(sddl) | ||
| raise SystemExit(0 if private else 1) | ||
| BINARY = str(Path(sys.argv.pop(1)).resolve()) | ||
| unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔎 Supported by static analysis
Script executed:
Repository: redhat-et/ripwire
Length of output: 17239
Web query:
official Python documentation sys.platform Cygwin value win32Result:
Use a native Windows Python for the Cygwin branch.
When
uname -sreturnsCYGWIN*, the script setswin32=1but both helper calls usepython. Ordinary Cygwin Python reportssys.platform == "cygwin", sotest/cache_ladder_windows.pyexits at its platform guard before performing the DACL check or regression tests. The Cygwin workflow therefore fails.Use an explicit native Windows Python for both helper calls. Keep
CYGWIN*on the Windows path so path conversion and DACL validation remain active.