From 918bc12eb65a21e45f8e1cfb871294e7d678a21d Mon Sep 17 00:00:00 2001 From: Mingxin Wang Date: Sat, 21 Nov 2026 19:49:57 +0800 Subject: [PATCH] Fix the flaky PAC tests caused by signature collisions - Relocated or copied each proxy 16 times in the relocation and binary representation tests, because the static meta pointer is signed with the DA key, whose PAC on arm64e macOS is only 8 bits wide (bits 47 to 54) since TBI is enabled for user data pointers. A single relocation kept the same signature with probability 1/256, which made `TestRelocationAttack_StaticMetaStorage` fail to die in https://github.com/ngcpp/proxy/actions/runs/44738964776/job/136297425371 and made `TestBinaryRepresentation_StaticMetaStorage` see identical bytes at the same rate. A spurious failure now requires all 16 relocation attempts to collide, which happens with probability 1/2^128 for the DA key and 1/2^256 for the 16-bit IA key of the inline invoker. - Switched the static binary representation test from copy construction to copy assignment into empty proxies, because the elements of an array cannot be copy constructed one at a time. --- tests/proxy_pac_tests.cpp | 46 ++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/tests/proxy_pac_tests.cpp b/tests/proxy_pac_tests.cpp index bd1d6011..fbf9d425 100644 --- a/tests/proxy_pac_tests.cpp +++ b/tests/proxy_pac_tests.cpp @@ -63,6 +63,11 @@ struct CopyableFacade : pro::facade_builder // ::build {}; static_assert(!IsInlineMetaPreferred); +// A signed data pointer has only 8 PAC bits, so a single relocation keeps its +// signature with probability 1/256. A test fails spuriously only when all 16 +// attempts collide, which lowers the rate to 1/2^128. +constexpr std::size_t RelocationAttemptCount = 16; + } // namespace proxy_pac_tests_detail namespace detail = proxy_pac_tests_detail; @@ -70,23 +75,38 @@ namespace detail = proxy_pac_tests_detail; TEST(ProxyPacTests, TestBinaryRepresentation_InlineMetaStorage) { pro::proxy p1 = std::make_shared(123); auto p1_data = detail::GetRawBytes(p1); - auto p2 = std::move(p1); - auto p2_data = detail::GetRawBytes(p2); + pro::proxy + destinations[detail::RelocationAttemptCount]; + bool changed = false; + for (auto& destination : destinations) { + destination = std::move(p1); + if (detail::GetRawBytes(destination) != p1_data) { + changed = true; + } + p1 = std::move(destination); + } // Pointer authentication signs the stored metadata with address diversity, so // a genuine relocation re-signs it and the raw bytes of the signed meta // change - EXPECT_NE(p1_data, p2_data); + EXPECT_TRUE(changed); } TEST(ProxyPacTests, TestBinaryRepresentation_StaticMetaStorage) { pro::proxy p1 = std::make_shared(123); auto p1_data = detail::GetRawBytes(p1); - auto p2 = p1; - auto p2_data = detail::GetRawBytes(p2); + pro::proxy + destinations[detail::RelocationAttemptCount]; + bool changed = false; + for (auto& destination : destinations) { + destination = p1; + if (detail::GetRawBytes(destination) != p1_data) { + changed = true; + } + } // Both inline metadata and static metadata has pointer authentication - EXPECT_NE(p1_data, p2_data); + EXPECT_TRUE(changed); } TEST(ProxyPacTests, TestBitFlipAttack_InlineMetaStorage) { @@ -114,8 +134,11 @@ TEST(ProxyPacTests, TestRelocationAttack_InlineMetaStorage) { { int a = 123; pro::proxy p1 = &a; - pro::proxy p2; - detail::BitwiseCopy(p1, p2); + pro::proxy + destinations[detail::RelocationAttemptCount]; + for (auto& destination : destinations) { + detail::BitwiseCopy(p1, destination); + } }, ""); } @@ -125,8 +148,11 @@ TEST(ProxyPacTests, TestRelocationAttack_StaticMetaStorage) { { int a = 123; pro::proxy p1 = &a; - pro::proxy p2; - detail::BitwiseCopy(p1, p2); + pro::proxy + destinations[detail::RelocationAttemptCount]; + for (auto& destination : destinations) { + detail::BitwiseCopy(p1, destination); + } }, ""); }