From f117962eb25bde75e981c3bff91ba708a55df65e Mon Sep 17 00:00:00 2001 From: Ethan Yonker Date: Thu, 25 Aug 2016 15:32:21 -0500 Subject: Make it backwards compatible with 4.4 Change-Id: I668604cddc8e8afbf78709f3f872bea4e9f4aa06 --- Android.mk | 76 +++++-- adb_install.cpp | 4 + bootloader.cpp | 19 +- etc/Android.mk | 20 ++ etc/init.rc | 4 +- etc/init.recovery.servicenew.rc | 5 + etc/init.recovery.serviceold.rc | 4 + fuse_sideload.cpp | 15 ++ fuse_sideload.h | 9 + gui/action.cpp | 2 +- legacy_property_service.c | 216 ------------------ legacy_property_service.cpp | 220 +++++++++++++++++++ minadbd.old/fuse_adb_provider.c | 2 +- oldverifier/verifier.cpp | 471 ++++++++++++++++++++++++++++++++++++++++ oldverifier/verifier.h | 58 +++++ prebuilt/Android.mk | 6 +- set_metadata.c | 89 -------- set_metadata.cpp | 89 ++++++++ set_metadata.h | 8 - toolbox/Android.mk | 17 +- toolbox/start.c | 21 ++ toolbox/stop.c | 19 ++ tools/recovery_l10n/Android.mk | 6 + twinstall.cpp | 6 +- twrp-functions.cpp | 2 +- twrp.cpp | 2 +- twrpTar.cpp | 18 +- 27 files changed, 1044 insertions(+), 364 deletions(-) create mode 100644 etc/init.recovery.servicenew.rc create mode 100644 etc/init.recovery.serviceold.rc delete mode 100644 legacy_property_service.c create mode 100644 legacy_property_service.cpp create mode 100644 oldverifier/verifier.cpp create mode 100644 oldverifier/verifier.h delete mode 100644 set_metadata.c create mode 100644 set_metadata.cpp create mode 100644 toolbox/start.c create mode 100644 toolbox/stop.c 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(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(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 #include +#ifdef USE_MINCRYPT +#include "mincrypt/sha256.h" +#define SHA256_DIGEST_LENGTH SHA256_DIGEST_SIZE +#else #include +#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.c deleted file mode 100644 index 490a24524..000000000 --- a/legacy_property_service.c +++ /dev/null @@ -1,216 +0,0 @@ -/* - * Copyright (C) 2007 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 -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "../../bionic/libc/private/bionic_futex.h" - -#include - -#include "legacy_properties.h" - -#include -// Not available in 5.0 -//#include -#include "legacy_property_service.h" - -static int persistent_properties_loaded = 0; -static int property_area_inited = 0; - -static int property_set_fd = -1; - -typedef struct { - void *data; - size_t size; - int fd; -} workspace; - -static int init_workspace(workspace *w, size_t size) -{ - void *data; - int fd; - - /* dev is a tmpfs that we can use to carve a shared workspace - * out of, so let's do that... - */ - fd = open("/dev/__legacy_properties__", O_RDWR | O_CREAT, 0600); - if (fd < 0) - return -1; - - if (ftruncate(fd, size) < 0) - goto out; - - data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); - if(data == MAP_FAILED) - goto out; - - close(fd); - - fd = open("/dev/__legacy_properties__", O_RDONLY); - if (fd < 0) - return -1; - - unlink("/dev/__legacy_properties__"); - - w->data = data; - w->size = size; - w->fd = fd; - return 0; - -out: - close(fd); - return -1; -} - -/* (8 header words + 247 toc words) = 1020 bytes */ -/* 1024 bytes header and toc + 247 prop_infos @ 128 bytes = 32640 bytes */ - -#define PA_COUNT_MAX 247 -#define PA_INFO_START 1024 -#define PA_SIZE 32768 - -static workspace pa_workspace; -static prop_info *pa_info_array; - -prop_area *__legacy_property_area__; - -static int init_property_area(void) -{ - prop_area *pa; - - if(pa_info_array) - return -1; - - if(init_workspace(&pa_workspace, PA_SIZE)) - return -1; - - fcntl(pa_workspace.fd, F_SETFD, FD_CLOEXEC); - - pa_info_array = (void*) (((char*) pa_workspace.data) + PA_INFO_START); - - pa = pa_workspace.data; - memset(pa, 0, PA_SIZE); - pa->magic = PROP_AREA_MAGIC; - pa->version = PROP_AREA_VERSION; - - /* plug into the lib property services */ - __legacy_property_area__ = pa; - property_area_inited = 1; - return 0; -} - -static void update_prop_info(prop_info *pi, const char *value, unsigned len) -{ - pi->serial = pi->serial | 1; - memcpy(pi->value, value, len + 1); - pi->serial = (len << 24) | ((pi->serial + 1) & 0xffffff); - __futex_wake(&pi->serial, INT32_MAX); -} - -static const prop_info *__legacy_property_find(const char *name) -{ - prop_area *pa = __legacy_property_area__; - unsigned count = pa->count; - unsigned *toc = pa->toc; - unsigned len = strlen(name); - prop_info *pi; - - while(count--) { - unsigned entry = *toc++; - if(TOC_NAME_LEN(entry) != len) continue; - - pi = TOC_TO_INFO(pa, entry); - if(memcmp(name, pi->name, len)) continue; - - return pi; - } - - return 0; -} - -static int legacy_property_set(const char *name, const char *value) -{ - prop_area *pa; - prop_info *pi; - - int namelen = strlen(name); - int valuelen = strlen(value); - - if(namelen >= PROP_NAME_MAX) return -1; - if(valuelen >= PROP_VALUE_MAX) return -1; - if(namelen < 1) return -1; - - pi = (prop_info*) __legacy_property_find(name); - - - if(pi != 0) { - /* ro.* properties may NEVER be modified once set */ - if(!strncmp(name, "ro.", 3)) return -1; - - pa = __legacy_property_area__; - update_prop_info(pi, value, valuelen); - pa->serial++; - __futex_wake(&pa->serial, INT32_MAX); - } else { - pa = __legacy_property_area__; - if(pa->count == PA_COUNT_MAX) return -1; - - pi = pa_info_array + pa->count; - pi->serial = (valuelen << 24); - memcpy(pi->name, name, namelen + 1); - memcpy(pi->value, value, valuelen + 1); - - pa->toc[pa->count] = - (namelen << 24) | (((unsigned long) pi) - ((unsigned long) pa)); - - pa->count++; - pa->serial++; - __futex_wake(&pa->serial, INT32_MAX); - } - - return 0; -} - -void legacy_get_property_workspace(int *fd, int *sz) -{ - *fd = pa_workspace.fd; - *sz = pa_workspace.size; -} - -static void copy_property_to_legacy(const char *key, const char *value, void *cookie) -{ - legacy_property_set(key, value); -} - -int legacy_properties_init() -{ - if(init_property_area() != 0) - return -1; - - if(property_list(copy_property_to_legacy, 0) != 0) - return -1; - - return 0; -} diff --git a/legacy_property_service.cpp b/legacy_property_service.cpp new file mode 100644 index 000000000..90a0d47df --- /dev/null +++ b/legacy_property_service.cpp @@ -0,0 +1,220 @@ +/* + * Copyright (C) 2007 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../../bionic/libc/private/bionic_futex.h" + +#include + +#include "legacy_properties.h" + +#include +// Not available in 5.0 +//#include +#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; + +static int property_set_fd = -1; + +typedef struct { + void *data; + size_t size; + int fd; +} workspace; + +static int init_workspace(workspace *w, size_t size) +{ + void *data; + int fd; + + /* dev is a tmpfs that we can use to carve a shared workspace + * out of, so let's do that... + */ + fd = open("/dev/__legacy_properties__", O_RDWR | O_CREAT, 0600); + if (fd < 0) + return -1; + + if (ftruncate(fd, size) < 0) + goto out; + + data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if(data == MAP_FAILED) + goto out; + + close(fd); + + fd = open("/dev/__legacy_properties__", O_RDONLY); + if (fd < 0) + return -1; + + unlink("/dev/__legacy_properties__"); + + w->data = data; + w->size = size; + w->fd = fd; + return 0; + +out: + close(fd); + return -1; +} + +/* (8 header words + 247 toc words) = 1020 bytes */ +/* 1024 bytes header and toc + 247 prop_infos @ 128 bytes = 32640 bytes */ + +#define PA_COUNT_MAX 247 +#define PA_INFO_START 1024 +#define PA_SIZE 32768 + +static workspace pa_workspace; +static prop_info *pa_info_array; + +prop_area *__legacy_property_area__; + +static int init_property_area(void) +{ + prop_area *pa; + + if(pa_info_array) + return -1; + + if(init_workspace(&pa_workspace, PA_SIZE)) + return -1; + + fcntl(pa_workspace.fd, F_SETFD, FD_CLOEXEC); + + pa_info_array = (prop_info*) (((char*) pa_workspace.data) + PA_INFO_START); + + pa = (prop_area*)(pa_workspace.data); + memset(pa, 0, PA_SIZE); + pa->magic = PROP_AREA_MAGIC; + pa->version = PROP_AREA_VERSION; + + /* plug into the lib property services */ + __legacy_property_area__ = pa; + property_area_inited = 1; + return 0; +} + +static void update_prop_info(prop_info *pi, const char *value, unsigned len) +{ + pi->serial = pi->serial | 1; + memcpy(pi->value, value, len + 1); + pi->serial = (len << 24) | ((pi->serial + 1) & 0xffffff); + __futex_wake(&pi->serial, INT32_MAX); +} + +static const prop_info *__legacy_property_find(const char *name) +{ + prop_area *pa = __legacy_property_area__; + unsigned count = pa->count; + unsigned *toc = pa->toc; + unsigned len = strlen(name); + prop_info *pi; + + while(count--) { + unsigned entry = *toc++; + if(TOC_NAME_LEN(entry) != len) continue; + + pi = TOC_TO_INFO(pa, entry); + if(memcmp(name, pi->name, len)) continue; + + return pi; + } + + return 0; +} + +static int legacy_property_set(const char *name, const char *value) +{ + prop_area *pa; + prop_info *pi; + + int namelen = strlen(name); + int valuelen = strlen(value); + + if(namelen >= PROP_NAME_MAX) return -1; + if(valuelen >= PROP_VALUE_MAX) return -1; + if(namelen < 1) return -1; + + pi = (prop_info*) __legacy_property_find(name); + + + if(pi != 0) { + /* ro.* properties may NEVER be modified once set */ + if(!strncmp(name, "ro.", 3)) return -1; + + pa = __legacy_property_area__; + update_prop_info(pi, value, valuelen); + pa->serial++; + __futex_wake(&pa->serial, INT32_MAX); + } else { + pa = __legacy_property_area__; + if(pa->count == PA_COUNT_MAX) return -1; + + pi = pa_info_array + pa->count; + pi->serial = (valuelen << 24); + memcpy(pi->name, name, namelen + 1); + memcpy(pi->value, value, valuelen + 1); + + pa->toc[pa->count] = + (namelen << 24) | (((unsigned long) pi) - ((unsigned long) pa)); + + pa->count++; + pa->serial++; + __futex_wake(&pa->serial, INT32_MAX); + } + + return 0; +} + +void legacy_get_property_workspace(int *fd, int *sz) +{ + *fd = pa_workspace.fd; + *sz = pa_workspace.size; +} + +static void copy_property_to_legacy(const char *key, const char *value, void *cookie) +{ + legacy_property_set(key, value); +} + +int legacy_properties_init() +{ + if(init_property_area() != 0) + return -1; + + if(property_list(copy_property_to_legacy, 0) != 0) + return -1; + + return 0; +} 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 +#include +#include +#include + +//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.c deleted file mode 100644 index 2e1d769d9..000000000 --- a/set_metadata.c +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright (C) 2014 The Team Win Recovery 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. - */ - -/* - * The purpose of these functions is to try to get and set the proper - * file permissions, SELinux contexts, owner, and group so that these - * files are accessible when we boot up to normal Android via MTP and to - * file manager apps. During early boot we try to read the contexts and - * owner / group info from /data/media or from /data/media/0 and store - * them in static variables. From there, we'll try to set the same - * contexts, owner, and group information on most files we create during - * operations like backups, copying the log, and MTP operations. - */ - -#include -#include -#include -#include "selinux/selinux.h" - -static security_context_t selinux_context; -struct stat s; -static int has_stat = 0; - -int tw_get_context(const char* filename) { - if (lgetfilecon(filename, &selinux_context) >= 0) { - printf("tw_get_context got selinux context: %s\n", selinux_context); - return 0; - } else { - printf("tw_get_context failed to get selinux context\n"); - selinux_context = NULL; - } - return -1; -} - -int tw_get_stat(const char* filename) { - if (lstat(filename, &s) == 0) { - has_stat = 1; - return 0; - } - printf("tw_get_stat failed to lstat '%s'\n", filename); - return -1; -} - -int tw_get_default_metadata(const char* filename) { - if (tw_get_context(filename) == 0 && tw_get_stat(filename) == 0) - return 0; - return -1; -} - -// Most of this logging is disabled to prevent log spam if we are trying -// to set contexts and permissions on file systems that do not support -// these types of things (e.g. vfat / FAT / FAT32). -int tw_set_default_metadata(const char* filename) { - int ret = 0; - struct stat st; - - if (selinux_context == NULL) { - //printf("selinux_context was null, '%s'\n", filename); - ret = -1; - } else if (lsetfilecon(filename, selinux_context) < 0) { - //printf("Failed to set default contexts on '%s'.\n", filename); - ret = -1; - } - - if (lstat(filename, &st) == 0 && st.st_mode & S_IFREG && chmod(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH) < 0) { - //printf("Failed to chmod '%s'\n", filename); - ret = -1; - } - - if (has_stat && chown(filename, s.st_uid, s.st_gid) < 0) { - //printf("Failed to lchown '%s'.\n", filename); - ret = -1; - } - //printf("Done trying to set defaults on '%s'\n"); - return ret; -} diff --git a/set_metadata.cpp b/set_metadata.cpp new file mode 100644 index 000000000..2e1d769d9 --- /dev/null +++ b/set_metadata.cpp @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2014 The Team Win Recovery 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. + */ + +/* + * The purpose of these functions is to try to get and set the proper + * file permissions, SELinux contexts, owner, and group so that these + * files are accessible when we boot up to normal Android via MTP and to + * file manager apps. During early boot we try to read the contexts and + * owner / group info from /data/media or from /data/media/0 and store + * them in static variables. From there, we'll try to set the same + * contexts, owner, and group information on most files we create during + * operations like backups, copying the log, and MTP operations. + */ + +#include +#include +#include +#include "selinux/selinux.h" + +static security_context_t selinux_context; +struct stat s; +static int has_stat = 0; + +int tw_get_context(const char* filename) { + if (lgetfilecon(filename, &selinux_context) >= 0) { + printf("tw_get_context got selinux context: %s\n", selinux_context); + return 0; + } else { + printf("tw_get_context failed to get selinux context\n"); + selinux_context = NULL; + } + return -1; +} + +int tw_get_stat(const char* filename) { + if (lstat(filename, &s) == 0) { + has_stat = 1; + return 0; + } + printf("tw_get_stat failed to lstat '%s'\n", filename); + return -1; +} + +int tw_get_default_metadata(const char* filename) { + if (tw_get_context(filename) == 0 && tw_get_stat(filename) == 0) + return 0; + return -1; +} + +// Most of this logging is disabled to prevent log spam if we are trying +// to set contexts and permissions on file systems that do not support +// these types of things (e.g. vfat / FAT / FAT32). +int tw_set_default_metadata(const char* filename) { + int ret = 0; + struct stat st; + + if (selinux_context == NULL) { + //printf("selinux_context was null, '%s'\n", filename); + ret = -1; + } else if (lsetfilecon(filename, selinux_context) < 0) { + //printf("Failed to set default contexts on '%s'.\n", filename); + ret = -1; + } + + if (lstat(filename, &st) == 0 && st.st_mode & S_IFREG && chmod(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH) < 0) { + //printf("Failed to chmod '%s'\n", filename); + ret = -1; + } + + if (has_stat && chown(filename, s.st_uid, s.st_gid) < 0) { + //printf("Failed to lchown '%s'.\n", filename); + ret = -1; + } + //printf("Done trying to set defaults on '%s'\n"); + return ret; +} 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 #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 +#include +#include + +#include + +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 +#include + +#include + +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 */ diff --git a/twrp.cpp b/twrp.cpp index e7076d992..3de35af98 100644 --- a/twrp.cpp +++ b/twrp.cpp @@ -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 #include @@ -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."); -- cgit v1.2.3