[GH-ISSUE #2065] *** buffer overflow detected *** on qemu-arm #1385

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

Originally created by @lei-april on GitHub (Feb 24, 2016).
Original GitHub issue: https://github.com/openwall/john/issues/2065

Originally assigned to: @lei-april on GitHub.

When I run john --test on qemu-arm, it aborts at format 'xsha512' and prints

*** buffer overflow detected ***: ../run/john terminated

But when I try to run john --test --format=xsha512 and the remaining formats individually, everything just goes fine.

Ideas?

Originally created by @lei-april on GitHub (Feb 24, 2016). Original GitHub issue: https://github.com/openwall/john/issues/2065 Originally assigned to: @lei-april on GitHub. When I run `john --test` on qemu-arm, it aborts at format 'xsha512' and prints ``` *** buffer overflow detected ***: ../run/john terminated ``` But when I try to run `john --test --format=xsha512` and the remaining formats individually, everything just goes fine. Ideas?
Author
Owner

@magnumripper commented on GitHub (Feb 24, 2016):

You could test individual format using -test-full=0 and see if it triggers something. Or a smaller subsets of "many" formats with -test -form:x*. Also, if you can build with ASan that is likely to help nailing it.

<!-- gh-comment-id:188118011 --> @magnumripper commented on GitHub (Feb 24, 2016): You could test individual format using `-test-full=0` and see if it triggers something. Or a smaller subsets of "many" formats with `-test -form:x*`. Also, if you can build with ASan that is likely to help nailing it.
Author
Owner

@lei-april commented on GitHub (Feb 24, 2016):

Building with ASan does re-trigger the *** buffer overflow detected *** error on xsha512, apart from which it gives no other useful info.

It's theoretically possible to remotely debug john in QEMU, which seems a bit painful though...

<!-- gh-comment-id:188306294 --> @lei-april commented on GitHub (Feb 24, 2016): Building with ASan does re-trigger the `*** buffer overflow detected ***` error on xsha512, apart from which it gives no other useful info. It's theoretically possible to remotely debug john in QEMU, which seems a bit painful though...
Author
Owner

@frank-dittrich commented on GitHub (Feb 24, 2016):

On my Raspberry Pi 2, I also get a segfault. I am sure I ran the --test successfully not too long ago. So I think it must be a recent regression. Will bisect and see if I can get a useful backtrace from an ASan build.

<!-- gh-comment-id:188369035 --> @frank-dittrich commented on GitHub (Feb 24, 2016): On my Raspberry Pi 2, I also get a segfault. I am sure I ran the --test successfully not too long ago. So I think it must be a recent regression. Will bisect and see if I can get a useful backtrace from an ASan build.
Author
Owner

@magnumripper commented on GitHub (Feb 24, 2016):

Building with ASan does re-trigger the *** buffer overflow detected *** error on xsha512, apart from which it gives no other useful info.

That sounds like you don't actually get ASan but perhaps only -Og or -O1 or something. The configure script doesn't handle "no ASan supported" very well - it's silently ignored.

<!-- gh-comment-id:188437871 --> @magnumripper commented on GitHub (Feb 24, 2016): > Building with ASan does re-trigger the **\* buffer overflow detected **\* error on xsha512, apart from which it gives no other useful info. That sounds like you don't actually get ASan but perhaps only -Og or -O1 or something. The configure script doesn't handle "no ASan supported" very well - it's silently ignored.
Author
Owner

@lei-april commented on GitHub (Feb 25, 2016):

That sounds like you don't actually get ASan but perhaps only -Og or -O1 or something. The configure script doesn't handle "no ASan supported" very well - it's silently ignored.

I think I did get ASan enabled. I upgraded the cross-compiler to gcc 5.2 to get this feature, and there is indeed a -fsanitize=address option in Makefile after configuring.

<!-- gh-comment-id:188569207 --> @lei-april commented on GitHub (Feb 25, 2016): > That sounds like you don't actually get ASan but perhaps only -Og or -O1 or something. The configure script doesn't handle "no ASan supported" very well - it's silently ignored. I think I did get ASan enabled. I upgraded the cross-compiler to gcc 5.2 to get this feature, and there is indeed a `-fsanitize=address` option in Makefile after configuring.
Author
Owner

@magnumripper commented on GitHub (Feb 25, 2016):

OK. But what's the actual origin of the message *** buffer overflow detected ***? Is it libc? Apparently it actually stops us from getting an ASan diagnose because it bails out before buffers are actually getting thrashed.

<!-- gh-comment-id:188641206 --> @magnumripper commented on GitHub (Feb 25, 2016): OK. But what's the actual origin of the message `*** buffer overflow detected ***`? Is it libc? Apparently it actually stops us from getting an ASan diagnose because it bails out before buffers are actually getting thrashed.
Author
Owner

@frank-dittrich commented on GitHub (Feb 25, 2016):

@zzlei Does ./john --list=build-info print ASan in the Build: line?

@magnumripper git grep on latest glibc:

(master)glibc $ git grep -B 4 -A 1 -n "buffer overflow detected"
debug/chk_fail.c-24-void
debug/chk_fail.c-25-__attribute__ ((noreturn))
debug/chk_fail.c-26-__chk_fail (void)
debug/chk_fail.c-27-{
debug/chk_fail.c:28:  __fortify_fail ("buffer overflow detected");
debug/chk_fail.c-29-}
(master)glibc $ git grep -n -B 2 -A 7 '^__fortify_fail '
debug/fortify_fail.c-24-void
debug/fortify_fail.c-25-__attribute__ ((noreturn)) internal_function
debug/fortify_fail.c:26:__fortify_fail (const char *msg)
debug/fortify_fail.c-27-{
debug/fortify_fail.c-28-  /* The loop is added only to keep gcc happy.  */
debug/fortify_fail.c-29-  while (1)
debug/fortify_fail.c-30-    __libc_message (2, "*** %s ***: %s terminated\n",
debug/fortify_fail.c-31-                    msg, __libc_argv[0] ?: "<unknown>");
debug/fortify_fail.c-32-}
debug/fortify_fail.c-33-libc_hidden_def (__fortify_fail)
<!-- gh-comment-id:188674162 --> @frank-dittrich commented on GitHub (Feb 25, 2016): @zzlei Does `./john --list=build-info` print `ASan` in the `Build:` line? @magnumripper `git grep` on latest glibc: ``` (master)glibc $ git grep -B 4 -A 1 -n "buffer overflow detected" debug/chk_fail.c-24-void debug/chk_fail.c-25-__attribute__ ((noreturn)) debug/chk_fail.c-26-__chk_fail (void) debug/chk_fail.c-27-{ debug/chk_fail.c:28: __fortify_fail ("buffer overflow detected"); debug/chk_fail.c-29-} ``` ``` (master)glibc $ git grep -n -B 2 -A 7 '^__fortify_fail ' debug/fortify_fail.c-24-void debug/fortify_fail.c-25-__attribute__ ((noreturn)) internal_function debug/fortify_fail.c:26:__fortify_fail (const char *msg) debug/fortify_fail.c-27-{ debug/fortify_fail.c-28- /* The loop is added only to keep gcc happy. */ debug/fortify_fail.c-29- while (1) debug/fortify_fail.c-30- __libc_message (2, "*** %s ***: %s terminated\n", debug/fortify_fail.c-31- msg, __libc_argv[0] ?: "<unknown>"); debug/fortify_fail.c-32-} debug/fortify_fail.c-33-libc_hidden_def (__fortify_fail) ```
Author
Owner

@lei-april commented on GitHub (Feb 25, 2016):

@zzlei Does ./john --list=build-info print ASan in the Build: line?

Yep.

<!-- gh-comment-id:188676702 --> @lei-april commented on GitHub (Feb 25, 2016): > @zzlei Does ./john --list=build-info print ASan in the Build: line? Yep.
Author
Owner

@frank-dittrich commented on GitHub (Feb 25, 2016):

@zzlei can you run the self test in gdb and see if you get a useful stack?
When I run the test, I always get a corrupted stack. But it looks like your build has -D_FORTIFY_SOURCE=1 -fstack-protector or something similar included.

May be I'll get a useful backtrace when I try to add

CFLAGS_EXTRA="-D_FORTIFY_SOURCE=1 -fstack-protector"

to ./configure.

If it helps to get a useful backtrace, we should add -fstack-protector to all debug builds.

<!-- gh-comment-id:188684171 --> @frank-dittrich commented on GitHub (Feb 25, 2016): @zzlei can you run the self test in gdb and see if you get a useful stack? When I run the test, I always get a corrupted stack. But it looks like your build has `-D_FORTIFY_SOURCE=1 -fstack-protector` or something similar included. May be I'll get a useful backtrace when I try to add ``` CFLAGS_EXTRA="-D_FORTIFY_SOURCE=1 -fstack-protector" ``` to ./configure. If it helps to get a useful backtrace, we should add -fstack-protector to all debug builds.
Author
Owner

@frank-dittrich commented on GitHub (Feb 25, 2016):

Still without CFLAGS_EXTRA="-D_FORTIFY_SOURCE=1 -fstack-protector", I tested the top 18 recent commits. 9 of those ended with a segfault, 9 didn't. The pattern looks somewhat random.
I couldn't reproduce the segfaults for recent commits when I tried to add --enable-memdbg or --enable-asan to ./configure.

<!-- gh-comment-id:188695809 --> @frank-dittrich commented on GitHub (Feb 25, 2016): Still without `CFLAGS_EXTRA="-D_FORTIFY_SOURCE=1 -fstack-protector"`, I tested the top 18 recent commits. 9 of those ended with a segfault, 9 didn't. The pattern looks somewhat random. I couldn't reproduce the segfaults for recent commits when I tried to add --enable-memdbg or --enable-asan to ./configure.
Author
Owner

@frank-dittrich commented on GitHub (Feb 25, 2016):

Here's a backtrace

Benchmarking: xsha512, Mac OS X 10.7 [SHA512 128/128 NEON 2x]... (4xOMP) *** buffer overflow detected ***: /home/pi/git/JtR/run/john terminated

Program received signal SIGABRT, Aborted.
0x76b8df70 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
56  ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0  0x76b8df70 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
#1  0x76b8f324 in __GI_abort () at abort.c:89
#2  0x76bc9954 in __libc_message (do_abort=do_abort@entry=2, fmt=0x76c83ce0 "*** %s ***: %s terminated\n") at ../sysdeps/posix/libc_fatal.c:175
#3  0x76c42cd0 in __GI___fortify_fail (msg=0x76c83c84 "buffer overflow detected") at fortify_fail.c:31
#4  0x76c40dcc in __GI___chk_fail () at chk_fail.c:28
#5  0x76c4008c in __strcpy_chk (
    dest=dest@entry=0x7effdf88 "\004\325\311v\376\256C\207\001\031\316\351m\365\221\065\370\tT\"\215\023\233g;K\204m\375\316t\315]W\223\306\304\363\231|\306\337O**a\340\070\004/\"\324\003)\v\220N\262j\n:\035Z\220\341@5Ȁ\367\v{J*\242\254\342\035\225\v\v\204\260n\376\256C\207\001\031\316\351m\365\221\065\370\tT\"\215\023\233g;K\204m", 
    src=src@entry=0xed4844 "\004\325\311v\376\256C\207\001\031\316\351m\365\221\065\370\tT\"\215\023\233g;K\204m\375\316t\315]W\223\306\304\363\231|\306\337O**a\340\070\004/\"\324\003)\v\220N\262j\n:\035Z\220\341@5Ȁ\367\v{J*\242\254\342\035\225\v\v\204\260n\376\256C\207\001\031\316\351m\365\221\065\370\tT\"\215\023\233g;K\204m\375\316t\315]W\223\306\304\363\231|\306\337O*\020\001", destlen=<optimized out>, destlen@entry=108) at strcpy_chk.c:60
