team-ljyzipelo

공개

ZIP 복구 EOCD 후보의 중앙 목록 경계를 검증함

작성자
시로사키 하나
작성 시각
커밋
1e285b55a4eda4770e1fd46bfa16b954f2a3536d
부모
86cb30a
이 시점의 파일 보기

변경된 파일 3개

+212-3
수정experiments/rust-archive-worker/src/zip_recovery.rs+109-3
@@ -129,6 +129,114 @@ fn charge(
129129 check.metadata_limits(),
130130 )
131131}
132추가:
133추가: fn zip64_bounds(source: &mut File, end: u64) -> Result<Option<(u64, u64)>> {
134추가: if end < 20 {
135추가: return Ok(None);
136추가: }
137추가: let locator_at = end - 20;
138추가: let locator = bytes(source, locator_at, 20, end)?;
139추가: if &locator[..4] != b"PK\x06\x07" || u32at(&locator, 4) != 0 || u32at(&locator, 16) != 1 {
140추가: return Ok(None);
141추가: }
142추가: let record_at = u64::from_le_bytes(locator[8..16].try_into().unwrap());
143추가: if record_at.checked_add(56).is_none_or(|n| n > locator_at) {
144추가: return Ok(None);
145추가: }
146추가: let record = bytes(source, record_at, 56, locator_at)?;
147추가: let length = u64::from_le_bytes(record[4..12].try_into().unwrap());
148추가: if &record[..4] != b"PK\x06\x06"
149추가: || length < 44
150추가: || record_at
151추가: .checked_add(12)
152추가: .and_then(|n| n.checked_add(length))
153추가: != Some(locator_at)
154추가: {
155추가: return Ok(None);
156추가: }
157추가: let size = u64::from_le_bytes(record[40..48].try_into().unwrap());
158추가: let offset = u64::from_le_bytes(record[48..56].try_into().unwrap());
159추가: Ok((offset.checked_add(size) == Some(record_at)).then_some((offset, size)))
160추가: }
161추가:
162추가: fn directory_span(
163추가: source: &mut File,
164추가: mut at: u64,
165추가: end: u64,
166추가: check: &mut impl Monitor,
167추가: ) -> Result<bool> {
168추가: let mut count = 0u32;
169추가: while at < end {
170추가: check.check()?;
171추가: if end - at < 46 {
172추가: return Ok(false);
173추가: }
174추가: let header = bytes(source, at, 46, end)?;
175추가: if &header[..4] != b"PK\x01\x02" {
176추가: return Ok(false);
177추가: }
178추가: let next = add(
179추가: at,
180추가: 46 + u16at(&header, 28) as u64 + u16at(&header, 30) as u64 + u16at(&header, 32) as u64,
181추가: )?;
182추가: if next > end {
183추가: return Ok(false);
184추가: }
185추가: count += 1;
186추가: if count > u16::MAX as u32 {
187추가: return Err(ErrorCode::UnsupportedFeature);
188추가: }
189추가: at = next;
190추가: }
191추가: Ok(true)
192추가: }
193추가:
194추가: fn end_record(
195추가: source: &mut File,
196추가: tail: &[u8],
197추가: tail_at: u64,
198추가: check: &mut impl Monitor,
199추가: ) -> Result<Option<usize>> {
200추가: for end in (0..=tail.len() - 22).rev() {
201추가: if &tail[end..end + 4] != b"PK\x05\x06"
202추가: || end + 22 + u16at(tail, end + 20) as usize != tail.len()
203추가: {
204추가: continue;
205추가: }
206추가: check.check()?;
207추가: let h = &tail[end..];
208추가: let at = tail_at + end as u64;
209추가: let mut offset = u32at(h, 16) as u64;
210추가: let mut size = u32at(h, 12) as u64;
211추가: let zip64 = offset == u32::MAX as u64 || size == u32::MAX as u64;
212추가: if zip64 {
213추가: let Some(bounds) = zip64_bounds(source, at)? else {
214추가: continue;
215추가: };
216추가: (offset, size) = bounds;
217추가: }
218추가: let Some(stop) = offset.checked_add(size).filter(|&n| n <= at) else {
219추가: continue;
220추가: };
221추가: if size < 46 || bytes(source, offset, 4, stop)? != b"PK\x01\x02" {
222추가: continue;
223추가: }
224추가: // 주석 안 후보는 중앙 목록 또는 실제 ZIP64 경계와 이어지지 않으면 제외함.
225추가: if stop != at && !zip64 {
226추가: let adjacent_locator =
227추가: at.checked_sub(20) == Some(stop) && bytes(source, stop, 4, at)? == b"PK\x06\x07";
228추가: if !adjacent_locator && zip64_bounds(source, at)? != Some((offset, size)) {
229추가: continue;
230추가: }
231추가: }
232추가: if !directory_span(source, offset, stop, check)? {
233추가: continue;
234추가: }
235추가: return Ok(Some(end));
236추가: }
237추가: Ok(None)
238추가: }
239추가:
132240fn central(
133241 source: &mut File,
134242 len: u64,
@@ -142,9 +250,7 @@ fn central(
142250 metadata.charge_bytes(len.min(65557) as usize, check.metadata_limits())?;
143251 let tail_at = len.saturating_sub(65557);
144252 let tail = bytes(source, tail_at, (len - tail_at) as usize, len)?;
145삭제: let Some(end) = (0..=tail.len() - 22).rev().find(|&i| {
146삭제: &tail[i..i + 4] == b"PK\x05\x06" && i + 22 + u16at(&tail, i + 20) as usize == tail.len()
147삭제: }) else {
253추가: let Some(end) = end_record(source, &tail, tail_at, check)? else {
148254 return Ok(None);
149255 };
150256 let h = &tail[end..];
수정tests/rust-archive-worker/recovery.rs+68-0
@@ -542,3 +542,71 @@ fn central_comment_locator_bytes_do_not_reject_zip32_recovery() {
542542 }
543543 }
544544}
545추가:
546추가: #[test]
547추가: fn fake_eocd_in_archive_comment_keeps_real_central_directory() {
548추가: let source = include_bytes!("../zip-recovery-fixtures/broken-local-header.zip");
549추가: for fake_kind in 0..7 {
550추가: for locator in [false, true] {
551추가: let mut comment = Vec::new();
552추가: if locator {
553추가: comment.extend(b"PK\x06\x07");
554추가: comment.extend([0; 16]);
555추가: }
556추가: let mut fake = [0u8; 22];
557추가: fake[..4].copy_from_slice(b"PK\x05\x06");
558추가: match fake_kind {
559추가: 0 => fake[4..6].copy_from_slice(&1u16.to_le_bytes()),
560추가: 1 => fake[10..12].copy_from_slice(&u16::MAX.to_le_bytes()),
561추가: 2 => fake[16..20].copy_from_slice(&u32::MAX.to_le_bytes()),
562추가: 3 => fake[12..16].copy_from_slice(&u32::MAX.to_le_bytes()),
563추가: 4 => {
564추가: fake[12..16].copy_from_slice(&46u32.to_le_bytes());
565추가: fake[16..20].copy_from_slice(&39u32.to_le_bytes());
566추가: }
567추가: 5 => {
568추가: fake[12..16].copy_from_slice(&340u32.to_le_bytes());
569추가: fake[16..20].copy_from_slice(&1372u32.to_le_bytes());
570추가: }
571추가: _ => {
572추가: fake[4..6].copy_from_slice(&1u16.to_le_bytes());
573추가: let size = (source.len() + comment.len() - 1372) as u32;
574추가: fake[12..16].copy_from_slice(&size.to_le_bytes());
575추가: fake[16..20].copy_from_slice(&1372u32.to_le_bytes());
576추가: }
577추가: }
578추가: comment.extend(fake);
579추가: let mut input = source.to_vec();
580추가: let end = input.len() - 22;
581추가: input[end + 20..end + 22].copy_from_slice(&(comment.len() as u16).to_le_bytes());
582추가: input.extend(comment);
583추가: let s = Scratch::new();
584추가: fs::write(s.0.join("damaged.zip"), &input).unwrap();
585추가: let events = execute(start(&s));
586추가: let terminal = terminal(&events);
587추가: assert_eq!(terminal.error, ErrorCode::None, "{fake_kind}/{locator}");
588추가: assert_eq!(terminal.outcome, Outcome::Completed);
589추가: assert_eq!(terminal.publication, Publication::Complete);
590추가: let summary = recovery_end(&events);
591추가: assert!(summary.complete());
592추가: assert_eq!(summary.mode, RecoveryMode::CentralDirectory);
593추가: assert_eq!(summary.known_entries, Some(6));
594추가: assert_eq!(
595추가: summary.source_sha256,
596추가: <[u8; 32]>::from(Sha256::digest(&input))
597추가: );
598추가: assert_eq!(fs::read(s.0.join("damaged.zip")).unwrap(), input);
599추가: let checked = Command::new("python3")
600추가: .arg("-B")
601추가: .arg(
602추가: PathBuf::from(env!("CARGO_MANIFEST_DIR"))
603추가: .join("../../tests/zip-recovery-fixtures/verify.py"),
604추가: )
605추가: .args(["--recovered", "broken-local-header.zip"])
606추가: .arg(s.0.join("recovered.zip"))
607추가: .output()
608추가: .unwrap();
609추가: assert!(checked.status.success(), "{checked:?}");
610추가: }
611추가: }
612추가: }
수정tests/rust-archive-worker/recovery_checks.rs+35-0
@@ -226,3 +226,38 @@ fn zip64_locator_after_central_directory_remains_unsupported() {
226226 ));
227227 assert_eq!(std::fs::read(s.path()).unwrap(), input);
228228}
229추가:
230추가: #[test]
231추가: fn structurally_valid_zip64_end_records_are_not_downgraded_to_local_recovery() {
232추가: let source = include_bytes!("../zip-recovery-fixtures/broken-local-header.zip");
233추가: for extended in [false, true] {
234추가: let mut input = source[..source.len() - 22].to_vec();
235추가: let mut end = source[source.len() - 22..].to_vec();
236추가: end[8..12].fill(0xff);
237추가: if extended {
238추가: let record_at = input.len() as u64;
239추가: let mut record = [0u8; 56];
240추가: record[..4].copy_from_slice(b"PK\x06\x06");
241추가: record[4..12].copy_from_slice(&44u64.to_le_bytes());
242추가: record[12..14].copy_from_slice(&45u16.to_le_bytes());
243추가: record[14..16].copy_from_slice(&45u16.to_le_bytes());
244추가: record[24..32].copy_from_slice(&6u64.to_le_bytes());
245추가: record[32..40].copy_from_slice(&6u64.to_le_bytes());
246추가: record[40..48].copy_from_slice(&340u64.to_le_bytes());
247추가: record[48..56].copy_from_slice(&1372u64.to_le_bytes());
248추가: input.extend(record);
249추가: input.extend(b"PK\x06\x07");
250추가: input.extend(0u32.to_le_bytes());
251추가: input.extend(record_at.to_le_bytes());
252추가: input.extend(1u32.to_le_bytes());
253추가: end[12..20].fill(0xff);
254추가: }
255추가: input.extend(end);
256추가: let s = Scratch::new(&input);
257추가: assert!(matches!(
258추가: Recovery::open(&s.path(), Limits::default(), &mut || Ok(())),
259추가: Err(ErrorCode::UnsupportedFeature)
260추가: ));
261추가: assert_eq!(std::fs::read(s.path()).unwrap(), input);
262추가: }
263추가: }