SlideShare a Scribd company logo
JZlibと
“Java7で看取られたバグ(享年12)”
         について


                  @ymnk



Java SE 7 Launch Event in Sendai (2011/09/04)
自己紹介
• 山中淳彦(@ymnk)
• 株式会社ジェイクラフト (JCraft,Inc.)
• FLOSSを幾つか開発 ・公開
  JSch、JZlib、JOrbis など
• Eclipse.org、Liftwebでコミッタ
• Scala勉強会@東北 (通算110回程、休止中)
• 仕事でJava、最近 Scala
• http://facebook.com/aymnk
JZlib and an aged fixed bug in java7
自己紹介
• ymnkの書いたソフトがBoeing 787に搭載
 JCraft's X Server Part of Boeing 787 Avionics Systems
 http://www.jcraft.com/news/06.18.2009pr.html
いきなり結論
JZlib and an aged fixed bug in java7
内容
• Bug ID: 4206909
• JZlib
• Zlib
• Deflateアルゴリズム
• SYNC_FLUSHの使用例
• java.util.zip まつわる話題
• まとめ
Bug ID: 4206909
Bug ID: 4206909
• http://bugs.sun.com/view_bug.do?bug_id=4206909
• Synopsis: want java.util.zip to work for
  interactive use (Z_SYNC_FLUSH)
• Submit Date: 28-JAN-1999
• Reported Against: 1.2, 1.3, 1.1.7, 1.1.8
• Release Fixed: 7(b77)
• State: 10-Fix Delivered, request for
  enhancement
Bug ID: 4206909
• Description:

 “Today it is not possible to reuse
 java.util.zip for compression because the
 Java VM calls deflate() in the libz with
 Z_NO_FLUSH (and Z_FINISH for EOF)
 only.”
Bug ID: 4206909
• Description:

 “ This means that in most cases the
 deflate() does not emit enough data for
 the decompressor to reassemble the
 complete data packet because it waits for
 more input.”
Bug ID: 4206909
• java.util.zip パッケージ
   – Deflater
   一般的な ZLIB 圧縮ライブラリを使用して汎用の圧縮
   アルゴリズムをサポート
  – Inflater
   一般的な ZLIB 圧縮ライブラリを使用して汎用の圧縮
   解除をサポート
  – InflaterInputStream
  – DeflaterOutputStream