#6  0x00169054 in strcpy (
    __src=0xed4844 "\004\325\311v\376\256C\207\001\031\316\351m\365\221\065\370\tT\"\215\023\233g;K\204m\375\316t\315]W\223\306\304\363\231|\306\337O**a\340\070\004/\"\324\003)\v\220N\262j\n:\035Z\220\341@5Ȁ\367\v{J*\242\254\342\035\225\v\v\204\260n\376\256C\207\001\031\316\351m\365\221\065\370\tT\"\215\023\233g;K\204m\375\316t\315]W\223\306\304\363\231|\306\337O*\020\001", 
    __dest=0x7effdf88 "\004\325\311v\376\256C\207\001\031\316\351m\365\221\065\370\tT\"\215\023\233g;K\204m\375\316t\315]W\223\306\304\363\231|\306\337O**a\340\070\004/\"\324\003)\v\220N\262j\n:\035Z\220\341@5Ȁ\367\v{J*\242\254\342\035\225\v\v\204\260n\376\256C\207\001\031\316\351m\365\221\065\370\tT\"\215\023\233g;K\204m") at /usr/include/arm-linux-gnueabihf/bits/string3.h:104
#7  set_key (key=0xed4840 "123", index=1992834272) at XSHA512_fmt_plug.c:203
#8  0x0022d18c in bench_set_keys (format=format@entry=0x3cdd9c <fmt_XSHA512>, current=0x3cdfa0 <sha512_common_tests_xsha512+288>, current@entry=0x3cde80 <sha512_common_tests_xsha512>, cond=cond@entry=0) at bench.c:262
#9  0x0022d89c in benchmark_format (format=0x3cdd9c <fmt_XSHA512>, salts=0, results=<optimized out>, test_db=<optimized out>) at bench.c:463
#10 0x0022e29c in benchmark_all () at bench.c:803
#11 0x0024285c in john_run () at john.c:1587
#12 0x00243514 in main (argc=2, argv=0x7efff504) at john.c:1973

