ruby-changes:36698
From: nobu <ko1@a...>
Date: Fri, 12 Dec 2014 10:49:34 +0900 (JST)
Subject: [ruby-changes:36698] nobu:r48779 (trunk): string.c: check arguments for crypt
nobu 2014-12-12 10:49:20 +0900 (Fri, 12 Dec 2014) New Revision: 48779 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=48779 Log: string.c: check arguments for crypt * string.c (rb_str_crypt): check arguments more strictly. * crypt() is not for wide char strings * salt bytes should not be NUL Modified files: trunk/ChangeLog trunk/string.c trunk/test/ruby/test_string.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 48778) +++ ChangeLog (revision 48779) @@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Fri Dec 12 10:49:18 2014 Nobuyoshi Nakada <nobu@r...> + + * string.c (rb_str_crypt): check arguments more strictly. + * crypt() is not for wide char strings + * salt bytes should not be NUL + Fri Dec 12 08:16:01 2014 Matt Hoyle <matt@d...> * io.c (io_read) Fix spelling in docco for read. [Fix GH-781] Index: string.c =================================================================== --- string.c (revision 48778) +++ string.c (revision 48779) @@ -179,6 +179,15 @@ mustnot_broken(VALUE str) https://github.com/ruby/ruby/blob/trunk/string.c#L179 } } +static void +mustnot_wchar(VALUE str) +{ + rb_encoding *enc = STR_ENC_GET(str); + if (rb_enc_mbminlen(enc) > 1) { + rb_raise(rb_eArgError, "wide char encoding: %s", rb_enc_name(enc)); + } +} + static int fstring_cmp(VALUE a, VALUE b); /* in case we restart MVM development, this needs to be per-VM */ @@ -7629,12 +7638,17 @@ rb_str_crypt(VALUE str, VALUE salt) https://github.com/ruby/ruby/blob/trunk/string.c#L7638 #endif StringValue(salt); - if (RSTRING_LEN(salt) < 2) + mustnot_wchar(str); + mustnot_wchar(salt); + if (RSTRING_LEN(salt) < 2) { + short_salt: rb_raise(rb_eArgError, "salt too short (need >=2 bytes)"); + } s = RSTRING_PTR(str); if (!s) s = ""; saltp = RSTRING_PTR(salt); + if (!saltp[0] || !saltp[1]) goto short_salt; #ifdef BROKEN_CRYPT if (!ISASCII((unsigned char)saltp[0]) || !ISASCII((unsigned char)saltp[1])) { salt_8bit_clean[0] = saltp[0] & 0x7f; Index: test/ruby/test_string.rb =================================================================== --- test/ruby/test_string.rb (revision 48778) +++ test/ruby/test_string.rb (revision 48779) @@ -504,6 +504,14 @@ class TestString < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_string.rb#L504 def test_crypt assert_equal(S('aaGUC/JkO9/Sc'), S("mypassword").crypt(S("aa"))) assert_not_equal(S('aaGUC/JkO9/Sc'), S("mypassword").crypt(S("ab"))) + assert_raise(ArgumentError) {S("mypassword").crypt(S(""))} + assert_raise(ArgumentError) {S("mypassword").crypt(S("\0a"))} + assert_raise(ArgumentError) {S("mypassword").crypt(S("a\0"))} + [Encoding::UTF_16BE, Encoding::UTF_16LE, + Encoding::UTF_32BE, Encoding::UTF_32LE].each do |enc| + assert_raise(ArgumentError) {S("mypassword").crypt(S("aa".encode(enc)))} + assert_raise(ArgumentError) {S("mypassword".encode(enc)).crypt(S("aa"))} + end end def test_delete -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/