Skip to content

Commit

Permalink
Support one kernelflinger image for multi VMs
Browse files Browse the repository at this point in the history
Some VM has TPM support requirement while others not, so
one Kernelflinger image should support multi VMs case

Test Done:
1. compile and boot
2. boot one KF image for multi-VMs

Tracked-On: OAM-126340
Signed-off-by: Chen, Gang G <[email protected]>
  • Loading branch information
GangSecurity committed Oct 12, 2024
1 parent ab62899 commit cb00ce9
Show file tree
Hide file tree
Showing 13 changed files with 198 additions and 171 deletions.
12 changes: 2 additions & 10 deletions Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ ifeq ($(TARGET_USE_TRUSTY),true)
KERNELFLINGER_CFLAGS += -DUSE_TRUSTY
endif

ifeq ($(TARGET_USE_IVSHMEM),true)
KERNELFLINGER_CFLAGS += -DUSE_IVSHMEM
endif

ifeq ($(TARGET_USE_MULTIBOOT),true)
KERNELFLINGER_CFLAGS += -DUSE_MULTIBOOT
endif
Expand Down Expand Up @@ -154,9 +150,7 @@ SHARED_STATIC_LIBRARIES := \
$(KERNELFLINGER_STATIC_LIBRARIES) \
libkernelflinger-$(TARGET_BUILD_VARIANT)

ifeq ($(TARGET_USE_TPM),true)
SHARED_STATIC_LIBRARIES += libedk2_tpm
endif
SHARED_STATIC_LIBRARIES += libedk2_tpm

include $(CLEAR_VARS)
LOCAL_MODULE := kernelflinger-$(TARGET_BUILD_VARIANT)
Expand Down Expand Up @@ -255,9 +249,7 @@ LOCAL_STATIC_LIBRARIES := \
libxbc-$(TARGET_BUILD_VARIANT)


ifeq ($(TARGET_USE_TPM),true)
SHARED_STATIC_LIBRARIES += libedk2_tpm
endif
SHARED_STATIC_LIBRARIES += libedk2_tpm

LOCAL_CFLAGS := $(SHARED_CFLAGS)
LOCAL_SRC_FILES := installer.c
Expand Down
24 changes: 12 additions & 12 deletions avb/libavb_user/uefi_avb_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@
#include "lib.h"
#include "log.h"
#include "security.h"
#ifdef USE_TPM
#include "tpm2_security.h"
#endif

