|
| 1 | +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Tom Anderson < [email protected]> |
| 3 | +Date: Tue, 14 Jul 2026 10:22:15 -0700 |
| 4 | +Subject: [M150] [aura] Guard against root window destruction in |
| 5 | + ScopedCursorHider |
| 6 | + |
| 7 | +Original change's description: |
| 8 | +> [aura] Guard against root window destruction in ScopedCursorHider |
| 9 | +> |
| 10 | +> Avoid Use-After-Free during device scale factor changes on Linux/X11 by |
| 11 | +> using an aura::WindowTracker inside |
| 12 | +> ScopedCursorHider::~ScopedCursorHider(). Previously, |
| 13 | +> display::Screen::Get()->GetDisplayNearestWindow() could synchronously |
| 14 | +> dispatch events that tear down the root window and the CursorClient. By |
| 15 | +> tracking window destruction, the window is verified to be alive after |
| 16 | +> returning from display queries before making calls on the CursorClient. |
| 17 | +> |
| 18 | +> Fixed: 532970574 |
| 19 | +> Change-Id: I495fa5f12bf0a58fb047009d792366879e75521b |
| 20 | +> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8072900 |
| 21 | +> Commit-Queue: Thomas Anderson <[email protected]> |
| 22 | +> Reviewed-by: Colin Blundell <[email protected]> |
| 23 | +> Cr-Commit-Position: refs/heads/main@{#1661444} |
| 24 | + |
| 25 | +(cherry picked from commit d5f0488a4d6a4a7fd8d4941d748b5d6112bb40c9) |
| 26 | + |
| 27 | +Bug: 534618099,532970574 |
| 28 | +Change-Id: I495fa5f12bf0a58fb047009d792366879e75521b |
| 29 | +Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8092250 |
| 30 | +Reviewed-by: Avi Drissman < [email protected]> |
| 31 | +Commit-Queue: Thomas Anderson < [email protected]> |
| 32 | +Cr-Commit-Position: refs/branch-heads/7871@{#3417} |
| 33 | +Cr-Branched-From: f542126b8c1b3e80104b26bb05ec830bd1206f29-refs/heads/main@{#1639810} |
| 34 | + |
| 35 | +diff --git a/ui/aura/window.cc b/ui/aura/window.cc |
| 36 | +index 00f951491f7d0e32aa5fa25ce5dbdfe7e282372d..2c08b169b4c79b6a62b7046e49f0859748b9f598 100644 |
| 37 | +--- a/ui/aura/window.cc |
| 38 | ++++ b/ui/aura/window.cc |
| 39 | +@@ -148,18 +148,31 @@ class ScopedCursorHider { |
| 40 | + ScopedCursorHider& operator=(const ScopedCursorHider&) = delete; |
| 41 | + |
| 42 | + ~ScopedCursorHider() { |
| 43 | +- if (!window_->IsRootWindow()) |
| 44 | ++ // Store the raw window pointer in a local variable and clear the `window_` |
| 45 | ++ // raw_ptr to nullptr before carrying out the rest of the destructor. Since |
| 46 | ++ // the window can be synchronously destroyed inside display query sink calls |
| 47 | ++ // below, clearing the raw_ptr while the window is still alive prevents |
| 48 | ++ // Chromium's dangling raw_ptr checks from triggering on destruction. |
| 49 | ++ Window* window = window_; |
| 50 | ++ window_ = nullptr; |
| 51 | ++ |
| 52 | ++ if (!window->IsRootWindow()) { |
| 53 | + return; |
| 54 | ++ } |
| 55 | + |
| 56 | + // Update the device scale factor of the cursor client only when the last |
| 57 | + // mouse location is on this root window. |
| 58 | + if (hid_cursor_) { |
| 59 | +- client::CursorClient* cursor_client = client::GetCursorClient(window_); |
| 60 | +- if (cursor_client) { |
| 61 | +- const display::Display& display = |
| 62 | +- display::Screen::Get()->GetDisplayNearestWindow(window_); |
| 63 | +- cursor_client->SetDisplay(display); |
| 64 | +- cursor_client->ShowCursor(); |
| 65 | ++ aura::WindowTracker tracker; |
| 66 | ++ tracker.Add(window); |
| 67 | ++ const display::Display& display = |
| 68 | ++ display::Screen::Get()->GetDisplayNearestWindow(window); |
| 69 | ++ if (tracker.Contains(window)) { |
| 70 | ++ client::CursorClient* cursor_client = client::GetCursorClient(window); |
| 71 | ++ if (cursor_client) { |
| 72 | ++ cursor_client->SetDisplay(display); |
| 73 | ++ cursor_client->ShowCursor(); |
| 74 | ++ } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | +diff --git a/ui/aura/window_unittest.cc b/ui/aura/window_unittest.cc |
| 79 | +index c59b14ef0962a847d5cd1f446bbc653dca3fdcfa..0c21e77e810f5680b8af352fda08673565906969 100644 |
| 80 | +--- a/ui/aura/window_unittest.cc |
| 81 | ++++ b/ui/aura/window_unittest.cc |
| 82 | +@@ -22,6 +22,7 @@ |
| 83 | + #include "cc/trees/layer_tree_frame_sink.h" |
| 84 | + #include "testing/gtest/include/gtest/gtest.h" |
| 85 | + #include "ui/aura/client/capture_client.h" |
| 86 | ++#include "ui/aura/client/cursor_client.h" |
| 87 | + #include "ui/aura/client/focus_change_observer.h" |
| 88 | + #include "ui/aura/client/visibility_client.h" |
| 89 | + #include "ui/aura/client/window_parenting_client.h" |
| 90 | +@@ -30,6 +31,7 @@ |
| 91 | + #include "ui/aura/scoped_window_event_targeting_blocker.h" |
| 92 | + #include "ui/aura/test/aura_test_base.h" |
| 93 | + #include "ui/aura/test/aura_test_utils.h" |
| 94 | ++#include "ui/aura/test/test_cursor_client.h" |
| 95 | + #include "ui/aura/test/test_window_delegate.h" |
| 96 | + #include "ui/aura/test/test_windows.h" |
| 97 | + #include "ui/aura/test/window_test_api.h" |
| 98 | +@@ -4078,6 +4080,106 @@ TEST_P(WindowActualScreenBoundsTest, VerifyWindowActualBoundsDuringAnimation) { |
| 99 | + bounds_checker.WaitForAnimationCompletion(); |
| 100 | + } |
| 101 | + |
| 102 | ++class ReentrantDisplayScreen : public display::Screen { |
| 103 | ++ public: |
| 104 | ++ explicit ReentrantDisplayScreen(display::Screen* original_screen, |
| 105 | ++ base::RepeatingClosure on_get_display) |
| 106 | ++ : original_screen_(original_screen), |
| 107 | ++ on_get_display_(std::move(on_get_display)) { |
| 108 | ++ set_shutdown(true); |
| 109 | ++ } |
| 110 | ++ |
| 111 | ++ ReentrantDisplayScreen(const ReentrantDisplayScreen&) = delete; |
| 112 | ++ ReentrantDisplayScreen& operator=(const ReentrantDisplayScreen&) = delete; |
| 113 | ++ |
| 114 | ++ ~ReentrantDisplayScreen() override = default; |
| 115 | ++ |
| 116 | ++ // Overridden from display::Screen: |
| 117 | ++ gfx::Point GetCursorScreenPoint() override { |
| 118 | ++ return original_screen_->GetCursorScreenPoint(); |
| 119 | ++ } |
| 120 | ++ bool IsWindowUnderCursor(gfx::NativeWindow window) override { |
| 121 | ++ return original_screen_->IsWindowUnderCursor(window); |
| 122 | ++ } |
| 123 | ++ gfx::NativeWindow GetWindowAtScreenPoint(const gfx::Point& point) override { |
| 124 | ++ return original_screen_->GetWindowAtScreenPoint(point); |
| 125 | ++ } |
| 126 | ++ gfx::NativeWindow GetLocalProcessWindowAtPoint( |
| 127 | ++ const gfx::Point& point, |
| 128 | ++ const std::set<gfx::NativeWindow>& ignore) override { |
| 129 | ++ return original_screen_->GetLocalProcessWindowAtPoint(point, ignore); |
| 130 | ++ } |
| 131 | ++ int GetNumDisplays() const override { |
| 132 | ++ return original_screen_->GetNumDisplays(); |
| 133 | ++ } |
| 134 | ++ const std::vector<display::Display>& GetAllDisplays() const override { |
| 135 | ++ return original_screen_->GetAllDisplays(); |
| 136 | ++ } |
| 137 | ++ display::Display GetDisplayNearestWindow( |
| 138 | ++ gfx::NativeWindow window) const override { |
| 139 | ++ on_get_display_.Run(); |
| 140 | ++ return original_screen_->GetDisplayNearestWindow(window); |
| 141 | ++ } |
| 142 | ++ display::Display GetDisplayNearestPoint( |
| 143 | ++ const gfx::Point& point) const override { |
| 144 | ++ return original_screen_->GetDisplayNearestPoint(point); |
| 145 | ++ } |
| 146 | ++ display::Display GetDisplayMatching( |
| 147 | ++ const gfx::Rect& match_rect) const override { |
| 148 | ++ return original_screen_->GetDisplayMatching(match_rect); |
| 149 | ++ } |
| 150 | ++ display::Display GetPrimaryDisplay() const override { |
| 151 | ++ return original_screen_->GetPrimaryDisplay(); |
| 152 | ++ } |
| 153 | ++ void AddObserver(display::DisplayObserver* observer) override { |
| 154 | ++ original_screen_->AddObserver(observer); |
| 155 | ++ } |
| 156 | ++ void RemoveObserver(display::DisplayObserver* observer) override { |
| 157 | ++ original_screen_->RemoveObserver(observer); |
| 158 | ++ } |
| 159 | ++ |
| 160 | ++ private: |
| 161 | ++ raw_ptr<display::Screen> original_screen_; |
| 162 | ++ base::RepeatingClosure on_get_display_; |
| 163 | ++}; |
| 164 | ++ |
| 165 | ++TEST_F(WindowTest, ScopedCursorHiderCursorClientFreedDuringGetDisplay) { |
| 166 | ++ display::Screen* original_screen = display::Screen::Get(); |
| 167 | ++ |
| 168 | ++ std::unique_ptr<Window> test_root = std::make_unique<Window>(nullptr); |
| 169 | ++ test_root->Init(ui::LAYER_NOT_DRAWN); |
| 170 | ++ test_root->set_host(host()); |
| 171 | ++ test_root->SetBounds(gfx::Rect(0, 0, 800, 600)); |
| 172 | ++ |
| 173 | ++ TestCursorClient cursor_client(root_window()); |
| 174 | ++ client::SetCursorClient(root_window(), nullptr); |
| 175 | ++ client::SetCursorClient(test_root.get(), &cursor_client); |
| 176 | ++ |
| 177 | ++ Env::GetInstance()->SetLastMouseLocation(gfx::Point(10, 10)); |
| 178 | ++ |
| 179 | ++ cursor_client.ShowCursor(); |
| 180 | ++ EXPECT_TRUE(cursor_client.IsCursorVisible()); |
| 181 | ++ |
| 182 | ++ ReentrantDisplayScreen reentrant_screen( |
| 183 | ++ original_screen, |
| 184 | ++ base::BindLambdaForTesting([&]() { test_root.reset(); })); |
| 185 | ++ |
| 186 | ++ display::Screen::SetScreenInstance(&reentrant_screen); |
| 187 | ++ |
| 188 | ++ // This call synchronously destroys `test_root` during the nested display |
| 189 | ++ // nearest window query. With the fix, the ScopedCursorHider destructor |
| 190 | ++ // detects this destruction via `WindowTracker` and avoids dereferencing the |
| 191 | ++ // freed `CursorClient` pointer or `test_root`, so this call should complete |
| 192 | ++ // without crashing. |
| 193 | ++ test_root->OnDeviceScaleFactorChanged(1.0f, 2.0f); |
| 194 | ++ |
| 195 | ++ display::Screen::SetScreenInstance(nullptr); |
| 196 | ++ display::Screen::SetScreenInstance(original_screen); |
| 197 | ++ |
| 198 | ++ // Restore cursor client. |
| 199 | ++ client::SetCursorClient(root_window(), &cursor_client); |
| 200 | ++} |
| 201 | ++ |
| 202 | + } // namespace |
| 203 | + } // namespace test |
| 204 | + } // namespace aura |
0 commit comments