Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1b49634f authored by Josh Gao's avatar Josh Gao
Browse files

libziparchive: use fdsan in ZipArchive.

Test: treehugger
Change-Id: I8586b8ad27c4f1eda1a5266867da8dbbf4870c5e
parent 3fa9637e
Loading
Loading
Loading
Loading
+42 −0
Original line number Diff line number Diff line
@@ -33,6 +33,10 @@
#include <memory>
#include <vector>

#if defined(__BIONIC__)
#include <android/fdsan.h>
#endif

#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/macros.h>  // TEMP_FAILURE_RETRY may or may not be in unistd
@@ -165,6 +169,44 @@ static int32_t AddToHash(ZipString* hash_table, const uint64_t hash_table_size,
  return 0;
}

ZipArchive::ZipArchive(const int fd, bool assume_ownership)
    : mapped_zip(fd),
      close_file(assume_ownership),
      directory_offset(0),
      central_directory(),
      directory_map(new android::FileMap()),
      num_entries(0),
      hash_table_size(0),
      hash_table(nullptr) {
#if defined(__BIONIC__)
  if (assume_ownership) {
    android_fdsan_exchange_owner_tag(fd, 0, reinterpret_cast<uint64_t>(this));
  }
#endif
}

ZipArchive::ZipArchive(void* address, size_t length)
    : mapped_zip(address, length),
      close_file(false),
      directory_offset(0),
      central_directory(),
      directory_map(new android::FileMap()),
      num_entries(0),
      hash_table_size(0),
      hash_table(nullptr) {}

ZipArchive::~ZipArchive() {
  if (close_file && mapped_zip.GetFileDescriptor() >= 0) {
#if defined(__BIONIC__)
    android_fdsan_close_with_tag(mapped_zip.GetFileDescriptor(), reinterpret_cast<uint64_t>(this));
#else
    close(mapped_zip.GetFileDescriptor());
#endif
  }

  free(hash_table);
}

static int32_t MapCentralDirectory0(const char* debug_file_name, ZipArchive* archive,
                                    off64_t file_length, off64_t read_amount, uint8_t* scan_buffer) {
  const off64_t search_start = file_length - read_amount;
+3 −27
Original line number Diff line number Diff line
@@ -156,33 +156,9 @@ struct ZipArchive {
  uint32_t hash_table_size;
  ZipString* hash_table;

  ZipArchive(const int fd, bool assume_ownership)
      : mapped_zip(fd),
        close_file(assume_ownership),
        directory_offset(0),
        central_directory(),
        directory_map(new android::FileMap()),
        num_entries(0),
        hash_table_size(0),
        hash_table(nullptr) {}

  ZipArchive(void* address, size_t length)
      : mapped_zip(address, length),
        close_file(false),
        directory_offset(0),
        central_directory(),
        directory_map(new android::FileMap()),
        num_entries(0),
        hash_table_size(0),
        hash_table(nullptr) {}

  ~ZipArchive() {
    if (close_file && mapped_zip.GetFileDescriptor() >= 0) {
      close(mapped_zip.GetFileDescriptor());
    }

    free(hash_table);
  }
  ZipArchive(const int fd, bool assume_ownership);
  ZipArchive(void* address, size_t length);
  ~ZipArchive();

  bool InitializeCentralDirectory(const char* debug_file_name, off64_t cd_start_offset,
                                  size_t cd_size);