about summary refs log tree commit diff
path: root/resolv/tst-resolv-txnid-collision.c
blob: 722392bcefc196a39862eab11d591942ac9345a0 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/* Test parallel queries with transaction ID collisions.
   Copyright (C) 2020-2023 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 <resolv-internal.h>
#include <resolv_context.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <support/check.h>
#include <support/check_nss.h>
#include <support/resolv_test.h>
#include <support/support.h>
#include <support/test-driver.h>

/* Result of parsing a DNS question name.

   A question name has the form reorder-N-M-rcode-C.example.net, where
   N and M are either 0 and 1, corresponding to the reorder member,
   and C is a number that will be stored in the rcode field.

   Also see parse_qname below.  */
struct parsed_qname
{
  /* The DNS response code requested from the first server.  The
     second server always responds with RCODE zero.  */
  int rcode;

  /* Indicates whether to perform reordering in the responses from the
     respective server.  */
  bool reorder[2];
};

/* Fills *PARSED based on QNAME.  */
static void
parse_qname (struct parsed_qname *parsed, const char *qname)
{
  int reorder0;
  int reorder1;
  int rcode;
  char *suffix;
  if (sscanf (qname, "reorder-%d-%d.rcode-%d.%ms",
              &reorder0, &reorder1, &rcode, &suffix) == 4)
    {
      if (reorder0 != 0)
        TEST_COMPARE (reorder0, 1);
      if (reorder1 != 0)
        TEST_COMPARE (reorder1, 1);
      TEST_VERIFY (rcode >= 0 && rcode <= 15);
      TEST_COMPARE_STRING (suffix, "example.net");
      free (suffix);

      parsed->rcode = rcode;
      parsed->reorder[0] = reorder0;
      parsed->reorder[1] = reorder1;
    }
  else
    FAIL_EXIT1 ("unexpected query: %s", qname);
}

/* Used to construct a response. The first server responds with an
   error, the second server succeeds.  */
