1 From c1d1f7121194036608bf555f08d3062a36fd344b Mon Sep 17 00:00:00 2001
2 From: Nick Wellnhofer <wellnhofer@aevum.de>
3 Date: Tue, 28 Jun 2016 18:34:52 +0200
4 Subject: [PATCH] Disallow namespace nodes in XPointer ranges
6 Namespace nodes must be copied to avoid use-after-free errors.
7 But they don't necessarily have a physical representation in a
8 document, so simply disallow them in XPointer ranges.
14 Signed-off-by: Baruch Siach <baruch@tkos.co.il>
16 Patch status: upstream commit c1d1f712119403
18 xpointer.c | 149 +++++++++++++++++++++++--------------------------------------
19 1 file changed, 56 insertions(+), 93 deletions(-)
21 diff --git a/xpointer.c b/xpointer.c
22 index a7b03fbdae16..694d120e2e0b 100644
25 @@ -320,6 +320,45 @@ xmlXPtrRangesEqual(xmlXPathObjectPtr range1, xmlXPathObjectPtr range2) {
29 + * xmlXPtrNewRangeInternal:
30 + * @start: the starting node
31 + * @startindex: the start index
32 + * @end: the ending point
33 + * @endindex: the ending index
35 + * Internal function to create a new xmlXPathObjectPtr of type range
37 + * Returns the newly created object.
39 +static xmlXPathObjectPtr
40 +xmlXPtrNewRangeInternal(xmlNodePtr start, int startindex,
41 + xmlNodePtr end, int endindex) {
42 + xmlXPathObjectPtr ret;
45 + * Namespace nodes must be copied (see xmlXPathNodeSetDupNs).
46 + * Disallow them for now.
48 + if ((start != NULL) && (start->type == XML_NAMESPACE_DECL))
50 + if ((end != NULL) && (end->type == XML_NAMESPACE_DECL))
53 + ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
55 + xmlXPtrErrMemory("allocating range");
58 + memset(ret, 0, sizeof(xmlXPathObject));
59 + ret->type = XPATH_RANGE;
61 + ret->index = startindex;
63 + ret->index2 = endindex;
69 * @start: the starting node
70 * @startindex: the start index
71 @@ -344,17 +383,7 @@ xmlXPtrNewRange(xmlNodePtr start, int startindex,
75 - ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
77 - xmlXPtrErrMemory("allocating range");
80 - memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
81 - ret->type = XPATH_RANGE;
83 - ret->index = startindex;
85 - ret->index2 = endindex;
86 + ret = xmlXPtrNewRangeInternal(start, startindex, end, endindex);
87 xmlXPtrRangeCheckOrder(ret);
90 @@ -381,17 +410,8 @@ xmlXPtrNewRangePoints(xmlXPathObjectPtr start, xmlXPathObjectPtr end) {
91 if (end->type != XPATH_POINT)
94 - ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
96 - xmlXPtrErrMemory("allocating range");
99 - memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
100 - ret->type = XPATH_RANGE;
101 - ret->user = start->user;
102 - ret->index = start->index;
103 - ret->user2 = end->user;
104 - ret->index2 = end->index;
105 + ret = xmlXPtrNewRangeInternal(start->user, start->index, end->user,
107 xmlXPtrRangeCheckOrder(ret);
110 @@ -416,17 +436,7 @@ xmlXPtrNewRangePointNode(xmlXPathObjectPtr start, xmlNodePtr end) {
111 if (start->type != XPATH_POINT)
114 - ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
116 - xmlXPtrErrMemory("allocating range");
119 - memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
120 - ret->type = XPATH_RANGE;
121 - ret->user = start->user;
122 - ret->index = start->index;
125 + ret = xmlXPtrNewRangeInternal(start->user, start->index, end, -1);
126 xmlXPtrRangeCheckOrder(ret);
129 @@ -453,17 +463,7 @@ xmlXPtrNewRangeNodePoint(xmlNodePtr start, xmlXPathObjectPtr end) {
130 if (end->type != XPATH_POINT)
133 - ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
135 - xmlXPtrErrMemory("allocating range");
138 - memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
139 - ret->type = XPATH_RANGE;
142 - ret->user2 = end->user;
143 - ret->index2 = end->index;
144 + ret = xmlXPtrNewRangeInternal(start, -1, end->user, end->index);
145 xmlXPtrRangeCheckOrder(ret);
148 @@ -486,17 +486,7 @@ xmlXPtrNewRangeNodes(xmlNodePtr start, xmlNodePtr end) {
152 - ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
154 - xmlXPtrErrMemory("allocating range");
157 - memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
158 - ret->type = XPATH_RANGE;
163 + ret = xmlXPtrNewRangeInternal(start, -1, end, -1);
164 xmlXPtrRangeCheckOrder(ret);
167 @@ -516,17 +506,7 @@ xmlXPtrNewCollapsedRange(xmlNodePtr start) {
171 - ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
173 - xmlXPtrErrMemory("allocating range");
176 - memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
177 - ret->type = XPATH_RANGE;
182 + ret = xmlXPtrNewRangeInternal(start, -1, NULL, -1);
186 @@ -541,6 +521,8 @@ xmlXPtrNewCollapsedRange(xmlNodePtr start) {
189 xmlXPtrNewRangeNodeObject(xmlNodePtr start, xmlXPathObjectPtr end) {
190 + xmlNodePtr endNode;
192 xmlXPathObjectPtr ret;
195 @@ -549,7 +531,12 @@ xmlXPtrNewRangeNodeObject(xmlNodePtr start, xmlXPathObjectPtr end) {
199 + endNode = end->user;
200 + endIndex = end->index;
203 + endNode = end->user2;
204 + endIndex = end->index2;
208 @@ -557,39 +544,15 @@ xmlXPtrNewRangeNodeObject(xmlNodePtr start, xmlXPathObjectPtr end) {
210 if (end->nodesetval->nodeNr <= 0)
212 + endNode = end->nodesetval->nodeTab[end->nodesetval->nodeNr - 1];
220 - ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
222 - xmlXPtrErrMemory("allocating range");
225 - memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
226 - ret->type = XPATH_RANGE;
229 - switch (end->type) {
231 - ret->user2 = end->user;
232 - ret->index2 = end->index;
235 - ret->user2 = end->user2;
236 - ret->index2 = end->index2;
238 - case XPATH_NODESET: {
239 - ret->user2 = end->nodesetval->nodeTab[end->nodesetval->nodeNr - 1];
247 + ret = xmlXPtrNewRangeInternal(start, -1, endNode, endIndex);
248 xmlXPtrRangeCheckOrder(ret);