-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
Add array memory allocation routines #28059
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
Conversation
8476a94
to
d564826
Compare
7c5db12
to
cb96c58
Compare
The reason you have trouble in the FIPS provider is that you need to reproduce the new allocation calls in providers/fips/fipsprov.c, and also ensure the corresponding upcalls exist |
if (ossl_unlikely(((num | size) >= HALF_SIZE_T) | ||
&& size && ((*bytes / size) != num))) { |
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.
Did you look at using our existing overflow detection code in internal/safe_math.h
for these calculations?
That code will be more efficient when compiler support is available (& it mostly is).
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.
Our coding style suggests size != 0
instead of size
.
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.
Your test of (num | size) >= HALF_SIZE_T
might be a worthwhile improvement to the safe_math.h
unsigned multiplication code since it will avoid a division most of the time.
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.
Did you look at using our existing overflow detection code in
internal/safe_math.h
for these calculations?That code will be more efficient when compiler support is available (& it mostly is).
I haven't, will take a look, thanks!
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.
So, I've looked into it, and it seems that it is unsuitable for use in a header, as the function names are not predetermined, and an invocation of OSSL_SAFE_MATH_SIGNED
creates a bunch of functions that may later collide with the ones defined by its usage in the compilation units.
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.
Why does the check have to be in a header?
Doing so feels like premature optimisation...
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.
Originally it has been envisioned as a small snippet that can be then used in different files, as it really doesn't do much, just a variation of safe_mul, but with setting an error, so the allocation-related issues are easier to trace; otherwise, I don't see a suitable file for storing the implementation, do you have a suggestion?
Alternatively, provide these new calls in a separate file that calls the normal memory allocation routines. Then build that file into the FIPS provider. Another advantage of this is your header could be pulled into the file and the |
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.
I like this change. I did use find ./ -name "*.c" -o -name "*.h" -o -name "*.h.in" |xargs grep OPENSSL_.*alloc |egrep -e '\* +'
to find more places which could be improved in existing code base. those are further suggestions which have popped up:
diff --git a/apps/x509.c b/apps/x509.c
index 6051d8642f..c9d26f8b20 100644
--- a/apps/x509.c
+++ b/apps/x509.c
@@ -1303,7 +1303,7 @@ static int print_x509v3_exts(BIO *bio, X509 *x, const char *ext_names)
BIO_printf(bio, "Invalid extension names: %s\n", ext_names);
goto end;
}
- if ((names = OPENSSL_malloc(sizeof(char *) * nn)) == NULL)
+ if ((names = OPENSSL_malloc_array(nn, sizeof(char *))) == NULL)
goto end;
parse_ext_names(tmp_ext_names, names);
diff --git a/fuzz/hashtable.c b/fuzz/hashtable.c
index 38d2295076..6fc033b3e6 100644
--- a/fuzz/hashtable.c
+++ b/fuzz/hashtable.c
@@ -103,7 +103,7 @@ int FuzzerInitialize(int *argc, char ***argv)
OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
ERR_clear_error();
- prediction_table = OPENSSL_zalloc(sizeof(FUZZER_VALUE) * 65537);
+ prediction_table = OPENSSL_calloc(65537, sizeof(FUZZER_VALUE));
if (prediction_table == NULL)
return -1;
fuzzer_table = ossl_ht_new(&fuzz_conf);
diff --git a/providers/implementations/encode_decode/ml_common_codecs.c b/providers/implementations/encode_decode/ml_common_codecs.c
index 773550c9fb..97d37ecd10 100644
--- a/providers/implementations/encode_decode/ml_common_codecs.c
+++ b/providers/implementations/encode_decode/ml_common_codecs.c
@@ -42,7 +42,7 @@ ossl_ml_common_pkcs8_fmt_order(const char *algorithm_name,
const char *sep = "\t ,";
/* Reserve an extra terminal slot with fmt == NULL */
- if ((ret = OPENSSL_zalloc((NUM_PKCS8_FORMATS + 1) * sizeof(*ret))) == NULL)
+ if ((ret = OPENSSL_calloc((NUM_PKCS8_FORMATS + 1), sizeof(*ret))) == NULL)
return NULL;
/* Entries that match a format will get a non-zero preference. */
diff --git a/providers/implementations/kdfs/argon2.c b/providers/implementations/kdfs/argon2.c
index e3f9aa4f0f..00305faaac 100644
--- a/providers/implementations/kdfs/argon2.c
+++ b/providers/implementations/kdfs/argon2.c
@@ -564,8 +564,8 @@ static int fill_mem_blocks_mt(KDF_ARGON2 *ctx)
void **t;
ARGON2_THREAD_DATA *t_data;
- t = OPENSSL_zalloc(sizeof(void *)*ctx->lanes);
- t_data = OPENSSL_zalloc(ctx->lanes * sizeof(ARGON2_THREAD_DATA));
+ t = OPENSSL_calloc(ctx->lanes, sizeof(void *));
+ t_data = OPENSSL_calloc(ctx->lanes, sizeof(ARGON2_THREAD_DATA));
if (t == NULL || t_data == NULL)
goto fail;
and of course all suggestions here can be addressed as follow up PRs. It's up to you to address it now, or later in follow up PRs. just let me know how you'll proceed and I will approve.
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 like you need to run make update and commit the changes to util/libcrypto.num so that the new alloc call gets exported properly.
Just FYI, since we discussed this on standup today, and there has been some concern over fips/provider compatibility here, I'm going to close and reopen this with the extended_tests label set, so that the provider compat tests get run. |
Seems like the case of realloc() returning NULL with non-zero size has been overlooked. Complements: 5639ee7 "ERR: Make CRYPTO_malloc() and friends report ERR_R_MALLOC_FAILURE" Signed-off-by: Eugene Syromiatnikov <esyr@openssl.org> Reviewed-by: Saša Nedvědický <sashan@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> Reviewed-by: Paul Dale <ppzgs1@gmail.com> Reviewed-by: Neil Horman <nhorman@openssl.org> (Merged from #28059)
Analogous to the way CRYPTO_malloc does it. Signed-off-by: Eugene Syromiatnikov <esyr@openssl.org> Reviewed-by: Saša Nedvědický <sashan@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> Reviewed-by: Paul Dale <ppzgs1@gmail.com> Reviewed-by: Neil Horman <nhorman@openssl.org> (Merged from #28059)
Signed-off-by: Eugene Syromiatnikov <esyr@openssl.org> Reviewed-by: Saša Nedvědický <sashan@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> Reviewed-by: Paul Dale <ppzgs1@gmail.com> Reviewed-by: Neil Horman <nhorman@openssl.org> (Merged from #28059)
Complements: b51bce9 "Add and use OPENSSL_zalloc" Signed-off-by: Eugene Syromiatnikov <esyr@openssl.org> Reviewed-by: Saša Nedvědický <sashan@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> Reviewed-by: Paul Dale <ppzgs1@gmail.com> Reviewed-by: Neil Horman <nhorman@openssl.org> (Merged from #28059)
Otherwise util/check-format-commit.sh complains about the wrong alignment. Signed-off-by: Eugene Syromiatnikov <esyr@openssl.org> Reviewed-by: Saša Nedvědický <sashan@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> Reviewed-by: Paul Dale <ppzgs1@gmail.com> Reviewed-by: Neil Horman <nhorman@openssl.org> (Merged from #28059)
Such routines allow alleviating the need to perform explicit integer overflow check during allocation size calculation and generally make the allocations more semantic (as they signify that a collection of NUM items, each occupying SIZE bytes is being allocated), which paves the road for additional correctness checks in the future. Signed-off-by: Eugene Syromiatnikov <esyr@openssl.org> Reviewed-by: Saša Nedvědický <sashan@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> Reviewed-by: Paul Dale <ppzgs1@gmail.com> Reviewed-by: Neil Horman <nhorman@openssl.org> (Merged from #28059)
Signed-off-by: Eugene Syromiatnikov <esyr@openssl.org> Reviewed-by: Saša Nedvědický <sashan@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> Reviewed-by: Paul Dale <ppzgs1@gmail.com> Reviewed-by: Neil Horman <nhorman@openssl.org> (Merged from #28059)
Signed-off-by: Eugene Syromiatnikov <esyr@openssl.org> Reviewed-by: Saša Nedvědický <sashan@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> Reviewed-by: Paul Dale <ppzgs1@gmail.com> Reviewed-by: Neil Horman <nhorman@openssl.org> (Merged from #28059)
Signed-off-by: Eugene Syromiatnikov <esyr@openssl.org> Reviewed-by: Saša Nedvědický <sashan@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> Reviewed-by: Paul Dale <ppzgs1@gmail.com> Reviewed-by: Neil Horman <nhorman@openssl.org> (Merged from #28059)
Co-Authored-by: Alexandr Nedvedicky <sashan@openssl.org> Signed-off-by: Eugene Syromiatnikov <esyr@openssl.org> Reviewed-by: Saša Nedvědický <sashan@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> Reviewed-by: Paul Dale <ppzgs1@gmail.com> Reviewed-by: Neil Horman <nhorman@openssl.org> (Merged from #28059)
Signed-off-by: Eugene Syromiatnikov <esyr@openssl.org> Reviewed-by: Saša Nedvědický <sashan@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> Reviewed-by: Paul Dale <ppzgs1@gmail.com> Reviewed-by: Neil Horman <nhorman@openssl.org> (Merged from #28059)
Signed-off-by: Eugene Syromiatnikov <esyr@openssl.org> Reviewed-by: Saša Nedvědický <sashan@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> Reviewed-by: Paul Dale <ppzgs1@gmail.com> Reviewed-by: Neil Horman <nhorman@openssl.org> (Merged from #28059)
Signed-off-by: Eugene Syromiatnikov <esyr@openssl.org> Reviewed-by: Saša Nedvědický <sashan@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> Reviewed-by: Paul Dale <ppzgs1@gmail.com> Reviewed-by: Neil Horman <nhorman@openssl.org> (Merged from #28059)
Co-Authored-by: Alexandr Nedvedicky <sashan@openssl.org> Signed-off-by: Eugene Syromiatnikov <esyr@openssl.org> Reviewed-by: Saša Nedvědický <sashan@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> Reviewed-by: Paul Dale <ppzgs1@gmail.com> Reviewed-by: Neil Horman <nhorman@openssl.org> (Merged from #28059)
Signed-off-by: Eugene Syromiatnikov <esyr@openssl.org> Reviewed-by: Saša Nedvědický <sashan@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> Reviewed-by: Paul Dale <ppzgs1@gmail.com> Reviewed-by: Neil Horman <nhorman@openssl.org> (Merged from #28059)
There are some *.inc already in the repository, mostly in demos/tests and related to some algorithm implementations. Introduction of array_alloc.inc has made including these files in the tags generation even more pertinent, so they are included now. Also, this commit explicitly marks *.h files as containing C code, overriding universal-ctags default of interpreting them as C++/ObjectiveC ones. Suggested-by: Neil Horman <nhorman@openssl.org> Signed-off-by: Eugene Syromiatnikov <esyr@openssl.org> Reviewed-by: Saša Nedvědický <sashan@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> Reviewed-by: Paul Dale <ppzgs1@gmail.com> Reviewed-by: Neil Horman <nhorman@openssl.org> (Merged from #28059)
* crypto/mem.c [OPENSSL_SMALL_FOOTPRINT] (CRYPTO_aligned_alloc): Change freeptr to *freeptr to properly update the variable passed by pointer. Signed-off-by: Eugene Syromiatnikov <esyr@openssl.org> Reviewed-by: Saša Nedvědický <sashan@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> Reviewed-by: Paul Dale <ppzgs1@gmail.com> Reviewed-by: Neil Horman <nhorman@openssl.org> (Merged from #28059)
…gned_alloc There is no need to initialise neither *freeptr, as it is initialised already, nor ret, as NULL can be simply returned instead. Signed-off-by: Eugene Syromiatnikov <esyr@openssl.org> Reviewed-by: Saša Nedvědický <sashan@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> Reviewed-by: Paul Dale <ppzgs1@gmail.com> Reviewed-by: Neil Horman <nhorman@openssl.org> (Merged from #28059)
The original C11 specification is pretty weird: it specifies that the size must be a multiple of alignment (rendering it useless for small page-aligned allocations that, for example, might be useful for RDMA) and until DR460[1] it was UB in failing to do so (as it is with OPENSSL_ligned_alloc() calls in alloc_new_neighborhood_list() on 32-bit systems, for example). Moreover, it has arguably not been used much before, as all supported POSIX systems have at least POSIX 2001 compatibility level nowadays, Windows doesn't implement aligned_alloc() at all (because implementation of free() in MS CRT is unable to handle aligned allocations[2]), and _ISOC11_SOURCE is a glibc-specific feature test macro. [1] https://open-std.org/JTC1/SC22/WG14/www/docs/summary.htm#dr_460 [2] https://learn.microsoft.com/en-us/cpp/standard-library/cstdlib?view=msvc-170#remarks-6 Signed-off-by: Eugene Syromiatnikov <esyr@openssl.org> Reviewed-by: Saša Nedvědický <sashan@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> Reviewed-by: Paul Dale <ppzgs1@gmail.com> Reviewed-by: Neil Horman <nhorman@openssl.org> (Merged from #28059)
…igned_alloc Otherwise the roundup calculation performed in the open-coded implementation may put the pointer out of bounds. Signed-off-by: Eugene Syromiatnikov <esyr@openssl.org> Reviewed-by: Saša Nedvědický <sashan@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> Reviewed-by: Paul Dale <ppzgs1@gmail.com> Reviewed-by: Neil Horman <nhorman@openssl.org> (Merged from #28059)
Report the errors for the known error codes returned by posix_memalign(). Signed-off-by: Eugene Syromiatnikov <esyr@openssl.org> Reviewed-by: Saša Nedvědický <sashan@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> Reviewed-by: Paul Dale <ppzgs1@gmail.com> Reviewed-by: Neil Horman <nhorman@openssl.org> (Merged from #28059)
…is used Per [1]: The value of alignment shall be a power of two multiple of sizeof(void *). [1] https://pubs.opengroup.org/onlinepubs/9799919799/functions/posix_memalign.html Signed-off-by: Eugene Syromiatnikov <esyr@openssl.org> Reviewed-by: Saša Nedvědický <sashan@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> Reviewed-by: Paul Dale <ppzgs1@gmail.com> Reviewed-by: Neil Horman <nhorman@openssl.org> (Merged from #28059)
…d_alloc The open-coded implementation performs addition of size and alignment, that may overflow. Signed-off-by: Eugene Syromiatnikov <esyr@openssl.org> Reviewed-by: Saša Nedvědický <sashan@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> Reviewed-by: Paul Dale <ppzgs1@gmail.com> Reviewed-by: Neil Horman <nhorman@openssl.org> (Merged from #28059)
Signed-off-by: Eugene Syromiatnikov <esyr@openssl.org> Reviewed-by: Saša Nedvědický <sashan@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> Reviewed-by: Paul Dale <ppzgs1@gmail.com> Reviewed-by: Neil Horman <nhorman@openssl.org> (Merged from #28059)
OPENSSL_aligned_alloc can return NULL in cases other than memory exhaustion or incorrect arguments, document that. Signed-off-by: Eugene Syromiatnikov <esyr@openssl.org> Reviewed-by: Saša Nedvědický <sashan@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> Reviewed-by: Paul Dale <ppzgs1@gmail.com> Reviewed-by: Neil Horman <nhorman@openssl.org> (Merged from #28059)
…lures Explicitly document that it is set to NULL, so can be passed to free() without additional checks. Signed-off-by: Eugene Syromiatnikov <esyr@openssl.org> Reviewed-by: Saša Nedvědický <sashan@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> Reviewed-by: Paul Dale <ppzgs1@gmail.com> Reviewed-by: Neil Horman <nhorman@openssl.org> (Merged from #28059)
…ng returned The semantics of OPENSSL_secure_[mz]alloc is somewhat unorthodox, as it silently return a pointer to non-secure memory if the arena is not initialised, which, while mentioned in the DESCRIPTION, is not clear from reading the pertaining part of the RETURNING VALUE section alone; explicitly state that the memory may be allocated by OPENSSL_calloc instead if the secure heap is not initialised. Signed-off-by: Eugene Syromiatnikov <esyr@openssl.org> Reviewed-by: Saša Nedvědický <sashan@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> Reviewed-by: Paul Dale <ppzgs1@gmail.com> Reviewed-by: Neil Horman <nhorman@openssl.org> (Merged from #28059)
So it is possible to change the allocator implementation, as it must be before the first malloc call. Suggested-by: Matt Caswell <matt@openssl.org> Signed-off-by: Eugene Syromiatnikov <esyr@openssl.org> Reviewed-by: Saša Nedvědický <sashan@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> Reviewed-by: Paul Dale <ppzgs1@gmail.com> Reviewed-by: Neil Horman <nhorman@openssl.org> (Merged from #28059)
Signed-off-by: Eugene Syromiatnikov <esyr@openssl.org> Reviewed-by: Saša Nedvědický <sashan@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> Reviewed-by: Paul Dale <ppzgs1@gmail.com> Reviewed-by: Neil Horman <nhorman@openssl.org> (Merged from #28059)
…rray [1] has introduced array allocation routines, and, since those are available, it is prudent to use where they are appropriate, like in this example. [1] openssl/openssl#28059 Signed-off-by: Eugene Syromiatnikov <esyr@openssl.org>
This patch set adds variants of memory allocation routines suitable for array allocation, that perform an additional check for integer overflow during the size calculation. Also, in order to add a test for the functions, some cleanup of the existing memory allocation routines' behaviour has been done.
Checklist