Skip to content

Commit 0208108

Browse files
ArthurSonzogniskcq-be@skia-corp.google.com.iam.gserviceaccount.com
authored andcommitted
[M151] Fix Use-After-Free in SubRunAllocator
Original change's description: > Fix Use-After-Free in SubRunAllocator > > The destruction order of `std::tuple` members is not specified by the > C++ standard. This is the root cause of a Use-After-Free (UAF) in > SubRunAllocator. Replacing the tuple with a custom struct resolves the > issue by guaranteeing the correct destruction order. > > The Bug: > During deserialization of a Slug (specifically in > SlugImpl::MakeFromBuffer), if the input buffer is invalid or corrupted, > Skia detects this and returns nullptr early. > > This early return destroys the temporary return value. In the old code > (using `std::tuple`), `SubRunInitializer` (index 0) was destructed > first and freed the backing memory. > > `SubRunAllocator` (index 2) was destructed next. Its destructor > (~BagOfBytes) then attempted to access fEndByte (which points inside > the freed memory), resulting in a UAF (read) followed by a wild-free > or double-free. > > The Fix: > We replaced the `std::tuple` with a custom helper struct > `AllocateAndArenaResult`: > > struct AllocateAndArenaResult { > SubRunInitializer<T> initializer; // Destructed last > int totalMemorySize; > SubRunAllocator alloc; // Destructed first > }; > > Since struct members are guaranteed to be destructed in the reverse > order of their declaration, declaring `alloc` last guarantees it is > destructed before `SubRunInitializer` frees the memory. > > Additionally, this CL refactors `SubRunInitializer` to use > `std::unique_ptr` with a custom deleter to manage the raw memory, > removing the need for a manual destructor and making the ownership > transfer explicit via `release()`. > > Bug: skia:530646115 > Change-Id: I80cba4fdb9eebfe16e5ec837d70b5646422fbcff > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/1284796 > Commit-Queue: Kaylee Lubick <[email protected]> > Reviewed-by: Kaylee Lubick <[email protected]> (cherry picked from commit 014a77e) Bug: 532103445,skia:530646115 Change-Id: I80cba4fdb9eebfe16e5ec837d70b5646422fbcff Reviewed-on: https://skia-review.googlesource.com/c/skia/+/1288616 Bot-Commit: [email protected] <[email protected]> Auto-Submit: Chrome Cherry Picker <chrome-cherry-picker@chops-service-accounts.iam.gserviceaccount.com> Commit-Queue: [email protected] <[email protected]>
1 parent 1536d39 commit 0208108

1 file changed

Lines changed: 45 additions & 22 deletions

File tree

