fips-core 0.3.19

Reusable FIPS mesh, endpoint, transport, and protocol library
Documentation
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
//! Host firewall helpers for FIPS mesh TUN interfaces.
//!
//! This module intentionally owns only narrowly scoped rules for one mesh
//! interface. The policy is default-deny for FIPS-addressed inbound traffic and
//! outbound traffic, with stateful outbound TCP allowed and optional inbound TCP
//! service ports.

#[cfg(any(test, target_os = "macos"))]
use std::fmt::Write as _;
#[cfg(any(target_os = "linux", target_os = "macos"))]
use std::process::Output;
#[cfg(any(target_os = "linux", target_os = "macos"))]
use std::process::{Command, Stdio};

use thiserror::Error;

/// The IPv6 prefix used by FIPS mesh addresses.
pub const FIPS_MESH_IPV6_PREFIX: &str = "fd00::/8";

const DEFAULT_LINUX_TABLE_NAME: &str = "fips_host";
const DEFAULT_MACOS_ANCHOR_NAME: &str = "com.apple/fips/host";

/// Platform firewall configuration for a FIPS host-facing TUN interface.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HostFirewallConfig {
    interface: String,
    inbound_tcp_ports: Vec<u16>,
    linux_table_name: String,
    macos_anchor_name: String,
}

impl HostFirewallConfig {
    /// Build a firewall config for `interface`.
    #[must_use]
    pub fn new(interface: impl Into<String>) -> Self {
        Self {
            interface: interface.into(),
            inbound_tcp_ports: Vec::new(),
            linux_table_name: DEFAULT_LINUX_TABLE_NAME.to_string(),
            macos_anchor_name: DEFAULT_MACOS_ANCHOR_NAME.to_string(),
        }
    }

    /// Allow inbound TCP connections to the supplied destination ports.
    #[must_use]
    pub fn with_inbound_tcp_ports(mut self, ports: impl IntoIterator<Item = u16>) -> Self {
        self.inbound_tcp_ports = normalized_tcp_ports(ports);
        self
    }

    /// Override the managed Linux nftables table name.
    #[must_use]
    pub fn with_linux_table_name(mut self, table_name: impl Into<String>) -> Self {
        self.linux_table_name = table_name.into();
        self
    }

    /// Override the managed macOS PF anchor name.
    #[must_use]
    pub fn with_macos_anchor_name(mut self, anchor_name: impl Into<String>) -> Self {
        self.macos_anchor_name = anchor_name.into();
        self
    }

    /// TUN interface matched by the rules.
    #[must_use]
    pub fn interface(&self) -> &str {
        &self.interface
    }

    /// Normalized inbound TCP destination ports.
    #[must_use]
    pub fn inbound_tcp_ports(&self) -> &[u16] {
        &self.inbound_tcp_ports
    }

    /// Managed Linux nftables table name.
    #[must_use]
    pub fn linux_table_name(&self) -> &str {
        &self.linux_table_name
    }

    /// Managed macOS PF anchor name.
    #[must_use]
    pub fn macos_anchor_name(&self) -> &str {
        &self.macos_anchor_name
    }

    fn validate(&self) -> Result<(), HostFirewallError> {
        validate_interface_name(&self.interface)?;
        validate_nft_table_name(&self.linux_table_name)?;
        validate_pf_anchor_name(&self.macos_anchor_name)?;
        Ok(())
    }
}

/// RAII guard for installed host firewall rules.
///
/// Dropping the guard removes only the managed table/anchor. It does not flush
/// the host's main firewall ruleset.
#[derive(Debug)]
pub struct HostFirewallGuard {
    backend: HostFirewallBackend,
}

#[derive(Debug)]
enum HostFirewallBackend {
    #[cfg(target_os = "linux")]
    Linux { table_name: String },
    #[cfg(target_os = "macos")]
    Macos {
        anchor_name: String,
        enable_token: Option<String>,
    },
    #[cfg_attr(not(any(target_os = "linux", target_os = "macos")), allow(dead_code))]
    #[cfg(not(any(target_os = "linux", target_os = "macos")))]
    Unsupported,
}

