ruby-changes:22330
From: knu <ko1@a...>
Date: Thu, 26 Jan 2012 16:10:17 +0900 (JST)
Subject: [ruby-changes:22330] knu:r34379 (ruby_1_8): Forward port r34151 from ruby_1_8_7.
knu 2012-01-26 16:10:03 +0900 (Thu, 26 Jan 2012) New Revision: 34379 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=34379 Log: Forward port r34151 from ruby_1_8_7. Modified files: branches/ruby_1_8/ChangeLog branches/ruby_1_8/inits.c branches/ruby_1_8/random.c branches/ruby_1_8/st.c branches/ruby_1_8/string.c branches/ruby_1_8/test/ruby/test_string.rb Index: ruby_1_8/ChangeLog =================================================================== --- ruby_1_8/ChangeLog (revision 34378) +++ ruby_1_8/ChangeLog (revision 34379) @@ -1,3 +1,29 @@ +Thu Jan 26 16:08:43 2012 URABE Shyouhei <shyouhei@r...> + + * string.c (rb_str_hash): randomize hash to avoid algorithmic + complexity attacks. CVE-2011-4815 + + * st.c (strhash): ditto. + + * string.c (Init_String): initialization of hash_seed to be at the + beginning of the process. + + * st.c (Init_st): ditto. + +Thu Jan 26 16:08:43 2012 Tanaka Akira <akr@f...> + + * inits.c (rb_call_inits): call Init_RandomSeed at first. + + * random.c (seed_initialized): defined. + (fill_random_seed): extracted from random_seed. + (make_seed_value): extracted from random_seed. + (rb_f_rand): initialize random seed at first. + (initial_seed): defined. + (Init_RandomSeed): defined. + (Init_RandomSeed2): defined. + (rb_reset_random_seed): defined. + (Init_Random): call Init_RandomSeed2. + Wed Nov 2 08:16:45 2011 Tanaka Akira <akr@f...> * lib/webrick/utils.rb: fix fcntl call. Index: ruby_1_8/inits.c =================================================================== --- ruby_1_8/inits.c (revision 34378) +++ ruby_1_8/inits.c (revision 34379) @@ -39,6 +39,7 @@ void Init_sym _((void)); void Init_process _((void)); void Init_Random _((void)); +void Init_RandomSeed _((void)); void Init_Range _((void)); void Init_Regexp _((void)); void Init_signal _((void)); @@ -47,10 +48,13 @@ void Init_Time _((void)); void Init_var_tables _((void)); void Init_version _((void)); +void Init_st _((void)); void rb_call_inits() { + Init_RandomSeed(); + Init_st(); Init_sym(); Init_var_tables(); Init_Object(); Index: ruby_1_8/string.c =================================================================== --- ruby_1_8/string.c (revision 34378) +++ ruby_1_8/string.c (revision 34379) @@ -919,13 +919,15 @@ return str1; } +static unsigned long hash_seed; + int rb_str_hash(str) VALUE str; { register long len = RSTRING(str)->len; register char *p = RSTRING(str)->ptr; - register int key = 0; + register unsigned long key = hash_seed; #if defined(HASH_ELFHASH) register unsigned int g; @@ -949,6 +951,7 @@ while (len--) { key = key*65599 + *p; p++; + key = (key << 13) | (key >> ((sizeof(unsigned long) * CHAR_BIT) - 13)); } key = key + (key>>5); #endif @@ -5413,4 +5416,6 @@ rb_define_method(rb_cSymbol, "downcase", sym_downcase, 0); rb_define_method(rb_cSymbol, "capitalize", sym_capitalize, 0); rb_define_method(rb_cSymbol, "swapcase", sym_swapcase, 0); + + hash_seed = rb_genrand_int32(); } Index: ruby_1_8/st.c =================================================================== --- ruby_1_8/st.c (revision 34378) +++ ruby_1_8/st.c (revision 34379) @@ -9,6 +9,7 @@ #include <stdlib.h> #endif #include <string.h> +#include <limits.h> #include "st.h" typedef struct st_table_entry st_table_entry; @@ -521,6 +522,8 @@ return 0; } +static unsigned long hash_seed = 0; + static int strhash(string) register const char *string; @@ -550,10 +553,11 @@ return val + (val << 15); #else - register int val = 0; + register unsigned long val = hash_seed; while ((c = *string++) != '\0') { val = val*997 + c; + val = (val << 13) | (val >> (sizeof(st_data_t) * CHAR_BIT - 13)); } return val + (val>>5); @@ -573,3 +577,11 @@ { return n; } + +extern unsigned long rb_genrand_int32(void); + +void +Init_st(void) +{ + hash_seed = rb_genrand_int32(); +} Index: ruby_1_8/test/ruby/test_string.rb =================================================================== --- ruby_1_8/test/ruby/test_string.rb (revision 34378) +++ ruby_1_8/test/ruby/test_string.rb (revision 34379) @@ -1,4 +1,5 @@ require 'test/unit' +require File.expand_path('envutil', File.dirname(__FILE__)) class TestString < Test::Unit::TestCase def check_sum(str, bits=16) @@ -259,4 +260,16 @@ "abc".end_with?("c") assert_nil($&) end + + def test_hash_random + str = 'abc' + a = [str.hash.to_s] + cmd = sprintf("%s -e 'print %s.hash'", EnvUtil.rubybin, str.dump) + 3.times { + IO.popen(cmd, "rb") {|o| + a << o.read + } + } + assert_not_equal([str.hash.to_s], a.uniq) + end end Index: ruby_1_8/random.c =================================================================== --- ruby_1_8/random.c (revision 34378) +++ ruby_1_8/random.c (revision 34379) @@ -189,6 +189,7 @@ #include <fcntl.h> #endif +static int seed_initialized = 0; static VALUE saved_seed = INT2FIX(0); static VALUE @@ -250,28 +251,23 @@ return old; } -static VALUE -random_seed() +#define DEFAULT_SEED_LEN (4 * sizeof(long)) + +static void +fill_random_seed(ptr) + char *ptr; { static int n = 0; + unsigned long *seed; struct timeval tv; int fd; struct stat statbuf; + char *buf = (char*)ptr; - int seed_len; - BDIGIT *digits; - unsigned long *seed; - NEWOBJ(big, struct RBignum); - OBJSETUP(big, rb_cBignum, T_BIGNUM); + seed = (unsigned long *)buf; - seed_len = 4 * sizeof(long); - big->sign = 1; - big->len = seed_len / SIZEOF_BDIGITS + 1; - digits = big->digits = ALLOC_N(BDIGIT, big->len); - seed = (unsigned long *)big->digits; + memset(buf, 0, DEFAULT_SEED_LEN); - memset(digits, 0, big->len * SIZEOF_BDIGITS); - #ifdef S_ISCHR if ((fd = open("/dev/urandom", O_RDONLY #ifdef O_NONBLOCK @@ -282,7 +278,7 @@ #endif )) >= 0) { if (fstat(fd, &statbuf) == 0 && S_ISCHR(statbuf.st_mode)) { - read(fd, seed, seed_len); + read(fd, seed, DEFAULT_SEED_LEN); } close(fd); } @@ -293,13 +289,37 @@ seed[1] ^= tv.tv_sec; seed[2] ^= getpid() ^ (n++ << 16); seed[3] ^= (unsigned long)&seed; +} +static VALUE +make_seed_value(char *ptr) +{ + BDIGIT *digits; + NEWOBJ(big, struct RBignum); + OBJSETUP(big, rb_cBignum, T_BIGNUM); + + RBIGNUM_SET_SIGN(big, 1); + + digits = ALLOC_N(char, DEFAULT_SEED_LEN); + RBIGNUM(big)->digits = digits; + RBIGNUM(big)->len = DEFAULT_SEED_LEN / SIZEOF_BDIGITS; + + MEMCPY(digits, ptr, char, DEFAULT_SEED_LEN); + /* set leading-zero-guard if need. */ - digits[big->len-1] = digits[big->len-2] <= 1 ? 1 : 0; + digits[RBIGNUM_LEN(big)-1] = digits[RBIGNUM_LEN(big)-2] <= 1 ? 1 : 0; return rb_big_norm((VALUE)big); } +static VALUE +random_seed(void) +{ + char buf[DEFAULT_SEED_LEN]; + fill_random_seed(buf); + return make_seed_value(buf); +} + /* * call-seq: * srand(number=0) => old_seed @@ -440,6 +460,9 @@ long val, max; rb_scan_args(argc, argv, "01", &vmax); + if (!seed_initialized) { + rand_init(random_seed()); + } switch (TYPE(vmax)) { case T_FLOAT: if (RFLOAT(vmax)->value <= LONG_MAX && RFLOAT(vmax)->value >= LONG_MIN) { @@ -487,6 +510,8 @@ return LONG2NUM(val); } +static char initial_seed[DEFAULT_SEED_LEN]; + void rb_reset_random_seed() { @@ -494,9 +519,24 @@ } void +Init_RandomSeed(void) +{ + fill_random_seed(initial_seed); + init_by_array((unsigned long*)initial_seed, DEFAULT_SEED_LEN/sizeof(unsigned long)); + seed_initialized = 1; +} + +static void +Init_RandomSeed2(void) +{ + saved_seed = make_seed_value(initial_seed); + memset(initial_seed, 0, DEFAULT_SEED_LEN); +} + +void Init_Random() { - rb_reset_random_seed(); + Init_RandomSeed2(); rb_define_global_function("srand", rb_f_srand, -1); rb_define_global_function("rand", rb_f_rand, -1); rb_global_variable(&saved_seed); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/