From a6410693b3109fc1a4ec0c9974151362a7732eda Mon Sep 17 00:00:00 2001 From: matejcik Date: Mon, 1 Aug 2022 16:29:46 +0200 Subject: [PATCH] Revert "feat(core): implement firmware dumping" This reverts commit 37c61c1381bf9f060687e6f7eea503ae77acb9ba. --- common/protob/messages-management.proto | 22 --------- common/protob/messages.proto | 4 +- core/.changelog.d/2433.removed | 1 + .../extmod/modtrezorutils/modtrezorutils.c | 47 ------------------- core/mocks/generated/trezorutils.pyi | 15 ------ core/src/all_modules.py | 2 - core/src/apps/misc/get_firmware.py | 47 ------------------- core/src/apps/workflow_handlers.py | 2 - core/src/trezor/enums/MessageType.py | 3 -- core/src/trezor/enums/__init__.py | 3 -- core/src/trezor/messages.py | 26 ---------- core/src/trezor/utils.py | 3 -- python/src/trezorlib/messages.py | 25 ---------- 13 files changed, 2 insertions(+), 198 deletions(-) create mode 100644 core/.changelog.d/2433.removed delete mode 100644 core/src/apps/misc/get_firmware.py diff --git a/common/protob/messages-management.proto b/common/protob/messages-management.proto index 8cab05d26..ae19c4c67 100644 --- a/common/protob/messages-management.proto +++ b/common/protob/messages-management.proto @@ -252,28 +252,6 @@ message FirmwareHash { required bytes hash = 1; } -/** - * Request: get firmware image. The firmware will send all chunks in sequence. - * @start - * @next FirmwareChunk - */ -message GetFirmware {} - -/** - * Response: firmware chunk. - * @next FirmwareChunkAck - */ -message FirmwareChunk { - required bytes chunk = 1; -} - -/** - * Request: acknowledge firmware chunk. - * @next FirmwareChunk - * @next Success - */ -message FirmwareChunkAck {} - /** * Request: Request device to wipe all sensitive data and settings * @start diff --git a/common/protob/messages.proto b/common/protob/messages.proto index b74dec494..60468bdd7 100644 --- a/common/protob/messages.proto +++ b/common/protob/messages.proto @@ -116,9 +116,7 @@ enum MessageType { MessageType_RebootToBootloader = 87 [(bitcoin_only) = true, (wire_in) = true]; MessageType_GetFirmwareHash = 88 [(bitcoin_only) = true, (wire_in) = true]; MessageType_FirmwareHash = 89 [(bitcoin_only) = true, (wire_out) = true]; - MessageType_GetFirmware = 90 [(bitcoin_only) = true, (wire_in) = true]; - MessageType_FirmwareChunk = 91 [(bitcoin_only) = true, (wire_out) = true]; - MessageType_FirmwareChunkAck = 92 [(bitcoin_only) = true, (wire_in) = true]; + reserved 90 to 92; MessageType_SetU2FCounter = 63 [(wire_in) = true]; MessageType_GetNextU2FCounter = 80 [(wire_in) = true]; diff --git a/core/.changelog.d/2433.removed b/core/.changelog.d/2433.removed new file mode 100644 index 000000000..6daff5d0a --- /dev/null +++ b/core/.changelog.d/2433.removed @@ -0,0 +1 @@ +Remove firmware dumping capability. diff --git a/core/embed/extmod/modtrezorutils/modtrezorutils.c b/core/embed/extmod/modtrezorutils/modtrezorutils.c index 0768ccf11..13e4ea4ea 100644 --- a/core/embed/extmod/modtrezorutils/modtrezorutils.c +++ b/core/embed/extmod/modtrezorutils/modtrezorutils.c @@ -208,46 +208,6 @@ STATIC mp_obj_t mod_trezorutils_firmware_vendor(void) { STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorutils_firmware_vendor_obj, mod_trezorutils_firmware_vendor); -/// def firmware_sector_size(sector: int) -> int: -/// """ -/// Returns the size of the firmware sector. -/// """ -STATIC mp_obj_t mod_trezorutils_firmware_sector_size(mp_obj_t sector) { - mp_uint_t sector_id = trezor_obj_get_uint(sector); - if (sector_id >= FIRMWARE_SECTORS_COUNT) { - mp_raise_msg(&mp_type_ValueError, "Invalid sector."); - } - return mp_obj_new_int(flash_sector_size(FIRMWARE_SECTORS[sector_id])); -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorutils_firmware_sector_size_obj, - mod_trezorutils_firmware_sector_size); - -/// def get_firmware_chunk(index: int, offset: int, buffer: bytearray) -> None: -/// """ -/// Reads a chunk of the firmware into `buffer`. -/// """ -STATIC mp_obj_t mod_trezorutils_get_firmware_chunk(const mp_obj_t index_obj, - const mp_obj_t offset_obj, - const mp_obj_t buffer) { - mp_uint_t index = trezor_obj_get_uint(index_obj); - if (index >= FIRMWARE_SECTORS_COUNT) { - mp_raise_msg(&mp_type_ValueError, "Invalid sector."); - } - int sector = FIRMWARE_SECTORS[index]; - mp_uint_t offset = trezor_obj_get_uint(offset_obj); - mp_buffer_info_t buf = {0}; - mp_get_buffer_raise(buffer, &buf, MP_BUFFER_WRITE); - const void *data = flash_get_address(sector, offset, buf.len); - if (data == NULL) { - mp_raise_msg(&mp_type_ValueError, "Invalid read."); - } - memcpy(buf.buf, data, buf.len); - - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorutils_get_firmware_chunk_obj, - mod_trezorutils_get_firmware_chunk); - /// def reboot_to_bootloader() -> None: /// """ /// Reboots to bootloader. @@ -271,7 +231,6 @@ STATIC mp_obj_str_t mod_trezorutils_revision_obj = { /// MODEL: str /// EMULATOR: bool /// BITCOIN_ONLY: bool -/// FIRMWARE_SECTORS_COUNT: int STATIC const mp_rom_map_elem_t mp_module_trezorutils_globals_table[] = { {MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_trezorutils)}, @@ -282,12 +241,6 @@ STATIC const mp_rom_map_elem_t mp_module_trezorutils_globals_table[] = { MP_ROM_PTR(&mod_trezorutils_firmware_hash_obj)}, {MP_ROM_QSTR(MP_QSTR_firmware_vendor), MP_ROM_PTR(&mod_trezorutils_firmware_vendor_obj)}, - {MP_ROM_QSTR(MP_QSTR_get_firmware_chunk), - MP_ROM_PTR(&mod_trezorutils_get_firmware_chunk_obj)}, - {MP_ROM_QSTR(MP_QSTR_firmware_sector_size), - MP_ROM_PTR(&mod_trezorutils_firmware_sector_size_obj)}, - {MP_ROM_QSTR(MP_QSTR_FIRMWARE_SECTORS_COUNT), - MP_ROM_INT(FIRMWARE_SECTORS_COUNT)}, {MP_ROM_QSTR(MP_QSTR_reboot_to_bootloader), MP_ROM_PTR(&mod_trezorutils_reboot_to_bootloader_obj)}, // various built-in constants diff --git a/core/mocks/generated/trezorutils.pyi b/core/mocks/generated/trezorutils.pyi index 50396a0b0..671eced57 100644 --- a/core/mocks/generated/trezorutils.pyi +++ b/core/mocks/generated/trezorutils.pyi @@ -60,20 +60,6 @@ def firmware_vendor() -> str: """ -# extmod/modtrezorutils/modtrezorutils.c -def firmware_sector_size(sector: int) -> int: - """ - Returns the size of the firmware sector. - """ - - -# extmod/modtrezorutils/modtrezorutils.c -def get_firmware_chunk(index: int, offset: int, buffer: bytearray) -> None: - """ - Reads a chunk of the firmware into `buffer`. - """ - - # extmod/modtrezorutils/modtrezorutils.c def reboot_to_bootloader() -> None: """ @@ -86,4 +72,3 @@ VERSION_PATCH: int MODEL: str EMULATOR: bool BITCOIN_ONLY: bool -FIRMWARE_SECTORS_COUNT: int diff --git a/core/src/all_modules.py b/core/src/all_modules.py index 5aeffb3e8..05ec259c2 100644 --- a/core/src/all_modules.py +++ b/core/src/all_modules.py @@ -401,8 +401,6 @@ apps.misc.get_ecdh_session_key import apps.misc.get_ecdh_session_key apps.misc.get_entropy import apps.misc.get_entropy -apps.misc.get_firmware -import apps.misc.get_firmware apps.misc.get_firmware_hash import apps.misc.get_firmware_hash apps.misc.sign_identity diff --git a/core/src/apps/misc/get_firmware.py b/core/src/apps/misc/get_firmware.py deleted file mode 100644 index eabcb6822..000000000 --- a/core/src/apps/misc/get_firmware.py +++ /dev/null @@ -1,47 +0,0 @@ -from micropython import const -from typing import TYPE_CHECKING - -from trezor import utils, wire, workflow -from trezor.messages import FirmwareChunk, FirmwareChunkAck, GetFirmware, Success -from trezor.ui.layouts import confirm_action, draw_simple_text - -from .get_firmware_hash import _render_progress - -if TYPE_CHECKING: - from trezor.wire import Context - -CHUNK_SIZE = const(1024 * 4) -# assuming that all sectors are of size 128 kB -PROGRESS_TOTAL = utils.FIRMWARE_SECTORS_COUNT * 128 * 1024 - - -async def get_firmware(ctx: Context, _msg: GetFirmware) -> Success: - await confirm_action( - ctx, - "dump_firmware", - title="Extract firmware", - action="Do you want to extract device firmware?", - description="Your seed will not be revealed.", - ) - sector_buffer = bytearray(CHUNK_SIZE) - packet = FirmwareChunk(chunk=sector_buffer) - - workflow.close_others() - draw_simple_text("Please wait") - - progress = 0 - _render_progress(progress, PROGRESS_TOTAL) - for i in range(utils.FIRMWARE_SECTORS_COUNT): - size = utils.firmware_sector_size(i) - try: - for ofs in range(0, size, CHUNK_SIZE): - utils.get_firmware_chunk(i, ofs, sector_buffer) - await ctx.call(packet, FirmwareChunkAck) - progress += CHUNK_SIZE - _render_progress(progress, PROGRESS_TOTAL) - # reset progress to known point, in case some sectors are not 128 kB - progress = (i + 1) * 128 * 1024 - _render_progress(progress, PROGRESS_TOTAL) - except ValueError: - raise wire.DataError("Failed to dump firmware.") - return Success(message="Firmware dumped.") diff --git a/core/src/apps/workflow_handlers.py b/core/src/apps/workflow_handlers.py index 22aa4a139..564c4cb1c 100644 --- a/core/src/apps/workflow_handlers.py +++ b/core/src/apps/workflow_handlers.py @@ -84,8 +84,6 @@ def find_message_handler_module(msg_type: int) -> str: return "apps.misc.cipher_key_value" if msg_type == MessageType.GetFirmwareHash: return "apps.misc.get_firmware_hash" - if msg_type == MessageType.GetFirmware: - return "apps.misc.get_firmware" if not utils.BITCOIN_ONLY: if msg_type == MessageType.SetU2FCounter: diff --git a/core/src/trezor/enums/MessageType.py b/core/src/trezor/enums/MessageType.py index 68d48bfb4..d0cece478 100644 --- a/core/src/trezor/enums/MessageType.py +++ b/core/src/trezor/enums/MessageType.py @@ -43,9 +43,6 @@ CancelAuthorization = 86 RebootToBootloader = 87 GetFirmwareHash = 88 FirmwareHash = 89 -GetFirmware = 90 -FirmwareChunk = 91 -FirmwareChunkAck = 92 FirmwareErase = 6 FirmwareUpload = 7 FirmwareRequest = 8 diff --git a/core/src/trezor/enums/__init__.py b/core/src/trezor/enums/__init__.py index 9b0045bb0..626c6d937 100644 --- a/core/src/trezor/enums/__init__.py +++ b/core/src/trezor/enums/__init__.py @@ -60,9 +60,6 @@ if TYPE_CHECKING: RebootToBootloader = 87 GetFirmwareHash = 88 FirmwareHash = 89 - GetFirmware = 90 - FirmwareChunk = 91 - FirmwareChunkAck = 92 SetU2FCounter = 63 GetNextU2FCounter = 80 NextU2FCounter = 81 diff --git a/core/src/trezor/messages.py b/core/src/trezor/messages.py index 295d82e3a..17b6e8f04 100644 --- a/core/src/trezor/messages.py +++ b/core/src/trezor/messages.py @@ -2276,32 +2276,6 @@ if TYPE_CHECKING: def is_type_of(cls, msg: protobuf.MessageType) -> TypeGuard["FirmwareHash"]: return isinstance(msg, cls) - class GetFirmware(protobuf.MessageType): - - @classmethod - def is_type_of(cls, msg: protobuf.MessageType) -> TypeGuard["GetFirmware"]: - return isinstance(msg, cls) - - class FirmwareChunk(protobuf.MessageType): - chunk: "bytes" - - def __init__( - self, - *, - chunk: "bytes", - ) -> None: - pass - - @classmethod - def is_type_of(cls, msg: protobuf.MessageType) -> TypeGuard["FirmwareChunk"]: - return isinstance(msg, cls) - - class FirmwareChunkAck(protobuf.MessageType): - - @classmethod - def is_type_of(cls, msg: protobuf.MessageType) -> TypeGuard["FirmwareChunkAck"]: - return isinstance(msg, cls) - class WipeDevice(protobuf.MessageType): @classmethod diff --git a/core/src/trezor/utils.py b/core/src/trezor/utils.py index ecd67208a..f96d792f4 100644 --- a/core/src/trezor/utils.py +++ b/core/src/trezor/utils.py @@ -3,7 +3,6 @@ import sys from trezorutils import ( # noqa: F401 BITCOIN_ONLY, EMULATOR, - FIRMWARE_SECTORS_COUNT, MODEL, SCM_REVISION, VERSION_MAJOR, @@ -11,9 +10,7 @@ from trezorutils import ( # noqa: F401 VERSION_PATCH, consteq, firmware_hash, - firmware_sector_size, firmware_vendor, - get_firmware_chunk, halt, memcpy, reboot_to_bootloader, diff --git a/python/src/trezorlib/messages.py b/python/src/trezorlib/messages.py index 76a8a4d38..b4afc81a5 100644 --- a/python/src/trezorlib/messages.py +++ b/python/src/trezorlib/messages.py @@ -68,9 +68,6 @@ class MessageType(IntEnum): RebootToBootloader = 87 GetFirmwareHash = 88 FirmwareHash = 89 - GetFirmware = 90 - FirmwareChunk = 91 - FirmwareChunkAck = 92 SetU2FCounter = 63 GetNextU2FCounter = 80 NextU2FCounter = 81 @@ -3333,28 +3330,6 @@ class FirmwareHash(protobuf.MessageType): self.hash = hash -class GetFirmware(protobuf.MessageType): - MESSAGE_WIRE_TYPE = 90 - - -class FirmwareChunk(protobuf.MessageType): - MESSAGE_WIRE_TYPE = 91 - FIELDS = { - 1: protobuf.Field("chunk", "bytes", repeated=False, required=True), - } - - def __init__( - self, - *, - chunk: "bytes", - ) -> None: - self.chunk = chunk - - -class FirmwareChunkAck(protobuf.MessageType): - MESSAGE_WIRE_TYPE = 92 - - class WipeDevice(protobuf.MessageType): MESSAGE_WIRE_TYPE = 5