From ca7d59a7943c3c2668c71dcb2cc74b88b444e8ea Mon Sep 17 00:00:00 2001 From: joe Date: Tue, 18 Aug 2009 14:19:39 +0000 Subject: Merge r1687 from trunk: Security fix for CVE-2009-2473: prevent the "billion laughs" attack against expat: * src/ne_xml.c (ne_xml_create) [HAVE_EXPAT]: Register entity decl handler. [HAVE_LIBXML]: Use xmlCtxtUseOptions interface. (entity_declaration): New function. * test/xml.c (fail_parse): Add billion laughs test case. * test/run.sh: Limit run-time CPU use to 120 seconds. git-svn-id: http://svn.webdav.org/repos/projects/neon/branches/0.28.x@1688 61a7d7f5-40b7-0310-9c16-bb0ea8cb1845 --- src/ne_xml.c | 44 +++++++++++++++++++++++++++++++++++++++++++- test/run.sh | 1 + test/xml.c | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 1 deletion(-) diff --git a/src/ne_xml.c b/src/ne_xml.c index 7c95a2e..3870701 100644 --- a/src/ne_xml.c +++ b/src/ne_xml.c @@ -1,6 +1,6 @@ /* Wrapper interface to XML parser - Copyright (C) 1999-2007, Joe Orton + Copyright (C) 1999-2007, 2009, Joe Orton This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public @@ -405,6 +405,28 @@ static void end_element(void *userdata, const ne_xml_char *name) destroy_element(elm); } +#if defined(HAVE_EXPAT) && XML_MAJOR_VERSION > 1 +/* Stop the parser if an entity declaration is hit. */ +static void entity_declaration(void *userData, const XML_Char *entityName, + int is_parameter_entity, const XML_Char *value, + int value_length, const XML_Char *base, + const XML_Char *systemId, const XML_Char *publicId, + const XML_Char *notationName) +{ + ne_xml_parser *parser = userData; + + NE_DEBUG(NE_DBG_XMLPARSE, "XML: entity declaration [%s]. Failing.\n", + entityName); + + XML_StopParser(parser->parser, XML_FALSE); +} +#elif defined(HAVE_EXPAT) +/* A noop default_handler. */ +static void default_handler(void *userData, const XML_Char *s, int len) +{ +} +#endif + /* Find a namespace definition for 'prefix' in given element, where * length of prefix is 'pfxlen'. Returns the URI or NULL. */ static const char *resolve_nspace(const struct element *elm, @@ -459,14 +481,34 @@ ne_xml_parser *ne_xml_create(void) XML_SetCharacterDataHandler(p->parser, char_data); XML_SetUserData(p->parser, (void *) p); XML_SetXmlDeclHandler(p->parser, decl_handler); + + /* Prevent the "billion laughs" attack against expat by disabling + * internal entity expansion. With 2.x, forcibly stop the parser + * if an entity is declared - this is safer and a more obvious + * failure mode. With older versions, installing a noop + * DefaultHandler means that internal entities will be expanded as + * the empty string, which is also sufficient to prevent the + * attack. */ +#if XML_MAJOR_VERSION > 1 + XML_SetEntityDeclHandler(p->parser, entity_declaration); #else + XML_SetDefaultHandler(p->parser, default_handler); +#endif + +#else /* HAVE_LIBXML */ p->parser = xmlCreatePushParserCtxt(&sax_handler, (void *)p, NULL, 0, NULL); if (p->parser == NULL) { abort(); } +#if LIBXML_VERSION < 20602 p->parser->replaceEntities = 1; +#else + /* Enable expansion of entities, and disable network access. */ + xmlCtxtUseOptions(p->parser, XML_PARSE_NOENT | XML_PARSE_NONET); #endif + +#endif /* HAVE_LIBXML || HAVE_EXPAT */ return p; } diff --git a/test/run.sh b/test/run.sh index 4e9c308..71e75f5 100644 --- a/test/run.sh +++ b/test/run.sh @@ -3,6 +3,7 @@ rm -f debug.log child.log ulimit -c unlimited +ulimit -t 120 unset LANG unset LC_MESSAGES diff --git a/test/xml.c b/test/xml.c index ac979a2..eafa94d 100644 --- a/test/xml.c +++ b/test/xml.c @@ -441,6 +441,44 @@ static int fail_parse(void) "\xEF\xBB" PFX "", "\xEF" PFX "", +"\ +\ +\ +\ +\ +\ +\ +\ +\ +\ +\ +\ +\ +\ +\ +\ +\ +\ +\ +\ +\ +\ +\ +\ +\ +\ +\ +\ +\ +\ +\ +\ +\ +]>\ +&laugh30;\ +", + NULL }; int n; -- cgit v1.2.1