LoroDoc::import can panic on crafted input

Aperta
#1,293 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
30/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
rust
Ambito
data, security

Direzione di ricerca

The faults land in loro-internal/src/diff_calc.rs (the assert_eq! after style_for_end_anchor, around line 1788) and in oplog.rs's get_greatest_timestamp unwrap, with sync.rs turning the first panic into a poisoned lock. Start by running the issue's reproduction against the pinned rev to confirm cases 2 and 3, then trace where import decodes op lengths and the text insert length. Done means all three cases return a DecodeError, the document is left unchanged, and dropping it does not panic.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Hi folks,

As part of a project I'm working on, I've been running a fuzzer and happened upon a few cases where a specially crafted input to LoroDoc::import can result in a panic - potentially crashing the app/server within which loro is used.

Specifically, LoroDoc::import trusts everything behind the header checksum. If an export is damaged/manipulated and its checksum still matches, import does not answer Err. Depending on the byte, it either:

  • panics inside Loro, which poisons the document's locks, so every later call on that LoroDoc
    panics too, including its Drop
  • answers Ok and takes in a history that contradicts itself, after which the document's own
    export writes bytes that import refuses with DecodeDataCorruptionError.

I would generally expecte that bytes which do not decode to a consistent history are refused with an error
(LoroError::DecodeError or similar), and the document is left as it was.

How it is triggered

Take any valid export, change one byte in the body, and rewrite the checksum at bytes 16..20. Three
single-byte examples on a 107-byte export of insert(0, "Hello world") followed by
mark(0..5, "bold", true). Its three ops (insert, style start, style end) have lengths 11, 1, 1.

# Byte changed What the history then says 1.17.1 main at 9846547
1 Value kind of the mark op: MarkStart (12) to Null (0) The style start reads as a second style end, so a style ends that never started import panics, diff_calc.rs:1161, unreachable!() Refused with DecodeError (fixed by #1261)
2 Op lengths 11, 1, 1 become 4, 4, 1 The insert of 11 characters holds counters 0..4 and the style start 4..8. The style end at 8 does follow a style start, which is why the #1261 check passes, but the start's counter is 4 and not 7 import panics, diff_calc.rs:1718 import panics, diff_calc.rs:1788, assert_eq!(style_op.cnt, op.id_start().counter - 1)
3 Op lengths 11, 1, 1 become 3, 1, 1 The insert of 11 characters holds counters 0..3, so the ops after it overlap its text import answers Ok for counters 0..5. export(all_updates) is then refused by import, and export(snapshot) panics at oplog.rs:1571 The same, oplog.rs:1798 (get_change_at(id).unwrap() in get_greatest_timestamp)

Cases 2 and 3 share a cause: a text insert whose length column disagrees with the number of characters
it carries is accepted. Checking that on decode would refuse both.

Fuzzing larger documents on 1.17.1 found more of the same. Of 42,000 valid small updates with 1 or 2
random bytes changed and the checksum rewritten, 33 panicked on import, 14 left a document whose export
could not be read back, and 11 left a document whose export read back with different contents
(get_deep_value() and oplog_vv() differ, with no error anywhere). Seven of those inputs were rerun on
main: one panic is now refused by #1261, and the other six behave as before (two panic at
diff_calc.rs:1788, three cannot be saved, one diverges). They are available on request.

Reproduction

Self-contained: it builds the valid export through the public API, damages it in code, and imports it
into a fresh LoroDoc. The peer id is fixed, so the bytes and offsets are the same on every run. The
result is the same in debug and release (rustc 1.97.0, Linux x86_64).

Cargo.toml

[package]
name = "loro-damaged-import"
version = "0.1.0"
edition = "2021"
 
[dependencies]
loro = { git = "https://github.com/loro-dev/loro", rev = "9846547" }
# loro = "=1.17.1"
xxhash-rust = { version = "0.8", features = ["xxh32"] }

src/main.rs

use loro::{ExportMode, LoroDoc};
use std::panic::{catch_unwind, AssertUnwindSafe};
 
/// A valid export: one peer types "Hello world" and makes "Hello" bold.
/// Peer id and timestamps are fixed, so these 107 bytes are the same on every run.
fn valid_export() -> Vec<u8> {
    let doc = LoroDoc::new();
    doc.set_peer_id(1).unwrap();
    let text = doc.get_text("text");
    text.insert(0, "Hello world").unwrap();
    text.mark(0..5, "bold", true).unwrap();
    doc.commit();
    doc.export(ExportMode::all_updates()).unwrap()
}
 
/// Changes one byte of the body, then rewrites the header checksum to match
/// (xxh32 of bytes 20.., seed "LORO", stored little-endian at 16..20).
fn damage(mut bytes: Vec<u8>, offset: usize, from: u8, to: u8) -> Vec<u8> {
    assert_eq!(bytes[offset], from, "the export format is not the one this was written for");
    bytes[offset] = to;
    let sum = xxhash_rust::xxh32::xxh32(&bytes[20..], u32::from_le_bytes(*b"LORO"));
    bytes[16..20].copy_from_slice(&sum.to_le_bytes());
    bytes
}
 
