team-ljyzipelo

공개

ZIP64 확장값 해석 순서를 바로잡음

작성자
시로사키 하나
작성 시각
커밋
865ac8feaee2ff28925c027fdbe590ace2808e53
부모
09de05b
이 시점의 파일 보기

변경된 파일 2개

+133-29
수정experiments/rust-archive-worker/src/streaming.rs+58-29
@@ -338,6 +338,40 @@ fn large_value(value: u32, extra: &mut &[u8]) -> Result<u64> {
338338 *extra = &extra[8..];
339339 Ok(result)
340340}
341추가: fn expanded(value: u64, maximum: u64, large: u64) -> Result<u64> {
342추가: if value == maximum || value == large {
343추가: Ok(large)
344추가: } else {
345추가: Err(ErrorCode::Integrity)
346추가: }
347추가: }
348추가: fn disk_start(value: u16, extra: &mut &[u8]) -> Result<u32> {
349추가: if value != u16::MAX {
350추가: return Ok(value as u32);
351추가: }
352추가: if extra.len() < 4 {
353추가: return Err(ErrorCode::Integrity);
354추가: }
355추가: let result = u32at(extra, 0);
356추가: *extra = &extra[4..];
357추가: Ok(result)
358추가: }
359추가: fn local_sizes(size: u32, compressed: u32, extra: Option<&[u8]>) -> Result<(u64, u64)> {
360추가: if let Some(extra) = extra {
361추가: // 로컬 ZIP64 추가 필드에는 두 크기가 함께 들어감.
362추가: if extra.len() < 16 {
363추가: return Err(ErrorCode::Integrity);
364추가: }
365추가: Ok((
366추가: expanded(size as u64, u32::MAX as u64, u64at(extra, 0))?,
367추가: expanded(compressed as u64, u32::MAX as u64, u64at(extra, 8))?,
368추가: ))
369추가: } else if size == u32::MAX || compressed == u32::MAX {
370추가: Err(ErrorCode::Integrity)
371추가: } else {
372추가: Ok((size as u64, compressed as u64))
373추가: }
374추가: }
341375fn zip(
342376 source: &mut File,
343377 len: u64,
@@ -358,18 +392,16 @@ fn zip(
358392 })
359393 .ok_or(ErrorCode::Integrity)?;
360394 let h = &tail[end..];
361삭제: if u16at(h, 4) != 0 || u16at(h, 6) != 0 {
362삭제: return Err(ErrorCode::UnsupportedFeature);
363삭제: }
395추가: let mut disk = u16at(h, 4) as u64;
396추가: let mut central_disk = u16at(h, 6) as u64;
397추가: let mut entries_on_disk = u16at(h, 8) as u64;
364398 let mut count = u16at(h, 10) as u64;
365삭제: if count != u16at(h, 8) as u64 {
366삭제: return Err(ErrorCode::UnsupportedFeature);
367삭제: }
368399 let mut central_size = u32at(h, 12) as u64;
369400 let mut central = u32at(h, 16) as u64;
370401 let mut central_end = tail_at + end as u64;
371삭제: let required_zip64 =
372삭제: count == u16::MAX as u64 || central_size == u32::MAX as u64 || central == u32::MAX as u64;
402추가: let required_zip64 = [disk, central_disk, entries_on_disk, count].contains(&(u16::MAX as u64))
403추가: || central_size == u32::MAX as u64
404추가: || central == u32::MAX as u64;
373405 let locator = if central_end >= 20 {
374406 bytes(source, central_end - 20, 20)?
375407 } else {
@@ -396,20 +428,17 @@ fn zip(
396428 {
397429 return Err(ErrorCode::Integrity);
398430 }
399삭제: if u32at(&large, 16) != 0
400삭제: || u32at(&large, 20) != 0
401삭제: || u64at(&large, 24) != u64at(&large, 32)
402삭제: {
403삭제: return Err(ErrorCode::UnsupportedFeature);
404삭제: }
405삭제: let (n, size, start) = (u64at(&large, 32), u64at(&large, 40), u64at(&large, 48));
406삭제: if (count != u16::MAX as u64 && count != n)
407삭제: || (central_size != u32::MAX as u64 && central_size != size)
408삭제: || (central != u32::MAX as u64 && central != start)
409삭제: {
410삭제: return Err(ErrorCode::Integrity);
411삭제: }
412삭제: (count, central_size, central, central_end) = (n, size, start, offset);
431추가: // 각 필드의 확장값을 해석한 뒤 단일 디스크 조건을 검사함.
432추가: disk = expanded(disk, u16::MAX as u64, u32at(&large, 16) as u64)?;
433추가: central_disk = expanded(central_disk, u16::MAX as u64, u32at(&large, 20) as u64)?;
434추가: entries_on_disk = expanded(entries_on_disk, u16::MAX as u64, u64at(&large, 24))?;
435추가: count = expanded(count, u16::MAX as u64, u64at(&large, 32))?;
436추가: central_size = expanded(central_size, u32::MAX as u64, u64at(&large, 40))?;
437추가: central = expanded(central, u32::MAX as u64, u64at(&large, 48))?;
438추가: central_end = offset;
439추가: }
440추가: if disk != 0 || central_disk != 0 || entries_on_disk != count {
441추가: return Err(ErrorCode::UnsupportedFeature);
413442 }
414443 if count > limits.entries as u64 {
415444 return Err(ErrorCode::Limit);
@@ -434,7 +463,7 @@ fn zip(
434463 }
435464 let flags = u16at(&h, 8);
436465 let method = u16at(&h, 10);
437삭제: if flags & !0x080f != 0 || !matches!(method, 0 | 8 | 99) || u16at(&h, 34) != 0 {
466추가: if flags & !0x080f != 0 || !matches!(method, 0 | 8 | 99) {
438467 return Err(ErrorCode::UnsupportedFeature);
439468 }
440469 let crc = u32at(&h, 16);
@@ -460,7 +489,7 @@ fn zip(
460489 let size = large_value(u32at(&h, 24), &mut large)?;
461490 let compressed = large_value(u32at(&h, 20), &mut large)?;
462491 let local = large_value(u32at(&h, 42), &mut large)?;
463삭제: if u16at(&h, 34) == 65535 && (large.len() < 4 || u32at(large, 0) != 0) {
492추가: if disk_start(u16at(&h, 34), &mut large)? != 0 {
464493 return Err(ErrorCode::UnsupportedFeature);
465494 }
466495 let encrypted = flags & 1 != 0;
@@ -522,17 +551,17 @@ fn zip(
522551 return Err(ErrorCode::Integrity);
523552 }
524553 let (local_aes, local_large) = extra(&bytes(source, local + 30 + ln, le as usize)?)?;
525삭제: if local_aes != aes {
554추가: if local_aes != aes || local_large.as_ref().is_some_and(|values| values.len() < 16) {
526555 return Err(ErrorCode::Integrity);
527556 }
528557 let size_zip64 = u32at(&h, 20) == u32::MAX
529558 || u32at(&h, 24) == u32::MAX
530559 || u32at(&lh, 18) == u32::MAX
531삭제: || u32at(&lh, 22) == u32::MAX;
560추가: || u32at(&lh, 22) == u32::MAX
561추가: || local_large.is_some();
532562 if flags & 8 == 0 {
533삭제: let mut values = local_large.as_deref().unwrap_or(&[]);
534삭제: let local_size = large_value(u32at(&lh, 22), &mut values)?;
535삭제: let local_compressed = large_value(u32at(&lh, 18), &mut values)?;
563추가: let (local_size, local_compressed) =
564추가: local_sizes(u32at(&lh, 22), u32at(&lh, 18), local_large.as_deref())?;
536565 if u32at(&lh, 14) != crc || local_size != size || local_compressed != compressed {
537566 return Err(ErrorCode::Integrity);
538567 }
수정tests/rust-archive-worker/streaming_bounds.rs+75-0
@@ -5,3 +5,78 @@ fn offsets_do_not_overflow() {
55 assert_eq!(add(u64::MAX, 1), Err(ErrorCode::Integrity));
66 assert_eq!(add(u64::MAX - 1, 1), Ok(u64::MAX));
77}
8추가:
9추가: #[test]
10추가: fn eocd_fields_expand_independently_and_match_unextended_values() {
11추가: for maximum in [u16::MAX as u64, u32::MAX as u64] {
12추가: assert_eq!(expanded(maximum, maximum, 0), Ok(0));
13추가: assert_eq!(expanded(maximum, maximum, u64::MAX), Ok(u64::MAX));
14추가: assert_eq!(expanded(7, maximum, 7), Ok(7));
15추가: assert_eq!(expanded(7, maximum, 8), Err(ErrorCode::Integrity));
16추가: }
17추가: }
18추가:
19추가: #[test]
20추가: fn central_extensions_consume_only_sentinel_fields_in_order() {
21추가: for mask in 0..16 {
22추가: let mut extra = Vec::new();
23추가: let values = [11u64, 22, 33];
24추가: for (i, value) in values.iter().enumerate() {
25추가: if mask & (1 << i) != 0 {
26추가: extra.extend(value.to_le_bytes());
27추가: }
28추가: }
29추가: if mask & 8 != 0 {
30추가: extra.extend(44u32.to_le_bytes());
31추가: }
32추가: let mut rest = extra.as_slice();
33추가: for (i, value) in values.iter().enumerate() {
34추가: let ordinary = if mask & (1 << i) != 0 {
35추가: u32::MAX
36추가: } else {
37추가: *value as u32
38추가: };
39추가: assert_eq!(large_value(ordinary, &mut rest), Ok(*value));
40추가: }
41추가: assert_eq!(
42추가: disk_start(if mask & 8 != 0 { u16::MAX } else { 44 }, &mut rest),
43추가: Ok(44)
44추가: );
45추가: assert!(rest.is_empty());
46추가: }
47추가: for length in 0..4 {
48추가: assert_eq!(
49추가: disk_start(u16::MAX, &mut vec![0; length].as_slice()),
50추가: Err(ErrorCode::Integrity)
51추가: );
52추가: }
53추가: for length in 0..8 {
54추가: assert_eq!(
55추가: large_value(u32::MAX, &mut vec![0; length].as_slice()),
56추가: Err(ErrorCode::Integrity)
57추가: );
58추가: }
59추가: }
60추가:
61추가: #[test]
62추가: fn local_zip64_always_reads_both_sizes() {
63추가: let extra = [11u64.to_le_bytes(), 22u64.to_le_bytes()].concat();
64추가: for size in [11, u32::MAX] {
65추가: for compressed in [22, u32::MAX] {
66추가: assert_eq!(local_sizes(size, compressed, Some(&extra)), Ok((11, 22)));
67추가: }
68추가: }
69추가: assert_eq!(local_sizes(11, 22, None), Ok((11, 22)));
70추가: assert_eq!(local_sizes(u32::MAX, 22, None), Err(ErrorCode::Integrity));
71추가: assert_eq!(local_sizes(11, u32::MAX, None), Err(ErrorCode::Integrity));
72추가: assert_eq!(
73추가: local_sizes(12, u32::MAX, Some(&extra)),
74추가: Err(ErrorCode::Integrity)
75추가: );
76추가: for length in 0..16 {
77추가: assert_eq!(
78추가: local_sizes(u32::MAX, u32::MAX, Some(&extra[..length])),
79추가: Err(ErrorCode::Integrity)
80추가: );
81추가: }
82추가: }