{"id":278,"date":"2016-02-19T08:49:53","date_gmt":"2016-02-18T22:49:53","guid":{"rendered":"https:\/\/anton.ozlabs.org\/blog\/?p=278"},"modified":"2016-02-19T11:39:14","modified_gmt":"2016-02-19T01:39:14","slug":"fuzzing-on-power-linux-with-afl","status":"publish","type":"post","link":"https:\/\/anton.ozlabs.org\/blog\/2016\/02\/19\/fuzzing-on-power-linux-with-afl\/","title":{"rendered":"Fuzzing on POWER Linux with AFL"},"content":{"rendered":"<p>Fuzzing has got\u00c2\u00a0a lot of attention lately and if you write C or C++ code and haven&#8217;t thought about fuzzing, you really should. It&#8217;s common to find dozens of bugs when first applying it\u00c2\u00a0to a project.<\/p>\n<p>To get familiar with the latest in fuzzing, I decided to fuzz\u00c2\u00a0<a href=\"https:\/\/git.kernel.org\/cgit\/utils\/dtc\/dtc.git\">DTC<\/a>,\u00c2\u00a0our tool for manipulating flattened device trees. Fuzzing is very\u00c2\u00a0effective at testing tools that take input from files, so DTC\u00c2\u00a0fits the bill. For other projects, you can either build a test harness that takes file input or use the\u00c2\u00a0<a href=\"http:\/\/llvm.org\/docs\/LibFuzzer.html\">LLVM<\/a>\u00c2\u00a0fuzzing library.<\/p>\n<p>As for the tool, I chose\u00c2\u00a0American Fuzzy Lop (AFL).\u00c2\u00a0AFL is a fuzzer that is both powerful and easy to use&#8212;you can be up and running in minutes. It instruments the code and uses that feedback in order to discover new significant test cases. It also comes with\u00c2\u00a0an LLVM plugin which works well on POWER Linux.<\/p>\n<p>Here are the steps I took to fuzz DTC.\u00c2\u00a0I&#8217;m starting from a ppc64le Ubuntu 15.10 Docker image, so the first step is to install some required packages:<\/p>\n<pre>apt-get install build-essential wget git llvm clang flex bison<\/pre>\n<p>Now download and build AFL, including the LLVM plugin:<\/p>\n<pre>mkdir -p $HOME\/afl\r\ncd $HOME\/afl\r\nwget -N http:\/\/lcamtuf.coredump.cx\/afl\/releases\/afl-latest.tgz\r\ntar xzf afl-latest.tgz --strip-components=1\r\nmake AFL_NOX86=1\r\ncd llvm_mode\r\nmake\r\ncd ..\/<\/pre>\n<p>Download and build DTC\u00c2\u00a0using the LLVM plugin:<\/p>\n<pre>git clone git:\/\/git.kernel.org\/pub\/scm\/utils\/dtc\/dtc.git\r\ncd dtc\r\nmake CC=$HOME\/afl\/afl-clang-fast<\/pre>\n<p>Create an input and output directory, and seed the input directory. I just chose one of the DTC\u00c2\u00a0test cases for this. These will be mutated to produce new test cases. You can add as many as you want but keep them\u00c2\u00a0reasonably small:<\/p>\n<pre>mkdir -p in out\r\ncp tests\/fdtdump.dts in<\/pre>\n<p>Run it!<\/p>\n<pre>$HOME\/afl\/afl-fuzz -i in -o out -- .\/dtc -I dts @@<\/pre>\n<p>Notice how @@ is used to specify the name of the input file to be tested. AFL\u00c2\u00a0will feed the file to STDIN otherwise.<\/p>\n<p>That simple setup has <a href=\"https:\/\/git.kernel.org\/cgit\/utils\/dtc\/dtc.git\/commit\/?id=19370955884ff0c49328956227c302225f4a014b\">found<\/a> <a href=\"https:\/\/git.kernel.org\/cgit\/utils\/dtc\/dtc.git\/commit\/?id=1ab2205a6f0f9e826a623e639da02787d372de37\">quite<\/a> <a href=\"https:\/\/git.kernel.org\/cgit\/utils\/dtc\/dtc.git\/commit\/?id=d728ad59f53e72be21fe9bc419953c94aea798cc\">a<\/a> <a href=\"https:\/\/git.kernel.org\/cgit\/utils\/dtc\/dtc.git\/commit\/?id=b43345039b03ac5691961e1ce37dfb8c4c03a863\">number<\/a> <a href=\"https:\/\/git.kernel.org\/cgit\/utils\/dtc\/dtc.git\/commit\/?id=b06e55c88b9b922ff7e25cd62a4709b65524f0fc\">of<\/a> <a href=\"https:\/\/git.kernel.org\/cgit\/utils\/dtc\/dtc.git\/commit\/?id=2e53f9d2f0a8faab6cec0d78958d52c155f6c6eb\">bugs.<\/a>\u00c2\u00a0Thanks to David Gibson, the maintainer of DTC, for fixing them all!<\/p>\n<p>AFL also produces a very useful test corpus in out\/queue which you can use for more heavyweight testing, eg valgrind or the LLVM sanitizers.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Fuzzing has got\u00c2\u00a0a lot of attention lately and if you write C or C++ code and haven&#8217;t thought about fuzzing, you really should. It&#8217;s common to find dozens of bugs when first applying it\u00c2\u00a0to a project. To get familiar with the latest in fuzzing, I decided to fuzz\u00c2\u00a0DTC,\u00c2\u00a0our tool for manipulating flattened device trees. Fuzzing &hellip; <a href=\"https:\/\/anton.ozlabs.org\/blog\/2016\/02\/19\/fuzzing-on-power-linux-with-afl\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Fuzzing on POWER Linux with AFL&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/anton.ozlabs.org\/blog\/wp-json\/wp\/v2\/posts\/278"}],"collection":[{"href":"https:\/\/anton.ozlabs.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/anton.ozlabs.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/anton.ozlabs.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/anton.ozlabs.org\/blog\/wp-json\/wp\/v2\/comments?post=278"}],"version-history":[{"count":24,"href":"https:\/\/anton.ozlabs.org\/blog\/wp-json\/wp\/v2\/posts\/278\/revisions"}],"predecessor-version":[{"id":286,"href":"https:\/\/anton.ozlabs.org\/blog\/wp-json\/wp\/v2\/posts\/278\/revisions\/286"}],"wp:attachment":[{"href":"https:\/\/anton.ozlabs.org\/blog\/wp-json\/wp\/v2\/media?parent=278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/anton.ozlabs.org\/blog\/wp-json\/wp\/v2\/categories?post=278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/anton.ozlabs.org\/blog\/wp-json\/wp\/v2\/tags?post=278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}