|
| 1 | +use anyhow::bail; |
| 2 | +use wasm_metadata::Producers; |
| 3 | +use wasmparser::{Encoding, ExternalKind, Parser, Payload}; |
| 4 | + |
| 5 | +/// Represents the detected likelihood of the allocation bug fixed in |
| 6 | +/// https://github.com/WebAssembly/wasi-libc/pull/377 being present in a Wasm |
| 7 | +/// module. |
| 8 | +#[derive(Debug, PartialEq)] |
| 9 | +pub enum WasiLibc377Bug { |
| 10 | + ProbablySafe, |
| 11 | + ProbablyUnsafe, |
| 12 | + Unknown, |
| 13 | +} |
| 14 | + |
| 15 | +impl WasiLibc377Bug { |
| 16 | + pub fn detect(module: &[u8]) -> anyhow::Result<Self> { |
| 17 | + for payload in Parser::new(0).parse_all(module) { |
| 18 | + match payload? { |
| 19 | + Payload::Version { encoding, .. } if encoding != Encoding::Module => { |
| 20 | + bail!("detection only applicable to modules"); |
| 21 | + } |
| 22 | + Payload::ExportSection(reader) => { |
| 23 | + for export in reader { |
| 24 | + let export = export?; |
| 25 | + if export.kind == ExternalKind::Func && export.name == "cabi_realloc" { |
| 26 | + // `cabi_realloc` is a good signal that this module |
| 27 | + // uses wit-bindgen, making it probably-safe. |
| 28 | + tracing::debug!("Found cabi_realloc export"); |
| 29 | + return Ok(Self::ProbablySafe); |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + Payload::CustomSection(c) if c.name() == "producers" => { |
| 34 | + let producers = Producers::from_bytes(c.data(), c.data_offset())?; |
| 35 | + if let Some(clang_version) = |
| 36 | + producers.get("processed-by").and_then(|f| f.get("clang")) |
| 37 | + { |
| 38 | + tracing::debug!(clang_version, "Parsed producers.processed-by.clang"); |
| 39 | + |
| 40 | + // Clang/LLVM version is a good proxy for wasi-sdk |
| 41 | + // version; the allocation bug was fixed in wasi-sdk-18 |
| 42 | + // and LLVM was updated to 15.0.7 in wasi-sdk-19. |
| 43 | + if let Some((major, minor, patch)) = parse_clang_version(clang_version) { |
| 44 | + return if (major, minor, patch) >= (15, 0, 7) { |
| 45 | + Ok(Self::ProbablySafe) |
| 46 | + } else { |
| 47 | + Ok(Self::ProbablyUnsafe) |
| 48 | + }; |
| 49 | + } else { |
| 50 | + tracing::warn!( |
| 51 | + clang_version, |
| 52 | + "Unexpected producers.processed-by.clang version" |
| 53 | + ); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + _ => (), |
| 58 | + } |
| 59 | + } |
| 60 | + Ok(Self::Unknown) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +fn parse_clang_version(ver: &str) -> Option<(u16, u16, u16)> { |
| 65 | + // Strip optional trailing detail after space |
| 66 | + let ver = ver.split(' ').next().unwrap(); |
| 67 | + let mut parts = ver.split('.'); |
| 68 | + let major = parts.next()?.parse().ok()?; |
| 69 | + let minor = parts.next()?.parse().ok()?; |
| 70 | + let patch = parts.next()?.parse().ok()?; |
| 71 | + Some((major, minor, patch)) |
| 72 | +} |
| 73 | + |
| 74 | +#[cfg(test)] |
| 75 | +mod tests { |
| 76 | + use super::*; |
| 77 | + |
| 78 | + #[test] |
| 79 | + fn wasi_libc_377_detect() { |
| 80 | + use WasiLibc377Bug::*; |
| 81 | + for (wasm, expected) in [ |
| 82 | + (r#"(module)"#, Unknown), |
| 83 | + ( |
| 84 | + r#"(module (func (export "cabi_realloc") (unreachable)))"#, |
| 85 | + ProbablySafe, |
| 86 | + ), |
| 87 | + ( |
| 88 | + r#"(module (func (export "some_other_function") (unreachable)))"#, |
| 89 | + Unknown, |
| 90 | + ), |
| 91 | + ( |
| 92 | + r#"(module (@producers (processed-by "clang" "16.0.0 extra-stuff")))"#, |
| 93 | + ProbablySafe, |
| 94 | + ), |
| 95 | + ( |
| 96 | + r#"(module (@producers (processed-by "clang" "15.0.7")))"#, |
| 97 | + ProbablySafe, |
| 98 | + ), |
| 99 | + ( |
| 100 | + r#"(module (@producers (processed-by "clang" "15.0.6")))"#, |
| 101 | + ProbablyUnsafe, |
| 102 | + ), |
| 103 | + ( |
| 104 | + r#"(module (@producers (processed-by "clang" "14.0.0")))"#, |
| 105 | + ProbablyUnsafe, |
| 106 | + ), |
| 107 | + ( |
| 108 | + r#"(module (@producers (processed-by "clang" "a.b.c")))"#, |
| 109 | + Unknown, |
| 110 | + ), |
| 111 | + ] { |
| 112 | + eprintln!("WAT: {wasm}"); |
| 113 | + let module = wat::parse_str(wasm).unwrap(); |
| 114 | + let detected = WasiLibc377Bug::detect(&module).unwrap(); |
| 115 | + assert_eq!(detected, expected); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments