blob: 2c9b1d06cd646ca11f533b1f179f71834b704d17 [file] [log] [blame]
Andrew Geisslerd159c7f2021-09-02 21:05:58 -05001From 234e8fa7b134d1ebabfdad980a3ae5b63c046c62 Mon Sep 17 00:00:00 2001
2From: Mike Gilbert <floppym@gentoo.org>
3Date: Sat, 14 Aug 2021 13:24:34 -0400
4Subject: [PATCH] libmisc: fix default value in SHA_get_salt_rounds()
5
6If SHA_CRYPT_MIN_ROUNDS and SHA_CRYPT_MAX_ROUNDS are both unspecified,
7use SHA_ROUNDS_DEFAULT.
8
9Previously, the code fell through, calling shadow_random(-1, -1). This
10ultimately set rounds = (unsigned long) -1, which ends up being a very
11large number! This then got capped to SHA_ROUNDS_MAX later in the
12function.
13
14The new behavior matches BCRYPT_get_salt_rounds().
15
16Bug: https://bugs.gentoo.org/808195
17Fixes: https://github.com/shadow-maint/shadow/issues/393
18
19Upstream-Status: Backport [https://github.com/shadow-maint/shadow/commit/234e8fa7b134d1ebabfdad980a3ae5b63c046c62]
20
21Signed-off-by: Mingli Yu <mingli.yu@windriver.com>
22---
23 libmisc/salt.c | 21 +++++++++++----------
24 1 file changed, 11 insertions(+), 10 deletions(-)
25
26diff --git a/libmisc/salt.c b/libmisc/salt.c
27index 91d528fd..30eefb9c 100644
28--- a/libmisc/salt.c
29+++ b/libmisc/salt.c
30@@ -223,20 +223,21 @@ static /*@observer@*/const unsigned long SHA_get_salt_rounds (/*@null@*/int *pre
31 if ((-1 == min_rounds) && (-1 == max_rounds)) {
32 rounds = SHA_ROUNDS_DEFAULT;
33 }
34+ else {
35+ if (-1 == min_rounds) {
36+ min_rounds = max_rounds;
37+ }
38
39- if (-1 == min_rounds) {
40- min_rounds = max_rounds;
41- }
42+ if (-1 == max_rounds) {
43+ max_rounds = min_rounds;
44+ }
45
46- if (-1 == max_rounds) {
47- max_rounds = min_rounds;
48- }
49+ if (min_rounds > max_rounds) {
50+ max_rounds = min_rounds;
51+ }
52
53- if (min_rounds > max_rounds) {
54- max_rounds = min_rounds;
55+ rounds = (unsigned long) shadow_random (min_rounds, max_rounds);
56 }
57-
58- rounds = (unsigned long) shadow_random (min_rounds, max_rounds);
59 } else if (0 == *prefered_rounds) {
60 rounds = SHA_ROUNDS_DEFAULT;
61 } else {
62--
632.17.1
64