[GH-ISSUE #752] Add support for dynamic salt [compare] size #387

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

Originally created by @magnumripper on GitHub (Sep 23, 2014).
Original GitHub issue: https://github.com/openwall/john/issues/752

Originally assigned to: @jfoug on GitHub.

http://www.openwall.com/lists/john-users/2014/07/06/1

See also #736. The pending functionality in OldOffice (as well as in RAR and ZIP, which currently have hacks in cracker.c) need a salt compare size different from the salt copy size.

Solar's suggestion fixes that as well as opens up for dynamically allocated salts (that can also be freed):
Have a flag like FMT_DYNAMIC_SALT that would tell the rest of JtR that salt() returns a dynamically allocated salt of arbitrary size and encodes the actual size in first size_t of the returned buffer. (...) Since we intend to use this for cases where salts are large, I think the dynamic allocation should be made in a way that can be free()d, and the rest of JtR should actually be free()ing these as appropriate

Originally created by @magnumripper on GitHub (Sep 23, 2014). Original GitHub issue: https://github.com/openwall/john/issues/752 Originally assigned to: @jfoug on GitHub. http://www.openwall.com/lists/john-users/2014/07/06/1 See also #736. The pending functionality in OldOffice (as well as in RAR and ZIP, which currently have hacks in cracker.c) need a salt _compare size_ different from the salt _copy size_. Solar's suggestion fixes that as well as opens up for dynamically allocated salts (that can also be freed): _Have a flag like FMT_DYNAMIC_SALT that would tell the rest of JtR that salt() returns a dynamically allocated salt of arbitrary size and encodes the actual size in first size_t of the returned buffer. (...) Since we intend to use this for cases where salts are large, I think the dynamic allocation should be made in a way that can be free()d, and the rest of JtR should actually be free()ing these as appropriate_
bsakaguchi 2026-10-14 03:04:11 +07:00
Author
Owner

@magnumripper commented on GitHub (Sep 23, 2014):

We'd need to encode the salt compare size separately too, in a second size_t.

<!-- gh-comment-id:56463973 --> @magnumripper commented on GitHub (Sep 23, 2014): We'd need to encode the _salt compare size_ separately too, in a second size_t.
Author
Owner

@magnumripper commented on GitHub (Sep 23, 2014):

We'd need to encode the salt compare size separately too, in a second size_t.

For RAR and ZIP, we could do without this, and use SALT_SIZE as salt compare size. But for OldOffice, we'll need to be able to change the compare size on the fly, for any given salt.

<!-- gh-comment-id:56464225 --> @magnumripper commented on GitHub (Sep 23, 2014): > We'd need to encode the salt compare size separately too, in a second size_t. For RAR and ZIP, we could do without this, and use SALT_SIZE as _salt compare size_. But for OldOffice, we'll need to be able to **change** the compare size on the fly, for any given salt.
Author
Owner

@magnumripper commented on GitHub (Sep 23, 2014):

No, wait. We can do that anyway. We just change self->params.salt_size! So Solar's suggestion is good as-is.

<!-- gh-comment-id:56606063 --> @magnumripper commented on GitHub (Sep 23, 2014): No, wait. We can do that anyway. We just change self->params.salt_size! So Solar's suggestion is good as-is.
Author
Owner

@magnumripper commented on GitHub (Sep 23, 2014):

Hmm but that is not per salt. I need to think this over.

<!-- gh-comment-id:56606141 --> @magnumripper commented on GitHub (Sep 23, 2014): Hmm but that is not per salt. I need to think this over.
Author
Owner

@jfoug commented on GitHub (Oct 2, 2014):

typedef struct dyna_salt_t {
size_t salt_cmp_size;
unsigned char buffer[1];
} dyna_salt;

#define DYNA_SALT_SIZE sizeof(dyna_salt*)

then simply set the salt_size in the format structure to be DYNA_SALT_SIZE

End of problem.

For a custom salt, all you need is a structure that has the first element be a size_t. allocate one of these structures, whatever 'shape' they are. They can be ANYTHING, as long as they have a size_t item FIRST in the structure, that is filled in. The size_t can be unique to each salt ITEM, not fixed for the class.

Then in the salt() function, allocate your struct, fill it in, and return a pointer to that structure. John core will treat it as a pointer of sizeof(pointer) copy the data. It can dereference that pointer to get the size, which will be the first item of a structure. Easy-peasy.

<!-- gh-comment-id:57567886 --> @jfoug commented on GitHub (Oct 2, 2014): typedef struct dyna_salt_t { size_t salt_cmp_size; unsigned char buffer[1]; } dyna_salt; #define DYNA_SALT_SIZE sizeof(dyna_salt*) then simply set the salt_size in the format structure to be DYNA_SALT_SIZE End of problem. For a custom salt, all you need is a structure that has the first element be a size_t. allocate one of these structures, whatever 'shape' they are. They can be ANYTHING, as long as they have a size_t item FIRST in the structure, that is filled in. The size_t can be unique to each salt ITEM, not fixed for the class. Then in the salt() function, allocate your struct, fill it in, and return a pointer to that structure. John core will treat it as a pointer of sizeof(pointer) copy the data. It can dereference that pointer to get the size, which will be the first item of a structure. Easy-peasy.
Author
Owner

@jfoug commented on GitHub (Oct 2, 2014):

Actually, all salts could be done this way, BUT that would take work on each salt. For formats that have a fixed size salt, this is not a gain. jtr core takes same amount of code to do it the way it is today, or this new way. But for some formats (old-office, dynamic) it makes a LOT of sense. I went to a LOT of trouble with salts in dyna, doing about the same thing. I always return a pointer. But I did add other logic, and got some very useful gain from it. The code i did certainly made things like the regen-lost-salts code easier to do. That is why it is ONLY in dyna formats at this time. But I could have done it if dyna was using a 'dyna_salt' structure method also. I doubt I will change dyna to use a dyna_salt structure pointer, but I could 'look' at it. There is about 1000 to 1500 lines of code in the salt handing. I have to deal with everything. salt hashing, uniqueing, etc, etc. A lot of tricky ugly code that I do not understand all that well any more, since it has been about 2 to 3 years since I have looked at it. But to undo it and change to another method is not super trivial either, and the re-gen salts code would have to be pretty much redone.

<!-- gh-comment-id:57568362 --> @jfoug commented on GitHub (Oct 2, 2014): Actually, all salts could be done this way, BUT that would take work on each salt. For formats that have a fixed size salt, this is not a gain. jtr core takes same amount of code to do it the way it is today, or this new way. But for some formats (old-office, dynamic) it makes a LOT of sense. I went to a LOT of trouble with salts in dyna, doing about the same thing. I always return a pointer. But I did add other logic, and got some very useful gain from it. The code i did certainly made things like the regen-lost-salts code easier to do. That is why it is ONLY in dyna formats at this time. But I could have done it if dyna was using a 'dyna_salt' structure method also. I doubt I will change dyna to use a dyna_salt structure pointer, but I could 'look' at it. There is about 1000 to 1500 lines of code in the salt handing. I have to deal with everything. salt hashing, uniqueing, etc, etc. A lot of tricky ugly code that I do not understand all that well any more, since it has been about 2 to 3 years since I have looked at it. But to undo it and change to another method is not super trivial either, and the re-gen salts code would have to be pretty much redone.
Author
Owner

@jfoug commented on GitHub (Oct 2, 2014):

Note, this is very similar to how MS does API functions that use binary structures and expect to be changed over time. You build a struct where the first item is a sizeof, then allocation a structure and set that element to sizeof the structure. Then when the API function is called, the sizeof value which is a fixed item in the structure can tell the API code what structure signature to use when processing. So there might be 4 versions of the structure, but the newest API dll can handle all versions. Some program that was built in 2009 that uses interface #1 still works, even though there are 3 updates to that structure since then. It is a very slick way to do things, keeping old code working, while extending functionality. This is a little different in usage, BUT can achieve same end result.

I ccan take on this task ,if you like. Hmm, I see that you can not have a issue assigned to 2 ppl. Too bad. I can take it, or you can keep it. Your call.

<!-- gh-comment-id:57568764 --> @jfoug commented on GitHub (Oct 2, 2014): Note, this is very similar to how MS does API functions that use binary structures and expect to be changed over time. You build a struct where the first item is a sizeof, then allocation a structure and set that element to sizeof the structure. Then when the API function is called, the sizeof value which is a fixed item in the structure can tell the API code what structure signature to use when processing. So there might be 4 versions of the structure, but the newest API dll can handle all versions. Some program that was built in 2009 that uses interface #1 still works, even though there are 3 updates to that structure since then. It is a very slick way to do things, keeping old code working, while extending functionality. This is a little different in usage, BUT can achieve same end result. I ccan take on this task ,if you like. Hmm, I see that you can not have a issue assigned to 2 ppl. Too bad. I can take it, or you can keep it. Your call.
Author
Owner

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

typedef struct dyna_salt_t {
size_t salt_cmp_size;
unsigned char buffer[1];
} dyna_salt;

#define DYNA_SALT_SIZE sizeof(dyna_salt*)

We need to support dynamic copy size, and dynamic compare size, one or both of them may be dynamic for a given format (or the simplest case: they are actually static but differ). So how will core know what size to copy? If fmt->params.salt_size then this is still static. I think we need to have

typedef struct dyna_salt_t {
    size_t salt_copy_size;
    size_t salt_comp_size;
    unsigned char buffer[1];
} dyna_salt;

First candidates to implement this might be RAR and ZIP. I did start a local topic branch but ran out of time, so it's dusty now and probably nothing to use. Feel free to take over.

<!-- gh-comment-id:57608190 --> @magnumripper commented on GitHub (Oct 2, 2014): > typedef struct dyna_salt_t { > size_t salt_cmp_size; > unsigned char buffer[1]; > } dyna_salt; > > #define DYNA_SALT_SIZE sizeof(dyna_salt*) We need to support dynamic _copy_ size, and dynamic _compare_ size, one or both of them may be dynamic for a given format (or the simplest case: they are actually static but differ). So how will core know what size to copy? If `fmt->params.salt_size` then this is still static. I think we need to have ``` typedef struct dyna_salt_t { size_t salt_copy_size; size_t salt_comp_size; unsigned char buffer[1]; } dyna_salt; ``` First candidates to implement this might be RAR and ZIP. I did start a local topic branch but ran out of time, so it's dusty now and probably nothing to use. Feel free to take over.
Author
Owner

@jfoug commented on GitHub (Oct 6, 2014):

So how will core know what size to copy? If fmt->params.salt_size

#define DYNA_SALT_SIZE sizeof(dyna_salt*)

So fmt->params.salt_size = sizeof(dyna_salt_); ALWAYS, and fmt->params->salt_align = sizeof(dyna_salt_); since we have a pointer.

The salt is a pointer. When the flag is set this will trigger code in the salt compare deep in john to deref the pointer, find the cmp size and compare

There SHOULD be a 2nd size_t value, but not for sizeof salt. I think this is the right way to do it:

typedef struct dyna_salt_t {
size_t salt_cmp_size;
size_t salt_cmp_offset;
unsigned char buffer[1];
} dyna_salt;

The 'offset' will be how many bytes into buffer to skip ahead, before compare. So if there were several pointers to data needed, but not part of the salt, or some other ints, floats, etc, that were not part of the salt, they could be put at the start of the buffer[1] field (as john core would see it), and would not be part of the compare.

I am actively working on this. It really should not be too hard to get this working, and working well.

Here is a 'first' version. It is not used by salt_hash or any salt compare stuff yet. Also, it should be updated in the salt smashing code, to also handle dyna salts. However, the .zip format has fully used this method now. John core simply copies the pointer and passes it back in. The core will need updated to properly access this pointer to do the comparisons.

811d9c2

<!-- gh-comment-id:58016594 --> @jfoug commented on GitHub (Oct 6, 2014): > So how will core know what size to copy? If fmt->params.salt_size > > > #define DYNA_SALT_SIZE sizeof(dyna_salt*) So fmt->params.salt_size = sizeof(dyna_salt_); ALWAYS, and fmt->params->salt_align = sizeof(dyna_salt_); since we have a pointer. The salt is a pointer. When the flag is set this will trigger code in the salt compare deep in john to deref the pointer, find the cmp size and compare There SHOULD be a 2nd size_t value, but not for sizeof salt. I think this is the right way to do it: ``` typedef struct dyna_salt_t { size_t salt_cmp_size; size_t salt_cmp_offset; unsigned char buffer[1]; } dyna_salt; ``` The 'offset' will be how many bytes into buffer to skip ahead, before compare. So if there were several pointers to data needed, but not part of the salt, or some other ints, floats, etc, that were not part of the salt, they could be put at the start of the buffer[1] field (as john core would see it), and would not be part of the compare. I am actively working on this. It really should not be too hard to get this working, and working well. Here is a 'first' version. It is not used by salt_hash or any salt compare stuff yet. Also, it should be updated in the salt smashing code, to also handle dyna salts. However, the .zip format has fully used this method now. John core simply copies the pointer and passes it back in. The core will need updated to properly access this pointer to do the comparisons. 811d9c2
Author
Owner

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

I had some totally different thing in mind but (now that I get it) your solution is probably what Solar meant too. Hmm so revisiting his suggestion, we can still free() this buffer in crk_remove_salt() if we want to (perhaps in the future).

It is not used by salt_hash

salt_hash() is format-local, so you should probably fix that right away in zip_fmt, no?

<!-- gh-comment-id:58072091 --> @magnumripper commented on GitHub (Oct 6, 2014): I had some totally different thing in mind but (now that I get it) your solution is probably what Solar meant too. Hmm so revisiting his suggestion, we can still free() this buffer in crk_remove_salt() if we want to (perhaps in the future). > It is not used by salt_hash salt_hash() is format-local, so you should probably fix that right away in zip_fmt, no?
Author
Owner

@jfoug commented on GitHub (Oct 6, 2014):

your solution is probably what Solar meant too

It is similar to how I do it in dyna already. I think solar dug into that one, and saw what was done, and simply realized it could be generalized and used in other locations. The dyna also has a stub header that must be the same, then the real 'salt-contents' are later in the structure. But the method of simply passing the 'contents' of a pointer back to john's core, and then later doing a dereference of a pointer to a pointer is the easy way to go. Then john thinks salts are simply a fixed length of the size of a pointer (4 or 8 bytes).

Hmm so revisiting his suggestion, we can still free() this buffer in crk_remove_salt()

We can ONLY call free if we do not use mem_alloc_tiny There are reasons to use the mem_alloc_tiny, since it does put salts close together. HOWEVER, for these type salts (large things), packing the salts close enough together may not be needed. But if we have formats that may have variable sized salts, but they are not huge, then using mem_alloc_tiny is really the way to go (such as the current dynamic format).

So yes, it would be pretty easy to remove the salt in crk_remove_salt. I might add another bit (set one of the sizes to *= -1 if the salt was allocated with mem_alloc_tiny. That way crk_remove_salt could be a little smarter.

Ok what parts of JtR need hooked to get this working?

salt_hash
crk_remove_salt
salt-smashing code in fmt_self_test

I already have it in the show options.

what else?

<!-- gh-comment-id:58074724 --> @jfoug commented on GitHub (Oct 6, 2014): > your solution is probably what Solar meant too It is similar to how I do it in dyna already. I think solar dug into that one, and saw what was done, and simply realized it could be generalized and used in other locations. The dyna also has a stub header that must be the same, then the real 'salt-contents' are later in the structure. But the method of simply passing the 'contents' of a pointer back to john's core, and then later doing a dereference of a pointer to a pointer is the easy way to go. Then john thinks salts are simply a fixed length of the size of a pointer (4 or 8 bytes). > Hmm so revisiting his suggestion, we can still free() this buffer in crk_remove_salt() We can ONLY call free if we do not use mem_alloc_tiny There are reasons to use the mem_alloc_tiny, since it does put salts close together. HOWEVER, for these type salts (large things), packing the salts close enough together may not be needed. But if we have formats that may have variable sized salts, but they are not huge, then using mem_alloc_tiny is really the way to go (such as the current dynamic format). So yes, it would be pretty easy to remove the salt in crk_remove_salt. I might add another bit (set one of the sizes to *= -1 if the salt was allocated with mem_alloc_tiny. That way crk_remove_salt could be a little smarter. Ok what parts of JtR need hooked to get this working? salt_hash crk_remove_salt salt-smashing code in fmt_self_test I already have it in the show options. what else?
Author
Owner

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

I'd have a look at these:

$ git grep -nC3 memcmp.*salt loader.c cracker.c
cracker.c-413-          salt = crk_db->salts;
cracker.c-414-
cracker.c-415-  do {
cracker.c:416:          if (!memcmp(pot_salt, salt->salt, potcheck_salt_size))
cracker.c-417-                  break;
cracker.c-418-  } while ((salt = salt->next));
cracker.c-419-
--
loader.c-734-
loader.c-735-           if ((current_salt = db->salt_hash[salt_hash]))
loader.c-736-           do {
loader.c:737:                   if (!memcmp(current_salt->salt, salt,
loader.c-738-                       format->params.salt_size))
loader.c-739-                           break;
loader.c-740-           } while ((current_salt = current_salt->next));

And also search for all mentions of potcheck_salt_size in cracker.c - that variable should eventually not be needed.

<!-- gh-comment-id:58077387 --> @magnumripper commented on GitHub (Oct 6, 2014): I'd have a look at these: ``` $ git grep -nC3 memcmp.*salt loader.c cracker.c cracker.c-413- salt = crk_db->salts; cracker.c-414- cracker.c-415- do { cracker.c:416: if (!memcmp(pot_salt, salt->salt, potcheck_salt_size)) cracker.c-417- break; cracker.c-418- } while ((salt = salt->next)); cracker.c-419- -- loader.c-734- loader.c-735- if ((current_salt = db->salt_hash[salt_hash])) loader.c-736- do { loader.c:737: if (!memcmp(current_salt->salt, salt, loader.c-738- format->params.salt_size)) loader.c-739- break; loader.c-740- } while ((current_salt = current_salt->next)); ``` And also search for all mentions of potcheck_salt_size in cracker.c - that variable should eventually not be needed.
Author
Owner

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

There is no salt_hash() in core, it's format-local. Any format using FMT_DYNA_SALT should handle it properly by themselves.

<!-- gh-comment-id:58078496 --> @magnumripper commented on GitHub (Oct 6, 2014): There is no salt_hash() in core, it's format-local. Any format using FMT_DYNA_SALT should handle it properly by themselves.
Author
Owner

@jfoug commented on GitHub (Oct 7, 2014):

Here is one reason for cmp_offset:

struct some_format_salt {
   size_t salt_cmp_size;
   size_t salt_cmp_off;
   void *p1, *p2;
   int buf_sz1, buf_sz2;
   char buffer[1];
}

In this salt, you have 2 variable sized buffers. To do this, you need to first know how big both are. Then allocate a buffer large enough for the base structure, + the size of both buffers (and null's, or whatever). Then set p1 to point to start of buffer (or wherever the data is, and p2 to point to its data. Then set the comp_size/comp_offset to exclude these 2 pointer, since they completely vary, and are dependent upon the location of the salt in memory BUT not on the salt.

This was what I meant, by putting all of the non-salt related stuff first, followed by the 'real' salt data. To do that, we need an offset, along with size. I am pretty sure with this design, that we can handle all variable sized salts, keying off the unique data to filter out dupes properly.

Also, we should delete memory during load time, when we detect a dupe, not just at salt crack remove.

Well, I am getting the other core code in right now. I am sure we will expand upon this over time, getting it to drop out memory from dupes, and drop memory when salts are cracked. The first time around may not be that important. Getting it working right, fast and solid are what needs done now. Later we will try to save some memory.

If my code works, I should have a checking soon that hooks into loader and cracker as you listed, and try to get it into the salt smashing code also.

<!-- gh-comment-id:58122463 --> @jfoug commented on GitHub (Oct 7, 2014): Here is one reason for cmp_offset: ``` struct some_format_salt { size_t salt_cmp_size; size_t salt_cmp_off; void *p1, *p2; int buf_sz1, buf_sz2; char buffer[1]; } ``` In this salt, you have 2 variable sized buffers. To do this, you need to first know how big both are. Then allocate a buffer large enough for the base structure, + the size of both buffers (and null's, or whatever). Then set p1 to point to start of buffer (or wherever the data is, and p2 to point to its data. Then set the comp_size/comp_offset to exclude these 2 pointer, since they completely vary, and are dependent upon the location of the salt in memory BUT not on the salt. This was what I meant, by putting all of the non-salt related stuff first, followed by the 'real' salt data. To do that, we need an offset, along with size. I am pretty sure with this design, that we can handle all variable sized salts, keying off the unique data to filter out dupes properly. Also, we should delete memory during load time, when we detect a dupe, not just at salt crack remove. Well, I am getting the other core code in right now. I am sure we will expand upon this over time, getting it to drop out memory from dupes, and drop memory when salts are cracked. The first time around may not be that important. Getting it working right, fast and solid are what needs done now. Later we will try to save some memory. If my code works, I should have a checking soon that hooks into loader and cracker as you listed, and try to get it into the salt smashing code also.
Author
Owner

@jfoug commented on GitHub (Oct 7, 2014):

Got it working, but still a few things to complete. b979611

Fixed a few bugs in zip_fmt (overwrite and flag not being set). Now added code to loader, and cracker to properly deal with FMT_DYNA_SALT types. The salt_hash in zip_fmt grown to handle entire blob (not just the salt item).

Tested by concatenating the 2 zip hashes in the file 9 times. When run on 'older' .zip format (or the 1st gen of this patch), it said 44 unique salts. The old zip format found them all. The new one found all but the first (due to the memory overwrite. The fixed version only sees 2 salts and finds them both.

NOTE, I need to get salt smashing code working for this. Also, salt dupe removal should free salts. But first I have to use mem_alloc, an not mem_alloc_tiny, and somehow pass back the information that they are free-able. Then we need to also carry that free code over into fmt_remove_salt function.

@magnumripper please peer review.

<!-- gh-comment-id:58128057 --> @jfoug commented on GitHub (Oct 7, 2014): Got it working, but still a few things to complete. b979611 Fixed a few bugs in zip_fmt (overwrite and flag not being set). Now added code to loader, and cracker to properly deal with FMT_DYNA_SALT types. The salt_hash in zip_fmt grown to handle entire blob (not just the salt item). Tested by concatenating the 2 zip hashes in the file 9 times. When run on 'older' .zip format (or the 1st gen of this patch), it said 44 unique salts. The old zip format found them all. The new one found all but the first (due to the memory overwrite. The fixed version only sees 2 salts and finds them both. NOTE, I need to get salt smashing code working for this. Also, salt dupe removal should free salts. But first I have to use mem_alloc, an not mem_alloc_tiny, and somehow pass back the information that they are free-able. Then we need to also carry that free code over into fmt_remove_salt function. @magnumripper please peer review.
Author
Owner

@jfoug commented on GitHub (Oct 7, 2014):

Found issue. Need to check that sizes/offsets match before checking buffer 866c48a

<!-- gh-comment-id:58128323 --> @jfoug commented on GitHub (Oct 7, 2014): Found issue. Need to check that sizes/offsets match before checking buffer 866c48a
Author
Owner

@jfoug commented on GitHub (Oct 7, 2014):

Added a new default format function for salt_hash for dyna_salt types. Now all of their salt_hash functions can simply use that one. 271911f

<!-- gh-comment-id:58129272 --> @jfoug commented on GitHub (Oct 7, 2014): Added a new default format function for salt_hash for dyna_salt types. Now all of their salt_hash functions can simply use that one. 271911f
Author
Owner

@jfoug commented on GitHub (Oct 7, 2014):

Added format self test salt smashing logic for dyna salts. Before the code was simply smashing the data holding the pointer. Now it smashes the actual salt data. 7040cbc I am not sure this is that big of a deal, since by definition, dyna_salts have to be allocated. But the code was put in for completeness.

<!-- gh-comment-id:58130339 --> @jfoug commented on GitHub (Oct 7, 2014): Added format self test salt smashing logic for dyna salts. Before the code was simply smashing the data holding the pointer. Now it smashes the actual salt data. 7040cbc I am not sure this is that big of a deal, since by definition, dyna_salts have to be allocated. But the code was put in for completeness.
Author
Owner

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

Here is one reason for cmp_offset:

struct some_format_salt {
   size_t salt_cmp_size;
   size_t salt_cmp_off;
   void *p1, *p2;
   int buf_sz1, buf_sz2;
   char buffer[1];
}

In this salt, you have 2 variable sized buffers. To do this, you need to first know how big both are. Then allocate a buffer large enough for the base structure, + the size of both buffers (and null's, or whatever). Then set p1 to point to start of buffer (or wherever the data is, and p2 to point to its data. Then set the comp_size/comp_offset to exclude these 2 pointer, since they completely vary, and are dependent upon the location of the salt in memory BUT not on the salt.

So when deallocating a salt, you'd still just free(salt) and all will be fine, right?

somehow pass back the information that they are free-able.

We could opt to always have them freeable by definition, never use tiny. FMT_DYNA_SALT would mean dynamic size and dynamically allocated. I believe most cases this is needed will be large salts anyway.

<!-- gh-comment-id:58133060 --> @magnumripper commented on GitHub (Oct 7, 2014): > Here is one reason for cmp_offset: > > ``` > struct some_format_salt { > size_t salt_cmp_size; > size_t salt_cmp_off; > void *p1, *p2; > int buf_sz1, buf_sz2; > char buffer[1]; > } > ``` > > In this salt, you have 2 variable sized buffers. To do this, you need to first know how big both are. Then allocate a buffer large enough for the base structure, + the size of both buffers (and null's, or whatever). Then set p1 to point to start of buffer (or wherever the data is, and p2 to point to its data. Then set the comp_size/comp_offset to exclude these 2 pointer, since they completely vary, and are dependent upon the location of the salt in memory BUT not on the salt. So when deallocating a salt, you'd still just free(salt) and all will be fine, right? > somehow pass back the information that they are free-able. We could opt to always have them freeable by definition, never use tiny. FMT_DYNA_SALT would mean dynamic size _and_ dynamically allocated. I believe most cases this is needed will be large salts anyway.
Author
Owner

@jfoug commented on GitHub (Oct 7, 2014):

I have code to list the salt can be freed. I stole 1 bit from offset using an anon union. I also start the myself with a dynasalt struct and not tell same members. I think doing that is safe, but I need to test more. I now have a bunch of memdbg found leaks, so I can make sure I track down all palaces that need frees. I will get it in morning. I'm crashing now

<!-- gh-comment-id:58133907 --> @jfoug commented on GitHub (Oct 7, 2014): I have code to list the salt can be freed. I stole 1 bit from offset using an anon union. I also start the myself with a dynasalt struct and not tell same members. I think doing that is safe, but I need to test more. I now have a bunch of memdbg found leaks, so I can make sure I track down all palaces that need frees. I will get it in morning. I'm crashing now
Author
Owner

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

Please note that ZIP, RAR, Office and likely most other formats needing this has OpenCL variants. All changes should be ported to the OpenCL sources too. I assume this will be trivial, not really changing any GPU stuff.

<!-- gh-comment-id:58134192 --> @magnumripper commented on GitHub (Oct 7, 2014): Please note that ZIP, RAR, Office and likely most other formats needing this has OpenCL variants. All changes should be ported to the OpenCL sources too. I assume this will be trivial, not really changing any GPU stuff.
Author
Owner

@jfoug commented on GitHub (Oct 11, 2014):

Code has now been put out there, which fully cleans up the salts, and which will free a salt when all hashes have been cracked for it (dyna_salts). I have also reduced some complexity (such as the salt compare), making a single function that does either dyna_salt or non-dyna_salt

Getting the memory release correct was not easy. Lots of scattered places in the code, which simply did not care about salts. All salt stuff was done with mem_alloc_tiny, so a great deal of code simply zipped right over it. I am pretty sure I have caught it all.

f606329

Now after others sign off on this, I will get the other formats we things should be dyna_salt done. I would assume it is much more than zip-rar-office, but I can easily start there.

<!-- gh-comment-id:58757232 --> @jfoug commented on GitHub (Oct 11, 2014): Code has now been put out there, which fully cleans up the salts, and which will free a salt when all hashes have been cracked for it (dyna_salts). I have also reduced some complexity (such as the salt compare), making a single function that does either dyna_salt or non-dyna_salt Getting the memory release correct was not easy. Lots of scattered places in the code, which simply did not care about salts. All salt stuff was done with mem_alloc_tiny, so a great deal of code simply zipped right over it. I am pretty sure I have caught it all. f606329 Now after others sign off on this, I will get the other formats we things should be dyna_salt done. I would assume it is much more than zip-rar-office, but I can easily start there.
Author
Owner

@jfoug commented on GitHub (Oct 11, 2014):

rar (cpu) added as dyna_salt. NOTE, I removed to vars from the salt structure. They were (or should have been) local temp vars in the get_salt function, even though they are part of the format line. Once those 2 were gone, all other salt data is now used in salt compare/hash.

619c6de

Small bug fix: 419e767

<!-- gh-comment-id:58766215 --> @jfoug commented on GitHub (Oct 11, 2014): rar (cpu) added as dyna_salt. NOTE, I removed to vars from the salt structure. They were (or should have been) local temp vars in the get_salt function, even though they are part of the format line. Once those 2 were gone, all other salt data is now used in salt compare/hash. 619c6de Small bug fix: 419e767
Author
Owner

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

What vars is that? How could we be that stupid? I implemented the (exact) same changes in rar-opencl now, and best of all that kludge in cracker.c is now history.

<!-- gh-comment-id:58767869 --> @magnumripper commented on GitHub (Oct 11, 2014): What vars is that? How could we be that stupid? I implemented the (exact) same changes in rar-opencl now, and best of all that kludge in cracker.c is now history.
Author
Owner

@jfoug commented on GitHub (Oct 11, 2014):

The vars are in the hash string, but have no place in the runtime salt.

<!-- gh-comment-id:58768055 --> @jfoug commented on GitHub (Oct 11, 2014): The vars are in the hash string, but have no place in the runtime salt.
Author
Owner

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

I got that part but which was it? Are you forcing me to read the diff and find out? ;-)

<!-- gh-comment-id:58768392 --> @magnumripper commented on GitHub (Oct 11, 2014): I got that part but which was it? Are you forcing me to read the diff and find out? ;-)
Author
Owner

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

Oh, I see. They are needed in the hash, but only for building our runtime salt. It's archive name and pos, right?

<!-- gh-comment-id:58768456 --> @magnumripper commented on GitHub (Oct 11, 2014): Oh, I see. They are needed in the hash, but only for building our runtime salt. It's archive name and pos, right?
Author
Owner

@jfoug commented on GitHub (Oct 11, 2014):

U took them both out. Fname and pos

<!-- gh-comment-id:58768536 --> @jfoug commented on GitHub (Oct 11, 2014): U took them both out. Fname and pos
Author
Owner

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

On a side note I've been contemplating to add mmap() support to RAR. Does this fit worse or better now with dynamic salt? Hmm I think it's better still. Instead of loading filename:offset into our allocated buffer, we'd just mmap() it. It's just the difference we must not free it but munmap() it. Maybe we should sacrifice another bit in the offset field for saying it's mmap'ed?

<!-- gh-comment-id:58768766 --> @magnumripper commented on GitHub (Oct 11, 2014): On a side note I've been contemplating to add mmap() support to RAR. Does this fit worse or better now with dynamic salt? Hmm I think it's better still. Instead of loading filename:offset into our allocated buffer, we'd just mmap() it. It's just the difference we _must not free it but munmap() it_. Maybe we should sacrifice another bit in the offset field for saying it's mmap'ed?
Author
Owner

@jfoug commented on GitHub (Oct 12, 2014):

We could take another bit from our off, or use a int32 for flags. Can u think of other flags ?

<!-- gh-comment-id:58769321 --> @jfoug commented on GitHub (Oct 12, 2014): We could take another bit from our off, or use a int32 for flags. Can u think of other flags ?
Author
Owner

@jfoug commented on GitHub (Oct 12, 2014):

Is mmap more or less efficient than a normal allocation ?

<!-- gh-comment-id:58769342 --> @jfoug commented on GitHub (Oct 12, 2014): Is mmap more or less efficient than a normal allocation ?
Author
Owner

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

It's extremely effiecient in case we have a huge file but never actually need much of the data at all. We can almost always do early rejection in the first fraction of it. As far as I know we have a really low number of false positives bad enough to end up in a full CRC32 of the whole data. An mmap() will not actually read ANY data until actually accessed. I bet only the first 4K page will actually be used in almost all uses!

It's also very efficient if you start eight separate sessions against a 4GB RAR file. It will only use one copy of the data (if that!).

<!-- gh-comment-id:58769523 --> @magnumripper commented on GitHub (Oct 12, 2014): It's extremely effiecient in case we have a huge file but never actually need much of the data at all. We can almost always do early rejection in the first fraction of it. As far as I know we have a really low number of false positives bad enough to end up in a full CRC32 of the whole data. An mmap() will not actually read ANY data until actually accessed. I bet only the first 4K page will actually be used in almost all uses! It's also very efficient if you start eight separate sessions against a 4GB RAR file. It will only use one copy of the data (if that!).
Author
Owner

@jfoug commented on GitHub (Oct 12, 2014):

Sounds much better than allocation.for many unfound items it uses little men.

<!-- gh-comment-id:58773334 --> @jfoug commented on GitHub (Oct 12, 2014): Sounds much better than allocation.for many unfound items it uses little men.
Author
Owner

@jfoug commented on GitHub (Oct 12, 2014):

For mmap() to work, would we not need another pointer in the main dyna_salt struct, that points to the part of the struct that is actually mapped? If so, we still could use the salt_alloc_needs_free==1 check for calling MEM_FREE, we would just check this pointer first and if not null we call munmap() first. This does require another pointer of space. I just do not see how we can do it otherwise generically, since the core will not have knowledge of salt record structure.

Thinking a bit more, I think we can do it without having to add a pointer to formats not needing it. Heres how:

typedef struct dyna_salt_t {
    size_t salt_cmp_size;
    struct {
        size_t salt_alloc_needs_free : 1; /* 1 if if malloc/calloc used (not mem_alloc_tiny) */
        size_t salt_alloc_needs_mmap : 1; /* mmap version used */
        size_t salt_cmp_offset : (sizeof(size_t)*8-2);
    };
} dyna_salt;

typedef struct dyna_salt_mmap_t {
    size_t salt_cmp_size;
    struct {
        size_t salt_alloc_needs_free : 1; /* 1 if if malloc/calloc used (not mem_alloc_tiny) */
        size_t salt_alloc_needs_mmap : 1; /* mmap version used */
        size_t salt_cmp_offset : (sizeof(size_t)*8-2);
    };
    char *mmaped;  // whatever type pointer is needed
} dyna_salt_mmap;

then in rar (or other formats needing mmap allocated items, they add a dyna_salt_mmap structure as their first item, setting the right bits from the bitmap structure. the dyna_salt_remove_fp function detects the mmap bit set, re-interpret the structure as a type dyna_salt_mmap type, calls the munmap() and then optionally calls the MEM_FREE on the whole structure. But it is possible that the structure would be better allocated using the mem_alloc_tiny, due to the additional memory overhead of allocation. Yes, a mem_alloc_tiny does not allow us to remove the memory later, but it does often speed up runtime and can reduce memory. Especially if the main large buffer is not being allocated. The way it is setup, a format can alternate between allocation, and mem_free, and can also alternate between mmap and non-mmap. So rar inlines could be allocated non-mmap, and files could be mmap.

One other thing I thought about, is we have to be very careful about cmp_size (possibly offset) if using mmap. Right now, rar has entire blob in the cmp_size range. If we did that for mmap() version, it would allocate the whole thing, correct just to build the hash, and then to later compare. Not good. OR are we dealing with JUST a pointer (within our memory block)? If so, then that pointer certainly would NOT be used during comparison, it has nothing to do with the salt. The reason I ask, is I really do not know much about mmap, so I am not 100% sure how it is implemented. Guess I need to read up some, lol.

<!-- gh-comment-id:58792465 --> @jfoug commented on GitHub (Oct 12, 2014): For mmap() to work, would we not need another pointer in the main dyna_salt struct, that points to the part of the struct that is actually mapped? If so, we still could use the salt_alloc_needs_free==1 check for calling MEM_FREE, we would just check this pointer first and if not null we call munmap() first. This does require another pointer of space. I just do not see how we can do it otherwise generically, since the core will not have knowledge of salt record structure. Thinking a bit more, I think we can do it without having to add a pointer to formats not needing it. Heres how: ``` typedef struct dyna_salt_t { size_t salt_cmp_size; struct { size_t salt_alloc_needs_free : 1; /* 1 if if malloc/calloc used (not mem_alloc_tiny) */ size_t salt_alloc_needs_mmap : 1; /* mmap version used */ size_t salt_cmp_offset : (sizeof(size_t)*8-2); }; } dyna_salt; typedef struct dyna_salt_mmap_t { size_t salt_cmp_size; struct { size_t salt_alloc_needs_free : 1; /* 1 if if malloc/calloc used (not mem_alloc_tiny) */ size_t salt_alloc_needs_mmap : 1; /* mmap version used */ size_t salt_cmp_offset : (sizeof(size_t)*8-2); }; char *mmaped; // whatever type pointer is needed } dyna_salt_mmap; ``` then in rar (or other formats needing mmap allocated items, they add a dyna_salt_mmap structure as their first item, setting the right bits from the bitmap structure. the dyna_salt_remove_fp function detects the mmap bit set, re-interpret the structure as a type dyna_salt_mmap type, calls the munmap() and then optionally calls the MEM_FREE on the whole structure. But it is possible that the structure would be better allocated using the mem_alloc_tiny, due to the additional memory overhead of allocation. Yes, a mem_alloc_tiny does not allow us to remove the memory later, but it does often speed up runtime and can reduce memory. Especially if the main large buffer is not being allocated. The way it is setup, a format can alternate between allocation, and mem_free, and can also alternate between mmap and non-mmap. So rar inlines could be allocated non-mmap, and files could be mmap. One other thing I thought about, is we have to be very careful about cmp_size (possibly offset) if using mmap. Right now, rar has entire blob in the cmp_size range. If we did that for mmap() version, it would allocate the whole thing, correct just to build the hash, and then to later compare. Not good. OR are we dealing with JUST a pointer (within our memory block)? If so, then that pointer certainly would NOT be used during comparison, it has nothing to do with the salt. The reason I ask, is I really do not know much about mmap, so I am not 100% sure how it is implemented. Guess I need to read up some, lol.
Author
Owner

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

I'm not 100% sure about anything. Just thinking loud below.

In the mmap() case we'd need psalt->raw_data to be just a pointer not included in the compare. This is not a problem for RAR, it's actually better than comparing all data during a salt compare. The rest of the salt "can't" be dupe, we still have real salt and CRC in there.

  typedef struct {
    dyna_salt dsalt; /* must be first. allows dyna_salt to work */
    /* place all items we are NOT going to use for salt comparison, first */
+   unsigned char *raw_data;

    /* data from this point on, is part of the salt for compare reasons */
    unsigned char salt[8];
    int type;   /* 0 = -hp, 1 = -p */
    /* for rar -p mode only: */
    union {
        unsigned int w;
        unsigned char c[4];
    } crc;
    unsigned long long pack_size;
    unsigned long long unp_size;
    int method;
-   unsigned char raw_data[1];
  } rarfile;

Current code reads part of the file into our data blob:

            if (!(fp = fopen(archive_name, "rb"))) {
                fprintf(stderr, "! %s: %s\n", archive_name, strerror(errno));
                error();
            }
            fseek(fp, pos, SEEK_SET);
            count = fread(psalt->raw_data, 1, psalt->pack_size, fp);
            if (count != psalt->pack_size) {
                fprintf(stderr, "Error loading file from archive '%s', expected %llu bytes, got %zu. Archive possibly damaged.\n", archive_name, psalt->pack_size, count);
                exit(0);
            }
            fclose(fp);

An mmap() version would be something like this (we only map that same data, not the whole file):

            if (!(fp = fopen(archive_name, "rb"))) {
                fprintf(stderr, "! %s: %s\n", archive_name, strerror(errno));
                error();
            }
            psalt->raw_data = mmap(NULL, psalt->pack_size,
                       PROT_READ, MAP_SHARED,
                       fileno(fp), pos);
            if (psalt->raw_data == MAP_FAILED) {
                fprintf(stderr, "Error loading file from archive '%s'. Archive possibly damaged.\n", archive_name);
                exit(0);
            }
            fclose(fp);

No other code in rar_fmt should need any change. It's still a pointer to the same data. I'm not sure about mmap() and offsets though, when it comes to offsets not being a multiple of page size.

To unmap it, we need the pointer and the length:

    munmap(psalt->raw_data, psalt->pack_size);
<!-- gh-comment-id:58794787 --> @magnumripper commented on GitHub (Oct 12, 2014): I'm not 100% sure about anything. Just thinking loud below. In the mmap() case we'd need psalt->raw_data to be just a pointer not included in the compare. This is not a problem for RAR, it's actually better than comparing all data during a salt compare. The rest of the salt "can't" be dupe, we still have real salt and CRC in there. ``` C typedef struct { dyna_salt dsalt; /* must be first. allows dyna_salt to work */ /* place all items we are NOT going to use for salt comparison, first */ + unsigned char *raw_data; /* data from this point on, is part of the salt for compare reasons */ unsigned char salt[8]; int type; /* 0 = -hp, 1 = -p */ /* for rar -p mode only: */ union { unsigned int w; unsigned char c[4]; } crc; unsigned long long pack_size; unsigned long long unp_size; int method; - unsigned char raw_data[1]; } rarfile; ``` Current code reads _part_ of the file into our data blob: ``` C if (!(fp = fopen(archive_name, "rb"))) { fprintf(stderr, "! %s: %s\n", archive_name, strerror(errno)); error(); } fseek(fp, pos, SEEK_SET); count = fread(psalt->raw_data, 1, psalt->pack_size, fp); if (count != psalt->pack_size) { fprintf(stderr, "Error loading file from archive '%s', expected %llu bytes, got %zu. Archive possibly damaged.\n", archive_name, psalt->pack_size, count); exit(0); } fclose(fp); ``` An mmap() version would be something like this (we only map that same data, not the whole file): ``` C if (!(fp = fopen(archive_name, "rb"))) { fprintf(stderr, "! %s: %s\n", archive_name, strerror(errno)); error(); } psalt->raw_data = mmap(NULL, psalt->pack_size, PROT_READ, MAP_SHARED, fileno(fp), pos); if (psalt->raw_data == MAP_FAILED) { fprintf(stderr, "Error loading file from archive '%s'. Archive possibly damaged.\n", archive_name); exit(0); } fclose(fp); ``` No other code in rar_fmt should need any change. It's still a pointer to the same data. I'm not sure about mmap() and offsets though, when it comes to offsets not being a multiple of page size. To unmap it, we need the pointer and the length: ``` C munmap(psalt->raw_data, psalt->pack_size); ```
Author
Owner

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

This is the uncertain part

     void *
     mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset);

DESCRIPTION
     The mmap() system call causes the pages starting at addr and continuing for at most
     len bytes to be mapped from the object described by fd, starting at byte offset
     offset.  If offset or len is not a multiple of the pagesize, the mapped region may
     extend past the specified range.  Any extension beyond the end of the mapped object
     will be zero-filled.

So what happens if we have a page size of 4K and an offset of 1234? I'm not sure if we'd get what we actually wanted, or if we'd get 1,233 zeros to skip over.

<!-- gh-comment-id:58794989 --> @magnumripper commented on GitHub (Oct 12, 2014): This is the uncertain part ``` void * mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset); DESCRIPTION The mmap() system call causes the pages starting at addr and continuing for at most len bytes to be mapped from the object described by fd, starting at byte offset offset. If offset or len is not a multiple of the pagesize, the mapped region may extend past the specified range. Any extension beyond the end of the mapped object will be zero-filled. ``` So what happens if we have a page size of 4K and an offset of 1234? I'm not sure if we'd get what we actually wanted, or if we'd get 1,233 zeros to skip over.
Author
Owner

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

BTW if I got it right, it's OK to use fclose() after mapping. It will still be mapped.

<!-- gh-comment-id:58795067 --> @magnumripper commented on GitHub (Oct 12, 2014): BTW if I got it right, it's OK to use fclose() after mapping. It will still be mapped.
Author
Owner

@jfoug commented on GitHub (Oct 12, 2014):

I will look at this later tonight. I still think this is a no-go

typedef struct {
    dyna_salt dsalt; /* must be first. allows dyna_salt to work */
    /* place all items we are NOT going to use for salt comparison, first */
+   unsigned char raw_data[1];
    /* data from this point on, is part of the salt for compare reasons */
 ...
 } rarfile;

for 2 reasons (one is EASY to fix). The syntax rar_data[1] will make an inlined array and not a pointer. No big deal. Just change to unsigned char *raw_data; then we can mmap that pointer.

The other issue is that functions in dyna_salt.c have to unmap that, and have NO idea what a rarfile structure is. They know what the dyna_salt struct is (and if you read my message about 5 up, would also know what a dyna_salt_mmap struct is). But they have NO idea how to deal with a raw_data pointer.

I have been thinking more about this, and it MAY be prudent to much with format struct again, adding a free_salt function. Then a format can be in charge of its salt, from beginning to end. But this additional functionality CAN be done with a 'generic' dyna_salt.c function, if we make the base structures smart enough.. Then the format would actually USE the pointer contained in the dyna_salt_mmap structure, IF it is used. For rar, you already have to deal with both types, both mmap and static allocated, since I think we can only mmap a file, not one of the inline objects.

I have also changed rar2john, to allow myself to get some test data for mmap work if we go down that path. I am not sure if you have been following along there or not.

Anyway, it sounds like if we go the struct dyna_salt_mmap method, we will also have to add the length of the mapping along with the pointer to that struct.

<!-- gh-comment-id:58800204 --> @jfoug commented on GitHub (Oct 12, 2014): I will look at this later tonight. I still think this is a no-go ``` typedef struct { dyna_salt dsalt; /* must be first. allows dyna_salt to work */ /* place all items we are NOT going to use for salt comparison, first */ + unsigned char raw_data[1]; /* data from this point on, is part of the salt for compare reasons */ ... } rarfile; ``` for 2 reasons (one is EASY to fix). The syntax rar_data[1] will make an inlined array and not a pointer. No big deal. Just change to unsigned char *raw_data; then we can mmap that pointer. The other issue is that functions in dyna_salt.c have to unmap that, and have NO idea what a rarfile structure is. They know what the dyna_salt struct is (and if you read my message about 5 up, would also know what a dyna_salt_mmap struct is). But they have NO idea how to deal with a raw_data pointer. I have been thinking more about this, and it MAY be prudent to much with format struct again, adding a free_salt function. Then a format can be in charge of its salt, from beginning to end. But this additional functionality CAN be done with a 'generic' dyna_salt.c function, if we make the base structures smart enough.. Then the format would actually USE the pointer contained in the dyna_salt_mmap structure, IF it is used. For rar, you already have to deal with both types, both mmap and static allocated, since I think we can only mmap a file, not one of the inline objects. I have also changed rar2john, to allow myself to get some test data for mmap work if we go down that path. I am not sure if you have been following along there or not. Anyway, it sounds like if we go the struct dyna_salt_mmap method, we will also have to add the length of the mapping along with the pointer to that struct.
Author
Owner

@jfoug commented on GitHub (Oct 12, 2014):

I'm not sure about mmap() and offset though, when it comes to offsets not being a multiple of page size.

Can we simply map from start of file, and simply access data += pos ? So ar[pos] would be the first byte? If we do that, will mmap pull the entire first part of the file in memory, when we hit that first byte??? I would hope not, but do not know the implementation. It would not be overly hard to change this to work that way. Then for non-mmap hashes (inlines), we just set pos to 0, so the same code still works.

<!-- gh-comment-id:58800476 --> @jfoug commented on GitHub (Oct 12, 2014): > I'm not sure about mmap() and offset though, when it comes to offsets not being a multiple of page size. Can we simply map from start of file, and simply access data += pos ? So ar[pos] would be the first byte? If we do that, will mmap pull the entire first part of the file in memory, when we hit that first byte??? I would hope not, but do not know the implementation. It would not be overly hard to change this to work that way. Then for non-mmap hashes (inlines), we just set pos to 0, so the same code still works.
Author
Owner

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

The other issue is that functions in dyna_salt.c have to unmap that, and have NO idea what a rarfile structure is. They know what the dyna_salt struct is (and if you read my message about 5 up, would also know what a dyna_salt_mmap struct is). But they have NO idea how to deal with a raw_data pointer.

Yes, I overlooked this. So we really need to add some kind of mmap-specific struct like the dyna_salt_mmap_t you suggested.

Can we simply map from start of file, and simply access data += pos ? So ar[pos] would be the first byte? If we do that, will mmap pull the entire first part of the file in memory, when we hit that first byte?

Actually it can be done like this:

    ptr = mmap(whole file);
    ptr += offset;
    (rest of format runs unchanged)

But the unmapping would obviously need to be

    munmap(ptr - offset, total_file_len)

Alternatively (EDITED):

    ptr = mmap(len + (offset % page_size), offset & ~(page_size - 1));
    ptr += offset % page_size;
    (rest of format runs unchanged)

And then the unmapping would be

    munmap(ptr - (offset % page_size), len + (offset % page_size))
<!-- gh-comment-id:58818783 --> @magnumripper commented on GitHub (Oct 12, 2014): > The other issue is that functions in dyna_salt.c have to unmap that, and have NO idea what a rarfile structure is. They know what the dyna_salt struct is (and if you read my message about 5 up, would also know what a dyna_salt_mmap struct is). But they have NO idea how to deal with a raw_data pointer. Yes, I overlooked this. So we really need to add some kind of mmap-specific struct like the dyna_salt_mmap_t you suggested. > Can we simply map from start of file, and simply access data += pos ? So ar[pos] would be the first byte? If we do that, will mmap pull the entire first part of the file in memory, when we hit that first byte? Actually it can be done like this: ``` C ptr = mmap(whole file); ptr += offset; (rest of format runs unchanged) ``` But the unmapping would obviously need to be ``` C munmap(ptr - offset, total_file_len) ``` Alternatively (_EDITED_): ``` C ptr = mmap(len + (offset % page_size), offset & ~(page_size - 1)); ptr += offset % page_size; (rest of format runs unchanged) ``` And then the unmapping would be ``` C munmap(ptr - (offset % page_size), len + (offset % page_size)) ```
Author
Owner

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

If we do that, will mmap pull the entire first part of the file in memory, when we hit that first byte?

No matter how we do it, nothing will be read from disk unless we actually read data from that page. This is all ran by the usual virtual memory machinery!

<!-- gh-comment-id:58818971 --> @magnumripper commented on GitHub (Oct 12, 2014): > If we do that, will mmap pull the entire first part of the file in memory, when we hit that first byte? No matter how we do it, nothing will be read from disk unless we actually read data from that page. This is all ran by the usual virtual memory machinery!
Author
Owner

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

I tried this

#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>

static void dump_text(void *in, int len)
{
    unsigned char *p = (unsigned char*)in;

    while (len--) {
        fputc(isprint(*p) ? *p : '.', stdout);
        p++;
    }
    fputc('\n', stdout);
}

int main(int argc, char **argv)
{
    FILE *file;
    char *p;
    char *name = argv[1];
    int offset = atoi(argv[2]);
    int len = atoi(argv[3]);

    if (!(file = fopen(name, "rb"))) {
        perror(NULL);
        exit(EXIT_FAILURE);
    }
    p = mmap(NULL, len, PROT_READ, MAP_SHARED, fileno(file), offset);
    if (p == MAP_FAILED) {
        fprintf(stderr, "mmap() failed: ");
        perror(NULL);
        exit(EXIT_FAILURE);
    }
    fclose(file);

    dump_text(p, len);

    munmap(p, len);
}

If I give an offset of 0 or eg. 4096, it works fine. If I use an offset that is not a multiple of 4K, the mmap() fails.

<!-- gh-comment-id:58820099 --> @magnumripper commented on GitHub (Oct 12, 2014): I tried this ``` C #include <stdio.h> #include <stdlib.h> #include <sys/mman.h> static void dump_text(void *in, int len) { unsigned char *p = (unsigned char*)in; while (len--) { fputc(isprint(*p) ? *p : '.', stdout); p++; } fputc('\n', stdout); } int main(int argc, char **argv) { FILE *file; char *p; char *name = argv[1]; int offset = atoi(argv[2]); int len = atoi(argv[3]); if (!(file = fopen(name, "rb"))) { perror(NULL); exit(EXIT_FAILURE); } p = mmap(NULL, len, PROT_READ, MAP_SHARED, fileno(file), offset); if (p == MAP_FAILED) { fprintf(stderr, "mmap() failed: "); perror(NULL); exit(EXIT_FAILURE); } fclose(file); dump_text(p, len); munmap(p, len); } ``` If I give an offset of 0 or eg. 4096, it works fine. If I use an offset that is not a multiple of 4K, the mmap() fails.
Author
Owner

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

This version works with any offset:

#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>

static void dump_text(void *in, int len)
{
    unsigned char *p = (unsigned char*)in;

    while (len--) {
        fputc(isprint(*p) ? *p : '.', stdout);
        p++;
    }
    fputc('\n', stdout);
}

int main(int argc, char **argv)
{
    FILE *file;
    char *p;
    int page_size = 4096;
    char *name = argv[1];
    int offset = atoi(argv[2]);
    int len = atoi(argv[3]);

    if (!(file = fopen(name, "rb"))) {
        perror(NULL);
        exit(EXIT_FAILURE);
    }
    fprintf(stderr, "Actual mmap() offset %zu len %zu\n",
            offset & ~(page_size - 1), len + (offset % page_size));
    p = mmap(NULL, len + (offset % page_size), PROT_READ, MAP_SHARED,
             fileno(file), offset & ~(page_size - 1));
    if (p == MAP_FAILED) {
        fprintf(stderr, "mmap() failed: ");
        perror(NULL);
        exit(EXIT_FAILURE);
    }
    fclose(file);

    p += offset % page_size;
    dump_text(p, len);

    p -= offset % page_size;
    if (munmap(p, len + (offset % page_size))) {
        fprintf(stderr, "munmap() failed: ");
        perror(NULL);
        exit(EXIT_FAILURE);
    }
    exit(EXIT_SUCCESS);
}
$ head -2 encoding_data.h 
/*
 * This source file is UTF-8 encoded, for auto-generated warnings

$ gcc test.c -o test && ./test encoding_data.h 6 11
Actual mmap() offset 0 len 17
This source

$ ./test encoding_data.h 4101 13
Actual mmap() offset 4096 len 18
#define CHARS

$ ./test encoding_data.h 36859 10
Actual mmap() offset 32768 len 4101
_high128[]

In the last invocation, we read across a page boundary

<!-- gh-comment-id:58820494 --> @magnumripper commented on GitHub (Oct 12, 2014): This version works with any offset: ``` C #include <stdio.h> #include <stdlib.h> #include <sys/mman.h> static void dump_text(void *in, int len) { unsigned char *p = (unsigned char*)in; while (len--) { fputc(isprint(*p) ? *p : '.', stdout); p++; } fputc('\n', stdout); } int main(int argc, char **argv) { FILE *file; char *p; int page_size = 4096; char *name = argv[1]; int offset = atoi(argv[2]); int len = atoi(argv[3]); if (!(file = fopen(name, "rb"))) { perror(NULL); exit(EXIT_FAILURE); } fprintf(stderr, "Actual mmap() offset %zu len %zu\n", offset & ~(page_size - 1), len + (offset % page_size)); p = mmap(NULL, len + (offset % page_size), PROT_READ, MAP_SHARED, fileno(file), offset & ~(page_size - 1)); if (p == MAP_FAILED) { fprintf(stderr, "mmap() failed: "); perror(NULL); exit(EXIT_FAILURE); } fclose(file); p += offset % page_size; dump_text(p, len); p -= offset % page_size; if (munmap(p, len + (offset % page_size))) { fprintf(stderr, "munmap() failed: "); perror(NULL); exit(EXIT_FAILURE); } exit(EXIT_SUCCESS); } ``` ``` $ head -2 encoding_data.h /* * This source file is UTF-8 encoded, for auto-generated warnings $ gcc test.c -o test && ./test encoding_data.h 6 11 Actual mmap() offset 0 len 17 This source $ ./test encoding_data.h 4101 13 Actual mmap() offset 4096 len 18 #define CHARS $ ./test encoding_data.h 36859 10 Actual mmap() offset 32768 len 4101 _high128[] ``` In the last invocation, we read across a page boundary
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#387
No description provided.