blob: 849b68c1e95f36c305f13762671dade13f35df39 [file] [log] [blame]
Narayan Kamath7462f022013-11-21 13:05:04 +00001/*
2 * Copyright (C) 2008 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
17/*
18 * Read-only access to Zip archives, with minimal heap allocation.
19 */
Narayan Kamath7462f022013-11-21 13:05:04 +000020
Mark Salyzyncfd5b082016-10-17 14:28:00 -070021#define LOG_TAG "ziparchive"
22
Elliott Hughese8f4b142018-10-19 16:09:39 -070023#include "ziparchive/zip_archive.h"
24
Narayan Kamath7462f022013-11-21 13:05:04 +000025#include <errno.h>
Mark Salyzyn99ef9912014-03-14 14:26:22 -070026#include <fcntl.h>
27#include <inttypes.h>
Narayan Kamath7462f022013-11-21 13:05:04 +000028#include <limits.h>
Narayan Kamath7462f022013-11-21 13:05:04 +000029#include <stdlib.h>
30#include <string.h>
Elliott Hughes55fd2932017-05-28 22:59:04 -070031#include <time.h>
Narayan Kamath7462f022013-11-21 13:05:04 +000032#include <unistd.h>
33
Dan Albert1ae07642015-04-09 14:11:18 -070034#include <memory>
Songchun Fanc33f5262020-03-24 09:15:51 -070035#include <optional>
Dan Albert1ae07642015-04-09 14:11:18 -070036#include <vector>
37
Elliott Hughes9c8bd662018-10-26 16:14:21 -070038#if defined(__APPLE__)
39#define lseek64 lseek
40#endif
41
Josh Gao1b496342018-07-17 11:08:48 -070042#if defined(__BIONIC__)
43#include <android/fdsan.h>
44#endif
45
Mark Salyzynff2dcd92016-09-28 15:54:45 -070046#include <android-base/file.h>
47#include <android-base/logging.h>
48#include <android-base/macros.h> // TEMP_FAILURE_RETRY may or may not be in unistd
Elliott Hughese8f4b142018-10-19 16:09:39 -070049#include <android-base/mapped_file.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070050#include <android-base/memory.h>
Elliott Hughes50ef29a2019-06-18 18:23:59 -070051#include <android-base/strings.h>
Ryan Mitchellc77f9d32018-08-25 14:06:29 -070052#include <android-base/utf8.h>
Mark Salyzyncfd5b082016-10-17 14:28:00 -070053#include <log/log.h>
Dan Albert1ae07642015-04-09 14:11:18 -070054#include "zlib.h"
Narayan Kamath7462f022013-11-21 13:05:04 +000055
Narayan Kamath044bc8e2014-12-03 18:22:53 +000056#include "entry_name_utils-inl.h"
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070057#include "zip_archive_common.h"
Christopher Ferrise6884ce2015-11-10 14:55:12 -080058#include "zip_archive_private.h"
Mark Salyzyn99ef9912014-03-14 14:26:22 -070059
Dan Albert1ae07642015-04-09 14:11:18 -070060using android::base::get_unaligned;
Narayan Kamath044bc8e2014-12-03 18:22:53 +000061
Narayan Kamath162b7052017-06-05 13:21:12 +010062// Used to turn on crc checks - verify that the content CRC matches the values
63// specified in the local file header and the central directory.
64static const bool kCrcChecksEnabled = false;
65
Narayan Kamath926973e2014-06-09 14:18:14 +010066// The maximum number of bytes to scan backwards for the EOCD start.
67static const uint32_t kMaxEOCDSearch = kMaxCommentLen + sizeof(EocdRecord);
68
Tianjie Xu69ee4b72020-03-11 11:59:10 -070069// Set a reasonable cap (256 GiB) for the zip file size. So the data is always valid when
70// we parse the fields in cd or local headers as 64 bits signed integers.
71static constexpr uint64_t kMaxFileLength = 256 * static_cast<uint64_t>(1u << 30u);
72
Narayan Kamath7462f022013-11-21 13:05:04 +000073/*
74 * A Read-only Zip archive.
75 *
76 * We want "open" and "find entry by name" to be fast operations, and
77 * we want to use as little memory as possible. We memory-map the zip
78 * central directory, and load a hash table with pointers to the filenames
79 * (which aren't null-terminated). The other fields are at a fixed offset
80 * from the filename, so we don't need to extract those (but we do need
81 * to byte-read and endian-swap them every time we want them).
82 *
83 * It's possible that somebody has handed us a massive (~1GB) zip archive,
84 * so we can't expect to mmap the entire file.
85 *
86 * To speed comparisons when doing a lookup by name, we could make the mapping
87 * "private" (copy-on-write) and null-terminate the filenames after verifying
88 * the record structure. However, this requires a private mapping of
89 * every page that the Central Directory touches. Easier to tuck a copy
90 * of the string length into the hash table entry.
91 */
Narayan Kamath7462f022013-11-21 13:05:04 +000092
Josh Gaoabdfc242018-09-07 12:44:40 -070093#if defined(__BIONIC__)
94uint64_t GetOwnerTag(const ZipArchive* archive) {
95 return android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_ZIPARCHIVE,
96 reinterpret_cast<uint64_t>(archive));
97}
98#endif
99
Ryan Mitchell23150e42020-03-09 09:33:46 -0700100ZipArchive::ZipArchive(MappedZipFile&& map, bool assume_ownership)
101 : mapped_zip(map),
Josh Gao1b496342018-07-17 11:08:48 -0700102 close_file(assume_ownership),
103 directory_offset(0),
104 central_directory(),
Elliott Hughese8f4b142018-10-19 16:09:39 -0700105 directory_map(),
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800106 num_entries(0) {
Josh Gao1b496342018-07-17 11:08:48 -0700107#if defined(__BIONIC__)
108 if (assume_ownership) {
Ryan Mitchell23150e42020-03-09 09:33:46 -0700109 CHECK(mapped_zip.HasFd());
110 android_fdsan_exchange_owner_tag(mapped_zip.GetFileDescriptor(), 0, GetOwnerTag(this));
Josh Gao1b496342018-07-17 11:08:48 -0700111 }
112#endif
113}
114
Elliott Hughesf66460b2019-10-22 11:44:50 -0700115ZipArchive::ZipArchive(const void* address, size_t length)
Josh Gao1b496342018-07-17 11:08:48 -0700116 : mapped_zip(address, length),
117 close_file(false),
118 directory_offset(0),
119 central_directory(),
Elliott Hughese8f4b142018-10-19 16:09:39 -0700120 directory_map(),
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800121 num_entries(0) {}
Josh Gao1b496342018-07-17 11:08:48 -0700122
123ZipArchive::~ZipArchive() {
124 if (close_file && mapped_zip.GetFileDescriptor() >= 0) {
125#if defined(__BIONIC__)
Josh Gaoabdfc242018-09-07 12:44:40 -0700126 android_fdsan_close_with_tag(mapped_zip.GetFileDescriptor(), GetOwnerTag(this));
Josh Gao1b496342018-07-17 11:08:48 -0700127#else
128 close(mapped_zip.GetFileDescriptor());
129#endif
130 }
Josh Gao1b496342018-07-17 11:08:48 -0700131}
132
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700133struct CentralDirectoryInfo {
134 uint64_t num_records;
135 // The size of the central directory (in bytes).
136 uint64_t cd_size;
137 // The offset of the start of the central directory, relative
138 // to the start of the file.
139 uint64_t cd_start_offset;
140};
141
Tianjie6ab29122020-03-18 17:44:30 -0700142static ZipError FindCentralDirectoryInfoForZip64(const char* debugFileName, ZipArchive* archive,
143 off64_t eocdOffset, CentralDirectoryInfo* cdInfo) {
144 if (eocdOffset <= sizeof(Zip64EocdLocator)) {
145 ALOGW("Zip: %s: Not enough space for zip64 eocd locator", debugFileName);
146 return kInvalidFile;
147 }
148 // We expect to find the zip64 eocd locator immediately before the zip eocd.
149 const int64_t locatorOffset = eocdOffset - sizeof(Zip64EocdLocator);
150 Zip64EocdLocator zip64EocdLocator{};
151 if (!archive->mapped_zip.ReadAtOffset(reinterpret_cast<uint8_t*>((&zip64EocdLocator)),
152 sizeof(Zip64EocdLocator), locatorOffset)) {
153 ALOGW("Zip: %s: Read %zu from offset %" PRId64 " failed %s", debugFileName,
154 sizeof(Zip64EocdLocator), locatorOffset, debugFileName);
155 return kIoError;
156 }
157
158 if (zip64EocdLocator.locator_signature != Zip64EocdLocator::kSignature) {
159 ALOGW("Zip: %s: Zip64 eocd locator signature not found at offset %" PRId64, debugFileName,
160 locatorOffset);
161 return kInvalidFile;
162 }
163
164 const int64_t zip64EocdOffset = zip64EocdLocator.zip64_eocd_offset;
Tianjie173aba02020-03-28 18:28:43 -0700165 if (locatorOffset <= sizeof(Zip64EocdRecord) ||
166 zip64EocdOffset > locatorOffset - sizeof(Zip64EocdRecord)) {
167 ALOGW("Zip: %s: Bad zip64 eocd offset %" PRId64 ", eocd locator offset %" PRId64, debugFileName,
168 zip64EocdOffset, locatorOffset);
Tianjie6ab29122020-03-18 17:44:30 -0700169 return kInvalidOffset;
170 }
171
172 Zip64EocdRecord zip64EocdRecord{};
173 if (!archive->mapped_zip.ReadAtOffset(reinterpret_cast<uint8_t*>(&zip64EocdRecord),
174 sizeof(Zip64EocdRecord), zip64EocdOffset)) {
175 ALOGW("Zip: %s: read %zu from offset %" PRId64 " failed %s", debugFileName,
Tianjie173aba02020-03-28 18:28:43 -0700176 sizeof(Zip64EocdLocator), zip64EocdOffset, debugFileName);
Tianjie6ab29122020-03-18 17:44:30 -0700177 return kIoError;
178 }
179
180 if (zip64EocdRecord.record_signature != Zip64EocdRecord::kSignature) {
181 ALOGW("Zip: %s: Zip64 eocd record signature not found at offset %" PRId64, debugFileName,
182 zip64EocdOffset);
183 return kInvalidFile;
184 }
185
Tianjie173aba02020-03-28 18:28:43 -0700186 if (zip64EocdOffset <= zip64EocdRecord.cd_size ||
187 zip64EocdRecord.cd_start_offset > zip64EocdOffset - zip64EocdRecord.cd_size) {
Tianjie6ab29122020-03-18 17:44:30 -0700188 ALOGW("Zip: %s: Bad offset for zip64 central directory. cd offset %" PRIu64 ", cd size %" PRIu64
189 ", zip64 eocd offset %" PRIu64,
190 debugFileName, zip64EocdRecord.cd_start_offset, zip64EocdRecord.cd_size, zip64EocdOffset);
191 return kInvalidOffset;
192 }
193
194 *cdInfo = {.num_records = zip64EocdRecord.num_records,
195 .cd_size = zip64EocdRecord.cd_size,
196 .cd_start_offset = zip64EocdRecord.cd_start_offset};
197
198 return kSuccess;
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700199}
200
201static ZipError FindCentralDirectoryInfo(const char* debug_file_name, ZipArchive* archive,
202 off64_t file_length, uint32_t read_amount,
203 CentralDirectoryInfo* cdInfo) {
204 std::vector<uint8_t> scan_buffer(read_amount);
Narayan Kamath7462f022013-11-21 13:05:04 +0000205 const off64_t search_start = file_length - read_amount;
206
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700207 if (!archive->mapped_zip.ReadAtOffset(scan_buffer.data(), read_amount, search_start)) {
Jiyong Parkcd997e62017-06-30 17:23:33 +0900208 ALOGE("Zip: read %" PRId64 " from offset %" PRId64 " failed", static_cast<int64_t>(read_amount),
209 static_cast<int64_t>(search_start));
Narayan Kamath7462f022013-11-21 13:05:04 +0000210 return kIoError;
211 }
212
213 /*
214 * Scan backward for the EOCD magic. In an archive without a trailing
215 * comment, we'll find it on the first try. (We may want to consider
216 * doing an initial minimal read; if we don't find it, retry with a
217 * second read as above.)
218 */
Andreas Gampe964b95c2019-04-05 13:48:02 -0700219 CHECK_LE(read_amount, std::numeric_limits<int32_t>::max());
220 int32_t i = read_amount - sizeof(EocdRecord);
Narayan Kamath926973e2014-06-09 14:18:14 +0100221 for (; i >= 0; i--) {
Dan Albert1ae07642015-04-09 14:11:18 -0700222 if (scan_buffer[i] == 0x50) {
223 uint32_t* sig_addr = reinterpret_cast<uint32_t*>(&scan_buffer[i]);
224 if (get_unaligned<uint32_t>(sig_addr) == EocdRecord::kSignature) {
225 ALOGV("+++ Found EOCD at buf+%d", i);
226 break;
227 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000228 }
229 }
230 if (i < 0) {
231 ALOGD("Zip: EOCD not found, %s is not zip", debug_file_name);
232 return kInvalidFile;
233 }
234
235 const off64_t eocd_offset = search_start + i;
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700236 auto eocd = reinterpret_cast<const EocdRecord*>(scan_buffer.data() + i);
Narayan Kamath7462f022013-11-21 13:05:04 +0000237 /*
Narayan Kamath926973e2014-06-09 14:18:14 +0100238 * Verify that there's no trailing space at the end of the central directory
239 * and its comment.
Narayan Kamath7462f022013-11-21 13:05:04 +0000240 */
Jiyong Parkcd997e62017-06-30 17:23:33 +0900241 const off64_t calculated_length = eocd_offset + sizeof(EocdRecord) + eocd->comment_length;
Narayan Kamath926973e2014-06-09 14:18:14 +0100242 if (calculated_length != file_length) {
Narayan Kamath4f6b4992014-06-03 13:59:23 +0100243 ALOGW("Zip: %" PRId64 " extraneous bytes at the end of the central directory",
Narayan Kamath926973e2014-06-09 14:18:14 +0100244 static_cast<int64_t>(file_length - calculated_length));
Narayan Kamath4f6b4992014-06-03 13:59:23 +0100245 return kInvalidFile;
246 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000247
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700248 // One of the field is 0xFFFFFFFF, look for the zip64 EOCD instead.
249 if (eocd->cd_size == UINT32_MAX || eocd->cd_start_offset == UINT32_MAX) {
250 ALOGV("Looking for the zip64 EOCD, cd_size: %" PRIu32 "cd_start_offset: %" PRId32,
251 eocd->cd_size, eocd->cd_start_offset);
Tianjie6ab29122020-03-18 17:44:30 -0700252 return FindCentralDirectoryInfoForZip64(debug_file_name, archive, eocd_offset, cdInfo);
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700253 }
254
Narayan Kamath926973e2014-06-09 14:18:14 +0100255 /*
256 * Grab the CD offset and size, and the number of entries in the
257 * archive and verify that they look reasonable.
258 */
Tianjie Xu1ee48922016-09-21 14:58:11 -0700259 if (static_cast<off64_t>(eocd->cd_start_offset) + eocd->cd_size > eocd_offset) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100260 ALOGW("Zip: bad offsets (dir %" PRIu32 ", size %" PRIu32 ", eocd %" PRId64 ")",
Jiyong Parkcd997e62017-06-30 17:23:33 +0900261 eocd->cd_start_offset, eocd->cd_size, static_cast<int64_t>(eocd_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000262 return kInvalidOffset;
263 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000264
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700265 *cdInfo = {.num_records = eocd->num_records,
266 .cd_size = eocd->cd_size,
267 .cd_start_offset = eocd->cd_start_offset};
268 return kSuccess;
Narayan Kamath7462f022013-11-21 13:05:04 +0000269}
270
271/*
272 * Find the zip Central Directory and memory-map it.
273 *
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700274 * On success, returns kSuccess after populating fields from the EOCD area:
Narayan Kamath7462f022013-11-21 13:05:04 +0000275 * directory_offset
Tianjie Xu18c25922016-09-29 15:27:41 -0700276 * directory_ptr
Narayan Kamath7462f022013-11-21 13:05:04 +0000277 * num_entries
278 */
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700279static ZipError MapCentralDirectory(const char* debug_file_name, ZipArchive* archive) {
280 // Test file length. We use lseek64 to make sure the file is small enough to be a zip file.
Tianjie Xu18c25922016-09-29 15:27:41 -0700281 off64_t file_length = archive->mapped_zip.GetFileLength();
Narayan Kamath7462f022013-11-21 13:05:04 +0000282 if (file_length == -1) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000283 return kInvalidFile;
284 }
285
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700286 if (file_length > kMaxFileLength) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100287 ALOGV("Zip: zip file too long %" PRId64, static_cast<int64_t>(file_length));
Narayan Kamath7462f022013-11-21 13:05:04 +0000288 return kInvalidFile;
289 }
290
Narayan Kamath926973e2014-06-09 14:18:14 +0100291 if (file_length < static_cast<off64_t>(sizeof(EocdRecord))) {
292 ALOGV("Zip: length %" PRId64 " is too small to be zip", static_cast<int64_t>(file_length));
Narayan Kamath7462f022013-11-21 13:05:04 +0000293 return kInvalidFile;
294 }
295
296 /*
297 * Perform the traditional EOCD snipe hunt.
298 *
299 * We're searching for the End of Central Directory magic number,
300 * which appears at the start of the EOCD block. It's followed by
301 * 18 bytes of EOCD stuff and up to 64KB of archive comment. We
302 * need to read the last part of the file into a buffer, dig through
303 * it to find the magic number, parse some values out, and use those
304 * to determine the extent of the CD.
305 *
306 * We start by pulling in the last part of the file.
307 */
Andreas Gampe964b95c2019-04-05 13:48:02 -0700308 uint32_t read_amount = kMaxEOCDSearch;
Narayan Kamath926973e2014-06-09 14:18:14 +0100309 if (file_length < read_amount) {
Andreas Gampe964b95c2019-04-05 13:48:02 -0700310 read_amount = static_cast<uint32_t>(file_length);
Narayan Kamath7462f022013-11-21 13:05:04 +0000311 }
312
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700313 CentralDirectoryInfo cdInfo = {};
314 if (auto result =
315 FindCentralDirectoryInfo(debug_file_name, archive, file_length, read_amount, &cdInfo);
316 result != kSuccess) {
317 return result;
318 }
319
320 if (cdInfo.num_records == 0) {
321#if defined(__ANDROID__)
322 ALOGW("Zip: empty archive?");
323#endif
324 return kEmptyArchive;
325 }
326
327 if (cdInfo.cd_size >= SIZE_MAX) {
328 ALOGW("Zip: The size of central directory doesn't fit in range of size_t: %" PRIu64,
329 cdInfo.cd_size);
330 return kInvalidFile;
331 }
332
333 ALOGV("+++ num_entries=%" PRIu64 " dir_size=%" PRIu64 " dir_offset=%" PRIu64, cdInfo.num_records,
334 cdInfo.cd_size, cdInfo.cd_start_offset);
335
336 // It all looks good. Create a mapping for the CD, and set the fields in archive.
337 if (!archive->InitializeCentralDirectory(static_cast<off64_t>(cdInfo.cd_start_offset),
338 static_cast<size_t>(cdInfo.cd_size))) {
339 return kMmapFailed;
340 }
341
342 archive->num_entries = cdInfo.num_records;
343 archive->directory_offset = cdInfo.cd_start_offset;
344
345 return kSuccess;
Narayan Kamath7462f022013-11-21 13:05:04 +0000346}
347
Tianjie6ab29122020-03-18 17:44:30 -0700348static ZipError ParseZip64ExtendedInfoInExtraField(
349 const uint8_t* extraFieldStart, uint16_t extraFieldLength, uint32_t zip32UncompressedSize,
350 uint32_t zip32CompressedSize, std::optional<uint32_t> zip32LocalFileHeaderOffset,
351 Zip64ExtendedInfo* zip64Info) {
352 if (extraFieldLength <= 4) {
353 ALOGW("Zip: Extra field isn't large enough to hold zip64 info, size %" PRIu16,
354 extraFieldLength);
355 return kInvalidFile;
356 }
357
358 // Each header MUST consist of:
359 // Header ID - 2 bytes
360 // Data Size - 2 bytes
361 uint16_t offset = 0;
362 while (offset < extraFieldLength - 4) {
363 auto headerId = get_unaligned<uint16_t>(extraFieldStart + offset);
364 auto dataSize = get_unaligned<uint16_t>(extraFieldStart + offset + 2);
365
366 offset += 4;
367 if (dataSize > extraFieldLength - offset) {
368 ALOGW("Zip: Data size exceeds the boundary of extra field, data size %" PRIu16, dataSize);
369 return kInvalidOffset;
370 }
371
372 // Skip the other types of extensible data fields. Details in
373 // https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT section 4.5
374 if (headerId != Zip64ExtendedInfo::kHeaderId) {
375 offset += dataSize;
376 continue;
377 }
378
379 uint16_t expectedDataSize = 0;
380 // We expect the extended field to include both uncompressed and compressed size.
381 if (zip32UncompressedSize == UINT32_MAX || zip32CompressedSize == UINT32_MAX) {
382 expectedDataSize += 16;
383 }
384 if (zip32LocalFileHeaderOffset == UINT32_MAX) {
385 expectedDataSize += 8;
386 }
387
388 if (expectedDataSize == 0) {
389 ALOGW("Zip: Data size should not be 0 in zip64 extended field");
390 return kInvalidFile;
391 }
392
393 if (dataSize != expectedDataSize) {
394 auto localOffsetString = zip32LocalFileHeaderOffset.has_value()
395 ? std::to_string(zip32LocalFileHeaderOffset.value())
396 : "missing";
397 ALOGW("Zip: Invalid data size in zip64 extended field, expect %" PRIu16 ", get %" PRIu16
398 ", uncompressed size %" PRIu32 ", compressed size %" PRIu32 ", local header offset %s",
399 expectedDataSize, dataSize, zip32UncompressedSize, zip32CompressedSize,
400 localOffsetString.c_str());
401 return kInvalidFile;
402 }
403
404 std::optional<uint64_t> uncompressedFileSize;
405 std::optional<uint64_t> compressedFileSize;
406 std::optional<uint64_t> localHeaderOffset;
407 if (zip32UncompressedSize == UINT32_MAX || zip32CompressedSize == UINT32_MAX) {
408 uncompressedFileSize = get_unaligned<uint64_t>(extraFieldStart + offset);
409 compressedFileSize = get_unaligned<uint64_t>(extraFieldStart + offset + 8);
410 offset += 16;
411
412 // TODO(xunchang) Support handling file large than UINT32_MAX. It's theoretically possible
413 // for libz to (de)compressing file larger than UINT32_MAX. But we should use our own
414 // bytes counter to replace stream.total_out.
415 if (uncompressedFileSize.value() >= UINT32_MAX || compressedFileSize.value() >= UINT32_MAX) {
416 ALOGW(
417 "Zip: File size larger than UINT32_MAX isn't supported yet. uncompressed size %" PRIu64
418 ", compressed size %" PRIu64,
419 uncompressedFileSize.value(), compressedFileSize.value());
420 return kInvalidFile;
421 }
422 }
423
424 if (zip32LocalFileHeaderOffset == UINT32_MAX) {
425 localHeaderOffset = get_unaligned<uint64_t>(extraFieldStart + offset);
426 offset += 8;
427 }
428
429 zip64Info->uncompressed_file_size = uncompressedFileSize;
430 zip64Info->compressed_file_size = compressedFileSize;
431 zip64Info->local_header_offset = localHeaderOffset;
432 return kSuccess;
433 }
434
435 ALOGW("Zip: zip64 extended info isn't found in the extra field.");
436 return kInvalidFile;
437}
438
Narayan Kamath7462f022013-11-21 13:05:04 +0000439/*
440 * Parses the Zip archive's Central Directory. Allocates and populates the
441 * hash table.
442 *
443 * Returns 0 on success.
444 */
Tianjie6ab29122020-03-18 17:44:30 -0700445static ZipError ParseZipArchive(ZipArchive* archive) {
Tianjie Xu18c25922016-09-29 15:27:41 -0700446 const uint8_t* const cd_ptr = archive->central_directory.GetBasePtr();
447 const size_t cd_length = archive->central_directory.GetMapLength();
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700448 const uint64_t num_entries = archive->num_entries;
Narayan Kamath7462f022013-11-21 13:05:04 +0000449
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700450 if (num_entries <= UINT16_MAX) {
451 archive->cd_entry_map = CdEntryMapZip32::Create(static_cast<uint16_t>(num_entries));
Tianjie Xu0ef97832020-03-15 21:23:24 -0700452 } else {
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700453 archive->cd_entry_map = CdEntryMapZip64::Create();
Tianjie Xu0ef97832020-03-15 21:23:24 -0700454 }
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800455 if (archive->cd_entry_map == nullptr) {
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800456 return kAllocationFailed;
Tianjie Xu9e020e22016-10-10 12:11:30 -0700457 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000458
459 /*
460 * Walk through the central directory, adding entries to the hash
461 * table and verifying values.
462 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100463 const uint8_t* const cd_end = cd_ptr + cd_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000464 const uint8_t* ptr = cd_ptr;
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700465 for (uint64_t i = 0; i < num_entries; i++) {
Tianjie Xu0fda1cf2017-04-05 14:46:27 -0700466 if (ptr > cd_end - sizeof(CentralDirectoryRecord)) {
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700467 ALOGW("Zip: ran off the end (item #%" PRIu64 ", %zu bytes of central directory)", i,
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800468 cd_length);
Tianjie Xu0fda1cf2017-04-05 14:46:27 -0700469#if defined(__ANDROID__)
470 android_errorWriteLog(0x534e4554, "36392138");
471#endif
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800472 return kInvalidFile;
Tianjie Xu0fda1cf2017-04-05 14:46:27 -0700473 }
474
Tianjie6ab29122020-03-18 17:44:30 -0700475 auto cdr = reinterpret_cast<const CentralDirectoryRecord*>(ptr);
Narayan Kamath926973e2014-06-09 14:18:14 +0100476 if (cdr->record_signature != CentralDirectoryRecord::kSignature) {
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700477 ALOGW("Zip: missed a central dir sig (at %" PRIu64 ")", i);
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800478 return kInvalidFile;
Narayan Kamath7462f022013-11-21 13:05:04 +0000479 }
480
Narayan Kamath926973e2014-06-09 14:18:14 +0100481 const uint16_t file_name_length = cdr->file_name_length;
482 const uint16_t extra_length = cdr->extra_field_length;
483 const uint16_t comment_length = cdr->comment_length;
Piotr Jastrzebski78271ba2014-08-15 12:53:00 +0100484 const uint8_t* file_name = ptr + sizeof(CentralDirectoryRecord);
485
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700486 if (file_name_length >= cd_length || file_name > cd_end - file_name_length) {
487 ALOGW("Zip: file name for entry %" PRIu64
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700488 " exceeds the central directory range, file_name_length: %" PRIu16 ", cd_length: %zu",
489 i, file_name_length, cd_length);
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800490 return kInvalidEntryName;
Tianjie Xu9e020e22016-10-10 12:11:30 -0700491 }
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700492
493 const uint8_t* extra_field = file_name + file_name_length;
494 if (extra_length >= cd_length || extra_field > cd_end - extra_length) {
495 ALOGW("Zip: extra field for entry %" PRIu64
496 " exceeds the central directory range, file_name_length: %" PRIu16 ", cd_length: %zu",
497 i, extra_length, cd_length);
498 return kInvalidFile;
499 }
500
501 off64_t local_header_offset = cdr->local_file_header_offset;
502 if (local_header_offset == UINT32_MAX) {
Tianjie6ab29122020-03-18 17:44:30 -0700503 Zip64ExtendedInfo zip64_info{};
504 if (auto status = ParseZip64ExtendedInfoInExtraField(
505 extra_field, extra_length, cdr->uncompressed_size, cdr->compressed_size,
506 cdr->local_file_header_offset, &zip64_info);
507 status != kSuccess) {
508 return status;
509 }
510 CHECK(zip64_info.local_header_offset.has_value());
511 local_header_offset = zip64_info.local_header_offset.value();
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700512 }
513
514 if (local_header_offset >= archive->directory_offset) {
515 ALOGW("Zip: bad LFH offset %" PRId64 " at entry %" PRIu64,
516 static_cast<int64_t>(local_header_offset), i);
517 return kInvalidFile;
518 }
519
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700520 // Check that file name is valid UTF-8 and doesn't contain NUL (U+0000) characters.
Narayan Kamath044bc8e2014-12-03 18:22:53 +0000521 if (!IsValidEntryName(file_name, file_name_length)) {
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700522 ALOGW("Zip: invalid file name at entry %" PRIu64, i);
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800523 return kInvalidEntryName;
Piotr Jastrzebski78271ba2014-08-15 12:53:00 +0100524 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000525
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700526 // Add the CDE filename to the hash table.
527 std::string_view entry_name{reinterpret_cast<const char*>(file_name), file_name_length};
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800528 if (auto add_result =
529 archive->cd_entry_map->AddToMap(entry_name, archive->central_directory.GetBasePtr());
530 add_result != 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000531 ALOGW("Zip: Error adding entry to hash table %d", add_result);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800532 return add_result;
Narayan Kamath7462f022013-11-21 13:05:04 +0000533 }
534
Narayan Kamath926973e2014-06-09 14:18:14 +0100535 ptr += sizeof(CentralDirectoryRecord) + file_name_length + extra_length + comment_length;
536 if ((ptr - cd_ptr) > static_cast<int64_t>(cd_length)) {
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700537 ALOGW("Zip: bad CD advance (%tu vs %zu) at entry %" PRIu64, ptr - cd_ptr, cd_length, i);
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800538 return kInvalidFile;
Narayan Kamath7462f022013-11-21 13:05:04 +0000539 }
540 }
Narayan Kamathc1a56dc2017-08-09 18:32:09 +0100541
542 uint32_t lfh_start_bytes;
543 if (!archive->mapped_zip.ReadAtOffset(reinterpret_cast<uint8_t*>(&lfh_start_bytes),
544 sizeof(uint32_t), 0)) {
545 ALOGW("Zip: Unable to read header for entry at offset == 0.");
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800546 return kInvalidFile;
Narayan Kamathc1a56dc2017-08-09 18:32:09 +0100547 }
548
549 if (lfh_start_bytes != LocalFileHeader::kSignature) {
550 ALOGW("Zip: Entry at offset zero has invalid LFH signature %" PRIx32, lfh_start_bytes);
551#if defined(__ANDROID__)
552 android_errorWriteLog(0x534e4554, "64211847");
553#endif
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800554 return kInvalidFile;
Narayan Kamathc1a56dc2017-08-09 18:32:09 +0100555 }
556
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700557 ALOGV("+++ zip good scan %" PRIu64 " entries", num_entries);
Narayan Kamath7462f022013-11-21 13:05:04 +0000558
Tianjie6ab29122020-03-18 17:44:30 -0700559 return kSuccess;
Narayan Kamath7462f022013-11-21 13:05:04 +0000560}
561
Jiyong Parkcd997e62017-06-30 17:23:33 +0900562static int32_t OpenArchiveInternal(ZipArchive* archive, const char* debug_file_name) {
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800563 int32_t result = MapCentralDirectory(debug_file_name, archive);
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700564 return result != kSuccess ? result : ParseZipArchive(archive);
Narayan Kamath7462f022013-11-21 13:05:04 +0000565}
566
Jiyong Parkcd997e62017-06-30 17:23:33 +0900567int32_t OpenArchiveFd(int fd, const char* debug_file_name, ZipArchiveHandle* handle,
568 bool assume_ownership) {
Ryan Mitchell23150e42020-03-09 09:33:46 -0700569 ZipArchive* archive = new ZipArchive(MappedZipFile(fd), assume_ownership);
Narayan Kamath7462f022013-11-21 13:05:04 +0000570 *handle = archive;
Narayan Kamath7462f022013-11-21 13:05:04 +0000571 return OpenArchiveInternal(archive, debug_file_name);
572}
573
Ryan Mitchell23150e42020-03-09 09:33:46 -0700574int32_t OpenArchiveFdRange(int fd, const char* debug_file_name, ZipArchiveHandle* handle,
575 off64_t length, off64_t offset, bool assume_ownership) {
576 ZipArchive* archive = new ZipArchive(MappedZipFile(fd, length, offset), assume_ownership);
577 *handle = archive;
578
579 if (length < 0) {
580 ALOGW("Invalid zip length %" PRId64, length);
581 return kIoError;
582 }
583
584 if (offset < 0) {
585 ALOGW("Invalid zip offset %" PRId64, offset);
586 return kIoError;
587 }
588
589 return OpenArchiveInternal(archive, debug_file_name);
590}
591
Narayan Kamath7462f022013-11-21 13:05:04 +0000592int32_t OpenArchive(const char* fileName, ZipArchiveHandle* handle) {
Nick Kralevich3bdf7442018-12-18 12:48:06 -0800593 const int fd = ::android::base::utf8::open(fileName, O_RDONLY | O_BINARY | O_CLOEXEC, 0);
Ryan Mitchell23150e42020-03-09 09:33:46 -0700594 ZipArchive* archive = new ZipArchive(MappedZipFile(fd), true);
Narayan Kamath7462f022013-11-21 13:05:04 +0000595 *handle = archive;
596
Narayan Kamath7462f022013-11-21 13:05:04 +0000597 if (fd < 0) {
598 ALOGW("Unable to open '%s': %s", fileName, strerror(errno));
599 return kIoError;
Narayan Kamath7462f022013-11-21 13:05:04 +0000600 }
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700601
Narayan Kamath7462f022013-11-21 13:05:04 +0000602 return OpenArchiveInternal(archive, fileName);
603}
604
Elliott Hughesf66460b2019-10-22 11:44:50 -0700605int32_t OpenArchiveFromMemory(const void* address, size_t length, const char* debug_file_name,
Jiyong Parkcd997e62017-06-30 17:23:33 +0900606 ZipArchiveHandle* handle) {
Tianjie Xu18c25922016-09-29 15:27:41 -0700607 ZipArchive* archive = new ZipArchive(address, length);
608 *handle = archive;
609 return OpenArchiveInternal(archive, debug_file_name);
610}
611
Elliott Hughes26724132019-10-25 09:57:58 -0700612ZipArchiveInfo GetArchiveInfo(ZipArchiveHandle archive) {
613 ZipArchiveInfo result;
614 result.archive_size = archive->mapped_zip.GetFileLength();
615 result.entry_count = archive->num_entries;
616 return result;
617}
618
Narayan Kamath7462f022013-11-21 13:05:04 +0000619/*
620 * Close a ZipArchive, closing the file and freeing the contents.
621 */
Ryan Prichard3673f992018-10-10 22:41:14 -0700622void CloseArchive(ZipArchiveHandle archive) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000623 ALOGV("Closing archive %p", archive);
Neil Fullerb1a113f2014-07-25 14:43:04 +0100624 delete archive;
Narayan Kamath7462f022013-11-21 13:05:04 +0000625}
626
Narayan Kamath162b7052017-06-05 13:21:12 +0100627static int32_t ValidateDataDescriptor(MappedZipFile& mapped_zip, ZipEntry* entry) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100628 uint8_t ddBuf[sizeof(DataDescriptor) + sizeof(DataDescriptor::kOptSignature)];
Adam Lesinskide117e42017-06-19 10:27:38 -0700629 off64_t offset = entry->offset;
630 if (entry->method != kCompressStored) {
631 offset += entry->compressed_length;
632 } else {
633 offset += entry->uncompressed_length;
634 }
635
636 if (!mapped_zip.ReadAtOffset(ddBuf, sizeof(ddBuf), offset)) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000637 return kIoError;
638 }
639
Narayan Kamath926973e2014-06-09 14:18:14 +0100640 const uint32_t ddSignature = *(reinterpret_cast<const uint32_t*>(ddBuf));
Adam Lesinskide117e42017-06-19 10:27:38 -0700641 const uint16_t ddOffset = (ddSignature == DataDescriptor::kOptSignature) ? 4 : 0;
642 const DataDescriptor* descriptor = reinterpret_cast<const DataDescriptor*>(ddBuf + ddOffset);
Narayan Kamath7462f022013-11-21 13:05:04 +0000643
Narayan Kamath162b7052017-06-05 13:21:12 +0100644 // Validate that the values in the data descriptor match those in the central
645 // directory.
646 if (entry->compressed_length != descriptor->compressed_size ||
647 entry->uncompressed_length != descriptor->uncompressed_size ||
648 entry->crc32 != descriptor->crc32) {
649 ALOGW("Zip: size/crc32 mismatch. expected {%" PRIu32 ", %" PRIu32 ", %" PRIx32
650 "}, was {%" PRIu32 ", %" PRIu32 ", %" PRIx32 "}",
651 entry->compressed_length, entry->uncompressed_length, entry->crc32,
652 descriptor->compressed_size, descriptor->uncompressed_size, descriptor->crc32);
653 return kInconsistentInformation;
654 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000655
656 return 0;
657}
658
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800659static int32_t FindEntry(const ZipArchive* archive, std::string_view entryName,
660 const uint64_t nameOffset, ZipEntry* data) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000661 // Recover the start of the central directory entry from the filename
662 // pointer. The filename is the first entry past the fixed-size data,
663 // so we can just subtract back from that.
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700664 const uint8_t* base_ptr = archive->central_directory.GetBasePtr();
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800665 const uint8_t* ptr = base_ptr + nameOffset;
Narayan Kamath926973e2014-06-09 14:18:14 +0100666 ptr -= sizeof(CentralDirectoryRecord);
Narayan Kamath7462f022013-11-21 13:05:04 +0000667
668 // This is the base of our mmapped region, we have to sanity check that
669 // the name that's in the hash table is a pointer to a location within
670 // this mapped region.
Tianjie Xu18c25922016-09-29 15:27:41 -0700671 if (ptr < base_ptr || ptr > base_ptr + archive->central_directory.GetMapLength()) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000672 ALOGW("Zip: Invalid entry pointer");
673 return kInvalidOffset;
674 }
675
Tianjie6ab29122020-03-18 17:44:30 -0700676 auto cdr = reinterpret_cast<const CentralDirectoryRecord*>(ptr);
Narayan Kamath926973e2014-06-09 14:18:14 +0100677
Narayan Kamath7462f022013-11-21 13:05:04 +0000678 // The offset of the start of the central directory in the zipfile.
679 // We keep this lying around so that we can sanity check all our lengths
680 // and our per-file structures.
681 const off64_t cd_offset = archive->directory_offset;
682
683 // Fill out the compression method, modification time, crc32
684 // and other interesting attributes from the central directory. These
685 // will later be compared against values from the local file header.
Narayan Kamath926973e2014-06-09 14:18:14 +0100686 data->method = cdr->compression_method;
beonit0e99a2f2015-07-18 02:08:16 +0900687 data->mod_time = cdr->last_mod_date << 16 | cdr->last_mod_time;
Narayan Kamath926973e2014-06-09 14:18:14 +0100688 data->crc32 = cdr->crc32;
689 data->compressed_length = cdr->compressed_size;
690 data->uncompressed_length = cdr->uncompressed_size;
Narayan Kamath7462f022013-11-21 13:05:04 +0000691
692 // Figure out the local header offset from the central directory. The
693 // actual file data will begin after the local header and the name /
694 // extra comments.
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700695 off64_t local_header_offset = cdr->local_file_header_offset;
696 // One of the info field is UINT32_MAX, try to parse the real value in the zip64 extended info in
697 // the extra field.
698 if (cdr->uncompressed_size == UINT32_MAX || cdr->compressed_size == UINT32_MAX ||
699 cdr->local_file_header_offset == UINT32_MAX) {
Tianjie6ab29122020-03-18 17:44:30 -0700700 const uint8_t* extra_field = ptr + sizeof(CentralDirectoryRecord) + cdr->file_name_length;
701 Zip64ExtendedInfo zip64_info{};
702 if (auto status = ParseZip64ExtendedInfoInExtraField(
703 extra_field, cdr->extra_field_length, cdr->uncompressed_size, cdr->compressed_size,
704 cdr->local_file_header_offset, &zip64_info);
705 status != kSuccess) {
706 return status;
707 }
708
709 if (cdr->uncompressed_size == UINT32_MAX || cdr->compressed_size == UINT32_MAX) {
710 CHECK(zip64_info.uncompressed_file_size.has_value());
711 CHECK(zip64_info.compressed_file_size.has_value());
712 // TODO(xunchang) remove the size limit and support entry length > UINT32_MAX.
713 data->uncompressed_length = static_cast<uint32_t>(zip64_info.uncompressed_file_size.value());
714 data->compressed_length = static_cast<uint32_t>(zip64_info.compressed_file_size.value());
715 }
716
717 if (local_header_offset == UINT32_MAX) {
718 CHECK(zip64_info.local_header_offset.has_value());
719 local_header_offset = zip64_info.local_header_offset.value();
720 }
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700721 }
722
Narayan Kamath926973e2014-06-09 14:18:14 +0100723 if (local_header_offset + static_cast<off64_t>(sizeof(LocalFileHeader)) >= cd_offset) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000724 ALOGW("Zip: bad local hdr offset in zip");
725 return kInvalidOffset;
726 }
727
Narayan Kamath926973e2014-06-09 14:18:14 +0100728 uint8_t lfh_buf[sizeof(LocalFileHeader)];
Tianjie Xu18c25922016-09-29 15:27:41 -0700729 if (!archive->mapped_zip.ReadAtOffset(lfh_buf, sizeof(lfh_buf), local_header_offset)) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800730 ALOGW("Zip: failed reading lfh name from offset %" PRId64,
Jiyong Parkcd997e62017-06-30 17:23:33 +0900731 static_cast<int64_t>(local_header_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000732 return kIoError;
733 }
734
Tianjie6ab29122020-03-18 17:44:30 -0700735 auto lfh = reinterpret_cast<const LocalFileHeader*>(lfh_buf);
Narayan Kamath926973e2014-06-09 14:18:14 +0100736 if (lfh->lfh_signature != LocalFileHeader::kSignature) {
Mark Salyzyn99ef9912014-03-14 14:26:22 -0700737 ALOGW("Zip: didn't find signature at start of lfh, offset=%" PRId64,
Jiyong Parkcd997e62017-06-30 17:23:33 +0900738 static_cast<int64_t>(local_header_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000739 return kInvalidOffset;
740 }
741
Tianjie6ab29122020-03-18 17:44:30 -0700742 // Check that the local file header name matches the declared name in the central directory.
743 CHECK_LE(entryName.size(), UINT16_MAX);
744 auto nameLen = static_cast<uint16_t>(entryName.size());
745 if (lfh->file_name_length != nameLen) {
746 ALOGW("Zip: lfh name length did not match central directory for %s: %" PRIu16 " %" PRIu16,
747 std::string(entryName).c_str(), lfh->file_name_length, nameLen);
748 return kInconsistentInformation;
749 }
750 const off64_t name_offset = local_header_offset + sizeof(LocalFileHeader);
751 if (name_offset > cd_offset - lfh->file_name_length) {
752 ALOGW("Zip: lfh name has invalid declared length");
753 return kInvalidOffset;
754 }
755
756 std::vector<uint8_t> name_buf(nameLen);
757 if (!archive->mapped_zip.ReadAtOffset(name_buf.data(), nameLen, name_offset)) {
758 ALOGW("Zip: failed reading lfh name from offset %" PRId64, static_cast<int64_t>(name_offset));
759 return kIoError;
760 }
761 if (memcmp(entryName.data(), name_buf.data(), nameLen) != 0) {
762 ALOGW("Zip: lfh name did not match central directory");
763 return kInconsistentInformation;
764 }
765
766 uint64_t lfh_uncompressed_size = lfh->uncompressed_size;
767 uint64_t lfh_compressed_size = lfh->compressed_size;
768 if (lfh_uncompressed_size == UINT32_MAX || lfh_compressed_size == UINT32_MAX) {
769 const off64_t lfh_extra_field_offset = name_offset + lfh->file_name_length;
770 const uint16_t lfh_extra_field_size = lfh->extra_field_length;
771 if (lfh_extra_field_offset > cd_offset - lfh_extra_field_size) {
772 ALOGW("Zip: extra field has a bad size for entry %s", std::string(entryName).c_str());
773 return kInvalidOffset;
774 }
775
776 std::vector<uint8_t> local_extra_field(lfh_extra_field_size);
777 if (!archive->mapped_zip.ReadAtOffset(local_extra_field.data(), lfh_extra_field_size,
778 lfh_extra_field_offset)) {
779 ALOGW("Zip: failed reading lfh extra field from offset %" PRId64, lfh_extra_field_offset);
780 return kIoError;
781 }
782
783 Zip64ExtendedInfo zip64_info{};
784 if (auto status = ParseZip64ExtendedInfoInExtraField(
785 local_extra_field.data(), lfh_extra_field_size, lfh->uncompressed_size,
786 lfh->compressed_size, std::nullopt, &zip64_info);
787 status != kSuccess) {
788 return status;
789 }
790
791 CHECK(zip64_info.uncompressed_file_size.has_value());
792 CHECK(zip64_info.compressed_file_size.has_value());
793 lfh_uncompressed_size = zip64_info.uncompressed_file_size.value();
794 lfh_compressed_size = zip64_info.compressed_file_size.value();
795 }
796
Narayan Kamath7462f022013-11-21 13:05:04 +0000797 // Paranoia: Match the values specified in the local file header
798 // to those specified in the central directory.
Adam Lesinskid987c9d2017-04-06 18:55:47 -0700799
Narayan Kamath162b7052017-06-05 13:21:12 +0100800 // Warn if central directory and local file header don't agree on the use
801 // of a trailing Data Descriptor. The reference implementation is inconsistent
802 // and appears to use the LFH value during extraction (unzip) but the CD value
803 // while displayng information about archives (zipinfo). The spec remains
804 // silent on this inconsistency as well.
805 //
806 // For now, always use the version from the LFH but make sure that the values
807 // specified in the central directory match those in the data descriptor.
808 //
809 // NOTE: It's also worth noting that unzip *does* warn about inconsistencies in
810 // bit 11 (EFS: The language encoding flag, marking that filename and comment are
811 // encoded using UTF-8). This implementation does not check for the presence of
812 // that flag and always enforces that entry names are valid UTF-8.
813 if ((lfh->gpb_flags & kGPBDDFlagMask) != (cdr->gpb_flags & kGPBDDFlagMask)) {
814 ALOGW("Zip: gpb flag mismatch at bit 3. expected {%04" PRIx16 "}, was {%04" PRIx16 "}",
Adam Lesinskid987c9d2017-04-06 18:55:47 -0700815 cdr->gpb_flags, lfh->gpb_flags);
Adam Lesinskid987c9d2017-04-06 18:55:47 -0700816 }
817
818 // If there is no trailing data descriptor, verify that the central directory and local file
819 // header agree on the crc, compressed, and uncompressed sizes of the entry.
Narayan Kamath926973e2014-06-09 14:18:14 +0100820 if ((lfh->gpb_flags & kGPBDDFlagMask) == 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000821 data->has_data_descriptor = 0;
Tianjie6ab29122020-03-18 17:44:30 -0700822 if (data->compressed_length != lfh_compressed_size ||
823 data->uncompressed_length != lfh_uncompressed_size || data->crc32 != lfh->crc32) {
Jiyong Parkcd997e62017-06-30 17:23:33 +0900824 ALOGW("Zip: size/crc32 mismatch. expected {%" PRIu32 ", %" PRIu32 ", %" PRIx32
Tianjie6ab29122020-03-18 17:44:30 -0700825 "}, was {%" PRIu64 ", %" PRIu64 ", %" PRIx32 "}",
826 data->compressed_length, data->uncompressed_length, data->crc32, lfh_compressed_size,
827 lfh_uncompressed_size, lfh->crc32);
Narayan Kamath7462f022013-11-21 13:05:04 +0000828 return kInconsistentInformation;
829 }
830 } else {
831 data->has_data_descriptor = 1;
832 }
833
Elliott Hughes55fd2932017-05-28 22:59:04 -0700834 // 4.4.2.1: the upper byte of `version_made_by` gives the source OS. Unix is 3.
Elliott Hughes26724132019-10-25 09:57:58 -0700835 data->version_made_by = cdr->version_made_by;
Elliott Hughesd5095252019-10-28 21:35:52 -0700836 data->external_file_attributes = cdr->external_file_attributes;
Elliott Hughes26724132019-10-25 09:57:58 -0700837 if ((data->version_made_by >> 8) == 3) {
Elliott Hughes55fd2932017-05-28 22:59:04 -0700838 data->unix_mode = (cdr->external_file_attributes >> 16) & 0xffff;
839 } else {
840 data->unix_mode = 0777;
841 }
842
Elliott Hughesd5095252019-10-28 21:35:52 -0700843 // 4.4.4: general purpose bit flags.
844 data->gpbf = lfh->gpb_flags;
845
Elliott Hughes26724132019-10-25 09:57:58 -0700846 // 4.4.14: the lowest bit of the internal file attributes field indicates text.
847 // Currently only needed to implement zipinfo.
848 data->is_text = (cdr->internal_file_attributes & 1);
849
Jiyong Parkcd997e62017-06-30 17:23:33 +0900850 const off64_t data_offset = local_header_offset + sizeof(LocalFileHeader) +
851 lfh->file_name_length + lfh->extra_field_length;
Narayan Kamath48953a12014-01-24 12:32:39 +0000852 if (data_offset > cd_offset) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800853 ALOGW("Zip: bad data offset %" PRId64 " in zip", static_cast<int64_t>(data_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000854 return kInvalidOffset;
855 }
856
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800857 if (static_cast<off64_t>(data_offset + data->compressed_length) > cd_offset) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700858 ALOGW("Zip: bad compressed length in zip (%" PRId64 " + %" PRIu32 " > %" PRId64 ")",
Jiyong Parkcd997e62017-06-30 17:23:33 +0900859 static_cast<int64_t>(data_offset), data->compressed_length,
860 static_cast<int64_t>(cd_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000861 return kInvalidOffset;
862 }
863
864 if (data->method == kCompressStored &&
Jiyong Parkcd997e62017-06-30 17:23:33 +0900865 static_cast<off64_t>(data_offset + data->uncompressed_length) > cd_offset) {
866 ALOGW("Zip: bad uncompressed length in zip (%" PRId64 " + %" PRIu32 " > %" PRId64 ")",
867 static_cast<int64_t>(data_offset), data->uncompressed_length,
868 static_cast<int64_t>(cd_offset));
869 return kInvalidOffset;
Narayan Kamath7462f022013-11-21 13:05:04 +0000870 }
871
872 data->offset = data_offset;
873 return 0;
874}
875
876struct IterationHandle {
Narayan Kamath7462f022013-11-21 13:05:04 +0000877 ZipArchive* archive;
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100878
Songchun Fanc33f5262020-03-24 09:15:51 -0700879 std::function<bool(std::string_view)> matcher;
Elliott Hughesa22ac0f2019-05-08 10:44:06 -0700880
881 uint32_t position = 0;
882
Songchun Fanc33f5262020-03-24 09:15:51 -0700883 IterationHandle(ZipArchive* archive, std::function<bool(std::string_view)> in_matcher)
884 : archive(archive), matcher(std::move(in_matcher)) {}
885
886 bool Match(std::string_view entry_name) const { return matcher(entry_name); }
Narayan Kamath7462f022013-11-21 13:05:04 +0000887};
888
Ryan Prichard3673f992018-10-10 22:41:14 -0700889int32_t StartIteration(ZipArchiveHandle archive, void** cookie_ptr,
Elliott Hughesa22ac0f2019-05-08 10:44:06 -0700890 const std::string_view optional_prefix,
891 const std::string_view optional_suffix) {
Elliott Hughesa22ac0f2019-05-08 10:44:06 -0700892 if (optional_prefix.size() > static_cast<size_t>(UINT16_MAX) ||
893 optional_suffix.size() > static_cast<size_t>(UINT16_MAX)) {
894 ALOGW("Zip: prefix/suffix too long");
895 return kInvalidEntryName;
896 }
Songchun Fanc33f5262020-03-24 09:15:51 -0700897 auto matcher = [prefix = std::string(optional_prefix),
898 suffix = std::string(optional_suffix)](std::string_view name) mutable {
899 return android::base::StartsWith(name, prefix) && android::base::EndsWith(name, suffix);
900 };
901 return StartIteration(archive, cookie_ptr, std::move(matcher));
902}
903
904int32_t StartIteration(ZipArchiveHandle archive, void** cookie_ptr,
905 std::function<bool(std::string_view)> matcher) {
906 if (archive == nullptr || archive->cd_entry_map == nullptr) {
907 ALOGW("Zip: Invalid ZipArchiveHandle");
908 return kInvalidHandle;
909 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000910
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800911 archive->cd_entry_map->ResetIteration();
Songchun Fanc33f5262020-03-24 09:15:51 -0700912 *cookie_ptr = new IterationHandle(archive, matcher);
Narayan Kamath7462f022013-11-21 13:05:04 +0000913 return 0;
914}
915
Piotr Jastrzebski79c8b342014-08-08 14:02:17 +0100916void EndIteration(void* cookie) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100917 delete reinterpret_cast<IterationHandle*>(cookie);
Piotr Jastrzebski79c8b342014-08-08 14:02:17 +0100918}
919
Elliott Hughesb17bf522019-05-03 22:38:44 -0700920int32_t FindEntry(const ZipArchiveHandle archive, const std::string_view entryName,
921 ZipEntry* data) {
922 if (entryName.empty() || entryName.size() > static_cast<size_t>(UINT16_MAX)) {
923 ALOGW("Zip: Invalid filename of length %zu", entryName.size());
924 return kInvalidEntryName;
925 }
926
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800927 const auto [result, offset] =
928 archive->cd_entry_map->GetCdEntryOffset(entryName, archive->central_directory.GetBasePtr());
929 if (result != 0) {
Elliott Hughesb17bf522019-05-03 22:38:44 -0700930 ALOGV("Zip: Could not find entry %.*s", static_cast<int>(entryName.size()), entryName.data());
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800931 return static_cast<int32_t>(result); // kEntryNotFound is safe to truncate.
Elliott Hughesb17bf522019-05-03 22:38:44 -0700932 }
Elliott Hughesa5ff19e2019-05-07 09:27:59 -0700933 // We know there are at most hash_table_size entries, safe to truncate.
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800934 return FindEntry(archive, entryName, offset, data);
Elliott Hughesb17bf522019-05-03 22:38:44 -0700935}
936
Elliott Hughese06a8082019-05-22 18:56:41 -0700937int32_t Next(void* cookie, ZipEntry* data, std::string* name) {
Elliott Hughes1e40c302019-06-12 12:12:47 -0700938 std::string_view sv;
939 int32_t result = Next(cookie, data, &sv);
940 if (result == 0 && name) {
941 *name = std::string(sv);
942 }
943 return result;
944}
945
946int32_t Next(void* cookie, ZipEntry* data, std::string_view* name) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800947 IterationHandle* handle = reinterpret_cast<IterationHandle*>(cookie);
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800948 if (handle == nullptr) {
Zimuzo5a503ef2018-09-17 19:49:55 +0100949 ALOGW("Zip: Null ZipArchiveHandle");
Narayan Kamath7462f022013-11-21 13:05:04 +0000950 return kInvalidHandle;
951 }
952
953 ZipArchive* archive = handle->archive;
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800954 if (archive == nullptr || archive->cd_entry_map == nullptr) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000955 ALOGW("Zip: Invalid ZipArchiveHandle");
956 return kInvalidHandle;
957 }
958
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800959 auto entry = archive->cd_entry_map->Next(archive->central_directory.GetBasePtr());
960 while (entry != std::pair<std::string_view, uint64_t>()) {
961 const auto [entry_name, offset] = entry;
Songchun Fanc33f5262020-03-24 09:15:51 -0700962 if (handle->Match(entry_name)) {
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800963 const int error = FindEntry(archive, entry_name, offset, data);
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700964 if (!error && name) {
965 *name = entry_name;
Narayan Kamath7462f022013-11-21 13:05:04 +0000966 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000967 return error;
968 }
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800969 entry = archive->cd_entry_map->Next(archive->central_directory.GetBasePtr());
Narayan Kamath7462f022013-11-21 13:05:04 +0000970 }
971
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800972 archive->cd_entry_map->ResetIteration();
Narayan Kamath7462f022013-11-21 13:05:04 +0000973 return kIterationEnd;
974}
975
Narayan Kamathf899bd52015-04-17 11:53:14 +0100976// A Writer that writes data to a fixed size memory region.
977// The size of the memory region must be equal to the total size of
978// the data appended to it.
Narayan Kamath485b3642017-10-26 14:42:39 +0100979class MemoryWriter : public zip_archive::Writer {
Narayan Kamathf899bd52015-04-17 11:53:14 +0100980 public:
Jiyong Parkcd997e62017-06-30 17:23:33 +0900981 MemoryWriter(uint8_t* buf, size_t size) : Writer(), buf_(buf), size_(size), bytes_written_(0) {}
Narayan Kamathf899bd52015-04-17 11:53:14 +0100982
983 virtual bool Append(uint8_t* buf, size_t buf_size) override {
984 if (bytes_written_ + buf_size > size_) {
Elliott Hughese8f4b142018-10-19 16:09:39 -0700985 ALOGW("Zip: Unexpected size %zu (declared) vs %zu (actual)", size_,
Jiyong Parkcd997e62017-06-30 17:23:33 +0900986 bytes_written_ + buf_size);
Narayan Kamathf899bd52015-04-17 11:53:14 +0100987 return false;
988 }
989
990 memcpy(buf_ + bytes_written_, buf, buf_size);
991 bytes_written_ += buf_size;
992 return true;
993 }
994
995 private:
996 uint8_t* const buf_;
997 const size_t size_;
998 size_t bytes_written_;
999};
1000
1001// A Writer that appends data to a file |fd| at its current position.
1002// The file will be truncated to the end of the written data.
Narayan Kamath485b3642017-10-26 14:42:39 +01001003class FileWriter : public zip_archive::Writer {
Narayan Kamathf899bd52015-04-17 11:53:14 +01001004 public:
Narayan Kamathf899bd52015-04-17 11:53:14 +01001005 // Creates a FileWriter for |fd| and prepare to write |entry| to it,
1006 // guaranteeing that the file descriptor is valid and that there's enough
1007 // space on the volume to write out the entry completely and that the file
Tao Baoa456c212016-11-15 10:08:07 -08001008 // is truncated to the correct length (no truncation if |fd| references a
1009 // block device).
Narayan Kamathf899bd52015-04-17 11:53:14 +01001010 //
1011 // Returns a valid FileWriter on success, |nullptr| if an error occurred.
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -08001012 static FileWriter Create(int fd, const ZipEntry* entry) {
Narayan Kamathf899bd52015-04-17 11:53:14 +01001013 const uint32_t declared_length = entry->uncompressed_length;
1014 const off64_t current_offset = lseek64(fd, 0, SEEK_CUR);
1015 if (current_offset == -1) {
1016 ALOGW("Zip: unable to seek to current location on fd %d: %s", fd, strerror(errno));
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -08001017 return FileWriter{};
Narayan Kamathf899bd52015-04-17 11:53:14 +01001018 }
1019
Narayan Kamathf899bd52015-04-17 11:53:14 +01001020#if defined(__linux__)
1021 if (declared_length > 0) {
1022 // Make sure we have enough space on the volume to extract the compressed
1023 // entry. Note that the call to ftruncate below will change the file size but
1024 // will not allocate space on disk and this call to fallocate will not
1025 // change the file size.
Badhri Jagan Sridharana68d0d12015-06-02 14:47:57 -07001026 // Note: fallocate is only supported by the following filesystems -
1027 // btrfs, ext4, ocfs2, and xfs. Therefore fallocate might fail with
1028 // EOPNOTSUPP error when issued in other filesystems.
1029 // Hence, check for the return error code before concluding that the
1030 // disk does not have enough space.
Andreas Gampe964b95c2019-04-05 13:48:02 -07001031 long result = TEMP_FAILURE_RETRY(fallocate(fd, 0, current_offset, declared_length));
Badhri Jagan Sridharana68d0d12015-06-02 14:47:57 -07001032 if (result == -1 && errno == ENOSPC) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001033 ALOGW("Zip: unable to allocate %" PRId64 " bytes at offset %" PRId64 ": %s",
Narayan Kamathd5d7abe2016-08-10 12:24:05 +01001034 static_cast<int64_t>(declared_length), static_cast<int64_t>(current_offset),
1035 strerror(errno));
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -08001036 return FileWriter{};
Narayan Kamathf899bd52015-04-17 11:53:14 +01001037 }
1038 }
1039#endif // __linux__
1040
Tao Baoa456c212016-11-15 10:08:07 -08001041 struct stat sb;
1042 if (fstat(fd, &sb) == -1) {
1043 ALOGW("Zip: unable to fstat file: %s", strerror(errno));
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -08001044 return FileWriter{};
Narayan Kamathf899bd52015-04-17 11:53:14 +01001045 }
1046
Tao Baoa456c212016-11-15 10:08:07 -08001047 // Block device doesn't support ftruncate(2).
1048 if (!S_ISBLK(sb.st_mode)) {
Andreas Gampe964b95c2019-04-05 13:48:02 -07001049 long result = TEMP_FAILURE_RETRY(ftruncate(fd, declared_length + current_offset));
Tao Baoa456c212016-11-15 10:08:07 -08001050 if (result == -1) {
1051 ALOGW("Zip: unable to truncate file to %" PRId64 ": %s",
1052 static_cast<int64_t>(declared_length + current_offset), strerror(errno));
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -08001053 return FileWriter{};
Tao Baoa456c212016-11-15 10:08:07 -08001054 }
1055 }
1056
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -08001057 return FileWriter(fd, declared_length);
Narayan Kamathf899bd52015-04-17 11:53:14 +01001058 }
1059
Chih-Hung Hsieh747eb142018-09-25 11:16:22 -07001060 FileWriter(FileWriter&& other) noexcept
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -08001061 : fd_(other.fd_),
1062 declared_length_(other.declared_length_),
1063 total_bytes_written_(other.total_bytes_written_) {
1064 other.fd_ = -1;
1065 }
1066
1067 bool IsValid() const { return fd_ != -1; }
1068
Narayan Kamathf899bd52015-04-17 11:53:14 +01001069 virtual bool Append(uint8_t* buf, size_t buf_size) override {
1070 if (total_bytes_written_ + buf_size > declared_length_) {
Elliott Hughese8f4b142018-10-19 16:09:39 -07001071 ALOGW("Zip: Unexpected size %zu (declared) vs %zu (actual)", declared_length_,
Jiyong Parkcd997e62017-06-30 17:23:33 +09001072 total_bytes_written_ + buf_size);
Narayan Kamathf899bd52015-04-17 11:53:14 +01001073 return false;
1074 }
1075
Narayan Kamathe97e66e2015-04-27 16:25:53 +01001076 const bool result = android::base::WriteFully(fd_, buf, buf_size);
1077 if (result) {
1078 total_bytes_written_ += buf_size;
1079 } else {
Elliott Hughese8f4b142018-10-19 16:09:39 -07001080 ALOGW("Zip: unable to write %zu bytes to file; %s", buf_size, strerror(errno));
Narayan Kamathf899bd52015-04-17 11:53:14 +01001081 }
1082
Narayan Kamathe97e66e2015-04-27 16:25:53 +01001083 return result;
Narayan Kamathf899bd52015-04-17 11:53:14 +01001084 }
Jiyong Parkcd997e62017-06-30 17:23:33 +09001085
Narayan Kamathf899bd52015-04-17 11:53:14 +01001086 private:
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -08001087 explicit FileWriter(const int fd = -1, const size_t declared_length = 0)
Jiyong Parkcd997e62017-06-30 17:23:33 +09001088 : Writer(), fd_(fd), declared_length_(declared_length), total_bytes_written_(0) {}
Narayan Kamathf899bd52015-04-17 11:53:14 +01001089
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -08001090 int fd_;
Narayan Kamathf899bd52015-04-17 11:53:14 +01001091 const size_t declared_length_;
1092 size_t total_bytes_written_;
1093};
1094
Narayan Kamath485b3642017-10-26 14:42:39 +01001095class EntryReader : public zip_archive::Reader {
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001096 public:
1097 EntryReader(const MappedZipFile& zip_file, const ZipEntry* entry)
1098 : Reader(), zip_file_(zip_file), entry_(entry) {}
1099
1100 virtual bool ReadAtOffset(uint8_t* buf, size_t len, uint32_t offset) const {
1101 return zip_file_.ReadAtOffset(buf, len, entry_->offset + offset);
1102 }
1103
1104 virtual ~EntryReader() {}
1105
1106 private:
1107 const MappedZipFile& zip_file_;
1108 const ZipEntry* entry_;
1109};
1110
Dmitriy Ivanovf94e1592015-03-06 13:27:59 -08001111// This method is using libz macros with old-style-casts
1112#pragma GCC diagnostic push
1113#pragma GCC diagnostic ignored "-Wold-style-cast"
1114static inline int zlib_inflateInit2(z_stream* stream, int window_bits) {
1115 return inflateInit2(stream, window_bits);
1116}
1117#pragma GCC diagnostic pop
1118
Narayan Kamath485b3642017-10-26 14:42:39 +01001119namespace zip_archive {
1120
1121// Moved out of line to avoid -Wweak-vtables.
1122Reader::~Reader() {}
1123Writer::~Writer() {}
1124
1125int32_t Inflate(const Reader& reader, const uint32_t compressed_length,
1126 const uint32_t uncompressed_length, Writer* writer, uint64_t* crc_out) {
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -07001127 const size_t kBufSize = 32768;
1128 std::vector<uint8_t> read_buf(kBufSize);
1129 std::vector<uint8_t> write_buf(kBufSize);
Narayan Kamath7462f022013-11-21 13:05:04 +00001130 z_stream zstream;
1131 int zerr;
1132
1133 /*
1134 * Initialize the zlib stream struct.
1135 */
1136 memset(&zstream, 0, sizeof(zstream));
1137 zstream.zalloc = Z_NULL;
1138 zstream.zfree = Z_NULL;
1139 zstream.opaque = Z_NULL;
1140 zstream.next_in = NULL;
1141 zstream.avail_in = 0;
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -07001142 zstream.next_out = &write_buf[0];
Narayan Kamath7462f022013-11-21 13:05:04 +00001143 zstream.avail_out = kBufSize;
1144 zstream.data_type = Z_UNKNOWN;
1145
1146 /*
1147 * Use the undocumented "negative window bits" feature to tell zlib
1148 * that there's no zlib header waiting for it.
1149 */
Dmitriy Ivanovf94e1592015-03-06 13:27:59 -08001150 zerr = zlib_inflateInit2(&zstream, -MAX_WBITS);
Narayan Kamath7462f022013-11-21 13:05:04 +00001151 if (zerr != Z_OK) {
1152 if (zerr == Z_VERSION_ERROR) {
Jiyong Parkcd997e62017-06-30 17:23:33 +09001153 ALOGE("Installed zlib is not compatible with linked version (%s)", ZLIB_VERSION);
Narayan Kamath7462f022013-11-21 13:05:04 +00001154 } else {
1155 ALOGW("Call to inflateInit2 failed (zerr=%d)", zerr);
1156 }
1157
1158 return kZlibError;
1159 }
1160
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001161 auto zstream_deleter = [](z_stream* stream) {
Jiyong Parkcd997e62017-06-30 17:23:33 +09001162 inflateEnd(stream); /* free up any allocated structures */
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001163 };
1164
1165 std::unique_ptr<z_stream, decltype(zstream_deleter)> zstream_guard(&zstream, zstream_deleter);
1166
Narayan Kamath2d1e23f2017-10-30 11:17:28 +00001167 const bool compute_crc = (crc_out != nullptr);
Andreas Gampe964b95c2019-04-05 13:48:02 -07001168 uLong crc = 0;
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001169 uint32_t remaining_bytes = compressed_length;
Narayan Kamath7462f022013-11-21 13:05:04 +00001170 do {
1171 /* read as much as we can */
1172 if (zstream.avail_in == 0) {
Andreas Gampe964b95c2019-04-05 13:48:02 -07001173 const uint32_t read_size = (remaining_bytes > kBufSize) ? kBufSize : remaining_bytes;
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001174 const uint32_t offset = (compressed_length - remaining_bytes);
Adam Lesinskide117e42017-06-19 10:27:38 -07001175 // Make sure to read at offset to ensure concurrent access to the fd.
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001176 if (!reader.ReadAtOffset(read_buf.data(), read_size, offset)) {
Andreas Gampe964b95c2019-04-05 13:48:02 -07001177 ALOGW("Zip: inflate read failed, getSize = %u: %s", read_size, strerror(errno));
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001178 return kIoError;
Narayan Kamath7462f022013-11-21 13:05:04 +00001179 }
1180
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001181 remaining_bytes -= read_size;
Narayan Kamath7462f022013-11-21 13:05:04 +00001182
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -07001183 zstream.next_in = &read_buf[0];
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001184 zstream.avail_in = read_size;
Narayan Kamath7462f022013-11-21 13:05:04 +00001185 }
1186
1187 /* uncompress the data */
1188 zerr = inflate(&zstream, Z_NO_FLUSH);
1189 if (zerr != Z_OK && zerr != Z_STREAM_END) {
Jiyong Parkcd997e62017-06-30 17:23:33 +09001190 ALOGW("Zip: inflate zerr=%d (nIn=%p aIn=%u nOut=%p aOut=%u)", zerr, zstream.next_in,
1191 zstream.avail_in, zstream.next_out, zstream.avail_out);
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001192 return kZlibError;
Narayan Kamath7462f022013-11-21 13:05:04 +00001193 }
1194
1195 /* write when we're full or when we're done */
Jiyong Parkcd997e62017-06-30 17:23:33 +09001196 if (zstream.avail_out == 0 || (zerr == Z_STREAM_END && zstream.avail_out != kBufSize)) {
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -07001197 const size_t write_size = zstream.next_out - &write_buf[0];
Narayan Kamathf899bd52015-04-17 11:53:14 +01001198 if (!writer->Append(&write_buf[0], write_size)) {
Narayan Kamath2d1e23f2017-10-30 11:17:28 +00001199 return kIoError;
1200 } else if (compute_crc) {
Andreas Gampe964b95c2019-04-05 13:48:02 -07001201 DCHECK_LE(write_size, kBufSize);
1202 crc = crc32(crc, &write_buf[0], static_cast<uint32_t>(write_size));
Narayan Kamath7462f022013-11-21 13:05:04 +00001203 }
Narayan Kamath7462f022013-11-21 13:05:04 +00001204
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -07001205 zstream.next_out = &write_buf[0];
Narayan Kamath7462f022013-11-21 13:05:04 +00001206 zstream.avail_out = kBufSize;
1207 }
1208 } while (zerr == Z_OK);
1209
Elliott Hughese8f4b142018-10-19 16:09:39 -07001210 CHECK_EQ(zerr, Z_STREAM_END); /* other errors should've been caught */
Narayan Kamath7462f022013-11-21 13:05:04 +00001211
Narayan Kamath162b7052017-06-05 13:21:12 +01001212 // NOTE: zstream.adler is always set to 0, because we're using the -MAX_WBITS
1213 // "feature" of zlib to tell it there won't be a zlib file header. zlib
1214 // doesn't bother calculating the checksum in that scenario. We just do
1215 // it ourselves above because there are no additional gains to be made by
1216 // having zlib calculate it for us, since they do it by calling crc32 in
1217 // the same manner that we have above.
Narayan Kamath2d1e23f2017-10-30 11:17:28 +00001218 if (compute_crc) {
1219 *crc_out = crc;
1220 }
Narayan Kamath7462f022013-11-21 13:05:04 +00001221
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001222 if (zstream.total_out != uncompressed_length || remaining_bytes != 0) {
Jiyong Parkcd997e62017-06-30 17:23:33 +09001223 ALOGW("Zip: size mismatch on inflated file (%lu vs %" PRIu32 ")", zstream.total_out,
1224 uncompressed_length);
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001225 return kInconsistentInformation;
Narayan Kamath7462f022013-11-21 13:05:04 +00001226 }
1227
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001228 return 0;
Narayan Kamath7462f022013-11-21 13:05:04 +00001229}
Narayan Kamath485b3642017-10-26 14:42:39 +01001230} // namespace zip_archive
Narayan Kamath7462f022013-11-21 13:05:04 +00001231
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001232static int32_t InflateEntryToWriter(MappedZipFile& mapped_zip, const ZipEntry* entry,
Narayan Kamath485b3642017-10-26 14:42:39 +01001233 zip_archive::Writer* writer, uint64_t* crc_out) {
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001234 const EntryReader reader(mapped_zip, entry);
1235
Narayan Kamath485b3642017-10-26 14:42:39 +01001236 return zip_archive::Inflate(reader, entry->compressed_length, entry->uncompressed_length, writer,
1237 crc_out);
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001238}
1239
Narayan Kamath485b3642017-10-26 14:42:39 +01001240static int32_t CopyEntryToWriter(MappedZipFile& mapped_zip, const ZipEntry* entry,
1241 zip_archive::Writer* writer, uint64_t* crc_out) {
Narayan Kamathf899bd52015-04-17 11:53:14 +01001242 static const uint32_t kBufSize = 32768;
1243 std::vector<uint8_t> buf(kBufSize);
1244
1245 const uint32_t length = entry->uncompressed_length;
1246 uint32_t count = 0;
Andreas Gampe964b95c2019-04-05 13:48:02 -07001247 uLong crc = 0;
Narayan Kamathf899bd52015-04-17 11:53:14 +01001248 while (count < length) {
1249 uint32_t remaining = length - count;
Adam Lesinskide117e42017-06-19 10:27:38 -07001250 off64_t offset = entry->offset + count;
Narayan Kamathf899bd52015-04-17 11:53:14 +01001251
Adam Lesinskide117e42017-06-19 10:27:38 -07001252 // Safe conversion because kBufSize is narrow enough for a 32 bit signed value.
Andreas Gampe964b95c2019-04-05 13:48:02 -07001253 const uint32_t block_size = (remaining > kBufSize) ? kBufSize : remaining;
Adam Lesinskide117e42017-06-19 10:27:38 -07001254
1255 // Make sure to read at offset to ensure concurrent access to the fd.
1256 if (!mapped_zip.ReadAtOffset(buf.data(), block_size, offset)) {
Andreas Gampe964b95c2019-04-05 13:48:02 -07001257 ALOGW("CopyFileToFile: copy read failed, block_size = %u, offset = %" PRId64 ": %s",
Adam Lesinskide117e42017-06-19 10:27:38 -07001258 block_size, static_cast<int64_t>(offset), strerror(errno));
Narayan Kamathf899bd52015-04-17 11:53:14 +01001259 return kIoError;
1260 }
1261
1262 if (!writer->Append(&buf[0], block_size)) {
1263 return kIoError;
1264 }
1265 crc = crc32(crc, &buf[0], block_size);
1266 count += block_size;
1267 }
1268
1269 *crc_out = crc;
1270
1271 return 0;
1272}
1273
Ryan Prichard3673f992018-10-10 22:41:14 -07001274int32_t ExtractToWriter(ZipArchiveHandle archive, ZipEntry* entry, zip_archive::Writer* writer) {
Narayan Kamath7462f022013-11-21 13:05:04 +00001275 const uint16_t method = entry->method;
Narayan Kamath7462f022013-11-21 13:05:04 +00001276
1277 // this should default to kUnknownCompressionMethod.
1278 int32_t return_value = -1;
1279 uint64_t crc = 0;
1280 if (method == kCompressStored) {
Tianjie Xu18c25922016-09-29 15:27:41 -07001281 return_value = CopyEntryToWriter(archive->mapped_zip, entry, writer, &crc);
Narayan Kamath7462f022013-11-21 13:05:04 +00001282 } else if (method == kCompressDeflated) {
Tianjie Xu18c25922016-09-29 15:27:41 -07001283 return_value = InflateEntryToWriter(archive->mapped_zip, entry, writer, &crc);
Narayan Kamath7462f022013-11-21 13:05:04 +00001284 }
1285
1286 if (!return_value && entry->has_data_descriptor) {
Narayan Kamath162b7052017-06-05 13:21:12 +01001287 return_value = ValidateDataDescriptor(archive->mapped_zip, entry);
Narayan Kamath7462f022013-11-21 13:05:04 +00001288 if (return_value) {
1289 return return_value;
1290 }
1291 }
1292
Narayan Kamath162b7052017-06-05 13:21:12 +01001293 // Validate that the CRC matches the calculated value.
1294 if (kCrcChecksEnabled && (entry->crc32 != static_cast<uint32_t>(crc))) {
Mark Salyzyn088bf902014-05-08 16:02:20 -07001295 ALOGW("Zip: crc mismatch: expected %" PRIu32 ", was %" PRIu64, entry->crc32, crc);
Narayan Kamath7462f022013-11-21 13:05:04 +00001296 return kInconsistentInformation;
1297 }
1298
1299 return return_value;
1300}
1301
Ryan Prichard3673f992018-10-10 22:41:14 -07001302int32_t ExtractToMemory(ZipArchiveHandle archive, ZipEntry* entry, uint8_t* begin, uint32_t size) {
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -08001303 MemoryWriter writer(begin, size);
Ryan Prichard3673f992018-10-10 22:41:14 -07001304 return ExtractToWriter(archive, entry, &writer);
Narayan Kamathf899bd52015-04-17 11:53:14 +01001305}
1306
Ryan Prichard3673f992018-10-10 22:41:14 -07001307int32_t ExtractEntryToFile(ZipArchiveHandle archive, ZipEntry* entry, int fd) {
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -08001308 auto writer = FileWriter::Create(fd, entry);
1309 if (!writer.IsValid()) {
Narayan Kamath7462f022013-11-21 13:05:04 +00001310 return kIoError;
1311 }
1312
Ryan Prichard3673f992018-10-10 22:41:14 -07001313 return ExtractToWriter(archive, entry, &writer);
Narayan Kamath7462f022013-11-21 13:05:04 +00001314}
1315
Ryan Prichard3673f992018-10-10 22:41:14 -07001316int GetFileDescriptor(const ZipArchiveHandle archive) {
1317 return archive->mapped_zip.GetFileDescriptor();
Narayan Kamath7462f022013-11-21 13:05:04 +00001318}
Colin Cross7c6c7f02016-09-16 10:15:51 -07001319
Ryan Mitchell23150e42020-03-09 09:33:46 -07001320off64_t GetFileDescriptorOffset(const ZipArchiveHandle archive) {
1321 return archive->mapped_zip.GetFileOffset();
1322}
1323
Tianjie Xu18c25922016-09-29 15:27:41 -07001324#if !defined(_WIN32)
Narayan Kamath485b3642017-10-26 14:42:39 +01001325class ProcessWriter : public zip_archive::Writer {
Tianjie Xu18c25922016-09-29 15:27:41 -07001326 public:
Jiyong Parkcd997e62017-06-30 17:23:33 +09001327 ProcessWriter(ProcessZipEntryFunction func, void* cookie)
1328 : Writer(), proc_function_(func), cookie_(cookie) {}
Tianjie Xu18c25922016-09-29 15:27:41 -07001329
1330 virtual bool Append(uint8_t* buf, size_t buf_size) override {
1331 return proc_function_(buf, buf_size, cookie_);
1332 }
1333
1334 private:
1335 ProcessZipEntryFunction proc_function_;
1336 void* cookie_;
1337};
1338
Ryan Prichard3673f992018-10-10 22:41:14 -07001339int32_t ProcessZipEntryContents(ZipArchiveHandle archive, ZipEntry* entry,
Tianjie Xu18c25922016-09-29 15:27:41 -07001340 ProcessZipEntryFunction func, void* cookie) {
1341 ProcessWriter writer(func, cookie);
Ryan Prichard3673f992018-10-10 22:41:14 -07001342 return ExtractToWriter(archive, entry, &writer);
Tianjie Xu18c25922016-09-29 15:27:41 -07001343}
1344
Jiyong Parkcd997e62017-06-30 17:23:33 +09001345#endif //! defined(_WIN32)
Tianjie Xu18c25922016-09-29 15:27:41 -07001346
1347int MappedZipFile::GetFileDescriptor() const {
1348 if (!has_fd_) {
1349 ALOGW("Zip: MappedZipFile doesn't have a file descriptor.");
1350 return -1;
1351 }
1352 return fd_;
1353}
1354
Elliott Hughesf66460b2019-10-22 11:44:50 -07001355const void* MappedZipFile::GetBasePtr() const {
Tianjie Xu18c25922016-09-29 15:27:41 -07001356 if (has_fd_) {
1357 ALOGW("Zip: MappedZipFile doesn't have a base pointer.");
1358 return nullptr;
1359 }
1360 return base_ptr_;
1361}
1362
Ryan Mitchell23150e42020-03-09 09:33:46 -07001363off64_t MappedZipFile::GetFileOffset() const {
1364 return fd_offset_;
1365}
1366
Tianjie Xu18c25922016-09-29 15:27:41 -07001367off64_t MappedZipFile::GetFileLength() const {
1368 if (has_fd_) {
Ryan Mitchell23150e42020-03-09 09:33:46 -07001369 if (data_length_ != -1) {
1370 return data_length_;
1371 }
1372 data_length_ = lseek64(fd_, 0, SEEK_END);
1373 if (data_length_ == -1) {
Tianjie Xu18c25922016-09-29 15:27:41 -07001374 ALOGE("Zip: lseek on fd %d failed: %s", fd_, strerror(errno));
1375 }
Ryan Mitchell23150e42020-03-09 09:33:46 -07001376 return data_length_;
Tianjie Xu18c25922016-09-29 15:27:41 -07001377 } else {
1378 if (base_ptr_ == nullptr) {
Elliott Hughesfba2a1a2019-12-16 16:16:16 -08001379 ALOGE("Zip: invalid file map");
Tianjie Xu18c25922016-09-29 15:27:41 -07001380 return -1;
1381 }
Ryan Mitchell23150e42020-03-09 09:33:46 -07001382 return data_length_;
Tianjie Xu18c25922016-09-29 15:27:41 -07001383 }
1384}
1385
Tianjie Xu18c25922016-09-29 15:27:41 -07001386// Attempts to read |len| bytes into |buf| at offset |off|.
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001387bool MappedZipFile::ReadAtOffset(uint8_t* buf, size_t len, off64_t off) const {
Tianjie Xu18c25922016-09-29 15:27:41 -07001388 if (has_fd_) {
Ryan Mitchell23150e42020-03-09 09:33:46 -07001389 if (off < 0) {
1390 ALOGE("Zip: invalid offset %" PRId64, off);
1391 return false;
1392 }
1393
1394 off64_t read_offset;
1395 if (__builtin_add_overflow(fd_offset_, off, &read_offset)) {
1396 ALOGE("Zip: invalid read offset %" PRId64 " overflows, fd offset %" PRId64, off, fd_offset_);
1397 return false;
1398 }
1399
1400 if (data_length_ != -1) {
1401 off64_t read_end;
1402 if (len > std::numeric_limits<off64_t>::max() ||
1403 __builtin_add_overflow(off, static_cast<off64_t>(len), &read_end)) {
1404 ALOGE("Zip: invalid read length %" PRId64 " overflows, offset %" PRId64,
1405 static_cast<off64_t>(len), off);
1406 return false;
1407 }
1408
1409 if (read_end > data_length_) {
1410 ALOGE("Zip: invalid read length %" PRId64 " exceeds data length %" PRId64 ", offset %"
1411 PRId64, static_cast<off64_t>(len), data_length_, off);
1412 return false;
1413 }
1414 }
1415
1416 if (!android::base::ReadFullyAtOffset(fd_, buf, len, read_offset)) {
Elliott Hughesfba2a1a2019-12-16 16:16:16 -08001417 ALOGE("Zip: failed to read at offset %" PRId64, off);
Tianjie Xu18c25922016-09-29 15:27:41 -07001418 return false;
1419 }
Adam Lesinskide117e42017-06-19 10:27:38 -07001420 } else {
Ryan Mitchell23150e42020-03-09 09:33:46 -07001421 if (off < 0 || off > data_length_) {
Elliott Hughesfba2a1a2019-12-16 16:16:16 -08001422 ALOGE("Zip: invalid offset: %" PRId64 ", data length: %" PRId64, off, data_length_);
Adam Lesinskide117e42017-06-19 10:27:38 -07001423 return false;
1424 }
Elliott Hughesf66460b2019-10-22 11:44:50 -07001425 memcpy(buf, static_cast<const uint8_t*>(base_ptr_) + off, len);
Tianjie Xu18c25922016-09-29 15:27:41 -07001426 }
Adam Lesinskide117e42017-06-19 10:27:38 -07001427 return true;
Tianjie Xu18c25922016-09-29 15:27:41 -07001428}
1429
Elliott Hughesf66460b2019-10-22 11:44:50 -07001430void CentralDirectory::Initialize(const void* map_base_ptr, off64_t cd_start_offset,
1431 size_t cd_size) {
1432 base_ptr_ = static_cast<const uint8_t*>(map_base_ptr) + cd_start_offset;
Tianjie Xu18c25922016-09-29 15:27:41 -07001433 length_ = cd_size;
1434}
1435
Elliott Hughese8f4b142018-10-19 16:09:39 -07001436bool ZipArchive::InitializeCentralDirectory(off64_t cd_start_offset, size_t cd_size) {
Tianjie Xu18c25922016-09-29 15:27:41 -07001437 if (mapped_zip.HasFd()) {
Elliott Hughese8f4b142018-10-19 16:09:39 -07001438 directory_map = android::base::MappedFile::FromFd(mapped_zip.GetFileDescriptor(),
Ryan Mitchell23150e42020-03-09 09:33:46 -07001439 mapped_zip.GetFileOffset() + cd_start_offset,
1440 cd_size, PROT_READ);
Elliott Hughesfba2a1a2019-12-16 16:16:16 -08001441 if (!directory_map) {
1442 ALOGE("Zip: failed to map central directory (offset %" PRId64 ", size %zu): %s",
1443 cd_start_offset, cd_size, strerror(errno));
1444 return false;
1445 }
Tianjie Xu18c25922016-09-29 15:27:41 -07001446
Elliott Hughese8f4b142018-10-19 16:09:39 -07001447 CHECK_EQ(directory_map->size(), cd_size);
1448 central_directory.Initialize(directory_map->data(), 0 /*offset*/, cd_size);
Tianjie Xu18c25922016-09-29 15:27:41 -07001449 } else {
1450 if (mapped_zip.GetBasePtr() == nullptr) {
Elliott Hughesfba2a1a2019-12-16 16:16:16 -08001451 ALOGE("Zip: Failed to map central directory, bad mapped_zip base pointer");
Tianjie Xu18c25922016-09-29 15:27:41 -07001452 return false;
1453 }
1454 if (static_cast<off64_t>(cd_start_offset) + static_cast<off64_t>(cd_size) >
1455 mapped_zip.GetFileLength()) {
Jiyong Parkcd997e62017-06-30 17:23:33 +09001456 ALOGE(
1457 "Zip: Failed to map central directory, offset exceeds mapped memory region ("
1458 "start_offset %" PRId64 ", cd_size %zu, mapped_region_size %" PRId64 ")",
1459 static_cast<int64_t>(cd_start_offset), cd_size, mapped_zip.GetFileLength());
Tianjie Xu18c25922016-09-29 15:27:41 -07001460 return false;
1461 }
1462
1463 central_directory.Initialize(mapped_zip.GetBasePtr(), cd_start_offset, cd_size);
1464 }
1465 return true;
1466}
Elliott Hughes55fd2932017-05-28 22:59:04 -07001467
1468tm ZipEntry::GetModificationTime() const {
1469 tm t = {};
1470
1471 t.tm_hour = (mod_time >> 11) & 0x1f;
1472 t.tm_min = (mod_time >> 5) & 0x3f;
1473 t.tm_sec = (mod_time & 0x1f) << 1;
1474
1475 t.tm_year = ((mod_time >> 25) & 0x7f) + 80;
1476 t.tm_mon = ((mod_time >> 21) & 0xf) - 1;
1477 t.tm_mday = (mod_time >> 16) & 0x1f;
1478
1479 return t;
1480}