Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): unaligned array in protobuf decoder #4283

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common/protob/pb2py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 5 additions & 3 deletions core/embed/rust/src/protobuf/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,16 +209,18 @@ 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],
// }

// SAFETY: `enum_offset` has to point to a beginning of a valid enum
// 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),
Expand Down
Loading