public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug sanitizer/107258] New: ASAN at -O1 failed to detect a stack-under-flow
@ 2022-10-14  8:44 shaohua.li at inf dot ethz.ch
  2022-10-14  9:04 ` [Bug sanitizer/107258] " marxin at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: shaohua.li at inf dot ethz.ch @ 2022-10-14  8:44 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 107258
           Summary: ASAN at -O1 failed to detect a stack-under-flow
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: sanitizer
          Assignee: unassigned at gcc dot gnu.org
          Reporter: shaohua.li at inf dot ethz.ch
                CC: dodji at gcc dot gnu.org, dvyukov at gcc dot gnu.org,
                    jakub at gcc dot gnu.org, kcc at gcc dot gnu.org, marxin at gcc dot gnu.org
  Target Milestone: ---

ASAN with -O1/-Os failed to detect the stack-buffer-underflow, while
-O0/-O2/-O3 can. 

% gcc-tk -v
Using built-in specs.
COLLECT_GCC=gcc-tk
COLLECT_LTO_WRAPPER=/zdata/shaoli/compilers/ccbuilder-compilers/gcc-99da523359e9333555585484eb3b8f854a98f1b4/libexec/gcc/x86_64-pc-linux-gnu/13.0.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../configure --disable-multilib --disable-bootstrap
--enable-languages=c,c++
--prefix=/zdata/shaoli/compilers/ccbuilder-compilers/gcc-99da523359e9333555585484eb3b8f854a98f1b4
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 13.0.0 20221013 (experimental) (GCC)
% 
% cat a.c
int a = -5, c, d;
static int f() {
  int b[10];
  printf("%d", b[a]);
}
int h() {
  for (; c;) {
    int *e[3];
    e;
  }
}
int main() {
  int g[4];
  g[d] = f();
  h();
}
%
% gcc-tk -w -O1 -fsanitize=address a.c && ./a.out
0
% gcc-tk -w -O3 -fsanitize=address a.c && ./a.out
=================================================================
==929009==ERROR: AddressSanitizer: stack-buffer-overflow on address
0x7fabc520007c at pc 0x00000040125d bp 0x7ffe232d2c00 sp 0x7ffe232d2bf8
READ of size 4 at 0x7fabc520007c thread T0
    #0 0x40125c in f /zdata/shaoli/ubfuzz/synthesizer/debug/a.c:4
    #1 0x40125c in main /zdata/shaoli/ubfuzz/synthesizer/debug/a.c:14
    #2 0x7fabc79a8082 in __libc_start_main
(/lib/x86_64-linux-gnu/libc.so.6+0x24082) (BuildId:
1878e6b475720c7c51969e69ab2d276fae6d1dee)
    #3 0x4012fd in _start
(/zdata/shaoli/ubfuzz/synthesizer/debug/a.out+0x4012fd)

Address 0x7fabc520007c is located in stack of thread T0 at offset 124 in frame
    #0 0x4010df in main /zdata/shaoli/ubfuzz/synthesizer/debug/a.c:12

  This frame has 3 object(s):
    [48, 64) 'g' (line 13)
    [80, 104) 'e' (line 8)
    [144, 184) 'b' (line 3) <== Memory access at offset 124 underflows this
variable
HINT: this may be a false positive if your program uses some custom stack
unwind mechanism, swapcontext or vfork
      (longjmp and C++ exceptions *are* supported)
SUMMARY: AddressSanitizer: stack-buffer-overflow
/zdata/shaoli/ubfuzz/synthesizer/debug/a.c:4 in f
Shadow bytes around the buggy address:
  0x0ff5f8a37fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0ff5f8a37fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0ff5f8a37fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0ff5f8a37fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0ff5f8a37ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0ff5f8a38000: f1 f1 f1 f1 f1 f1 00 00 f2 f2 00 00 00 f2 f2[f2]
  0x0ff5f8a38010: f2 f2 00 00 00 00 00 f3 f3 f3 f3 f3 00 00 00 00
  0x0ff5f8a38020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0ff5f8a38030: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0ff5f8a38040: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0ff5f8a38050: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
==929009==ABORTING
%

Compiler explorer: https://godbolt.org/z/Gea6cr9E1

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug sanitizer/107258] ASAN at -O1 failed to detect a stack-under-flow
  2022-10-14  8:44 [Bug sanitizer/107258] New: ASAN at -O1 failed to detect a stack-under-flow shaohua.li at inf dot ethz.ch
@ 2022-10-14  9:04 ` marxin at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: marxin at gcc dot gnu.org @ 2022-10-14  9:04 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Liška <marxin at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |INVALID
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #1 from Martin Liška <marxin at gcc dot gnu.org> ---
Well, it's a stack underflow and it depends how are the variables placed in the
stack frame:

setting a = -1 is caught also in -O1:

==3723==ERROR: AddressSanitizer: stack-buffer-overflow on address
0x7fffffffd5dc at pc 0x000000401404 bp 0x7fffffffd580 sp 0x7fffffffd578
READ of size 4 at 0x7fffffffd5dc thread T0
    #0 0x401403 in main (/home/marxin/Programming/testcases/a.out+0x401403)
    #1 0x7ffff762c5af in __libc_start_call_main (/lib64/libc.so.6+0x275af)
    #2 0x7ffff762c678 in __libc_start_main_impl (/lib64/libc.so.6+0x27678)
    #3 0x4010e4 in _start ../sysdeps/x86_64/start.S:115

Address 0x7fffffffd5dc is located in stack of thread T0 at offset 76 in frame
    #0 0x4012ab in main (/home/marxin/Programming/testcases/a.out+0x4012ab)

  This frame has 2 object(s):
    [48, 64) 'g' (line 13)
    [80, 120) 'b' (line 3) <== Memory access at offset 76 underflows this
variable
HINT: this may be a false positive if your program uses some custom stack
unwind mechanism, swapcontext or vfork
      (longjmp and C++ exceptions *are* supported)
SUMMARY: AddressSanitizer: stack-buffer-overflow
(/home/marxin/Programming/testcases/a.out+0x401403) in main
Shadow bytes around the buggy address:
  0x10007fff7a60: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10007fff7a70: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10007fff7a80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10007fff7a90: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10007fff7aa0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x10007fff7ab0: 00 00 f1 f1 f1 f1 f1 f1 00 00 f2[f2]00 00 00 00
  0x10007fff7ac0: 00 f3 f3 f3 f3 f3 00 00 00 00 00 00 00 00 00 00
  0x10007fff7ad0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10007fff7ae0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10007fff7af0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10007fff7b00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Shadow byte legend (one shadow byte represents 8 application bytes):

As seen using a == -5 causes we load variable 'g'. That's the risk of bigger
overflows and they can happen.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-10-14  9:04 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-14  8:44 [Bug sanitizer/107258] New: ASAN at -O1 failed to detect a stack-under-flow shaohua.li at inf dot ethz.ch
2022-10-14  9:04 ` [Bug sanitizer/107258] " marxin at gcc dot gnu.org

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).