add anti-rollback checking to the signature check routine

one minor issue with this implemnetation is it requires dev
images to have an anti-rollback counter that matches or exceed
the last signed image seen.

This is more of a developr ergonomics issue - this will certainly
lead to some surprise by developers who are trying to take
production devices and "downgrade" them to an old dev tree
that has an old anti-rollback firmware number in it.

However, I think this is navigable and probably the simplicity
of not having an "escape" path for dev-signed imgaes is worth it.
All a developer has to do is to set the anti-rollback version
number to be equal to or greater than what was reported in
the device.
This commit is contained in:
bunnie 2026-01-22 19:48:37 +08:00
parent 23b7bc0142
commit 04dd447745

View file

@ -123,10 +123,44 @@ pub fn validate_image(
return Err(String::from("Partition has invalid function code"));
}
let one_way_counters = OneWayCounter::new();
// It is safe to check the anti-rollback counter before the signature because the
// anti-rollback field is part of the signed data. Thus even if the anti-rollback data
// is forged, it should be caught due to a failing signature check later on.
let claimed_function: FunctionCode =
sig.sealed_data.function_code.try_into().unwrap_or(FunctionCode::Invalid);
let arb_offset = claimed_function.to_anti_rollback_counter();
match arb_offset {
Some(arb) => {
let arb_value = one_way_counters.get(arb).expect("Can't read anti-rollback value");
csprng.as_deref_mut().map(|rng| rng.random_delay());
bollard!(die_no_std, 4);
if arb_value > sig.sealed_data.anti_rollback {
return Err(String::from("Anti-rollback code too old, refusing image"));
}
bollard!(die_no_std, 4);
if arb_value < sig.sealed_data.anti_rollback {
csprng.as_deref_mut().map(|rng| rng.random_delay());
if sig.sealed_data.anti_rollback >= crate::acram::ONEWAY_MAX_VALUE {
return Err(String::from("Proposed anti-rollback value out of range"));
}
// increment anti-rollback counter to match the current value of the signed image
bollard!(die_no_std, 4);
while one_way_counters.get(arb).unwrap() < sig.sealed_data.anti_rollback {
bollard!(die_no_std, 4);
// safety: anti-rollback counter argument is from a set of constants in bao1x_api that
// are pre-validated.
unsafe { one_way_counters.inc(arb).ok() };
}
}
}
_ => return Err(String::from("Invalid anti-rollback code, aborting")),
}
bollard!(die_no_std, 4);
// crate::println!("Signature: {:x?}", sig.signature);
let one_way_counters = OneWayCounter::new();
let mut passing_key: Option<usize> = None;
let mut passing_key2: Option<usize> = None;
csprng.as_deref_mut().map(|rng| rng.random_delay());