[GH-ISSUE #1943] ASan crash in Raw-MD5 format #1296

Closed
opened 2026-10-14 03:43:05 +07:00 by bsakaguchi · 17 comments
Owner

Originally created by @kholia on GitHub (Dec 9, 2015).
Original GitHub issue: https://github.com/openwall/john/issues/1943

Originally assigned to: @magnumripper on GitHub.

$ ../run/john --test=0
...
Testing: Raw-MD5 [MD5 256/256 AVX2 8x3]... 
==21643==ERROR: AddressSanitizer: global-buffer-overflow on address 0x0000012e0924 at pc 0x0000004a80d1 bp 0x7ffd45fe3fc0 sp 0x7ffd45fe3fb0
WRITE of size 1 at 0x0000012e0924 thread T0
    #0 0x4a80d0 in mime_to_hex /code/JohnTheRipper/src/base64_convert.c:679
    #1 0x4a9e19 in base64_convert /code/JohnTheRipper/src/base64_convert.c:909
    #2 0x832392 in prepare /code/JohnTheRipper/src/rawMD5_fmt_plug.c:158
    #3 0x8cf6b6 in ldr_split_line /code/JohnTheRipper/src/loader.c:624
    #4 0x8d099b in ldr_load_pw_line /code/JohnTheRipper/src/loader.c:825
    #5 0x8d57d6 in ldr_init_test_db /code/JohnTheRipper/src/loader.c:1094
    #6 0x8a00aa in benchmark_all /code/JohnTheRipper/src/bench.c:684
    #7 0x8ca5a3 in john_run /code/JohnTheRipper/src/john.c:1585
    #8 0x8cbcc8 in main /code/JohnTheRipper/src/john.c:1970
    #9 0x7fed67c0f9ff in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x209ff)
    #10 0x4078b8 in _start (/code/JohnTheRipper/run/john+0x4078b8)

0x0000012e0924 is located 60 bytes to the left of global variable '_out' defined in 'rawMD5_fmt_plug.c:200:4' (0x12e0960) of size 16
0x0000012e0924 is located 3 bytes to the right of global variable 'out' defined in 'rawMD5_fmt_plug.c:153:14' (0x12e0900) of size 33
  'out' is ascii string '098f6bcd4621d373cade4e832627b4f6'
SUMMARY: AddressSanitizer: global-buffer-overflow /home/dhiru/projects/JohnTheRipper/src/base64_convert.c:679 mime_to_hex
Shadow bytes around the buggy address:
  0x0000802540d0: 00 f9 f9 f9 f9 f9 f9 f9 00 f9 f9 f9 f9 f9 f9 f9
  0x0000802540e0: 00 f9 f9 f9 f9 f9 f9 f9 00 f9 f9 f9 f9 f9 f9 f9
  0x0000802540f0: 00 00 00 00 06 f9 f9 f9 f9 f9 f9 f9 00 00 f9 f9
  0x000080254100: f9 f9 f9 f9 00 00 00 00 00 00 00 f9 f9 f9 f9 f9
  0x000080254110: 00 f9 f9 f9 f9 f9 f9 f9 00 f9 f9 f9 f9 f9 f9 f9
=>0x000080254120: 00 00 00 00[01]f9 f9 f9 f9 f9 f9 f9 00 00 f9 f9
  0x000080254130: f9 f9 f9 f9 00 00 00 00 00 00 00 f9 f9 f9 f9 f9
  0x000080254140: 00 f9 f9 f9 f9 f9 f9 f9 00 f9 f9 f9 f9 f9 f9 f9
  0x000080254150: 00 00 00 00 00 06 f9 f9 f9 f9 f9 f9 00 00 04 f9
  0x000080254160: f9 f9 f9 f9 00 00 00 00 00 00 00 00 00 00 00 00
  0x000080254170: f9 f9 f9 f9 00 00 04 f9 f9 f9 f9 f9 00 00 00 00
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Heap right redzone:      fb
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack partial redzone:   f4
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
Originally created by @kholia on GitHub (Dec 9, 2015). Original GitHub issue: https://github.com/openwall/john/issues/1943 Originally assigned to: @magnumripper on GitHub. ``` $ ../run/john --test=0 ... Testing: Raw-MD5 [MD5 256/256 AVX2 8x3]... ==21643==ERROR: AddressSanitizer: global-buffer-overflow on address 0x0000012e0924 at pc 0x0000004a80d1 bp 0x7ffd45fe3fc0 sp 0x7ffd45fe3fb0 WRITE of size 1 at 0x0000012e0924 thread T0 #0 0x4a80d0 in mime_to_hex /code/JohnTheRipper/src/base64_convert.c:679 #1 0x4a9e19 in base64_convert /code/JohnTheRipper/src/base64_convert.c:909 #2 0x832392 in prepare /code/JohnTheRipper/src/rawMD5_fmt_plug.c:158 #3 0x8cf6b6 in ldr_split_line /code/JohnTheRipper/src/loader.c:624 #4 0x8d099b in ldr_load_pw_line /code/JohnTheRipper/src/loader.c:825 #5 0x8d57d6 in ldr_init_test_db /code/JohnTheRipper/src/loader.c:1094 #6 0x8a00aa in benchmark_all /code/JohnTheRipper/src/bench.c:684 #7 0x8ca5a3 in john_run /code/JohnTheRipper/src/john.c:1585 #8 0x8cbcc8 in main /code/JohnTheRipper/src/john.c:1970 #9 0x7fed67c0f9ff in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x209ff) #10 0x4078b8 in _start (/code/JohnTheRipper/run/john+0x4078b8) 0x0000012e0924 is located 60 bytes to the left of global variable '_out' defined in 'rawMD5_fmt_plug.c:200:4' (0x12e0960) of size 16 0x0000012e0924 is located 3 bytes to the right of global variable 'out' defined in 'rawMD5_fmt_plug.c:153:14' (0x12e0900) of size 33 'out' is ascii string '098f6bcd4621d373cade4e832627b4f6' SUMMARY: AddressSanitizer: global-buffer-overflow /home/dhiru/projects/JohnTheRipper/src/base64_convert.c:679 mime_to_hex Shadow bytes around the buggy address: 0x0000802540d0: 00 f9 f9 f9 f9 f9 f9 f9 00 f9 f9 f9 f9 f9 f9 f9 0x0000802540e0: 00 f9 f9 f9 f9 f9 f9 f9 00 f9 f9 f9 f9 f9 f9 f9 0x0000802540f0: 00 00 00 00 06 f9 f9 f9 f9 f9 f9 f9 00 00 f9 f9 0x000080254100: f9 f9 f9 f9 00 00 00 00 00 00 00 f9 f9 f9 f9 f9 0x000080254110: 00 f9 f9 f9 f9 f9 f9 f9 00 f9 f9 f9 f9 f9 f9 f9 =>0x000080254120: 00 00 00 00[01]f9 f9 f9 f9 f9 f9 f9 00 00 f9 f9 0x000080254130: f9 f9 f9 f9 00 00 00 00 00 00 00 f9 f9 f9 f9 f9 0x000080254140: 00 f9 f9 f9 f9 f9 f9 f9 00 f9 f9 f9 f9 f9 f9 f9 0x000080254150: 00 00 00 00 00 06 f9 f9 f9 f9 f9 f9 00 00 04 f9 0x000080254160: f9 f9 f9 f9 00 00 00 00 00 00 00 00 00 00 00 00 0x000080254170: f9 f9 f9 f9 00 00 04 f9 f9 f9 f9 f9 00 00 00 00 Shadow byte legend (one shadow byte represents 8 application bytes): Addressable: 00 Partially addressable: 01 02 03 04 05 06 07 Heap left redzone: fa Heap right redzone: fb Freed heap region: fd Stack left redzone: f1 Stack mid redzone: f2 Stack right redzone: f3 Stack partial redzone: f4 Stack after return: f5 Stack use after scope: f8 Global redzone: f9 Global init order: f6 Poisoned by user: f7 Container overflow: fc Array cookie: ac Intra object redzone: bb ASan internal: fe ```
bsakaguchi 2026-10-14 03:43:05 +07:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@magnumripper commented on GitHub (Dec 9, 2015):

Must be c899f16cd

<!-- gh-comment-id:163067420 --> @magnumripper commented on GitHub (Dec 9, 2015): Must be c899f16cd
Author
Owner

@magnumripper commented on GitHub (Dec 9, 2015):

@jfoug please tell me what I did wrong.

/* Convert {MD5}CY9rzUYh03PK3k6DJie09g== to 098f6bcd4621d373cade4e832627b4f6 */
static char *prepare(char *fields[10], struct fmt_main *self)
{
    static char out[CIPHERTEXT_LENGTH + 1];

    if (!strncmp(fields[1], "{MD5}", 5) && strlen(fields[1]) == 29) {
        int res;

        res = base64_convert(&fields[1][5], e_b64_mime, 24,
                             out, e_b64_hex, sizeof(out),
                             flg_Base64_HEX_LOCASE);
        if (res >= 0)
            return out;
    }

    return fields[1];
}

We checked length of fields[1] and out has sufficient size. We pass sizeof(out). As far as I can see the bug is in base64_convert?

<!-- gh-comment-id:163068538 --> @magnumripper commented on GitHub (Dec 9, 2015): @jfoug please tell me what I did wrong. ``` c /* Convert {MD5}CY9rzUYh03PK3k6DJie09g== to 098f6bcd4621d373cade4e832627b4f6 */ static char *prepare(char *fields[10], struct fmt_main *self) { static char out[CIPHERTEXT_LENGTH + 1]; if (!strncmp(fields[1], "{MD5}", 5) && strlen(fields[1]) == 29) { int res; res = base64_convert(&fields[1][5], e_b64_mime, 24, out, e_b64_hex, sizeof(out), flg_Base64_HEX_LOCASE); if (res >= 0) return out; } return fields[1]; } ``` We checked length of fields[1] and out has sufficient size. We pass sizeof(out). As far as I can see the bug is in base64_convert?
Author
Owner

@magnumripper commented on GitHub (Dec 9, 2015):

@kholia did you by any chance miss to make clean? I can't reproduce.

<!-- gh-comment-id:163073188 --> @magnumripper commented on GitHub (Dec 9, 2015): @kholia did you by any chance miss to `make clean`? I can't reproduce.
Author
Owner

@kholia commented on GitHub (Dec 9, 2015):

Yes, I did make clean and I can reproduce this on my system running gcc (Ubuntu 5.3.1-3ubuntu1) 5.3.1 20151207.

I will handle this "tomorrow".

<!-- gh-comment-id:163074688 --> @kholia commented on GitHub (Dec 9, 2015): Yes, I did `make clean` and I can reproduce this on my system running `gcc (Ubuntu 5.3.1-3ubuntu1) 5.3.1 20151207`. I will handle this "tomorrow".
Author
Owner

@magnumripper commented on GitHub (Dec 9, 2015):

Hmm why can I not reproduce on super then? Very very strange.

<!-- gh-comment-id:163074782 --> @magnumripper commented on GitHub (Dec 9, 2015): Hmm why can I not reproduce on super then? Very very strange.
Author
Owner

@jfoug commented on GitHub (Dec 9, 2015):

I can not reproduce either, BUT I think this is the 'classic' We need 4 extra bytes for base64 convert.

@kholia can you make this change and test?

static char *prepare(char *fields[10], struct fmt_main *self)
{
-    static char out[CIPHERTEXT_LENGTH + 1];
+    static char out[CIPHERTEXT_LENGTH + 4];
<!-- gh-comment-id:163322207 --> @jfoug commented on GitHub (Dec 9, 2015): I can not reproduce either, BUT I think this is the 'classic' We need 4 extra bytes for base64 convert. @kholia can you make this change and test? ``` diff static char *prepare(char *fields[10], struct fmt_main *self) { - static char out[CIPHERTEXT_LENGTH + 1]; + static char out[CIPHERTEXT_LENGTH + 4]; ```
Author
Owner

@kholia commented on GitHub (Dec 9, 2015):

@jfoug that still results in a crash.

diff --git a/src/rawMD5_fmt_plug.c b/src/rawMD5_fmt_plug.c
index 28ac609..22733bc 100644
--- a/src/rawMD5_fmt_plug.c
+++ b/src/rawMD5_fmt_plug.c
@@ -150,7 +150,7 @@ static void done(void)
 /* Convert {MD5}CY9rzUYh03PK3k6DJie09g== to 098f6bcd4621d373cade4e832627b4f6 */
 static char *prepare(char *fields[10], struct fmt_main *self)
 {
-       static char out[CIPHERTEXT_LENGTH + 1];
+       static char out[CIPHERTEXT_LENGTH + 5];

        if (!strncmp(fields[1], "{MD5}", 5) && strlen(fields[1]) == 29) {
                int res;

This works, WTF :|

<!-- gh-comment-id:163325485 --> @kholia commented on GitHub (Dec 9, 2015): @jfoug that still results in a crash. ``` diff --git a/src/rawMD5_fmt_plug.c b/src/rawMD5_fmt_plug.c index 28ac609..22733bc 100644 --- a/src/rawMD5_fmt_plug.c +++ b/src/rawMD5_fmt_plug.c @@ -150,7 +150,7 @@ static void done(void) /* Convert {MD5}CY9rzUYh03PK3k6DJie09g== to 098f6bcd4621d373cade4e832627b4f6 */ static char *prepare(char *fields[10], struct fmt_main *self) { - static char out[CIPHERTEXT_LENGTH + 1]; + static char out[CIPHERTEXT_LENGTH + 5]; if (!strncmp(fields[1], "{MD5}", 5) && strlen(fields[1]) == 29) { int res; ``` This works, WTF :|
Author
Owner

@jfoug commented on GitHub (Dec 9, 2015):

CY9rzUYh03PK3k6DJie09g== requires 36 bytes (plus null)

CY9r
zUYh
03PK
3k6D
Jie0
9g==

Each of those are 6 bytes of hex output.

<!-- gh-comment-id:163326035 --> @jfoug commented on GitHub (Dec 9, 2015): CY9rzUYh03PK3k6DJie09g== requires 36 bytes (plus null) CY9r zUYh 03PK 3k6D Jie0 9g== Each of those are 6 bytes of hex output.
Author
Owner

@kholia commented on GitHub (Dec 9, 2015):

Fixed in commit 72b3c7cadc. Thanks @jfoug 👍

<!-- gh-comment-id:163332747 --> @kholia commented on GitHub (Dec 9, 2015): Fixed in commit 72b3c7cadc9d5f57414f9f8c392479b148b0df53. Thanks @jfoug :+1:
Author
Owner

@magnumripper commented on GitHub (Dec 9, 2015):

OK I can see how/why this happened now. But I do not like this fix very much. We should fix the base64 conversion. Why would we supply sizeof(out) at all, if it's blatantly ignored?

<!-- gh-comment-id:163336922 --> @magnumripper commented on GitHub (Dec 9, 2015): OK I can see how/why this happened now. But I do not like this fix very much. We should fix the base64 conversion. Why would we supply `sizeof(out)` at all, if it's blatantly ignored?
Author
Owner

@jfoug commented on GitHub (Dec 9, 2015):

The way to do this will lose performance. That is to always convert into a temp buffer, and then copy from that temp buffer into the 'final' buffer. This could be done with a 7 byte buffer (large enough for the 6 bytes + null. Then the problem goes away.

The 'bug' is due to using the output buffer to write into.

<!-- gh-comment-id:163339356 --> @jfoug commented on GitHub (Dec 9, 2015): The way to do this will lose performance. That is to always convert into a temp buffer, and then copy from that temp buffer into the 'final' buffer. This could be done with a 7 byte buffer (large enough for the 6 bytes + null. Then the problem goes away. The 'bug' is due to using the output buffer to write into.
Author
Owner

@jfoug commented on GitHub (Dec 9, 2015):

This has been a 'well known' bug. But usually it was 4 extra bytes in size, since usually we were doing base-64 into raw. That gives us 4 byte blocks into 3 bytes. For the 64-2-hex, it is 4 bytes into 6 + null, so it is 7 bytes longer (at max).

<!-- gh-comment-id:163339795 --> @jfoug commented on GitHub (Dec 9, 2015): This has been a 'well known' bug. But usually it was 4 extra bytes in size, since usually we were doing base-64 into raw. That gives us 4 byte blocks into 3 bytes. For the 64-2-hex, it is 4 bytes into 6 + null, so it is 7 bytes longer (at max).
Author
Owner

@magnumripper commented on GitHub (Dec 9, 2015):

Is base64_convert ever performance critical?

<!-- gh-comment-id:163346083 --> @magnumripper commented on GitHub (Dec 9, 2015): Is base64_convert ever performance critical?
Author
Owner

@jfoug commented on GitHub (Dec 9, 2015):

I believe it is in some formats, but I am not sure any of them are quick. Things like scrypt (IIRC) use it in the inner loop. I am not sure anything moderately fast uses it.

It does make load times longer for high count of hashes. But 'most' high count hash bundles are base-16, and thus are quick, even if the base64_convert code is used.

<!-- gh-comment-id:163349069 --> @jfoug commented on GitHub (Dec 9, 2015): I believe it is in some formats, but I am not sure any of them are quick. Things like scrypt (IIRC) use it in the inner loop. I am not sure anything moderately fast uses it. It does make load times longer for high count of hashes. But 'most' high count hash bundles are base-16, and thus are quick, even if the base64_convert code is used.
Author
Owner

@magnumripper commented on GitHub (Dec 9, 2015):

I think mime_to_hex could have some added cleverness with regards to to_len. But I'm not sure we should touch it until after Jumbo-2.

OTOH I doubt a memcpy would give a measurable drop in speed. The code isn't written for speed at all.

<!-- gh-comment-id:163360579 --> @magnumripper commented on GitHub (Dec 9, 2015): I think mime_to_hex could have some added cleverness with regards to to_len. But I'm not sure we should touch it until after Jumbo-2. OTOH I doubt a memcpy would give a measurable drop in speed. The code isn't written for speed at all.
Author
Owner

@magnumripper commented on GitHub (Dec 9, 2015):

I'll ignore this until next time it bites me.

<!-- gh-comment-id:163375298 --> @magnumripper commented on GitHub (Dec 9, 2015): I'll ignore this until next time it bites me.
Author
Owner

@jfoug commented on GitHub (Dec 9, 2015):

I'll ignore this until next time it bites me.

lol. Understood.

<!-- gh-comment-id:163383793 --> @jfoug commented on GitHub (Dec 9, 2015): > I'll ignore this until next time it bites me. lol. Understood.
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#1296
No description provided.