diff --git a/tools/aurora-sep/tests/test_policy_window.py b/tools/aurora-sep/tests/test_policy_window.py index fda87d68a3f0c..8ae2a619e44ad 100644 --- a/tools/aurora-sep/tests/test_policy_window.py +++ b/tools/aurora-sep/tests/test_policy_window.py @@ -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+(?Punlock)\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*(?Punlock)\(\)\s*\}\s*else\s*" + r"\{\s*crate::xart_guard::policy_window\((?Punlock)\)\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)) @@ -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. @@ -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,