Commit 7d7efd9f authored by Joe Laughlin's avatar Joe Laughlin Committed by Commit Bot

Fix leak of process_token.

The calls to GetCurrentProcessToken() return a process token handle
that must be closed. This function is implemented in the same file,
and calls OpenProcessToken(). Win8 and later OSes have a function
of the same name ::GetCurrentProcessToken() which returns a
psuedo-handle that does not need to be closed.

The # Enter a description of the change.
Fix handle leaks of the process_token.

Bug: 1126279
Change-Id: Idbeaa92a173409a7b7152c04dcc67c917e8c306e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2402081
Commit-Queue: Joe Laughlin <joel@microsoft.com>
Commit-Queue: Nico Weber <thakis@chromium.org>
Reviewed-by: default avatarBruce Dawson <brucedawson@chromium.org>
Reviewed-by: default avatarNico Weber <thakis@chromium.org>
Cr-Commit-Position: refs/heads/master@{#810385}
parent ec185351
...@@ -17,35 +17,34 @@ namespace base { ...@@ -17,35 +17,34 @@ namespace base {
namespace { namespace {
HANDLE GetCurrentProcessToken() { base::win::ScopedHandle CreateCurrentProcessToken() {
HANDLE process_token; // OpenProcessToken can fail due to permission or allocation failures
// for the kernel object. Return nullptr in those cases.
HANDLE process_token = nullptr;
OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &process_token); OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &process_token);
DCHECK(process_token != NULL && process_token != INVALID_HANDLE_VALUE); return base::win::ScopedHandle(process_token);
return process_token;
} }
} // namespace } // namespace
IntegrityLevel GetCurrentProcessIntegrityLevel() { IntegrityLevel GetCurrentProcessIntegrityLevel() {
HANDLE process_token(GetCurrentProcessToken()); base::win::ScopedHandle scoped_process_token(CreateCurrentProcessToken());
DWORD token_info_length = 0; DWORD token_info_length = 0;
if (::GetTokenInformation(process_token, TokenIntegrityLevel, nullptr, 0, if (::GetTokenInformation(scoped_process_token.Get(), TokenIntegrityLevel,
&token_info_length) || nullptr, 0, &token_info_length) ||
::GetLastError() != ERROR_INSUFFICIENT_BUFFER) { ::GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
NOTREACHED();
return INTEGRITY_UNKNOWN; return INTEGRITY_UNKNOWN;
} }
auto token_label_bytes = std::make_unique<char[]>(token_info_length); auto token_label_bytes = std::make_unique<char[]>(token_info_length);
TOKEN_MANDATORY_LABEL* token_label = TOKEN_MANDATORY_LABEL* token_label =
reinterpret_cast<TOKEN_MANDATORY_LABEL*>(token_label_bytes.get()); reinterpret_cast<TOKEN_MANDATORY_LABEL*>(token_label_bytes.get());
if (!::GetTokenInformation(process_token, TokenIntegrityLevel, token_label, if (!::GetTokenInformation(scoped_process_token.Get(), TokenIntegrityLevel,
token_info_length, &token_info_length)) { token_label, token_info_length,
NOTREACHED(); &token_info_length)) {
return INTEGRITY_UNKNOWN; return INTEGRITY_UNKNOWN;
} }
DWORD integrity_level = *::GetSidSubAuthority( DWORD integrity_level = *::GetSidSubAuthority(
token_label->Label.Sid, token_label->Label.Sid,
static_cast<DWORD>(*::GetSidSubAuthorityCount(token_label->Label.Sid) - static_cast<DWORD>(*::GetSidSubAuthorityCount(token_label->Label.Sid) -
...@@ -70,14 +69,14 @@ IntegrityLevel GetCurrentProcessIntegrityLevel() { ...@@ -70,14 +69,14 @@ IntegrityLevel GetCurrentProcessIntegrityLevel() {
} }
bool IsCurrentProcessElevated() { bool IsCurrentProcessElevated() {
HANDLE process_token(GetCurrentProcessToken()); base::win::ScopedHandle scoped_process_token(CreateCurrentProcessToken());
// Unlike TOKEN_ELEVATION_TYPE which returns TokenElevationTypeDefault when // Unlike TOKEN_ELEVATION_TYPE which returns TokenElevationTypeDefault when
// UAC is turned off, TOKEN_ELEVATION returns whether the process is elevated. // UAC is turned off, TOKEN_ELEVATION returns whether the process is elevated.
DWORD size; DWORD size;
TOKEN_ELEVATION elevation; TOKEN_ELEVATION elevation;
if (!GetTokenInformation(process_token, TokenElevation, &elevation, if (!GetTokenInformation(scoped_process_token.Get(), TokenElevation,
sizeof(elevation), &size)) { &elevation, sizeof(elevation), &size)) {
PLOG(ERROR) << "GetTokenInformation() failed"; PLOG(ERROR) << "GetTokenInformation() failed";
return false; return false;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment