From 1df02be57d3ba42bd3c99664346a87d6d593a446 Mon Sep 17 00:00:00 2001 From: Arthur Chan Date: Fri, 27 Nov 2026 08:33:27 +0000 Subject: [PATCH] OSS-Fuzz: Add new fuzzer/corpus/dict targets lyd_cbor Signed-off-by: Arthur Chan --- tests/fuzz/CMakeLists.txt | 2 +- .../fuzz/corpus/lyd_cbor_roundtrip/seed01.xml | 1 + .../fuzz/corpus/lyd_cbor_roundtrip/seed02.xml | 1 + .../fuzz/corpus/lyd_cbor_roundtrip/seed03.xml | 1 + tests/fuzz/lyd_cbor_roundtrip.c | 89 +++++++++++++++++++ tests/fuzz/lyd_cbor_roundtrip.dict | 26 ++++++ 6 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 tests/fuzz/corpus/lyd_cbor_roundtrip/seed01.xml create mode 100644 tests/fuzz/corpus/lyd_cbor_roundtrip/seed02.xml create mode 100644 tests/fuzz/corpus/lyd_cbor_roundtrip/seed03.xml create mode 100644 tests/fuzz/lyd_cbor_roundtrip.c create mode 100644 tests/fuzz/lyd_cbor_roundtrip.dict diff --git a/tests/fuzz/CMakeLists.txt b/tests/fuzz/CMakeLists.txt index d14e444e7e..e908e6403e 100644 --- a/tests/fuzz/CMakeLists.txt +++ b/tests/fuzz/CMakeLists.txt @@ -1,5 +1,5 @@ if(ENABLE_FUZZ_TARGETS) - set(fuzz_targets lys_parse_mem lyd_parse_mem_xml lyd_parse_mem_json lyd_parse_lyb lyd_parse_schema_mount_xml yang_parse_module) + set(fuzz_targets lys_parse_mem lyd_parse_mem_xml lyd_parse_mem_json lyd_parse_lyb lyd_parse_schema_mount_xml yang_parse_module lyd_cbor_roundtrip) if(FUZZER STREQUAL "AFL") foreach(target_name IN LISTS fuzz_targets) diff --git a/tests/fuzz/corpus/lyd_cbor_roundtrip/seed01.xml b/tests/fuzz/corpus/lyd_cbor_roundtrip/seed01.xml new file mode 100644 index 0000000000..b5b8cc3a61 --- /dev/null +++ b/tests/fuzz/corpus/lyd_cbor_roundtrip/seed01.xml @@ -0,0 +1 @@ +hello-922337203685477580818446744073709551615 \ No newline at end of file diff --git a/tests/fuzz/corpus/lyd_cbor_roundtrip/seed02.xml b/tests/fuzz/corpus/lyd_cbor_roundtrip/seed02.xml new file mode 100644 index 0000000000..27ee2ebfb8 --- /dev/null +++ b/tests/fuzz/corpus/lyd_cbor_roundtrip/seed02.xml @@ -0,0 +1 @@ +true3.142-132767 \ No newline at end of file diff --git a/tests/fuzz/corpus/lyd_cbor_roundtrip/seed03.xml b/tests/fuzz/corpus/lyd_cbor_roundtrip/seed03.xml new file mode 100644 index 0000000000..4e697668df --- /dev/null +++ b/tests/fuzz/corpus/lyd_cbor_roundtrip/seed03.xml @@ -0,0 +1 @@ +k1AAECAP0=k2//8= \ No newline at end of file diff --git a/tests/fuzz/lyd_cbor_roundtrip.c b/tests/fuzz/lyd_cbor_roundtrip.c new file mode 100644 index 0000000000..f2846b987c --- /dev/null +++ b/tests/fuzz/lyd_cbor_roundtrip.c @@ -0,0 +1,89 @@ +/** + * @file lyd_cbor_roundtrip.c + * @author OSS-Fuzz coverage pipeline + * @brief fuzzing harness for the CBOR instance data format round trip. + * + * The fuzzed input is parsed as XML instance data against a fixed schema with + * diverse node types, printed as YANG-CBOR (src/printer_cbor.c) and re-parsed + * from the printed bytes (src/parser_cbor.c, src/lcbor.c), covering the CBOR + * printer and deserializer on printer-valid structures. Requires the build to + * have CBOR support enabled (libcbor present, ENABLE_CBOR_SUPPORT defined). + * + * Copyright (c) 2026 CESNET, z.s.p.o. + * + * This source code is licensed under BSD 3-Clause License (the "License"). + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + */ + +#include +#include +#include + +#include "libyang.h" +#include "tests_config.h" + +static const char *schema = + "module fuzz-cbor {namespace urn:tests:fuzz-cbor;prefix x;" + "container c {" + "leaf s {type string;}" + "leaf i {type int64;}" + "leaf u {type uint64;}" + "leaf b {type boolean;}" + "leaf f {type decimal64 {fraction-digits 3;}}" + "leaf-list ll {type int16;}" + "list l {key k; leaf k {type string;} leaf v {type binary;}}" + "leaf empty {type empty;}" + "}}"; + +int LLVMFuzzerTestOneInput(uint8_t const *buf, size_t len) +{ + struct ly_ctx *ctx = NULL; + struct lyd_node *tree = NULL; + struct lyd_node *tree2 = NULL; + static bool log = false; + char *data = NULL; + char *cbor = NULL; + LY_ERR err; + + if (!log) { + ly_log_options(0); + log = true; + } + + err = ly_ctx_new(TESTS_SRC "/../modules", 0, &ctx); + if (err != LY_SUCCESS) { + return 0; + } + + if (lys_parse_mem(ctx, schema, LYS_IN_YANG, NULL) != LY_SUCCESS) { + goto cleanup; + } + + data = malloc(len + 1); + if (data == NULL) { + goto cleanup; + } + memcpy(data, buf, len); + data[len] = 0; + + if (lyd_parse_data_mem(ctx, data, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, &tree) != LY_SUCCESS) { + goto cleanup; + } + + if (lyd_print_mem(&cbor, tree, LYD_CBOR, 0) != LY_SUCCESS) { + goto cleanup; + } + + lyd_parse_data_mem(ctx, cbor, LYD_CBOR, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, &tree2); + +cleanup: + free(data); + free(cbor); + lyd_free_all(tree); + lyd_free_all(tree2); + ly_ctx_destroy(ctx); + return 0; +} diff --git a/tests/fuzz/lyd_cbor_roundtrip.dict b/tests/fuzz/lyd_cbor_roundtrip.dict new file mode 100644 index 0000000000..2546461100 --- /dev/null +++ b/tests/fuzz/lyd_cbor_roundtrip.dict @@ -0,0 +1,26 @@ +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"true" +"false" +"-9223372036854775808" +"18446744073709551615" +"xmlns=\"urn:tests:fuzz-cbor\""