<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/xml.xsl" type="text/xsl"?><feed xmlns="http://www.w3.org/2005/Atom">
  <title>nulog, NULL::something : out of the headphone &gt; 2005 &gt; January &gt; 22 &gt; Ruby コードを安全に実行する。</title>
  <link href="http://lowreal.net/logs/2005/01/22/1"/>
  <icon>http://lowreal.net/img/banner.png</icon>
  <link rel="self" type="application/atom+xml" href="http://lowreal.net/logs/2005/01/22/1.atom"/>
  <link rel="alternate" type="application/xhtml+xml" href="http://lowreal.net/logs/2005/01/22/1.xhtml"/>
  <updated>2005-01-22T03:05:02+09:00</updated>
  <author>
    <name>cho45(砂糖)</name>
  </author>
  <id>http://lowreal.net/2005/01/22/1</id>
  <entry>
    <title>Ruby コードを安全に実行する。</title>
    <link rel="alternate" type="text/html" href="http://lowreal.net/logs/2005/01/22/1.html"/>
    <link rel="alternate" type="application/xml+xhtml" href="http://lowreal.net/logs/2005/01/22/1.xhtml"/>
    <updated>2005-01-22T03:05:02+09:00</updated>
    <published>2005-01-22T03:05:02+09:00</published>
    <id>http://lowreal.net/2005/01/22/1</id>
    <category term="ruby"/>
    <category term="prog"/>
    <content type="xhtml" xml:base="http://lowreal.net/">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>前にも書いたけど完全じゃないので。たぶん、これでいいはず。</p>
        <pre class="Ruby" title="全く信頼できない Ruby コードを安全に実行">def safe(lvl, tm=1)
    result = nil
    tg = nil
    th = Thread.start(lvl) do |level|
        tg = ThreadGroup.new.add(Thread.current)
        $SAFE = level
        result = yield
    end.join(tm)
    tg.list.each {|t| t.kill}
    raise TimeoutError unless th
    result
end
</pre>
        <p>ThreadGroup を新しく作り、それにカレント実行スレッドを突っ込んで、信頼できないコードから生成されるスレッドを全てこいつの所属させ、実行終了したら kill! kill! kill!</p>
        <p><abbr title="Internet Relay Chat">IRC</abbr> BOT の Ruby コード実行機能をこれにした。まぁしかし！ SEGV されたら終りというどうしようもない脆弱性がありますけれども。</p>
      </div>
    </content>
  </entry>
</feed>
