ReactOS  0.4.15-dev-2964-ge2a8585
NtImpersonateAnonymousToken.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS API tests
3  * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4  * PURPOSE: Tests for the NtImpersonateAnonymousToken API
5  * COPYRIGHT: Copyright 2021 George BiČ™oc <george.bisoc@reactos.org>
6  */
7 
8 #include "precomp.h"
9 #include <winreg.h>
10 
11 #define TOKEN_WITH_EVERYONE_GROUP 1
12 #define TOKEN_WITHOUT_EVERYONE_GROUP 0
13 
14 static
15 HANDLE
17 {
18  HANDLE Thread;
19 
21  if (!Thread)
22  {
23  skip("OpenThread() has failed to open the current process' thread (error code: %lu)\n", GetLastError());
24  return NULL;
25  }
26 
27  return Thread;
28 }
29 
30 static
31 VOID
33 {
34  LONG Result;
35  HKEY Key;
36 
38  L"SYSTEM\\CurrentControlSet\\Control\\Lsa",
39  0,
41  &Key);
42  if (Result != ERROR_SUCCESS)
43  {
44  skip("RegOpenKeyExW() has failed to open the key (error code: %li)\n", Result);
45  return;
46  }
47 
49  L"EveryoneIncludesAnonymous",
50  0,
51  REG_DWORD,
52  (PBYTE)&Value,
53  sizeof(Value));
54  if (Result != ERROR_SUCCESS)
55  {
56  skip("RegSetValueExW() has failed to set the value (error code: %li)\n", Result);
58  return;
59  }
60 
62 }
63 
65 {
67  BOOL Success;
68  HANDLE ThreadHandle;
69 
71 
72  /* We give an invalid thread handle */
75 
76  /* We want to impersonate the token including Everyone Group SID */
78 
79  /* Impersonate the anonymous logon token */
80  Status = NtImpersonateAnonymousToken(ThreadHandle);
82 
83  /* Now revert to the previous security properties */
85  ok(Success == TRUE, "We should have terminated the impersonation but we couldn't (error code: %lu)\n", GetLastError());
86 
87  /* Return to default setting -- token without Everyone Group SID */
89 
90  /* Impersonate the anonymous logon token again */
91  Status = NtImpersonateAnonymousToken(ThreadHandle);
93 
94  /* Now revert to the previous security properties */
96  ok(Success == TRUE, "We should have terminated the impersonation but we couldn't (error code: %lu)\n", GetLastError());
97 
98  /*
99  * Invalidate the handle and open a new one. This time
100  * with the wrong access right mask, the function will
101  * outright fail on impersonating the token.
102  */
103  CloseHandle(ThreadHandle);
105 
106  /* The thread handle has incorrect right access */
107  Status = NtImpersonateAnonymousToken(ThreadHandle);
109 
110  /* We're done with the tests */
111  CloseHandle(ThreadHandle);
112 }
_Must_inspect_result_ _In_ WDFDEVICE _In_ ULONG _In_ ACCESS_MASK DesiredAccess
Definition: wdfdevice.h:2654
#define TOKEN_WITHOUT_EVERYONE_GROUP
#define CloseHandle
Definition: compat.h:598
#define ERROR_SUCCESS
Definition: deptool.c:10
#define KEY_SET_VALUE
Definition: nt_native.h:1017
START_TEST(NtImpersonateAnonymousToken)
#define THREAD_IMPERSONATE
Definition: pstypes.h:151
#define TRUE
Definition: types.h:120
BOOL WINAPI RevertToSelf(VOID)
Definition: security.c:1487
static VOID ImpersonateTokenWithEveryoneOrWithout(_In_ DWORD Value)
LONG NTSTATUS
Definition: precomp.h:26
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1040
#define ok_hex(expression, result)
Definition: atltest.h:94
#define STATUS_INVALID_HANDLE
Definition: ntstatus.h:245
NTSTATUS NTAPI NtImpersonateAnonymousToken(_In_ HANDLE ThreadHandle)
Allows the calling thread to impersonate the system's anonymous logon token.
Definition: token.c:4561
LONG WINAPI RegCloseKey(HKEY hKey)
Definition: reg.c:423
#define FALSE
Definition: types.h:117
unsigned int BOOL
Definition: ntddk_ex.h:94
long LONG
Definition: pedump.c:60
_At_(*)(_In_ PWSK_CLIENT Client, _In_opt_ PUNICODE_STRING NodeName, _In_opt_ PUNICODE_STRING ServiceName, _In_opt_ ULONG NameSpace, _In_opt_ GUID *Provider, _In_opt_ PADDRINFOEXW Hints, _Outptr_ PADDRINFOEXW *Result, _In_opt_ PEPROCESS OwningProcess, _In_opt_ PETHREAD OwningThread, _Inout_ PIRP Irp Result)(Mem)) NTSTATUS(WSKAPI *PFN_WSK_GET_ADDRESS_INFO
Definition: wsk.h:426
#define _In_
Definition: ms_sal.h:308
_Must_inspect_result_ _In_ WDFDEVICE _In_ ULONG _In_ ACCESS_MASK _In_opt_ PWDF_OBJECT_ATTRIBUTES _Out_ WDFKEY * Key
Definition: wdfdevice.h:2654
DWORD WINAPI GetCurrentThreadId(VOID)
Definition: thread.c:458
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING _Out_opt_ PUSHORT _Inout_opt_ PUNICODE_STRING Value
Definition: wdfregistry.h:406
Status
Definition: gdiplustypes.h:24
LONG WINAPI RegSetValueExW(_In_ HKEY hKey, _In_ LPCWSTR lpValueName, _In_ DWORD Reserved, _In_ DWORD dwType, _In_ CONST BYTE *lpData, _In_ DWORD cbData)
Definition: reg.c:4895
unsigned long DWORD
Definition: ntddk_ex.h:95
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2652
#define STATUS_ACCESS_DENIED
Definition: udferr_usr.h:145
static const WCHAR L[]
Definition: oid.c:1250
HANDLE WINAPI OpenThread(IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN DWORD dwThreadId)
Definition: thread.c:402
#define SYNCHRONIZE
Definition: nt_native.h:61
static HANDLE GetThreadFromCurrentProcess(_In_ DWORD DesiredAccess)
#define ok(value,...)
Definition: atltest.h:57
#define NULL
Definition: types.h:112
#define TOKEN_WITH_EVERYONE_GROUP
#define skip(...)
Definition: atltest.h:64
LONG WINAPI RegOpenKeyExW(HKEY hKey, LPCWSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)
Definition: reg.c:3366
#define STATUS_SUCCESS
Definition: shellext.h:65
#define REG_DWORD
Definition: sdbapi.c:596
BYTE * PBYTE
Definition: pedump.c:66
#define HKEY_LOCAL_MACHINE
Definition: winreg.h:12