Skip to content

Fix xp_ctx heap overflow: struct layout depended on XPATH_CURRENT_OPTIMIZE - #701

Open
khromenokroman wants to merge 2 commits into
clicon:masterfrom
khromenokroman:fix/xpath-ctx-struct-layout
Open

khromenokroman wants to merge 2 commits into
clicon:masterfrom
khromenokroman:fix/xpath-ctx-struct-layout

Conversation

@khromenokroman

Copy link
Copy Markdown
Contributor

@olofhagsand Hello

Description:
Fixes #698

Problem

struct xp_ctx (xc_invariant) and struct xpath_tree (xs_cached_result,
xs_cached_initial) defined some fields only under #ifdef XPATH_CURRENT_OPTIMIZE.
That macro comes from clixon_custom.h via clixon_config.h, which is not
included by:

  • lib/src/clixon_xpath_ctx.c
  • external code (plugins) using the installed headers via <clixon/clixon.h>

So the struct layout differed between compilation units. 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 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 master with AddressSanitizer and __attribute__((packed)) on
struct xp_ctx (as suggested in #698) to remove the padding, then validate a
leafref whose path uses current(), e.g.
/group[name=current()/../group]/member/id:

  ERROR: AddressSanitizer: heap-buffer-overflow
  READ of size 4 ... 0 bytes after 64-byte region
      #0 xp_eval clixon_xpath_eval.c:1595
      ...
  allocated by thread T0 here:
      #1 ctx_dup clixon_xpath_ctx.c:97

Fix

  1. Define the fields unconditionally, so that the struct layout is the same in
    all compilation units and in external code regardless of compile options.
    The cost when XPATH_CURRENT_OPTIMIZE is disabled is one int in xp_ctx
    and two pointers in xpath_tree; the fields simply stay zero.
  2. Include clixon_config.h in clixon_xpath_ctx.c, like the other xpath
    sources 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

  • ASan + packed xp_ctx: overflow reproduced on master, gone with this PR.
  • Builds and validates correctly with XPATH_CURRENT_OPTIMIZE both defined
    and undefined.
  • Regular library build has no new warnings.

No regression test is added: on x86_64 the bug is masked by padding, so a
test would not detect it there.

…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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Intermittent Wrong NACM Decisions

1 participant