fix(solana): instruction data misalignment
What changed, and why it matters
This commit fixes a bug in how Trezor's Solana app read transaction instructions. Previously, if an instruction's data was shorter than its expected ID header, the parser skipped reading the data entirely and treated leftover bytes as the next instruction. That could misalign the whole instruction stream and potentially trick the device into showing or signing a different transaction than the user expects. The fix reads the full data first and only splits off the instruction ID later, marking too-short data as unsupported so it requires blind signing.
Treat this as a security-relevant parser hardening fix. Review whether the misalignment could have been exploited in practice to bypass UI warnings or induce unintended signatures, and consider whether a security advisory or firmware release note is warranted. Ensure the new blind-signing path cannot be abused to silently downgrade supported instructions.
Security signals we found
Parser misalignment when instruction data shorter than expected instruction-id length
Unread bytes re-parsed as next instruction, risking incorrect transaction interpretation
Fix moves instruction-id parsing to a later stage after full data is captured
Short-data instructions now classified as unsupported and trigger blind signing
New unit tests cover both normal and malformed short-data cases
Evidence from the diff
In core/src/apps/solana/transaction/__init__.py, _parse_instructions previously called get_instruction_id_length(program_id) while parsing and consumed the instruction-id bytes before reading the remaining instruction data. When data_length was smaller than instruction_id_length, it set instruction_id = None and then read max(0, data_length - instruction_id_length) bytes—i.e., zero bytes—leaving the short data unread in the buffer. Those unread bytes were then parsed as the start of the next instruction, causing stream misalignment. The patch moves the ID split into _create_instructions, where the whole data is first stored in raw_instructions and only later split into instruction_id and instruction_data. If data is too short for the program’s ID length, instruction_id is set to None, the instruction is shown as unsupported, and blind_signing is set. A new test verifies both normal ID splitting and the short-data case.
Changed components
Trezor firmware Solana transaction parsercore/src/apps/solana/transaction/__init__.pycore/src/apps/solana/types.pycore/tests/test_apps.solana.transaction.pyInspect captured patch +59 / −30
### core/src/apps/solana/transaction/__init__.py
@@ -121,31 +121,14 @@ def _parse_instructions(self, serialized_tx_reader: BufferReader) -> None:
for _ in range(num_of_instructions):
program_index = serialized_tx_reader.get()
- program_id = base58.encode(self.addresses[program_index][0])
+
num_of_accounts = parse_var_int(serialized_tx_reader)
- accounts: list[int] = []
- for _ in range(num_of_accounts):
- account_index = serialized_tx_reader.get()
- accounts.append(account_index)
+ accounts = serialized_tx_reader.read_memoryview(num_of_accounts)
data_length = parse_var_int(serialized_tx_reader)
+ data = serialized_tx_reader.read_memoryview(data_length)
- instruction_id_length = get_instruction_id_length(program_id)
- if 0 < instruction_id_length <= data_length:
- instruction_id = int.from_bytes(
- serialized_tx_reader.read_memoryview(instruction_id_length),
- "little",
- )
- else:
- instruction_id = None
-
- instruction_data = serialized_tx_reader.read_memoryview(
- max(0, data_length - instruction_id_length)
- )
-
- self.raw_instructions.append(
- (program_index, instruction_id, accounts, instruction_data)
- )
+ self.raw_instructions.append((program_index, list(accounts), data))
def _parse_address_lookup_tables(self, serialized_tx: BufferReader) -> None:
self.address_lookup_tables_rw_addresses = []
@@ -181,13 +164,20 @@ def _create_instructions(self) -> None:
)
self.instructions = []
- for (
- program_index,
- instruction_id,
- accounts,
- instruction_data,
- ) in self.raw_instructions:
+ for program_index, accounts, instruction_data in self.raw_instructions:
program_id = base58.encode(self.addresses[program_index][0])
+
+ instruction_id_length = get_instruction_id_length(program_id)
+ if 0 < instruction_id_length <= len(instruction_data):
+ instruction_id = int.from_bytes(
+ instruction_data[:instruction_id_length], "little"
+ )
+ instruction_data = instruction_data[instruction_id_length:]
+ else:
+ # The program has no instruction ids, or the data is too short
+ # to contain one. The latter is shown as an unsupported instruction.
+ instruction_id = None
+
instruction_accounts = [
combined_accounts[account_index] for account_index in accounts
]
### core/src/apps/solana/types.py
@@ -19,9 +19,7 @@
InstructionId = int | None
AccountIndex = int
InstructionData = memoryview
- RawInstruction = tuple[
- ProgramIndex, InstructionId, list[AccountIndex], InstructionData
- ]
+ RawInstruction = tuple[ProgramIndex, list[AccountIndex], InstructionData]
T = TypeVar("T")
else:
### core/tests/test_apps.solana.transaction.py
@@ -203,5 +203,46 @@ def test_invalid(self):
Transaction(build_tx(header, TRANSFER_ACCOUNTS, [TRANSFER_INSTRUCTION]))
+@unittest.skipUnless(not utils.BITCOIN_ONLY, "altcoin")
+class TestSolanaTransactionInstructions(unittest.TestCase):
+ def test_instruction_id(self):
+ transaction = Transaction(
+ build_tx((1, 0, 1), TRANSFER_ACCOUNTS, [TRANSFER_INSTRUCTION])
+ )
+
+ # The parsed instruction keeps the whole data.
+ ((program_index, accounts, data),) = transaction.raw_instructions
+ self.assertEqual(program_index, 2)
+ self.assertEqual(accounts, [0, 1])
+ self.assertEqual(bytes(data), TRANSFER_INSTRUCTION[2])
+
+ # The instruction id is split off when the instruction is created.
+ (transfer,) = transaction.instructions
+ self.assertEqual(transfer.instruction_id, 2)
+ self.assertEqual(bytes(transfer.instruction_data), (1000).to_bytes(8, "little"))
+
+ def test_data_shorter_than_instruction_id(self):
+ # System Program instruction ids take 4 bytes.
+ short_instruction = (2, (0, 1), b"\x02\x00")
+ transaction = Transaction(
+ build_tx(
+ (1, 0, 1),
+ TRANSFER_ACCOUNTS,
+ [short_instruction, TRANSFER_INSTRUCTION],
+ )
+ )
+ short, transfer = transaction.instructions
+
+ # The whole data is kept and the instruction is shown as unsupported.
+ self.assertIsNone(short.instruction_id)
+ self.assertFalse(short.is_instruction_supported)
+ self.assertEqual(bytes(short.instruction_data), b"\x02\x00")
+ self.assertTrue(transaction.blind_signing)
+
+ # The following instruction is not affected.
+ self.assertEqual(transfer.instruction_id, 2)
+ self.assertEqual(transfer.lamports, 1000)
+
+
if __name__ == "__main__":
unittest.main()Why this scored 61/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.