1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2024-10-26 12:25:09 +03:00
Commit Graph

175 Commits

Author SHA1 Message Date
Nick Wellnhofer
687a2b719e xinclude: Lower initial table size when fuzzing
We don't have test cases with many documents, so set the initial table
size to 1 when fuzzing, so there is a chance to detect reallocation
issues.
2023-05-11 13:27:52 +02:00
Nick Wellnhofer
c40cbf07a3 malloc-fail: Fix null deref after xmlXIncludeNewRef
See #344.
2023-05-11 13:27:52 +02:00
Nick Wellnhofer
105ce73da0 xinclude: Fix false positives in inclusion loop detection
xmlXIncludeRecurseDoc can realloc the cache.
2023-05-11 13:27:52 +02:00
Nick Wellnhofer
ec05f04d8b malloc-fail: Fix memory leak in xmlXIncludeLoadTxt
Found with libFuzzer, see #344.
2023-02-17 17:16:52 +01:00
Nick Wellnhofer
c02df68651 malloc-fail: Fix memory leak in xmlXIncludeLoadDoc
Found with libFuzzer, see #344.
2023-02-17 17:16:52 +01:00
Nick Wellnhofer
3b59fdf001 malloc-fail: Fix memory leak in xmlXIncludeAddNode
Found with libFuzzer, see #344.
2023-02-17 17:16:51 +01:00
Nick Wellnhofer
e20f4d7a65 xinclude: Fix quadratic behavior in xmlXIncludeLoadTxt
Also make text inclusions work with memory buffers, for example when
using a custom entity loader, and fix a memory leak in case of invalid
characters.

Fixes #483.
2023-02-14 12:25:07 +01:00
Nick Wellnhofer
a96312db51 xinclude: Avoid timeouts when fuzzing
Fix the check for maximum number of inclusions.
2023-02-13 11:29:26 +01:00
Nick Wellnhofer
be0ec005f3 xinclude: Abort immediately if max depth was exceeded
Avoids resource exhaustion if the maximum recursion depth was exceeded.

Note that the XInclude engine offers no protection against other
"billion laughs"-style amplification attacks as long as they stay below
the maximum depth.
2023-02-13 11:29:26 +01:00
Nick Wellnhofer
dc2dde1ab9 malloc-fail: Fix null deref in xmlXIncludeLoadTxt
Found with libFuzzer, see #344.
2023-02-13 11:19:55 +01:00
Nick Wellnhofer
a3749551e6 malloc-fail: Fix reallocation in xmlXIncludeNewRef
Avoid null deref.

Found with libFuzzer, see #344.
2023-02-13 11:19:55 +01:00
Nick Wellnhofer
5a19e21605 malloc-fail: Fix use-after-free in xmlXIncludeAddNode
Found with libFuzzer, see #344.
2022-11-02 16:13:27 +01:00
Nick Wellnhofer
b456e3bb42 xinclude: Always allow XPtr expressions in external documents 2022-10-31 16:49:36 +01:00
Nick Wellnhofer
f14529baf5 xinclude: Fix more memory leaks in xmlXIncludeLoadDoc 2022-10-30 14:12:55 +01:00
Nick Wellnhofer
eef0a7395c xinclude: Implement "streaming" mode
When using xmlreader, XPointer expressions in XIncludes simply cannot
work. Expressions can reference nodes which weren't parsed yet or which
were already deleted.

After fixing nested XIncludes, we reference includes which were parsed
previously. When streaming, these nodes could have been deleted, leading
to use-after-free errors.

Disallow XPointer expressions and truncate the include table in
streaming mode.
2022-10-30 14:12:55 +01:00
Nick Wellnhofer
7e078cc1fe xinclude: Clean up xmlXIncludeDoProcess
Only scan new includes in the second pass. Always reset the "replace"
flag.

This only matters if xmlXIncludeDoProcess is called multiple times on
the same document which currently only happens when called from
xmlreader.
2022-10-30 14:12:55 +01:00
Nick Wellnhofer
075cee9e9f xinclude: Fix memory leak when fuzzing
This only affects the fuzzing build mode.
2022-10-29 15:42:01 +02:00
Nick Wellnhofer
b59ff58728 xinclude: Fix memory leak in xmlXIncludeLoadDoc
Regressed in a recent commit.
2022-10-28 20:08:02 +02:00
Nick Wellnhofer
20e2fb4c1c xinclude: Avoid creation of subcontexts
Don't create subcontext in xmlXIncludeRecurseDoc. Save and restore 'doc'
and 'incTab' instead.

