Skip to content

Commit f8ebef2

Browse files
Brian C. Youngandi34
authored andcommitted
DO NOT MERGE: Fix XPointer paths beginning with range-to
The old code would invoke the broken xmlXPtrRangeToFunction. range-to isn't really a function but a special kind of location step. Remove this function and always handle range-to in the XPath code. The old xmlXPtrRangeToFunction could also be abused to trigger a use-after-free error with the potential for remote code execution. Found with afl-fuzz. Fixes CVE-2016-5131. Bug: 36554209 Change-Id: I2bd369290a884c432d16796884d48db6285f8502 (cherry picked from commit e875e1cd1fc92fd2daa57826024125cbd0b195c7)
1 parent a2bb4a2 commit f8ebef2

File tree

2 files changed

+12
-71
lines changed

2 files changed

+12
-71
lines changed

xpath.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10596,13 +10596,18 @@ xmlXPathCompPathExpr(xmlXPathParserContextPtr ctxt) {
1059610596
lc = 1;
1059710597
break;
1059810598
} else if ((NXT(len) == '(')) {
10599-
/* Note Type or Function */
10599+
/* Node Type or Function */
1060010600
if (xmlXPathIsNodeType(name)) {
1060110601
#ifdef DEBUG_STEP
1060210602
xmlGenericError(xmlGenericErrorContext,
1060310603
"PathExpr: Type search\n");
1060410604
#endif
1060510605
lc = 1;
10606+
#ifdef LIBXML_XPTR_ENABLED
10607+
} else if (ctxt->xptr &&
10608+
xmlStrEqual(name, BAD_CAST "range-to")) {
10609+
lc = 1;
10610+
#endif
1060610611
} else {
1060710612
#ifdef DEBUG_STEP
1060810613
xmlGenericError(xmlGenericErrorContext,

xpointer.c

Lines changed: 6 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,8 +1339,6 @@ xmlXPtrNewContext(xmlDocPtr doc, xmlNodePtr here, xmlNodePtr origin) {
13391339
ret->here = here;
13401340
ret->origin = origin;
13411341

1342-
xmlXPathRegisterFunc(ret, (xmlChar *)"range-to",
1343-
xmlXPtrRangeToFunction);
13441342
xmlXPathRegisterFunc(ret, (xmlChar *)"range",
13451343
xmlXPtrRangeFunction);
13461344
xmlXPathRegisterFunc(ret, (xmlChar *)"range-inside",
@@ -2226,76 +2224,14 @@ xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs) {
22262224
* @nargs: the number of args
22272225
*
22282226
* Implement the range-to() XPointer function
2227+
*
2228+
* Obsolete. range-to is not a real function but a special type of location
2229+
* step which is handled in xpath.c.
22292230
*/
22302231
void
2231-
xmlXPtrRangeToFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2232-
xmlXPathObjectPtr range;
2233-
const xmlChar *cur;
2234-
xmlXPathObjectPtr res, obj;
2235-
xmlXPathObjectPtr tmp;
2236-
xmlLocationSetPtr newset = NULL;
2237-
xmlNodeSetPtr oldset;
2238-
int i;
2239-
2240-
if (ctxt == NULL) return;
2241-
CHECK_ARITY(1);
2242-
/*
2243-
* Save the expression pointer since we will have to evaluate
2244-
* it multiple times. Initialize the new set.
2245-
*/
2246-
CHECK_TYPE(XPATH_NODESET);
2247-
obj = valuePop(ctxt);
2248-
oldset = obj->nodesetval;
2249-
ctxt->context->node = NULL;
2250-
2251-
cur = ctxt->cur;
2252-
newset = xmlXPtrLocationSetCreate(NULL);
2253-
2254-
for (i = 0; i < oldset->nodeNr; i++) {
2255-
ctxt->cur = cur;
2256-
2257-
/*
2258-
* Run the evaluation with a node list made of a single item
2259-
* in the nodeset.
2260-
*/
2261-
ctxt->context->node = oldset->nodeTab[i];
2262-
tmp = xmlXPathNewNodeSet(ctxt->context->node);
2263-
valuePush(ctxt, tmp);
2264-
2265-
xmlXPathEvalExpr(ctxt);
2266-
CHECK_ERROR;
2267-
2268-
/*
2269-
* The result of the evaluation need to be tested to
2270-
* decided whether the filter succeeded or not
2271-
*/
2272-
res = valuePop(ctxt);
2273-
range = xmlXPtrNewRangeNodeObject(oldset->nodeTab[i], res);
2274-
if (range != NULL) {
2275-
xmlXPtrLocationSetAdd(newset, range);
2276-
}
2277-
2278-
/*
2279-
* Cleanup
2280-
*/
2281-
if (res != NULL)
2282-
xmlXPathFreeObject(res);
2283-
if (ctxt->value == tmp) {
2284-
res = valuePop(ctxt);
2285-
xmlXPathFreeObject(res);
2286-
}
2287-
2288-
ctxt->context->node = NULL;
2289-
}
2290-
2291-
/*
2292-
* The result is used as the new evaluation set.
2293-
*/
2294-
xmlXPathFreeObject(obj);
2295-
ctxt->context->node = NULL;
2296-
ctxt->context->contextSize = -1;
2297-
ctxt->context->proximityPosition = -1;
2298-
valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2232+
xmlXPtrRangeToFunction(xmlXPathParserContextPtr ctxt,
2233+
int nargs ATTRIBUTE_UNUSED) {
2234+
XP_ERROR(XPATH_EXPR_ERROR);
22992235
}
23002236

23012237
/**

0 commit comments

Comments
 (0)