Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(448)

Side by Side Diff: content/browser/security_exploit_browsertest.cc

Issue 92873004: Prevent the browser process from creating duplicate RenderViewHosts. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixes based on Charlie's review. Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/command_line.h" 5 #include "base/command_line.h"
6 #include "base/containers/hash_tables.h"
7 #include "content/browser/dom_storage/dom_storage_context_wrapper.h"
8 #include "content/browser/dom_storage/session_storage_namespace_impl.h"
9 #include "content/browser/renderer_host/render_view_host_factory.h"
6 #include "content/browser/renderer_host/render_view_host_impl.h" 10 #include "content/browser/renderer_host/render_view_host_impl.h"
7 #include "content/browser/web_contents/web_contents_impl.h" 11 #include "content/browser/web_contents/web_contents_impl.h"
12 #include "content/common/view_messages.h"
13 #include "content/public/browser/browser_context.h"
8 #include "content/public/browser/notification_service.h" 14 #include "content/public/browser/notification_service.h"
9 #include "content/public/browser/notification_types.h" 15 #include "content/public/browser/notification_types.h"
16 #include "content/public/browser/storage_partition.h"
10 #include "content/public/common/content_switches.h" 17 #include "content/public/common/content_switches.h"
18 #include "content/public/test/browser_test_utils.h"
11 #include "content/public/test/test_utils.h" 19 #include "content/public/test/test_utils.h"
12 #include "content/shell/browser/shell.h" 20 #include "content/shell/browser/shell.h"
13 #include "content/test/content_browser_test.h" 21 #include "content/test/content_browser_test.h"
14 #include "content/test/content_browser_test_utils.h" 22 #include "content/test/content_browser_test_utils.h"
15 23
16 namespace content { 24 namespace content {
17 25
18 // The goal of these tests will be to "simulate" exploited renderer processes, 26 // The goal of these tests will be to "simulate" exploited renderer processes,
19 // which can send arbitrary IPC messages and confuse browser process internal 27 // which can send arbitrary IPC messages and confuse browser process internal
20 // state, leading to security bugs. We are trying to verify that the browser 28 // state, leading to security bugs. We are trying to verify that the browser
(...skipping 24 matching lines...) Expand all
45 shell()->web_contents()->GetRenderViewHost()->GetEnabledBindings()); 53 shell()->web_contents()->GetRenderViewHost()->GetEnabledBindings());
46 54
47 content::WindowedNotificationObserver terminated( 55 content::WindowedNotificationObserver terminated(
48 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, 56 content::NOTIFICATION_RENDERER_PROCESS_CLOSED,
49 content::NotificationService::AllSources()); 57 content::NotificationService::AllSources());
50 shell()->web_contents()->GetRenderViewHost()->SetWebUIProperty( 58 shell()->web_contents()->GetRenderViewHost()->SetWebUIProperty(
51 "toolkit", "views"); 59 "toolkit", "views");
52 terminated.Wait(); 60 terminated.Wait();
53 } 61 }
54 62
63 // RenderViewHostFactory object which tracks creations of RenderViewHost objects
64 // to ensure there are no duplicates. It is used by the following
65 // AttemptDuplicateRVH test case.
66 class TrackingRenderViewHostFactory : public RenderViewHostFactory {
67 public:
68 TrackingRenderViewHostFactory() : has_duplicates_(false) {
69 RegisterFactory(this);
70 }
71
72 virtual ~TrackingRenderViewHostFactory() {
73 UnregisterFactory();
74 }
75
76 // RenderViewHostFactory implementation.
77 virtual RenderViewHost* CreateRenderViewHost(
78 SiteInstance* instance,
79 RenderViewHostDelegate* delegate,
80 RenderWidgetHostDelegate* widget_delegate,
81 int routing_id,
82 int main_frame_routing_id,
83 bool swapped_out) OVERRIDE {
84 std::pair<int, int> id = std::make_pair(
85 instance->GetProcess()->GetID(), routing_id);
86
87 DuplicatesSet::iterator it = rvh_map_.find(id);
88 if (it != rvh_map_.end())
89 has_duplicates_ = true;
90
91 RenderViewHostImpl* rvh = new RenderViewHostImpl(
92 instance, delegate, widget_delegate, routing_id, main_frame_routing_id,
93 swapped_out, false);
94 rvh_map_.insert(id);
95
96 return rvh;
97 }
98
99 bool has_duplicates() {
100 return has_duplicates_;
101 }
102
103 private:
104 typedef base::hash_set<std::pair<int,int> > DuplicatesSet;
Charlie Reis 2013/12/02 22:25:34 nit: DuplicatesSet -> IDSet (It doesn't actually
nasko 2013/12/02 22:51:08 Done.
105 DuplicatesSet rvh_map_;
Charlie Reis 2013/12/02 22:25:34 nit: rvh_map_ isn't the right name anymore. rvh_i
nasko 2013/12/02 22:51:08 Done.
106
107 bool has_duplicates_;
108
109 DISALLOW_COPY_AND_ASSIGN(TrackingRenderViewHostFactory);
110 };
111
112 // This is a test for crbug.com/312016. It tries to create two RenderViewHosts
113 // with the same process and routing ids, which causes a collision.
114 // It creates a couple of windows in process 1, which causes a few routing ids
115 // to be allocated. Then a cross-process navigation is initiated, which causes a
116 // new process 2 to be created and have a pending RenderViewHost for it.
117 // Before the commit of the new pending RenderViewHost, it creates a new window
118 // through process 2. The original bug caused the new window RenderViewHost
119 // to be created in process 1 with a routing id which has already been
120 // allocated.
121 IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, AttemptDuplicateRVH) {
122 TrackingRenderViewHostFactory rvh_factory;
123 GURL foo("http://foo.com/files/simple_page.html");
124
125 // Start off with initial navigation, so we get the first process allocated.
126 NavigateToURL(shell(), foo);
127
128 // Open another window, so we generate some more routing ids.
129 ShellAddedObserver shell2_observer;
130 EXPECT_TRUE(ExecuteScript(
131 shell()->web_contents(), "window.open(document.URL + '#2');"));
132 Shell* shell2 = shell2_observer.GetShell();
133
134 // The new window must be in the same process, but have a new routing id.
135 EXPECT_EQ(shell()->web_contents()->GetRenderViewHost()->GetProcess()->GetID(),
136 shell2->web_contents()->GetRenderViewHost()->GetProcess()->GetID());
137 int window2_routing_id =
138 shell2->web_contents()->GetRenderViewHost()->GetRoutingID();
139 EXPECT_NE(window2_routing_id,
140 shell()->web_contents()->GetRenderViewHost()->GetRoutingID());
141
142 // Now, simulate a link click coming from the renderer.
143 GURL extension_url("https://bar.com/files/simple_page.html");
144 WebContentsImpl* wc = static_cast<WebContentsImpl*>(
145 shell()->web_contents());
146 wc->RequestOpenURL(
147 shell()->web_contents()->GetRenderViewHost(), extension_url,
148 Referrer(), CURRENT_TAB, wc->GetFrameTree()->root()->frame_id(),
149 false, true);
150
151 // Since the navigation above requires a cross-process swap, there will be a
152 // pending RenderViewHost. Ensure it exists and is in a different process
153 // than the initial page.
154 RenderViewHostImpl* pending_rvh =
155 wc->GetRenderManagerForTesting()->pending_render_view_host();
156 EXPECT_TRUE(pending_rvh != NULL);
157 EXPECT_NE(shell()->web_contents()->GetRenderViewHost()->GetProcess()->GetID(),
158 pending_rvh->GetProcess()->GetID());
159
160 // Since this test executes on the UI thread and hopping threads might cause
161 // different timing in the test, let's simulate a CreateNewWindow call coming
162 // from the IO thread.
163 ViewHostMsg_CreateWindow_Params params;
164 DOMStorageContextWrapper* dom_storage_context =
165 static_cast<DOMStorageContextWrapper*>(
166 BrowserContext::GetStoragePartition(
167 wc->GetBrowserContext(),
168 pending_rvh->GetSiteInstance())->GetDOMStorageContext());
169 SessionStorageNamespaceImpl* session_storage =
170 new SessionStorageNamespaceImpl(dom_storage_context);
171 // Cause a deliberate collision in routing ids.
172 int routing_id = window2_routing_id;
173 int main_frame_routing_id = routing_id + 1;
174
175 pending_rvh->CreateNewWindow(
176 routing_id, main_frame_routing_id, params, session_storage);
177
178 EXPECT_FALSE(rvh_factory.has_duplicates());
55 } 179 }
180
181 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698