Skip to content

Commit 385f882

Browse files
Flavio Ceolinjhedberg
authored andcommitted
net: coap: Fix possible overflow
Fix possible integer overflow when parsing a CoAP packet. Signed-off-by: Flavio Ceolin <[email protected]>
1 parent 4e8e72b commit 385f882

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

subsys/net/lib/coap/coap.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ LOG_MODULE_REGISTER(net_coap, CONFIG_COAP_LOG_LEVEL);
1717

1818
#include <zephyr/types.h>
1919
#include <sys/byteorder.h>
20+
#include <sys/math_extras.h>
2021

2122
#include <net/net_ip.h>
2223
#include <net/net_core.h>
@@ -469,7 +470,9 @@ static int parse_option(u8_t *data, u16_t offset, u16_t *pos,
469470
return -EINVAL;
470471
}
471472

472-
*opt_len += hdr_len;
473+
if (u16_add_overflow(*opt_len, hdr_len, opt_len)) {
474+
return -EINVAL;
475+
}
473476
}
474477

475478
if (len > COAP_OPTION_NO_EXT) {
@@ -480,11 +483,15 @@ static int parse_option(u8_t *data, u16_t offset, u16_t *pos,
480483
return -EINVAL;
481484
}
482485

483-
*opt_len += hdr_len;
486+
if (u16_add_overflow(*opt_len, hdr_len, opt_len)) {
487+
return -EINVAL;
488+
}
484489
}
485490

486-
*opt_delta += delta;
487-
*opt_len += len;
491+
if (u16_add_overflow(*opt_delta, delta, opt_delta) ||
492+
u16_add_overflow(*opt_len, len, opt_len)) {
493+
return -EINVAL;
494+
}
488495

489496
if (r == 0) {
490497
if (len == 0U) {
@@ -519,7 +526,10 @@ static int parse_option(u8_t *data, u16_t offset, u16_t *pos,
519526
return -EINVAL;
520527
}
521528
} else {
522-
*pos += len;
529+
if (u16_add_overflow(*pos, len, pos)) {
530+
return -EINVAL;
531+
}
532+
523533
r = max_len - *pos;
524534
}
525535

0 commit comments

Comments
 (0)