blackship 0.6.1

A FreeBSD jail manager with state machine control
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
//! Orphan reclamation: scope records that outlived their jail.

use crate::audit::{AuditEvent, AuditLog, AuditRecord};
use crate::bulkhead::BulkheadManager;
use crate::error::{Error, Result};
use crate::jail::{RunningJails, jail_getname};
use crate::manifest::BlackshipConfig;
use crate::network::Bridge;
use crate::network::store::{
    FileLock, NetworkLeaseStore, NetworkStore, VnetStateRecord, VnetStateStore,
};
use crate::network::vnet::VnetSetup;
use crate::scope::{ScopeRecord, ScopeStore};
use crate::zfs::ZfsManager;
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;

/// What was found stranded, and what it takes to release it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OrphanKind {
    /// A scope record whose jail is no longer running.
    DeadScope(Box<ScopeRecord>),
    /// A VNET record with no scope record and no live jail.
    StrayVnet(Box<VnetStateRecord>),
    /// A blackship-tagged interface no VNET record claims.
    StrayInterface(String),
    /// A dataset under `jails/` with no scope record and no live jail.
    StrayDataset(String),
    /// A drain sub-anchor left loaded after its jail is gone.
    StrayDrainAnchor(String),
    /// A blackship-created bridge with no members and no network naming it.
    StrayBridge(String),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Orphan {
    pub jail: String,
    pub kind: OrphanKind,
    pub reason: String,
    /// Resources this orphan claims, as human-readable labels.
    pub resources: Vec<String>,
    /// True when reaping needs `--force`: blackship cannot prove it owns this.
    pub requires_force: bool,
}

impl Orphan {
    pub const fn kind_label(&self) -> &'static str {
        match self.kind {
            OrphanKind::DeadScope(_) => "scope",
            OrphanKind::StrayVnet(_) => "vnet",
            OrphanKind::StrayInterface(_) => "interface",
            OrphanKind::StrayDataset(_) => "dataset",
            OrphanKind::StrayDrainAnchor(_) => "drain-anchor",
            OrphanKind::StrayBridge(_) => "bridge",
        }
    }
}

/// A blackship-tagged `if_bridge` and how many interfaces hang off it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BridgeFacts {
    pub name: String,
    pub members: Vec<String>,
}

/// Facts a classification is made from, gathered by [`survey`].
#[derive(Default)]
pub struct Survey {
    pub live: HashSet<String>,
    pub scopes: Vec<ScopeRecord>,
    pub vnet: Vec<VnetStateRecord>,
    pub tagged_ifaces: Vec<String>,
    pub datasets: Vec<String>,
    pub drain_anchors: Vec<String>,
    /// Tagged interfaces that are bridges.
    pub bridges: Vec<BridgeFacts>,
    /// Bridges a network record or the manifest still names.
    pub named_bridges: HashSet<String>,
    /// Jail datasets fenced by a migration, with where each went.
    pub migrated: HashMap<String, String>,
}