This is with 1.8.0-jumbo-1-4690-g16aa93b, using ./configure CFLAGS_EXTRA="-D_FORTIFY_SOURCE=1 -fstack-protector"

<!-- gh-comment-id:188748367 --> @frank-dittrich commented on GitHub (Feb 25, 2016): Here's a backtrace ``` Benchmarking: xsha512, Mac OS X 10.7 [SHA512 128/128 NEON 2x]... (4xOMP) *** buffer overflow detected ***: /home/pi/git/JtR/run/john terminated Program received signal SIGABRT, Aborted. 0x76b8df70 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56 56 ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory. (gdb) bt #0 0x76b8df70 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56 #1 0x76b8f324 in __GI_abort () at abort.c:89 #2 0x76bc9954 in __libc_message (do_abort=do_abort@entry=2, fmt=0x76c83ce0 "*** %s ***: %s terminated\n") at ../sysdeps/posix/libc_fatal.c:175 #3 0x76c42cd0 in __GI___fortify_fail (msg=0x76c83c84 "buffer overflow detected") at fortify_fail.c:31 #4 0x76c40dcc in __GI___chk_fail () at chk_fail.c:28 #5 0x76c4008c in __strcpy_chk ( dest=dest@entry=0x7effdf88 "\004\325\311v\376\256C\207\001\031\316\351m\365\221\065\370\tT\"\215\023\233g;K\204m\375\316t\315]W\223\306\304\363\231|\306\337O**a\340\070\004/\"\324\003)\v\220N\262j\n:\035Z\220\341@5Ȁ\367\v{J*\242\254\342\035\225\v\v\204\260n\376\256C\207\001\031\316\351m\365\221\065\370\tT\"\215\023\233g;K\204m", src=src@entry=0xed4844 "\004\325\311v\376\256C\207\001\031\316\351m\365\221\065\370\tT\"\215\023\233g;K\204m\375\316t\315]W\223\306\304\363\231|\306\337O**a\340\070\004/\"\324\003)\v\220N\262j\n:\035Z\220\341@5Ȁ\367\v{J*\242\254\342\035\225\v\v\204\260n\376\256C\207\001\031\316\351m\365\221\065\370\tT\"\215\023\233g;K\204m\375\316t\315]W\223\306\304\363\231|\306\337O*\020\001", destlen=<optimized out>, destlen@entry=108) at strcpy_chk.c:60 #6 0x00169054 in strcpy ( __src=0xed4844 "\004\325\311v\376\256C\207\001\031\316\351m\365\221\065\370\tT\"\215\023\233g;K\204m\375\316t\315]W\223\306\304\363\231|\306\337O**a\340\070\004/\"\324\003)\v\220N\262j\n:\035Z\220\341@5Ȁ\367\v{J*\242\254\342\035\225\v\v\204\260n\376\256C\207\001\031\316\351m\365\221\065\370\tT\"\215\023\233g;K\204m\375\316t\315]W\223\306\304\363\231|\306\337O*\020\001", __dest=0x7effdf88 "\004\325\311v\376\256C\207\001\031\316\351m\365\221\065\370\tT\"\215\023\233g;K\204m\375\316t\315]W\223\306\304\363\231|\306\337O**a\340\070\004/\"\324\003)\v\220N\262j\n:\035Z\220\341@5Ȁ\367\v{J*\242\254\342\035\225\v\v\204\260n\376\256C\207\001\031\316\351m\365\221\065\370\tT\"\215\023\233g;K\204m") at /usr/include/arm-linux-gnueabihf/bits/string3.h:104 #7 set_key (key=0xed4840 "123", index=1992834272) at XSHA512_fmt_plug.c:203 #8 0x0022d18c in bench_set_keys (format=format@entry=0x3cdd9c <fmt_XSHA512>, current=0x3cdfa0 <sha512_common_tests_xsha512+288>, current@entry=0x3cde80 <sha512_common_tests_xsha512>, cond=cond@entry=0) at bench.c:262 #9 0x0022d89c in benchmark_format (format=0x3cdd9c <fmt_XSHA512>, salts=0, results=<optimized out>, test_db=<optimized out>) at bench.c:463 #10 0x0022e29c in benchmark_all () at bench.c:803 #11 0x0024285c in john_run () at john.c:1587 #12 0x00243514 in main (argc=2, argv=0x7efff504) at john.c:1973 ``` This is with 1.8.0-jumbo-1-4690-g16aa93b, using `./configure CFLAGS_EXTRA="-D_FORTIFY_SOURCE=1 -fstack-protector"`
Author
Owner

