efree() on uninitialized Heap data in imagescale leads to use-after-free
The imagescale() function in PHP's bundled libgd frees an uninitialised pointer when an integer overflow check fails, and a related use-after-free exists in the same function. An attacker could potentially exploit this to bypass safe mode locally and possibly remotely.
Originally disclosed on HackerOne
- Triaged in 4 months
- Fixed in 21 months
- Paid in 21 months
- Disclosed after 21 months
The core bug: https://bugs.php.net/bug.php?id=77269
This bugfix actually involves two vulnerabilities: a call to efree on uninitialized data and another free() based vulnerability. What is described below is a bug that was fixed in libgd two years ago (CVE-2016-10166), but the patch was never applied to PHP's libgd. Furthermore, the patch for that CVE introduced a use after free vulnerability, also in PHPs imagescale() function. This can be seen in the comment history of the PHP bug.
The bug occurs in ext/gd/libgd/gd_interpolation.c in the function _gdContributionsAlloc(int line_size, int windows_size). The function will attempt to allocate helper structs and receives two parameters: the line size and the windows size. To prevent integer overflows, each parameter is passed to gd's overflow2() function before being used in the gdMalloc function. (gdMalloc is just #define gdMalloc emalloc).
However, if the overflow2 check for windows size is positive, overflow_error is set to true, which leads to gd attempting to free all the lines allocated so far. The issue is that gd does not check if any lines have been allocated so far at all. By supplying input that leads to overflow2 being true, .Weights is freed, which is an unintialized pointer.
if (overflow2(line_length, sizeof(ContributionType))) {
gdFree(res);
return NULL;
}
res->ContribRow = (ContributionType *) gdMalloc(line_length * sizeof(ContributionType));
if (res->ContribRow == NULL) {
gdFree(res);
return NULL;
}
for (u = 0 ; u < line_length ; u++) {
if (overflow2(windows_size, sizeof(double))) {
overflow_error = 1;
} else {
res->ContribRow[u].Weights = (double *) gdMalloc(windows_size * sizeof(double));
}
if (overflow_error == 1 || res->ContribRow[u].Weights == NULL) {
unsigned int i;
u--;
for (i=0;i<=u;i++) {
gdFree(res->ContribRow[i].Weights);
}
When the for loop is reached that frees the uninitialized pointers, i will be 0 and u too. However, before the for loop is entered u is decremented by one so it will turn into -1 , which leads to the condition i <=0 never being met.
Impact
This vulnerability can be used to write a local safe mode bypass and can potentially be exploited remotely.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The cleanup loop frees pointers that were never allocated because the code does not check whether any lines were allocated before freeing, and a decrement of the loop counter prevents the loop from running.
Impact
This vulnerability can be used to write a local safe mode bypass and can potentially be exploited remotely.
What the attacker needed
Ability to call imagescale() with crafted input
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
When a program needs memory, it asks the system for a block and gets back an address. If it later frees that address without ever setting it, it may free a random location, which can crash the program or let an attacker control what happens next. Here, a size check fails and the cleanup code frees a pointer that was never given a valid value.
What is Memory corruption?
Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.