title: Causing a buffer overflow, segfault
body: <p>I'm trying to cause a buffer overflow in the following, very simple, program:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdint.h&gt;
  
  void badf(int n, char c, char* buffer)
  {
  
      char mycode[] = {
  0xeb, 0x0f, 0xb8, 0x0b,
  0x00, 0x00, 0x00, 0x8b,
  0x1c, 0x24, 0x8d, 0x0c,
  0x24, 0x31, 0xd2, 0xcd,
  0x80, 0xe8, 0xec, 0xff, 
  0xff, 0xff, 0x2f, 0x62,
  0x69, 0x6e, 0x2f, 0x6c, 
  0x73, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00,
  0x00
  }; // 37 bytes
  
  // Overwrite Base Pointer
      //mycode[37] = 0x29;
  //mycode[38] = 0xf4;
  //mycode[39] = 0xff;
  //mycode[40] = 0xbf;
  
      // Overwrite Instruction Pointer
      // Using debugger, found mycode[] to be loaded in: 0xbffff42d
      mycode[41] = 0x2d;
      mycode[42] = 0xf4;
      mycode[43] = 0xff;
      mycode[44] = 0xbf;
  
  }
  
  void f(int n, char c)
  {
      char buffer[37];    
      badf(n,c,buffer);
  }
  
  void test()
  {
      printf("test");
  }
  
  int main()
  {
      f(37,0x00);
      return 0;
  }
  </code></pre>
  
  <p>(I've successfully managed to execute <code>test()</code> from the buffer overflow before) Now I'm trying to execute mycode[] by overwriting the instruction pointer with the start of mycode in the stack.</p>
  
  <p>This only half works, the program jumps to the right address, where I can see the correct machine code in the debugger, but then crashes with a segmentation fault, instead of executing the following instructions (see screenshot).</p>
  
  <p><img src="https://i.stack.imgur.com/EWPdO.png" alt="enter image description here"></p>
  
  <p><strong>I'm trying to figure out why he crashes before executing the contents of the "injected" code.</strong> I'm relatively new to this sort of stuff, I understand segmentation fault means I'm attempting to access memory the OS doesn't want me to?</p>
  
  <p>(PS: 32bit Linux machine, compiling with <code>-fno-stack-protector</code> so I can play with this stuff)</p>
  
  <p>(If any more info is needed I will gladly update the post)</p>