Index: base/process/process_info_win.cc |
diff --git a/base/process/process_info_win.cc b/base/process/process_info_win.cc |
index 8c4b36cb19e7a39a18b5d86b46c85d64030ed75e..06780838fec2b36e3b81a00e77bb51cf0c68ce7c 100644 |
--- a/base/process/process_info_win.cc |
+++ b/base/process/process_info_win.cc |
@@ -13,6 +13,17 @@ |
namespace base { |
+namespace { |
+ |
+HANDLE GetCurrentProcessToken() { |
(unused - use chromium)
2017/06/30 16:15:18
We should probably make ScopedHandle movable and r
jam
2017/06/30 16:16:49
yep agreed, i checked first to see if it's moveabl
|
+ HANDLE process_token; |
+ OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &process_token); |
+ DCHECK(process_token != NULL && process_token != INVALID_HANDLE_VALUE); |
+ return process_token; |
+} |
+ |
+} // namespace |
+ |
// static |
const Time CurrentProcessInfo::CreationTime() { |
FILETIME creation_time = {}; |
@@ -27,16 +38,11 @@ const Time CurrentProcessInfo::CreationTime() { |
} |
IntegrityLevel GetCurrentProcessIntegrityLevel() { |
- HANDLE process_token; |
- if (!::OpenProcessToken(::GetCurrentProcess(), |
- TOKEN_QUERY | TOKEN_QUERY_SOURCE, &process_token)) { |
- return INTEGRITY_UNKNOWN; |
- } |
- win::ScopedHandle scoped_process_token(process_token); |
+ base::win::ScopedHandle scoped_process_token(GetCurrentProcessToken()); |
DWORD token_info_length = 0; |
- if (::GetTokenInformation(process_token, TokenIntegrityLevel, nullptr, 0, |
- &token_info_length) || |
+ if (::GetTokenInformation(scoped_process_token.Get(), TokenIntegrityLevel, |
+ nullptr, 0, &token_info_length) || |
::GetLastError() != ERROR_INSUFFICIENT_BUFFER) { |
return INTEGRITY_UNKNOWN; |
} |
@@ -44,8 +50,9 @@ IntegrityLevel GetCurrentProcessIntegrityLevel() { |
auto token_label_bytes = MakeUnique<char[]>(token_info_length); |
TOKEN_MANDATORY_LABEL* token_label = |
reinterpret_cast<TOKEN_MANDATORY_LABEL*>(token_label_bytes.get()); |
- if (!::GetTokenInformation(process_token, TokenIntegrityLevel, token_label, |
- token_info_length, &token_info_length)) { |
+ if (!::GetTokenInformation(scoped_process_token.Get(), TokenIntegrityLevel, |
+ token_label, token_info_length, |
+ &token_info_length)) { |
return INTEGRITY_UNKNOWN; |
} |
@@ -69,4 +76,19 @@ IntegrityLevel GetCurrentProcessIntegrityLevel() { |
return INTEGRITY_UNKNOWN; |
} |
+bool IsCurrentProcessElevated() { |
+ base::win::ScopedHandle scoped_process_token(GetCurrentProcessToken()); |
+ |
+ // Unlike TOKEN_ELEVATION_TYPE which returns TokenElevationTypeDefault when |
+ // UAC is turned off, TOKEN_ELEVATION returns whether the process is elevated. |
+ DWORD size; |
+ TOKEN_ELEVATION elevation; |
+ if (!GetTokenInformation(scoped_process_token.Get(), TokenElevation, |
+ &elevation, sizeof(elevation), &size)) { |
+ PLOG(ERROR) << "GetTokenInformation() failed"; |
+ return false; |
+ } |
+ return !!elevation.TokenIsElevated; |
+} |
+ |
} // namespace base |