OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 "chrome/installer/util/scoped_token_privilege.h" |
| 6 |
| 7 #include <shlobj.h> |
| 8 #include <memory> |
| 9 |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 namespace installer { |
| 13 namespace { |
| 14 |
| 15 // The privilege tested in ScopeTokenPrivilege tests below. |
| 16 // Use SE_RESTORE_NAME as it is one of the many privileges that is available, |
| 17 // but not enabled by default on processes running at high integrity. |
| 18 constexpr wchar_t kTestedPrivilege[] = SE_RESTORE_NAME; |
| 19 |
| 20 // Returns true if the current process' token has privilege |privilege_name| |
| 21 // enabled. |
| 22 bool CurrentProcessHasPrivilege(const wchar_t* privilege_name) { |
| 23 HANDLE temp_handle; |
| 24 if (!::OpenProcessToken(::GetCurrentProcess(), TOKEN_QUERY, &temp_handle)) { |
| 25 ADD_FAILURE(); |
| 26 return false; |
| 27 } |
| 28 |
| 29 base::win::ScopedHandle token(temp_handle); |
| 30 |
| 31 // First get the size of the buffer needed for |privileges| below. |
| 32 DWORD size; |
| 33 EXPECT_FALSE( |
| 34 ::GetTokenInformation(token.Get(), TokenPrivileges, NULL, 0, &size)); |
| 35 |
| 36 std::unique_ptr<BYTE[]> privileges_bytes(new BYTE[size]); |
| 37 TOKEN_PRIVILEGES* privileges = |
| 38 reinterpret_cast<TOKEN_PRIVILEGES*>(privileges_bytes.get()); |
| 39 |
| 40 if (!::GetTokenInformation(token.Get(), TokenPrivileges, privileges, size, |
| 41 &size)) { |
| 42 ADD_FAILURE(); |
| 43 return false; |
| 44 } |
| 45 |
| 46 // There is no point getting a buffer to store more than |privilege_name|\0 as |
| 47 // anything longer will obviously not be equal to |privilege_name|. |
| 48 const DWORD desired_size = static_cast<DWORD>(wcslen(privilege_name)); |
| 49 const DWORD buffer_size = desired_size + 1; |
| 50 std::unique_ptr<wchar_t[]> name_buffer(new wchar_t[buffer_size]); |
| 51 for (int i = privileges->PrivilegeCount - 1; i >= 0; --i) { |
| 52 LUID_AND_ATTRIBUTES& luid_and_att = privileges->Privileges[i]; |
| 53 DWORD size = buffer_size; |
| 54 ::LookupPrivilegeName(NULL, &luid_and_att.Luid, name_buffer.get(), &size); |
| 55 if (size == desired_size && |
| 56 wcscmp(name_buffer.get(), privilege_name) == 0) { |
| 57 return luid_and_att.Attributes == SE_PRIVILEGE_ENABLED; |
| 58 } |
| 59 } |
| 60 return false; |
| 61 } |
| 62 |
| 63 } // namespace |
| 64 |
| 65 // Note: This test is only valid when run at high integrity (i.e. it will fail |
| 66 // at medium integrity). |
| 67 TEST(ScopedTokenPrivilegeTest, Basic) { |
| 68 ASSERT_FALSE(CurrentProcessHasPrivilege(kTestedPrivilege)); |
| 69 |
| 70 if (!::IsUserAnAdmin()) { |
| 71 LOG(WARNING) << "Skipping SetupUtilTest.ScopedTokenPrivilegeBasic due to " |
| 72 "not running as admin."; |
| 73 return; |
| 74 } |
| 75 |
| 76 { |
| 77 ScopedTokenPrivilege test_scoped_privilege(kTestedPrivilege); |
| 78 ASSERT_TRUE(test_scoped_privilege.is_enabled()); |
| 79 ASSERT_TRUE(CurrentProcessHasPrivilege(kTestedPrivilege)); |
| 80 } |
| 81 |
| 82 ASSERT_FALSE(CurrentProcessHasPrivilege(kTestedPrivilege)); |
| 83 } |
| 84 |
| 85 // Note: This test is only valid when run at high integrity (i.e. it will fail |
| 86 // at medium integrity). |
| 87 TEST(ScopedTokenPrivilegeTest, AlreadyEnabled) { |
| 88 ASSERT_FALSE(CurrentProcessHasPrivilege(kTestedPrivilege)); |
| 89 |
| 90 if (!::IsUserAnAdmin()) { |
| 91 LOG(WARNING) << "Skipping SetupUtilTest.ScopedTokenPrivilegeAlreadyEnabled " |
| 92 "due to not running as admin."; |
| 93 return; |
| 94 } |
| 95 |
| 96 { |
| 97 ScopedTokenPrivilege test_scoped_privilege(kTestedPrivilege); |
| 98 ASSERT_TRUE(test_scoped_privilege.is_enabled()); |
| 99 ASSERT_TRUE(CurrentProcessHasPrivilege(kTestedPrivilege)); |
| 100 { |
| 101 ScopedTokenPrivilege dup_scoped_privilege(kTestedPrivilege); |
| 102 ASSERT_TRUE(dup_scoped_privilege.is_enabled()); |
| 103 ASSERT_TRUE(CurrentProcessHasPrivilege(kTestedPrivilege)); |
| 104 } |
| 105 ASSERT_TRUE(CurrentProcessHasPrivilege(kTestedPrivilege)); |
| 106 } |
| 107 |
| 108 ASSERT_FALSE(CurrentProcessHasPrivilege(kTestedPrivilege)); |
| 109 } |
| 110 |
| 111 } // namespace installer |
OLD | NEW |