impl HostFirewallGuard {
    /// True when this target has an implemented host firewall backend.
    #[must_use]
    pub const fn platform_supported() -> bool {
        cfg!(any(target_os = "linux", target_os = "macos"))
    }

    /// True when this target has an implemented backend and the required
    /// platform command is present.
    #[must_use]
    pub fn platform_available() -> bool {
        #[cfg(target_os = "linux")]
        return command_exists("nft");
        #[cfg(target_os = "macos")]
        return command_exists("pfctl");
        #[cfg(not(any(target_os = "linux", target_os = "macos")))]
        return false;
    }

    /// Install host firewall rules for the configured mesh interface.
    ///
    /// Returns an error on unsupported platforms or when the platform firewall
    /// command is unavailable. Callers should treat that as a hard failure
    /// before exposing the host tunnel.
    pub fn install(config: &HostFirewallConfig) -> Result<Self, HostFirewallError> {
        config.validate()?;

        #[cfg(target_os = "linux")]
        {
            install_linux_firewall(config)
        }
        #[cfg(target_os = "macos")]
        {
            install_macos_firewall(config)
        }
        #[cfg(not(any(target_os = "linux", target_os = "macos")))]
        {
            let _ = config;
            Err(HostFirewallError::UnsupportedPlatform)
        }
    }

    /// Remove managed firewall artifacts without requiring a live guard.
    ///
    /// Useful after crash/restart paths where the previous process may have
    /// died before its guard could drop.
    pub fn cleanup_disabled_artifacts(config: &HostFirewallConfig) {
        #[cfg(target_os = "linux")]
        remove_nft_table(config.linux_table_name());
        #[cfg(target_os = "macos")]
        flush_pf_anchor(config.macos_anchor_name());
        #[cfg(not(any(target_os = "linux", target_os = "macos")))]
        let _ = config;
    }
}

impl Drop for HostFirewallGuard {
    fn drop(&mut self) {
        match &self.backend {
            #[cfg(target_os = "linux")]
            HostFirewallBackend::Linux { table_name } => remove_nft_table(table_name),
            #[cfg(target_os = "macos")]
            HostFirewallBackend::Macos {
                anchor_name,
                enable_token,
            } => {
                flush_pf_anchor(anchor_name);
                if let Some(token) = enable_token {
                    release_pf_enable_token(token);
                }
            }
            #[cfg(not(any(target_os = "linux", target_os = "macos")))]
            HostFirewallBackend::Unsupported => {}
        }
    }
}

/// Errors returned while installing platform firewall rules.
#[derive(Debug, Error)]
pub enum HostFirewallError {
    /// Host firewall support is not implemented for the current platform.
    #[error("host firewall is not supported on this platform")]
    UnsupportedPlatform,

