fix(noise): size handshake read buffer with correct overhead - #631
Open
dimartiro wants to merge 1 commit into
Open
fix(noise): size handshake read buffer with correct overhead#631dimartiro wants to merge 1 commit into
dimartiro wants to merge 1 commit into
Conversation
lexnv
reviewed
Jul 8, 2026
| // Noise decryption strips the 16-byte AEAD (Poly1305) tag, so the decrypted | ||
| // plaintext is never larger than the ciphertext. `message.len()` is therefore | ||
| // a guaranteed-sufficient upper bound for the output buffer. | ||
| let mut out = BytesMut::zeroed(message.len()); |
Collaborator
There was a problem hiding this comment.
Could you please double check for running nodes in Kusama and Polkadot? 🙏 I would simply place info logs for before and after with the actual length.
IIRC the actual message we receive is bigger than the message.len() hence the added 200, which would otherwise trigger a reallocation and affect performance
Contributor
Author
There was a problem hiding this comment.
The out buffer holds the decrypted plaintext, not the received frame — and in Noise the plaintext is always smaller than the ciphertext, so message.len() can't be exceeded. I traced this in snow 0.9.6:
_read_messagewrites tooutonly in the finaldecrypt_and_mix_hash(ptr, payload)(handshakestate.rs:445); the ephemeral/static DH keys go to internal buffers (self.re/self.rs), never toout.ptris a suffix ofmessage, soptr.len() <= message.len().- The size check is
out.len() >= ciphertext.len() - TAGLENwithTAGLEN = 16(cipherstate.rs:53), soout.len() = message.len()is sufficient with margin. read_messagetakes a fixed&mut [u8]; an undersized buffer returnsError::Decrypt, never a reallocation. So the old+200was pure over-allocation, not realloc-avoidance.
Happy to add temporary before/after info logs on a Kusama/Polkadot node if you'd still like empirical confirmation, but the bound is provable from the snow source.
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.
Description
read_handshake_messageallocated its decryption output buffer asmessage.len() + 200— an arbitrary overhead flagged with aTODO.Closes #332