lpc_policy_test.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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
  3. // found in the LICENSE file.
  4. // These tests have been added to specifically tests issues arising from (A)LPC
  5. // lock down.
  6. #include <algorithm>
  7. #include <cctype>
  8. #include <windows.h>
  9. #include <winioctl.h>
  10. #include "base/win/windows_version.h"
  11. #include "build/build_config.h"
  12. #include "sandbox/win/src/heap_helper.h"
  13. #include "sandbox/win/src/sandbox.h"
  14. #include "sandbox/win/src/sandbox_factory.h"
  15. #include "sandbox/win/src/sandbox_policy.h"
  16. #include "sandbox/win/tests/common/controller.h"
  17. #include "sandbox/win/tests/common/test_utils.h"
  18. #include "testing/gtest/include/gtest/gtest.h"
  19. namespace sandbox {
  20. namespace {
  21. bool CsrssDisconnectSupported() {
  22. // This functionality has not been verified on versions before Win10.
  23. if (base::win::GetVersion() < base::win::Version::WIN10)
  24. return false;
  25. // Does not work on 32-bit on x64 (ie Wow64).
  26. return (!base::win::OSInfo::GetInstance()->IsWowX86OnAMD64());
  27. }
  28. } // namespace
  29. // Converts LCID to std::wstring for passing to sbox tests.
  30. std::wstring LcidToWString(LCID lcid) {
  31. wchar_t buff[10] = {0};
  32. int res = swprintf_s(buff, sizeof(buff) / sizeof(buff[0]), L"%08x", lcid);
  33. if (-1 != res) {
  34. return std::wstring(buff);
  35. }
  36. return std::wstring();
  37. }
  38. // Converts LANGID to std::wstring for passing to sbox tests.
  39. std::wstring LangidToWString(LANGID langid) {
  40. wchar_t buff[10] = {0};
  41. int res = swprintf_s(buff, sizeof(buff) / sizeof(buff[0]), L"%04x", langid);
  42. if (-1 != res) {
  43. return std::wstring(buff);
  44. }
  45. return std::wstring();
  46. }
  47. SBOX_TESTS_COMMAND int Lpc_GetUserDefaultLangID(int argc, wchar_t** argv) {
  48. if (argc != 1)
  49. return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND;
  50. std::wstring expected_langid_string(argv[0]);
  51. // This will cause an exception if not warmed up suitably.
  52. LANGID langid = ::GetUserDefaultLangID();
  53. std::wstring langid_string = LangidToWString(langid);
  54. if (0 == wcsncmp(langid_string.c_str(), expected_langid_string.c_str(), 4)) {
  55. return SBOX_TEST_SUCCEEDED;
  56. }
  57. return SBOX_TEST_FAILED;
  58. }
  59. TEST(LpcPolicyTest, GetUserDefaultLangID) {
  60. LANGID langid = ::GetUserDefaultLangID();
  61. std::wstring cmd = L"Lpc_GetUserDefaultLangID " + LangidToWString(langid);
  62. TestRunner runner;
  63. EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(cmd.c_str()));
  64. }
  65. SBOX_TESTS_COMMAND int Lpc_GetUserDefaultLCID(int argc, wchar_t** argv) {
  66. if (argc != 1)
  67. return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND;
  68. std::wstring expected_lcid_string(argv[0]);
  69. // This will cause an exception if not warmed up suitably.
  70. LCID lcid = ::GetUserDefaultLCID();
  71. std::wstring lcid_string = LcidToWString(lcid);
  72. if (0 == wcsncmp(lcid_string.c_str(), expected_lcid_string.c_str(), 8)) {
  73. return SBOX_TEST_SUCCEEDED;
  74. }
  75. return SBOX_TEST_FAILED;
  76. }
  77. TEST(LpcPolicyTest, GetUserDefaultLCID) {
  78. LCID lcid = ::GetUserDefaultLCID();
  79. std::wstring cmd = L"Lpc_GetUserDefaultLCID " + LcidToWString(lcid);
  80. TestRunner runner;
  81. EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(cmd.c_str()));
  82. }
  83. // GetUserDefaultLocaleName is not available on WIN XP. So we'll
  84. // load it on-the-fly.
  85. const wchar_t kKernel32DllName[] = L"kernel32.dll";
  86. typedef int(WINAPI* GetUserDefaultLocaleNameFunction)(LPWSTR lpLocaleName,
  87. int cchLocaleName);
  88. SBOX_TESTS_COMMAND int Lpc_GetUserDefaultLocaleName(int argc, wchar_t** argv) {
  89. if (argc != 1)
  90. return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND;
  91. std::wstring expected_locale_name(argv[0]);
  92. static GetUserDefaultLocaleNameFunction GetUserDefaultLocaleName_func =
  93. nullptr;
  94. if (!GetUserDefaultLocaleName_func) {
  95. // GetUserDefaultLocaleName is not available on WIN XP. So we'll
  96. // load it on-the-fly.
  97. HMODULE kernel32_dll = ::GetModuleHandle(kKernel32DllName);
  98. if (!kernel32_dll) {
  99. return SBOX_TEST_FAILED;
  100. }
  101. GetUserDefaultLocaleName_func =
  102. reinterpret_cast<GetUserDefaultLocaleNameFunction>(
  103. GetProcAddress(kernel32_dll, "GetUserDefaultLocaleName"));
  104. if (!GetUserDefaultLocaleName_func) {
  105. return SBOX_TEST_FAILED;
  106. }
  107. }
  108. wchar_t locale_name[LOCALE_NAME_MAX_LENGTH] = {0};
  109. // This will cause an exception if not warmed up suitably.
  110. int ret = GetUserDefaultLocaleName_func(
  111. locale_name, LOCALE_NAME_MAX_LENGTH * sizeof(wchar_t));
  112. if (!ret) {
  113. return SBOX_TEST_FAILED;
  114. }
  115. if (!wcsnlen(locale_name, LOCALE_NAME_MAX_LENGTH)) {
  116. return SBOX_TEST_FAILED;
  117. }
  118. if (0 == wcsncmp(locale_name, expected_locale_name.c_str(),
  119. LOCALE_NAME_MAX_LENGTH)) {
  120. return SBOX_TEST_SUCCEEDED;
  121. }
  122. return SBOX_TEST_FAILED;
  123. }
  124. TEST(LpcPolicyTest, GetUserDefaultLocaleName) {
  125. static GetUserDefaultLocaleNameFunction GetUserDefaultLocaleName_func =
  126. nullptr;
  127. if (!GetUserDefaultLocaleName_func) {
  128. // GetUserDefaultLocaleName is not available on WIN XP. So we'll
  129. // load it on-the-fly.
  130. HMODULE kernel32_dll = ::GetModuleHandle(kKernel32DllName);
  131. EXPECT_NE(nullptr, kernel32_dll);
  132. GetUserDefaultLocaleName_func =
  133. reinterpret_cast<GetUserDefaultLocaleNameFunction>(
  134. GetProcAddress(kernel32_dll, "GetUserDefaultLocaleName"));
  135. EXPECT_NE(nullptr, GetUserDefaultLocaleName_func);
  136. }
  137. wchar_t locale_name[LOCALE_NAME_MAX_LENGTH] = {0};
  138. EXPECT_NE(0, GetUserDefaultLocaleName_func(
  139. locale_name, LOCALE_NAME_MAX_LENGTH * sizeof(wchar_t)));
  140. EXPECT_NE(0U, wcsnlen(locale_name, LOCALE_NAME_MAX_LENGTH));
  141. std::wstring cmd =
  142. L"Lpc_GetUserDefaultLocaleName " + std::wstring(locale_name);
  143. TestRunner runner;
  144. EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(cmd.c_str()));
  145. }
  146. // Closing ALPC port can invalidate its heap.
  147. // Test that all heaps are valid.
  148. SBOX_TESTS_COMMAND int Lpc_TestValidProcessHeaps(int argc, wchar_t** argv) {
  149. if (argc != 0)
  150. return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND;
  151. // Retrieves the number of heaps in the current process.
  152. DWORD number_of_heaps = ::GetProcessHeaps(0, nullptr);
  153. // Try to retrieve a handle to all the heaps owned by this process. Returns
  154. // false if the number of heaps has changed.
  155. //
  156. // This is inherently racy as is, but it's not something that we observe a lot
  157. // in Chrome, the heaps tend to be created at startup only.
  158. std::unique_ptr<HANDLE[]> all_heaps(new HANDLE[number_of_heaps]);
  159. if (::GetProcessHeaps(number_of_heaps, all_heaps.get()) != number_of_heaps)
  160. return SBOX_TEST_FIRST_ERROR;
  161. for (size_t i = 0; i < number_of_heaps; ++i) {
  162. HANDLE handle = all_heaps[i];
  163. ULONG HeapInformation;
  164. bool result = HeapQueryInformation(handle, HeapCompatibilityInformation,
  165. &HeapInformation,
  166. sizeof(HeapInformation), nullptr);
  167. if (!result)
  168. return SBOX_TEST_SECOND_ERROR;
  169. }
  170. return SBOX_TEST_SUCCEEDED;
  171. }
  172. TEST(LpcPolicyTest, TestValidProcessHeaps) {
  173. TestRunner runner;
  174. EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"Lpc_TestValidProcessHeaps"));
  175. }
  176. // All processes should have a shared heap with csrss.exe. This test ensures
  177. // that this heap can be found.
  178. TEST(LpcPolicyTest, TestCanFindCsrPortHeap) {
  179. if (!CsrssDisconnectSupported()) {
  180. return;
  181. }
  182. HANDLE csr_port_handle = sandbox::FindCsrPortHeap();
  183. EXPECT_NE(nullptr, csr_port_handle);
  184. }
  185. // Fails on Windows ARM64: https://crbug.com/905328
  186. #if defined(ARCH_CPU_ARM64)
  187. #define MAYBE_TestHeapFlags DISABLED_TestHeapFlags
  188. #else
  189. #define MAYBE_TestHeapFlags TestHeapFlags
  190. #endif
  191. TEST(LpcPolicyTest, MAYBE_TestHeapFlags) {
  192. if (!CsrssDisconnectSupported()) {
  193. // This functionality has not been verified on versions before Win10.
  194. return;
  195. }
  196. // Windows does not support callers supplying arbritary flag values. So we
  197. // write some non-trivial value to reduce the chance we match this in random
  198. // data.
  199. DWORD flags = 0x41007;
  200. HANDLE heap = HeapCreate(flags, 0, 0);
  201. EXPECT_NE(nullptr, heap);
  202. DWORD actual_flags = 0;
  203. EXPECT_TRUE(sandbox::HeapFlags(heap, &actual_flags));
  204. EXPECT_EQ(flags, actual_flags);
  205. EXPECT_TRUE(HeapDestroy(heap));
  206. }
  207. } // namespace sandbox