From 82e20c2da974f5eeceb06ef92b2a404b7a74ce47 Mon Sep 17 00:00:00 2001 From: Greg Daniel Date: Mon, 4 May 2026 16:02:40 +0000 Subject: [PATCH] Fix potential integer overflows in SurfaceContext using SkSafeMath Bug: https://issues.chromium.org/issues/500505046 Change-Id: I68462f3a73f2e1491151c7b9c5fc68fadb9d04a6 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/1223518 Commit-Queue: Greg Daniel Reviewed-by: Michael Ludwig --- src/gpu/ganesh/SurfaceContext.cpp | 33 ++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/gpu/ganesh/SurfaceContext.cpp b/src/gpu/ganesh/SurfaceContext.cpp index 6eaa922ff4..c6fa47fbca 100644 --- a/src/gpu/ganesh/SurfaceContext.cpp +++ b/src/gpu/ganesh/SurfaceContext.cpp @@ -26,6 +26,7 @@ #include "include/private/base/SkTemplates.h" #include "include/private/base/SkTo.h" #include "include/private/gpu/ganesh/GrTypesPriv.h" +#include "src/base/SkSafeMath.h" #include "src/core/SkColorSpaceXformSteps.h" #include "src/core/SkMipmap.h" #include "src/core/SkTraceEvent.h" @@ -276,7 +277,11 @@ bool SurfaceContext::readPixels(GrDirectContext* dContext, GrPixmap dst, SkIPoin this->colorInfo().refColorSpace(), dst.dimensions()); size_t tmpRB = tmpInfo.minRowBytes(); - size_t size = tmpRB * tmpInfo.height(); + SkSafeMath safe; + size_t size = safe.mul(tmpRB, tmpInfo.height()); + if (!safe.ok()) { + return false; + } // Chrome MSAN bots require the data to be initialized (hence the ()). tmpPixels = std::make_unique(size); tmp = {tmpInfo, tmpPixels.get(), tmpRB}; @@ -536,16 +541,24 @@ bool SurfaceContext::internalWritePixels(GrDirectContext* dContext, bool mustBeTight = !caps->writePixelsRowBytesSupport(); size_t tmpSize = 0; if (mustBeTight || convertAll) { + SkSafeMath safe; for (int i = 0; i < numLevels; ++i) { if (convertAll || (mustBeTight && src[i].rowBytes() != src[i].info().minRowBytes())) { - tmpSize += src[i].info().makeColorType(allowedColorType).minRowBytes()* - src[i].height(); + size_t minRowBytes = src[i].info().makeColorType(allowedColorType).minRowBytes(); + size_t levelSize = safe.mul(minRowBytes, src[i].height()); + tmpSize = safe.add(tmpSize, levelSize); } } + if (!safe.ok()) { + return false; + } } auto tmpData = tmpSize ? SkData::MakeUninitialized(tmpSize) : nullptr; - void* tmp = tmpSize ? tmpData->writable_data() : nullptr; + if (tmpSize && !tmpData) { + return false; + } + void* tmp = tmpSize ? tmpData->writable_data() : nullptr; AutoSTArray<15, GrMipLevel> srcLevels(numLevels); bool ownAllStorage = true; for (int i = 0; i < numLevels; ++i) { @@ -1353,9 +1366,15 @@ SurfaceContext::PixelTransferResult SurfaceContext::transferPixels(GrColorType d return {}; } - size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width(); - rowBytes = SkAlignTo(rowBytes, this->caps()->transferBufferRowBytesAlignment()); - size_t size = rowBytes * rect.height(); + SkSafeMath safe; + size_t bytesPerPixel = GrColorTypeBytesPerPixel(supportedRead.fColorType); + size_t rowBytes = safe.mul(bytesPerPixel, rect.width()); + size_t maxTransAlignment = this->caps()->transferBufferRowBytesAlignment(); + rowBytes = safe.alignUp(rowBytes, maxTransAlignment); + size_t size = safe.mul(rowBytes, rect.height()); + if (!safe.ok()) { + return {}; + } // By using kStream_GrAccessPattern here, we are not able to cache and reuse the buffer for // multiple reads. Switching to kDynamic_GrAccessPattern would allow for this, however doing // so causes a crash in a chromium test. See skbug.com/40042671 -- 2.53.0