| 1 | // Copyright (c) 2012 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 "base/process/process_info.h" |
| 6 | |
| 7 | #include <windows.h> |
| 8 | #include <memory> |
| 9 | |
| 10 | #include "base/logging.h" |
| 11 | #include "base/memory/ptr_util.h" |
| 12 | #include "base/time/time.h" |
| 13 | #include "base/win/scoped_handle.h" |
| 14 | |
| 15 | namespace base { |
| 16 | |
| 17 | namespace { |
| 18 | |
| 19 | HANDLE GetCurrentProcessToken() { |
| 20 | HANDLE process_token; |
| 21 | OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &process_token); |
| 22 | DCHECK(process_token != NULL && process_token != INVALID_HANDLE_VALUE); |
| 23 | return process_token; |
| 24 | } |
| 25 | |
| 26 | } // namespace |
| 27 | |
| 28 | IntegrityLevel GetCurrentProcessIntegrityLevel() { |
| 29 | HANDLE process_token(GetCurrentProcessToken()); |
| 30 | |
| 31 | DWORD token_info_length = 0; |
| 32 | if (::GetTokenInformation(process_token, TokenIntegrityLevel, nullptr, 0, |
| 33 | &token_info_length) || |
| 34 | ::GetLastError() != ERROR_INSUFFICIENT_BUFFER) { |
| 35 | NOTREACHED(); |
| 36 | return INTEGRITY_UNKNOWN; |
| 37 | } |
| 38 | |
| 39 | auto token_label_bytes = std::make_unique<char[]>(token_info_length); |
| 40 | TOKEN_MANDATORY_LABEL* token_label = |
| 41 | reinterpret_cast<TOKEN_MANDATORY_LABEL*>(token_label_bytes.get()); |
| 42 | if (!::GetTokenInformation(process_token, TokenIntegrityLevel, token_label, |
| 43 | token_info_length, &token_info_length)) { |
| 44 | NOTREACHED(); |
| 45 | return INTEGRITY_UNKNOWN; |
| 46 | } |
| 47 | |
| 48 | DWORD integrity_level = *::GetSidSubAuthority( |
| 49 | token_label->Label.Sid, |
| 50 | static_cast<DWORD>(*::GetSidSubAuthorityCount(token_label->Label.Sid) - |
| 51 | 1)); |
| 52 | |
| 53 | if (integrity_level < SECURITY_MANDATORY_LOW_RID) |
| 54 | return UNTRUSTED_INTEGRITY; |
| 55 | |
| 56 | if (integrity_level < SECURITY_MANDATORY_MEDIUM_RID) |
| 57 | return LOW_INTEGRITY; |
| 58 | |
| 59 | if (integrity_level >= SECURITY_MANDATORY_MEDIUM_RID && |
| 60 | integrity_level < SECURITY_MANDATORY_HIGH_RID) { |
| 61 | return MEDIUM_INTEGRITY; |
| 62 | } |
| 63 | |
| 64 | if (integrity_level >= SECURITY_MANDATORY_HIGH_RID) |
| 65 | return HIGH_INTEGRITY; |
| 66 | |
| 67 | NOTREACHED(); |
| 68 | return INTEGRITY_UNKNOWN; |
| 69 | } |
| 70 | |
| 71 | bool IsCurrentProcessElevated() { |
| 72 | HANDLE process_token(GetCurrentProcessToken()); |
| 73 | |
| 74 | // Unlike TOKEN_ELEVATION_TYPE which returns TokenElevationTypeDefault when |
| 75 | // UAC is turned off, TOKEN_ELEVATION returns whether the process is elevated. |
| 76 | DWORD size; |
| 77 | TOKEN_ELEVATION elevation; |
| 78 | if (!GetTokenInformation(process_token, TokenElevation, &elevation, |
| 79 | sizeof(elevation), &size)) { |
| 80 | PLOG(ERROR) << "GetTokenInformation() failed"; |
| 81 | return false; |
| 82 | } |
| 83 | return !!elevation.TokenIsElevated; |
| 84 | } |
| 85 | |
| 86 | } // namespace base |
| 87 | |