OLD | NEW |
1 // Copyright 2015 The Crashpad Authors. All rights reserved. | 1 // Copyright 2015 The Crashpad Authors. All rights reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 #include "util/win/process_info.h" | 15 #include "util/win/process_info.h" |
16 | 16 |
17 #include <dbghelp.h> | 17 #include <dbghelp.h> |
18 #include <intrin.h> | 18 #include <intrin.h> |
19 #include <wchar.h> | 19 #include <wchar.h> |
20 | 20 |
21 #include "base/files/file_path.h" | 21 #include "base/files/file_path.h" |
22 #include "base/memory/scoped_ptr.h" | 22 #include "base/memory/scoped_ptr.h" |
| 23 #include "base/rand_util.h" |
| 24 #include "base/strings/stringprintf.h" |
| 25 #include "base/strings/utf_string_conversions.h" |
23 #include "build/build_config.h" | 26 #include "build/build_config.h" |
24 #include "gtest/gtest.h" | 27 #include "gtest/gtest.h" |
| 28 #include "test/scoped_temp_dir.h" |
25 #include "test/paths.h" | 29 #include "test/paths.h" |
26 #include "test/win/child_launcher.h" | 30 #include "test/win/child_launcher.h" |
27 #include "util/file/file_io.h" | 31 #include "util/file/file_io.h" |
28 #include "util/misc/uuid.h" | 32 #include "util/misc/uuid.h" |
29 #include "util/win/scoped_handle.h" | 33 #include "util/win/scoped_handle.h" |
30 | 34 |
31 namespace crashpad { | 35 namespace crashpad { |
32 namespace test { | 36 namespace test { |
33 namespace { | 37 namespace { |
34 | 38 |
(...skipping 468 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
503 current_process, no_access, into.get(), kBlockSize, &bytes_read)); | 507 current_process, no_access, into.get(), kBlockSize, &bytes_read)); |
504 EXPECT_FALSE(ReadProcessMemory( | 508 EXPECT_FALSE(ReadProcessMemory( |
505 current_process, reserve_region, into.get(), kBlockSize, &bytes_read)); | 509 current_process, reserve_region, into.get(), kBlockSize, &bytes_read)); |
506 EXPECT_FALSE(ReadProcessMemory(current_process, | 510 EXPECT_FALSE(ReadProcessMemory(current_process, |
507 reserve_region, | 511 reserve_region, |
508 into.get(), | 512 into.get(), |
509 kBlockSize * 6, | 513 kBlockSize * 6, |
510 &bytes_read)); | 514 &bytes_read)); |
511 } | 515 } |
512 | 516 |
| 517 struct ScopedRegistryKeyCloseTraits { |
| 518 static HKEY InvalidValue() { |
| 519 return nullptr; |
| 520 } |
| 521 static void Free(HKEY key) { |
| 522 RegCloseKey(key); |
| 523 } |
| 524 }; |
| 525 using ScopedRegistryKey = |
| 526 base::ScopedGeneric<HKEY, ScopedRegistryKeyCloseTraits>; |
| 527 |
| 528 TEST(ProcessInfo, Handles) { |
| 529 ScopedTempDir temp_dir; |
| 530 |
| 531 ScopedFileHandle file(LoggingOpenFileForWrite( |
| 532 temp_dir.path().Append(FILE_PATH_LITERAL("test_file")), |
| 533 FileWriteMode::kTruncateOrCreate, |
| 534 FilePermissions::kWorldReadable)); |
| 535 ASSERT_TRUE(file.is_valid()); |
| 536 |
| 537 SECURITY_ATTRIBUTES security_attributes = {0}; |
| 538 security_attributes.bInheritHandle = true; |
| 539 ScopedFileHandle inherited_file(CreateFile(L"CONOUT$", |
| 540 GENERIC_WRITE, |
| 541 0, |
| 542 &security_attributes, |
| 543 OPEN_EXISTING, |
| 544 FILE_ATTRIBUTE_NORMAL, |
| 545 nullptr)); |
| 546 ASSERT_TRUE(inherited_file.is_valid()); |
| 547 |
| 548 HKEY key; |
| 549 ASSERT_EQ(ERROR_SUCCESS, |
| 550 RegOpenKeyEx( |
| 551 HKEY_CURRENT_USER, L"SOFTWARE\\Microsoft", 0, KEY_READ, &key)); |
| 552 ScopedRegistryKey scoped_key(key); |
| 553 ASSERT_TRUE(scoped_key.is_valid()); |
| 554 |
| 555 std::wstring mapping_name = |
| 556 base::UTF8ToUTF16(base::StringPrintf("Global\\test_mapping_%d_%I64x", |
| 557 GetCurrentProcessId(), |
| 558 base::RandUint64())); |
| 559 ScopedKernelHANDLE mapping(CreateFileMapping(INVALID_HANDLE_VALUE, |
| 560 nullptr, |
| 561 PAGE_READWRITE, |
| 562 0, |
| 563 1024, |
| 564 mapping_name.c_str())); |
| 565 ASSERT_TRUE(mapping.is_valid()); |
| 566 |
| 567 ProcessInfo info; |
| 568 info.Initialize(GetCurrentProcess()); |
| 569 bool found_file_handle = false; |
| 570 bool found_inherited_file_handle = false; |
| 571 bool found_key_handle = false; |
| 572 bool found_mapping_handle = false; |
| 573 for (auto handle : info.Handles()) { |
| 574 if (reinterpret_cast<uint32_t>(file.get()) == handle.handle) { |
| 575 EXPECT_FALSE(found_file_handle); |
| 576 found_file_handle = true; |
| 577 EXPECT_EQ(L"File", handle.type_name); |
| 578 EXPECT_EQ(1, handle.handle_count); |
| 579 EXPECT_NE(0u, handle.pointer_count); |
| 580 EXPECT_EQ(STANDARD_RIGHTS_READ | STANDARD_RIGHTS_WRITE | SYNCHRONIZE, |
| 581 handle.granted_access & STANDARD_RIGHTS_ALL); |
| 582 EXPECT_EQ(0, handle.attributes); |
| 583 } |
| 584 if (reinterpret_cast<uint32_t>(inherited_file.get()) == handle.handle) { |
| 585 EXPECT_FALSE(found_inherited_file_handle); |
| 586 found_inherited_file_handle = true; |
| 587 EXPECT_EQ(L"File", handle.type_name); |
| 588 EXPECT_EQ(1, handle.handle_count); |
| 589 EXPECT_NE(0u, handle.pointer_count); |
| 590 EXPECT_EQ(STANDARD_RIGHTS_READ | STANDARD_RIGHTS_WRITE | SYNCHRONIZE, |
| 591 handle.granted_access & STANDARD_RIGHTS_ALL); |
| 592 |
| 593 // OBJ_INHERIT from ntdef.h, but including that conflicts with other |
| 594 // headers. |
| 595 const int kObjInherit = 0x2; |
| 596 EXPECT_EQ(kObjInherit, handle.attributes); |
| 597 } |
| 598 if (reinterpret_cast<uint32_t>(scoped_key.get()) == handle.handle) { |
| 599 EXPECT_FALSE(found_key_handle); |
| 600 found_key_handle = true; |
| 601 EXPECT_EQ(L"Key", handle.type_name); |
| 602 EXPECT_EQ(1, handle.handle_count); |
| 603 EXPECT_NE(0u, handle.pointer_count); |
| 604 EXPECT_EQ(STANDARD_RIGHTS_READ, |
| 605 handle.granted_access & STANDARD_RIGHTS_ALL); |
| 606 EXPECT_EQ(0, handle.attributes); |
| 607 } |
| 608 if (reinterpret_cast<uint32_t>(mapping.get()) == handle.handle) { |
| 609 EXPECT_FALSE(found_mapping_handle); |
| 610 found_mapping_handle = true; |
| 611 EXPECT_EQ(L"Section", handle.type_name); |
| 612 EXPECT_EQ(1, handle.handle_count); |
| 613 EXPECT_NE(0u, handle.pointer_count); |
| 614 EXPECT_EQ(DELETE | READ_CONTROL | WRITE_DAC | WRITE_OWNER | |
| 615 STANDARD_RIGHTS_READ | STANDARD_RIGHTS_WRITE, |
| 616 handle.granted_access & STANDARD_RIGHTS_ALL); |
| 617 EXPECT_EQ(0, handle.attributes); |
| 618 } |
| 619 } |
| 620 EXPECT_TRUE(found_file_handle); |
| 621 EXPECT_TRUE(found_key_handle); |
| 622 EXPECT_TRUE(found_mapping_handle); |
| 623 } |
| 624 |
513 } // namespace | 625 } // namespace |
514 } // namespace test | 626 } // namespace test |
515 } // namespace crashpad | 627 } // namespace crashpad |
OLD | NEW |