childflow 0.4.0

A per-command-tree network sandbox for Linux
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
// Copyright (c) 2026 Blacknon. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.

use std::borrow::Cow;
use std::fs::File;
use std::io::{BufWriter, ErrorKind, Write};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::thread::{self, JoinHandle};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use anyhow::{anyhow, bail, Context, Result};
use etherparse::{EtherType, Ethernet2HeaderSlice};
use pcap_file::pcapng::blocks::enhanced_packet::EnhancedPacketBlock;
use pcap_file::pcapng::blocks::interface_description::{
    InterfaceDescriptionBlock, InterfaceDescriptionOption,
};
use pcap_file::pcapng::PcapNgWriter;
use pcap_file::DataLink;
use pnet_datalink::{self, Channel::Ethernet};

use crate::cli::OutputView;
use crate::network::rootless_internal::packet::{
    self, Icmpv4EchoFrame, Icmpv6EchoFrame, ParsedPacket, TcpReply,
};

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum CaptureMode {
    AfPacket { interface_name: String },
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum CapturePlan {
    ChildOnly {
        mode: CaptureMode,
        output_path: PathBuf,
        metadata: CaptureMetadata,
    },
    RootfulSyntheticEgress {
        mode: CaptureMode,
        output_path: PathBuf,
        rewrite: RootfulEgressRewrite,
    },
    RootfulChildAndSyntheticEgress {
        mode: CaptureMode,
        child_output_path: PathBuf,
        egress_output_path: PathBuf,
        rewrite: RootfulEgressRewrite,
    },
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct RootfulEgressRewrite {
    pub child_ipv4: Ipv4Addr,
    pub child_ipv6: Ipv6Addr,
    pub host_egress_ipv4: Option<Ipv4Addr>,
    pub host_egress_ipv6: Option<Ipv6Addr>,
}

pub struct FrameCaptureWriter {
    pcap: Option<PcapNgWriter<BufWriter<File>>>,
}

impl FrameCaptureWriter {
    pub fn open(output_path: &Path, metadata: &CaptureMetadata) -> Result<Self> {
        let pcap = open_pcap_writer(output_path, metadata)?;
        Ok(Self { pcap: Some(pcap) })
    }

    pub fn write_frame(&mut self, frame: &[u8]) -> Result<()> {
        let timestamp = capture_timestamp();
        let block = EnhancedPacketBlock {
            interface_id: 0,
            timestamp,
            original_len: frame.len() as u32,
            data: Cow::Owned(frame.to_vec()),
            options: vec![],
        };
        self.pcap
            .as_mut()
            .ok_or_else(|| anyhow!("rootless frame capture writer is already closed"))?
            .write_pcapng_block(block)
            .context("failed to append enhanced packet block")?;
        self.flush()?;
        Ok(())
    }

    pub fn close(&mut self) -> Result<()> {
        let Some(pcap) = self.pcap.take() else {
            return Ok(());
        };
        let mut writer = pcap.into_inner();
        writer
            .flush()
            .context("failed to flush rootless pcapng output")?;
        Ok(())
    }

    pub fn flush(&mut self) -> Result<()> {
        let Some(pcap) = self.pcap.as_mut() else {
            return Ok(());
        };
        pcap.get_mut()
            .flush()
            .context("failed to flush rootless pcapng output")?;
        Ok(())
    }
}

impl Drop for FrameCaptureWriter {
    fn drop(&mut self) {
        if let Err(err) = self.close() {
            crate::util::warn(format!(
                "failed to finalize rootless packet capture output: {err:#}"
            ));
        }
    }
}

pub struct CaptureHandle {
    stop: Arc<AtomicBool>,
    join: Option<JoinHandle<Result<()>>>,
}

impl CaptureHandle {
    pub fn start(plan: CapturePlan) -> Result<Self> {
        let stop = Arc::new(AtomicBool::new(false));
        let stop_for_thread = Arc::clone(&stop);

        let join = thread::spawn(move || capture_loop(plan, stop_for_thread));

        Ok(Self {
            stop,
            join: Some(join),
        })
    }

    fn stop_and_join(&mut self) -> Result<()> {
        self.stop.store(true, Ordering::Relaxed);
        if let Some(join) = self.join.take() {
            match join.join() {
                Ok(Ok(())) => {}
                Ok(Err(err)) => {
                    return Err(err).context("packet capture stopped with an error");
                }
                Err(_) => {
                    bail!("packet capture thread panicked");
                }
            }
        }
        Ok(())
    }

    pub fn shutdown(mut self) -> Result<()> {
        self.stop_and_join()
    }
}

impl Drop for CaptureHandle {
    fn drop(&mut self) {
        if let Err(err) = self.stop_and_join() {
            crate::util::warn(format!("{err:#}"));
        }
    }
}

fn capture_loop(plan: CapturePlan, stop: Arc<AtomicBool>) -> Result<()> {
    let interface_name = match &plan {
        CapturePlan::ChildOnly { mode, .. }
        | CapturePlan::RootfulSyntheticEgress { mode, .. }
        | CapturePlan::RootfulChildAndSyntheticEgress { mode, .. } => match mode {
            CaptureMode::AfPacket { interface_name } => interface_name.clone(),
        },
    };

    let interface = pnet_datalink::interfaces()
        .into_iter()
        .find(|iface| iface.name == interface_name)
        .ok_or_else(|| anyhow!("capture interface not found: {interface_name}"))?;

    let config = pnet_datalink::Config {
        read_timeout: Some(Duration::from_millis(250)),
        read_buffer_size: 65_535,
        promiscuous: true,
        ..Default::default()
    };

    let (_, mut rx) = match pnet_datalink::channel(&interface, config)
        .with_context(|| format!("failed to open AF_PACKET channel on {interface_name}"))?
    {
        Ethernet(tx, rx) => (tx, rx),
        _ => bail!("unsupported datalink channel type"),
    };

    let mut writers = CaptureWriters::open(plan)?;

    while !stop.load(Ordering::Relaxed) {
        match rx.next() {
            Ok(packet) => {
                writers.write(packet)?;
            }
            Err(err)
                if err.kind() == ErrorKind::WouldBlock || err.kind() == ErrorKind::TimedOut => {}
            Err(err) => {
                if stop.load(Ordering::Relaxed) {
                    crate::util::debug(format!(
                        "stopping AF_PACKET capture on {interface_name} after shutdown signal: {err}"
                    ));
                    break;
                }
                return Err(err)
                    .with_context(|| format!("AF_PACKET receive failed on {interface_name}"));
            }
        }
    }

    writers.flush()?;
    Ok(())
}

pub struct CaptureWriters {
    child: Option<FrameCaptureWriter>,
    egress: Option<FrameCaptureWriter>,
    rewrite: Option<RootfulEgressRewrite>,
}

impl CaptureWriters {
    fn open(plan: CapturePlan) -> Result<Self> {
        match plan {
            CapturePlan::ChildOnly {
                output_path,
                metadata,
                ..
            } => Self::open_child_only(&output_path, metadata),
            CapturePlan::RootfulSyntheticEgress {
                output_path,
                rewrite,
                ..
            } => Self::open_synthetic_egress(
                &output_path,
                rewrite,
                CaptureMetadata::new("egress", "rootful", "synthetic", "synthetic-egress"),
            ),
            CapturePlan::RootfulChildAndSyntheticEgress {
                mode,
                child_output_path,
                egress_output_path,
                rewrite,
            } => Self::open_child_and_synthetic_egress(
                &child_output_path,
                &egress_output_path,
                rewrite,
                CaptureMetadata::new(
                    "child",
                    "rootful",
                    "isolated",
                    match mode {
                        CaptureMode::AfPacket { interface_name } => interface_name,
                    },
                ),
                CaptureMetadata::new("egress", "rootful", "synthetic", "synthetic-egress"),
            ),
        }
    }

    pub fn open_child_only(output_path: &Path, metadata: CaptureMetadata) -> Result<Self> {
        Ok(Self {
            child: Some(FrameCaptureWriter::open(output_path, &metadata)?),
            egress: None,
            rewrite: None,
        })
    }

    pub fn open_synthetic_egress(
        output_path: &Path,
        rewrite: RootfulEgressRewrite,
        metadata: CaptureMetadata,
    ) -> Result<Self> {
        Ok(Self {
            child: None,
            egress: Some(FrameCaptureWriter::open(output_path, &metadata)?),
            rewrite: Some(rewrite),
        })
    }

    pub fn open_child_and_synthetic_egress(
        child_output_path: &Path,
        egress_output_path: &Path,
        rewrite: RootfulEgressRewrite,
        child_metadata: CaptureMetadata,
        egress_metadata: CaptureMetadata,
    ) -> Result<Self> {
        Ok(Self {
            child: Some(FrameCaptureWriter::open(
                child_output_path,
                &child_metadata,
            )?),
            egress: Some(FrameCaptureWriter::open(
                egress_output_path,
                &egress_metadata,
            )?),
            rewrite: Some(rewrite),
        })
    }

    pub fn write(&mut self, packet: &[u8]) -> Result<()> {
        if let Some(writer) = self.child.as_mut() {
            writer.write_frame(packet)?;
        }

        if let Some(writer) = self.egress.as_mut() {
            if let Some(rewrite) = self.rewrite {
                if let Some(frame) = rewrite_rootful_egress_frame(packet, rewrite)? {
                    writer.write_frame(&frame)?;
                }
            } else {
                writer.write_frame(packet)?;
            }
        }

        Ok(())
    }

    pub fn flush(&mut self) -> Result<()> {
        if let Some(writer) = self.child.as_mut() {
            writer.flush()?;
        }
        if let Some(writer) = self.egress.as_mut() {
            writer.flush()?;
        }
        Ok(())
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CaptureMetadata {
    view: &'static str,
    backend: &'static str,
    kind: &'static str,
    interface_name: String,
}

impl CaptureMetadata {
    pub fn new(
        view: &'static str,
        backend: &'static str,
        kind: &'static str,
        interface_name: impl Into<String>,
    ) -> Self {
        Self {
            view,
            backend,
            kind,
            interface_name: interface_name.into(),
        }
    }

    fn description(&self) -> String {
        format!(
            "childflow view={} backend={} kind={} interface={}",
            self.view, self.backend, self.kind, self.interface_name
        )
    }
}

fn open_pcap_writer(
    output_path: &Path,
    metadata: &CaptureMetadata,
) -> Result<PcapNgWriter<BufWriter<File>>> {
    let file = File::create(output_path)
        .with_context(|| format!("failed to create {}", output_path.display()))?;
    let writer = BufWriter::new(file);
    let mut pcap = PcapNgWriter::new(writer).context("failed to create pcapng writer")?;

    let idb = InterfaceDescriptionBlock {
        linktype: DataLink::ETHERNET,
        snaplen: 65_535,
        // `pcap-file` serializes EPB timestamps as raw nanoseconds. Advertise that
        // resolution explicitly so readers such as tcpdump do not assume the pcapng
        // default of microseconds and inflate wall-clock time by 1000x.
        options: vec![
            InterfaceDescriptionOption::IfTsResol(9),
            InterfaceDescriptionOption::IfName(metadata.interface_name.clone().into()),
            InterfaceDescriptionOption::IfDescription(metadata.description().into()),
        ],
    };
    pcap.write_pcapng_block(idb)
        .context("failed to write pcapng interface description block")?;
    pcap.get_mut()
        .flush()
        .context("failed to flush the initial pcapng header")?;
    Ok(pcap)
}

fn capture_timestamp() -> Duration {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_else(|_| Duration::from_secs(0))
}

pub fn derive_output_paths(base: &Path, output_view: OutputView) -> Result<(PathBuf, PathBuf)> {
    match output_view {
        OutputView::Child | OutputView::Egress | OutputView::WireEgress => {
            Ok((base.to_path_buf(), base.to_path_buf()))
        }
        OutputView::Both => {
            let parent = base.parent().unwrap_or_else(|| Path::new("."));
            let file_name = base
                .file_name()
                .and_then(|name| name.to_str())
                .ok_or_else(|| anyhow!("output path must end with a valid UTF-8 file name"))?;
            let stem = file_name.strip_suffix(".pcapng").unwrap_or(file_name);
            Ok((
                parent.join(format!("{stem}.child.pcapng")),
                parent.join(format!("{stem}.egress.pcapng")),
            ))
        }
    }
}

fn rewrite_rootful_egress_frame(
    frame: &[u8],
    rewrite: RootfulEgressRewrite,
) -> Result<Option<Vec<u8>>> {
    let eth = Ethernet2HeaderSlice::from_slice(frame).context("failed to parse Ethernet header")?;

    match eth.ether_type() {
        EtherType::IPV4 | EtherType::IPV6 => {}
        _ => return Ok(None),
    }

    match packet::parse_frame(frame) {
        Ok(ParsedPacket::Tcp(tcp)) => {
            let Some((src_ip, dst_ip)) = rewrite_ips(tcp.meta.src_ip, tcp.meta.dst_ip, rewrite)?
            else {
                return Ok(None);
            };
            Ok(Some(packet::build_tcp_frame(TcpReply {
                src_mac: tcp.meta.src_mac,
                dst_mac: tcp.meta.dst_mac,
                src_ip,
                dst_ip,
                src_port: tcp.src_port,
                dst_port: tcp.dst_port,
                seq: tcp.sequence_number,
                ack: tcp.acknowledgment_number,
                syn: tcp.syn,
                ack_flag: tcp.ack,
                fin: tcp.fin,
                rst: tcp.rst,
                psh: tcp.psh,
                payload: &tcp.payload,
            })?))
        }
        Ok(ParsedPacket::Udp(udp)) => {
            let Some((src_ip, dst_ip)) = rewrite_ips(udp.meta.src_ip, udp.meta.dst_ip, rewrite)?
            else {
                return Ok(None);
            };
            Ok(Some(packet::build_udp_frame(
                udp.meta.src_mac,
                udp.meta.dst_mac,
                src_ip,
                dst_ip,
                udp.src_port,
                udp.dst_port,
                &udp.payload,
            )?))
        }
        Ok(ParsedPacket::Icmpv4(icmp)) => {
            let Some((src_ip, dst_ip)) = rewrite_ips(icmp.meta.src_ip, icmp.meta.dst_ip, rewrite)?
            else {
                return Ok(None);
            };
            let (src_ip, dst_ip) = match (src_ip, dst_ip) {
                (IpAddr::V4(src_ip), IpAddr::V4(dst_ip)) => (src_ip, dst_ip),
                _ => return Ok(None),
            };
            Ok(Some(packet::build_icmpv4_echo_frame(Icmpv4EchoFrame {
                src_mac: icmp.meta.src_mac,
                dst_mac: icmp.meta.dst_mac,
                src_ip,
                dst_ip,
                icmp_type: icmp.icmp_type,
                code: icmp.code,
                identifier: icmp.identifier,
                sequence: icmp.sequence,
                payload: &icmp.payload,
            })?))
        }
        Ok(ParsedPacket::Icmpv6(icmp)) => {
            let Some((src_ip, dst_ip)) = rewrite_ips(icmp.meta.src_ip, icmp.meta.dst_ip, rewrite)?
            else {
                return Ok(None);
            };
            let (src_ip, dst_ip) = match (src_ip, dst_ip) {
                (IpAddr::V6(src_ip), IpAddr::V6(dst_ip)) => (src_ip, dst_ip),
                _ => return Ok(None),
            };
            Ok(Some(packet::build_icmpv6_echo_frame(Icmpv6EchoFrame {
                src_mac: icmp.meta.src_mac,
                dst_mac: icmp.meta.dst_mac,
                src_ip,
                dst_ip,
                icmp_type: icmp.icmp_type,
                code: icmp.code,
                identifier: icmp.identifier,
                sequence: icmp.sequence,
                payload: &icmp.payload,
            })?))
        }
        Ok(ParsedPacket::Unsupported) => Ok(None),
        Err(_) => Ok(None),
    }
}

fn rewrite_ips(
    src_ip: IpAddr,
    dst_ip: IpAddr,
    rewrite: RootfulEgressRewrite,
) -> Result<Option<(IpAddr, IpAddr)>> {
    match (src_ip, dst_ip) {
        (IpAddr::V4(src), IpAddr::V4(dst)) => {
            let Some(host_ip) = rewrite.host_egress_ipv4 else {
                return Ok(None);
            };
            Ok(Some((
                if src == rewrite.child_ipv4 {
                    IpAddr::V4(host_ip)
                } else {
                    IpAddr::V4(src)
                },
                if dst == rewrite.child_ipv4 {
                    IpAddr::V4(host_ip)
                } else {
                    IpAddr::V4(dst)
                },
            )))
        }
        (IpAddr::V6(src), IpAddr::V6(dst)) => {
            let Some(host_ip) = rewrite.host_egress_ipv6 else {
                return Ok(None);
            };
            Ok(Some((
                if src == rewrite.child_ipv6 {
                    IpAddr::V6(host_ip)
                } else {
                    IpAddr::V6(src)
                },
                if dst == rewrite.child_ipv6 {
                    IpAddr::V6(host_ip)
                } else {
                    IpAddr::V6(dst)
                },
            )))
        }
        _ => bail!("mixed IPv4/IPv6 packet addresses are unsupported for capture rewriting"),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::network::rootless_internal::packet::{build_tcp_frame, ParsedPacket, TcpReply};

    #[test]
    fn derive_output_paths_for_both_appends_child_and_egress_suffixes() {
        let (child, egress) =
            derive_output_paths(Path::new("/tmp/capture.pcapng"), OutputView::Both).unwrap();

        assert_eq!(child, PathBuf::from("/tmp/capture.child.pcapng"));
        assert_eq!(egress, PathBuf::from("/tmp/capture.egress.pcapng"));
    }

    #[test]
    fn rewrite_rootful_egress_frame_rewrites_child_ipv4_endpoint_to_host_ipv4() {
        let frame = build_tcp_frame(TcpReply {
            src_mac: [0, 1, 2, 3, 4, 5],
            dst_mac: [6, 7, 8, 9, 10, 11],
            src_ip: IpAddr::V4(Ipv4Addr::new(10, 240, 0, 2)),
            dst_ip: IpAddr::V4(Ipv4Addr::new(93, 184, 216, 34)),
            src_port: 12345,
            dst_port: 443,
            seq: 1,
            ack: 2,
            syn: true,
            ack_flag: true,
            fin: false,
            rst: false,
            psh: false,
            payload: &[],
        })
        .unwrap();

        let rewritten = rewrite_rootful_egress_frame(
            &frame,
            RootfulEgressRewrite {
                child_ipv4: Ipv4Addr::new(10, 240, 0, 2),
                child_ipv6: "fd42::2".parse().unwrap(),
                host_egress_ipv4: Some(Ipv4Addr::new(192, 0, 2, 10)),
                host_egress_ipv6: Some("2001:db8::10".parse().unwrap()),
            },
        )
        .unwrap()
        .unwrap();

        match packet::parse_frame(&rewritten).unwrap() {
            ParsedPacket::Tcp(tcp) => {
                assert_eq!(tcp.meta.src_ip, IpAddr::V4(Ipv4Addr::new(192, 0, 2, 10)));
                assert_eq!(tcp.meta.dst_ip, IpAddr::V4(Ipv4Addr::new(93, 184, 216, 34)));
            }
            other => panic!("expected rewritten TCP packet, got {other:?}"),
        }
    }

    #[test]
    fn rewrite_rootful_egress_frame_skips_ipv6_when_no_ipv6_egress_is_known() {
        let frame = build_tcp_frame(TcpReply {
            src_mac: [0, 1, 2, 3, 4, 5],
            dst_mac: [6, 7, 8, 9, 10, 11],
            src_ip: IpAddr::V6("fd42::2".parse().unwrap()),
            dst_ip: IpAddr::V6("2001:db8::1".parse().unwrap()),
            src_port: 12345,
            dst_port: 443,
            seq: 1,
            ack: 2,
            syn: true,
            ack_flag: true,
            fin: false,
            rst: false,
            psh: false,
            payload: &[],
        })
        .unwrap();

        let rewritten = rewrite_rootful_egress_frame(
            &frame,
            RootfulEgressRewrite {
                child_ipv4: Ipv4Addr::new(10, 240, 0, 2),
                child_ipv6: "fd42::2".parse().unwrap(),
                host_egress_ipv4: Some(Ipv4Addr::new(192, 0, 2, 10)),
                host_egress_ipv6: None,
            },
        )
        .unwrap();

        assert!(rewritten.is_none());
    }
}