SetProcessUserModeIOPL(VOID)
{
LPVOID Arg[2];
- UINT prev_error_mode;
HMODULE ntdll;
BOOL ret;
- /*
- * Load ntdll.dll library with disabled critical-error-handler and
- * file-not-found message box.
- * It means that NT kernel does not show unwanted GUI message box to user
- * when LoadLibrary() function fails.
- */
- prev_error_mode = win32_change_error_mode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
- ntdll = LoadLibrary(TEXT("ntdll.dll"));
- win32_change_error_mode(prev_error_mode);
+ /* Ntdll.dll is loaded into every process on all NT systems. */
+ ntdll = GetModuleHandle(TEXT("ntdll.dll"));
if (!ntdll)
{
SetLastError(ERROR_INVALID_FUNCTION);
Arg[0] = (LPVOID)GetProcAddress(ntdll, "NtSetInformationProcess");
if (!Arg[0])
{
- FreeLibrary(ntdll);
SetLastError(ERROR_INVALID_FUNCTION);
return FALSE;
}
/* Call ProcessUserModeIOPL with Tcb privilege. */
ret = win32_call_func_with_tcb_privilege(SetProcessUserModeIOPLFunc, (LPVOID)&Arg);
- FreeLibrary(ntdll);
-
if (!ret)
return FALSE;
#define DPMI_PHYSICAL_ADDRESS_MAPPING 0x0800
struct physmem {
- HMODULE ntdll;
HANDLE section_handle;
NTSTATUS (NTAPI *NtOpenSection)(PHANDLE SectionHandle, ACCESS_MASK DesiredAccess, POBJECT_ATTRIBUTES ObjectAttributes);
NTSTATUS (NTAPI *NtMapViewOfSection)(HANDLE SectionHandle, HANDLE ProcessHandle, PVOID *BaseAddress, ULONG_PTR ZeroBits, SIZE_T CommitSize, PLARGE_INTEGER SectionOffset, PSIZE_T ViewSize, SECTION_INHERIT InheritDisposition, ULONG AllocationType, ULONG Win32Protect);
wchar_t *wide_filename;
UNICODE_STRING unicode_filename;
OBJECT_ATTRIBUTES attributes;
- UINT prev_error_mode;
NTSTATUS status;
+ HMODULE ntdll;
int len;
a->debug("resolving section functions from ntdll.dll...");
- prev_error_mode = win32_change_error_mode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
- physmem->ntdll = LoadLibrary(TEXT("ntdll.dll"));
- win32_change_error_mode(prev_error_mode);
- if (!physmem->ntdll)
+ ntdll = GetModuleHandle(TEXT("ntdll.dll"));
+ if (!ntdll)
{
- a->debug("failed: cannot open ntdll.dll library: %s.", win32_strerror(GetLastError()));
+ a->debug("failed: library ntdll.dll is not present.");
errno = ENOENT;
return 0;
}
- physmem->RtlNtStatusToDosError = (LPVOID)GetProcAddress(physmem->ntdll, "RtlNtStatusToDosError");
+ physmem->RtlNtStatusToDosError = (LPVOID)GetProcAddress(ntdll, "RtlNtStatusToDosError");
- physmem->NtOpenSection = (LPVOID)GetProcAddress(physmem->ntdll, "NtOpenSection");
+ physmem->NtOpenSection = (LPVOID)GetProcAddress(ntdll, "NtOpenSection");
if (!physmem->NtOpenSection)
{
a->debug("failed: function NtOpenSection() not found.");
- FreeLibrary(physmem->ntdll);
- physmem->ntdll = NULL;
errno = ENOENT;
return 0;
}
- physmem->NtMapViewOfSection = (LPVOID)GetProcAddress(physmem->ntdll, "NtMapViewOfSection");
+ physmem->NtMapViewOfSection = (LPVOID)GetProcAddress(ntdll, "NtMapViewOfSection");
if (!physmem->NtMapViewOfSection)
{
a->debug("failed: function NtMapViewOfSection() not found.");
- FreeLibrary(physmem->ntdll);
- physmem->ntdll = NULL;
errno = ENOENT;
return 0;
}
- physmem->NtUnmapViewOfSection = (LPVOID)GetProcAddress(physmem->ntdll, "NtUnmapViewOfSection");
+ physmem->NtUnmapViewOfSection = (LPVOID)GetProcAddress(ntdll, "NtUnmapViewOfSection");
if (!physmem->NtUnmapViewOfSection)
{
a->debug("failed: function NtUnmapViewOfSection() not found.");
- FreeLibrary(physmem->ntdll);
- physmem->ntdll = NULL;
errno = ENOENT;
return 0;
}
if (len <= 0)
{
a->debug("Option devmem.path '%s' is invalid multibyte string.", filename);
- FreeLibrary(physmem->ntdll);
- physmem->ntdll = NULL;
errno = EINVAL;
return 0;
}
{
a->debug("Option devmem.path '%s' is invalid multibyte string.", filename);
pci_mfree(wide_filename);
- FreeLibrary(physmem->ntdll);
- physmem->ntdll = NULL;
errno = EINVAL;
return 0;
}
if (status < 0 || physmem->section_handle == INVALID_HANDLE_VALUE)
{
- FreeLibrary(physmem->ntdll);
- physmem->ntdll = NULL;
physmem->section_handle = INVALID_HANDLE_VALUE;
if (status == 0)
a->debug("failed.");
{
if (physmem->section_handle != INVALID_HANDLE_VALUE)
CloseHandle(physmem->section_handle);
- if (physmem->ntdll)
- FreeLibrary(physmem->ntdll);
pci_mfree(physmem);
}
static LUID luid_debug_privilege;
static BOOL revert_only_privilege;
static HANDLE revert_token;
-static HMODULE ntdll;
static int win32_sysdbg_initialized;
static int
win32_sysdbg_setup(struct pci_access *a)
{
- UINT prev_error_mode;
NTSTATUS status;
+ HMODULE ntdll;
ULONG ret_len;
DWORD id;
if (win32_sysdbg_initialized)
return 1;
- prev_error_mode = win32_change_error_mode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
- ntdll = LoadLibrary(TEXT("ntdll.dll"));
- win32_change_error_mode(prev_error_mode);
+ ntdll = GetModuleHandle(TEXT("ntdll.dll"));
if (!ntdll)
{
- a->debug("Cannot open ntdll.dll library.");
+ a->debug("Library ntdll.dll is not present.");
return 0;
}
if (!NtSystemDebugControl)
{
a->debug("Function NtSystemDebugControl() is not supported.");
- FreeLibrary(ntdll);
- ntdll = NULL;
return 0;
}
a->debug("NT SysDbg is disabled.");
else
a->debug("NT SysDbg returned error 0x%lx.", status);
- FreeLibrary(ntdll);
- ntdll = NULL;
NtSystemDebugControl = NULL;
return 0;
}
if (!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &luid_debug_privilege))
{
a->debug("Debug privilege is not supported.");
- FreeLibrary(ntdll);
- ntdll = NULL;
NtSystemDebugControl = NULL;
return 0;
}
if (!win32_enable_privilege(luid_debug_privilege, &revert_token, &revert_only_privilege))
{
a->debug("Cannot enable Debug privilege.");
- FreeLibrary(ntdll);
- ntdll = NULL;
NtSystemDebugControl = NULL;
return 0;
}
revert_token = NULL;
revert_only_privilege = FALSE;
- FreeLibrary(ntdll);
- ntdll = NULL;
NtSystemDebugControl = NULL;
if (status == STATUS_NOT_IMPLEMENTED || status == STATUS_INVALID_INFO_CLASS)
debug_privilege_enabled = FALSE;
}
- FreeLibrary(ntdll);
- ntdll = NULL;
NtSystemDebugControl = NULL;
win32_sysdbg_initialized = 0;