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

closes bpo-38402: Check error of primitive crypt/crypt_r. #16599

Merged
merged 3 commits into from
Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 9 additions & 1 deletion Lib/crypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Check the error from the system's underlying ``crypt`` or ``crypt_r``.
3 changes: 3 additions & 0 deletions Modules/_cryptmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down