Skip to content

Commit 3d2f1b2

Browse files
authored
[3.14] gh-149816: fix a race when fetching OpenSSL digests (GH-153019) (#160006)
gh-149816: fix a race when fetching OpenSSL digests (GH-153019) (cherry picked from commit 2346981) Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
1 parent 8e382c3 commit 3d2f1b2

2 files changed

Lines changed: 60 additions & 47 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:mod:`hashlib`: fix a crash arising from a race condition when looking
2+
up an OpenSSL hash algorithm by name the first time. Patch by Bénédikt Tran.

‎Modules/_hashopenssl.c‎

Lines changed: 58 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -392,66 +392,77 @@ py_digest_name(const EVP_MD *md)
392392
return PyUnicode_FromString(name);
393393
}
394394

395+
static inline int
396+
disable_fips_property(enum Py_hash_type py_ht)
397+
{
398+
switch (py_ht) {
399+
case Py_ht_evp:
400+
case Py_ht_mac:
401+
case Py_ht_pbkdf2:
402+
return 0;
403+
case Py_ht_evp_nosecurity:
404+
return 1;
405+
default:
406+
// To preserve the legacy behavior, we do not raise a fatal error here
407+
// but delegate it to a regular ValueError. This should however never
408+
// happen in practice since Py_hash_type is internal.
409+
return -1;
410+
}
411+
}
412+
395413
/* Get EVP_MD by HID and purpose */
396414
static PY_EVP_MD*
397415
py_digest_by_name(PyObject *module, const char *name, enum Py_hash_type py_ht)
398416
{
399-
PY_EVP_MD *digest = NULL;
400-
PY_EVP_MD *other_digest = NULL;
417+
PY_EVP_MD *digest = NULL, *fresh = NULL;
401418
_hashlibstate *state = get_hashlib_state(module);
402-
py_hashentry_t *entry = (py_hashentry_t *)_Py_hashtable_get(
403-
state->hashtable, (const void*)name
404-
);
419+
py_hashentry_t *entry = _Py_hashtable_get(state->hashtable, name);
420+
int fips = disable_fips_property(py_ht);
421+
if (fips == -1) {
422+
goto finally;
423+
}
405424

406425
if (entry != NULL) {
407-
switch (py_ht) {
408-
case Py_ht_evp:
409-
case Py_ht_mac:
410-
case Py_ht_pbkdf2:
411-
digest = FT_ATOMIC_LOAD_PTR_RELAXED(entry->evp);
412-
if (digest == NULL) {
413-
digest = PY_EVP_MD_fetch(entry->ossl_name, NULL);
414-
#ifdef Py_GIL_DISABLED
415-
// exchange just in case another thread did same thing at same time
416-
other_digest = _Py_atomic_exchange_ptr(&entry->evp, (void *)digest);
417-
#else
418-
entry->evp = digest;
419-
#endif
420-
}
421-
break;
422-
case Py_ht_evp_nosecurity:
423-
digest = FT_ATOMIC_LOAD_PTR_RELAXED(entry->evp_nosecurity);
424-
if (digest == NULL) {
425-
digest = PY_EVP_MD_fetch(entry->ossl_name, "-fips");
426-
#ifdef Py_GIL_DISABLED
427-
// exchange just in case another thread did same thing at same time
428-
other_digest = _Py_atomic_exchange_ptr(&entry->evp_nosecurity, (void *)digest);
429-
#else
430-
entry->evp_nosecurity = digest;
431-
#endif
426+
PY_EVP_MD **cached = fips ? &entry->evp : &entry->evp_nosecurity;
427+
digest = FT_ATOMIC_LOAD_PTR_RELAXED(*cached);
428+
if (digest == NULL) {
429+
fresh = PY_EVP_MD_fetch(entry->ossl_name, fips ? NULL : "-fips");
430+
if (fresh != NULL) {
431+
/*
432+
* Publish a freshly fetched digest into the cache exactly once.
433+
*
434+
* On the first publication, 'cached' adopts the 'fresh' ref.
435+
* If another thread wins the race, 'fresh' is released and
436+
* the cached digest is used instead.
437+
*
438+
* See https://github.com/python/cpython/issues/128657
439+
* and https://github.com/python/cpython/issues/149816.
440+
*/
441+
PY_EVP_MD *expected = NULL;
442+
if (_Py_atomic_compare_exchange_ptr(cached, &expected, (void *)fresh)) {
443+
digest = fresh; // we won: 'cached' now owns the fresh digest
444+
}
445+
else {
446+
PY_EVP_MD_free(fresh); // we lost: discard the fresh digest
447+
digest = expected; // reuse the winner's digest
448+
}
432449
}
433-
break;
434450
}
435-
// if another thread same thing at same time make sure we got same ptr
436-
assert(other_digest == NULL || other_digest == digest);
451+
/* Hand the caller its own reference; the cache keeps its own. Valid
452+
* whether `digest` was already cached, freshly published by this
453+
* thread, or published by a thread that won the race. */
437454
if (digest != NULL) {
438-
if (other_digest == NULL) {
439-
PY_EVP_MD_up_ref(digest);
440-
}
455+
PY_EVP_MD_up_ref(digest);
441456
}
442-
} else {
457+
}
458+
else {
443459
// Fall back for looking up an unindexed OpenSSL specific name.
444-
switch (py_ht) {
445-
case Py_ht_evp:
446-
case Py_ht_mac:
447-
case Py_ht_pbkdf2:
448-
digest = PY_EVP_MD_fetch(name, NULL);
449-
break;
450-
case Py_ht_evp_nosecurity:
451-
digest = PY_EVP_MD_fetch(name, "-fips");
452-
break;
453-
}
460+
const char *props = fips ? NULL : "-fips";
461+
(void)props; // will only be used in OpenSSL 3.0 and later
462+
digest = PY_EVP_MD_fetch(name, props);
454463
}
464+
465+
finally:
455466
if (digest == NULL) {
456467
raise_ssl_error(state->unsupported_digestmod_error,
457468
"unsupported hash type %s", name);

0 commit comments

Comments
 (0)