public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "cheyenne.wills at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug analyzer/110501] New: Invalid use-after-free / realloc
Date: Fri, 30 Jun 2023 15:10:32 +0000	[thread overview]
Message-ID: <bug-110501-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 110501
           Summary: Invalid use-after-free / realloc
           Product: gcc
           Version: 12.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: analyzer
          Assignee: dmalcolm at gcc dot gnu.org
          Reporter: cheyenne.wills at gmail dot com
  Target Milestone: ---

Created attachment 55433
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55433&action=edit
testcase.i

I've ran into a problem where gcc-12 (and later versions) is producing a false
positive on a use-after-free following a realloc.

The attached information obtained from a current gentoo system using gcc
(Gentoo 12.3.1_p20230526 p2) 12.3.1 20230526) 

gcc -v -save-temps -Wall -c testcase.c &> testcase.log


I've been able to duplicate the problem on godbolt.org using different versions
of gcc from gcc 12.1 through gcc master (I also tried various architectures,
x86_64, arm, etc.). godbolt's gcc 11 does work as expected.

The gist of the problem is:
---
 S->sp = realloc(p, size * 2);
 if (S->sp == NULL && size != 0) {
     free(p);    /* << is flagged as a use after free */
     return 0;
 }
---
However the following works:
---
 char *t;
 t = realloc(p, size * 2);
 if (t == NULL && size != 0) {
     free(p);   /* << is not flagged as a use after free */
     return 0;
 }
 S->sp = t;
---

The provided testcase contains 3 simple functions.  The function fail1 and
fail2  has code that shows the invalid use-after-free, while the function
succeeds has code that does not produce the use-after-free message.  The only
difference between the failed functions and the success that the success
function uses a stack based temporary variable to hold the result of the
realloc.


==
$ $ gcc -Wall -Wextra  -c testcase.c 
testcase.c: In function ‘fail1’:
testcase.c:10:9: warning: pointer ‘p’ may be used after ‘realloc’
[-Wuse-after-free]
   10 |         free(p); /* Is flagged as a use after free */
      |         ^~~~~~~
testcase.c:8:13: note: call to ‘realloc’ here
    8 |     S->sp = realloc(p, size*2);
      |             ^~~~~~~~~~~~~~~~~~
testcase.c: In function ‘fail2’:
testcase.c:20:9: warning: pointer ‘p’ may be used after ‘realloc’
[-Wuse-after-free]
   20 |         free(p); /* Is flagged as a use after free */
      |         ^~~~~~~
testcase.c:18:9: note: call to ‘realloc’ here
   18 |     t = realloc(p, size*2);
      |         ^~~~~~~~~~~~~~~~~~
$

==

The problem was originally discovered while building from openafs's master
branch (www.openafs.org) with a gcc-13 compiler. 

  src/external/heimdal/krb5/crypto.c:1157:9: error: pointer ‘p’ may be
      used after ‘realloc’ [-Werror=use-after-free]
   1157 |         free(p);
        |         ^~~~~~~
  src/external/heimdal/krb5/crypto.c:1155:20: note: call to ‘realloc’
      here
   1155 |     result->data = realloc(p, sz);
        |                    ^~~~~~~~~~~~~~

The failing code is part of an "external library" from the heimdal project.

             reply	other threads:[~2023-06-30 15:10 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-30 15:10 cheyenne.wills at gmail dot com [this message]
2023-06-30 15:11 ` [Bug analyzer/110501] " cheyenne.wills at gmail dot com
2023-06-30 16:10 ` [Bug tree-optimization/110501] Invalid use-after-free / realloc at -O0 pinskia at gcc dot gnu.org
2023-06-30 16:12 ` [Bug tree-optimization/110501] Invalid use-after-free / realloc with a store/load happening pinskia at gcc dot gnu.org
2023-06-30 16:16 ` cheyenne.wills at gmail dot com
2023-07-03  6:24 ` rguenth at gcc dot gnu.org
2023-07-06 20:49 ` cheyenne.wills at gmail dot com
2023-10-31 16:18 ` jhb at FreeBSD dot org
2024-03-10 14:03 ` [Bug tree-optimization/110501] [13/14 regression] Invalid -Wuse-after-free " sjames at gcc dot gnu.org
2024-04-04  7:39 ` rguenth at gcc dot gnu.org
2024-04-04  7:39 ` rguenth at gcc dot gnu.org
2024-05-21  9:16 ` [Bug tree-optimization/110501] [13/14/15 " jakub 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-110501-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).