Skip to content

Commit c3a4133

Browse files
enh-googleandi34
authored andcommitted
Check for bad packets in getaddrinfo.c's getanswer.
The near duplicate in gethnamaddr.c was already doing so (this fix is basically copy and pasted from there, but with both copies modified to avoid skirting undefined behavior). Bug: 32322088 Test: browser still works Change-Id: Ied6662be567fb1bddc7ceb138cae1da77fb57976 (cherry picked from commit 27a4459d945e34fabd7166791a5b862ccea83f23) (cherry picked from commit 418fe1e) (cherry picked from commit 0f6bfb7068224ecc7e80c9b91bffa3dfa912b4e9)
1 parent 410a6d5 commit c3a4133

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

libc/netbsd/gethnamaddr.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,12 @@ dprintf(const char *msg, res_state res, ...)
159159
#define BOUNDED_INCR(x) \
160160
do { \
161161
cp += (x); \
162-
if (cp > eom) { \
163-
h_errno = NO_RECOVERY; \
164-
return NULL; \
165-
} \
162+
BOUNDS_CHECK(cp, x); \
166163
} while (/*CONSTCOND*/0)
167164

168165
#define BOUNDS_CHECK(ptr, count) \
169166
do { \
170-
if ((ptr) + (count) > eom) { \
167+
if (eom - (ptr) < (count)) {\
171168
h_errno = NO_RECOVERY; \
172169
return NULL; \
173170
} \

libc/netbsd/net/getaddrinfo.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,6 +1292,17 @@ ip6_str2scopeid(char *scope, struct sockaddr_in6 *sin6, u_int32_t *scopeid)
12921292
static const char AskedForGot[] =
12931293
"gethostby*.getanswer: asked for \"%s\", got \"%s\"";
12941294

1295+
#define BOUNDED_INCR(x) \
1296+
do { \
1297+
BOUNDS_CHECK(cp, x); \
1298+
cp += (x); \
1299+
} while (/*CONSTCOND*/0)
1300+
1301+
#define BOUNDS_CHECK(ptr, count) \
1302+
do { \
1303+
if (eom - (ptr) < (count)) { h_errno = NO_RECOVERY; return NULL; } \
1304+
} while (/*CONSTCOND*/0)
1305+
12951306
static struct addrinfo *
12961307
getanswer(const querybuf *answer, int anslen, const char *qname, int qtype,
12971308
const struct addrinfo *pai)
@@ -1337,7 +1348,8 @@ getanswer(const querybuf *answer, int anslen, const char *qname, int qtype,
13371348
qdcount = ntohs(hp->qdcount);
13381349
bp = hostbuf;
13391350
ep = hostbuf + sizeof hostbuf;
1340-
cp = answer->buf + HFIXEDSZ;
1351+
cp = answer->buf;
1352+
BOUNDED_INCR(HFIXEDSZ);
13411353
if (qdcount != 1) {
13421354
h_errno = NO_RECOVERY;
13431355
return (NULL);
@@ -1347,7 +1359,7 @@ getanswer(const querybuf *answer, int anslen, const char *qname, int qtype,
13471359
h_errno = NO_RECOVERY;
13481360
return (NULL);
13491361
}
1350-
cp += n + QFIXEDSZ;
1362+
BOUNDED_INCR(n + QFIXEDSZ);
13511363
if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
13521364
/* res_send() has already verified that the query name is the
13531365
* same as the one we sent; this just gets the expanded name
@@ -1372,12 +1384,14 @@ getanswer(const querybuf *answer, int anslen, const char *qname, int qtype,
13721384
continue;
13731385
}
13741386
cp += n; /* name */
1387+
BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
13751388
type = _getshort(cp);
13761389
cp += INT16SZ; /* type */
13771390
class = _getshort(cp);
13781391
cp += INT16SZ + INT32SZ; /* class, TTL */
13791392
n = _getshort(cp);
13801393
cp += INT16SZ; /* len */
1394+
BOUNDS_CHECK(cp, n);
13811395
if (class != C_IN) {
13821396
/* XXX - debug? syslog? */
13831397
cp += n;

0 commit comments

Comments
 (0)