[PATCH] contrib/xml2: backend crash in xpath_nodeset() on the namespace axis

From: Andrey Chernyy <andrey(dot)cherny(at)tantorlabs(dot)com>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: Michael Paquier <michael(at)paquier(dot)xyz>
Subject: [PATCH] contrib/xml2: backend crash in xpath_nodeset() on the namespace axis
Date: 2026-06-11 00:14:36
Message-ID: 20260611031436.5afde3cb@andrnote
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hi,

In the xml2 extension, xpath_nodeset() crashes the backend on an XPath
that selects namespace-axis nodes. xpath_nodeset() is executable by
PUBLIC by default, so any role that can run SQL can crash the server
with one query:

CREATE EXTENSION xml2;
SELECT xpath_nodeset('<root
xmlns:foo="http://example.com/foo"><child/></root>',
'//namespace::*');

Cause: pgxmlNodeSetToText() (contrib/xml2/xpath.c:197) calls

xmlNodeDump(buf, nodeset->nodeTab[i]->doc, nodeset->nodeTab[i], 1,
0);

with no node-type check. Namespace-axis results are XML_NAMESPACE_DECL
nodes (xmlNs structs cast to xmlNodePtr), so reading the node's ->doc
field runs past the smaller xmlNs allocation, and the bogus value is
then dereferenced as the document by xmlNodeDump(). xpath_list() and
xpath_table() already avoid this via xmlXPathCastNodeToString(); only
the xmlNodeDump() path is exposed.

Reproduced on master; the same unguarded xmlNodeDump() call in
pgxmlNodeSetToText() is present on every supported back-branch (REL_18
through REL_14).

Patch attached: render XML_NAMESPACE_DECL nodes with
xmlXPathCastNodeToString() like xpath_table() does. The repro then
returns the namespace text, ordinary node-set output is unchanged, and
the xml2 regression test passes.

--
Andrey Chernyy

Attachment Content-Type Size
0001-xml2-fix-crash-on-namespace-nodes-in-xpath_nodeset.patch text/x-patch 1.8 KB
xml2-nsdump-crash-repro.sql application/sql 161 bytes

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Tender Wang 2026-06-11 01:03:22 Re: BUG #19517: Eager Aggregation produces wrong count(*) when pushed into RHS of Hash Semi Join
Previous Message Matheus Alcantara 2026-06-10 20:19:09 Re: BUG #19517: Eager Aggregation produces wrong count(*) when pushed into RHS of Hash Semi Join