systemd-resolved-rs 0.1.1

A compatibility-oriented reimplementation of systemd-resolved
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#[cfg(test)]
mod test_11_cross_transaction_redirects {
    use super::*;
    use crate::wire::{TYPE_A, TYPE_CNAME};
    use std::net::UdpSocket;
    use std::thread;

    fn append_answer(packet: &mut Vec<u8>, owner: &[u8], rr_type: u16, rdata: &[u8]) {
        packet.extend_from_slice(owner);
        packet.extend_from_slice(&rr_type.to_be_bytes());
        packet.extend_from_slice(&wire::CLASS_IN.to_be_bytes());
        packet.extend_from_slice(&60u32.to_be_bytes());
        packet.extend_from_slice(
            &u16::try_from(rdata.len())
                .expect("test RDATA length")
                .to_be_bytes(),
        );
        packet.extend_from_slice(rdata);
    }

    fn response(query: &[u8], answer_count: u16) -> Vec<u8> {
        let end = wire::question_end(query).expect("question end");
        let mut response = query[..end].to_vec();
        response[2..4].copy_from_slice(&0x8180u16.to_be_bytes());
        response[6..8].copy_from_slice(&answer_count.to_be_bytes());
        response[8..12].fill(0);
        response
    }

    fn receive_query(socket: &UdpSocket) -> (Vec<u8>, SocketAddr) {
        let mut buffer = [0; 4096];
        let (length, peer) = socket.recv_from(&mut buffer).expect("receive DNS query");
        (buffer[..length].to_vec(), peer)
    }

    fn send_redirect(
        socket: &UdpSocket,
        query: &[u8],
        peer: SocketAddr,
        owner: &[u8],
        rr_type: u16,
        target: &str,
    ) {
        let mut reply = response(query, 1);
        let target = wire::encode_name(target).expect("redirect target");
        append_answer(&mut reply, owner, rr_type, &target);
        socket.send_to(&reply, peer).expect("send redirect");
    }

    fn send_error(
        socket: &UdpSocket,
        query: &[u8],
        peer: SocketAddr,
        rcode: u8,
    ) {
        let mut reply = response(query, 0);
        reply[3] = (reply[3] & 0xF0) | (rcode & 0x0f);
        socket.send_to(&reply, peer).expect("send DNS error");
    }

    fn send_a(socket: &UdpSocket, query: &[u8], peer: SocketAddr, address: [u8; 4]) {
        let mut reply = response(query, 1);
        append_answer(&mut reply, &[0xc0, 0x0c], TYPE_A, &address);
        socket.send_to(&reply, peer).expect("send address");
    }

    fn send_redirect_chain(
        socket: &UdpSocket,
        query: &[u8],
        peer: SocketAddr,
        start: u16,
        redirects: usize,
        terminal_has_address: bool,
    ) {
        let mut reply = response(query, u16::try_from(redirects + usize::from(terminal_has_address)).expect("answers"));
        let mut current = start;
        for redirect_index in 0..redirects {
            let owner = if redirect_index == 0 {
                vec![0xc0, 0x0c]
            } else {
                wire::encode_name(&format!("{current}.example")).expect("owner")
            };
            current = current.saturating_add(1);
            let target = wire::encode_name(&format!("{current}.example")).expect("target");
            append_answer(&mut reply, &owner, TYPE_CNAME, &target);
        }
        if terminal_has_address {
            let terminal = format!("{current}.example");
            let terminal_owner = wire::encode_name(&terminal).expect("terminal");
            append_answer(&mut reply, &terminal_owner, TYPE_A, &[192, 0, 2, 100]);
        }
        socket.send_to(&reply, peer).expect("send redirect chain");
    }

    fn resolver_for(server: SocketAddr) -> Resolver {
        Resolver::new(Config {
            upstreams: vec![server],
            fallback_upstreams: Vec::new(),
            query_timeout: Duration::from_millis(500),
            attempts: 1,
            cache: false,
            read_etc_hosts: false,
            ..Config::default()
        })
    }

    #[test]
    fn cname_redirect_continues_in_a_follow_up_transaction() {
        let socket = UdpSocket::bind("127.0.0.1:0").expect("bind test DNS server");
        socket
            .set_read_timeout(Some(Duration::from_secs(2)))
            .expect("set test timeout");
        let server = socket.local_addr().expect("test DNS server address");

        let worker = thread::spawn(move || {
            let (first, peer) = receive_query(&socket);
            assert_eq!(
                first_question(&first).expect("first question").name.text(),
                "alias.example.test"
            );
            send_redirect(
                &socket,
                &first,
                peer,
                &[0xc0, 0x0c],
                wire::TYPE_CNAME,
                "real.example.test",
            );

            let (second, peer) = receive_query(&socket);
            assert_eq!(
                first_question(&second)
                    .expect("follow-up question")
                    .name
                    .text(),
                "real.example.test"
            );
            send_a(&socket, &second, peer, [192, 0, 2, 71]);
        });

        let lookup = resolver_for(server)
            .lookup_name("alias.example.test", 2)
            .expect("cross-transaction CNAME lookup");
        worker.join().expect("test DNS worker");

        assert_eq!(lookup.canonical_name, "real.example.test");
        assert_eq!(
            lookup.addresses,
            vec![IpAddr::V4(Ipv4Addr::new(192, 0, 2, 71))]
        );
    }

