blackship 0.4.2

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
//! EVA: hot update of running jails via `jail_set(JAIL_UPDATE)`, no restart.

use std::collections::HashMap;
use std::net::IpAddr;
use std::path::PathBuf;

use crate::error::{Error, Result};
use crate::hooks::{HookContext, HookPhase, HookRunner};
use crate::jail::{ParamValue, jail_getid, jail_update};
use crate::manifest::JailDef;
use crate::rctl::ResourceConfig;

use super::Bridge;
use super::lifecycle::build_jail_params;
use super::plan::PlanStep;

/// How a parameter behaves once the jail is running. `ip4.addr`/`ip6.addr`
/// are kernel-updatable on non-vnet jails but stay immutable here: blackship
/// couples them to the lease store and pf anchors.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ParamClass {
    Selector,
    Immutable,
    Updatable,
}

fn classify_param(key: &str) -> ParamClass {
    use crate::sys::consts::{
        JAIL_PARAM_DEVFS_RULESET, JAIL_PARAM_IP4_ADDR, JAIL_PARAM_IP6_ADDR, JAIL_PARAM_JID,
        JAIL_PARAM_NAME, JAIL_PARAM_PATH, JAIL_PARAM_VNET,
    };
    match key {
        JAIL_PARAM_NAME | JAIL_PARAM_JID => ParamClass::Selector,
        JAIL_PARAM_PATH
        | JAIL_PARAM_VNET
        | JAIL_PARAM_IP4_ADDR
        | JAIL_PARAM_IP6_ADDR
        | JAIL_PARAM_DEVFS_RULESET => ParamClass::Immutable,
        _ => ParamClass::Updatable,
    }
}

fn param_display(value: &ParamValue) -> String {
    match value {
        ParamValue::Int(v) => v.to_string(),
        ParamValue::String(s) => s.clone(),
        ParamValue::Bool(b) => b.to_string(),
        ParamValue::Ipv4(addrs) => addrs
            .iter()
            .map(std::string::ToString::to_string)
            .collect::<Vec<_>>()
            .join(","),
        ParamValue::Ipv6(addrs) => addrs
            .iter()
            .map(std::string::ToString::to_string)
            .collect::<Vec<_>>()
            .join(","),
    }
}

fn param_value_matches(desired: &ParamValue, live: &str) -> bool {
    let live = live.trim();
    match desired {
        ParamValue::Int(v) => {
            live.parse::<i32>() == Ok(*v)
                || matches!(
                    (live, *v),
                    ("disable", libc::JAIL_SYS_DISABLE)
                        | ("new", libc::JAIL_SYS_NEW)
                        | ("inherit", libc::JAIL_SYS_INHERIT)
                )
        }
        ParamValue::String(s) => s == live,
        ParamValue::Bool(b) => match live {
            "true" | "1" => *b,
            "false" | "0" => !*b,
            _ => false,
        },
        ParamValue::Ipv4(addrs) => addr_list_matches(live, addrs),
        ParamValue::Ipv6(addrs) => addr_list_matches(live, addrs),
    }
}

fn addr_list_matches<T: std::str::FromStr + PartialEq>(live: &str, desired: &[T]) -> bool {
    let parsed: Vec<T> = live
        .split([',', ' '])
        .filter(|s| !s.is_empty())
        .filter_map(|s| s.parse().ok())
        .collect();
    parsed.len() == desired.len() && parsed.iter().zip(desired).all(|(a, b)| a == b)
}

#[derive(Debug, Default)]
struct EvaDiff {
    changes: HashMap<String, ParamValue>,
    display: Vec<String>,
    immutable_diffs: Vec<String>,
}

