Skip to content
Memory corruption Disclosed report Advanced

[tor] libevent dns remote stack overread vulnerability

The DNS name parser in libevent does not check that a label's length stays within the packet buffer before copying it, so a crafted DNS response can make it read past the end of the buffer. This overread may crash the process, but no memory is overwritten.

guido Follow

Originally disclosed on HackerOne

  • Triaged in 3 days
  • Fixed in 12 days
  • Paid in 36 hours
  • Disclosed after 21 months

Hello,

the name_parse function in libevent's DNS code is vulnerable to a buffer overread.

C
 935 static int
 936 name_parse(u8 *packet, int length, int *idx, char *name_out, int name_out_len) {
 937     int name_end = -1;
 938     int j = *idx;
 939     int ptr_count = 0;
 940 #define GET32(x) do { if (j + 4 > length) goto err; memcpy(&_t32, packet + j, 4); j += 4; x = ntohl(_t32); } while      (0)
 941 #define GET16(x) do { if (j + 2 > length) goto err; memcpy(&_t, packet + j, 2); j += 2; x = ntohs(_t); } while (0)
 942 #define GET8(x) do { if (j >= length) goto err; x = packet[j++]; } while (0)
 943 
 944     char *cp = name_out;
 945     const char *const end = name_out + name_out_len;
 946 
 947     /* Normally, names are a series of length prefixed strings terminated */
 948     /* with a length of 0 (the lengths are u8's < 63). */
 949     /* However, the length can start with a pair of 1 bits and that */
 950     /* means that the next 14 bits are a pointer within the current */
 951     /* packet. */
 952 
 953     for (;;) {
 954         u8 label_len;
 955         if (j >= length) return -1;
 956         GET8(label_len);
 957         if (!label_len) break;
 958         if (label_len & 0xc0) {
 959             u8 ptr_low;
 960             GET8(ptr_low);
 961             if (name_end < 0) name_end = j;
 962             j = (((int)label_len & 0x3f) << 8) + ptr_low;
 963             /* Make sure that the target offset is in-bounds. */
 964             if (j < 0 || j >= length) return -1;
 965             /* If we've jumped more times than there are characters in the
 966              * message, we must have a loop. */
 967             if (++ptr_count > length) return -1;
 968             continue;
 969         }
 970         if (label_len > 63) return -1;
 971         if (cp != name_out) {
 972             if (cp + 1 >= end) return -1;
 973             *cp++ = '.';
 974         }
 975         if (cp + label_len >= end) return -1;
 976         memcpy(cp, packet + j, label_len);
 977         cp += label_len;
 978         j += label_len;
 979     }
 980     if (cp >= end) return -1;
 981     *cp = '\0';                                                                                                   
 982     if (name_end < 0)
 983         *idx = j;
 984     else
 985         *idx = name_end;
 986     return 0;
 987  err:
 988     return -1;
 989 }

Prior to the memcpy on line 976 it is not asserted that the range (packet + j) - (packet + j + label_len) does not exceed the length of the packet buffer (as stored in variable length). My proof of concept exploits the possibility to keep jumping around in the buffer (lines 959 - 968), until the buffer index (j) is at the very end --- then, an overread of 63 bytes will occur. No overwrite takes place.

As you know, an overread may result in a crash, depending on a variety of factors.

I believe the code in Tor is vulnerable and I will update this report with PoC against Tor itself as soon as I've constructed one.

Nobody has been notified of this bug except you. I will not contact the libevent developers about this issue unless you explicitly request so. It's up to you to notify them yourself.

To test against the latest version of libevent (libevent-2.0.22-stable.tar.gz):

Shell
CFLAGS="-fomit-frame-pointer -fsanitize=address" ./configure
make -j4
cd samples
./dns-example -servertest

in a different terminal run

Shell
python libevent-poc.py

Output of dns-example should be:

EVUTIL_AI_CANONNAME in example = 2
=================================================================
==27496== ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffcf5a7355c at pc 0x7f6096a6b652 bp 0x7ffcf5a72bb0 sp 0x7ffcf5a72ba8
READ of size 1 at 0x7ffcf5a7355c thread T0

...
...

Guido

The original report as published on HackerOne, by guido. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

The parser validates the output buffer and the pointer target offset but never checks that the label copy source (packet + j plus label_len) stays within the packet length.

Impact

A crafted DNS response could cause an out-of-bounds read of up to 63 bytes, which may crash the process depending on other factors; no overwrite occurs.

What the attacker needed

Attacker can send a crafted DNS response; target uses libevent DNS parsing

How it was found

  1. Build libevent with AddressSanitizer enabled and run the bundled DNS example in server-test mode.
  2. From another terminal, run the proof-of-concept script that sends the crafted DNS response.
  3. Observe the AddressSanitizer stack-buffer-overflow report showing an out-of-bounds read.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

When a program reads a DNS name, it follows length markers to copy each piece of the name. Here it never checked that the piece it was about to copy actually fits inside the received message, so it could read a little past the end of the data. That kind of out-of-bounds read can make a program crash, but it does not let an attacker change memory.

What is Memory corruption?

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.

More by guido

View all

49 more disclosed reports, best first.

More from Tor

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.