Skip to content

Commit bdc844f

Browse files
committed
Bootmgr (Windows): prefers NT APIs
1 parent d49c018 commit bdc844f

1 file changed

Lines changed: 11 additions & 8 deletions

File tree

src/detection/bootmgr/bootmgr_windows.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
#include "common/io.h"
44
#include "common/windows/nt.h"
55

6+
#include <ntstatus.h>
67
#include <windows.h>
78

89
const char* enablePrivilege(const wchar_t* privilege)
910
{
1011
FF_AUTO_CLOSE_FD HANDLE token = NULL;
11-
if (!OpenProcessToken(NtCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token))
12-
return "OpenProcessToken() failed";
12+
if (!NT_SUCCESS(NtOpenProcessToken(NtCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token)))
13+
return "NtOpenProcessToken() failed";
1314

1415
TOKEN_PRIVILEGES tp = {
1516
.PrivilegeCount = 1,
@@ -20,19 +21,21 @@ const char* enablePrivilege(const wchar_t* privilege)
2021
if (!LookupPrivilegeValueW(NULL, privilege, &tp.Privileges[0].Luid))
2122
return "LookupPrivilegeValue() failed";
2223

23-
if (!AdjustTokenPrivileges(token, false, &tp, sizeof(tp), NULL, NULL))
24-
return "AdjustTokenPrivileges() failed";
24+
NTSTATUS status = NtAdjustPrivilegesToken(token, false, &tp, sizeof(tp), NULL, NULL);
25+
if (!NT_SUCCESS(status))
26+
return "NtAdjustPrivilegesToken() failed";
2527

26-
if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)
27-
return "The token does not have the specified privilege";
28+
if (status == STATUS_NOT_ALL_ASSIGNED)
29+
return "The token does not have the specified privilege; try sudo please";
2830

2931
return NULL;
3032
}
3133

3234
const char* ffDetectBootmgr(FFBootmgrResult* result)
3335
{
34-
if (enablePrivilege(L"SeSystemEnvironmentPrivilege") != NULL)
35-
return "Failed to enable SeSystemEnvironmentPrivilege";
36+
const char* err = enablePrivilege(L"SeSystemEnvironmentPrivilege");
37+
if (err != NULL)
38+
return err;
3639

3740
GUID efiGlobalGuid;
3841
if (!NT_SUCCESS(RtlGUIDFromString(&(UNICODE_STRING) RTL_CONSTANT_STRING(L"{" FF_EFI_GLOBAL_GUID L"}"), &efiGlobalGuid)))

0 commit comments

Comments
 (0)