‎src/text/gpu/SubRunAllocator.h‎

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -188,23 +188,30 @@ class BagOfBytes {
188188
template <typename T>
189189
class SubRunInitializer {
190190
public:
191-
SubRunInitializer(void* memory) : fMemory{memory} { SkASSERT(memory != nullptr); }
192-
~SubRunInitializer() {
193-
::operator delete(fMemory);
194-
}
191+
explicit SubRunInitializer(void* memory) : fMemory{memory} { SkASSERT(memory != nullptr); }
192+
195193
template <typename... Args>
196194
T* initialize(Args&&... args) {
197195
// Warn on more than one initialization.
198196
SkASSERT(fMemory != nullptr);
199-
return new (std::exchange(fMemory, nullptr)) T(std::forward<Args>(args)...);
197+
return new (fMemory.release()) T(std::forward<Args>(args)...);
200198
}
201199

202200
private:
203-
void* fMemory;
201+
struct Deleter {
202+
// Frees the heap memory without calling a destructor.
203+
// We must use `::operator delete(p)` instead of `delete p` because `p` is `void*`.
204+
// Deleting a `void*` is undefined behavior in C++.
205+
// This is paired with the placement `::operator new` in AllocateClassMemoryAndArena.
206+
void operator()(void* p) const { ::operator delete(p); }
207+
};
208+
std::unique_ptr<void, Deleter> fMemory;
204209
};
205210

206-
// GrSubRunAllocator provides fast allocation where the user takes care of calling the destructors
207-
// of the returned pointers, and GrSubRunAllocator takes care of deleting the storage. The
211+
template <typename T> struct AllocateAndArenaResult;
212+
213+
// SubRunAllocator provides fast allocation where the user takes care of calling the destructors
214+
// of the returned pointers, and SubRunAllocator takes care of deleting the storage. The
208215
// unique_ptrs returned, are to assist in assuring the object's destructor is called.
209216
// A note on zero length arrays: according to the standard a pointer must be returned, and it
210217
// can't be a nullptr. In such a case, SkArena allocates one byte, but does not initialize it.
@@ -234,20 +241,8 @@ class SubRunAllocator {
234241
SubRunAllocator& operator=(SubRunAllocator&&) = default;
235242

236243
template <typename T>
237-
static std::tuple<SubRunInitializer<T>, int, SubRunAllocator>
238-
AllocateClassMemoryAndArena(int allocSizeHint) {
239-
SkASSERT_RELEASE(allocSizeHint >= 0);
240-
// Round the size after the object the optimal amount.
241-
int extraSize = BagOfBytes::PlatformMinimumSizeWithOverhead(allocSizeHint, alignof(T));
242-
243-
// Don't overflow or die.
244-
SkASSERT_RELEASE(INT_MAX - SkTo<int>(sizeof(T)) > extraSize);
245-
int totalMemorySize = sizeof(T) + extraSize;
246-
247-
void* memory = ::operator new (totalMemorySize);
248-
SubRunAllocator alloc{SkTAddOffset<char>(memory, sizeof(T)), extraSize, extraSize/2};
249-
return {memory, totalMemorySize, std::move(alloc)};
250-
}
244+
static AllocateAndArenaResult<T>
245+
AllocateClassMemoryAndArena(int allocSizeHint);
251246

252247
template <typename T, typename... Args> T* makePOD(Args&&... args) {
253248
static_assert(HasNoDestructor<T>, "This is not POD. Use makeUnique.");
@@ -328,6 +323,34 @@ class SubRunAllocator {
328323
BagOfBytes fAlloc;
329324
};
330325

326+
// Members are destroyed in the reverse order of their declaration:
327+
// https://isocpp.org/wiki/faq/dtors#order-dtors-for-members
328+
// `alloc` must be destroyed first because it may contain pointers to memory owned by `initializer`.
329+
// `initializer` must be destroyed last because it owns the backing memory.
330+
// See also: https://issues.skia.org/issues/530646115
331+
template <typename T>
332+
struct AllocateAndArenaResult {
333+
SubRunInitializer<T> initializer;
334+
int totalMemorySize;
335+
SubRunAllocator alloc;
336+
};
337+
338+
template <typename T>
339+
inline AllocateAndArenaResult<T>
340+
SubRunAllocator::AllocateClassMemoryAndArena(int allocSizeHint) {
341+
SkASSERT_RELEASE(allocSizeHint >= 0);
342+
// Round the size after the object the optimal amount.
343+
int extraSize = BagOfBytes::PlatformMinimumSizeWithOverhead(allocSizeHint, alignof(T));
344+
345+
// Don't overflow or die.
346+
SkASSERT_RELEASE(INT_MAX - SkTo<int>(sizeof(T)) > extraSize);
347+
int totalMemorySize = sizeof(T) + extraSize;
348+
349+
void* memory = ::operator new (totalMemorySize);
350+
SubRunAllocator alloc{SkTAddOffset<char>(memory, sizeof(T)), extraSize, extraSize/2};
351+
return {SubRunInitializer<T>{memory}, totalMemorySize, std::move(alloc)};
352+
}
353+
331354
// Helper for defining allocators with inline/reserved storage.
332355
// For argument declarations, stick to the base type (SubRunAllocator).
333356
// Note: Inheriting from the storage first means the storage will outlive the

0 commit comments

Comments
 (0)