DuplicateTokenEx ImpersonationLevel argument mismatches
- Dominant language
- C#
- Stars
- 72
- Forks
- 33
- Avg merge
- 3h 58m
- Merged PRs (30d)
- 3
Description
There appear to be a mismatches in the DllImport signatures for [`DuplicateTokenEx`](https://learn.microsoft.com/en-us/windows/win32/api/securitybaseapi/nf-securitybaseapi-duplicatetokenex) in this project. The 4th `ImpersonationLevel` argument is supposed to be of type [`SECURITY_IMPERSONATION_LEVEL`](https://learn.microsoft.com/en-us/windows/win32/api/winnt/ne-winnt-security_impersonation_level), but is mapped to the .Net [TokenImpersonationLevel](https://learn.microsoft.com/en-us/dotnet/api/system.security.principal.tokenimpersonationlevel) counterpart. This .Net enumeration does on the surface appear identical, but the underlying values are shifted by one.
Affected files:
* [ProcessUtilities.cs](https://github.com/dotnet/wpf-test/blob/main/src/Test/Infra/QualityVault/QualityVaultUtilities/Execution/ProcessUtilities.cs#L177)
* [ProcessHelper.cs](https://github.com/dotnet/wpf-test/blob/main/src/Test/Common/Code/Microsoft/Test/Diagnostics/ProcessHelper.cs#L714)
## Suggested fix
* Change `TokenImpersonationLevel ImpersonationLevel` argument to `uint ImpersonationLevel` or define a matching .Net `SECURITY_IMPERSONATION_LEVEL` enumeration. Both strategies are already applied in the [dotnet/runtime](https://github.com/search?q=repo%3Adotnet%2Fruntime%20DuplicateTokenEx&type=code) repo.
## Background material
From `winnt.h` [SECURITY_IMPERSONATION_LEVEL](https://learn.microsoft.com/en-us/windows/win32/api/winnt/ne-winnt-security_impersonation_level):
```
// Impersonation Level
//
// Impersonation level is represented by a pair of bits in Windows.
// If a new impersonation level is added or lowest value is changed from
// 0 to something else, fix the Windows CreateFile call.
//
typedef enum _SECURITY_IMPERSONATION_LEVEL {
SecurityAnonymous,
SecurityIdentification,
SecurityImpersonation,
SecurityDelegation
} SECURITY_IMPERSONATION_LEVEL, * PSECURITY_IMPERSONATION_LEVEL;
```
From .NET [TokenImpersonationLevel](https://learn.microsoft.com/en-us/dotnet/api/system.security.principal.tokenimpersonationlevel):
```
namespace System.Security.Principal
{
//
// Summary:
// Defines security impersonation levels. Security impersonation levels govern the
// degree to which a server process can act on behalf of a client process.
public enum TokenImpersonationLevel
{
None = 0,
Anonymous = 1,
Identification = 2,
Impersonation = 3,
Delegation = 4
}
}
```
Contributor guide
Assessment
This issue has not been assessed yet.