Skip to content

Commit c20c82b

Browse files
committed
Fix pointer loop bug and add descriptive exceptions
1 parent 5858132 commit c20c82b

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

include/tins/exceptions.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ class malformed_packet : public exception_base {
6666
malformed_packet() : exception_base("Malformed packet") { }
6767
};
6868

69+
class DNS_decompression_pointer_out_of_bounds : public exception_base {
70+
public:
71+
DNS_decompression_pointer_out_of_bounds() : exception_base("DNS decompression pointer out of bounds") { }
72+
};
73+
74+
/**
75+
* \brief Exception thrown when a DNS decompression pointer loops.
76+
*/
77+
class DNS_decompression_pointer_loops : public exception_base {
78+
public:
79+
DNS_decompression_pointer_loops() : exception_base("DNS decompression pointer loops") { }
80+
};
81+
6982
/**
7083
* \brief Exception thrown when serializing a packet fails.
7184
*/

src/dns.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,11 @@ uint32_t DNS::compose_name(const uint8_t* ptr, char* out_ptr) const {
336336
const uint8_t* end = &records_data_[0] + records_data_.size();
337337
const uint8_t* end_ptr = 0;
338338
char* current_out_ptr = out_ptr;
339+
int pointer_counter = 0;
339340
while (*ptr) {
341+
if (pointer_counter++ > 30){
342+
throw DNS_decompression_pointer_loops();
343+
}
340344
// It's an offset
341345
if ((*ptr & 0xc0)) {
342346
if (TINS_UNLIKELY(ptr + sizeof(uint16_t) > end)) {
@@ -347,7 +351,7 @@ uint32_t DNS::compose_name(const uint8_t* ptr, char* out_ptr) const {
347351
index = Endian::be_to_host(index) & 0x3fff;
348352
// Check that the offset is neither too low or too high
349353
if (index < 0x0c || (&records_data_[0] + (index - 0x0c)) >= end) {
350-
throw malformed_packet();
354+
throw DNS_decompression_pointer_out_of_bounds();
351355
}
352356
// We've probably found the end of the original domain name. Save it.
353357
if (end_ptr == 0) {

0 commit comments

Comments
 (0)