@lei-april commented on GitHub (Feb 25, 2016):

@zzlei can you run the self test in gdb and see if you get a useful stack?

OK.

But as I'm running john in QEMU (user mode), remote debugging is the only way, which probably need some setup first.

<!-- gh-comment-id:188750221 --> @lei-april commented on GitHub (Feb 25, 2016): > @zzlei can you run the self test in gdb and see if you get a useful stack? OK. But as I'm running john in QEMU (user mode), remote debugging is the only way, which probably need some setup first.
Author
Owner

@lei-april commented on GitHub (Feb 25, 2016):

I find no -fstack-proctector in Makefile. But even after I recompiled XSHA512_fmt_plug.c with this option and then rebuilt john, I still get this error.

<!-- gh-comment-id:188754023 --> @lei-april commented on GitHub (Feb 25, 2016): I find no `-fstack-proctector` in Makefile. But even after I recompiled XSHA512_fmt_plug.c with this option and then rebuilt john, I still get this error.
Author
Owner

@frank-dittrich commented on GitHub (Feb 25, 2016):

@zzlei Since I got the backtrace on my Raspberry Pi 2, I think you don't need to do this anymore.

For some reason, your build envoronment must have used -fstack-protector or -D_FORTIFY_SOURCE=1 or -D_FORTIFY_SOURCE=2 (see man 7 feature_test_macros).
After I used

