Alternate handover batch TOCTOU fix (#397)

* Revert "Correct the prior documented TOCTOU"

This reverts commit d50fe87801.

* Correct the prior documented TOCTOU

d50fe87801 edited the challenge for the Batch to
fix it. This won't produce Batch n+1 until Batch n is successfully published
and verified. It's an alternative strategy able to be reviewed, with a much
smaller impact to scope.
This commit is contained in:
Luke Parker
2023-10-13 12:14:59 -04:00
committed by GitHub
parent 7d0d1dc382
commit f6e8bc3352
9 changed files with 144 additions and 78 deletions

View File

@@ -155,21 +155,14 @@ pub mod pallet {
// verify the signature
let network = batch.batch.network;
let (current_session, prior, current) = keys_for_network::<T>(network)?;
let batch_message = batch_message(&batch.batch);
// Check the prior key first since only a single `Batch` (the last one) will be when prior is
// Some yet prior wasn't the signing key
let valid_by_prior = if let Some(key) = prior {
key.verify(&batch_message(false, &batch.batch), &batch.signature)
} else {
false
};
let valid_by_prior =
if let Some(key) = prior { key.verify(&batch_message, &batch.signature) } else { false };
let valid = valid_by_prior ||
(if let Some(key) = current {
key.verify(
// This `== 0` is valid as either it'll be the first Batch for the first set, or if
// they never had a Batch, the first Batch for the next set
&batch_message((batch.batch.id == 0) || prior.is_some(), &batch.batch),
&batch.signature,
)
key.verify(&batch_message, &batch.signature)
} else {
false
});