1 // Copyright (c) 2011 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 "sandbox/win/src/win_utils.h"
6 
7 #include <windows.h>
8 
9 #include <psapi.h>
10 
11 #include <vector>
12 
13 #include "base/files/file_path.h"
14 #include "base/files/file_util.h"
15 #include "base/numerics/safe_conversions.h"
16 #include "base/path_service.h"
17 #include "base/win/scoped_handle.h"
18 #include "base/win/scoped_process_information.h"
19 #include "sandbox/win/src/nt_internals.h"
20 #include "sandbox/win/tests/common/test_utils.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 
23 namespace sandbox {
24 
25 namespace {
26 
27 class ScopedTerminateProcess {
28  public:
ScopedTerminateProcess(HANDLE process)29   ScopedTerminateProcess(HANDLE process) : process_(process) {}
30 
~ScopedTerminateProcess()31   ~ScopedTerminateProcess() { ::TerminateProcess(process_, 0); }
32 
33  private:
34   HANDLE process_;
35 };
36 
GetModuleList(HANDLE process,std::vector<HMODULE> * result)37 bool GetModuleList(HANDLE process, std::vector<HMODULE>* result) {
38   std::vector<HMODULE> modules(256);
39   DWORD size_needed = 0;
40   if (EnumProcessModules(
41           process, &modules[0],
42           base::checked_cast<DWORD>(modules.size() * sizeof(HMODULE)),
43           &size_needed)) {
44     result->assign(modules.begin(),
45                    modules.begin() + (size_needed / sizeof(HMODULE)));
46     return true;
47   }
48   modules.resize(size_needed / sizeof(HMODULE));
49   if (EnumProcessModules(
50           process, &modules[0],
51           base::checked_cast<DWORD>(modules.size() * sizeof(HMODULE)),
52           &size_needed)) {
53     result->assign(modules.begin(),
54                    modules.begin() + (size_needed / sizeof(HMODULE)));
55     return true;
56   }
57   return false;
58 }
59 
60 }  // namespace
61 
TEST(WinUtils,IsReparsePoint)62 TEST(WinUtils, IsReparsePoint) {
63   using sandbox::IsReparsePoint;
64 
65   // Create a temp file because we need write access to it.
66   wchar_t temp_directory[MAX_PATH];
67   wchar_t my_folder[MAX_PATH];
68   ASSERT_NE(::GetTempPath(MAX_PATH, temp_directory), 0u);
69   ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, my_folder), 0u);
70 
71   // Delete the file and create a directory instead.
72   ASSERT_TRUE(::DeleteFile(my_folder));
73   ASSERT_TRUE(::CreateDirectory(my_folder, nullptr));
74 
75   EXPECT_EQ(static_cast<DWORD>(ERROR_NOT_A_REPARSE_POINT),
76             IsReparsePoint(my_folder));
77 
78   std::wstring not_found = std::wstring(my_folder) + L"\\foo\\bar";
79   EXPECT_EQ(static_cast<DWORD>(ERROR_NOT_A_REPARSE_POINT),
80             IsReparsePoint(not_found));
81 
82   std::wstring new_file = std::wstring(my_folder) + L"\\foo";
83   EXPECT_EQ(static_cast<DWORD>(ERROR_NOT_A_REPARSE_POINT),
84             IsReparsePoint(new_file));
85 
86   // Replace the directory with a reparse point to %temp%.
87   HANDLE dir = ::CreateFile(my_folder, FILE_ALL_ACCESS,
88                             FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
89                             OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr);
90   EXPECT_NE(INVALID_HANDLE_VALUE, dir);
91 
92   std::wstring temp_dir_nt = std::wstring(L"\\??\\") + temp_directory;
93   EXPECT_TRUE(SetReparsePoint(dir, temp_dir_nt.c_str()));
94 
95   EXPECT_EQ(static_cast<DWORD>(ERROR_SUCCESS), IsReparsePoint(new_file));
96 
97   EXPECT_TRUE(DeleteReparsePoint(dir));
98   EXPECT_TRUE(::CloseHandle(dir));
99   EXPECT_TRUE(::RemoveDirectory(my_folder));
100 }
101 
TEST(WinUtils,SameObject)102 TEST(WinUtils, SameObject) {
103   using sandbox::SameObject;
104 
105   // Create a temp file because we need write access to it.
106   wchar_t temp_directory[MAX_PATH];
107   wchar_t my_folder[MAX_PATH];
108   ASSERT_NE(::GetTempPath(MAX_PATH, temp_directory), 0u);
109   ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, my_folder), 0u);
110 
111   // Delete the file and create a directory instead.
112   ASSERT_TRUE(::DeleteFile(my_folder));
113   ASSERT_TRUE(::CreateDirectory(my_folder, nullptr));
114 
115   std::wstring folder(my_folder);
116   std::wstring file_name = folder + L"\\foo.txt";
117   const ULONG kSharing = FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE;
118   base::win::ScopedHandle file(CreateFile(file_name.c_str(), GENERIC_WRITE,
119                                           kSharing, nullptr, CREATE_ALWAYS,
120                                           FILE_FLAG_DELETE_ON_CLOSE, nullptr));
121 
122   EXPECT_TRUE(file.IsValid());
123   std::wstring file_name_nt1 = std::wstring(L"\\??\\") + file_name;
124   std::wstring file_name_nt2 = std::wstring(L"\\??\\") + folder + L"\\FOO.txT";
125   EXPECT_TRUE(SameObject(file.Get(), file_name_nt1.c_str()));
126   EXPECT_TRUE(SameObject(file.Get(), file_name_nt2.c_str()));
127 
128   file.Close();
129   EXPECT_TRUE(::RemoveDirectory(my_folder));
130 }
131 
TEST(WinUtils,IsPipe)132 TEST(WinUtils, IsPipe) {
133   using sandbox::IsPipe;
134 
135   std::wstring pipe_name = L"\\??\\pipe\\mypipe";
136   EXPECT_TRUE(IsPipe(pipe_name));
137 
138   pipe_name = L"\\??\\PiPe\\mypipe";
139   EXPECT_TRUE(IsPipe(pipe_name));
140 
141   pipe_name = L"\\??\\pipe";
142   EXPECT_FALSE(IsPipe(pipe_name));
143 
144   pipe_name = L"\\??\\_pipe_\\mypipe";
145   EXPECT_FALSE(IsPipe(pipe_name));
146 
147   pipe_name = L"\\??\\ABCD\\mypipe";
148   EXPECT_FALSE(IsPipe(pipe_name));
149 
150   // Written as two strings to prevent trigraph '?' '?' '/'.
151   pipe_name =
152       L"/?"
153       L"?/pipe/mypipe";
154   EXPECT_FALSE(IsPipe(pipe_name));
155 
156   pipe_name = L"\\XX\\pipe\\mypipe";
157   EXPECT_FALSE(IsPipe(pipe_name));
158 
159   pipe_name = L"\\Device\\NamedPipe\\mypipe";
160   EXPECT_FALSE(IsPipe(pipe_name));
161 }
162 
TEST(WinUtils,NtStatusToWin32Error)163 TEST(WinUtils, NtStatusToWin32Error) {
164   using sandbox::GetLastErrorFromNtStatus;
165   EXPECT_EQ(static_cast<DWORD>(ERROR_SUCCESS),
166             GetLastErrorFromNtStatus(STATUS_SUCCESS));
167   EXPECT_EQ(static_cast<DWORD>(ERROR_NOT_SUPPORTED),
168             GetLastErrorFromNtStatus(STATUS_NOT_SUPPORTED));
169   EXPECT_EQ(static_cast<DWORD>(ERROR_ALREADY_EXISTS),
170             GetLastErrorFromNtStatus(STATUS_OBJECT_NAME_COLLISION));
171   EXPECT_EQ(static_cast<DWORD>(ERROR_ACCESS_DENIED),
172             GetLastErrorFromNtStatus(STATUS_ACCESS_DENIED));
173 }
174 
TEST(WinUtils,GetProcessBaseAddress)175 TEST(WinUtils, GetProcessBaseAddress) {
176   using sandbox::GetProcessBaseAddress;
177   STARTUPINFO start_info = {};
178   PROCESS_INFORMATION proc_info = {};
179   WCHAR command_line[] = L"notepad";
180   start_info.cb = sizeof(start_info);
181   start_info.dwFlags = STARTF_USESHOWWINDOW;
182   start_info.wShowWindow = SW_HIDE;
183   ASSERT_TRUE(::CreateProcessW(nullptr, command_line, nullptr, nullptr, false,
184                                CREATE_SUSPENDED, nullptr, nullptr, &start_info,
185                                &proc_info));
186   base::win::ScopedProcessInformation scoped_proc_info(proc_info);
187   ScopedTerminateProcess process_terminate(scoped_proc_info.process_handle());
188   void* base_address = GetProcessBaseAddress(scoped_proc_info.process_handle());
189   ASSERT_NE(nullptr, base_address);
190   ASSERT_NE(static_cast<DWORD>(-1),
191             ::ResumeThread(scoped_proc_info.thread_handle()));
192   ::WaitForInputIdle(scoped_proc_info.process_handle(), 1000);
193   ASSERT_NE(static_cast<DWORD>(-1),
194             ::SuspendThread(scoped_proc_info.thread_handle()));
195 
196   std::vector<HMODULE> modules;
197   // Compare against the loader's module list (which should now be initialized).
198   ASSERT_TRUE(GetModuleList(scoped_proc_info.process_handle(), &modules));
199   ASSERT_GT(modules.size(), 0U);
200   EXPECT_EQ(base_address, modules[0]);
201 }
202 
203 // This test requires an elevated prompt to setup.
TEST(WinUtils,ConvertToLongPath)204 TEST(WinUtils, ConvertToLongPath) {
205   // Test setup.
206   base::FilePath orig_path;
207   ASSERT_TRUE(base::PathService::Get(base::DIR_SYSTEM, &orig_path));
208   orig_path = orig_path.Append(L"calc.exe");
209 
210   base::FilePath temp_path;
211   ASSERT_TRUE(base::PathService::Get(base::DIR_PROGRAM_FILES, &temp_path));
212   temp_path = temp_path.Append(L"test_calc.exe");
213 
214   ASSERT_TRUE(base::CopyFile(orig_path, temp_path));
215   // No more asserts until cleanup.
216 
217   // WIN32 long path: "c:\Program Files\test_calc.exe"
218   wchar_t short_path[MAX_PATH] = {};
219   DWORD size =
220       ::GetShortPathNameW(temp_path.value().c_str(), short_path, MAX_PATH);
221   EXPECT_TRUE(size > 0 && size < MAX_PATH);
222   // WIN32 short path: "C:\PROGRA~1\TEST_C~1.exe"
223 
224   // Sanity check that we actually got a short path above!  Small chance
225   // it was disabled in the filesystem setup.
226   EXPECT_NE(temp_path.value().length(), ::wcslen(short_path));
227 
228   std::wstring short_form_native_path;
229   EXPECT_TRUE(sandbox::GetNtPathFromWin32Path(std::wstring(short_path),
230                                               &short_form_native_path));
231   // NT short path: "\Device\HarddiskVolume4\PROGRA~1\TEST_C~1.EXE"
232 
233   // Test 1: convert win32 short path to long:
234   std::wstring test1(short_path);
235   EXPECT_TRUE(sandbox::ConvertToLongPath(&test1));
236   EXPECT_TRUE(::wcsicmp(temp_path.value().c_str(), test1.c_str()) == 0);
237   // Expected result: "c:\Program Files\test_calc.exe"
238 
239   // Test 2: convert native short path to long:
240   std::wstring drive_letter = temp_path.value().substr(0, 3);
241   std::wstring test2(short_form_native_path);
242   EXPECT_TRUE(sandbox::ConvertToLongPath(&test2, &drive_letter));
243 
244   size_t index = short_form_native_path.find_first_of(
245       L'\\', ::wcslen(L"\\Device\\HarddiskVolume"));
246   EXPECT_TRUE(index != std::wstring::npos);
247   std::wstring expected_result = short_form_native_path.substr(0, index + 1);
248   expected_result.append(temp_path.value().substr(3));
249   EXPECT_TRUE(::wcsicmp(expected_result.c_str(), test2.c_str()) == 0);
250   // Expected result: "\Device\HarddiskVolumeX\Program Files\test_calc.exe"
251 
252   // clean up
253   EXPECT_TRUE(base::DeleteFileW(temp_path, false));
254 
255   return;
256 }
257 
258 }  // namespace sandbox
259