llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

StackProtector - stack violation not caught

Open rlougher opened this issue 7 years ago • 12 comments

Bugzilla Link 43308
Resolution FIXED
Resolved on Nov 21, 2019 16:04
Version trunk
OS Windows NT
Blocks llvm/llvm-project#42705
CC @aemerson,@efriedma-quic,@fhahn,@gregbedwell,@zmodem,@jdoerfert,@jmorse,@arsenm,@pogo59,@pogo59,@tstellar,@wjristow,@yuanfang-chen
Fixed by commit(s) r373216 r373219 r373220 c1d76f4 76817ab 28c1f51

Extended Description

The following simple program corrupts the stack:

================= ssp.c ================== #include <string.h>

attribute((noinline)) void bar(int *p) { memset(p, 0, 100); }

int main() { int i; bar(&i); }

If this is compiled at -O0 with strong protection the violation is caught:

$ clang ssp.c -o ssp -O0 -fstack-protector-strong $ ./ssp *** stack smashing detected ***: terminated Aborted (core dumped)

However, if it is compiled at -O1 or above, no violation is detected and the program crashes with a SIGSEGV (return to addresss 0):

$ clang ssp.c -o ssp -O1 -fstack-protector-strong $ ./ssp Segmentation fault (core dumped)

The regression was introduced by commit r363169, which changed the stack protector to use PointerMayBeCaptured.

This routine is inappropriate for SSP use, as protection is needed in cases where a pointer is not captured.

If we look at the IR for the above program, we see that the pointer parameter to bar() is marked as "nocapture" (i.e. it does not outlive the call). This means that although it is used to corrupt the stack, after r363169 main() is no longer considered to need a stack check.

define dso_local void @​bar(i32* nocapture %p) local_unnamed_addr #​0 { entry: %0 = bitcast i32* %p to i8* tail call void @​llvm.memset.p0i8.i64(i8* align 4 dereferenceable(100) %0, i8 0, i64 100, i1 false) ret void }

define dso_local i32 @​main() local_unnamed_addr #​2 { entry: %i = alloca i32, align 4 %0 = bitcast i32* %i to i8* call void @​llvm.lifetime.start.p0i8(i64 4, i8* nonnull %0) #​3 call void @​bar(i32* nonnull %i) call void @​llvm.lifetime.end.p0i8(i64 4, i8* nonnull %0) #​3 ret i32 0 }

rlougher avatar Sep 13 '19 14:09 rlougher

mentioned in issue llvm/llvm-bugzilla-archive#43478

pogo59 avatar Nov 27 '21 02:11 pogo59

mentioned in issue llvm/llvm-project#42705

nico avatar Nov 27 '21 02:11 nico

Merged: c1d76f4 76817ab 28c1f51

tstellar avatar Nov 22 '19 00:11 tstellar

For r373216, I had to modify the commit to avoid changing the ABI, can someone take a look at the patch and make sure it looks OK:

https://github.com/tstellar/llvm-project/pull/15

I put a comment on that pull request--IMO the VisitedPHIs needs to be outside the BasicBlock loop.

pogo59 avatar Nov 21 '19 18:11 pogo59

Can we merge these 3 commits now? Were there any other follow-up fixes?

No, it's just those 3. There are opportunities to refine the SSP checks further, but this set gives us the additional coverage Matt A was looking for, without losing anything we had previously.

For r373216, I had to modify the commit to avoid changing the ABI, can someone take a look at the patch and make sure it looks OK:

https://github.com/tstellar/llvm-project/pull/15

tstellar avatar Nov 21 '19 17:11 tstellar

Can we merge these 3 commits now? Were there any other follow-up fixes?

No, it's just those 3. There are opportunities to refine the SSP checks further, but this set gives us the additional coverage Matt A was looking for, without losing anything we had previously.

pogo59 avatar Nov 12 '19 23:11 pogo59

r373216 reverted r363169; fixes this PR, re-opens bug 42238 and bug 40436. r373219 did an NFC refactor. r373220 re-fixes bug 42238.

Assuming they stick, all three should be merged to 9.0.1. Leaving this open to allow tracking that.

Can we merge these 3 commits now? Were there any other follow-up fixes?

tstellar avatar Nov 12 '19 22:11 tstellar

r373216 reverted r363169; fixes this PR, re-opens bug 42238 and bug 40436. r373219 did an NFC refactor. r373220 re-fixes bug 42238.

Assuming they stick, all three should be merged to 9.0.1. Leaving this open to allow tracking that.

pogo59 avatar Sep 30 '19 15:09 pogo59

Do we want to consider fixing this in llvm 9 (9.0.1 if it's too late for 9.0.0)?

It's too late for 9.0.1, but fixing for 9.0.1 would be good.

zmodem avatar Sep 16 '19 08:09 zmodem

Yes, I agree, the nocapture deduction is right. It's just that "Local variables that have had their address taken" from the description of sspstrong doesn't really describe what we really care about. The important property is whether we can prove there are no out-of-bounds accesses.

efriedma-quic avatar Sep 13 '19 22:09 efriedma-quic

Tldr, IMHO nocapture deduction is fine here.

AsWithout looking into the actual use, it sounds to me that Robert is right in saying:

This routine (PointerMayBeCaptured) is inappropriate for SSP use, as protection is needed in cases where a pointer is not captured.

Once we turn on the Attributor this will only become more evident.

If you think it is helpful for me to look at the use site in ssp, please let me know.

jdoerfert avatar Sep 13 '19 22:09 jdoerfert

So when optimizing the test-case that Robert posted (and when applying '-fstack-protector-strong'), llvm 8.0 properly detects that security violation: $ llvm.8.0/bin/clang ssp.c -o ssp -O1 -fstack-protector-strong $ ./ssp *** stack smashing detected ***: ./ssp terminated Aborted (core dumped) $

But with the current llvm 9.0 compiler, this blatant security hole is missed: $ llvm.9.0.pre/bin/clang ssp.c -o ssp -O1 -fstack-protector-strong $ ./ssp Segmentation fault (core dumped) $

For people concerned about security, I think this is a very serious security regression in the current llvm 9.0 compiler relative to llvm 8.0 (and earlier releases).

Do we want to consider fixing this in llvm 9 (9.0.1 if it's too late for 9.0.0)?

wjristow avatar Sep 13 '19 17:09 wjristow