From fd7d835fd9eba3b87f9d5f287801082bf93b9962 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Wed, 27 Mar 2019 23:25:54 -0700 Subject: [PATCH 1/6] Remove the extern declaration of `sehandle` from roots.cpp. It has become obsolete since we replaced the call to make_ext4 with e2fsprogs (commit ded2dac082fd703f1cd7a5c3de59450cd3dc2530, which landed into P). Test: mmma -j bootable/recovery Change-Id: I09141322874213dcb0f1280bba239376e71a4d17 --- roots.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/roots.cpp b/roots.cpp index 7a922b8e..c42723f0 100644 --- a/roots.cpp +++ b/roots.cpp @@ -51,8 +51,6 @@ using android::fs_mgr::ReadDefaultFstab; static Fstab fstab; -extern struct selabel_handle* sehandle; - void load_volume_table() { if (!ReadDefaultFstab(&fstab)) { LOG(ERROR) << "Failed to read default fstab"; From 8bd6f455d261fa62a20321a426723c491903a82e Mon Sep 17 00:00:00 2001 From: Bernie Innocenti Date: Thu, 28 Mar 2019 15:48:08 +0900 Subject: [PATCH 2/6] Fix bogus error checking on unique_fd The expression "!fd" calls the implicit conversion to int, but comparing the raw fd against 0 does not work, since open() and other POSIX calls returning a file descriptor use -1 to signal an error. Test: m recovery Change-Id: I0847c276f39cb9dd09c7ffb96951276113418fc8 --- applypatch/applypatch.cpp | 2 +- fuse_sideload/fuse_sideload.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/applypatch/applypatch.cpp b/applypatch/applypatch.cpp index f9383dde..90d8e860 100644 --- a/applypatch/applypatch.cpp +++ b/applypatch/applypatch.cpp @@ -76,7 +76,7 @@ static bool ReadPartitionToBuffer(const Partition& partition, FileContents* out, } android::base::unique_fd dev(open(partition.name.c_str(), O_RDONLY)); - if (!dev) { + if (dev == -1) { PLOG(ERROR) << "Failed to open eMMC partition \"" << partition << "\""; } else { std::vector buffer(partition.size); diff --git a/fuse_sideload/fuse_sideload.cpp b/fuse_sideload/fuse_sideload.cpp index b5b6ac15..3d948030 100644 --- a/fuse_sideload/fuse_sideload.cpp +++ b/fuse_sideload/fuse_sideload.cpp @@ -392,7 +392,7 @@ int run_fuse_sideload(std::unique_ptr&& provider, const char* } fd.ffd.reset(open("/dev/fuse", O_RDWR)); - if (!fd.ffd) { + if (fd.ffd == -1) { perror("open /dev/fuse"); result = -1; goto done; From 08ba1ad9b1a6406a67feddeff211387225ccd603 Mon Sep 17 00:00:00 2001 From: Bill Peckham Date: Thu, 28 Mar 2019 18:42:13 -0700 Subject: [PATCH 3/6] Use flags = 0 to avoid fd closing for child updater process If we use the default parameter we'll get O_CLOEXEC, which will close the fd that we pass to the child process, which will cause it to fail. Passing zero avoids the problem. Bug: 122813742 Test: Verify that non-A/B OTA update is successful. Change-Id: Ia61bc7260f17d9209715583e6ded69e1196ed3f6 --- install.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/install.cpp b/install.cpp index b7fb7887..ffa39e46 100644 --- a/install.cpp +++ b/install.cpp @@ -342,7 +342,9 @@ static int try_update_binary(const std::string& package, ZipArchiveHandle zip, b // The updater in child process writes to the pipe to communicate with recovery. android::base::unique_fd pipe_read, pipe_write; - if (!android::base::Pipe(&pipe_read, &pipe_write)) { + // Explicitly disable O_CLOEXEC using 0 as the flags (last) parameter to Pipe + // so that the child updater process will recieve a non-closed fd. + if (!android::base::Pipe(&pipe_read, &pipe_write, 0)) { PLOG(ERROR) << "Failed to create pipe for updater-recovery communication"; return INSTALL_CORRUPT; } From 2478885f3ca47fe2c4073df1100f7bd6ad4931af Mon Sep 17 00:00:00 2001 From: xunchang Date: Fri, 22 Mar 2019 16:08:52 -0700 Subject: [PATCH 4/6] Move install to separate module Build libinstall as a shared library. Also drop the dependency on the global variables in common.h. Test: unit tests pass, sideload an OTA Change-Id: I30a20047768ce00689fc0e7851c1c5d712a365a0 --- Android.bp | 73 +---------------- common.h | 1 - fsck_unshare_blocks.cpp | 2 +- install/Android.bp | 78 +++++++++++++++++++ adb_install.cpp => install/adb_install.cpp | 14 ++-- asn1_decoder.cpp => install/asn1_decoder.cpp | 4 +- .../fuse_sdcard_install.cpp | 8 +- .../include/install/adb_install.h | 7 +- .../include/install/fuse_sdcard_install.h | 0 .../include/install/install.h | 12 ++- .../include/install/package.h | 0 .../include/install/verifier.h | 38 ++++----- .../include/private/asn1_decoder.h | 1 + .../include/private/setup_commands.h | 0 install.cpp => install/install.cpp | 32 ++++---- package.cpp => install/package.cpp | 2 +- verifier.cpp => install/verifier.cpp | 37 +++++---- logging.cpp | 2 +- otautil/Android.bp | 11 +++ roots.h => otautil/include/otautil/roots.h | 5 +- roots.cpp => otautil/roots.cpp | 8 +- recovery.cpp | 21 ++--- recovery_main.cpp | 2 +- tests/Android.bp | 7 +- tests/component/install_test.cpp | 4 +- tests/component/verifier_test.cpp | 4 +- tests/unit/asn1_decoder_test.cpp | 2 +- tests/unit/package_test.cpp | 2 +- 28 files changed, 194 insertions(+), 183 deletions(-) create mode 100644 install/Android.bp rename adb_install.cpp => install/adb_install.cpp (92%) rename asn1_decoder.cpp => install/asn1_decoder.cpp (98%) rename fuse_sdcard_install.cpp => install/fuse_sdcard_install.cpp (97%) rename adb_install.h => install/include/install/adb_install.h (86%) rename fuse_sdcard_install.h => install/include/install/fuse_sdcard_install.h (100%) rename install.h => install/include/install/install.h (92%) rename package.h => install/include/install/package.h (100%) rename verifier.h => install/include/install/verifier.h (80%) rename asn1_decoder.h => install/include/private/asn1_decoder.h (98%) rename private/install.h => install/include/private/setup_commands.h (100%) rename install.cpp => install/install.cpp (96%) rename package.cpp => install/package.cpp (99%) rename verifier.cpp => install/verifier.cpp (93%) rename roots.h => otautil/include/otautil/roots.h (96%) rename roots.cpp => otautil/roots.cpp (98%) diff --git a/Android.bp b/Android.bp index 7b67f407..a44a2c62 100644 --- a/Android.bp +++ b/Android.bp @@ -63,32 +63,18 @@ cc_defaults { "libbootloader_message", "libcrypto", "libcutils", - "libext4_utils", "libfs_mgr", - "libfusesideload", - "libhidl-gen-utils", - "libhidlbase", - "libhidltransport", "liblog", - "libpng", - "libselinux", - "libtinyxml2", - "libutils", - "libz", "libziparchive", ], static_libs: [ "librecovery_fastboot", "libminui", - "libpackage", - "libverifier", "libotautil", // external dependencies "libhealthhalutils", - "libvintf_recovery", - "libvintf", "libfstab", ], } @@ -102,69 +88,14 @@ cc_library_static { ], srcs: [ - "adb_install.cpp", "fsck_unshare_blocks.cpp", - "fuse_sdcard_install.cpp", - "install.cpp", "recovery.cpp", - "roots.cpp", ], shared_libs: [ + "libinstall", "librecovery_ui", ], - - include_dirs: [ - "system/vold", - ], -} - -cc_library_static { - name: "libverifier", - recovery_available: true, - - defaults: [ - "recovery_defaults", - ], - - srcs: [ - "asn1_decoder.cpp", - "verifier.cpp", - ], - - shared_libs: [ - "libbase", - "libcrypto", - "libcrypto_utils", - "libziparchive", - ], - - static_libs: [ - "libotautil", - ], -} - -cc_library_static { - name: "libpackage", - recovery_available: true, - - defaults: [ - "recovery_defaults", - ], - - srcs: [ - "package.cpp", - ], - - shared_libs: [ - "libbase", - "libcrypto", - "libziparchive", - ], - - static_libs: [ - "libotautil", - ], } cc_binary { @@ -172,6 +103,7 @@ cc_binary { recovery: true, defaults: [ + "libinstall_defaults", "librecovery_defaults", ], @@ -181,6 +113,7 @@ cc_binary { ], shared_libs: [ + "libinstall", "libminadbd_services", "librecovery_ui", ], diff --git a/common.h b/common.h index 38bdd526..22b2f0a0 100644 --- a/common.h +++ b/common.h @@ -31,7 +31,6 @@ struct selabel_handle; extern struct selabel_handle* sehandle; extern RecoveryUI* ui; -extern bool modified_flash; extern bool has_cache; // The current stage, e.g. "1/2". diff --git a/fsck_unshare_blocks.cpp b/fsck_unshare_blocks.cpp index ce6940a6..e74f8ba6 100644 --- a/fsck_unshare_blocks.cpp +++ b/fsck_unshare_blocks.cpp @@ -36,7 +36,7 @@ #include #include -#include "roots.h" +#include "otautil/roots.h" static constexpr const char* SYSTEM_E2FSCK_BIN = "/system/bin/e2fsck_static"; static constexpr const char* TMP_E2FSCK_BIN = "/tmp/e2fsck.bin"; diff --git a/install/Android.bp b/install/Android.bp new file mode 100644 index 00000000..aa47990a --- /dev/null +++ b/install/Android.bp @@ -0,0 +1,78 @@ +// 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. + +cc_defaults { + name: "libinstall_defaults", + + defaults: [ + "recovery_defaults", + ], + + shared_libs: [ + "libbase", + "libbootloader_message", + "libcrypto", + "libext4_utils", + "libfs_mgr", + "libfusesideload", + "libhidl-gen-utils", + "libhidlbase", + "libhidltransport", + "liblog", + "libselinux", + "libtinyxml2", + "libutils", + "libz", + "libziparchive", + ], + + static_libs: [ + "libotautil", + + // external dependencies + "libvintf_recovery", + "libvintf", + "libfstab", + ], +} + +cc_library { + name: "libinstall", + recovery_available: true, + + defaults: [ + "libinstall_defaults", + ], + + srcs: [ + "adb_install.cpp", + "asn1_decoder.cpp", + "fuse_sdcard_install.cpp", + "install.cpp", + "package.cpp", + "verifier.cpp", + ], + + shared_libs: [ + "librecovery_ui", + ], + + export_include_dirs: [ + "include", + ], + + export_shared_lib_headers: [ + "librecovery_ui", + ], +} diff --git a/adb_install.cpp b/install/adb_install.cpp similarity index 92% rename from adb_install.cpp rename to install/adb_install.cpp index 1d19fd3a..5296ff60 100644 --- a/adb_install.cpp +++ b/install/adb_install.cpp @@ -14,7 +14,7 @@ * limitations under the License. */ -#include "adb_install.h" +#include "install/adb_install.h" #include #include @@ -29,12 +29,16 @@ #include #include -#include "common.h" #include "fuse_sideload.h" -#include "install.h" +#include "install/install.h" #include "recovery_ui/ui.h" -int apply_from_adb(bool* wipe_cache) { +static bool SetUsbConfig(const std::string& state) { + android::base::SetProperty("sys.usb.config", state); + return android::base::WaitForProperty("sys.usb.state", state); +} + +int apply_from_adb(bool* wipe_cache, RecoveryUI* ui) { // Save the usb state to restore after the sideload operation. std::string usb_state = android::base::GetProperty("sys.usb.state", "none"); // Clean up state and stop adbd. @@ -85,7 +89,7 @@ int apply_from_adb(bool* wipe_cache) { break; } } - result = install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache, false, 0); + result = install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache, false, 0, ui); break; } diff --git a/asn1_decoder.cpp b/install/asn1_decoder.cpp similarity index 98% rename from asn1_decoder.cpp rename to install/asn1_decoder.cpp index 285214f1..2d81a6e1 100644 --- a/asn1_decoder.cpp +++ b/install/asn1_decoder.cpp @@ -14,9 +14,7 @@ * limitations under the License. */ -#include "asn1_decoder.h" - -#include +#include "private/asn1_decoder.h" int asn1_context::peek_byte() const { if (length_ == 0) { diff --git a/fuse_sdcard_install.cpp b/install/fuse_sdcard_install.cpp similarity index 97% rename from fuse_sdcard_install.cpp rename to install/fuse_sdcard_install.cpp index 1f385093..dde289f8 100644 --- a/fuse_sdcard_install.cpp +++ b/install/fuse_sdcard_install.cpp @@ -14,7 +14,7 @@ * limitations under the License. */ -#include "fuse_sdcard_install.h" +#include "install/fuse_sdcard_install.h" #include #include @@ -35,8 +35,8 @@ #include "bootloader_message/bootloader_message.h" #include "fuse_provider.h" #include "fuse_sideload.h" -#include "install.h" -#include "roots.h" +#include "install/install.h" +#include "otautil/roots.h" static constexpr const char* SDCARD_ROOT = "/sdcard"; // How long (in seconds) we wait for the fuse-provided package file to @@ -184,7 +184,7 @@ int ApplyFromSdcard(Device* device, bool* wipe_cache, RecoveryUI* ui) { } } - result = install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache, false, 0 /*retry_count*/); + result = install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache, false, 0 /*retry_count*/, ui); break; } diff --git a/adb_install.h b/install/include/install/adb_install.h similarity index 86% rename from adb_install.h rename to install/include/install/adb_install.h index cc3ca267..dbc82450 100644 --- a/adb_install.h +++ b/install/include/install/adb_install.h @@ -14,9 +14,8 @@ * limitations under the License. */ -#ifndef _ADB_INSTALL_H -#define _ADB_INSTALL_H +#pragma once -int apply_from_adb(bool* wipe_cache); +#include -#endif +int apply_from_adb(bool* wipe_cache, RecoveryUI* ui); diff --git a/fuse_sdcard_install.h b/install/include/install/fuse_sdcard_install.h similarity index 100% rename from fuse_sdcard_install.h rename to install/include/install/fuse_sdcard_install.h diff --git a/install.h b/install/include/install/install.h similarity index 92% rename from install.h rename to install/include/install/install.h index da8aa5e3..74fb3d17 100644 --- a/install.h +++ b/install/include/install/install.h @@ -14,8 +14,7 @@ * limitations under the License. */ -#ifndef RECOVERY_INSTALL_H_ -#define RECOVERY_INSTALL_H_ +#pragma once #include @@ -26,6 +25,7 @@ #include #include "package.h" +#include "recovery_ui/ui.h" enum InstallResult { INSTALL_SUCCESS, @@ -45,12 +45,12 @@ enum class OtaType { // Installs the given update package. If INSTALL_SUCCESS is returned and *wipe_cache is true on // exit, caller should wipe the cache partition. -int install_package(const std::string& package, bool* wipe_cache, bool needs_mount, - int retry_count); +int install_package(const std::string& package, bool* wipe_cache, bool needs_mount, int retry_count, + RecoveryUI* ui); // Verifies the package by ota keys. Returns true if the package is verified successfully, // otherwise returns false. -bool verify_package(Package* package); +bool verify_package(Package* package, RecoveryUI* ui); // 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. @@ -67,5 +67,3 @@ bool verify_package_compatibility(ZipArchiveHandle package_zip); // Mandatory checks: ota-type, pre-device and serial number(if presents) // AB OTA specific checks: pre-build version, fingerprint, timestamp. int CheckPackageMetadata(const std::map& metadata, OtaType ota_type); - -#endif // RECOVERY_INSTALL_H_ diff --git a/package.h b/install/include/install/package.h similarity index 100% rename from package.h rename to install/include/install/package.h diff --git a/verifier.h b/install/include/install/verifier.h similarity index 80% rename from verifier.h rename to install/include/install/verifier.h index ef9feaff..f9e94758 100644 --- a/verifier.h +++ b/install/include/install/verifier.h @@ -14,8 +14,7 @@ * limitations under the License. */ -#ifndef _RECOVERY_VERIFIER_H -#define _RECOVERY_VERIFIER_H +#pragma once #include @@ -44,25 +43,20 @@ struct ECKEYDeleter { }; struct Certificate { - typedef enum { - KEY_TYPE_RSA, - KEY_TYPE_EC, - } KeyType; + typedef enum { + KEY_TYPE_RSA, + KEY_TYPE_EC, + } KeyType; - Certificate(int hash_len_, - KeyType key_type_, - std::unique_ptr&& rsa_, - std::unique_ptr&& ec_) - : hash_len(hash_len_), - key_type(key_type_), - rsa(std::move(rsa_)), - ec(std::move(ec_)) {} + Certificate(int hash_len_, KeyType key_type_, std::unique_ptr&& rsa_, + std::unique_ptr&& ec_) + : hash_len(hash_len_), key_type(key_type_), rsa(std::move(rsa_)), ec(std::move(ec_)) {} - // SHA_DIGEST_LENGTH (SHA-1) or SHA256_DIGEST_LENGTH (SHA-256) - int hash_len; - KeyType key_type; - std::unique_ptr rsa; - std::unique_ptr ec; + // SHA_DIGEST_LENGTH (SHA-1) or SHA256_DIGEST_LENGTH (SHA-256) + int hash_len; + KeyType key_type; + std::unique_ptr rsa; + std::unique_ptr ec; }; class VerifierInterface { @@ -103,7 +97,5 @@ bool LoadCertificateFromBuffer(const std::vector& pem_content, Certific // certificates. Returns an empty list if we fail to parse any of the entries. std::vector LoadKeysFromZipfile(const std::string& zip_name); -#define VERIFY_SUCCESS 0 -#define VERIFY_FAILURE 1 - -#endif /* _RECOVERY_VERIFIER_H */ +#define VERIFY_SUCCESS 0 +#define VERIFY_FAILURE 1 diff --git a/asn1_decoder.h b/install/include/private/asn1_decoder.h similarity index 98% rename from asn1_decoder.h rename to install/include/private/asn1_decoder.h index 3e992115..e5337d9c 100644 --- a/asn1_decoder.h +++ b/install/include/private/asn1_decoder.h @@ -17,6 +17,7 @@ #ifndef ASN1_DECODER_H_ #define ASN1_DECODER_H_ +#include #include class asn1_context { diff --git a/private/install.h b/install/include/private/setup_commands.h similarity index 100% rename from private/install.h rename to install/include/private/setup_commands.h diff --git a/install.cpp b/install/install.cpp similarity index 96% rename from install.cpp rename to install/install.cpp index ffa39e46..a1124c36 100644 --- a/install.cpp +++ b/install/install.cpp @@ -14,7 +14,7 @@ * limitations under the License. */ -#include "install.h" +#include "install/install.h" #include #include @@ -46,19 +46,22 @@ #include #include -#include "common.h" +#include "install/package.h" +#include "install/verifier.h" #include "otautil/error_code.h" #include "otautil/paths.h" +#include "otautil/roots.h" #include "otautil/sysutil.h" #include "otautil/thermalutil.h" -#include "package.h" -#include "private/install.h" +#include "private/setup_commands.h" #include "recovery_ui/ui.h" -#include "roots.h" -#include "verifier.h" using namespace std::chrono_literals; +static constexpr int kRecoveryApiVersion = 3; +// Assert the version defined in code and in Android.mk are consistent. +static_assert(kRecoveryApiVersion == RECOVERY_API_VERSION, "Mismatching recovery API versions."); + // Default allocation of progress bar segments to operations static constexpr int VERIFICATION_PROGRESS_TIME = 60; static constexpr float VERIFICATION_PROGRESS_FRACTION = 0.25; @@ -323,7 +326,7 @@ static void log_max_temperature(int* max_temperature, const std::atomic& l // If the package contains an update binary, extract it and run it. static int try_update_binary(const std::string& package, ZipArchiveHandle zip, bool* wipe_cache, std::vector* log_buffer, int retry_count, - int* max_temperature) { + int* max_temperature, RecoveryUI* ui) { std::map metadata; if (!ReadMetadataFromPackage(zip, &metadata)) { LOG(ERROR) << "Failed to parse metadata in the zip file"; @@ -569,7 +572,7 @@ bool verify_package_compatibility(ZipArchiveHandle package_zip) { static int really_install_package(const std::string& path, bool* wipe_cache, bool needs_mount, std::vector* log_buffer, int retry_count, - int* max_temperature) { + int* max_temperature, RecoveryUI* ui) { ui->SetBackground(RecoveryUI::INSTALLING_UPDATE); ui->Print("Finding update package...\n"); // Give verification half the progress bar... @@ -596,7 +599,7 @@ static int really_install_package(const std::string& path, bool* wipe_cache, boo } // Verify package. - if (!verify_package(package.get())) { + if (!verify_package(package.get(), ui)) { log_buffer->push_back(android::base::StringPrintf("error: %d", kZipVerificationFailure)); return INSTALL_CORRUPT; } @@ -620,18 +623,19 @@ static int really_install_package(const std::string& path, bool* wipe_cache, boo ui->Print("Retry attempt: %d\n", retry_count); } ui->SetEnableReboot(false); - int result = try_update_binary(path, zip, wipe_cache, log_buffer, retry_count, max_temperature); + int result = + try_update_binary(path, zip, wipe_cache, log_buffer, retry_count, max_temperature, ui); ui->SetEnableReboot(true); ui->Print("\n"); return result; } -int install_package(const std::string& path, bool* wipe_cache, bool needs_mount, int retry_count) { +int install_package(const std::string& path, bool* wipe_cache, bool needs_mount, int retry_count, + RecoveryUI* ui) { CHECK(!path.empty()); CHECK(wipe_cache != nullptr); - modified_flash = true; auto start = std::chrono::system_clock::now(); int start_temperature = GetMaxValueFromThermalZone(); @@ -644,7 +648,7 @@ int install_package(const std::string& path, bool* wipe_cache, bool needs_mount, result = INSTALL_ERROR; } else { result = really_install_package(path, wipe_cache, needs_mount, &log_buffer, retry_count, - &max_temperature); + &max_temperature, ui); } // Measure the time spent to apply OTA update in seconds. @@ -702,7 +706,7 @@ int install_package(const std::string& path, bool* wipe_cache, bool needs_mount, return result; } -bool verify_package(Package* package) { +bool verify_package(Package* package, RecoveryUI* ui) { static constexpr const char* CERTIFICATE_ZIP_FILE = "/system/etc/security/otacerts.zip"; std::vector loaded_keys = LoadKeysFromZipfile(CERTIFICATE_ZIP_FILE); if (loaded_keys.empty()) { diff --git a/package.cpp b/install/package.cpp similarity index 99% rename from package.cpp rename to install/package.cpp index 6c7289f3..4402f485 100644 --- a/package.cpp +++ b/install/package.cpp @@ -14,7 +14,7 @@ * limitations under the License. */ -#include "package.h" +#include "install/package.h" #include #include diff --git a/verifier.cpp b/install/verifier.cpp similarity index 93% rename from verifier.cpp rename to install/verifier.cpp index 08d852b3..6ba1d77c 100644 --- a/verifier.cpp +++ b/install/verifier.cpp @@ -14,7 +14,7 @@ * limitations under the License. */ -#include "verifier.h" +#include "install/verifier.h" #include #include @@ -36,8 +36,8 @@ #include #include -#include "asn1_decoder.h" #include "otautil/print_sha1.h" +#include "private/asn1_decoder.h" /* * Simple version of PKCS#7 SignedData extraction. This extracts the @@ -82,10 +82,8 @@ static bool read_pkcs7(const uint8_t* pkcs7_der, size_t pkcs7_der_len, } std::unique_ptr signed_data_seq(signed_data_app->asn1_sequence_get()); - if (signed_data_seq == nullptr || - !signed_data_seq->asn1_sequence_next() || - !signed_data_seq->asn1_sequence_next() || - !signed_data_seq->asn1_sequence_next() || + if (signed_data_seq == nullptr || !signed_data_seq->asn1_sequence_next() || + !signed_data_seq->asn1_sequence_next() || !signed_data_seq->asn1_sequence_next() || !signed_data_seq->asn1_constructed_skip_all()) { return false; } @@ -96,11 +94,8 @@ static bool read_pkcs7(const uint8_t* pkcs7_der, size_t pkcs7_der_len, } std::unique_ptr sig_seq(sig_set->asn1_sequence_get()); - if (sig_seq == nullptr || - !sig_seq->asn1_sequence_next() || - !sig_seq->asn1_sequence_next() || - !sig_seq->asn1_sequence_next() || - !sig_seq->asn1_sequence_next()) { + if (sig_seq == nullptr || !sig_seq->asn1_sequence_next() || !sig_seq->asn1_sequence_next() || + !sig_seq->asn1_sequence_next() || !sig_seq->asn1_sequence_next()) { return false; } @@ -152,8 +147,8 @@ int verify_file(VerifierInterface* package, const std::vector& keys << " bytes from end"; if (signature_start > comment_size) { - LOG(ERROR) << "signature start: " << signature_start << " is larger than comment size: " - << comment_size; + LOG(ERROR) << "signature start: " << signature_start + << " is larger than comment size: " << comment_size; return VERIFY_FAILURE; } @@ -189,8 +184,8 @@ int verify_file(VerifierInterface* package, const std::vector& keys return VERIFY_FAILURE; } - for (size_t i = 4; i < eocd_size-3; ++i) { - if (eocd[i] == 0x50 && eocd[i+1] == 0x4b && eocd[i+2] == 0x05 && eocd[i+3] == 0x06) { + for (size_t i = 4; i < eocd_size - 3; ++i) { + if (eocd[i] == 0x50 && eocd[i + 1] == 0x4b && eocd[i + 2] == 0x05 && eocd[i + 3] == 0x06) { // If the sequence $50 $4b $05 $06 appears anywhere after the real one, libziparchive will // find the later (wrong) one, which could be exploitable. Fail the verification if this // sequence occurs anywhere after the real one. @@ -203,8 +198,12 @@ int verify_file(VerifierInterface* package, const std::vector& keys bool need_sha256 = false; for (const auto& key : keys) { switch (key.hash_len) { - case SHA_DIGEST_LENGTH: need_sha1 = true; break; - case SHA256_DIGEST_LENGTH: need_sha256 = true; break; + case SHA_DIGEST_LENGTH: + need_sha1 = true; + break; + case SHA256_DIGEST_LENGTH: + need_sha256 = true; + break; } } @@ -247,8 +246,8 @@ int verify_file(VerifierInterface* package, const std::vector& keys const uint8_t* signature = eocd + eocd_size - signature_start; size_t signature_size = signature_start - FOOTER_SIZE; - LOG(INFO) << "signature (offset: " << std::hex << (length - signature_start) << ", length: " - << signature_size << "): " << print_hex(signature, signature_size); + LOG(INFO) << "signature (offset: " << std::hex << (length - signature_start) + << ", length: " << signature_size << "): " << print_hex(signature, signature_size); std::vector sig_der; if (!read_pkcs7(signature, signature_size, &sig_der)) { diff --git a/logging.cpp b/logging.cpp index 283d1150..48f9ec31 100644 --- a/logging.cpp +++ b/logging.cpp @@ -33,7 +33,7 @@ #include "common.h" #include "otautil/dirutil.h" #include "otautil/paths.h" -#include "roots.h" +#include "otautil/roots.h" static constexpr const char* LOG_FILE = "/cache/recovery/log"; static constexpr const char* LAST_INSTALL_FILE = "/cache/recovery/last_install"; diff --git a/otautil/Android.bp b/otautil/Android.bp index 41018dd2..b4936c08 100644 --- a/otautil/Android.bp +++ b/otautil/Android.bp @@ -42,12 +42,23 @@ cc_library_static { "dirutil.cpp", "mounts.cpp", "parse_install_logs.cpp", + "roots.cpp", "sysutil.cpp", "thermalutil.cpp", ], + include_dirs: [ + "system/vold", + ], + + static_libs: [ + "libfstab", + ], + shared_libs: [ "libcutils", + "libext4_utils", + "libfs_mgr", "libselinux", ], }, diff --git a/roots.h b/otautil/include/otautil/roots.h similarity index 96% rename from roots.h rename to otautil/include/otautil/roots.h index 7b031a18..482f3d05 100644 --- a/roots.h +++ b/otautil/include/otautil/roots.h @@ -14,8 +14,7 @@ * limitations under the License. */ -#ifndef RECOVERY_ROOTS_H_ -#define RECOVERY_ROOTS_H_ +#pragma once #include @@ -58,5 +57,3 @@ int setup_install_mounts(); bool logical_partitions_mapped(); std::string get_system_root(); - -#endif // RECOVERY_ROOTS_H_ diff --git a/roots.cpp b/otautil/roots.cpp similarity index 98% rename from roots.cpp rename to otautil/roots.cpp index c42723f0..815d644e 100644 --- a/roots.cpp +++ b/otautil/roots.cpp @@ -14,7 +14,7 @@ * limitations under the License. */ -#include "roots.h" +#include "otautil/roots.h" #include #include @@ -174,11 +174,9 @@ int format_volume(const std::string& volume, const std::string& directory) { PLOG(ERROR) << "format_volume: failed to open " << v->blk_device; return -1; } - length = - get_file_size(fd.get(), v->length ? -v->length : CRYPT_FOOTER_OFFSET); + length = get_file_size(fd.get(), v->length ? -v->length : CRYPT_FOOTER_OFFSET); if (length <= 0) { - LOG(ERROR) << "get_file_size: invalid size " << length << " for " - << v->blk_device; + LOG(ERROR) << "get_file_size: invalid size " << length << " for " << v->blk_device; return -1; } } diff --git a/recovery.cpp b/recovery.cpp index d9c1f22f..421bc12f 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -50,20 +50,20 @@ #include #include -#include "adb_install.h" #include "common.h" #include "fsck_unshare_blocks.h" -#include "fuse_sdcard_install.h" -#include "install.h" +#include "install/adb_install.h" +#include "install/fuse_sdcard_install.h" +#include "install/install.h" +#include "install/package.h" #include "logging.h" #include "otautil/dirutil.h" #include "otautil/error_code.h" #include "otautil/paths.h" +#include "otautil/roots.h" #include "otautil/sysutil.h" -#include "package.h" #include "recovery_ui/screen_ui.h" #include "recovery_ui/ui.h" -#include "roots.h" static constexpr const char* CACHE_LOG_DIR = "/cache/recovery"; static constexpr const char* COMMAND_FILE = "/cache/recovery/command"; @@ -79,7 +79,7 @@ static constexpr const char* METADATA_ROOT = "/metadata"; // into target_files.zip. Assert the version defined in code and in Android.mk are consistent. static_assert(kRecoveryApiVersion == RECOVERY_API_VERSION, "Mismatching recovery API versions."); -bool modified_flash = false; +static bool modified_flash = false; std::string stage; const char* reason = nullptr; @@ -439,7 +439,7 @@ static std::unique_ptr 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(Package* wipe_package) { - if (!verify_package(wipe_package)) { + if (!verify_package(wipe_package, ui)) { LOG(ERROR) << "Failed to verify package"; return false; } @@ -693,7 +693,7 @@ static Device::BuiltinAction prompt_and_wait(Device* device, int status) { modified_flash = true; bool adb = (chosen_action == Device::APPLY_ADB_SIDELOAD); if (adb) { - status = apply_from_adb(&should_wipe_cache); + status = apply_from_adb(&should_wipe_cache, ui); } else { status = ApplyFromSdcard(device, &should_wipe_cache, ui); } @@ -1030,7 +1030,8 @@ Device::BuiltinAction start_recovery(Device* device, const std::vectorShowText(true); } - status = apply_from_adb(&should_wipe_cache); + status = apply_from_adb(&should_wipe_cache, ui); if (status == INSTALL_SUCCESS && should_wipe_cache) { if (!wipe_cache(false, device)) { status = INSTALL_ERROR; diff --git a/recovery_main.cpp b/recovery_main.cpp index 2f5a1845..56ebe16f 100644 --- a/recovery_main.cpp +++ b/recovery_main.cpp @@ -53,12 +53,12 @@ #include "logging.h" #include "minadbd/minadbd.h" #include "otautil/paths.h" +#include "otautil/roots.h" #include "otautil/sysutil.h" #include "recovery.h" #include "recovery_ui/device.h" #include "recovery_ui/stub_ui.h" #include "recovery_ui/ui.h" -#include "roots.h" static constexpr const char* COMMAND_FILE = "/cache/recovery/command"; static constexpr const char* LOCALE_FILE = "/cache/recovery/last_locale"; diff --git a/tests/Android.bp b/tests/Android.bp index ef5919eb..09ef716d 100644 --- a/tests/Android.bp +++ b/tests/Android.bp @@ -76,9 +76,9 @@ libapplypatch_static_libs = [ librecovery_static_libs = [ "librecovery", "librecovery_fastboot", + "libinstall", + "librecovery_ui", "libminui", - "libpackage", - "libverifier", "libotautil", "libhealthhalutils", @@ -116,10 +116,9 @@ cc_test { ], static_libs: libapplypatch_static_libs + [ + "libinstall", "librecovery_ui", "libminui", - "libpackage", - "libverifier", "libotautil", "libupdater", "libgtest_prod", diff --git a/tests/component/install_test.cpp b/tests/component/install_test.cpp index 969805b4..38513293 100644 --- a/tests/component/install_test.cpp +++ b/tests/component/install_test.cpp @@ -32,9 +32,9 @@ #include #include -#include "install.h" +#include "install/install.h" #include "otautil/paths.h" -#include "private/install.h" +#include "private/setup_commands.h" static void BuildZipArchive(const std::map& file_map, int fd, int compression_type) { diff --git a/tests/component/verifier_test.cpp b/tests/component/verifier_test.cpp index bdb8af23..ded23c52 100644 --- a/tests/component/verifier_test.cpp +++ b/tests/component/verifier_test.cpp @@ -35,9 +35,9 @@ #include #include "common/test_constants.h" +#include "install/package.h" +#include "install/verifier.h" #include "otautil/sysutil.h" -#include "package.h" -#include "verifier.h" using namespace std::string_literals; diff --git a/tests/unit/asn1_decoder_test.cpp b/tests/unit/asn1_decoder_test.cpp index b334a655..d94dd435 100644 --- a/tests/unit/asn1_decoder_test.cpp +++ b/tests/unit/asn1_decoder_test.cpp @@ -20,7 +20,7 @@ #include -#include "asn1_decoder.h" +#include "private/asn1_decoder.h" TEST(Asn1DecoderTest, Empty_Failure) { uint8_t empty[] = {}; diff --git a/tests/unit/package_test.cpp b/tests/unit/package_test.cpp index fa492d38..a735a699 100644 --- a/tests/unit/package_test.cpp +++ b/tests/unit/package_test.cpp @@ -26,7 +26,7 @@ #include #include "common/test_constants.h" -#include "package.h" +#include "install/package.h" class PackageTest : public ::testing::Test { protected: From bc982a4f8845e5fa0cd2630ddcc3d8f70066b083 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Fri, 29 Mar 2019 15:43:09 -0700 Subject: [PATCH 5/6] Remove ui_print(). This used to be a helper function that allows printing message to UI. We no longer have any active user in bootable/recovery. Device-specific code can achieve the same functionality by calling GetUI()->Print() instead. Test: mmma -j bootable/recovery Change-Id: If584fc8a51d1af466f1d94d8ea5faa262603a784 --- common.h | 9 +-------- recovery.cpp | 15 --------------- recovery_ui/include/recovery_ui/device.h | 4 ++-- 3 files changed, 3 insertions(+), 25 deletions(-) diff --git a/common.h b/common.h index 22b2f0a0..9cb44bd3 100644 --- a/common.h +++ b/common.h @@ -14,11 +14,7 @@ * limitations under the License. */ -#ifndef RECOVERY_COMMON_H -#define RECOVERY_COMMON_H - -#include -#include +#pragma once #include @@ -39,9 +35,6 @@ extern std::string stage; // The reason argument provided in "--reason=". extern const char* reason; -void ui_print(const char* format, ...) __printflike(1, 2); - bool is_ro_debuggable(); bool SetUsbConfig(const std::string& state); -#endif // RECOVERY_COMMON_H diff --git a/recovery.cpp b/recovery.cpp index 421bc12f..02cc53c2 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -24,7 +24,6 @@ #include #include #include -#include #include #include #include @@ -746,20 +745,6 @@ static void print_property(const char* key, const char* name, void* /* cookie */ printf("%s=%s\n", key, name); } -void ui_print(const char* format, ...) { - std::string buffer; - va_list ap; - va_start(ap, format); - android::base::StringAppendV(&buffer, format, ap); - va_end(ap); - - if (ui != nullptr) { - ui->Print("%s", buffer.c_str()); - } else { - fputs(buffer.c_str(), stdout); - } -} - static bool is_battery_ok(int* required_battery_level) { using android::hardware::health::V1_0::BatteryStatus; using android::hardware::health::V2_0::get_health_service; diff --git a/recovery_ui/include/recovery_ui/device.h b/recovery_ui/include/recovery_ui/device.h index cfa914e7..3c44510c 100644 --- a/recovery_ui/include/recovery_ui/device.h +++ b/recovery_ui/include/recovery_ui/device.h @@ -93,8 +93,8 @@ class Device { // Performs a recovery action selected from the menu. 'menu_position' will be the index of the // selected menu item, or a non-negative value returned from HandleMenuKey(). The menu will be - // hidden when this is called; implementations can call ui_print() to print information to the - // screen. If the menu position is one of the builtin actions, you can just return the + // hidden when this is called; implementations can call GetUI()->Print() to print information to + // the screen. If the menu position is one of the builtin actions, you can just return the // corresponding enum value. If it is an action specific to your device, you actually perform it // here and return NO_ACTION. virtual BuiltinAction InvokeMenuItem(size_t menu_position); From e0cfab3de9d0e1b09799c7e488f171b31ddfe7bf Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Fri, 29 Mar 2019 15:53:23 -0700 Subject: [PATCH 6/6] recovery: Remove SetUsbConfig() out of common.h. libinstall now has its own copy. Test: mmma -j bootable/recovery Change-Id: Ibbe7084e15baeb7e744f2175d5944477092acc9e --- common.h | 2 -- recovery.cpp | 6 ------ recovery_main.cpp | 6 ++++++ 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/common.h b/common.h index 9cb44bd3..a524a418 100644 --- a/common.h +++ b/common.h @@ -36,5 +36,3 @@ extern std::string stage; extern const char* reason; bool is_ro_debuggable(); - -bool SetUsbConfig(const std::string& state); diff --git a/recovery.cpp b/recovery.cpp index 02cc53c2..03491849 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -271,12 +271,6 @@ static bool erase_volume(const char* volume) { return (result == 0); } -// Sets the usb config to 'state' -bool SetUsbConfig(const std::string& state) { - android::base::SetProperty("sys.usb.config", state); - return android::base::WaitForProperty("sys.usb.state", state); -} - static bool yes_no(Device* device, const char* question1, const char* question2) { std::vector headers{ question1, question2 }; std::vector items{ " No", " Yes" }; diff --git a/recovery_main.cpp b/recovery_main.cpp index 56ebe16f..b41368d7 100644 --- a/recovery_main.cpp +++ b/recovery_main.cpp @@ -178,6 +178,12 @@ static std::string load_locale_from_cache() { return android::base::Trim(content); } +// Sets the usb config to 'state'. +static bool SetUsbConfig(const std::string& state) { + android::base::SetProperty("sys.usb.config", state); + return android::base::WaitForProperty("sys.usb.state", state); +} + static void ListenRecoverySocket(RecoveryUI* ui, std::atomic& action) { android::base::unique_fd sock_fd(android_get_control_socket("recovery")); if (sock_fd < 0) {