From eb74c1a8a5fd49f5ebbf771824ef0ba0007c429e Mon Sep 17 00:00:00 2001 From: Martin Milata Date: Fri, 25 Oct 2024 01:12:55 +0200 Subject: [PATCH] fix(core): unaligned array in protobuf decoder [no changelog] --- common/protob/pb2py | 2 +- core/embed/rust/src/protobuf/defs.rs | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/common/protob/pb2py b/common/protob/pb2py index d6cbbde171e..6be88695d96 100755 --- a/common/protob/pb2py +++ b/common/protob/pb2py @@ -93,7 +93,7 @@ if not PROTOC: PROTOC_PREFIX = Path(PROTOC).resolve().parent.parent -ENUM_ENTRY = c.PrefixedArray(c.Byte, c.Int16ul) +ENUM_ENTRY = c.PrefixedArray(c.Int16ul, c.Int16ul) FIELD_STRUCT = c.Struct( "tag" / c.Byte, diff --git a/core/embed/rust/src/protobuf/defs.rs b/core/embed/rust/src/protobuf/defs.rs index acf4165683d..6b6097af141 100644 --- a/core/embed/rust/src/protobuf/defs.rs +++ b/core/embed/rust/src/protobuf/defs.rs @@ -209,7 +209,7 @@ pub unsafe fn get_msg(msg_offset: u16) -> MsgDef { unsafe fn get_enum(enum_offset: u16) -> EnumDef { // #[repr(C, packed)] // struct EnumDef { - // count: u8, + // count: u16, // vals: [u16], // } @@ -217,8 +217,10 @@ unsafe fn get_enum(enum_offset: u16) -> EnumDef { // definition inside `ENUM_DEFS`. unsafe { let ptr = ENUM_DEFS.as_ptr().add(enum_offset as usize); - let count = ptr.offset(0).read() as usize; - let vals = ptr.offset(1); + let count_lo = ptr.offset(0).read(); + let count_hi = ptr.offset(1).read(); + let count = u16::from_le_bytes([count_lo, count_hi]) as usize; + let vals = ptr.offset(2); EnumDef { values: slice::from_raw_parts(vals.cast(), count),