Locking pages in memory (Windows systems)

Status
Not open for further replies.

Feshy

Ars Tribunus Militum
2,777
I want to write my own custom allocators (and, if necessary, free memory manager) that can be used to build objects that are never paged to disk. I've written allocators before, and had some experience with free memory managers. But I don't know how to lock a page into RAM, or even if it can be done in windows. I know under unix, a process can be locked into memory, but I would like more granularity than per-process. And, the target is a windows environment.

The "idea," basically, is security: I don't want the data to touch any persistent medium. It may be an unnecessary step, but mostly I'm curious to see if it can be done.

Ideas?
 
Even pages locked with <tt>VirtualLock()</tt> can be paged out if all the threads in that process are not running. What <tt>VirtualLock()</tt> does is to place pages into a process's working set (which is why the number of pages you can lock corresponds to the size of your working set). <tt>VirtualLock()</tt> is a performance feature, not a security feature, and so this behaviour is fine -- if your process is paged out in entirety, its performance is nil anyway; all <tt>VirtualLock()</tt> does is to guarantee that when your process is running it doesn't have to incur any paging when accessing that memory.

Further, the re-use requirements of C2 OSes mean that any new files can't let one see what was on-disk prior to their creation (that is to say, a new file can't accidentally contain the contents of an old, deleted file). Similarly, any memory-mapped chunk of pagefile can't let one see historic data.

The only way one could read the data is to either invade the process (attach to it with SeDebugPrivilege and read its memory), read the PhysicalMemory section object (though finding the data would be pot luck), or, if it's stored somewhere on disk, to read the appropriate physical disk object. The first of those requires SeDebugPrivilege (granted to Administrators by default), the last requires Administrator privileges (due to the ACL on the object). I think the second requires Administrator privileges, again due to the object's ACL, thought it could actually be even stricter than that (one may have to run under the system context -- though Administrators can do this). IOW, the only people who could obtain data written out to the backing store could acquire it by other (simpler) means anyway.

There is an issue if the data is paged out and the system is brought down in a disorderly fashion (so that even if one tells the system to clear the pagefile on shutdown this doesn't get done) and someone has physical access to the machine so that they might boot it into a different OS and read the pagefile... but given that level of access, there are easier solutions to the problem.

There is an internal (and I think undocumented) API call called <tt>ZwLockVirtualMemory()</tt> (and its corresponding unlock function, <tt>ZwUnlockVirtualMemory()</tt>), and their corresponding user-mode NTDLL exports, <tt>NtLockVirtualMemory()</tt> and <tt>NtUnlockVirtualMemory()</tt>, which can both lock pages into the process's working set and into RAM.

It can be used something like this. It requires the "lock pages in physical memory" privilege (which regular <tt>VirtualLock()</tt> doesn't, I believe).

Code:
typedef LONG NTSTATUS;
#define NTAPI __stdcall
// these functions have identical prototypes
typedef NTSTATUS (NTAPI* NLVM)(IN HANDLE process, IN OUT void** baseAddress, IN OUT ULONG* size, IN ULONG flags);
typedef NTSTATUS (NTAPI* NULVM)(IN HANDLE process, IN OUT void** baseAddress, IN OUT ULONG* size, IN ULONG flags);

#define LOCK_VM_IN_WORKING_SET 1
#define LOCK_VM_IN_RAM 2

BOOL EnablePrivilege(PCSTR name)
{
	TOKEN_PRIVILEGES priv = {1, {0, 0, SE_PRIVILEGE_ENABLED}};
	LookupPrivilegeValue(0, name, &priv.Privileges[0].Luid);
	HANDLE hToken;
	OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken);
	AdjustTokenPrivileges(hToken, FALSE, &priv, sizeof priv, 0, 0);
	BOOL rv = GetLastError() == ERROR_SUCCESS;
	CloseHandle(hToken);
	return rv;
}

int main(int argc, char* argv[])
{
	// if the lock pages in physical memory privilege is not held, the lock/unlock functions will return 0xc0000061 -- "privilege not held"
	EnablePrivilege(SE_LOCK_MEMORY_NAME);
	NLVM NtLockVirtualMemory = (NLVM)GetProcAddress(GetModuleHandle("NTDLL.DLL"), "NtLockVirtualMemory");
	NULVM NtUnlockVirtualMemory = (NULVM)GetProcAddress(GetModuleHandle("NTDLL.DLL"), "NtUnlockVirtualMemory");
	SIZE_T s = 1024;
	void* m = VirtualAlloc(0, s, MEM_COMMIT, PAGE_READWRITE);
	//. the meanings of error values from these functions are found in the DDK.  They do *not* correspond with regular Win32 error values.
	NtLockVirtualMemory(GetCurrentProcess(), &m, &s, LOCK_VM_IN_WORKING_SET | LOCK_VM_IN_RAM);
	// memory is now locked both into the working set and into physical memory
	NtUnlockVirtualMemory(GetCurrentProcess(), &m, &s, LOCK_VM_IN_WORKING_SET | LOCK_VM_IN_RAM); // I believe the flags must match
	// and now it's not
}

It may be the case that <tt>AllocateUserPhysicalPages()</tt> and its AWE friends can be used to achieve something similar. If you feel it's really worthwhile, you could look at them.
 
Status
Not open for further replies.