| 1 |
From 8c705ac8636616101a502587df5019e0d16b8fec Mon Sep 17 00:00:00 2001 |
| 2 |
From: Stephen Nusko <[email protected]> |
| 3 |
Date: Wed, 25 Mar 2026 01:25:24 -0700 |
| 4 |
Subject: [PATCH] Use SkSafeMath to prevent overflow in pixel offset |
| 5 |
calculations. |
| 6 |
|
| 7 |
The trim methods in SkReadPixelsRec and SkWritePixelsRec now use |
| 8 |
SkSafeMath to calculate the offset for fPixels. This addresses potential |
| 9 |
integer overflows when computing the y and x offsets and their sum. |
| 10 |
Additionally, a check for fInfo.minRowBytes() == 0 is added, as |
| 11 |
minRowBytes() returns 0 on overflow. If any overflow occurs during |
| 12 |
offset calculation, trim will now return false. |
| 13 |
|
| 14 |
See linked bug for potential security issue that motivated this. |
| 15 |
|
| 16 |
And see (sorry internal only) go/code-terracotta-review-explainer for |
| 17 |
evaluting this phase (1) patch. |
| 18 |
|
| 19 |
Bug: https://issues.chromium.org/issues/495534710 |
| 20 |
Change-Id: I0b2a684b5ad1105c7d25418556e40b4d9f511daf |
| 21 |
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/1194336 |
| 22 |
Auto-Submit: Stephen Nusko <[email protected]> |
| 23 |
Commit-Queue: Stephen Nusko <[email protected]> |
| 24 |
Reviewed-by: Kaylee Lubick <[email protected]> |
| 25 |
Reviewed-by: Florin Malita <[email protected]> |
| 26 |
--- |
| 27 |
src/core/SkReadPixelsRec.cpp | 17 ++++++++++++++--- |
| 28 |
src/core/SkWritePixelsRec.cpp | 17 ++++++++++++++--- |
| 29 |
2 files changed, 28 insertions(+), 6 deletions(-) |
| 30 |
|
| 31 |
diff --git a/src/core/SkReadPixelsRec.cpp b/src/core/SkReadPixelsRec.cpp |
| 32 |
index 505bfb51b3..f7e9661629 100644 |
| 33 |
--- a/src/core/SkReadPixelsRec.cpp |
| 34 |
+++ b/src/core/SkReadPixelsRec.cpp |
| 35 |
@@ -7,9 +7,12 @@ |
| 36 |
#include "src/core/SkReadPixelsRec.h" |
| 37 |
|
| 38 |
#include "include/core/SkRect.h" |
| 39 |
+#include "src/base/SkSafeMath.h" |
| 40 |
|
| 41 |
bool SkReadPixelsRec::trim(int srcWidth, int srcHeight) { |
| 42 |
- if (nullptr == fPixels || fRowBytes < fInfo.minRowBytes()) { |
| 43 |
+ // fInfo.minRowBytes() returns 0 if the size doesn't fit in `size_t`. |
| 44 |
+ const size_t minRowBytes = fInfo.minRowBytes(); |
| 45 |
+ if (nullptr == fPixels || fRowBytes < minRowBytes || minRowBytes == 0) { |
| 46 |
return false; |
| 47 |
} |
| 48 |
if (0 >= fInfo.width() || 0 >= fInfo.height()) { |
| 49 |
@@ -30,9 +33,17 @@ bool SkReadPixelsRec::trim(int srcWidth, int srcHeight) { |
| 50 |
if (y > 0) { |
| 51 |
y = 0; |
| 52 |
} |
| 53 |
- // here x,y are either 0 or negative |
| 54 |
+ // here x,y are either 0 or negative (safe to cast to size_t) |
| 55 |
// we negate and add them so UBSAN (pointer-overflow) doesn't get confused. |
| 56 |
- fPixels = ((char*)fPixels + -y*fRowBytes + -x*fInfo.bytesPerPixel()); |
| 57 |
+ SkSafeMath safeMath; |
| 58 |
+ const size_t y_offset = safeMath.mul(-y, fRowBytes); |
| 59 |
+ const size_t x_offset = safeMath.mul(-x, fInfo.bytesPerPixel()); |
| 60 |
+ const size_t total = safeMath.add(y_offset, x_offset); |
| 61 |
+ if (!safeMath.ok()) { |
| 62 |
+ return false; |
| 63 |
+ } |
| 64 |
+ |
| 65 |
+ fPixels = ((char*)fPixels + total); |
| 66 |
// the intersect may have shrunk info's logical size |
| 67 |
fInfo = fInfo.makeDimensions(srcR.size()); |
| 68 |
fX = srcR.x(); |
| 69 |
diff --git a/src/core/SkWritePixelsRec.cpp b/src/core/SkWritePixelsRec.cpp |
| 70 |
index 20b2003f59..d1bad0f51e 100644 |
| 71 |
--- a/src/core/SkWritePixelsRec.cpp |
| 72 |
+++ b/src/core/SkWritePixelsRec.cpp |
| 73 |
@@ -8,9 +8,12 @@ |
| 74 |
#include "src/core/SkWritePixelsRec.h" |
| 75 |
|
| 76 |
#include "include/core/SkRect.h" |
| 77 |
+#include "src/base/SkSafeMath.h" |
| 78 |
|
| 79 |
bool SkWritePixelsRec::trim(int dstWidth, int dstHeight) { |
| 80 |
- if (nullptr == fPixels || fRowBytes < fInfo.minRowBytes()) { |
| 81 |
+ // fInfo.minRowBytes() returns 0 if the size doesn't fit in `size_t`. |
| 82 |
+ const size_t minRowBytes = fInfo.minRowBytes(); |
| 83 |
+ if (nullptr == fPixels || fRowBytes < minRowBytes || minRowBytes == 0) { |
| 84 |
return false; |
| 85 |
} |
| 86 |
if (0 >= fInfo.width() || 0 >= fInfo.height()) { |
| 87 |
@@ -31,9 +34,17 @@ bool SkWritePixelsRec::trim(int dstWidth, int dstHeight) { |
| 88 |
if (y > 0) { |
| 89 |
y = 0; |
| 90 |
} |
| 91 |
- // here x,y are either 0 or negative |
| 92 |
+ // here x,y are either 0 or negative (safe to cast to size_t) |
| 93 |
// we negate and add them so UBSAN (pointer-overflow) doesn't get confused. |
| 94 |
- fPixels = ((const char*)fPixels + -y*fRowBytes + -x*fInfo.bytesPerPixel()); |
| 95 |
+ SkSafeMath safeMath; |
| 96 |
+ const size_t y_offset = safeMath.mul(-y, fRowBytes); |
| 97 |
+ const size_t x_offset = safeMath.mul(-x, fInfo.bytesPerPixel()); |
| 98 |
+ const size_t total = safeMath.add(y_offset, x_offset); |
| 99 |
+ if (!safeMath.ok()) { |
| 100 |
+ return false; |
| 101 |
+ } |
| 102 |
+ |
| 103 |
+ fPixels = ((const char*)fPixels + total); |
| 104 |
// the intersect may have shrunk info's logical size |
| 105 |
fInfo = fInfo.makeDimensions(dstR.size()); |
| 106 |
fX = dstR.x(); |
| 107 |
-- |
| 108 |
2.53.0 |
| 109 |
|