    #[test]
    fn redirect_follow_error_uses_original_question_name() {
        let socket = UdpSocket::bind("127.0.0.1:0").expect("bind test DNS server");
        socket
            .set_read_timeout(Some(Duration::from_secs(2)))
            .expect("set test timeout");
        let server = socket.local_addr().expect("test DNS server address");

        let worker = thread::spawn(move || {
            let (first, peer) = receive_query(&socket);
            assert_eq!(
                first_question(&first).expect("first follow-up query").name.text(),
                "alias.example.test"
            );
            send_redirect(
                &socket,
                &first,
                peer,
                &[0xc0, 0x0c],
                wire::TYPE_CNAME,
                "real.example.test",
            );

            let (second, peer) = receive_query(&socket);
            assert_eq!(
                first_question(&second).expect("follow-up query").name.text(),
                "real.example.test"
            );
            send_error(&socket, &second, peer, 2);
        });

        let error = resolver_for(server)
            .lookup_name("alias.example.test", 2)
            .expect_err("DNSSEC error with redirect");
        worker.join().expect("test DNS worker");
        match error {
            ResolveError::DnsError { query, rcode, .. } => {
                assert_eq!(rcode, 2);
                assert_eq!(query, "alias.example.test");
            }
            other => panic!("expected DNS error, got {other:?}"),
        }
    }

    #[test]
    fn no_cname_stops_before_a_follow_up_transaction() {
        let socket = UdpSocket::bind("127.0.0.1:0").expect("bind test DNS server");
        socket
            .set_read_timeout(Some(Duration::from_millis(500)))
            .expect("set test timeout");
        let server = socket.local_addr().expect("test DNS server address");

        let worker = thread::spawn(move || {
            let (first, peer) = receive_query(&socket);
            send_redirect(
                &socket,
                &first,
                peer,
                &[0xc0, 0x0c],
                wire::TYPE_CNAME,
                "real.example.test",
            );
            let mut buffer = [0; 512];
            assert!(socket.recv_from(&mut buffer).is_err());
        });

        let result = resolver_for(server).resolve_record_on_link_with_request_flags(
            "alias.example.test",
            wire::CLASS_IN,
            TYPE_A,
            None,
            crate::dbus_resolve1_abi::flags::SD_RESOLVED_NO_CNAME,
        );
        assert!(matches!(
            result,
            Err(ResolveError::Wire(WireError::CnameLoop))
        ));
        worker.join().expect("mock DNS thread");
    }

    #[test]
    fn dname_redirect_continues_in_a_follow_up_transaction() {
        let socket = UdpSocket::bind("127.0.0.1:0").expect("bind test DNS server");
        socket
            .set_read_timeout(Some(Duration::from_secs(2)))
            .expect("set test timeout");
        let server = socket.local_addr().expect("test DNS server address");

        let worker = thread::spawn(move || {
            let (first, peer) = receive_query(&socket);
            assert_eq!(
                first_question(&first).expect("first question").name.text(),
                "host.branch.example.test"
            );
            let owner = wire::encode_name("branch.example.test").expect("DNAME owner");
            send_redirect(
                &socket,
                &first,
                peer,
                &owner,
                wire::TYPE_DNAME,
                "service.example.test",
            );

            let (second, peer) = receive_query(&socket);
            assert_eq!(
                first_question(&second)
                    .expect("follow-up question")
                    .name
                    .text(),
                "host.service.example.test"
            );
            send_a(&socket, &second, peer, [192, 0, 2, 72]);
        });

        let lookup = resolver_for(server)
            .lookup_name("host.branch.example.test", 2)
            .expect("cross-transaction DNAME lookup");
        worker.join().expect("test DNS worker");

        assert_eq!(lookup.canonical_name, "host.service.example.test");
        assert_eq!(
            lookup.addresses,
            vec![IpAddr::V4(Ipv4Addr::new(192, 0, 2, 72))]
        );
    }

