Skip to content

Commit fc82b0a

Browse files
committed
CVE-2015-5180: resolv: Fix crash with internal QTYPE [BZ #18784]
Also rename T_UNSPEC because an upcoming public header file update will use that name.
1 parent 3c589b1 commit fc82b0a

File tree

8 files changed

+220
-8
lines changed

8 files changed

+220
-8
lines changed

ChangeLog

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
2016-12-31 Florian Weimer <[email protected]>
2+
3+
[BZ #18784]
4+
CVE-2015-5180
5+
* include/arpa/nameser_compat.h (T_QUERY_A_AND_AAAA): Rename from
6+
T_UNSPEC. Adjust value.
7+
* resolv/nss_dns/dns-host.c (_nss_dns_gethostbyname4_r): Use it.
8+
* resolv/res_query.c (__libc_res_nquery): Likewise.
9+
* resolv/res_mkquery.c (res_nmkquery): Check for out-of-range
10+
QTYPEs.
11+
* resolv/tst-resolv-qtypes.c: New file.
12+
* resolv/Makefile (xtests): Add tst-resolv-qtypes.
13+
(tst-resolv-qtypes): Link against libresolv and libpthread.
14+
115
2016-12-31 Florian Weimer <[email protected]>
216

317
* elf/dl-tunables.h (__tunables_init): Fix unused attribute.

NEWS

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,18 @@ Version 2.25
191191

192192
Security related changes:
193193

194-
On ARM EABI (32-bit), generating a backtrace for execution contexts which
194+
* On ARM EABI (32-bit), generating a backtrace for execution contexts which
195195
have been created with makecontext could fail to terminate due to a
196196
missing .cantunwind annotation. This has been observed to lead to a hang
197197
(denial of service) in some Go applications compiled with gccgo. Reported
198198
by Andreas Schwab. (CVE-2016-6323)
199199

200+
* The DNS stub resolver functions would crash due to a NULL pointer
201+
dereference when processing a query with a valid DNS question type which
202+
was used internally in the implementation. The stub resolver now uses a
203+
question type which is outside the range of valid question type values.
204+
(CVE-2015-5180)
205+
200206
The following bugs are resolved with this release:
201207

202208
[The release manager will add the list generated by

include/arpa/nameser_compat.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
# ifndef _ISOMAC
55

6-
/* Picksome unused number to represent lookups of IPv4 and IPv6 (i.e.,
7-
T_A and T_AAAA). */
8-
#define T_UNSPEC 62321
6+
/* The number is outside the 16-bit RR type range and is used
7+
internally by the implementation. */
8+
#define T_QUERY_A_AND_AAAA 439963904
99

1010
# endif /* !_ISOMAC */
1111
#endif

resolv/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ tests += \
4949
tst-resolv-network \
5050
tst-resolv-search \
5151

52+
# This test sends millions of packets and is rather slow.
53+
xtests += tst-resolv-qtypes
5254
endif
5355
extra-libs-others = $(extra-libs)
5456
libresolv-routines := res_comp res_debug \
@@ -123,6 +125,7 @@ $(objpfx)tst-bug18665: $(objpfx)libresolv.so $(shared-thread-library)
123125
$(objpfx)tst-res_use_inet6: $(objpfx)libresolv.so $(shared-thread-library)
124126
$(objpfx)tst-resolv-basic: $(objpfx)libresolv.so $(shared-thread-library)
125127
$(objpfx)tst-resolv-network: $(objpfx)libresolv.so $(shared-thread-library)
128+
$(objpfx)tst-resolv-qtypes: $(objpfx)libresolv.so $(shared-thread-library)
126129
$(objpfx)tst-resolv-search: $(objpfx)libresolv.so $(shared-thread-library)
127130

128131
# This test case uses the deprecated RES_USE_INET6 resolver option.

resolv/nss_dns/dns-host.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ _nss_dns_gethostbyname4_r (const char *name, struct gaih_addrtuple **pat,
324324

325325
int olderr = errno;
326326
enum nss_status status;
327-
int n = __libc_res_nsearch (&_res, name, C_IN, T_UNSPEC,
327+
int n = __libc_res_nsearch (&_res, name, C_IN, T_QUERY_A_AND_AAAA,
328328
host_buffer.buf->buf, 2048, &host_buffer.ptr,
329329
&ans2p, &nans2p, &resplen2, &ans2p_malloced);
330330
if (n >= 0)

resolv/res_mkquery.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ res_nmkquery(res_state statp,
103103
int n;
104104
u_char *dnptrs[20], **dpp, **lastdnptr;
105105

106+
if (class < 0 || class > 65535
107+
|| type < 0 || type > 65535)
108+
return -1;
109+
106110
#ifdef DEBUG
107111
if (statp->options & RES_DEBUG)
108112
printf(";; res_nmkquery(%s, %s, %s, %s)\n",

resolv/res_query.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ __libc_res_nquery(res_state statp,
122122
int n, use_malloc = 0;
123123
u_int oflags = statp->_flags;
124124

125-
size_t bufsize = (type == T_UNSPEC ? 2 : 1) * QUERYSIZE;
125+
size_t bufsize = (type == T_QUERY_A_AND_AAAA ? 2 : 1) * QUERYSIZE;
126126
u_char *buf = alloca (bufsize);
127127
u_char *query1 = buf;
128128
int nquery1 = -1;
@@ -137,7 +137,7 @@ __libc_res_nquery(res_state statp,
137137
printf(";; res_query(%s, %d, %d)\n", name, class, type);
138138
#endif
139139

140-
if (type == T_UNSPEC)
140+
if (type == T_QUERY_A_AND_AAAA)
141141
{
142142
n = res_nmkquery(statp, QUERY, name, class, T_A, NULL, 0, NULL,
143143
query1, bufsize);
@@ -190,7 +190,7 @@ __libc_res_nquery(res_state statp,
190190
if (__builtin_expect (n <= 0, 0) && !use_malloc) {
191191
/* Retry just in case res_nmkquery failed because of too
192192
short buffer. Shouldn't happen. */
193-
bufsize = (type == T_UNSPEC ? 2 : 1) * MAXPACKET;
193+
bufsize = (type == T_QUERY_A_AND_AAAA ? 2 : 1) * MAXPACKET;
194194
buf = malloc (bufsize);
195195
if (buf != NULL) {
196196
query1 = buf;

resolv/tst-resolv-qtypes.c

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
/* Exercise low-level query functions with different QTYPEs.
2+
Copyright (C) 2016 Free Software Foundation, Inc.
3+
This file is part of the GNU C Library.
4+
5+
The GNU C Library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
The GNU C Library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with the GNU C Library; if not, see
17+
<http://www.gnu.org/licenses/>. */
18+
19+
#include <resolv.h>
20+
#include <string.h>
21+
#include <support/check.h>
22+
#include <support/check_nss.h>
23+
#include <support/resolv_test.h>
24+
#include <support/support.h>
25+
#include <support/test-driver.h>
26+
#include <support/xmemstream.h>
27+
28+
/* If ture, the response function will send the actual response packet
29+
over TCP instead of UDP. */
30+
static volatile bool force_tcp;
31+
32+
/* Send back a fake resource record matching the QTYPE. */
33+
static void
34+
response (const struct resolv_response_context *ctx,
35+
struct resolv_response_builder *b,
36+
const char *qname, uint16_t qclass, uint16_t qtype)
37+
{
38+
if (force_tcp && ctx->tcp)
39+
{
40+
resolv_response_init (b, (struct resolv_response_flags) { .tc = 1 });
41+
resolv_response_add_question (b, qname, qclass, qtype);
42+
return;
43+
}
44+
45+
resolv_response_init (b, (struct resolv_response_flags) { });
46+
resolv_response_add_question (b, qname, qclass, qtype);
47+
resolv_response_section (b, ns_s_an);
48+
resolv_response_open_record (b, qname, qclass, qtype, 0);
49+
resolv_response_add_data (b, &qtype, sizeof (qtype));
50+
resolv_response_close_record (b);
51+
}
52+
53+
static const const char *domain = "www.example.com";
54+
55+
static int
56+
wrap_res_query (int type, unsigned char *answer, int answer_length)
57+
{
58+
return res_query (domain, C_IN, type, answer, answer_length);
59+
}
60+
61+
static int
62+
wrap_res_search (int type, unsigned char *answer, int answer_length)
63+
{
64+
return res_query (domain, C_IN, type, answer, answer_length);
65+
}
66+
67+
static int
68+
wrap_res_querydomain (int type, unsigned char *answer, int answer_length)
69+
{
70+
return res_querydomain ("www", "example.com", C_IN, type,
71+
answer, answer_length);
72+
}
73+
74+
static int
75+
wrap_res_send (int type, unsigned char *answer, int answer_length)
76+
{
77+
unsigned char buf[512];
78+
int ret = res_mkquery (QUERY, domain, C_IN, type,
79+
(const unsigned char *) "", 0, NULL,
80+
buf, sizeof (buf));
81+
if (type < 0 || type >= 65536)
82+
{
83+
/* res_mkquery fails for out-of-range record types. */
84+
TEST_VERIFY_EXIT (ret == -1);
85+
return -1;
86+
}
87+
TEST_VERIFY_EXIT (ret > 12); /* DNS header length. */
88+
return res_send (buf, ret, answer, answer_length);
89+
}
90+
91+
static int
92+
wrap_res_nquery (int type, unsigned char *answer, int answer_length)
93+
{
94+
return res_nquery (&_res, domain, C_IN, type, answer, answer_length);
95+
}
96+
97+
static int
98+
wrap_res_nsearch (int type, unsigned char *answer, int answer_length)
99+
{
100+
return res_nquery (&_res, domain, C_IN, type, answer, answer_length);
101+
}
102+
103+
static int
104+
wrap_res_nquerydomain (int type, unsigned char *answer, int answer_length)
105+
{
106+
return res_nquerydomain (&_res, "www", "example.com", C_IN, type,
107+
answer, answer_length);
108+
}
109+
110+
static int
111+
wrap_res_nsend (int type, unsigned char *answer, int answer_length)
112+
{
113+
unsigned char buf[512];
114+
int ret = res_nmkquery (&_res, QUERY, domain, C_IN, type,
115+
(const unsigned char *) "", 0, NULL,
116+
buf, sizeof (buf));
117+
if (type < 0 || type >= 65536)
118+
{
119+
/* res_mkquery fails for out-of-range record types. */
120+
TEST_VERIFY_EXIT (ret == -1);
121+
return -1;
122+
}
123+
TEST_VERIFY_EXIT (ret > 12); /* DNS header length. */
124+
return res_nsend (&_res, buf, ret, answer, answer_length);
125+
}
126+
127+
static void
128+
test_function (const char *fname,
129+
int (*func) (int type,
130+
unsigned char *answer, int answer_length))
131+
{
132+
unsigned char buf[512];
133+
for (int tcp = 0; tcp < 2; ++tcp)
134+
{
135+
force_tcp = tcp;
136+
for (unsigned int type = 1; type <= 65535; ++type)
137+
{
138+
if (test_verbose)
139+
printf ("info: sending QTYPE %d with %s (tcp=%d)\n",
140+
type, fname, tcp);
141+
int ret = func (type, buf, sizeof (buf));
142+
if (ret != 47)
143+
FAIL_EXIT1 ("%s tcp=%d qtype=%d return value %d",
144+
fname,tcp, type, ret);
145+
/* One question, one answer record. */
146+
TEST_VERIFY (memcmp (buf + 4, "\0\1\0\1\0\0\0\0", 8) == 0);
147+
/* Question section. */
148+
static const char qname[] = "\3www\7example\3com";
149+
size_t qname_length = sizeof (qname);
150+
TEST_VERIFY (memcmp (buf + 12, qname, qname_length) == 0);
151+
/* RDATA part of answer. */
152+
uint16_t type16 = type;
153+
TEST_VERIFY (memcmp (buf + ret - 2, &type16, sizeof (type16)) == 0);
154+
}
155+
}
156+
157+
TEST_VERIFY (func (-1, buf, sizeof (buf) == -1));
158+
TEST_VERIFY (func (65536, buf, sizeof (buf) == -1));
159+
}
160+
161+
static int
162+
do_test (void)
163+
{
164+
struct resolv_redirect_config config =
165+
{
166+
.response_callback = response,
167+
};
168+
struct resolv_test *obj = resolv_test_start (config);
169+
170+
test_function ("res_query", &wrap_res_query);
171+
test_function ("res_search", &wrap_res_search);
172+
test_function ("res_querydomain", &wrap_res_querydomain);
173+
test_function ("res_send", &wrap_res_send);
174+
175+
test_function ("res_nquery", &wrap_res_nquery);
176+
test_function ("res_nsearch", &wrap_res_nsearch);
177+
test_function ("res_nquerydomain", &wrap_res_nquerydomain);
178+
test_function ("res_nsend", &wrap_res_nsend);
179+
180+
resolv_test_end (obj);
181+
return 0;
182+
}
183+
184+
#define TIMEOUT 300
185+
#include <support/test-driver.c>

0 commit comments

Comments
 (0)