Skip to content

Move Map hash seed to runtime - #1716

Open
yanjiew1 wants to merge 1 commit into
quickjs-ng:masterfrom
yanjiew1:hash_seed
Open

Move Map hash seed to runtime#1716
yanjiew1 wants to merge 1 commit into
quickjs-ng:masterfrom
yanjiew1:hash_seed

Conversation

@yanjiew1

@yanjiew1 yanjiew1 commented Sep 7, 2026

Copy link
Copy Markdown

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.

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 bnoordhuis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 thread quickjs.c
Comment on lines +2338 to +2341
random_state = js__gettimeofday_us();
if (random_state == 0)
random_state = 1;
rt->hash_seed = xorshift64star(&random_state);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread quickjs.c
{
JSRuntime *rt;
JSMallocState ms;
uint64_t random_state;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
uint64_t random_state;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants