public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug sanitizer/108817] New: ASAN at -O3 failed to detect a global-buffer-overflow
@ 2023-02-16  8:16 shaohua.li at inf dot ethz.ch
  2023-02-17 14:31 ` [Bug sanitizer/108817] " marxin at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: shaohua.li at inf dot ethz.ch @ 2023-02-16  8:16 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 108817
           Summary: ASAN at -O3 failed to detect a global-buffer-overflow
           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: ---

For the following code, ASAN at -O3 (GCC>=12) missed the global
buffer-overflow, while other opt levels detected it. GCC-11 can detect it at
all opt levels. Although I was using -O3, I indeed observed the overflow read
and the ASAN check in the assembly code.

Compiler explorer: https://godbolt.org/z/9Yenz8roT


% cat a.c
long a;
int b[3];
char c;
int main() {
  int d;
  a = 2;
  for (; a >= 0; a--) {
    d = 0;
    for (; d <= 3; d++)
      c = b[a+1];
  }
  return c;
}
%
% gcc-tk -O3 -fsanitize=address a.c && ./a.out
% 
% gcc-tk -O2 -fsanitize=address a.c && ./a.out
=================================================================
==1==ERROR: AddressSanitizer: global-buffer-overflow on address 0x0000004041cc
at pc 0x00000040112b bp 0x7ffd88ccd090 sp 0x7ffd88ccd088
READ of size 4 at 0x0000004041cc thread T0
    #0 0x40112a in main /a.c:10
    #1 0x7f8b3fad2082 in __libc_start_main
(/lib/x86_64-linux-gnu/libc.so.6+0x24082) (BuildId:
1878e6b475720c7c51969e69ab2d276fae6d1dee)
    #2 0x40119d in _start (/a.s+0x40119d) (BuildId:
8f8919b865053b4da8e28daf8de45ed9390d0057)

0x0000004041cc is located 52 bytes before global variable 'a' defined in '/a.c'
(0x404200) of size 8
0x0000004041cc is located 0 bytes after global variable 'b' defined in '/a.c'
(0x4041c0) of size 12
SUMMARY: AddressSanitizer: global-buffer-overflow /a.c:10 in main
Shadow bytes around the buggy address:
  0x000000403f00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x000000403f80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x000000404000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x000000404080: 00 00 00 00 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9
  0x000000404100: f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 00 00 00 00
=>0x000000404180: 01 f9 f9 f9 f9 f9 f9 f9 00[04]f9 f9 f9 f9 f9 f9
  0x000000404200: 00 f9 f9 f9 f9 f9 f9 f9 00 00 00 00 00 00 00 00
  0x000000404280: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x000000404300: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x000000404380: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x000000404400: 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 
...
%

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

* [Bug sanitizer/108817] ASAN at -O3 failed to detect a global-buffer-overflow
  2023-02-16  8:16 [Bug sanitizer/108817] New: ASAN at -O3 failed to detect a global-buffer-overflow shaohua.li at inf dot ethz.ch
@ 2023-02-17 14:31 ` marxin at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: marxin at gcc dot gnu.org @ 2023-02-17 14:31 UTC (permalink / raw)
  To: gcc-bugs

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

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> ---
Here we prove that 'return c' only depends on the last execution of 'c =
b[a+1];' which is going to happen with a == 0 and thus we optimize out that.

$ gcc pr108817.C -fsanitize=address -O3 -fdump-tree-optimized=/dev/stdout

int main ()
{
  signed char _4;
  unsigned long _8;
  int _12;
  unsigned long _15;
  bool _16;
  unsigned long _18;
  int _19;
  char _20;
  bool _21;
  signed char _22;
  signed char _23;
  unsigned long _25;
  signed char * _27;
  bool _38;

  <bb 2> [local count: 26541933]:
  a = 2;
  _15 = (unsigned long) &MEM <int> [(void *)&b + 4B];
(checking if &b + 4 is valid in shadow memory) 
  if (_16 != 0)
    goto <bb 3>; [0.05%]
  else
    goto <bb 4>; [99.95%]

  <bb 3> [local count: 13271]:
  __builtin___asan_report_load4 (_15);

  <bb 4> [local count: 26541933]:
  _19 = b[1]; <- here we use it as the future value
  _20 = (char) _19;
  c = _20;
  a = -1;
  _12 = (int) _20;
  return _12;
}

Final note: note clang does not report the issue even with -O1.

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

end of thread, other threads:[~2023-02-17 14:31 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-16  8:16 [Bug sanitizer/108817] New: ASAN at -O3 failed to detect a global-buffer-overflow shaohua.li at inf dot ethz.ch
2023-02-17 14:31 ` [Bug sanitizer/108817] " 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).