| 1 |
From 014a77e8a6cd4abbd5952fd4a1815ec3a521f300 Mon Sep 17 00:00:00 2001 |
| 2 |
From: Arthur Sonzogni <[email protected]> |
| 3 |
Date: Fri, 3 Jul 2026 15:03:41 +0000 |
| 4 |
Subject: [PATCH] Fix Use-After-Free in SubRunAllocator |
| 5 |
|
| 6 |
The destruction order of `std::tuple` members is not specified by the |
| 7 |
C++ standard. This is the root cause of a Use-After-Free (UAF) in |
| 8 |
SubRunAllocator. Replacing the tuple with a custom struct resolves the |
| 9 |
issue by guaranteeing the correct destruction order. |
| 10 |
|
| 11 |
The Bug: |
| 12 |
During deserialization of a Slug (specifically in |
| 13 |
SlugImpl::MakeFromBuffer), if the input buffer is invalid or corrupted, |
| 14 |
Skia detects this and returns nullptr early. |
| 15 |
|
| 16 |
This early return destroys the temporary return value. In the old code |
| 17 |
(using `std::tuple`), `SubRunInitializer` (index 0) was destructed |
| 18 |
first and freed the backing memory. |
| 19 |
|
| 20 |
`SubRunAllocator` (index 2) was destructed next. Its destructor |
| 21 |
(~BagOfBytes) then attempted to access fEndByte (which points inside |
| 22 |
the freed memory), resulting in a UAF (read) followed by a wild-free |
| 23 |
or double-free. |
| 24 |
|
| 25 |
The Fix: |
| 26 |
We replaced the `std::tuple` with a custom helper struct |
| 27 |
`AllocateAndArenaResult`: |
| 28 |
|
| 29 |
struct AllocateAndArenaResult { |
| 30 |
SubRunInitializer<T> initializer; // Destructed last |
| 31 |
int totalMemorySize; |
| 32 |
SubRunAllocator alloc; // Destructed first |
| 33 |
}; |
| 34 |
|
| 35 |
Since struct members are guaranteed to be destructed in the reverse |
| 36 |
order of their declaration, declaring `alloc` last guarantees it is |
| 37 |
destructed before `SubRunInitializer` frees the memory. |
| 38 |
|
| 39 |
Additionally, this CL refactors `SubRunInitializer` to use |
| 40 |
`std::unique_ptr` with a custom deleter to manage the raw memory, |
| 41 |
removing the need for a manual destructor and making the ownership |
| 42 |
transfer explicit via `release()`. |
| 43 |
|
| 44 |
Bug: https://issues.chromium.org/issues/530646115 |
| 45 |
Change-Id: I80cba4fdb9eebfe16e5ec837d70b5646422fbcff |
| 46 |
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/1284796 |
| 47 |
Commit-Queue: Kaylee Lubick <[email protected]> |
| 48 |
Reviewed-by: Kaylee Lubick <[email protected]> |
| 49 |
--- |
| 50 |
src/text/gpu/SubRunAllocator.h | 67 +++++++++++++++++++++++----------- |
| 51 |
1 file changed, 45 insertions(+), 22 deletions(-) |
| 52 |
|
| 53 |
diff --git a/src/text/gpu/SubRunAllocator.h b/src/text/gpu/SubRunAllocator.h |
| 54 |
index 921f0f7ec5..dd5c9d74e7 100644 |
| 55 |
--- a/src/text/gpu/SubRunAllocator.h |
| 56 |
+++ b/src/text/gpu/SubRunAllocator.h |
| 57 |
@@ -188,23 +188,30 @@ private: |
| 58 |
template <typename T> |
| 59 |
class SubRunInitializer { |
| 60 |
public: |
| 61 |
- SubRunInitializer(void* memory) : fMemory{memory} { SkASSERT(memory != nullptr); } |
| 62 |
- ~SubRunInitializer() { |
| 63 |
- ::operator delete(fMemory); |
| 64 |
- } |
| 65 |
+ explicit SubRunInitializer(void* memory) : fMemory{memory} { SkASSERT(memory != nullptr); } |
| 66 |
+ |
| 67 |
template <typename... Args> |
| 68 |
T* initialize(Args&&... args) { |
| 69 |
// Warn on more than one initialization. |
| 70 |
SkASSERT(fMemory != nullptr); |
| 71 |
- return new (std::exchange(fMemory, nullptr)) T(std::forward<Args>(args)...); |
| 72 |
+ return new (fMemory.release()) T(std::forward<Args>(args)...); |
| 73 |
} |
| 74 |
|
| 75 |
private: |
| 76 |
- void* fMemory; |
| 77 |
+ struct Deleter { |
| 78 |
+ // Frees the heap memory without calling a destructor. |
| 79 |
+ // We must use `::operator delete(p)` instead of `delete p` because `p` is `void*`. |
| 80 |
+ // Deleting a `void*` is undefined behavior in C++. |
| 81 |
+ // This is paired with the placement `::operator new` in AllocateClassMemoryAndArena. |
| 82 |
+ void operator()(void* p) const { ::operator delete(p); } |
| 83 |
+ }; |
| 84 |
+ std::unique_ptr<void, Deleter> fMemory; |
| 85 |
}; |
| 86 |
|
| 87 |
-// GrSubRunAllocator provides fast allocation where the user takes care of calling the destructors |
| 88 |
-// of the returned pointers, and GrSubRunAllocator takes care of deleting the storage. The |
| 89 |
+template <typename T> struct AllocateAndArenaResult; |
| 90 |
+ |
| 91 |
+// SubRunAllocator provides fast allocation where the user takes care of calling the destructors |
| 92 |
+// of the returned pointers, and SubRunAllocator takes care of deleting the storage. The |
| 93 |
// unique_ptrs returned, are to assist in assuring the object's destructor is called. |
| 94 |
// A note on zero length arrays: according to the standard a pointer must be returned, and it |
| 95 |
// can't be a nullptr. In such a case, SkArena allocates one byte, but does not initialize it. |
| 96 |
@@ -234,20 +241,8 @@ public: |
| 97 |
SubRunAllocator& operator=(SubRunAllocator&&) = default; |
| 98 |
|
| 99 |
template <typename T> |
| 100 |
- static std::tuple<SubRunInitializer<T>, int, SubRunAllocator> |
| 101 |
- AllocateClassMemoryAndArena(int allocSizeHint) { |
| 102 |
- SkASSERT_RELEASE(allocSizeHint >= 0); |
| 103 |
- // Round the size after the object the optimal amount. |
| 104 |
- int extraSize = BagOfBytes::PlatformMinimumSizeWithOverhead(allocSizeHint, alignof(T)); |
| 105 |
- |
| 106 |
- // Don't overflow or die. |
| 107 |
- SkASSERT_RELEASE(INT_MAX - SkTo<int>(sizeof(T)) > extraSize); |
| 108 |
- int totalMemorySize = sizeof(T) + extraSize; |
| 109 |
- |
| 110 |
- void* memory = ::operator new (totalMemorySize); |
| 111 |
- SubRunAllocator alloc{SkTAddOffset<char>(memory, sizeof(T)), extraSize, extraSize/2}; |
| 112 |
- return {memory, totalMemorySize, std::move(alloc)}; |
| 113 |
- } |
| 114 |
+ static AllocateAndArenaResult<T> |
| 115 |
+ AllocateClassMemoryAndArena(int allocSizeHint); |
| 116 |
|
| 117 |
template <typename T, typename... Args> T* makePOD(Args&&... args) { |
| 118 |
static_assert(HasNoDestructor<T>, "This is not POD. Use makeUnique."); |
| 119 |
@@ -328,6 +323,34 @@ private: |
| 120 |
BagOfBytes fAlloc; |
| 121 |
}; |
| 122 |
|
| 123 |
+// Members are destroyed in the reverse order of their declaration: |
| 124 |
+// https://isocpp.org/wiki/faq/dtors#order-dtors-for-members |
| 125 |
+// `alloc` must be destroyed first because it may contain pointers to memory owned by `initializer`. |
| 126 |
+// `initializer` must be destroyed last because it owns the backing memory. |
| 127 |
+// See also: https://issues.skia.org/issues/530646115 |
| 128 |
+template <typename T> |
| 129 |
+struct AllocateAndArenaResult { |
| 130 |
+ SubRunInitializer<T> initializer; |
| 131 |
+ int totalMemorySize; |
| 132 |
+ SubRunAllocator alloc; |
| 133 |
+}; |
| 134 |
+ |
| 135 |
+template <typename T> |
| 136 |
+inline AllocateAndArenaResult<T> |
| 137 |
+SubRunAllocator::AllocateClassMemoryAndArena(int allocSizeHint) { |
| 138 |
+ SkASSERT_RELEASE(allocSizeHint >= 0); |
| 139 |
+ // Round the size after the object the optimal amount. |
| 140 |
+ int extraSize = BagOfBytes::PlatformMinimumSizeWithOverhead(allocSizeHint, alignof(T)); |
| 141 |
+ |
| 142 |
+ // Don't overflow or die. |
| 143 |
+ SkASSERT_RELEASE(INT_MAX - SkTo<int>(sizeof(T)) > extraSize); |
| 144 |
+ int totalMemorySize = sizeof(T) + extraSize; |
| 145 |
+ |
| 146 |
+ void* memory = ::operator new (totalMemorySize); |
| 147 |
+ SubRunAllocator alloc{SkTAddOffset<char>(memory, sizeof(T)), extraSize, extraSize/2}; |
| 148 |
+ return {SubRunInitializer<T>{memory}, totalMemorySize, std::move(alloc)}; |
| 149 |
+} |
| 150 |
+ |
| 151 |
// Helper for defining allocators with inline/reserved storage. |
| 152 |
// For argument declarations, stick to the base type (SubRunAllocator). |
| 153 |
// Note: Inheriting from the storage first means the storage will outlive the |
| 154 |
-- |
| 155 |
2.53.0 |
| 156 |
|