Make xmlXIncludeLoadFallback call xmlXIncludeCopyNode which seems safer
than xmlXIncludeDoProcess since the latter may modify the document.
This should also be more performant since we need to copy the whole
fallback subtree anyway. Also make sure to avoid replacements in
fallback elements in xmlXIncludeDoProcess.
2022-10-25 19:34:38 +02:00
Nick Wellnhofer
010b682fee xinclude: Rework XInclude cache
Use a single cache stored in the "root" context, avoiding cache misses
in some scenarios. Separate the cache from the "include" table. Rework
recursion check.
2022-10-23 18:55:10 +02:00
Nick Wellnhofer
3b68aa4753 xinclude: Remove unused 'doc' argument from xmlXIncludeDoProcess 2022-10-23 18:55:10 +02:00
Nick Wellnhofer
0b09f132f4 xinclude: Remove unused member 'url' from xmlXIncludeCtxt 2022-10-23 18:52:56 +02:00
Nick Wellnhofer
966f5ebd77 xinclude: Remove inefficient refcounting scheme
xinclude.c used to implement a refcounting mechanism for documents which
could result in documents being freed too early and reloaded. We really
should keep parsed and processed documents around for as long as
possible to avoid reprocessing.
2022-10-23 18:52:56 +02:00
Nick Wellnhofer
fe0194d56f xinlucde: Pass xmlXIncludeRefPtrs directly
Stop passing indices into the include table.
2022-10-23 18:52:56 +02:00
Nick Wellnhofer
0fc5be0ad1 xinclude: Remove dead code in xmlXIncludeLoadDoc
The include table was already scanned for entries with a matching URI a
couple of lines before.
2022-10-23 18:52:56 +02:00
Nick Wellnhofer
2fc8d12327 xinclude: Make xmlXIncludeCopyNode non-recursive
Avoid call stack overflows.

Also switch to xmlStaticCopyNode which avoids duplicate namespace
definitions.
2022-10-23 18:52:56 +02:00
Nick Wellnhofer
bc32c3862c xinclude: Don't pass documents to xmlXIncludeCopyNode
Extract the root element directly in xmlXIncludeCopyXPointer.
2022-10-23 18:52:56 +02:00
Nick Wellnhofer
46e4cfb643 xinclude: Remove unused source and target arguments
source is unused, target is always ctxt->doc.
2022-10-23 18:52:56 +02:00
Nick Wellnhofer
d2ed1e4f99 xinclude: Limit recursion depth
This avoids call stack overflows.
2022-10-23 18:52:56 +02:00
Nick Wellnhofer
34496f26db xinclude: Test for inclusion loops 2022-10-23 14:27:05 +02:00
Nick Wellnhofer
bad30e2b2e xinclude: Copy included docs with xmlDocCopyNode
All includes should already be expanded after calling
xmlXIncludeRecurseDoc, so there's no need to invoke the more costly
xmlXIncludeCopyNodeList. Now xmlXIncludeCopyNode is only used to copy
XPointer fragments.
2022-10-23 14:27:05 +02:00
Nick Wellnhofer
bc267cb9bc xinclude: Expand includes in xmlXIncludeCopyNode
This should make nested includes work reliably.

Fixes #424.
2022-10-23 14:27:05 +02:00
Nick Wellnhofer
5e4b751f32 xinclude: Make xmlXIncludeCopyNode return a node list
Convert callers of xmlXIncludeCopyNode to handle node lists.
2022-10-23 14:27:05 +02:00
Nick Wellnhofer
64eaf4d429 xinclude: Make xmlXIncludeCopyNode iterate all descendants 2022-10-23 14:27:05 +02:00
Nick Wellnhofer
5f664ab2cf xinclude: Rework xmlXIncludePreProcessNode
Rename xmlXIncludePreProcessNode to xmlXIncludeExpandNode.

Search the include table for an already existing entry. Also load the
included items in xmlXIncludeExpandNode and remove the second phase in
xmlXIncludeDoProcess.
2022-10-23 14:27:05 +02:00
Nick Wellnhofer
4d7524af7f xinclude: Ignore XML_XINCLUDE_START in xmlXIncludeCopyXPointer
The XPath engine shouldn't return such nodes anyway.
2022-10-23 14:27:05 +02:00
Nick Wellnhofer
938105b572 Revert "xinclude: Fix regression with nested includes"
This reverts commit 7f04e29731 which
caused memory errors.