fn diff_params(
    desired: &HashMap<String, ParamValue>,
    live: &HashMap<String, String>,
    unreadable: &[String],
) -> EvaDiff {
    let mut diff = EvaDiff::default();

    let mut keys: Vec<&String> = desired.keys().collect();
    keys.sort();

    for key in keys {
        let value = &desired[key];
        let class = classify_param(key);
        if class == ParamClass::Selector {
            continue;
        }

        let (differs, live_str) = match live.get(key) {
            Some(live_value) => (!param_value_matches(value, live_value), live_value.clone()),
            None if unreadable.contains(key) => (true, "(unverified)".to_string()),
            None => (true, "(unset)".to_string()),
        };

        if !differs {
            continue;
        }

        let change = format!("{}: {} -> {}", key, live_str, param_display(value));
        match class {
            ParamClass::Immutable => diff.immutable_diffs.push(change),
            ParamClass::Updatable => {
                diff.display.push(change);
                diff.changes.insert(key.clone(), value.clone());
            }
            ParamClass::Selector => unreachable!(),
        }
    }

    diff
}

/// jls fails the whole invocation on any key it does not know, so fall
/// back to per-key reads and report the losers as unreadable.
fn read_live_params(full_name: &str, keys: &[&str]) -> (HashMap<String, String>, Vec<String>) {
    if let Ok(map) = jls_json(full_name, keys) {
        (map, Vec::new())
    } else {
        let mut live = HashMap::new();
        let mut unreadable = Vec::new();
        for &key in keys {
            match jls_json(full_name, &[key]) {
                Ok(map) => live.extend(map),
                Err(_) => unreadable.push(key.to_string()),
            }
        }
        (live, unreadable)
    }
}

fn jls_json(full_name: &str, keys: &[&str]) -> Result<HashMap<String, String>> {
    let mut args = vec!["--libxo", "json", "-j", full_name];
    args.extend_from_slice(keys);
    let json = crate::sys::run_json("/usr/sbin/jls", &args)?;

    let entry = json
        .get("jail-information")
        .and_then(|i| i.get("jail"))
        .and_then(|j| j.as_array())
        .and_then(|a| a.first())
        .ok_or_else(|| Error::JailOperation(format!("jls returned no data for '{full_name}'")))?;

    let mut map = HashMap::new();
    for key in keys {
        if let Some(value) = entry.get(*key) {
            let s = match value {
                serde_json::Value::String(s) => s.clone(),
                other => other.to_string(),
            };
            map.insert(key.to_string(), s);
        }
    }
    Ok(map)
}

/// Addresses of `desired`'s family on `iface` inside the jail's vnet, or
/// `None` when they cannot be read (not root, interface gone).
fn vnet_addresses(full_name: &str, iface: &str, desired: IpAddr) -> Option<Vec<IpAddr>> {
    let family = if desired.is_ipv4() { "inet" } else { "inet6" };
    let output = std::process::Command::new("/sbin/ifconfig")
        .args(["-j", full_name, iface, family])
        .output()
        .ok()
        .filter(|o| o.status.success())?;
    Some(
        String::from_utf8_lossy(&output.stdout)
            .lines()
            .filter_map(|line| {
                let mut words = line.split_whitespace();
                (words.next() == Some(family)).then(|| words.next())?
            })
            .filter_map(|addr| addr.split('%').next()?.parse().ok())
            .collect(),
    )
}

/// Everything EVA needs to know about one running jail.
struct EvaPlan<'a> {
    jail_def: &'a JailDef,
    full_name: String,
    jid: i32,
    path: PathBuf,
    static_ip: Option<IpAddr>,
    diff: EvaDiff,
    unreadable: Vec<String>,
    resources: &'a ResourceConfig,
    rctl_rules: Vec<String>,
    /// The kernel already holds exactly these rules; `None` when unreadable.
    rctl_current: Option<bool>,
    cpu_list: Option<String>,
}

