title: How to create self-decompressing executables on linux when you cannot execve a pipe
body: <p>I've been working on a small hack to get the filesize of an executable down. I'm aware there exist tools that do executable compressing properly, but this is more for my own enjoyment than anything serious.</p>
  <p>My idea is to compress the executable with gzip, then embed it in another c program, called the launcher, as an array. When the launcher runs, it sets up a piping system like so:</p>
  <pre><code>parent launcher -&gt; fork 1 of launcher -&gt; fork 2 of launcher
  </code></pre>
  <p>fork 1 turns itself into gzip, so it decompresses whatever the parent feeds it, and spits out the decompressed version to fork 2.</p>
  <p>Here's where the hack kicks in. Fork 2 tries to exec the file &quot;/dev/fd/n&quot;, where n is the file number of the pipe that goes from fork 1 to fork 2. In essence this means fork 2 will try to execute whatever binary gzip spits out.</p>
  <p>However, this doesn't work (surprise surprise.) I tried stracing my sample implementation and the line that does the execv on &quot;/dev/fd/n&quot; returns -1 EACCES (Permission denied). However, if I open up a terminal and run <code>ls -l /dev/fd/</code> I get something like:</p>
  <pre><code>lrwx------ 1 blackle users 64 Nov 10 05:14 0 -&gt; /dev/pts/0
  lrwx------ 1 blackle users 64 Nov 10 05:14 1 -&gt; /dev/pts/0
  lrwx------ 1 blackle users 64 Nov 10 05:14 2 -&gt; /dev/pts/0
  lr-x------ 1 blackle users 64 Nov 10 05:14 3 -&gt; /proc/17138/fd
  </code></pre>
  <p>All of them have permissions +x for the user (me.) This means it should be executable, no? Or is this just a very strange kernel edge case that <em>says</em> it hasn't got the permissions, but really it can't execute because it's not a real file.</p>
  <h2>UPDATE after nearly 7 years</h2>
  <p>With the advent of linux &quot;memfd&quot;s, it's now possible to create self-decompressing executables on linux that don't touch the filesystem. See: <a href="https://gitlab.com/PoroCYon/vondehi" rel="nofollow noreferrer">https://gitlab.com/PoroCYon/vondehi</a></p>