From 5f2ea0f6ef587f9f7a2fec9f80dbc82b94c97400 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Wed, 19 Sep 2018 17:26:34 +0000 Subject: Validate decoder pipelines. PDF decoders, AKA filters, can be chained together. There can be an arbitrary number of decoding / decompressing filters in the pipeline, but there should be at most 1 image decoder, and the image decoder should only be at the end of the chain. BUG=chromium:880675 Change-Id: Iffa27c70ec1ed7574e38e0de23413840ee900959 Reviewed-on: https://pdfium-review.googlesource.com/42711 Reviewed-by: Ryan Harrison Reviewed-by: Tom Sepez Commit-Queue: Lei Zhang --- core/fpdfapi/parser/fpdf_parser_decode.cpp | 20 ++++++ core/fpdfapi/parser/fpdf_parser_decode.h | 3 + .../fpdfapi/parser/fpdf_parser_decode_unittest.cpp | 80 ++++++++++++++++++++++ 3 files changed, 103 insertions(+) diff --git a/core/fpdfapi/parser/fpdf_parser_decode.cpp b/core/fpdfapi/parser/fpdf_parser_decode.cpp index eafb8ee8a3..2f73b3a4cb 100644 --- a/core/fpdfapi/parser/fpdf_parser_decode.cpp +++ b/core/fpdfapi/parser/fpdf_parser_decode.cpp @@ -24,6 +24,7 @@ #include "core/fxcodec/fx_codec.h" #include "core/fxcrt/fx_extension.h" #include "third_party/base/numerics/safe_math.h" +#include "third_party/base/stl_util.h" namespace { @@ -87,6 +88,22 @@ const uint16_t PDFDocEncoding[256] = { 0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x00f7, 0x00f8, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x00fd, 0x00fe, 0x00ff}; +bool ValidateDecoderPipeline(const CPDF_Array* pDecoders) { + size_t count = pDecoders->GetCount(); + if (count <= 1) + return true; + + // TODO(thestig): Consolidate all the places that use these filter names. + static const char kValidDecoders[][16] = { + "FlateDecode", "Fl", "LZWDecode", "LZW", "ASCII85Decode", "A85", + "ASCIIHexDecode", "AHx", "RunLengthDecode", "RL"}; + for (size_t i = 0; i < count - 1; ++i) { + if (!pdfium::ContainsValue(kValidDecoders, pDecoders->GetStringAt(i))) + return false; + } + return true; +} + uint32_t A85Decode(pdfium::span src_span, std::unique_ptr* dest_buf, uint32_t* dest_size) { @@ -359,6 +376,9 @@ bool PDF_DataDecode(pdfium::span src_span, std::vector> DecoderArray; if (const CPDF_Array* pDecoders = pDecoder->AsArray()) { + if (!ValidateDecoderPipeline(pDecoders)) + return false; + const CPDF_Array* pParamsArray = ToArray(pParams); for (size_t i = 0; i < pDecoders->GetCount(); ++i) { DecoderArray.push_back( diff --git a/core/fpdfapi/parser/fpdf_parser_decode.h b/core/fpdfapi/parser/fpdf_parser_decode.h index e4fdce1037..57ee271656 100644 --- a/core/fpdfapi/parser/fpdf_parser_decode.h +++ b/core/fpdfapi/parser/fpdf_parser_decode.h @@ -15,11 +15,14 @@ #include "third_party/base/span.h" class CCodec_ScanlineDecoder; +class CPDF_Array; class CPDF_Dictionary; // Indexed by 8-bit char code, contains unicode code points. extern const uint16_t PDFDocEncoding[256]; +bool ValidateDecoderPipeline(const CPDF_Array* pDecoders); + ByteString PDF_EncodeString(const ByteString& src, bool bHex); WideString PDF_DecodeText(const uint8_t* pData, uint32_t size); WideString PDF_DecodeText(const ByteString& bstr); diff --git a/core/fpdfapi/parser/fpdf_parser_decode_unittest.cpp b/core/fpdfapi/parser/fpdf_parser_decode_unittest.cpp index b6d7679ef5..a4572396e6 100644 --- a/core/fpdfapi/parser/fpdf_parser_decode_unittest.cpp +++ b/core/fpdfapi/parser/fpdf_parser_decode_unittest.cpp @@ -4,9 +4,89 @@ #include "core/fpdfapi/parser/fpdf_parser_decode.h" +#include "core/fpdfapi/parser/cpdf_array.h" +#include "core/fpdfapi/parser/cpdf_name.h" #include "testing/gtest/include/gtest/gtest.h" #include "testing/test_support.h" +TEST(fpdf_parser_decode, ValidateDecoderPipeline) { + { + // Empty decoder list is always valid. + CPDF_Array decoders; + EXPECT_TRUE(ValidateDecoderPipeline(&decoders)); + } + { + // 1 decoder is always valid. + CPDF_Array decoders; + decoders.AddNew("FlateEncode"); + EXPECT_TRUE(ValidateDecoderPipeline(&decoders)); + } + { + // 1 decoder is always valid, even with an unknown decoder. + CPDF_Array decoders; + decoders.AddNew("FooBar"); + EXPECT_TRUE(ValidateDecoderPipeline(&decoders)); + } + { + // Valid 2 decoder pipeline. + CPDF_Array decoders; + decoders.AddNew("AHx"); + decoders.AddNew("LZWDecode"); + EXPECT_TRUE(ValidateDecoderPipeline(&decoders)); + } + { + // Valid 2 decoder pipeline. + CPDF_Array decoders; + decoders.AddNew("ASCII85Decode"); + decoders.AddNew("ASCII85Decode"); + EXPECT_TRUE(ValidateDecoderPipeline(&decoders)); + } + { + // Valid 5 decoder pipeline. + CPDF_Array decoders; + decoders.AddNew("ASCII85Decode"); + decoders.AddNew("A85"); + decoders.AddNew("RunLengthDecode"); + decoders.AddNew("FlateDecode"); + decoders.AddNew("RL"); + EXPECT_TRUE(ValidateDecoderPipeline(&decoders)); + } + { + // Valid 5 decoder pipeline, with an image decoder at the end. + CPDF_Array decoders; + decoders.AddNew("RunLengthDecode"); + decoders.AddNew("ASCII85Decode"); + decoders.AddNew("FlateDecode"); + decoders.AddNew("LZW"); + decoders.AddNew("DCTDecode"); + EXPECT_TRUE(ValidateDecoderPipeline(&decoders)); + } + { + // Invalid 2 decoder pipeline, with 2 image decoders. + CPDF_Array decoders; + decoders.AddNew("DCTDecode"); + decoders.AddNew("CCITTFaxDecode"); + EXPECT_FALSE(ValidateDecoderPipeline(&decoders)); + } + { + // Invalid 2 decoder pipeline, with 1 image decoder at the start. + CPDF_Array decoders; + decoders.AddNew("DCTDecode"); + decoders.AddNew("FlateDecode"); + EXPECT_FALSE(ValidateDecoderPipeline(&decoders)); + } + { + // Invalid 5 decoder pipeline. + CPDF_Array decoders; + decoders.AddNew("FlateDecode"); + decoders.AddNew("FlateDecode"); + decoders.AddNew("DCTDecode"); + decoders.AddNew("FlateDecode"); + decoders.AddNew("FlateDecode"); + EXPECT_FALSE(ValidateDecoderPipeline(&decoders)); + } +} + TEST(fpdf_parser_decode, A85Decode) { const pdfium::DecodeTestData test_data[] = { // Empty src string. -- cgit v1.2.3