
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#1205 · main · 10 min 08 s
1Étoiles
0Forks
0Issues
| 1 | //! Second security review of #404 (`dist/secreview2`): tests that confirm |
| 2 | //! the transport findings on what changed since 4a7f75da (#411 to #417). |
| 3 | //! Those that failed at 13fd8080 pass with their fixes (`dist/hardening2`); |
| 4 | //! the measurement is `#[ignore]`d: run it with `--ignored --nocapture`. |
| 5 | |
| 6 | use std::collections::HashSet; |
| 7 | use std::net::SocketAddr; |
| 8 | use std::time::{Duration, Instant}; |
| 9 | |
| 10 | use super::*; |
| 11 | |
| 12 | /// An address where nothing listens: a dial there is refused at once. |
| 13 | fn nowhere() -> SocketAddr { |
| 14 | "127.0.0.1:1".parse().expect("an address") |
| 15 | } |
| 16 | |
| 17 | /// SR2-1 (major). The discovery budget is global: `Thread::learn` takes a |
| 18 | /// name while fewer than `max_peers` are discovered, from whichever peer |
| 19 | /// lists it, and a name stays while that peer keeps listing it. One |
| 20 | /// authenticated peer that lists `max_peers` names nobody answers at |
| 21 | /// holds every slot for as long as it stays up: no other node is ever |
| 22 | /// discovered. ADR 0072's *Trust* bounds a lying list to delaying the |
| 23 | /// discovery of a node until that node confirms its own address; this |
| 24 | /// prevents the discovery of every node, with no lie to correct. |
| 25 | /// |
| 26 | /// C does not discover (so only A can make A and C meet); X seeds C, so |
| 27 | /// X's lists name C at its own address. Without B, A meets C through X's |
| 28 | /// list. With B's four names first, it did not: now X's list takes the |
| 29 | /// place of one of B's names, B holding more than its share |
| 30 | /// (`Thread::room`). |
| 31 | #[test] |
| 32 | fn secreview2_one_peers_list_cannot_hold_every_discovery_slot() { |
| 33 | let (c, _ec) = start(config("c", "node-c")); |
| 34 | let mut cx = config("x", "node-impostor"); |
| 35 | cx.mesh = true; |
| 36 | cx.seeds = vec![(name("c"), c.local_addr())]; |
| 37 | let (x, ex) = start(cx); |
| 38 | ups(&ex, "X sees C", &["c"]); |
| 39 | for junk in [false, true] { |
| 40 | let mut ca = mesh("a"); |
| 41 | ca.max_peers = 4; |
| 42 | // The fake B sends no heartbeat: it must stay up meanwhile. |
| 43 | ca.node_down_after_ms = 60_000; |
| 44 | let (a, ea) = start(ca); |
| 45 | let _b = junk.then(|| { |
| 46 | let mut b = fake_up_with(&a, &ea, "b", 1, frame::FLAG_PEERS); |
| 47 | let junk = ["j0", "j1", "j2", "j3"]; |
| 48 | let listed: Vec<(&str, SocketAddr)> = junk.iter().map(|n| (*n, nowhere())).collect(); |
| 49 | put(&mut b, frame::SIGNAL, &list(&listed)); |
| 50 | let mut left: HashSet<String> = junk.iter().map(|n| name(n)).collect(); |
| 51 | while !left.is_empty() { |
| 52 | if let Ev::Closed(peer, _) = ea.next("A dials B's names") { |
| 53 | left.remove(&peer); |
| 54 | } |
| 55 | } |
| 56 | b |
| 57 | }); |
| 58 | a.connect(&name("x"), x.local_addr()); |
| 59 | let seen = ea.during(Duration::from_secs(3)); |
| 60 | let met = seen |
| 61 | .iter() |
| 62 | .any(|e| matches!(e, Ev::Up(n) if n.name == name("c"))); |
| 63 | assert!(met, "B's four names listed first: {junk}: A never met C"); |
| 64 | a.stop(); |
| 65 | down(&ex, "X loses A", "a"); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /// SR2-2 (minor). A list of peers replaces the one before it (`first`). |
| 70 | /// Each node that came up or went down queued a whole new list for every |
| 71 | /// peer that discovers, behind what that peer had not read, however many |
| 72 | /// waited there already: a peer that sends heartbeats and reads nothing |
| 73 | /// held one list per up or down for as long as it stayed up. A peer's |
| 74 | /// list now waits in one place, which a newer one takes ([`Conn::list`]). |
| 75 | /// |
| 76 | /// B fills its connection (it reads nothing), then C comes and goes ten |
| 77 | /// times; then B reads everything: one list comes after the messages, |
| 78 | /// the latest, not twenty. |
| 79 | #[test] |
| 80 | fn secreview2_unread_lists_of_peers_do_not_pile_up() { |
| 81 | let mut ca = mesh("a"); |
| 82 | ca.max_queue = 1 << 20; |
| 83 | ca.node_down_after_ms = 60_000; |
| 84 | let (a, ea) = start(ca); |
| 85 | let mut b = fake_up_with(&a, &ea, "b", 1, frame::FLAG_PEERS); |
| 86 | let to_b = Node { |
| 87 | name: name("b"), |
| 88 | incarnation: 1, |
| 89 | }; |
| 90 | // Messages until the socket, TLS and the queue hold all they take. |
| 91 | let payload = vec![0u8; 256 << 10]; |
| 92 | let mut serial = 0; |
| 93 | let mut full_since: Option<Instant> = None; |
| 94 | loop { |
| 95 | let frame = Outbound::Message(message(&to_b, serial, &payload)); |
| 96 | match a.queue(&to_b, frame, None) { |
| 97 | Ok(()) => { |
| 98 | serial += 1; |
| 99 | full_since = None; |
| 100 | } |
| 101 | Err((SendError::Full, _)) => { |
| 102 | let since = *full_since.get_or_insert_with(Instant::now); |
| 103 | if since.elapsed() > Duration::from_millis(500) { |
| 104 | break; |
| 105 | } |
| 106 | std::thread::sleep(Duration::from_millis(10)); |
| 107 | } |
| 108 | Err((e, _)) => panic!("{e:?}"), |
| 109 | } |
| 110 | } |
| 111 | for i in 0..10 { |
| 112 | let c = fake_up_with(&a, &ea, "c", 100 + i, 0); |
| 113 | // Past a heartbeat: each up and each down is a list of its own. |
| 114 | std::thread::sleep(Duration::from_millis(150)); |
| 115 | drop(c); |
| 116 | down(&ea, "A loses C", "c"); |
| 117 | std::thread::sleep(Duration::from_millis(150)); |
| 118 | } |
| 119 | let queued = a.outbox.routes().get(&name("b")).map_or(0, |r| { |
| 120 | r.queue |
| 121 | .iter() |
| 122 | .filter(|e| e.kind() == frame::SIGNAL && e.body.first() == Some(&frame::PEERS)) |
| 123 | .count() |
| 124 | }); |
| 125 | assert_eq!(queued, 0, "lists of peers in B's queue"); |
| 126 | // B reads it all: the messages, and the lists among and after them. |
| 127 | b.sock |
| 128 | .set_read_timeout(Some(Duration::from_millis(1_000))) |
| 129 | .expect("a timeout"); |
| 130 | let (mut messages, mut lists) = (0u64, 0); |
| 131 | // A's heartbeats keep coming: read for a while past the last message. |
| 132 | let mut until = None; |
| 133 | while until.is_none_or(|t| Instant::now() < t) |
| 134 | && let Some((kind, body)) = get(&mut b) |
| 135 | { |
| 136 | if messages == serial { |
| 137 | until.get_or_insert_with(|| Instant::now() + Duration::from_millis(500)); |
| 138 | } |
| 139 | match kind { |
| 140 | frame::MESSAGE => messages += 1, |
| 141 | frame::SIGNAL if messages > 0 && body.first() == Some(&frame::PEERS) => lists += 1, |
| 142 | _ => {} |
| 143 | } |
| 144 | } |
| 145 | eprintln!("secreview2: B read {messages} messages, then {lists} lists of peers"); |
| 146 | assert_eq!(messages, serial, "every message"); |
| 147 | assert!(lists <= 1, "{lists} lists of peers waited for B"); |
| 148 | } |
| 149 | |
| 150 | /// SR2-3 (minor). A peer's list makes A dial each name it does not know, |
| 151 | /// and each failed dial (refused, a TLS name that does not match, a |
| 152 | /// timeout) is a non-routine `closed` labelled by that name. `node.rs` |
| 153 | /// throttled only labels that are not node names (`State::closed`), so |
| 154 | /// every one was a stderr line, and an entry in `failures` (256 at most, |
| 155 | /// then cleared): the peer chooses the names, and with them the lines. |
| 156 | /// The transport now marks such a dial *hearsay* (an address only lists |
| 157 | /// gave), which `node.rs` logs through a throttle and keeps out of |
| 158 | /// `failures` (`node::tests`). |
| 159 | #[test] |
| 160 | fn secreview2_each_listed_name_is_a_logged_failure() { |
| 161 | let mut ca = mesh("a"); |
| 162 | ca.max_peers = 64; |
| 163 | ca.node_down_after_ms = 60_000; |
| 164 | let (a, ea) = start(ca); |
| 165 | let mut b = fake_up_with(&a, &ea, "b", 1, frame::FLAG_PEERS); |
| 166 | let names: Vec<String> = (0..64).map(|i| format!("j{i}")).collect(); |
| 167 | let listed: Vec<(&str, SocketAddr)> = names.iter().map(|n| (n.as_str(), nowhere())).collect(); |
| 168 | put(&mut b, frame::SIGNAL, &list(&listed)); |
| 169 | let seen = ea.during(Duration::from_secs(2)); |
| 170 | let failures: Vec<&String> = seen |
| 171 | .iter() |
| 172 | .filter_map(|e| match e { |
| 173 | Ev::Closed(peer, r) if peer.starts_with("node-j") && !r.routine() => Some(peer), |
| 174 | _ => None, |
| 175 | }) |
| 176 | .collect(); |
| 177 | let hearsay = seen |
| 178 | .iter() |
| 179 | .filter(|e| matches!(e, Ev::Hearsay(peer) if peer.starts_with("node-j"))) |
| 180 | .count(); |
| 181 | let distinct: HashSet<&String> = failures.iter().copied().collect(); |
| 182 | eprintln!( |
| 183 | "secreview2: one list of 64 names: {} failed dials in 2 s (reconnect_ms 50), {hearsay} of them hearsay", |
| 184 | failures.len() |
| 185 | ); |
| 186 | assert_eq!(distinct.len(), 64); |
| 187 | assert!(failures.len() >= 64); |
| 188 | assert_eq!( |
| 189 | hearsay, |
| 190 | failures.len(), |
| 191 | "every failed dial to a listed name" |
| 192 | ); |
| 193 | drop(a); |
| 194 | } |
| 195 | |
| 196 | /// SR2-4 (major). After a node comes up or goes down, `Thread::tell` sent |
| 197 | /// every peer that discovers the whole list, built and encoded for each |
| 198 | /// (it left out the receiver): with `n` such peers, `n` lists of about `n` |
| 199 | /// entries, on the `louise-dist` thread that also sends heartbeats, at |
| 200 | /// most once per heartbeat. Now a round encodes the list once for each |
| 201 | /// frame limit, each peer ignoring its own entry, and shares it; and a |
| 202 | /// round comes at most once per heartbeat for each 256 peers up. B, which |
| 203 | /// gave its own address, so that A lists it, gets the very list C gets. |
| 204 | #[test] |
| 205 | fn secreview2_a_round_of_lists_is_encoded_once() { |
| 206 | let (a, ea) = start(mesh("a")); |
| 207 | let mut b = fake_up_with(&a, &ea, "b", 1, frame::FLAG_PEERS); |
| 208 | let welcome = next_list(&mut b); |
| 209 | assert_eq!(welcome.nodes, [(name("a"), a.local_addr())]); |
| 210 | put(&mut b, frame::SIGNAL, &list(&[("b", nowhere())])); |
| 211 | let mut c = fake_up_with(&a, &ea, "c", 1, frame::FLAG_PEERS); |
| 212 | let to_c = next_list(&mut c); |
| 213 | assert_eq!( |
| 214 | to_c.nodes, |
| 215 | [(name("a"), a.local_addr()), (name("b"), nowhere())] |
| 216 | ); |
| 217 | // C came up: B's next list, the same, its own entry included. |
| 218 | assert_eq!(next_list(&mut b), to_c); |
| 219 | } |
| 220 | |
| 221 | /// SR2-4, measured: one round of lists for `n` peers, as `Thread::tell` |
| 222 | /// makes it now (one encoding, shared), against the earlier one (a list |
| 223 | /// built and encoded for each peer). Each peer still gets the whole list |
| 224 | /// written: those bytes are the price of whole lists (deltas would cut |
| 225 | /// them, a change of the wire format), paid at most once per heartbeat |
| 226 | /// for each 256 peers. |
| 227 | #[test] |
| 228 | #[ignore = "SR2-4: a measurement: run with --ignored --nocapture, best with --release"] |
| 229 | fn secreview2_a_round_of_lists_costs_n_squared() { |
| 230 | for n in [256usize, 1024, 4096] { |
| 231 | let list: Vec<(String, SocketAddr)> = (0..n) |
| 232 | .map(|i| (format!("node-{i}.cluster.example"), nowhere())) |
| 233 | .collect(); |
| 234 | let started = Instant::now(); |
| 235 | let mut bytes = 0; |
| 236 | for receiver in &list { |
| 237 | let theirs: Vec<(String, SocketAddr)> = list |
| 238 | .iter() |
| 239 | .filter(|(m, _)| *m != receiver.0) |
| 240 | .cloned() |
| 241 | .collect(); |
| 242 | let frames = frame::Peers::frames(&theirs, 8 << 20); |
| 243 | bytes += frames.iter().map(Vec::len).sum::<usize>(); |
| 244 | } |
| 245 | let before = started.elapsed(); |
| 246 | let started = Instant::now(); |
| 247 | let shared: std::sync::Arc<[Vec<u8>]> = frame::Peers::frames(&list, 8 << 20).into(); |
| 248 | let each: Vec<std::sync::Arc<[Vec<u8>]>> = list.iter().map(|_| shared.clone()).collect(); |
| 249 | let now = started.elapsed(); |
| 250 | eprintln!( |
| 251 | "secreview2: one round of lists for {n} peers: {before:?} before, {now:?} now \ |
| 252 | ({} KiB held for all; {} MiB to write, as before)", |
| 253 | shared.iter().map(Vec::len).sum::<usize>() >> 10, |
| 254 | bytes >> 20 |
| 255 | ); |
| 256 | drop(each); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | /// SR2-8 (minor). `Peers::decode` refused a frame whose count was past the |
| 261 | /// receiver's `max_peers`, and `listed` a frame that took the list past |
| 262 | /// it: a node keeping more peers than another's `max_peers` (seeds and |
| 263 | /// connected nodes are exempt from the cap, and nodes may be configured |
| 264 | /// differently) had every list dropped there, and discovery through it |
| 265 | /// stopped. The receiver now reads the first `max_peers` names. |
| 266 | #[test] |
| 267 | fn secreview2_a_list_past_max_peers_is_cut_not_dropped() { |
| 268 | let mut ca = mesh("a"); |
| 269 | ca.max_peers = 2; |
| 270 | ca.node_down_after_ms = 60_000; |
| 271 | let (a, ea) = start(ca); |
| 272 | let mut b = fake_up_with(&a, &ea, "b", 1, frame::FLAG_PEERS); |
| 273 | put( |
| 274 | &mut b, |
| 275 | frame::SIGNAL, |
| 276 | &list(&[("j0", nowhere()), ("j1", nowhere()), ("j2", nowhere())]), |
| 277 | ); |
| 278 | let seen = ea.during(Duration::from_secs(1)); |
| 279 | assert!(dialed(&seen, "j0") && dialed(&seen, "j1"), "{seen:?}"); |
| 280 | assert!(!dialed(&seen, "j2"), "{seen:?}"); |
| 281 | assert_eq!(report(&a, "b").dropped, 0); |
| 282 | } |
| 283 | |
| 284 | /// SR2-6 (major). "A third party's list replaces only an unconfirmed |
| 285 | /// address", and the latest list wins: an honest peer sends its list when |
| 286 | /// a node comes up or goes down, a lying one as often as it likes. A node |
| 287 | /// that never dials this one (its discovery is `seeds`, or it does not |
| 288 | /// know this node) can only be confirmed by a dial that comes up, which |
| 289 | /// the liar prevents by listing a wrong address again before each dial. |
| 290 | /// ADR 0072's *Trust* calls this a delay "before that node confirms its |
| 291 | /// own"; for such a node it lasts as long as the liar stays up. |
| 292 | /// |
| 293 | /// C does not discover; X seeds C, so X's lists name C where it is. B |
| 294 | /// lists C at a dead address first: told once, A meets C once X's list |
| 295 | /// comes (the documented delay); told again every 5 ms, it never did. Now |
| 296 | /// A's dials try each lister's address in turn (`Thread::dial`). |
| 297 | #[test] |
| 298 | fn secreview2_a_repeated_lie_does_not_keep_a_node_out() { |
| 299 | let (c, _ec) = start(config("c", "node-c")); |
| 300 | let mut cx = config("x", "node-impostor"); |
| 301 | cx.mesh = true; |
| 302 | cx.seeds = vec![(name("c"), c.local_addr())]; |
| 303 | let (x, ex) = start(cx); |
| 304 | ups(&ex, "X sees C", &["c"]); |
| 305 | for repeated in [false, true] { |
| 306 | let mut ca = mesh("a"); |
| 307 | ca.node_down_after_ms = 60_000; |
| 308 | let (a, ea) = start(ca); |
| 309 | let mut b = fake_up_with(&a, &ea, "b", 1, frame::FLAG_PEERS); |
| 310 | let lie = list(&[("c", nowhere())]); |
| 311 | put(&mut b, frame::SIGNAL, &lie); |
| 312 | a.connect(&name("x"), x.local_addr()); |
| 313 | let until = Instant::now() + Duration::from_secs(5); |
| 314 | let mut met = false; |
| 315 | while !met && Instant::now() < until { |
| 316 | if repeated { |
| 317 | put(&mut b, frame::SIGNAL, &lie); |
| 318 | } |
| 319 | met = ea |
| 320 | .during(Duration::from_millis(5)) |
| 321 | .iter() |
| 322 | .any(|e| matches!(e, Ev::Up(n) if n.name == name("c"))); |
| 323 | } |
| 324 | assert!(met, "B lied every 5 ms: {repeated}: A never met C in 5 s"); |
| 325 | drop(b); |
| 326 | a.stop(); |
| 327 | down(&ex, "X loses A", "a"); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | /// SR2-1's fix, against a lister that hands its names to others' lists: |
| 332 | /// B lists Z, and so does C; B then drops Z for names of its own. Z goes |
| 333 | /// to C's share, not to nobody's, where B's next name would take its |
| 334 | /// place: B, over its share, gets no more, and Z, which C still lists, is |
| 335 | /// still dialed. |
| 336 | #[test] |
| 337 | fn a_dropped_name_another_peer_lists_stays() { |
| 338 | let mut ca = mesh("a"); |
| 339 | ca.max_peers = 3; |
| 340 | ca.node_down_after_ms = 60_000; |
| 341 | let (a, ea) = start(ca); |
| 342 | let mut b = fake_up_with(&a, &ea, "b", 1, frame::FLAG_PEERS); |
| 343 | let mut c = fake_up_with(&a, &ea, "c", 1, frame::FLAG_PEERS); |
| 344 | put(&mut b, frame::SIGNAL, &list(&[("z", nowhere())])); |
| 345 | ea.closed("A dials Z", |_| true); |
| 346 | put(&mut c, frame::SIGNAL, &list(&[("z", nowhere())])); |
| 347 | std::thread::sleep(Duration::from_millis(200)); |
| 348 | put( |
| 349 | &mut b, |
| 350 | frame::SIGNAL, |
| 351 | &list(&[("j0", nowhere()), ("j1", nowhere())]), |
| 352 | ); |
| 353 | std::thread::sleep(Duration::from_millis(200)); |
| 354 | let more = [("j0", nowhere()), ("j1", nowhere()), ("j2", nowhere())]; |
| 355 | put(&mut b, frame::SIGNAL, &list(&more)); |
| 356 | let seen = ea.during(Duration::from_secs(4)); |
| 357 | assert!(dialed(&seen, "z"), "Z forgotten: {seen:?}"); |
| 358 | assert!(!dialed(&seen, "j2"), "B got past its share: {seen:?}"); |
| 359 | drop(a); |
| 360 | } |
| 361 | |
| 362 | /// SR2-6's fix, against a liar that moves its lie around the honest |
| 363 | /// address: dials took the addresses in their sorted order, the next one |
| 364 | /// each time, so a liar that sees each dial (at an address of its own) |
| 365 | /// could list, before the next one, an address that sorts below the honest |
| 366 | /// one, then one above it (any IPv6 address sorts after every IPv4 one), |
| 367 | /// so that the next in turn was always its own. Now the turn goes by |
| 368 | /// lister, not by address: the liar holds its place in it. |
| 369 | /// |
| 370 | /// As in [`secreview2_a_repeated_lie_does_not_keep_a_node_out`], C does |
| 371 | /// not discover and X seeds C. B lists C at 127.0.0.1:1 or at [::1]:1, |
| 372 | /// each time A's dial to C fails, whichever is the next in turn. |
| 373 | #[test] |
| 374 | fn a_lie_moved_around_the_honest_address_does_not_keep_a_node_out() { |
| 375 | let (c, _ec) = start(config("c", "node-c")); |
| 376 | let mut cx = config("x", "node-impostor"); |
| 377 | cx.mesh = true; |
| 378 | cx.seeds = vec![(name("c"), c.local_addr())]; |
| 379 | let (x, ex) = start(cx); |
| 380 | ups(&ex, "X sees C", &["c"]); |
| 381 | let below: SocketAddr = "127.0.0.1:1".parse().expect("an address"); |
| 382 | let above: SocketAddr = "[::1]:1".parse().expect("an address"); |
| 383 | assert!(below < c.local_addr() && c.local_addr() < above); |
| 384 | let mut ca = mesh("a"); |
| 385 | ca.node_down_after_ms = 60_000; |
| 386 | let (a, ea) = start(ca); |
| 387 | let mut b = fake_up_with(&a, &ea, "b", 1, frame::FLAG_PEERS); |
| 388 | put(&mut b, frame::SIGNAL, &list(&[("c", below)])); |
| 389 | a.connect(&name("x"), x.local_addr()); |
| 390 | let until = Instant::now() + Duration::from_secs(5); |
| 391 | let (mut dials, mut met) = (0usize, false); |
| 392 | while !met && Instant::now() < until { |
| 393 | for e in ea.during(Duration::from_millis(5)) { |
| 394 | match e { |
| 395 | Ev::Up(n) if n.name == name("c") => met = true, |
| 396 | Ev::Closed(peer, _) if peer == name("c") => { |
| 397 | dials += 1; |
| 398 | // The next dial takes index `dials % 2` of the sorted |
| 399 | // two: be there. |
| 400 | let lie = if dials % 2 == 0 { below } else { above }; |
| 401 | put(&mut b, frame::SIGNAL, &list(&[("c", lie)])); |
| 402 | } |
| 403 | _ => {} |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | assert!( |
| 408 | met, |
| 409 | "B moved its lie after each of {dials} dials: A never met C in 5 s" |
| 410 | ); |
| 411 | drop(b); |
| 412 | a.stop(); |
| 413 | drop(x); |
| 414 | } |