oxedyne/fe2o3/fe2o3_o3db_sync/tests/stored_decode_depth.rs
1.7 KiB, 1 run
created by r3025735233:77629, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! Stored bytes are decoded with a depth limit. |
| 2 | //! |
| 3 | //! The stream walker reads a list by its length prefix without descending, so a list holding a |
| 4 | //! box chain a million deep passes `Dat::load_bytes` at depth 1. The reader then decoded those |
| 5 | //! bytes without limits, descended a level a byte and aborted the process on a stack overflow. |
| 6 | //! One crafted record in a store file was enough to kill whatever opened the store. |
| 7 | |
| 8 | use oxedyne_fe2o3_core::{ |
| 9 | prelude::*, |
| 10 | byte::ToBytes, |
| 11 | }; |
| 12 | use oxedyne_fe2o3_hash::csum::ChecksumScheme; |
| 13 | use oxedyne_fe2o3_jdat::prelude::*; |
| 14 | use oxedyne_fe2o3_o3db_sync::{ |
| 15 | base::alias::ChooseHash, |
| 16 | file::stored::StoredKey, |
| 17 | }; |
| 18 | |
| 19 | use std::io::Cursor; |
| 20 | |
| 21 | // Deep enough to overflow a test thread's stack many times over without a limit. |
| 22 | const HOSTILE_LEVELS: usize = 1 << 20; |
| 23 | |
| 24 | // A list whose one item is `levels` box codes around an empty value. |
| 25 | fn deep_in_list(levels: usize) -> Outcome<Vec<u8>> { |
| 26 | let mut buf = vec![Dat::LIST_CODE]; |
| 27 | buf = res!(Dat::C64((levels + 1) as u64).to_bytes(buf)); |
| 28 | buf.extend(std::iter::repeat(Dat::BOX_CODE).take(levels)); |
| 29 | buf.push(Dat::EMPTY_CODE); |
| 30 | Ok(buf) |
| 31 | } |
| 32 | |
| 33 | #[test] |
| 34 | fn stored_key_with_deep_chunk_index_is_refused() -> Outcome<()> { |
| 35 | let mut rec = ChooseHash::default().to_vec(); |
| 36 | rec = res!(Dat::Str(fmt!("key")).to_bytes(rec)); |
| 37 | rec.extend(res!(deep_in_list(HOSTILE_LEVELS))); |
| 38 | let mut r = Cursor::new(rec); |
| 39 | match StoredKey::<8, u64>::load(&mut r, ChecksumScheme::new_crc32()) { |
| 40 | Err(e) => { |
| 41 | let msg = fmt!("{}", e); |
| 42 | assert!(msg.contains("depth"), "refusal names the depth: {}", msg); |
| 43 | }, |
| 44 | Ok(_) => return Err(err!("A chunk index nested {} deep was accepted.", HOSTILE_LEVELS; |
| 45 | Test, Unexpected)), |
| 46 | } |
| 47 | Ok(()) |
| 48 | } |