Skip to content

Commit 24cc5f8

Browse files
committed
Prevent access to external files/URLs via XML entity references.
xml_parse() would attempt to fetch external files or URLs as needed to resolve DTD and entity references in an XML value, thus allowing unprivileged database users to attempt to fetch data with the privileges of the database server. While the external data wouldn't get returned directly to the user, portions of it could be exposed in error messages if the data didn't parse as valid XML; and in any case the mere ability to check existence of a file might be useful to an attacker. The ideal solution to this would still allow fetching of references that are listed in the host system's XML catalogs, so that documents can be validated according to installed DTDs. However, doing that with the available libxml2 APIs appears complex and error-prone, so we're not going to risk it in a security patch that necessarily hasn't gotten wide review. So this patch merely shuts off all access, causing any external fetch to silently expand to an empty string. A future patch may improve this. In HEAD and 9.2, also suppress warnings about undefined entities, which would otherwise occur as a result of not loading referenced DTDs. Previous branches don't show such warnings anyway, due to different error handling arrangements. Credit to Noah Misch for first reporting the problem, and for much work towards a solution, though this simplistic approach was not his preference. Also thanks to Daniel Veillard for consultation. Security: CVE-2012-3489
1 parent 1bfe3d6 commit 24cc5f8

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

src/backend/utils/adt/xml.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#ifdef USE_LIBXML
4949
#include <libxml/chvalid.h>
5050
#include <libxml/parser.h>
51+
#include <libxml/parserInternals.h>
5152
#include <libxml/tree.h>
5253
#include <libxml/uri.h>
5354
#include <libxml/xmlerror.h>
@@ -86,6 +87,8 @@ int xmloption;
8687

8788
static StringInfo xml_err_buf = NULL;
8889

90+
static xmlParserInputPtr xmlPgEntityLoader(const char *URL, const char *ID,
91+
xmlParserCtxtPtr ctxt);
8992
static void xml_errorHandler(void *ctxt, const char *msg,...);
9093
static void xml_ereport_by_code(int level, int sqlcode,
9194
const char *msg, int errcode);
@@ -886,6 +889,9 @@ pg_xml_init(void)
886889
/* Now that xml_err_buf exists, safe to call xml_errorHandler */
887890
xmlSetGenericErrorFunc(NULL, xml_errorHandler);
888891

892+
/* set up our entity loader, too */
893+
xmlSetExternalEntityLoader(xmlPgEntityLoader);
894+
889895
#ifdef USE_LIBXMLCONTEXT
890896
/* Set up memory allocation our way, too */
891897
xml_memory_init();
@@ -910,6 +916,9 @@ pg_xml_init(void)
910916
* about, anyway.
911917
*/
912918
xmlSetGenericErrorFunc(NULL, xml_errorHandler);
919+
920+
/* set up our entity loader, too */
921+
xmlSetExternalEntityLoader(xmlPgEntityLoader);
913922
}
914923
}
915924

@@ -1322,6 +1331,25 @@ xml_pstrdup(const char *string)
13221331
#endif /* USE_LIBXMLCONTEXT */
13231332

13241333

