SFT-8680: check derivation path length before indexing into it
What changed, and why it matters
This commit fixes a bug in the Passport hardware wallet's BIP-322 message signing code. Previously, when parsing a PSBT (a Bitcoin data format), the code could read past the end of an empty or too-short 'derivation path' field because it checked the first byte before confirming the field was long enough. The fix reorders the checks so the length is validated before any byte is accessed. This prevents a potential crash or undefined behavior when handling a malformed signing request.
Treat as a low-to-moderate reliability/security hardening fix. Review whether the crash could be triggered from an untrusted source (e.g., a scanned QR code or USB/NFC PSBT) and confirm the device fails safely rather than entering an unhandled exception. No immediate CVE is indicated by the commit alone, but a defensive advisory note may be warranted if the malformed input is attacker-controllable.
Security signals we found
Out-of-bounds read / index error on untrusted PSBT input
Validation order bug: first-byte check before length check
BIP-322 signing request parsing hardened
New unit tests for empty and one-byte derivation values
Evidence from the diff
In bip322.py::read_signing_request, the original validation was if derivation[0] != 0 or (len(derivation) - 1) % 4 or not 2 <= count <= MAX_PATH_DEPTH. Because derivation[0] was evaluated first, an empty derivation (length 0) or a single-byte derivation would cause an out-of-bounds read/indexing before the length checks could reject it. The patch moves the length/count checks (not 2 <= count <= MAX_PATH_DEPTH and (len(derivation) - 1) % 4) ahead of derivation[0] != 0, ensuring malformed inputs are rejected safely. Tests are added for b'' and b'\x00'.
Changed components
ports/stm32/boards/Passport/modules/bip322.pyports/stm32/boards/Passport/modules/tests/unit/bip322.pyports/stm32/boards/Passport/modules/tests/unit/bip322_request.pyInspect captured patch +8 / −4
### ports/stm32/boards/Passport/modules/bip322.py
@@ -181,7 +181,7 @@ def read_signing_request(psbt):
raise ValueError(INVALID_REQUEST)
derivation = derivations[0]
count = (len(derivation) - 1) // 4
- if derivation[0] != 0 or (len(derivation) - 1) % 4 or not 2 <= count <= MAX_PATH_DEPTH:
+ if not 2 <= count <= MAX_PATH_DEPTH or (len(derivation) - 1) % 4 or derivation[0] != 0:
raise ValueError(INVALID_REQUEST)
xfp_path = list(unpack_from('<%dI' % count, derivation, 1))
### ports/stm32/boards/Passport/modules/tests/unit/bip322.py
@@ -289,7 +289,7 @@ def rejected(psbt, error):
rejected(serialize((global_map, input_map, output_map)), 'Unsupported signature hash type.')
# Exactly one key-path derivation below a master fingerprint.
-for bad_derivation in (derivation(leaves=1), derivation(path=[]), derivation()[:-2]):
+for bad_derivation in (b'', b'\x00', derivation(leaves=1), derivation(path=[]), derivation()[:-2]):
global_map, input_map, output_map = request_maps(message, message_challenge)
input_map[2] = (input_map[2][0], bad_derivation)
rejected(serialize((global_map, input_map, output_map)), INVALID_REQUEST)
### ports/stm32/boards/Passport/modules/tests/unit/bip322_request.py
@@ -47,12 +47,14 @@ def set(self, key, value):
MESSAGE = 'Proof of reserves for tb1p'
-def request_psbt(message=MESSAGE.encode(), path=PATH, script=SCRIPT):
+def request_psbt(message=MESSAGE.encode(), path=PATH, script=SCRIPT, derivation_value=None):
_, to_sign = create_virtual_transactions(message, script)
+ if derivation_value is None:
+ derivation_value = b'\x00' + pack('<%dI' % (1 + len(path)), XFP, *path)
entries = ((b'\x00', to_sign), (b'\x09', message), None,
(b'\x01', pack('<q', 0) + ser_string(script)),
(b'\x03', pack('<I', SIGHASH_ALL)),
- (b'\x16' + INTERNAL_PUBKEY, b'\x00' + pack('<%dI' % (1 + len(path)), XFP, *path)),
+ (b'\x16' + INTERNAL_PUBKEY, derivation_value),
(b'\x17', INTERNAL_PUBKEY), None, None)
out = b'psbt\xff'
for entry in entries:
@@ -150,6 +152,8 @@ async def run_task_tests():
KeyNode.public_key_bytes = b'\x02' + INTERNAL_PUBKEY
assert (await validate(b'psbt\xff'))[1] == INVALID_REQUEST
+ for derivation_value in (b'', b'\x00'):
+ assert await validate(request_psbt(derivation_value=derivation_value)) == (None, INVALID_REQUEST)
# Signing returns the request's PSBT with the key-path signature added to its input.
signed, address, error = await sign(request)Why this scored 59/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.