public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "jpegqs at gmail dot com" <gcc-bugzilla@gcc.gnu.org> To: gcc-bugs@gcc.gnu.org Subject: [Bug c/98168] New: Optimization that can lead to security vulnerabilities Date: Mon, 07 Dec 2020 04:51:56 +0000 [thread overview] Message-ID: <bug-98168-4@http.gcc.gnu.org/bugzilla/> (raw) https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98168 Bug ID: 98168 Summary: Optimization that can lead to security vulnerabilities Product: gcc Version: 10.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: jpegqs at gmail dot com Target Milestone: --- Created attachment 49692 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=49692&action=edit bounds.c I encountered a bug (98159) that you refused to fix because it is "undefined behavior". But this code proves that this "compiler" behavior can lead to security vulnerabilities in some software. Here GCC thinks that if both signed integers are positive, then the sum of these integers is also positive. And removes the next bounds check for the negative values (it could be written different, but this is the common way). int test(int a, int b, int *buf) { if (a >= 0 && b >= 0) { a += b; // let's check that we are not reading outside the buffer if (a >= 0 && a < 8) return buf[a]; } return -1; } So this code supposed to read the element A+B from a buffer of 8 values. And if the sum is out of the buffer, then return -1. But when compiling with GCC -O2/O3 on x86/x86_64 (and possibly others), you can pass A=0x7fffffff, B=0x7fffffff and access buf[-2] (as with any negative value except -1). Thus, optimizations that falsely assume that the target machine is performing signed integer saturation when it is not - should be considered dangerous. In my opinion, UB in C has a different purpose, it exists because C is a low-level language and in most cases can use a single machine instruction for a general operation. So for compilers it should be "target machine behavior", not "we can do anything". And compilers must maintain this behavior while removing some operations when optimizing the code.
next reply other threads:[~2020-12-07 4:51 UTC|newest] Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top 2020-12-07 4:51 jpegqs at gmail dot com [this message] 2020-12-07 5:10 ` [Bug c/98168] " pinskia at gcc dot gnu.org 2020-12-07 16:02 ` msebor at gcc dot gnu.org 2020-12-07 21:49 ` egallager at gcc dot gnu.org 2020-12-07 21:57 ` egallager at gcc dot gnu.org 2020-12-08 10:22 ` redi 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-98168-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: linkBe 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).