public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug analyzer/110501] New: Invalid use-after-free / realloc
@ 2023-06-30 15:10 cheyenne.wills at gmail dot com
  2023-06-30 15:11 ` [Bug analyzer/110501] " cheyenne.wills at gmail dot com
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: cheyenne.wills at gmail dot com @ 2023-06-30 15:10 UTC (permalink / raw)
  To: gcc-bugs

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.

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

* [Bug analyzer/110501] Invalid use-after-free / realloc
  2023-06-30 15:10 [Bug analyzer/110501] New: Invalid use-after-free / realloc cheyenne.wills at gmail dot com
@ 2023-06-30 15:11 ` 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
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: cheyenne.wills at gmail dot com @ 2023-06-30 15:11 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Cheyenne Wills <cheyenne.wills at gmail dot com> ---
Created attachment 55434
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55434&action=edit
stdout/stderr from compile

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

* [Bug tree-optimization/110501] Invalid use-after-free / realloc at -O0
  2023-06-30 15:10 [Bug analyzer/110501] New: Invalid use-after-free / realloc cheyenne.wills at gmail dot com
  2023-06-30 15:11 ` [Bug analyzer/110501] " cheyenne.wills at gmail dot com
@ 2023-06-30 16:10 ` 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
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-30 16:10 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|Invalid use-after-free /    |Invalid use-after-free /
                   |realloc                     |realloc at -O0

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
At -O0, the redundant load from S->sp is not optimized out ... so the warning
does not realize that it was checking against the return value of realloc ...

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

* [Bug tree-optimization/110501] Invalid use-after-free / realloc with a store/load happening
  2023-06-30 15:10 [Bug analyzer/110501] New: Invalid use-after-free / realloc cheyenne.wills at gmail dot com
  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 ` pinskia at gcc dot gnu.org
  2023-06-30 16:16 ` cheyenne.wills at gmail dot com
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-30 16:12 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Oh GCC warns even with optimizations turned on ...

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

* [Bug tree-optimization/110501] Invalid use-after-free / realloc with a store/load happening
  2023-06-30 15:10 [Bug analyzer/110501] New: Invalid use-after-free / realloc cheyenne.wills at gmail dot com
                   ` (2 preceding siblings ...)
  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
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: cheyenne.wills at gmail dot com @ 2023-06-30 16:16 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Cheyenne Wills <cheyenne.wills at gmail dot com> ---
(In reply to Andrew Pinski from comment #3)
> Oh GCC warns even with optimizations turned on ...

Correct.  For gcc-12 the failure only occurs with -O0.  With gcc-13 (and
later), the problem occurs with or without optimization.

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

* [Bug tree-optimization/110501] Invalid use-after-free / realloc with a store/load happening
  2023-06-30 15:10 [Bug analyzer/110501] New: Invalid use-after-free / realloc cheyenne.wills at gmail dot com
                   ` (3 preceding siblings ...)
  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
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-07-03  6:24 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2023-07-03
     Ever confirmed|0                           |1

--- Comment #5 from Richard Biener <rguenth at gcc dot gnu.org> ---
So confirmed.

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

* [Bug tree-optimization/110501] Invalid use-after-free / realloc with a store/load happening
  2023-06-30 15:10 [Bug analyzer/110501] New: Invalid use-after-free / realloc cheyenne.wills at gmail dot com
                   ` (4 preceding siblings ...)
  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
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: cheyenne.wills at gmail dot com @ 2023-07-06 20:49 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Cheyenne Wills <cheyenne.wills at gmail dot com> ---
Just another bit of information.

Specifying just -Werror=use-after-free appears to be not not enough to trigger
the problem.  Using -Wall however does trigger the problem.

(tried on gcc-12 and gcc-13)

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

* [Bug tree-optimization/110501] Invalid use-after-free / realloc with a store/load happening
  2023-06-30 15:10 [Bug analyzer/110501] New: Invalid use-after-free / realloc cheyenne.wills at gmail dot com
                   ` (5 preceding siblings ...)
  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
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: jhb at FreeBSD dot org @ 2023-10-31 16:18 UTC (permalink / raw)
  To: gcc-bugs

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

John Baldwin <jhb at FreeBSD dot org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jhb at FreeBSD dot org

