Skip to content

Commit a4ce257

Browse files
[CP-stable]Fix a potential buffer overflow in the animated PNG decoder when parsing malformed fdAT chunks (#188038)
### Issue Link: #183179 ### Impact Description: Potential crash when rendering animated PNG images. ### Changelog Description: Fixes an issue that can cause crashes when rendering animated PNG images. ### Workaround: Is there a workaround for this issue? No ### Risk: What is the risk level of this cherry-pick? ### Test Coverage: Are you confident that your fix is well-tested by automated tests? ### Validation Steps: Run the unit tests (`./ui_unittests --gtest_filter=APNGImageGeneratorTest.*`)
1 parent 0700fc3 commit a4ce257

4 files changed

Lines changed: 158 additions & 6 deletions

File tree

‎engine/src/flutter/lib/ui/BUILD.gn‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ if (enable_unittests) {
269269
"painting/image_decoder_no_gl_unittests.h",
270270
"painting/image_dispose_unittests.cc",
271271
"painting/image_encoding_unittests.cc",
272+
"painting/image_generator_apng_unittests.cc",
272273
"painting/image_generator_registry_unittests.cc",
273274
"painting/paint_unittests.cc",
274275
"painting/path_unittests.cc",

‎engine/src/flutter/lib/ui/painting/image_generator_apng.cc‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,9 @@ std::unique_ptr<ImageGenerator> APNGImageGenerator::MakeFromData(
286286
}
287287
}
288288

289+
if (chunk->get_data_length() < sizeof(AnimationControlChunkData)) {
290+
return nullptr;
291+
}
289292
const AnimationControlChunkData* animation_data =
290293
CastChunkData<AnimationControlChunkData>(chunk);
291294

@@ -430,6 +433,9 @@ APNGImageGenerator::DemuxNextImage(const void* buffer_p,
430433
// The presence of an fcTL chunk is optional for the first (default) image
431434
// of a PNG. Both cases are handled in APNGImage.
432435
if (chunk->get_type() == kFrameControlChunkType) {
436+
if (chunk->get_data_length() < sizeof(FrameControlChunkData)) {
437+
return std::make_pair(std::nullopt, nullptr);
438+
}
433439
control_data = CastChunkData<FrameControlChunkData>(chunk);
434440

435441
ImageGenerator::FrameInfo frame_info;
@@ -491,7 +497,10 @@ APNGImageGenerator::DemuxNextImage(const void* buffer_p,
491497
// sequence number prepended to its data, so subtract that space from
492498
// the buffer.
493499
if (chunk->get_type() == kFrameDataChunkType) {
494-
chunk_space -= 4;
500+
if (chunk->get_data_length() < kFrameDataSequenceNumberSize) {
501+
return std::make_pair(std::nullopt, nullptr);
502+
}
503+
chunk_space -= kFrameDataSequenceNumberSize;
495504
}
496505
}
497506

@@ -527,17 +536,21 @@ APNGImageGenerator::DemuxNextImage(const void* buffer_p,
527536
// Copy the image data/ancillary chunks.
528537
for (const ChunkHeader* c : image_chunks) {
529538
if (c->get_type() == kFrameDataChunkType) {
539+
FML_DCHECK(c->get_data_length() >= kFrameDataSequenceNumberSize);
540+
530541
// Write a new IDAT chunk header.
531542
ChunkHeader* write_header =
532543
reinterpret_cast<ChunkHeader*>(write_cursor);
533-
write_header->set_data_length(c->get_data_length() - 4);
544+
write_header->set_data_length(c->get_data_length() -
545+
kFrameDataSequenceNumberSize);
534546
write_header->set_type(kImageDataChunkType);
535547
write_cursor += sizeof(ChunkHeader);
536548

537549
// Copy all of the data except for the 4 byte sequence number at the
538550
// beginning of the fdAT data.
539551
memcpy(write_cursor,
540-
reinterpret_cast<const uint8_t*>(c) + sizeof(ChunkHeader) + 4,
552+
reinterpret_cast<const uint8_t*>(c) + sizeof(ChunkHeader) +
553+
kFrameDataSequenceNumberSize,
541554
write_header->get_data_length());
542555
write_cursor += write_header->get_data_length();
543556

@@ -645,8 +658,13 @@ void APNGImageGenerator::ChunkHeader::UpdateChunkCrc32() {
645658
uint32_t APNGImageGenerator::ChunkHeader::ComputeChunkCrc32() {
646659
// Exclude the length field at the beginning of the chunk header.
647660
size_t length = sizeof(ChunkHeader) - 4 + get_data_length();
648-
uint8_t* chunk_data_p = reinterpret_cast<uint8_t*>(this) + 4;
661+
const uint8_t* chunk_data = reinterpret_cast<const uint8_t*>(this) + 4;
662+
return ComputeCrc32(chunk_data, length);
663+
}
664+
665+
uint32_t APNGImageGenerator::ComputeCrc32(const uint8_t* data, size_t length) {
649666
uint32_t crc = 0;
667+
const uint8_t* data_p = data;
650668

651669
// zlib's crc32 can only take 16 bits at a time for the length, but PNG
652670
// supports a 32 bit chunk length, so looping is necessary here.
@@ -658,9 +676,9 @@ uint32_t APNGImageGenerator::ChunkHeader::ComputeChunkCrc32() {
658676
length16 = std::numeric_limits<uint16_t>::max();
659677
}
660678

661-
crc = crc32(crc, chunk_data_p, length16);
679+
crc = crc32(crc, data_p, length16);
662680
length -= length16;
663-
chunk_data_p += length16;
681+
data_p += length16;
664682
} while (length > 0);
665683

666684
return crc;

‎engine/src/flutter/lib/ui/painting/image_generator_apng.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,16 @@ class APNGImageGenerator : public ImageGenerator {
5353

5454
static std::unique_ptr<ImageGenerator> MakeFromData(sk_sp<SkData> data);
5555

56+
/// Computes the CRC of the data in a PNG chunk.
57+
static uint32_t ComputeCrc32(const uint8_t* data, size_t length);
58+
5659
private:
5760
static constexpr uint8_t kPngSignature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
5861
static constexpr size_t kChunkCrcSize = 4;
5962

63+
/// The size of the sequence number at the beginning of an fdAT chunk.
64+
static constexpr size_t kFrameDataSequenceNumberSize = 4;
65+
6066
enum ChunkType {
6167
kImageHeaderChunkType = 'IHDR',
6268
kAnimationControlChunkType = 'acTL',
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "flutter/lib/ui/painting/image_generator_apng.h"
6+
7+
#include <cstdint>
8+
#include <cstring>
9+
#include <vector>
10+
11+
#include "flutter/lib/ui/painting/image_generator_registry.h"
12+
#include "flutter/testing/testing.h"
13+
#include "third_party/skia/include/core/SkData.h"
14+
15+
namespace flutter {
16+
namespace testing {
17+
18+
namespace {
19+
20+
// Writes a big-endian uint32_t to a buffer.
21+
void WriteBE32(std::vector<uint8_t>& buf, uint32_t val) {
22+
buf.push_back((val >> 24) & 0xFF);
23+
buf.push_back((val >> 16) & 0xFF);
24+
buf.push_back((val >> 8) & 0xFF);
25+
buf.push_back(val & 0xFF);
26+
}
27+
28+
// Writes a big-endian uint16_t to a buffer.
29+
void WriteBE16(std::vector<uint8_t>& buf, uint16_t val) {
30+
buf.push_back((val >> 8) & 0xFF);
31+
buf.push_back(val & 0xFF);
32+
}
33+
34+
// Appends a PNG chunk (length + type + data + CRC) to the buffer.
35+
void AppendChunk(std::vector<uint8_t>& buf,
36+
const char type[4],
37+
const std::vector<uint8_t>& data) {
38+
FML_CHECK(data.size() <= std::numeric_limits<uint32_t>::max());
39+
WriteBE32(buf, static_cast<uint32_t>(data.size()));
40+
size_t type_start = buf.size();
41+
buf.insert(buf.end(), type, type + 4);
42+
buf.insert(buf.end(), data.begin(), data.end());
43+
uint32_t crc = APNGImageGenerator::ComputeCrc32(buf.data() + type_start,
44+
4 + data.size());
45+
WriteBE32(buf, crc);
46+
}
47+
48+
// Builds a minimal valid APNG with a malicious fdAT chunk whose
49+
// data_length is less than 4, which would trigger an integer underflow
50+
// in DemuxNextImage() without the bounds check fix.
51+
std::vector<uint8_t> BuildMaliciousApng(uint32_t fdat_data_length) {
52+
std::vector<uint8_t> apng;
53+
54+
// PNG signature
55+
const uint8_t sig[] = {137, 80, 78, 71, 13, 10, 26, 10};
56+
apng.insert(apng.end(), sig, sig + 8);
57+
58+
// IHDR: 1x1 RGBA, 8-bit
59+
{
60+
std::vector<uint8_t> ihdr;
61+
WriteBE32(ihdr, 1); // width
62+
WriteBE32(ihdr, 1); // height
63+
ihdr.push_back(8); // bit depth
64+
ihdr.push_back(6); // color type (RGBA)
65+
ihdr.push_back(0); // compression
66+
ihdr.push_back(0); // filter
67+
ihdr.push_back(0); // interlace
68+
AppendChunk(apng, "IHDR", ihdr);
69+
}
70+
71+
// acTL: 1 frame, loop forever
72+
{
73+
std::vector<uint8_t> actl;
74+
WriteBE32(actl, 1); // num_frames
75+
WriteBE32(actl, 0); // num_plays (0 = infinite)
76+
AppendChunk(apng, "acTL", actl);
77+
}
78+
79+
// fcTL for frame 0
80+
{
81+
std::vector<uint8_t> fctl;
82+
WriteBE32(fctl, 0); // sequence_number
83+
WriteBE32(fctl, 1); // width
84+
WriteBE32(fctl, 1); // height
85+
WriteBE32(fctl, 0); // x_offset
86+
WriteBE32(fctl, 0); // y_offset
87+
WriteBE16(fctl, 1); // delay_num
88+
WriteBE16(fctl, 10); // delay_den
89+
fctl.push_back(0); // dispose_op
90+
fctl.push_back(0); // blend_op
91+
AppendChunk(apng, "fcTL", fctl);
92+
}
93+
94+
// Malicious fdAT for frame 0: data_length < 4
95+
// An fdAT chunk must have at least 4 bytes (sequence number).
96+
// With data_length < 4, the subtraction in DemuxNextImage() underflows.
97+
AppendChunk(apng, "fdAT", std::vector<uint8_t>(fdat_data_length, 0));
98+
99+
// IEND
100+
AppendChunk(apng, "IEND", {});
101+
102+
return apng;
103+
}
104+
105+
} // namespace
106+
107+
// Verify that the APNG decoder can handle fdAT chunks whose length is shorter
108+
// than the required 4-byte sequence number.
109+
TEST(APNGImageGeneratorTest, FdATWithShortDataLengthDoesNotCrash) {
110+
ImageGeneratorRegistry registry;
111+
112+
auto make_generator = [](uint32_t fdat_length) -> auto {
113+
auto apng_bytes = BuildMaliciousApng(fdat_length);
114+
auto data = SkData::MakeWithCopy(apng_bytes.data(), apng_bytes.size());
115+
return APNGImageGenerator::MakeFromData(data);
116+
};
117+
118+
// The decoder should reject fdAT chunks that are less than 4 bytes long.
119+
EXPECT_EQ(make_generator(0), nullptr);
120+
EXPECT_EQ(make_generator(2), nullptr);
121+
122+
// Creating the generator should succeed if the fdAT has sufficient length.
123+
EXPECT_NE(make_generator(4), nullptr);
124+
}
125+
126+
} // namespace testing
127+
} // namespace flutter

0 commit comments

Comments
 (0)