fix: s2n_shutdown should handle partial records #4421
Merged
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 of changes:
Fix yet another shutdown bug :/
When our bindings test failed, we realized that s2n_shutdown couldn't handle malformed records left in the input stuffers. I fixed that by just wiping the input stuffers: #4350
However, that broke another case we don't have tests for: If we have a partial record, we need to continue reading it rather than just wiping it. For example, we might have read just a header. If we wipe that header and attempt to start reading over, we'll be reading fragment data as header data. Since we instruct applications to call s2n_recv until it blocks, this is likely to be a common case. This is also unfortunately difficult to distinguish from "malformed record" from just the state of the input stuffers: we might have partial header data because of a partial read, or because we failed halfway through parsing the header, or both.
I fixed this problem by making two changes:
Call-outs:
Are there any other possible states for the record stuffers that s2n_shutdown could encounter? I keep missing cases :/ Please suggest states even if you think they're impossible / nonsensical. Let's get creative. s2n_shutdown is difficult because unlike other APIs, it is supposed to be called after an error so can't necessarily rely on sane state.
Testing:
I modified the existing test from #4350 to reflect the new behavior.
The possible states before s2n_shutdown is called that I'm now covering:
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.