static void
build_response (const struct resolv_response_context *ctx,
                struct resolv_response_builder *b,
                const char *qname, uint16_t qclass, uint16_t qtype)
{
  struct parsed_qname parsed;
  parse_qname (&parsed, qname);

  switch (ctx->server_index)
    {
    case 0:
      {
        struct resolv_response_flags flags = { 0 };
        if (parsed.rcode == 0)
          /* Simulate a delegation in case a NODATA (RCODE zero)
             response is requested.  */
          flags.clear_ra = true;
        else
          flags.rcode = parsed.rcode;

        resolv_response_init (b, flags);
        resolv_response_add_question (b, qname, qclass, qtype);
      }
      break;

    case 1:
      {
        struct resolv_response_flags flags = { 0, };
        resolv_response_init (b, flags);
        resolv_response_add_question (b, qname, qclass, qtype);

        resolv_response_section (b, ns_s_an);
        resolv_response_open_record (b, qname, qclass, qtype, 0);
        if (qtype == T_A)
          {
            char ipv4[4] = { 192, 0, 2, 1 };
            resolv_response_add_data (b, &ipv4, sizeof (ipv4));
          }
        else
          {
            char ipv6[16]
              = { 0x20, 0x01, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
            resolv_response_add_data (b, &ipv6, sizeof (ipv6));
          }
        resolv_response_close_record (b);
      }
      break;
    }
}

/* Used to reorder responses.  */
struct resolv_response_context *previous_query;

/* Used to keep track of the queries received.  */
static int previous_server_index = -1;
static uint16_t previous_qtype;

/* For each server, buffer the first query and then send both answers
   to the second query, reordered if requested.  */
static void
response (const struct resolv_response_context *ctx,
          struct resolv_response_builder *b,
          const char *qname, uint16_t qclass, uint16_t qtype)
{
  TEST_VERIFY (qtype == T_A || qtype == T_AAAA);
  if (ctx->server_index != 0)
    TEST_COMPARE (ctx->server_index, 1);

  struct parsed_qname parsed;
  parse_qname (&parsed, qname);

  if (previous_query == NULL)
    {
      /* No buffered query.  Record this query and do not send a
         response.  */
      TEST_COMPARE (previous_qtype, 0);
      previous_query = resolv_response_context_duplicate (ctx);
      previous_qtype = qtype;
      resolv_response_drop (b);
      previous_server_index = ctx->server_index;

      if (test_verbose)
        printf ("info: buffering first query for: %s\n", qname);
    }
  else
    {
      TEST_VERIFY (previous_query != 0);
      TEST_COMPARE (ctx->server_index, previous_server_index);
      TEST_VERIFY (previous_qtype != qtype); /* Not a duplicate.  */

      /* If reordering, send a response for this query explicitly, and
         then skip the implicit send.  */
      if (parsed.reorder[ctx->server_index])
        {
          if (test_verbose)
            printf ("info: sending reordered second response for: %s\n",
                    qname);
          build_response (ctx, b, qname, qclass, qtype);
          resolv_response_send_udp (ctx, b);
          resolv_response_drop (b);
        }

      /* Build a response for the previous query and send it, thus
         reordering the two responses.  */
      {
        if (test_verbose)
          printf ("info: sending first response for: %s\n", qname);
        struct resolv_response_builder *btmp
          = resolv_response_builder_allocate (previous_query->query_buffer,
                                              previous_query->query_length);
        build_response (ctx, btmp, qname, qclass, previous_qtype);
        resolv_response_send_udp (ctx, btmp);
        resolv_response_builder_free (btmp);
      }

      /* If not reordering, send the reply as usual.  */
      if (!parsed.reorder[ctx->server_index])
        {
          if (test_verbose)
            printf ("info: sending non-reordered second response for: %s\n",
                    qname);
          build_response (ctx, b, qname, qclass, qtype);
        }

      /* Unbuffer the response and prepare for the next query.  */
      resolv_response_context_free (previous_query);
      previous_query = NULL;
      previous_qtype = 0;
      previous_server_index = -1;
    }
}

/* Runs a query for QNAME and checks for the expected reply.  See
   struct parsed_qname for the expected format for QNAME.  */
static void
test_qname (const char *qname, int rcode)
{
  struct resolv_context *ctx = __resolv_context_get ();
  TEST_VERIFY_EXIT (ctx != NULL);

  unsigned char q1[512];
  int q1len = res_mkquery (QUERY, qname, C_IN, T_A, NULL, 0, NULL,
                           q1, sizeof (q1));
  TEST_VERIFY_EXIT (q1len > 12);

  unsigned char q2[512];
  int q2len = res_mkquery (QUERY, qname, C_IN, T_AAAA, NULL, 0, NULL,
                           q2, sizeof (q2));
  TEST_VERIFY_EXIT (q2len > 12);

  /* Produce a transaction ID collision.  */
  memcpy (q2, q1, 2);

  unsigned char ans1[512];
  unsigned char *ans1p = ans1;
  unsigned char *ans2p = NULL;
  int nans2p = 0;
  int resplen2 = 0;
  int ans2p_malloced = 0;

  /* Perform a parallel A/AAAA query.  */
  int resplen1 = __res_context_send (ctx, q1, q1len, q2, q2len,
                                     ans1, sizeof (ans1), &ans1p,
                                     &ans2p, &nans2p,
                                     &resplen2, &ans2p_malloced);

  TEST_VERIFY (resplen1 > 12);
  TEST_VERIFY (resplen2 > 12);
  if (resplen1 <= 12 || resplen2 <= 12)
    return;

  if (rcode == 1 || rcode == 3)
    {
      /* Format Error and Name Error responses does not trigger
         switching to the next server.  */
      TEST_COMPARE (ans1p[3] & 0x0f, rcode);
      TEST_COMPARE (ans2p[3] & 0x0f, rcode);
      return;
    }

  /* The response should be successful.  */
  TEST_COMPARE (ans1p[3] & 0x0f, 0);
  TEST_COMPARE (ans2p[3] & 0x0f, 0);

  /* Due to bug 19691, the answer may not be in the slot matching the
     query.  Assume that the AAAA response is the longer one.  */
  unsigned char *a_answer;
  int a_answer_length;
  unsigned char *aaaa_answer;
  int aaaa_answer_length;
  if (resplen2 > resplen1)
    {
      a_answer = ans1p;
      a_answer_length = resplen1;
      aaaa_answer = ans2p;
      aaaa_answer_length = resplen2;
    }
  else
    {
      a_answer = ans2p;
      a_answer_length = resplen2;
      aaaa_answer = ans1p;
      aaaa_answer_length = resplen1;
    }

  {
    char *expected = xasprintf ("name: %s\n"
                                "address: 192.0.2.1\n",
                                qname);
    check_dns_packet (qname, a_answer, a_answer_length, expected);
    free (expected);
  }
  {
    char *expected = xasprintf ("name: %s\n"
                                "address: 2001:db8::1\n",
                                qname);
    check_dns_packet (qname, aaaa_answer, aaaa_answer_length, expected);
    free (expected);
  }

  if (ans2p_malloced)
    free (ans2p);

  __resolv_context_put (ctx);
}

static int
do_test (void)
{
  struct resolv_test *aux = resolv_test_start
    ((struct resolv_redirect_config)
     {
       .response_callback = response,

       /* The response callback use global state (the previous_*
          variables), and query processing must therefore be
          serialized.  */
       .single_thread_udp = true,
     });

  for (int rcode = 0; rcode <= 5; ++rcode)
    for (int do_reorder_0 = 0; do_reorder_0 < 2; ++do_reorder_0)
      for (int do_reorder_1 = 0; do_reorder_1 < 2; ++do_reorder_1)
        {
          char *qname = xasprintf ("reorder-%d-%d.rcode-%d.example.net",
                                   do_reorder_0, do_reorder_1, rcode);
          test_qname (qname, rcode);
          free (qname);
        }

  resolv_test_end (aux);

  return 0;
}

#include <support/test-driver.c>