From d40c0eca284be88eb91c5a47e8fa3c3e2850cc88 Mon Sep 17 00:00:00 2001 From: Affan Ziya Date: Tue, 17 Nov 2026 13:56:05 +0530 Subject: [PATCH] plugins types BUGFIX read past short date values lyplg_type_store_date() builds a date-and-time string from the first 10 characters of the value and the rest after them, relying on the pattern to guarantee a full date. With LYPLG_TYPE_STORE_ONLY the pattern is skipped, so for a shorter value the precision of the second %.*s is negative and asprintf() reads from value + 10, past the end of the value, until a NUL byte. An escaped value such as 2 is passed in an exact-size heap buffer, and a value pointing into the input is parsed together with the following markup, so 2 followed by another element was accepted as a date. --- src/plugins_types/date.c | 7 +++++++ tests/utests/types/yang_types.c | 13 ++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/plugins_types/date.c b/src/plugins_types/date.c index c0fa883d2..af9fcec3d 100644 --- a/src/plugins_types/date.c +++ b/src/plugins_types/date.c @@ -131,6 +131,13 @@ lyplg_type_store_date(const struct ly_ctx *ctx, const struct lysc_type *type, co } if (val) { + if (value_size < 10) { + /* not even a full date, the pattern was not checked with LYPLG_TYPE_STORE_ONLY */ + ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to parse %s value \"%.*s\".", type->name, + (int)value_size, (char *)value); + goto cleanup; + } + /* create date-and-time value */ if (asprintf(&str, "%.*sT00:00:00%.*s", 10, (char *)value, (int)(value_size - 10), (char *)value + 10) == -1) { ret = ly_err_new_no_mem(err); diff --git a/tests/utests/types/yang_types.c b/tests/utests/types/yang_types.c index 85398b8f8..f918d5b6e 100644 --- a/tests/utests/types/yang_types.c +++ b/tests/utests/types/yang_types.c @@ -80,7 +80,8 @@ static void test_data_xml(void **state) { - const char *schema; + const char *schema, *data; + struct lyd_node *tree; /* xml test */ schema = MODULE_CREATE_YANG("a", @@ -151,6 +152,16 @@ test_data_xml(void **state) "\"[0-9]{4}-(1[0-2]|0[1-9])-(0[1-9]|[1-2][0-9]|3[0-1])(Z|[\\+\\-]((1[0-3]|0[0-9]):([0-5][0-9])|14:00))?\".", "/a:l2", 1); + /* date stored only, shorter than a date */ + data = "22005-05-31"; + CHECK_PARSE_LYD_PARAM(data, LYD_XML, LYD_PARSE_STORE_ONLY, 0, LY_EVALID, tree); + assert_null(tree); + CHECK_LOG_CTX("Failed to parse date value \"2\".", "/a:l2", 1); + data = "2"; + CHECK_PARSE_LYD_PARAM(data, LYD_XML, LYD_PARSE_STORE_ONLY, 0, LY_EVALID, tree); + assert_null(tree); + CHECK_LOG_CTX("Failed to parse date value \"2\".", "/a:l2", 1); + /* date-no-zone */ TEST_SUCCESS_XML("a", "l2nz", "2005-05-31", STRING, "2005-05-31"); #ifndef _WIN32