impl EvaPlan<'_> {
    fn is_noop(&self) -> bool {
        self.diff.changes.is_empty() && self.rctl_current == Some(true) && self.cpu_list.is_none()
    }

    fn step(&self) -> PlanStep {
        PlanStep::Update {
            jail: self.full_name.clone(),
            jid: self.jid,
            parameter_changes: self.diff.display.clone(),
            restart_required: self.diff.immutable_diffs.clone(),
            unreadable_parameters: {
                let mut unreadable = self.unreadable.clone();
                if self.rctl_current.is_none() {
                    unreadable.push("rctl".to_string());
                }
                unreadable
            },
            rctl_rules: if self.rctl_current == Some(false) {
                self.rctl_rules.clone()
            } else {
                Vec::new()
            },
            rctl_clear: self.rctl_current == Some(false) && self.rctl_rules.is_empty(),
            cpu_list: self.cpu_list.clone(),
            no_changes: self.is_noop(),
        }
    }
}

enum EvaTarget<'a> {
    Skip(PlanStep),
    Ready(Box<EvaPlan<'a>>),
}

impl Bridge {
    fn eva_target(&self, name: &str) -> Result<EvaTarget<'_>> {
        let (service_name, full_name) = self.resolve_jail_names(name)?;
        let skip = |reason| {
            EvaTarget::Skip(PlanStep::Skip {
                jail: full_name.clone(),
                reason,
            })
        };
        let Some(jail_def) = self.config.get_jail(&service_name) else {
            return Ok(skip("not_configured"));
        };
        let Ok(jid) = jail_getid(&full_name) else {
            return Ok(skip("not_running"));
        };

        let is_vnet = jail_def.network.as_ref().is_some_and(|n| n.vnet);
        let static_ip = jail_def
            .network
            .as_ref()
            .and_then(crate::manifest::JailNetworkConfig::static_ip);
        let path = jail_def.effective_path(&self.config.config, &full_name);
        let mut desired = build_jail_params(jail_def, &full_name, is_vnet, static_ip)?;
        desired.insert(
            crate::sys::consts::JAIL_PARAM_PATH.to_string(),
            ParamValue::String(path.display().to_string()),
        );
        let keys: Vec<&str> = desired.keys().map(String::as_str).collect();
        let (live, unreadable) = read_live_params(&full_name, &keys);
        let mut unreadable = unreadable;
        let mut diff = diff_params(&desired, &live, &unreadable);
        if is_vnet && let Some(ip) = static_ip {
            self.diff_vnet_address(&full_name, ip, &mut diff, &mut unreadable);
        }

