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

(Windows) use PROCESS_QUERY_LIMITED_INFORMATION access rights #1376

Merged
merged 3 commits into from
Dec 8, 2018
Merged
Show file tree
Hide file tree
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
44 changes: 24 additions & 20 deletions psutil/_psutil_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,8 @@ psutil_proc_cpu_times(PyObject *self, PyObject *args) {
if (! PyArg_ParseTuple(args, "l", &pid))
return NULL;

hProcess = psutil_handle_from_pid(pid);
hProcess = psutil_handle_from_pid(pid, PROCESS_QUERY_LIMITED_INFORMATION);

if (hProcess == NULL)
return NULL;
if (! GetProcessTimes(hProcess, &ftCreate, &ftExit, &ftKernel, &ftUser)) {
Expand Down Expand Up @@ -546,7 +547,7 @@ psutil_proc_create_time(PyObject *self, PyObject *args) {
if (0 == pid || 4 == pid)
return psutil_boot_time(NULL, NULL);

hProcess = psutil_handle_from_pid(pid);
hProcess = psutil_handle_from_pid(pid, PROCESS_QUERY_LIMITED_INFORMATION);
if (hProcess == NULL)
return NULL;
if (! GetProcessTimes(hProcess, &ftCreate, &ftExit, &ftKernel, &ftUser)) {
Expand Down Expand Up @@ -756,7 +757,7 @@ psutil_proc_exe(PyObject *self, PyObject *args) {

if (! PyArg_ParseTuple(args, "l", &pid))
return NULL;
hProcess = psutil_handle_from_pid_waccess(pid, PROCESS_QUERY_INFORMATION);
hProcess = psutil_handle_from_pid(pid, PROCESS_QUERY_LIMITED_INFORMATION);
if (NULL == hProcess)
return NULL;
if (GetProcessImageFileNameW(hProcess, exe, MAX_PATH) == 0) {
Expand Down Expand Up @@ -824,7 +825,7 @@ psutil_proc_memory_info(PyObject *self, PyObject *args) {
if (! PyArg_ParseTuple(args, "l", &pid))
return NULL;

hProcess = psutil_handle_from_pid(pid);
hProcess = psutil_handle_from_pid(pid, PROCESS_QUERY_LIMITED_INFORMATION);
if (NULL == hProcess)
return NULL;

Expand Down Expand Up @@ -892,6 +893,8 @@ psutil_proc_memory_uss(PyObject *self, PyObject *args)
size_t private_pages;
size_t i;
DWORD info_array_size;
// needed by QueryWorkingSet
DWORD access = PROCESS_QUERY_INFORMATION | PROCESS_VM_READ;
PSAPI_WORKING_SET_INFORMATION* info_array;
SYSTEM_INFO system_info;
PyObject* py_result = NULL;
Expand All @@ -900,7 +903,8 @@ psutil_proc_memory_uss(PyObject *self, PyObject *args)
if (! PyArg_ParseTuple(args, "l", &pid))
return NULL;

proc = psutil_handle_from_pid(pid);

proc = psutil_handle_from_pid(pid, access);
if (proc == NULL)
return NULL;

Expand Down Expand Up @@ -1350,7 +1354,7 @@ psutil_proc_open_files(PyObject *self, PyObject *args) {
if (! PyArg_ParseTuple(args, "l", &pid))
return NULL;

processHandle = psutil_handle_from_pid_waccess(pid, access);
processHandle = psutil_handle_from_pid(pid, access);
if (processHandle == NULL)
return NULL;
py_retlist = psutil_get_open_files(pid, processHandle);
Expand Down Expand Up @@ -1412,8 +1416,7 @@ psutil_proc_username(PyObject *self, PyObject *args) {
if (! PyArg_ParseTuple(args, "l", &pid))
return NULL;

processHandle = psutil_handle_from_pid_waccess(
pid, PROCESS_QUERY_INFORMATION);
processHandle = psutil_handle_from_pid(pid, PROCESS_QUERY_INFORMATION);
if (processHandle == NULL)
return NULL;

Expand Down Expand Up @@ -2055,7 +2058,7 @@ psutil_proc_priority_get(PyObject *self, PyObject *args) {

if (! PyArg_ParseTuple(args, "l", &pid))
return NULL;
hProcess = psutil_handle_from_pid(pid);
hProcess = psutil_handle_from_pid(pid, PROCESS_QUERY_LIMITED_INFORMATION);
if (hProcess == NULL)
return NULL;
priority = GetPriorityClass(hProcess);
Expand All @@ -2079,7 +2082,7 @@ psutil_proc_priority_set(PyObject *self, PyObject *args) {

if (! PyArg_ParseTuple(args, "li", &pid, &priority))
return NULL;
hProcess = psutil_handle_from_pid_waccess(pid, access);
hProcess = psutil_handle_from_pid(pid, access);
if (hProcess == NULL)
return NULL;
retval = SetPriorityClass(hProcess, priority);
Expand All @@ -2106,7 +2109,7 @@ psutil_proc_io_priority_get(PyObject *self, PyObject *args) {

if (! PyArg_ParseTuple(args, "l", &pid))
return NULL;
hProcess = psutil_handle_from_pid(pid);
hProcess = psutil_handle_from_pid(pid, PROCESS_QUERY_LIMITED_INFORMATION);
if (hProcess == NULL)
return NULL;

Expand All @@ -2130,7 +2133,7 @@ psutil_proc_io_priority_set(PyObject *self, PyObject *args) {
long pid;
DWORD prio;
HANDLE hProcess;
DWORD dwDesiredAccess = PROCESS_QUERY_INFORMATION | PROCESS_SET_INFORMATION;
DWORD access = PROCESS_QUERY_INFORMATION | PROCESS_SET_INFORMATION;

_NtSetInformationProcess NtSetInformationProcess =
(_NtSetInformationProcess)GetProcAddress(
Expand All @@ -2144,7 +2147,7 @@ psutil_proc_io_priority_set(PyObject *self, PyObject *args) {

if (! PyArg_ParseTuple(args, "li", &pid, &prio))
return NULL;
hProcess = psutil_handle_from_pid_waccess(pid, dwDesiredAccess);
hProcess = psutil_handle_from_pid(pid, access);
if (hProcess == NULL)
return NULL;

Expand Down Expand Up @@ -2172,7 +2175,7 @@ psutil_proc_io_counters(PyObject *self, PyObject *args) {

if (! PyArg_ParseTuple(args, "l", &pid))
return NULL;
hProcess = psutil_handle_from_pid(pid);
hProcess = psutil_handle_from_pid(pid, PROCESS_QUERY_LIMITED_INFORMATION);
if (NULL == hProcess)
return NULL;
if (! GetProcessIoCounters(hProcess, &IoCounters)) {
Expand Down Expand Up @@ -2202,7 +2205,7 @@ psutil_proc_cpu_affinity_get(PyObject *self, PyObject *args) {

if (! PyArg_ParseTuple(args, "l", &pid))
return NULL;
hProcess = psutil_handle_from_pid(pid);
hProcess = psutil_handle_from_pid(pid, PROCESS_QUERY_LIMITED_INFORMATION);
if (hProcess == NULL) {
return NULL;
}
Expand All @@ -2227,8 +2230,7 @@ static PyObject *
psutil_proc_cpu_affinity_set(PyObject *self, PyObject *args) {
DWORD pid;
HANDLE hProcess;
DWORD dwDesiredAccess = \
PROCESS_QUERY_INFORMATION | PROCESS_SET_INFORMATION;
DWORD access = PROCESS_QUERY_INFORMATION | PROCESS_SET_INFORMATION;
DWORD_PTR mask;

#ifdef _WIN64
Expand All @@ -2239,7 +2241,7 @@ psutil_proc_cpu_affinity_set(PyObject *self, PyObject *args) {
{
return NULL;
}
hProcess = psutil_handle_from_pid_waccess(pid, dwDesiredAccess);
hProcess = psutil_handle_from_pid(pid, access);
if (hProcess == NULL)
return NULL;

Expand Down Expand Up @@ -2877,7 +2879,7 @@ psutil_proc_num_handles(PyObject *self, PyObject *args) {

if (! PyArg_ParseTuple(args, "l", &pid))
return NULL;
hProcess = psutil_handle_from_pid(pid);
hProcess = psutil_handle_from_pid(pid, PROCESS_QUERY_LIMITED_INFORMATION);
if (NULL == hProcess)
return NULL;
if (! GetProcessHandleCount(hProcess, &handleCount)) {
Expand Down Expand Up @@ -3025,6 +3027,8 @@ psutil_proc_memory_maps(PyObject *self, PyObject *args) {
WCHAR mappedFileName[MAX_PATH];
SYSTEM_INFO system_info;
LPVOID maxAddr;
// required by GetMappedFileNameW
DWORD access = PROCESS_QUERY_INFORMATION | PROCESS_VM_READ;
PyObject *py_retlist = PyList_New(0);
PyObject *py_tuple = NULL;
PyObject *py_str = NULL;
Expand All @@ -3033,7 +3037,7 @@ psutil_proc_memory_maps(PyObject *self, PyObject *args) {
return NULL;
if (! PyArg_ParseTuple(args, "l", &pid))
goto error;
hProcess = psutil_handle_from_pid(pid);
hProcess = psutil_handle_from_pid(pid, access);
if (NULL == hProcess)
goto error;

Expand Down
17 changes: 3 additions & 14 deletions psutil/arch/windows/process_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ psutil_check_phandle(HANDLE hProcess, DWORD pid) {
* Return a process handle or NULL.
*/
HANDLE
psutil_handle_from_pid_waccess(DWORD pid, DWORD dwDesiredAccess) {
psutil_handle_from_pid(DWORD pid, DWORD dwDesiredAccess) {
HANDLE hProcess;

if (pid == 0) {
Expand All @@ -285,18 +285,6 @@ psutil_handle_from_pid_waccess(DWORD pid, DWORD dwDesiredAccess) {
}


/*
* Same as psutil_handle_from_pid_waccess but implicitly uses
* PROCESS_QUERY_INFORMATION | PROCESS_VM_READ as dwDesiredAccess
* parameter for OpenProcess.
*/
HANDLE
psutil_handle_from_pid(DWORD pid) {
DWORD dwDesiredAccess = PROCESS_QUERY_INFORMATION | PROCESS_VM_READ;
return psutil_handle_from_pid_waccess(pid, dwDesiredAccess);
}


DWORD *
psutil_get_pids(DWORD *numberOfReturnedPIDs) {
// Win32 SDK says the only way to know if our process array
Expand Down Expand Up @@ -553,8 +541,9 @@ static int psutil_get_process_data(long pid,
BOOL weAreWow64;
BOOL theyAreWow64;
#endif
DWORD access = PROCESS_QUERY_INFORMATION | PROCESS_VM_READ;

hProcess = psutil_handle_from_pid(pid);
hProcess = psutil_handle_from_pid(pid, access);
if (hProcess == NULL)
return -1;

Expand Down
3 changes: 1 addition & 2 deletions psutil/arch/windows/process_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@


DWORD* psutil_get_pids(DWORD *numberOfReturnedPIDs);
HANDLE psutil_handle_from_pid(DWORD pid);
HANDLE psutil_handle_from_pid_waccess(DWORD pid, DWORD dwDesiredAccess);
HANDLE psutil_handle_from_pid(DWORD pid, DWORD dwDesiredAccess);
int psutil_pid_is_running(DWORD pid);
int psutil_get_proc_info(DWORD pid, PSYSTEM_PROCESS_INFORMATION *retProcess,
PVOID *retBuffer);
Expand Down