1 /* Test getnetbyname and getnetbyaddr.
2 Copyright (C) 2016-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
22 #include <support/check.h>
23 #include <support/check_nss.h>
24 #include <support/resolv_test.h>
25 #include <support/support.h>
26 #include <support/xmemstream.h>
29 send_ptr (struct resolv_response_builder
*b
,
30 const char *qname
, uint16_t qclass
, uint16_t qtype
,
33 resolv_response_init (b
, (struct resolv_response_flags
) {});
34 resolv_response_add_question (b
, qname
, qclass
, qtype
);
35 resolv_response_section (b
, ns_s_an
);
36 resolv_response_open_record (b
, qname
, qclass
, T_PTR
, 0);
37 resolv_response_add_name (b
, alias
);
38 resolv_response_close_record (b
);
42 handle_code (const struct resolv_response_context
*ctx
,
43 struct resolv_response_builder
*b
,
44 const char *qname
, uint16_t qclass
, uint16_t qtype
,
50 send_ptr (b
, qname
, qclass
, qtype
, "1.in-addr.arpa");
53 send_ptr (b
, qname
, qclass
, qtype
, "2.1.in-addr.arpa");
56 send_ptr (b
, qname
, qclass
, qtype
, "3.2.1.in-addr.arpa");
59 send_ptr (b
, qname
, qclass
, qtype
, "4.3.2.1.in-addr.arpa");
62 /* Test multiple PTR records. */
63 resolv_response_init (b
, (struct resolv_response_flags
) {});
64 resolv_response_add_question (b
, qname
, qclass
, qtype
);
65 resolv_response_section (b
, ns_s_an
);
66 resolv_response_open_record (b
, qname
, qclass
, T_PTR
, 0);
67 resolv_response_add_name (b
, "127.in-addr.arpa");
68 resolv_response_close_record (b
);
69 resolv_response_open_record (b
, qname
, qclass
, T_PTR
, 0);
70 resolv_response_add_name (b
, "0.in-addr.arpa");
71 resolv_response_close_record (b
);
74 /* Test skipping of RRSIG record. */
75 resolv_response_init (b
, (struct resolv_response_flags
) { });
76 resolv_response_add_question (b
, qname
, qclass
, qtype
);
77 resolv_response_section (b
, ns_s_an
);
79 resolv_response_open_record (b
, qname
, qclass
, T_PTR
, 0);
80 resolv_response_add_name (b
, "127.in-addr.arpa");
81 resolv_response_close_record (b
);
83 resolv_response_open_record (b
, qname
, qclass
, 46 /* RRSIG */, 0);
86 memset (buf
, 0x3f, sizeof (buf
));
87 resolv_response_add_data (b
, buf
, sizeof (buf
));
89 resolv_response_close_record (b
);
91 resolv_response_open_record (b
, qname
, qclass
, T_PTR
, 0);
92 resolv_response_add_name (b
, "0.in-addr.arpa");
93 resolv_response_close_record (b
);
96 /* Test CNAME handling. */
97 resolv_response_init (b
, (struct resolv_response_flags
) { });
98 resolv_response_add_question (b
, qname
, qclass
, qtype
);
99 resolv_response_section (b
, ns_s_an
);
100 resolv_response_open_record (b
, qname
, qclass
, T_CNAME
, 0);
101 resolv_response_add_name (b
, "cname.example");
102 resolv_response_close_record (b
);
103 resolv_response_open_record (b
, "cname.example", qclass
, T_PTR
, 0);
104 resolv_response_add_name (b
, "4.3.2.1.in-addr.arpa");
105 resolv_response_close_record (b
);
109 resolv_response_init (b
, (struct resolv_response_flags
) { .rcode
= 0, });
110 resolv_response_add_question (b
, qname
, qclass
, qtype
);
113 resolv_response_init (b
, (struct resolv_response_flags
)
114 { .rcode
= NXDOMAIN
, });
115 resolv_response_add_question (b
, qname
, qclass
, qtype
);
118 resolv_response_init (b
, (struct resolv_response_flags
) {.rcode
= SERVFAIL
});
119 resolv_response_add_question (b
, qname
, qclass
, qtype
);
122 /* Check response length matching. */
125 resolv_response_init (b
, (struct resolv_response_flags
) {.tc
= true});
126 resolv_response_add_question (b
, qname
, qclass
, qtype
);
130 resolv_response_init (b
, (struct resolv_response_flags
) {.ancount
= 1});
131 resolv_response_add_question (b
, qname
, qclass
, qtype
);
132 resolv_response_section (b
, ns_s_an
);
133 resolv_response_open_record (b
, qname
, qclass
, T_PTR
, 0);
134 resolv_response_add_name (b
, "127.in-addr.arpa");
135 resolv_response_close_record (b
);
136 resolv_response_open_record (b
, qname
, qclass
, T_PTR
, 0);
137 resolv_response_add_name (b
, "example");
138 resolv_response_close_record (b
);
140 resolv_response_open_record (b
, qname
, qclass
, T_PTR
, 0);
141 size_t to_fill
= 65535 - resolv_response_length (b
)
142 - 2 /* length, "n" */ - 2 /* compression reference */
144 for (size_t i
= 0; i
< to_fill
; ++i
)
145 resolv_response_add_data (b
, "", 1);
146 resolv_response_close_record (b
);
147 resolv_response_add_name (b
, "n.example");
148 uint16_t rrtype
= htons (T_PTR
);
149 resolv_response_add_data (b
, &rrtype
, sizeof (rrtype
));
153 FAIL_EXIT1 ("invalid QNAME: %s (code %d)", qname
, code
);
158 response (const struct resolv_response_context
*ctx
,
159 struct resolv_response_builder
*b
,
160 const char *qname
, uint16_t qclass
, uint16_t qtype
)
163 if (strstr (qname
, "in-addr.arpa") == NULL
)
166 if (sscanf (qname
, "code%d.%ms", &code
, &tail
) != 2
167 || strcmp (tail
, "example") != 0)
168 FAIL_EXIT1 ("invalid QNAME: %s", qname
);
170 handle_code (ctx
, b
, qname
, qclass
, qtype
, code
);
174 /* Reverse lookup. */
177 if (sscanf (qname
, "%d.%d.%d.%d.%ms",
178 components
, components
+ 1, components
+ 2, components
+ 3,
180 || strcmp (tail
, "in-addr.arpa") != 0)
181 FAIL_EXIT1 ("invalid QNAME: %s", qname
);
183 handle_code (ctx
, b
, qname
, qclass
, qtype
, components
[3]);
188 check_reverse (int code
, const char *expected
)
190 char *query
= xasprintf ("code=%d", code
);
191 check_netent (query
, getnetbyaddr (code
, AF_INET
), expected
);
195 /* Test for CVE-2016-3075. */
197 check_long_name (void)
199 struct xmemstream mem
;
200 xopen_memstream (&mem
);
203 memset (label
, 'x', 63);
206 for (unsigned i
= 0; i
< 64 * 1024 * 1024 / strlen (label
); ++i
)
207 fprintf (mem
.out
, "%s", label
);
209 xfclose_memstream (&mem
);
211 check_netent ("long name", getnetbyname (mem
.buffer
),
212 "error: NO_RECOVERY\n");
220 struct resolv_test
*obj
= resolv_test_start
221 ((struct resolv_redirect_config
)
223 .response_callback
= response
226 /* Lookup by name, success cases. */
227 check_netent ("code1.example", getnetbyname ("code1.example"),
228 "alias: 1.in-addr.arpa\n"
229 "net: 0x00000001\n");
230 check_netent ("code2.example", getnetbyname ("code2.example"),
231 "alias: 2.1.in-addr.arpa\n"
232 "net: 0x00000102\n");
233 check_netent ("code3.example", getnetbyname ("code3.example"),
234 "alias: 3.2.1.in-addr.arpa\n"
235 "net: 0x00010203\n");
236 check_netent ("code4.example", getnetbyname ("code4.example"),
237 "alias: 4.3.2.1.in-addr.arpa\n"
238 "net: 0x01020304\n");
239 check_netent ("code5.example", getnetbyname ("code5.example"),
240 "alias: 127.in-addr.arpa\n"
241 "alias: 0.in-addr.arpa\n"
242 "net: 0x0000007f\n");
243 check_netent ("code6.example", getnetbyname ("code6.example"),
244 "alias: 127.in-addr.arpa\n"
245 "alias: 0.in-addr.arpa\n"
246 "net: 0x0000007f\n");
247 check_netent ("code7.example", getnetbyname ("code7.example"),
248 "alias: 4.3.2.1.in-addr.arpa\n"
249 "net: 0x01020304\n");
251 /* Lookup by name, failure cases. */
252 check_netent ("code100.example", getnetbyname ("code100.example"),
253 "error: NO_ADDRESS\n");
254 check_netent ("code101.example", getnetbyname ("code101.example"),
255 "error: HOST_NOT_FOUND\n");
256 check_netent ("code102.example", getnetbyname ("code102.example"),
257 "error: TRY_AGAIN\n");
258 check_netent ("code103.example", getnetbyname ("code103.example"),
259 "error: NO_RECOVERY\n");
261 /* Lookup by address, success cases. */
263 "name: 1.in-addr.arpa\n"
264 "net: 0x00000001\n");
266 "name: 2.1.in-addr.arpa\n"
267 "net: 0x00000002\n");
269 "name: 3.2.1.in-addr.arpa\n"
270 "net: 0x00000003\n");
272 "name: 4.3.2.1.in-addr.arpa\n"
273 "net: 0x00000004\n");
275 "name: 127.in-addr.arpa\n"
276 "alias: 0.in-addr.arpa\n"
277 "net: 0x00000005\n");
279 "name: 127.in-addr.arpa\n"
280 "alias: 0.in-addr.arpa\n"
281 "net: 0x00000006\n");
283 "name: 4.3.2.1.in-addr.arpa\n"
284 "net: 0x00000007\n");
286 /* Lookup by address, failure cases. */
288 "error: NO_ADDRESS\n");
290 "error: HOST_NOT_FOUND\n");
292 "error: TRY_AGAIN\n");
294 "error: NO_RECOVERY\n");
298 resolv_test_end (obj
);