Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gcoap: fix underflow when correcting ETag from cache #19968

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
gcoap: fix underflow when correcting ETag from cache
  • Loading branch information
miri64 committed Oct 10, 2023
commit 8d1cb1bd2ba14e95b74cf32ca7690196897f5ace
16 changes: 14 additions & 2 deletions sys/net/application_layer/gcoap/gcoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1331,8 +1331,21 @@ static ssize_t _cache_check(const uint8_t *buf, size_t len,
if ((resp_etag_len > 0) && ((size_t)resp_etag_len <= COAP_ETAG_LENGTH_MAX)) {
uint8_t *tmp_etag;
ssize_t tmp_etag_len = coap_opt_get_opaque(&req, COAP_OPT_ETAG, &tmp_etag);

if (tmp_etag_len >= resp_etag_len) {
/* peak length without padding */
size_t rem_len = (len - (tmp_etag + tmp_etag_len - buf));

if ((tmp_etag < buf) || (tmp_etag > (buf + len)) ||
(rem_len > (len - ((tmp_etag + COAP_ETAG_LENGTH_MAX) - buf)))) {
DEBUG("gcoap: invalid calculated padding length (%lu) for ETag injection "
"during cache lookup.\n", (long unsigned)rem_len);
/* something fishy happened in the request. Better don't return cache entry */
*cache_hit = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's already false from an earlier check, but it doesn't hurt to be explicit either (the compiler will remove it anyway).

#if IS_USED(MODULE_NANOCOAP_CACHE)
memset(memo->cache_key, 0, sizeof(memo->cache_key));
#endif
return -EINVAL;
}
memcpy(tmp_etag, resp_etag, resp_etag_len);
/* shorten ETag option if necessary */
if ((size_t)resp_etag_len < COAP_ETAG_LENGTH_MAX) {
Expand All @@ -1345,7 +1358,6 @@ static ssize_t _cache_check(const uint8_t *buf, size_t len,
* bitmask resp_etag_len */
*start |= (uint8_t)resp_etag_len;
/* remove padding */
size_t rem_len = (len - (tmp_etag + COAP_ETAG_LENGTH_MAX - buf));
memmove(tmp_etag + resp_etag_len, tmp_etag + COAP_ETAG_LENGTH_MAX, rem_len);
len -= (COAP_ETAG_LENGTH_MAX - resp_etag_len);
}
Expand Down
Loading