Went threw some code I wrote many years ago for testing LARGE_PAGES. Here
is what i had to do:
static LPVOID Alloc(DWORD dwSize)
{
#ifdef _WIN32
DWORD type = MEM_COMMIT | MEM_RESERVE;
#if defined (_USE_LARGE_PAGES)
DWORD isize = dwSize;
HANDLE hToken;
TOKEN_PRIVILEGES tp;
// open process token
if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES |
TOKEN_QUERY, &hToken))
{
// get the luid
if (LookupPrivilegeValueA(NULL, “SeLockMemoryPrivilege”,
&tp.Privileges[0].Luid))
{
BOOL status;
DWORD error;
tp.PrivilegeCount = 1;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
// enable privilege
status = AdjustTokenPrivileges(hToken, FALSE, &tp, 0,
(PTOKEN_PRIVILEGES)NULL, 0);
// It is possible for AdjustTokenPrivileges to return TRUE and still not
succeed.
// So always check for the last error value.
error = GetLastError();
if (status && (error == ERROR_SUCCESS))
{
HMODULE hModule;
PGLPM pGLPM;
pGLPM = (PGLPM)NULL;
hModule = GetModuleHandleA( “Kernel32.dll” );
if ( hModule )
pGLPM = (PGLPM)GetProcAddress( hModule, “GetLargePageMinimum” );
// Get environemnt specific large page size
//SIZE_T minsize = GetLargePageMinimum();
SIZE_T minsize = 0;
if ( (PGLPM)NULL != pGLPM )
minsize = pGLPM();
if ( 0 == minsize )
{
// If the processor does not support large pages, the return value is zero.
// Or GetLargePageMinimum is not exist in Kernel32.dll
minsize = 2 * 1024 * 1024; // The minimum large page size varies, but it is
typically 2 MB or greater.
}
if (isize >= minsize)
{ // If we get this far, we know that we can allocate large pages
SIZE_T blocks = isize / minsize;
// Allocation size must be multiple of large page size
if (isize % minsize)
blocks++;
isize = blocks * minsize;
type |= MEM_LARGE_PAGES;
} // Too small; no need for large pages
#if defined (_FORCE_LARGE_PAGES)
else
{
isize = minsize;
type |= MEM_LARGE_PAGES;
}
#endif
} // Error setting privileges
} // Error on privilege lookup
} // Error opening token
// If we failed to enable large page security above, the function will
allocate
// as normal; so no failure in this case
PVOID m = VirtualAlloc(NULL, isize, type, PAGE_READWRITE);
if (hToken != INVALID_HANDLE_VALUE)
{
tp.Privileges[0].Attributes = 0;
tp.PrivilegeCount = 1;
// disable privilege
AdjustTokenPrivileges(hToken, FALSE, &tp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
// close the handle
CloseHandle(hToken);
}
if (!m && (type & MEM_LARGE_PAGES))
{ // Large page allocation failed, revert back to normal allocation
type &= ~MEM_LARGE_PAGES;
m = VirtualAlloc(NULL, dwSize, type, PAGE_READWRITE);
}
else
{
}
return m;
#else
return VirtualAlloc(NULL, dwSize, type, PAGE_READWRITE);
#endif
#else
void*p = NULL;
if(0 == posix_memalign(&p, 0x1000, dwSize) )
return p;
return NULL;
#endif
}
On Mon, Sep 3, 2018 at 7:18 AM xxxxx@hotmail.com
wrote:
> > IIRC Anton is wrong however in that this privilege is granted (but not
> enabled) for
> > administrators by default.
>
>
> Please read my post more carefully. Look what I had said
>
>
>
>
>
> If it was granted by default there would be no problem whatsoever - the
> only thing that you would be required to do in such case is to enable this
> privilege in a token, which, unlike adding privileges to the account, may
> be done programatically. In fact, there would be no need to even mention
> it, in the first place. However, the fact that it requires user interaction
> with a console adds sort of an extra “complication”. Let’s face it -
> telling end users that they have to configure the OS in a certain way
> before your piece of hardware can be utilised does not really seem to add
> any extra selling points to your product, don’t you think…
>
>
>
> > This has changed from Windows version to version and it has been a long
> time
> >since I worked outside of a GPO controlled environment so I might have
> this mistaken
>
>
> Fair enough -assuming that this feature may change from one OS version to
> another, I have to admit that my practical experience with Windows as a
> user is VERY outdated. Unless we count booting up a new machine and taking
> whatever steps are necessary under the given OS version
> before “defenestration process” can be successfully launched, as a “user
> experience”, the last Windows version that I have practical experience
> with is XP.
>
>
>
> The only thing that I don’t understand is WHY it should be changing under
> different OS versions, in the first place. After all, the principle of
> “least privilege by default” seems to be pretty universal, and the ability
> of user apps to lock physical pages in RAM does not seem to be of crucial
> importance in 95+% of cases, does it…
>
>
> Anton Bassov
>
>
>
> —
> NTDEV is sponsored by OSR
>
> Visit the list online at: <
> http://www.osronline.com/showlists.cfm?list=ntdev>
>
> MONTHLY seminars on crash dump analysis, WDF, Windows internals and
> software drivers!
> Details at http:
>
> To unsubscribe, visit the List Server section of OSR Online at <
> http://www.osronline.com/page.cfm?name=ListServer>
>
–
Jamey Kirby
Disrupting the establishment since 1964
This is a personal email account and as such, emails are not subject to
archiving. Nothing else really matters.</http:>