[GH-ISSUE #833] Cost is uninitialized if format doesn't use it, but ldr_filter_costs() is called! #442

Closed
opened 2026-10-14 03:07:46 +07:00 by bsakaguchi · 7 comments
Owner

Originally created by @magnumripper on GitHub (Oct 27, 2014).
Original GitHub issue: https://github.com/openwall/john/issues/833

Originally assigned to: @frank-dittrich on GitHub.

loader.c:757 (8f37e8ce) cost figures are uninitialized (array is alloc, not calloc) if format doesn't use costs. But ldr_filter_costs() is called regardless, using those uninitialized values. I saw Valgrind complain over it for raw-md5 and for once it seems not to be a false positive.

Originally created by @magnumripper on GitHub (Oct 27, 2014). Original GitHub issue: https://github.com/openwall/john/issues/833 Originally assigned to: @frank-dittrich on GitHub. loader.c:757 (8f37e8ce) cost figures are uninitialized (array is alloc, not calloc) if format doesn't use costs. But ldr_filter_costs() is called regardless, using those uninitialized values. I saw Valgrind complain over it for raw-md5 and for once it seems not to be a false positive.
bsakaguchi 2026-10-14 03:07:46 +07:00
Author
Owner

@frank-dittrich commented on GitHub (Oct 28, 2014):

loader.c:757 (8f37e8c) cost figures are uninitialized (array is alloc, not calloc) if format doesn't use costs. But ldr_filter_costs() is called regardless, using those uninitialized values. I saw Valgrind complain over it for raw-md5 and for once it seems not to be a false positive.

loader.c:757 has

for (i = 0; i < FMT_TUNABLE_COSTS && format->methods.tunable_cost_value[i] != NULL; ++i)

Are you saying that even though the tunable cost functions in raw-md5 are defined like this

#if FMT_MAIN_VERSION > 11
                { NULL },
#endif

there'll be some of the 3 tunable functions not NULL?

Otherwise, I don't see how here

#if FMT_MAIN_VERSION > 11
                        for (i = 0; i < FMT_TUNABLE_COSTS && format->methods.tunable_cost_value[i] != NULL; ++i)
                                current_salt->cost[i] = format->methods.tunable_cost_value[i](current_salt->salt);
#endif

could be an uninitialized access.

ldr_filter_costs() calls ldr_cost_ok() with db->options->min_cost and db->options->max_cost

Is this access

if (salt->cost[i] < min_cost[i] || salt->cost[i] > max_cost[i])

of (the uninitialized?) salt->cost[i] what valgrind complains about?

But since I initialize

                        options.loader.min_cost[i] = 0;
                        options.loader.max_cost[i] = UINT_MAX;

in opt_init(), even if salt->cost[i] would be uninitialized,it will always be >= min_cost[i] and <= max_cost[i].

I temporarily added

printf("_i_%d, salt->cost[i] %u, min_cost[i] %u,  max_cost[i] %u\n",
       i, salt->cost[i], min_cost[i], max_cost[i]);

immediately before the if statement in ldr_cost_ok() and tested both the saltless raw-md5 and the salted sapb format (both without tunable costs).

(bleeding-jumbo)run $ ./john hashes.raw-md5 --format=raw-md5
Loaded 6 password hashes with no different salts (Raw-MD5 [MD5 128/128 AVX 12x])
_i_0, salt->cost[i] 0, min_cost[i] 0,  max_cost[i] 4294967295
_i_1, salt->cost[i] 0, min_cost[i] 0,  max_cost[i] 4294967295
_i_2, salt->cost[i] 0, min_cost[i] 0,  max_cost[i] 4294967295
Remaining 1 password hash
Warning: poor OpenMP scalability for this hash type, consider --fork=8
Will run 8 OpenMP threads
Press 'q' or Ctrl-C to abort, almost any other key for status
0g 0:00:00:02  3/3 0g/s 647831p/s 647831c/s 647831C/s autty4..addybry
Session aborted
(bleeding-jumbo)run $ ./john hashes.sapb --format=sapb   
Loaded 10 password hashes with 10 different salts (sapb, SAP CODVN B (BCODE) [MD5 128/128 AVX 12x])
_i_0, salt->cost[i] 0, min_cost[i] 0,  max_cost[i] 4294967295
_i_1, salt->cost[i] 0, min_cost[i] 0,  max_cost[i] 4294967295
_i_2, salt->cost[i] 0, min_cost[i] 0,  max_cost[i] 4294967295
_i_0, salt->cost[i] 0, min_cost[i] 0,  max_cost[i] 4294967295
_i_1, salt->cost[i] 0, min_cost[i] 0,  max_cost[i] 4294967295
_i_2, salt->cost[i] 0, min_cost[i] 0,  max_cost[i] 4294967295
_i_0, salt->cost[i] 0, min_cost[i] 0,  max_cost[i] 4294967295
_i_1, salt->cost[i] 0, min_cost[i] 0,  max_cost[i] 4294967295
_i_2, salt->cost[i] 0, min_cost[i] 0,  max_cost[i] 4294967295
_i_0, salt->cost[i] 0, min_cost[i] 0,  max_cost[i] 4294967295
_i_1, salt->cost[i] 0, min_cost[i] 0,  max_cost[i] 4294967295
_i_2, salt->cost[i] 0, min_cost[i] 0,  max_cost[i] 4294967295
_i_0, salt->cost[i] 0, min_cost[i] 0,  max_cost[i] 4294967295
_i_1, salt->cost[i] 0, min_cost[i] 0,  max_cost[i] 4294967295
_i_2, salt->cost[i] 0, min_cost[i] 0,  max_cost[i] 4294967295
_i_0, salt->cost[i] 0, min_cost[i] 0,  max_cost[i] 4294967295
_i_1, salt->cost[i] 0, min_cost[i] 0,  max_cost[i] 4294967295
_i_2, salt->cost[i] 0, min_cost[i] 0,  max_cost[i] 4294967295
_i_0, salt->cost[i] 0, min_cost[i] 0,  max_cost[i] 4294967295
_i_1, salt->cost[i] 0, min_cost[i] 0,  max_cost[i] 4294967295
_i_2, salt->cost[i] 0, min_cost[i] 0,  max_cost[i] 4294967295
_i_0, salt->cost[i] 0, min_cost[i] 0,  max_cost[i] 4294967295
_i_1, salt->cost[i] 0, min_cost[i] 0,  max_cost[i] 4294967295
_i_2, salt->cost[i] 0, min_cost[i] 0,  max_cost[i] 4294967295
_i_0, salt->cost[i] 0, min_cost[i] 0,  max_cost[i] 4294967295
_i_1, salt->cost[i] 0, min_cost[i] 0,  max_cost[i] 4294967295
_i_2, salt->cost[i] 0, min_cost[i] 0,  max_cost[i] 4294967295
_i_0, salt->cost[i] 0, min_cost[i] 0,  max_cost[i] 4294967295
_i_1, salt->cost[i] 0, min_cost[i] 0,  max_cost[i] 4294967295
_i_2, salt->cost[i] 0, min_cost[i] 0,  max_cost[i] 4294967295
Remaining 8 password hashes with 8 different salts
Will run 8 OpenMP threads
Press 'q' or Ctrl-C to abort, almost any other key for status

So, salt[i] doesn't even look uninitialized.
Can you enlighten me what I am missing?

I see a (minor) other proble, though:

While this is OK (warning if more than FMT_TUNABLE_COST cost values (or ranges) are specified:

$ ./john --format=bcrypt hashes.bcrypt --cost=2,3,4,5
max. 3 different tunable cost parameters supported

$ ./john --format=bcrypt hashes.bcrypt --cost=2  
Loaded 14 password hashes with 7 different salts (bcrypt [Blowfish 32/64 X3])
Remaining 12 password hashes with 7 different salts
Will run 8 OpenMP threads
Press 'q' or Ctrl-C to abort, almost any other key for status
0g 0:00:00:01  2/3 0g/s 432.0p/s 3312c/s 5712C/s roman..xavier
Session aborted

This is not:

$ ./john --format=bcrypt hashes.bcrypt --cost=2,3
Loaded 14 password hashes with 7 different salts (bcrypt [Blowfish 32/64 X3])
No password hashes left to crack (see FAQ)

$ ./john hashes.sapb --cost=0:1
Loaded 10 password hashes with 10 different salts (sapb, SAP CODVN B (BCODE) [MD5 128/128 AVX 12x])
Remaining 6 password hashes with 6 different salts
Will run 8 OpenMP threads
Press 'q' or Ctrl-C to abort, almost any other key for status
Warning: MaxLen = 13 is too large for the current hash type, reduced to 8
Warning: mixed-case charset, but the current hash type is case-insensitive;
some candidate passwords may be unnecessarily tried more than once.
0g 0:00:00:03  3/3 0g/s 1021Kp/s 6126Kc/s 6126KC/s LUPICO1..GROMI
Session aborted

In both cases, I was specifying more tunable cost values (or ranges) than the format supports.
I could have stopped with an error message.

<!-- gh-comment-id:60693896 --> @frank-dittrich commented on GitHub (Oct 28, 2014): > loader.c:757 (8f37e8c) cost figures are uninitialized (array is alloc, not calloc) if format doesn't use costs. But ldr_filter_costs() is called regardless, using those uninitialized values. I saw Valgrind complain over it for raw-md5 and for once it seems not to be a false positive. loader.c:757 has ``` C for (i = 0; i < FMT_TUNABLE_COSTS && format->methods.tunable_cost_value[i] != NULL; ++i) ``` Are you saying that even though the tunable cost functions in raw-md5 are defined like this ``` C #if FMT_MAIN_VERSION > 11 { NULL }, #endif ``` there'll be some of the 3 tunable functions not NULL? Otherwise, I don't see how here ``` C #if FMT_MAIN_VERSION > 11 for (i = 0; i < FMT_TUNABLE_COSTS && format->methods.tunable_cost_value[i] != NULL; ++i) current_salt->cost[i] = format->methods.tunable_cost_value[i](current_salt->salt); #endif ``` could be an uninitialized access. `ldr_filter_costs()` calls `ldr_cost_ok()` with `db->options->min_cost` and `db->options->max_cost` Is this access ``` C if (salt->cost[i] < min_cost[i] || salt->cost[i] > max_cost[i]) ``` of (the uninitialized?) salt->cost[i] what valgrind complains about? But since I initialize ``` C options.loader.min_cost[i] = 0; options.loader.max_cost[i] = UINT_MAX; ``` in `opt_init()`, even if salt->cost[i] would be uninitialized,it will always be >= min_cost[i] and <= max_cost[i]. I temporarily added ``` C printf("_i_%d, salt->cost[i] %u, min_cost[i] %u, max_cost[i] %u\n", i, salt->cost[i], min_cost[i], max_cost[i]); ``` immediately before the if statement in `ldr_cost_ok()` and tested both the saltless raw-md5 and the salted sapb format (both without tunable costs). ``` (bleeding-jumbo)run $ ./john hashes.raw-md5 --format=raw-md5 Loaded 6 password hashes with no different salts (Raw-MD5 [MD5 128/128 AVX 12x]) _i_0, salt->cost[i] 0, min_cost[i] 0, max_cost[i] 4294967295 _i_1, salt->cost[i] 0, min_cost[i] 0, max_cost[i] 4294967295 _i_2, salt->cost[i] 0, min_cost[i] 0, max_cost[i] 4294967295 Remaining 1 password hash Warning: poor OpenMP scalability for this hash type, consider --fork=8 Will run 8 OpenMP threads Press 'q' or Ctrl-C to abort, almost any other key for status 0g 0:00:00:02 3/3 0g/s 647831p/s 647831c/s 647831C/s autty4..addybry Session aborted ``` ``` (bleeding-jumbo)run $ ./john hashes.sapb --format=sapb Loaded 10 password hashes with 10 different salts (sapb, SAP CODVN B (BCODE) [MD5 128/128 AVX 12x]) _i_0, salt->cost[i] 0, min_cost[i] 0, max_cost[i] 4294967295 _i_1, salt->cost[i] 0, min_cost[i] 0, max_cost[i] 4294967295 _i_2, salt->cost[i] 0, min_cost[i] 0, max_cost[i] 4294967295 _i_0, salt->cost[i] 0, min_cost[i] 0, max_cost[i] 4294967295 _i_1, salt->cost[i] 0, min_cost[i] 0, max_cost[i] 4294967295 _i_2, salt->cost[i] 0, min_cost[i] 0, max_cost[i] 4294967295 _i_0, salt->cost[i] 0, min_cost[i] 0, max_cost[i] 4294967295 _i_1, salt->cost[i] 0, min_cost[i] 0, max_cost[i] 4294967295 _i_2, salt->cost[i] 0, min_cost[i] 0, max_cost[i] 4294967295 _i_0, salt->cost[i] 0, min_cost[i] 0, max_cost[i] 4294967295 _i_1, salt->cost[i] 0, min_cost[i] 0, max_cost[i] 4294967295 _i_2, salt->cost[i] 0, min_cost[i] 0, max_cost[i] 4294967295 _i_0, salt->cost[i] 0, min_cost[i] 0, max_cost[i] 4294967295 _i_1, salt->cost[i] 0, min_cost[i] 0, max_cost[i] 4294967295 _i_2, salt->cost[i] 0, min_cost[i] 0, max_cost[i] 4294967295 _i_0, salt->cost[i] 0, min_cost[i] 0, max_cost[i] 4294967295 _i_1, salt->cost[i] 0, min_cost[i] 0, max_cost[i] 4294967295 _i_2, salt->cost[i] 0, min_cost[i] 0, max_cost[i] 4294967295 _i_0, salt->cost[i] 0, min_cost[i] 0, max_cost[i] 4294967295 _i_1, salt->cost[i] 0, min_cost[i] 0, max_cost[i] 4294967295 _i_2, salt->cost[i] 0, min_cost[i] 0, max_cost[i] 4294967295 _i_0, salt->cost[i] 0, min_cost[i] 0, max_cost[i] 4294967295 _i_1, salt->cost[i] 0, min_cost[i] 0, max_cost[i] 4294967295 _i_2, salt->cost[i] 0, min_cost[i] 0, max_cost[i] 4294967295 _i_0, salt->cost[i] 0, min_cost[i] 0, max_cost[i] 4294967295 _i_1, salt->cost[i] 0, min_cost[i] 0, max_cost[i] 4294967295 _i_2, salt->cost[i] 0, min_cost[i] 0, max_cost[i] 4294967295 _i_0, salt->cost[i] 0, min_cost[i] 0, max_cost[i] 4294967295 _i_1, salt->cost[i] 0, min_cost[i] 0, max_cost[i] 4294967295 _i_2, salt->cost[i] 0, min_cost[i] 0, max_cost[i] 4294967295 Remaining 8 password hashes with 8 different salts Will run 8 OpenMP threads Press 'q' or Ctrl-C to abort, almost any other key for status ``` So, salt[i] doesn't even look uninitialized. Can you enlighten me what I am missing? I see a (minor) other proble, though: While this is OK (warning if more than FMT_TUNABLE_COST cost values (or ranges) are specified: ``` $ ./john --format=bcrypt hashes.bcrypt --cost=2,3,4,5 max. 3 different tunable cost parameters supported $ ./john --format=bcrypt hashes.bcrypt --cost=2 Loaded 14 password hashes with 7 different salts (bcrypt [Blowfish 32/64 X3]) Remaining 12 password hashes with 7 different salts Will run 8 OpenMP threads Press 'q' or Ctrl-C to abort, almost any other key for status 0g 0:00:00:01 2/3 0g/s 432.0p/s 3312c/s 5712C/s roman..xavier Session aborted ``` This is not: ``` $ ./john --format=bcrypt hashes.bcrypt --cost=2,3 Loaded 14 password hashes with 7 different salts (bcrypt [Blowfish 32/64 X3]) No password hashes left to crack (see FAQ) $ ./john hashes.sapb --cost=0:1 Loaded 10 password hashes with 10 different salts (sapb, SAP CODVN B (BCODE) [MD5 128/128 AVX 12x]) Remaining 6 password hashes with 6 different salts Will run 8 OpenMP threads Press 'q' or Ctrl-C to abort, almost any other key for status Warning: MaxLen = 13 is too large for the current hash type, reduced to 8 Warning: mixed-case charset, but the current hash type is case-insensitive; some candidate passwords may be unnecessarily tried more than once. 0g 0:00:00:03 3/3 0g/s 1021Kp/s 6126Kc/s 6126KC/s LUPICO1..GROMI Session aborted ``` In both cases, I was specifying more tunable cost values (or ranges) than the format supports. I could have stopped with an error message.
Author
Owner

@frank-dittrich commented on GitHub (Oct 28, 2014):

When I run john --format=raw-md5 with valgrind, I see

==20383== Conditional jump or move depends on uninitialised value(s)
==20383==    at 0x5D24DD: ldr_fix_database (loader.c:1214)
==20383==    by 0x5CE41C: john_load (john.c:989)
==20383==    by 0x5CEB05: john_init (john.c:1220)
==20383==    by 0x5CF7A4: main (john.c:1604)

loader.c:1214 is exactly the line I mentioned in my previous comment.
_i_2, salt->cost[i] 0, min_cost[i] 0, max_cost[i] 4294967295
salt->cost[i] can never be < 0 or > UINT_MAX here.

Does that qualify for false positive?
Should I add a source code comment?

<!-- gh-comment-id:60695012 --> @frank-dittrich commented on GitHub (Oct 28, 2014): When I run john --format=raw-md5 with valgrind, I see ``` ==20383== Conditional jump or move depends on uninitialised value(s) ==20383== at 0x5D24DD: ldr_fix_database (loader.c:1214) ==20383== by 0x5CE41C: john_load (john.c:989) ==20383== by 0x5CEB05: john_init (john.c:1220) ==20383== by 0x5CF7A4: main (john.c:1604) ``` loader.c:1214 is exactly the line I mentioned in my previous comment. _i_2, salt->cost[i] 0, min_cost[i] 0, max_cost[i] 4294967295 salt->cost[i] can never be < 0 or > UINT_MAX here. Does that qualify for false positive? Should I add a source code comment?
Author
Owner

@frank-dittrich commented on GitHub (Oct 28, 2014):

I could change the code to always set cost[i] to 0 whenever the corresponding tunable cost reporting function is NULL, just to make valgrind happy. (It just needs to be done once per salt.)

<!-- gh-comment-id:60695305 --> @frank-dittrich commented on GitHub (Oct 28, 2014): I could change the code to always set cost[i] to 0 whenever the corresponding tunable cost reporting function is NULL, just to make valgrind happy. (It just needs to be done once per salt.)
Author
Owner

@magnumripper commented on GitHub (Oct 28, 2014):

    for (i = 0; i < FMT_TUNABLE_COSTS && format->methods.tunable_cost_value[i] != NULL; ++i)
        current_salt->cost[i] = format->methods.tunable_cost_value[i](current_salt->salt);

Are you saying that even though the tunable cost functions in raw-md5 are defined like this ... there'll be some of the 3 tunable functions not NULL?

No, I'm saying because they are null, current_salt->cost[i] is never initialized at all. It's what it happens to be after the alloc. Or so I thought...

But exactly where ARE they initialized then?

<!-- gh-comment-id:60695335 --> @magnumripper commented on GitHub (Oct 28, 2014): ``` for (i = 0; i < FMT_TUNABLE_COSTS && format->methods.tunable_cost_value[i] != NULL; ++i) current_salt->cost[i] = format->methods.tunable_cost_value[i](current_salt->salt); ``` > Are you saying that even though the tunable cost functions in raw-md5 are defined like this ... there'll be some of the 3 tunable functions not NULL? No, I'm saying because they are null, current_salt->cost[i] is never initialized at all. It's what it happens to be after the alloc. Or so I thought... But exactly where ARE they initialized then?
Author
Owner

@magnumripper commented on GitHub (Oct 28, 2014):

OK; I see. It's not a problem because of the defaults of min_cost and max_cost. So Valgrind is right complaining about it, but it's no issue at all.

<!-- gh-comment-id:60695587 --> @magnumripper commented on GitHub (Oct 28, 2014): OK; I see. It's not a problem because of the defaults of min_cost and max_cost. So Valgrind is right complaining about it, but it's no issue at all.
Author
Owner

@magnumripper commented on GitHub (Oct 28, 2014):

Adding comments or extra initialization is not of much use. Let's just close this?

<!-- gh-comment-id:60695693 --> @magnumripper commented on GitHub (Oct 28, 2014): Adding comments or extra initialization is not of much use. Let's just close this?
Author
Owner

@frank-dittrich commented on GitHub (Oct 28, 2014):

You could file a valgrind bug report so it doesn't claim the program flow depends on uninitialized values when I compare against min and / or max value of the given type.

<!-- gh-comment-id:60696289 --> @frank-dittrich commented on GitHub (Oct 28, 2014): You could file a valgrind bug report so it doesn't claim the program flow depends on uninitialized values when I compare against min and / or max value of the given type.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
openwall/john#442
No description provided.