Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle STATUS_INVALID_INFO_CLASS in psutil_proc_exe #2493

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions psutil/arch/windows/proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@

// Fixes clash between winsock2.h and windows.h
#define WIN32_LEAN_AND_MEAN
// Fixes clash between ntstatus.h and windows.h
#define UMDF_USING_NTSTATUS

#include <Python.h>
#include <windows.h>
#include <ntstatus.h>
#include <Psapi.h> // memory_info(), memory_maps()
#include <signal.h>
#include <tlhelp32.h> // threads(), PROCESSENTRY32
Expand Down Expand Up @@ -261,6 +264,8 @@ psutil_proc_exe(PyObject *self, PyObject *args) {
ULONG bufferSize = 0x104 * 2; // WIN_MAX_PATH * sizeof(wchar_t)
SYSTEM_PROCESS_ID_INFORMATION processIdInfo;
PyObject *py_exe;
HANDLE hProcess;
DWORD size;

if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid))
return NULL;
Expand Down Expand Up @@ -333,6 +338,39 @@ psutil_proc_exe(PyObject *self, PyObject *args) {
sizeof(SYSTEM_PROCESS_ID_INFORMATION),
NULL);
}
else if (status == STATUS_INVALID_INFO_CLASS) {
FREE(buffer);
hProcess = psutil_handle_from_pid(pid, PROCESS_QUERY_LIMITED_INFORMATION);

if (NULL == hProcess)
return NULL;

buffer = MALLOC_ZERO(bufferSize);
if (! buffer) {
PyErr_NoMemory();
return NULL;
}

size = bufferSize / 2;

if (QueryFullProcessImageNameW(hProcess, 0, buffer, &size) == 0) {
FREE(buffer);
// https://github.com/giampaolo/psutil/issues/1662
if (GetLastError() == 0)
AccessDenied("QueryFullProcessImageNameW (forced EPERM)");
else
psutil_PyErr_SetFromOSErrnoWithSyscall("QueryFullProcessImageNameW");
CloseHandle(hProcess);
return NULL;
}

CloseHandle(hProcess);

processIdInfo.ImageName.Buffer = buffer;
processIdInfo.ImageName.Length = (USHORT)(size * 2);

status = STATUS_SUCCESS;
}

if (! NT_SUCCESS(status)) {
FREE(buffer);
Expand Down
Loading