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

Side by Side Diff: sandbox/win/src/lpc_policy_test.cc

Issue 2726733003: CSRSS lockdown: destroy CSRSS heap (Closed)
Patch Set: refactor heap code to heap_helper, add some explicit tests of these heap_helper functions Created 3 years, 9 months 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 // These tests have been added to specifically tests issues arising from (A)LPC 5 // These tests have been added to specifically tests issues arising from (A)LPC
6 // lock down. 6 // lock down.
7 7
8 #include <algorithm> 8 #include <algorithm>
9 #include <cctype> 9 #include <cctype>
10 10
11 #include <windows.h> 11 #include <windows.h>
12 #include <winioctl.h> 12 #include <winioctl.h>
13 13
14 #include "base/win/windows_version.h" 14 #include "base/win/windows_version.h"
15 #include "sandbox/win/src/heap_helper.h"
15 #include "sandbox/win/src/sandbox.h" 16 #include "sandbox/win/src/sandbox.h"
16 #include "sandbox/win/src/sandbox_factory.h" 17 #include "sandbox/win/src/sandbox_factory.h"
17 #include "sandbox/win/src/sandbox_policy.h" 18 #include "sandbox/win/src/sandbox_policy.h"
18 #include "sandbox/win/tests/common/controller.h" 19 #include "sandbox/win/tests/common/controller.h"
19 #include "sandbox/win/tests/common/test_utils.h" 20 #include "sandbox/win/tests/common/test_utils.h"
20 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
21 22
22 namespace sandbox { 23 namespace sandbox {
23 24
24 // Converts LCID to std::wstring for passing to sbox tests. 25 // Converts LCID to std::wstring for passing to sbox tests.
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 wchar_t locale_name[LOCALE_NAME_MAX_LENGTH] = {0}; 143 wchar_t locale_name[LOCALE_NAME_MAX_LENGTH] = {0};
143 EXPECT_NE(0, GetUserDefaultLocaleName_func( 144 EXPECT_NE(0, GetUserDefaultLocaleName_func(
144 locale_name, LOCALE_NAME_MAX_LENGTH * sizeof(wchar_t))); 145 locale_name, LOCALE_NAME_MAX_LENGTH * sizeof(wchar_t)));
145 EXPECT_NE(0U, wcsnlen(locale_name, LOCALE_NAME_MAX_LENGTH)); 146 EXPECT_NE(0U, wcsnlen(locale_name, LOCALE_NAME_MAX_LENGTH));
146 std::wstring cmd = 147 std::wstring cmd =
147 L"Lpc_GetUserDefaultLocaleName " + std::wstring(locale_name); 148 L"Lpc_GetUserDefaultLocaleName " + std::wstring(locale_name);
148 TestRunner runner; 149 TestRunner runner;
149 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(cmd.c_str())); 150 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(cmd.c_str()));
150 } 151 }
151 152
153 // Closing ALPC port can invalidate its heap.
154 // Test that all heaps are valid.
155 SBOX_TESTS_COMMAND int Lpc_TestValidProcessHeaps(int argc, wchar_t** argv) {
156 if (argc != 0)
157 return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND;
158 // Retrieves the number of heaps in the current process.
159 DWORD number_of_heaps = ::GetProcessHeaps(0, NULL);
160 // Try to retrieve a handle to all the heaps owned by this process. Returns
161 // false if the number of heaps has changed.
162 //
163 // This is inherently racy as is, but it's not something that we observe a lot
164 // in Chrome, the heaps tend to be created at startup only.
165 std::unique_ptr<HANDLE[]> all_heaps(new HANDLE[number_of_heaps]);
166 if (::GetProcessHeaps(number_of_heaps, all_heaps.get()) != number_of_heaps)
167 return SBOX_TEST_FAILED;
168
169 for (size_t i = 0; i < number_of_heaps; ++i) {
170 HANDLE handle = all_heaps[i];
171 if (!HeapLock(handle)) {
172 return SBOX_TEST_FAILED;
173 }
174
175 if (!HeapUnlock(handle)) {
176 return SBOX_TEST_FAILED;
177 }
178 }
179 return SBOX_TEST_SUCCEEDED;
180 }
181
182 TEST(LpcPolicyTest, TestValidProcessHeaps) {
183 TestRunner runner;
184 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"Lpc_TestValidProcessHeaps"));
185 }
186
187 // All processes should have a shared heap with csrss.exe. This test ensures
188 // that this heap can be found.
189 TEST(LpcPolicyTest, TestCanFindCsrPortHeap) {
190 HANDLE csr_port_handle = sandbox::FindCsrPortHeap();
191 EXPECT_NE(nullptr, csr_port_handle);
192 }
193
194 TEST(LpcPolicyTest, TestHeapFlags) {
Will Harris 2017/03/22 19:21:49 nice test!
liamjm (20p) 2017/04/14 17:27:20 Acknowledged.
195 // Windows does not support callers supplying arbritary flag values. So we
196 // write some non-trivial value to reduce the chance we match this in random
197 // data.
198 DWORD flags = 0x41007;
199 HANDLE heap = HeapCreate(flags, 0, 0);
200 EXPECT_NE(nullptr, heap);
201 DWORD actual_flags = sandbox::HeapFlags(heap);
202 EXPECT_EQ(flags, actual_flags);
203 EXPECT_TRUE(HeapDestroy(heap));
204 }
205
152 } // namespace sandbox 206 } // namespace sandbox
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698