From 48f49e61033f2ebbbb6a958bc74a4495eefa3478 Mon Sep 17 00:00:00 2001 From: Mohil-Ahuja Date: Fri, 4 Dec 2026 11:24:03 +0530 Subject: [PATCH] parser lyb BUGFIX reject a child hash without a schema parent A child node read at the top level, or as a child of an opaque node, has neither a schema parent nor a module. Its hash then never matches, and the error message dereferenced the NULL schema parent, which an assert hid only in debug builds. Fail with an error before the lookup instead. Also reject an empty module name, which was only asserted. --- src/parser_lyb.c | 12 +++++++++++- tests/utests/data/test_lyb.c | 25 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/parser_lyb.c b/src/parser_lyb.c index e0f376e6b..1fd8d1ea7 100644 --- a/src/parser_lyb.c +++ b/src/parser_lyb.c @@ -492,7 +492,11 @@ lyb_parse_module(struct lyd_lyb_ctx *lybctx, char **name, char *revision, char * /* module name */ LY_CHECK_RET(lyb_read_string(name, pctx)); - assert((*name)[0]); + if (!(*name)[0]) { + LOGERR(pctx->ctx, LY_EINVAL, "Invalid LYB data, empty module name."); + rc = LY_EINVAL; + goto cleanup; + } /* module revision */ LY_CHECK_GOTO(rc = lyb_read(&rev, 2 * 8, pctx), cleanup); @@ -967,6 +971,12 @@ lyb_parse_schema_hash(struct lyd_lyb_ctx *lybctx, const struct lysc_node *sparen return LY_SUCCESS; } + if (!sparent && !mod) { + /* a child node at the top level or a child of an opaque node */ + LOGERR(lybctx->parse_ctx->ctx, LY_EINVAL, "Invalid LYB data, schema node hash without a schema parent."); + return LY_EINVAL; + } + getnext_opts = lybctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0; /* find our node with matching hashes */ diff --git a/tests/utests/data/test_lyb.c b/tests/utests/data/test_lyb.c index d53746dcc..c01ab1d43 100644 --- a/tests/utests/data/test_lyb.c +++ b/tests/utests/data/test_lyb.c @@ -2937,6 +2937,30 @@ test_memory_bounds(void **state) lyd_free_all(parsed); } +static void +test_invalid_hash_parent(void **state) +{ + struct lyd_node *tree = NULL; + + /* top-level node with an empty module name */ + const char empty_mod[] = {0x12, 0x00, 0x03}; + + /* top-level node encoded as a child, so without any schema parent */ + const char top_child[] = {0x02, (char)0xfb, 0x02, (char)0xfb}; + + (void)state; + + assert_int_equal(LY_EINVAL, lyd_parse_data_mem_len(UTEST_LYCTX, empty_mod, sizeof empty_mod, LYD_LYB, + LYD_PARSE_STRICT, 0, &tree)); + assert_null(tree); + CHECK_LOG_CTX("Invalid LYB data, empty module name.", NULL, 0); + + assert_int_equal(LY_EINVAL, lyd_parse_data_mem_len(UTEST_LYCTX, top_child, sizeof top_child, LYD_LYB, + LYD_PARSE_STRICT, 0, &tree)); + assert_null(tree); + CHECK_LOG_CTX("Invalid LYB data, schema node hash without a schema parent.", NULL, 0); +} + int main(void) { @@ -2955,6 +2979,7 @@ main(void) UTEST(test_different_contexts, setup), UTEST(test_skip_module_check, setup), UTEST(test_memory_bounds), + UTEST(test_invalid_hash_parent), }; return cmocka_run_group_tests(tests, NULL, NULL);