Red Hat Bugzilla – Bug 358111
glibc's printf(3) tries to allocate a 2^64-byte buffer
Last modified: 2013-03-13 16:41:41 EDT
Description of problem:printf format string abuse can make the function try to
allocate a buffer of length just short of 2^64 bytes.
Version-Release number of selected component (if applicable): glibc-2.7-2
How reproducible: every time
Steps to Reproduce:
1. strace /usr/bin/printf %.2147483647f 1
2. note attempt to allocate ridiculously large amount of memory:
mmap(NULL, 18446744071562072064, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = -1 ENOMEM (Cannot allocate memory)
Actual results: see above
At worst, I would expect it to try to allocate ~2^31 bytes, not 2^64.
Even better would be if it worked like FreeBSD 6.1's printf and didn't
allocate all of that memory for nothing.
Created attachment 243321 [details]
add check of malloc return value
I looked at the code and see two problems.
First, an unchecked malloc, then there's the fact that
both prec and width are of type "int", so it looks like this expression can
overflow, when either approaches 2^31:
workstart = (CHAR_T *) malloc ((MAX (prec, width) + 32) * sizeof (CHAR_T));
besides, this is the only assignment of malloc'd memory to "workstart" that is
not checked. The patch below adds the missing check. This doesn't fix the bug,
but at least keeps glibc from trying to allocate so much memory a second time.
I've fixed the computation of the buffer size but I won't change the
implementation to avoid the allocations since this only means that reasonable
code doesn't run as well. We're not optimizing for the ridiculous.
Thanks for looking at that.
However, don't you want the following patch, too?
From my reading, your new __builtin_expect-0 test will always be false,
since prec is a positive signed int value: thus guaranteed to be smaller
than the unsigned SIZE_MAX-31.
And even if an "int" value could approach the maximum size_t in magnitude,
your "31" would then have to be "32". Otherwise, with prec = SIZE_MAX - 31,
that test would fail (because it'd be ==, not > ) and the subsequent
(size_t) prec + 32 would overflow to 0.
attached patch below...
Created attachment 295108 [details]
* stdio-common/vfprintf.c (vfprintf): Remove dead code.
No, that test is not dead, it's just incomplete. I've checked in a patch.
This bug can be closed, I think.
Thanks. That looks good now. For the record, even now, the test/block *is*
still dead code in most cases: when sizeof CHAR_T == 1 || sizeof int < sizeof
So it's *not* dead only for the wide-char functions and then, only when compiled
with int and size_t having the same width.