Truly independent web browser
AK: Zero a Function’s storage before constructing its callable
sideshowbarker2972e1aa15e6
diff --git a/AK/Function.h b/AK/Function.hindex 183585a2073..b04d0d8c5cb 100644--- a/AK/Function.h+++ b/AK/Function.h@@ -31,6 +31,7 @@#include <AK/Assertions.h>#include <AK/Atomic.h>#include <AK/BitCast.h>+#include <AK/Memory.h>#include <AK/Noncopyable.h>#include <AK/ScopeGuard.h>#include <AK/Span.h>@@ -243,6 +244,7 @@ private:void init_and_swap(u8* destination, size_t size) final override{VERIFY(size >= sizeof(CallableWrapper));+ zero_callable_storage(destination, size);new (destination) CallableWrapper { move(m_callable) };}@@ -265,6 +267,17 @@ private:Block,};+ // A callable is constructed into zeroed storage, since its bytes get scanned conservatively: GC::Function and the+ // other cells that visit raw_capture_range() take every pointer-sized word in it that points at a cell for an edge.+ // So padding, and storage the callable doesn't use, must not keep whatever the memory held before — a stale pointer+ // there would keep alive the cell it points at, and everything that cell reaches. And secure_zero() keeps a+ // compiler from dropping the zeroing as a dead store, as GCC's -flifetime-dse otherwise does, since the constructor+ // that follows starts the object's lifetime.+ static void zero_callable_storage(void* storage, size_t size)+ {+ secure_zero(storage, size);+ }+CallableWrapperBase* callable_wrapper() const{switch (m_kind) {@@ -333,10 +346,17 @@ private:}if constexpr (alignof(Callable) > inline_alignment || sizeof(WrapperType) > inline_capacity) {- *bit_cast<CallableWrapperBase**>(&m_storage) = new WrapperType(forward<Callable>(callable));+ void* storage;+ if constexpr (alignof(WrapperType) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)+ storage = WrapperType::operator new(sizeof(WrapperType), std::align_val_t { alignof(WrapperType) });+ else+ storage = WrapperType::operator new(sizeof(WrapperType));+ zero_callable_storage(storage, sizeof(WrapperType));+ *bit_cast<CallableWrapperBase**>(&m_storage) = new (storage) WrapperType(forward<Callable>(callable));m_kind = FunctionKind::Outline;} else {static_assert(sizeof(WrapperType) <= inline_capacity);+ zero_callable_storage(m_storage, inline_capacity);if constexpr (IsBlockClosure<Callable>) {if constexpr (Detail::HaveObjcArc) {new (m_storage) WrapperType(forward<Callable>(callable));diff --git a/Tests/AK/TestFunction.cpp b/Tests/AK/TestFunction.cppindex 1f7ad314b7b..ee8370f18d4 100644--- a/Tests/AK/TestFunction.cpp+++ b/Tests/AK/TestFunction.cpp@@ -9,6 +9,7 @@#include <AK/Function.h>#include <AK/NonnullRefPtr.h>#include <AK/RefCounted.h>+#include <AK/Vector.h>namespace {@@ -90,3 +91,83 @@ TEST_CASE(outline_wrapper_respects_callable_alignment)Function<bool()> moved = move(function);EXPECT(moved());}++// The GC scans a callable's bytes conservatively (raw_capture_range()), so the padding between captures, and storage+// a callable doesn't use, must not keep whatever stale data the memory held before.+static bool capture_range_holds(ReadonlyBytes range, u8 byte)+{+ for (auto value : range) {+ if (value == byte)+ return true;+ }+ return false;+}++static constexpr u8 stale_byte = 0xaa;++TEST_CASE(inline_callable_padding_is_zeroed)+{+ alignas(Function<u64()>) u8 buffer[sizeof(Function<u64()>)];+ __builtin_memset(buffer, stale_byte, sizeof(buffer));++ // A bool followed by a u64 leaves padding between them.+ auto* function = new (buffer) Function<u64()>([flag = true, number = 0x1122334455667788ull]() {+ return flag ? number : 0;+ });+ EXPECT_EQ((*function)(), 0x1122334455667788ull);+ EXPECT(!capture_range_holds(function->raw_capture_range(), stale_byte));+ function->~Function();+}++TEST_CASE(moved_inline_callable_padding_is_zeroed)+{+ alignas(Function<u64()>) u8 source_buffer[sizeof(Function<u64()>)];+ __builtin_memset(source_buffer, stale_byte, sizeof(source_buffer));+ auto* source = new (source_buffer) Function<u64()>([flag = true, number = 0x1122334455667788ull]() {+ return flag ? number : 0;+ });++ alignas(Function<u64()>) u8 buffer[sizeof(Function<u64()>)];+ __builtin_memset(buffer, stale_byte, sizeof(buffer));+ auto* destination = new (buffer) Function<u64()>(move(*source));+ EXPECT_EQ((*destination)(), 0x1122334455667788ull);+ EXPECT(!capture_range_holds(destination->raw_capture_range(), stale_byte));+ destination->~Function();+ source->~Function();+}++TEST_CASE(outline_callable_padding_is_zeroed)+{+ int instance_count = 0;+ // The padding after the bool sits before a member with a copy constructor of its own, so no copy of the captures+ // writes it: It keeps whatever the wrapper's storage held.+ struct Captures {+ bool flag { true };+ CopyOnly counted;+ u64 numbers[6] { 1, 2, 3, 4, 5, 6 };+ };+ static constexpr size_t wrapper_size = sizeof(void*) + sizeof(Captures);++ // Fill enough blocks of the wrapper's size class with stale bytes, and free them, that the allocator hands one of+ // them back for the wrapper.+ Vector<void*> stale_blocks;+ for (size_t i = 0; i < 4096; ++i) {+ auto* block = kmalloc(HeapPartition::General, wrapper_size);+ __builtin_memset(block, stale_byte, wrapper_size);+ stale_blocks.append(block);+ }+ for (auto* block : stale_blocks)+ kfree(block);++ Function<Captures const*()> function = [captures = Captures { true, CopyOnly(instance_count) }]() {+ return &captures;+ };+ auto const* captures = function();+ EXPECT_EQ(captures->numbers[5], 6u);++ // Check just the padding, since the reference's bytes are an address, and any of them could be the stale byte.+ auto const* start = reinterpret_cast<u8 const*>(captures);+ auto padding_size = static_cast<size_t>(reinterpret_cast<u8 const*>(&captures->counted) - start) - sizeof(bool);+ EXPECT(padding_size > 0);+ EXPECT(!capture_range_holds({ start + sizeof(bool), padding_size }, stale_byte));+}