Skip to content

Commit eebc2c1

Browse files
committed
read: prevent integer overflow in image grid validation
Add explicit overflow checks before multiplying tile dimensions by grid rows/columns to avoid wraparound that could bypass validation and lead to malformed image handling. Reject unsafe grids early with AVIF_RESULT_INVALID_IMAGE_GRID.
1 parent 2a4a06f commit eebc2c1

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

src/read.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1737,12 +1737,29 @@ static avifResult avifDecoderDataAllocateImagePlanes(const avifDecoderData * dat
17371737
//
17381738
// HEIF (ISO/IEC 23008-12:2017), Section 6.6.2.3.1:
17391739
// The tiled input images shall completely "cover" the reconstructed image grid canvas, ...
1740+
1741+
// Check for integer overflow before performing multiplications
1742+
if ((tile->image->width > 0 && grid->columns > UINT32_MAX / tile->image->width) ||
1743+
(tile->image->height > 0 && grid->rows > UINT32_MAX / tile->image->height)) {
1744+
avifDiagnosticsPrintf(data->diag,
1745+
"Grid image dimensions would cause integer overflow");
1746+
return AVIF_RESULT_INVALID_IMAGE_GRID;
1747+
}
1748+
17401749
if (((tile->image->width * grid->columns) < grid->outputWidth) || ((tile->image->height * grid->rows) < grid->outputHeight)) {
17411750
avifDiagnosticsPrintf(data->diag,
17421751
"Grid image tiles do not completely cover the image (HEIF (ISO/IEC 23008-12:2017), Section 6.6.2.3.1)");
17431752
return AVIF_RESULT_INVALID_IMAGE_GRID;
17441753
}
17451754
// Tiles in the rightmost column and bottommost row must overlap the reconstructed image grid canvas. See MIAF (ISO/IEC 23000-22:2019), Section 7.3.11.4.2, Figure 2.
1755+
// Check for overflow in (columns - 1) and (rows - 1) multiplications
1756+
if ((tile->image->width > 0 && grid->columns > 1 && (grid->columns - 1) > UINT32_MAX / tile->image->width) ||
1757+
(tile->image->height > 0 && grid->rows > 1 && (grid->rows - 1) > UINT32_MAX / tile->image->height)) {
1758+
avifDiagnosticsPrintf(data->diag,
1759+
"Grid image dimensions would cause integer overflow");
1760+
return AVIF_RESULT_INVALID_IMAGE_GRID;
1761+
}
1762+
17461763
if (((tile->image->width * (grid->columns - 1)) >= grid->outputWidth) ||
17471764
((tile->image->height * (grid->rows - 1)) >= grid->outputHeight)) {
17481765
avifDiagnosticsPrintf(data->diag,

0 commit comments

Comments
 (0)