diff --git a/crates/dex/src/dex.rs b/crates/dex/src/dex.rs index e46d97b5b..b01a1b247 100644 --- a/crates/dex/src/dex.rs +++ b/crates/dex/src/dex.rs @@ -148,18 +148,7 @@ pub fn mutf8(r: &Reader<'_>, mut at: usize, max_units: usize) -> (String, usize) if b0 == 0 { break; } - let (u, n) = if b0 < 0x80 { - (u16::from(b0), 1) - } else if b0 & 0xe0 == 0xc0 { - let b1 = r.u8(at + 1).unwrap_or(0); - ((u16::from(b0 & 0x1f) << 6) | u16::from(b1 & 0x3f), 2) - } else if b0 & 0xf0 == 0xe0 { - let b1 = r.u8(at + 1).unwrap_or(0); - let b2 = r.u8(at + 2).unwrap_or(0); - ((u16::from(b0 & 0x0f) << 12) | (u16::from(b1 & 0x3f) << 6) | u16::from(b2 & 0x3f), 3) - } else { - (0xfffd, 1) - }; + let (u, n) = crate::bytes::mutf8_unit(r.data().get(at..).unwrap_or_default()); units.push(u); at = at.saturating_add(n); consumed += n; @@ -173,6 +162,10 @@ pub fn sleb128(r: &Reader<'_>, off: usize) -> Option<(i64, usize)> { let mut shift = 0u32; for i in 0..10usize { let b = r.u8(off.checked_add(i)?)?; + // Only one value bit remains in the tenth byte; the rest must extend its sign. + if i == 9 && !matches!(b, 0x00 | 0x7f) { + return None; + } result |= i64::from(b & 0x7f).checked_shl(shift)?; shift += 7; if b & 0x80 == 0 { diff --git a/crates/dex/src/dex_ops.rs b/crates/dex/src/dex_ops.rs index 139ad77ac..53fbf1d05 100644 --- a/crates/dex/src/dex_ops.rs +++ b/crates/dex/src/dex_ops.rs @@ -384,6 +384,9 @@ fn payload(code: &[u16], pc: usize) -> Option<(Payload, usize)> { } 0x0300 => { let width = *code.get(pc + 1)?; + if !matches!(width, 1 | 2 | 4 | 8) { + return None; + } let count = u32_at(code, pc + 2)?; let bytes = usize::from(width).checked_mul(count as usize)?; let units = bytes.checked_add(1)? / 2 + 4; @@ -513,7 +516,10 @@ pub fn decode_one(code: &[u16], pc: usize) -> Option { insn.index = Some(u32::from(u(1)) | (u32::from(u(2)) << 16)); } F35c | F45cc => { - let count = b4.min(5) as usize; + if b4 > 5 { + return None; + } + let count = b4 as usize; let g = a4; let w = u(2); let all = [u32::from(w & 0xf), u32::from((w >> 4) & 0xf), u32::from((w >> 8) & 0xf), u32::from(w >> 12), g]; @@ -525,7 +531,11 @@ pub fn decode_one(code: &[u16], pc: usize) -> Option { } F3rc | F4rcc => { let first = u32::from(u(2)); - insn.regs = (0..aa).map(|i| first.saturating_add(i)).collect(); + let end = first.checked_add(aa)?; + if end > u32::from(u16::MAX) + 1 { + return None; + } + insn.regs = (first..end).collect(); insn.range = true; insn.index = Some(u32::from(u(1))); if o.fmt == F4rcc {