oxedyne/fe2o3/fe2o3_jdat/tests/bdat_hostile.rs
10.8 KiB, 1 run
created by r1870400018:67476, 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 | //! Hostile BDAT input is refused and never panics. |
| 2 | //! |
| 3 | //! A decoder that reads bytes from a peer meets lengths and nesting a peer chose. These tests |
| 4 | //! build the bytes by hand, from the layouts in `golden.rs`, and require three things of the |
| 5 | //! decoder: |
| 6 | //! |
| 7 | //! - a declared length near `u64::MAX` is refused, where adding it to an offset would overflow; |
| 8 | //! - an `Adec` payload shorter than its own 8-byte exponent is refused, where it was sliced |
| 9 | //! backwards; |
| 10 | //! - an item of a list, map or ordered map is cut at the container's declared payload, so a |
| 11 | //! container cannot read bytes that belong to what follows it and then decode to a value that |
| 12 | //! encodes to other bytes. |
| 13 | //! |
| 14 | //! Each test names the fault it holds, and turns red when that fault is put back. |
| 15 | |
| 16 | use oxedyne_fe2o3_core::prelude::*; |
| 17 | use oxedyne_fe2o3_jdat::{ |
| 18 | note::NoteConfig, |
| 19 | prelude::*, |
| 20 | }; |
| 21 | use oxedyne_fe2o3_num::{ |
| 22 | BigDecimal, |
| 23 | BigInt, |
| 24 | }; |
| 25 | |
| 26 | use std::panic::{ |
| 27 | catch_unwind, |
| 28 | AssertUnwindSafe, |
| 29 | }; |
| 30 | |
| 31 | // Lengths that overflow an offset sum, and some that do not but are far past any buffer. |
| 32 | const HUGE: [u64; 8] = [ |
| 33 | u64::MAX, |
| 34 | u64::MAX - 1, |
| 35 | u64::MAX - 7, |
| 36 | u64::MAX - 8, |
| 37 | u64::MAX - 9, |
| 38 | 1 << 63, |
| 39 | (1 << 63) - 1, |
| 40 | 1 << 40, |
| 41 | ]; |
| 42 | |
| 43 | // The minimal `c64` of a value: a code that says how many bytes follow, then those bytes. |
| 44 | fn c64(v: u64) -> Vec<u8> { |
| 45 | let be = v.to_be_bytes(); |
| 46 | let skip = be.iter().take_while(|b| **b == 0).count(); |
| 47 | let mut out = vec![Dat::C64_CODE_START + (8 - skip) as u8]; |
| 48 | out.extend_from_slice(&be[skip..]); |
| 49 | out |
| 50 | } |
| 51 | |
| 52 | // Decodes under `catch_unwind`, so a panic is a message the test can name and not an abort. |
| 53 | fn decode(buf: &[u8]) -> Result<Outcome<(Dat, usize)>, String> { |
| 54 | match catch_unwind(AssertUnwindSafe(|| Dat::from_bytes(buf))) { |
| 55 | Ok(outcome) => Ok(outcome), |
| 56 | Err(panic) => { |
| 57 | let msg = match panic.downcast_ref::<String>() { |
| 58 | Some(s) => s.clone(), |
| 59 | None => match panic.downcast_ref::<&str>() { |
| 60 | Some(s) => s.to_string(), |
| 61 | None => "a panic with no message".to_string(), |
| 62 | }, |
| 63 | }; |
| 64 | Err(msg) |
| 65 | }, |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | fn refused(what: &str, buf: &[u8]) { |
| 70 | match decode(buf) { |
| 71 | Err(msg) => panic!("{} made the decoder panic: {}", what, msg), |
| 72 | Ok(Ok((d, n))) => panic!("{} was accepted as {:?} using {} bytes.", what, d, n), |
| 73 | Ok(Err(_)) => (), |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | fn accepted(what: &str, buf: &[u8]) -> Dat { |
| 78 | match decode(buf) { |
| 79 | Err(msg) => panic!("{} made the decoder panic: {}", what, msg), |
| 80 | Ok(Err(e)) => panic!("{} was refused: {}", what, e), |
| 81 | Ok(Ok((d, n))) => { |
| 82 | assert_eq!(n, buf.len(), "{} used {} of {} bytes.", what, n, buf.len()); |
| 83 | d |
| 84 | }, |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | fn encode(dat: &Dat) -> Vec<u8> { |
| 89 | match dat.to_bytes(Vec::new()) { |
| 90 | Ok(b) => b, |
| 91 | Err(e) => panic!("A fixture daticle did not encode: {}", e), |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // The encoding of a container with its declared payload length cut by `by` bytes. The bytes |
| 96 | // themselves stay, so the last item or items lie in the buffer but outside the container. |
| 97 | fn shortened(dat: &Dat, by: u8) -> Vec<u8> { |
| 98 | let mut b = encode(dat); |
| 99 | // Every fixture's payload is under 256 bytes, so its length is the code 0x21 and one byte. |
| 100 | assert_eq!(b[1], Dat::C64_CODE_START + 1, "The fixture's length is not one byte."); |
| 101 | assert!(b[2] > by, "The fixture's payload is shorter than the cut."); |
| 102 | b[2] -= by; |
| 103 | b |
| 104 | } |
| 105 | |
| 106 | // A kind whose length is a `c64` straight after its code. |
| 107 | fn huge_c64_length(code: u8, what: &str) { |
| 108 | for v in HUGE { |
| 109 | let mut b = vec![code]; |
| 110 | b.extend(c64(v)); |
| 111 | b.extend_from_slice(&[0u8; 16]); |
| 112 | refused(&fmt!("{} declaring {} bytes", what, v), &b); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | #[test] |
| 117 | fn a_str_length_past_the_maximum_is_refused() { |
| 118 | huge_c64_length(Dat::STR_CODE, "a Str"); |
| 119 | } |
| 120 | |
| 121 | #[test] |
| 122 | fn a_bc64_length_past_the_maximum_is_refused() { |
| 123 | huge_c64_length(Dat::BC64_CODE, "a BC64"); |
| 124 | } |
| 125 | |
| 126 | #[test] |
| 127 | fn an_aint_length_past_the_maximum_is_refused() { |
| 128 | huge_c64_length(Dat::AINT_CODE, "an Aint"); |
| 129 | } |
| 130 | |
| 131 | #[test] |
| 132 | fn an_adec_length_past_the_maximum_is_refused() { |
| 133 | huge_c64_length(Dat::ADEC_CODE, "an Adec"); |
| 134 | } |
| 135 | |
| 136 | #[test] |
| 137 | fn an_abox_note_length_past_the_maximum_is_refused() { |
| 138 | // An annotated box ends in the note's length, so cut the encoding of an empty note, a lone |
| 139 | // zero byte, and put the huge length in its place. |
| 140 | let abox = Dat::ABox(NoteConfig::default(), Box::new(Dat::U8(1)), String::new()); |
| 141 | let mut head = encode(&abox); |
| 142 | assert_eq!(head.pop(), Some(Dat::C64_CODE_START), "The fixture's note is not empty."); |
| 143 | accepted("an annotated box with an empty note", &encode(&abox)); |
| 144 | for v in HUGE { |
| 145 | let mut b = head.clone(); |
| 146 | b.extend(c64(v)); |
| 147 | b.extend_from_slice(&[0u8; 16]); |
| 148 | refused(&fmt!("an annotated box with a note of {} bytes", v), &b); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | #[test] |
| 153 | fn an_ordered_map_length_past_the_maximum_is_refused() { |
| 154 | huge_c64_length(Dat::OMAP_CODE, "an ordered map"); |
| 155 | } |
| 156 | |
| 157 | #[test] |
| 158 | fn a_bu64_length_past_the_maximum_is_refused() { |
| 159 | // The length is a raw u64 and not a c64, and a value of this kind is how the distributed |
| 160 | // Ozone codec carries record bytes. |
| 161 | for v in HUGE { |
| 162 | let mut b = vec![Dat::BU64_CODE]; |
| 163 | b.extend_from_slice(&v.to_be_bytes()); |
| 164 | b.extend_from_slice(&[0u8; 16]); |
| 165 | refused(&fmt!("a BU64 declaring {} bytes", v), &b); |
| 166 | } |
| 167 | accepted("a BU64 of three bytes", &encode(&Dat::BU64(vec![1, 2, 3]))); |
| 168 | } |
| 169 | |
| 170 | #[test] |
| 171 | fn an_adec_payload_under_eight_bytes_is_refused() { |
| 172 | // The payload is the number's bytes, then an 8-byte exponent, so under eight cannot hold it. |
| 173 | for v in 1..8usize { |
| 174 | let mut b = vec![Dat::ADEC_CODE]; |
| 175 | b.extend(c64(v as u64)); |
| 176 | b.extend(vec![1u8; v]); |
| 177 | refused(&fmt!("an Adec payload of {} bytes", v), &b); |
| 178 | b.extend_from_slice(&[0u8; 16]); |
| 179 | refused(&fmt!("an Adec payload of {} bytes with bytes after it", v), &b); |
| 180 | } |
| 181 | // Eight bytes is a zero number with an exponent, and a longer payload still decodes. |
| 182 | let mut ok = vec![Dat::ADEC_CODE]; |
| 183 | ok.extend(c64(8)); |
| 184 | ok.extend_from_slice(&[0u8; 8]); |
| 185 | accepted("an Adec payload of exactly eight bytes", &ok); |
| 186 | let long = Dat::Adec(BigDecimal::new(BigInt::from(123456789u64), 3)); |
| 187 | assert_eq!(accepted("an Adec with a number and an exponent", &encode(&long)), long); |
| 188 | } |
| 189 | |
| 190 | #[test] |
| 191 | fn an_adec_in_a_list_reads_only_its_own_payload() { |
| 192 | // An Adec followed by another item: its exponent is the last 8 bytes of its own payload, not |
| 193 | // of the buffer. |
| 194 | let list = Dat::List(vec![ |
| 195 | Dat::Adec(BigDecimal::new(BigInt::from(5u8), 2)), |
| 196 | Dat::U8(7), |
| 197 | ]); |
| 198 | assert_eq!(accepted("a list holding an Adec then a U8", &encode(&list)), list); |
| 199 | } |
| 200 | |
| 201 | #[test] |
| 202 | fn a_list_item_cannot_read_past_its_list() { |
| 203 | let fixtures = [ |
| 204 | (Dat::List(vec![Dat::U16(1)]), 1), // a fixed width integer |
| 205 | (Dat::List(vec![Dat::U8(1), Dat::Str("abc".to_string())]), 1), // a string |
| 206 | (Dat::List(vec![Dat::List(vec![Dat::U8(1), Dat::U8(2)])]), 1), // a list in the list |
| 207 | (Dat::List(vec![Dat::BU64(vec![9, 9, 9])]), 1), // a byte string |
| 208 | ]; |
| 209 | for (dat, by) in fixtures { |
| 210 | assert_eq!(accepted("a list of the right length", &encode(&dat)), dat); |
| 211 | refused(&fmt!("{:?} with its length cut by {}", dat, by), &shortened(&dat, by)); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | #[test] |
| 216 | fn a_map_value_cannot_read_past_its_map() { |
| 217 | let fixtures = [ |
| 218 | mapdat!{ "k" => 7u16 }, |
| 219 | mapdat!{ "k" => "value" }, |
| 220 | mapdat!{ "a" => 1u8, "k" => Dat::List(vec![Dat::U8(1)]) }, |
| 221 | ]; |
| 222 | for dat in fixtures { |
| 223 | assert_eq!(accepted("a map of the right length", &encode(&dat)), dat); |
| 224 | refused(&fmt!("{:?} with its length cut by 1", dat), &shortened(&dat, 1)); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | #[test] |
| 229 | fn a_map_key_cannot_read_past_its_map() { |
| 230 | // A key cut off by the container leaves no room for its value, which the decoder refuses |
| 231 | // either way, so this holds the refusal and not the bound. |
| 232 | let dat = Dat::Map(DaticleMap::new()); |
| 233 | assert_eq!(accepted("an empty map", &encode(&dat)), dat); |
| 234 | let dat = mapdat!{ 7u16 => 1u8 }; |
| 235 | assert_eq!(accepted("a map of the right length", &encode(&dat)), dat); |
| 236 | for by in 1..=4u8 { |
| 237 | refused(&fmt!("a map with its length cut by {}", by), &shortened(&dat, by)); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | #[test] |
| 242 | fn an_ordered_map_value_cannot_read_past_its_map() { |
| 243 | let fixtures = [ |
| 244 | omapdat!{ "k" => 7u16 }, |
| 245 | omapdat!{ "k" => "value" }, |
| 246 | omapdat!{ "a" => 1u8, "k" => Dat::List(vec![Dat::U8(1)]) }, |
| 247 | ]; |
| 248 | for dat in fixtures { |
| 249 | assert_eq!(accepted("an ordered map of the right length", &encode(&dat)), dat); |
| 250 | refused(&fmt!("{:?} with its length cut by 1", dat), &shortened(&dat, 1)); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | #[test] |
| 255 | fn an_ordered_map_key_cannot_read_past_its_map() { |
| 256 | let dat = omapdat!{ 7u16 => 1u8 }; |
| 257 | assert_eq!(accepted("an ordered map of the right length", &encode(&dat)), dat); |
| 258 | for by in 1..=4u8 { |
| 259 | refused(&fmt!("an ordered map with its length cut by {}", by), &shortened(&dat, by)); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | #[test] |
| 264 | fn a_tuple_item_is_bounded_by_the_container_it_sits_in() { |
| 265 | // A tuple declares no length of its own, so it cannot overrun itself, but one inside a list |
| 266 | // is cut at the list's payload like any other item. |
| 267 | let tup = Dat::Tup2(Box::new([Dat::U8(1), Dat::Str("abc".to_string())])); |
| 268 | assert_eq!(accepted("a tuple", &encode(&tup)), tup); |
| 269 | let list = Dat::List(vec![tup]); |
| 270 | assert_eq!(accepted("a list holding a tuple", &encode(&list)), list); |
| 271 | refused("a list holding a tuple with its length cut by 1", &shortened(&list, 1)); |
| 272 | } |
| 273 | |
| 274 | // Valid encodings of every kind the decoder reads with a length, a count or a nest. |
| 275 | fn corpus() -> Vec<Dat> { |
| 276 | vec![ |
| 277 | Dat::List(vec![Dat::U8(1), Dat::Str("hello".to_string()), Dat::U16(7)]), |
| 278 | Dat::List(vec![Dat::List(vec![Dat::I8(-1), Dat::Bool(true)]), Dat::Empty]), |
| 279 | mapdat!{ "k" => 42u8, "key" => "value" }, |
| 280 | omapdat!{ "k" => 42u8, "key" => "value" }, |
| 281 | Dat::Tup2(Box::new([Dat::U8(1), Dat::Str("x".to_string())])), |
| 282 | Dat::Tup3(Box::new([Dat::U8(1), Dat::U16(2), Dat::U32(3)])), |
| 283 | Dat::BC64(vec![1, 2, 3]), |
| 284 | Dat::BU8(vec![1, 2, 3]), |
| 285 | Dat::BU64(vec![4, 5, 6]), |
| 286 | Dat::Aint(BigInt::from(-123456789i64)), |
| 287 | Dat::Adec(BigDecimal::new(BigInt::from(123456789u64), 3)), |
| 288 | Dat::ABox(NoteConfig::default(), Box::new(Dat::U8(1)), "note".to_string()), |
| 289 | Dat::Opt(Box::new(Some(Dat::Str("some".to_string())))), |
| 290 | Dat::Box(Box::new(Dat::List(vec![Dat::U8(1)]))), |
| 291 | ] |
| 292 | } |
| 293 | |
| 294 | #[test] |
| 295 | fn every_one_byte_change_is_refused_or_read_within_bounds() { |
| 296 | for dat in corpus() { |
| 297 | let good = encode(&dat); |
| 298 | for at in 0..good.len() { |
| 299 | for byte in 0..=255u8 { |
| 300 | let mut b = good.clone(); |
| 301 | b[at] = byte; |
| 302 | // With bytes after the encoding, so a longer length has something to read. |
| 303 | for tail in [0usize, 16] { |
| 304 | let mut c = b.clone(); |
| 305 | c.extend(vec![0xa5u8; tail]); |
| 306 | match decode(&c) { |
| 307 | Err(msg) => panic!( |
| 308 | "{:?} with byte {} set to {} and {} bytes after it made the decoder \ |
| 309 | panic: {}", dat, at, byte, tail, msg), |
| 310 | Ok(Ok((_, n))) => assert!( |
| 311 | n <= c.len(), |
| 312 | "{:?} with byte {} set to {} used {} of {} bytes.", |
| 313 | dat, at, byte, n, c.len()), |
| 314 | Ok(Err(_)) => (), |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | #[test] |
| 323 | fn every_prefix_of_a_valid_encoding_is_refused_or_complete() { |
| 324 | for dat in corpus() { |
| 325 | let good = encode(&dat); |
| 326 | for n in 0..good.len() { |
| 327 | if let Ok((_, used)) = match decode(&good[..n]) { |
| 328 | Err(msg) => panic!("{:?} cut to {} bytes made the decoder panic: {}", dat, n, msg), |
| 329 | Ok(r) => r, |
| 330 | } { |
| 331 | assert!(used <= n, "{:?} cut to {} bytes used {}.", dat, n, used); |
| 332 | } |
| 333 | } |
| 334 | accepted(&fmt!("{:?}", dat), &good); |
| 335 | } |
| 336 | } |