Skip to content

Commit 156d318

Browse files
manfrenessita
authored andcommitted
[4.2.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 79f3687 commit 156d318

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

django/contrib/auth/hashers.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,20 @@ def check_password(password, encoded, setter=None, preferred="default"):
4343
If setter is specified, it'll be called when you need to
4444
regenerate the password.
4545
"""
46-
if password is None or not is_password_usable(encoded):
47-
return False
46+
fake_runtime = password is None or not is_password_usable(encoded)
4847

4948
preferred = get_hasher(preferred)
5049
try:
5150
hasher = identify_hasher(encoded)
5251
except ValueError:
5352
# encoded is gibberish or uses a hasher that's no longer installed.
53+
fake_runtime = True
54+
55+
if fake_runtime:
56+
# Run the default password hasher once to reduce the timing difference
57+
# between an existing user with an unusable password and a nonexistent
58+
# user or missing hasher (similar to #20760).
59+
make_password(get_random_string(UNUSABLE_PASSWORD_SUFFIX_LENGTH))
5460
return False
5561

5662
hasher_changed = hasher.algorithm != preferred.algorithm

docs/releases/4.2.14.txt

Lines changed: 7 additions & 0 deletions
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.

tests/auth_tests/test_hashers.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,38 @@ def test_check_password_calls_harden_runtime(self):
613613
check_password("wrong_password", encoded)
614614
self.assertEqual(hasher.harden_runtime.call_count, 1)
615615

616+
def test_check_password_calls_make_password_to_fake_runtime(self):
617+
hasher = get_hasher("default")
618+
cases = [
619+
(None, None, None), # no plain text password provided
620+
("foo", make_password(password=None), None), # unusable encoded
621+
("letmein", make_password(password="letmein"), ValueError), # valid encoded
622+
]
623+
for password, encoded, hasher_side_effect in cases:
624+
with (
625+
self.subTest(encoded=encoded),
626+
mock.patch(
627+
"django.contrib.auth.hashers.identify_hasher",
628+
side_effect=hasher_side_effect,
629+
) as mock_identify_hasher,
630+
mock.patch(
631+
"django.contrib.auth.hashers.make_password"
632+
) as mock_make_password,
633+
mock.patch(
634+
"django.contrib.auth.hashers.get_random_string",
635+
side_effect=lambda size: "x" * size,
636+
),
637+
mock.patch.object(hasher, "verify"),
638+
):
639+
# Ensure make_password is called to standardize timing.
640+
check_password(password, encoded)
641+
self.assertEqual(hasher.verify.call_count, 0)
642+
self.assertEqual(mock_identify_hasher.mock_calls, [mock.call(encoded)])
643+
self.assertEqual(
644+
mock_make_password.mock_calls,
645+
[mock.call("x" * UNUSABLE_PASSWORD_SUFFIX_LENGTH)],
646+
)
647+
616648
def test_encode_invalid_salt(self):
617649
hasher_classes = [
618650
MD5PasswordHasher,

0 commit comments

Comments
 (0)