From 828967bb4391709b293f79ac7a2359a975c0a33c Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Mon, 7 May 2018 19:41:34 +0100 Subject: [PATCH] examples/coap-server.c: Sanity check of input to hnd_put_time to prevent core The input value for time in hnd_put_time() is not sanity checked for the upper limit. If too large, gmtime() etc. functions will return a failure (errno is EOVERFLOW). Validate the input value to hnd_put_time(). Return 4.00 if failure Verify return from gmtime() in hnd_get_time() before trying to convert to ascii readable string. Return 4.04 if failure. [Time may just be valid when updated in hnd_put_time(), but then increments to be too large - hence check also in hnd_get_time()] --- examples/coap-server.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/examples/coap-server.c b/examples/coap-server.c index 533937b7..cfc7e001 100644 --- a/examples/coap-server.c +++ b/examples/coap-server.c @@ -142,8 +142,14 @@ hnd_get_time(coap_context_t *ctx UNUSED_PARAM, } else { /* output human-readable time */ struct tm *tmp; tmp = gmtime(&now); - len = strftime((char *)buf, sizeof(buf), "%b %d %H:%M:%S", tmp); - coap_add_data(response, len, buf); + if (!tmp) { + /* If 'now' is not valid */ + response->code = COAP_RESPONSE_CODE(404); + } + else { + len = strftime((char *)buf, sizeof(buf), "%b %d %H:%M:%S", tmp); + coap_add_data(response, len, buf); + } } } } @@ -182,6 +188,18 @@ hnd_put_time(coap_context_t *ctx UNUSED_PARAM, while(size--) my_clock_base = my_clock_base * 10 + *data++; my_clock_base -= t / COAP_TICKS_PER_SECOND; + + /* Sanity check input value */ + if (!gmtime(&my_clock_base)) { + unsigned char buf[3]; + response->code = COAP_RESPONSE_CODE(400); + coap_add_option(response, + COAP_OPTION_CONTENT_FORMAT, + coap_encode_var_bytes(buf, COAP_MEDIATYPE_TEXT_PLAIN), buf); + coap_add_data(response, 22, (const uint8_t*)"Invalid set time value"); + /* re-init as value is bad */ + my_clock_base = clock_offset; + } } }