about summary refs log tree commit diff
path: root/resolv/tst-ns_name_length_uncompressed.c
blob: 698c0cd2344403a875db7f4d5d80ab5b1f6e8cd3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* Test __ns_name_length_uncompressed.
   Copyright (C) 2022-2024 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#include <arpa/nameser.h>
#include <array_length.h>
#include <errno.h>
#include <stdio.h>
#include <support/check.h>
#include <support/next_to_fault.h>

/* Reference implementation based on other building blocks.  */
static int
reference_length (const unsigned char *p, const unsigned char *eom)
{
  unsigned char buf[NS_MAXCDNAME];
  int n = __ns_name_unpack (p, eom, p, buf, sizeof (buf));
  if (n < 0)
    return n;
  const unsigned char *q = buf;
  if (__ns_name_skip (&q, array_end (buf)) < 0)
    return -1;
  if (q - buf != n)
    /* Compressed name.  */
    return -1;
  return n;
}

static int
do_test (void)
{
  {
    unsigned char buf[] = { 3, 'w', 'w', 'w', 0, 0, 0 };
    TEST_COMPARE (reference_length (buf, array_end (buf)), sizeof (buf) - 2);
    TEST_COMPARE (__ns_name_length_uncompressed (buf, array_end (buf)),
                  sizeof (buf) - 2);
    TEST_COMPARE (reference_length (array_end (buf) - 1, array_end (buf)), 1);
    TEST_COMPARE (__ns_name_length_uncompressed (array_end (buf) - 1,
                                                 array_end (buf)), 1);
    buf[4]  = 0xc0;             /* Forward compression reference.  */
    buf[5]  = 0x06;
    TEST_COMPARE (reference_length (buf, array_end (buf)), -1);
    TEST_COMPARE (__ns_name_length_uncompressed (buf, array_end (buf)), -1);
  }

  struct support_next_to_fault ntf = support_next_to_fault_allocate (300);

  /* Buffer region with all possible bytes at start and end.  */
  for (int length = 1; length <= 300; ++length)
    {
      unsigned char *end = (unsigned char *) ntf.buffer + ntf.length;
      unsigned char *start = end - length;
      memset (start, 'X', length);
      for (int first = 0; first <= 255; ++first)
        {
          *start = first;
          for (int last = 0; last <= 255; ++last)
            {
              start[length - 1] = last;
              TEST_COMPARE (reference_length (start, end),
                            __ns_name_length_uncompressed (start, end));
            }
        }
    }

  /* Poor man's fuzz testing: patch two bytes.   */
  {
    unsigned char ref[] =
      {
        7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 3, 'n', 'e', 't', 0, 0, 0
      };
    TEST_COMPARE (reference_length (ref, array_end (ref)), 13);
    TEST_COMPARE (__ns_name_length_uncompressed (ref, array_end (ref)), 13);

    int good = 0;
    int bad = 0;
    for (int length = 1; length <= sizeof (ref); ++length)
      {
        unsigned char *end = (unsigned char *) ntf.buffer + ntf.length;
        unsigned char *start = end - length;
        memcpy (start, ref, length);

        for (int patch1_pos = 0; patch1_pos < length; ++patch1_pos)
          {
            for (int patch1_value = 0; patch1_value <= 255; ++patch1_value)
              {
                start[patch1_pos] = patch1_value;
                for (int patch2_pos = 0; patch2_pos < length; ++patch2_pos)
                  {
                    for (int patch2_value = 0; patch2_value <= 255;
                         ++patch2_value)
                      {
                        start[patch2_pos] = patch2_value;
                        int expected = reference_length (start, end);
                        errno = EINVAL;
                        int actual
                          =  __ns_name_length_uncompressed (start, end);
                        if (actual > 0)
                          ++good;
                        else
                          {
                            TEST_COMPARE (errno, EMSGSIZE);
                            ++bad;
                          }
                        TEST_COMPARE (expected, actual);
                      }
                    start[patch2_pos] = ref[patch2_pos];
                  }
              }
            start[patch1_pos] = ref[patch1_pos];
          }
      }
    printf ("info: patched inputs with success: %d\n", good);
    printf ("info: patched inputs with failure: %d\n", bad);
  }

  support_next_to_fault_free (&ntf);
  return 0;
}

#include <support/test-driver.c>