Skip to content

Commit b35781d

Browse files
committed
merge revision(s) r46547: [Backport ruby#9976]
* 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 ruby#9976] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_1@47346 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 7706aa1 commit b35781d

File tree

4 files changed

+97
-6
lines changed

4 files changed

+97
-6
lines changed

ChangeLog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
Tue Sep 2 02:21:58 2014 Nobuyoshi Nakada <[email protected]>
2+
3+
* hash.c (env_aset, env_has_key, env_assoc, env_has_value),
4+
(env_rassoc, env_key): prohibit tainted strings if $SAFE is
5+
non-zero. [Bug #9976]
6+
17
Tue Sep 2 02:08:12 2014 Nobuyoshi Nakada <[email protected]>
28

39
* signal.c (rb_f_kill): directly enqueue an ignored signal to self,

hash.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2876,8 +2876,8 @@ env_aset(VALUE obj, VALUE nm, VALUE val)
28762876
env_delete(obj, nm);
28772877
return Qnil;
28782878
}
2879-
StringValue(nm);
2880-
StringValue(val);
2879+
SafeStringValue(nm);
2880+
SafeStringValue(val);
28812881
name = RSTRING_PTR(nm);
28822882
value = RSTRING_PTR(val);
28832883
if (memchr(name, '\0', RSTRING_LEN(nm)))
@@ -3372,7 +3372,8 @@ env_has_key(VALUE env, VALUE key)
33723372
{
33733373
char *s;
33743374

3375-
s = StringValuePtr(key);
3375+
SafeStringValue(key);
3376+
s = RSTRING_PTR(key);
33763377
if (memchr(s, '\0', RSTRING_LEN(key)))
33773378
rb_raise(rb_eArgError, "bad environment variable name");
33783379
if (getenv(s)) return Qtrue;
@@ -3391,7 +3392,8 @@ env_assoc(VALUE env, VALUE key)
33913392
{
33923393
char *s, *e;
33933394

3394-
s = StringValuePtr(key);
3395+
SafeStringValue(key);
3396+
s = RSTRING_PTR(key);
33953397
if (memchr(s, '\0', RSTRING_LEN(key)))
33963398
rb_raise(rb_eArgError, "bad environment variable name");
33973399
e = getenv(s);
@@ -3413,6 +3415,7 @@ env_has_value(VALUE dmy, VALUE obj)
34133415

34143416
obj = rb_check_string_type(obj);
34153417
if (NIL_P(obj)) return Qnil;
3418+
rb_check_safe_obj(obj);
34163419
env = GET_ENVIRON(environ);
34173420
while (*env) {
34183421
char *s = strchr(*env, '=');
@@ -3443,6 +3446,7 @@ env_rassoc(VALUE dmy, VALUE obj)
34433446

34443447
obj = rb_check_string_type(obj);
34453448
if (NIL_P(obj)) return Qnil;
3449+
rb_check_safe_obj(obj);
34463450
env = GET_ENVIRON(environ);
34473451
while (*env) {
34483452
char *s = strchr(*env, '=');
@@ -3473,7 +3477,7 @@ env_key(VALUE dmy, VALUE value)
34733477
char **env;
34743478
VALUE str;
34753479

3476-
StringValue(value);
3480+
SafeStringValue(value);
34773481
env = GET_ENVIRON(environ);
34783482
while (*env) {
34793483
char *s = strchr(*env, '=');

test/ruby/test_env.rb

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,4 +451,85 @@ def test_memory_leak_shift
451451
end;
452452
end
453453
end
454+
455+
def test_taint_aref
456+
assert_raise(SecurityError) do
457+
proc do
458+
$SAFE = 2
459+
ENV["FOO".taint]
460+
end.call
461+
end
462+
end
463+
464+
def test_taint_fetch
465+
assert_raise(SecurityError) do
466+
proc do
467+
$SAFE = 2
468+
ENV.fetch("FOO".taint)
469+
end.call
470+
end
471+
end
472+
473+
def test_taint_assoc
474+
assert_raise(SecurityError) do
475+
proc do
476+
$SAFE = 2
477+
ENV.assoc("FOO".taint)
478+
end.call
479+
end
480+
end
481+
482+
def test_taint_rassoc
483+
assert_raise(SecurityError) do
484+
proc do
485+
$SAFE = 2
486+
ENV.rassoc("FOO".taint)
487+
end.call
488+
end
489+
end
490+
491+
def test_taint_key
492+
assert_raise(SecurityError) do
493+
proc do
494+
$SAFE = 2
495+
ENV.key("FOO".taint)
496+
end.call
497+
end
498+
end
499+
500+
def test_taint_key_p
501+
assert_raise(SecurityError) do
502+
proc do
503+
$SAFE = 2
504+
ENV.key?("FOO".taint)
505+
end.call
506+
end
507+
end
508+
509+
def test_taint_value_p
510+
assert_raise(SecurityError) do
511+
proc do
512+
$SAFE = 2
513+
ENV.value?("FOO".taint)
514+
end.call
515+
end
516+
end
517+
518+
def test_taint_aset_value
519+
assert_raise(SecurityError) do
520+
proc do
521+
$SAFE = 2
522+
ENV["FOO"] = "BAR".taint
523+
end.call
524+
end
525+
end
526+
527+
def test_taint_aset_key
528+
assert_raise(SecurityError) do
529+
proc do
530+
$SAFE = 2
531+
ENV["FOO".taint] = "BAR"
532+
end.call
533+
end
534+
end
454535
end

version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#define RUBY_VERSION "2.1.2"
22
#define RUBY_RELEASE_DATE "2014-09-02"
3-
#define RUBY_PATCHLEVEL 217
3+
#define RUBY_PATCHLEVEL 218
44

55
#define RUBY_RELEASE_YEAR 2014
66
#define RUBY_RELEASE_MONTH 9

0 commit comments

Comments
 (0)