Repository navigation
Fix xp_ctx heap overflow: struct layout depended on XPATH_CURRENT_OPTIMIZE - #701
Open
khromenokroman wants to merge 2 commits into
Open
khromenokroman wants to merge 2 commits into
khromenokroman wants to merge 2 commits into
Conversation
…PTIMIZE xc_invariant in struct xp_ctx and xs_cached_result/xs_cached_initial in struct xpath_tree were only defined if XPATH_CURRENT_OPTIMIZE is set. That macro comes from clixon_custom.h via clixon_config.h, which is not included by clixon_xpath_ctx.c nor by external code using the installed headers via clixon.h. As a result ctx_dup() in clixon_xpath_ctx.c allocated xp_ctx without xc_invariant, while clixon_xpath_eval.c read and wrote that field past the end of the allocation. On 64-bit platforms this is hidden by struct padding, on 32-bit ARM it corrupts the heap and leads to random wrong XPath results, eg intermittent wrong NACM decisions. Define the fields unconditionally so that the struct layout is the same in all compilation units regardless of compile options. Fixes clicon#698
All other xpath sources include clixon_config.h (and thereby clixon_custom.h) first. Without it compile-time options such as XPATH_CURRENT_OPTIMIZE were not visible in this file, see clicon#698
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
@olofhagsand Hello
Description:
Fixes #698
Problem
struct xp_ctx(xc_invariant) andstruct xpath_tree(xs_cached_result,xs_cached_initial) defined some fields only under#ifdef XPATH_CURRENT_OPTIMIZE.That macro comes from
clixon_custom.hviaclixon_config.h, which is notincluded by:
lib/src/clixon_xpath_ctx.c<clixon/clixon.h>So the struct layout differed between compilation units.
ctx_dup()inclixon_xpath_ctx.callocatedxp_ctxwithoutxc_invariant, whileclixon_xpath_eval.cread and wrote that field past the end of the allocation.On x86_64 this is hidden by struct padding (both variants are 72 bytes). On
32-bit ARM the sizes are 48 vs 56 bytes, so the heap gets corrupted, which
explains the random wrong NACM decisions reported in #698.
Reproduction
Build
masterwith AddressSanitizer and__attribute__((packed))onstruct xp_ctx(as suggested in #698) to remove the padding, then validate aleafref whose path uses
current(), e.g./group[name=current()/../group]/member/id:Fix
all compilation units and in external code regardless of compile options.
The cost when
XPATH_CURRENT_OPTIMIZEis disabled is oneintinxp_ctxand two pointers in
xpath_tree; the fields simply stay zero.clixon_config.hinclixon_xpath_ctx.c, like the other xpathsources do.
(1) is the actual fix; (2) alone would fix the library but leave plugins
compiled against the installed headers with a different layout.
Testing
xp_ctx: overflow reproduced onmaster, gone with this PR.XPATH_CURRENT_OPTIMIZEboth definedand undefined.
No regression test is added: on x86_64 the bug is masked by padding, so a
test would not detect it there.