Move Map hash seed to runtime - #1716
Open
yanjiew1 wants to merge 1 commit into
Open
Conversation
Map and Set objects can be accessed from a context other than the one that created them. With a per-context hash seed, insertion and lookup may select different buckets. Store the seed on JSRuntime so every context sharing these objects uses the same hash seed. Signed-off-by: Yan-Jie Wang <yanjiewtw@gmail.com>
bnoordhuis
approved these changes
Sep 13, 2026
bnoordhuis
left a comment
Contributor
There was a problem hiding this comment.
LGTM with comments.
The reason I put the hash seed on JSContext instead of JSRuntime is that the latter is often far longer-lived, making it easier to guess.
I suppose that can be mitigated to some extent by reseeding in JS_FreeContext if the context being freed is the runtime's last context.
Comment on lines
+2338
to
+2341
| random_state = js__gettimeofday_us(); | ||
| if (random_state == 0) | ||
| random_state = 1; | ||
| rt->hash_seed = xorshift64star(&random_state); |
Contributor
There was a problem hiding this comment.
You can simplify this to:
Suggested change
| random_state = js__gettimeofday_us(); | |
| if (random_state == 0) | |
| random_state = 1; | |
| rt->hash_seed = xorshift64star(&random_state); | |
| // TODO(bnoordhuis) use getrandom() etc. | |
| rt->hash_seed = js__gettimeofday_us(); |
Using xorshift doesn't add any additional protection; if an adversary can guess the startup timestamp, he can also apply a xorshift to it.
| { | ||
| JSRuntime *rt; | ||
| JSMallocState ms; | ||
| uint64_t random_state; |
Contributor
There was a problem hiding this comment.
Suggested change
| uint64_t random_state; |
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.
Map and Set objects can be accessed from a context other than the one that created them. With a per-context hash seed, insertion and lookup may select different buckets.
Store the seed on JSRuntime so every context sharing these objects uses the same hash seed.