|
| 1 | +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Andrea Orru < [email protected]> |
| 3 | +Date: Mon, 6 Jul 2026 18:20:55 -0700 |
| 4 | +Subject: [M148] [Extensions] Fix UAF in AppWindow::SetNativeWindowFullscreen |
| 5 | + |
| 6 | +Original change's description: |
| 7 | +> [Extensions] Fix UAF in AppWindow::SetNativeWindowFullscreen |
| 8 | +> |
| 9 | +> If the window is destroyed during a nested message loop spun by the |
| 10 | +> platform fullscreen transition (e.g. on macOS), the AppWindow instance |
| 11 | +> is deleted, resulting in a UAF when execution returns to |
| 12 | +> SetNativeWindowFullscreen() and calls RestoreAlwaysOnTop(). |
| 13 | +> |
| 14 | +> This change adds a dedicated weak pointer factory to AppWindow and |
| 15 | +> checks liveness before executing any logic following the platform |
| 16 | +> SetFullscreen call. We avoid reusing image_loader_ptr_factory_ as it |
| 17 | +> gets explicitly invalidated during favicon downloading. |
| 18 | +> |
| 19 | +> Fixed: 516948486 |
| 20 | +> Change-Id: I371d778366eaaa9dac5e565b6ec00ee666833e03 |
| 21 | +> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7902487 |
| 22 | +> Reviewed-by: Toni Barzic <[email protected]> |
| 23 | +> Commit-Queue: Andrea Orru <[email protected]> |
| 24 | +> Reviewed-by: Solomon Kinard <[email protected]> |
| 25 | +> Cr-Commit-Position: refs/heads/main@{#1647781} |
| 26 | + |
| 27 | +(cherry picked from commit c73802e7eca02587d75cc2db56a1fc851ba5d76f) |
| 28 | + |
| 29 | +Bug: 524890377,516948486 |
| 30 | +Change-Id: I371d778366eaaa9dac5e565b6ec00ee666833e03 |
| 31 | +Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7997178 |
| 32 | +Commit-Queue: Andrea Orru < [email protected]> |
| 33 | +Reviewed-by: Andrea Orru < [email protected]> |
| 34 | +Reviewed-by: Marc Treib < [email protected]> |
| 35 | +Reviewed-by: Finnur Thorarinsson < [email protected]> |
| 36 | +Cr-Commit-Position: refs/branch-heads/7778@{#4445} |
| 37 | +Cr-Branched-From: 77f495ee216d4c3cc784d33658bad4778c0680ee-refs/heads/main@{#1610480} |
| 38 | + |
| 39 | +diff --git a/chrome/browser/apps/platform_apps/DEPS b/chrome/browser/apps/platform_apps/DEPS |
| 40 | +new file mode 100644 |
| 41 | +index 0000000000000000000000000000000000000000..47ca473c62749a8ab1127bce421311ad14ff50a6 |
| 42 | +--- /dev/null |
| 43 | ++++ b/chrome/browser/apps/platform_apps/DEPS |
| 44 | +@@ -0,0 +1,5 @@ |
| 45 | ++specific_include_rules = { |
| 46 | ++ "app_window_browsertest.cc": [ |
| 47 | ++ "+extensions/components/native_app_window/native_app_window_views.h", |
| 48 | ++ ], |
| 49 | ++} |
| 50 | +diff --git a/chrome/browser/apps/platform_apps/app_browsertest_util.cc b/chrome/browser/apps/platform_apps/app_browsertest_util.cc |
| 51 | +index f471b64fe4b990da200d0c50295b0c59f872bed0..4334c6f967e0954443c3e352eabbc0577b53aa50 100644 |
| 52 | +--- a/chrome/browser/apps/platform_apps/app_browsertest_util.cc |
| 53 | ++++ b/chrome/browser/apps/platform_apps/app_browsertest_util.cc |
| 54 | +@@ -284,6 +284,11 @@ void PlatformAppBrowserTest::CallAdjustBoundsToBeVisibleOnScreenForAppWindow( |
| 55 | + bounds); |
| 56 | + } |
| 57 | + |
| 58 | ++void PlatformAppBrowserTest::SetNativeWindowFullscreenForTesting( |
| 59 | ++ AppWindow* window) { |
| 60 | ++ window->SetNativeWindowFullscreen(); |
| 61 | ++} |
| 62 | ++ |
| 63 | + AppWindow* PlatformAppBrowserTest::CreateTestAppWindow( |
| 64 | + const std::string& window_create_options) { |
| 65 | + ExtensionTestMessageListener launched_listener("launched", |
| 66 | +diff --git a/chrome/browser/apps/platform_apps/app_browsertest_util.h b/chrome/browser/apps/platform_apps/app_browsertest_util.h |
| 67 | +index 5cff6b85c4af0616fb0299d9ca4e7382175b3c1f..b732814a0322f0e888b3a4bddcc1dda3b3f2c314 100644 |
| 68 | +--- a/chrome/browser/apps/platform_apps/app_browsertest_util.h |
| 69 | ++++ b/chrome/browser/apps/platform_apps/app_browsertest_util.h |
| 70 | +@@ -123,6 +123,9 @@ class PlatformAppBrowserTest : public MixinBasedExtensionApiTest { |
| 71 | + const gfx::Size& minimum_size, |
| 72 | + gfx::Rect* bounds); |
| 73 | + |
| 74 | ++ // Call SetNativeWindowFullscreen of |window|. |
| 75 | ++ void SetNativeWindowFullscreenForTesting(AppWindow* window); |
| 76 | ++ |
| 77 | + // Load a simple test app and create a window. The window must be closed by |
| 78 | + // the caller in order to terminate the test - use CloseAppWindow(). |
| 79 | + // |window_create_options| are the options that will be passed to |
| 80 | +diff --git a/chrome/browser/apps/platform_apps/app_window_browsertest.cc b/chrome/browser/apps/platform_apps/app_window_browsertest.cc |
| 81 | +index 1393919680bb2780854733f2fb66b64799b15df3..8adb63c0447c0fe19f351b8515275c96993a8598 100644 |
| 82 | +--- a/chrome/browser/apps/platform_apps/app_window_browsertest.cc |
| 83 | ++++ b/chrome/browser/apps/platform_apps/app_window_browsertest.cc |
| 84 | +@@ -2,6 +2,8 @@ |
| 85 | + // Use of this source code is governed by a BSD-style license that can be |
| 86 | + // found in the LICENSE file. |
| 87 | + |
| 88 | ++#include "extensions/browser/app_window/app_window.h" |
| 89 | ++ |
| 90 | + #include "base/memory/raw_ptr.h" |
| 91 | + #include "base/run_loop.h" |
| 92 | + #include "build/build_config.h" |
| 93 | +@@ -9,15 +11,18 @@ |
| 94 | + #include "chrome/browser/apps/app_service/app_service_proxy_factory.h" |
| 95 | + #include "chrome/browser/apps/platform_apps/app_browsertest_util.h" |
| 96 | + #include "chrome/browser/profiles/profile.h" |
| 97 | ++#include "chrome/browser/ui/apps/chrome_app_window_client.h" |
| 98 | + #include "chrome/browser/ui/browser.h" |
| 99 | + #include "components/services/app_service/public/cpp/app_launch_params.h" |
| 100 | + #include "components/services/app_service/public/cpp/app_launch_util.h" |
| 101 | + #include "content/public/test/browser_test.h" |
| 102 | + #include "content/public/test/test_utils.h" |
| 103 | + #include "extensions/browser/app_window/app_window_geometry_cache.h" |
| 104 | ++#include "extensions/browser/app_window/native_app_window.h" |
| 105 | + #include "extensions/common/constants.h" |
| 106 | + #include "extensions/common/extension.h" |
| 107 | + #include "extensions/common/extension_id.h" |
| 108 | ++#include "extensions/components/native_app_window/native_app_window_views.h" |
| 109 | + #include "extensions/test/extension_test_message_listener.h" |
| 110 | + #include "extensions/test/result_catcher.h" |
| 111 | + #include "ui/display/display_switches.h" |
| 112 | +@@ -273,3 +278,63 @@ IN_PROC_BROWSER_TEST_F(AppWindowAPITest, TestVisibleOnAllWorkspaces) { |
| 113 | + RunAppWindowAPITestAndWaitForRoundTrip("testVisibleOnAllWorkspaces")) |
| 114 | + << message_; |
| 115 | + } |
| 116 | ++ |
| 117 | ++namespace { |
| 118 | ++ |
| 119 | ++class ClosingOnFullscreenTransitionWindow |
| 120 | ++ : public native_app_window::NativeAppWindowViews { |
| 121 | ++ public: |
| 122 | ++ ClosingOnFullscreenTransitionWindow() = default; |
| 123 | ++ ~ClosingOnFullscreenTransitionWindow() override = default; |
| 124 | ++ |
| 125 | ++ void SetFullscreen(int fullscreen_types) override { |
| 126 | ++ // Simulate window closure during fullscreen transition (e.g. as can happen |
| 127 | ++ // on macOS when spinning a nested run loop). |
| 128 | ++ widget()->CloseNow(); |
| 129 | ++ } |
| 130 | ++}; |
| 131 | ++ |
| 132 | ++class TestAppWindowClient : public ChromeAppWindowClient { |
| 133 | ++ public: |
| 134 | ++ TestAppWindowClient() = default; |
| 135 | ++ ~TestAppWindowClient() override = default; |
| 136 | ++ |
| 137 | ++ std::unique_ptr<extensions::NativeAppWindow> CreateNativeAppWindow( |
| 138 | ++ extensions::AppWindow* window, |
| 139 | ++ extensions::AppWindow::CreateParams* params) override { |
| 140 | ++ auto native_window = |
| 141 | ++ std::make_unique<ClosingOnFullscreenTransitionWindow>(); |
| 142 | ++ native_window->Init(window, *params); |
| 143 | ++ return native_window; |
| 144 | ++ } |
| 145 | ++}; |
| 146 | ++ |
| 147 | ++} // namespace |
| 148 | ++ |
| 149 | ++// Regression test for crbug.com/516948486. |
| 150 | ++IN_PROC_BROWSER_TEST_F(AppWindowAPITest, UafInSetNativeWindowFullscreen) { |
| 151 | ++ const extensions::Extension* extension = LoadExtension( |
| 152 | ++ test_data_dir_.AppendASCII("platform_apps").AppendASCII("window_api")); |
| 153 | ++ ASSERT_TRUE(extension); |
| 154 | ++ |
| 155 | ++ TestAppWindowClient test_client; |
| 156 | ++ // `AppWindowClient::Set()` has a DCHECK verifying that we don't overwrite a |
| 157 | ++ // non-null client with another non-null client. We must clear the existing |
| 158 | ++ // client (set during browser startup) before setting our test client. |
| 159 | ++ extensions::AppWindowClient::Set(nullptr); |
| 160 | ++ extensions::AppWindowClient::Set(&test_client); |
| 161 | ++ |
| 162 | ++ extensions::AppWindow* window = CreateAppWindowFromParams( |
| 163 | ++ browser()->profile(), extension, extensions::AppWindow::CreateParams()); |
| 164 | ++ ASSERT_TRUE(window); |
| 165 | ++ |
| 166 | ++ // Trigger fullscreen transition. In our placeholder `SetFullscreen()`, |
| 167 | ++ // `OnNativeClose()` will be called, deleting `window`. Without the weak |
| 168 | ++ // pointer check in `SetNativeWindowFullscreen()`, this call would result in a |
| 169 | ++ // Use-After-Free when `RestoreAlwaysOnTop()` is reached. |
| 170 | ++ SetNativeWindowFullscreenForTesting(window); |
| 171 | ++ |
| 172 | ++ // Clear our test client before restoring the production client. |
| 173 | ++ extensions::AppWindowClient::Set(nullptr); |
| 174 | ++ extensions::AppWindowClient::Set(ChromeAppWindowClient::GetInstance()); |
| 175 | ++} |
| 176 | +diff --git a/extensions/browser/app_window/app_window.cc b/extensions/browser/app_window/app_window.cc |
| 177 | +index e2115471c65e1c3914877945ca4a9616cabfaba9..ba9b728f28300c74f4e551ea7c1de38e21f34603 100644 |
| 178 | +--- a/extensions/browser/app_window/app_window.cc |
| 179 | ++++ b/extensions/browser/app_window/app_window.cc |
| 180 | +@@ -855,7 +855,14 @@ void AppWindow::DidDownloadFavicon( |
| 181 | + } |
| 182 | + |
| 183 | + void AppWindow::SetNativeWindowFullscreen() { |
| 184 | ++ // `SetFullscreen()` can trigger window closure (e.g. on macOS when spinning a |
| 185 | ++ // nested run loop), destroying `this`. Use a WeakPtr to avoid Use-After-Free |
| 186 | ++ // when calling RestoreAlwaysOnTop(). See crbug.com/516948486. |
| 187 | ++ base::WeakPtr<AppWindow> weak_this = weak_ptr_factory_.GetWeakPtr(); |
| 188 | + native_app_window_->SetFullscreen(fullscreen_types_); |
| 189 | ++ if (!weak_this) { |
| 190 | ++ return; |
| 191 | ++ } |
| 192 | + |
| 193 | + RestoreAlwaysOnTop(); |
| 194 | + } |
| 195 | +diff --git a/extensions/browser/app_window/app_window.h b/extensions/browser/app_window/app_window.h |
| 196 | +index bf59ce45114e99296f84693e329c7e238a351ab8..78276db2dba87f1e803d6d9b6c2b6529eddc51b0 100644 |
| 197 | +--- a/extensions/browser/app_window/app_window.h |
| 198 | ++++ b/extensions/browser/app_window/app_window.h |
| 199 | +@@ -601,6 +601,7 @@ class AppWindow : public content::WebContentsDelegate, |
| 200 | + base::OnceClosure on_update_draggable_regions_callback_for_testing_; |
| 201 | + |
| 202 | + base::WeakPtrFactory<AppWindow> image_loader_ptr_factory_{this}; |
| 203 | ++ base::WeakPtrFactory<AppWindow> weak_ptr_factory_{this}; |
| 204 | + }; |
| 205 | + |
| 206 | + } // namespace extensions |
0 commit comments