Fix undefined behaviour reading bool flags from untrusted backup blobs#8
Open
d33mobile wants to merge 1 commit into
Open
Fix undefined behaviour reading bool flags from untrusted backup blobs#8d33mobile wants to merge 1 commit into
d33mobile wants to merge 1 commit into
Conversation
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug
to_key_record()instorage/backup.ccopies an importedbackup_key_tinto akey_record_tby loading theis_enabled/is_adminfields (declaredbool) directly:The backup blob is untrusted input: it arrives over the serial
import-keyscommand as a base64 payload. Its flag bytes can hold any value, not just0or1. Loading aboolwhose object representation is not0/1is undefined behaviour in C. A crafted backup whose flag byte is e.g.67but whose header CRC is valid reaches this load and trips the sanitizer:The fix
Read the two flag bytes as raw
uint8_tviamemcpyand canonicalise with!= 0, so the resultingboolalways has a valid representation:Regression test
Added a case to the existing
test/harness_storage.c(RAM-backed littlefs harness that runsstorage.c+backup.cundermake -C test asan, i.e.-fsanitize=address,undefined). It builds a valid 1-key backup, pokes a non-bool byte (67) into a flag, refreshes the header CRC so the blob still validates, imports it, and asserts the import succeeds and the flag reads back canonicalised (exactly1). With the fixmake -C test asanis green; temporarily reverting the fix makes the same test fail with the UBSannot a valid value for type '_Bool'error, confirming the guard works.Severity
Low practical impact: a non-canonical bool byte is still truthy, so there is no auth bypass or logic change from this path. The value of the fix is removing the undefined behaviour and canonicalising the stored flags.