| 1 |
From 82e20c2da974f5eeceb06ef92b2a404b7a74ce47 Mon Sep 17 00:00:00 2001 |
| 2 |
From: Greg Daniel <[email protected]> |
| 3 |
Date: Mon, 4 May 2026 16:02:40 +0000 |
| 4 |
Subject: [PATCH] Fix potential integer overflows in SurfaceContext using |
| 5 |
SkSafeMath |
| 6 |
|
| 7 |
Bug: https://issues.chromium.org/issues/500505046 |
| 8 |
Change-Id: I68462f3a73f2e1491151c7b9c5fc68fadb9d04a6 |
| 9 |
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/1223518 |
| 10 |
Commit-Queue: Greg Daniel <[email protected]> |
| 11 |
Reviewed-by: Michael Ludwig <[email protected]> |
| 12 |
--- |
| 13 |
src/gpu/ganesh/SurfaceContext.cpp | 33 ++++++++++++++++++++++++------- |
| 14 |
1 file changed, 26 insertions(+), 7 deletions(-) |
| 15 |
|
| 16 |
diff --git a/src/gpu/ganesh/SurfaceContext.cpp b/src/gpu/ganesh/SurfaceContext.cpp |
| 17 |
index 6eaa922ff4..c6fa47fbca 100644 |
| 18 |
--- a/src/gpu/ganesh/SurfaceContext.cpp |
| 19 |
+++ b/src/gpu/ganesh/SurfaceContext.cpp |
| 20 |
@@ -26,6 +26,7 @@ |
| 21 |
#include "include/private/base/SkTemplates.h" |
| 22 |
#include "include/private/base/SkTo.h" |
| 23 |
#include "include/private/gpu/ganesh/GrTypesPriv.h" |
| 24 |
+#include "src/base/SkSafeMath.h" |
| 25 |
#include "src/core/SkColorSpaceXformSteps.h" |
| 26 |
#include "src/core/SkMipmap.h" |
| 27 |
#include "src/core/SkTraceEvent.h" |
| 28 |
@@ -276,7 +277,11 @@ bool SurfaceContext::readPixels(GrDirectContext* dContext, GrPixmap dst, SkIPoin |
| 29 |
this->colorInfo().refColorSpace(), |
| 30 |
dst.dimensions()); |
| 31 |
size_t tmpRB = tmpInfo.minRowBytes(); |
| 32 |
- size_t size = tmpRB * tmpInfo.height(); |
| 33 |
+ SkSafeMath safe; |
| 34 |
+ size_t size = safe.mul(tmpRB, tmpInfo.height()); |
| 35 |
+ if (!safe.ok()) { |
| 36 |
+ return false; |
| 37 |
+ } |
| 38 |
// Chrome MSAN bots require the data to be initialized (hence the ()). |
| 39 |
tmpPixels = std::make_unique<char[]>(size); |
| 40 |
tmp = {tmpInfo, tmpPixels.get(), tmpRB}; |
| 41 |
@@ -536,16 +541,24 @@ bool SurfaceContext::internalWritePixels(GrDirectContext* dContext, |
| 42 |
bool mustBeTight = !caps->writePixelsRowBytesSupport(); |
| 43 |
size_t tmpSize = 0; |
| 44 |
if (mustBeTight || convertAll) { |
| 45 |
+ SkSafeMath safe; |
| 46 |
for (int i = 0; i < numLevels; ++i) { |
| 47 |
if (convertAll || (mustBeTight && src[i].rowBytes() != src[i].info().minRowBytes())) { |
| 48 |
- tmpSize += src[i].info().makeColorType(allowedColorType).minRowBytes()* |
| 49 |
- src[i].height(); |
| 50 |
+ size_t minRowBytes = src[i].info().makeColorType(allowedColorType).minRowBytes(); |
| 51 |
+ size_t levelSize = safe.mul(minRowBytes, src[i].height()); |
| 52 |
+ tmpSize = safe.add(tmpSize, levelSize); |
| 53 |
} |
| 54 |
} |
| 55 |
+ if (!safe.ok()) { |
| 56 |
+ return false; |
| 57 |
+ } |
| 58 |
} |
| 59 |
|
| 60 |
auto tmpData = tmpSize ? SkData::MakeUninitialized(tmpSize) : nullptr; |
| 61 |
- void* tmp = tmpSize ? tmpData->writable_data() : nullptr; |
| 62 |
+ if (tmpSize && !tmpData) { |
| 63 |
+ return false; |
| 64 |
+ } |
| 65 |
+ void* tmp = tmpSize ? tmpData->writable_data() : nullptr; |
| 66 |
AutoSTArray<15, GrMipLevel> srcLevels(numLevels); |
| 67 |
bool ownAllStorage = true; |
| 68 |
for (int i = 0; i < numLevels; ++i) { |
| 69 |
@@ -1353,9 +1366,15 @@ SurfaceContext::PixelTransferResult SurfaceContext::transferPixels(GrColorType d |
| 70 |
return {}; |
| 71 |
} |
| 72 |
|
| 73 |
- size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width(); |
| 74 |
- rowBytes = SkAlignTo(rowBytes, this->caps()->transferBufferRowBytesAlignment()); |
| 75 |
- size_t size = rowBytes * rect.height(); |
| 76 |
+ SkSafeMath safe; |
| 77 |
+ size_t bytesPerPixel = GrColorTypeBytesPerPixel(supportedRead.fColorType); |
| 78 |
+ size_t rowBytes = safe.mul(bytesPerPixel, rect.width()); |
| 79 |
+ size_t maxTransAlignment = this->caps()->transferBufferRowBytesAlignment(); |
| 80 |
+ rowBytes = safe.alignUp(rowBytes, maxTransAlignment); |
| 81 |
+ size_t size = safe.mul(rowBytes, rect.height()); |
| 82 |
+ if (!safe.ok()) { |
| 83 |
+ return {}; |
| 84 |
+ } |
| 85 |
// By using kStream_GrAccessPattern here, we are not able to cache and reuse the buffer for |
| 86 |
// multiple reads. Switching to kDynamic_GrAccessPattern would allow for this, however doing |
| 87 |
// so causes a crash in a chromium test. See skbug.com/40042671 |
| 88 |
-- |
| 89 |
2.53.0 |
| 90 |
|