ReactOS 0.4.15-dev-8109-gd7be748
RtlRemovePrivileges.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS api tests
3 * LICENSE: See COPYING in the top level directory
4 * PURPOSE: Test for RtlRemovePrivileges
5 * PROGRAMMER: Ratin Gao <ratin@knsoft.org>
6 */
7
8#include "precomp.h"
9
11{
12#if (NTDDI_VERSION >= NTDDI_VISTA)
14 HANDLE TokenHandle, TestTokenHandle;
17 [sizeof(TOKEN_PRIVILEGES) +
20 ULONG PrivilegesToKeep[2];
21
22 /* Duplicate current process token to run this test */
24 if (!NT_SUCCESS(Status))
25 {
26 ok(0, "Failed to open current process token with TOKEN_DUPLICATE access (Status code %lx)!\n", Status);
27 return;
28 }
29
32 if (!NT_SUCCESS(Status))
33 {
34 ok(0, "Failed to duplicate current process token (Status code %lx)!\n", Status);
35 return;
36 }
37
38 /* Retrieve token privileges, we need at least 3 privileges to run following tests */
40 if (!NT_SUCCESS(Status))
41 {
42 NtClose(TestTokenHandle);
43 ok(0, "Failed to retrieve token privileges (Status code %lx)!\n", Status);
44 return;
45 }
47 if (Privileges->PrivilegeCount < 3)
48 {
49 NtClose(TestTokenHandle);
50 ok(0, "No enough privileges to run the test (Number of privilege: %lu)!\n", Privileges->PrivilegeCount);
51 return;
52 }
53
54 /* Remove all privileges except 2nd and 3rd privileges, this should succeed */
55 PrivilegesToKeep[0] = Privileges->Privileges[1].Luid.LowPart;
56 PrivilegesToKeep[1] = Privileges->Privileges[2].Luid.LowPart;
57 Status = RtlRemovePrivileges(TestTokenHandle, PrivilegesToKeep, ARRAYSIZE(PrivilegesToKeep));
58
59 /* Do not use NT_SUCCESS, RtlRemovePrivileges may returns STATUS_NOT_ALL_ASSIGNED */
61 {
62 NtClose(TestTokenHandle);
64 return;
65 }
66
67 /* Now, only two privileges we kept should be present */
69 if (!NT_SUCCESS(Status))
70 {
71 NtClose(TestTokenHandle);
72 ok(0, "Failed to retrieve token privileges (Status code %lx)!\n", Status);
73 return;
74 }
75 ok(Privileges->PrivilegeCount == ARRAYSIZE(PrivilegesToKeep),
76 "Number of privileges after RtlRemovePrivileges is %lu, expected %u\n", Privileges->PrivilegeCount,
77 ARRAYSIZE(PrivilegesToKeep));
78 ok(PrivilegesToKeep[0] + PrivilegesToKeep[1] ==
79 Privileges->Privileges[0].Luid.LowPart + Privileges->Privileges[1].Luid.LowPart,
80 "Incorrect privileges kept by RtlRemovePrivileges: %lu and %lu, expected %lu and %lu",
81 Privileges->Privileges[0].Luid.LowPart, Privileges->Privileges[1].Luid.LowPart, PrivilegesToKeep[0],
82 PrivilegesToKeep[1]);
83
84 /* Remove all privileges, this should succeed */
85 Status = RtlRemovePrivileges(TestTokenHandle, NULL, 0);
86
87 /* Do not use NT_SUCCESS, RtlRemovePrivileges may returns STATUS_NOT_ALL_ASSIGNED */
89 {
90 NtClose(TestTokenHandle);
92 return;
93 }
94
95 /* Now, no privilege should be present */
97 if (!NT_SUCCESS(Status))
98 {
99 NtClose(TestTokenHandle);
100 ok(0, "Failed to retrieve token privileges (Status code %lx)!\n", Status);
101 return;
102 }
103 ok(Privileges->PrivilegeCount == 0, "There are %lu privileges still exist after RtlRemovePrivileges\n",
104 Privileges->PrivilegeCount);
105
106 NtClose(TestTokenHandle);
107 return;
108#else
109 skip("RtlRemovePrivileges available on NT6.0+ (NTDDI_VERSION >= NTDDI_VISTA)");
110#endif /* (NTDDI_VERSION >= NTDDI_VISTA) */
111}
#define ok_ntstatus(status, expected)
Definition: atltest.h:135
#define ok(value,...)
Definition: atltest.h:57
#define skip(...)
Definition: atltest.h:64
#define START_TEST(x)
Definition: atltest.h:75
LONG NTSTATUS
Definition: precomp.h:26
Definition: bufpool.h:45
#define NULL
Definition: types.h:112
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
#define GetCurrentProcess()
Definition: compat.h:759
IN CINT OUT PVOID IN ULONG OUT PULONG ReturnLength
Definition: dumpinfo.c:43
Status
Definition: gdiplustypes.h:25
@ TokenPrimary
Definition: imports.h:273
#define SE_MIN_WELL_KNOWN_PRIVILEGE
Definition: security.c:655
#define SE_MAX_WELL_KNOWN_PRIVILEGE
Definition: security.c:685
_In_ ACCESS_MASK _In_ ULONG _Out_ PHANDLE TokenHandle
Definition: psfuncs.h:726
NTSYSAPI NTSTATUS NTAPI RtlRemovePrivileges(_In_ HANDLE TokenHandle, _In_reads_opt_(PrivilegeCount) _When_(PrivilegeCount !=0, _Notnull_) PULONG PrivilegesToKeep, _In_ ULONG PrivilegeCount)
Removes all privileges in the specified access token.
Definition: priv.c:515
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3402
NTSTATUS NTAPI NtOpenProcessToken(IN HANDLE ProcessHandle, IN ACCESS_MASK DesiredAccess, OUT PHANDLE TokenHandle)
Definition: security.c:350
#define STATUS_SUCCESS
Definition: shellext.h:65
_Must_inspect_result_ __kernel_entry NTSTATUS NTAPI NtQueryInformationToken(_In_ HANDLE TokenHandle, _In_ TOKEN_INFORMATION_CLASS TokenInformationClass, _Out_writes_bytes_to_opt_(TokenInformationLength, *ReturnLength) PVOID TokenInformation, _In_ ULONG TokenInformationLength, _Out_ PULONG ReturnLength)
Queries a specific type of information in regard of an access token based upon the information class....
Definition: tokencls.c:473
_Must_inspect_result_ __kernel_entry NTSTATUS NTAPI NtDuplicateToken(_In_ HANDLE ExistingTokenHandle, _In_ ACCESS_MASK DesiredAccess, _In_opt_ POBJECT_ATTRIBUTES ObjectAttributes, _In_ BOOLEAN EffectiveOnly, _In_ TOKEN_TYPE TokenType, _Out_ PHANDLE NewTokenHandle)
Duplicates a token.
Definition: tokenlif.c:1869
uint32_t ULONG
Definition: typedefs.h:59
_In_ PSECURITY_SUBJECT_CONTEXT _In_ BOOLEAN _In_ ACCESS_MASK _In_ ACCESS_MASK _Outptr_opt_ PPRIVILEGE_SET * Privileges
Definition: sefuncs.h:17
#define TOKEN_DUPLICATE
Definition: setypes.h:926
struct _TOKEN_PRIVILEGES TOKEN_PRIVILEGES
@ TokenPrivileges
Definition: setypes.h:968
struct _TOKEN_PRIVILEGES * PTOKEN_PRIVILEGES
#define TOKEN_ALL_ACCESS
Definition: setypes.h:946
unsigned char UCHAR
Definition: xmlstorage.h:181