ruby-changes:52912
From: nagachika <ko1@a...>
Date: Thu, 18 Oct 2018 00:03:16 +0900 (JST)
Subject: [ruby-changes:52912] nagachika:r65125 (trunk): infect taint flag on Array#pack and String#unpack
nagachika 2018-10-18 00:03:10 +0900 (Thu, 18 Oct 2018) New Revision: 65125 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=65125 Log: infect taint flag on Array#pack and String#unpack with the directives "B", "b", "H" and "h". * pack.c (pack_pack, pack_unpack_internal): infect taint flag. * test/ruby/test_pack.rb: add test for above. Modified files: trunk/pack.c trunk/test/ruby/test_pack.rb Index: test/ruby/test_pack.rb =================================================================== --- test/ruby/test_pack.rb (revision 65124) +++ test/ruby/test_pack.rb (revision 65125) @@ -862,4 +862,20 @@ EXPECTED https://github.com/ruby/ruby/blob/trunk/test/ruby/test_pack.rb#L862 assert_equal "hogefuga", "aG9nZWZ1Z2E=".unpack1("m") assert_equal "01000001", "A".unpack1("B*") end + + def test_pack_infection + tainted_array_string = ["123456"] + tainted_array_string.first.taint + ['a', 'A', 'Z', 'B', 'b', 'H', 'h', 'u', 'M', 'm', 'P', 'p'].each do |f| + assert_predicate(tainted_array_string.pack(f), :tainted?) + end + end + + def test_unpack_infection + tainted_string = "123456" + tainted_string.taint + ['a', 'A', 'Z', 'B', 'b', 'H', 'h', 'u', 'M', 'm'].each do |f| + assert_predicate(tainted_string.unpack(f).first, :tainted?) + end + end end Index: pack.c =================================================================== --- pack.c (revision 65124) +++ pack.c (revision 65125) @@ -751,6 +751,7 @@ pack_pack(int argc, VALUE *argv, VALUE a https://github.com/ruby/ruby/blob/trunk/pack.c#L751 StringValue(from); ptr = RSTRING_PTR(from); plen = RSTRING_LEN(from); + OBJ_INFECT(res, from); if (len == 0 && type == 'm') { encodes(res, ptr, plen, type, 0); @@ -778,6 +779,7 @@ pack_pack(int argc, VALUE *argv, VALUE a https://github.com/ruby/ruby/blob/trunk/pack.c#L779 case 'M': /* quoted-printable encoded string */ from = rb_obj_as_string(NEXTFROM); + OBJ_INFECT(res, from); if (len <= 1) len = 72; qpencode(res, from, len); @@ -803,6 +805,7 @@ pack_pack(int argc, VALUE *argv, VALUE a https://github.com/ruby/ruby/blob/trunk/pack.c#L805 } else { t = StringValuePtr(from); + OBJ_INFECT(res, from); rb_obj_taint(from); } if (!associates) { @@ -1186,6 +1189,7 @@ pack_unpack_internal(VALUE str, VALUE fm https://github.com/ruby/ruby/blob/trunk/pack.c#L1189 len = (send - s) * 8; bits = 0; bitstr = rb_usascii_str_new(0, len); + OBJ_INFECT(bitstr, str); t = RSTRING_PTR(bitstr); for (i=0; i<len; i++) { if (i & 7) bits >>= 1; @@ -1207,6 +1211,7 @@ pack_unpack_internal(VALUE str, VALUE fm https://github.com/ruby/ruby/blob/trunk/pack.c#L1211 len = (send - s) * 8; bits = 0; bitstr = rb_usascii_str_new(0, len); + OBJ_INFECT(bitstr, str); t = RSTRING_PTR(bitstr); for (i=0; i<len; i++) { if (i & 7) bits <<= 1; @@ -1228,6 +1233,7 @@ pack_unpack_internal(VALUE str, VALUE fm https://github.com/ruby/ruby/blob/trunk/pack.c#L1233 len = (send - s) * 2; bits = 0; bitstr = rb_usascii_str_new(0, len); + OBJ_INFECT(bitstr, str); t = RSTRING_PTR(bitstr); for (i=0; i<len; i++) { if (i & 1) @@ -1251,6 +1257,7 @@ pack_unpack_internal(VALUE str, VALUE fm https://github.com/ruby/ruby/blob/trunk/pack.c#L1257 len = (send - s) * 2; bits = 0; bitstr = rb_usascii_str_new(0, len); + OBJ_INFECT(bitstr, str); t = RSTRING_PTR(bitstr); for (i=0; i<len; i++) { if (i & 1) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/