From 9243e51f7618ad77264ecb3ae45332a3caeb5f9f Mon Sep 17 00:00:00 2001 From: Antonio Gutierrez Date: Sat, 5 Oct 2019 21:26:05 +0200 Subject: [PATCH 1/3] crypt: Check error of primitive crypt/crypt_r Checks also for encryption algorithms methods not supported in different OSs. Fixes partially: https://bugs.python.org/issue36161 Signed-off-by: Antonio Gutierrez --- Lib/crypt.py | 10 +++++++++- Modules/_cryptmodule.c | 3 +++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Lib/crypt.py b/Lib/crypt.py index 8846602d7613ec..33dbc46bb3e96b 100644 --- a/Lib/crypt.py +++ b/Lib/crypt.py @@ -10,6 +10,7 @@ else: raise ImportError("The required _crypt module was not built as part of CPython") +import errno import string as _string from random import SystemRandom as _SystemRandom from collections import namedtuple as _namedtuple @@ -88,7 +89,14 @@ def _add_method(name, *args, rounds=None): method = _Method(name, *args) globals()['METHOD_' + name] = method salt = mksalt(method, rounds=rounds) - result = crypt('', salt) + result = None + try: + result = crypt('', salt) + except OSError as e: + # Not all libc libraries support all encryption methods. + if e.errno == errno.EINVAL: + return False + raise if result and len(result) == method.total_size: methods.append(method) return True diff --git a/Modules/_cryptmodule.c b/Modules/_cryptmodule.c index 5d03f45f643615..00c1f4f69841b2 100644 --- a/Modules/_cryptmodule.c +++ b/Modules/_cryptmodule.c @@ -42,6 +42,9 @@ crypt_crypt_impl(PyObject *module, const char *word, const char *salt) #else crypt_result = crypt(word, salt); #endif + if (crypt_result == NULL) { + return PyErr_SetFromErrno(PyExc_OSError); + } return Py_BuildValue("s", crypt_result); } From 19466f6dad90b104e69261ea4b2ff44246625665 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 5 Oct 2019 19:36:18 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Core and Builtins/2019-10-05-19-36-16.bpo-36161.EZuzgK.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2019-10-05-19-36-16.bpo-36161.EZuzgK.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-10-05-19-36-16.bpo-36161.EZuzgK.rst b/Misc/NEWS.d/next/Core and Builtins/2019-10-05-19-36-16.bpo-36161.EZuzgK.rst new file mode 100644 index 00000000000000..334f7ea3fe610a --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2019-10-05-19-36-16.bpo-36161.EZuzgK.rst @@ -0,0 +1 @@ +Check error of primitive crypt/crypt_r \ No newline at end of file From 3a657bf4d3cee69fa9b949c0e5b21036ab6928c4 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Mon, 7 Oct 2019 21:02:16 -0700 Subject: [PATCH 3/3] update issue number and description --- .../Core and Builtins/2019-10-05-19-36-16.bpo-36161.EZuzgK.rst | 1 - .../Core and Builtins/2019-10-05-19-36-16.bpo-38402.EZuzgK.rst | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) delete mode 100644 Misc/NEWS.d/next/Core and Builtins/2019-10-05-19-36-16.bpo-36161.EZuzgK.rst create mode 100644 Misc/NEWS.d/next/Core and Builtins/2019-10-05-19-36-16.bpo-38402.EZuzgK.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-10-05-19-36-16.bpo-36161.EZuzgK.rst b/Misc/NEWS.d/next/Core and Builtins/2019-10-05-19-36-16.bpo-36161.EZuzgK.rst deleted file mode 100644 index 334f7ea3fe610a..00000000000000 --- a/Misc/NEWS.d/next/Core and Builtins/2019-10-05-19-36-16.bpo-36161.EZuzgK.rst +++ /dev/null @@ -1 +0,0 @@ -Check error of primitive crypt/crypt_r \ No newline at end of file diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-10-05-19-36-16.bpo-38402.EZuzgK.rst b/Misc/NEWS.d/next/Core and Builtins/2019-10-05-19-36-16.bpo-38402.EZuzgK.rst new file mode 100644 index 00000000000000..8331500bf1a3eb --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2019-10-05-19-36-16.bpo-38402.EZuzgK.rst @@ -0,0 +1 @@ +Check the error from the system's underlying ``crypt`` or ``crypt_r``.