REDHAT-BUG-2574514: Double Free
Attached is the patch for the gpgme package (https://github.com/gpg/gpgme/blob/master/src/engine-gpg.c).
You can credit Johan Carlsson (joaxcar) (https://hackerone.com/joaxcar?type=user) for discovering this flaw and reporting it to GitLab via HackerOne.
The attached diagram walks the full chain: 1. The crafted signature drives readstatus down the early-return-after-realloc path. 2. The freed status buffer is handed back out to a live object, the backing store of a Ruby Array. 3. The attacker overwrites that object's data pointer and length and aims it at the key's address elsewhere in the process, and the key bytes are returned in an ordinary HTTP response.
== The jemalloc environment ==
GitLab's Linux package runs the whole web process on a Ruby interpreter built with jemalloc 5.3.0. Every malloc/realloc/free in the process goes through jemalloc, including those inside libgpgme. The freed status buffer in the report is 6144 bytes, which is a jemalloc small size class, so jemalloc holds it in its per-thread cache (tcache) and reuses it last-in-first-out.
jemalloc's production free path performs no double-free check, so the second free silently returns the chunk to the cache while the first owner is still live. That reuse is deterministic, which is what makes the aliasing reliable.
== Is this really different on glibc and Windows? ==
The defect is allocator-independent. What differs is only how easily the double free becomes exploitable. glibc is not a safety net here, for two independent reasons:
1. Size. glibc’s double-free check (the tcache key, since 2.29) only covers chunks up to ~1032 bytes by default. The 6144-byte buffer is too large, so the check never applies.
2. Access pattern. glibc’s check only catches back-to-back free(p); free(p). This bug is free(p); malloc(...); free(p): the allocation in between clears the tcache key, so the second free isn’t flagged even for small chunks. glibc would give the same aliasing with different grooming.
See tcachedoublefreeverify() in malloc.c (https://sourceware.org/git/?p=glibc.git;a=blob;f=malloc/malloc.c) for more details.
https://ir0nstone.gitbook.io/notes/binexp/heap/tcache-keys has a good explanation of this code.
Per the section of "Security Mechanisms" from https://blackhat.com/docs/us-16/materials/us-16-Yason-Windows-10-Segment-Heap-Internals.pdf, Windows encodes and key-protects its heap metadata and randomizes low-fragmentation heap (LFH) block selection, which makes heap corruption harder to turn into a reliable primitive. It's quite possible that a double free is more likely to crash than to be cleanly exploitable.
== Minimal allocator test ==
This small test shows the pattern directly. It uses a 64-byte (tcache-eligible) chunk so the point is independent of chunk size:
#include <stdio.h> #include <stdlib.h>
int main(void) { char p = malloc(64); printf("p = %p\n", (void )p); #ifdef BACKTOBACK free(p); free(p); / naive double free: glibc aborts here / #else free(p); / 1st free (in the bug: realloc frees it) / char q = malloc(64); / reuse: pulls p back out, clears tcache key / printf("q = %p %s\n", (void )q, q == p ? "(reused p)" : ""); free(p); / 2nd free of p: NOT flagged, p is live as q / char r = malloc(64); / attacker alloc / printf("r = %p %s\n", (void )r, r == q ? "== q >>> ALIASED (two live owners) <<<" : ""); #endif return 0; }
On stock glibc (gcc:13, glibc 2.36) the interleaved free-use-free is not flagged. All three allocations print the same address: p is freed, reused as q, freed again, then handed out once more as r, so q and r are two live pointers to one chunk:
p = 0x150542a0 q = 0x150542a0 (reused p) r = 0x150542a0 == q >>> ALIASED (two live owners) <<<
Rebuilt with -DBACKTOBACK, the naive free(p); free(p) does abort:
free(): double free detected in tcache 2
So glibc's check exists but is bypassed by the intervening allocation, which is exactly the shape readstatus produces. Under jemalloc the same test also aliases and does not abort.
The attached fix for T8557 (write the reallocated pointer back before any early return) removes it at the source for all callers and allocators.
--- a/src/engine-gpg.c +++ b/src/engine-gpg.c @@ -1494,6 +1494,15 @@ buffer = realloc (buffer, bufsize); if (!buffer) return gpgerrorfromsyserror (); + / Write the reallocated pointer and size back immediately. The + block may have moved, freeing the old one, and several of the + code paths below return early (e.g. on a callback error) + without reaching the update at the end of this function. If we + did not store the new pointer now, gpg->status.buffer would be + left pointing at the already-freed old block and freed a second + time during cleanup. / + gpg->status.buffer = buffer; + gpg->status.bufsize = bufsize; } nread = gpgmeioread (gpg->status.fd[0], @@ -1647,6 +1656,12 @@ buffer = realloc (buffer, bufsize); if (!buffer) return gpgerrorfromsyserror (); + / Write the reallocated pointer and size back immediately; see the + matching comment in readstatus. A preprocess callback below + can return early, so leaving the stale pointer in + gpg->colon.buffer would cause a double free during cleanup. / + gpg->colon.buffer = buffer; + gpg->colon.bufsize = bufsize; } nread = gpgmeioread (gpg->colon.fd[0], buffer+readpos, bufsize-readpos);
Affected Software
Remediation
Recommended actions to resolve this vulnerability, in priority order.
- Upgrade
Upgrade to a fixed release to a version that resolves this vulnerability.
Patch T8557
Event History
Frequently Asked Questions
Which deployments match the reported exploitation environment?
The report describes GitLab's Linux package, where the web process runs on a Ruby interpreter built with jemalloc 5.3.0. In that environment, allocations made inside libgpgme also use jemalloc.
What allocator behavior makes the reported double free exploitable?
The reported 6144-byte status buffer falls into a jemalloc small size class and is retained in the per-thread tcache, which reuses chunks last-in-first-out. The report states that jemalloc's production free path does not perform a double-free check, allowing the second free to return the chunk to the cache silently.
What attacker-controlled input is involved in the demonstrated chain?
The chain begins with a crafted signature that causes read_status to take an early-return-after-realloc path. The report says the resulting freed status buffer can be reused as the backing storage of a live Ruby Array, enabling disclosure of key bytes in an HTTP response.