/// Decide what is stranded. Pure: no syscalls, no filesystem.
pub fn classify(survey: &Survey) -> Vec<Orphan> {
    let scoped: HashSet<&str> = survey.scopes.iter().map(|s| s.name.as_str()).collect();
    let claimed_ifaces: HashSet<&str> = survey
        .vnet
        .iter()
        .flat_map(|r| [r.host_interface.as_str(), r.jail_interface.as_str()])
        .collect();

    let mut orphans = Vec::new();

    for scope in &survey.scopes {
        if !survey.live.contains(&scope.name) {
            orphans.push(dead_scope_orphan(scope));
        }
    }

    for record in &survey.vnet {
        if survey.live.contains(&record.owner) || scoped.contains(record.owner.as_str()) {
            continue;
        }
        orphans.push(Orphan {
            jail: record.owner.clone(),
            kind: OrphanKind::StrayVnet(Box::new(record.clone())),
            reason: "VNET record with no scope record and no live jail".to_string(),
            resources: vec![format!(
                "{} {}/{}",
                record.backend, record.host_interface, record.jail_interface
            )],
            requires_force: false,
        });
    }

    let bridges: HashMap<&str, &BridgeFacts> = survey
        .bridges
        .iter()
        .map(|bridge| (bridge.name.as_str(), bridge))
        .collect();

    for iface in &survey.tagged_ifaces {
        if claimed_ifaces.contains(iface.as_str()) {
            continue;
        }
        if let Some(bridge) = bridges.get(iface.as_str()) {
            orphans.extend(stray_bridge(survey, bridge));
            continue;
        }
        orphans.push(Orphan {
            jail: String::new(),
            kind: OrphanKind::StrayInterface(iface.clone()),
            reason: "interface carries the blackship group but no record claims it".to_string(),
            resources: vec![format!("interface {}", iface)],
            requires_force: true,
        });
    }

    for dataset in &survey.datasets {
        if survey.live.contains(dataset) || scoped.contains(dataset.as_str()) {
            continue;
        }
        orphans.push(Orphan {
            jail: dataset.clone(),
            kind: OrphanKind::StrayDataset(dataset.clone()),
            reason: stray_dataset_reason(survey.migrated.get(dataset)),
            resources: vec![format!("dataset {}", dataset)],
            requires_force: true,
        });
    }

    let claimed_anchors: HashSet<String> = survey
        .live
        .iter()
        .map(String::as_str)
        .chain(scoped.iter().copied())
        .map(crate::bulkhead::drain_anchor_path)
        .collect();

    for anchor in &survey.drain_anchors {
        if claimed_anchors.contains(anchor) {
            continue;
        }
        orphans.push(Orphan {
            jail: anchor.rsplit('/').next().unwrap_or_default().to_string(),
            kind: OrphanKind::StrayDrainAnchor(anchor.clone()),
            reason: "drain anchor outlived its jail and is still blocking traffic".to_string(),
            resources: vec![format!("pf anchor {}", anchor)],
            requires_force: false,
        });
    }

    orphans
}

/// A tagged bridge only a gone jail's epair, or nothing, still uses.
///
/// Records and members of a gone jail are reaped in the same pass, so they hold nothing.
fn stray_bridge(survey: &Survey, bridge: &BridgeFacts) -> Option<Orphan> {
    let gone = |owner: &str| !survey.live.contains(owner);
    let in_use = survey
        .vnet
        .iter()
        .any(|r| !gone(&r.owner) && r.bridge == bridge.name)
        || survey.named_bridges.contains(&bridge.name);
    let held = bridge.members.iter().any(|member| {
        !survey
            .vnet
            .iter()
            .any(|r| gone(&r.owner) && r.host_interface == *member)
    });
    if in_use || held {
        return None;
    }
    Some(Orphan {
        jail: bridge.name.clone(),
        kind: OrphanKind::StrayBridge(bridge.name.clone()),
        reason: "bridge blackship created has no members and no network naming it".to_string(),
        resources: vec![format!("bridge {}", bridge.name)],
        requires_force: false,
    })
}

/// Why a dataset with no owner is reported; a migrated one says where it went.
fn stray_dataset_reason(migrated_to: Option<&String>) -> String {
    migrated_to.map_or_else(
        || "dataset has no scope record and no live jail".to_string(),
        |fence| {
            let destination = fence.rsplit_once('@').map_or(fence.as_str(), |(dest, _)| dest);
            format!(
                "dataset was migrated to {destination} and is fenced; no scope record and no live jail"
            )
        },
    )
}

