Note that there are some explanatory texts on larger screens.

plurals
  1. POC Buffer Overflow - Why is there a constant number of bytes that trips a segfault? (Mac OS 10.8 64-bit, clang)
    primarykey
    data
    text
    <p>I was experimenting with buffer overflow in C, and found an interesting quirk:</p> <p>For any given array size, there seems to be a set number of overflow bytes that can be written to memory before a SIGABRT crash. For example, in the code below the 10 byte array can be overflowed to 26 bytes before crashing at 27. Similarly, an array of 20 <code>char</code>s can be overflowed to 40 <code>char</code>s before it aborts on the 41st.</p> <p>Can anyone explain why this is? Also, is the SIGABRT the same as (or caused by) a "segmentation fault"?</p> <p>Mac OS 10.8 - Xcode 4.6, clang and lldb. Thanks!</p> <pre><code>#include &lt;stdio.h&gt; int main(int argc, const char * argv[]) { char aString[ 10 ]; char aLetter = 'a'; printf("The size of one array slot sizeof( aString[0] ) is %zu\n", sizeof(aString[0])); printf("The size of one letter sizeof( aLetter ) is %zu\n", sizeof(aLetter)); // Overflow the aString array of chars // lldb claims aString is initialized with values \0 or NULL at all locations // Substitute i&lt;27 and this code will crash regularly for (int i=0; i&lt;26; i++) { aString[i]= aLetter; } return 0; } </code></pre> <p>EDIT - I've stepped through it in disassembly and found this protection just after the for-loop:</p> <pre><code>0x100000f27: movq 226(%rip), %rax ; (void *)0x00007fff793f24b0: __stack_chk_guard 0x100000f2e: movq (%rax), %rax 0x100000f31: movq -8(%rbp), %rcx 0x100000f35: cmpq %rcx, %rax 0x100000f38: jne 0x100000f49 ; main + 121 at main.c:26 . . . 0x100000f49: callq 0x100000f4e ; symbol stub for: __stack_chk_fail </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload