Skip to content

Commit 1a050c2

Browse files
committed
imageio: Fix vulnerabilities in PSDDecoder from PR #9881
- PSDDecoder: Add dimension limits (max 30,000) to prevent integer overflows during LinearImage allocation. - Add checks for stream failure during pixel decoding to handle truncated files safely. - Follow existing pattern in this file by using std::runtime_error for these checks, which are caught and handled by resetting the stream and returning an empty image. - Add comment that LinearImage allocation cannot overflow with the new limits.
1 parent f0c3d82 commit 1a050c2

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

libs/imageio/src/ImageDecoder.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
# include <arpa/inet.h>
3636
#endif
3737

38+
#include <utils/Log.h>
3839
#include <math/vec3.h>
3940
#include <math/vec4.h>
4041

@@ -373,6 +374,12 @@ LinearImage PSDDecoder::decode() {
373374
uint32_t width = ntohl(h.width);
374375
uint32_t height = ntohl(h.height);
375376

377+
// Limit dimensions to 30,000 as per PSD specification to prevent integer overflows
378+
// during allocation in LinearImage.
379+
if (width == 0 || height == 0 || width > 30000 || height > 30000) {
380+
throw std::runtime_error("PSD dimensions exceed maximum allowed size (30,000)");
381+
}
382+
376383
uint32_t length;
377384

378385
// color mode data section
@@ -394,6 +401,8 @@ LinearImage PSDDecoder::decode() {
394401
throw std::runtime_error("compressed images are not supported");
395402
}
396403

404+
// The multiplication width * height * 3 cannot overflow uint32_t because
405+
// width and height are at most 30,000. (30000 * 30000 * 3 = 2.7e9 < 4.29e9)
397406
LinearImage image(width, height, 3);
398407

399408
if (depth == 32) {
@@ -403,6 +412,9 @@ LinearImage PSDDecoder::decode() {
403412
filament::math::float3& pixel =
404413
*reinterpret_cast< filament::math::float3*>(image.getPixelRef(x, y));
405414
pixel[i] = read32(mStream);
415+
if (!mStream.good()) {
416+
throw std::runtime_error("Truncated PSD file");
417+
}
406418
}
407419
}
408420
}
@@ -413,6 +425,9 @@ LinearImage PSDDecoder::decode() {
413425
filament::math::float3& pixel =
414426
*reinterpret_cast< filament::math::float3*>(image.getPixelRef(x, y));
415427
pixel[i] = read16(mStream);
428+
if (!mStream.good()) {
429+
throw std::runtime_error("Truncated PSD file");
430+
}
416431
}
417432
}
418433
}

0 commit comments

Comments
 (0)