Heap-use-after-free from adopting a pending fullscreen element during popover `beforetoggle`
Firefox's fullscreen handling validated the requested element's ownership before firing popover closing events, but did not re-check ownership afterwards. Page script could adopt the pending fullscreen element into an iframe in another window during that event, leaving the helper document's retained display list holding pointers to frames that were later freed, causing a heap-use-after-free and a potentially controllable crash.
Originally disclosed on Mozilla
- Fixed in 13 days
- Paid in 3 months
- Disclosed after 13 days
Created attachment 9622371 repro-two-normal-clicks.html
Summary
Document::ApplyFullscreen validates that the requested element belongs to the helper window's document, then synchronously hides popovers with author events enabled. During the resulting closing beforetoggle event, page script adopts the pending fullscreen element into an iframe in the opener window. ApplyFullscreen does not repeat its ownership and composed-document checks after dispatching that event, so it continues by placing the now-foreign element in the helper document's top layer. Painting the helper then creates retained display items containing raw nsIFrame* pointers owned by the opener iframe's PresShell. When page script removes that iframe, its frame arena is freed while the helper's retained display list still references those frames, and the next partial retained-list update dereferences a freed frame in AnyContentAncestorModified.
Reproduction Steps for UAF
tested firefox commit: 202150dcdade5798ca858b843b51b20112b4d061
Build firefox with:
mk_add_options MOZ_OBJDIR=/home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser
ac_add_options --enable-project=browser
ac_add_options --enable-address-sanitizer
ac_add_options --disable-install-strip
ac_add_options --disable-jemalloc
ac_add_options --disable-crashreporter
ac_add_options --disable-profiling
ac_add_options --enable-optimize="-O1"
ac_add_options --enable-debug-symbols=-gline-tables-only
mk_add_options MOZ_PARALLEL_BUILD=24
Serve the attached testcase:
python3 -m http.server 8000 --bind 127.0.0.1
Execute:
fullscreen_profile_dir="$(mktemp -d /tmp/fullscreen-uaf-profile.XXXXXX)"
/home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/dist/bin/firefox \
-no-remote -profile "$fullscreen_profile_dir" \
http://127.0.0.1:8000/repro-two-normal-clicks.html > asan.log 2>&1
Click the two visible controls:
- Click 1. Open demo in the source page. Firefox opens and focuses the helper window.
- In that foreground helper, click 2. Start fullscreen.
Result:
=================================================================
==2392528==ERROR: AddressSanitizer: heap-use-after-free on address 0x7bb7f09464b6 at pc 0x7967e22f87d1 bp 0x7ffe68226db0 sp 0x7ffe68226da8
READ of size 2 at 0x7bb7f09464b6 thread T0 (Isolated Web Co)
#0 0x7967e22f87d0 in IsFrameModified /home/ubuntu-slave/firefox-bug-bounty/firefox/layout/generic/nsIFrame.h:5220:41
#1 0x7967e22f87d0 in AnyContentAncestorModified /home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/layout/painting/./../../../firefox/layout/painting/RetainedDisplayListBuilder.cpp:135:12
#2 0x7967e22f87d0 in mozilla::RetainedDisplayListBuilder::PreProcessDisplayList(mozilla::RetainedDisplayList*, nsIFrame*, mozilla::PartialUpdateResult&, nsIFrame*, mozilla::ActiveScrolledRoot const*, nsIFrame*, unsigned int, unsigned int, bool) /home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/layout/painting/./../../../firefox/layout/painting/RetainedDisplayListBuilder.cpp:216:9
#3 0x7967e22f7a9c in mozilla::RetainedDisplayListBuilder::PreProcessDisplayList(mozilla::RetainedDisplayList*, nsIFrame*, mozilla::PartialUpdateResult&, nsIFrame*, mozilla::ActiveScrolledRoot const*, nsIFrame*, unsigned int, unsigned int, bool) /home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/layout/painting/./../../../firefox/layout/painting/RetainedDisplayListBuilder.cpp:274:12
...
#43 0x7967d6825334 in RunHandler /home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/ipc/chromium/./../../../firefox/ipc/chromium/src/base/message_loop.cc:364:3
#44 0x7967d6825334 in MessageLoop::Run() /home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/ipc/chromium/./../../../firefox/ipc/chromium/src/base/message_loop.cc:346:3
#45 0x7967e38657dc in XRE_InitChildProcess(int, char**, XREChildData const*) /home/ubuntu-slave/firefox-bug-bounty/firefox/toolkit/xre/nsEmbedFunctions.cpp:590:34
#46 0x61c146644bee in main /home/ubuntu-slave/firefox-bug-bounty/firefox/browser/app/nsBrowserApp.cpp:469:22
#47 0x7d67f142a600 in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:59:16
#48 0x7d67f142a717 in __libc_start_main csu/../csu/libc-start.c:360:3
#49 0x61c14655cc98 in _start (/home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/dist/bin/firefox+0xb8c98) (BuildId: bb635c8ca8104948e8e0a78847cb46b9)
0x7bb7f09464b6 is located 2998 bytes inside of 8192-byte region [0x7bb7f0945900,0x7bb7f0947900)
freed by thread T0 (Isolated Web Co) here:
/home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/dist/bin/llvm-symbolizer: error: '[anon:js-executable-memory]': No such file or directory
#0 0x61c1465ffb06 in free /builds/worker/fetches/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:51:3
#1 0x7967e1c006a0 in Clear /home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/dist/include/mozilla/ArenaAllocator.h:88:7
#2 0x7967e1c006a0 in ~ArenaAllocator /home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/dist/include/mozilla/ArenaAllocator.h:55:23
#3 0x7967e1c006a0 in nsPresArena<8192ul, mozilla::ArenaObjectID, 140ul>::~nsPresArena() /home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/layout/base/./../../../firefox/layout/base/nsPresArena.cpp:32:1
#4 0x7967e1a5e689 in mozilla::PresShell::~PresShell() /home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/layout/base/./../../../firefox/layout/base/PresShell.cpp:788:1
#5 0x7967e1a5e3a0 in mozilla::PresShell::Release() /home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/layout/base/./../../../firefox/layout/base/PresShell.cpp:765:1
#6 0x7967e1b544ee in Release /home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/dist/include/mozilla/RefPtr.h:48:40
...
#34 0x14b6d0391467 ([anon:js-executable-memory]+0x467)
#35 0x7967e51f03ff in EnterJit(JSContext*, js::RunState&, unsigned char*) /home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/js/src/jit/./../../../../firefox/js/src/jit/Jit.cpp:134:5
#36 0x7967e543d358 in js::RunScript(JSContext*, js::RunState&) /home/ubuntu-slave/firefox-bug-bounty/firefox/js/src/vm/Interpreter.cpp:476:32
#37 0x7967e57d8dca in js::ResumeGenerator(JSContext*, JS::Handle<js::AbstractGeneratorObject*>, JS::Handle<JS::Value>, js::GeneratorResumeKind, JS::MutableHandle<JS::Value>) /home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/js/src/vm/./../../../../firefox/js/src/vm/GeneratorObject.cpp:314:10
previously allocated by thread T0 (Isolated Web Co) here:
#0 0x61c1465ffda4 in malloc /builds/worker/fetches/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:67:3
#1 0x7967e1c00da0 in AllocateChunk /home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/dist/include/mozilla/ArenaAllocator.h:168:15
#2 0x7967e1c00da0 in InternalAllocate /home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/dist/include/mozilla/ArenaAllocator.h:202:25
#3 0x7967e1c00da0 in Allocate /home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/dist/include/mozilla/ArenaAllocator.h:64:12
#4 0x7967e1c00da0 in mozilla::ArenaAllocator<8192ul, 8ul>::Allocate(unsigned long) /home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/dist/include/mozilla/ArenaAllocator.h:68:15
#5 0x7967e1ce28b9 in AllocateByObjectID /home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/dist/include/mozilla/PresShell.h:293:32
#6 0x7967e1ce28b9 in AllocateFrame /home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/dist/include/mozilla/PresShell.h:285:12
...
#36 0x7967d9c0811b in nsFrameLoader::ReallyStartLoadingInternal() /home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/dom/base/./../../../firefox/dom/base/nsFrameLoader.cpp:766:18
#37 0x7967d9c0736d in nsFrameLoader::ReallyStartLoading() /home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/dom/base/./../../../firefox/dom/base/nsFrameLoader.cpp:626:17
#38 0x7967d98c057b in mozilla::dom::Document::MaybeInitializeFinalizeFrameLoaders() /home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/dom/base/./../../../firefox/dom/base/Document.cpp:10290:13
SUMMARY: AddressSanitizer: heap-use-after-free /home/ubuntu-slave/firefox-bug-bounty/firefox/layout/generic/nsIFrame.h:5220:41 in IsFrameModified
Shadow bytes around the buggy address:
0x7bb7f0946200: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
0x7bb7f0946280: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
0x7bb7f0946300: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
0x7bb7f0946380: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
0x7bb7f0946400: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
=>0x7bb7f0946480: fd fd fd fd fd fd[fd]fd fd fd fd fd fd fd fd fd
0x7bb7f0946500: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
0x7bb7f0946580: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
0x7bb7f0946600: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
0x7bb7f0946680: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
0x7bb7f0946700: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
Heap left redzone: fa
Freed heap region: fd
Stack left redzone: f1
Stack mid redzone: f2
Stack right redzone: f3
Stack after return: f5
Stack use after scope: f8
Global redzone: f9
Global init order: f6
Poisoned by user: f7
Container overflow: fc
Array cookie: ac
Intra object redzone: bb
ASan internal: fe
Left alloca redzone: ca
Right alloca redzone: cb
==2392528==ABORTING
[Parent 2392269, IPC I/O Parent] WARNING: process 2392528 exited with status 1: file firefox/ipc/chromium/src/chrome/common/process_watcher_posix_sigchld.cc:155
Reproduction Steps for potential control-flow-hijack
I ran the ASan Firefox build with both global and thread-local quarantine disabled, allowing the freed 8 KiB presentation-arena block to be immediately reused by a live UTF-16 allocation. After the two visible clicks, repro-control-flow-asan.html adopts the fullscreen element into the iframe, removes that iframe, and allocates one exact-size replacement buffer. The payload places 0x0000414141414141 over the stale frame’s vtable pointer and 0x100 over mState, forcing the GetPrevInFlow virtual-call path. Firefox then attempts to read the function pointer at vptr + 0x1c0, producing controlled fault address 0x414141414301.
Serve the attached testcase:
python3 -m http.server 8000 --bind 127.0.0.1
Execute and click twice:
export ASAN_OPTIONS='quarantine_size_mb=0:thread_local_quarantine_size_kb=0:halt_on_error=1:abort_on_error=1:symbolize=1'
fullscreen_profile_dir="$(mktemp -d /tmp/fullscreen-uaf-profile.XXXXXX)"
/home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/dist/bin/firefox \
-no-remote \
-profile "$fullscreen_profile_dir" \
'http://127.0.0.1:8000/repro-control-flow-asan.html?spray=1&marker=0' > asan-control-flow-vcall.log 2>&1
Result:
AddressSanitizer:DEADLYSIGNAL
=================================================================
==2471444==ERROR: AddressSanitizer: SEGV on unknown address 0x414141414301 (pc 0x7abda2ba2636 bp 0x7ffc88fb1b70 sp 0x7ffc88fb1b60 T0)
==2471444==The signal is caused by a READ memory access.
#0 0x7abda2ba2636 in GetParentOrPlaceholderFor /home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/layout/base/./../../../firefox/layout/base/nsLayoutUtils.cpp:4057:16
#1 0x7abda2ba2636 in GetParentOrPlaceholderForCrossDoc /home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/layout/base/./../../../firefox/layout/base/nsLayoutUtils.cpp:4065:17
#2 0x7abda2ba2636 in nsLayoutUtils::GetDisplayListParent(nsIFrame*) /home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/layout/base/./../../../firefox/layout/base/nsLayoutUtils.cpp:4076:10
#3 0x7abda32f7329 in AnyContentAncestorModified /home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/layout/painting/./../../../firefox/layout/painting/RetainedDisplayListBuilder.cpp:143:9
#4 0x7abda32f7329 in mozilla::RetainedDisplayListBuilder::PreProcessDisplayList(mozilla::RetainedDisplayList*, nsIFrame*, mozilla::PartialUpdateResult&, nsIFrame*, mozilla::ActiveScrolledRoot const*, nsIFrame*, unsigned int, unsigned int, bool) /home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/layout/painting/./../../../firefox/layout/painting/RetainedDisplayListBuilder.cpp:216:9
#5 0x7abda32f7a9c in mozilla::RetainedDisplayListBuilder::PreProcessDisplayList(mozilla::RetainedDisplayList*, nsIFrame*, mozilla::PartialUpdateResult&, nsIFrame*, mozilla::ActiveScrolledRoot const*, nsIFrame*, unsigned int, unsigned int, bool) /home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/layout/painting/./../../../firefox/layout/painting/RetainedDisplayListBuilder.cpp:274:12
#6 0x7abda3300ee1 in mozilla::RetainedDisplayListBuilder::AttemptPartialUpdate(unsigned int) /home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/layout/painting/./../../../firefox/layout/painting/RetainedDisplayListBuilder.cpp:1596:10
#7 0x7abda2b9a888 in nsLayoutUtils::PaintFrame(gfxContext*, nsIFrame*, nsRegion const&, unsigned int, mozilla::nsDisplayListBuilderMode, nsLayoutUtils::PaintFrameFlags) /home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/layout/base/./../../../firefox/layout/base/nsLayoutUtils.cpp:3241:38
#8 0x7abda2a920ca in mozilla::PresShell::PaintInternal(nsIFrame*, mozilla::WindowRenderer*, mozilla::PaintInternalFlags) /home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/layout/base/./../../../firefox/layout/base/PresShell.cpp:6790:5
...
#46 0x7abd97825334 in MessageLoop::Run() /home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/ipc/chromium/./../../../firefox/ipc/chromium/src/base/message_loop.cc:346:3
#47 0x7abda48657dc in XRE_InitChildProcess(int, char**, XREChildData const*) /home/ubuntu-slave/firefox-bug-bounty/firefox/toolkit/xre/nsEmbedFunctions.cpp:590:34
#48 0x57a1e216dbee in main /home/ubuntu-slave/firefox-bug-bounty/firefox/browser/app/nsBrowserApp.cpp:469:22
#49 0x7ebdb242a600 in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:59:16
#50 0x7ebdb242a717 in __libc_start_main csu/../csu/libc-start.c:360:3
#51 0x57a1e2085c98 in _start (/home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/dist/bin/firefox+0xb8c98) (BuildId: bb635c8ca8104948e8e0a78847cb46b9)
==2471444==Register values:
rax = 0x0000414141414301 rbx = 0x00007d0db183d448 rcx = 0x0000082828282860 rdx = 0x0000000000000000
rdi = 0x00007d0db183d448 rsi = 0x0000000000000000 rbp = 0x00007ffc88fb1b70 rsp = 0x00007ffc88fb1b60
r8 = 0x00007ffc88fb1c18 r9 = 0x00007d8db1845190 r10 = 0x0000000000000000 r11 = 0x0000000000000000
r12 = 0x0000000000000000 r13 = 0x00007d8db18461f0 r14 = 0x00000fb1b6308c40 r15 = 0x00007d8db1845188
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV /home/ubuntu-slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/layout/base/./../../../firefox/layout/base/nsLayoutUtils.cpp:4057:16 in GetParentOrPlaceholderFor
==2471444==ABORTING
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
Likely a missing re-validation of element ownership and composed document after synchronously dispatching the popover closing beforetoggle event, so a now-foreign element is placed in the helper's top layer and its frames are retained across documents.
Impact
An attacker could trigger a heap-use-after-free in the content process, and with heap grooming the freed arena block could be reused to redirect a virtual call, producing a controlled crash address; the report does not demonstrate full code execution.
What the attacker needed
Victim visits attacker page; two user clicks; popover closing event runs script
How it was found
- Open the demo page so Firefox opens and focuses the helper window.
- In the foreground helper window, start fullscreen.
- During the closing beforetoggle event, script adopts the pending fullscreen element into an iframe in the opener window.
- Script removes that iframe, freeing its frame arena while the helper's retained display list still references those frames.
- The next partial retained-list update dereferences the freed frame.
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 page asks for fullscreen, the browser checks the element belongs to that page, then briefly runs page code (to close popovers). That code can move the element into a different window's iframe. The browser then keeps drawing instructions that point at the other window's internal layout objects; when that iframe is removed, those objects are freed but still referenced, so the browser reads freed memory and crashes.
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.