./configure CFLAGS_EXTRA="-D_FORTIFY_SOURCE=1 -fstack-protector"

I also got the *** buffer overflow detected ***: /home/pi/git/JtR/run/john terminated output.

(I probably could have used gdb to get a back trace even without this CFLAGS_EXTRA change.
I would just have needed to continue after gdb detected the illegal statement caused by OpenSSL's CPU probing.)

So, the error is in the strcpy in line 203 of XSHA512_fmt_plug.c

        const ARCH_WORD_64 *wkey = is_aligned(key+4, sizeof(uint64_t)) ?
                        (ARCH_WORD_64*)(key+4) : (ARCH_WORD_64*)strcpy(buf_aligned, key+4);

To reproduce, an architecture where ARCH_ALLOWS_UNALIGNED isn't true is needed.
(Not sure if you could use CFLAGS_EXTRA="-DARCH_ALLOWS_UNALIGNED=0" to reproduce this on x86-64.)

<!-- gh-comment-id:188757614 --> @frank-dittrich commented on GitHub (Feb 25, 2016): @zzlei Since I got the backtrace on my Raspberry Pi 2, I think you don't need to do this anymore. For some reason, your build envoronment must have used -fstack-protector or -D_FORTIFY_SOURCE=1 or -D_FORTIFY_SOURCE=2 (see man 7 feature_test_macros). After I used ``` ./configure CFLAGS_EXTRA="-D_FORTIFY_SOURCE=1 -fstack-protector" ``` I also got the `*** buffer overflow detected ***: /home/pi/git/JtR/run/john terminated` output. (I probably could have used gdb to get a back trace even without this CFLAGS_EXTRA change. I would just have needed to `continue` after gdb detected the illegal statement caused by OpenSSL's CPU probing.) So, the error is in the strcpy in line 203 of XSHA512_fmt_plug.c ``` const ARCH_WORD_64 *wkey = is_aligned(key+4, sizeof(uint64_t)) ? (ARCH_WORD_64*)(key+4) : (ARCH_WORD_64*)strcpy(buf_aligned, key+4); ``` To reproduce, an architecture where ARCH_ALLOWS_UNALIGNED isn't true is needed. (Not sure if you could use `CFLAGS_EXTRA="-DARCH_ALLOWS_UNALIGNED=0"` to reproduce this on x86-64.)
Author
Owner

