From 4d039eb01d7efd752c18b491f52c039a1da51bce Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Thu, 13 Aug 2020 13:04:58 +0100 Subject: [PATCH] Options: Sanity check CoAP options in the PDU include/coap2/pdu.h: src/pdu.c: tests/test_pdu.c: Change parameter max_delta to max_opt to better reflect actual usage. include/coap2/pdu.h: src/pdu.c: Remove unneeded COAP_OPT_LONG and COAP_OPT_END definitions. Remove unused function coap_add_option_later(). src/pdu.c: Update coap_pdu_parse_opt() to sanity check the valid lengths of all the known options. src/coap_io_lwip.c: src/net.c: Ignore packets < 4 bytes Send back a RST for UDP/DTLS if malformed PDU. src/option.c: Clean up coap_option_filter_get() documentation. --- include/coap2/pdu.h | 8 +-- src/coap_io_lwip.c | 17 ++++- src/net.c | 10 ++- src/option.c | 4 +- src/pdu.c | 157 ++++++++++++++++++++++++++++++-------------- tests/test_pdu.c | 24 +++---- 6 files changed, 145 insertions(+), 75 deletions(-) diff --git a/include/coap2/pdu.h b/include/coap2/pdu.h index 6f808e50..5399c2bd 100644 --- a/include/coap2/pdu.h +++ b/include/coap2/pdu.h @@ -256,12 +256,6 @@ typedef int coap_tid_t; #define COAP_PDU_DELAYED -3 -#define COAP_OPT_LONG 0x0F /* OC == 0b1111 indicates that the option list - * in a CoAP message is limited by 0b11110000 - * marker */ - -#define COAP_OPT_END 0xF0 /* end marker */ - #define COAP_PAYLOAD_START 0xFF /* payload marker */ /** @@ -299,7 +293,7 @@ typedef struct coap_pdu_t { uint8_t hdr_size; /**< actual size used for protocol-specific header */ uint8_t token_length; /**< length of Token */ uint16_t tid; /**< transaction id, if any, in regular host byte order */ - uint16_t max_delta; /**< highest option number */ + uint16_t max_opt; /**< highest option number in PDU */ size_t alloc_size; /**< allocated storage for token, options and payload */ size_t used_size; /**< used bytes of storage for token, options and payload */ size_t max_size; /**< maximum size for token, options and payload, or zero for variable size pdu */ diff --git a/src/coap_io_lwip.c b/src/coap_io_lwip.c index dcdbdafb..50250238 100644 --- a/src/coap_io_lwip.c +++ b/src/coap_io_lwip.c @@ -58,7 +58,14 @@ static void coap_recv(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_ coap_pdu_t *pdu = NULL; coap_session_t *session; coap_tick_t now; - coap_packet_t *packet = coap_malloc_type(COAP_PACKET, sizeof(coap_packet_t)); + coap_packet_t *packet; + + if (p->len < 4) { + /* Minimum size of CoAP header - ignore runt */ + return; + } + + packet = coap_malloc_type(COAP_PACKET, sizeof(coap_packet_t)); /* this is fatal because due to the short life of the packet, never should there be more than one coap_packet_t required */ LWIP_ASSERT("Insufficient coap_packet_t resources.", packet != NULL); @@ -92,8 +99,12 @@ static void coap_recv(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_ return; error: - /* FIXME: send back RST? */ - if (pdu) coap_delete_pdu(pdu); + /* + * https://tools.ietf.org/html/rfc7252#section-4.2 MUST send RST + * https://tools.ietf.org/html/rfc7252#section-4.3 MAY send RST + */ + coap_send_rst(session, pdu); + coap_delete_pdu(pdu); if (packet) { packet->pbuf = NULL; coap_free_packet(packet); diff --git a/src/net.c b/src/net.c index 061c7569..53d33028 100644 --- a/src/net.c +++ b/src/net.c @@ -1744,6 +1744,10 @@ coap_handle_dgram(coap_context_t *ctx, coap_session_t *session, coap_pdu_t *pdu = NULL; assert(COAP_PROTO_NOT_RELIABLE(session->proto)); + if (msg_len < 4) { + /* Minimum size of CoAP header - ignore runt */ + return -1; + } pdu = coap_pdu_init(0, 0, 0, msg_len - 4); if (!pdu) @@ -1759,7 +1763,11 @@ coap_handle_dgram(coap_context_t *ctx, coap_session_t *session, return 0; error: - /* FIXME: send back RST? */ + /* + * https://tools.ietf.org/html/rfc7252#section-4.2 MUST send RST + * https://tools.ietf.org/html/rfc7252#section-4.3 MAY send RST + */ + coap_send_rst(session, pdu); coap_delete_pdu(pdu); return -1; } diff --git a/src/option.c b/src/option.c index c7bd748f..f75c92e7 100644 --- a/src/option.c +++ b/src/option.c @@ -531,9 +531,7 @@ coap_option_filter_unset(coap_opt_filter_t filter, uint16_t type) { int coap_option_filter_get(coap_opt_filter_t filter, uint16_t type) { - /* Ugly cast to make the const go away (FILTER_GET wont change filter - * but as _set and _unset do, the function does not take a const). */ - return coap_option_filter_op((uint16_t *)filter, type, FILTER_GET); + return coap_option_filter_op(filter, type, FILTER_GET); } coap_optlist_t * diff --git a/src/pdu.c b/src/pdu.c index 181f4818..b9c08f3c 100644 --- a/src/pdu.c +++ b/src/pdu.c @@ -42,7 +42,7 @@ coap_pdu_clear(coap_pdu_t *pdu, size_t size) { pdu->hdr_size = 0; pdu->token_length = 0; pdu->tid = 0; - pdu->max_delta = 0; + pdu->max_opt = 0; pdu->max_size = size; pdu->used_size = 0; pdu->data = NULL; @@ -203,7 +203,7 @@ coap_add_token(coap_pdu_t *pdu, size_t len, const uint8_t *data) { pdu->token_length = (uint8_t)len; if (len) memcpy(pdu->token, data, len); - pdu->max_delta = 0; + pdu->max_opt = 0; pdu->used_size = len; pdu->data = NULL; @@ -221,7 +221,7 @@ coap_insert_option(coap_pdu_t *pdu, uint16_t type, size_t len, coap_option_t decode; size_t shrink = 0; - if (type >= pdu->max_delta) + if (type >= pdu->max_opt) return coap_add_option(pdu, type, len, data); /* Need to locate where in current options to insert this one */ @@ -349,7 +349,7 @@ coap_add_option(coap_pdu_t *pdu, uint16_t type, size_t len, assert(pdu); pdu->data = NULL; - if (type == pdu->max_delta) { + if (type == pdu->max_opt) { /* Validate that the option is repeatable */ switch (type) { /* Ignore list of genuine repeatable */ @@ -382,71 +382,34 @@ coap_add_option(coap_pdu_t *pdu, uint16_t type, size_t len, } } - if (type < pdu->max_delta) { - coap_log(LOG_DEBUG, + if (type < pdu->max_opt) { + coap_log(LOG_WARNING, "coap_add_option: options are not in correct order\n"); return coap_insert_option(pdu, type, len, data); } if (!coap_pdu_check_resize(pdu, - pdu->used_size + coap_opt_encode_size(type - pdu->max_delta, len))) + pdu->used_size + coap_opt_encode_size(type - pdu->max_opt, len))) return 0; opt = pdu->token + pdu->used_size; /* encode option and check length */ optsize = coap_opt_encode(opt, pdu->alloc_size - pdu->used_size, - type - pdu->max_delta, data, len); + type - pdu->max_opt, data, len); if (!optsize) { coap_log(LOG_WARNING, "coap_add_option: cannot add option\n"); /* error */ return 0; } else { - pdu->max_delta = type; + pdu->max_opt = type; pdu->used_size += optsize; } return optsize; } -/* FIXME: de-duplicate code with coap_add_option */ -uint8_t* -coap_add_option_later(coap_pdu_t *pdu, uint16_t type, size_t len) { - size_t optsize; - coap_opt_t *opt; - - assert(pdu); - pdu->data = NULL; - - if (type < pdu->max_delta) { - coap_log(LOG_WARNING, - "coap_add_option: options are not in correct order\n"); - return NULL; - } - - if (!coap_pdu_check_resize(pdu, - pdu->used_size + coap_opt_encode_size(type - pdu->max_delta, len))) - return 0; - - opt = pdu->token + pdu->used_size; - - /* encode option and check length */ - optsize = coap_opt_encode(opt, pdu->alloc_size - pdu->used_size, - type - pdu->max_delta, NULL, len); - - if (!optsize) { - coap_log(LOG_WARNING, "coap_add_option: cannot add option\n"); - /* error */ - return NULL; - } else { - pdu->max_delta = type; - pdu->used_size += (uint16_t)optsize; - } - - return opt + optsize - len; -} - int coap_add_data(coap_pdu_t *pdu, size_t len, const uint8_t *data) { if (len == 0) { @@ -547,7 +510,7 @@ coap_response_phrase(unsigned char code) { * on error. */ static size_t -next_option_safe(coap_opt_t **optp, size_t *length) { +next_option_safe(coap_opt_t **optp, size_t *length, uint16_t *max_opt) { coap_option_t option; size_t optsize; @@ -558,6 +521,7 @@ next_option_safe(coap_opt_t **optp, size_t *length) { if (optsize) { assert(optsize <= *length); + *max_opt += option.delta; *optp += optsize; *length -= optsize; } @@ -652,9 +616,64 @@ coap_pdu_parse_header(coap_pdu_t *pdu, coap_proto_t proto) { return 1; } +static int +coap_pdu_parse_opt_csm(coap_pdu_t *pdu, uint16_t len) { + switch (pdu->code) { + case COAP_SIGNALING_CSM: + switch (pdu->max_opt) { + case COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE: + if (len > 4) goto bad; + break; + case COAP_SIGNALING_OPTION_BLOCK_WISE_TRANSFER: + if (len > 0) goto bad; + break; + default: + ; + } + break; + case COAP_SIGNALING_PING: + case COAP_SIGNALING_PONG: + switch (pdu->max_opt) { + case COAP_SIGNALING_OPTION_CUSTODY: + if (len > 0) goto bad; + break; + default: + ; + } + break; + case COAP_SIGNALING_RELEASE: + switch (pdu->max_opt) { + case COAP_SIGNALING_OPTION_ALTERNATIVE_ADDRESS: + if (len < 1 || len > 255) goto bad; + break; + case COAP_SIGNALING_OPTION_HOLD_OFF: + if (len > 3) goto bad; + break; + default: + ; + } + break; + case COAP_SIGNALING_ABORT: + switch (pdu->max_opt) { + case COAP_SIGNALING_OPTION_BAD_CSM_OPTION: + if (len > 2) goto bad; + break; + default: + ; + } + break; + default: + ; + } + return 1; +bad: + return 0; +} + int coap_pdu_parse_opt(coap_pdu_t *pdu) { + int good = 1; /* sanity checks */ if (pdu->code == 0) { if (pdu->used_size != 0 || pdu->token_length) { @@ -676,12 +695,52 @@ coap_pdu_parse_opt(coap_pdu_t *pdu) { /* skip header + token */ coap_opt_t *opt = pdu->token + pdu->token_length; size_t length = pdu->used_size - pdu->token_length; + uint16_t len; + pdu->max_opt = 0; while (length > 0 && *opt != COAP_PAYLOAD_START) { - if ( !next_option_safe( &opt, (size_t *)&length ) ) { + len = coap_opt_length(opt); + if ( !next_option_safe( &opt, &length, &pdu->max_opt ) ) { coap_log(LOG_DEBUG, "coap_pdu_parse: missing payload start code\n"); return 0; } + if (COAP_PDU_IS_SIGNALING(pdu)) { + if (!coap_pdu_parse_opt_csm(pdu, len)) + goto bad; + continue; + } + switch (pdu->max_opt) { + case COAP_OPTION_IF_MATCH: if (len > 8) goto bad; break; + case COAP_OPTION_URI_HOST: if (len < 1 || len > 255) goto bad; break; + case COAP_OPTION_ETAG: if (len < 1 || len > 8) goto bad; break; + case COAP_OPTION_IF_NONE_MATCH: if (len != 0) goto bad; break; + case COAP_OPTION_OBSERVE: if (len > 3) goto bad; break; + case COAP_OPTION_URI_PORT: if (len > 2) goto bad; break; + case COAP_OPTION_LOCATION_PATH: if (len > 255) goto bad; break; + case COAP_OPTION_OSCORE: if (len > 255) goto bad; break; + case COAP_OPTION_URI_PATH: if (len > 255) goto bad; break; + case COAP_OPTION_CONTENT_FORMAT:if (len > 2) goto bad; break; + case COAP_OPTION_MAXAGE: if (len > 4) goto bad; break; + case COAP_OPTION_URI_QUERY: if (len < 1 || len > 255) goto bad; break; + case COAP_OPTION_HOP_LIMIT: if (len < 1 || len > 1) goto bad; break; + case COAP_OPTION_ACCEPT: if (len > 2) goto bad; break; + case COAP_OPTION_LOCATION_QUERY:if (len > 255) goto bad; break; + case COAP_OPTION_BLOCK2: if (len > 3) goto bad; break; + case COAP_OPTION_BLOCK1: if (len > 3) goto bad; break; + case COAP_OPTION_SIZE2: if (len > 4) goto bad; break; + case COAP_OPTION_PROXY_URI: if (len < 1 || len > 1034) goto bad; break; + case COAP_OPTION_PROXY_SCHEME: if (len < 1 || len > 255) goto bad; break; + case COAP_OPTION_SIZE1: if (len > 4) goto bad; break; + case COAP_OPTION_NORESPONSE: if (len > 1) goto bad; break; + default: + ; + } + continue; +bad: + coap_log(LOG_DEBUG, + "coap_pdu_parse: %d.%02d: option %u has bad length %u\n", + pdu->code >> 5, pdu->code & 0x1F, pdu->max_opt, len); + good = 0; } if (length > 0) { @@ -700,7 +759,7 @@ coap_pdu_parse_opt(coap_pdu_t *pdu) { pdu->data = NULL; } - return 1; + return good; } int diff --git a/tests/test_pdu.c b/tests/test_pdu.c index c715c342..122ffa2b 100644 --- a/tests/test_pdu.c +++ b/tests/test_pdu.c @@ -382,7 +382,7 @@ t_encode_pdu4(void) { 18, (const uint8_t *)"fancyproxy.coap.me"); CU_ASSERT(result == 20); - CU_ASSERT(pdu->max_delta == 3); + CU_ASSERT(pdu->max_opt == 3); CU_ASSERT(pdu->used_size == 20); CU_ASSERT_PTR_NULL(pdu->data); @@ -390,21 +390,21 @@ t_encode_pdu4(void) { 4, (const uint8_t *)"path"); CU_ASSERT(result == 5); - CU_ASSERT(pdu->max_delta == 11); + CU_ASSERT(pdu->max_opt == 11); CU_ASSERT(pdu->used_size == 25); CU_ASSERT_PTR_NULL(pdu->data); result = coap_add_option(pdu, COAP_OPTION_URI_PATH, 0, NULL); CU_ASSERT(result == 1); - CU_ASSERT(pdu->max_delta == 11); + CU_ASSERT(pdu->max_opt == 11); CU_ASSERT(pdu->used_size == 26); CU_ASSERT_PTR_NULL(pdu->data); result = coap_add_option(pdu, 8000, 8, (const uint8_t *)"fancyopt"); CU_ASSERT(result == 11); - CU_ASSERT(pdu->max_delta == 8000); + CU_ASSERT(pdu->max_opt == 8000); CU_ASSERT(pdu->used_size == 37); CU_ASSERT_PTR_NULL(pdu->data); @@ -437,7 +437,7 @@ t_encode_pdu5(void) { 8, (const uint8_t *)"ABCDEFGH"); CU_ASSERT(result == 9); - CU_ASSERT(pdu->max_delta == 1); + CU_ASSERT(pdu->max_opt == 1); CU_ASSERT(pdu->used_size == 17); CU_ASSERT_PTR_NULL(pdu->data); @@ -445,7 +445,7 @@ t_encode_pdu5(void) { 1, (const uint8_t *)"\x12"); CU_ASSERT(result == 3); - CU_ASSERT(pdu->max_delta == 17); + CU_ASSERT(pdu->max_opt == 17); CU_ASSERT(pdu->used_size == 20); CU_ASSERT_PTR_NULL(pdu->data); @@ -547,14 +547,14 @@ t_encode_pdu9(void) { result = coap_add_option(pdu, COAP_OPTION_ETAG, 8, (const uint8_t *)"someetag"); CU_ASSERT(result == 9); - CU_ASSERT(pdu->max_delta == 4); + CU_ASSERT(pdu->max_opt == 4); CU_ASSERT(pdu->used_size == 9); CU_ASSERT_PTR_NULL(pdu->data); result = coap_add_option(pdu, COAP_OPTION_IF_NONE_MATCH, 0, NULL); CU_ASSERT(result == 1); - CU_ASSERT(pdu->max_delta == 5); + CU_ASSERT(pdu->max_opt == 5); CU_ASSERT(pdu->used_size == 10); CU_ASSERT_PTR_NULL(pdu->data); @@ -562,7 +562,7 @@ t_encode_pdu9(void) { 17, (const uint8_t *)"someratherlonguri"); CU_ASSERT(result == 20); - CU_ASSERT(pdu->max_delta == 35); + CU_ASSERT(pdu->max_opt == 35); CU_ASSERT(pdu->used_size == 30); CU_ASSERT_PTR_NULL(pdu->data); @@ -633,7 +633,7 @@ t_encode_pdu10(void) { (const uint8_t *)"coap://example.com/12345/%3Fxyz/3048234234/23402348234/239084234-23/%AB%30%af/+123/hfksdh/23480-234-98235/1204/243546345345243/0198sdn3-a-3///aff0934/97u2141/0002/3932423532/56234023/----/=1234=/098141-9564643/21970-----/82364923472wererewr0-921-39123-34/"); CU_ASSERT(result == 257); - CU_ASSERT(pdu->max_delta == 8); + CU_ASSERT(pdu->max_opt == 8); CU_ASSERT(pdu->used_size == 259); CU_ASSERT_PTR_NULL(pdu->data); @@ -641,7 +641,7 @@ t_encode_pdu10(void) { (const uint8_t *)"//492403--098/"); CU_ASSERT(result == 16); - CU_ASSERT(pdu->max_delta == 8); + CU_ASSERT(pdu->max_opt == 8); CU_ASSERT(pdu->used_size == 275); CU_ASSERT_PTR_NULL(pdu->data); @@ -649,7 +649,7 @@ t_encode_pdu10(void) { 1, (const uint8_t *)"*"); CU_ASSERT(result == 2); - CU_ASSERT(pdu->max_delta == 20); + CU_ASSERT(pdu->max_opt == 20); CU_ASSERT(pdu->used_size == 277); CU_ASSERT_PTR_NULL(pdu->data);