[Python-checkins] closes bpo-38402: Check error of primitive crypt/crypt_r. (GH-16599)

Benjamin Peterson webhook-mailer at python.org
Tue Oct 8 00:22:22 EDT 2019


https://github.com/python/cpython/commit/0d3fe8ae4961bf551e7d5e42559e2ede1a08fd7c
commit: 0d3fe8ae4961bf551e7d5e42559e2ede1a08fd7c
branch: master
author: Antonio Gutierrez <chibby0ne at gmail.com>
committer: Benjamin Peterson <benjamin at python.org>
date: 2019-10-07T21:22:17-07:00
summary:

closes bpo-38402: Check error of primitive crypt/crypt_r. (GH-16599)

Checks also for encryption algorithms methods not supported in different
OSs.

Signed-off-by: Antonio Gutierrez <chibby0ne at gmail.com>

files:
A Misc/NEWS.d/next/Core and Builtins/2019-10-05-19-36-16.bpo-38402.EZuzgK.rst
M Lib/crypt.py
M Modules/_cryptmodule.c

diff --git a/Lib/crypt.py b/Lib/crypt.py
index 8846602d7613e..33dbc46bb3e96 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/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 0000000000000..8331500bf1a3e
--- /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``.
diff --git a/Modules/_cryptmodule.c b/Modules/_cryptmodule.c
index 5d03f45f64361..00c1f4f69841b 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);
 }
 



More information about the Python-checkins mailing list