Bug List: (This bug is not in your last search results)   Show last search results      Search page      Enter new bug
Bug#: 19351
Product:  
Component:  
Status: ASSIGNED
Resolution:
Assigned To: Florian Weimer <fw@deneb.enyo.de>
Host:
Reported against  
Priority:  
Severity:  
Target Milestone:  
 
 
Target:
Reporter: Florian Weimer <fw@deneb.enyo.de>
Add CC:
CC:
Remove selected CCs
Build:
URL:
Summary:
Keywords:
Known to work:
Known to fail:

Attachment Description Type Created Size Actions
Create a New Attachment (proposed patch, testcase, etc.) View All

Bug 19351 depends on: Show dependency tree
Show dependency graph
Bug 19351 blocks:

Additional Comments:




Mark bug as waiting for feedback
Mark bug as suspended




View Bug Activity   |   Format For Printing   |   Clone This Bug


Description:   Last confirmed: 2005-12-18 20:26 Opened: 2005-01-09 22:18
operator new[] sometimes returns pointers to heap blocks which are too small. 
When a new array is allocated, the C++ run-time has to calculate its size.  The
product may exceed the maximum value which can be stored in a machine register.
 This error is ignored, and the truncated value is used for the heap allocation.

This may lead to heap overflows and therefore security bugs.  (See
http://cert.uni-stuttgart.de/advisories/calloc.php for further references.)

The test case below uses a user-defined operator new[] to test for the presence
of this problem.  However, the problem itself occurs also with the default
operator new[], but it is probably harder to write a portable test case.

#include <testsuite_hooks.h>

struct foo
{
  char data[16];
  void* operator new[] (size_t size)
  {
    VERIFY(size != sizeof(foo));
    VERIFY (false);
    return malloc(size);
  }
};

int
main()
{
  size_t size = size_t (-1) / sizeof(foo) + 2;
  try
    {
      foo* f = new foo[size];
      VERIFY (f == 0);
      VERIFY (false);
    }
  catch(std::bad_alloc&)
    {
      return 0;
    }
}

------- Comment #1 From Andrew Pinski 2005-01-09 22:25 -------
This is undefined behavor. 

Really there is nothing we can do, just think about this again, the programmer should catch this when 
they read in the length.

------- Comment #2 From Florian Weimer 2005-01-09 22:35 -------
Why is this undefined behavior?  Would you quote chapter and verse, please?

GCC's behavior violates 5.3.4(10):

"A new-expression passes the amount of space requested to the allocation
function as the first argument of type std::size_t.  That argument shall be no
less than the size of the object being created; [...]"

In this case, the passed value is 16, which is much smaller than the size of the
array.

------- Comment #3 From Andrew Pinski 2005-01-09 22:45 -------
But the C++ standard does not say anything about this case.

------- Comment #4 From Andrew Pinski 2005-01-09 22:47 -------
I would get a clearification from the standards comittee if I were you. 
multiplying a large unsigned 
number by 16 and getting an overflow is werid case but again, the developer
should be checking the 
size for reality, if they don't it can cause other problems like a seg fault as
malloc on linux does not 
return null when running out of memory.

------- Comment #5 From Wolfgang Bangerth 2005-01-09 22:48 -------
I also wonder what the semantics are that you are expecting? I mean,  
you try to allocate an array that is so large that you can't address  
the individual bytes using a size_t, in other words one that is larger   
than the address space the OS provides to your program. That clearly  
doesn't make any sense.  
  
That being said, I understand that the behavior of this is security  
relevant and that any attempt to allocate more memory than is available  
will necessarily have to fail. Thus, our present implementation isn't  
standards conforming, since it returns a reasonable pointer even in  
the case that the allocation should have failed. I would therefore agree  
that this is a problem, and given that memory allocation is an expensive  
operation, one overflow check isn't really time critical.  
  
Unfortunately, the situation is not restricted to libstdc++'s implementation  
of operator new[], since that operator only gets the total size of  
the memory to be allocation, not the size per element and the number of  
elements. Therefore, by the time we get into the implementation of this  
operator, it is already too late. In other words, the overflow check has  
to happen in compiler-generated code, not in the libstdc++ implementation.  
  
I would support the introduction of such code, if necessary guarded by  
some flag, or unconditionally, as a matter of quality of implemetation.  
  
W.  

------- Comment #6 From Florian Weimer 2005-01-09 23:07 -------
There's no multiplication in the source code.  The multiplication is an
implementation detail.  You can hardly use it to justify the semantics of the
operation.

I would expect that std::bad_alloc is thrown.  But I agree that the C++ standard
isn't very clear in this area.  The implementation must ensure that the
postcondition in 5.3.4(10) holds, but the standard doesn't provide a means to
signal failure.  I'm going to post a note to comp.std.c++ on this matter, but
hopefully this will be fixed in GCC as a quality of implementation issue.

The necessary overflow check is should be very cheap because the multiplication
is always by a constant.

------- Comment #7 From Andrew Pinski 2005-01-09 23:11 -------
(In reply to comment #6)
> There's no multiplication in the source code.  The multiplication is an
> implementation detail.  You can hardly use it to justify the semantics of the
> operation.

Actually the multiplication is not an implementation detail.
The standard says what opator new[] should be passed, just the multiplication is included.
sizeof(int[i]) (well if it was valid C++, it is valid C99) is defined as a multiplication so it is not an 
implemenation detail.

------- Comment #8 From Geoff Keating 2006-09-27 23:51 -------
Isn't this handled by -ftrapv?

------- Comment #9 From Andrew Pinski 2006-09-27 23:56 -------
Subject: Re:  operator new[] can return heap blocks which are too small

> 
> 
> 
> ------- Comment #8 from geoffk at gcc dot gnu dot org  2006-09-27 23:51 -------
> Isn't this handled by -ftrapv?

No because sizeof is unsigned and -ftrapv only deals with signed types.

-- Pinski

------- Comment #10 From Mark Mitchell 2007-03-23 15:00 -------
What does the C standard say about calloc?  That's a similar case; the
multiplication is in calloc.  Does it have to report an error?

------- Comment #11 From Andreas Schwab 2007-03-23 15:09 -------
"The calloc function allocates space for an array of nmemb objects, each of
whose size is size."
There is no mentioning of overflow, but the allocated space must surely be big
enough to hold the array, and calloc shall fail if it cannot fulfill the
request.

------- Comment #12 From Florian Weimer 2007-03-23 15:23 -------
Subject: Re:  operator new[] can return heap blocks which are too small

* mmitchel at gcc dot gnu dot org:

> What does the C standard say about calloc?  That's a similar case; the
> multiplication is in calloc.  Does it have to report an error?

My interpretation is that it must return NULL.  (This was fixed in GNU
libc years ago.)

------- Comment #13 From Andrew Pinski 2008-04-01 20:46 -------
*** Bug 35790 has been marked as a duplicate of this bug. ***

------- Comment #14 From Andrew Pinski 2008-04-01 20:47 -------
Also note unsigned types don't overflow, they wrap.  So as far as I can tell,
C++ defines this as being returning too small of a size.

------- Comment #15 From felix-gcc@fefe.de 2008-04-01 21:24 -------
I think we can all agree it does not matter what we call this problem.
Real world programs have security problems because of this.
-fstack-protector carries a much larger run-time cost and gcc still offers it,
and there is even less grounds to argue by any C or C++ standard that it's not
the programmer's fault.  gcc still offers it.

As mentioned in the other bug, Microsoft Visual C++ already does this check. 
They do it like this.  After the multiplication they check of the overflow flag
is set, which on x86 indicates the result does not fit in the lower 32 bits. 
If so, instead of the truncated value it passes (size_t)-1 the operator new,
which causes that operator new to fail (in the default case at least, a user
may define its own operator new and that one might still return something).

My favorite solution would be for the code to fail immediately.  Throw an
exception or return NULL, depending on which operator new the program called.

------- Comment #16 From Richard Guenther 2008-04-01 21:51 -------
I agree.  Patches welcome.

------- Comment #17 From Mike Stump 2008-12-09 23:24 -------
I agree, Apple would like this as well...

radr://5739832

Bug List: (This bug is not in your last search results)   Show last search results      Search page      Enter new bug