Skip to content
Merged
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: 2 additions & 0 deletions changelog.d/+checkpoint-consistency.bugfix.md
Original file line number Diff line number Diff line change
@@ -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.
58 changes: 50 additions & 8 deletions crates/rfc9162-tiles/src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,11 @@ impl<S: TileStore> Writer<S> {

/// 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<Note> {
let body = format!("{}\n", std::process::id());
Expand All @@ -309,19 +312,58 @@ impl<S: TileStore> Writer<S> {
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()));
self.store.put(CHECKPOINT, note.to_text().as_bytes())?;
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<bool> {
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)?;
Expand Down
38 changes: 38 additions & 0 deletions crates/rfc9162-tiles/tests/checkpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32>| {
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();
Expand Down
Loading