[前][次][番号順一覧][スレッド一覧]

ruby-changes:34695

From: nobu <ko1@a...>
Date: Fri, 11 Jul 2014 10:09:19 +0900 (JST)
Subject: [ruby-changes:34695] nobu:r46778 (trunk): pack.c: fix buffer overrun

nobu	2014-07-11 10:09:05 +0900 (Fri, 11 Jul 2014)

  New Revision: 46778

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=46778

  Log:
    pack.c: fix buffer overrun
    
    * pack.c (encodes): fix buffer overrun by tail_lf.  Thanks to
      Mamoru Tasaka and Tomas Hoger.  [ruby-core:63604] [Bug #10019]

  Modified files:
    trunk/ChangeLog
    trunk/pack.c
    trunk/test/ruby/test_pack.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 46777)
+++ ChangeLog	(revision 46778)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Fri Jul 11 10:09:03 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* pack.c (encodes): fix buffer overrun by tail_lf.  Thanks to
+	  Mamoru Tasaka and Tomas Hoger.  [ruby-core:63604] [Bug #10019]
+
 Thu Jul 10 23:51:36 2014  Naohisa Goto  <ngotogenome@g...>
 
 	* hash.c (ruby_setenv): Fix TestEnv#test_aset failure on Solaris 9.
Index: pack.c
===================================================================
--- pack.c	(revision 46777)
+++ pack.c	(revision 46778)
@@ -945,7 +945,8 @@ static const char b64_table[] = https://github.com/ruby/ruby/blob/trunk/pack.c#L945
 static void
 encodes(VALUE str, const char *s, long len, int type, int tail_lf)
 {
-    char buff[4096];
+    enum {buff_size = 4096, encoded_unit = 4};
+    char buff[buff_size + 1];	/* +1 for tail_lf */
     long i = 0;
     const char *trans = type == 'u' ? uu_table : b64_table;
     char padding;
@@ -958,7 +959,7 @@ encodes(VALUE str, const char *s, long l https://github.com/ruby/ruby/blob/trunk/pack.c#L959
 	padding = '=';
     }
     while (len >= 3) {
-        while (len >= 3 && sizeof(buff)-i >= 4) {
+        while (len >= 3 && buff_size-i >= encoded_unit) {
             buff[i++] = trans[077 & (*s >> 2)];
             buff[i++] = trans[077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017))];
             buff[i++] = trans[077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03))];
@@ -966,7 +967,7 @@ encodes(VALUE str, const char *s, long l https://github.com/ruby/ruby/blob/trunk/pack.c#L967
             s += 3;
             len -= 3;
         }
-        if (sizeof(buff)-i < 4) {
+        if (buff_size-i < encoded_unit) {
             rb_str_buf_cat(str, buff, i);
             i = 0;
         }
@@ -986,6 +987,7 @@ encodes(VALUE str, const char *s, long l https://github.com/ruby/ruby/blob/trunk/pack.c#L987
     }
     if (tail_lf) buff[i++] = '\n';
     rb_str_buf_cat(str, buff, i);
+    if ((size_t)i > sizeof(buff)) rb_bug("encodes() buffer overrun");
 }
 
 static const char hex_table[] = "0123456789ABCDEF";
Index: test/ruby/test_pack.rb
===================================================================
--- test/ruby/test_pack.rb	(revision 46777)
+++ test/ruby/test_pack.rb	(revision 46778)
@@ -550,6 +550,14 @@ EXPECTED https://github.com/ruby/ruby/blob/trunk/test/ruby/test_pack.rb#L550
     assert_equal(["\0"], "AA\n".unpack("m"))
     assert_equal(["\0"], "AA=\n".unpack("m"))
     assert_equal(["\0\0"], "AAA\n".unpack("m"))
+
+    bug10019 = '[ruby-core:63604] [Bug #10019]'
+    size = ((4096-4)/4*3+1)
+    assert_separately(%W[- #{size} #{bug10019}], <<-'end;')
+      size = ARGV.shift.to_i
+      bug = ARGV.shift
+      assert_equal(size, ["a"*size].pack("m#{size+2}").unpack("m")[0].size, bug)
+    end;
   end
 
   def test_pack_unpack_m0

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]