@lei-april commented on GitHub (Feb 25, 2016):

buf_aligned is a local array of size PLAINTEXT + 1, and PLAINTEXT_LENGTH is just 107 in this context. I'm not sure if this is big enough to cause buffer overflow...

I'll try tinkering with ARCH_ALLOWS_UNALIGNED in an x86 build then.

UPDATE: I do get a buffer overflow in a x86-64 build with -DARCH_ALLOWS_UNALIGNED=0.

<!-- gh-comment-id:188763019 --> @lei-april commented on GitHub (Feb 25, 2016): `buf_aligned` is a local array of size `PLAINTEXT + 1`, and `PLAINTEXT_LENGTH` is just 107 in this context. I'm not sure if this is big enough to cause buffer overflow... I'll try tinkering with `ARCH_ALLOWS_UNALIGNED` in an x86 build then. **UPDATE**: I do get a buffer overflow in a x86-64 build with `-DARCH_ALLOWS_UNALIGNED=0`.
Author
Owner

@frank-dittrich commented on GitHub (Feb 25, 2016):

I just added an assert(strlen(key+4) <= PLAINTEXT_LEN).
With just testing xsha512, everything is fine.

Now I test everything and see if that assert fails.
If so, I'll repeat this from within gdb. (It's just that testing all formats on Raspberry Pi 2 takes a while.)

<!-- gh-comment-id:188767068 --> @frank-dittrich commented on GitHub (Feb 25, 2016): I just added an assert(strlen(key+4) <= PLAINTEXT_LEN). With just testing xsha512, everything is fine. Now I test everything and see if that assert fails. If so, I'll repeat this from within gdb. (It's just that testing all formats on Raspberry Pi 2 takes a while.)
Author
Owner

@lei-april commented on GitHub (Feb 25, 2016):

Curiously, after inserting assert(strlen(key+4) <= PLAINTEXT_LEN); before the problematic strcpy, ASan kicks in right when strlen is executed and reports a heap buffer overflow.

So it's not likely that key is too long, since it could only cause a stack overflow. I'm even more confused now...

<!-- gh-comment-id:188777707 --> @lei-april commented on GitHub (Feb 25, 2016): Curiously, after inserting `assert(strlen(key+4) <= PLAINTEXT_LEN);` before the problematic `strcpy`, ASan kicks in right when `strlen` is executed and reports a heap buffer overflow. So it's not likely that `key` is too long, since it could only cause a stack overflow. I'm even more confused now...
Author
Owner

@lei-april commented on GitHub (Feb 25, 2016):

Oh I see. The problem is that key + 4 is not necessary null-terminated.

We could change strcpy(buf_aligned, key+4) to memcpy(buf_aligned, key + 4, PLAINTEXT_SIZE + 1) or memcpy(buf_aligned, key + 4, max(strlen(key) - 4, 0)).

<!-- gh-comment-id:188781041 --> @lei-april commented on GitHub (Feb 25, 2016): Oh I see. The problem is that `key + 4` is not necessary null-terminated. We could change `strcpy(buf_aligned, key+4)` to `memcpy(buf_aligned, key + 4, PLAINTEXT_SIZE + 1)` or `memcpy(buf_aligned, key + 4, max(strlen(key) - 4, 0))`.
Author
Owner

@frank-dittrich commented on GitHub (Feb 25, 2016):

What is the purpose of that key+4, anyway?
If the password candidate has a strlen of 1 or 2, key+4 points to some random memory location.

<!-- gh-comment-id:188782630 --> @frank-dittrich commented on GitHub (Feb 25, 2016): What is the purpose of that key+4, anyway? If the password candidate has a strlen of 1 or 2, key+4 points to some random memory location.
Author
Owner

@jfoug commented on GitHub (Feb 25, 2016):

Try this:

(Note, this was handed edited a couple times, so take the diff from this post, not what may have been emailed out. The length >= 4 vs length > 4 is critical.)

diff --git a/src/XSHA512_fmt_plug.c b/src/XSHA512_fmt_plug.c
index 2ea6774..a6a566b 100644
--- a/src/XSHA512_fmt_plug.c
+++ b/src/XSHA512_fmt_plug.c
@@ -200,7 +200,9 @@ static void set_key(char *key, int index)
 #else
        char buf_aligned[PLAINTEXT_LENGTH + 1] JTR_ALIGN(sizeof(uint64_t));
        const ARCH_WORD_64 *wkey = is_aligned(key+4, sizeof(uint64_t)) ?