extern char _binary_avb_pk_start;
extern char _binary_avb_pk_end;
Expand Down Expand Up @@ -257,11 +255,12 @@ static AvbIOResult read_rollback_index(__attribute__((unused)) AvbOps* ops,
if (is_live_boot())
ret = EFI_NOT_FOUND;
else {
#ifdef USE_TPM
ret = read_rollback_index_tpm2(rollback_index_slot, out_rollback_index);
#else
ret = read_efi_rollback_index(rollback_index_slot, out_rollback_index);
#endif
if (tee_tpm)
ret = tee_read_rollback_index_tpm2(rollback_index_slot, out_rollback_index);
else if (andr_tpm)
ret = read_rollback_index_tpm2(rollback_index_slot, out_rollback_index);
else
ret = read_efi_rollback_index(rollback_index_slot, out_rollback_index);
}

if (ret == EFI_NOT_FOUND) {
Expand All @@ -287,11 +286,12 @@ static AvbIOResult write_rollback_index(__attribute__((unused)) AvbOps* ops,
if (is_live_boot())
ret = EFI_SUCCESS;
else {
#ifdef USE_TPM
ret = write_rollback_index_tpm2(rollback_index_slot, rollback_index);
#else
ret = write_efi_rollback_index(rollback_index_slot, rollback_index);
#endif
if (tee_tpm)
ret = tee_write_rollback_index_tpm2(rollback_index_slot, rollback_index);
else if (andr_tpm)
ret = write_rollback_index_tpm2(rollback_index_slot, rollback_index);
else
ret = write_efi_rollback_index(rollback_index_slot, rollback_index);
}
if (EFI_ERROR(ret)) {
efi_perror(ret, L"Couldn't write rollback index");
Expand Down
2 changes: 0 additions & 2 deletions include/security.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,8 @@ EFI_STATUS init_rot_data(
/* Return rot data instance pointer*/
struct rot_data_t *get_rot_data();

#ifdef USE_IVSHMEM
EFI_STATUS ivsh_send_rot_data(IN VOID *bootimage, IN UINT8 boot_state,
IN VBDATA *vb_data);
#endif

EFI_STATUS raw_pub_key_sha256(
IN const UINT8 *pub_key,
Expand Down
22 changes: 20 additions & 2 deletions include/tpm2_security.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@
#include <lib.h>

#define TRUSTY_SEED_SIZE 32
extern BOOLEAN tee_tpm;
extern BOOLEAN andr_tpm;

EFI_STATUS tpm2_init(void);
EFI_STATUS tpm2_end(void);

#ifndef USE_IVSHMEM
EFI_STATUS tpm2_fuse_trusty_seed(void);
EFI_STATUS tpm2_read_trusty_seed(UINT8 seed[TRUSTY_SEED_SIZE]);
#endif

EFI_STATUS tpm2_fuse_perm_attr(void *data, uint32_t size);

Expand All @@ -66,4 +66,22 @@ EFI_STATUS tpm2_delete_index(UINT32 index);

EFI_STATUS tpm2_fuse_lock_owner(void);
EFI_STATUS tpm2_fuse_provision_seed(void);

EFI_STATUS tee_tpm2_init(void);
EFI_STATUS tee_tpm2_end(void);

EFI_STATUS tee_read_device_state_tpm2(UINT8 *state);
EFI_STATUS tee_write_device_state_tpm2(UINT8 state);
EFI_STATUS tee_read_rollback_index_tpm2(size_t rollback_index_slot, uint64_t *out_rollback_index);
EFI_STATUS tee_write_rollback_index_tpm2(size_t rollback_index_slot, uint64_t rollback_index);
BOOLEAN tee_tpm2_bootloader_need_init(void);

#ifndef USER
EFI_STATUS tee_tpm2_show_index(UINT32 index, uint8_t *out_buffer, UINTN out_buffer_size);
EFI_STATUS tee_tpm2_delete_index(UINT32 index);
#endif

EFI_STATUS tee_tpm2_fuse_lock_owner(void);
EFI_STATUS tee_tpm2_fuse_provision_seed(void);

#endif /* _TPM2_SECURITY_H_ */
118 changes: 80 additions & 38 deletions kernelflinger.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,15 @@
#include "uefi_utils.h"
#include "security_interface.h"
#include "security_efi.h"
#ifdef USE_TPM
#include "tpm2_security.h"
#endif
#ifdef USE_IVSHMEM
#include "ivshmem.h"

BOOLEAN tee_tpm = false;

#ifdef USE_TPM
BOOLEAN andr_tpm = true;
#else
BOOLEAN andr_tpm = false;
#endif

/* Ensure this is embedded in the EFI binary somewhere */
Expand Down Expand Up @@ -537,6 +541,8 @@ static enum boot_target check_command_line()
FIRMWARE_BOOTTIME,
BOOTREASON,
FIRMWARE_STATUS,
OPTEE,
TPM
};

struct Cmdline
Expand Down Expand Up @@ -592,6 +598,17 @@ static enum boot_target check_command_line()
strlen((CHAR8 *)"fw.status="),
FIRMWARE_STATUS
},
{
(CHAR8 *)"tee=",
strlen((CHAR8 *)"tee="),
OPTEE
},
{
(CHAR8 *)"tpm=",
strlen((CHAR8 *)"tpm="),
TPM
},

};

CHAR8 *nptr = NULL;
Expand Down Expand Up @@ -711,7 +728,29 @@ static enum boot_target check_command_line()
/* Parse "androidboot.bootreason=xxxxx " */
case BOOTREASON:
continue;
case OPTEE: {
UINT8 val;
nptr = (CHAR8 *)(arg8 + CmdlineArray[j].length);
val = (UINT8)strtoul((char *)nptr, 0, 10);

debug(L"optee TPM = %u\n", val);
if (val)
tee_tpm = true;
else
tee_tpm = false;
continue;
}
case TPM: {
UINT8 val;
nptr = (CHAR8 *)(arg8 + CmdlineArray[j].length);
val = (UINT8)strtoul((char *)nptr, 0, 10);
debug(L"Android TPM = %u\n", val);
if (val)
andr_tpm = true;
else
andr_tpm = false;
continue;
}
default:
continue;
}
Expand Down Expand Up @@ -1230,10 +1269,11 @@ static EFI_STATUS load_image(VOID *bootimage, VOID *vendorbootimage, UINT8 boot_
}
#endif

#ifdef USE_TPM
// Make sure the TPM2 is ended
tpm2_end();
#endif
if (tee_tpm)
tee_tpm2_end();
else if (andr_tpm)
tpm2_end();

debug(L"chainloading boot image, boot state is %s",
boot_state_to_string(boot_state));
Expand Down Expand Up @@ -1508,38 +1548,6 @@ EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table)
uefi_check_upgrade(g_loaded_image, BOOTLOADER_LABEL, KFUPDATE_FILE,
BOOTLOADER_FILE, BOOTLOADER_FILE_BAK, KFSELF_FILE, KFBACKUP_FILE);

#ifdef USE_IVSHMEM
ret = ivshmem_init();
if (EFI_ERROR(ret) && ret != EFI_NOT_FOUND) {
efi_perror(ret, L"Failed to init ivshmem, enter fastboot mode");
boot_target = FASTBOOT;
}
#endif

#ifdef USE_TPM
if (!is_live_boot()) {
ret = tpm2_init();
if (EFI_ERROR(ret) && ret != EFI_NOT_FOUND) {
efi_perror(ret, L"Failed to init TPM, enter fastboot mode");
boot_target = FASTBOOT;
}
}
#endif

need_lock = device_need_locked();

#ifndef USER
/* WA patch to set device as unlocked by default for userdebug build
*/
set_current_state(UNLOCKED);
#else
/* For civ, flash images to disk is not MUST. So set device to LOCKED
* state by default on the first boot.
*/
if (need_lock)
set_current_state(LOCKED);
#endif

ret = set_device_security_info(NULL);
if (EFI_ERROR(ret)) {
efi_perror(ret, L"Failed to init security info, enter fastboot mode");
Expand Down Expand Up @@ -1585,6 +1593,40 @@ EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table)
#endif
}