/// The orphan a dead jail's scope record stands for.
fn dead_scope_orphan(scope: &ScopeRecord) -> Orphan {
    if !scope.holds_resources() && scope.jid.is_none() {
        return Orphan {
            jail: scope.name.clone(),
            kind: OrphanKind::DeadScope(Box::new(scope.clone())),
            reason: "scope record holds no resources".to_string(),
            resources: Vec::new(),
            requires_force: false,
        };
    }

    let mut resources = Vec::new();
    if let Some(subject) = &scope.rctl_subject {
        resources.push(format!("rctl {subject}"));
    }
    if scope.has_vnet_record {
        resources.push("vnet".to_string());
    }
    if let Some(anchor) = &scope.drain_anchor {
        resources.push(format!("pf anchor {anchor}"));
    }
    for mount in &scope.mounts {
        resources.push(format!("mount {}", mount.display()));
    }
    for ip in &scope.allocated_ips {
        resources.push(format!("lease {ip}"));
    }
    if let Some(dataset) = &scope.zfs_dataset {
        resources.push(if scope.ephemeral {
            format!("dataset {dataset} (ephemeral)")
        } else {
            format!("dataset {dataset} (retained)")
        });
    }

    Orphan {
        jail: scope.name.clone(),
        kind: OrphanKind::DeadScope(Box::new(scope.clone())),
        reason: format!("jail is not running, scope phase {}", scope.phase.name()),
        resources,
        requires_force: false,
    }
}

/// Mountpoints a reap must detach, deepest first: everything under the default
/// jail root, plus every mount the scope record claims. A jail with an explicit
/// `path` lives outside the default root, so only the record knows its volumes.
fn reapable_mounts(
    jail_root: &std::path::Path,
    claimed: &[PathBuf],
    table: &str,
) -> Vec<PathBuf> {
    let mut mountpoints = crate::sys::mounted_paths_under(jail_root, table);
    for mount in claimed {
        for found in crate::sys::mounted_paths_under(mount, table) {
            if !mountpoints.contains(&found) {
                mountpoints.push(found);
            }
        }
    }
    mountpoints.sort_by_key(|path| std::cmp::Reverse(path.as_os_str().len()));
    mountpoints
}

/// Owns the stores a reap needs, plus the lock that serializes it.
pub struct Reaper {
    scope_store: ScopeStore,
    vnet_state_store: VnetStateStore,
    lease_store: NetworkLeaseStore,
    audit: AuditLog,
    health_store: crate::sickbay::HealthStateStore,
    network_store: NetworkStore,
    manifest_bridges: Vec<String>,
    zfs: Option<ZfsManager>,
    data_dir: PathBuf,
    verbose: bool,
    _lock: FileLock,
}

impl Reaper {
    pub fn new(config: &BlackshipConfig, verbose: bool) -> Result<Self> {
        let data_dir = config.config.data_dir.clone();
        let lock = FileLock::acquire(&data_dir.join("gc.lock"))?;

        let zfs = match (config.config.zfs_enabled, config.config.zpool.as_ref()) {
            (true, Some(pool)) => Some(ZfsManager::new(
                pool,
                &config.config.dataset,
                data_dir.join("jails"),
            )),
            _ => None,
        };

        Ok(Self {
            scope_store: ScopeStore::from_config(Some(config)),
            vnet_state_store: VnetStateStore::from_config(Some(config)),
            lease_store: NetworkLeaseStore::from_config(Some(config)),
            audit: AuditLog::from_config(Some(config)),
            health_store: crate::sickbay::HealthStateStore::from_config(Some(config)),
            network_store: NetworkStore::from_config(Some(config)),
            manifest_bridges: config
                .config
                .bridge
                .iter()
                .map(|bridge| bridge.name.clone())
                .collect(),
            zfs,
            data_dir,
            verbose,
            _lock: lock,
        })
    }

