public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "ago at gentoo dot org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libgcc/112635] New: stack smash protection does not work when code is compiled with -O
Date: Mon, 20 Nov 2023 10:32:45 +0000	[thread overview]
Message-ID: <bug-112635-4@http.gcc.gnu.org/bugzilla/> (raw)

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	other threads:[~2023-11-20 10:32 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-20 10:32 ago at gentoo dot org [this message]
2023-11-20 10:45 ` [Bug tree-optimization/112635] " rguenth at gcc dot gnu.org
2023-11-20 10:52 ` ago at gentoo dot org
2023-12-07  8:20 ` ago at gentoo dot org
2023-12-07 10:58 ` xry111 at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-112635-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).