Skip to content

Commit

Permalink
correction to uaf1 cve (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
galkahana authored Jul 24, 2023
1 parent 48bfb97 commit 6c0aa49
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 34 deletions.
96 changes: 62 additions & 34 deletions PDFWriter/PDFParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1934,37 +1934,13 @@ IByteReader* PDFParser::CreateInputStreamReader(PDFStreamInput* inStream)
return result;
}

EStatusCodeAndIByteReader PDFParser::CreateFilterForStream(IByteReader* inStream,PDFName* inFilterName,PDFDictionary* inDecodeParams, PDFStreamInput* inPDFStream)
{

EStatusCodeAndIByteReader PDFParser::WrapWithPredictorStream(IByteReader* inputStream, PDFDictionary* inDecodeParams) {
EStatusCode status = eSuccess;
IByteReader* result = NULL;

do
do
{

if(inFilterName->GetValue() == "FlateDecode" || inFilterName->GetValue() == "LZWDecode")
{
if (inFilterName->GetValue() == "FlateDecode")
{
InputFlateDecodeStream* flateStream;
flateStream = new InputFlateDecodeStream(NULL); // assigning null, so later delete, if failure occurs won't delete the input stream
flateStream->Assign(inStream);
result = flateStream;
}
else if (inFilterName->GetValue() == "LZWDecode")
{
InputLZWDecodeStream* lzwStream;
int early = 1;
if (inDecodeParams)
{
PDFObjectCastPtr<PDFInteger> earlyObj(QueryDictionaryObject(inDecodeParams, "EarlyChange"));
early = earlyObj->GetValue();
}
lzwStream = new InputLZWDecodeStream(early);
lzwStream->Assign(inStream);
result = lzwStream;
}

// check for predictor n' such
if (!inDecodeParams)
// no predictor, stop here
Expand Down Expand Up @@ -1995,7 +1971,7 @@ EStatusCodeAndIByteReader PDFParser::CreateFilterForStream(IByteReader* inStream
{
case 2:
{
result = new InputPredictorTIFFSubStream(result,
result = new InputPredictorTIFFSubStream(inputStream,
colorsValue,
bitsPerComponentValue,
columnsValue);
Expand All @@ -2010,7 +1986,7 @@ EStatusCodeAndIByteReader PDFParser::CreateFilterForStream(IByteReader* inStream
{
// Gal: optimum can handle all presets, because non-optimum presets still require a function sign flag
// at line start...so optimum can handle them.
result = new InputPredictorPNGOptimumStream(result,
result = new InputPredictorPNGOptimumStream(inputStream,
colorsValue,
bitsPerComponentValue,
columnsValue);
Expand All @@ -2023,6 +1999,62 @@ EStatusCodeAndIByteReader PDFParser::CreateFilterForStream(IByteReader* inStream
break;
}
}
} while(false);

return EStatusCodeAndIByteReader(status,result);

}

EStatusCodeAndIByteReader PDFParser::CreateFilterForStream(IByteReader* inStream,PDFName* inFilterName,PDFDictionary* inDecodeParams, PDFStreamInput* inPDFStream)
{
EStatusCode status = eSuccess;
IByteReader* result = NULL;

// Important! in case of failure CreateFilterForStream must NOT delete inStream, as its caller
// is assuming ownership is NOT transferred in that case. And each clause should clean
// after its done in case of failure (but again - not the incoming stream)

do
{

if (inFilterName->GetValue() == "FlateDecode")
{
InputFlateDecodeStream* flateStream;
flateStream = new InputFlateDecodeStream(inStream);
result = flateStream;
EStatusCodeAndIByteReader createStatus = WrapWithPredictorStream(result, inDecodeParams);
if(createStatus.first == eFailure) {
flateStream->Assign(NULL); // assign null to remove ownership of input stream so later delete does NOT delete it
delete flateStream;
result = NULL;
status = eFailure;
}
else if(createStatus.second != NULL) {
result = createStatus.second;
}
}
else if (inFilterName->GetValue() == "LZWDecode")
{
InputLZWDecodeStream* lzwStream;
int early = 1;
if (inDecodeParams)
{
PDFObjectCastPtr<PDFInteger> earlyObj(QueryDictionaryObject(inDecodeParams, "EarlyChange"));
early = earlyObj->GetValue();
}
lzwStream = new InputLZWDecodeStream(early);
lzwStream->Assign(inStream);
result = lzwStream;
EStatusCodeAndIByteReader createStatus = WrapWithPredictorStream(result, inDecodeParams);
if(createStatus.first == eFailure) {
lzwStream->Assign(NULL); // assign null to remove ownership of input stream so later delete does NOT delete it
delete lzwStream;
result = NULL;
status = eFailure;
}
else if(createStatus.second != NULL) {
result = createStatus.second;
}
}
else if (inFilterName->GetValue() == "ASCIIHexDecode")
{
Expand Down Expand Up @@ -2051,6 +2083,7 @@ EStatusCodeAndIByteReader PDFParser::CreateFilterForStream(IByteReader* inStream
{
TRACE_LOG1("PDFParser::CreateFilterForStream, filter is not supported by extender - %s",inFilterName->GetValue().substr(0, MAX_TRACE_SIZE - 200).c_str());
status = PDFHummus::eFailure;
result = NULL;
break;
}
}
Expand All @@ -2062,11 +2095,6 @@ EStatusCodeAndIByteReader PDFParser::CreateFilterForStream(IByteReader* inStream
}
}while(false);

if(status != PDFHummus::eSuccess)
{
delete result;
result = NULL;
}
return EStatusCodeAndIByteReader(status,result);

}
Expand Down
1 change: 1 addition & 0 deletions PDFWriter/PDFParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ class PDFParser
PDFObject* ParseExistingInDirectStreamObject(ObjectIDType inObjectId);
PDFHummus::EStatusCode ParseObjectStreamHeader(ObjectStreamHeaderEntry* inHeaderInfo,ObjectIDType inObjectsCount);
void MovePositionInStream(LongFilePositionType inPosition);
EStatusCodeAndIByteReader WrapWithPredictorStream(IByteReader* inputStream, PDFDictionary* inDecodeParams);
EStatusCodeAndIByteReader CreateFilterForStream(IByteReader* inStream,PDFName* inFilterName,PDFDictionary* inDecodeParams, PDFStreamInput* inPDFStream);

void NotifyIndirectObjectStart(long long inObjectID, long long inGenerationNumber);
Expand Down

0 comments on commit 6c0aa49

Please sign in to comment.