summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2019-02-19 18:36:46 -0800
committerGiampaolo Rodola <g.rodola@gmail.com>2019-02-19 18:36:46 -0800
commitb0ad9b6484c9d0b1907b2141664f631f481af73e (patch)
tree91bedda37de009f7435787f4eaaea3536164ba98
parent127a2e8b52564294b74c4f33e329fb314ebdbe9f (diff)
downloadpsutil-b0ad9b6484c9d0b1907b2141664f631f481af73e.tar.gz
fix #1419: Process.environ() raise NotImplementedError for 32-bit-WoW process
-rw-r--r--HISTORY.rst2
-rw-r--r--psutil/arch/windows/process_info.c78
-rwxr-xr-xpsutil/tests/test_windows.py6
3 files changed, 19 insertions, 67 deletions
diff --git a/HISTORY.rst b/HISTORY.rst
index b1eb8ea4..be3787e7 100644
--- a/HISTORY.rst
+++ b/HISTORY.rst
@@ -10,6 +10,8 @@ XXXX-XX-XX
- 1394_: [Windows] Process name() and exe() may erronously return "Registry".
QueryFullProcessImageNameW is now used instead of GetProcessImageFileNameW
in order to prevent that.
+- 1419_: [Windows] Process.environ() raises NotImplementedError when querying
+ a 64-bit process in 32-bit-WoW mode. Now it raises AccessDenied.
5.5.1
=====
diff --git a/psutil/arch/windows/process_info.c b/psutil/arch/windows/process_info.c
index 9001ab14..a2edca58 100644
--- a/psutil/arch/windows/process_info.c
+++ b/psutil/arch/windows/process_info.c
@@ -107,14 +107,6 @@ typedef enum {
MemoryInformationBasic
} MEMORY_INFORMATION_CLASS;
-typedef NTSTATUS (NTAPI *_NtWow64QueryVirtualMemory64)(
- IN HANDLE ProcessHandle,
- IN PVOID64 BaseAddress,
- IN MEMORY_INFORMATION_CLASS MemoryInformationClass,
- OUT PMEMORY_BASIC_INFORMATION64 MemoryInformation,
- IN ULONG64 Size,
- OUT PULONG64 ReturnLength OPTIONAL);
-
typedef struct {
PVOID Reserved1[2];
PVOID64 PebBaseAddress;
@@ -484,43 +476,6 @@ psutil_get_process_region_size(HANDLE hProcess, LPCVOID src, SIZE_T *psize) {
}
-#ifndef _WIN64
-/* Given a pointer into a process's memory, figure out how much data can be
- * read from it. */
-static int
-psutil_get_process_region_size64(HANDLE hProcess,
- const PVOID64 src64,
- PULONG64 psize) {
- static _NtWow64QueryVirtualMemory64 NtWow64QueryVirtualMemory64 = NULL;
- MEMORY_BASIC_INFORMATION64 info64;
-
- if (NtWow64QueryVirtualMemory64 == NULL) {
- NtWow64QueryVirtualMemory64 = psutil_GetProcAddressFromLib(
- "ntdll.dll", "NtWow64QueryVirtualMemory64");
- if (NtWow64QueryVirtualMemory64 == NULL) {
- PyErr_SetString(PyExc_NotImplementedError,
- "NtWow64QueryVirtualMemory64 missing");
- return -1;
- }
- }
-
- if (!NT_SUCCESS(NtWow64QueryVirtualMemory64(
- hProcess,
- src64,
- 0,
- &info64,
- sizeof(info64),
- NULL))) {
- PyErr_SetFromWindowsErr(0);
- return -1;
- }
-
- *psize = info64.RegionSize - ((char*)src64 - (char*)info64.BaseAddress);
- return 0;
-}
-#endif
-
-
enum psutil_process_data_kind {
KIND_CMDLINE,
KIND_CWD,
@@ -655,8 +610,18 @@ psutil_get_process_data(long pid,
psutil_GetProcAddressFromLib(
"ntdll.dll", "NtWow64QueryInformationProcess64");
if (NtWow64QueryInformationProcess64 == NULL) {
- PyErr_SetString(PyExc_NotImplementedError,
- "NtWow64QueryInformationProcess64 missing");
+ // Too complicated. Give up.
+ AccessDenied("can't query 64-bit process in 32-bit-WoW mode");
+ goto error;
+ }
+ }
+ if (NtWow64ReadVirtualMemory64 == NULL) {
+ NtWow64ReadVirtualMemory64 = \
+ psutil_GetProcAddressFromLib(
+ "ntdll.dll", "NtWow64ReadVirtualMemory64");
+ if (NtWow64ReadVirtualMemory64 == NULL) {
+ // Too complicated. Give up.
+ AccessDenied("can't query 64-bit process in 32-bit-WoW mode");
goto error;
}
}
@@ -672,17 +637,6 @@ psutil_get_process_data(long pid,
}
// read peb
- if (NtWow64ReadVirtualMemory64 == NULL) {
- NtWow64ReadVirtualMemory64 = \
- psutil_GetProcAddressFromLib(
- "ntdll.dll", "NtWow64ReadVirtualMemory64");
- if (NtWow64ReadVirtualMemory64 == NULL) {
- PyErr_SetString(PyExc_NotImplementedError,
- "NtWow64ReadVirtualMemory64 missing");
- goto error;
- }
- }
-
if (!NT_SUCCESS(NtWow64ReadVirtualMemory64(hProcess,
pbi64.PebBaseAddress,
&peb64,
@@ -771,12 +725,8 @@ psutil_get_process_data(long pid,
if (kind == KIND_ENVIRON) {
#ifndef _WIN64
if (weAreWow64 && !theyAreWow64) {
- ULONG64 size64;
-
- if (psutil_get_process_region_size64(hProcess, src64, &size64) != 0)
- goto error;
-
- size = (SIZE_T)size64;
+ AccessDenied("can't query 64-bit process in 32-bit-WoW mode");
+ goto error;
}
else
#endif
diff --git a/psutil/tests/test_windows.py b/psutil/tests/test_windows.py
index 35ea6217..9cb2624a 100755
--- a/psutil/tests/test_windows.py
+++ b/psutil/tests/test_windows.py
@@ -774,10 +774,10 @@ class RemoteProcessTestCase(unittest.TestCase):
self.assertEquals(e["THINK_OF_A_NUMBER"], str(os.getpid()))
def test_environ_64(self):
+ # Environ 32 is not supported.
p = psutil.Process(self.proc64.pid)
- e = p.environ()
- self.assertIn("THINK_OF_A_NUMBER", e)
- self.assertEquals(e["THINK_OF_A_NUMBER"], str(os.getpid()))
+ with self.assertRaises(psutil.AccessDenied):
+ p.environ()
# ===================================================================