--- Comment #7 from John Baldwin <jhb at FreeBSD dot org> ---
I believe I am hitting this same issue using GCC 13 to compile FreeBSD's
userland. (I looked through several of the other bugs for the Wuse-after-free
metabug and this one seems the closest to what I'm encountering).  The code in
question does not trigger the -Wuse-after-free warning when using GCC 12, but
now triggers the warning with GCC 13.  It is just calling free on the old value
after realloc fails which should be safe.  The simplest version I've
encountered so far in FreeBSD's tree is this:

static int
group_resize(void)
{
        char *buf;

        if (gbufsize == 0)
                gbufsize = 1024;
        else
                gbufsize *= 2;

        buf = gbuffer;
        gbuffer = realloc(buf, gbufsize);
        if (gbuffer == NULL) {
                free(buf);
                gbufsize = 0;
                return (ENOMEM);
        }
        memset(gbuffer, 0, gbufsize);

        return (0);
}

The warning triggers on the call to free:

    lib/libcasper/services/cap_grp/cap_grp.c: In function 'group_resize':
    lib/libcasper/services/cap_grp/cap_grp.c:65:17: error: pointer 'buf' may be
used after 'realloc' [-Werror=use-after-free]
       65 |                 free(buf);
          |                 ^~~~~~~~~
    lib/libcasper/services/cap_grp/cap_grp.c:63:19: note: call to 'realloc'
here
       63 |         gbuffer = realloc(buf, gbufsize);
          |                   ^~~~~~~~~~~~~~~~~~~~~~

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

* [Bug tree-optimization/110501] [13/14 regression] Invalid -Wuse-after-free / realloc with a store/load happening
  2023-06-30 15:10 [Bug analyzer/110501] New: Invalid use-after-free / realloc cheyenne.wills at gmail dot com
                   ` (6 preceding siblings ...)
  2023-10-31 16:18 ` jhb at FreeBSD dot org
@ 2024-03-10 14:03 ` 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
  9 siblings, 0 replies; 11+ messages in thread
From: sjames at gcc dot gnu.org @ 2024-03-10 14:03 UTC (permalink / raw)
  To: gcc-bugs

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

Sam James <sjames at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[12/13/14 regression]       |[13/14 regression] Invalid
                   |Invalid -Wuse-after-free /  |-Wuse-after-free / realloc
                   |realloc with a store/load   |with a store/load happening
                   |happening                   |

--- Comment #8 from Sam James <sjames at gcc dot gnu.org> ---
oh, right, it was added in 12. But given 12 only warns with -O0 and 13+ warn
with optimisation, let's call it a 13/14 regression.

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

* [Bug tree-optimization/110501] [13/14 regression] Invalid -Wuse-after-free / realloc with a store/load happening
  2023-06-30 15:10 [Bug analyzer/110501] New: Invalid use-after-free / realloc cheyenne.wills at gmail dot com
                   ` (7 preceding siblings ...)
  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
  9 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu.org @ 2024-04-04  7:39 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |13.3

--- Comment #9 from Richard Biener <rguenth at gcc dot gnu.org> ---
(In reply to John Baldwin from comment #7)
> I believe I am hitting this same issue using GCC 13 to compile FreeBSD's
> userland. (I looked through several of the other bugs for the
> Wuse-after-free metabug and this one seems the closest to what I'm
> encountering).  The code in question does not trigger the -Wuse-after-free
> warning when using GCC 12, but now triggers the warning with GCC 13.  It is
> just calling free on the old value after realloc fails which should be safe.
> The simplest version I've encountered so far in FreeBSD's tree is this:
> 
> static int
> group_resize(void)
> {
> 	char *buf;
> 
> 	if (gbufsize == 0)
> 		gbufsize = 1024;
> 	else
> 		gbufsize *= 2;
> 
> 	buf = gbuffer;
> 	gbuffer = realloc(buf, gbufsize);
> 	if (gbuffer == NULL) {
> 		free(buf);

the problem with this is that 'gbuffer' is a global and we diagnose this
early before applying memory CSE so we don't see the 'gbuffer' being
stored and 'gbuffer' being tested against NULL as equal.

IIRC with GCC 13 we moved some diagnostics early from late as late has
issues with jump threading exposing very many false positives.

Testcase (which I believe we have a duplicate for):

void *gbuffer;
unsigned long gbufsize;
void foo()
{
  char *buf = gbuffer;
  gbuffer = __builtin_realloc(buf, gbufsize);
  if (!gbuffer) {
      __builtin_free(buf);
      return;
  }
  __builtin_memset (gbuffer, 0, gbufsize);
}

I'll note that for the early -Wuninitialized I added cheap value numbering
to prune unreachable code paths.  The plan was to wrap all of the early
diagnostics with such cheap VN _analysis_ (but not do any code changes)
which would allow these cases to be identified as equal.  The diagnostic
passes would need to apply some valueization then of course.

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

* [Bug tree-optimization/110501] [13/14 regression] Invalid -Wuse-after-free / realloc with a store/load happening
  2023-06-30 15:10 [Bug analyzer/110501] New: Invalid use-after-free / realloc cheyenne.wills at gmail dot com
                   ` (8 preceding siblings ...)
  2024-04-04  7:39 ` rguenth at gcc dot gnu.org
@ 2024-04-04  7:39 ` rguenth at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu.org @ 2024-04-04  7:39 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P3                          |P2

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

end of thread, other threads:[~2024-04-04  7:39 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-30 15:10 [Bug analyzer/110501] New: Invalid use-after-free / realloc cheyenne.wills at gmail dot com
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

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