1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2025-01-27 14:03:36 +03:00

461 Commits

Author SHA1 Message Date
Nick Wellnhofer
e6ec58ecf7 Fix null deref in XPointer expression error path
Make sure that the filter functions introduced with commit c2f4da1a
return node-sets without NULL pointers also in the error case.

Found by OSS-Fuzz.
2020-09-21 12:49:36 +02:00
Nick Wellnhofer
8b88503a27 Don't call xmlXPathInit directly
Call xmlInitParser which uses a lock to avoid race conditions.

Fixes #184.
2020-09-18 19:15:27 +02:00
Nick Wellnhofer
6f1470a5d6 Hardcode maximum XPath recursion depth
Always limit nested functions calls to 5000. This avoids call stack
overflows with deeply nested expressions.

The expression parser produces about 10 nested function calls when
parsing a subexpression in parentheses, so the effective nesting limit
is about 500 which should be more than enough.

Use a lower limit when fuzzing to account for increased memory usage
when using sanitizers.
2020-08-26 00:22:25 +02:00
Nick Wellnhofer
804c52978f Stop using maxParserDepth in xpath.c
Only use a single maxDepth value.
2020-08-17 03:39:51 +02:00
Nick Wellnhofer
beb7d71a8f Remove misleading comments in xpath.c
Fixes #169
2020-07-13 12:41:19 +02:00
Nick Wellnhofer
9f42f6baaa Don't follow next pointer on documents in xmlXPathRunStreamEval
RVTs from libxslt are document nodes which are linked using the 'next'
pointer. These pointers must never be used to navigate the document
tree. Otherwise, random content from other RVTs could be returned
when evaluating XPath expressions.

It's interesting that this seemingly long-standing bug wasn't
discovered earlier. This issue could also cause severe performance
degradation.

Fixes https://gitlab.gnome.org/GNOME/libxslt/-/issues/37
2020-06-24 15:33:38 +02:00
Nick Wellnhofer
487871b0e3 Fix undefined behavior in xmlXPathTryStreamCompile
&NULL[0] is undefined behavior.
2020-06-10 13:23:43 +02:00
Nick Wellnhofer
20c60886e4 Fix typos
Resolves #133.
2020-03-08 17:41:53 +01:00
Nick Wellnhofer
2c80fc9116 Fix more memory leaks in error paths of XPath parser
Found by OSS-Fuzz.
2019-12-02 11:30:30 +01:00
Nick Wellnhofer
d5f2f74d0f Fix memory leak in error path of XPath expr parser
Also propagate memory errors.

Found by OSS-Fuzz.
2019-11-11 11:27:40 +01:00
Nick Wellnhofer
bfc0f674cc Audit memory error handling in xpath.c
Memory allocation errors in the following functions a often ignored.
Add TODO comments.

- xmlXPathNodeSetCreate
- xmlXPathNodeSetAdd*
- xmlXPathNodeSetMerge*
- xmlXPathNodeSetDupNs

Note that the following functions currently lack a way to propagate
memory errors:

- xmlXPathCompareNodeSets
- xmlXPathEqualNodeSets
2019-10-20 14:39:46 +02:00
Nick Wellnhofer
429d4ecaae Propagate memory errors in valuePush
Currently, many memory allocation errors in xpath.c aren't propagated to
the parser/evaluation context and for the most part ignored. Most
XPath objects allocated via one of the New, Wrap or Copy functions end
up being pushed on the stack, so adding a check in valuePush handles
many cases without much effort.

Also simplify the code a little and make sure to return -1 in case of
error.
2019-10-20 14:38:05 +02:00
Nick Wellnhofer
390f05e703 Propagate memory errors in xmlXPathCompExprAdd
Make sure that memory errors in xmlXPathCompExprAdd are propagated to
the parser context. Hitting the step limit or running out of memory
without raising an error could also lead to an out-of-bounds read.

Also fixes a memory leak in xmlXPathErrMemory.

Found by OSS-Fuzz.
2019-10-20 14:13:05 +02:00
Jared Yanovich
2a350ee9b4 Large batch of typo fixes
Closes #109.
2019-09-30 18:04:38 +02:00
Nick Wellnhofer
6705f4d28e Remove executable bit from non-executable files 2019-09-16 15:48:59 +02:00
Jan Pokorný
5c0e48b883 Fix typo: xpath: simpli{ -> fi}ed
Signed-off-by: Jan Pokorný <jpokorny@redhat.com>
2019-08-25 13:31:18 +02:00
zhouzhongyuan
59028ba08c Fix possible null dereference in xmlXPathIdFunction
If a certain memory allocation fails, xmlXPathIdFunction would
dereference a null pointer.

Closes #77.
2019-08-25 13:19:29 +02:00
Daniel Richard G
b17e3d1c2b Work around buggy ceil() function on AIX
AIX has a buggy ceil() function that does not handle negative-zero
correctly.

