-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Argon2 format: Custom malloc/free using callbacks #5566
Argon2 format: Custom malloc/free using callbacks #5566
Conversation
src/argon2_fmt_plug.c
Outdated
static void deallocate(uint8_t *memory, size_t size) | ||
{ | ||
if (!thread_mem[THREAD_NUMBER].used) | ||
error_msg("%s(): thread %u: Freed memory not in use\n", __FUNCTION__, THREAD_NUMBER); | ||
if (thread_mem[THREAD_NUMBER].size < size) | ||
error_msg("%s(): thread %u: incorrect size %zu, was %zu\n", __FUNCTION__, THREAD_NUMBER, size, thread_mem[THREAD_NUMBER].size); | ||
|
||
thread_mem[THREAD_NUMBER].used = 0; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure why they supply a size to deallocate()
but I used it for an extra check, bc why not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe can even insist on == size
in that check?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it might be smaller after we reused the existing alloc for a smaller request.
Well that crashed and burned. I'll be trying to understand what ASan wants to explain to me. Edit: OK that was a fencepost error for non-OMP. Thread was hard coded as 1 instead of 0. |
70b1abd
to
bd96371
Compare
@solardiz if this PR is correct, I can go ahead and switch to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. I've made a couple of suggestions for a fixup of the existing commit.
Please do also switch to using the "regions" functions, but in a separate commit. Thank you!
src/argon2_fmt_plug.c
Outdated
static void deallocate(uint8_t *memory, size_t size) | ||
{ | ||
if (!thread_mem[THREAD_NUMBER].used) | ||
error_msg("%s(): thread %u: Freed memory not in use\n", __FUNCTION__, THREAD_NUMBER); | ||
if (thread_mem[THREAD_NUMBER].size < size) | ||
error_msg("%s(): thread %u: incorrect size %zu, was %zu\n", __FUNCTION__, THREAD_NUMBER, size, thread_mem[THREAD_NUMBER].size); | ||
|
||
thread_mem[THREAD_NUMBER].used = 0; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe can even insist on == size
in that check?
src/argon2_fmt_plug.c
Outdated
context.allocate_cbk = &allocate; | ||
context.free_cbk = &deallocate; | ||
context.flags = ARGON2_DEFAULT_FLAGS; | ||
context.version = ARGON2_VERSION_NUMBER; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe use struct initialized with named fields here, which is a C feature that we already use in the tree anyway (and for a related purpose). Like this:
argon2_context context = {
.out = (uint8_t*)crypted[i],
and so on
};
omitting the fields where we initialize to NULL or 0. That way, we'll be sure to initialize the entire struct even if more fields are added later.
Our existing usage is e.g.:
yescrypt_params_t params = {.N = cur_salt->N, .r = cur_salt->r, .p = cur_salt->p};
We never free memory except in format's done(), instead we reuse the allocations per thread, and realloc if needed. We also got rid of a small malloc/free per call for a temporary buffer, by moving away from argon2_hash() in favor of argon2_ctx(). Closes openwall#5558
bd96371
to
282e50b
Compare
The switch to regions looks good to me, except that our |
e.g. yescrypt does: if (local->aligned_size < need) {
if (free_region(local))
return -1;
if (!alloc_region(local, need))
return -1;
} without needing to maintain a separate size field. |
and in the Armory format I did: lut_item *lut = memory->aligned;
size_t bytes_reqd = (size_t)n * sizeof(*lut);
if (!lut || memory->aligned_size < bytes_reqd) {
free_region_t(memory);
if (!(lut = alloc_region_t(memory, bytes_reqd)))
return -1;
} |
Ah, yes. The fact we can hand out an oversized existing alloc had me confused but right now they are indeed always the same value. I can throw in a commit resetting MKPC and OMP_SCALE to 1 as well, while at it. |
That's OK with me. |
282e50b
to
6f8192a
Compare
We were ignoring possible error returns from Argon2 code before this PR and made it even worse with this PR. Memory allocation errors go undetected, resulting in "high speeds". I'm going to fix that. |
We never free memory except in format's done(), instead we reuse the allocations per thread, and realloc if needed.
We also got rid of a small malloc/free per call for a temporary buffer, by moving away from argon2_hash() in favor of argon2_ctx().
Closes #5558