-                       (ARCH_WORD_64*)(key+4) : (ARCH_WORD_64*)strcpy(buf_aligned, key+4);
+                       (ARCH_WORD_64*)(key+4) : (ARCH_WORD_64*)buf_aligned;
+       if (wkey == buf_aligned && length >= 4)
+               strcpy(buf_aligned, key+4);
 #endif
        ARCH_WORD_64 *keybuffer = &((ARCH_WORD_64 *)saved_key)[(index&(SIMD_COEF_64-1)) + (unsigned int)index/SIMD_COEF_64*SHA_BUF_SIZ*SIMD_COEF_64];
        ARCH_WORD_64 *keybuf_word = keybuffer;
<!-- gh-comment-id:188788033 --> @jfoug commented on GitHub (Feb 25, 2016): Try this: (Note, this was handed edited a couple times, so take the diff from this post, not what may have been emailed out. The length >= 4 vs length > 4 is critical.) ``` diff diff --git a/src/XSHA512_fmt_plug.c b/src/XSHA512_fmt_plug.c index 2ea6774..a6a566b 100644 --- a/src/XSHA512_fmt_plug.c +++ b/src/XSHA512_fmt_plug.c @@ -200,7 +200,9 @@ static void set_key(char *key, int index) #else char buf_aligned[PLAINTEXT_LENGTH + 1] JTR_ALIGN(sizeof(uint64_t)); const ARCH_WORD_64 *wkey = is_aligned(key+4, sizeof(uint64_t)) ? - (ARCH_WORD_64*)(key+4) : (ARCH_WORD_64*)strcpy(buf_aligned, key+4); + (ARCH_WORD_64*)(key+4) : (ARCH_WORD_64*)buf_aligned; + if (wkey == buf_aligned && length >= 4) + strcpy(buf_aligned, key+4); #endif ARCH_WORD_64 *keybuffer = &((ARCH_WORD_64 *)saved_key)[(index&(SIMD_COEF_64-1)) + (unsigned int)index/SIMD_COEF_64*SHA_BUF_SIZ*SIMD_COEF_64]; ARCH_WORD_64 *keybuf_word = keybuffer; ```
Author
Owner

@jfoug commented on GitHub (Feb 25, 2016):

key+4 gets us to the next 8 byte aligned spot in the key buffer. There is 4 bytes already loaded for salt, we load the next 4 using one-sy inserts, then we can roll into using 64 byte faster copy logic.

This was an obvious bug, but untested, since if key WAS aligned, then k+4 is NOT aligned, and we would blindly do a strcpy of data which may or may not be part of the key. So when key was < 4 bytes long, we would start strcpying random data, and since we had no idea what it was, buffer overflow...

<!-- gh-comment-id:188789180 --> @jfoug commented on GitHub (Feb 25, 2016): key+4 gets us to the next 8 byte aligned spot in the key buffer. There is 4 bytes already loaded for salt, we load the next 4 using one-sy inserts, then we can roll into using 64 byte faster copy logic. This was an obvious bug, but untested, since if key WAS aligned, then k+4 is NOT aligned, and we would blindly do a strcpy of data which may or may not be part of the key. So when key was < 4 bytes long, we would start strcpying random data, and since we had no idea what it was, buffer overflow...
Author
Owner

@frank-dittrich commented on GitHub (Feb 25, 2016):

@jfoug will test your change with that commit github.com/frank-dittrich/JohnTheRipper@a090d91ffb on top of it.
(Fixing a syntax error and compiler warnings.)
Once that's tested, I'll push my commit upstream.

<!-- gh-comment-id:188872959 --> @frank-dittrich commented on GitHub (Feb 25, 2016): @jfoug will test your change with that commit https://github.com/frank-dittrich/JohnTheRipper/commit/a090d91ffb28aa66e828d28b1ce190c801303b8c on top of it. (Fixing a syntax error and compiler warnings.) Once that's tested, I'll push my commit upstream.
Author
Owner

@frank-dittrich commented on GitHub (Feb 25, 2016):

With commit c8ad432d38, the segfault / buffer overflow is gone.
@zzlei If you still see any problems, please reopen.

