fix out-of-bounds reads in __dns_parse
[musl] / src / network / dns_parse.c
1 #include <string.h>
2 #include "lookup.h"
3
4 int __dns_parse(const unsigned char *r, int rlen, int (*callback)(void *, int, const void *, int, const void *), void *ctx)
5 {
6         int qdcount, ancount;
7         const unsigned char *p;
8         int len;
9
10         if (rlen<12) return -1;
11         if ((r[3]&15)) return 0;
12         p = r+12;
13         qdcount = r[4]*256 + r[5];
14         ancount = r[6]*256 + r[7];
15         if (qdcount+ancount > 64) return -1;
16         while (qdcount--) {
17                 while (p-r < rlen && *p-1U < 127) p++;
18                 if (p>r+rlen-6 || *p>193 || (*p==193 && p[1]>254))
19                         return -1;
20                 p += 5 + !!*p;
21         }
22         while (ancount--) {
23                 while (p-r < rlen && *p-1U < 127) p++;
24                 if (p>r+rlen-12 || *p>193 || (*p==193 && p[1]>254))
25                         return -1;
26                 p += 1 + !!*p;
27                 len = p[8]*256 + p[9];
28                 if (len+10 > r+rlen-p) return -1;
29                 if (callback(ctx, p[1], p+10, len, r) < 0) return -1;
30                 p += 10 + len;
31         }
32         return 0;
33 }