From 3b6f258dc91a368d6207e8305bfc66600c68ab11 Mon Sep 17 00:00:00 2001 From: MonocleAI <54963236+monocle-ai@users.noreply.github.com> Date: Wed, 14 Oct 2020 08:34:11 -0700 Subject: [PATCH 1/4] bpo-42036:Fixed unchecked return in Modules/posixmodule.c **Instance 1** File : `Modules/posixmodule.c` Enclosing Function : `os_times_impl` Function : `GetProcessTimes@20` https://github.com/siva-msft/cpython/blob/582aaf19e8b94a70c1f96792197770d604ba0fdf/Modules/posixmodule.c#L8505 **Issue in**: _user, kernel_ **Code extract**: ```cpp FILETIME create, exit, kernel, user; HANDLE hProc; hProc = GetCurrentProcess(); GetProcessTimes(hProc, &create, &exit, &kernel, &user); <------ HERE /* The fields of a FILETIME structure are the hi and lo part of a 64-bit value expressed in 100 nanosecond units. ``` **How can I fix it?** Correct reference usage found in `Modules/timemodule.c` at line `1133`. https://github.com/siva-msft/cpython/blob/582aaf19e8b94a70c1f96792197770d604ba0fdf/Modules/timemodule.c#L1133 **Code extract**: ```cpp BOOL ok; process = GetCurrentProcess(); ok = GetProcessTimes(process, &creation_time, &exit_time, <------ HERE &kernel_time, &user_time); if (!ok) { ``` --- **Instance 2** File : `Modules/posixmodule.c` Enclosing Function : `posixmodule_exec` Function : `dir_fd_converter` https://github.com/siva-msft/cpython/blob/582aaf19e8b94a70c1f96792197770d604ba0fdf/Modules/posixmodule.c#L15060 **Issue in**: _ignored_ **Code extract**: ```cpp fd_specified("", -1); follow_symlinks_specified("", 1); dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1); dir_fd_converter(Py_None, &ignored); <------ HERE dir_fd_unavailable(Py_None, &ignored); } ``` **How can I fix it?** Correct reference usage found in `Modules/posixmodule.c` at line `1258`. https://github.com/siva-msft/cpython/blob/582aaf19e8b94a70c1f96792197770d604ba0fdf/Modules/posixmodule.c#L1258 **Code extract**: ```cpp dir_fd_unavailable(PyObject *o, void *p) { int dir_fd; if (!dir_fd_converter(o, &dir_fd)) <------ HERE return 0; if (dir_fd != DEFAULT_DIR_FD) { ``` --- Modules/posixmodule.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 6ce0bcb9fe8ca48..e0b3031d64e44b8 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -8490,7 +8490,11 @@ os_times_impl(PyObject *module) FILETIME create, exit, kernel, user; HANDLE hProc; hProc = GetCurrentProcess(); - GetProcessTimes(hProc, &create, &exit, &kernel, &user); + ok = GetProcessTimes(hProc, &create, &exit, &kernel, &user); + if (!ok) { + PyErr_SetFromWindowsErr(0); + return -1; + } /* The fields of a FILETIME structure are the hi and lo part of a 64-bit value expressed in 100 nanosecond units. 1e7 is one second in such units; 1e-7 the inverse. @@ -15022,7 +15026,8 @@ posixmodule_exec(PyObject *m) fd_specified("", -1); follow_symlinks_specified("", 1); dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1); - dir_fd_converter(Py_None, &ignored); + if(!dir_fd_converter(Py_None, &ignored)) + return 0; dir_fd_unavailable(Py_None, &ignored); } From 184714cc053cf05065e07325df31d958dd5c4573 Mon Sep 17 00:00:00 2001 From: MonocleAI <54963236+monocle-ai@users.noreply.github.com> Date: Wed, 14 Oct 2020 08:40:06 -0700 Subject: [PATCH 2/4] Update posixmodule.c --- Modules/posixmodule.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index e0b3031d64e44b8..a277b9574b372f1 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -8490,6 +8490,7 @@ os_times_impl(PyObject *module) FILETIME create, exit, kernel, user; HANDLE hProc; hProc = GetCurrentProcess(); + BOOL ok; ok = GetProcessTimes(hProc, &create, &exit, &kernel, &user); if (!ok) { PyErr_SetFromWindowsErr(0); From 6045037158a8db1dd99cdfd9521e573237f67492 Mon Sep 17 00:00:00 2001 From: MonocleAI <54963236+monocle-ai@users.noreply.github.com> Date: Thu, 15 Oct 2020 07:34:08 -0700 Subject: [PATCH 3/4] Created 2020-10-15-07-33-50.bpo-42036.MonocleAI.rst --- .../next/Library/2020-10-15-07-33-50.bpo-42036.MonocleAI.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2020-10-15-07-33-50.bpo-42036.MonocleAI.rst diff --git a/Misc/NEWS.d/next/Library/2020-10-15-07-33-50.bpo-42036.MonocleAI.rst b/Misc/NEWS.d/next/Library/2020-10-15-07-33-50.bpo-42036.MonocleAI.rst new file mode 100644 index 000000000000000..5e0d6b90afa0539 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-10-15-07-33-50.bpo-42036.MonocleAI.rst @@ -0,0 +1 @@ +Fix unchecked return in Modules/posixmodule.c. The return value of a function that is potentially used to initialize a local variable is not checked. Therefore, reading the local variable may result in an undefined behavior. From 1f5be7493dbf08bd575dbb2c0092ab20ec228996 Mon Sep 17 00:00:00 2001 From: MonocleAI <54963236+monocle-ai@users.noreply.github.com> Date: Mon, 2 Nov 2020 14:16:26 -0800 Subject: [PATCH 4/4] Complying to PEP 7 --- Modules/posixmodule.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index a277b9574b372f1..84dc1496d8d5001 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -15028,7 +15028,9 @@ posixmodule_exec(PyObject *m) follow_symlinks_specified("", 1); dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1); if(!dir_fd_converter(Py_None, &ignored)) + { return 0; + } dir_fd_unavailable(Py_None, &ignored); }