<!-- gh-comment-id:188915275 --> @frank-dittrich commented on GitHub (Feb 25, 2016): With commit c8ad432d387fd1edaed3fee98b4bf2e081291f22, the segfault / buffer overflow is gone. @zzlei If you still see any problems, please reopen.
Author
Owner

@jfoug commented on GitHub (Feb 25, 2016):

I had no real way to test this, so the original commit was done blind. @frank-dittrich thanks for correcting the code. The 'logic' was right, and it was pretty easy to see that the strcpy should only OPTIONALLY be called, and that if the strlen was <= 3, that the 1-off byte copying would goto the end, skipping any usage of that buf_aligned data, so skipping the strcpy was 100% valid in that situation.

Good this bug is smoked.

<!-- gh-comment-id:188944599 --> @jfoug commented on GitHub (Feb 25, 2016): I had no real way to test this, so the original commit was done blind. @frank-dittrich thanks for correcting the code. The 'logic' was right, and it was pretty easy to see that the strcpy should only OPTIONALLY be called, and that if the strlen was <= 3, that the 1-off byte copying would goto the end, skipping any usage of that buf_aligned data, so skipping the strcpy was 100% valid in that situation. Good this bug is smoked.
Author
Owner

@frank-dittrich commented on GitHub (Feb 25, 2016):

Should we add -D_FORTIFY_SOURCE=1 -fstack-protector to debug builds (memdbg, ASan, UbSan), because it hopefully detects problems which might bot be found or harder to find otherwise?

BTW: Do we need to #include <features.h>? See man 7 feature_test_macros.

So far, only the (copied) config.guess uses features.h:

(bleeding-jumbo)JtR $ git grep -B 2 -A 10 features\.h
src/config.guess-       eval $set_cc_for_build
src/config.guess-       cat <<-EOF > $dummy.c
src/config.guess:       #include <features.h>
src/config.guess-       #if defined(__UCLIBC__)
src/config.guess-       LIBC=uclibc
src/config.guess-       #elif defined(__dietlibc__)
src/config.guess-       LIBC=dietlibc
src/config.guess-       #else
src/config.guess-       LIBC=gnu
src/config.guess-       #endif
src/config.guess-       EOF
src/config.guess-       eval `$CC_FOR_BUILD -E $dummy.c 2>/dev/null | grep '^LIBC'`
src/config.guess-       ;;
<!-- gh-comment-id:189002751 --> @frank-dittrich commented on GitHub (Feb 25, 2016): Should we add `-D_FORTIFY_SOURCE=1 -fstack-protector` to debug builds (memdbg, ASan, UbSan), because it hopefully detects problems which might bot be found or harder to find otherwise? BTW: Do we need to `#include <features.h>`? See `man 7 feature_test_macros`. So far, only the (copied) config.guess uses features.h: ``` (bleeding-jumbo)JtR $ git grep -B 2 -A 10 features\.h src/config.guess- eval $set_cc_for_build src/config.guess- cat <<-EOF > $dummy.c src/config.guess: #include <features.h> src/config.guess- #if defined(__UCLIBC__) src/config.guess- LIBC=uclibc src/config.guess- #elif defined(__dietlibc__) src/config.guess- LIBC=dietlibc src/config.guess- #else src/config.guess- LIBC=gnu src/config.guess- #endif src/config.guess- EOF src/config.guess- eval `$CC_FOR_BUILD -E $dummy.c 2>/dev/null | grep '^LIBC'` src/config.guess- ;; ```
Author
Owner

@magnumripper commented on GitHub (Feb 25, 2016):

Should we add -D_FORTIFY_SOURCE=1 -fstack-protector to debug builds (memdbg, ASan, UbSan)

Unless I missed something here, those options were the reason Lei didn't get a proper ASan crash report. So I would say definitely not!

<!-- gh-comment-id:189005514 --> @magnumripper commented on GitHub (Feb 25, 2016): > Should we add -D_FORTIFY_SOURCE=1 -fstack-protector to debug builds (memdbg, ASan, UbSan) Unless I missed something here, those options were the reason Lei **didn't** get a proper ASan crash report. So I would say definitely not!
Author
Owner

@jfoug commented on GitHub (Feb 25, 2016):

I fully agree. This would have shown up much easier with ASAN, than buried deep in the clib. This would be an option that would hinder more than help

<!-- gh-comment-id:189010589 --> @jfoug commented on GitHub (Feb 25, 2016): I fully agree. This would have shown up much easier with ASAN, than buried deep in the clib. This would be an option that would hinder more than help
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#1385
No description provided.