Skip to content

Commit 7dd4096

Browse files
Downgrade system IL impersonation token to high (microsoft#40447)
Downgrade system IL in impersonated token to high IL
1 parent 2bd9656 commit 7dd4096

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

src/windows/common/WslSecurity.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,23 @@ wil::unique_handle wsl::windows::common::security::GetUserToken(_In_ TOKEN_TYPE
172172

173173
wil::unique_handle newToken;
174174
THROW_IF_WIN32_BOOL_FALSE(::DuplicateTokenEx(
175-
contextToken.get(), TOKEN_DUPLICATE | TOKEN_IMPERSONATE | TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES, nullptr, SecurityImpersonation, tokenType, &newToken));
175+
contextToken.get(),
176+
TOKEN_DUPLICATE | TOKEN_IMPERSONATE | TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES | TOKEN_ADJUST_DEFAULT,
177+
nullptr,
178+
SecurityImpersonation,
179+
tokenType,
180+
&newToken));
181+
182+
// If the token integrity level is system, reduce it to high integrity level. The VM worker process runs at
183+
// high integrity level and objects created with a higher integrity level token may be inaccessible.
184+
if (GetUserBasicIntegrityLevel(newToken.get()) == SECURITY_MANDATORY_SYSTEM_RID)
185+
{
186+
auto [sid, sidBuffer] = wsl::windows::common::security::CreateSid(SECURITY_MANDATORY_LABEL_AUTHORITY, SECURITY_MANDATORY_HIGH_RID);
187+
TOKEN_MANDATORY_LABEL tokenLabel{};
188+
tokenLabel.Label.Attributes = SE_GROUP_INTEGRITY;
189+
tokenLabel.Label.Sid = sid;
190+
THROW_IF_WIN32_BOOL_FALSE(::SetTokenInformation(newToken.get(), TokenIntegrityLevel, &tokenLabel, sizeof(tokenLabel)));
191+
}
176192

177193
return newToken;
178194
}

0 commit comments

Comments
 (0)