OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #include "chrome_frame/module_utils.h" | 5 #include "chrome_frame/module_utils.h" |
6 | 6 |
7 #include "base/scoped_handle.h" | 7 #include "base/scoped_handle.h" |
8 #include "base/shared_memory.h" | 8 #include "base/shared_memory.h" |
| 9 #include "base/sys_info.h" |
9 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
10 #include "base/version.h" | 11 #include "base/version.h" |
| 12 #include "chrome_frame/test/chrome_frame_test_utils.h" |
11 #include "gtest/gtest.h" | 13 #include "gtest/gtest.h" |
12 | 14 |
13 extern "C" IMAGE_DOS_HEADER __ImageBase; | 15 extern "C" IMAGE_DOS_HEADER __ImageBase; |
14 | 16 |
15 const char kMockVersionString[] = "42.42.42.42"; | 17 const char kMockVersionString[] = "42.42.42.42"; |
16 const char kMockVersionString2[] = "133.33.33.7"; | 18 const char kMockVersionString2[] = "133.33.33.7"; |
17 | 19 |
18 const HMODULE kMockModuleHandle = reinterpret_cast<HMODULE>(42); | 20 const HMODULE kMockModuleHandle = reinterpret_cast<HMODULE>(42); |
19 const HMODULE kMockModuleHandle2 = reinterpret_cast<HMODULE>(43); | 21 const HMODULE kMockModuleHandle2 = reinterpret_cast<HMODULE>(43); |
20 | 22 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 | 63 |
62 virtual HMODULE LoadVersionedModule() { | 64 virtual HMODULE LoadVersionedModule() { |
63 return kMockModuleHandle2; | 65 return kMockModuleHandle2; |
64 } | 66 } |
65 | 67 |
66 virtual Version* GetCurrentModuleVersion() { | 68 virtual Version* GetCurrentModuleVersion() { |
67 return Version::GetVersionFromString(kMockVersionString2); | 69 return Version::GetVersionFromString(kMockVersionString2); |
68 } | 70 } |
69 }; | 71 }; |
70 | 72 |
| 73 class MockDllRedirectorNoPermissions : public MockDllRedirector { |
| 74 public: |
| 75 explicit MockDllRedirectorNoPermissions(const char* beacon_name) |
| 76 : MockDllRedirector(beacon_name) {} |
| 77 |
| 78 virtual bool BuildSecurityAttributesForLock( |
| 79 ATL::CSecurityAttributes* sec_attr) { |
| 80 return false; |
| 81 } |
| 82 |
| 83 virtual bool SetFileMappingToReadOnly(base::SharedMemoryHandle mapping) { |
| 84 return true; |
| 85 } |
| 86 }; |
| 87 |
71 class DllRedirectorTest : public testing::Test { | 88 class DllRedirectorTest : public testing::Test { |
72 public: | 89 public: |
73 virtual void SetUp() { | 90 virtual void SetUp() { |
74 shared_memory_.reset(new base::SharedMemory); | 91 shared_memory_.reset(new base::SharedMemory); |
75 mock_version_.reset(Version::GetVersionFromString(kMockVersionString)); | 92 mock_version_.reset(Version::GetVersionFromString(kMockVersionString)); |
76 mock_version2_.reset(Version::GetVersionFromString(kMockVersionString2)); | 93 mock_version2_.reset(Version::GetVersionFromString(kMockVersionString2)); |
77 } | 94 } |
78 | 95 |
79 virtual void TearDown() { | 96 virtual void TearDown() { |
80 CloseBeacon(); | 97 CloseBeacon(); |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 // The redirector should fail to read the version number and defer to | 300 // The redirector should fail to read the version number and defer to |
284 // its own version. | 301 // its own version. |
285 scoped_ptr<MockDllRedirector> first_redirector( | 302 scoped_ptr<MockDllRedirector> first_redirector( |
286 new MockDllRedirector(kTestVersionBeaconName)); | 303 new MockDllRedirector(kTestVersionBeaconName)); |
287 EXPECT_TRUE(first_redirector->RegisterAsFirstCFModule()); | 304 EXPECT_TRUE(first_redirector->RegisterAsFirstCFModule()); |
288 | 305 |
289 HMODULE first_module = first_redirector->GetFirstModule(); | 306 HMODULE first_module = first_redirector->GetFirstModule(); |
290 EXPECT_EQ(reinterpret_cast<HMODULE>(&__ImageBase), first_module); | 307 EXPECT_EQ(reinterpret_cast<HMODULE>(&__ImageBase), first_module); |
291 } | 308 } |
292 | 309 |
| 310 // TODO(robertshield): These tests rely on simulating access checks from a low |
| 311 // integrity process using impersonation. This may not be exactly identical to |
| 312 // actually having a separate low integrity process. |
| 313 TEST_F(DllRedirectorTest, LowIntegrityAccess) { |
| 314 scoped_ptr<MockDllRedirector> first_redirector( |
| 315 new MockDllRedirector(kTestVersionBeaconName)); |
| 316 EXPECT_TRUE(first_redirector->RegisterAsFirstCFModule()); |
| 317 |
| 318 // Ensure that we can acquire the mutex from medium integrity: |
| 319 { |
| 320 base::SharedMemory shared_memory(ASCIIToWide(kTestVersionBeaconName)); |
| 321 bool mutex_locked = shared_memory.Lock(kWaitTestTimeout, NULL); |
| 322 EXPECT_TRUE(mutex_locked); |
| 323 |
| 324 // Ensure that the shared memory is read-only: |
| 325 EXPECT_FALSE(shared_memory.Open(kTestVersionBeaconName, false)); |
| 326 shared_memory.Close(); |
| 327 EXPECT_TRUE(shared_memory.Open(kTestVersionBeaconName, true)); |
| 328 shared_memory.Close(); |
| 329 |
| 330 if (mutex_locked) |
| 331 shared_memory.Unlock(); |
| 332 } |
| 333 |
| 334 int32 major_version, minor_version, fix_version; |
| 335 base::SysInfo::OperatingSystemVersionNumbers(&major_version, |
| 336 &minor_version, |
| 337 &fix_version); |
| 338 if (major_version >= 6) { |
| 339 // Now move to low integrity |
| 340 chrome_frame_test::LowIntegrityToken low_integrity_token; |
| 341 ASSERT_TRUE(low_integrity_token.Impersonate()); |
| 342 |
| 343 // Ensure that we can also acquire the mutex from low integrity. |
| 344 base::SharedMemory shared_memory(ASCIIToWide(kTestVersionBeaconName)); |
| 345 bool mutex_locked = shared_memory.Lock(kWaitTestTimeout, NULL); |
| 346 EXPECT_TRUE(mutex_locked); |
| 347 |
| 348 // Ensure that the shared memory is read-only: |
| 349 EXPECT_FALSE(shared_memory.Open(kTestVersionBeaconName, false)); |
| 350 shared_memory.Close(); |
| 351 EXPECT_TRUE(shared_memory.Open(kTestVersionBeaconName, true)); |
| 352 shared_memory.Close(); |
| 353 |
| 354 if (mutex_locked) |
| 355 shared_memory.Unlock(); |
| 356 } |
| 357 } |
| 358 |
| 359 TEST_F(DllRedirectorTest, LowIntegrityAccessDenied) { |
| 360 // Run this test with a mock DllRedirector that doesn't set permissions |
| 361 // on the shared memory. |
| 362 scoped_ptr<MockDllRedirectorNoPermissions> first_redirector( |
| 363 new MockDllRedirectorNoPermissions(kTestVersionBeaconName)); |
| 364 EXPECT_TRUE(first_redirector->RegisterAsFirstCFModule()); |
| 365 |
| 366 // Ensure that we can acquire the mutex from medium integrity: |
| 367 { |
| 368 base::SharedMemory shared_memory(ASCIIToWide(kTestVersionBeaconName)); |
| 369 bool mutex_locked = shared_memory.Lock(kWaitTestTimeout, NULL); |
| 370 EXPECT_TRUE(mutex_locked); |
| 371 |
| 372 // We should be able to open the memory as read/write. |
| 373 EXPECT_TRUE(shared_memory.Open(kTestVersionBeaconName, false)); |
| 374 shared_memory.Close(); |
| 375 |
| 376 if (mutex_locked) |
| 377 shared_memory.Unlock(); |
| 378 } |
| 379 |
| 380 int32 major_version, minor_version, fix_version; |
| 381 base::SysInfo::OperatingSystemVersionNumbers(&major_version, |
| 382 &minor_version, |
| 383 &fix_version); |
| 384 if (major_version >= 6) { |
| 385 // Now move to low integrity |
| 386 chrome_frame_test::LowIntegrityToken low_integrity_token; |
| 387 low_integrity_token.Impersonate(); |
| 388 |
| 389 // Ensure that we can't acquire the mutex without having set the |
| 390 // Low Integrity ACE in the SACL. |
| 391 base::SharedMemory shared_memory(ASCIIToWide(kTestVersionBeaconName)); |
| 392 bool mutex_locked = shared_memory.Lock(kWaitTestTimeout, NULL); |
| 393 EXPECT_FALSE(mutex_locked); |
| 394 |
| 395 // We shouldn't be able to open the memory. |
| 396 EXPECT_FALSE(shared_memory.Open(kTestVersionBeaconName, false)); |
| 397 shared_memory.Close(); |
| 398 |
| 399 if (mutex_locked) |
| 400 shared_memory.Unlock(); |
| 401 } |
| 402 } |
| 403 |
OLD | NEW |