Skip to content

Commit

Permalink
shared: detect when it's infeasible to sign a stub parameter
Browse files Browse the repository at this point in the history
This is relevant for a remote signer who relies on the existence of store paths
remotely, for example.
  • Loading branch information
RaitoBezarius committed Jan 4, 2024
1 parent a53fcec commit 2c46efc
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 1 deletion.
6 changes: 6 additions & 0 deletions rust/tool/shared/src/pe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ impl StubParameters {
self.kernel_cmdline = cmdline.to_vec();
self
}

pub fn all_signables_in_store(&self) -> bool {
self.lanzaboote_store_path.starts_with("/nix/store")
&& self.kernel_store_path.starts_with("/nix/store")
&& self.initrd_store_path.starts_with("/nix/store")
}
}

/// Performs the evil operation
Expand Down
4 changes: 4 additions & 0 deletions rust/tool/shared/src/signature/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ impl LanzabooteSigner for LocalKeyPair {
Ok(std::fs::read(&self.public_key)?)
}

fn can_sign_stub(&self, _stub: &crate::pe::StubParameters) -> bool {
true
}

fn sign_and_copy(&self, from: &Path, to: &Path) -> Result<()> {
let args: Vec<OsString> = vec![
OsString::from("--key"),
Expand Down
1 change: 1 addition & 0 deletions rust/tool/shared/src/signature/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::pe::StubParameters;

pub trait LanzabooteSigner {
fn sign_store_path(&self, store_path: &Path) -> Result<Vec<u8>>;
fn can_sign_stub(&self, stub: &StubParameters) -> bool;
fn build_and_sign_stub(&self, stub: &StubParameters) -> Result<Vec<u8>>;
fn get_public_key(&self) -> Result<Vec<u8>>;

Expand Down
10 changes: 9 additions & 1 deletion rust/tool/shared/src/signature/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::time::Duration;
use crate::pe::StubParameters;

use super::LanzabooteSigner;
use anyhow::{Context, Result};
use anyhow::{bail, Context, Result};
use serde::{Deserialize, Serialize};
use ureq::{Agent, AgentBuilder};
use url::Url;
Expand Down Expand Up @@ -57,6 +57,10 @@ impl RemoteSigningServer {
/// If the remote server agrees on providing that stub
/// It will return it signed.
fn request_signature(&self, stub_parameters: &StubParameters) -> Result<Vec<u8>> {
if !stub_parameters.all_signables_in_store() {
bail!("Signable stub parameters contains non-Nix store paths, the remote server cannot sign that!");
}

let response = self
.client
.post(self.server_url.join("/sign-stub")?.as_str())
Expand Down Expand Up @@ -166,6 +170,10 @@ impl LanzabooteSigner for RemoteSigningServer {
Ok(binary)
}

fn can_sign_stub(&self, stub: &StubParameters) -> bool {
stub.all_signables_in_store()
}

fn build_and_sign_stub(&self, stub: &StubParameters) -> Result<Vec<u8>> {
self.request_signature(stub)
}
Expand Down
6 changes: 6 additions & 0 deletions rust/tool/systemd/src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,12 @@ impl<S: LanzabooteSigner> Installer<S> {
.with_cmdline(&kernel_cmdline)
.with_os_release_contents(os_release_contents.as_bytes());

// TODO: how should we handle those cases?
if !self.signer.can_sign_stub(&parameters) {
log::warn!("Signer is not able to sign this stub, skipping...");
return Ok(());
}

let lanzaboote_image = self
.signer
.build_and_sign_stub(&parameters)
Expand Down

0 comments on commit 2c46efc

Please sign in to comment.