diff --git a/ChangeLog b/ChangeLog index c2c0d5dfa9..0e2c8b342a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Wed Sep 10 11:39:54 2014 Nobuyoshi Nakada + + * hash.c (env_aset, env_has_key, env_assoc, env_has_value), + (env_rassoc, env_key): prohibit tainted strings if $SAFE is + non-zero. [Bug #9976] + Sat Sep 6 09:10:45 2014 Zachary Scott * lib/rdoc/generator/template/darkfish/js/jquery.js: Backport diff --git a/hash.c b/hash.c index 1ca87de1ec..0cb3379236 100644 --- a/hash.c +++ b/hash.c @@ -2547,8 +2547,8 @@ env_aset(VALUE obj, VALUE nm, VALUE val) env_delete(obj, nm); return Qnil; } - StringValue(nm); - StringValue(val); + SafeStringValue(nm); + SafeStringValue(val); name = RSTRING_PTR(nm); value = RSTRING_PTR(val); if (memchr(name, '\0', RSTRING_LEN(nm))) @@ -3048,7 +3048,8 @@ env_has_key(VALUE env, VALUE key) char *s; rb_secure(4); - s = StringValuePtr(key); + SafeStringValue(key); + s = RSTRING_PTR(key); if (memchr(s, '\0', RSTRING_LEN(key))) rb_raise(rb_eArgError, "bad environment variable name"); if (getenv(s)) return Qtrue; @@ -3068,7 +3069,8 @@ env_assoc(VALUE env, VALUE key) char *s, *e; rb_secure(4); - s = StringValuePtr(key); + SafeStringValue(key); + s = RSTRING_PTR(key); if (memchr(s, '\0', RSTRING_LEN(key))) rb_raise(rb_eArgError, "bad environment variable name"); e = getenv(s); @@ -3091,6 +3093,7 @@ env_has_value(VALUE dmy, VALUE obj) rb_secure(4); obj = rb_check_string_type(obj); if (NIL_P(obj)) return Qnil; + rb_check_safe_obj(obj); env = GET_ENVIRON(environ); while (*env) { char *s = strchr(*env, '='); @@ -3122,6 +3125,7 @@ env_rassoc(VALUE dmy, VALUE obj) rb_secure(4); obj = rb_check_string_type(obj); if (NIL_P(obj)) return Qnil; + rb_check_safe_obj(obj); env = GET_ENVIRON(environ); while (*env) { char *s = strchr(*env, '='); @@ -3153,7 +3157,7 @@ env_key(VALUE dmy, VALUE value) VALUE str; rb_secure(4); - StringValue(value); + SafeStringValue(value); env = GET_ENVIRON(environ); while (*env) { char *s = strchr(*env, '='); diff --git a/test/ruby/test_env.rb b/test/ruby/test_env.rb index 98357ba201..9c97adeb82 100644 --- a/test/ruby/test_env.rb +++ b/test/ruby/test_env.rb @@ -448,4 +448,85 @@ class TestEnv < Test::Unit::TestCase end; end end + + def test_taint_aref + assert_raise(SecurityError) do + proc do + $SAFE = 2 + ENV["FOO".taint] + end.call + end + end + + def test_taint_fetch + assert_raise(SecurityError) do + proc do + $SAFE = 2 + ENV.fetch("FOO".taint) + end.call + end + end + + def test_taint_assoc + assert_raise(SecurityError) do + proc do + $SAFE = 2 + ENV.assoc("FOO".taint) + end.call + end + end + + def test_taint_rassoc + assert_raise(SecurityError) do + proc do + $SAFE = 2 + ENV.rassoc("FOO".taint) + end.call + end + end + + def test_taint_key + assert_raise(SecurityError) do + proc do + $SAFE = 2 + ENV.key("FOO".taint) + end.call + end + end + + def test_taint_key_p + assert_raise(SecurityError) do + proc do + $SAFE = 2 + ENV.key?("FOO".taint) + end.call + end + end + + def test_taint_value_p + assert_raise(SecurityError) do + proc do + $SAFE = 2 + ENV.value?("FOO".taint) + end.call + end + end + + def test_taint_aset_value + assert_raise(SecurityError) do + proc do + $SAFE = 2 + ENV["FOO"] = "BAR".taint + end.call + end + end + + def test_taint_aset_key + assert_raise(SecurityError) do + proc do + $SAFE = 2 + ENV["FOO".taint] = "BAR" + end.call + end + end end diff --git a/version.h b/version.h index 7f06df1d59..49d79e07a1 100644 --- a/version.h +++ b/version.h @@ -1,10 +1,10 @@ #define RUBY_VERSION "2.0.0" -#define RUBY_RELEASE_DATE "2014-09-06" -#define RUBY_PATCHLEVEL 556 +#define RUBY_RELEASE_DATE "2014-09-10" +#define RUBY_PATCHLEVEL 557 #define RUBY_RELEASE_YEAR 2014 #define RUBY_RELEASE_MONTH 9 -#define RUBY_RELEASE_DAY 6 +#define RUBY_RELEASE_DAY 10 #include "ruby/version.h"