1334+
/*
1335+
* xmlPgEntityLoader --- entity loader callback function
1336+
*
1337+
* Silently prevent any external entity URL from being loaded. We don't want
1338+
* to throw an error, so instead make the entity appear to expand to an empty
1339+
* string.
1340+
*
1341+
* We would prefer to allow loading entities that exist in the system's
1342+
* global XML catalog; but the available libxml2 APIs make that a complex
1343+
* and fragile task. For now, just shut down all external access.
1344+
*/
1345+
static xmlParserInputPtr
1346+
xmlPgEntityLoader(const char *URL, const char *ID,
1347+
xmlParserCtxtPtr ctxt)
1348+
{
1349+
return xmlNewStringInputStream(ctxt, (const xmlChar *) "");
1350+
}
1351+
1352+
13251353
/*
13261354
* xml_ereport --- report an XML-related error
13271355
*

src/test/regress/expected/xml.out

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,3 +502,23 @@ SELECT xpath('//b', '<a>one <b>two</b> three <b>etc</b></a>');
502502
{<b>two</b>,<b>etc</b>}
503503
(1 row)
504504

505+
-- External entity references should not leak filesystem information.
506+
SELECT XMLPARSE(DOCUMENT '<!DOCTYPE foo [<!ENTITY c SYSTEM "/etc/passwd">]><foo>&c;</foo>');
507+
xmlparse
508+
-----------------------------------------------------------------
509+
<!DOCTYPE foo [<!ENTITY c SYSTEM "/etc/passwd">]><foo>&c;</foo>
510+
(1 row)
511+
512+
SELECT XMLPARSE(DOCUMENT '<!DOCTYPE foo [<!ENTITY c SYSTEM "/etc/no.such.file">]><foo>&c;</foo>');
513+
xmlparse
514+
-----------------------------------------------------------------------
515+
<!DOCTYPE foo [<!ENTITY c SYSTEM "/etc/no.such.file">]><foo>&c;</foo>
516+
(1 row)
517+
518+
-- This might or might not load the requested DTD, but it mustn't throw error.
519+
SELECT XMLPARSE(DOCUMENT '<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN" "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd"><chapter>&nbsp;</chapter>');
520+
xmlparse
521+
------------------------------------------------------------------------------------------------------------------------------------------------------
522+
<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN" "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd"><chapter>&nbsp;</chapter>
523+
(1 row)
524+

src/test/regress/expected/xml_1.out

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,3 +456,17 @@ LINE 1: SELECT xpath('//b', '<a>one <b>two</b> three <b>etc</b></a>'...
456456
^
457457
DETAIL: This functionality requires the server to be built with libxml support.
458458
HINT: You need to rebuild PostgreSQL using --with-libxml.
459+
-- External entity references should not leak filesystem information.
460+
SELECT XMLPARSE(DOCUMENT '<!DOCTYPE foo [<!ENTITY c SYSTEM "/etc/passwd">]><foo>&c;</foo>');
461+
ERROR: unsupported XML feature
462+
DETAIL: This functionality requires the server to be built with libxml support.
463+
HINT: You need to rebuild PostgreSQL using --with-libxml.
464+
SELECT XMLPARSE(DOCUMENT '<!DOCTYPE foo [<!ENTITY c SYSTEM "/etc/no.such.file">]><foo>&c;</foo>');
465+
ERROR: unsupported XML feature
466+
DETAIL: This functionality requires the server to be built with libxml support.
467+
HINT: You need to rebuild PostgreSQL using --with-libxml.
468+
-- This might or might not load the requested DTD, but it mustn't throw error.
469+
SELECT XMLPARSE(DOCUMENT '<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN" "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd"><chapter>&nbsp;</chapter>');
470+
ERROR: unsupported XML feature
471+
DETAIL: This functionality requires the server to be built with libxml support.
472+
HINT: You need to rebuild PostgreSQL using --with-libxml.

src/test/regress/sql/xml.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,9 @@ SELECT xpath('', '<!-- error -->');
163163
SELECT xpath('//text()', '<local:data xmlns:local="http://127.0.0.1"><local:piece id="1">number one</local:piece><local:piece id="2" /></local:data>');
164164
SELECT xpath('//loc:piece/@id', '<local:data xmlns:local="http://127.0.0.1"><local:piece id="1">number one</local:piece><local:piece id="2" /></local:data>', ARRAY[ARRAY['loc', 'http://127.0.0.1']]);
165165
SELECT xpath('//b', '<a>one <b>two</b> three <b>etc</b></a>');
166+
167+
-- External entity references should not leak filesystem information.
168+
SELECT XMLPARSE(DOCUMENT '<!DOCTYPE foo [<!ENTITY c SYSTEM "/etc/passwd">]><foo>&c;</foo>');
169+
SELECT XMLPARSE(DOCUMENT '<!DOCTYPE foo [<!ENTITY c SYSTEM "/etc/no.such.file">]><foo>&c;</foo>');
170+
-- This might or might not load the requested DTD, but it mustn't throw error.
171+
SELECT XMLPARSE(DOCUMENT '<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN" "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd"><chapter>&nbsp;</chapter>');

0 commit comments

Comments
 (0)