team-ljyzipelo

공개

ZIP64 locator 기반 감지와 검증을 보완함

작성자
시로사키 하나
작성 시각
커밋
bcc9c5cd729812921878072a22953412dfe5dd78
부모
608bf6f
이 시점의 파일 보기

변경된 파일 3개

+85-7
수정experiments/rust-archive-worker/src/streaming.rs+12-3
@@ -392,11 +392,20 @@ fn zip(
392392 let mut central_size = u32at(h, 12) as u64;
393393 let mut central = u32at(h, 16) as u64;
394394 let mut central_end = tail_at + end as u64;
395삭제: if count == u16::MAX as u64 || central_size == u32::MAX as u64 || central == u32::MAX as u64 {
396삭제: if central_end < 20 {
395추가: let required_zip64 =
396추가: count == u16::MAX as u64 || central_size == u32::MAX as u64 || central == u32::MAX as u64;
397추가: let locator = if central_end >= 20 {
398추가: bytes(source, central_end - 20, 20)?
399추가: } else {
400추가: Vec::new()
401추가: };
402추가: // 중앙 디렉터리 주석의 동일 서명을 locator로 오인하지 않음
403추가: let has_locator =
404추가: locator.starts_with(b"PK\x06\x07") && add(central, central_size)? != central_end;
405추가: if required_zip64 || has_locator {
406추가: if locator.len() != 20 {
397407 return Err(ErrorCode::Integrity);
398408 }
399삭제: let locator = bytes(source, central_end - 20, 20)?;
400409 if &locator[..4] != b"PK\x06\x07" || u32at(&locator, 4) != 0 || u32at(&locator, 16) != 1 {
401410 return Err(ErrorCode::UnsupportedFeature);
402411 }
수정tests/rust-archive-worker/hardening.rs+41-0
@@ -149,6 +149,47 @@ fn zip64_offset_only_keeps_32_bit_descriptors() {
149149 }
150150}
151151#[test]
152추가: fn zip64_locator_with_ordinary_eocd_fields_interoperates() {
153추가: let s = Scratch::new();
154추가: generate(&s, "locator64", 20);
155추가: assert_eq!(fs::metadata(s.0.join("input")).unwrap().len(), 260);
156추가: let events = execute(Start {
157추가: action: Action::List {
158추가: archive: s.path("input"),
159추가: },
160추가: limits: Limits::default(),
161추가: });
162추가: assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}");
163추가: let events = execute(start(&s, false, Limits::default().output));
164추가: assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}");
165추가: assert_zero_file(&s.0.join("output/payload.bin"), 20);
166추가: }
167추가: #[test]
168추가: fn zip64_locator_corruption_never_publishes() {
169추가: for (kind, error) in [
170추가: ("locator64-bad-offset", ErrorCode::Integrity),
171추가: ("locator64-bad-size", ErrorCode::Integrity),
172추가: ("locator64-conflicting-count", ErrorCode::Integrity),
173추가: ("locator64-multidisk", ErrorCode::UnsupportedFeature),
174추가: ] {
175추가: let s = Scratch::new();
176추가: generate(&s, kind, 20);
177추가: let events = execute(start(&s, false, Limits::default().output));
178추가: assert_eq!(terminal(&events).error, error, "{kind}: {events:?}");
179추가: assert_eq!(terminal(&events).publication, Publication::None);
180추가: assert_eq!(terminal(&events).cleanup, Cleanup::Complete);
181추가: assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0);
182추가: }
183추가: }
184추가: #[test]
185추가: fn classic_directory_comment_is_not_a_zip64_locator() {
186추가: let s = Scratch::new();
187추가: generate(&s, "classic-locator-decoy", 20);
188추가: let events = execute(start(&s, false, Limits::default().output));
189추가: assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}");
190추가: assert_zero_file(&s.0.join("output/payload.bin"), 20);
191추가: }
192추가: #[test]
152193fn untrusted_zip64_u64_sizes_and_offsets_never_allocate_or_publish() {
153194 for (kind, error) in [
154195 ("central64-size-overflow", ErrorCode::Limit),
수정tests/rust-archive-worker/hardening_cases.py+32-4
@@ -67,7 +67,7 @@ def generate(directory, kind, size):
6767 if kind == "central64":
6868 with zipfile.ZipFile(output) as reference:
6969 assert reference.read(fixtures.NAME) == bytes(size)
70삭제: elif kind == "descriptor64":
70추가: elif kind == "descriptor64" or kind.startswith("locator64"):
7171 class Pipe(io.BytesIO):
7272 def seekable(self):
7373 return False
@@ -77,9 +77,37 @@ def generate(directory, kind, size):
7777 with zipfile.ZipFile(data, "w", compression=zipfile.ZIP_STORED) as archive:
7878 with archive.open(fixtures.NAME, "w", force_zip64=True) as target:
7979 target.write(bytes(size))
80삭제: output.write_bytes(data.getvalue())
81삭제: with zipfile.ZipFile(output) as reference:
82삭제: assert reference.read(fixtures.NAME) == bytes(size)
80추가: original = data.getvalue()
81추가: if kind.startswith("locator64"):
82추가: end = original[-22:]
83추가: count, central_size, central_at = struct.unpack_from("<HII", end, 10)
84추가: record_at = len(original) - 22
85추가: record = bytearray(struct.pack(
86추가: "<4sQHHIIQQQQ", b"PK\x06\x06", 44, 45, 45,
87추가: 0, 0, count, count, central_size, central_at,
88추가: ))
89추가: locator = bytearray(struct.pack("<4sIQI", b"PK\x06\x07", 0, record_at, 1))
90추가: if kind == "locator64-bad-offset":
91추가: struct.pack_into("<Q", locator, 8, (1 << 64) - 1)
92추가: elif kind == "locator64-bad-size":
93추가: struct.pack_into("<Q", record, 4, (1 << 64) - 1)
94추가: elif kind == "locator64-conflicting-count":
95추가: struct.pack_into("<QQ", record, 24, count + 1, count + 1)
96추가: elif kind == "locator64-multidisk":
97추가: struct.pack_into("<I", locator, 16, 2)
98추가: original = original[:-22] + record + locator + end
99추가: if kind == "locator64":
100추가: assert len(original) == 260
101추가: assert count == 1 and central_size < 0xffffffff and central_at < 0xffffffff
102추가: output.write_bytes(original)
103추가: if kind in ("descriptor64", "locator64"):
104추가: with zipfile.ZipFile(output) as reference:
105추가: assert reference.read(fixtures.NAME) == bytes(size)
106추가: elif kind == "classic-locator-decoy":
107추가: info = zipfile.ZipInfo(fixtures.NAME)
108추가: info.comment = struct.pack("<4sIQI", b"PK\x06\x07", 0, 0, 1)
109추가: with zipfile.ZipFile(output, "x") as archive:
110추가: archive.writestr(info, bytes(size))
83111 elif kind == "gzip-bomb-joined":
84112 with output.open("xb") as raw:
85113 for level in (6, 0):