if (tee_tpm) {
debug(L"tee tpm enable, ivshmem_init#############");
ret = ivshmem_init();
if (EFI_ERROR(ret) && ret != EFI_NOT_FOUND) {
efi_perror(ret, L"Failed to init ivshmem, enter fastboot mode");
boot_target = FASTBOOT;
}
}

if (!is_live_boot() && (tee_tpm || andr_tpm)) {
if (tee_tpm)
ret = tee_tpm2_init();
else if (andr_tpm)
ret = tpm2_init();
if (EFI_ERROR(ret) && ret != EFI_NOT_FOUND) {
efi_perror(ret, L"Failed to init TPM, enter fastboot mode");
boot_target = FASTBOOT;
}
}

need_lock = device_need_locked();

#ifndef USER
/* WA patch to set device as unlocked by default for userdebug build
*/
set_current_state(UNLOCKED);
#else
/* For civ, flash images to disk is not MUST. So set device to LOCKED
* state by default on the first boot.
*/
if (need_lock)
set_current_state(LOCKED);
#endif

if (boot_target == POWER_OFF)
halt_system();

Expand Down
4 changes: 1 addition & 3 deletions libfastboot/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ SHARED_STATIC_LIBRARIES := \
libkernelflinger-$(TARGET_BUILD_VARIANT) \
libavb_kernelflinger-$(TARGET_BUILD_VARIANT)

ifeq ($(TARGET_USE_TPM),true)
SHARED_STATIC_LIBRARIES += libedk2_tpm
endif
SHARED_STATIC_LIBRARIES += libedk2_tpm

SHARED_SRC_FILES := \
fastboot.c \
Expand Down
35 changes: 21 additions & 14 deletions libfastboot/fastboot_flashing.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,21 +124,28 @@ EFI_STATUS change_device_state(enum device_state new_state, BOOLEAN interactive)
*/
for (int slot = 0; slot < 2; slot++) {
uint64_t idx;
#ifdef USE_TPM
ret = read_rollback_index_tpm2(slot, &idx);
if (EFI_SUCCESS == ret) {
ret = write_rollback_index_tpm2(slot, 0);
if (EFI_ERROR(ret))
return ret;
if (tee_tpm) {
ret = tee_read_rollback_index_tpm2(slot, &idx);
if (EFI_SUCCESS == ret) {
ret = tee_write_rollback_index_tpm2(slot, 0);
if (EFI_ERROR(ret))
return ret;
}
} else if (andr_tpm) {
ret = read_rollback_index_tpm2(slot, &idx);
if (EFI_SUCCESS == ret) {
ret = write_rollback_index_tpm2(slot, 0);
if (EFI_ERROR(ret))
return ret;
}
} else {
ret = read_efi_rollback_index(slot, &idx);
if (EFI_SUCCESS == ret) {
ret = write_efi_rollback_index(slot, 0);
if (EFI_ERROR(ret))
return ret;
}
}
#else
ret = read_efi_rollback_index(slot, &idx);
if (EFI_SUCCESS == ret) {
ret = write_efi_rollback_index(slot, 0);
if (EFI_ERROR(ret))
return ret;
}
#endif
}
}

Expand Down
Loading

0 comments on commit cb00ce9

Please sign in to comment.