From c63248941708bc1d2e3a4292954593312212f6ca Mon Sep 17 00:00 00:00 +0000
From: yirou_yang <yirouyang0622@gmail.com>
Date: Tue, 11 Aug 2026 05:37:03 +0000
Subject: [PATCH] catalog: Fix NULL deref for nextCatalog without 'catalog'
 attribute

Fix https://gitlab.gnome.org/GNOME/libxml2/-/issues/1143

Backport of upstream commit c63248941708bc1d2e3a4292954593312212f6ca to
2.13.8. CVE-2026-76781 / bsc#1281148.

Adapted for 2.13.8: the vulnerable dedup loop comes from the
backport of CVE-2026-0990 (upstream commit 1961208e), so the
catalog.c change is identical to upstream. The testcatalog.c
hunks are regenerated, because the backported testcatalog.c
(from CVE-2026-0992, upstream commit f8399e62) has no
testRepeatedResolveURI and lacks the string.h include that the
new test needs. The CMakeLists.txt hunk is regenerated for the
testcatalog add_test line in this tree.
--- a/catalog.c
index 53f3cc4a4b8ac5f2186c25f153060fbcb20f5702..1e34e52e52aadec83e3eb452e17fabc1de9800b5 100644
+++ b/catalog.c
@@ -1257,7 +1257,7 @@
 		BAD_CAST "nextCatalog", NULL,
 		BAD_CAST "catalog", prefer, cgroup);
 	/* Avoid duplication of nextCatalog */
-	while (prev != NULL) {
+	while ((entry != NULL) && (prev != NULL)) {
 	    if ((prev->type == XML_CATA_NEXT_CATALOG) &&
 		(xmlStrEqual (prev->URL, entry->URL)) &&
 		(prev->prefer == entry->prefer) &&
--- a/CMakeLists.txt
index 85e7558d3731a0fbcadfe197f04b277993f4aeb2..7ce4db86c2d8cc07570589a3ec61a1e587f2db83 100644
+++ b/CMakeLists.txt
@@ -532,7 +532,7 @@
     if(NOT WIN32)
         add_test(NAME testapi COMMAND testapi)
     endif()
-    add_test(NAME testcatalog COMMAND testcatalog)
+    add_test(NAME testcatalog COMMAND testcatalog WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
     add_test(NAME testchar COMMAND testchar)
     add_test(NAME testdict COMMAND testdict)
     add_test(NAME testparser COMMAND testparser WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
--- /dev/null
index 0000000000000000000000000000000000000000..b31a4906293a072acfaa347aecad44db27a45c7a 100644
+++ b/test/catalogs/next-catalog-missing-attr.xml
@@ -0,0 +1,4 @@
+<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
+  <nextCatalog catalog="registry.xml"/>
+  <nextCatalog/>
+</catalog>
--- a/testcatalog.c
index 86d33bd0d01d1a6df8c5e431b213bd27eb2887be..56bd13af760771b06ef6fd0bcab5479bf983b145 100644
+++ b/testcatalog.c
@@ -12,6 +12,7 @@
 
 #include "libxml.h"
 #include <stdio.h>
+#include <string.h>
 
 #ifdef LIBXML_CATALOG_ENABLED
 #include <libxml/catalog.h>
@@ -78,12 +79,69 @@
     return ret;
 }
 
+/*
+ * Test a nextCatalog entry which lacks the mandatory 'catalog' attribute
+ * See https://gitlab.gnome.org/GNOME/libxml2/-/issues/1143
+ */
+static int
+testNextCatalogMissingAttr(void) {
+    int ret = 0;
+    int i = 0;
+    const char *cat = "test/catalogs/next-catalog-missing-attr.xml";
+    const char *sysId = "http://www.oasis-open.org/docbook/xml/4.1.2/dbpoolx.mod";
+    const char *expect = "/usr/share/xml/docbook/xml/4.1.2/dbpoolx.mod";
+    xmlChar *resolved = NULL;
+    xmlDocPtr doc = NULL;
+    xmlNodePtr node = NULL;
+
+    xmlInitParser();
+
+    xmlLoadCatalog(cat);
+    /*
+     * The malformed nextCatalog must be ignored, the valid one must still
+     * be followed. This also forces the complete recursive load.
+     */
+    resolved = xmlCatalogResolveSystem(BAD_CAST sysId);
+    if ((resolved == NULL) || (strcmp((char *) resolved, expect) != 0)) {
+        fprintf(stderr, "CATALOG-FAILURE: %s resolved to %s, expected %s\n",
+                sysId, resolved == NULL ? "NULL" : (char *) resolved, expect);
+        ret = 1;
+    }
+    if (resolved != NULL)
+        xmlFree(resolved);
+
+    /**
+     * Ensure that only the valid nextCatalog was recorded
+     */
+    doc = xmlCatalogDumpDoc();
+    xmlCatalogCleanup();
+
+    if (doc == NULL) {
+        fprintf(stderr, "CATALOG-FAILURE: Failed to dump the catalog\n");
+        return 1;
+    }
+
+    /* Just the root "catalog" node with a single nextCatalog */
+    node = xmlDocGetRootElement(doc);
+    node = node->children;
+    for (i=0; node != NULL; node=node->next, i++) {}
+    if (i != 1) {
+        fprintf(stderr, "CATALOG-FAILURE: Found %d nextCatalog entries and should be 1\n", i);
+        ret = 1;
+    }
+
+    xmlFreeDoc(doc);
+
+    return ret;
+}
+
 int
 main(void) {
     int err = 0;
 
     err |= testRecursiveDelegateUri();
     err |= testRepeatedNextCatalog();
+    err |= testNextCatalogMissingAttr();
 
     return err;
 }
