Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions tools/aurora-sep/tests/test_policy_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,35 @@ def setUp(self):
self.sbio = (APPLE / "sbio.rs").read_text()
self.activate = function(self.sbio, "activate_touchid")

def assert_cached_refkey_window(self, body, at):
# A deferred closure's wrapper comes after its definition. Match its
# complete dispatch rather than accepting any later window in the fn.
# Preserve offsets while ignoring words in comments.
clean = re.sub(r"//[^\n]*|/\*.*?\*/", lambda m: " " * len(m[0]),
body, flags=re.S)
closure = re.search(r"\blet\s+(?P<binding>unlock)\s*=\s*\|\|\s*\{", clean)
self.assertIsNotNone(closure)
opening = closure.end() - 1
depth, end = 1, opening + 1
while depth and end < len(clean):
depth += (clean[end] == "{") - (clean[end] == "}")
end += 1
self.assertEqual(depth, 0)
self.assertTrue(opening < at < end)
self.assertEqual(clean.count(PREPARE), 1)
# The initrd deliberately leaves the real root's window unused; its
# guarded store is read-only. Only that branch may call unlock directly.
dispatch = re.search(
r"\blet\s+unlocked\s*=\s*if\s+self\.keystore_only\.load\(Relaxed\)\s*"
r"\{\s*(?P<initrd>unlock)\(\)\s*\}\s*else\s*"
r"\{\s*crate::xart_guard::policy_window\((?P<root>unlock)\)\s*\};",
clean)
self.assertIsNotNone(dispatch)
self.assertGreater(dispatch.start(), end)
self.assertEqual(
[m.start() for m in re.finditer(r"\bunlock\b", clean)],
[closure.start("binding"), dispatch.start("initrd"), dispatch.start("root")])

def test_touchid_designates_inside_the_window(self):
at = self.activate.index(DESIGNATE)
self.assertIn("crate::xart_guard::policy_window(", statement(self.activate, at))
Expand All @@ -87,6 +116,26 @@ def test_touchid_designates_a_keybag_that_already_existed(self):
self.assertLess(recovered, self.activate.index(DESIGNATE))
self.assertNotIn("sks_provision_identity_keybag(", self.activate)

def test_cached_refkey_window_wraps_deferred_unlock(self):
body = function((APPLE / "refkey.rs").read_text(), "refkey_unseal_cached")
self.assert_cached_refkey_window(body, body.index(PREPARE))

def test_cached_refkey_rejects_unguarded_real_root_unlock(self):
body = function((APPLE / "refkey.rs").read_text(), "refkey_unseal_cached")
wrapped = "crate::xart_guard::policy_window(unlock)"
self.assertEqual(body.count(wrapped), 1)
body = body.replace(wrapped, "unlock()")
with self.assertRaises(AssertionError):
self.assert_cached_refkey_window(body, body.index(PREPARE))

def test_cached_refkey_rejects_extra_unguarded_unlock(self):
body = function((APPLE / "refkey.rs").read_text(), "refkey_unseal_cached")
dispatch = "let unlocked = if self.keystore_only.load(Relaxed)"
self.assertEqual(body.count(dispatch), 1)
body = body.replace(dispatch, "unlock();\n " + dispatch)
with self.assertRaises(AssertionError):
self.assert_cached_refkey_window(body, body.index(PREPARE))

def test_no_other_path_designates_outside_a_window(self):
# A new designate of an existing keybag needs the window too; add it
# here once it runs in one.
Expand All @@ -99,6 +148,9 @@ def test_no_other_path_designates_outside_a_window(self):
self.assertTrue(callers)
for f, outer, body, at in callers:
with self.subTest(caller=outer):
if (f, outer) == ("refkey.rs", "refkey_unseal_cached"):
self.assert_cached_refkey_window(body, at)
continue
before = body[:at]
self.assertTrue("crate::xart_guard::policy_window(" in before
or "crate::xart_guard::close_policy_window(" in before,
Expand Down
Loading