| 1 |
From 4320748aa7d3dc82b7064b23625a78d621f21963 Mon Sep 17 00:00:00 2001 |
| 2 |
From: Kaylee Lubick <[email protected]> |
| 3 |
Date: Mon, 30 Mar 2026 08:44:58 -0700 |
| 4 |
Subject: [PATCH] Validate sizes in mskp reading and SkTableMaskFilter |
| 5 |
|
| 6 |
The max dimension for mskps is approximately the sqrt of INT32_MAX |
| 7 |
which felt "big enough" |
| 8 |
|
| 9 |
Bug: https://issues.chromium.org/issues/496526419 |
| 10 |
Change-Id: I6850c28e18f7427d85ea0ea724c5d056a4682970 |
| 11 |
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/1198616 |
| 12 |
Commit-Queue: Kaylee Lubick <[email protected]> |
| 13 |
Reviewed-by: Jorge Betancourt <[email protected]> |
| 14 |
--- |
| 15 |
src/effects/SkTableMaskFilter.cpp | 12 ++++++++++-- |
| 16 |
src/utils/SkMultiPictureDocument.cpp | 18 +++++++++++++----- |
| 17 |
2 files changed, 23 insertions(+), 7 deletions(-) |
| 18 |
|
| 19 |
diff --git a/src/effects/SkTableMaskFilter.cpp b/src/effects/SkTableMaskFilter.cpp |
| 20 |
index d422ce02d4..ad423727cf 100644 |
| 21 |
--- a/src/effects/SkTableMaskFilter.cpp |
| 22 |
+++ b/src/effects/SkTableMaskFilter.cpp |
| 23 |
@@ -78,14 +78,22 @@ bool SkTableMaskFilterImpl::filterMask(SkMaskBuilder* dst, const SkMask& src, |
| 24 |
if (src.fFormat != SkMask::kA8_Format) { |
| 25 |
return false; |
| 26 |
} |
| 27 |
- |
| 28 |
+ // SkAlign4 overflows when too close to INT32_MAX, so reject when too big. |
| 29 |
+ constexpr int32_t kMaxWidth = 1 << 30; |
| 30 |
+ if (src.fBounds.width() > kMaxWidth) { |
| 31 |
+ return false; |
| 32 |
+ } |
| 33 |
dst->bounds() = src.fBounds; |
| 34 |
dst->rowBytes() = SkAlign4(dst->fBounds.width()); |
| 35 |
dst->format() = SkMask::kA8_Format; |
| 36 |
dst->image() = nullptr; |
| 37 |
|
| 38 |
if (src.fImage) { |
| 39 |
- dst->image() = SkMaskBuilder::AllocImage(dst->computeImageSize()); |
| 40 |
+ auto imgSize = dst->computeImageSize(); |
| 41 |
+ if (imgSize == 0) { |
| 42 |
+ return false; |
| 43 |
+ } |
| 44 |
+ dst->image() = SkMaskBuilder::AllocImage(imgSize); |
| 45 |
|
| 46 |
const uint8_t* srcP = src.fImage; |
| 47 |
uint8_t* dstP = dst->image(); |
| 48 |
diff --git a/src/utils/SkMultiPictureDocument.cpp b/src/utils/SkMultiPictureDocument.cpp |
| 49 |
index 5f327e6f70..72377d0eb6 100644 |
| 50 |
--- a/src/utils/SkMultiPictureDocument.cpp |
| 51 |
+++ b/src/utils/SkMultiPictureDocument.cpp |
| 52 |
@@ -50,7 +50,9 @@ static constexpr char kMagic[] = "Skia Multi-Picture Doc\n\n"; |
| 53 |
|
| 54 |
static constexpr char kEndPage[] = "SkMultiPictureEndPage"; |
| 55 |
|
| 56 |
-const uint32_t kVersion = 2; |
| 57 |
+static constexpr uint32_t kVersion = 2; |
| 58 |
+ |
| 59 |
+static constexpr uint32_t kMaxDimension = 1 << 16; |
| 60 |
|
| 61 |
static SkSize join(const TArray<SkSize>& sizes) { |
| 62 |
SkSize joined = {0, 0}; |
| 63 |
@@ -169,28 +171,34 @@ int ReadPageCount(SkStreamSeekable* src) { |
| 64 |
return 0; |
| 65 |
} |
| 66 |
uint32_t pageCount; |
| 67 |
- if (!src->readU32(&pageCount) || pageCount > INT_MAX) { |
| 68 |
+ if (!src->readU32(&pageCount) || pageCount > (INT_MAX / sizeof(SkSize))) { |
| 69 |
return 0; |
| 70 |
} |
| 71 |
// leave stream position right here. |
| 72 |
return SkTo<int>(pageCount); |
| 73 |
} |
| 74 |
|
| 75 |
-bool ReadPageSizes(SkStreamSeekable* stream, |
| 76 |
+bool ReadPageSizes(SkStreamSeekable* src, |
| 77 |
SkDocumentPage* dstArray, |
| 78 |
int dstArrayCount) { |
| 79 |
if (!dstArray || dstArrayCount < 1) { |
| 80 |
return false; |
| 81 |
} |
| 82 |
- int pageCount = ReadPageCount(stream); |
| 83 |
+ int pageCount = ReadPageCount(src); |
| 84 |
if (pageCount < 1 || pageCount != dstArrayCount) { |
| 85 |
return false; |
| 86 |
} |
| 87 |
+ if (src->hasLength() && src->getLength() < (static_cast<size_t>(pageCount) * sizeof(SkSize))) { |
| 88 |
+ return false; // not enough memory to read all the sizes |
| 89 |
+ } |
| 90 |
for (int i = 0; i < pageCount; ++i) { |
| 91 |
SkSize& s = dstArray[i].fSize; |
| 92 |
- if (sizeof(s) != stream->read(&s, sizeof(s))) { |
| 93 |
+ if (sizeof(s) != src->read(&s, sizeof(s))) { |
| 94 |
return false; |
| 95 |
} |
| 96 |
+ if (s.isEmpty() || s.width() >= kMaxDimension || s.height() >= kMaxDimension) { |
| 97 |
+ return false; // invalid sizes |
| 98 |
+ } |
| 99 |
} |
| 100 |
// leave stream position right here. |
| 101 |
return true; |
| 102 |
-- |
| 103 |
2.53.0 |
| 104 |
|