Skip to content

Commit

Permalink
Merge "Create a wrapper class for update package"
Browse files Browse the repository at this point in the history
  • Loading branch information
Tianjie Xu authored and Gerrit Code Review committed Mar 11, 2019
2 parents 6c54127 + f07ed2e commit ba99651
Show file tree
Hide file tree
Showing 9 changed files with 314 additions and 88 deletions.
1 change: 1 addition & 0 deletions Android.bp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ cc_library_static {
"fsck_unshare_blocks.cpp",
"fuse_sdcard_provider.cpp",
"install.cpp",
"package.cpp",
"recovery.cpp",
"roots.cpp",
],
Expand Down
24 changes: 9 additions & 15 deletions install.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include "otautil/paths.h"
#include "otautil/sysutil.h"
#include "otautil/thermalutil.h"
#include "package.h"
#include "private/install.h"
#include "roots.h"
#include "ui.h"
Expand Down Expand Up @@ -585,34 +586,29 @@ static int really_install_package(const std::string& path, bool* wipe_cache, boo
}
}

MemMapping map;
if (!map.MapFile(path)) {
LOG(ERROR) << "failed to map file";
auto package = Package::CreateMemoryPackage(
path, std::bind(&RecoveryUI::SetProgress, ui, std::placeholders::_1));
if (!package) {
log_buffer->push_back(android::base::StringPrintf("error: %d", kMapFileFailure));
return INSTALL_CORRUPT;
}

// Verify package.
if (!verify_package(map.addr, map.length)) {
if (!verify_package(package.get())) {
log_buffer->push_back(android::base::StringPrintf("error: %d", kZipVerificationFailure));
return INSTALL_CORRUPT;
}

// Try to open the package.
ZipArchiveHandle zip;
int err = OpenArchiveFromMemory(map.addr, map.length, path.c_str(), &zip);
if (err != 0) {
LOG(ERROR) << "Can't open " << path << " : " << ErrorCodeString(err);
ZipArchiveHandle zip = package->GetZipArchiveHandle();
if (!zip) {
log_buffer->push_back(android::base::StringPrintf("error: %d", kZipOpenFailure));

CloseArchive(zip);
return INSTALL_CORRUPT;
}

// Additionally verify the compatibility of the package.
if (!verify_package_compatibility(zip)) {
log_buffer->push_back(android::base::StringPrintf("error: %d", kPackageCompatibilityFailure));
CloseArchive(zip);
return INSTALL_CORRUPT;
}

Expand All @@ -626,7 +622,6 @@ static int really_install_package(const std::string& path, bool* wipe_cache, boo
ui->SetEnableReboot(true);
ui->Print("\n");

CloseArchive(zip);
return result;
}

Expand Down Expand Up @@ -705,7 +700,7 @@ int install_package(const std::string& path, bool* wipe_cache, bool needs_mount,
return result;
}

bool verify_package(const unsigned char* package_data, size_t package_size) {
bool verify_package(Package* package) {
static constexpr const char* CERTIFICATE_ZIP_FILE = "/system/etc/security/otacerts.zip";
std::vector<Certificate> loaded_keys = LoadKeysFromZipfile(CERTIFICATE_ZIP_FILE);
if (loaded_keys.empty()) {
Expand All @@ -717,8 +712,7 @@ bool verify_package(const unsigned char* package_data, size_t package_size) {
// Verify package.
ui->Print("Verifying update package...\n");
auto t0 = std::chrono::system_clock::now();
int err = verify_file(package_data, package_size, loaded_keys,
std::bind(&RecoveryUI::SetProgress, ui, std::placeholders::_1));
int err = verify_file(package, loaded_keys);
std::chrono::duration<double> duration = std::chrono::system_clock::now() - t0;
ui->Print("Update package verification took %.1f s (result %d).\n", duration.count(), err);
if (err != VERIFY_SUCCESS) {
Expand Down
8 changes: 5 additions & 3 deletions install.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

#include <ziparchive/zip_archive.h>

#include "package.h"

enum InstallResult {
INSTALL_SUCCESS,
INSTALL_ERROR,
Expand All @@ -46,9 +48,9 @@ enum class OtaType {
int install_package(const std::string& package, bool* wipe_cache, bool needs_mount,
int retry_count);

// Verify the package by ota keys. Return true if the package is verified successfully,
// otherwise return false.
bool verify_package(const unsigned char* package_data, size_t package_size);
// Verifies the package by ota keys. Returns true if the package is verified successfully,
// otherwise returns false.
bool verify_package(Package* package);

// Reads meta data file of the package; parses each line in the format "key=value"; and writes the
// result to |metadata|. Return true if succeed, otherwise return false.
Expand Down
154 changes: 154 additions & 0 deletions package.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "package.h"

#include <string.h>

#include <android-base/logging.h>
#include <android-base/stringprintf.h>
#include <openssl/sha.h>

#include "otautil/error_code.h"
#include "otautil/sysutil.h"

// This class wraps the package in memory, i.e. a memory mapped package, or a package loaded
// to a string/vector.
class MemoryPackage : public Package {
public:
// Constructs the class from a file. We will memory maps the file later.
MemoryPackage(const std::string& path, std::unique_ptr<MemMapping> map,
const std::function<void(float)>& set_progress);

// Constructs the class from the package bytes in |content|.
MemoryPackage(std::vector<uint8_t> content, const std::function<void(float)>& set_progress);

~MemoryPackage() override;

// Memory maps the package file if necessary. Initializes the start address and size of the
// package.
uint64_t GetPackageSize() const override {
return package_size_;
}

bool ReadFullyAtOffset(uint8_t* buffer, uint64_t byte_count, uint64_t offset) override;

ZipArchiveHandle GetZipArchiveHandle() override;

bool UpdateHashAtOffset(const std::vector<HasherUpdateCallback>& hashers, uint64_t start,
uint64_t length) override;

private:
const uint8_t* addr_; // Start address of the package in memory.
uint64_t package_size_; // Package size in bytes.

// The memory mapped package.
std::unique_ptr<MemMapping> map_;
// A copy of the package content, valid only if we create the class with the exact bytes of
// the package.
std::vector<uint8_t> package_content_;
// The physical path to the package, empty if we create the class with the package content.
std::string path_;

// The ZipArchiveHandle of the package.
ZipArchiveHandle zip_handle_;
};

// TODO(xunchang) Implement the PackageFromFd.

void Package::SetProgress(float progress) {
if (set_progress_) {
set_progress_(progress);
}
}

std::unique_ptr<Package> Package::CreateMemoryPackage(
const std::string& path, const std::function<void(float)>& set_progress) {
std::unique_ptr<MemMapping> mmap = std::make_unique<MemMapping>();
if (!mmap->MapFile(path)) {
LOG(ERROR) << "failed to map file";
return nullptr;
}

return std::make_unique<MemoryPackage>(path, std::move(mmap), set_progress);
}

std::unique_ptr<Package> Package::CreateMemoryPackage(
std::vector<uint8_t> content, const std::function<void(float)>& set_progress) {
return std::make_unique<MemoryPackage>(std::move(content), set_progress);
}

MemoryPackage::MemoryPackage(const std::string& path, std::unique_ptr<MemMapping> map,
const std::function<void(float)>& set_progress)
: map_(std::move(map)), path_(path), zip_handle_(nullptr) {
addr_ = map_->addr;
package_size_ = map_->length;
set_progress_ = set_progress;
}

MemoryPackage::MemoryPackage(std::vector<uint8_t> content,
const std::function<void(float)>& set_progress)
: package_content_(std::move(content)), zip_handle_(nullptr) {
CHECK(!package_content_.empty());
addr_ = package_content_.data();
package_size_ = package_content_.size();
set_progress_ = set_progress;
}

MemoryPackage::~MemoryPackage() {
if (zip_handle_) {
CloseArchive(zip_handle_);
}
}

bool MemoryPackage::ReadFullyAtOffset(uint8_t* buffer, uint64_t byte_count, uint64_t offset) {
if (byte_count > package_size_ || offset > package_size_ - byte_count) {
LOG(ERROR) << "Out of bound read, offset: " << offset << ", size: " << byte_count
<< ", total package_size: " << package_size_;
return false;
}
memcpy(buffer, addr_ + offset, byte_count);
return true;
}

bool MemoryPackage::UpdateHashAtOffset(const std::vector<HasherUpdateCallback>& hashers,
uint64_t start, uint64_t length) {
if (length > package_size_ || start > package_size_ - length) {
LOG(ERROR) << "Out of bound read, offset: " << start << ", size: " << length
<< ", total package_size: " << package_size_;
return false;
}

for (const auto& hasher : hashers) {
hasher(addr_ + start, length);
}
return true;
}

ZipArchiveHandle MemoryPackage::GetZipArchiveHandle() {
if (zip_handle_) {
return zip_handle_;
}

if (auto err = OpenArchiveFromMemory(const_cast<uint8_t*>(addr_), package_size_, path_.c_str(),
&zip_handle_);
err != 0) {
LOG(ERROR) << "Can't open package" << path_ << " : " << ErrorCodeString(err);
return nullptr;
}

return zip_handle_;
}
52 changes: 52 additions & 0 deletions package.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <stdint.h>

#include <functional>
#include <map>
#include <memory>
#include <string>
#include <vector>

#include <ziparchive/zip_archive.h>

#include "verifier.h"

// This class serves as a wrapper for an OTA update package. It aims to provide the common
// interface for both packages loaded in memory and packages read from fd.
class Package : public VerifierInterface {
public:
virtual ~Package() = default;

// Opens the package as a zip file and returns the ZipArchiveHandle.
virtual ZipArchiveHandle GetZipArchiveHandle() = 0;

// Updates the progress in fraction during package verification.
void SetProgress(float progress) override;

static std::unique_ptr<Package> CreateMemoryPackage(
const std::string& path, const std::function<void(float)>& set_progress);

static std::unique_ptr<Package> CreateMemoryPackage(
std::vector<uint8_t> content, const std::function<void(float)>& set_progress);

protected:
// An optional function to update the progress.
std::function<void(float)> set_progress_;
};
9 changes: 6 additions & 3 deletions recovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
#include "otautil/error_code.h"
#include "otautil/paths.h"
#include "otautil/sysutil.h"
#include "package.h"
#include "roots.h"
#include "screen_ui.h"
#include "ui.h"
Expand Down Expand Up @@ -517,13 +518,15 @@ static std::string ReadWipePackage(size_t wipe_package_size) {
// 1. verify the package.
// 2. check metadata (ota-type, pre-device and serial number if having one).
static bool CheckWipePackage(const std::string& wipe_package) {
if (!verify_package(reinterpret_cast<const unsigned char*>(wipe_package.data()),
wipe_package.size())) {
auto package = Package::CreateMemoryPackage(
std::vector<uint8_t>(wipe_package.begin(), wipe_package.end()), nullptr);

if (!package || !verify_package(package.get())) {
LOG(ERROR) << "Failed to verify package";
return false;
}

// Extract metadata
// TODO(xunchang) get zip archive from package.
ZipArchiveHandle zip;
if (auto err =
OpenArchiveFromMemory(const_cast<void*>(static_cast<const void*>(&wipe_package[0])),
Expand Down
Loading

0 comments on commit ba99651

Please sign in to comment.