blob: 4da524aa64d88e05209b01f984d87da47d0e7088 [file] [log] [blame]
[email protected]81374f22013-02-07 02:03:451// 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
3// found in the LICENSE file.
4
5#include "base/command_line.h"
[email protected]04cbd3d2013-12-04 04:58:206#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"
[email protected]65920f332014-03-04 21:14:189#include "content/browser/frame_host/navigator.h"
[email protected]04cbd3d2013-12-04 04:58:2010#include "content/browser/renderer_host/render_view_host_factory.h"
[email protected]81374f22013-02-07 02:03:4511#include "content/browser/renderer_host/render_view_host_impl.h"
12#include "content/browser/web_contents/web_contents_impl.h"
[email protected]04cbd3d2013-12-04 04:58:2013#include "content/common/view_messages.h"
14#include "content/public/browser/browser_context.h"
[email protected]04cbd3d2013-12-04 04:58:2015#include "content/public/browser/storage_partition.h"
[email protected]81374f22013-02-07 02:03:4516#include "content/public/common/content_switches.h"
[email protected]04cbd3d2013-12-04 04:58:2017#include "content/public/test/browser_test_utils.h"
[email protected]81374f22013-02-07 02:03:4518#include "content/public/test/test_utils.h"
[email protected]de7d61ff2013-08-20 11:30:4119#include "content/shell/browser/shell.h"
[email protected]81374f22013-02-07 02:03:4520#include "content/test/content_browser_test.h"
21#include "content/test/content_browser_test_utils.h"
22
23namespace content {
24
[email protected]04cbd3d2013-12-04 04:58:2025namespace {
26
27// This is a helper function for the tests which attempt to create a
28// duplicate RenderViewHost or RenderWidgetHost. It tries to create two objects
29// with the same process and routing ids, which causes a collision.
30// It creates a couple of windows in process 1, which causes a few routing ids
31// to be allocated. Then a cross-process navigation is initiated, which causes a
32// new process 2 to be created and have a pending RenderViewHost for it. The
33// routing id of the RenderViewHost which is target for a duplicate is set
34// into |target_routing_id| and the pending RenderViewHost which is used for
35// the attempt is the return value.
36RenderViewHostImpl* PrepareToDuplicateHosts(Shell* shell,
37 int* target_routing_id) {
38 GURL foo("http://foo.com/files/simple_page.html");
39
40 // Start off with initial navigation, so we get the first process allocated.
41 NavigateToURL(shell, foo);
42
43 // Open another window, so we generate some more routing ids.
44 ShellAddedObserver shell2_observer;
45 EXPECT_TRUE(ExecuteScript(
46 shell->web_contents(), "window.open(document.URL + '#2');"));
47 Shell* shell2 = shell2_observer.GetShell();
48
49 // The new window must be in the same process, but have a new routing id.
50 EXPECT_EQ(shell->web_contents()->GetRenderViewHost()->GetProcess()->GetID(),
51 shell2->web_contents()->GetRenderViewHost()->GetProcess()->GetID());
52 *target_routing_id =
53 shell2->web_contents()->GetRenderViewHost()->GetRoutingID();
54 EXPECT_NE(*target_routing_id,
55 shell->web_contents()->GetRenderViewHost()->GetRoutingID());
56
57 // Now, simulate a link click coming from the renderer.
58 GURL extension_url("https://bar.com/files/simple_page.html");
59 WebContentsImpl* wc = static_cast<WebContentsImpl*>(shell->web_contents());
[email protected]65920f332014-03-04 21:14:1860 wc->GetFrameTree()->root()->navigator()->RequestOpenURL(
61 wc->GetFrameTree()->root()->current_frame_host(), extension_url,
[email protected]58faf942014-02-20 21:03:5862 Referrer(), CURRENT_TAB,
[email protected]65920f332014-03-04 21:14:1863 wc->GetFrameTree()->root()->current_frame_host()->GetRoutingID(),
[email protected]04cbd3d2013-12-04 04:58:2064 false, true);
65
66 // Since the navigation above requires a cross-process swap, there will be a
67 // pending RenderViewHost. Ensure it exists and is in a different process
68 // than the initial page.
69 RenderViewHostImpl* pending_rvh =
70 wc->GetRenderManagerForTesting()->pending_render_view_host();
71 EXPECT_TRUE(pending_rvh != NULL);
72 EXPECT_NE(shell->web_contents()->GetRenderViewHost()->GetProcess()->GetID(),
73 pending_rvh->GetProcess()->GetID());
74
75 return pending_rvh;
76}
77
78} // namespace
79
80
[email protected]81374f22013-02-07 02:03:4581// The goal of these tests will be to "simulate" exploited renderer processes,
82// which can send arbitrary IPC messages and confuse browser process internal
83// state, leading to security bugs. We are trying to verify that the browser
84// doesn't perform any dangerous operations in such cases.
85class SecurityExploitBrowserTest : public ContentBrowserTest {
86 public:
87 SecurityExploitBrowserTest() {}
[email protected]9b85c922c2013-02-20 10:31:4588 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
[email protected]81374f22013-02-07 02:03:4589 ASSERT_TRUE(test_server()->Start());
90
91 // Add a host resolver rule to map all outgoing requests to the test server.
92 // This allows us to use "real" hostnames in URLs, which we can use to
93 // create arbitrary SiteInstances.
94 command_line->AppendSwitchASCII(
95 switches::kHostResolverRules,
96 "MAP * " + test_server()->host_port_pair().ToString() +
97 ",EXCLUDE localhost");
98 }
99};
100
101// Ensure that we kill the renderer process if we try to give it WebUI
102// properties and it doesn't have enabled WebUI bindings.
103IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, SetWebUIProperty) {
104 GURL foo("http://foo.com/files/simple_page.html");
105
106 NavigateToURL(shell(), foo);
107 EXPECT_EQ(0,
108 shell()->web_contents()->GetRenderViewHost()->GetEnabledBindings());
109
[email protected]8ffad4e2014-01-02 23:18:26110 content::RenderProcessHostWatcher terminated(
111 shell()->web_contents(),
112 content::RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT);
[email protected]81374f22013-02-07 02:03:45113 shell()->web_contents()->GetRenderViewHost()->SetWebUIProperty(
114 "toolkit", "views");
115 terminated.Wait();
116}
117
[email protected]04cbd3d2013-12-04 04:58:20118// This is a test for crbug.com/312016 attempting to create duplicate
119// RenderViewHosts. SetupForDuplicateHosts sets up this test case and leaves
120// it in a state with pending RenderViewHost. Before the commit of the new
121// pending RenderViewHost, this test case creates a new window through the new
122// process.
123IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest,
124 AttemptDuplicateRenderViewHost) {
125 int duplicate_routing_id = MSG_ROUTING_NONE;
126 RenderViewHostImpl* pending_rvh =
127 PrepareToDuplicateHosts(shell(), &duplicate_routing_id);
128 EXPECT_NE(MSG_ROUTING_NONE, duplicate_routing_id);
129
130 // Since this test executes on the UI thread and hopping threads might cause
131 // different timing in the test, let's simulate a CreateNewWindow call coming
132 // from the IO thread.
133 ViewHostMsg_CreateWindow_Params params;
134 DOMStorageContextWrapper* dom_storage_context =
135 static_cast<DOMStorageContextWrapper*>(
136 BrowserContext::GetStoragePartition(
137 shell()->web_contents()->GetBrowserContext(),
138 pending_rvh->GetSiteInstance())->GetDOMStorageContext());
[email protected]4af624512013-12-13 14:58:43139 scoped_refptr<SessionStorageNamespaceImpl> session_storage(
140 new SessionStorageNamespaceImpl(dom_storage_context));
[email protected]04cbd3d2013-12-04 04:58:20141 // Cause a deliberate collision in routing ids.
142 int main_frame_routing_id = duplicate_routing_id + 1;
143 pending_rvh->CreateNewWindow(
144 duplicate_routing_id, main_frame_routing_id, params, session_storage);
145
146 // If the above operation doesn't cause a crash, the test has succeeded!
[email protected]81374f22013-02-07 02:03:45147}
[email protected]04cbd3d2013-12-04 04:58:20148
[email protected]a8504022013-12-04 20:23:51149// This is a test for crbug.com/312016. It tries to create two RenderWidgetHosts
150// with the same process and routing ids, which causes a collision. It is almost
151// identical to the AttemptDuplicateRenderViewHost test case.
152IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest,
153 AttemptDuplicateRenderWidgetHost) {
154 int duplicate_routing_id = MSG_ROUTING_NONE;
155 RenderViewHostImpl* pending_rvh =
156 PrepareToDuplicateHosts(shell(), &duplicate_routing_id);
157 EXPECT_NE(MSG_ROUTING_NONE, duplicate_routing_id);
158
159 // Since this test executes on the UI thread and hopping threads might cause
160 // different timing in the test, let's simulate a CreateNewWidget call coming
161 // from the IO thread. Use the existing window routing id to cause a
162 // deliberate collision.
163 pending_rvh->CreateNewWidget(duplicate_routing_id, blink::WebPopupTypeSelect);
164
165 // If the above operation doesn't crash, the test has succeeded!
166}
167
[email protected]04cbd3d2013-12-04 04:58:20168} // namespace content