fix: make encode_item panic/error-safe so it never exposes uninitialized header bytes#2742
Open
mvanhorn wants to merge 1 commit into
Open
fix: make encode_item panic/error-safe so it never exposes uninitialized header bytes#2742mvanhorn wants to merge 1 commit into
mvanhorn wants to merge 1 commit into
Conversation
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.
Motivation
encode_itemintonic/src/codec/encode.rsreserves a 5-byte frame header viabuf.reserve(HEADER_SIZE)and advances the buffer length over those bytes before the message payload is encoded. If encoding then fails, either by returning an error or by panicking inside a user-suppliedEncoder/compression codec, theBytesMutis left with its length advanced over header bytes that were never written. Those bytes hold whatever was previously in the buffer's spare capacity. On the error path the frame is abandoned; on an unwinding panic that a caller catches (or that crosses acatch_unwindboundary in a surrounding runtime) the buffer can subsequently be observed with a partially-formed frame whose header is uninitialized leftover memory.Closes #2720.
Solution
Wrap the header reservation and payload encoding in a drop guard that records the buffer's original length up front and truncates back to it unless
encode_itemreaches its normal return. On the success path the guard is disarmed and the fully-framed buffer is preserved; on any early return or unwinding panic the guard'sDroprestores the buffer to exactly the bytes that were present before the call, so no half-written frame or uninitialized header is ever left behind.Regression tests cover the five relevant paths: successful framing (header + payload written), encoder error,
finish_encoding/framing error raised late, encoder panic (buffer restored after unwinding), and compressed-encoder failure with thegzipfeature enabled.cargo test -p tonic codec::encodepasses with default features (4 tests) and with--features gzip(5 tests);cargo fmt --checkandgit diff --checkare clean.