]> git.ipfire.org Git - thirdparty/glibc.git/blob - resolv/tst-inet_pton.c
inet_pton: Reject IPv6 addresses with many leading zeros [BZ #16637]
[thirdparty/glibc.git] / resolv / tst-inet_pton.c
1 /* Test inet_pton functions.
2 Copyright (C) 2017 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 <arpa/inet.h>
20 #include <resolv/resolv-internal.h>
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <support/check.h>
25 #include <support/xunistd.h>
26 #include <sys/mman.h>
27 #include <sys/param.h>
28 #include <unistd.h>
29
30 /* Memory region created by next_to_fault_allocate. */
31 struct next_to_fault
32 {
33 /* The user data. */
34 char *buffer;
35 size_t length;
36
37 /* The entire allocated region. */
38 void *region_start;
39 size_t region_size;
40 };
41
42 /* Allocate a buffer of SIZE bytes just before a page which is mapped
43 with PROT_NONE (so that overrunning the buffer will cause a
44 fault). */
45 static struct next_to_fault
46 next_to_fault_allocate (size_t size)
47 {
48 long page_size = sysconf (_SC_PAGE_SIZE);
49 TEST_VERIFY_EXIT (page_size > 0);
50 struct next_to_fault result;
51 result.region_size = roundup (size, page_size) + page_size;
52 TEST_VERIFY_EXIT (size + page_size > size);
53 TEST_VERIFY_EXIT (result.region_size > size);
54 result.region_start
55 = xmmap (NULL, result.region_size, PROT_READ | PROT_WRITE,
56 MAP_PRIVATE | MAP_ANONYMOUS, -1);
57 /* Unmap the page after the allocation. */
58 xmprotect (result.region_start + (result.region_size - page_size),
59 page_size, PROT_NONE);
60 /* Align the allocation within the region so that it ends just
61 before the PROT_NONE page. */
62 result.buffer = result.region_start + result.region_size - page_size - size;
63 result.length = size;
64 return result;
65 }
66
67 /* Deallocate the memory region allocated by
68 next_to_fault_allocate. */
69 static void
70 next_to_fault_free (struct next_to_fault *ntf)
71 {
72 xmunmap (ntf->region_start, ntf->region_size);
73 *ntf = (struct next_to_fault) { NULL, };
74 }
75
76 struct test_case
77 {
78 /* The input data. */
79 const char *input;
80
81 /* True if AF_INET parses successfully. */
82 bool ipv4_ok;
83
84 /* True if AF_INET6 parses successfully. */
85 bool ipv6_ok;
86
87 /* Expected result for AF_INET. */
88 unsigned char ipv4_expected[4];
89
90 /* Expected result for AF_INET6. */
91 unsigned char ipv6_expected[16];
92 };
93
94 static void
95 check_result (const char *what, const struct test_case *t, int family,
96 void *result_buffer, int inet_ret)
97 {
98 TEST_VERIFY_EXIT (inet_ret >= -1);
99 TEST_VERIFY_EXIT (inet_ret <= 1);
100
101 int ok;
102 const unsigned char *expected;
103 size_t result_size;
104 switch (family)
105 {
106 case AF_INET:
107 ok = t->ipv4_ok;
108 expected = t->ipv4_expected;
109 result_size = 4;
110 break;
111 case AF_INET6:
112 ok = t->ipv6_ok;
113 expected = t->ipv6_expected;
114 result_size = 16;
115 break;
116 default:
117 FAIL_EXIT1 ("invalid address family %d", family);
118 }
119
120 if (inet_ret != ok)
121 {
122 support_record_failure ();
123 printf ("error: %s return value mismatch for [[%s]], family %d\n"
124 " expected: %d\n"
125 " actual: %d\n",
126 what, t->input, family, ok, inet_ret);
127 return;
128 }
129 if (memcmp (result_buffer, expected, result_size) != 0)
130 {
131 support_record_failure ();
132 printf ("error: %s result mismatch for [[%s]], family %d\n",
133 what, t->input, family);
134 }
135 }
136
137 static void
138 run_one_test (const struct test_case *t)
139 {
140 size_t test_len = strlen (t->input);
141
142 struct next_to_fault ntf_out4 = next_to_fault_allocate (4);
143 struct next_to_fault ntf_out6 = next_to_fault_allocate (16);
144
145 /* inet_pton requires NUL termination. */
146 {
147 struct next_to_fault ntf_in = next_to_fault_allocate (test_len + 1);
148 memcpy (ntf_in.buffer, t->input, test_len + 1);
149 memset (ntf_out4.buffer, 0, 4);
150 check_result ("inet_pton", t, AF_INET, ntf_out4.buffer,
151 inet_pton (AF_INET, ntf_in.buffer, ntf_out4.buffer));
152 memset (ntf_out6.buffer, 0, 16);
153 check_result ("inet_pton", t, AF_INET6, ntf_out6.buffer,
154 inet_pton (AF_INET6, ntf_in.buffer, ntf_out6.buffer));
155 next_to_fault_free (&ntf_in);
156 }
157
158 /* __inet_pton_length does not require NUL termination. */
159 {
160 struct next_to_fault ntf_in = next_to_fault_allocate (test_len);
161 memcpy (ntf_in.buffer, t->input, test_len);
162 memset (ntf_out4.buffer, 0, 4);
163 check_result ("__inet_pton_length", t, AF_INET, ntf_out4.buffer,
164 __inet_pton_length (AF_INET, ntf_in.buffer, ntf_in.length,
165 ntf_out4.buffer));
166 memset (ntf_out6.buffer, 0, 16);
167 check_result ("__inet_pton_length", t, AF_INET6, ntf_out6.buffer,
168 __inet_pton_length (AF_INET6, ntf_in.buffer, ntf_in.length,
169 ntf_out6.buffer));
170 next_to_fault_free (&ntf_in);
171 }
172
173 next_to_fault_free (&ntf_out4);
174 next_to_fault_free (&ntf_out6);
175 }
176
177 /* The test cases were manually crafted and the set enhanced with
178 American Fuzzy Lop. */
179 const struct test_case test_cases[] =
180 {
181 {.input = ".:", },
182 {.input = "0.0.0.0",
183 .ipv4_ok = true,
184 .ipv4_expected = {0, 0, 0, 0},
185 },
186 {.input = "0.:", },
187 {.input = "00", },
188 {.input = "0000000", },
189 {.input = "00000000000000000", },
190 {.input = "092.", },
191 {.input = "10.0.301.2", },
192 {.input = "127.0.0.1",
193 .ipv4_ok = true,
194 .ipv4_expected = {127, 0, 0, 1},
195 },
196 {.input = "19..", },
197 {.input = "192.0.2.-1", },
198 {.input = "192.0.2.01", },
199 {.input = "192.0.2.1.", },
200 {.input = "192.0.2.1192.", },
201 {.input = "192.0.2.192.\377..", },
202 {.input = "192.0.2.256", },
203 {.input = "192.0.2.27",
204 .ipv4_ok = true,
205 .ipv4_expected = {192, 0, 2, 27},
206 },
207 {.input = "192.0.201.", },
208 {.input = "192.0.261.", },
209 {.input = "192.0.2\256", },
210 {.input = "192.0.\262.", },
211 {.input = "192.062.", },
212 {.input = "192.092.\256", },
213 {.input = "192.0\2562.", },
214 {.input = "192.192.0.2661\031", },
215 {.input = "192.192.00n2.1.", },
216 {.input = "192.192.2.190.", },
217 {.input = "192.255.255.2555", },
218 {.input = "192.92.219\023.", },
219 {.input = "192.\260.2.", },
220 {.input = "1:1::1:1",
221 .ipv6_ok = true,
222 .ipv6_expected = {
223 0x0, 0x1, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0,
224 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x1
225 },
226 },
227 {.input = "2", },
228 {.input = "2.", },
229 {.input = "2001:db8:00001::f", },
230 {.input = "2001:db8:10000::f", },
231 {.input = "2001:db8:1234:5678:abcd:ef01:2345:67",
232 .ipv6_ok = true,
233 .ipv6_expected = {
234 0x20, 0x1, 0xd, 0xb8, 0x12, 0x34, 0x56, 0x78,
235 0xab, 0xcd, 0xef, 0x1, 0x23, 0x45, 0x0, 0x67
236 },
237 },
238 {.input = "2001:db8:1234:5678:abcd:ef01:2345:6789:1", },
239 {.input = "2001:db8:1234:5678:abcd:ef01:2345::6789", },
240 {.input = "2001:db8::0",
241 .ipv6_ok = true,
242 .ipv6_expected = {
243 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
244 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
245 },
246 },
247 {.input = "2001:db8::00",
248 .ipv6_ok = true,
249 .ipv6_expected = {
250 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
251 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
252 },
253 },
254 {.input = "2001:db8::1",
255 .ipv6_ok = true,
256 .ipv6_expected = {
257 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
258 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1
259 },
260 },
261 {.input = "2001:db8::10",
262 .ipv6_ok = true,
263 .ipv6_expected = {
264 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
265 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x10
266 },
267 },
268 {.input = "2001:db8::19",
269 .ipv6_ok = true,
270 .ipv6_expected = {
271 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
272 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x19
273 },
274 },
275 {.input = "2001:db8::1::\012", },
276 {.input = "2001:db8::1::2\012", },
277 {.input = "2001:db8::2",
278 .ipv6_ok = true,
279 .ipv6_expected = {
280 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
281 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2
282 },
283 },
284 {.input = "2001:db8::3",
285 .ipv6_ok = true,
286 .ipv6_expected = {
287 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
288 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3
289 },
290 },
291 {.input = "2001:db8::4",
292 .ipv6_ok = true,
293 .ipv6_expected = {
294 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
295 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4
296 },
297 },
298 {.input = "2001:db8::5",
299 .ipv6_ok = true,
300 .ipv6_expected = {
301 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
302 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x5
303 },
304 },
305 {.input = "2001:db8::6",
306 .ipv6_ok = true,
307 .ipv6_expected = {
308 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
309 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x6
310 },
311 },
312 {.input = "2001:db8::7",
313 .ipv6_ok = true,
314 .ipv6_expected = {
315 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
316 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7
317 },
318 },
319 {.input = "2001:db8::8",
320 .ipv6_ok = true,
321 .ipv6_expected = {
322 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
323 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x8
324 },
325 },
326 {.input = "2001:db8::9",
327 .ipv6_ok = true,
328 .ipv6_expected = {
329 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
330 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x9
331 },
332 },
333 {.input = "2001:db8::A",
334 .ipv6_ok = true,
335 .ipv6_expected = {
336 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
337 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa
338 },
339 },
340 {.input = "2001:db8::B",
341 .ipv6_ok = true,
342 .ipv6_expected = {
343 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
344 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xb
345 },
346 },
347 {.input = "2001:db8::C",
348 .ipv6_ok = true,
349 .ipv6_expected = {
350 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
351 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc
352 },
353 },
354 {.input = "2001:db8::D",
355 .ipv6_ok = true,
356 .ipv6_expected = {
357 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
358 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xd
359 },
360 },
361 {.input = "2001:db8::E",
362 .ipv6_ok = true,
363 .ipv6_expected = {
364 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
365 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xe
366 },
367 },
368 {.input = "2001:db8::F",
369 .ipv6_ok = true,
370 .ipv6_expected = {
371 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
372 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf
373 },
374 },
375 {.input = "2001:db8::a",
376 .ipv6_ok = true,
377 .ipv6_expected = {
378 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
379 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa
380 },
381 },
382 {.input = "2001:db8::b",
383 .ipv6_ok = true,
384 .ipv6_expected = {
385 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
386 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xb
387 },
388 },
389 {.input = "2001:db8::c",
390 .ipv6_ok = true,
391 .ipv6_expected = {
392 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
393 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc
394 },
395 },
396 {.input = "2001:db8::d",
397 .ipv6_ok = true,
398 .ipv6_expected = {
399 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
400 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xd
401 },
402 },
403 {.input = "2001:db8::e",
404 .ipv6_ok = true,
405 .ipv6_expected = {
406 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
407 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xe
408 },
409 },
410 {.input = "2001:db8::f",
411 .ipv6_ok = true,
412 .ipv6_expected = {
413 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
414 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf
415 },
416 },
417 {.input = "2001:db8::ff",
418 .ipv6_ok = true,
419 .ipv6_expected = {
420 0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
421 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff
422 },
423 },
424 {.input = "2001:db8::ffff:2\012", },
425 {.input = "22", },
426 {.input = "2222@", },
427 {.input = "255.255.255.255",
428 .ipv4_ok = true,
429 .ipv4_expected = {255, 255, 255, 255},
430 },
431 {.input = "255.255.255.255\001", },
432 {.input = "255.255.255.25555", },
433 {.input = "2:", },
434 {.input = "2:a:8:EEEE::EEEE:F:EEE8:EEEE\034*:", },
435 {.input = "2:ff:1:1:7:ff:1:1:7.", },
436 {.input = "2f:0000000000000000000000000000000000000000000000000000000000"
437 "0000000000000000000000000000000000000000000000000000000000000000000000"
438 "0G01",
439 },
440 {.input = "429495", },
441 {.input = "5::5::", },
442 {.input = "6.6.", },
443 {.input = "992.", },
444 {.input = "::",
445 .ipv6_ok = true,
446 .ipv6_expected = {
447 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
448 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
449 },
450 },
451 {.input = "::00001", },
452 {.input = "::1",
453 .ipv6_ok = true,
454 .ipv6_expected = {
455 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
456 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1
457 },
458 },
459 {.input = "::10000", },
460 {.input = "::1:1",
461 .ipv6_ok = true,
462 .ipv6_expected = {
463 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
464 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x1
465 },
466 },
467 {.input = "::ff:1:1:7.0.0.1",
468 .ipv6_ok = true,
469 .ipv6_expected = {
470 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff,
471 0x0, 0x1, 0x0, 0x1, 0x7, 0x0, 0x0, 0x1
472 },
473 },
474 {.input = "::ff:1:1:7:ff:1:1:7.", },
475 {.input = "::ff:1:1:7ff:1:8:7.0.0.1", },
476 {.input = "::ff:1:1:7ff:1:8f:1:1:71", },
477 {.input = "::ffff:02fff:127.0.S1", },
478 {.input = "::ffff:127.0.0.1",
479 .ipv6_ok = true,
480 .ipv6_expected = {
481 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
482 0x0, 0x0, 0xff, 0xff, 0x7f, 0x0, 0x0, 0x1
483 },
484 },
485 {.input = "::ffff:1:7.0.0.1",
486 .ipv6_ok = true,
487 .ipv6_expected = {
488 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
489 0xff, 0xff, 0x0, 0x1, 0x7, 0x0, 0x0, 0x1
490 },
491 },
492 {.input = ":\272", },
493 {.input = "A:f:ff:1:1:D:ff:1:1::7.", },
494 {.input = "AAAAA.", },
495 {.input = "D:::", },
496 {.input = "DF8F", },
497 {.input = "F::",
498 .ipv6_ok = true,
499 .ipv6_expected = {
500 0x0, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
501 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
502 },
503 },
504 {.input = "F:A:8:EEEE:8:EEEE\034*:", },
505 {.input = "F:a:8:EEEE:8:EEEE\034*:", },
506 {.input = "F:ff:100:7ff:1:8:7.0.10.1",
507 .ipv6_ok = true,
508 .ipv6_expected = {
509 0x0, 0xf, 0x0, 0xff, 0x1, 0x0, 0x7, 0xff,
510 0x0, 0x1, 0x0, 0x8, 0x7, 0x0, 0xa, 0x1
511 },
512 },
513 {.input = "d92.", },
514 {.input = "ff:00000000000000000000000000000000000000000000000000000000000"
515 "00000000000000000000000000000000000000000000000000000000000000000001",
516 },
517 {.input = "fff2:2::ff2:2:f7",
518 .ipv6_ok = true,
519 .ipv6_expected = {
520 0xff, 0xf2, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0,
521 0x0, 0x0, 0xf, 0xf2, 0x0, 0x2, 0x0, 0xf7
522 },
523 },
524 {.input = "ffff:ff:ff:fff:ff:ff:ff:", },
525 {.input = "\272:", },
526 {NULL}
527 };
528
529 static int
530 do_test (void)
531 {
532 for (size_t i = 0; test_cases[i].input != NULL; ++i)
533 run_one_test (test_cases + i);
534 return 0;
535 }
536
537 #include <support/test-driver.c>