Skip to content

Commit

Permalink
#fix #1595 / windows: kill() may not raise AccessDenied
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Nov 20, 2019
1 parent 1f8d432 commit 72c84cb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Expand Up @@ -7,6 +7,7 @@ XXXX-XX-XX

**Bug fixes**

- 1595_: [Windows] Process.kill() may not throw AccessDenied.
- 1616_: use of Py_DECREF instead of Py_CLEAR will result in double free and
segfault (CVE). (patch by Riccardo Schirone)
- 1619_: [OpenBSD] compilation fails due to C syntax error. (patch by Nathan
Expand Down
30 changes: 23 additions & 7 deletions psutil/_psutil_windows.c
Expand Up @@ -243,8 +243,8 @@ psutil_pids(PyObject *self, PyObject *args) {
static PyObject *
psutil_proc_kill(PyObject *self, PyObject *args) {
HANDLE hProcess;
DWORD err;
long pid;
DWORD exitCode;

if (! PyArg_ParseTuple(args, "l", &pid))
return NULL;
Expand All @@ -265,19 +265,35 @@ psutil_proc_kill(PyObject *self, PyObject *args) {
return NULL;
}

// kill the process
if (! TerminateProcess(hProcess, SIGTERM)) {
err = GetLastError();
// See: https://github.com/giampaolo/psutil/issues/1099
if (err != ERROR_ACCESS_DENIED) {
PyErr_SetFromOSErrnoWithSyscall("TerminateProcess");
if (GetLastError() == ERROR_ACCESS_DENIED) {
// ERROR_ACCESS_DENIED (winerror 5) may happen if the
// process already died. See:
// https://github.com/giampaolo/psutil/issues/1099
// https://github.com/giampaolo/psutil/issues/1595
if (GetExitCodeProcess(hProcess, &exitCode) == 0) {
PyErr_SetFromOSErrnoWithSyscall("GetExitCodeProcess");
goto error;
}
if (exitCode == STILL_ACTIVE) {
PyErr_SetFromOSErrnoWithSyscall("TerminateProcess");
goto error;
}
CloseHandle(hProcess);
return NULL;
Py_RETURN_NONE;
}
else {
PyErr_SetFromOSErrnoWithSyscall("TerminateProcess");
goto error;
}
}

CloseHandle(hProcess);
Py_RETURN_NONE;

error:
CloseHandle(hProcess);
return NULL;
}


Expand Down
2 changes: 1 addition & 1 deletion scripts/internal/download_exes.py
Expand Up @@ -26,7 +26,7 @@


BASE_URL = 'https://ci.appveyor.com/api'
PY_VERSIONS = ['2.7', '3.5', '3.6', '3.7']
PY_VERSIONS = ['2.7', '3.5', '3.6', '3.7', '3.8']
TIMEOUT = 30
COLORS = True

Expand Down

0 comments on commit 72c84cb

Please sign in to comment.