LibSSH: Fix off-by-one in read_packet's payload length check - #26903
Open
Chandan3456 wants to merge 1 commit into
Open
LibSSH: Fix off-by-one in read_packet's payload length check#26903Chandan3456 wants to merge 1 commit into
Chandan3456 wants to merge 1 commit into
Conversation
The payload is packet_length - padding_length - 1, but the guard only rejected packet_length == padding_length. A packet declaring length 5 with 4 bytes of padding therefore returned a zero-length payload, and a padding_length larger than packet_length wrapped the subtraction to just under 4 GiB before it reached ByteBuffer::create_uninitialized.
Comment on lines
+52
to
54
| // "byte[n1] payload; n1 = packet_length - padding_length - 1" | ||
| if (packet_length <= padding_length + 1u) | ||
| return Error::from_string_literal("Packet doesn't have a payload"); |
Member
There was a problem hiding this comment.
Thanks for catching this and the tests!
Very small nit: do you mind moving that check just before we compute payload_length?
I think it's just a bit clearer this way (I also dropped the comment as this is literally the same thing as the code).
// "There MUST be at least four bytes of padding."
if (padding_length < 4)
return Error::from_string_literal("Padding length is too short");
if (packet_length <= padding_length + 1u)
return Error::from_string_literal("Packet doesn't have a payload");
auto payload_length = packet_length - padding_length - 1;
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.
read_packet computes the payload as packet_length - padding_length - 1 but only rejects packet_length == padding_length, so the guard is one byte short. length 5 with padding 4 slips through and returns a zero-length payload, which breaks the size >= 1 invariant unpack_generic_message asserts on and that handle_new_keys_message relies on when it indexes payload[0]. padding_length can also just exceed packet_length: 4 with padding 255 wraps the subtraction and asks create_uninitialized for 4294967044 bytes off a nine byte packet, which asan flags as an allocation-size-too-big at Peer.cpp:60. both land before authentication since the transport is still on the identity cipher until NEWKEYS. found it reading the framing code against rfc 4253 section 6.