Closes #79.
2019-08-02 13:18:13 +02:00
Nick Wellnhofer
e79a903f3e Remove redundant code in xmlXPathCompRelationalExpr
Thanks to Anish K Kurian for the report. Closes #59.
2019-05-20 13:38:22 +02:00
Nick Wellnhofer
44e7a0d5f7 Annotate functions with __attribute__((no_sanitize)) 2019-05-20 13:38:22 +02:00
Nick Wellnhofer
14ed63b7d0 Limit recursion depth in xmlXPathCompOpEvalPredicate 2019-05-08 12:00:51 +02:00
Nick Wellnhofer
f75256e7db Remove unreachable code in xmlXPathCountFunction
After the initial test, the condition

    (type == XPATH_NODESET) || (type == XPATH_XSLT_TREE)

always holds true.
2019-04-23 18:04:19 +02:00
Nick Wellnhofer
949eced484 Fix null deref in previous commit 2019-04-22 16:04:26 +02:00
Nick Wellnhofer
c2f4da1a93 Improve XPath predicate and filter evaluation
Consolidate code paths evaluating XPath predicates and filters.

Don't push context node on stack when evaluating predicates. I have no
idea why this was done. It seems completely useless and trying to pop
the context node from a corrupted stack has already caused security
issues.

Filter nodesets in-place and don't create node sets with NULL gaps which
allows to simplify merging a great deal. Simply move matched nodes
backward and create a compact node set.

Merge xmlXPathCompOpEvalPositionalPredicate into
xmlXPathCompOpEvalPredicate.
2019-04-22 14:48:46 +02:00
Nick Wellnhofer
012f8e9284 Limit recursion depth in xmlXPathOptimizeExpression 2019-04-20 17:01:19 +02:00
Nick Wellnhofer
93a1d22380 Fix memory leaks in xmlXPathParseNameComplex error paths
Found by OSS-Fuzz.
2019-04-16 13:37:47 +02:00
Nick Wellnhofer
2d97a97aa5 Optional recursion limit when parsing XPath expressions
Useful to avoid call stack overflows when fuzzing. Note that parsing a
parenthesized expression currently consumes more than 10 stack frames,
so this limit should be set rather low.
2019-04-11 15:35:57 +02:00
Nick Wellnhofer
64115ed62d Optional recursion limit when evaluating XPath expressions
Useful to avoid call stack overflows when fuzzing.
2019-04-11 15:35:57 +02:00
Nick Wellnhofer
5153c7bace Use break statements in xmlXPathCompOpEval
This prepares for the next commit.
2019-04-11 15:35:57 +02:00
Nick Wellnhofer
852c93a2dc Optional XPath operation limit
Optionally limit the maximum numbers of XPath operations when evaluating
an expression. Useful to avoid timeouts when fuzzing. The following
operations count towards the limit:

- XPath operations
- Location step iterations
- Union operations

Enabled by setting opLimit to a non-zero value. Note that it's the user's
responsibility to reset opCount. This allows to enforce the operation
limit across multiple reuses of an XPath context.
2019-04-11 15:35:57 +02:00
Nick Wellnhofer
236dd6ab2e Check XPath stack after calling functions
Check that there's exactly one return value on the stack after calling
XPath functions. Otherwise, functions that corrupt the stack without
signaling an error could lead to memory errors.

Found with libFuzzer and UBSan.
2019-03-29 13:03:03 +01:00
Nick Wellnhofer
30a6533e01 Fix float casts in xmlXPathSubstringFunction
Rewrite conversion of double to int in xmlXPathSubstringFunction, adding
range checks to avoid undefined behavior. Make sure to add start and
length as floating-point numbers before converting to int. Fix a bug
when rounding negative start indices.

Remove unneeded calls to xmlXPathIs{Inf,NaN} and rely on IEEE math
instead. Avoid computing the string length. xmlUTF8Strsub works as
expected if the length of the requested substring exceeds the input.

Found with libFuzzer and UBSan.
2019-03-08 14:29:59 +01:00
Nick Wellnhofer
a436374994 Fix nullptr deref with XPath logic ops
If the XPath stack is corrupted, for example by a misbehaving extension
function, the "and" and "or" XPath operators could dereference NULL
pointers. Check that the XPath stack isn't empty and optimize the
logic operators slightly.

Closes: https://gitlab.gnome.org/GNOME/libxml2/issues/5

Also see
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=901817
https://bugzilla.redhat.com/show_bug.cgi?id=1595985

This is CVE-2018-14404.

Thanks to Guy Inbar for the report.
2018-07-30 12:54:38 +02:00
Nick Wellnhofer
b7c50b8dde Remove stray character from comment
Fixes bug #795316:

https://bugzilla.gnome.org/show_bug.cgi?id=795316
2018-07-30 12:40:59 +02:00
Nick Wellnhofer
fa33bf317a Improve restoring of context size and position
Restore context size and position where it is modified, not in
seemingly random places.
2018-04-16 20:23:48 +02:00
Nick Wellnhofer
665df41dcc Simplify and harden nodeset filtering
If a nodeset to be filtered is empty, it can be returned without popping
it from the stack.

