mirror of
https://github.com/openwall/john.git
synced 2026-12-03 18:45:21 +07:00
[GH-ISSUE #1110] Dynamic allocations of large buffers #693
Labels
No labels
bug
core branch
documentation
enhancement
enhancement
fixed - pending verify
fuzz bug
good first issue
invalid
invalid
maintenance/cleanup
new cracking mode
new format
new tool
non-trivial
notes/external issues
portability
question
RFC / discussion
testing
WONTFIX
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
openwall/john#693
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @magnumripper on GitHub (Mar 16, 2015).
Original GitHub issue: https://github.com/openwall/john/issues/1110
At 240 threads (eg. Solar's little MIC) and a plaintext length of 125, and an OMP_SCALE of 16K, the saved_keys buffer alone is near half a gigabyte. We need to start using malloc/free for these buffers.
http://www.openwall.com/lists/john-dev/2015/03/15/6
I have fixed all the worst offenders (sorted by OMP_SCALE and/or BLOCK_LOOPS) but there are a lot more to do.
46b9ee5,3b6727f,0e4b5fb,db1f7e7@magnumripper commented on GitHub (Mar 16, 2015):
OK so we have a big problem: Some versions of malloc() (at least on 32-bit) apparently may return a pointer not aligned suitably for SIMD.
@jfoug:
Yes, let's stop and think. I can revert my commits, or we can just leave everything as-is for a short while. I really do not like ugly workarounds. Even a semi-nice workaround like your mem_alloc_align() will be less nice due to the need for keeping two pointers for every buffer.
@magnumripper commented on GitHub (Mar 16, 2015):
@jfoug how hard do you think it would be to instead implement that "mem_alloc_tiny with domains" that we have discussed over the years? Then a format's done() could call some kind of "free everything this format alloced tiny".
@magnumripper commented on GitHub (Mar 16, 2015):
One question is "what do we use as identifier" for the "domain". It would be nice to be able to use the format label (which could be hashed to a 32-bit or 64-bit number behind the scenes).
Eg.
@jfoug commented on GitHub (Mar 16, 2015):
That 'might' be an option. I really do not know. I do not overly care for the 2 pointers, BUT it really is not the big of a change. I have done scores of them already. The change is really pretty minute, BUT does require someone knowing the nuances.
@magnumripper commented on GitHub (Mar 16, 2015):
The more you commit, the more I will need to revert - if that is what we end up doing.
Do you think we should just move ahead and live with this? I will have a look at your fixes.
@magnumripper commented on GitHub (Mar 16, 2015):
OK it's not that ugly. Let's just fix everything like you have done so far. I will take a break now though and fix a bunch of formats tomorrow.
@magnumripper commented on GitHub (Mar 16, 2015):
BTW a trivial temporary hack in mem_alloc() so it always returns aligned to 8 but not to 16 will show all problems.
@jfoug commented on GitHub (Mar 16, 2015):
The 'domain' would simply be a handle. How it is handled under the hood does not really matter to the calling function. But tracking a handle here, vs a couple instances of 'double' pointers is not really that big of a trade off.
I would think we would want this to be the calling interface:
HANDLE malloc_tiny_init(const char *name, size_t starting size);
Then do malloc_tiny passing int the handle like you show.
The nice things about this, are that one call to free, does it all. Also, we may gain memory localization. My concern is we can do this easily IF we force the user to list the amount of memory needed up front. Then we do not have to track a list of memory buffers, like we currently do for the tiny stuff. But we 'could' make handle driven tiny memory that can grow (like current tiny does).
Should we keep the 'current' implementation? I think there are 1000's of usages, so if we do change, then we would have to change it all. One thing we 'could' do, is to have john VERY early on, call the init on a 'global' section, and then simply change the code in memory.c to 'use' this global handle, IF the older functions were used.
@magnumripper commented on GitHub (Mar 16, 2015):
Note that if we walk this path, we should not alter the core mem_alloc_tiny(). We should add an alternative (let's call it malloc_tiny() because it's short and I hate long names) that handles domains.
@jfoug commented on GitHub (Mar 16, 2015):
Ok, how do you do that ??? Remember, the pointer returned HAS to be passed into free. I guess we could allocate an extra 16 bytes, then put a pointer RIGHT before the pointer we return that is the 'real' 2nd pointer. So we would never return the 'real' pointer. Then we would have to change MEM_FREE() to handle this case. I think we would want to have a special MAGIC value, AND a pointer, just in case someone uses malloc() and then MEM_FREE. But if anyone used mem_alloc that is hacked, then free() would choke.
@magnumripper commented on GitHub (Mar 16, 2015):
OK, well you are right... but we could do it with mem_dbg! 😄
@jfoug commented on GitHub (Mar 16, 2015):
But why not change the mem_alloc_tiny implementation (the real code), to simply call malloc_tiny(GLOBAL_HANDLE, size) ?
@magnumripper commented on GitHub (Mar 16, 2015):
That we could do. But I dislike core changes. When possible I prefer to add to core, not change it.
@jfoug commented on GitHub (Mar 16, 2015):
it is memdbg 'like'. But another layer. That return having to be 'valid' for free is why I had to do the 2 pointer trick already. I 'could' have hidden the 2nd pointer just like I am talking about here, BUT then we have serious issues mixing and matching (but with the 2 pointer model, we still have issues mixing/matching anyway).
Well, I am out for while, so I will not be jamming more changed files
@magnumripper commented on GitHub (Mar 16, 2015):
Hmm could we not simply use
posix_memalign()and not having to bother with the extra pointers? That one is "IEEE Std 1003.1-2001 (``POSIX.1'')".@jfoug commented on GitHub (Mar 16, 2015):
But what portability?
@magnumripper commented on GitHub (Mar 17, 2015):
Drats :(
@jfoug commented on GitHub (Mar 17, 2015):
Did sound like the proper function however. Directly allowed user set alignment (power of 2), worked directly with free, etc. if it was as portable as malloc, then wham, that is certainly the way to go. I am not saying it is not the way. BUT since this one IS tied into the normal allocator, if it is NOT available on a system,, I am not sure we can emulate it in jumbo.c The workarounds I am seeing all require changes, and unfortunately either 2 pointers or a 'smart' pointer (the memdbg uses a smart pointer and back and forward references the outer pointer wrapper). But to use them, we would HAVE to assure that all matching frees were done through our wrapper code, such as MEM_FREE()
@magnumripper commented on GitHub (Mar 17, 2015):
Yes. Some googling indicates there is no really portable function until C11's aligned_alloc(), which is a bit too new a standard...
I really like the idea of
malloc_tiny(handle, size, align). Not sure if we should just go ahead and implement that (with all caveats that will lead to, lol) or try to patch things up using double pointers first.@magnumripper commented on GitHub (Mar 17, 2015):
These are still to fix provided my one-liner[tm] is correct:
@magnumripper commented on GitHub (Mar 17, 2015):
@jfoug how about this: Tomorrow I'll fix all of the above, and you just concentrate on implementing malloc_tiny(handle, size, align)? Implement it in just a few formats to start with (and none of the above) and test heavily so we get through teething problems.
I think we'll not see many caveats as long as we use the global domain for things like the output arrays allocated in salt() and binary(). If we do not, we also need to add logic for re-allocating them if called again after done() (eg. in dynamic, or in any format when using --stress-test=0). And there's no need to complicate things like that, so we should only use this domain alloc for things allocated in init().
@magnumripper commented on GitHub (Mar 17, 2015):
BTW Solar has mention keeping "hot" and "cold" stuff separated. The things we've been dealing with today are almost exclusively hot but we could keep it in mind. If we go with the "handle derived from name" we could just do this for making two pools with no extra things added in the functions:
@magnumripper commented on GitHub (Mar 17, 2015):
Obviously we could opt to do with just this
The function would provide the unique id (which may be just a sequential number or whatever it sees fit).
However, using a string as identifier would enable sharing pools without sharing any global variable. For example, some shared OpenCL code could use
malloc_tiny_init("OpenCL", MEM_ALLOC_SIZE);and code elsewhere could alloc from that domain with no other knowledge than that string "OpenCL".For this to work we also need some "get handle to already initialized pool" as in
OTOH, the shared OpenCL code could just as well share a global handle
opencl_mem_pool. So maybe this whole idea of passing a string is silly.@magnumripper commented on GitHub (Mar 17, 2015):
Here's Solar's suggestion: "Since jumbo is autoconf'ed anyway, I think using posix_memalign() is fine. We're probably not going to find a relevant system supporting SIMD vectors wider than a double that doesn't also have posix_memalign()."
@magnumripper commented on GitHub (Mar 17, 2015):
I will get some sleep now, maybe I know what to do next once I wake up.
@magnumripper commented on GitHub (Mar 17, 2015):
OK, this is my plan:
@jfoug commented on GitHub (Mar 17, 2015):
I'm fine with this as long as it works right with 32 bit systems that do not have posix_memalign such as VC. I just do not see how you are going to do that. If you can do that, then why do we need the posix function?
@jfoug commented on GitHub (Mar 17, 2015):
Ok, I have these functions. Note, they still use the dual pointer interface, BUT do not need to, I simply cut code into the existing stuff to test.
memory.c:
memory.h
What say you? This seems to work (at least on VC). I will test on other 32 bit systems shortly. It does avoid an infinite loop. It may loop a couple times to get into alignment, but then does so. NOTE, I am ONLY testing this on 16 byte alignment now, so once we need to go to 32 or 64 or 128, it will become more difficult, and there we probably would want to either use a 2 pointer method, OR use a block buffer allocator like mem_alloc_tiny or a named pool type of that code.
@jfoug commented on GitHub (Mar 17, 2015):
Here was a run (VC, and I turned on the warning message). It crashed on xsha512, but I think that one was not completed yet (had not crashed prior to this run.
@jfoug commented on GitHub (Mar 17, 2015):
I did catch one what may have been inf loop (allocating a 128 byte buffer). I have made changes, and hope that has been worked around. I will let you know after more testing.
@jfoug commented on GitHub (Mar 17, 2015):
Nope, I can not get it to easily work on linux-32. It inf-loops :(
@magnumripper commented on GitHub (Mar 17, 2015):
VC has never been a supported target. If it would I suppose we'd have a project file or something?
We need to get MinGW and Cygwin up and running though (and of course fix VC some way too, eventually).
f22ab24adds use of posix_memalign() with some alternatives from autoconf. I added a mingw version I saw mentioned in some google hit but that is untested. You may need to tweak something just a little but I think this might just work on Cygwin and MinGW, at least recent versions of them.For VC, you could add your loop code if you like. An alternative may be to use _aligned_malloc() (which is in fact already used as last alternative right now) but I suspect that one really needs to be paired with _aligned_free() or some such. That is doable, for example you could make all allocs end up using _aligned_malloc() (with align 8 as default) and all free's using _aligned_free(). But that would be only for systems lacking alternatives.
@jfoug commented on GitHub (Mar 17, 2015):
Here is cygwin64 after
f22ab24I am pretty sure this is all due to -D_GNU_SOURCE -D_XOPEN_SOURCE=600
But it did build (with the warnings, some of which were there prior), and it appears to run.
@magnumripper commented on GitHub (Mar 17, 2015):
So we need to separate this thing in m4/jtr_systems_specific_logic.m4
Into some variant of
@jfoug commented on GitHub (Mar 17, 2015):
Let me first see if it even builds without _GNU_SOURCE and _XOPEN_SOURCE=600 it MAY NOT build.
@magnumripper commented on GitHub (Mar 17, 2015):
I can't find any man pages for Cygwin on the 'net. They should state what feature test macros are needed.
@jfoug commented on GitHub (Mar 17, 2015):
Ok with this (which configure was using)
I get all of the warnings.
If I change to just
I just get a single warning in pp.c about implicent declaration of fileno (I have been getting that one for some time). The code built and linked with no other errors, and it runs.
NOTE, it does core in SSH-ng (could that be a format missed?)
@magnumripper commented on GitHub (Mar 17, 2015):
BTW for Linux we can use
_POSIX_C_SOURCE >= 200112Linstead of_XOPEN_SOURCE >= 600. Maybe try that?@jfoug commented on GitHub (Mar 17, 2015):
-D_POSIX_C_SOURCE=200112gives same warnings (actually MANY MORE), than-XOPEN_SOURCE=600But does link and run. I get all sorts issues with the str*() functions (incompatible type warnings, etc).
@magnumripper commented on GitHub (Mar 17, 2015):
I do not think I missed anything, I verified the whole tree since pre-
46b9ee5. What malloc did it end up using?EDIT: No, that file is unchanged.
@jfoug commented on GitHub (Mar 17, 2015):
All I know is a -test=0 stops there. No crash, no 'warning', But when that happens, it is a crash, it just is that cygwin often hides the crash when in OMP mode (and the crash is in a thread).
@magnumripper commented on GitHub (Mar 17, 2015):
Yes but this
@jfoug commented on GitHub (Mar 17, 2015):
checking for aligned_alloc... no
checking for posix_memalign... yes
@magnumripper commented on GitHub (Mar 17, 2015):
OK, with posix_memalign there should definitely not be any problem. BTW SSH-ng does not use the align function, nor SIMD at all!
@jfoug commented on GitHub (Mar 17, 2015):
Ok, then it could be some other change recently. That might be one that I worked on preparing for strtokm (iirc)
@jfoug commented on GitHub (Mar 17, 2015):
NOTE, I will be making some changes to the aligned functions, to get them into proper memdbg usage (i.e. using macros and 2 different prototypes).
I am pretty sure the _aligned_malloc will be all that is needed for VC. I will just switch over to using it exclusively. Within the MSDN, it shows _aligned_free, BUT does not list that it has to be used to free. I will see.
@magnumripper commented on GitHub (Mar 17, 2015):
Sure, I'll do some regression testing on odd systems.
@magnumripper commented on GitHub (Mar 17, 2015):
OpenBSD 5.6 works fine (in this regard)
@jfoug commented on GitHub (Mar 17, 2015):
memdbg is totally busted with this new change. I am not 100% sure I can unbust it or not. Well I am sure I can, but it may not be super trivial.
@jfoug commented on GitHub (Mar 17, 2015):
Note, if VC becomes an 'unsupportable' target, then my work on, or at least ability to debug on john would likely come to an end, or be greatly throttled down.
NOTE, there has never been 'official' support I have kept things working. But something THIS large, where it can not be either built or run, would greatly impact the development that I do, plain and simple.
@magnumripper commented on GitHub (Mar 17, 2015):
We will obviously get the VC pieces together. I'd be surprised if it takes more than just a few lines of magic, be it _aligned_malloc() or something else. We found solutions for every other bad stuff I added, like mmap() and whatnot, and you were worried then too ;-)
The way I picture it, there are two ways to solve this:
or, (failing that):
2. Find a function that does it and needs a special free() [or write one, that's trivial]. Then just use those for everything (with trivial changes to the existing macros/functions). Problem solved.
@jfoug commented on GitHub (Mar 17, 2015):
I have gotten VC working (I hope). I also have memdbg working with the new changes (well, I thought I had it, until I started building on POSIX systems). memdbg should be close.
@jfoug commented on GitHub (Mar 17, 2015):
Btw, I chose #2.
@jfoug commented on GitHub (Mar 17, 2015):
memdbg with mem_alloc_align is getting closer. I have it compiling and running (just running for test right now).
SSH-ng cores, but with that one commented out in john.conf, memdbg run went to end, and listed
MemDbg_Validate level 0 checking PassedNow that is with a mem_alloc_align that can actually 'use' the standard free() function. VC was very different. I had to overload malloc/calloc/realloc/free/strdup, both in the memory/memdbg functions AND within jtr 'proper'. This is due to usage of MEM_FREE() calling free. I likely have a bit more work to do to get VC 100% right, but at least I am pretty sure that I can do it now.
@jfoug commented on GitHub (Mar 17, 2015):
Ok, this gets memdbg working again. It also is the VC port (may still have a little work to do here).
7e1b5b6@magnumripper commented on GitHub (Mar 17, 2015):
I get this (before your commit too!) on gcc-4.9 and when using posix_memalign()
I hate that kind of false positives. The code is effectively
Apparently I have to initialize ptr to NULL even though that's a total waste. Not that it matters but it irritates me to no end.
@jfoug commented on GitHub (Mar 17, 2015):
what a wast of time, for sure!
I am working with VC (in memdbg mode). Mostly, I am finding source files that do strdup which do NOT include memdbg.h Before it was not as big a problem. Now that I have to use _align_free() in VC builds, it is a HUGE problem, and has to be fixed. I have to do macro magic to get strdup to work happily with MEM_FREE() macro.
Looks like I may have gotten them all. At least in encfs_common_plug.c and crypt_sha1_common_plug.c the include was missing. Found a mem leak in encfs also. We were returning in valid, and not doing a goto err;
@magnumripper commented on GitHub (Mar 17, 2015):
@jfoug commented on GitHub (Mar 17, 2015):
Thanks. It is only a problem if strdup is used. Using the CRTL strdup (by not including memdbg.h in VC), and then later using _align_free() is a crash.
@magnumripper commented on GitHub (Mar 17, 2015):
Remaining task now: Change formats that still use mem_alloc_tiny(). I took them in order of OMP_SCALE and the like, and I believe only formats with a scaling of 1024 or less are left. Eventually all should be fixed.
@kholia please note that all new formats should use mem_alloc() and init() and MEM_FREE() in done() for dynamic buffers.
@jfoug commented on GitHub (Mar 17, 2015):
Note 2, for simd buffers, usr the aligned allocation or 32 bit builds will core
@jfoug commented on GitHub (Jul 25, 2015):
I am going to simply bite the bullet, and correct all of them. All data allocated in valid()/salt() I think has been fixed. no more mem_alloc_tiny/mem_calloc_tiny there. I am now fixing all formats to use mem_alloc_align (if there was alignment used) for globals used in crypt_all and adding a done() to clean things up.
@jfoug commented on GitHub (Jul 25, 2015):
NOTE, we might want to run some fuzzing, mangling again, since switching from the _tiny() allocation to real allocation exposes the real buffers. In other words, there may be -ASan errors which were hidden, since we were doing mem_alloc_tiny, and the buffer over/underwrites were hidden because the pointer returned was just somewhere inside of a much larger allocated buffer. Now they all live on their own, and if there are problems, they may show up now.