From 96d887760665e02b849f607d998acfe4a06d120a Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Mon, 2 Jul 2018 11:46:45 +0100 Subject: [PATCH] Fix reported issues #212, #214 and refix #199, add in valgrind to do the testing build-env/Dockerfile.build-env scripts/build.sh Add in valgrind testing of the tests src/option.c Issue #199 Better fix of ADVANCE_OPT_CHECK() src/pdu.c Issue #214 Validate token length in coap_pdu_parse_header() src/uri.c Issue #212 Check not reading off end of buffer in dots() tests/test_pdu.c tests/test_uri.c Tests updated to include tests for #212 & #214 --- build-env/Dockerfile.build-env | 2 +- scripts/build.sh | 4 +++- src/option.c | 9 +++++---- src/pdu.c | 6 ++++++ src/uri.c | 2 +- tests/test_pdu.c | 32 ++++++++++++++++++++++++++++++++ tests/test_uri.c | 20 ++++++++++++++++++++ 7 files changed, 68 insertions(+), 7 deletions(-) diff --git a/build-env/Dockerfile.build-env b/build-env/Dockerfile.build-env index eb6d3945..0dca6209 100644 --- a/build-env/Dockerfile.build-env +++ b/build-env/Dockerfile.build-env @@ -2,5 +2,5 @@ FROM debian:testing-slim RUN apt-get update && apt-get install -y autoconf automake gcc clang \ libtool libtool-bin make pkg-config libcunit1-dev libssl-dev \ - exuberant-ctags git + exuberant-ctags git valgrind RUN apt-get clean diff --git a/scripts/build.sh b/scripts/build.sh index 6539cf63..79fe6ac2 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -36,7 +36,9 @@ esac err=$? if test $err = 0 -a -n "$WITH_TESTS" ; then - tests/testdriver + EXEC_FILE=tests/testdriver + # then run valgrind on the actual executable + libtool --mode=execute valgrind --track-origins=yes --leak-check=yes --show-reachable=yes --error-exitcode=123 --quiet $EXEC_FILE err=$? fi diff --git a/src/option.c b/src/option.c index f4f8eb97..4e1a90d6 100644 --- a/src/option.c +++ b/src/option.c @@ -36,10 +36,11 @@ * Used to prevent access to *opt when pointing to after end of buffer * after doing a ADVANCE_OPT() */ -#define ADVANCE_OPT_CHECK(o,e,step) \ - ADVANCE_OPT(o,e,step); \ - if ((e) < 1) \ - return 0; +#define ADVANCE_OPT_CHECK(o,e,step) do { \ + ADVANCE_OPT(o,e,step); \ + if ((e) < 1) \ + return 0; \ + } while (0) size_t coap_opt_parse(const coap_opt_t *opt, size_t length, coap_option_t *result) { diff --git a/src/pdu.c b/src/pdu.c index 12935582..eb4da80d 100644 --- a/src/pdu.c +++ b/src/pdu.c @@ -489,6 +489,12 @@ coap_pdu_parse_header(coap_pdu_t *pdu, coap_proto_t proto) { debug( "coap_pdu_parse: unsupported protocol\n" ); return 0; } + if (pdu->token_length > pdu->alloc_size) { + /* Invalid PDU provided - not wise to assert here though */ + coap_log(LOG_DEBUG, "coap_pdu_parse: PDU header token size broken\n"); + pdu->token_length = pdu->alloc_size; + return 0; + } return 1; } diff --git a/src/uri.c b/src/uri.c index 48021313..5c8e712d 100644 --- a/src/uri.c +++ b/src/uri.c @@ -333,7 +333,7 @@ typedef void (*segment_handler_t)(const uint8_t *, size_t, void *); */ COAP_STATIC_INLINE int dots(const uint8_t *s, size_t len) { - return *s == '.' && (len == 1 || (*(s+1) == '.' && len == 2)); + return len && *s == '.' && (len == 1 || (len == 2 && *(s+1) == '.')); } /** diff --git a/tests/test_pdu.c b/tests/test_pdu.c index 37e5f69d..0b5ed02b 100644 --- a/tests/test_pdu.c +++ b/tests/test_pdu.c @@ -276,6 +276,37 @@ t_parse_pdu15(void) { CU_ASSERT(result == 0); } +static void log_handler(coap_log_t level, const char *message) +{ + (void)level; + (void)message; +} + +/* + * To test Issue #214 which allows the token size to be set larger than the + * decoded PDU in coap_pdu_parse_header(). This then causes coap_show_pdu() + * to access invalid memory. + * Credit to OSS-Fuzz for finding this, work done by Bhargava Shastry + */ +static void +t_parse_pdu16(void) { + int result; + coap_pdu_t *testpdu; + uint8_t teststr[] = { 0x5a, 0x0a, 0x5b, 0x5b }; + + testpdu = coap_pdu_init(0, 0, 0, sizeof(teststr)); + CU_ASSERT(testpdu != NULL); + + result = coap_pdu_parse(COAP_PROTO_UDP, teststr, sizeof(teststr), testpdu); + CU_ASSERT(result == 0); + + coap_set_log_handler(log_handler); + coap_show_pdu(testpdu); /* display PDU */ + coap_set_log_handler(NULL); + + coap_delete_pdu(testpdu); +} + /************************************************************************ ** PDU encoder ************************************************************************/ @@ -804,6 +835,7 @@ t_init_pdu_tests(void) { PDU_TEST(suite[0], t_parse_pdu13); PDU_TEST(suite[0], t_parse_pdu14); PDU_TEST(suite[0], t_parse_pdu15); + PDU_TEST(suite[0], t_parse_pdu16); suite[1] = CU_add_suite("pdu encoder", t_pdu_tests_create, t_pdu_tests_remove); if (suite[1]) { diff --git a/tests/test_uri.c b/tests/test_uri.c index 1e11af9b..ec0afb59 100644 --- a/tests/test_uri.c +++ b/tests/test_uri.c @@ -556,6 +556,25 @@ t_parse_uri23(void) { coap_delete_string (uri_path); } +/* + * To test Issue #212 which reads off the end of the input buffer when looking + * for . or .. in the path. + * Credit to OSS-Fuzz for finding this, work done by Bhargava Shastry + */ +static void +t_parse_uri24(void) { + /* coap://\206cap:// */ + uint8_t teststr[] = { 0x63, 0x6f, 0x61, 0x70, 0x3a, 0x2f, 0x2f, 0x86, 0x63, 0x6f, 0x61, 0x70, 0x3a, 0x2f, 0x2f }; + int result; + unsigned char buf[40]; + size_t buflen = sizeof(buf); + + result = coap_split_path(teststr, sizeof(teststr), buf, &buflen); + CU_ASSERT(result == 5); + CU_ASSERT(buflen == 16); +} + + CU_pSuite t_init_uri_tests(void) { CU_pSuite suite; @@ -597,6 +616,7 @@ t_init_uri_tests(void) { URI_TEST(suite, t_parse_uri21); URI_TEST(suite, t_parse_uri22); URI_TEST(suite, t_parse_uri23); + URI_TEST(suite, t_parse_uri24); return suite; }