Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

Safe-stack leaks stack with dynamic alloca #26121

Open Quuxplusone opened 8 years ago

Quuxplusone commented 8 years ago
Bugzilla Link PR26122
Status NEW
Importance P normal
Reported by Kuba Mracek (mracek@apple.com)
Reported on 2016-01-12 15:14:17 -0800
Last modified on 2016-01-12 20:18:34 -0800
Version trunk
Hardware All All
CC eugeni.stepanov@gmail.com, ganna@apple.com, llvm-bugs@lists.llvm.org
Fixed by commit(s)
Attachments
Blocks
Blocked by
See also
Safe-stack instrumentation seems to wrongly handle some dynamic alloca cases.
Here's a test case that crashes when -fsanitize=safe-stack is used on it. We
seem to forget to reset the unsafe stack pointer when leaving the function,
causing a stack leak and eventually overrunning the unsafe stack.

#include <alloca.h>
#include <stdio.h>

void f() {
    for (int i = 0; i < 10; i++) {
        int *p = alloca(4096);
        *p = 42;
    }
}

int main(int argc, const char * argv[]) {
    printf("Hello, World!\n");

    for (int i = 0; i < 10000000; i++) {
        f();
    }

    return 0;
}

Disassembly:

f:
    0x100000ed0 <+0>:  pushq  %rbp
    0x100000ed1 <+1>:  movq   %rsp, %rbp
    0x100000ed4 <+4>:  movl   $0x0, -0x4(%rbp)
    0x100000edb <+11>: cmpl   $0xa, -0x4(%rbp)
    0x100000edf <+15>: jge    0x100000f1d               ; <+77> at safe-stack-alloca-crash.c:9
    0x100000ee5 <+21>: movq   %gs:0x80, %rax
    0x100000eee <+30>: addq   $-0x1000, %rax
    0x100000ef4 <+36>: andq   $-0x10, %rax
    0x100000ef8 <+40>: movq   %rax, %gs:0x80
    0x100000f01 <+49>: movq   %rax, -0x10(%rbp)
    0x100000f05 <+53>: movq   -0x10(%rbp), %rax
    0x100000f09 <+57>: movl   $0x2a, (%rax)
    0x100000f0f <+63>: movl   -0x4(%rbp), %eax
    0x100000f12 <+66>: addl   $0x1, %eax
    0x100000f15 <+69>: movl   %eax, -0x4(%rbp)
    0x100000f18 <+72>: jmp    0x100000edb               ; <+11> at safe-stack-alloca-crash.c:5
    0x100000f1d <+77>: popq   %rbp
    0x100000f1e <+78>: retq
Quuxplusone commented 8 years ago

The stack pointer is popped in moveStaticAllocasToUnsafeStack and it won't happen if there are no static allocas. I'll prepare a patch.