Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a caboose EPOC tag used by RoT update_server for rollback protection #33

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions hubedit/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ pub enum Command {
#[clap(short, long)]
version: String,

#[clap(short, long)]
epoch: Option<u32>,

#[clap(short, long)]
force: bool,

Expand Down Expand Up @@ -93,6 +96,8 @@ pub enum Command {
board: String,
/// Git has for the hubris archive
gitc: String,
/// Epoch for rollback protection
epoch: Option<u32>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be optional here, or should it be a bare u32 (with responsibility for deciding on a default value given to the caller)?

},
}

Expand Down Expand Up @@ -129,6 +134,7 @@ fn main() -> Result<()> {
}
Command::WriteCaboose {
version,
epoch,
force,
no_defaults,
} => {
Expand All @@ -143,9 +149,9 @@ fn main() -> Result<()> {
}
}
if no_defaults {
archive.write_version_to_caboose(&version)?;
archive.write_version_to_caboose(&version, epoch)?;
} else {
archive.write_default_caboose(Some(&version))?;
archive.write_default_caboose(Some(&version), epoch)?;
}
archive.overwrite()?;
}
Expand Down Expand Up @@ -202,8 +208,10 @@ fn main() -> Result<()> {
board,
name,
gitc,
epoch,
} => {
let archive = bootleby_to_archive(elf_file, board, name, gitc)?;
let archive =
bootleby_to_archive(elf_file, board, name, gitc, epoch)?;

std::fs::write(&args.archive, archive)?;

Expand Down
2 changes: 1 addition & 1 deletion hubtools/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hubtools"
version = "0.4.6"
version = "0.4.7"
edition = "2021"
rust-version = "1.66"

Expand Down
11 changes: 6 additions & 5 deletions hubtools/src/bootleby.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,7 @@ fn add_image_header(path: PathBuf) -> Result<Vec<u8>, Error> {
let offset = header_offset(&elf)?;
drop(elf);

let header = header::ImageHeader {
magic: 0x64_CE_D6_CA,
total_image_len: len as u32,
};
Comment on lines -75 to -78
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was an intentional choice when this was added to only add the magic and length fields to save space. It feels like a step in the wrong direction to add the full header back.

let header = header::ImageHeader::new(len as usize);

header
.write_to_prefix(&mut f[(offset as usize)..])
Expand All @@ -89,6 +86,7 @@ pub fn bootleby_to_archive(
board: String,
name: String,
gitc: String,
epoc: Option<u32>,
) -> Result<Vec<u8>, Error> {
let f = add_image_header(path)?;

Expand All @@ -103,8 +101,11 @@ pub fn bootleby_to_archive(
name = "{}"
board = "{}"
chip = "lpc55"
epoch = {}
"#,
name, board
name,
board,
epoc.unwrap_or(0u32)
);

archive.add_file("elf/kernel", &f)?;
Expand Down
61 changes: 61 additions & 0 deletions hubtools/src/caboose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ impl Caboose {
self.get_tag(tags::SIGN)
}

pub fn epoch(&self) -> Result<&[u8], CabooseError> {
self.get_tag(tags::EPOC)
}

/// Interpret the `EPOC` value as a u32 if present and well formed.
pub fn epoch_u32(&self) -> Option<u32> {
if let Ok(epoc) = self.epoch() {
if let Ok(epoc_str) = std::str::from_utf8(epoc) {
if let Ok(number) = epoc_str.parse::<u32>() {
return Some(number);
}
}
}
None
}

fn get_tag(&self, tag: [u8; 4]) -> Result<&[u8], CabooseError> {
use tlvc::TlvcReader;
let mut reader = TlvcReader::begin(self.as_slice())
Expand Down Expand Up @@ -85,6 +101,7 @@ pub(crate) mod tags {
pub(crate) const NAME: [u8; 4] = *b"NAME";
pub(crate) const VERS: [u8; 4] = *b"VERS";
pub(crate) const SIGN: [u8; 4] = *b"SIGN";
pub(crate) const EPOC: [u8; 4] = *b"EPOC";
}

#[derive(Debug, Default, Clone, PartialEq, Eq)]
Expand All @@ -94,6 +111,7 @@ pub struct CabooseBuilder {
name: Option<String>,
version: Option<String>,
sign: Option<String>,
epoch: Option<u32>,
}

impl CabooseBuilder {
Expand Down Expand Up @@ -122,6 +140,11 @@ impl CabooseBuilder {
self
}

pub fn epoch(mut self, epoch: u32) -> Self {
self.epoch = Some(epoch);
self
}

pub fn build(self) -> Caboose {
let mut pieces = Vec::new();
for (tag, maybe_value) in [
Expand All @@ -130,6 +153,7 @@ impl CabooseBuilder {
(tags::NAME, self.name),
(tags::VERS, self.version),
(tags::SIGN, self.sign),
(tags::EPOC, self.epoch.map(|e| e.to_string())),
] {
let Some(value) = maybe_value else {
continue;
Expand Down Expand Up @@ -166,6 +190,10 @@ mod tests {
caboose.version(),
Err(CabooseError::MissingTag { tag: tags::VERS })
);
assert_eq!(
caboose.epoch(),
Err(CabooseError::MissingTag { tag: tags::EPOC })
);
}

#[test]
Expand All @@ -184,6 +212,10 @@ mod tests {
caboose.version(),
Err(CabooseError::MissingTag { tag: tags::VERS })
);
assert_eq!(
caboose.epoch(),
Err(CabooseError::MissingTag { tag: tags::EPOC })
);
}

#[test]
Expand All @@ -193,10 +225,39 @@ mod tests {
.board("bar")
.name("fizz")
.version("buzz")
.epoch(0)
.build();
assert_eq!(caboose.git_commit(), Ok("foo".as_bytes()));
assert_eq!(caboose.board(), Ok("bar".as_bytes()));
assert_eq!(caboose.name(), Ok("fizz".as_bytes()));
assert_eq!(caboose.version(), Ok("buzz".as_bytes()));
assert_eq!(caboose.epoch(), Ok("0".as_bytes()));
}

#[test]
fn builder_can_make_caboose_with_zero_epoch() {
let caboose = CabooseBuilder::default().epoch(0).build();
assert_eq!(caboose.epoch(), Ok("0".as_bytes()));
}

#[test]
fn builder_can_make_caboose_with_non_zero_epoch() {
let caboose = CabooseBuilder::default().epoch(1234567890).build();
assert_eq!(caboose.epoch(), Ok("1234567890".as_bytes()));
}

#[test]
fn builder_missing_tag_epoc() {
let caboose = CabooseBuilder::default().build();
assert_eq!(
caboose.epoch(),
Err(CabooseError::MissingTag { tag: tags::EPOC })
);
}

#[test]
fn builder_will_normalize_short_epoch() {
let caboose = CabooseBuilder::default().epoch(1234).build();
assert_eq!(caboose.epoch(), Ok("1234".as_bytes()));
}
}
Loading
Loading