[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 5 | #include <stdint.h> |
| 6 | |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 7 | #include "base/command_line.h" |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 8 | #include "base/containers/hash_tables.h" |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 9 | #include "base/macros.h" |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 10 | #include "base/strings/utf_string_conversions.h" |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 11 | #include "build/build_config.h" |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 12 | #include "content/browser/dom_storage/dom_storage_context_wrapper.h" |
| 13 | #include "content/browser/dom_storage/session_storage_namespace_impl.h" |
[email protected] | 65920f33 | 2014-03-04 21:14:18 | [diff] [blame] | 14 | #include "content/browser/frame_host/navigator.h" |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 15 | #include "content/browser/frame_host/render_frame_host_impl.h" |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 16 | #include "content/browser/renderer_host/render_view_host_factory.h" |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 17 | #include "content/browser/renderer_host/render_view_host_impl.h" |
| 18 | #include "content/browser/web_contents/web_contents_impl.h" |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 19 | #include "content/common/frame_messages.h" |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 20 | #include "content/common/resource_messages.h" |
yhirano | 20c94ea9b | 2016-05-18 05:20:45 | [diff] [blame] | 21 | #include "content/common/resource_request.h" |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 22 | #include "content/common/view_messages.h" |
| 23 | #include "content/public/browser/browser_context.h" |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 24 | #include "content/public/browser/content_browser_client.h" |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 25 | #include "content/public/browser/interstitial_page.h" |
| 26 | #include "content/public/browser/interstitial_page_delegate.h" |
gzobqq | 1af4fad | 2016-01-30 13:07:06 | [diff] [blame] | 27 | #include "content/public/browser/resource_dispatcher_host.h" |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 28 | #include "content/public/browser/storage_partition.h" |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 29 | #include "content/public/common/appcache_info.h" |
carlosk | d80262f5 | 2015-12-16 14:40:35 | [diff] [blame] | 30 | #include "content/public/common/browser_side_navigation_policy.h" |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 31 | #include "content/public/common/content_switches.h" |
wfh | 815c487 | 2015-02-25 21:01:31 | [diff] [blame] | 32 | #include "content/public/common/file_chooser_params.h" |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 33 | #include "content/public/test/browser_test_utils.h" |
[email protected] | 6e9def1 | 2014-03-27 20:23:28 | [diff] [blame] | 34 | #include "content/public/test/content_browser_test.h" |
| 35 | #include "content/public/test/content_browser_test_utils.h" |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 36 | #include "content/public/test/test_utils.h" |
[email protected] | de7d61ff | 2013-08-20 11:30:41 | [diff] [blame] | 37 | #include "content/shell/browser/shell.h" |
nasko | 402cc29 | 2016-03-11 09:16:11 | [diff] [blame] | 38 | #include "content/test/content_browser_test_utils_internal.h" |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 39 | #include "content/test/test_content_browser_client.h" |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 40 | #include "ipc/ipc_security_test_util.h" |
| 41 | #include "net/dns/mock_host_resolver.h" |
| 42 | #include "net/test/embedded_test_server/embedded_test_server.h" |
gzobqq | 1af4fad | 2016-01-30 13:07:06 | [diff] [blame] | 43 | #include "net/test/url_request/url_request_slow_download_job.h" |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 44 | |
| 45 | using IPC::IpcSecurityTestUtil; |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 46 | |
| 47 | namespace content { |
| 48 | |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 49 | namespace { |
| 50 | |
gzobqq | 1af4fad | 2016-01-30 13:07:06 | [diff] [blame] | 51 | // This request id is used by tests that craft a |
| 52 | // ResourceHostMsg_RequestResource. The id is sufficiently large that it doesn't |
| 53 | // collide with ids used by previous navigation requests. |
| 54 | const int kRequestIdNotPreviouslyUsed = 10000; |
| 55 | |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 56 | // This is a helper function for the tests which attempt to create a |
| 57 | // duplicate RenderViewHost or RenderWidgetHost. It tries to create two objects |
| 58 | // with the same process and routing ids, which causes a collision. |
| 59 | // It creates a couple of windows in process 1, which causes a few routing ids |
| 60 | // to be allocated. Then a cross-process navigation is initiated, which causes a |
| 61 | // new process 2 to be created and have a pending RenderViewHost for it. The |
| 62 | // routing id of the RenderViewHost which is target for a duplicate is set |
| 63 | // into |target_routing_id| and the pending RenderViewHost which is used for |
| 64 | // the attempt is the return value. |
| 65 | RenderViewHostImpl* PrepareToDuplicateHosts(Shell* shell, |
| 66 | int* target_routing_id) { |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 67 | GURL foo("http://foo.com/simple_page.html"); |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 68 | |
| 69 | // Start off with initial navigation, so we get the first process allocated. |
| 70 | NavigateToURL(shell, foo); |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 71 | EXPECT_EQ(base::ASCIIToUTF16("OK"), shell->web_contents()->GetTitle()); |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 72 | |
| 73 | // Open another window, so we generate some more routing ids. |
| 74 | ShellAddedObserver shell2_observer; |
nick | adef4a5 | 2016-06-09 18:45:54 | [diff] [blame^] | 75 | EXPECT_TRUE(ExecuteScript(shell, "window.open(document.URL + '#2');")); |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 76 | Shell* shell2 = shell2_observer.GetShell(); |
| 77 | |
| 78 | // The new window must be in the same process, but have a new routing id. |
| 79 | EXPECT_EQ(shell->web_contents()->GetRenderViewHost()->GetProcess()->GetID(), |
| 80 | shell2->web_contents()->GetRenderViewHost()->GetProcess()->GetID()); |
| 81 | *target_routing_id = |
| 82 | shell2->web_contents()->GetRenderViewHost()->GetRoutingID(); |
| 83 | EXPECT_NE(*target_routing_id, |
| 84 | shell->web_contents()->GetRenderViewHost()->GetRoutingID()); |
| 85 | |
| 86 | // Now, simulate a link click coming from the renderer. |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 87 | GURL extension_url("https://bar.com/simple_page.html"); |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 88 | WebContentsImpl* wc = static_cast<WebContentsImpl*>(shell->web_contents()); |
[email protected] | 65920f33 | 2014-03-04 21:14:18 | [diff] [blame] | 89 | wc->GetFrameTree()->root()->navigator()->RequestOpenURL( |
lfg | 9ef7d2d | 2014-12-15 22:32:30 | [diff] [blame] | 90 | wc->GetFrameTree()->root()->current_frame_host(), extension_url, nullptr, |
nick | 94144d4 | 2015-04-27 19:21:40 | [diff] [blame] | 91 | Referrer(), CURRENT_TAB, false, true); |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 92 | |
| 93 | // Since the navigation above requires a cross-process swap, there will be a |
carlosk | c49005eb | 2015-06-16 11:25:07 | [diff] [blame] | 94 | // speculative/pending RenderFrameHost. Ensure it exists and is in a different |
| 95 | // process than the initial page. |
| 96 | RenderFrameHostImpl* next_rfh; |
carlosk | d80262f5 | 2015-12-16 14:40:35 | [diff] [blame] | 97 | if (IsBrowserSideNavigationEnabled()) |
| 98 | next_rfh = wc->GetRenderManagerForTesting()->speculative_frame_host(); |
| 99 | else |
carlosk | c49005eb | 2015-06-16 11:25:07 | [diff] [blame] | 100 | next_rfh = wc->GetRenderManagerForTesting()->pending_frame_host(); |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 101 | |
carlosk | c49005eb | 2015-06-16 11:25:07 | [diff] [blame] | 102 | EXPECT_TRUE(next_rfh); |
| 103 | EXPECT_NE(shell->web_contents()->GetRenderProcessHost()->GetID(), |
| 104 | next_rfh->GetProcess()->GetID()); |
| 105 | |
| 106 | return next_rfh->render_view_host(); |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 107 | } |
| 108 | |
yhirano | 20c94ea9b | 2016-05-18 05:20:45 | [diff] [blame] | 109 | ResourceRequest CreateXHRRequest(const char* url) { |
| 110 | ResourceRequest request; |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 111 | request.method = "GET"; |
gzobqq | 1af4fad | 2016-01-30 13:07:06 | [diff] [blame] | 112 | request.url = GURL(url); |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 113 | request.referrer_policy = blink::WebReferrerPolicyDefault; |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 114 | request.load_flags = 0; |
| 115 | request.origin_pid = 0; |
| 116 | request.resource_type = RESOURCE_TYPE_XHR; |
| 117 | request.request_context = 0; |
| 118 | request.appcache_host_id = kAppCacheNoHostId; |
| 119 | request.download_to_file = false; |
| 120 | request.should_reset_appcache = false; |
| 121 | request.is_main_frame = true; |
| 122 | request.parent_is_main_frame = false; |
| 123 | request.parent_render_frame_id = -1; |
| 124 | request.transition_type = ui::PAGE_TRANSITION_LINK; |
| 125 | request.allow_download = true; |
| 126 | return request; |
| 127 | } |
| 128 | |
yhirano | 20c94ea9b | 2016-05-18 05:20:45 | [diff] [blame] | 129 | ResourceRequest CreateXHRRequestWithOrigin(const char* origin) { |
| 130 | ResourceRequest request = CreateXHRRequest("http://bar.com/simple_page.html"); |
gzobqq | 1af4fad | 2016-01-30 13:07:06 | [diff] [blame] | 131 | request.first_party_for_cookies = GURL(origin); |
| 132 | request.headers = base::StringPrintf("Origin: %s\r\n", origin); |
| 133 | return request; |
| 134 | } |
| 135 | |
| 136 | void TryCreateDuplicateRequestIds(Shell* shell, bool block_loaders) { |
| 137 | NavigateToURL(shell, GURL("http://foo.com/simple_page.html")); |
| 138 | RenderFrameHost* rfh = shell->web_contents()->GetMainFrame(); |
| 139 | |
| 140 | if (block_loaders) { |
| 141 | // Test the case where loaders are placed into blocked_loaders_map_. |
csharrison | a2280cd | 2016-02-03 23:21:15 | [diff] [blame] | 142 | ResourceDispatcherHost::BlockRequestsForFrameFromUI(rfh); |
gzobqq | 1af4fad | 2016-01-30 13:07:06 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | // URLRequestSlowDownloadJob waits for another request to kFinishDownloadUrl |
| 146 | // to finish all pending requests. It is never sent, so the following URL |
| 147 | // blocks indefinitely, which is good because the request stays alive and the |
| 148 | // test can try to reuse the request id without a race. |
| 149 | const char* blocking_url = net::URLRequestSlowDownloadJob::kUnknownSizeUrl; |
yhirano | 20c94ea9b | 2016-05-18 05:20:45 | [diff] [blame] | 150 | ResourceRequest request(CreateXHRRequest(blocking_url)); |
gzobqq | 1af4fad | 2016-01-30 13:07:06 | [diff] [blame] | 151 | |
| 152 | // Use the same request id twice. |
| 153 | RenderProcessHostWatcher process_killed( |
| 154 | rfh->GetProcess(), RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT); |
| 155 | IPC::IpcSecurityTestUtil::PwnMessageReceived( |
| 156 | rfh->GetProcess()->GetChannel(), |
| 157 | ResourceHostMsg_RequestResource(rfh->GetRoutingID(), |
| 158 | kRequestIdNotPreviouslyUsed, request)); |
| 159 | IPC::IpcSecurityTestUtil::PwnMessageReceived( |
| 160 | rfh->GetProcess()->GetChannel(), |
| 161 | ResourceHostMsg_RequestResource(rfh->GetRoutingID(), |
| 162 | kRequestIdNotPreviouslyUsed, request)); |
| 163 | process_killed.Wait(); |
| 164 | } |
| 165 | |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 166 | } // namespace |
| 167 | |
| 168 | |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 169 | // The goal of these tests will be to "simulate" exploited renderer processes, |
| 170 | // which can send arbitrary IPC messages and confuse browser process internal |
| 171 | // state, leading to security bugs. We are trying to verify that the browser |
| 172 | // doesn't perform any dangerous operations in such cases. |
| 173 | class SecurityExploitBrowserTest : public ContentBrowserTest { |
| 174 | public: |
| 175 | SecurityExploitBrowserTest() {} |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 176 | |
avi | 83883c8 | 2014-12-23 00:08:49 | [diff] [blame] | 177 | void SetUpCommandLine(base::CommandLine* command_line) override { |
svaldez | c3a9a17 | 2015-11-03 22:01:33 | [diff] [blame] | 178 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 179 | |
| 180 | // Add a host resolver rule to map all outgoing requests to the test server. |
| 181 | // This allows us to use "real" hostnames in URLs, which we can use to |
| 182 | // create arbitrary SiteInstances. |
| 183 | command_line->AppendSwitchASCII( |
| 184 | switches::kHostResolverRules, |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 185 | "MAP * " + |
| 186 | net::HostPortPair::FromURL(embedded_test_server()->base_url()) |
| 187 | .ToString() + |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 188 | ",EXCLUDE localhost"); |
| 189 | } |
wfh | 815c487 | 2015-02-25 21:01:31 | [diff] [blame] | 190 | |
gzobqq | 1af4fad | 2016-01-30 13:07:06 | [diff] [blame] | 191 | void SetUpOnMainThread() override { |
| 192 | BrowserThread::PostTask( |
| 193 | BrowserThread::IO, FROM_HERE, |
| 194 | base::Bind(&net::URLRequestSlowDownloadJob::AddUrlHandler)); |
| 195 | } |
| 196 | |
wfh | 815c487 | 2015-02-25 21:01:31 | [diff] [blame] | 197 | protected: |
| 198 | // Tests that a given file path sent in a ViewHostMsg_RunFileChooser will |
| 199 | // cause renderer to be killed. |
| 200 | void TestFileChooserWithPath(const base::FilePath& path); |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 201 | }; |
| 202 | |
wfh | 815c487 | 2015-02-25 21:01:31 | [diff] [blame] | 203 | void SecurityExploitBrowserTest::TestFileChooserWithPath( |
| 204 | const base::FilePath& path) { |
| 205 | GURL foo("http://foo.com/simple_page.html"); |
| 206 | NavigateToURL(shell(), foo); |
| 207 | EXPECT_EQ(base::ASCIIToUTF16("OK"), shell()->web_contents()->GetTitle()); |
| 208 | |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 209 | RenderViewHost* compromised_renderer = |
wfh | 815c487 | 2015-02-25 21:01:31 | [diff] [blame] | 210 | shell()->web_contents()->GetRenderViewHost(); |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 211 | RenderProcessHostWatcher terminated( |
wfh | 815c487 | 2015-02-25 21:01:31 | [diff] [blame] | 212 | shell()->web_contents(), |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 213 | RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT); |
wfh | 815c487 | 2015-02-25 21:01:31 | [diff] [blame] | 214 | |
| 215 | FileChooserParams params; |
| 216 | params.default_file_name = path; |
| 217 | |
| 218 | ViewHostMsg_RunFileChooser evil(compromised_renderer->GetRoutingID(), params); |
| 219 | |
| 220 | IpcSecurityTestUtil::PwnMessageReceived( |
| 221 | compromised_renderer->GetProcess()->GetChannel(), evil); |
| 222 | terminated.Wait(); |
| 223 | } |
| 224 | |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 225 | // Ensure that we kill the renderer process if we try to give it WebUI |
| 226 | // properties and it doesn't have enabled WebUI bindings. |
jaekyun | 37e572a3 | 2014-12-04 23:33:35 | [diff] [blame] | 227 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, SetWebUIProperty) { |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 228 | GURL foo("http://foo.com/simple_page.html"); |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 229 | |
| 230 | NavigateToURL(shell(), foo); |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 231 | EXPECT_EQ(base::ASCIIToUTF16("OK"), shell()->web_contents()->GetTitle()); |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 232 | EXPECT_EQ(0, |
| 233 | shell()->web_contents()->GetRenderViewHost()->GetEnabledBindings()); |
| 234 | |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 235 | RenderProcessHostWatcher terminated( |
[email protected] | 8ffad4e | 2014-01-02 23:18:26 | [diff] [blame] | 236 | shell()->web_contents(), |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 237 | RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT); |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 238 | shell()->web_contents()->GetRenderViewHost()->SetWebUIProperty( |
| 239 | "toolkit", "views"); |
| 240 | terminated.Wait(); |
| 241 | } |
| 242 | |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 243 | // This is a test for crbug.com/312016 attempting to create duplicate |
| 244 | // RenderViewHosts. SetupForDuplicateHosts sets up this test case and leaves |
| 245 | // it in a state with pending RenderViewHost. Before the commit of the new |
| 246 | // pending RenderViewHost, this test case creates a new window through the new |
| 247 | // process. |
| 248 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, |
| 249 | AttemptDuplicateRenderViewHost) { |
dcheng | 3ce04b6 | 2015-10-26 23:30:55 | [diff] [blame] | 250 | int32_t duplicate_routing_id = MSG_ROUTING_NONE; |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 251 | RenderViewHostImpl* pending_rvh = |
| 252 | PrepareToDuplicateHosts(shell(), &duplicate_routing_id); |
| 253 | EXPECT_NE(MSG_ROUTING_NONE, duplicate_routing_id); |
| 254 | |
| 255 | // Since this test executes on the UI thread and hopping threads might cause |
| 256 | // different timing in the test, let's simulate a CreateNewWindow call coming |
| 257 | // from the IO thread. |
| 258 | ViewHostMsg_CreateWindow_Params params; |
| 259 | DOMStorageContextWrapper* dom_storage_context = |
| 260 | static_cast<DOMStorageContextWrapper*>( |
| 261 | BrowserContext::GetStoragePartition( |
| 262 | shell()->web_contents()->GetBrowserContext(), |
| 263 | pending_rvh->GetSiteInstance())->GetDOMStorageContext()); |
[email protected] | 4af62451 | 2013-12-13 14:58:43 | [diff] [blame] | 264 | scoped_refptr<SessionStorageNamespaceImpl> session_storage( |
| 265 | new SessionStorageNamespaceImpl(dom_storage_context)); |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 266 | // Cause a deliberate collision in routing ids. |
dcheng | 3ce04b6 | 2015-10-26 23:30:55 | [diff] [blame] | 267 | int32_t main_frame_routing_id = duplicate_routing_id + 1; |
| 268 | // TODO(avi): This should be made unique from the view routing ID once |
| 269 | // RenderViewHostImpl has-a RenderWidgetHostImpl. https://crbug.com/545684 |
| 270 | int32_t main_frame_widget_routing_id = duplicate_routing_id; |
| 271 | pending_rvh->CreateNewWindow(duplicate_routing_id, main_frame_routing_id, |
| 272 | main_frame_widget_routing_id, params, |
dcheng | 54c3719d | 2014-08-26 21:52:56 | [diff] [blame] | 273 | session_storage.get()); |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 274 | |
| 275 | // If the above operation doesn't cause a crash, the test has succeeded! |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 276 | } |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 277 | |
[email protected] | a850402 | 2013-12-04 20:23:51 | [diff] [blame] | 278 | // This is a test for crbug.com/312016. It tries to create two RenderWidgetHosts |
| 279 | // with the same process and routing ids, which causes a collision. It is almost |
| 280 | // identical to the AttemptDuplicateRenderViewHost test case. |
| 281 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, |
| 282 | AttemptDuplicateRenderWidgetHost) { |
| 283 | int duplicate_routing_id = MSG_ROUTING_NONE; |
| 284 | RenderViewHostImpl* pending_rvh = |
| 285 | PrepareToDuplicateHosts(shell(), &duplicate_routing_id); |
| 286 | EXPECT_NE(MSG_ROUTING_NONE, duplicate_routing_id); |
| 287 | |
| 288 | // Since this test executes on the UI thread and hopping threads might cause |
| 289 | // different timing in the test, let's simulate a CreateNewWidget call coming |
| 290 | // from the IO thread. Use the existing window routing id to cause a |
| 291 | // deliberate collision. |
piman | 5d36dae | 2015-09-24 22:47:05 | [diff] [blame] | 292 | pending_rvh->CreateNewWidget(duplicate_routing_id, blink::WebPopupTypePage); |
[email protected] | a850402 | 2013-12-04 20:23:51 | [diff] [blame] | 293 | |
| 294 | // If the above operation doesn't crash, the test has succeeded! |
| 295 | } |
| 296 | |
wfh | 815c487 | 2015-02-25 21:01:31 | [diff] [blame] | 297 | // This is a test for crbug.com/444198. It tries to send a |
| 298 | // ViewHostMsg_RunFileChooser containing an invalid path. The browser should |
| 299 | // correctly terminate the renderer in these cases. |
| 300 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, AttemptRunFileChoosers) { |
| 301 | TestFileChooserWithPath(base::FilePath(FILE_PATH_LITERAL("../../*.txt"))); |
| 302 | TestFileChooserWithPath(base::FilePath(FILE_PATH_LITERAL("/etc/*.conf"))); |
| 303 | #if defined(OS_WIN) |
| 304 | TestFileChooserWithPath( |
| 305 | base::FilePath(FILE_PATH_LITERAL("\\\\evilserver\\evilshare\\*.txt"))); |
| 306 | TestFileChooserWithPath(base::FilePath(FILE_PATH_LITERAL("c:\\*.txt"))); |
| 307 | TestFileChooserWithPath(base::FilePath(FILE_PATH_LITERAL("..\\..\\*.txt"))); |
| 308 | #endif |
| 309 | } |
| 310 | |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 311 | class SecurityExploitTestInterstitialPage : public InterstitialPageDelegate { |
| 312 | public: |
| 313 | explicit SecurityExploitTestInterstitialPage(WebContents* contents) { |
| 314 | InterstitialPage* interstitial = InterstitialPage::Create( |
| 315 | contents, true, contents->GetLastCommittedURL(), this); |
| 316 | interstitial->Show(); |
| 317 | } |
| 318 | |
| 319 | // InterstitialPageDelegate implementation. |
| 320 | void CommandReceived(const std::string& command) override { |
| 321 | last_command_ = command; |
| 322 | } |
| 323 | |
| 324 | std::string GetHTMLContents() override { |
| 325 | return "<html><head><script>" |
| 326 | "window.domAutomationController.setAutomationId(1);" |
| 327 | "window.domAutomationController.send(\"okay\");" |
| 328 | "</script></head>" |
| 329 | "<body>this page is an interstitial</body></html>"; |
| 330 | } |
| 331 | |
| 332 | std::string last_command() { return last_command_; } |
| 333 | |
| 334 | private: |
| 335 | std::string last_command_; |
| 336 | DISALLOW_COPY_AND_ASSIGN(SecurityExploitTestInterstitialPage); |
| 337 | }; |
| 338 | |
| 339 | // Fails due to InterstitialPage's reliance on PostNonNestableTask |
| 340 | // http://crbug.com/432737 |
| 341 | #if defined(OS_ANDROID) |
| 342 | #define MAYBE_InterstitialCommandFromUnderlyingContent \ |
| 343 | DISABLED_InterstitialCommandFromUnderlyingContent |
| 344 | #else |
| 345 | #define MAYBE_InterstitialCommandFromUnderlyingContent \ |
| 346 | InterstitialCommandFromUnderlyingContent |
| 347 | #endif |
| 348 | |
| 349 | // The interstitial should not be controllable by the underlying content. |
| 350 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, |
| 351 | MAYBE_InterstitialCommandFromUnderlyingContent) { |
| 352 | // Start off with initial navigation, to allocate the process. |
| 353 | GURL foo("http://foo.com/simple_page.html"); |
| 354 | NavigateToURL(shell(), foo); |
| 355 | EXPECT_EQ(base::ASCIIToUTF16("OK"), shell()->web_contents()->GetTitle()); |
| 356 | |
| 357 | DOMMessageQueue message_queue; |
| 358 | |
| 359 | // Install and show an interstitial page. |
| 360 | SecurityExploitTestInterstitialPage* interstitial = |
| 361 | new SecurityExploitTestInterstitialPage(shell()->web_contents()); |
| 362 | |
| 363 | ASSERT_EQ("", interstitial->last_command()); |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 364 | WaitForInterstitialAttach(shell()->web_contents()); |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 365 | |
| 366 | InterstitialPage* interstitial_page = |
| 367 | shell()->web_contents()->GetInterstitialPage(); |
| 368 | ASSERT_TRUE(interstitial_page != NULL); |
| 369 | ASSERT_TRUE(shell()->web_contents()->ShowingInterstitialPage()); |
| 370 | ASSERT_TRUE(interstitial_page->GetDelegateForTesting() == interstitial); |
| 371 | |
| 372 | // The interstitial page ought to be able to send a message. |
| 373 | std::string message; |
| 374 | ASSERT_TRUE(message_queue.WaitForMessage(&message)); |
| 375 | ASSERT_EQ("\"okay\"", message); |
| 376 | ASSERT_EQ("\"okay\"", interstitial->last_command()); |
| 377 | |
| 378 | // Send an automation message from the underlying content and wait for it to |
| 379 | // be dispatched on this thread. This message should not be received by the |
| 380 | // interstitial. |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 381 | RenderFrameHost* compromised_renderer = |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 382 | shell()->web_contents()->GetMainFrame(); |
| 383 | FrameHostMsg_DomOperationResponse evil(compromised_renderer->GetRoutingID(), |
avi | 60bd490 | 2015-09-23 20:39:24 | [diff] [blame] | 384 | "evil"); |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 385 | IpcSecurityTestUtil::PwnMessageReceived( |
| 386 | compromised_renderer->GetProcess()->GetChannel(), evil); |
| 387 | |
| 388 | ASSERT_TRUE(message_queue.WaitForMessage(&message)); |
| 389 | ASSERT_EQ("evil", message) |
| 390 | << "Automation message should be received by WebContents."; |
| 391 | ASSERT_EQ("\"okay\"", interstitial->last_command()) |
| 392 | << "Interstitial should not be affected."; |
| 393 | |
| 394 | // Send a second message from the interstitial page, and make sure that the |
| 395 | // "evil" message doesn't arrive in the intervening period. |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 396 | ASSERT_TRUE(ExecuteScript(interstitial_page->GetMainFrame(), |
| 397 | "window.domAutomationController.send(\"okay2\");")); |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 398 | ASSERT_TRUE(message_queue.WaitForMessage(&message)); |
| 399 | ASSERT_EQ("\"okay2\"", message); |
| 400 | ASSERT_EQ("\"okay2\"", interstitial->last_command()); |
| 401 | } |
| 402 | |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 403 | class IsolatedAppContentBrowserClient : public TestContentBrowserClient { |
| 404 | public: |
| 405 | bool IsIllegalOrigin(content::ResourceContext* resource_context, |
| 406 | int child_process_id, |
| 407 | const GURL& origin) override { |
| 408 | // Simulate a case where an app origin is not in an app process. |
| 409 | return true; |
| 410 | } |
| 411 | }; |
| 412 | |
| 413 | // Renderer processes should not be able to spoof Origin HTTP headers. |
| 414 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, InvalidOriginHeaders) { |
| 415 | // Create a set of IPC messages with various Origin headers. |
yhirano | 20c94ea9b | 2016-05-18 05:20:45 | [diff] [blame] | 416 | ResourceRequest chrome_origin_msg( |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 417 | CreateXHRRequestWithOrigin("chrome://settings")); |
yhirano | 20c94ea9b | 2016-05-18 05:20:45 | [diff] [blame] | 418 | ResourceRequest embedder_isolated_origin_msg( |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 419 | CreateXHRRequestWithOrigin("https://isolated.bar.com")); |
yhirano | 20c94ea9b | 2016-05-18 05:20:45 | [diff] [blame] | 420 | ResourceRequest invalid_origin_msg(CreateXHRRequestWithOrigin("invalidurl")); |
| 421 | ResourceRequest invalid_scheme_origin_msg( |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 422 | CreateXHRRequestWithOrigin("fake-scheme://foo")); |
| 423 | |
| 424 | GURL web_url("http://foo.com/simple_page.html"); |
| 425 | NavigateToURL(shell(), web_url); |
| 426 | RenderFrameHost* web_rfh = shell()->web_contents()->GetMainFrame(); |
| 427 | |
| 428 | // Web processes cannot make XHRs with chrome:// Origin headers. |
| 429 | { |
| 430 | RenderProcessHostWatcher web_process_killed( |
| 431 | web_rfh->GetProcess(), |
| 432 | RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT); |
| 433 | IPC::IpcSecurityTestUtil::PwnMessageReceived( |
| 434 | web_rfh->GetProcess()->GetChannel(), |
gzobqq | 1af4fad | 2016-01-30 13:07:06 | [diff] [blame] | 435 | ResourceHostMsg_RequestResource(web_rfh->GetRoutingID(), |
| 436 | kRequestIdNotPreviouslyUsed, |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 437 | chrome_origin_msg)); |
| 438 | web_process_killed.Wait(); |
| 439 | } |
| 440 | |
| 441 | // Web processes cannot make XHRs with URLs that the content embedder expects |
| 442 | // to have process isolation. Ideally this would test chrome-extension:// |
| 443 | // URLs for Chrome Apps, but those can't be tested inside content/ and the |
yhirano | 20c94ea9b | 2016-05-18 05:20:45 | [diff] [blame] | 444 | // ResourceRequest IPC can't be created in a test outside content/. |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 445 | NavigateToURL(shell(), web_url); |
| 446 | { |
| 447 | // Set up a ContentBrowserClient that simulates an app URL in a non-app |
| 448 | // process. |
| 449 | IsolatedAppContentBrowserClient app_client; |
| 450 | ContentBrowserClient* old_client = SetBrowserClientForTesting(&app_client); |
| 451 | RenderProcessHostWatcher web_process_killed( |
| 452 | web_rfh->GetProcess(), |
| 453 | RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT); |
| 454 | IPC::IpcSecurityTestUtil::PwnMessageReceived( |
| 455 | web_rfh->GetProcess()->GetChannel(), |
gzobqq | 1af4fad | 2016-01-30 13:07:06 | [diff] [blame] | 456 | ResourceHostMsg_RequestResource(web_rfh->GetRoutingID(), |
| 457 | kRequestIdNotPreviouslyUsed, |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 458 | embedder_isolated_origin_msg)); |
| 459 | web_process_killed.Wait(); |
| 460 | SetBrowserClientForTesting(old_client); |
| 461 | } |
| 462 | |
| 463 | // Web processes cannot make XHRs with invalid Origin headers. |
| 464 | NavigateToURL(shell(), web_url); |
| 465 | { |
| 466 | RenderProcessHostWatcher web_process_killed( |
| 467 | web_rfh->GetProcess(), |
| 468 | RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT); |
| 469 | IPC::IpcSecurityTestUtil::PwnMessageReceived( |
| 470 | web_rfh->GetProcess()->GetChannel(), |
gzobqq | 1af4fad | 2016-01-30 13:07:06 | [diff] [blame] | 471 | ResourceHostMsg_RequestResource(web_rfh->GetRoutingID(), |
| 472 | kRequestIdNotPreviouslyUsed, |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 473 | invalid_origin_msg)); |
| 474 | web_process_killed.Wait(); |
| 475 | } |
| 476 | |
| 477 | // Web processes cannot make XHRs with invalid scheme Origin headers. |
| 478 | NavigateToURL(shell(), web_url); |
| 479 | { |
| 480 | RenderProcessHostWatcher web_process_killed( |
| 481 | web_rfh->GetProcess(), |
| 482 | RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT); |
| 483 | IPC::IpcSecurityTestUtil::PwnMessageReceived( |
| 484 | web_rfh->GetProcess()->GetChannel(), |
gzobqq | 1af4fad | 2016-01-30 13:07:06 | [diff] [blame] | 485 | ResourceHostMsg_RequestResource(web_rfh->GetRoutingID(), |
| 486 | kRequestIdNotPreviouslyUsed, |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 487 | invalid_scheme_origin_msg)); |
| 488 | web_process_killed.Wait(); |
| 489 | } |
| 490 | } |
| 491 | |
gzobqq | 1af4fad | 2016-01-30 13:07:06 | [diff] [blame] | 492 | // Renderer process should not be able to create multiple requests with the same |
| 493 | // id. |
| 494 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, InvalidRequestId) { |
| 495 | // Existing loader in pending_loaders_. |
| 496 | TryCreateDuplicateRequestIds(shell(), false); |
| 497 | // Existing loader in blocked_loaders_map_. |
| 498 | TryCreateDuplicateRequestIds(shell(), true); |
| 499 | } |
| 500 | |
nasko | 402cc29 | 2016-03-11 09:16:11 | [diff] [blame] | 501 | // Test that receiving a commit with incorrect origin properly terminates the |
| 502 | // renderer process. |
| 503 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, MismatchedOriginOnCommit) { |
| 504 | GURL start_url(embedded_test_server()->GetURL("/title1.html")); |
| 505 | EXPECT_TRUE(NavigateToURL(shell(), start_url)); |
| 506 | |
| 507 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
| 508 | ->GetFrameTree() |
| 509 | ->root(); |
| 510 | |
| 511 | // Setup an URL which will never commit, allowing this test to send its own, |
| 512 | // malformed, commit message. |
| 513 | GURL url(embedded_test_server()->GetURL("/title2.html")); |
| 514 | NavigationStallDelegate stall_delegate(url); |
| 515 | ResourceDispatcherHost::Get()->SetDelegate(&stall_delegate); |
| 516 | |
| 517 | // Use LoadURL, as the test shouldn't wait for navigation commit. |
| 518 | NavigationController& controller = shell()->web_contents()->GetController(); |
| 519 | controller.LoadURL(url, Referrer(), ui::PAGE_TRANSITION_LINK, std::string()); |
| 520 | EXPECT_NE(nullptr, controller.GetPendingEntry()); |
| 521 | EXPECT_EQ(url, controller.GetPendingEntry()->GetURL()); |
| 522 | |
| 523 | RenderProcessHostWatcher exit_observer( |
| 524 | root->current_frame_host()->GetProcess(), |
| 525 | RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT); |
| 526 | |
| 527 | // Create commit params with different origins in params.url and |
| 528 | // params.origin. |
| 529 | FrameHostMsg_DidCommitProvisionalLoad_Params params; |
| 530 | params.page_id = 0; |
| 531 | params.nav_entry_id = 0; |
| 532 | params.did_create_new_entry = false; |
| 533 | params.url = url; |
| 534 | params.transition = ui::PAGE_TRANSITION_LINK; |
| 535 | params.should_update_history = false; |
| 536 | params.gesture = NavigationGestureAuto; |
| 537 | params.was_within_same_page = false; |
clamy | 432acb2 | 2016-04-15 19:41:43 | [diff] [blame] | 538 | params.method = "GET"; |
nasko | 402cc29 | 2016-03-11 09:16:11 | [diff] [blame] | 539 | params.page_state = PageState::CreateFromURL(url); |
| 540 | params.origin = url::Origin(GURL("http://bar.com/")); |
| 541 | |
| 542 | FrameHostMsg_DidCommitProvisionalLoad msg( |
| 543 | root->current_frame_host()->routing_id(), params); |
| 544 | IPC::IpcSecurityTestUtil::PwnMessageReceived( |
| 545 | root->current_frame_host()->GetProcess()->GetChannel(), msg); |
| 546 | |
| 547 | // When the IPC message is received and validation fails, the process is |
| 548 | // terminated. However, the notification for that should be processed in a |
| 549 | // separate task of the message loop, so ensure that the process is still |
| 550 | // considered alive. |
| 551 | EXPECT_TRUE(root->current_frame_host()->GetProcess()->HasConnection()); |
| 552 | |
| 553 | exit_observer.Wait(); |
| 554 | EXPECT_FALSE(exit_observer.did_exit_normally()); |
clamy | eeb96fd | 2016-03-23 12:31:47 | [diff] [blame] | 555 | ResourceDispatcherHost::Get()->SetDelegate(nullptr); |
nasko | 402cc29 | 2016-03-11 09:16:11 | [diff] [blame] | 556 | } |
| 557 | |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 558 | } // namespace content |