From 72c84cb4edb5c0968a83c1f45ad5cc51235e0af3 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 20 Nov 2019 00:46:35 -0800 Subject: #fix #1595 / windows: kill() may not raise AccessDenied --- HISTORY.rst | 1 + psutil/_psutil_windows.c | 30 +++++++++++++++++++++++------- scripts/internal/download_exes.py | 2 +- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 5aaf9da1..045bb5d5 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -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 diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 08b208dc..e01fa50d 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -243,8 +243,8 @@ error: 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; @@ -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; } diff --git a/scripts/internal/download_exes.py b/scripts/internal/download_exes.py index 1b72a177..4a559bb0 100755 --- a/scripts/internal/download_exes.py +++ b/scripts/internal/download_exes.py @@ -26,7 +26,7 @@ from scriptutils import printerr, exit 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 -- cgit v1.2.1