From 95e63233122ab948bf07af45d08a0d54927df422 Mon Sep 17 00:00:00 2001 From: Alessandro Bono Date: Fri, 1 Sep 2023 16:20:55 +0200 Subject: [PATCH] libxml2: Backport buffer overflow fixes Issue: https://gitlab.gnome.org/GNOME/libxml2/-/issues/535 Backported patches: https://gitlab.gnome.org/GNOME/libxml2/-/commit/d0c3f01e110d54415611c5fa0040cdf4a56053f9 https://gitlab.gnome.org/GNOME/libxml2/-/commit/235b15a590eecf97b09e87bdb7e4f8333e9de129 --- ...ld-SAX1-parser-with-custom-callbacks.patch | 32 +++++++++ ...ays-initialize-SAX1-element-handlers.patch | 67 +++++++++++++++++++ gvsbuild/projects/libxml2.py | 4 ++ 3 files changed, 103 insertions(+) create mode 100644 gvsbuild/patches/libxml2/0001-parser-fix-old-SAX1-parser-with-custom-callbacks.patch create mode 100644 gvsbuild/patches/libxml2/0002-sax-always-initialize-SAX1-element-handlers.patch diff --git a/gvsbuild/patches/libxml2/0001-parser-fix-old-SAX1-parser-with-custom-callbacks.patch b/gvsbuild/patches/libxml2/0001-parser-fix-old-SAX1-parser-with-custom-callbacks.patch new file mode 100644 index 000000000..50fba5536 --- /dev/null +++ b/gvsbuild/patches/libxml2/0001-parser-fix-old-SAX1-parser-with-custom-callbacks.patch @@ -0,0 +1,32 @@ +From d0c3f01e110d54415611c5fa0040cdf4a56053f9 Mon Sep 17 00:00:00 2001 +From: Nick Wellnhofer +Date: Sat, 6 May 2023 17:47:37 +0200 +Subject: [PATCH] parser: Fix old SAX1 parser with custom callbacks + +For some reason, xmlCtxtUseOptionsInternal set the start and end element +SAX handlers to the internal DOM builder functions when XML_PARSE_SAX1 +was specified. This means that custom SAX handlers could never work with +that flag because these functions would receive the wrong user data +argument and crash immediately. + +Fixes #535. +--- + parser.c | 2 -- + 1 file changed, 2 deletions(-) + +diff --git a/parser.c b/parser.c +index bb05791d3..0c8bed129 100644 +--- a/parser.c ++++ b/parser.c +@@ -14479,8 +14479,6 @@ xmlCtxtUseOptionsInternal(xmlParserCtxtPtr ctxt, int options, const char *encodi + } + #ifdef LIBXML_SAX1_ENABLED + if (options & XML_PARSE_SAX1) { +- ctxt->sax->startElement = xmlSAX2StartElement; +- ctxt->sax->endElement = xmlSAX2EndElement; + ctxt->sax->startElementNs = NULL; + ctxt->sax->endElementNs = NULL; + ctxt->sax->initialized = 1; +-- +GitLab + diff --git a/gvsbuild/patches/libxml2/0002-sax-always-initialize-SAX1-element-handlers.patch b/gvsbuild/patches/libxml2/0002-sax-always-initialize-SAX1-element-handlers.patch new file mode 100644 index 000000000..ec9968169 --- /dev/null +++ b/gvsbuild/patches/libxml2/0002-sax-always-initialize-SAX1-element-handlers.patch @@ -0,0 +1,67 @@ +From 235b15a590eecf97b09e87bdb7e4f8333e9de129 Mon Sep 17 00:00:00 2001 +From: Nick Wellnhofer +Date: Mon, 8 May 2023 17:58:02 +0200 +Subject: [PATCH] SAX: Always initialize SAX1 element handlers + +Follow-up to commit d0c3f01e. A parser context will be initialized to +SAX version 2, but this can be overridden with XML_PARSE_SAX1 later, +so we must initialize the SAX1 element handlers as well. + +Change the check in xmlDetectSAX2 to only look for XML_SAX2_MAGIC, so +we don't switch to SAX1 if the SAX2 element handlers are NULL. +--- + SAX2.c | 11 +++++++---- + parser.c | 5 +---- + 2 files changed, 8 insertions(+), 8 deletions(-) + +diff --git a/SAX2.c b/SAX2.c +index b6be1a6c4..910193892 100644 +--- a/SAX2.c ++++ b/SAX2.c +@@ -2874,20 +2874,23 @@ xmlSAXVersion(xmlSAXHandler *hdlr, int version) + { + if (hdlr == NULL) return(-1); + if (version == 2) { +- hdlr->startElement = NULL; +- hdlr->endElement = NULL; + hdlr->startElementNs = xmlSAX2StartElementNs; + hdlr->endElementNs = xmlSAX2EndElementNs; + hdlr->serror = NULL; + hdlr->initialized = XML_SAX2_MAGIC; + #ifdef LIBXML_SAX1_ENABLED + } else if (version == 1) { +- hdlr->startElement = xmlSAX2StartElement; +- hdlr->endElement = xmlSAX2EndElement; + hdlr->initialized = 1; + #endif /* LIBXML_SAX1_ENABLED */ + } else + return(-1); ++#ifdef LIBXML_SAX1_ENABLED ++ hdlr->startElement = xmlSAX2StartElement; ++ hdlr->endElement = xmlSAX2EndElement; ++#else ++ hdlr->startElement = NULL; ++ hdlr->endElement = NULL; ++#endif /* LIBXML_SAX1_ENABLED */ + hdlr->internalSubset = xmlSAX2InternalSubset; + hdlr->externalSubset = xmlSAX2ExternalSubset; + hdlr->isStandalone = xmlSAX2IsStandalone; +diff --git a/parser.c b/parser.c +index 0c8bed129..e133fe0a6 100644 +--- a/parser.c ++++ b/parser.c +@@ -842,10 +842,7 @@ xmlDetectSAX2(xmlParserCtxtPtr ctxt) { + if (ctxt == NULL) return; + sax = ctxt->sax; + #ifdef LIBXML_SAX1_ENABLED +- if ((sax) && (sax->initialized == XML_SAX2_MAGIC) && +- ((sax->startElementNs != NULL) || +- (sax->endElementNs != NULL) || +- ((sax->startElement == NULL) && (sax->endElement == NULL)))) ++ if ((sax) && (sax->initialized == XML_SAX2_MAGIC)) + ctxt->sax2 = 1; + #else + ctxt->sax2 = 1; +-- +GitLab + diff --git a/gvsbuild/projects/libxml2.py b/gvsbuild/projects/libxml2.py index aa7ae96e7..b6a177f4a 100644 --- a/gvsbuild/projects/libxml2.py +++ b/gvsbuild/projects/libxml2.py @@ -32,6 +32,10 @@ def __init__(self): archive_url="https://download.gnome.org/sources/libxml2/{major}.{minor}/libxml2-{version}.tar.xz", hash="3727b078c360ec69fa869de14bd6f75d7ee8d36987b071e6928d4720a28df3a6", dependencies=["win-iconv", "meson", "ninja"], + patches=[ + "0001-parser-fix-old-SAX1-parser-with-custom-callbacks.patch", + "0002-sax-always-initialize-SAX1-element-handlers.patch", + ], ) def build(self):