Skip to content

Commit 7bd45cd

Browse files
rlubosnashif
authored andcommitted
net: dns: Fix potential buffer overflow when unpacking labels
As the loop unpacking the DNS name from records checks the current label length on each iteration, it's also needed to update the remaining buffer length on each iteration, otherwise the buffer length checks doesn't work as expected. Additionally, the remaining buffer checks while technically worked, they were conceptually wrong and unintuitive. The buf->data pointer doesn't move, so comparing against this pointer when adding new labels doesn't make sense. It's more intuitive to simply compare the label size vs the remaining buffer space. Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
1 parent 353dadf commit 7bd45cd

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

subsys/net/lib/dns/dns_pack.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,6 @@ int mdns_unpack_query_header(struct dns_msg_t *msg, uint16_t *src_id)
486486
int dns_unpack_name(const uint8_t *msg, int maxlen, const uint8_t *src,
487487
struct net_buf *buf, const uint8_t **eol)
488488
{
489-
int dest_size = net_buf_tailroom(buf);
490489
const uint8_t *end_of_label = NULL;
491490
const uint8_t *curr_src = src;
492491
int loop_check = 0, len = -1;
@@ -525,6 +524,8 @@ int dns_unpack_name(const uint8_t *msg, int maxlen, const uint8_t *src,
525524
return -EMSGSIZE;
526525
}
527526
} else {
527+
size_t dest_size = net_buf_tailroom(buf);
528+
528529
/* Max label length is 64 bytes (because 2 bits are
529530
* used for pointer)
530531
*/
@@ -533,8 +534,7 @@ int dns_unpack_name(const uint8_t *msg, int maxlen, const uint8_t *src,
533534
return -EMSGSIZE;
534535
}
535536

536-
if (((buf->data + label_len + 1) >=
537-
(buf->data + dest_size)) ||
537+
if ((label_len + 1 >= dest_size) ||
538538
((curr_src + label_len) >= (msg + maxlen))) {
539539
return -EMSGSIZE;
540540
}

0 commit comments

Comments
 (0)