Randomize CoseHeaderLabel hash codes#19
Closed
GrabYourPitchforks wants to merge 2 commits intomainfrom
Closed
Conversation
Owner
Author
|
Checked in to runtime. |
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
Dictionary<TKey, TValue>instances are not secure against untrusted inputs except for whenTKey = string. In our scenario, since COSE header values are adversary-provided and the COSE message is backed by anDictionary<CoseHeaderLabel, ...>populated by attacker-provided keys, this represents an algorithmic complexity (hash code collision) DoS attack vector.Note the demonstration below, which shows a clear O(n^2) algorithmic complexity as n entries are added to the map.
Discussion
The downlevel "generate a randomized string hash code value" logic could be a little faster, but at that point it would require dropping down to unsafe-equivalent code. I didn't want to do that here until we have real evidence that we need such an optimization.
I also didn't add a "validate that there aren't a significant number of collisions in the dictionary" unit test because they're very complicated and take a while to run. See here for an example of such a test elsewhere in our repo. I updated the existing
CoseHeaderLabel.GetHashCodeunit test so that it will catch the case where somebody inadvertently reverts the method logic. That should be sufficient here, but I'm open to suggestions if you feel this needs more coverage.