From d01615ff6e1a46590694f1c8b2578bdb353d99eb Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 30 Jan 2026 20:25:51 +0800 Subject: [PATCH] add handling for anti-rollback code in signed images for out of tree builds, we'll just stick with the default code of '1': if you're doing a build out of tree, it's assumed that you don't care about security (not tying your application to a given OS version opens up an attack surface; presumably if you're doing something security-critical, the extra hundred megabytes to clone the xous-core repo isn't a big deal? but if you're just trying to blink some LEDs you really don't care about the anti-rollback feature). --- tools/src/bin/xous-app-uf2.rs | 30 +++++++++++++++++++++++++++++- tools/src/bin/xous-create-image.rs | 2 +- tools/src/sign_image.rs | 20 ++++++++++++++------ tools/src/swap_writer.rs | 8 +++++++- 4 files changed, 51 insertions(+), 9 deletions(-) diff --git a/tools/src/bin/xous-app-uf2.rs b/tools/src/bin/xous-app-uf2.rs index 3d2d6bc07..00b239e53 100644 --- a/tools/src/bin/xous-app-uf2.rs +++ b/tools/src/bin/xous-app-uf2.rs @@ -15,6 +15,7 @@ use xous_tools::swap_writer::SwapWriter; use xous_tools::tags::inif::IniF; use xous_tools::tags::inis::IniS; use xous_tools::tags::pnam::ProcessNames; +use xous_tools::utils::parse_u32; use xous_tools::xous_arguments::XousArgumentCode; use xous_tools::xous_arguments::XousArguments; @@ -36,6 +37,19 @@ fn main() -> Result<(), Box> { .required(true) .help("List of ELF files to incorporate in the detached app"), ) + .arg( + Arg::with_name("antirollback") + .long("antirollback") + .takes_value(true) + .help("Anti-rollback number. Must be greater than or equal to the current anti-rollback number on the target system.") + // 1 is probably reasonable default value, as for out-of-tree builds I don't see any scenario where for developers + // the anti-rollback counter would be incremented. Security-conscious developers would care to see that the kernel + // version is intimately linked to the app version; if you're preferring to do out of tree builds then you've + // YOLO'd on security, anyways. The place where this might need to be notched up is if someone had a device + // once upon a time used for secure things, and then they decided to give it a new life as a toy, in which case, + // the anti-rollback counter could be incremented and they'd have to increment this to match the current ARB state. + .default_value("1") + ) .arg( Arg::with_name("swap") .long("swap") @@ -48,6 +62,19 @@ fn main() -> Result<(), Box> { let mut pid: u32 = 1; + let anti_rollback = if let Some(arb) = matches.value_of("antirollback") { + parse_u32(arb).map_err(|_| String::from("Antirollback should be a number"))? + } else { + 1 + }; + // set this to a...somewhat conservative value. Hopefully we're not deprecating 500 firmware + // versions ever, but also much less than the wear-out threshold of the counter. This helps + // prevent someone "just poking around" from configuring this value to something harmful + // to the hardware. + if anti_rollback > 500 { + panic!("Antirollback value of {} is too large, refusing to sign", anti_rollback); + } + let mut args = if matches.is_present("swap") { XousArguments::new( 0, @@ -95,7 +122,7 @@ fn main() -> Result<(), Box> { // Create the swap target image and encrypt swap_buffer to it let mut swap = Cursor::new(Vec::new()); - swap_buffer.encrypt_to(&mut swap, &private_key)?; + swap_buffer.encrypt_to(&mut swap, &private_key, Some(anti_rollback as usize))?; // generate a uf2 file let swap_uf2 = "swap.uf2"; @@ -135,6 +162,7 @@ fn main() -> Result<(), Box> { bao1x_api::signatures::SIGBLOCK_LEN, xous_tools::sign_image::Version::Bao1xV1, Some("app"), + Some(anti_rollback as usize), )?; let app_uf2 = "apps.uf2"; diff --git a/tools/src/bin/xous-create-image.rs b/tools/src/bin/xous-create-image.rs index 2ef6004ff..70bfb9cbb 100644 --- a/tools/src/bin/xous-create-image.rs +++ b/tools/src/bin/xous-create-image.rs @@ -508,7 +508,7 @@ fn main() -> Result<(), Box> { { let sf = File::create(swap_filename) .unwrap_or_else(|_| panic!("Couldn't create output file {}", swap_filename)); - swap_buffer.encrypt_to(sf, &swap_pkey).expect("Couldn't flush swap buffer to disk"); + swap_buffer.encrypt_to(sf, &swap_pkey, None).expect("Couldn't flush swap buffer to disk"); } // drop sf, so it closes println!("Swap arguments: {}", sargs); diff --git a/tools/src/sign_image.rs b/tools/src/sign_image.rs index bac9256ef..b9ff5602b 100644 --- a/tools/src/sign_image.rs +++ b/tools/src/sign_image.rs @@ -75,6 +75,7 @@ pub fn sign_image( length: usize, version: Version, function_code: Option<&str>, + anti_rollback_manual: Option, ) -> Result, Box> { let mut dest_file = vec![]; @@ -188,8 +189,6 @@ pub fn sign_image( Ok(dest_file) } Version::Bao1xV1 => { - let anti_rollback = read_anti_rollback("./signing/anti-rollback.hjson")?; - let pkinfo = PrivateKeyInfo::from_der(&private_key.contents).map_err(|e| format!("{}", e))?; // First 2 bytes of the `private_key` are a record specifier and length field. Check they are // correct. @@ -207,9 +206,17 @@ pub fn sign_image( .map_err(|e| format!("{}", e))?; let pubkey = sk.public_key(); */ - let anti_rollback = anti_rollback - .get(function_code.unwrap_or("unspecified")) - .expect("anti-rollback code not specified"); + let anti_rollback = if let Some(code) = anti_rollback_manual { + code as u32 + } else { + let anti_rollback = + read_anti_rollback("./signing/anti-rollback.hjson").inspect_err(|_e| { + println!("anti-rollback.hjson file not present, can't determine anti-rollback code!"); + })?; + *(anti_rollback + .get(function_code.unwrap_or("unspecified")) + .expect("anti-rollback code not specified")) + }; let function_code = match function_code { Some("boot0") => FunctionCode::Boot0, @@ -225,7 +232,7 @@ pub fn sign_image( header.sealed_data.version = version as u32; header.sealed_data.signed_len = (source.len() + SIGBLOCK_LEN - UNSIGNED_LEN) as u32; header.sealed_data.function_code = function_code as u32; - header.sealed_data.anti_rollback = *anti_rollback; + header.sealed_data.anti_rollback = anti_rollback; header.sealed_data.min_semver = minver_bytes; header.sealed_data.semver = semver; @@ -317,6 +324,7 @@ where sector_length, version, function_code, + None, )?; dest_file.write_all(&result)?; Ok(()) diff --git a/tools/src/swap_writer.rs b/tools/src/swap_writer.rs index da6142030..62859ce37 100644 --- a/tools/src/swap_writer.rs +++ b/tools/src/swap_writer.rs @@ -83,7 +83,12 @@ impl SwapWriter { /// Take the swap file and wrap it data structures that facilitate per-device encryption /// after deployment to a user device. - pub fn encrypt_to(&mut self, mut f: T, private_key: &pem::Pem) -> Result + pub fn encrypt_to( + &mut self, + mut f: T, + private_key: &pem::Pem, + anti_rollback_manual: Option, + ) -> Result where T: Write + Seek, { @@ -136,6 +141,7 @@ impl SwapWriter { bao1x_api::signatures::SIGBLOCK_LEN, Version::Bao1xV1, function, + anti_rollback_manual, ) .map_err(|_| std::io::Error::new(std::io::ErrorKind::Other, "Can't sign swap image"))?; // write the header, less space for the signature