title: ASLR Entropy Bits for Stack on Linux
body: <p>I am looking at a <a href="https://www.usenix.org/sites/default/files/conference/protected-files/cset14_slides_herlands.pdf" rel="noreferrer">presentation</a> from MIT where they explain different types of ASLR implementations. </p>
  
  <p>For example, they point out that for static ASLR, stack has 19-bits of entropy. In my understanding, this means the stack base address can only be randomized to take 2^19 different values.</p>
  
  <p>I want to ask how to calculate that the stack has 19-bits of entropy ?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>After checking online, I found some <a href="https://xorl.wordpress.com/2011/01/16/linux-kernel-aslr-implementation/" rel="noreferrer">explanation</a> of stack ASLR on Linux. Learning from another <a href="https://stackoverflow.com/q/13826479/682869">question</a>, the code I thought may be relevant is:</p>
  
  <pre><code>#ifndef STACK_RND_MASK
  #define STACK_RND_MASK (0x7ff &gt;&gt; (PAGE_SHIFT - 12))     /* 8MB of VA */
  #endif
  
  static unsigned long randomize_stack_top(unsigned long stack_top)
  {
      unsigned int random_variable = 0;
  
      if ((current-&gt;flags &amp; PF_RANDOMIZE) &amp;&amp;
              !(current-&gt;personality &amp; ADDR_NO_RANDOMIZE)) {
              random_variable = get_random_int() &amp; STACK_RND_MASK;
              random_variable &lt;&lt;= PAGE_SHIFT;
      }
  #ifdef CONFIG_STACK_GROWSUP
      return PAGE_ALIGN(stack_top) + random_variable;
  #else
      return PAGE_ALIGN(stack_top) - random_variable;
  #endif
  }
  </code></pre>
  
  <p>I want to ask if this is the right place to reason about my question ?</p>