        let resources = self.effective_resources(&full_name, jail_def);
        let cpu_list =
            crate::rctl::resolve_cpu_list(resources, self.other_cpuset_pins(&service_name));
        Ok(EvaTarget::Ready(Box::new(EvaPlan {
            jail_def,
            rctl_rules: resources.to_rules(&full_name),
            rctl_current: crate::rctl::rules_current(&full_name, resources),
            full_name,
            jid,
            path,
            static_ip,
            diff,
            unreadable,
            resources,
            cpu_list,
        })))
    }

    /// A VNET jail's address lives in its own stack, where jls cannot see it:
    /// read it from the jail's interface and report drift as restart-required.
    fn diff_vnet_address(
        &self,
        full_name: &str,
        desired: IpAddr,
        diff: &mut EvaDiff,
        unreadable: &mut Vec<String>,
    ) {
        let key = match desired {
            IpAddr::V4(_) => crate::sys::consts::JAIL_PARAM_IP4_ADDR,
            IpAddr::V6(_) => crate::sys::consts::JAIL_PARAM_IP6_ADDR,
        };
        let live = self
            .vnet_state_store
            .get(full_name)
            .ok()
            .flatten()
            .and_then(|record| vnet_addresses(full_name, &record.jail_interface, desired));
        match live {
            Some(addrs) if addrs.contains(&desired) => {}
            Some(addrs) => {
                let live = addrs
                    .iter()
                    .map(ToString::to_string)
                    .collect::<Vec<_>>()
                    .join(",");
                let live = if live.is_empty() {
                    "(unset)".into()
                } else {
                    live
                };
                diff.immutable_diffs
                    .push(format!("{key}: {live} -> {desired}"));
            }
            None => unreadable.push(key.to_string()),
        }
    }

    /// What `eva` would do, per target jail.
    pub(crate) fn eva_steps(&self, jail: Option<&str>) -> Result<Vec<PlanStep>> {
        self.jails_for_up(jail)?
            .iter()
            .map(|name| {
                Ok(match self.eva_target(name)? {
                    EvaTarget::Skip(step) => step,
                    EvaTarget::Ready(plan) => plan.step(),
                })
            })
            .collect()
    }

    /// Hot-update running jails to match the manifest (EVA).
    pub fn eva(&self, jail: Option<&str>) -> Result<()> {
        self.eva_inner(jail, false)
    }

    /// Dry run: show what `eva` would change without applying anything.
    pub fn eva_dry_run(&self, jail: Option<&str>) -> Result<()> {
        println!("=== DRY RUN - No changes will be made ===\n");
        self.eva_inner(jail, true)
    }

    fn eva_inner(&self, jail: Option<&str>, dry_run: bool) -> Result<()> {
        let mut failures = Vec::new();
        for name in self.jails_for_up(jail)? {
            let outcome = self.eva_target(&name).and_then(|target| match target {
                EvaTarget::Skip(step) => {
                    step.print();
                    Ok(())
                }
                EvaTarget::Ready(plan) if dry_run => {
                    plan.step().print();
                    Ok(())
                }
                EvaTarget::Ready(plan) => self.apply_eva(&plan),
            });
            if let Err(e) = outcome {
                eprintln!("EVA failed for '{name}': {e}");
                failures.push(name);
            }
        }

        if failures.is_empty() {
            Ok(())
        } else {
            Err(Error::JailOperation(format!(
                "EVA failed for {} jail(s): {}",
                failures.len(),
                failures.join(", ")
            )))
        }
    }

    fn apply_eva(&self, plan: &EvaPlan<'_>) -> Result<()> {
        let EvaPlan {
            full_name,
            jid,
            diff,
            resources,
            cpu_list,
            ..
        } = plan;
        let hook_runner = HookRunner::new(plan.jail_def.hooks.clone()).verbose(self.verbose);
        let mut hook_context = HookContext::new(full_name, &plan.path).with_jid(*jid);
        if let Some(ip) = plan.static_ip {
            hook_context = hook_context.with_ip(ip.to_string());
        }

        hook_runner.execute_phase(HookPhase::PreEva, &hook_context)?;

        for line in &diff.immutable_diffs {
            eprintln!("Warning: '{full_name}': {line} -- requires restart, not applied");
        }

        if !diff.changes.is_empty() {
            jail_update(*jid, &diff.changes)?;
            for line in &diff.display {
                println!("  {full_name}: {line}");
            }
        }

        let rctl_stale = plan.rctl_current != Some(true);
        if rctl_stale {
            crate::rctl::remove_limits(full_name);
        }
        if rctl_stale
            && resources.has_rctl_rules()
            && let Err(e) = crate::rctl::apply_limits(full_name, resources)
        {
            eprintln!(
                "Warning: rctl limits partially applied for '{full_name}': {e} -- re-run eva or restart"
            );
        }

        if let Some(list) = cpu_list {
            match crate::rctl::apply_cpuset(*jid, list) {
                Ok(()) => println!("  {full_name}: pinned to CPUs {list}"),
                Err(e) => eprintln!(
                    "Warning: cpuset re-pin failed for '{full_name}': {e} -- re-run eva or restart"
                ),
            }
        }

        hook_runner.execute_phase(HookPhase::PostEva, &hook_context)?;

        if plan.is_noop() {
            println!("  [OK] {full_name} (no changes)");
        } else {
            println!(
                "  [OK] {full_name} ({} param(s) updated)",
                diff.changes.len()
            );
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests;