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

Check PID range before any cext call #2266

Merged
merged 9 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ I: 1598

N: Xuehai Pan
W: https://github.com/XuehaiPan
I: 1948
I: 1948, 2264

N: Saeed Rasooli
W: https://github.com/ilius
Expand Down
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ XXXX-XX-XX

- 2241_, [NetBSD]: can't compile On NetBSD 10.99.3/amd64. (patch by Thomas
Klausner)
- 2266_, Check PID range before any cext call. (patch by Xuehai Pan)

5.9.5
=====
Expand Down
5 changes: 5 additions & 0 deletions psutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,11 @@ def _init(self, pid, _ignore_nsp=False):
self._exitcode = _SENTINEL
# cache creation time for later use in is_running() method
try:
try:
_psplatform.cext._check_pid_range(pid)
except OverflowError:
raise NoSuchProcess(pid, msg="process PID out of range")

XuehaiPan marked this conversation as resolved.
Show resolved Hide resolved
self.create_time()
except AccessDenied:
# We should never get here as AFAIK we're able to get
Expand Down
1 change: 1 addition & 0 deletions psutil/_psutil_aix.c
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,7 @@ PsutilMethods[] =
{"net_if_stats", psutil_net_if_stats, METH_VARARGS},

// --- others
{"_check_pid_range", psutil_check_pid_range, METH_VARARGS},
XuehaiPan marked this conversation as resolved.
Show resolved Hide resolved
{"set_debug", psutil_set_debug, METH_VARARGS},

{NULL, NULL, 0, NULL}
Expand Down
1 change: 1 addition & 0 deletions psutil/_psutil_bsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ static PyMethodDef mod_methods[] = {
{"sensors_cpu_temperature", psutil_sensors_cpu_temperature, METH_VARARGS},
#endif
// --- others
{"_check_pid_range", psutil_check_pid_range, METH_VARARGS},
{"set_debug", psutil_set_debug, METH_VARARGS},

{NULL, NULL, 0, NULL}
Expand Down
21 changes: 21 additions & 0 deletions psutil/_psutil_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,27 @@ AccessDenied(const char *syscall) {
return NULL;
}

/*
* Raise OverflowError if Python int value overflowed when converting to pid_t.
* Raise ValueError if Python int value is negative.
* Otherwise, return None.
*/
PyObject *
psutil_check_pid_range(PyObject *self, PyObject *args) {
#ifdef PSUTIL_WINDOWS
DWORD pid;
#else
pid_t pid;
#endif

if (!PyArg_ParseTuple(args, _Py_PARSE_PID, &pid))
return NULL;
if (pid < 0) {
PyErr_SetString(PyExc_ValueError, "pid must be a positive integer");
return NULL;
}
Py_RETURN_NONE;
}

// Enable or disable PSUTIL_DEBUG messages.
PyObject *
Expand Down
2 changes: 2 additions & 0 deletions psutil/_psutil_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ PyObject* PyErr_SetFromOSErrnoWithSyscall(const char *syscall);
// --- Global utils
// ====================================================================

PyObject* psutil_check_pid_range(PyObject *self, PyObject *args);

PyObject* psutil_set_debug(PyObject *self, PyObject *args);
int psutil_setup(void);

Expand Down
1 change: 1 addition & 0 deletions psutil/_psutil_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ static PyMethodDef mod_methods[] = {
// --- linux specific
{"linux_sysinfo", psutil_linux_sysinfo, METH_VARARGS},
// --- others
{"_check_pid_range", psutil_check_pid_range, METH_VARARGS},
{"set_debug", psutil_set_debug, METH_VARARGS},

{NULL, NULL, 0, NULL}
Expand Down
1 change: 1 addition & 0 deletions psutil/_psutil_osx.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ static PyMethodDef mod_methods[] = {
{"virtual_mem", psutil_virtual_mem, METH_VARARGS},

// --- others
{"_check_pid_range", psutil_check_pid_range, METH_VARARGS},
{"set_debug", psutil_set_debug, METH_VARARGS},

{NULL, NULL, 0, NULL}
Expand Down
1 change: 1 addition & 0 deletions psutil/_psutil_sunos.c
Original file line number Diff line number Diff line change
Expand Up @@ -1659,6 +1659,7 @@ PsutilMethods[] = {
{"users", psutil_users, METH_VARARGS},

// --- others
{"_check_pid_range", psutil_check_pid_range, METH_VARARGS},
{"set_debug", psutil_set_debug, METH_VARARGS},

{NULL, NULL, 0, NULL}
Expand Down
1 change: 1 addition & 0 deletions psutil/_psutil_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ PsutilMethods[] = {
{"QueryDosDevice", psutil_QueryDosDevice, METH_VARARGS},

// --- others
{"_check_pid_range", psutil_check_pid_range, METH_VARARGS},
{"set_debug", psutil_set_debug, METH_VARARGS},

{NULL, NULL, 0, NULL}
Expand Down
4 changes: 4 additions & 0 deletions psutil/tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@

class TestSpecialMethods(PsutilTestCase):

def test_check_pid_range(self):
with self.assertRaises(OverflowError):
psutil._psplatform.cext._check_pid_range(2 ** 128)
XuehaiPan marked this conversation as resolved.
Show resolved Hide resolved

def test_process__repr__(self, func=repr):
p = psutil.Process(self.spawn_testproc().pid)
r = func(p)
Expand Down