https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112635

            Bug ID: 112635
           Summary: stack smash protection does not work when code is
                    compiled with -O
           Product: gcc
           Version: 13.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libgcc
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ago at gentoo dot org
  Target Milestone: ---

If this is a known thing, please forgive me.

I was playing with the stack-smash protection and this is what I have found.

Please consider the following block of code:

#include <stdio.h>

int main () {
        char *buf[1];
        buf[1] = "A (stack-based) buffer overflow";
        printf("%s \n", buf[1]);
        return 0;
}

If compiled with -fsanitize=address I get:
==24180==ERROR: AddressSanitizer: stack-buffer-overflow on address
0x7f54c7600028 at pc 0x000000401210 bp 0x7fff54f73180 sp 0x7fff54f73178
WRITE of size 8 at 0x7f54c7600028 thread T0

If compiled with -fstack-protector-strong I get:

A (stack-based) buffer overflow 
*** stack smashing detected ***: terminated
Aborted

But while compiled with -fstack-protector-strong and -O1 (and also -O2/-O3/-Os)
I just get it working without any issue (it prints "A (stack-based) buffer
overflow")

So in the practice it does not work when -O is used.

I'm running gcc 13 on Gentoo, but I have reproduced this issue on ubuntu-22
(gcc-11.4.0), ubuntu-20 (gcc-9.4.0), and also centos-7 (gcc-4.8.5)

A side note:
I used checksec (https://github.com/slimm609/checksec.sh) basically a `readelf
-W -s a.out | grep "stack_chk_fail"` in this case to understand what happens
and I can see:
- when '-fstack-protector-strong' reports canary found (as expected)
- when '-fstack-protector-strong -O2' reports NO canary found
- when '-fstack-protector-all -O2' reports canary found but no 'stack smashing
detected' at runtime

Reply via email to