396e5c
From 3d35d209c19c1d3b09b794a0c863ba5de44a9c0a Mon Sep 17 00:00:00 2001
396e5c
From: Marek Kasik <mkasik@redhat.com>
396e5c
Date: Mon, 29 Oct 2018 17:44:47 +0100
396e5c
Subject: [PATCH] Avoid cycles in PDF parsing
396e5c
396e5c
Mark objects being processed in Parser::makeStream() as being processed
396e5c
and check the mark when entering this method to avoid processing
396e5c
of the same object recursively.
396e5c
---
396e5c
 poppler/Parser.cc | 15 +++++++++++++++
396e5c
 poppler/XRef.h    |  1 +
396e5c
 2 files changed, 16 insertions(+)
396e5c
396e5c
diff --git a/poppler/Parser.cc b/poppler/Parser.cc
396e5c
index bd4845ab..8f48efbe 100644
396e5c
--- a/poppler/Parser.cc
396e5c
+++ b/poppler/Parser.cc
396e5c
@@ -197,6 +197,18 @@ Stream *Parser::makeStream(Object &&dict, Guchar *fileKey,
396e5c
   Stream *str;
396e5c
   Goffset length;
396e5c
   Goffset pos, endPos;
396e5c
+  XRefEntry *entry = nullptr;
396e5c
+
396e5c
+  if (xref && (entry = xref->getEntry(objNum, false))) {
396e5c
+    if (!entry->getFlag(XRefEntry::Parsing) ||
396e5c
+        (objNum == 0 && objGen == 0)) {
396e5c
+      entry->setFlag(XRefEntry::Parsing, true);
396e5c
+    } else {
396e5c
+      error(errSyntaxError, getPos(),
396e5c
+            "Object '{0:d} {1:d} obj' is being already parsed", objNum, objGen);
396e5c
+      return nullptr;
396e5c
+    }
396e5c
+  }
396e5c
 
396e5c
   // get stream start position
396e5c
   lexer->skipToNextLine();
396e5c
@@ -278,6 +290,9 @@ Stream *Parser::makeStream(Object &&dict, Guchar *fileKey,
396e5c
   // get filters
396e5c
   str = str->addFilters(str->getDict(), recursion);
396e5c
 
396e5c
+  if (entry)
396e5c
+    entry->setFlag(XRefEntry::Parsing, false);
396e5c
+
396e5c
   return str;
396e5c
 }
396e5c
 
396e5c
diff --git a/poppler/XRef.h b/poppler/XRef.h
396e5c
index 11ee5e03..2eb2f9fd 100644
396e5c
--- a/poppler/XRef.h
396e5c
+++ b/poppler/XRef.h
396e5c
@@ -65,6 +65,7 @@ struct XRefEntry {
396e5c
   enum Flag {
396e5c
     // Regular flags
396e5c
     Updated,     // Entry was modified
396e5c
+    Parsing,     // Entry is currently being parsed
396e5c
 
396e5c
     // Special flags -- available only after xref->scanSpecialFlags() is run
396e5c
     Unencrypted, // Entry is stored in unencrypted form (meaningless in unencrypted documents)
396e5c
-- 
396e5c
2.19.1
396e5c