Use after free in Downloads (CVE-2024-6988)
A use-after-free occurs in DownloadManagerTabHelper::DidCreateDownload because an asynchronous completion handler can run after the helper object has been destroyed, leading to access to freed memory. An attacker could potentially trigger a browser crash or memory corruption by causing a download and then closing the page before the handler executes.
- Fix shipped in 29 days
Steps to reproduce the problem
- apply the patch, (The path of the patch is not important, just for better triggering)
- see the mov.
Problem Description
- similar issue:40057634, the block function here uses completionHandler, so it is asynchronous, and then destroying this before
DidCreateDownloadis run will cause UAF, because in the block function, the member variable of this is used Equivalent tothis->member.
void DownloadManagerTabHelper::SetCurrentDownload(
std::unique_ptr<web::DownloadTask> task) {
// If downloads are persistent, they cannot be lost once completed.
if (!task_ || (task_->GetState() == web::DownloadTask::State::kComplete &&
!WillDownloadTaskBeSavedToDrive())) {
// The task is the first download for this web state.
DidCreateDownload(std::move(task));
return;
}
__block std::unique_ptr<web::DownloadTask> block_task = std::move(task);
[delegate_ downloadManagerTabHelper:this
decidePolicyForDownload:block_task.get()
completionHandler:^(NewDownloadPolicy policy) {
if (policy == kNewDownloadPolicyReplace) {
DidCreateDownload(std::move(block_task)); //<--
}
}];
}
DidCreateDownloaduses this member variables task_, web_state_, delegate_started_
void DownloadManagerTabHelper::DidCreateDownload(
std::unique_ptr<web::DownloadTask> task) {
if (task_) {
task_->RemoveObserver(this); //<-- UAF
task_ = nullptr;//<--
}
task_ = std::move(task);//<--
task_->AddObserver(this);//<--
if (web_state_->IsVisible() && delegate_) {//<--
delegate_started_ = true;//<--
[delegate_ downloadManagerTabHelper:this
didCreateDownload:task_.get()//
webStateIsVisible:true];
}
}
- The life cycle of DownloadManagerTabHelper is related to
web::WebStateUserData, which is equivalent toSupportsUserData, which means thatDownloadManagerTabHelperwill be destroyed after the web page is closed.
class DownloadManagerTabHelper
: public web::WebStateUserData<DownloadManagerTabHelper>,
public web::WebStateObserver,
public web::DownloadTaskObserver {
public:
DownloadManagerTabHelper(const DownloadManagerTabHelper&) = delete;
DownloadManagerTabHelper& operator=(const DownloadManagerTabHelper&) = delete;
[0]. https://source.chromium.org/chromium/chromium/src/+/main:ios/chrome/browser/download/model/download_manager_tab_helper.mm;l=52;bpv=0;bpt=1 [1]. https://source.chromium.org/chromium/chromium/src/+/main:ios/chrome/browser/download/model/download_manager_tab_helper.mm;l=157;drc=90cac1911508d3d682a67c97aa62483eb712f69a;bpv=0;bpt=1 [2]. https://source.chromium.org/chromium/chromium/src/+/main:ios/chrome/browser/download/model/download_manager_tab_helper.h;l=27;drc=90cac1911508d3d682a67c97aa62483eb712f69a;bpv=0;bpt=1
bitset: https://source.chromium.org/chromium/chromium/src/+/d3fe4f47ab33d6ca1e24712492e25f2f0e997695
fix suggestion: don't use member variable.
Summary
heap-use-after-free on DownloadManagerTabHelper::DidCreateDownload
Custom Questions
Type of crash:
browser
Crash state:
see uaf4-asan.log
Additional Data
Category: Security
Chrome Channel: Not sure
Regression: N/A
Use after free in Downloads (CVE-2024-6988)
Context
This is a Chrome VRP report against Google Chrome, in the Downloads feature. The affected code is the iOS download manager tab helper, DownloadManagerTabHelper, which lives in ios/chrome/browser/download/model/download_manager_tab_helper.mm. The class is attached to a web state through web::WebStateUserData, the same mechanism as SupportsUserData, so its lifetime is tied to the page: when the web page is closed, the helper is destroyed.
The helper tracks the current download task in a member called task_, observes it as a web::DownloadTaskObserver, and notifies its delegate when a download is created. The bug is a heap use after free in DownloadManagerTabHelper::DidCreateDownload, rated high severity, with an $11,000 bounty, disclosed on 23 July 2024. The researcher is lime (@limeSec_) from the TIANGONG Team of Legendsec at QI-ANXIN Group.
Setup
The report gives few environmental details. The reproduction requires a patch to be applied first; the researcher notes that the path of the patch is not important and that it is only there for better triggering. After that, the behaviour is shown in a video (see the mov). The crash type is recorded as browser, and the crash state is in a file named uaf4-asan.log. The Chrome channel is listed as "Not sure" and regression as "N/A".
Finding it
The researcher points to a similar earlier issue, 40057634, and explains the pattern: the block function in SetCurrentDownload uses a completionHandler, so it is asynchronous. If the helper is destroyed before DidCreateDownload runs, the block still executes and touches the helper's member variables, which is equivalent to this->member. That is the use after free.
The relevant code in SetCurrentDownload is:
void DownloadManagerTabHelper::SetCurrentDownload(
std::unique_ptr<web::DownloadTask> task) {
// If downloads are persistent, they cannot be lost once completed.
if (!task_ || (task_->GetState() == web::DownloadTask::State::kComplete &&
!WillDownloadTaskBeSavedToDrive())) {
// The task is the first download for this web state.
DidCreateDownload(std::move(task));
return;
}
__block std::unique_ptr<web::DownloadTask> block_task = std::move(task);
[delegate_ downloadManagerTabHelper:this
decidePolicyForDownload:block_task.get()
completionHandler:^(NewDownloadPolicy policy) {
if (policy == kNewDownloadPolicyReplace) {
DidCreateDownload(std::move(block_task)); //<--
}
}];
}
When there is no current task, or the current task is complete and will not be saved to Drive, DidCreateDownload is called synchronously and returns. Otherwise the task is moved into a __block variable and the delegate is asked to decide the policy for the download, with a completion handler that calls DidCreateDownload when the policy is kNewDownloadPolicyReplace. Because that handler is asynchronous, the call into DidCreateDownload can happen after the helper has gone away.
Exploitation
Step 1: apply the patch
The first reproduction step is to apply the patch. The report does not include the patch itself and says the path of the patch is not important; it exists only to make triggering more reliable.
Step 2: watch the video
The second step is to watch the recording (see the mov), which demonstrates the trigger. The report does not describe the contents of the video beyond that.
The crash path
The researcher walks through what happens once the asynchronous handler fires after destruction. DidCreateDownload uses the member variables task_, web_state_ and delegate_started_:
void DownloadManagerTabHelper::DidCreateDownload(
std::unique_ptr<web::DownloadTask> task) {
if (task_) {
task_->RemoveObserver(this); //<-- UAF
task_ = nullptr;//<--
}
task_ = std::move(task);//<--
task_->AddObserver(this);//<--
if (web_state_->IsVisible() && delegate_) {//<--
delegate_started_ = true;//<--
[delegate_ downloadManagerTabHelper:this
didCreateDownload:task_.get()//
webStateIsVisible:true];
}
}
If task_ is set, the code calls task_->RemoveObserver(this), and the following lines touch task_, web_state_ or delegate_started_ on the freed object. The report marks those lines as the UAF. The report marks those lines as the UAF.
Why the object can be gone
The lifetime of DownloadManagerTabHelper is tied to web::WebStateUserData, which the researcher equates with SupportsUserData: the helper is destroyed after the web page is closed. The class declaration shows the inheritance and the deleted copy operations:
class DownloadManagerTabHelper
: public web::WebStateUserData<DownloadManagerTabHelper>,
public web::WebStateObserver,
public web::DownloadTaskObserver {
public:
DownloadManagerTabHelper(const DownloadManagerTabHelper&) = delete;
DownloadManagerTabHelper& operator=(const DownloadManagerTabHelper&) = delete;
So the sequence is: a download is started while a task already exists, the policy decision is deferred to the asynchronous completion handler, the helper is destroyed before the handler runs, and then the handler calls DidCreateDownload on the freed helper.
Root cause
The block passed as completionHandler captures this and calls DidCreateDownload, which dereferences member variables. The block captures this and calls DidCreateDownload, which dereferences member variables. The report's fix suggestion is direct: don't use member variables in that path.
Impact
The summary states the outcome plainly: heap-use-after-free on DownloadManagerTabHelper::DidCreateDownload. The crash type is recorded as browser. The report does not describe a specific attacker scenario, so the practical impact beyond the memory-safety violation is not spelled out.
Fix
The report only offers the suggestion not to use member variables in the asynchronous path.
Variants to test
- Look for other
completionHandlerblocks in the same codebase that capturethisand call methods touching member state. A positive result is a handler that can run after the owning object is destroyed, for example when the page or tab is closed while the decision is pending. - Check other
WebStateUserDatasubclasses that register asynchronous delegate callbacks. A positive result is a callback path with no lifetime guard, such as a weak pointer or a cancellation on destruction. - Re-examine the synchronous branch of
SetCurrentDownloadagainst the asynchronous one. A positive result is a state where the synchronous path is safe but the deferred path reaches the same member accesses after teardown. - Test the policy outcomes other than
kNewDownloadPolicyReplace. A positive result is any other policy value that still leads to a deferred call into the helper after destruction.
What happened, in plain words
Google Chrome has a feature called Downloads. On the iPhone and iPad version of Chrome, a small piece of code called DownloadManagerTabHelper keeps track of the download that belongs to the page you are looking at. Think of it as a clerk sitting next to each browser tab, holding a clipboard with the current download written on it.
When you start a second download while one is already on the clipboard, Chrome asks a helper (the "delegate") what to do, and the handler shown acts when the policy is to replace the old download. That question is asked in a way that does not wait for the answer. The clerk writes a note saying "when the answer comes back, do this", and carries on. The problem is that if you close the page before the answer comes back, the clerk is sent home and the clipboard is thrown away. When the answer finally arrives, the note tells the code to write on a clipboard that no longer exists. In programming terms, the program reaches into memory that has already been freed. That is called a use after free, and it can crash the browser.
This report was filed through Chrome's Vulnerability Reward Program (VRP), a scheme where Google pays people who find and report security bugs. The researcher is lime (@limeSec_) from the TIANGONG Team of Legendsec at QI-ANXIN Group. The bug was rated high severity, paid a bounty of 11,000.00 USD, and was disclosed on 23 July 2024 under the identifier CVE-2024-6988. CVE stands for Common Vulnerabilities and Exposures: a public catalogue number given to a security flaw so everyone can refer to the same thing.
Context
The product and the feature
Google Chrome is a web browser. The Downloads feature is the part that saves files from the web to your device. On iOS (the operating system of iPhones and iPads), Chrome is built on top of Apple's web view, and its download handling lives in a file called ios/chrome/browser/download/model/download_manager_tab_helper.mm.
The class at the centre of the report is DownloadManagerTabHelper. A class is a blueprint for an object: a bundle of data (called member variables) and actions (called methods) that belong together. An object made from that blueprint is called an instance. Here, each browser tab gets its own instance of DownloadManagerTabHelper, and that instance keeps track of the download for that tab.
The class is attached to a "web state" through a mechanism called web::WebStateUserData, which the researcher says is the same as SupportsUserData. In plain terms, this is a way of gluing extra data to a page so that the data lives and dies with the page. When the web page is closed, the helper is destroyed. That detail is the heart of the bug: the helper's lifetime is tied to the page, but an answer it is waiting for can arrive after the page is gone.
The helper keeps the current download in a member called task_. It also registers itself as a web::DownloadTaskObserver, meaning it wants to be told when something happens to that download (for example, when it finishes). Finally, it notifies its delegate when a download is created. A delegate is another object that the helper hands work to, a bit like a manager you escalate a question to.
What kind of bug this is
A use after free is a memory-safety bug. Programs reserve blocks of memory for objects, and when an object is no longer needed the program gives the block back (frees it). If some other part of the program still holds a reference to that block and writes to or reads from it after it has been freed, that is a use after free. The block may already have been handed to something else, so the old reference now points at unrelated data. Reading it gives nonsense; writing it corrupts whatever now lives there. A common everyday comparison: you cancel your hotel room, the hotel gives the room to a new guest, and you still have the old key card. If you walk in and start rearranging the furniture, you are not in your room any more.
A heap use after free is the same thing on the "heap", the area of memory where programs allocate objects whose size or lifetime is not known in advance. The report's summary states the outcome plainly: heap-use-after-free on DownloadManagerTabHelper::DidCreateDownload.
Setup
The report gives few environmental details. To reproduce the problem, a patch has to be applied first. The researcher notes that the path of the patch is not important and that it is only there for better triggering, meaning it makes the bug easier to hit reliably rather than being part of the flaw itself. After that, the behaviour is shown in a video (see the mov).
The report records the crash type as browser. The crash state is said to be in a file named uaf4-asan.log. ASan is AddressSanitizer, a tool built into development builds of programs that watches memory accesses and shouts when code touches memory it should not. The Chrome channel is listed as "Not sure" (Chrome ships in channels such as stable, beta and dev, which differ in how finished the code is), and regression is listed as "N/A", meaning the report does not claim this is a regression, a bug that appeared in a version where things used to work.
Finding it
The researcher points to a similar earlier issue, 40057634, and explains the pattern. The block function in SetCurrentDownload uses a completionHandler, so it is asynchronous. "Asynchronous" means the code does not wait for the answer: it asks the question, registers what to do when the answer arrives, and moves on. If the helper is destroyed before DidCreateDownload runs, the block still executes and touches the helper's member variables, which the researcher describes as equivalent to this->member. In object-oriented code, this is the object the method is running on, so this->member means "a variable that belongs to this object". If the object has been freed, that is a use after free.
The relevant code in SetCurrentDownload is:
void DownloadManagerTabHelper::SetCurrentDownload(
std::unique_ptr<web::DownloadTask> task) {
// If downloads are persistent, they cannot be lost once completed.
if (!task_ || (task_->GetState() == web::DownloadTask::State::kComplete &&
!WillDownloadTaskBeSavedToDrive())) {
// The task is the first download for this web state.
DidCreateDownload(std::move(task));
return;
}
__block std::unique_ptr<web::DownloadTask> block_task = std::move(task);
[delegate_ downloadManagerTabHelper:this
decidePolicyForDownload:block_task.get()
completionHandler:^(NewDownloadPolicy policy) {
if (policy == kNewDownloadPolicyReplace) {
DidCreateDownload(std::move(block_task)); //<--
}
}];
}
Line by line, in plain words:
void DownloadManagerTabHelper::SetCurrentDownload(std::unique_ptr<web::DownloadTask> task)declares a method calledSetCurrentDownloadthat belongs toDownloadManagerTabHelperand takes one argument,task, which is the download to record.std::unique_ptris a C++ type that owns an object and frees it automatically when the pointer goes away, so nobody has to remember to free it by hand.- The comment explains the rule that follows: if downloads are persistent, they cannot be lost once completed.
if (!task_ || (task_->GetState() == web::DownloadTask::State::kComplete && !WillDownloadTaskBeSavedToDrive()))checks whether there is no current download, or the current one is complete and will not be saved to Drive (Google's cloud storage). In either case the new download simply becomes the current one.DidCreateDownload(std::move(task));records the download immediately.std::movehands ownership of the task over to the method rather than copying it.return;ends the method here, so the rest only runs when a current download exists and is not finished-and-unsaved.__block std::unique_ptr<web::DownloadTask> block_task = std::move(task);stores the task in a variable marked__block. In Apple's Objective-C, a block is a chunk of code you can pass around and run later, similar to a callback or a lambda. Variables from outside the block are normally copied when the block is created;__blocklets the block share the variable instead, so it can be changed and outlive the current call.[delegate_ downloadManagerTabHelper:this decidePolicyForDownload:block_task.get() completionHandler:^(NewDownloadPolicy policy) { ... }];is an Objective-C message send. It asks the delegate to decide the policy for this download, passing the helper itself (this), the task, and a completion handler. The completion handler is the block that runs later, when the delegate has an answer.block_task.get()returns a plain pointer to the task without giving up ownership.if (policy == kNewDownloadPolicyReplace) { DidCreateDownload(std::move(block_task)); }is the body of the completion handler: if the delegate decided to replace the existing download, record the new one. The//<--comment marks the line the researcher considers the trigger.
Because the handler is asynchronous, the call into DidCreateDownload can happen after the helper has gone away. The synchronous branch (step 4) is safe because it runs immediately, while the object certainly still exists.
Exploitation
Step 1: apply the patch
The first reproduction step is to apply the patch. The report does not include the patch itself and says the path of the patch is not important; it exists only to make triggering more reliable.
Step 2: watch the video
The second step is to watch the recording (see the mov), which demonstrates the trigger. The report does not describe the contents of the video beyond that.
The crash path
The researcher walks through what happens once the asynchronous handler fires after destruction. DidCreateDownload uses the member variables task_, web_state_ and delegate_started_:
void DownloadManagerTabHelper::DidCreateDownload(
std::unique_ptr<web::DownloadTask> task) {
if (task_) {
task_->RemoveObserver(this); //<-- UAF
task_ = nullptr;//<--
}
task_ = std::move(task);//<--
task_->AddObserver(this);//<--
if (web_state_->IsVisible() && delegate_) {//<--
delegate_started_ = true;//<--
[delegate_ downloadManagerTabHelper:this
didCreateDownload:task_.get()//
webStateIsVisible:true];
}
}
Line by line:
void DownloadManagerTabHelper::DidCreateDownload(std::unique_ptr<web::DownloadTask> task)declares the method that records a newly created download.if (task_) {checks whether a current download is already recorded.task_->RemoveObserver(this); //<-- UAFtells the old download that this helper no longer wants to be notified about it. The//<-- UAFmarks this as the first dangerous access:task_is a member of the helper, and the helper has been freed, so readingtask_already touches freed memory.task_ = nullptr;//<--clears the member. Also a write to freed memory.task_ = std::move(task);//<--stores the new download in the member. Also a write to freed memory.task_->AddObserver(this);//<--registers the helper as an observer of the new download. Also touches freed memory.if (web_state_->IsVisible() && delegate_) {//<--reads two more members,web_state_anddelegate_, to check whether the page is visible and a delegate exists. Also freed memory.delegate_started_ = true;//<--records that the delegate has been told. Also freed memory.[delegate_ downloadManagerTabHelper:this didCreateDownload:task_.get() webStateIsVisible:true];tells the delegate that a download was created, passing the helper, the task and the visibility flag. Also freed memory.
If task_ is set, the code calls task_->RemoveObserver(this), and the following lines touch task_, web_state_ or delegate_started_ on the freed object. The report marks those lines as the UAF. The report marks those lines as the UAF.
Why the object can be gone
The lifetime of DownloadManagerTabHelper is tied to web::WebStateUserData, which the researcher equates with SupportsUserData: the helper is destroyed after the web page is closed. The class declaration shows the inheritance and the deleted copy operations:
class DownloadManagerTabHelper
: public web::WebStateUserData<DownloadManagerTabHelper>,
public web::WebStateObserver,
public web::DownloadTaskObserver {
public:
DownloadManagerTabHelper(const DownloadManagerTabHelper&) = delete;
DownloadManagerTabHelper& operator=(const DownloadManagerTabHelper&) = delete;
Line by line:
class DownloadManagerTabHelperstarts the declaration of the class.: public web::WebStateUserData<DownloadManagerTabHelper>,says the class inherits fromWebStateUserData, which is the glue that ties it to a web page. Inheritance means the class gets the behaviour of the parent and is treated as one of its kind.public web::WebStateObserver,says it also inherits fromWebStateObserver, so it can be told when things happen to the page.public web::DownloadTaskObserver {says it also inherits fromDownloadTaskObserver, so it can be told when things happen to the download.public:begins the section of the class that is visible to other code.DownloadManagerTabHelper(const DownloadManagerTabHelper&) = delete;deletes the copy constructor, so nobody can make a duplicate of the helper by copying it.DownloadManagerTabHelper& operator=(const DownloadManagerTabHelper&) = delete;deletes the copy assignment operator, so nobody can copy one helper over another. Both deletions prevent two objects from believing they own the same download.
So the sequence is: a download is started while a task already exists, the policy decision is deferred to the asynchronous completion handler, the helper is destroyed before the handler runs, and then the handler calls DidCreateDownload on the freed helper.
Root cause
The block passed as completionHandler captures this and calls DidCreateDownload, which dereferences member variables. The block captures this and calls DidCreateDownload, which dereferences member variables. The report's fix suggestion is direct: don't use member variables in that path.
Impact
The summary states the outcome plainly: heap-use-after-free on DownloadManagerTabHelper::DidCreateDownload. The crash type is recorded as browser. The report does not describe a specific attacker scenario, so the practical impact beyond the memory-safety violation is not spelled out.
Fix
The report only offers the suggestion not to use member variables in the asynchronous path.
Variants to test
- Look for other
completionHandlerblocks in the same codebase that capturethisand call methods touching member state. A positive result is a handler that can run after the owning object is destroyed, for example when the page or tab is closed while the decision is pending. - Check other
WebStateUserDatasubclasses that register asynchronous delegate callbacks. A positive result is a callback path with no lifetime guard, such as a weak pointer or a cancellation on destruction. - Re-examine the synchronous branch of
SetCurrentDownloadagainst the asynchronous one. A positive result is a state where the synchronous path is safe but the deferred path reaches the same member accesses after teardown. - Test the policy outcomes other than
kNewDownloadPolicyReplace. A positive result is any other policy value that still leads to a deferred call into the helper after destruction.
Glossary
- API: a defined way for one piece of software to ask another piece of software to do something, like a menu of requests a program accepts.
- Asynchronous: not waiting for a result before carrying on; the answer is handled later by a callback.
- Block: Apple's term for a chunk of code that can be passed around and run later, similar to a callback or a lambda.
- Bounty: a payment made by a company to someone who reports a security bug.
- Browser process: the main process of a browser, which manages tabs and the user interface, as opposed to separate processes that render pages.
- Callback: code you hand to another piece of code so it can run it later, usually when something finishes.
- Chrome channel: one of the release tracks of Chrome (such as stable, beta, dev), which differ in how finished the code is.
- Class: a blueprint for an object, bundling data and the actions that work on it.
- Completion handler: a callback that runs when an operation has finished and produced a result.
- CVE: Common Vulnerabilities and Exposures, a public catalogue number for a security flaw.
- Delegate: an object that another object hands work or decisions to.
- Dereference: to follow a pointer to the memory it points at and read or write that memory.
- Free: to give a block of memory back to the system so it can be reused.
- Heap: the area of memory where a program allocates objects whose size or lifetime is not known in advance.
- Heap use after free: a use after free on the heap, where code touches a freed heap block.
- Inheritance: the mechanism by which a class takes on the behaviour of another class and is treated as one of its kind.
- Instance: a concrete object made from a class blueprint.
- Member variable: a variable that belongs to an object.
- Method: an action that belongs to a class or object.
- Objective-C: the programming language used for Apple platforms, which uses square brackets for message sends and supports blocks.
- Patch: a change to source code, here used to make the bug easier to trigger.
- Pointer: a value that refers to a location in memory.
- Regression: a bug that appears in a version where things used to work.
- Severity: a rating of how serious a security flaw is.
- Synchronous: running immediately and waiting for the result before continuing.
- Use after free: a bug where code reads or writes memory that has already been freed.
- VRP: Vulnerability Reward Program, a scheme that pays researchers for reporting security bugs.
- Web state: the browser's internal representation of a page and its state.
__block: an Objective-C keyword that lets a block share a variable with the surrounding code instead of copying it.std::move: a C++ operation that transfers ownership of a value instead of copying it.std::unique_ptr: a C++ type that owns an object and frees it automatically when the pointer goes away.