From b012adb0f0f64065673bd4dca85d66036663ce12 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 23 Nov 2026 04:33:04 +0100 Subject: [PATCH 1/4] android-res: mark the table partial when ARSC chunks or entries are skipped Every path that drops a package, a type chunk or entries now sets Table::truncated, and entries running past their chunk get a warning instead of being skipped silently. Consumers use truncated to tell a resource that is absent from one that was not read. Signed-off-by: FredApps <153173854+FredApps@users.noreply.github.com> --- crates/android-res/src/arsc.rs | 41 +++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/crates/android-res/src/arsc.rs b/crates/android-res/src/arsc.rs index 79a029625..21c700b14 100644 --- a/crates/android-res/src/arsc.rs +++ b/crates/android-res/src/arsc.rs @@ -303,6 +303,7 @@ pub fn parse(data: &[u8], limits: Limits) -> Result { let mut pos = h; while pos < size { let Some((ct, _ch, cs)) = chunk(&r, pos) else { + table.truncated = true; table.warnings.push(format!("unreadable chunk at offset {pos}")); break; }; @@ -338,12 +339,14 @@ pub fn parse(data: &[u8], limits: Limits) -> Result { fn parse_package(data: &[u8], pos: usize, cs: usize, global: &StringPool, table: &mut Table, limits: Limits) { let Some(data) = pos.checked_add(cs).and_then(|end| data.get(..end)) else { + table.truncated = true; table.warnings.push("package runs past its resource table".into()); return; }; let r = Reader::new(data); let hsize = usize::from(r.u16_le(pos + 2).unwrap_or(0)); if hsize < 284 { + table.truncated = true; table.warnings.push(format!("package at offset {pos}: incomplete header")); return; } @@ -393,12 +396,14 @@ fn parse_package(data: &[u8], pos: usize, cs: usize, global: &StringPool, table: let mut c = pos.saturating_add(hsize.max(8)); while c < end { let Some((ct, ch, sz)) = chunk(&r, c) else { + table.truncated = true; table.warnings.push(format!("package {name}: unreadable chunk at offset {c}")); break; }; // Each child was checked against its package; reads within it must also stop before // the next sibling, including fixed-width library records and bag headers. let Some(child) = data.get(..c.saturating_add(sz)) else { + table.truncated = true; table.warnings.push(format!("package {name}: child chunk runs past its package")); break; }; @@ -457,6 +462,7 @@ fn parse_package(data: &[u8], pos: usize, cs: usize, global: &StringPool, table: } } (RES_TYPE_SPEC | RES_TYPE | RES_LIBRARY, _) => { + table.truncated = true; table.warnings.push(format!("package {name}: incomplete chunk header at offset {c}")); } _ => {} @@ -493,10 +499,12 @@ fn parse_type( let mut slots: Vec<(u32, usize)> = Vec::new(); let table_bytes = if sparse || off16 { count.saturating_mul(if sparse { 4 } else { 2 }) } else { count.saturating_mul(4) }; if c.saturating_add(ch).saturating_add(table_bytes) > end { + table.truncated = true; table.warnings.push(format!("{tname} ({cfg}): entry table runs past its chunk")); return; } if count != 0 && (entries_start < ch.saturating_add(table_bytes) || entries_start > sz) { + table.truncated = true; table.warnings.push(format!("{tname} ({cfg}): invalid entry data start")); return; } @@ -517,15 +525,16 @@ fn parse_type( } } } + let mut skipped = 0usize; for (idx, o) in slots { if table.entries.len() >= limits.max_entries { table.truncated = true; return; } - let Some(e) = c.checked_add(entries_start).and_then(|x| x.checked_add(o)) else { continue }; - if e.saturating_add(8) > end { + let Some(e) = c.checked_add(entries_start).and_then(|x| x.checked_add(o)).filter(|e| e.saturating_add(8) <= end) else { + skipped += 1; continue; - } + }; let esize = usize::from(r.u16_le(e).unwrap_or(0)); let eflags = r.u16_le(e + 2).unwrap_or(0); let id = (p.id << 24) | (u32::from(type_id) << 16) | (idx & 0xffff); @@ -594,6 +603,10 @@ fn parse_type( } table.entries.push(entry); } + if skipped > 0 { + table.truncated = true; + table.warnings.push(format!("{tname} ({cfg}): {skipped} entries run past their chunk")); + } } impl Table { @@ -642,6 +655,28 @@ mod tests { assert_eq!(style.config, "night-xhdpi-v21"); } + #[test] + fn skipped_type_chunks_and_entries_mark_the_table_partial() { + let bytes = fx::resources_arsc(); + // The French type chunk: RES_TYPE, an 84-byte header, and "fr" in its configuration. + let fr = (0..bytes.len() - 30).find(|&i| bytes[i..i + 4] == [0x01, 0x02, 84, 0] && &bytes[i + 28..i + 30] == b"fr").unwrap(); + let corrupt = |at: usize, value: u32| { + let mut b = bytes.clone(); + b[at..at + 4].copy_from_slice(&value.to_le_bytes()); + parse(&b, Limits::default()).unwrap() + }; + for (what, t) in [ + ("entry table past its chunk", corrupt(fr + 12, 1000)), + ("invalid entry data start", corrupt(fr + 16, 0xffff)), + ("an entry past its chunk", corrupt(fr + 84, 0x1000)), + ] { + let french = t.entries.iter().filter(|e| e.locale == "fr").count(); + assert!(french < 2, "{what}: the corruption should drop a French entry"); + assert!(t.truncated, "{what}: dropped entries must mark the table partial"); + assert!(!t.warnings.is_empty(), "{what}: dropped entries must be reported"); + } + } + #[test] fn distinct_locale_scripts_and_variants_keep_separate_keys() { let mut cfg = [0u8; 64]; From f812d2f59ce07aa9dddbd5be6f66a53d2f907fff Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 23 Nov 2026 04:43:36 +0100 Subject: [PATCH 2/4] android-res: report skipped entries when the entry limit stops a type Stop the entry loop with break instead of return, so entries skipped before the limit still get their warning. Signed-off-by: FredApps <153173854+FredApps@users.noreply.github.com> --- crates/android-res/src/arsc.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/android-res/src/arsc.rs b/crates/android-res/src/arsc.rs index 21c700b14..d497e545e 100644 --- a/crates/android-res/src/arsc.rs +++ b/crates/android-res/src/arsc.rs @@ -529,7 +529,7 @@ fn parse_type( for (idx, o) in slots { if table.entries.len() >= limits.max_entries { table.truncated = true; - return; + break; } let Some(e) = c.checked_add(entries_start).and_then(|x| x.checked_add(o)).filter(|e| e.saturating_add(8) <= end) else { skipped += 1; From f6b339e0206704f3bf26fad9a3922cca3f332dfa Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 23 Nov 2026 04:54:08 +0100 Subject: [PATCH 3/4] android-res: mark the table partial when a string pool or value is unreadable A package or global string pool that is outside its payload or fails to parse, and a simple entry whose value runs past its chunk, now set Table::truncated as well, so names and values that were not read are not taken for absent ones. Signed-off-by: FredApps <153173854+FredApps@users.noreply.github.com> --- crates/android-res/src/arsc.rs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/crates/android-res/src/arsc.rs b/crates/android-res/src/arsc.rs index d497e545e..8db61e26c 100644 --- a/crates/android-res/src/arsc.rs +++ b/crates/android-res/src/arsc.rs @@ -317,7 +317,10 @@ pub fn parse(data: &[u8], limits: Limits) -> Result { table.global_strings = p.declared; global = p; } - Err(e) => table.warnings.push(format!("global string pool: {e}")), + Err(e) => { + table.truncated = true; + table.warnings.push(format!("global string pool: {e}")); + } }, RES_PACKAGE => { if table.packages.len() >= MAX_PACKAGES { @@ -368,6 +371,7 @@ fn parse_package(data: &[u8], pos: usize, cs: usize, global: &StringPool, table: return StringPool::default(); } let Some(start) = pos.checked_add(offset).filter(|&start| offset >= hsize && start < end) else { + table.truncated = true; table.warnings.push(format!("package {name}: {label} string pool is outside its payload")); return StringPool::default(); }; @@ -384,6 +388,7 @@ fn parse_package(data: &[u8], pos: usize, cs: usize, global: &StringPool, table: pool } Err(error) => { + table.truncated = true; table.warnings.push(format!("package {name}: {label} string pool: {error}")); StringPool::default() } @@ -598,6 +603,9 @@ fn parse_type( entry.value = Some(v.format(global)); entry.value_type = Some(v.type_name()); entry.raw = Some(v); + } else { + table.truncated = true; + table.warnings.push(format!("{tname} ({cfg}): incomplete value")); } } } @@ -669,11 +677,13 @@ mod tests { ("entry table past its chunk", corrupt(fr + 12, 1000)), ("invalid entry data start", corrupt(fr + 16, 0xffff)), ("an entry past its chunk", corrupt(fr + 84, 0x1000)), + // The second entry's header fits, but its value runs past the chunk. + ("a value past its chunk", corrupt(fr + 88, 24)), ] { - let french = t.entries.iter().filter(|e| e.locale == "fr").count(); - assert!(french < 2, "{what}: the corruption should drop a French entry"); - assert!(t.truncated, "{what}: dropped entries must mark the table partial"); - assert!(!t.warnings.is_empty(), "{what}: dropped entries must be reported"); + let french = t.entries.iter().filter(|e| e.locale == "fr" && e.value.is_some()).count(); + assert!(french < 2, "{what}: the corruption should lose a French value"); + assert!(t.truncated, "{what}: lost content must mark the table partial"); + assert!(!t.warnings.is_empty(), "{what}: lost content must be reported"); } } From a1be794fec9126d32edbe0cbd4d32676f722240a Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 23 Nov 2026 11:07:32 +0700 Subject: [PATCH 4/4] android-res: mark the table partial when library records are omitted Library records past their chunk or beyond the 256-record cap are not read, so the table is partial. Signed-off-by: FredApps <153173854+FredApps@users.noreply.github.com> --- crates/android-res/src/arsc.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/android-res/src/arsc.rs b/crates/android-res/src/arsc.rs index 8db61e26c..c9b9507a0 100644 --- a/crates/android-res/src/arsc.rs +++ b/crates/android-res/src/arsc.rs @@ -459,6 +459,9 @@ fn parse_package(data: &[u8], pos: usize, cs: usize, global: &StringPool, table: if n > available { table.warnings.push(format!("package {name}: library records run past their chunk")); } + if n > available.min(256) { + table.truncated = true; + } for i in 0..n.min(256).min(available) { let e = c + ch + i * 260; if let Some(lid) = r.u32_le(e) {