Jooyung Han | 5d94bfc | 2021-08-06 14:07:49 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Alice Wang | ed79eab | 2022-09-08 11:16:31 +0000 | [diff] [blame] | 17 | //! Utilities for zip handling of APK files. |
Jooyung Han | 5d94bfc | 2021-08-06 14:07:49 +0900 | [diff] [blame] | 18 | |
Alice Wang | bc4b9a9 | 2022-09-16 13:13:18 +0000 | [diff] [blame] | 19 | use anyhow::{ensure, Result}; |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 20 | use bytes::{Buf, BufMut}; |
Jooyung Han | 5d94bfc | 2021-08-06 14:07:49 +0900 | [diff] [blame] | 21 | use std::io::{Read, Seek, SeekFrom}; |
| 22 | use zip::ZipArchive; |
| 23 | |
Alice Wang | ed79eab | 2022-09-08 11:16:31 +0000 | [diff] [blame] | 24 | const EOCD_SIZE_WITHOUT_COMMENT: usize = 22; |
Jooyung Han | cee6de6 | 2021-08-11 15:52:07 +0900 | [diff] [blame] | 25 | const EOCD_CENTRAL_DIRECTORY_SIZE_FIELD_OFFSET: usize = 12; |
Jooyung Han | 5d94bfc | 2021-08-06 14:07:49 +0900 | [diff] [blame] | 26 | const EOCD_CENTRAL_DIRECTORY_OFFSET_FIELD_OFFSET: usize = 16; |
Alice Wang | ed79eab | 2022-09-08 11:16:31 +0000 | [diff] [blame] | 27 | /// End of Central Directory signature |
| 28 | const EOCD_SIGNATURE: u32 = 0x06054b50; |
Jooyung Han | cee6de6 | 2021-08-11 15:52:07 +0900 | [diff] [blame] | 29 | const ZIP64_MARK: u32 = 0xffffffff; |
Jooyung Han | 5d94bfc | 2021-08-06 14:07:49 +0900 | [diff] [blame] | 30 | |
Chris Wailes | 6f5a9b5 | 2022-08-11 15:01:54 -0700 | [diff] [blame] | 31 | #[derive(Debug, PartialEq, Eq)] |
Jooyung Han | 5d94bfc | 2021-08-06 14:07:49 +0900 | [diff] [blame] | 32 | pub struct ZipSections { |
| 33 | pub central_directory_offset: u32, |
| 34 | pub central_directory_size: u32, |
| 35 | pub eocd_offset: u32, |
| 36 | pub eocd_size: u32, |
| 37 | } |
| 38 | |
| 39 | /// Discover the layout of a zip file. |
| 40 | pub fn zip_sections<R: Read + Seek>(mut reader: R) -> Result<(R, ZipSections)> { |
| 41 | // open a zip to parse EOCD |
| 42 | let archive = ZipArchive::new(reader)?; |
Alice Wang | ed79eab | 2022-09-08 11:16:31 +0000 | [diff] [blame] | 43 | let eocd_size = archive.comment().len() + EOCD_SIZE_WITHOUT_COMMENT; |
Alice Wang | bc4b9a9 | 2022-09-16 13:13:18 +0000 | [diff] [blame] | 44 | ensure!(archive.offset() == 0, "Invalid ZIP: offset should be 0, but {}.", archive.offset()); |
Jooyung Han | 5d94bfc | 2021-08-06 14:07:49 +0900 | [diff] [blame] | 45 | // retrieve reader back |
| 46 | reader = archive.into_inner(); |
| 47 | // the current position should point EOCD offset |
Jooyung Han | cee6de6 | 2021-08-11 15:52:07 +0900 | [diff] [blame] | 48 | let eocd_offset = reader.seek(SeekFrom::Current(0))? as u32; |
Jooyung Han | 5d94bfc | 2021-08-06 14:07:49 +0900 | [diff] [blame] | 49 | let mut eocd = vec![0u8; eocd_size as usize]; |
| 50 | reader.read_exact(&mut eocd)?; |
Alice Wang | bc4b9a9 | 2022-09-16 13:13:18 +0000 | [diff] [blame] | 51 | ensure!( |
| 52 | (&eocd[0..]).get_u32_le() == EOCD_SIGNATURE, |
| 53 | "Invalid ZIP: ZipArchive::new() should point EOCD after reading." |
| 54 | ); |
Jooyung Han | cee6de6 | 2021-08-11 15:52:07 +0900 | [diff] [blame] | 55 | let (central_directory_size, central_directory_offset) = get_central_directory(&eocd)?; |
Alice Wang | bc4b9a9 | 2022-09-16 13:13:18 +0000 | [diff] [blame] | 56 | ensure!( |
| 57 | central_directory_offset != ZIP64_MARK && central_directory_size != ZIP64_MARK, |
| 58 | "Unsupported ZIP: ZIP64 is not supported." |
| 59 | ); |
| 60 | ensure!( |
| 61 | central_directory_offset + central_directory_size == eocd_offset, |
| 62 | "Invalid ZIP: EOCD should follow CD with no extra data or overlap." |
| 63 | ); |
Jooyung Han | cee6de6 | 2021-08-11 15:52:07 +0900 | [diff] [blame] | 64 | |
Jooyung Han | 5d94bfc | 2021-08-06 14:07:49 +0900 | [diff] [blame] | 65 | Ok(( |
| 66 | reader, |
| 67 | ZipSections { |
| 68 | central_directory_offset, |
| 69 | central_directory_size, |
Jooyung Han | cee6de6 | 2021-08-11 15:52:07 +0900 | [diff] [blame] | 70 | eocd_offset, |
Jooyung Han | 5d94bfc | 2021-08-06 14:07:49 +0900 | [diff] [blame] | 71 | eocd_size: eocd_size as u32, |
| 72 | }, |
| 73 | )) |
| 74 | } |
| 75 | |
Jooyung Han | cee6de6 | 2021-08-11 15:52:07 +0900 | [diff] [blame] | 76 | fn get_central_directory(buf: &[u8]) -> Result<(u32, u32)> { |
Alice Wang | bc4b9a9 | 2022-09-16 13:13:18 +0000 | [diff] [blame] | 77 | ensure!(buf.len() >= EOCD_SIZE_WITHOUT_COMMENT, "Invalid EOCD size: {}", buf.len()); |
Jooyung Han | cee6de6 | 2021-08-11 15:52:07 +0900 | [diff] [blame] | 78 | let mut buf = &buf[EOCD_CENTRAL_DIRECTORY_SIZE_FIELD_OFFSET..]; |
| 79 | let size = buf.get_u32_le(); |
| 80 | let offset = buf.get_u32_le(); |
| 81 | Ok((size, offset)) |
Jooyung Han | 5d94bfc | 2021-08-06 14:07:49 +0900 | [diff] [blame] | 82 | } |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 83 | |
| 84 | /// Update EOCD's central_directory_offset field. |
| 85 | pub fn set_central_directory_offset(buf: &mut [u8], value: u32) -> Result<()> { |
Alice Wang | bc4b9a9 | 2022-09-16 13:13:18 +0000 | [diff] [blame] | 86 | ensure!(buf.len() >= EOCD_SIZE_WITHOUT_COMMENT, "Invalid EOCD size: {}", buf.len()); |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 87 | (&mut buf[EOCD_CENTRAL_DIRECTORY_OFFSET_FIELD_OFFSET..]).put_u32_le(value); |
| 88 | Ok(()) |
| 89 | } |
Jooyung Han | cee6de6 | 2021-08-11 15:52:07 +0900 | [diff] [blame] | 90 | |
| 91 | #[cfg(test)] |
| 92 | mod tests { |
| 93 | use super::*; |
Andrew Walbran | 117cd5e | 2021-08-13 11:42:13 +0000 | [diff] [blame] | 94 | use crate::testing::assert_contains; |
Alice Wang | ed79eab | 2022-09-08 11:16:31 +0000 | [diff] [blame] | 95 | use byteorder::{LittleEndian, ReadBytesExt}; |
| 96 | use std::fs::File; |
Jooyung Han | cee6de6 | 2021-08-11 15:52:07 +0900 | [diff] [blame] | 97 | use std::io::{Cursor, Write}; |
| 98 | use zip::{write::FileOptions, ZipWriter}; |
| 99 | |
| 100 | fn create_test_zip() -> Cursor<Vec<u8>> { |
| 101 | let mut writer = ZipWriter::new(Cursor::new(Vec::new())); |
| 102 | writer.start_file("testfile", FileOptions::default()).unwrap(); |
| 103 | writer.write_all(b"testcontent").unwrap(); |
| 104 | writer.finish().unwrap() |
| 105 | } |
| 106 | |
| 107 | #[test] |
| 108 | fn test_zip_sections() { |
| 109 | let (cursor, sections) = zip_sections(create_test_zip()).unwrap(); |
Alice Wang | ed79eab | 2022-09-08 11:16:31 +0000 | [diff] [blame] | 110 | assert_eq!( |
| 111 | sections.eocd_offset, |
| 112 | (cursor.get_ref().len() - EOCD_SIZE_WITHOUT_COMMENT) as u32 |
| 113 | ); |
Jooyung Han | cee6de6 | 2021-08-11 15:52:07 +0900 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | #[test] |
| 117 | fn test_reject_if_extra_data_between_cd_and_eocd() { |
| 118 | // prepare normal zip |
| 119 | let buf = create_test_zip().into_inner(); |
| 120 | |
| 121 | // insert garbage between CD and EOCD. |
| 122 | // by the way, to mock zip-rs, use CD as garbage. This is implementation detail of zip-rs, |
| 123 | // which reads CD at (eocd_offset - cd_size) instead of at cd_offset from EOCD. |
Alice Wang | ed79eab | 2022-09-08 11:16:31 +0000 | [diff] [blame] | 124 | let (pre_eocd, eocd) = buf.split_at(buf.len() - EOCD_SIZE_WITHOUT_COMMENT); |
Jooyung Han | cee6de6 | 2021-08-11 15:52:07 +0900 | [diff] [blame] | 125 | let (_, cd_offset) = get_central_directory(eocd).unwrap(); |
| 126 | let cd = &pre_eocd[cd_offset as usize..]; |
| 127 | |
| 128 | // ZipArchive::new() succeeds, but we should reject |
| 129 | let res = zip_sections(Cursor::new([pre_eocd, cd, eocd].concat())); |
| 130 | assert!(res.is_err()); |
Andrew Walbran | 117cd5e | 2021-08-13 11:42:13 +0000 | [diff] [blame] | 131 | assert_contains(&res.err().unwrap().to_string(), "Invalid ZIP: offset should be 0"); |
Jooyung Han | cee6de6 | 2021-08-11 15:52:07 +0900 | [diff] [blame] | 132 | } |
Alice Wang | ed79eab | 2022-09-08 11:16:31 +0000 | [diff] [blame] | 133 | |
| 134 | #[test] |
| 135 | fn test_zip_sections_with_apk() { |
| 136 | let apk = File::open("tests/data/v3-only-with-stamp.apk").unwrap(); |
| 137 | let (mut reader, sections) = zip_sections(apk).unwrap(); |
| 138 | |
| 139 | // Checks Central directory. |
| 140 | assert_eq!( |
| 141 | sections.central_directory_offset + sections.central_directory_size, |
| 142 | sections.eocd_offset |
| 143 | ); |
| 144 | |
| 145 | // Checks EOCD. |
| 146 | reader.seek(SeekFrom::Start(sections.eocd_offset as u64)).unwrap(); |
| 147 | assert_eq!(reader.read_u32::<LittleEndian>().unwrap(), EOCD_SIGNATURE); |
| 148 | assert_eq!( |
| 149 | reader.metadata().unwrap().len(), |
| 150 | (sections.eocd_offset + sections.eocd_size) as u64 |
| 151 | ); |
| 152 | } |
Jooyung Han | cee6de6 | 2021-08-11 15:52:07 +0900 | [diff] [blame] | 153 | } |