pastebin - collaborative debugging tool
rovema.kpaste.net RSS


Win32: Get large MMU pages for memory
Posted by Anonymous on Tue 16th Apr 2024 02:20
raw | new post

  1. #define UNICODE
  2. #define _UNICODE
  3.  
  4. #include <windows.h>
  5. #include <ntsecapi.h>
  6. #include <ntstatus.h>
  7. #include <sddl.h>
  8. #include <wchar.h>
  9. #include <stdio.h>
  10.  
  11. void InitLsaString(PLSA_UNICODE_STRING LsaString, LPWSTR String)
  12. {
  13.     DWORD StringLength;
  14.  
  15.     if (String == NULL) {
  16.         LsaString->Buffer = NULL;
  17.         LsaString->Length = 0;
  18.         LsaString->MaximumLength = 0;
  19.         return;
  20.     }
  21.  
  22.     StringLength = wcslen(String);
  23.     LsaString->Buffer = String;
  24.     LsaString->Length = (USHORT)StringLength * sizeof(WCHAR);
  25.     LsaString->MaximumLength = (USHORT)(StringLength + 1) * sizeof(WCHAR);
  26. }
  27.  
  28. NTSTATUS OpenPolicy(LPWSTR ServerName, DWORD DesiredAccess, PLSA_HANDLE PolicyHandle)
  29. {
  30.     LSA_OBJECT_ATTRIBUTES ObjectAttributes;
  31.     LSA_UNICODE_STRING ServerString;
  32.     PLSA_UNICODE_STRING Server = NULL;
  33.  
  34.     //
  35.     // Always initialize the object attributes to all zeroes.
  36.     //
  37.     ZeroMemory(&ObjectAttributes, sizeof(ObjectAttributes));
  38.  
  39.     if (ServerName != NULL) {
  40.         //
  41.         // Make a LSA_UNICODE_STRING out of the LPWSTR passed in
  42.         //
  43.         InitLsaString(&ServerString, ServerName);
  44.         Server = &ServerString;
  45.     }
  46.  
  47.     //
  48.     // Attempt to open the policy.
  49.     //
  50.     return LsaOpenPolicy(
  51.         Server,
  52.         &ObjectAttributes,
  53.         DesiredAccess,
  54.         PolicyHandle
  55.     );
  56. }
  57.  
  58. NTSTATUS SetPrivilegeOnAccount(LSA_HANDLE PolicyHandle, PSID AccountSid, LPWSTR PrivilegeName, BOOL bEnable)
  59. {
  60.     LSA_UNICODE_STRING PrivilegeString;
  61.  
  62.     //
  63.     // Create a LSA_UNICODE_STRING for the privilege name.
  64.     //
  65.     InitLsaString(&PrivilegeString, PrivilegeName);
  66.  
  67.     //
  68.     // grant or revoke the privilege, accordingly
  69.     //
  70.     if (bEnable) {
  71.         return LsaAddAccountRights(
  72.             PolicyHandle,       // open policy handle
  73.             AccountSid,         // target SID
  74.             &PrivilegeString,   // privileges
  75.             1                   // privilege count
  76.         );
  77.     }
  78.     else {
  79.         return LsaRemoveAccountRights(
  80.             PolicyHandle,       // open policy handle
  81.             AccountSid,         // target SID
  82.             FALSE,              // do not disable all rights
  83.             &PrivilegeString,   // privileges
  84.             1                   // privilege count
  85.         );
  86.     }
  87. }
  88.  
  89. int main(int ac, char *av[])
  90. {
  91.     HANDLE hToken = NULL;
  92.  
  93.     if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
  94.     {
  95.         printf("OpenProcessToken failed. GetLastError returned: %d\n", GetLastError());
  96.         return -1;
  97.     }
  98.  
  99.     DWORD dwBufferSize = 0;
  100.  
  101.     // Probe the buffer size reqired for PTOKEN_USER structure
  102.     if (!GetTokenInformation(hToken, TokenUser, NULL, 0, &dwBufferSize) &&
  103.         (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
  104.     {
  105.         printf("GetTokenInformation failed. GetLastError returned: %d\n", GetLastError());
  106.  
  107.         // Cleanup
  108.         CloseHandle(hToken);
  109.         hToken = NULL;
  110.  
  111.         return -1;
  112.     }
  113.  
  114.     PTOKEN_USER pTokenUser = (PTOKEN_USER) malloc(dwBufferSize);
  115.  
  116.     // Retrieve the token information in a TOKEN_USER structure
  117.     if (!GetTokenInformation(
  118.         hToken,
  119.         TokenUser,
  120.         pTokenUser,
  121.         dwBufferSize,
  122.         &dwBufferSize))
  123.     {
  124.         printf("GetTokenInformation failed. GetLastError returned: %d\n", GetLastError());
  125.  
  126.         // Cleanup
  127.         CloseHandle(hToken);
  128.         hToken = NULL;
  129.  
  130.         return -1;
  131.     }
  132.  
  133.     // Print SID string
  134.     LPWSTR strsid;
  135.     ConvertSidToStringSid(pTokenUser->User.Sid, &strsid);
  136.     printf("User SID: %S\n", strsid);
  137.  
  138.     // Cleanup
  139.     CloseHandle(hToken);
  140.     hToken = NULL;
  141.  
  142.     NTSTATUS status;
  143.     LSA_HANDLE policyHandle;
  144.  
  145.     if (status = OpenPolicy(NULL, POLICY_CREATE_ACCOUNT | POLICY_LOOKUP_NAMES, &policyHandle))
  146.     {
  147.         printf("OpenPolicy %d\n", status);
  148.     }
  149. #if 0
  150.     // Add new privelege to the account
  151.     if (status = SetPrivilegeOnAccount(policyHandle, pTokenUser->User.Sid, SE_LOCK_MEMORY_NAME, TRUE))
  152.     {
  153.         printf("OpenPSetPrivilegeOnAccountolicy %d\n", status);
  154.     }
  155. #endif
  156.     // Enable this priveledge for the current process
  157.     hToken = NULL;
  158.     TOKEN_PRIVILEGES tp;
  159.  
  160.     if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES, &hToken))
  161.     {
  162.         printf("OpenProcessToken #2 failed. GetLastError returned: %d\n", GetLastError());
  163.         return -1;
  164.     }
  165.  
  166.     tp.PrivilegeCount = 1;
  167.     tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  168.  
  169.     if (!LookupPrivilegeValue(NULL, SE_LOCK_MEMORY_NAME, &tp.Privileges[0].Luid))
  170.     {
  171.         printf("LookupPrivilegeValue failed. GetLastError returned: %d\n", GetLastError());
  172.         return -1;
  173.     }
  174.  
  175.     BOOL result = AdjustTokenPrivileges(hToken, FALSE, &tp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
  176.     DWORD error = GetLastError();
  177.  
  178.     if (!result || (error != ERROR_SUCCESS))
  179.     {
  180.         printf("AdjustTokenPrivileges failed. GetLastError returned: %d\n", error);
  181.         return -1;
  182.     }
  183.  
  184.     // Cleanup
  185.     CloseHandle(hToken);
  186.     hToken = NULL;
  187.  
  188.     SIZE_T pageSize = GetLargePageMinimum();
  189. #define N_PAGES_TO_ALLOC 4
  190.     // Finally allocate the memory
  191.     char *largeBuffer = VirtualAlloc(NULL, pageSize * N_PAGES_TO_ALLOC, MEM_RESERVE | MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE);
  192.     if (largeBuffer)
  193.     {
  194.         printf("VirtualAlloc success, ptr = %p\n", largeBuffer);
  195.     }
  196.     else
  197.     {
  198.         printf("VirtualAlloc failed, error 0x%x\n", GetLastError());
  199.     }
  200. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at