Modern applications make heavy use of “HTML to PDF” converters to create exports (e.g. invoices, reports or tickets). Some of them are still powered by wkhtmltopdf, which embeds a 2012-era WebKit to render the HTML. If an attacker can influence the HTML that such a converter service does render, they may be able to run arbitrary JavaScript inside this ancient engine.
This article is the story of taking a use-after-free from 2012 (CVE-2012-3748) all the way to an arbitrary-code-execution exploit against a current build of wkhtmltopdf.
TL;DR
wkhtmltopdf renders HTML using a QtWebKit (including the JavaScriptCore engine) version from around 2012 (WebKit build 534.34). This version of WebKit is prone to several publicly known vulnerabilities, including a use-after-free in Array.prototype.sort. Wherever a service lets an attacker influence the HTML wkhtmltopdf converts, that decade-old bug can turn into remote code execution on the server.
Building on the publicly available exploits for this bug, we reuse the use-after-free → object-aliasing → arbitrary read/write ladder. As wkhtmltopdf is built with additional hardening measures, we wrap a Uint8Array around a fake C++ vtable, which turns an ordinary array read into a ROP chain and, from there, arbitrary command execution.
Furthermore, we challenged the ability of AI: How fast can such exploits be ported to new targets?
How we got here
During a recent Attack Surface Analysis assessment for a customer, we found an internet-facing service that used wkhtmltopdf to turn HTML into PDF reports. The HTML code was partially controlled by the user, so we could influence what the converter rendered.
wkhtmltopdf does execute JavaScript code within HTML by default. In a prior internal research project, we had already identified that wkhtmltopdf embeds an ancient browser engine prone to several publicly known vulnerabilities, some of which can be turned into arbitrary code execution. The problem: Our internally developed exploit was targeting wkhtmltopdf on Linux, whereas the internet-facing service runs wkhtmltopdf on Windows.
In this blog post, we first walk through the Linux exploit in detail, and then answer a more practical question: Is AI capable of porting such an exploit to a new target within a short, time-boxed project?
Background: wkhtmltopdf, Qt, and a 2012-era JavaScriptCore
wkhtmltopdf is a command-line HTML-to-PDF renderer. Under the hood, it embeds QtWebKit, which is WebKit wrapped inside Qt. Therefore, it also contains JavaScriptCore (JSC), Apple’s JavaScript engine. JavaScript support is enabled by default within wkhtmltopdf. Converting an HTML document, therefore, means parsing and executing that document’s JavaScript in JSC. When the HTML comes from an attacker, it runs in the context of the process doing the conversion. This is especially critical if one of the many available wkhtmltopdf wrapper libraries (e.g. phpwkhtmltopdf) is used within a web application/service, as the JavaScript code gets executed on the server side.
The wkhtmltopdf Qt fork is pinned (via a qt submodule) at revision cb88757e7c9383a0504689c8373c101a44214132. At that revision, the source code (src/3rdparty/webkit/Source/JavaScriptCore/runtime/JSArray.cpp) still contains the vulnerable code for CVE-2012-3748.
wkhtmltopdf is no longer actively developed. Its own status page points out that the Qt 4 it builds on has been unsupported since 2015 and that the bundled WebKit has not been updated since 2012. It even warns: “Do not use wkhtmltopdf with any untrusted HTML – be sure to sanitize any user-supplied HTML/JS, otherwise it can lead to complete takeover of the server it is running on!”
The vulnerability: CVE-2012-3748
CVE-2012-3748 is a use-after-free in Array.prototype.sort, which can be triggered by abusing the user-supplied callback comparison function (JavaScriptCore/runtime/JSArray.cpp). The essence of the bug is that sort takes a snapshot of the array’s backing store before it starts calling back into attacker-controlled JavaScript, and then keeps using that snapshot even though the callback is free to reallocate, move or free the store.
Here is the relevant part of the QtWebKit variant that wkhtmltopdf ships (trimmed for clarity):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void JSArray::sort(ExecState* exec, JSValue compareFunction, CallType callType, const CallData& callData)
{
// ...
ArrayStorage* storage = m_storage; // (1) the backing store is cached up front
// ...
for (; numDefined < usedVectorLength; ++numDefined) {
JSValue v = storage->m_vector[numDefined].get();
// ...
tree.abstractor().m_nodes[numDefined].value = v;
tree.insert(numDefined); // (2) this fires the attacker's compare callback
}
// ...
for (unsigned i = 0; i < numDefined; ++i) {
storage->m_vector[i].set(globalData, this, tree.abstractor().m_nodes[*iter].value);
++iter; // (3) write-back through the *stale* cached pointer
}
}
Line (1) caches m_storage into a local storage. Building the AVL tree at (2) invokes the comparator, i.e. our JavaScript. If that JavaScript makes the array grow past its capacity, JSC reallocates the backing store to a new chunk and frees the old one. The pointer storage still points at the old, now freed, memory. When sort later writes the sorted values back at (3), it writes through a dangling pointer into freed (and possibly by then reclaimed) memory.
As mentioned by Chris Evans, the same bug offers a few other primitives, including an arbitrary free:
1
2
3
4
if (SparseArrayValueMap* map = storage->m_sparseValueMap) {
// ...
delete map; // frees storage->m_sparseValueMap
}
Because storage is stale and the reclaimed chunk is attacker-controlled, we can plant any pointer in m_sparseValueMap, and delete map will fastFree() an address of our choosing.
Ways to exploit it
CVE-2012-3748 is not new, and there are two well-known public exploitation strategies.
Strategy I (Pwn2Own 2012) The original exploit used an Array.shift() trick mid-sort to land the write 8 bytes out of bounds, type-confused a Uint32Array via a fake GetterSetter, then leaked and overwrote the RWX JIT-compiled machine code of a JS function to get code execution. That fails for wkhtmltopdf, because it builds JSC with the JIT switched off. No JIT means no RWX pages and no attacker-writable code to overwrite.
Strategy II (“Ode to use-after-free”) Chris Evans’ write-up turns the same bug into a full exploit, built around one trick: object aliasing. Free a chunk mid-sort and immediately reclaim it with an object of a different type, and you are left with a single region of memory that is two live objects at once, which lets us write through one view and observe the change through the other. Chris Evans uses that to leak a code pointer (defeating ASLR) and to forge a fake Uint8Array that hands him arbitrary read/write. This can be achieved deterministically as QtWebKit does use its own WTF::fastMalloc allocator. This is a size-classed, thread-caching allocator (a TCMalloc derivative) that reuses chunks in LIFO (Last In, First Out) order, which means that if a chunk is freed and memory with the same size class is immediately allocated afterwards, the same memory chunk is allocated. In the last step, one achieves arbitrary code execution by reading the address of strtol(), calculating the base address of libc, computing the system() address, overwriting strtol@GOT with it, and calling new Date('<cmd>'). As the constructor of Date does internally call strtol, now redirected to system, this yields arbitrary code execution. The final step needs a writable GOT, but the wkhtmltopdf build is compiled with Full RELRO, which makes the GOT read-only after startup.
Strategy III We keep Chris Evans’ aliasing-based read/write ladder and swap out only the ending. We will use the arbitrary read/write to overwrite a function pointer. The Uint8Array we forge has its own vtable pointer sitting on the heap, which we can control. Repoint it, and an array read becomes a call into our ROP chain.
Step 1: use-after-free to self-pointer leak
To abuse the arbitrary-free primitive to obtain an aliased object, we first need the heap address of our ArrayStorage. We can achieve this by letting the comparison function behave differently on the first and last comparison. On the first comparison, our exploit does arr.splice(0, 7), which slides the live ArrayStorage header forward by 7 slots (56 bytes). This is done to slide the m_allocBase (base address of our ArrayStorage) from the header section into the data section. The sort function then reads every item of our array, including the m_allocBase. On the last comparison it grows the array (forcing a realloc that frees the original chunk) and then immediately reclaims that hole with a fresh ArrayBuffer. When sort’s write-back runs, it writes through the stale pointer straight into our ArrayBuffer, including the values from the shifted header (e.g. m_allocBase).
Our ArrayBuffer now physically overlaps with the freed ArrayStorage. This allows us to read the raw bytes of the m_allocBase self-pointer, leaking the chunk’s own heap address.
Step through the frames below to watch the pointers diverge, the chunk get freed, and the ArrayBuffer reclaim it.
Figure 1: The sort use-after-free and self-pointer leak, frame by frame (use the arrows to step through). Cache the pointer → splice to diverge it → grow to free the chunk → reclaim with an ArrayBuffer → write-back through the stale pointer, leaking m_allocBase.
Step 2: Aliasing: one chunk, two views
With the self-pointer leak in hand, we can abuse the arbitrary-free primitive to build a stable aliasing primitive: One memory chunk that we can see both, as raw bytes (through a Uint8Array) and as a live engine object of our choosing. Composing the primitives gets us there:
- Free-and-reclaim a chunk as an
ArrayBuffer(use-after-free #1) and leak its address fromm_allocBase - Clear the buffer (otherwise a later
HashMapdestructor trips over stale pointers) - Use the arbitrary-free path to free that exact address again (use-after-free #2), via a forged
m_sparseValueMap - Immediately allocate our chosen object into the now-free hole, so the
Uint8Arrayand that object share one chunk byte-for-byte
Step through the frames below to watch one chunk get reclaimed as an ArrayBuffer, freed a second time through the forged m_sparseValueMap, and finally re-taken by our chosen object — leaving both views aliased over the same bytes.
Figure 2: The aliasing workbench — reclaim a chunk as an ArrayBuffer, arbitrary-free it via a forged m_sparseValueMap, then let a chosen object (WebCore::ScriptRunner) reclaim the hole so the Uint8Array’s bytes are that object’s internals.
Step 3: Defeating ASLR: leaking the image base
wkhtmltopdf is compiled as a PIE and runs under ASLR, so before anything else we need the binary’s load base at runtime. As Chris Evans mentions, one of the child allocations of a Document is a WebCore::ScriptRunner object of exactly 112 bytes, allocated with fastMalloc. So we can alias a 112-byte chunk with a freshly created document, then read a code pointer straight out of the ScriptRunner object.
ScriptRunner::timerFired is a function pointer into the wkhtmltopdf image. Reading it and subtracting its known static offset (calibrated once for each build) gives the load base. From there, every vtable, gadget and GOT entry is base + fixed offset.
Figure 3: Turning one leaked code pointer into the image base — the Uint8Array overlays a WebCore::ScriptRunner, and timerFired (read at offset 96) minus its fixed offset is the load base.
Step 4: Turning a byte-write into a fake object
Now we need the fakeobj primitive: the ability to make the engine hand us a JavaScript object of our own construction. We alias a 2088-byte chunk with a 256-slot array, so the Uint8Array’s bytes are that array’s element storage. Then we byte-write a fake object structure into the array’s own element slots, and set element[0] to point at it. By type-confusing the engine into decoding those bytes as a cell pointer (top 16 bits 0x0000) rather than a number, reading element[0] hands us our forged object.
Figure 4: The fakeobj primitive — the aliased Uint8Array and a 256-element array share one chunk, so byte-writes into elem[0..8] forge a fake cell, and Array[0] reads it back as a genuine object reference.
Step 5: Arbitrary read/write: forging a Uint8Array
The object we forge is a fake Uint8Array, which consists of two chained objects. The outer JS cell (JSUint8Array) is what JavaScript sees. Within the JS cell, we only touch its vtable (+0x00) and m_impl (+0x30), a pointer to the inner implementation object. That inner WebCore Uint8Array is where the view over memory is described: m_baseAddress (+0x10) is the window’s base pointer and m_length (+0x28) its length. With control over those two fields, an indexed access resolves to *(m_baseAddress + i), giving us an arbitrary read/write primitive.
We use our byte-lens (Uint8Array) to first write the fake cell’s vtable and set it to the real JSUint8Array vtable, so the object passes every type check the engine makes. Then we point the field m_impl of our fake cell to our own Uint8Array implementation which we store at offset +0x68 of our ArrayStorage.
In the next step, we set the field m_length of our fake Uint8Array to 0xffffffff, giving us a window that spans over 4 GiB. The field m_baseAddress will be set dynamically during our exploit and allows us to aim our read/write primitive at a given memory address. As an immediate use, we read the addresses of execve and _exit straight out of the GOT, so we require no hard-coded libc offsets.
Step through Figure 5 to see the whole forge: the two struct layouts, the four writes that build the fake cell and impl inside the aliased buffer, and how an element[0] access then resolves cell → impl → target memory as m_baseAddress slides the window.
Figure 5: Forging the Uint8Array for arbitrary read/write — only the highlighted fields (vtable, m_impl, m_baseAddress, m_length) are set; everything else is left as garbage the engine never reads.
Step 6: Hijacking control flow: a vtable hijack, a stack pivot, and ROP
Arbitrary read/write is not yet code execution, and with JIT spray and GOT overwrite both dead, we need another solution. The fake Uint8Array cell’s vtable pointer lives on the heap, in memory we fully control. When JavaScript reads an element of the object, the engine performs a virtual call. In this version, the first vtable slot the engine invokes is at index 22, i.e. offset +0xb0 into the vtable.
So we forge a fake vtable in the aliased buffer and wire up a small vtable hijack that ends in a classic ROP chain. We stage everything – the fake vtable, the ROP chain, the argv/envp arrays and the command strings – inside our 2088-byte buffer. The ROP chain loads rdi/rsi/rdx with the command, argv and envp. Then call the execve address we leaked from the GOT and call _exit(0) for a clean program exit. The vtable hijack itself uses the fake vtable slot at index 22 to point at the mov rdi, rax; call [rax+0x70] gadget. The slot at offset +0x70 in our fake vtable then points at our stack pivot gadget (push rdi; pop rsp; ret) and the fake cell’s own vtable pointer is repointed at the fake vtable.
Reading element 0 of the object then pulls the trigger, and the control-flow ladder unfolds:
- The engine does
mov rax, [rdi]—raxis now our fake vtable (fake_vtable). - It calls
[rax + 0xb0]= slot 22 =mov rdi, rax; call [rax + 0x70]. Sordi = fake_vtable, and control jumps through[fake_vtable + 0x70]. [fake_vtable + 0x70]=push rdi; pop rsp; ret. Nowrsp = fake_vtable= the stack is our ROP chain.retruns into the chain: setrdi/rsi/rdx, callexecve("/bin/sh", ["/bin/sh","-c","touch /tmp/syss"], envp), then_exit(0)for a clean exit.
That is the whole Linux chain: a JavaScript array sort becomes arbitrary command execution in wkhtmltopdf.
Figure 6 walks the staging and the jump, frame by frame:
Figure 6: The vtable hijack, staged and wired — strings, argv/envp and the ROP chain are laid into the buffer, then one read of the fake Uint8Array turns a virtual call (slot 22 → G_MOVRDI_CALL70 → pivot) into the ROP chain ending in execve.
What about Windows?
The same bug carries over to the Windows build of wkhtmltopdf. What changes for the exploit is the MSVC C++ ABI and vtable layout, the Win64 calling convention, imports resolved through the IAT instead of the GOT, and the payload needs to be built around CreateProcessW and ExitProcess.
As our client project was time-boxed, we tried to let an AI agent port the exploit end to end. Starting from the Linux code, the AI agent rebuilt the primitive ladder against wkhtmltopdf.exe with the correct Win64 calling convention, the import resolved through IAT and created a CreateProcessW payload. Creating a first working exploit took the AI agent roughly an hour.
Unfortunately, that first version of the exploit was very unreliable, it landed only ~30% of the time on our local target and never on our remote target. After manual review, the root cause of the unreliability seems to be that the fastMalloc (WebKit’s own allocator) hands a freed chunk straight back from its size-classed, LIFO free-list on Linux. On the Windows build of wkhtmltopdf, fastMalloc is backed with the Windows system heap instead, whose Low-Fragmentation Heap randomizes reclaim on modern Windows.
With that hint, the AI agent added heap grooming that pushed reliability to ~80% on our local target, and also got the exploit working against our remote target. In total, the agent spent about three hours to create the final version of the exploit.
Conclusion and mitigations
A bug from 2012 can still be abused to take over a remote system in 2026. wkhtmltopdf renders attacker-influenced HTML on a JavaScriptCore engine that stopped receiving fixes over a decade ago, and CVE-2012-3748 turns that into a straight-line path to remote code execution. Anywhere untrusted HTML reaches wkhtmltopdf, an attacker can run commands as the converting process.
AI collapses the effort of porting an n-day. Given the working PoC exploit as a reference, an AI agent ported it to an entirely different platform in about three hours for a few dollars. Work that used to take a specialist days now fits into an afternoon. The same leverage is available to attackers, not just researchers. The barrier to turning a public CVE into a working, reliable exploit for your specific build and platform has dropped sharply — so the window between a vulnerability becoming public and a weaponized exploit existing for it keeps shrinking, and n-days that were “technically exploitable, but not worth the effort” become cheap to weaponize.
What to do about it:
- Stop using wkhtmltopdf. It is unmaintained, and its own developers warn against feeding it untrusted HTML. Migrate to a maintained renderer. If you genuinely cannot migrate yet, make sure no attacker-influenced input ever reaches it: Treat all input as untrusted, disable JavaScript if possible, disable local-file and remote-resource access, and sandbox the converter (seccomp, a locked-down container, no outbound network, an unprivileged user, no secrets within reach) so that any code execution stays contained.
- Treat patch, update, and asset management as first-class controls. As weaponizing known bugs gets cheaper and faster, the grace period between a fix landing and a working exploit appearing only shrinks. Keep an accurate inventory of what you actually run, track known vulnerabilities against it, and retire unmaintained software.
