fn main() {
    let valid = valid_export();
    let cases = [
        // Control: fixed on main by #1261, panics on 1.17.1.
        ("1. the mark's value kind, MarkStart to Null", 82, 0x0c, 0x00),
        // The insert of 11 characters is given 4 counters, the mark start the next 4.
        ("2. op lengths 11, 1, 1 to 4, 4, 1", 85, 0x01, 0x80),
        // The insert of 11 characters is given 3 counters.
        ("3. op lengths 11, 1, 1 to 3, 1, 1", 86, 0x0b, 0x03),
    ];
    for (name, offset, from, to) in cases {
        println!("\n== {name}");
        let bytes = damage(valid.clone(), offset, from, to);
        let doc = LoroDoc::new();
        match catch_unwind(AssertUnwindSafe(|| doc.import(&bytes))) {
            Err(_) => {
                println!("import: PANICKED");
                // The document's locks are poisoned now. Dropping it panics again.
                std::mem::forget(doc);
            }
            Ok(Err(e)) => println!("import: refused, as expected ({e})"),
            Ok(Ok(status)) => {
                println!("import: Ok, {:?}", status.success);
                let saved = doc.export(ExportMode::all_updates()).unwrap();
                let reread = LoroDoc::new().import(&saved).map(|_| ());
                println!("import of its own export: {reread:?}");
                let snapshot = catch_unwind(AssertUnwindSafe(|| doc.export(ExportMode::snapshot())));
                println!("snapshot export: {}", if snapshot.is_ok() { "returned" } else { "PANICKED" });
                std::mem::forget(doc);
            }
        }
    }
}
Actual output on main at 9846547 (RUST_BACKTRACE=0 cargo run --release)
== 1. the mark's value kind, MarkStart to Null
import: refused, as expected (Decode error: (Text style end anchor 11@1 is not preceded by a style start anchor in the same text))
 
== 2. op lengths 11, 1, 1 to 4, 4, 1
 
thread 'main' panicked at loro-internal/src/diff_calc.rs:1788:25:
assertion `left == right` failed
  left: 4
 right: 7
 
thread 'main' panicked at loro-internal/src/sync.rs:44:31:
poisoned LoroMutex
import: PANICKED
 
== 3. op lengths 11, 1, 1 to 3, 1, 1
import: Ok, VersionRange({1: (0, 5)})
import of its own export: Err(DecodeDataCorruptionError)
 
thread 'main' panicked at loro-internal/src/oplog.rs:1798:49:
called `Option::unwrap()` on a `None` value
snapshot export: PANICKED

The panic in case 2 comes as a pair: the first is the fault, the second is import renewing its
transaction on the lock the first one poisoned.

Actual output on 1.17.1 ```text == 1. the mark's value kind, MarkStart to Null

thread 'main' panicked at loro-internal-1.17.1/src/diff_calc.rs:1161:17:
internal error: entered unreachable code

thread 'main' panicked at loro-internal-1.17.1/src/sync.rs:34:31:
poisoned LoroMutex
import: PANICKED

== 2. op lengths 11, 1, 1 to 4, 4, 1

thread 'main' panicked at loro-internal-1.17.1/src/diff_calc.rs:1718:25:
assertion left == right failed
left: 4
right: 7

thread 'main' panicked at loro-internal-1.17.1/src/sync.rs:34:31:
poisoned LoroMutex
import: PANICKED

== 3. op lengths 11, 1, 1 to 3, 1, 1
import: Ok, VersionRange({1: (0, 5)})
import of its own export: Err(DecodeDataCorruptionError)

thread 'main' panicked at loro-internal-1.17.1/src/oplog.rs:1571:49:
called Option::unwrap() on a None value
snapshot export: PANICKED

 
</details>
 
### Expected output
 
```text
== 1. the mark's value kind, MarkStart to Null
import: refused, as expected (...)
 
== 2. op lengths 11, 1, 1 to 4, 4, 1
import: refused, as expected (...)
 
== 3. op lengths 11, 1, 1 to 3, 1, 1
import: refused, as expected (...)

Possible solutions

In an ideal world, I'd love to not need to worry about this (the panic rate is relatively low even with the checksum re-computed, and the checksum should protect most legitimate imports. That said, for anything which is going to allow arbitrary users to interact with it, I'm loathe to trust that nobody will abuse this.

As far as I can tell, there are three paths forward (and ideally a combination of these three would provide layered defence):

  • On decode, refuse a text insert whose length differs from the characters it carries, alongside the
    op reference checks #1261 added to the import rollback scope.
  • Turn the two assert_eq! calls after style_for_end_anchor into the DecodeError #1261 returns, or
    have its check also require that the style start is one counter long.
  • If a panic does escape import, leave the document usable, or at least safe to drop (#793).
Lingua principale
Rust
Stelle
6.3k
Fork
205
Merge medio
3g 18h
PR unite (30g)
45

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di loro-dev/loro

Tutte le issue di loro-dev/loro

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.