Skip to content
Memory corruption Write-up Advanced

Ode to the use-after-free: one vulnerable function, a thousand possibilities

The Array.sort() implementation in WebKit's JavaScript engine cached a pointer to the array's backing storage and other state, then invoked a JavaScript comparison callback that could reallocate, shift or expand the array, leaving the cached values stale. An attacker could use this to cause out-of-bounds writes, information leaks and use-after-free writes, and the post demonstrates reliable exploits on Ubuntu 12.04's Konqueror.

Chris Evans Follow

Published on scarybeastsecurity.blogspot.com

This post explores an old but wonderful vulnerability that enables us to really showcase the (oft underestimated) power of the use-after-free vulnerability class.

Chris Evans, on scarybeastsecurity.blogspot.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the sort code cached the array storage pointer, node buffer length and map iterator across calls into attacker-controlled JavaScript, so any reallocation or mutation during the callback left those cached values dangling.

Impact

An attacker could leak pointers and memory contents and corrupt a chosen same-size allocation, and by planting a controlled pointer in the sparse map field could cause an arbitrary free, though the author notes threading and timers were not fully analysed for 100% reliability.

What the attacker needed

Victim visits a malicious page; browser uses vulnerable QtWebKit 2.2.1; JavaScript enabled

How it was found

  1. Create a dense array of two elements and pass a custom comparison function to sort.
  2. Inside the comparison callback, expand the array so its storage must be reallocated.
  3. Allocate an ArrayBuffer of the same size as the freed storage so it lands in the freed slot.
  4. Read back the buffer to observe the sorted element count and object pointers written into it.

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 write-up has not been written yet. The Original tab links to the source.

When you sort a list in JavaScript you can supply your own comparison function, and the browser calls it repeatedly while sorting. The browser had saved a note of where the list's data lives, but the comparison function could change the list so that note became wrong. The browser then read or wrote at the old location, which can leak memory contents or corrupt other data.

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.

More by Chris Evans

View all

11 more disclosed reports, best first.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.