hardening: mark append_fixed_slice_as_bytes as unsafe and document safety requirements#20
Open
MegaRedHand wants to merge 2 commits into
Open
hardening: mark append_fixed_slice_as_bytes as unsafe and document safety requirements#20MegaRedHand wants to merge 2 commits into
append_fixed_slice_as_bytes as unsafe and document safety requirements#20MegaRedHand wants to merge 2 commits into
Conversation
decode_byte_array_vec: drop "no padding" (not a soundness requirement for the decode direction; [u8; N] is valid for any bit pattern). append_fixed_slice_as_bytes: keep "no padding" (the encode direction reads T as bytes, and the Reference allows padding in valid T values to hold uninitialized bytes, making the u8 reinterpretation UB).
unsafe blocksappend_fixed_slice_as_bytes as unsafe and document safety requirements
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.
Summary
This PR marks
append_fixed_slice_as_bytesasunsafeand documents the safety requirements for both remainingunsafeblocks in the SSZ encode/decode paths.append_fixed_slice_as_bytesreinterprets&[T]as&[u8]viafrom_raw_parts. This is only sound ifThas no padding bytes, because the Reference allows padding within a validTto hold uninitialized bytes, and a&[u8]covering uninitialized bytes violates the requirement that a reference must point to valid values.decode_byte_array_vecusescopy_nonoverlapping+set_lento bulk-decodeVec<[u8; N]>. Since[u8; N]is trivially valid for any bit pattern, no safety contract is needed on the function itself; the// SAFETY:comment documents the bounds check.Changes
encode.rsappend_fixed_slice_as_bytesunsafe fn, added# Safetydoc ("T must not have padding"), added// SAFETY:at each call sitedecode.rsdecode_byte_array_vec// SAFETY:comment documenting the bounds invariant