From ac36affcc8486de38e8905a8d6547a3464ff46e5 Mon Sep 17 00:00:00 2001
From: Sune Vuorela <sune@vuorela.dk>
Date: Tue, 3 Jun 2025 00:35:19 +0200
Subject: [PATCH] Limit ammount of annots per document/page

CVE: CVE-2025-52886
Upstream-Status: Backport [https://gitlab.freedesktop.org/poppler/poppler/-/commit/ac36affcc8486de38e8905a8d6547a3464ff46e5]

Signed-off-by: Yogita Urade <yogita.urade@windriver.com>
---
 poppler/Annot.cc |  4 ++++
 poppler/Page.cc  | 16 ++++++++++++++++
 2 files changed, 20 insertions(+)

diff --git a/poppler/Annot.cc b/poppler/Annot.cc
index 19ec824..9b5145c 100644
--- a/poppler/Annot.cc
+++ b/poppler/Annot.cc
@@ -7253,6 +7253,10 @@ Annots::Annots(PDFDoc *docA, int page, Object *annotsObj)
                 const Object &obj2 = annotsObj->arrayGetNF(i);
                 std::shared_ptr<Annot> annot = createAnnot(std::move(obj1), &obj2);
                 if (annot) {
+                    if (annot.use_count() > 100000) {
+                        error(errSyntaxError, -1, "Annotations likely malformed. Too many references. Stopping processing annots on page {0:d}", page);
+                        break;
+                    }
                     if (annot->isOk()) {
                         annot->setPage(page, false); // Don't change /P
                         appendAnnot(annot);
diff --git a/poppler/Page.cc b/poppler/Page.cc
index b27c90d..8b5da2f 100644
--- a/poppler/Page.cc
+++ b/poppler/Page.cc
@@ -289,6 +289,22 @@ Page::Page(PDFDoc *docA, int numA, Object &&pageDict, Ref pageRefA, PageAttrs *a
         goto err2;
     }

+    if (annotsObj.isArray() && annotsObj.arrayGetLength() > 10000) {
+        error(errSyntaxError, -1, "Page annotations object (page {0:d}) is likely malformed. Too big: ({1:d})", num, annotsObj.arrayGetLength());
+        goto err2;
+    }
+    if (annotsObj.isRef()) {
+        auto resolvedObj = getAnnotsObject();
+        if (resolvedObj.isArray() && resolvedObj.arrayGetLength() > 10000) {
+            error(errSyntaxError, -1, "Page annotations object (page {0:d}) is likely malformed. Too big: ({1:d})", num, resolvedObj.arrayGetLength());
+            goto err2;
+        }
+        if (!resolvedObj.isArray() && !resolvedObj.isNull()) {
+            error(errSyntaxError, -1, "Page annotations object (page {0:d}) is wrong type ({1:s})", num, resolvedObj.getTypeName());
+            goto err2;
+        }
+    }
+
     // contents
     contents = pageObj.dictLookupNF("Contents").copy();
     if (!(contents.isRef() || contents.isArray() || contents.isNull())) {
--
2.40.0
