diff --git a/changelog.d/+checkpoint-consistency.bugfix.md b/changelog.d/+checkpoint-consistency.bugfix.md new file mode 100644 index 0000000..81bae54 --- /dev/null +++ b/changelog.d/+checkpoint-consistency.bugfix.md @@ -0,0 +1,2 @@ +`rfc9162-tiles`: `Writer::checkpoint` signs only a tree that extends the checkpoint already published under its origin: the same root at that size, or a larger tree the tiles prove consistent with it. +A tree of the published size with another root, or a larger one that does not extend it, is `Error::Corrupt`, where it was signed. diff --git a/crates/rfc9162-tiles/src/log.rs b/crates/rfc9162-tiles/src/log.rs index fff03ca..5254019 100644 --- a/crates/rfc9162-tiles/src/log.rs +++ b/crates/rfc9162-tiles/src/log.rs @@ -289,8 +289,11 @@ impl Writer { /// Signs the tree state as a C2SP checkpoint under `origin` and writes /// it at `checkpoint`, holding `.state/publish.lock` meanwhile. A - /// checkpoint already published for a larger size is never replaced, so - /// the signer never signs a checkpoint inconsistent with one it signed. + /// checkpoint already published under `origin` is replaced only by one + /// for a tree that extends it: the same root at its size, or a larger + /// tree the tiles prove consistent with it. A larger published size is + /// [`Error::OutOfRange`], any other tree [`Error::Corrupt`], so the + /// signer never signs a checkpoint inconsistent with one it signed. /// Returns the note. pub fn checkpoint(&self, signer: &Signer, origin: &str) -> Result { let body = format!("{}\n", std::process::id()); @@ -309,12 +312,26 @@ impl Writer { reason: "not UTF-8".to_string(), })?; let published = Checkpoint::parse(&Note::parse(&text)?.text)?; - if published.origin == origin && published.size > self.size() { - return Err(Error::OutOfRange(format!( - "a checkpoint for size {} is published, the log is at {}", - published.size, - self.size() - ))); + if published.origin == origin { + if published.size > self.size() { + return Err(Error::OutOfRange(format!( + "a checkpoint for size {} is published, the log is at {}", + published.size, + self.size() + ))); + } + if !self.extends(published.size, &published.root)? { + return Err(Error::Corrupt { + path: CHECKPOINT.to_string(), + reason: format!( + "the published tree of size {} and root {} is not a prefix of the log's tree of size {} and root {}", + published.size, + rfc9162::hex(&published.root), + self.size(), + rfc9162::hex(&self.root()) + ), + }); + } } } let note = signer.sign(&Checkpoint::new(origin, self.size(), self.root())); @@ -322,6 +339,31 @@ impl Writer { Ok(note) } + /// Whether the tree of `size` leaves with `root`, at most the log's size, + /// is a prefix of the log's tree: the empty tree's root, the log's own + /// root at its size, or a consistency proof from the tiles that checks. + fn extends(&self, size: u64, root: &Hash) -> Result { + if size == 0 { + return Ok(*root == rfc9162::root(&[])); + } + if size == self.size() { + return Ok(*root == self.root()); + } + let reader = TileHashReader::new(&self.store, self.size(), self.root()); + let proof = reader + .consistency_proof(size)? + .ok_or_else(|| Error::OutOfRange(format!("no consistency proof from size {size}")))?; + let width = + |n: u64| usize::try_from(n).map_err(|_| Error::OutOfRange(format!("tree size {n}"))); + Ok(rfc9162::verify_consistency( + width(size)?, + &rfc9162::hex(root), + width(self.size())?, + &rfc9162::hex(&self.root()), + &proof, + )) + } + fn remove_partials(&self, full_path: &str) -> Result<()> { for path in self.store.list(&format!("{full_path}.p"))? { self.store.delete(&path)?; diff --git a/crates/rfc9162-tiles/tests/checkpoint.rs b/crates/rfc9162-tiles/tests/checkpoint.rs index c8103ae..0c2fc98 100644 --- a/crates/rfc9162-tiles/tests/checkpoint.rs +++ b/crates/rfc9162-tiles/tests/checkpoint.rs @@ -224,6 +224,44 @@ fn a_log_publishes_a_checkpoint_a_reader_opens() { shorter.checkpoint(&signer, "example.com/log").unwrap(); } +#[test] +fn a_tree_that_does_not_extend_the_published_checkpoint_is_not_signed() { + let signer = Signer::new("example.com/log", signing_key(7)).unwrap(); + let entries = |tag: u8, range: std::ops::Range| { + range.map(move |i| [&[tag][..], &i.to_be_bytes()].concat()) + }; + let store = MemoryStore::new(); + let mut writer = Writer::open(store.clone(), no_keys()).unwrap(); + writer.checkpoint(&signer, "example.com/log").unwrap(); + writer.append_batch(entries(0, 0..300)).unwrap(); + let published = writer + .checkpoint(&signer, "example.com/log") + .unwrap() + .to_text(); + drop(writer); + + // Other entries in the log's place: neither the tree of the published + // size nor a larger one is signed, and the checkpoint stays. + let rewritten = MemoryStore::new(); + let mut other = Writer::open(rewritten.clone(), no_keys()).unwrap(); + other.append_batch(entries(1, 0..300)).unwrap(); + rewritten.put(CHECKPOINT, published.as_bytes()).unwrap(); + let err = other.checkpoint(&signer, "example.com/log").unwrap_err(); + assert!(matches!(err, Error::Corrupt { .. }), "{err}"); + other.append_batch(entries(1, 300..400)).unwrap(); + let err = other.checkpoint(&signer, "example.com/log").unwrap_err(); + assert!(matches!(err, Error::Corrupt { .. }), "{err}"); + assert_eq!( + rewritten.get(CHECKPOINT).unwrap().unwrap(), + published.as_bytes() + ); + + // The log grown from the published tree is signed. + let mut writer = Writer::open(store.clone(), no_keys()).unwrap(); + writer.append_batch(entries(0, 300..400)).unwrap(); + writer.checkpoint(&signer, "example.com/log").unwrap(); +} + #[test] fn a_cosignature_verifies_over_the_cosignature_lines() { let signer = Signer::new("example.com/log", signing_key(5)).unwrap();