sanitizer: Fix reported issues

coap_new_cache_entry() does not correctly check for no PDU data when called
with COAP_CACHE_RECORD_PDU. No current libcoap code (examples and library)
call coap_new_cache_entry() with COAP_CACHE_RECORD_PDU set.

Internal function coap_pdu_resize() can be used to reduce a PDU size,
creating current options confusion.  Fix is not to reduce PDU if new
size is smaller than the current used size. No current libcoap code calls
coap_pdu_resize() to reduce the size.

If there is an issue with the PDU options where the maximum used option
value is larger than the last defined option value, an assert() is triggered.

All of the coap_*_option() functions correctly manage pdu->max_opt, but
this issue could occur if coap_pdu_resize() was called to reduce the PDU size
below that of pdu->used_size.
This commit is contained in:
Jon Shallow committed 2026-03-25 08:53:41 +00:00
1 parent 37ee78a8c2
commit b7847c4dbb
2 files changed
+11 -3

No files matched your search

+2 -1
View File
@@ -203,7 +203,8 @@ coap_new_cache_entry_lkd(coap_session_t *session, const coap_pdu_t *pdu,
memcpy(entry->pdu, pdu, offsetof(coap_pdu_t, token));
memcpy(entry->pdu->token, pdu->token, pdu->used_size);
/* And adjust all the pointers etc. */
entry->pdu->data = entry->pdu->token + (pdu->data - pdu->token);
if (pdu->data)
entry->pdu->data = entry->pdu->token + (pdu->data - pdu->token);
}
}
entry->cache_key = coap_cache_derive_key(session, pdu, session_based);
+9 -2
View File
@@ -280,10 +280,12 @@ fail:
int
coap_pdu_resize(coap_pdu_t *pdu, size_t new_size) {
if (new_size > pdu->alloc_size) {
/* Expanding the PDU usage */
#if !defined(WITH_LWIP)
uint8_t *new_hdr;
size_t offset;
#endif
if (pdu->max_size && new_size > pdu->max_size) {
coap_log_warn("coap_pdu_resize: pdu too big\n");
return 0;
@@ -314,8 +316,8 @@ coap_pdu_resize(coap_pdu_t *pdu, size_t new_size) {
else
pdu->actual_token.s = &pdu->token[2];
#endif
pdu->alloc_size = new_size;
}
pdu->alloc_size = new_size;
return 1;
}
@@ -629,7 +631,12 @@ coap_insert_option(coap_pdu_t *pdu, coap_option_num_t number, size_t len,
}
prev_number = opt_iter.number;
}
assert(option != NULL);
if (option == NULL) {
/* Code is broken somewhere */
coap_log_warn("coap_insert_option: Broken max_opt\n");
return 0;
}
/* size of option inc header to insert */
shift = coap_opt_encode_size(number - prev_number, len);