From: Nathan Date: Wed, 10 Sep 2025 18:11:50 +0300 Subject: fix: Prevent infinite recursion in xmlCatalogListXMLResolve Origin: https://gitlab.gnome.org/GNOME/libxml2/-/commit/3425dece47c8db600f8d7328ae2d7ddfaa0d7b2d Bug: https://gitlab.gnome.org/GNOME/libxml2/-/issues/958 Bug-Debian-Security: https://security-tracker.debian.org/tracker/CVE-2025-8732 --- catalog.c | 28 ++++++++++++++++++++-------- result/catalogs/recursive | 1 + test/catalogs/recursive.script | 0 test/catalogs/recursive.sgml | 1 + 4 files changed, 22 insertions(+), 8 deletions(-) create mode 100644 result/catalogs/recursive create mode 100644 test/catalogs/recursive.script create mode 100644 test/catalogs/recursive.sgml diff --git a/catalog.c b/catalog.c index e071b49..b25a630 100644 --- a/catalog.c +++ b/catalog.c @@ -92,7 +92,7 @@ unsigned long __stdcall GetModuleFileNameA(void*, char*, unsigned long); #endif static xmlChar *xmlCatalogNormalizePublic(const xmlChar *pubID); -static int xmlExpandCatalog(xmlCatalogPtr catal, const char *filename); +static int xmlExpandCatalog(xmlCatalogPtr catal, const char *filename, int depth); /************************************************************************ * * @@ -2320,6 +2320,7 @@ xmlGetSGMLCatalogEntryType(const xmlChar *name) { * @file: the filepath for the catalog * @super: should this be handled as a Super Catalog in which case * parsing is not recursive + * @depth: the current depth of the catalog * * Parse an SGML catalog content and fill up the @catal hash table with * the new entries found. @@ -2328,13 +2329,19 @@ xmlGetSGMLCatalogEntryType(const xmlChar *name) { */ static int xmlParseSGMLCatalog(xmlCatalogPtr catal, const xmlChar *value, - const char *file, int super) { + const char *file, int super, int depth) { const xmlChar *cur = value; xmlChar *base = NULL; int res; if ((cur == NULL) || (file == NULL)) return(-1); + + /* Check recursion depth */ + if (depth > MAX_CATAL_DEPTH) { + return(-1); + } + base = xmlStrdup((const xmlChar *) file); while ((cur != NULL) && (cur[0] != 0)) { @@ -2512,7 +2519,7 @@ xmlParseSGMLCatalog(xmlCatalogPtr catal, const xmlChar *value, filename = xmlBuildURI(sysid, base); if (filename != NULL) { - xmlExpandCatalog(catal, (const char *)filename); + xmlExpandCatalog(catal, (const char *)filename, depth); xmlFree(filename); } } @@ -2662,7 +2669,7 @@ xmlLoadSGMLSuperCatalog(const char *filename) return(NULL); } - ret = xmlParseSGMLCatalog(catal, content, filename, 1); + ret = xmlParseSGMLCatalog(catal, content, filename, 1, 0); xmlFree(content); if (ret < 0) { xmlFreeCatalog(catal); @@ -2708,7 +2715,7 @@ xmlLoadACatalog(const char *filename) xmlFree(content); return(NULL); } - ret = xmlParseSGMLCatalog(catal, content, filename, 0); + ret = xmlParseSGMLCatalog(catal, content, filename, 0, 0); if (ret < 0) { xmlFreeCatalog(catal); xmlFree(content); @@ -2731,6 +2738,7 @@ xmlLoadACatalog(const char *filename) * xmlExpandCatalog: * @catal: a catalog * @filename: a file path + * @depth: the current depth of the catalog * * Load the catalog and expand the existing catal structure. * This can be either an XML Catalog or an SGML Catalog @@ -2738,13 +2746,17 @@ xmlLoadACatalog(const char *filename) * Returns 0 in case of success, -1 in case of error */ static int -xmlExpandCatalog(xmlCatalogPtr catal, const char *filename) +xmlExpandCatalog(xmlCatalogPtr catal, const char *filename, int depth) { int ret; if ((catal == NULL) || (filename == NULL)) return(-1); + /* Check recursion depth */ + if (depth > MAX_CATAL_DEPTH) { + return(-1); + } if (catal->type == XML_SGML_CATALOG_TYPE) { xmlChar *content; @@ -2753,7 +2765,7 @@ xmlExpandCatalog(xmlCatalogPtr catal, const char *filename) if (content == NULL) return(-1); - ret = xmlParseSGMLCatalog(catal, content, filename, 0); + ret = xmlParseSGMLCatalog(catal, content, filename, 0, depth + 1); if (ret < 0) { xmlFree(content); return(-1); @@ -3221,7 +3233,7 @@ xmlLoadCatalog(const char *filename) return(0); } - ret = xmlExpandCatalog(xmlDefaultCatalog, filename); + ret = xmlExpandCatalog(xmlDefaultCatalog, filename, 0); xmlRMutexUnlock(xmlCatalogMutex); return(ret); } diff --git a/result/catalogs/recursive b/result/catalogs/recursive new file mode 100644 index 0000000..56ca91e --- /dev/null +++ b/result/catalogs/recursive @@ -0,0 +1 @@ +> \ No newline at end of file diff --git a/test/catalogs/recursive.script b/test/catalogs/recursive.script new file mode 100644 index 0000000..e69de29 diff --git a/test/catalogs/recursive.sgml b/test/catalogs/recursive.sgml new file mode 100644 index 0000000..ac2148b --- /dev/null +++ b/test/catalogs/recursive.sgml @@ -0,0 +1 @@ +CATALOG recursive.sgml