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

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: Don't kill invalid process. 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 explicit TrackingRenderViewHostFactory() : has_duplicates_(false) {
Charlie Reis 2013/12/02 19:22:50 nit: explicit is only needed for one-arg construct
nasko 2013/12/02 20:19:13 Done.
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 DuplicatesMap::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(std::make_pair(id, rvh));
95
96 return rvh;
97 }
98
99 bool has_duplicates() {
100 return has_duplicates_;
101 }
102
103 private:
104 typedef base::hash_map<std::pair<int,int>, RenderViewHostImpl*> DuplicatesMap;
Charlie Reis 2013/12/02 19:22:50 Is this really a map of duplicates? Looks like we
nasko 2013/12/02 20:19:13 Yes, using a hash_set definitely fits here. Fixed.
105 DuplicatesMap rvh_map;
Charlie Reis 2013/12/02 19:22:50 style nit: trailing underscore
nasko 2013/12/02 20:19:13 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
Charlie Reis 2013/12/02 19:22:50 nit: No parens around 1 or 2. That makes it feel
nasko 2013/12/02 20:19:13 Done.
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();
Charlie Reis 2013/12/02 19:22:50 Should we have a EXPECT_NE for the routing IDs, si
nasko 2013/12/02 20:19:13 Done.
139
140 // Now, simulate a click to chrome-extension:// URL coming from the renderer.
Charlie Reis 2013/12/02 19:22:50 chrome-extension:// shouldn't do anything special
nasko 2013/12/02 20:19:13 No real reason other than the PoC used it and it s
141 GURL extension_url("chrome-extension://foo/");
142 WebContentsImpl* wc = static_cast<WebContentsImpl*>(
143 shell()->web_contents());
144 wc->RequestOpenURL(
145 shell()->web_contents()->GetRenderViewHost(), extension_url,
146 Referrer(), CURRENT_TAB, wc->GetFrameTree()->root()->frame_id(),
147 false, true);
148
149 // Since the navigation above requires a cross-process swap, there will be a
150 // pending RenderViewHost. Ensure it exists and is in a different process
151 // than the initial page.
152 RenderViewHostImpl* pending_rvh =
153 wc->GetRenderManagerForTesting()->pending_render_view_host();
154 EXPECT_TRUE(pending_rvh != NULL);
155 EXPECT_NE(shell()->web_contents()->GetRenderViewHost()->GetProcess()->GetID(),
156 pending_rvh->GetProcess()->GetID());
157
158 // Since this test executes on the UI thread and hopping threads might cause
159 // different timing in the test, let's simulate a CreateNewWindow call coming
160 // from the IO thread.
161 ViewHostMsg_CreateWindow_Params params;
162 DOMStorageContextWrapper* dom_storage_context =
163 static_cast<DOMStorageContextWrapper*>(
164 BrowserContext::GetStoragePartition(
165 wc->GetBrowserContext(),
166 pending_rvh->GetSiteInstance())->GetDOMStorageContext());
167 SessionStorageNamespaceImpl* session_storage =
168 new SessionStorageNamespaceImpl(dom_storage_context);
Charlie Reis 2013/12/02 19:22:50 Huh, unfortunate that we have to add this SessionS
nasko 2013/12/02 20:19:13 This pattern is used very rarely. I'd rather not c
169 // Cause a deliberate collision in routing ids.
170 int routing_id = window2_routing_id;
171 int main_frame_routing_id = routing_id + 1;
172
173 pending_rvh->CreateNewWindow(
174 routing_id, main_frame_routing_id, params, session_storage);
175
176 EXPECT_FALSE(rvh_factory.has_duplicates());
Charlie Reis 2013/12/02 19:22:50 Do we expect the process to be killed?
nasko 2013/12/02 20:19:13 In the lifetime of the test, the process doesn't h
55 } 177 }
178
179 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698