    #[test]
    fn cross_transaction_redirect_loop_is_rejected_without_a_third_query() {
        let socket = UdpSocket::bind("127.0.0.1:0").expect("bind test DNS server");
        socket
            .set_read_timeout(Some(Duration::from_secs(2)))
            .expect("set test timeout");
        let server = socket.local_addr().expect("test DNS server address");

        let worker = thread::spawn(move || {
            let (first, peer) = receive_query(&socket);
            assert_eq!(
                first_question(&first).expect("first question").name.text(),
                "loop-a.example.test"
            );
            send_redirect(
                &socket,
                &first,
                peer,
                &[0xc0, 0x0c],
                wire::TYPE_CNAME,
                "loop-b.example.test",
            );

            let (second, peer) = receive_query(&socket);
            assert_eq!(
                first_question(&second)
                    .expect("second question")
                    .name
                    .text(),
                "loop-b.example.test"
            );
            send_redirect(
                &socket,
                &second,
                peer,
                &[0xc0, 0x0c],
                wire::TYPE_CNAME,
                "loop-a.example.test",
            );
        });

        let result = resolver_for(server).lookup_name("loop-a.example.test", 2);
        worker.join().expect("test DNS worker");
        assert!(matches!(
            result,
            Err(ResolveError::Wire(WireError::CnameLoop))
        ));
    }

    #[test]
    fn exactly_sixteen_in_packet_redirects_are_accepted() {
        let query = make_query("n0.example", TYPE_A, 0x4400).expect("query");
        let mut reply = response(&query, 17);
        for index in 0..16 {
            let owner = if index == 0 {
                vec![0xc0, 0x0c]
            } else {
                wire::encode_name(&format!("n{index}.example")).expect("owner")
            };
            let next = index + 1;
            let target = wire::encode_name(&format!("n{next}.example")).expect("target");
            append_answer(&mut reply, &owner, wire::TYPE_CNAME, &target);
        }
        let terminal = wire::encode_name("n16.example").expect("terminal owner");
        append_answer(&mut reply, &terminal, TYPE_A, &[192, 0, 2, 73]);

        assert_eq!(
            wire::classify_redirect_answer(&reply),
            Ok(wire::RedirectAnswer::Direct {
                canonical_name: "n16.example".to_owned(),
                redirects: 16,
            })
        );
    }

    #[test]
    fn seventeenth_in_packet_redirect_is_rejected() {
        let query = make_query("n0.example", TYPE_A, 0x4401).expect("query");
        let mut reply = response(&query, 17);
        for index in 0..17 {
            let owner = if index == 0 {
                vec![0xc0, 0x0c]
            } else {
                wire::encode_name(&format!("n{index}.example")).expect("owner")
            };
            let next = index + 1;
            let target = wire::encode_name(&format!("n{next}.example")).expect("target");
            append_answer(&mut reply, &owner, wire::TYPE_CNAME, &target);
        }

        assert_eq!(
            wire::classify_redirect_answer(&reply),
            Err(WireError::CnameLoop)
        );
    }

    #[test]
    fn redirect_following_across_transactions_accumulates_redirect_limits() {
        let socket = UdpSocket::bind("127.0.0.1:0").expect("bind test DNS server");
        socket
            .set_read_timeout(Some(Duration::from_secs(2)))
            .expect("set test timeout");
        let server = socket.local_addr().expect("test DNS server address");

        let worker = thread::spawn(move || {
            let (first, peer) = receive_query(&socket);
            assert_eq!(
                first_question(&first)
                    .expect("first follow-up query")
                    .name
                    .text(),
                "0.example"
            );
            send_redirect_chain(&socket, &first, peer, 0, 8, false);

            let (second, peer) = receive_query(&socket);
            assert_eq!(
                first_question(&second)
                    .expect("second follow-up query")
                    .name
                    .text(),
                "8.example"
            );
            send_redirect_chain(&socket, &second, peer, 8, 8, true);
        });

        let lookup = resolver_for(server)
            .lookup_name("0.example", 2)
            .expect("cumulative redirects across transactions");
        worker.join().expect("mock DNS thread");

        assert_eq!(
            lookup.canonical_name,
            "16.example",
            "redirect chain should terminate after crossing two transactions"
        );
        assert_eq!(
            lookup.addresses,
            vec![IpAddr::V4(Ipv4Addr::new(192, 0, 2, 100))]
        );
    }

    #[test]
    fn redirect_following_rejects_cumulative_overflow_across_transactions() {
        let socket = UdpSocket::bind("127.0.0.1:0").expect("bind test DNS server");
        socket
            .set_read_timeout(Some(Duration::from_secs(2)))
            .expect("set test timeout");
        let server = socket.local_addr().expect("test DNS server address");

        let worker = thread::spawn(move || {
            let (first, peer) = receive_query(&socket);
            assert_eq!(
                first_question(&first)
                    .expect("first follow-up query")
                    .name
                    .text(),
                "0.example"
            );
            send_redirect_chain(&socket, &first, peer, 0, 9, false);

            let (second, peer) = receive_query(&socket);
            assert_eq!(
                first_question(&second)
                    .expect("second follow-up query")
                    .name
                    .text(),
                "9.example"
            );
            send_redirect_chain(&socket, &second, peer, 9, 8, false);
        });

        assert!(matches!(
            resolver_for(server).lookup_name("0.example", 2),
            Err(ResolveError::Wire(WireError::CnameLoop))
        ));
        worker.join().expect("mock DNS thread");
    }
}