lanzaboote/rust/tool/src/install.rs

284 lines
9.2 KiB
Rust
Raw Normal View History

2022-11-23 09:26:26 -05:00
use std::fs;
use std::os::unix::prelude::PermissionsExt;
2022-11-25 07:07:04 -05:00
use std::path::{Path, PathBuf};
2022-11-26 08:55:15 -05:00
use std::process::Command;
2022-11-23 09:26:26 -05:00
2022-11-24 07:33:01 -05:00
use anyhow::{Context, Result};
2022-11-26 08:55:15 -05:00
use nix::unistd::sync;
2022-11-23 09:26:26 -05:00
use crate::esp::{EspGenerationPaths, EspPaths};
use crate::gc::Roots;
use crate::generation::{Generation, GenerationLink};
2023-01-01 19:54:57 -05:00
use crate::os_release::OsRelease;
2022-11-23 14:40:01 -05:00
use crate::pe;
2022-11-26 17:19:08 -05:00
use crate::signature::KeyPair;
use crate::utils::SecureTempDirExt;
2022-11-26 08:55:15 -05:00
pub struct Installer {
gc_roots: Roots,
2022-11-26 08:55:15 -05:00
lanzaboote_stub: PathBuf,
2022-11-26 17:19:08 -05:00
key_pair: KeyPair,
configuration_limit: usize,
esp_paths: EspPaths,
generation_links: Vec<PathBuf>,
2022-11-26 08:55:15 -05:00
}
impl Installer {
pub fn new(
lanzaboote_stub: PathBuf,
2022-11-26 17:19:08 -05:00
key_pair: KeyPair,
configuration_limit: usize,
esp: PathBuf,
generation_links: Vec<PathBuf>,
2022-11-26 08:55:15 -05:00
) -> Self {
let mut gc_roots = Roots::new();
let esp_paths = EspPaths::new(esp);
gc_roots.extend(esp_paths.to_iter());
2022-11-26 08:55:15 -05:00
Self {
gc_roots,
2022-11-26 08:55:15 -05:00
lanzaboote_stub,
2022-11-26 17:19:08 -05:00
key_pair,
configuration_limit,
esp_paths,
generation_links,
2022-11-26 08:55:15 -05:00
}
2022-11-25 19:50:51 -05:00
}
pub fn install(&mut self) -> Result<()> {
let mut links = self
.generation_links
.iter()
.map(GenerationLink::from_path)
.collect::<Result<Vec<GenerationLink>>>()?;
// A configuration limit of 0 means there is no limit.
if self.configuration_limit > 0 {
// Sort the links by version.
links.sort_by_key(|l| l.version);
// Only install the number of generations configured.
links = links
.into_iter()
.rev()
.take(self.configuration_limit)
.collect()
};
self.install_links(links)?;
// Only collect garbage in these two directories. This way, no files that do not belong to
// the NixOS installation are deleted. Lanzatool takes full control over the esp/EFI/nixos
// directory and deletes ALL files that it doesn't know about. Dual- or multiboot setups
// that need files in this directory will NOT work.
self.gc_roots.collect_garbage(&self.esp_paths.nixos)?;
// The esp/EFI/Linux directory is assumed to be potentially shared with other distros.
// Thus, only files that start with "nixos-" are garbage collected (i.e. potentially
// deleted).
self.gc_roots
.collect_garbage_with_filter(&self.esp_paths.linux, |p| {
p.file_name()
.and_then(|n| n.to_str())
.map_or(false, |n| n.starts_with("nixos-"))
})?;
2022-11-25 07:07:04 -05:00
Ok(())
}
fn install_links(&mut self, links: Vec<GenerationLink>) -> Result<()> {
for link in links {
let generation_result = Generation::from_link(&link)
.with_context(|| format!("Failed to build generation from link: {link:?}"));
// Ignore failing to read a generation so that old malformed generations do not stop
// lanzatool from working.
let generation = match generation_result {
Ok(generation) => generation,
Err(e) => {
println!("Malformed generation: {:?}", e);
continue;
}
};
2022-12-28 17:51:51 -05:00
2022-11-26 08:55:15 -05:00
println!("Installing generation {generation}");
2022-11-25 07:07:04 -05:00
2022-11-27 05:19:02 -05:00
self.install_generation(&generation)
.context("Failed to install generation")?;
for (name, bootspec) in &generation.spec.bootspec.specialisation {
let specialised_generation = generation.specialise(name, bootspec)?;
2022-11-27 05:19:02 -05:00
println!("Installing specialisation: {name} of generation: {generation}");
self.install_generation(&specialised_generation)
.context("Failed to install specialisation")?;
}
2022-11-26 08:55:15 -05:00
}
Ok(())
2022-11-25 07:07:04 -05:00
}
fn install_generation(&mut self, generation: &Generation) -> Result<()> {
let bootspec = &generation.spec.bootspec;
2022-11-26 08:55:15 -05:00
let esp_gen_paths = EspGenerationPaths::new(&self.esp_paths, generation)?;
self.gc_roots.extend(esp_gen_paths.to_iter());
2022-11-26 08:55:15 -05:00
let kernel_cmdline =
assemble_kernel_cmdline(&bootspec.init, bootspec.kernel_params.clone());
2022-11-26 08:55:15 -05:00
// This tempdir must live for the entire lifetime of the current function.
let tempdir = tempfile::tempdir()?;
2022-11-26 08:55:15 -05:00
2023-01-01 19:54:57 -05:00
let os_release = OsRelease::from_generation(generation)
.context("Failed to build OsRelease from generation.")?;
let os_release_path = tempdir
.write_secure_file("os-release", os_release.to_string().as_bytes())
.context("Failed to write os-release file.")?;
2023-01-01 19:54:57 -05:00
2022-12-03 07:16:46 -05:00
println!("Appending secrets to initrd...");
2022-11-26 08:55:15 -05:00
let initrd_location = tempdir.path().join("initrd");
copy(
bootspec
.initrd
.as_ref()
.context("Lanzaboote does not support missing initrd yet")?,
&initrd_location,
)?;
if let Some(initrd_secrets_script) = &bootspec.initrd_secrets {
2022-11-26 17:19:08 -05:00
append_initrd_secrets(initrd_secrets_script, &initrd_location)?;
2022-11-26 08:55:15 -05:00
}
let systemd_boot = bootspec
.toplevel
.0
.join("systemd/lib/systemd/boot/efi/systemd-bootx64.efi");
2022-11-26 08:55:15 -05:00
[
(&systemd_boot, &self.esp_paths.efi_fallback),
(&systemd_boot, &self.esp_paths.systemd_boot),
(&bootspec.kernel, &esp_gen_paths.kernel),
]
.into_iter()
.try_for_each(|(from, to)| install_signed(&self.key_pair, from, to))?;
// The initrd doesn't need to be signed. Lanzaboote has its
2022-12-03 07:16:46 -05:00
// hash embedded and will refuse loading it when the hash
// mismatches.
install(&initrd_location, &esp_gen_paths.initrd)
.context("Failed to install initrd to ESP")?;
let lanzaboote_image = pe::lanzaboote_image(
&tempdir,
&self.lanzaboote_stub,
2023-01-01 19:54:57 -05:00
&os_release_path,
&kernel_cmdline,
&esp_gen_paths.kernel,
&esp_gen_paths.initrd,
&self.esp_paths.esp,
)
.context("Failed to assemble stub")?;
2022-11-26 08:55:15 -05:00
install_signed(
&self.key_pair,
&lanzaboote_image,
&esp_gen_paths.lanzaboote_image,
)
.context("Failed to install lanzaboote")?;
2022-11-26 08:55:15 -05:00
// Sync files to persistent storage. This may improve the
// chance of a consistent boot directory in case the system
// crashes.
sync();
2022-11-26 08:55:15 -05:00
println!(
"Successfully installed lanzaboote to '{}'",
self.esp_paths.esp.display()
2022-11-26 08:55:15 -05:00
);
Ok(())
}
2022-11-24 07:33:01 -05:00
}
2022-11-23 09:26:26 -05:00
/// Install a PE file. The PE gets signed in the process.
2022-12-03 07:16:46 -05:00
///
/// The file is only signed and copied if it doesn't exist at the destination
fn install_signed(key_pair: &KeyPair, from: &Path, to: &Path) -> Result<()> {
2022-12-03 07:16:46 -05:00
if to.exists() {
println!("{} already exists, skipping...", to.display());
} else {
println!("Signing and installing {}...", to.display());
ensure_parent_dir(to);
key_pair
.sign_and_copy(from, to)
.with_context(|| format!("Failed to copy and sign file from {:?} to {:?}", from, to))?;
}
2022-12-03 07:16:46 -05:00
Ok(())
}
/// Install an arbitrary file
///
/// The file is only copied if it doesn't exist at the destination
fn install(from: &Path, to: &Path) -> Result<()> {
if to.exists() {
println!("{} already exists, skipping...", to.display());
} else {
println!("Installing {}...", to.display());
ensure_parent_dir(to);
copy(from, to)?;
}
Ok(())
}
2022-11-26 08:55:15 -05:00
pub fn append_initrd_secrets(
append_initrd_secrets_path: &Path,
initrd_path: &PathBuf,
) -> Result<()> {
2022-11-25 19:50:51 -05:00
let status = Command::new(append_initrd_secrets_path)
2022-11-26 08:55:15 -05:00
.args(vec![initrd_path])
2022-11-25 19:50:51 -05:00
.status()
.context("Failed to append initrd secrets")?;
if !status.success() {
2022-11-26 08:55:15 -05:00
return Err(anyhow::anyhow!(
"Failed to append initrd secrets with args `{:?}`",
vec![append_initrd_secrets_path, initrd_path]
2022-11-26 17:19:08 -05:00
));
2022-11-25 19:50:51 -05:00
}
Ok(())
}
fn assemble_kernel_cmdline(init: &Path, kernel_params: Vec<String>) -> Vec<String> {
let init_string = String::from(
init.to_str()
.expect("Failed to convert init path to string"),
);
2022-11-25 07:07:04 -05:00
let mut kernel_cmdline: Vec<String> = vec![format!("init={}", init_string)];
kernel_cmdline.extend(kernel_params);
kernel_cmdline
}
2022-11-24 07:33:01 -05:00
fn copy(from: &Path, to: &Path) -> Result<()> {
2022-11-26 09:32:43 -05:00
ensure_parent_dir(to);
2022-11-24 07:33:01 -05:00
fs::copy(from, to)
.with_context(|| format!("Failed to copy from {} to {}", from.display(), to.display()))?;
// Set permission of all files copied to 0o755
let mut perms = fs::metadata(to)
.with_context(|| format!("File {} doesn't have metadata", to.display()))?
.permissions();
perms.set_mode(0o755);
fs::set_permissions(to, perms)
.with_context(|| format!("Failed to set permissions to: {}", to.display()))?;
2022-11-23 09:26:26 -05:00
Ok(())
}
2022-11-26 09:32:43 -05:00
// Ensures the parent directory of an arbitrary path exists
fn ensure_parent_dir(path: &Path) {
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).ok();
}
}