Skip to content

Commit d720971

Browse files
rlubosdkalowsk
authored andcommitted
net: coap: Verify block number before processing
Verify if the block number isn't negative before processing it, to prevent potentially undefined behavior. This was reported by the undefined behavior sanitizer. Signed-off-by: Robert Lubos <[email protected]>
1 parent ad032fc commit d720971

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

subsys/net/lib/coap/coap.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,14 +1469,19 @@ static int update_descriptive_block(struct coap_block_context *ctx,
14691469
}
14701470

14711471
static int update_control_block1(struct coap_block_context *ctx,
1472-
int block, int size)
1472+
int block, int size)
14731473
{
1474-
size_t new_current = GET_NUM(block) << (GET_BLOCK_SIZE(block) + 4);
1474+
size_t new_current;
14751475

14761476
if (block == -ENOENT) {
14771477
return 0;
14781478
}
14791479

1480+
if (block < 0) {
1481+
return -EINVAL;
1482+
}
1483+
1484+
new_current = GET_NUM(block) << (GET_BLOCK_SIZE(block) + 4);
14801485
if (new_current != ctx->current) {
14811486
return -EINVAL;
14821487
}
@@ -1497,12 +1502,18 @@ static int update_control_block1(struct coap_block_context *ctx,
14971502
static int update_control_block2(struct coap_block_context *ctx,
14981503
int block, int size)
14991504
{
1500-
size_t new_current = GET_NUM(block) << (GET_BLOCK_SIZE(block) + 4);
1505+
size_t new_current;
15011506

15021507
if (block == -ENOENT) {
15031508
return 0;
15041509
}
15051510

1511+
if (block < 0) {
1512+
return -EINVAL;
1513+
}
1514+
1515+
new_current = GET_NUM(block) << (GET_BLOCK_SIZE(block) + 4);
1516+
15061517
if (GET_MORE(block)) {
15071518
return -EINVAL;
15081519
}

0 commit comments

Comments
 (0)