    /// A required platform command was not found in PATH.
    #[error("required firewall command `{0}` was not found")]
    MissingCommand(&'static str),

    /// A user-supplied or kernel-supplied name was unsafe for rule rendering.
    #[error("invalid {field}: {value}")]
    InvalidName {
        /// Field name.
        field: &'static str,
        /// Invalid value.
        value: String,
    },

    /// Failed to spawn or communicate with a platform firewall command.
    #[error("failed to run `{command}`: {source}")]
    CommandIo {
        /// Command name.
        command: &'static str,
        /// I/O error.
        #[source]
        source: std::io::Error,
    },

    /// A platform firewall command exited unsuccessfully.
    #[error("`{command}` exited with {status}: {stderr}")]
    CommandFailed {
        /// Command name.
        command: &'static str,
        /// Process exit status.
        status: std::process::ExitStatus,
        /// Captured stderr.
        stderr: String,
    },
}

fn normalized_tcp_ports(ports: impl IntoIterator<Item = u16>) -> Vec<u16> {
    let mut ports = ports.into_iter().collect::<Vec<_>>();
    ports.sort_unstable();
    ports.dedup();
    ports
}

fn validate_interface_name(name: &str) -> Result<(), HostFirewallError> {
    validate_name(
        "interface",
        name,
        |ch| ch.is_ascii_alphanumeric() || matches!(ch, '_' | '-' | '.'),
        false,
    )
}

fn validate_nft_table_name(name: &str) -> Result<(), HostFirewallError> {
    if name.is_empty()
        || !name
            .chars()
            .next()
            .is_some_and(|ch| ch.is_ascii_alphabetic() || ch == '_')
    {
        return Err(HostFirewallError::InvalidName {
            field: "nft table name",
            value: name.to_string(),
        });
    }
    validate_name(
        "nft table name",
        name,
        |ch| ch.is_ascii_alphanumeric() || ch == '_',
        false,
    )
}

fn validate_pf_anchor_name(name: &str) -> Result<(), HostFirewallError> {
    validate_name(
        "pf anchor name",
        name,
        |ch| ch.is_ascii_alphanumeric() || matches!(ch, '_' | '-' | '.' | '/'),
        true,
    )
}

fn validate_name(
    field: &'static str,
    value: &str,
    valid_char: impl Fn(char) -> bool,
    allow_slash: bool,
) -> Result<(), HostFirewallError> {
    let slash_ok = allow_slash && !value.starts_with('/') && !value.ends_with('/');
    let slash_valid = !value.contains('/') || slash_ok;
    if value.is_empty() || !slash_valid || !value.chars().all(valid_char) {
        return Err(HostFirewallError::InvalidName {
            field,
            value: value.to_string(),
        });
    }
    Ok(())
}

#[cfg(any(test, target_os = "linux"))]
#[must_use]
pub fn render_nft_host_firewall_rules(
    table_name: &str,
    iface: &str,
    inbound_tcp_ports: &[u16],
) -> String {
    let ports = normalized_tcp_ports(inbound_tcp_ports.iter().copied());
    let inbound_tcp_rule = match ports.as_slice() {
        [] => String::new(),
        [port] => format!("    tcp dport {port} accept\n"),
        ports => {
            let joined = ports
                .iter()
                .map(u16::to_string)
                .collect::<Vec<_>>()
                .join(", ");
            format!("    tcp dport {{ {joined} }} accept\n")
        }
    };

    format!(
        "table inet {table_name} {{\n\
           chain input {{\n\
             type filter hook input priority 0; policy accept;\n\
             iifname != \"{iface}\" return\n\
             meta nfproto != ipv6 return\n\
             ip6 saddr != {FIPS_MESH_IPV6_PREFIX} return\n\
             ct state established,related accept\n\
         {inbound_tcp_rule}\
             counter drop\n\
           }}\n\
           chain output {{\n\
             type filter hook output priority 0; policy accept;\n\
             oifname != \"{iface}\" return\n\
             meta nfproto != ipv6 return\n\
             ip6 daddr != {FIPS_MESH_IPV6_PREFIX} return\n\
             ct state established,related accept\n\
             meta l4proto tcp accept\n\
             counter drop\n\
           }}\n\
         }}\n"
    )
}

#[cfg(any(test, target_os = "macos"))]
#[must_use]
pub fn render_macos_pf_host_firewall_rules(iface: &str, inbound_tcp_ports: &[u16]) -> String {
    let ports = normalized_tcp_ports(inbound_tcp_ports.iter().copied());
    let mut rules = String::from("# Managed by fips-core for FIPS host routing.\n");

    match ports.as_slice() {
        [] => {}
        [port] => {
            let _ = writeln!(
                rules,
                "pass in quick on {iface} inet6 proto tcp from {FIPS_MESH_IPV6_PREFIX} to any port {port} flags S/SA keep state"
            );
        }
        ports => {
            let joined = ports
                .iter()
                .map(u16::to_string)
                .collect::<Vec<_>>()
                .join(", ");
            let _ = writeln!(
                rules,
                "pass in quick on {iface} inet6 proto tcp from {FIPS_MESH_IPV6_PREFIX} to any port {{ {joined} }} flags S/SA keep state"
            );
        }
    }

    let _ = write!(
        rules,
        "pass out quick on {iface} inet6 proto tcp from any to {FIPS_MESH_IPV6_PREFIX} flags S/SA keep state\n\
         block drop in quick on {iface} inet6 from {FIPS_MESH_IPV6_PREFIX} to any\n\
         block drop out quick on {iface} inet6 from any to {FIPS_MESH_IPV6_PREFIX}\n"
    );
    rules
}

#[cfg(target_os = "linux")]
fn install_linux_firewall(
    config: &HostFirewallConfig,
) -> Result<HostFirewallGuard, HostFirewallError> {
    if !command_exists("nft") {
        return Err(HostFirewallError::MissingCommand("nft"));
    }

    let rules = render_nft_host_firewall_rules(
        config.linux_table_name(),
        config.interface(),
        config.inbound_tcp_ports(),
    );
    remove_nft_table(config.linux_table_name());
    let mut child = Command::new("nft")
        .arg("-f")
        .arg("-")
        .stdin(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .map_err(|source| HostFirewallError::CommandIo {
            command: "nft",
            source,
        })?;
    {
        let stdin = child
            .stdin
            .as_mut()
            .ok_or_else(|| HostFirewallError::CommandIo {
                command: "nft",
                source: std::io::Error::new(
                    std::io::ErrorKind::BrokenPipe,
                    "nft stdin unavailable",
                ),
            })?;
        use std::io::Write as _;
        stdin
            .write_all(rules.as_bytes())
            .map_err(|source| HostFirewallError::CommandIo {
                command: "nft",
                source,
            })?;
    }
    let output = child
        .wait_with_output()
        .map_err(|source| HostFirewallError::CommandIo {
            command: "nft",
            source,
        })?;
    ensure_success("nft", output)?;

    Ok(HostFirewallGuard {
        backend: HostFirewallBackend::Linux {
            table_name: config.linux_table_name().to_string(),
        },
    })
}

#[cfg(target_os = "linux")]
fn remove_nft_table(table_name: &str) {
    if !command_exists("nft") {
        return;
    }
    let _ = Command::new("nft")
        .arg("delete")
        .arg("table")
        .arg("inet")
        .arg(table_name)
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status();
}

#[cfg(target_os = "macos")]
fn install_macos_firewall(
    config: &HostFirewallConfig,
) -> Result<HostFirewallGuard, HostFirewallError> {
    if !command_exists("pfctl") {
        return Err(HostFirewallError::MissingCommand("pfctl"));
    }

    let rules = render_macos_pf_host_firewall_rules(config.interface(), config.inbound_tcp_ports());
    let _ = run_pfctl(&["-a", config.macos_anchor_name(), "-F", "rules"], None)?;
    run_pfctl(&["-a", config.macos_anchor_name(), "-f", "-"], Some(&rules))?;
    let enable_output = run_pfctl(&["-E"], None)?;
    let enable_token = parse_pf_enable_token(&String::from_utf8_lossy(&enable_output.stdout));

    Ok(HostFirewallGuard {
        backend: HostFirewallBackend::Macos {
            anchor_name: config.macos_anchor_name().to_string(),
            enable_token,
        },
    })
}

#[cfg(target_os = "macos")]
fn flush_pf_anchor(anchor_name: &str) {
    if !command_exists("pfctl") {
        return;
    }
    let _ = run_pfctl(&["-a", anchor_name, "-F", "rules"], None);
}

#[cfg(target_os = "macos")]
fn release_pf_enable_token(token: &str) {
    if !command_exists("pfctl") {
        return;
    }
    let _ = run_pfctl(&["-X", token], None);
}

#[cfg(target_os = "macos")]
fn run_pfctl(args: &[&str], stdin: Option<&str>) -> Result<Output, HostFirewallError> {
    let mut command = Command::new("pfctl");
    command.args(args).stderr(Stdio::piped());
    if stdin.is_some() {
        command.stdin(Stdio::piped());
    }
    let mut child = command
        .spawn()
        .map_err(|source| HostFirewallError::CommandIo {
            command: "pfctl",
            source,
        })?;
    if let Some(input) = stdin {
        let child_stdin = child
            .stdin
            .as_mut()
            .ok_or_else(|| HostFirewallError::CommandIo {
                command: "pfctl",
                source: std::io::Error::new(
                    std::io::ErrorKind::BrokenPipe,
                    "pfctl stdin unavailable",
                ),
            })?;
        use std::io::Write as _;
        child_stdin
            .write_all(input.as_bytes())
            .map_err(|source| HostFirewallError::CommandIo {
                command: "pfctl",
                source,
            })?;
    }
    let output = child
        .wait_with_output()
        .map_err(|source| HostFirewallError::CommandIo {
            command: "pfctl",
            source,
        })?;
    ensure_success("pfctl", output)
}

#[cfg(target_os = "macos")]
fn parse_pf_enable_token(output: &str) -> Option<String> {
    output.lines().find_map(|line| {
        let (label, value) = line.split_once(':')?;
        if label.trim().eq_ignore_ascii_case("token") {
            let token = value.trim();
            if !token.is_empty() {
                return Some(token.to_string());
            }
        }
        None
    })
}

#[cfg(any(target_os = "linux", target_os = "macos"))]
fn ensure_success(command: &'static str, output: Output) -> Result<Output, HostFirewallError> {
    if output.status.success() {
        Ok(output)
    } else {
        Err(HostFirewallError::CommandFailed {
            command,
            status: output.status,
            stderr: String::from_utf8_lossy(&output.stderr).trim().to_string(),
        })
    }
}

#[cfg(any(target_os = "linux", target_os = "macos"))]
fn command_exists(command: &str) -> bool {
    Command::new("sh")
        .arg("-c")
        .arg(format!("command -v {command} >/dev/null 2>&1"))
        .status()
        .is_ok_and(|status| status.success())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn config_normalizes_inbound_tcp_ports() {
        let config = HostFirewallConfig::new("fips0").with_inbound_tcp_ports([443, 22, 22]);

        assert_eq!(config.inbound_tcp_ports(), &[22, 443]);
    }

    #[test]
    fn rejects_unsafe_names() {
        assert!(HostFirewallConfig::new("utun0").validate().is_ok());
        assert!(HostFirewallConfig::new("utun0; reboot").validate().is_err());
        assert!(
            HostFirewallConfig::new("utun0")
                .with_linux_table_name("1bad")
                .validate()
                .is_err()
        );
        assert!(
            HostFirewallConfig::new("utun0")
                .with_macos_anchor_name("/bad")
                .validate()
                .is_err()
        );
    }

    #[test]
    fn nft_rules_default_to_outbound_tcp_only() {
        let rules = render_nft_host_firewall_rules("fips_host", "nvpn0", &[]);

        assert!(rules.contains("table inet fips_host"));
        assert!(rules.contains("iifname != \"nvpn0\" return"));
        assert!(rules.contains("oifname != \"nvpn0\" return"));
        assert!(rules.contains("ip6 saddr != fd00::/8 return"));
        assert!(rules.contains("ip6 daddr != fd00::/8 return"));
        assert!(rules.contains("meta l4proto tcp accept"));
        assert!(!rules.contains("tcp dport"));
    }

    #[test]
    fn nft_rules_allow_configured_inbound_tcp_ports() {
        let rules = render_nft_host_firewall_rules("fips_host", "nvpn0", &[443, 22, 22]);

        assert!(rules.contains("tcp dport { 22, 443 } accept"));
    }

    #[test]
    fn macos_pf_rules_default_to_outbound_tcp_only() {
        let rules = render_macos_pf_host_firewall_rules("utun8", &[]);

        assert!(rules.contains("pass out quick on utun8 inet6 proto tcp"));
        assert!(rules.contains("block drop in quick on utun8 inet6 from fd00::/8 to any"));
        assert!(rules.contains("block drop out quick on utun8 inet6 from any to fd00::/8"));
        assert!(!rules.contains("pass in quick"));
        assert!(!rules.contains("proto udp"));
    }

    #[test]
    fn macos_pf_rules_allow_configured_inbound_tcp_ports() {
        let rules = render_macos_pf_host_firewall_rules("utun8", &[443, 22, 22]);

        assert!(rules.contains(
            "pass in quick on utun8 inet6 proto tcp from fd00::/8 to any port { 22, 443 }"
        ));
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn parses_pf_enable_token() {
        assert_eq!(
            parse_pf_enable_token("Token : 1234567890\n"),
            Some("1234567890".to_string())
        );
    }
}