    /// Gather the live-vs-recorded picture of the host.
    pub fn survey(&self) -> Result<Survey> {
        let mut live = HashSet::new();
        for jid in RunningJails::new() {
            match jail_getname(jid) {
                Ok(name) => {
                    live.insert(name);
                }
                Err(e) if self.verbose => {
                    eprintln!("Warning: could not read name of JID {jid}: {e}");
                }
                Err(_) => {}
            }
        }

        let datasets = self
            .zfs
            .as_ref()
            .map(super::zfs::ZfsManager::list_jail_datasets)
            .unwrap_or_default();

        Ok(Survey {
            live,
            scopes: self.scope_store.list()?,
            vnet: self.vnet_state_store.list()?,
            tagged_ifaces: crate::sys::list_tagged_interfaces(),
            datasets: datasets.clone(),
            drain_anchors: crate::bulkhead::list_drain_anchors(),
            bridges: tagged_bridges(),
            named_bridges: self.named_bridges()?,
            migrated: self.migrated_datasets(&datasets),
        })
    }

    fn migrated_datasets(&self, datasets: &[String]) -> HashMap<String, String> {
        let Some(zfs) = &self.zfs else {
            return HashMap::new();
        };
        datasets
            .iter()
            .filter_map(|name| Some((name.clone(), zfs.jail_migration_fence(name)?)))
            .collect()
    }

    fn named_bridges(&self) -> Result<HashSet<String>> {
        let mut named: HashSet<String> = self
            .network_store
            .list()?
            .into_iter()
            .map(|record| record.bridge)
            .collect();
        named.extend(self.manifest_bridges.iter().cloned());
        Ok(named)
    }

    /// Release everything an orphan claims. `dry_run` reports without mutating.
    pub fn reap(&self, orphan: &Orphan, force: bool, dry_run: bool) -> Result<()> {
        if orphan.requires_force && !force {
            return Err(Error::InvalidArgument(format!(
                "refusing to reap {} '{}' without --force",
                orphan.kind_label(),
                orphan.jail
            )));
        }
        if dry_run {
            return Ok(());
        }

        match &orphan.kind {
            OrphanKind::DeadScope(scope) => self.reap_scope(scope, force),
            OrphanKind::StrayVnet(record) => {
                VnetSetup::cleanup_state(record)?;
                self.vnet_state_store.delete(&record.owner)?;
                Ok(())
            }
            OrphanKind::StrayInterface(iface) => crate::network::ioctl::destroy_interface(iface),
            OrphanKind::StrayDataset(name) => match &self.zfs {
                Some(zfs) => zfs.destroy_jail_dataset(name),
                None => Err(Error::Zfs("ZFS is not enabled".to_string())),
            },
            OrphanKind::StrayDrainAnchor(anchor) => {
                crate::bulkhead::flush_drain_anchor(anchor);
                Ok(())
            }
            OrphanKind::StrayBridge(name) => reap_bridge(name),
        }
    }