Bug ID: 4206909
Java6まで(j.u.z.Deflater#deflate+NO_FLUSH)

•“Hello”を圧縮して伸張しても”Hello”が得られるとは限らない
•双方向でやり取りする場合、デッドロックになる可能性がある

  “Hello”
               compressor
                             “lo”は処理待ち


                              “Hel”
              decompressor
Bug ID: 4206909
Java7(j.u.z.Deflater#deflate+SYNC_FLUSH)

•“Hello”をSYNC_FLUSHで圧縮して伸張すると必ず
”Hello”が得られる

 “Hello”
               compressor


                              “Hello”
              decompressor
で、いままではどうしてた?
JZlib and an aged fixed bug in java7
JZlib
JZlib
•   zlib(1.1.3)のpure Java実装
•   修正BSDライセンスで公開
•   2000年12月より ymnk が開発
•   最近、Inflater/Deflaterでgzip
    format(RFC1952)を操作できるようにhack中
JZlib
• 開発の動機
  SSHのパケット圧縮[RFC4253]実現のため

 “The 'zlib' compression is described in
 [RFC1950] and in [RFC1951].The
 compression context is initialized after each
 key exchange, and is passed from one packet
 to the next, with only a partial flush being
 performed at the end of each packet. “
JZlib
• JZlibを使っているソフトウェア
 – JSch
 – Apache MINA
 – Netty
 – Zimbra
 – as3zlib
 – Zlib.cs
 – IronPython.Zlib
 – Nokia Data Gathering 等
zlib
zlib
• Wikipediaから
 “zlibは、データの圧縮および伸張を行うためのフリーの
 ライブラリである。可逆圧縮アルゴリズムの Deflate
 (RFC 1951)を実装している。ヘッダーやフッターなどの
 データ形式はRFC 1950 (ZLIB Compressed Data
 Format Specification)として仕様化されている。”
zlib
• 作者: Jean-Loup Gailly, Mark Adler
• ライセンス: zlib license
• GIF特許問題を受けて開発開始
• 当初は、性能・効率よりは、特許に触れないよう
  に開発
 ITmedia: LZWに震え上がった10年前の人たち
 Glamenv-Septzen.net: 技術/歴史/zip,gzip,zlib,bzip2
Deflate アルゴリズム
Deflate アルゴリズム
• Wikipediaから
 “LZ77とハフマン符号化を組み合わせた可逆データ圧
 縮アルゴリズム。”
                                       特許あり

   LZ77         LZ78                        LZW

                            GIF
                            Compress コマンド



    LZSS          Deflate       PNG
                                zlib、gzip

                 特許なし(と広く信じられている)
SYNC_FLUSHの使用例
SYNC_FLUSHの使用例
scala> val defl_infl = {
    import java.util.zip._
    val (defl, infl, buf) = (new Deflater, new Inflater, new Array[Byte](100))
    def f(in: String): String = synchronized {
     in.getBytes match {
      case result =>
         defl.setInput(result)
         defl.deflate(buf, 0, buf.length, Deflater.SYNC_FLUSH) match{
         case l =>  l は buf に書き出された圧縮データのサイズ
                   //
         infl.setInput(buf, 0, l)
         val len = infl.inflate(result, 0, result.length)
          new String(result, 0, len)
      }}} // ちゃんと実装するには、中間バッファ(buf)の溢れに注意
    f_    // また、伸張後のバッファ溢れにも注意すべき
}
SYNC_FLUSHの使用例
scala> println(new java.io.File(".").list.forall{ l => defl_infl(l)==l })
true
java.util.zip まつわる話題
java.util.zip にまつわる話題
• JDK は native な zlib を独自に抱えてる
 2002年ごろ double free 問題で大騒ぎに
 zlib 1.1.3の問題
 S ymantec: Zlib compression library double free bug could allow arbitrary code Zlib

• Inflaterがgzip format(RFC1952)を処理できない
  zlib 1.2からの機能 (Java6以降は zlib 1.2.3を使用)
  最近、JRubyのZip::Inflaterの実装で話題に
  JRubyでj.u.zipに代わって、JZlib を使うかも?
• zlibのinflaterSyncをInflaterから使えない
JZlib and an aged fixed bug in java7
JZlib and an aged fixed bug in java7
まとめ
JZlib and an aged fixed bug in java7

More Related Content

PPTX
PHP AST 徹底解説(補遺)
PPTX
Javascript basic code
PDF
Docker inside containers
PDF
NanoStrand
PPT
YAPC Asia 2010 30days Albumの裏側 後日談
PPT
関西オープンソース 2008 30days Albumの裏側
KEY
Lxc on cloud
PHP AST 徹底解説(補遺)
Javascript basic code
Docker inside containers
NanoStrand
YAPC Asia 2010 30days Albumの裏側 後日談
関西オープンソース 2008 30days Albumの裏側
Lxc on cloud

What's hot (20)

PDF
超簡単!Subversion入門 概念編
PDF
シェル入門
PPTX
php7's ast
PPTX
PHP AST 徹底解説
PPT
Google Perf Tools (tcmalloc) の使い方
PPTX
190925 python-windows
PPTX
PHP と SAPI と ZendEngine3 と
PDF
超簡単!Subversion入門 準備編
ODP
Symfony2 workshop-0 (nagoya 2011/2/10)
PDF
Kickstart, Puppet, Docker
KEY
Cost of ovs receiving process
PPTX
php-src の歩き方
PDF
Nas4 freeへzabbix agentを導入してみた
PDF
"More" Introduction to Zend Tool
PDF
タスクマネージャーの上級版!Process Explorerの紹介
PPT
Webサーバの基礎知識【編集済み】
PPTX
Kubernetesできること
PPTX
Metasploit framework
PDF
覚えておきたい! zypper コマンドの使い方
PPTX
Casperjsのインストール
超簡単!Subversion入門 概念編
シェル入門
php7's ast
PHP AST 徹底解説
Google Perf Tools (tcmalloc) の使い方
190925 python-windows
PHP と SAPI と ZendEngine3 と
超簡単!Subversion入門 準備編
Symfony2 workshop-0 (nagoya 2011/2/10)
Kickstart, Puppet, Docker
Cost of ovs receiving process
php-src の歩き方
Nas4 freeへzabbix agentを導入してみた
"More" Introduction to Zend Tool
タスクマネージャーの上級版!Process Explorerの紹介
Webサーバの基礎知識【編集済み】
Kubernetesできること
Metasploit framework
覚えておきたい! zypper コマンドの使い方
Casperjsのインストール
Ad

Viewers also liked (20)

PDF
Implementing SSH in Java
PDF
Man-in-the-Middle Attack for SSH with Scala and JSch
PDF
an introduction to Distem
PDF
A Hippopotamus for Christmas
PPTX
Four failures and one hit
PPTX
ZUGFeRD: an overview
PDF
Infrastructure as code might be literally impossible
PPTX
Startup Legal and IP
PDF
Porting linux to a new architecture
PDF
All of Your Network Monitoring is (probably) Wrong
PPTX
Digital Signatures: how it's done in PDF
PPTX
Vert.x vs akka
PDF
Why vREST?
PDF
Java 9 – The Ultimate Feature List
PDF
Ingesting Drone Data into Big Data Platforms
PDF
Linux Performance Analysis: New Tools and Old Secrets
PDF
Linux Systems Performance 2016
PPTX
Broken Linux Performance Tools 2016
PDF
BPF: Tracing and more
PDF
Velocity 2015 linux perf tools
Implementing SSH in Java
Man-in-the-Middle Attack for SSH with Scala and JSch
an introduction to Distem
A Hippopotamus for Christmas
Four failures and one hit
ZUGFeRD: an overview
Infrastructure as code might be literally impossible
Startup Legal and IP
Porting linux to a new architecture
All of Your Network Monitoring is (probably) Wrong
Digital Signatures: how it's done in PDF
Vert.x vs akka
Why vREST?
Java 9 – The Ultimate Feature List
Ingesting Drone Data into Big Data Platforms
Linux Performance Analysis: New Tools and Old Secrets
Linux Systems Performance 2016
Broken Linux Performance Tools 2016
BPF: Tracing and more
Velocity 2015 linux perf tools
Ad

JZlib and an aged fixed bug in java7