Skip to content

Commit 07cefde

Browse files
manfrenessita
authored andcommitted
[5.0.x] Fixed CVE-2024-39329 -- Standarized timing of verify_password() when checking unusuable passwords.
Refs #20760. Thanks Michael Manfre for the fix and to Adam Johnson for the review.
1 parent 7285644 commit 07cefde

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

django/contrib/auth/hashers.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,20 @@ def verify_password(password, encoded, preferred="default"):
4040
three part encoded digest, and the second whether to regenerate the
4141
password.
4242
"""
43-
if password is None or not is_password_usable(encoded):
44-
return False, False
43+
fake_runtime = password is None or not is_password_usable(encoded)
4544

4645
preferred = get_hasher(preferred)
4746
try:
4847
hasher = identify_hasher(encoded)
4948
except ValueError:
5049
# encoded is gibberish or uses a hasher that's no longer installed.
50+
fake_runtime = True
51+
52+
if fake_runtime:
53+
# Run the default password hasher once to reduce the timing difference
54+
# between an existing user with an unusable password and a nonexistent
55+
# user or missing hasher (similar to #20760).
56+
make_password(get_random_string(UNUSABLE_PASSWORD_SUFFIX_LENGTH))
5157
return False, False
5258

5359
hasher_changed = hasher.algorithm != preferred.algorithm

docs/releases/4.2.14.txt

+7
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,10 @@ CVE-2024-38875: Potential denial-of-service vulnerability in ``django.utils.html
1313
:tfilter:`urlize` and :tfilter:`urlizetrunc` were subject to a potential
1414
denial-of-service attack via certain inputs with a very large number of
1515
brackets.
16+
17+
CVE-2024-39329: Username enumeration through timing difference for users with unusable passwords
18+
================================================================================================
19+
20+
The :meth:`~django.contrib.auth.backends.ModelBackend.authenticate()` method
21+
allowed remote attackers to enumerate users via a timing attack involving login
22+
requests for users with unusable passwords.

docs/releases/5.0.7.txt

+7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ CVE-2024-38875: Potential denial-of-service vulnerability in ``django.utils.html
1414
denial-of-service attack via certain inputs with a very large number of
1515
brackets.
1616

17+
CVE-2024-39329: Username enumeration through timing difference for users with unusable passwords
18+
================================================================================================
19+
20+
The :meth:`~django.contrib.auth.backends.ModelBackend.authenticate()` method
21+
allowed remote attackers to enumerate users via a timing attack involving login
22+
requests for users with unusable passwords.
23+
1724
Bugfixes
1825
========
1926

tests/auth_tests/test_hashers.py

+32
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,38 @@ def test_check_password_calls_harden_runtime(self):
565565
check_password("wrong_password", encoded)
566566
self.assertEqual(hasher.harden_runtime.call_count, 1)
567567

568+
def test_check_password_calls_make_password_to_fake_runtime(self):
569+
hasher = get_hasher("default")
570+
cases = [
571+
(None, None, None), # no plain text password provided
572+
("foo", make_password(password=None), None), # unusable encoded
573+
("letmein", make_password(password="letmein"), ValueError), # valid encoded
574+
]
575+
for password, encoded, hasher_side_effect in cases:
576+
with (
577+
self.subTest(encoded=encoded),
578+
mock.patch(
579+
"django.contrib.auth.hashers.identify_hasher",
580+
side_effect=hasher_side_effect,
581+
) as mock_identify_hasher,
582+
mock.patch(
583+
"django.contrib.auth.hashers.make_password"
584+
) as mock_make_password,
585+
mock.patch(
586+
"django.contrib.auth.hashers.get_random_string",
587+
side_effect=lambda size: "x" * size,
588+
),
589+
mock.patch.object(hasher, "verify"),
590+
):
591+
# Ensure make_password is called to standardize timing.
592+
check_password(password, encoded)
593+
self.assertEqual(hasher.verify.call_count, 0)
594+
self.assertEqual(mock_identify_hasher.mock_calls, [mock.call(encoded)])
595+
self.assertEqual(
596+
mock_make_password.mock_calls,
597+
[mock.call("x" * UNUSABLE_PASSWORD_SUFFIX_LENGTH)],
598+
)
599+
568600
def test_encode_invalid_salt(self):
569601
hasher_classes = [
570602
MD5PasswordHasher,

0 commit comments

Comments
 (0)