From 83d36af187f4d57d88403b62a3fd740296addc8b Mon Sep 17 00:00:00 2001 From: Brandon DeRosier Date: Fri, 6 Dec 2024 15:26:31 -0800 Subject: [PATCH] Cover offset+bounds wrapping in the APNG frame region check. (flutter/engine#57025) The `offset + bounds` calculation in the bounds checks could wrap around, bypassing the check. (Follow up to https://github.com/flutter/engine/pull/56928) --- .../ui/fixtures/out_of_bounds_wrapping.apng | Bin 0 -> 126 bytes .../lib/ui/painting/image_generator_apng.cc | 10 ++++++++- .../src/flutter/testing/dart/codec_test.dart | 20 ++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 engine/src/flutter/lib/ui/fixtures/out_of_bounds_wrapping.apng diff --git a/engine/src/flutter/lib/ui/fixtures/out_of_bounds_wrapping.apng b/engine/src/flutter/lib/ui/fixtures/out_of_bounds_wrapping.apng new file mode 100644 index 0000000000000000000000000000000000000000..e4cc5d50cfe5f69ba28d6b3676aae19df9e21b8e GIT binary patch literal 126 zcmeAS@N?(olHy`uVBq!ia0y~yU|<4a4mJh`hN5k}Dhvz^9Er&xJ`4;Dj39hi>!uk4 z1A|l=M2rEX7>XJG|NsA=ff1zcN6C^51_lOhPZ!6KiaE&%4(tpJLJW-MAAf-~FnGH9 KxvX + if ( + // Check for unsigned integer wrapping for + // frame.{x|y}_offset + frame_info.{width|height}(). + frame.x_offset > + std::numeric_limits::max() - frame_info.width() || + frame.y_offset > + std::numeric_limits::max() - frame_info.height() || + + frame.x_offset + frame_info.width() > static_cast(info.width()) || frame.y_offset + frame_info.height() > static_cast(info.height())) { diff --git a/engine/src/flutter/testing/dart/codec_test.dart b/engine/src/flutter/testing/dart/codec_test.dart index 197369c609..7d0a2a23d2 100644 --- a/engine/src/flutter/testing/dart/codec_test.dart +++ b/engine/src/flutter/testing/dart/codec_test.dart @@ -272,6 +272,26 @@ void main() { } } }); + + test( + 'Animated apng frame decode does not crash with invalid destination region and bounds wrapping', + () async { + final Uint8List data = File( + path.join('flutter', 'lib', 'ui', 'fixtures', 'out_of_bounds_wrapping.apng'), + ).readAsBytesSync(); + + final ui.Codec codec = await ui.instantiateImageCodec(data); + try { + await codec.getNextFrame(); + fail('exception not thrown'); + } on Exception catch (e) { + if (impellerEnabled) { + expect(e.toString(), contains('Could not decompress image.')); + } else { + expect(e.toString(), contains('Codec failed')); + } + } + }); } /// Returns a File handle to a file in the skia/resources directory.