Skip to content

Mimic CRuby's numeric hashing#6305

Merged
headius merged 1 commit intojruby:jruby-9.2from
headius:numeric_rand_hash
Jun 30, 2020
Merged

Mimic CRuby's numeric hashing#6305
headius merged 1 commit intojruby:jruby-9.2from
headius:numeric_rand_hash

Conversation

@headius
Copy link
Member

@headius headius commented Jun 29, 2020

This PR makes changes to RubyFixnum, RubyBignum, and RubyFloat hashing to incorporate the per-runtime random seed, in an effort to mimic CRuby's fixes for CVE-2011-4815.

This CVE was part of a flurry of "hashDOS" fixes that happened in the early 2010s to address poor hash use by application frameworks like Rails. Because untrusted data from web requests was blindly used for internal hashing, these frameworks could be DOS'ed by predicting the hashes of strings and other data and tricking Ruby into putting all elements in the same linear-scanned hash box.

The change here roughly mimics the CRuby logic for combining a random seed with the 64-bit long representation of these core numeric types, which in turn propagates that randomness to the composite types like Complex and Rational.

We may want to make this randomization optional, since we have had requests like #590 to allow users to opt into repeatable hashing.

This PR can safely merge to master.

See #6304 for the security specs failing without this change. This PR addresses all the failing numeric hash security specs.

@headius headius added this to the JRuby 9.2.12.0 milestone Jun 29, 2020
headius added a commit to headius/jruby that referenced this pull request Jun 29, 2020
This corrects the error raised for the case where the unpack spec
requests a size that's outside the string, which relates to
CVE-2018-8778.

The other two cases that raised the ArgumentError for "outside of
string" appear to be correct, since making them raise RangeError
causes spec failures.

This addresses a security spec failure as shown in jruby#6305.
@headius headius force-pushed the numeric_rand_hash branch from f224f3a to 067f752 Compare June 29, 2020 13:17
CRuby modified the hashing of core numeric types to incorporate a
random seed, as part of the flurry of "hashDOS" fixes in the early
2010s. This commit mimics those changes.

See jruby#6304 for the failing security spec.
@headius
Copy link
Member Author

headius commented Jun 30, 2020

I will modify this to put the random seed logic behind the same "consistent hash" option, so it can be disabled.

@headius
Copy link
Member Author

headius commented Jun 30, 2020

Aha, the random seed property jruby.consistent.hashing actually modifies how we acquire the seed; when on, it uses a static value. This is ready to go.

[] ~/projects/jruby $ jruby -e "1000.upto(1010) {|i| puts i.hash}"
5850910177678188037
-4122927456021001207
4349978983989361182
-5623858649709827614
2849047790300534774
-7124789843398654470
1348116596611707855
-8625721037087481389
-152814597077119065
8320091842933243307
-1653745790765945921

[] ~/projects/jruby $ jruby -e "1000.upto(1010) {|i| puts i.hash}"
-7227042336866544064
-2770157313365699149
1686727710135145767
6143612733635990664
-7846246316572715779
-3389361293071870866
1067523730428974035
5524408753929818948
-8465450296278887767
-4008565272778042854
448319750722802060

[] ~/projects/jruby $ jruby -Xconsistent.hashing -e "1000.upto(1010) {|i| puts i.hash}"
-2980970509909832607
-1858700091923459759
-736429673937086975
385840744049285873
1508111162035658657
2630381580022031505
3752651998008404289
4874922415994777137
5997192833981149921
7119463251967522769
8241733669953895553

[] ~/projects/jruby $ jruby -Xconsistent.hashing -e "1000.upto(1010) {|i| puts i.hash}"
-2980970509909832607
-1858700091923459759
-736429673937086975
385840744049285873
1508111162035658657
2630381580022031505
3752651998008404289
4874922415994777137
5997192833981149921
7119463251967522769
8241733669953895553

@headius headius merged commit ab9dc4a into jruby:jruby-9.2 Jun 30, 2020
@headius headius deleted the numeric_rand_hash branch June 30, 2020 20:55
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.

1 participant