Make sure to restore the context node in all error paths and never set
it to NULL.

Save and restore the context node in RANGETO operations.
2018-04-16 19:45:37 +02:00
Nick Wellnhofer
029d0e960c Avoid unnecessary backups of the context node 2018-04-16 18:28:42 +02:00
Nick Wellnhofer
938835e763 Don't change context node in xmlXPathRoot 2018-04-16 18:28:42 +02:00
Nick Wellnhofer
e22a83b1d0 Stop using XPATH_OP_RESET
It only sets the context node to NULL which doesn't seem useful and can
even cause bugs like bug #795299:

https://bugzilla.gnome.org/show_bug.cgi?id=795299
2018-04-16 18:20:43 +02:00
Nick Wellnhofer
ebe12882ee Fix inconsistency in xmlXPathIsInf
We don't use HUGE_VAL for INFINITY after the most recent fix.
2018-04-16 18:18:11 +02:00
Nick Wellnhofer
7abec67147 NaN and Inf fixes for pre-C99 compilers
On some pre-C99 compilers, the NAN and INFINITY macros don't expand to
constant expressions.

Some MSVC versions complain about floating point division by zero in
constants.

Thanks to Fabrice Manfroi for the report.
2018-03-16 15:09:42 +01:00
Nick Wellnhofer
ddbb075b70 Fix xmlXPathIsNaN broken by recent commit 2017-11-27 14:33:26 +01:00
Nick Wellnhofer
b21895717d Fix -Wenum-compare warnings
Also fixes a serious bug in xmlXPtrNewRangeNodePoint.
2017-11-27 13:42:30 +01:00
Nick Wellnhofer
13acadbb1c Ignore function pointer cast warnings
Use GCC pragmas as these warnings are enabled by -Wpedantic and there's
no way to disable them selectively.
2017-11-14 16:01:48 +01:00
Nick Wellnhofer
8813f397f8 Simplify XPath NaN, inf and -0 handling
Use C99 macros NAN, INFINITY, isnan, isinf. If they're not available:

- Assume that (0.0 / 0.0) generates a NaN and !(x == x) tests for NaN.
- Use C89's HUGE_VAL for INFINITY.

Remove manual handling of NaN, infinity and negative zero in functions
xmlXPathValueFlipSign and xmlXPathDivValues.

Remove xmlXPathGetSign. All the tests for negative zero can be replaced
with a test for negative or positive zero.

Simplify xmlXPathRoundFunction.

Remove Trio dependency.

This should work on IEEE 754 compliant implementations even if the C99
macros aren't available, but will likely break some ancient platforms.
If problems arise, my plan is to port the relevant trionan.c solution
to xpath.c. Note that non-compliant implementations are impossible
to fully support, anyway, since XPath requires IEEE 754.
2017-11-14 15:58:40 +01:00
Nick Wellnhofer
e03f0a199a Fix hash callback signatures
Make sure that all parameters and return values of hash callback
functions exactly match the callback function type. This is required
to pass clang's Control Flow Integrity checks and to allow compilation
to asm.js with Emscripten.

Fixes bug 784861.
2017-11-09 16:42:47 +01:00
J. Peter Mugaas
d2c329a9a4 Fix -Wimplicit-fallthrough warnings
Add "falls through" comments to quench implicit-fallthrough warnings
which are enabled by -Wextra under GCC 7.
2017-10-21 13:49:31 +02:00
Nick Wellnhofer
d422b954be Fix pointer/int cast warnings on 64-bit Windows
On 64-bit Windows, `long` is 32 bits wide and can't hold a pointer.
Switch to ptrdiff_t instead which should be the same size as a pointer
on every somewhat sane platform without requiring C99 types like
intptr_t.

Fixes bug 788312.

Thanks to J. Peter Mugaas for the report and initial patch.
2017-10-09 13:47:49 +02:00
Nick Wellnhofer
5af594d8bc Fix comparison of nodesets to strings
Fix two bugs in xmlXPathNodeValHash which could lead to errors when
comparing nodesets to strings:

- Only use contents of text nodes to compute the hash for element nodes.
  Comments, PIs, and other node types don't affect the string-value and
  must be ignored.
- Reset `string` to NULL for node types other than text.

Reported by Aleksei on the mailing list:

    https://mail.gnome.org/archives/xml/2017-September/msg00016.html
2017-10-07 15:22:57 +02:00
Nick Wellnhofer
0f3b843b35 Fix XPath stack frame logic
Move the calls to xmlXPathSetFrame and xmlXPathPopFrame around in
xmlXPathCompOpEvalPositionalPredicate to make sure that the context
object on the stack is actually protected. Otherwise, memory corruption
can occur when calling sloppily coded XPath extension functions.

Fixes bug 783160.
2017-09-21 17:55:39 +02:00