blackship 0.3.0

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
//! 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::store::{FileLock, NetworkLeaseStore, VnetStateRecord, VnetStateStore};
use crate::network::vnet::VnetSetup;
use crate::scope::{ScopeRecord, ScopeStore};
use crate::zfs::ZfsManager;
use std::collections::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),
}

#[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",
        }
    }
}

/// Facts a classification is made from, gathered by [`survey`].
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>,
}

/// 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,
        });
    }

    for iface in &survey.tagged_ifaces {
        if claimed_ifaces.contains(iface.as_str()) {
            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: "dataset has no scope record and no live jail".to_string(),
            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
}

/// 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,
    }
}

/// 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,
    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)),
            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(_) => {}
            }
        }

        Ok(Survey {
            live,
            scopes: self.scope_store.list()?,
            vnet: self.vnet_state_store.list()?,
            tagged_ifaces: crate::sys::list_tagged_interfaces(),
            datasets: self
                .zfs
                .as_ref()
                .map(super::zfs::ZfsManager::list_jail_datasets)
                .unwrap_or_default(),
            drain_anchors: crate::bulkhead::list_drain_anchors(),
        })
    }

    /// 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(())
            }
        }
    }

    /// 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() {
            for mountpoint in crate::sys::mounted_paths_under(&jail_root, &table) {
                crate::sys::unmount_quiet(&mountpoint);
            }
            if let Ok(table) = crate::sys::mount_table() {
                for mountpoint in crate::sys::mounted_paths_under(&jail_root, &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());
        }

        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(", ")
            )))
        }
    }
}

/// 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;