Seed FlatHashtable's hash-to-slot mapping against hash-flooding - #12476
Draft
dougqh wants to merge 1 commit into
Draft
Seed FlatHashtable's hash-to-slot mapping against hash-flooding#12476dougqh wants to merge 1 commit into
dougqh wants to merge 1 commit into
Conversation
home()'s golden-ratio mix was a fixed, public function of the hash alone, so a strategy fed attacker-influenced keys (e.g. a String through the default hashCode, or CaseInsensitiveStringStrategy) let an attacker precompute a batch of keys that all land on the same slot, forcing every probe onto one long linear run -- an O(n^2) CPU-flooding vector, the same class of bug that motivated hash randomization elsewhere. Fold in a per-process random seed (ThreadLocalRandom, not SecureRandom -- no startup-blocking risk, and no cryptographic-strength guarantee is needed here) before the mix. It's a single process-wide constant, not per-table: every hash, however it was produced upstream, has to pass through this one chokepoint before it can influence probe placement, so seeding here closes the hole for every FlatHashtable/D1/D2 instance and every hash source (raw hashCode, HashingUtils/LongHashingUtils composites, cached Entry#hash) without threading a seed through any public API. Existing tests are unaffected: home() stays self-consistent within a single run, which is all they rely on.
This comment has been minimized.
This comment has been minimized.
Contributor
🟢 Java Benchmark SLOs — All performance SLOs passed
PR vs. master results
Commit: Load and DaCapo benchmarks can be triggered manually in the GitLab pipeline. Results will appear in the Benchmarking Platform UI after completion. |
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.
What Does This Do
Folds a per-process random seed into
FlatHashtable#home(the hash-to-slot mapping shared byFlatHashtable,D1, andD2), generated once at class-init viaThreadLocalRandom(fast, non-blocking — noSecureRandomstartup cost, and no cryptographic-strength guarantee is needed here).Adds
FlatHashtableTest#homeFoldsInProcessSeed, which pins down that the seed actually participates in the mix (not just that the golden-ratio mix runs) by comparing against what the old unseeded formula would have produced for the same input.Motivation
home()'s golden-ratio mix was previously a fixed, public function of the hash alone. When a table is keyed by attacker-influenced input (e.g.Stringkeys through the defaulthashCode, orCaseInsensitiveStringStrategy), an attacker who can predict or choose colliding hash values can craft a key set that all probe into the same bucket run, degrading every insert/lookup from O(1) to a long linear scan — the same class of algorithmic-complexity/CPU-flooding attack that motivated hash randomization elsewhere in the JDK ecosystem.Seeding is done once at the single
home()chokepoint rather than per-table or per-strategy: every hash — however it was produced upstream (a rawhashCode, a composite fromHashingUtils/LongHashingUtils, or a cachedEntry#hash) — has to pass throughhome()before it can influence probe placement, so this one change closes the gap for everyFlatHashtable/D1/D2instance and every hash source, with no public API change.This was raised as a follow-up finding from the
@Strategy/@StrategyConsumerannotation review (#12475) and split out into its own PR since it's an orthogonal, independently reviewable fix.Additional Notes
Existing tests are unaffected:
home()stays self-consistent within a single process/run, which is all they rely on (several precompute a hash that lands on a specific slot viahome()itself, so they adapt automatically to whatever seed that run has).Contributor Checklist
type:and (comp:orinst:) labels in addition to any other useful labelsclose,fix, or any linking keywords when referencing an issueJira ticket: [none]
🤖 Generated with Claude Code