See #424.
2022-10-21 15:56:12 +02:00
Nick Wellnhofer
7f04e29731 xinclude: Fix regression with nested includes
This reverts commits 74dcc10b and 87d20b55.

Fixes #424.
2022-10-18 19:17:45 +02:00
Nick Wellnhofer
6843fc726f Remove or annotate char casts 2022-09-01 04:31:30 +02:00
Nick Wellnhofer
0f568c0b73 Consolidate private header files
Private functions were previously declared

- in header files in the root directory
- in public headers guarded with IN_LIBXML
- in libxml.h
- redundantly in source files that used them.

Consolidate all private header files in include/private.
2022-08-26 02:11:56 +02:00
Nick Wellnhofer
3e7b4f37aa Avoid calling xmlSetTreeDoc
Create text nodes with xmlNewDocText or set the document directly to
avoid xmlSetTreeDoc being called when the node is inserted.
2022-06-20 01:49:39 +02:00
Nick Wellnhofer
0aa8652e59 Use xmlNewDocText in xmlXIncludeCopyRange
Otherwise, the initial node of the copy could be a text node with a
NULL document. This results in the NULL document being propagated to
copies of other nodes, losing information about the dictionary in which
node data is stored, and freeing a dict-allocated string.

See discussion in !175.
2022-05-20 14:54:49 +02:00
Nick Wellnhofer
670701075b Add configuration flag for XPointer locations support
Add a new configuration flag that controls whether the outdated support
for XPointer locations (ranges and points) is enabled.

    --with-xptr-locs          # Autotools
    LIBXML2_WITH_XPTR_LOCS    # CMake

The latest spec for what it essentially an XPath extension seems to be
this working draft from 2002:

    https://www.w3.org/TR/xptr-xpointer/

The xpointer() scheme is listed as "being reviewed" in the XPointer
registry since at least 2006. libxml2 seems to be the only modern
software that tries to implement this spec, but the code has many bugs
and quality issues.

The flag defaults to "off" and support for this extensions has to be
requested explicitly. The relevant API functions are deprecated.
2022-04-21 02:41:58 +02:00
Nick Wellnhofer
41afa89fc9 Fix short-lived regression in xmlStaticCopyNode
Commit 7618a3b1 didn't account for coalesced text nodes.

I think it would be better if xmlStaticCopyNode didn't try to coalesce
text nodes at all. This code path can only be triggered if some other
code doesn't coalesce text nodes properly. In this case, OSS-Fuzz found
such behavior in xinclude.c.
2022-04-10 14:17:31 +02:00
Nick Wellnhofer
4a8c71eb7c Remove DOCBparser
This code has been broken and deprecated since version 2.6.0, released
in 2003. Because of a bug in commit 961b535c, DOCBparser.c was never
compiled since 2012. I couldn't find a Debian package using any of its
symbols, so it seems safe to remove this module.
2022-03-04 22:56:21 +01:00
Nick Wellnhofer
bc06a522c1 Fix recursion check in xinclude.c
Compare the included URL with the document's URL to detect local
inclusions.

Fixes #348.
2022-03-02 20:44:41 +01:00
Nick Wellnhofer
346c3a930c Remove elfgcchack.h
The same optimization can be enabled with -fno-semantic-interposition
since GCC 5. clang has always used this option by default.
2022-02-20 21:49:04 +01:00
David Kilzer
8f5ccada05 xmlAddChild() and xmlAddNextSibling() may not attach their second argument
Use the return value of xmlAddChild() and xmlAddNextSibling()
instead of the second argument directly.

Found by OSS-Fuzz.

Fixes #316
2022-01-25 16:30:48 +00:00
Nick Wellnhofer
1098c30a04 Fix user-after-free with xmllint --xinclude --dropdtd
The --dropdtd option can leave dangling pointers in entity reference
nodes. Make sure to skip these nodes when processing XIncludes.

This also avoids scanning entity declarations and even modifying
them inadvertently during XInclude processing.

Move from a block list to an allow list approach to avoid descending
into other node types that can't contain elements.

Fixes #237.
2021-04-22 19:44:26 +02:00
Nick Wellnhofer
847a3a1181 Fix use-after-free when XIncluding text from Reader
The XML Reader can free text nodes coming from the XInclude engine
before parsing has finished. Cache a copy of the text string, not the
included node to avoid use after free.

Found by OSS-Fuzz.
2020-09-28 12:37:51 +02:00