Skip to content

Commit 0d3fe8a

Browse files
chibby0nebenjaminp
authored andcommitted
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 <[email protected]>
1 parent 4d5f94b commit 0d3fe8a

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

Lib/crypt.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
else:
1111
raise ImportError("The required _crypt module was not built as part of CPython")
1212

13+
import errno
1314
import string as _string
1415
from random import SystemRandom as _SystemRandom
1516
from collections import namedtuple as _namedtuple
@@ -88,7 +89,14 @@ def _add_method(name, *args, rounds=None):
8889
method = _Method(name, *args)
8990
globals()['METHOD_' + name] = method
9091
salt = mksalt(method, rounds=rounds)
91-
result = crypt('', salt)
92+
result = None
93+
try:
94+
result = crypt('', salt)
95+
except OSError as e:
96+
# Not all libc libraries support all encryption methods.
97+
if e.errno == errno.EINVAL:
98+
return False
99+
raise
92100
if result and len(result) == method.total_size:
93101
methods.append(method)
94102
return True
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Check the error from the system's underlying ``crypt`` or ``crypt_r``.

Modules/_cryptmodule.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ crypt_crypt_impl(PyObject *module, const char *word, const char *salt)
4242
#else
4343
crypt_result = crypt(word, salt);
4444
#endif
45+
if (crypt_result == NULL) {
46+
return PyErr_SetFromErrno(PyExc_OSError);
47+
}
4548
return Py_BuildValue("s", crypt_result);
4649
}
4750

0 commit comments

Comments
 (0)