    /// Mirrors `stop_jail`'s teardown order: fence, then unbind, then destroy.
    fn reap_scope(&self, scope: &ScopeRecord, force: bool) -> Result<()> {
        let mut residual = Vec::new();

        if let Some(anchor) = &scope.drain_anchor {
            crate::bulkhead::flush_drain_anchor(anchor);
        }

        if scope.rctl_subject.is_some() {
            crate::rctl::remove_limits(&scope.name);
        }

        if scope.has_vnet_record
            && let Some(record) = self.vnet_state_store.get(&scope.name)?
        {
            match VnetSetup::cleanup_state(&record) {
                Ok(()) => {
                    self.vnet_state_store.delete(&scope.name)?;
                }
                Err(e) => {
                    eprintln!("Warning: VNET cleanup for '{}' failed: {}", scope.name, e);
                    residual.push("vnet".to_string());
                }
            }
        }

        let jail_root = self.data_dir.join("jails").join(&scope.name);
        if let Ok(table) = crate::sys::mount_table() {
            let mountpoints = reapable_mounts(&jail_root, &scope.mounts, &table);
            for mountpoint in &mountpoints {
                crate::sys::unmount_quiet(mountpoint);
            }
            if let Ok(table) = crate::sys::mount_table() {
                for mountpoint in reapable_mounts(&jail_root, &scope.mounts, &table) {
                    residual.push(format!("mount {}", mountpoint.display()));
                }
            }
        }

        if let Some(dataset) = &scope.zfs_dataset {
            if scope.ephemeral || force {
                match &self.zfs {
                    Some(zfs) => {
                        if let Err(e) = zfs.destroy_jail_dataset(&scope.name) {
                            eprintln!("Warning: failed to destroy '{dataset}': {e}");
                            residual.push(format!("dataset {dataset}"));
                        }
                    }
                    None => residual.push(format!("dataset {dataset}")),
                }
            } else {
                println!("  Keeping non-ephemeral dataset '{dataset}' (use --force to destroy)");
            }
        }

        let released = self.lease_store.release_owner(&scope.name)?;
        if self.verbose {
            for (network, ip) in &released {
                println!("  Released {ip} from network '{network}'");
            }
        }

        if let Ok(mut bulkhead) = BulkheadManager::from_data_dir(&self.data_dir)
            && let Err(e) = bulkhead.remove_jail_forwards(&scope.name)
        {
            eprintln!(
                "Warning: failed to remove port forwards for '{}': {}",
                scope.name, e
            );
            residual.push("port-forwards".to_string());
        }

        self.health_store.forget(&scope.name);

        let mut record = AuditRecord::new(&scope.name, AuditEvent::Reaped);
        if residual.is_empty() {
            self.audit.record(&record);
            self.scope_store.delete(&scope.name)?;
            if force && let Err(e) = self.audit.delete(&scope.name) {
                eprintln!(
                    "Warning: failed to remove audit log for '{}': {}",
                    scope.name, e
                );
            }
            Ok(())
        } else {
            record = record.with("residual", residual.join(","));
            self.audit.record(&record);
            Err(Error::State(format!(
                "reaped '{}' but {} remain; scope record kept",
                scope.name,
                residual.join(", ")
            )))
        }
    }
}

/// Tagged interfaces that are `if_bridge`s, with their member counts.
fn tagged_bridges() -> Vec<BridgeFacts> {
    crate::sys::list_tagged_interfaces()
        .into_iter()
        .filter_map(|name| {
            let members = Bridge::open(&name).ok()?.members().ok()?;
            Some(BridgeFacts { name, members })
        })
        .collect()
}

/// Destroy a bridge only while it is still tagged and still empty.
fn reap_bridge(name: &str) -> Result<()> {
    if !crate::sys::interface_is_tagged(name) {
        return Err(Error::Network(format!(
            "Bridge '{name}' is not tagged with group '{}'; refusing to destroy it",
            crate::sys::IFACE_GROUP
        )));
    }
    let bridge = Bridge::open(name)?;
    if !bridge.members()?.is_empty() {
        return Err(Error::Network(format!(
            "Bridge '{name}' gained members; leaving it"
        )));
    }
    bridge.destroy()
}

/// Reap only what blackship can prove it owns. Never fails the caller.
pub fn auto_reconcile(config: &BlackshipConfig, verbose: bool) {
    let reaper = match Reaper::new(config, verbose) {
        Ok(reaper) => reaper,
        Err(e) => {
            if verbose {
                eprintln!("Warning: skipping reconcile: {e}");
            }
            return;
        }
    };

    let survey = match reaper.survey() {
        Ok(survey) => survey,
        Err(e) => {
            eprintln!("Warning: reconcile survey failed: {e}");
            return;
        }
    };

    for orphan in classify(&survey) {
        if orphan.requires_force {
            continue;
        }
        println!(
            "Reconciling orphaned {} '{}'",
            orphan.kind_label(),
            orphan.jail
        );
        if let Err(e) = reaper.reap(&orphan, false, false) {
            eprintln!("Warning: {e}");
        }
    }
}

#[cfg(test)]
mod tests;