diff options
-rw-r--r-- | Android.mk | 76 | ||||
-rw-r--r-- | adb_install.cpp | 4 | ||||
-rw-r--r-- | bootloader.cpp | 19 | ||||
-rw-r--r-- | etc/Android.mk | 20 | ||||
-rw-r--r-- | etc/init.rc | 4 | ||||
-rw-r--r-- | etc/init.recovery.servicenew.rc | 5 | ||||
-rw-r--r-- | etc/init.recovery.serviceold.rc | 4 | ||||
-rw-r--r-- | fuse_sideload.cpp | 15 | ||||
-rw-r--r-- | fuse_sideload.h | 9 | ||||
-rw-r--r-- | gui/action.cpp | 2 | ||||
-rw-r--r-- | legacy_property_service.cpp (renamed from legacy_property_service.c) | 8 | ||||
-rw-r--r-- | minadbd.old/fuse_adb_provider.c | 2 | ||||
-rw-r--r-- | oldverifier/verifier.cpp | 471 | ||||
-rw-r--r-- | oldverifier/verifier.h | 58 | ||||
-rw-r--r-- | prebuilt/Android.mk | 6 | ||||
-rw-r--r-- | set_metadata.cpp (renamed from set_metadata.c) | 0 | ||||
-rw-r--r-- | set_metadata.h | 8 | ||||
-rw-r--r-- | toolbox/Android.mk | 17 | ||||
-rw-r--r-- | toolbox/start.c | 21 | ||||
-rw-r--r-- | toolbox/stop.c | 19 | ||||
-rw-r--r-- | tools/recovery_l10n/Android.mk | 6 | ||||
-rw-r--r-- | twinstall.cpp | 6 | ||||
-rw-r--r-- | twrp-functions.cpp | 2 | ||||
-rw-r--r-- | twrp.cpp | 2 | ||||
-rw-r--r-- | twrpTar.cpp | 18 |
25 files changed, 741 insertions, 61 deletions
diff --git a/Android.mk b/Android.mk index 1e2141dc4..a6fe2a4d7 100644 --- a/Android.mk +++ b/Android.mk @@ -116,12 +116,18 @@ LOCAL_SHARED_LIBRARIES := LOCAL_STATIC_LIBRARIES += libguitwrp LOCAL_SHARED_LIBRARIES += libaosprecovery libz libc libcutils libstdc++ libtar libblkid libminuitwrp libminadbd libmtdutils libminzip libtwadbbu -LOCAL_SHARED_LIBRARIES += libcrecovery libbase libcrypto +LOCAL_SHARED_LIBRARIES += libcrecovery ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 23; echo $$?),0) - LOCAL_SHARED_LIBRARIES += libstlport + LOCAL_SHARED_LIBRARIES += libstlport libmincrypttwrp + LOCAL_C_INCLUDES += $(LOCAL_PATH)/libmincrypt/includes + LOCAL_CFLAGS += -DUSE_OLD_VERIFIER else - LOCAL_SHARED_LIBRARIES += libc++ + LOCAL_SHARED_LIBRARIES += libc++ libcrypto +endif + +ifeq ($(shell test $(PLATFORM_SDK_VERSION) -ge 24; echo $$?),0) + LOCAL_SHARED_LIBRARIES += libbase endif ifneq ($(wildcard system/core/libsparse/Android.mk),) @@ -340,13 +346,15 @@ LOCAL_ADDITIONAL_DEPENDENCIES := \ mke2fs.conf \ pigz \ teamwin \ + toolbox_symlinks \ twrp \ unpigz_symlink \ fsck.fat \ fatlabel \ mkfs.fat \ permissive.sh \ - simg2img_twrp + simg2img_twrp \ + init.recovery.service.rc ifneq ($(TARGET_ARCH), arm64) ifneq ($(TARGET_ARCH), x86_64) @@ -503,23 +511,27 @@ endif # !TW_USE_TOOLBOX # recovery-persist (system partition dynamic executable run after /data mounts) # =============================== -include $(CLEAR_VARS) -LOCAL_SRC_FILES := recovery-persist.cpp -LOCAL_MODULE := recovery-persist -LOCAL_SHARED_LIBRARIES := liblog libbase -LOCAL_CFLAGS := -Werror -LOCAL_INIT_RC := recovery-persist.rc -include $(BUILD_EXECUTABLE) +ifeq ($(shell test $(PLATFORM_SDK_VERSION) -ge 24; echo $$?),0) + include $(CLEAR_VARS) + LOCAL_SRC_FILES := recovery-persist.cpp + LOCAL_MODULE := recovery-persist + LOCAL_SHARED_LIBRARIES := liblog libbase + LOCAL_CFLAGS := -Werror + LOCAL_INIT_RC := recovery-persist.rc + include $(BUILD_EXECUTABLE) +endif # recovery-refresh (system partition dynamic executable run at init) # =============================== -include $(CLEAR_VARS) -LOCAL_SRC_FILES := recovery-refresh.cpp -LOCAL_MODULE := recovery-refresh -LOCAL_SHARED_LIBRARIES := liblog -LOCAL_CFLAGS := -Werror -LOCAL_INIT_RC := recovery-refresh.rc -include $(BUILD_EXECUTABLE) +ifeq ($(shell test $(PLATFORM_SDK_VERSION) -ge 24; echo $$?),0) + include $(CLEAR_VARS) + LOCAL_SRC_FILES := recovery-refresh.cpp + LOCAL_MODULE := recovery-refresh + LOCAL_SHARED_LIBRARIES := liblog + LOCAL_CFLAGS := -Werror + LOCAL_INIT_RC := recovery-refresh.rc + include $(BUILD_EXECUTABLE) +endif # shared libfusesideload # =============================== @@ -531,7 +543,14 @@ LOCAL_CFLAGS += -D_XOPEN_SOURCE -D_GNU_SOURCE LOCAL_MODULE_TAGS := optional LOCAL_MODULE := libfusesideload -LOCAL_SHARED_LIBRARIES := libcutils libc libcrypto +LOCAL_SHARED_LIBRARIES := libcutils libc +ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 22; echo $$?),0) + LOCAL_C_INCLUDES := $(LOCAL_PATH)/libmincrypt/includes + LOCAL_SHARED_LIBRARIES += libmincrypttwrp + LOCAL_CFLAGS += -DUSE_MINCRYPT +else + LOCAL_SHARED_LIBRARIES += libcrypto +endif include $(BUILD_SHARED_LIBRARY) # shared libaosprecovery for Apache code @@ -540,8 +559,18 @@ include $(CLEAR_VARS) LOCAL_MODULE := libaosprecovery LOCAL_MODULE_TAGS := eng optional -LOCAL_SRC_FILES := adb_install.cpp asn1_decoder.cpp bootloader.cpp legacy_property_service.c verifier.cpp set_metadata.c tw_atomic.cpp -LOCAL_SHARED_LIBRARIES += libc liblog libcutils libmtdutils libfusesideload libselinux libcrypto +LOCAL_CFLAGS := -std=gnu++0x +LOCAL_SRC_FILES := adb_install.cpp asn1_decoder.cpp bootloader.cpp legacy_property_service.cpp set_metadata.cpp tw_atomic.cpp +LOCAL_SHARED_LIBRARIES += libc liblog libcutils libmtdutils libfusesideload libselinux +ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 23; echo $$?),0) + LOCAL_SHARED_LIBRARIES += libstdc++ libstlport libmincrypttwrp + LOCAL_C_INCLUDES := bionic external/stlport/stlport $(LOCAL_PATH)/libmincrypt/includes + LOCAL_SRC_FILES += oldverifier/verifier.cpp + LOCAL_CFLAGS += -DUSE_OLD_VERIFIER +else + LOCAL_SHARED_LIBRARIES += libc++ libcrypto + LOCAL_SRC_FILES += verifier.cpp +endif ifneq ($(BOARD_RECOVERY_BLDRMSG_OFFSET),) LOCAL_CFLAGS += -DBOARD_RECOVERY_BLDRMSG_OFFSET=$(BOARD_RECOVERY_BLDRMSG_OFFSET) @@ -598,6 +627,7 @@ include $(commands_recovery_local_path)/injecttwrp/Android.mk \ $(commands_recovery_local_path)/libblkid/Android.mk \ $(commands_recovery_local_path)/minuitwrp/Android.mk \ $(commands_recovery_local_path)/openaes/Android.mk \ + $(commands_recovery_local_path)/toolbox/Android.mk \ $(commands_recovery_local_path)/twrpTarMain/Android.mk \ $(commands_recovery_local_path)/mtp/Android.mk \ $(commands_recovery_local_path)/minzip/Android.mk \ @@ -608,6 +638,10 @@ include $(commands_recovery_local_path)/injecttwrp/Android.mk \ $(commands_recovery_local_path)/adbbu/Android.mk \ $(commands_recovery_local_path)/libpixelflinger/Android.mk +ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 23; echo $$?),0) + include $(commands_recovery_local_path)/libmincrypt/Android.mk +endif + ifeq ($(TW_INCLUDE_CRYPTO), true) include $(commands_recovery_local_path)/crypto/lollipop/Android.mk include $(commands_recovery_local_path)/crypto/scrypt/Android.mk diff --git a/adb_install.cpp b/adb_install.cpp index d9936f222..a4655dc97 100644 --- a/adb_install.cpp +++ b/adb_install.cpp @@ -31,7 +31,11 @@ #include "adb_install.h" #include "minadbd/fuse_adb_provider.h" #include "fuse_sideload.h" +#ifdef USE_OLD_VERIFIER +#include "oldverifier/verifier.h" +#else #include "verifier.h" +#endif static RecoveryUI* ui = NULL; diff --git a/bootloader.cpp b/bootloader.cpp index 13bc39167..90b8998b0 100644 --- a/bootloader.cpp +++ b/bootloader.cpp @@ -32,7 +32,7 @@ extern "C" { #include "common.h" #include "mtdutils/mtdutils.h" //#include "roots.h" -#include "unique_fd.h" +//#include "unique_fd.h" // fake Volume struct that allows us to use the AOSP code easily struct Volume @@ -232,33 +232,36 @@ static int get_bootloader_message_block(bootloader_message* out, static int set_bootloader_message_block(const bootloader_message* in, const Volume* v) { wait_for_device(v->blk_device); - unique_fd fd(open(v->blk_device, O_WRONLY | O_SYNC)); - if (fd.get() == -1) { + int fd = open(v->blk_device, O_WRONLY | O_SYNC); + if (fd == -1) { LOGE("failed to open \"%s\": %s\n", v->blk_device, strerror(errno)); return -1; } #ifdef BOARD_RECOVERY_BLDRMSG_OFFSET - lseek(fd.get(), BOARD_RECOVERY_BLDRMSG_OFFSET, SEEK_SET); + lseek(fd, BOARD_RECOVERY_BLDRMSG_OFFSET, SEEK_SET); #endif size_t written = 0; const uint8_t* start = reinterpret_cast<const uint8_t*>(in); size_t total = sizeof(*in); while (written < total) { - ssize_t wrote = TEMP_FAILURE_RETRY(write(fd.get(), start + written, total - written)); + ssize_t wrote = TEMP_FAILURE_RETRY(write(fd, start + written, total - written)); if (wrote == -1) { - LOGE("failed to write %" PRId64 " bytes: %s\n", - static_cast<off64_t>(written), strerror(errno)); + LOGE("failed to write some bytes: %s\n", + strerror(errno)); + close(fd); return -1; } written += wrote; } - if (fsync(fd.get()) == -1) { + if (fsync(fd) == -1) { LOGE("failed to fsync \"%s\": %s\n", v->blk_device, strerror(errno)); + close(fd); return -1; } + close(fd); return 0; } diff --git a/etc/Android.mk b/etc/Android.mk index 5e612b63e..461457787 100644 --- a/etc/Android.mk +++ b/etc/Android.mk @@ -31,6 +31,26 @@ include $(BUILD_PREBUILT) endif +ifeq ($(shell test $(PLATFORM_SDK_VERSION) -ge 22; echo $$?),0) + include $(CLEAR_VARS) + LOCAL_MODULE := init.recovery.service.rc + LOCAL_MODULE_TAGS := eng + LOCAL_MODULE_CLASS := RECOVERY_EXECUTABLES + LOCAL_MODULE_PATH := $(TARGET_ROOT_OUT) + + LOCAL_SRC_FILES := init.recovery.servicenew.rc + include $(BUILD_PREBUILT) +else + include $(CLEAR_VARS) + LOCAL_MODULE := init.recovery.service.rc + LOCAL_MODULE_TAGS := eng + LOCAL_MODULE_CLASS := RECOVERY_EXECUTABLES + LOCAL_MODULE_PATH := $(TARGET_ROOT_OUT) + + LOCAL_SRC_FILES := init.recovery.serviceold.rc + include $(BUILD_PREBUILT) +endif + ifeq ($(TWRP_INCLUDE_LOGCAT), true) ifeq ($(TARGET_USES_LOGD), true) diff --git a/etc/init.rc b/etc/init.rc index 907436c71..a62b68d84 100644 --- a/etc/init.rc +++ b/etc/init.rc @@ -1,5 +1,6 @@ import /init.recovery.logd.rc import /init.recovery.usb.rc +import /init.recovery.service.rc import /init.recovery.${ro.hardware}.rc on early-init @@ -109,9 +110,6 @@ service healthd /sbin/healthd -r critical seclabel u:r:healthd:s0 -service recovery /sbin/recovery - seclabel u:r:recovery:s0 - service adbd /sbin/adbd --root_seclabel=u:r:su:s0 --device_banner=recovery disabled socket adbd stream 660 system system diff --git a/etc/init.recovery.servicenew.rc b/etc/init.recovery.servicenew.rc new file mode 100644 index 000000000..bb2853c1a --- /dev/null +++ b/etc/init.recovery.servicenew.rc @@ -0,0 +1,5 @@ +on boot + +# For starting recovery on 5.0 and newer +service recovery /sbin/recovery + seclabel u:r:recovery:s0 diff --git a/etc/init.recovery.serviceold.rc b/etc/init.recovery.serviceold.rc new file mode 100644 index 000000000..892b226ff --- /dev/null +++ b/etc/init.recovery.serviceold.rc @@ -0,0 +1,4 @@ +on boot + +# For starting recovery on 4.4 and older +service recovery /sbin/recovery diff --git a/fuse_sideload.cpp b/fuse_sideload.cpp index 66dc4be86..f57d479b0 100644 --- a/fuse_sideload.cpp +++ b/fuse_sideload.cpp @@ -61,7 +61,12 @@ #include <sys/uio.h> #include <unistd.h> +#ifdef USE_MINCRYPT +#include "mincrypt/sha256.h" +#define SHA256_DIGEST_LENGTH SHA256_DIGEST_SIZE +#else #include <openssl/sha.h> +#endif #include "fuse_sideload.h" @@ -275,7 +280,11 @@ static int fetch_block(struct fuse_data* fd, uint32_t block) { // - Otherwise, return -EINVAL for the read. uint8_t hash[SHA256_DIGEST_LENGTH]; +#ifdef USE_MINCRYPT + SHA256_hash(fd->block_data, fd->block_size, hash); +#else SHA256(fd->block_data, fd->block_size, hash); +#endif uint8_t* blockhash = fd->hashes + block * SHA256_DIGEST_LENGTH; if (memcmp(hash, blockhash, SHA256_DIGEST_LENGTH) == 0) { return 0; @@ -527,3 +536,9 @@ int run_fuse_sideload(struct provider_vtab* vtab, void* cookie, return result; } + +extern "C" int run_old_fuse_sideload(struct provider_vtab* vtab, void* cookie, + uint64_t file_size, uint32_t block_size) +{ + return run_fuse_sideload(vtab, cookie, file_size, block_size); +} diff --git a/fuse_sideload.h b/fuse_sideload.h index c0b16efbe..2a8ebfc5e 100644 --- a/fuse_sideload.h +++ b/fuse_sideload.h @@ -35,4 +35,13 @@ struct provider_vtab { int run_fuse_sideload(struct provider_vtab* vtab, void* cookie, uint64_t file_size, uint32_t block_size); +#ifdef __cplusplus +extern "C" { +#endif +int run_old_fuse_sideload(struct provider_vtab* vtab, void* cookie, + uint64_t file_size, uint32_t block_size); +#ifdef __cplusplus +} +#endif + #endif diff --git a/gui/action.cpp b/gui/action.cpp index 328fc2c52..073f1a394 100644 --- a/gui/action.cpp +++ b/gui/action.cpp @@ -50,8 +50,8 @@ extern "C" { #include "../twinstall.h" #include "cutils/properties.h" #include "../adb_install.h" -#include "../set_metadata.h" }; +#include "../set_metadata.h" #include "../minuitwrp/minui.h" #include "rapidxml.hpp" diff --git a/legacy_property_service.c b/legacy_property_service.cpp index 490a24524..90a0d47df 100644 --- a/legacy_property_service.c +++ b/legacy_property_service.cpp @@ -36,6 +36,10 @@ //#include <sys/atomics.h> #include "legacy_property_service.h" +#ifndef INT32_MAX +#define INT32_MAX (2147483647) +#endif + static int persistent_properties_loaded = 0; static int property_area_inited = 0; @@ -108,9 +112,9 @@ static int init_property_area(void) fcntl(pa_workspace.fd, F_SETFD, FD_CLOEXEC); - pa_info_array = (void*) (((char*) pa_workspace.data) + PA_INFO_START); + pa_info_array = (prop_info*) (((char*) pa_workspace.data) + PA_INFO_START); - pa = pa_workspace.data; + pa = (prop_area*)(pa_workspace.data); memset(pa, 0, PA_SIZE); pa->magic = PROP_AREA_MAGIC; pa->version = PROP_AREA_VERSION; diff --git a/minadbd.old/fuse_adb_provider.c b/minadbd.old/fuse_adb_provider.c index f80533a8c..18e990682 100644 --- a/minadbd.old/fuse_adb_provider.c +++ b/minadbd.old/fuse_adb_provider.c @@ -63,5 +63,5 @@ int run_adb_fuse(int sfd, uint64_t file_size, uint32_t block_size) { vtab.read_block = read_block_adb; vtab.close = close_adb; - return run_fuse_sideload(&vtab, &ad, file_size, block_size); + return run_old_fuse_sideload(&vtab, &ad, file_size, block_size); } diff --git a/oldverifier/verifier.cpp b/oldverifier/verifier.cpp new file mode 100644 index 000000000..98c733732 --- /dev/null +++ b/oldverifier/verifier.cpp @@ -0,0 +1,471 @@ +/* + * Copyright (C) 2008 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 "asn1_decoder.h" +#include "common.h" +#include "ui.h" +#include "verifier.h" + +#include "mincrypt/dsa_sig.h" +#include "mincrypt/p256.h" +#include "mincrypt/p256_ecdsa.h" +#include "mincrypt/rsa.h" +#include "mincrypt/sha.h" +#include "mincrypt/sha256.h" + +#include <errno.h> +#include <malloc.h> +#include <stdio.h> +#include <string.h> + +//extern RecoveryUI* ui; + +#define PUBLIC_KEYS_FILE "/res/keys" + +/* + * Simple version of PKCS#7 SignedData extraction. This extracts the + * signature OCTET STRING to be used for signature verification. + * + * For full details, see http://www.ietf.org/rfc/rfc3852.txt + * + * The PKCS#7 structure looks like: + * + * SEQUENCE (ContentInfo) + * OID (ContentType) + * [0] (content) + * SEQUENCE (SignedData) + * INTEGER (version CMSVersion) + * SET (DigestAlgorithmIdentifiers) + * SEQUENCE (EncapsulatedContentInfo) + * [0] (CertificateSet OPTIONAL) + * [1] (RevocationInfoChoices OPTIONAL) + * SET (SignerInfos) + * SEQUENCE (SignerInfo) + * INTEGER (CMSVersion) + * SEQUENCE (SignerIdentifier) + * SEQUENCE (DigestAlgorithmIdentifier) + * SEQUENCE (SignatureAlgorithmIdentifier) + * OCTET STRING (SignatureValue) + */ +static bool read_pkcs7(uint8_t* pkcs7_der, size_t pkcs7_der_len, uint8_t** sig_der, + size_t* sig_der_length) { + asn1_context_t* ctx = asn1_context_new(pkcs7_der, pkcs7_der_len); + if (ctx == NULL) { + return false; + } + + asn1_context_t* pkcs7_seq = asn1_sequence_get(ctx); + if (pkcs7_seq != NULL && asn1_sequence_next(pkcs7_seq)) { + asn1_context_t *signed_data_app = asn1_constructed_get(pkcs7_seq); + if (signed_data_app != NULL) { + asn1_context_t* signed_data_seq = asn1_sequence_get(signed_data_app); + if (signed_data_seq != NULL + && asn1_sequence_next(signed_data_seq) + && asn1_sequence_next(signed_data_seq) + && asn1_sequence_next(signed_data_seq) + && asn1_constructed_skip_all(signed_data_seq)) { + asn1_context_t *sig_set = asn1_set_get(signed_data_seq); + if (sig_set != NULL) { + asn1_context_t* sig_seq = asn1_sequence_get(sig_set); + if (sig_seq != NULL + && asn1_sequence_next(sig_seq) + && asn1_sequence_next(sig_seq) + && asn1_sequence_next(sig_seq) + && asn1_sequence_next(sig_seq)) { + uint8_t* sig_der_ptr; + if (asn1_octet_string_get(sig_seq, &sig_der_ptr, sig_der_length)) { + *sig_der = (uint8_t*) malloc(*sig_der_length); + if (*sig_der != NULL) { + memcpy(*sig_der, sig_der_ptr, *sig_der_length); + } + } + asn1_context_free(sig_seq); + } + asn1_context_free(sig_set); + } + asn1_context_free(signed_data_seq); + } + asn1_context_free(signed_data_app); + } + asn1_context_free(pkcs7_seq); + } + asn1_context_free(ctx); + + return *sig_der != NULL; +} + +// Look for an RSA signature embedded in the .ZIP file comment given +// the path to the zip. Verify it matches one of the given public +// keys. +// +// Return VERIFY_SUCCESS, VERIFY_FAILURE (if any error is encountered +// or no key matches the signature). +int verify_file(unsigned char* addr, size_t length) { + //ui->SetProgress(0.0); + + int numKeys; + Certificate* pKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys); + if (pKeys == NULL) { + LOGE("Failed to load keys\n"); + return INSTALL_CORRUPT; + } + LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE); + + // An archive with a whole-file signature will end in six bytes: + // + // (2-byte signature start) $ff $ff (2-byte comment size) + // + // (As far as the ZIP format is concerned, these are part of the + // archive comment.) We start by reading this footer, this tells + // us how far back from the end we have to start reading to find + // the whole comment. + +#define FOOTER_SIZE 6 + + if (length < FOOTER_SIZE) { + LOGE("not big enough to contain footer\n"); + return VERIFY_FAILURE; + } + + unsigned char* footer = addr + length - FOOTER_SIZE; + + if (footer[2] != 0xff || footer[3] != 0xff) { + LOGE("footer is wrong\n"); + return VERIFY_FAILURE; + } + + size_t comment_size = footer[4] + (footer[5] << 8); + size_t signature_start = footer[0] + (footer[1] << 8); + LOGI("comment is %zu bytes; signature %zu bytes from end\n", + comment_size, signature_start); + + if (signature_start <= FOOTER_SIZE) { + LOGE("Signature start is in the footer"); + return VERIFY_FAILURE; + } + +#define EOCD_HEADER_SIZE 22 + + // The end-of-central-directory record is 22 bytes plus any + // comment length. + size_t eocd_size = comment_size + EOCD_HEADER_SIZE; + + if (length < eocd_size) { + LOGE("not big enough to contain EOCD\n"); + return VERIFY_FAILURE; + } + + // Determine how much of the file is covered by the signature. + // This is everything except the signature data and length, which + // includes all of the EOCD except for the comment length field (2 + // bytes) and the comment data. + size_t signed_len = length - eocd_size + EOCD_HEADER_SIZE - 2; + + unsigned char* eocd = addr + length - eocd_size; + + // If this is really is the EOCD record, it will begin with the + // magic number $50 $4b $05 $06. + if (eocd[0] != 0x50 || eocd[1] != 0x4b || + eocd[2] != 0x05 || eocd[3] != 0x06) { + LOGE("signature length doesn't match EOCD marker\n"); + return VERIFY_FAILURE; + } + + size_t i; + for (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, minzip will find the later (wrong) one, + // which could be exploitable. Fail verification if + // this sequence occurs anywhere after the real one. + LOGE("EOCD marker occurs after start of EOCD\n"); + return VERIFY_FAILURE; + } + } + +#define BUFFER_SIZE 4096 + + bool need_sha1 = false; + bool need_sha256 = false; + for (i = 0; i < numKeys; ++i) { + switch (pKeys[i].hash_len) { + case SHA_DIGEST_SIZE: need_sha1 = true; break; + case SHA256_DIGEST_SIZE: need_sha256 = true; break; + } + } + + SHA_CTX sha1_ctx; + SHA256_CTX sha256_ctx; + SHA_init(&sha1_ctx); + SHA256_init(&sha256_ctx); + + double frac = -1.0; + size_t so_far = 0; + while (so_far < signed_len) { + size_t size = signed_len - so_far; + if (size > BUFFER_SIZE) size = BUFFER_SIZE; + + if (need_sha1) SHA_update(&sha1_ctx, addr + so_far, size); + if (need_sha256) SHA256_update(&sha256_ctx, addr + so_far, size); + so_far += size; + + double f = so_far / (double)signed_len; + if (f > frac + 0.02 || size == so_far) { + //ui->SetProgress(f); + frac = f; + } + } + + const uint8_t* sha1 = SHA_final(&sha1_ctx); + const uint8_t* sha256 = SHA256_final(&sha256_ctx); + + uint8_t* sig_der = NULL; + size_t sig_der_length = 0; + + size_t signature_size = signature_start - FOOTER_SIZE; + if (!read_pkcs7(eocd + eocd_size - signature_start, signature_size, &sig_der, + &sig_der_length)) { + LOGE("Could not find signature DER block\n"); + return VERIFY_FAILURE; + } + + /* + * Check to make sure at least one of the keys matches the signature. Since + * any key can match, we need to try each before determining a verification + * failure has happened. + */ + for (i = 0; i < numKeys; ++i) { + const uint8_t* hash; + switch (pKeys[i].hash_len) { + case SHA_DIGEST_SIZE: hash = sha1; break; + case SHA256_DIGEST_SIZE: hash = sha256; break; + default: continue; + } + + // The 6 bytes is the "(signature_start) $ff $ff (comment_size)" that + // the signing tool appends after the signature itself. + if (pKeys[i].key_type == Certificate::RSA) { + if (sig_der_length < RSANUMBYTES) { + // "signature" block isn't big enough to contain an RSA block. + LOGI("signature is too short for RSA key %zu\n", i); + continue; + } + + if (!RSA_verify(pKeys[i].rsa, sig_der, RSANUMBYTES, + hash, pKeys[i].hash_len)) { + LOGI("failed to verify against RSA key %zu\n", i); + continue; + } + + LOGI("whole-file signature verified against RSA key %zu\n", i); + free(sig_der); + return VERIFY_SUCCESS; + } else if (pKeys[i].key_type == Certificate::EC + && pKeys[i].hash_len == SHA256_DIGEST_SIZE) { + p256_int r, s; + if (!dsa_sig_unpack(sig_der, sig_der_length, &r, &s)) { + LOGI("Not a DSA signature block for EC key %zu\n", i); + continue; + } + + p256_int p256_hash; + p256_from_bin(hash, &p256_hash); + if (!p256_ecdsa_verify(&(pKeys[i].ec->x), &(pKeys[i].ec->y), + &p256_hash, &r, &s)) { + LOGI("failed to verify against EC key %zu\n", i); + continue; + } + + LOGI("whole-file signature verified against EC key %zu\n", i); + free(sig_der); + return VERIFY_SUCCESS; + } else { + LOGI("Unknown key type %d\n", pKeys[i].key_type); + } + LOGI("i: %i, eocd_size: %i, RSANUMBYTES: %i\n", i, eocd_size, RSANUMBYTES); + } + free(sig_der); + LOGE("failed to verify whole-file signature\n"); + return VERIFY_FAILURE; +} + +// Reads a file containing one or more public keys as produced by +// DumpPublicKey: this is an RSAPublicKey struct as it would appear +// as a C source literal, eg: +// +// "{64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}" +// +// For key versions newer than the original 2048-bit e=3 keys +// supported by Android, the string is preceded by a version +// identifier, eg: +// +// "v2 {64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}" +// +// (Note that the braces and commas in this example are actual +// characters the parser expects to find in the file; the ellipses +// indicate more numbers omitted from this example.) +// +// The file may contain multiple keys in this format, separated by +// commas. The last key must not be followed by a comma. +// +// A Certificate is a pair of an RSAPublicKey and a particular hash +// (we support SHA-1 and SHA-256; we store the hash length to signify +// which is being used). The hash used is implied by the version number. +// +// 1: 2048-bit RSA key with e=3 and SHA-1 hash +// 2: 2048-bit RSA key with e=65537 and SHA-1 hash +// 3: 2048-bit RSA key with e=3 and SHA-256 hash +// 4: 2048-bit RSA key with e=65537 and SHA-256 hash +// 5: 256-bit EC key using the NIST P-256 curve parameters and SHA-256 hash +// +// Returns NULL if the file failed to parse, or if it contain zero keys. +Certificate* +load_keys(const char* filename, int* numKeys) { + Certificate* out = NULL; + *numKeys = 0; + + FILE* f = fopen(filename, "r"); + if (f == NULL) { + LOGE("opening %s: %s\n", filename, strerror(errno)); + goto exit; + } + + { + int i; + bool done = false; + while (!done) { + ++*numKeys; + out = (Certificate*)realloc(out, *numKeys * sizeof(Certificate)); + Certificate* cert = out + (*numKeys - 1); + memset(cert, '\0', sizeof(Certificate)); + + char start_char; + if (fscanf(f, " %c", &start_char) != 1) goto exit; + if (start_char == '{') { + // a version 1 key has no version specifier. + cert->key_type = Certificate::RSA; + cert->rsa = (RSAPublicKey*)malloc(sizeof(RSAPublicKey)); + cert->rsa->exponent = 3; + cert->hash_len = SHA_DIGEST_SIZE; + } else if (start_char == 'v') { + int version; + if (fscanf(f, "%d {", &version) != 1) goto exit; + switch (version) { + case 2: + cert->key_type = Certificate::RSA; + cert->rsa = (RSAPublicKey*)malloc(sizeof(RSAPublicKey)); + cert->rsa->exponent = 65537; + cert->hash_len = SHA_DIGEST_SIZE; + break; + case 3: + cert->key_type = Certificate::RSA; + cert->rsa = (RSAPublicKey*)malloc(sizeof(RSAPublicKey)); + cert->rsa->exponent = 3; + cert->hash_len = SHA256_DIGEST_SIZE; + break; + case 4: + cert->key_type = Certificate::RSA; + cert->rsa = (RSAPublicKey*)malloc(sizeof(RSAPublicKey)); + cert->rsa->exponent = 65537; + cert->hash_len = SHA256_DIGEST_SIZE; + break; + case 5: + cert->key_type = Certificate::EC; + cert->ec = (ECPublicKey*)calloc(1, sizeof(ECPublicKey)); + cert->hash_len = SHA256_DIGEST_SIZE; + break; + default: + goto exit; + } + } + + if (cert->key_type == Certificate::RSA) { + RSAPublicKey* key = cert->rsa; + if (fscanf(f, " %i , 0x%x , { %u", + &(key->len), &(key->n0inv), &(key->n[0])) != 3) { + goto exit; + } + if (key->len != RSANUMWORDS) { + LOGE("key length (%d) does not match expected size\n", key->len); + goto exit; + } + for (i = 1; i < key->len; ++i) { + if (fscanf(f, " , %u", &(key->n[i])) != 1) goto exit; + } + if (fscanf(f, " } , { %u", &(key->rr[0])) != 1) goto exit; + for (i = 1; i < key->len; ++i) { + if (fscanf(f, " , %u", &(key->rr[i])) != 1) goto exit; + } + fscanf(f, " } } "); + + LOGI("read key e=%d hash=%d\n", key->exponent, cert->hash_len); + } else if (cert->key_type == Certificate::EC) { + ECPublicKey* key = cert->ec; + int key_len; + unsigned int byte; + uint8_t x_bytes[P256_NBYTES]; + uint8_t y_bytes[P256_NBYTES]; + if (fscanf(f, " %i , { %u", &key_len, &byte) != 2) goto exit; + if (key_len != P256_NBYTES) { + LOGE("Key length (%d) does not match expected size %d\n", key_len, P256_NBYTES); + goto exit; + } + x_bytes[P256_NBYTES - 1] = byte; + for (i = P256_NBYTES - 2; i >= 0; --i) { + if (fscanf(f, " , %u", &byte) != 1) goto exit; + x_bytes[i] = byte; + } + if (fscanf(f, " } , { %u", &byte) != 1) goto exit; + y_bytes[P256_NBYTES - 1] = byte; + for (i = P256_NBYTES - 2; i >= 0; --i) { + if (fscanf(f, " , %u", &byte) != 1) goto exit; + y_bytes[i] = byte; + } + fscanf(f, " } } "); + p256_from_bin(x_bytes, &key->x); + p256_from_bin(y_bytes, &key->y); + } else { + LOGE("Unknown key type %d\n", cert->key_type); + goto exit; + } + + // if the line ends in a comma, this file has more keys. + switch (fgetc(f)) { + case ',': + // more keys to come. + break; + + case EOF: + done = true; + break; + + default: + LOGE("unexpected character between keys\n"); + goto exit; + } + } + } + + fclose(f); + return out; + +exit: + if (f) fclose(f); + free(out); + *numKeys = 0; + return NULL; +} diff --git a/oldverifier/verifier.h b/oldverifier/verifier.h new file mode 100644 index 000000000..17ab257ad --- /dev/null +++ b/oldverifier/verifier.h @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2008 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. + */ + +#ifndef _RECOVERY_VERIFIER_H +#define _RECOVERY_VERIFIER_H + +#include "mincrypt/p256.h" +#include "mincrypt/rsa.h" + +#define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary" + +enum { INSTALL_SUCCESS, INSTALL_ERROR, INSTALL_CORRUPT }; + +static const float VERIFICATION_PROGRESS_FRACTION = 0.25; + +typedef struct { + p256_int x; + p256_int y; +} ECPublicKey; + +typedef struct { + typedef enum { + RSA, + EC, + } KeyType; + + int hash_len; // SHA_DIGEST_SIZE (SHA-1) or SHA256_DIGEST_SIZE (SHA-256) + KeyType key_type; + RSAPublicKey* rsa; + ECPublicKey* ec; +} Certificate; + +/* addr and length define a an update package file that has been + * loaded (or mmap'ed, or whatever) into memory. Verify that the file + * is signed and the signature matches one of the given keys. Return + * one of the constants below. + */ +int verify_file(unsigned char* addr, size_t length); + +Certificate* load_keys(const char* filename, int* numKeys); + +#define VERIFY_SUCCESS 0 +#define VERIFY_FAILURE 1 + +#endif /* _RECOVERY_VERIFIER_H */ diff --git a/prebuilt/Android.mk b/prebuilt/Android.mk index c5b46dfbb..538be7177 100644 --- a/prebuilt/Android.mk +++ b/prebuilt/Android.mk @@ -117,8 +117,10 @@ else RELINK_SOURCE_FILES += $(TARGET_OUT_SHARED_LIBRARIES)/libcorkscrew.so endif endif -#RELINK_SOURCE_FILES += $(TARGET_OUT_SHARED_LIBRARIES)/libmincrypttwrp.so -#RELINK_SOURCE_FILES += $(TARGET_RECOVERY_ROOT_OUT)/sbin/toolbox +ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 24; echo $$?),0) + RELINK_SOURCE_FILES += $(TARGET_OUT_SHARED_LIBRARIES)/libmincrypttwrp.so +endif +RELINK_SOURCE_FILES += $(TARGET_RECOVERY_ROOT_OUT)/sbin/toolbox ifneq ($(TW_OEM_BUILD),true) RELINK_SOURCE_FILES += $(TARGET_RECOVERY_ROOT_OUT)/sbin/twrp else diff --git a/set_metadata.c b/set_metadata.cpp index 2e1d769d9..2e1d769d9 100644 --- a/set_metadata.c +++ b/set_metadata.cpp diff --git a/set_metadata.h b/set_metadata.h index 835c31cd7..9a46be995 100644 --- a/set_metadata.h +++ b/set_metadata.h @@ -28,18 +28,10 @@ #ifndef _RECOVERY_SET_CONTEXTS_H #define _RECOVERY_SET_CONTEXTS_H -#ifdef __cplusplus -extern "C" { -#endif - #include <sys/stat.h> #include "selinux/selinux.h" int tw_get_default_metadata(const char* filename); int tw_set_default_metadata(const char* filename); -#ifdef __cplusplus -} -#endif - #endif //_RECOVERY_SET_CONTEXTS_H diff --git a/toolbox/Android.mk b/toolbox/Android.mk index c721bbd41..4c3e433c2 100644 --- a/toolbox/Android.mk +++ b/toolbox/Android.mk @@ -2,9 +2,13 @@ TWRP_TOOLBOX_PATH := $(call my-dir) LOCAL_PATH := system/core/toolbox include $(CLEAR_VARS) -OUR_TOOLS := \ - start \ - stop \ +OUR_TOOLS := + +ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 24; echo $$?),0) + OUR_TOOLS := \ + start \ + stop +endif ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 23; echo $$?),0) OUR_TOOLS += \ @@ -227,6 +231,13 @@ ifeq ($(shell test $(PLATFORM_SDK_VERSION) -gt 22; echo $$?),0) LOCAL_SRC_FILES += ../../../$(TWRP_TOOLBOX_PATH)/ls.c endif endif +ifeq ($(shell test $(PLATFORM_SDK_VERSION) -gt 23; echo $$?),0) + # Rule for making start and stop in N trees + LOCAL_SRC_FILES += \ + ../../../$(TWRP_TOOLBOX_PATH)/start.c \ + ../../../$(TWRP_TOOLBOX_PATH)/stop.c + OUR_TOOLS += start stop +endif LOCAL_MODULE := toolbox_recovery LOCAL_MODULE_STEM := toolbox diff --git a/toolbox/start.c b/toolbox/start.c new file mode 100644 index 000000000..6c8a3f2b0 --- /dev/null +++ b/toolbox/start.c @@ -0,0 +1,21 @@ + +#include <string.h> +#include <stdio.h> +#include <stdlib.h> + +#include <cutils/properties.h> + +int start_main(int argc, char *argv[]) +{ + if(argc > 1) { + property_set("ctl.start", argv[1]); + } else { + /* defaults to starting the common services stopped by stop.c */ + property_set("ctl.start", "netd"); + property_set("ctl.start", "surfaceflinger"); + property_set("ctl.start", "zygote"); + property_set("ctl.start", "zygote_secondary"); + } + + return 0; +} diff --git a/toolbox/stop.c b/toolbox/stop.c new file mode 100644 index 000000000..5e3ce3c8c --- /dev/null +++ b/toolbox/stop.c @@ -0,0 +1,19 @@ +#include <stdio.h> +#include <string.h> + +#include <cutils/properties.h> + +int stop_main(int argc, char *argv[]) +{ + if(argc > 1) { + property_set("ctl.stop", argv[1]); + } else{ + /* defaults to stopping the common services */ + property_set("ctl.stop", "zygote_secondary"); + property_set("ctl.stop", "zygote"); + property_set("ctl.stop", "surfaceflinger"); + property_set("ctl.stop", "netd"); + } + + return 0; +} diff --git a/tools/recovery_l10n/Android.mk b/tools/recovery_l10n/Android.mk index 937abd1e1..9fb797cf3 100644 --- a/tools/recovery_l10n/Android.mk +++ b/tools/recovery_l10n/Android.mk @@ -1,5 +1,9 @@ # Copyright 2012 Google Inc. All Rights Reserved. + +# Prevent RecoveryLocalizer already defined errors in older trees +ifeq ($(shell test $(PLATFORM_SDK_VERSION) -ge 24; echo $$?),0) + LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) @@ -10,3 +14,5 @@ LOCAL_MODULE_TAGS := optional LOCAL_SRC_FILES := $(call all-java-files-under, src) include $(BUILD_PACKAGE) + +endif diff --git a/twinstall.cpp b/twinstall.cpp index 0162c767b..83cd383bc 100644 --- a/twinstall.cpp +++ b/twinstall.cpp @@ -32,7 +32,11 @@ #include "mtdutils/mtdutils.h" #include "minzip/SysUtil.h" #include "minzip/Zip.h" +#ifdef USE_OLD_VERIFIER +#include "oldverifier/verifier.h" +#else #include "verifier.h" +#endif #include "variables.h" #include "data.hpp" #include "partitions.hpp" @@ -40,9 +44,9 @@ #include "twrp-functions.hpp" #include "gui/gui.hpp" #include "gui/pages.hpp" +#include "legacy_property_service.h" extern "C" { #include "gui/gui.h" - #include "legacy_property_service.h" } static const char* properties_path = "/dev/__properties__"; diff --git a/twrp-functions.cpp b/twrp-functions.cpp index 8cc094fc2..fffd9b738 100644 --- a/twrp-functions.cpp +++ b/twrp-functions.cpp @@ -51,10 +51,10 @@ #ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS #include "openaes/inc/oaes_lib.h" #endif +#include "set_metadata.h" extern "C" { #include "libcrecovery/common.h" - #include "set_metadata.h" } /* Execute a command */ @@ -34,8 +34,8 @@ extern "C" { #include "gui/gui.h" -#include "set_metadata.h" } +#include "set_metadata.h" #include "gui/gui.hpp" #include "gui/pages.hpp" #include "gui/objects.hpp" diff --git a/twrpTar.cpp b/twrpTar.cpp index dcbb28202..d192f2cc5 100644 --- a/twrpTar.cpp +++ b/twrpTar.cpp @@ -21,7 +21,6 @@ extern "C" { #include "libtar/libtar.h" #include "twrpTar.h" #include "tarWrite.h" - #include "set_metadata.h" } #include <sys/types.h> #include <sys/stat.h> @@ -51,9 +50,7 @@ extern "C" { #ifndef BUILD_TWRPTAR_MAIN #include "data.hpp" #include "infomanager.hpp" -extern "C" { - #include "set_metadata.h" -} +#include "set_metadata.h" #endif //ndef BUILD_TWRPTAR_MAIN using namespace std; @@ -69,6 +66,9 @@ twrpTar::twrpTar(void) { Total_Backup_Size = 0; Archive_Current_Size = 0; include_root_dir = true; + tar_type.openfunc = open; + tar_type.closefunc = close; + tar_type.readfunc = read; } twrpTar::~twrpTar(void) { @@ -961,7 +961,7 @@ int twrpTar::createTar() { close(pipes[3]); fd = pipes[1]; init_libtar_no_buffer(progress_pipe_fd); - tar_type = { open, close, read, write_tar_no_buffer }; + tar_type.writefunc = write_tar_no_buffer; if(tar_fdopen(&t, fd, charRootDir, &tar_type, O_WRONLY | O_CREAT | O_EXCL | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TAR_GNU | TAR_STORE_SELINUX) != 0) { close(fd); LOGINFO("tar_fdopen failed\n"); @@ -1021,7 +1021,7 @@ int twrpTar::createTar() { close(pigzfd[0]); // close parent input fd = pigzfd[1]; // copy parent output init_libtar_no_buffer(progress_pipe_fd); - tar_type = { open, close, read, write_tar_no_buffer }; + tar_type.writefunc = write_tar_no_buffer; if(tar_fdopen(&t, fd, charRootDir, &tar_type, O_WRONLY | O_CREAT | O_EXCL | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TAR_GNU | TAR_STORE_SELINUX) != 0) { close(fd); LOGINFO("tar_fdopen failed\n"); @@ -1071,7 +1071,7 @@ int twrpTar::createTar() { close(oaesfd[0]); // close parent input fd = oaesfd[1]; // copy parent output init_libtar_no_buffer(progress_pipe_fd); - tar_type = { open, close, read, write_tar_no_buffer }; + tar_type.writefunc = write_tar_no_buffer; if(tar_fdopen(&t, fd, charRootDir, &tar_type, O_WRONLY | O_CREAT | O_EXCL | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TAR_GNU | TAR_STORE_SELINUX) != 0) { close(fd); LOGINFO("tar_fdopen failed\n"); @@ -1083,10 +1083,9 @@ int twrpTar::createTar() { } else { // Not compressed or encrypted init_libtar_buffer(0, progress_pipe_fd); - tar_type = { open, close, read, write_tar }; if (part_settings->adbbackup) { LOGINFO("Opening TW_ADB_BACKUP uncompressed stream\n"); - tar_type = { open, close, read, write_tar_no_buffer }; + tar_type.writefunc = write_tar_no_buffer; output_fd = open(TW_ADB_BACKUP, O_WRONLY); if(tar_fdopen(&t, output_fd, charRootDir, &tar_type, O_WRONLY | O_CREAT | O_EXCL | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TAR_GNU | TAR_STORE_SELINUX) != 0) { close(output_fd); @@ -1095,6 +1094,7 @@ int twrpTar::createTar() { } } else { + tar_type.writefunc = write_tar; if (tar_open(&t, charTarFile, &tar_type, O_WRONLY | O_CREAT | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, TAR_GNU | TAR_STORE_SELINUX) == -1) { LOGERR("tar_open error opening '%s'\n", tarfn.c_str()); gui_err("backup_error=Error creating backup."); |