diff --git a/.gitmodules b/.gitmodules index b4c756a67b179..e69de29bb2d1d 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,37 +0,0 @@ -[submodule "pyca.cryptography"] - path = pyca-cryptography - url = https://github.com/pyca/cryptography.git - -[submodule "krb5"] - path = krb5 - url = https://github.com/krb5/krb5 - -[submodule "gost-engine"] - path = gost-engine - url = https://github.com/gost-engine/engine - update = rebase -[submodule "wycheproof"] - path = wycheproof - url = https://github.com/google/wycheproof -[submodule "tlsfuzzer"] - path = tlsfuzzer - url = https://github.com/tlsfuzzer/tlsfuzzer -[submodule "python-ecdsa"] - path = python-ecdsa - url = https://github.com/tlsfuzzer/python-ecdsa -[submodule "tlslite-ng"] - path = tlslite-ng - url = https://github.com/tlsfuzzer/tlslite-ng -[submodule "oqs-provider"] - path = oqs-provider - url = https://github.com/open-quantum-safe/oqs-provider.git -[submodule "cloudflare-quiche"] - path = cloudflare-quiche - url = https://github.com/cloudflare/quiche -[submodule "fuzz/corpora"] - path = fuzz/corpora - url = https://github.com/openssl/fuzz-corpora - branch = main -[submodule "pkcs11-provider"] - path = pkcs11-provider - url = https://github.com/latchset/pkcs11-provider.git diff --git a/cloudflare-quiche b/cloudflare-quiche deleted file mode 160000 index 7ab6a55cfe471..0000000000000 --- a/cloudflare-quiche +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 7ab6a55cfe471267d61e4d28ba43d41defcd87e0 diff --git a/crypto/armcap.c b/crypto/armcap.c index 3707cfafe7a10..fc1b85a23fc3e 100644 --- a/crypto/armcap.c +++ b/crypto/armcap.c @@ -77,6 +77,10 @@ void OPENSSL_cpuid_setup(void) __attribute__((constructor)); #include #define OSSL_IMPLEMENT_GETAUXVAL #endif +#elif defined(__MUSL__) +/* musl has always provided getauxval(), unversioned. */ +#include +#define OSSL_IMPLEMENT_GETAUXVAL #endif #if defined(__FreeBSD__) || defined(__OpenBSD__) #include diff --git a/crypto/bio/bss_dgram.c b/crypto/bio/bss_dgram.c index 6511219427f78..f526663cd52d6 100644 --- a/crypto/bio/bss_dgram.c +++ b/crypto/bio/bss_dgram.c @@ -49,7 +49,9 @@ #define M_METHOD_WSARECVMSG 4 #if defined(__GLIBC__) && defined(__GLIBC_PREREQ) -#if !(__GLIBC_PREREQ(2, 14)) +/// ClickHouse-specific patch: Pretend to use a stone age glibc because we use a stone age glibc. +/// Otherwise, system calls sendmmsg and recvmmsg are used which work only with too-new glibc 2.14. +/// #if !(__GLIBC_PREREQ(2, 14)) #undef NO_RECVMMSG /* * Some old glibc versions may have recvmmsg and MSG_WAITFORONE flag, but @@ -57,7 +59,7 @@ * versions */ #define NO_RECVMMSG -#endif +/// #endif #endif #if defined(__GNU__) /* GNU/Hurd does not have IP_PKTINFO yet */ diff --git a/crypto/bn/bn_intern.c b/crypto/bn/bn_intern.c index 547738761cf2b..c4a3d84fc0a71 100644 --- a/crypto/bn/bn_intern.c +++ b/crypto/bn/bn_intern.c @@ -11,6 +11,11 @@ #include "bn_local.h" #include "internal/constant_time.h" +#if defined(__has_feature) +# if __has_feature(memory_sanitizer) +# include +# endif +#endif /* * Determine the modified width-(w+1) Non-Adjacent Form (wNAF) of 'scalar'. * This is an array r[] of values that are either zero or odd with an @@ -226,6 +231,11 @@ int bn_set_words(BIGNUM *a, const BN_ULONG *words, int num_words) return 0; } +#if defined(__has_feature) +# if __has_feature(memory_sanitizer) + __msan_unpoison(words, sizeof(BN_ULONG) * num_words); +# endif +#endif memcpy(a->d, words, sizeof(BN_ULONG) * num_words); a->top = num_words; bn_correct_top(a); diff --git a/crypto/bsearch.c b/crypto/bsearch.c index 790e37915cbcd..a5d76c839c692 100644 --- a/crypto/bsearch.c +++ b/crypto/bsearch.c @@ -11,8 +11,10 @@ #include #include "internal/cryptlib.h" +typedef int (*cmpthunk_fn)(const void *, const void *); const void *ossl_bsearch(const void *key, const void *base, int num, int size, int (*cmp)(const void *, const void *), + int (*cmp_thunk)(cmpthunk_fn real_cmp_fn, const void *, const void *), int flags) { const char *base_ = base; @@ -26,7 +28,10 @@ const void *ossl_bsearch(const void *key, const void *base, int num, while (l < h) { i = l + (h - l) / 2; p = &(base_[i * size]); - c = (*cmp)(key, p); + if (cmp_thunk != NULL) + c = cmp_thunk((cmpthunk_fn)cmp, key, (const void *)p); + else + c = cmp(key, p); if (c < 0) h = i; else if (c > 0) @@ -37,8 +42,16 @@ const void *ossl_bsearch(const void *key, const void *base, int num, if (c != 0 && !(flags & OSSL_BSEARCH_VALUE_ON_NOMATCH)) p = NULL; else if (c == 0 && (flags & OSSL_BSEARCH_FIRST_VALUE_ON_MATCH)) { - while (i > 0 && (*cmp)(key, &(base_[(i - 1) * size])) == 0) + while (i > 0) { + if (cmp_thunk != NULL) { + if (cmp_thunk((cmpthunk_fn)cmp, key, (const void *)&(base_[(i - 1) * size]))) + break; + } else { + if (cmp(key, &(base_[(i - 1) * size]))) + break; + } i--; + } p = &(base_[i * size]); } return p; diff --git a/crypto/cpuid.c b/crypto/cpuid.c index d659135919d1f..18c648148a30c 100644 --- a/crypto/cpuid.c +++ b/crypto/cpuid.c @@ -102,7 +102,16 @@ void OPENSSL_cpuid_setup(void) if (trigger) return; + /// This function is called from .init section before memory sanitizer mmaps shadow memory. + /// Program will crash with segmentation fault when trying access `trigger`, + /// because its address was replaced with some not mapped address. + /// Also see https://github.com/ClickHouse/openssl/pull/5 + /// Unfortunately, __msan_init() is no longer part of msan's public header and there seems to be no replacement. +#if defined(__has_feature) +# if !__has_feature(memory_sanitizer) trigger = 1; +# endif +#endif if ((env = ossl_getenv("OPENSSL_ia32cap")) != NULL) { int off = (env[0] == '~') ? 1 : 0; diff --git a/crypto/ec/curve25519.c b/crypto/ec/curve25519.c index c6886763aba7e..5661d33485832 100644 --- a/crypto/ec/curve25519.c +++ b/crypto/ec/curve25519.c @@ -21,6 +21,12 @@ #include "internal/numbers.h" +#if defined(__has_feature) +# if __has_feature(memory_sanitizer) +# include +# endif +#endif + #if defined(X25519_ASM) && (defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || defined(_M_X64)) #define BASE_2_64_IMPLEMENTED @@ -5846,6 +5852,12 @@ int ossl_x25519(uint8_t out_shared_key[32], const uint8_t private_key[32], { static const uint8_t kZeros[32] = { 0 }; x25519_scalar_mult(out_shared_key, private_key, peer_public_value); +#if defined(__has_feature) +# if __has_feature(memory_sanitizer) + /* x25519_scalar_mult may use assembly that MSan cannot instrument. */ + __msan_unpoison(out_shared_key, 32); +# endif +#endif /* The all-zero output results when the input is a point of small order. */ return CRYPTO_memcmp(kZeros, out_shared_key, 32) != 0; } @@ -5875,5 +5887,11 @@ void ossl_x25519_public_from_private(uint8_t out_public_value[32], fe_mul(zplusy, zplusy, zminusy_inv); fe_tobytes(out_public_value, zplusy); +#if defined(__has_feature) +# if __has_feature(memory_sanitizer) + __msan_unpoison(out_public_value, 32); +# endif +#endif + OPENSSL_cleanse(e, sizeof(e)); } diff --git a/crypto/engine/eng_lib.c b/crypto/engine/eng_lib.c index 04a2602ed2d8c..bc643a1b4e67f 100644 --- a/crypto/engine/eng_lib.c +++ b/crypto/engine/eng_lib.c @@ -12,6 +12,12 @@ #include #include "internal/refcount.h" +#if defined(__has_feature) +# if __has_feature(address_sanitizer) +#include +# endif +#endif + CRYPTO_RWLOCK *global_engine_lock; CRYPTO_ONCE engine_lock_init = CRYPTO_ONCE_STATIC_INIT; @@ -33,8 +39,18 @@ ENGINE *ENGINE_new(void) ERR_raise(ERR_LIB_ENGINE, ERR_R_CRYPTO_LIB); return 0; } +#if defined(__has_feature) +# if __has_feature(address_sanitizer) + __lsan_disable(); +# endif +#endif if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) return NULL; +#if defined(__has_feature) +# if __has_feature(address_sanitizer) + __lsan_enable(); +# endif +#endif if (!CRYPTO_NEW_REF(&ret->struct_ref, 1)) { OPENSSL_free(ret); return NULL; diff --git a/crypto/err/err.c b/crypto/err/err.c index 94c542cd3f504..8f06fc6e87e5f 100644 --- a/crypto/err/err.c +++ b/crypto/err/err.c @@ -26,6 +26,12 @@ #include "internal/e_os.h" #include "err_local.h" +#if defined(__has_feature) +# if __has_feature(address_sanitizer) +#include +# endif +#endif + /* Forward declaration in case it's not published because of configuration */ ERR_STATE *ERR_get_state(void); @@ -693,7 +699,17 @@ ERR_STATE *ossl_err_get_state_int(void) if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE *)-1)) return NULL; +#if defined(__has_feature) +# if __has_feature(address_sanitizer) + __lsan_disable(); +# endif +#endif state = OSSL_ERR_STATE_new(); +#if defined(__has_feature) +# if __has_feature(address_sanitizer) + __lsan_enable(); +# endif +#endif if (state == NULL) { CRYPTO_THREAD_set_local(&err_thread_local, NULL); return NULL; diff --git a/crypto/objects/obj_dat.c b/crypto/objects/obj_dat.c index 9a823d3cd9216..b82d160ff570f 100644 --- a/crypto/objects/obj_dat.c +++ b/crypto/objects/obj_dat.c @@ -695,7 +695,7 @@ const void *OBJ_bsearch_ex_(const void *key, const void *base, int num, int (*cmp)(const void *, const void *), int flags) { - const char *p = ossl_bsearch(key, base, num, size, cmp, flags); + const char *p = ossl_bsearch(key, base, num, size, cmp, NULL, flags); #ifdef CHARSET_EBCDIC /* diff --git a/crypto/poly1305/poly1305.c b/crypto/poly1305/poly1305.c index 8836dbf79ae41..3e81cc21db2cb 100644 --- a/crypto/poly1305/poly1305.c +++ b/crypto/poly1305/poly1305.c @@ -433,9 +433,20 @@ void Poly1305_Init(POLY1305 *ctx, const unsigned char key[32]) */ #define poly1305_blocks (*poly1305_blocks_p) #define poly1305_emit (*poly1305_emit_p) +/* + * The assembly `poly1305_init` stores raw addresses of assembly routines in + * `ctx->func`: they have no Clang CFI type, so these calls cannot be checked. + */ +#if defined(__clang__) +#define POLY1305_NO_CFI_ICALL __attribute__((no_sanitize("cfi-icall"))) +#else +#define POLY1305_NO_CFI_ICALL +#endif +#else +#define POLY1305_NO_CFI_ICALL #endif -void Poly1305_Update(POLY1305 *ctx, const unsigned char *inp, size_t len) +POLY1305_NO_CFI_ICALL void Poly1305_Update(POLY1305 *ctx, const unsigned char *inp, size_t len) { #ifdef POLY1305_ASM /* @@ -477,7 +488,7 @@ void Poly1305_Update(POLY1305 *ctx, const unsigned char *inp, size_t len) ctx->num = rem; } -void Poly1305_Final(POLY1305 *ctx, unsigned char mac[16]) +POLY1305_NO_CFI_ICALL void Poly1305_Final(POLY1305 *ctx, unsigned char mac[16]) { #ifdef POLY1305_ASM poly1305_blocks_f poly1305_blocks_p = ctx->func.blocks; diff --git a/crypto/property/property_query.c b/crypto/property/property_query.c index f5daca2aad17c..e4935b66d29ad 100644 --- a/crypto/property/property_query.c +++ b/crypto/property/property_query.c @@ -30,7 +30,7 @@ ossl_property_find_property(const OSSL_PROPERTY_LIST *list, return NULL; return ossl_bsearch(&name_idx, list->properties, list->num_properties, - sizeof(*list->properties), &property_idx_cmp, 0); + sizeof(*list->properties), &property_idx_cmp, NULL, 0); } OSSL_PROPERTY_TYPE ossl_property_get_type(const OSSL_PROPERTY_DEFINITION *prop) diff --git a/crypto/rand/rand_lib.c b/crypto/rand/rand_lib.c index df70d1c2b1dad..893cadd22bb79 100644 --- a/crypto/rand/rand_lib.c +++ b/crypto/rand/rand_lib.c @@ -103,6 +103,13 @@ static RAND_GLOBAL *rand_get_global(OSSL_LIB_CTX *libctx) return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DRBG_INDEX); } + +#if defined(__has_feature) +# if __has_feature(address_sanitizer) +#include +# endif +#endif + #ifndef FIPS_MODULE #include #include @@ -857,8 +864,18 @@ static EVP_RAND_CTX *rand_get0_public(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl) if (CRYPTO_THREAD_get_local(&dgbl->private) == NULL && !ossl_init_thread_start(NULL, ctx, rand_delete_thread_state)) return NULL; +#if defined(__has_feature) +# if __has_feature(address_sanitizer) + __lsan_disable(); +# endif +#endif rand = rand_new_drbg(ctx, primary, SECONDARY_RESEED_INTERVAL, SECONDARY_RESEED_TIME_INTERVAL); +#if defined(__has_feature) +# if __has_feature(address_sanitizer) + __lsan_enable(); +# endif +#endif if (!CRYPTO_THREAD_set_local(&dgbl->public, rand)) { EVP_RAND_CTX_free(rand); rand = NULL; @@ -899,8 +916,18 @@ static EVP_RAND_CTX *rand_get0_private(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl) if (CRYPTO_THREAD_get_local(&dgbl->public) == NULL && !ossl_init_thread_start(NULL, ctx, rand_delete_thread_state)) return NULL; +#if defined(__has_feature) +# if __has_feature(address_sanitizer) + __lsan_disable(); +# endif +#endif rand = rand_new_drbg(ctx, primary, SECONDARY_RESEED_INTERVAL, SECONDARY_RESEED_TIME_INTERVAL); +#if defined(__has_feature) +# if __has_feature(address_sanitizer) + __lsan_enable(); +# endif +#endif if (!CRYPTO_THREAD_set_local(&dgbl->private, rand)) { EVP_RAND_CTX_free(rand); rand = NULL; diff --git a/crypto/sha/sha3.c b/crypto/sha/sha3.c index 21e1070beed27..1cdc8e983e8e8 100644 --- a/crypto/sha/sha3.c +++ b/crypto/sha/sha3.c @@ -13,6 +13,12 @@ #endif #include "internal/sha3.h" +#if defined(__has_feature) +# if __has_feature(memory_sanitizer) +# include +# endif +#endif + void SHA3_squeeze(uint64_t A[5][5], unsigned char *out, size_t len, size_t r, int next); void ossl_sha3_reset(KECCAK1600_CTX *ctx) @@ -122,8 +128,21 @@ int ossl_sha3_final(KECCAK1600_CTX *ctx, unsigned char *out, size_t outlen) (void)SHA3_absorb(ctx->A, ctx->buf, bsz, bsz); +#if defined(__has_feature) +# if __has_feature(memory_sanitizer) + __msan_unpoison(ctx->buf, bsz); +# endif +#endif + ctx->xof_state = XOF_STATE_FINAL; SHA3_squeeze(ctx->A, out, outlen, bsz, 0); + +#if defined(__has_feature) +# if __has_feature(memory_sanitizer) + __msan_unpoison(out, outlen); +# endif +#endif + return 1; } @@ -191,6 +210,13 @@ int ossl_sha3_squeeze(KECCAK1600_CTX *ctx, unsigned char *out, size_t outlen) if (outlen >= bsz) { len = bsz * (outlen / bsz); SHA3_squeeze(ctx->A, out, len, bsz, next); + +#if defined(__has_feature) +# if __has_feature(memory_sanitizer) + __msan_unpoison(out, len); +# endif +#endif + next = 1; out += len; outlen -= len; @@ -198,6 +224,13 @@ int ossl_sha3_squeeze(KECCAK1600_CTX *ctx, unsigned char *out, size_t outlen) if (outlen > 0) { /* Step 3. Squeeze one more block into a buffer */ SHA3_squeeze(ctx->A, ctx->buf, bsz, bsz, next); + +#if defined(__has_feature) +# if __has_feature(memory_sanitizer) + __msan_unpoison(ctx->buf, bsz); +# endif +#endif + memcpy(out, ctx->buf, outlen); /* Step 4. Remember the leftover part of the squeezed block */ ctx->bufsz = bsz - outlen; diff --git a/crypto/stack/stack.c b/crypto/stack/stack.c index 633e9f1bf2444..d8a85fa0a71e7 100644 --- a/crypto/stack/stack.c +++ b/crypto/stack/stack.c @@ -10,6 +10,7 @@ #include #include "internal/cryptlib.h" #include "internal/numbers.h" +#include "internal/nelem.h" #include "internal/safe_math.h" #include #include @@ -31,6 +32,9 @@ struct stack_st { int sorted; int num_alloc; OPENSSL_sk_compfunc comp; + int (*cmp_thunk)(OPENSSL_sk_compfunc, const void *, const void *); + OPENSSL_sk_freefunc_thunk free_thunk; + OPENSSL_sk_copyfunc_thunk copy_thunk; }; OPENSSL_sk_compfunc OPENSSL_sk_set_cmp_func(OPENSSL_STACK *sk, @@ -45,11 +49,21 @@ OPENSSL_sk_compfunc OPENSSL_sk_set_cmp_func(OPENSSL_STACK *sk, return old; } +static void free_with_thunk(OPENSSL_STACK *sk, OPENSSL_sk_freefunc free_func, const void *data) +{ + if (data == NULL) + return; + if (sk->free_thunk != NULL) + sk->free_thunk(free_func, (void *)data); + else + free_func((void *)data); +} + OPENSSL_STACK *OPENSSL_sk_dup(const OPENSSL_STACK *sk) { OPENSSL_STACK *ret; - if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL) + if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) goto err; if (sk == NULL) { @@ -87,7 +101,7 @@ OPENSSL_STACK *OPENSSL_sk_deep_copy(const OPENSSL_STACK *sk, OPENSSL_STACK *ret; int i; - if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL) + if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) goto err; if (sk == NULL) { @@ -114,10 +128,14 @@ OPENSSL_STACK *OPENSSL_sk_deep_copy(const OPENSSL_STACK *sk, for (i = 0; i < ret->num; ++i) { if (sk->data[i] == NULL) continue; - if ((ret->data[i] = copy_func(sk->data[i])) == NULL) { + if (ret->copy_thunk != NULL) + ret->data[i] = ret->copy_thunk(copy_func, sk->data[i]); + else + ret->data[i] = copy_func(sk->data[i]); + + if (ret->data[i] == NULL) { while (--i >= 0) - if (ret->data[i] != NULL) - free_func((void *)ret->data[i]); + free_with_thunk(ret, free_func, ret->data[i]); goto err; } } @@ -224,6 +242,11 @@ static int sk_reserve(OPENSSL_STACK *st, int n, int exact) return 1; } +static ossl_inline int cmp_with_thunk(const OPENSSL_STACK *st, const void *a, const void *b) +{ + return (st->cmp_thunk == NULL) ? st->comp(a, b) : st->cmp_thunk(st->comp, a, b); +} + OPENSSL_STACK *OPENSSL_sk_new_reserve(OPENSSL_sk_compfunc c, int n) { OPENSSL_STACK *st = OPENSSL_zalloc(sizeof(OPENSSL_STACK)); @@ -256,6 +279,30 @@ int OPENSSL_sk_reserve(OPENSSL_STACK *st, int n) return sk_reserve(st, n, 1); } +OPENSSL_STACK *OPENSSL_sk_set_thunks(OPENSSL_STACK *st, OPENSSL_sk_freefunc_thunk f_thunk) +{ + if (st != NULL) + st->free_thunk = f_thunk; + + return st; +} + +OPENSSL_STACK *OPENSSL_sk_set_cmp_thunks(OPENSSL_STACK *st, int (*c_thunk)(int (*)(const void *, const void *), const void *, const void *)) +{ + if (st != NULL) + st->cmp_thunk = c_thunk; + + return st; +} + +OPENSSL_STACK *OPENSSL_sk_set_copy_thunks(OPENSSL_STACK *st, OPENSSL_sk_copyfunc_thunk cp_thunk) +{ + if (st != NULL) + st->copy_thunk = cp_thunk; + + return st; +} + int OPENSSL_sk_insert(OPENSSL_STACK *st, const void *data, int loc) { if (st == NULL) { @@ -320,6 +367,7 @@ static int internal_find(OPENSSL_STACK *st, const void *data, { const void *r; int i, count = 0; + int cmp_ret; int *pnum = pnum_matched; if (st == NULL || st->num == 0) @@ -344,8 +392,9 @@ static int internal_find(OPENSSL_STACK *st, const void *data, if (!st->sorted) { int res = -1; - for (i = 0; i < st->num; i++) - if (st->comp(&data, st->data + i) == 0) { + for (i = 0; i < st->num; i++) { + cmp_ret = cmp_with_thunk(st, &data, st->data + i); + if (cmp_ret == 0) { if (res == -1) res = i; ++*pnum; @@ -353,6 +402,7 @@ static int internal_find(OPENSSL_STACK *st, const void *data, if (pnum_matched == NULL) return i; } + } if (res == -1) *pnum = 0; return res; @@ -360,7 +410,7 @@ static int internal_find(OPENSSL_STACK *st, const void *data, if (pnum_matched != NULL) ret_val_options |= OSSL_BSEARCH_FIRST_VALUE_ON_MATCH; - r = ossl_bsearch(&data, st->data, st->num, sizeof(void *), st->comp, + r = ossl_bsearch(&data, st->data, st->num, sizeof(void *), st->comp, st->cmp_thunk, ret_val_options); if (pnum_matched != NULL) { @@ -369,7 +419,8 @@ static int internal_find(OPENSSL_STACK *st, const void *data, const void **p = (const void **)r; while (p < st->data + st->num) { - if (st->comp(&data, p) != 0) + cmp_ret = cmp_with_thunk(st, &data, p); + if (cmp_ret != 0) break; ++*pnum; ++p; @@ -435,9 +486,10 @@ void OPENSSL_sk_pop_free(OPENSSL_STACK *st, OPENSSL_sk_freefunc func) if (st == NULL) return; + for (i = 0; i < st->num; i++) - if (st->data[i] != NULL) - func((char *)st->data[i]); + free_with_thunk(st, func, st->data[i]); + OPENSSL_sk_free(st); } @@ -477,10 +529,90 @@ void *OPENSSL_sk_set(OPENSSL_STACK *st, int i, const void *data) return (void *)st->data[i]; } +static ossl_inline void swap_data(const void **data, int i, int j) +{ + const void *tmp = data[i]; + + data[i] = data[j]; + data[j] = tmp; +} + +/* Restore the max-heap order of |st->data[root..end)| below |root| */ +static ossl_inline void sift_down(const OPENSSL_STACK *st, int root, int end) +{ + int child; + + while (root < end / 2) { + child = 2 * root + 1; + if (child + 1 < end && cmp_with_thunk(st, &st->data[child], &st->data[child + 1]) < 0) + child++; + if (cmp_with_thunk(st, &st->data[root], &st->data[child]) >= 0) + break; + swap_data(st->data, root, child); + root = child; + } +} + +static void heap_sort(const OPENSSL_STACK *st) +{ + int i; + + for (i = st->num / 2 - 1; i >= 0; i--) + sift_down(st, i, st->num); + for (i = st->num - 1; i > 0; i--) { + swap_data(st->data, 0, i); + sift_down(st, 0, i); + } +} + +/* Sort |st->data[lo..hi)| stably, using |tmp[lo..hi)| */ +static void merge_sort(const OPENSSL_STACK *st, const void **tmp, int lo, int hi) +{ + const void **data = st->data; + int mid = lo + (hi - lo) / 2; + int i = lo, j = mid, k = lo; + + if (hi - lo < 2) + return; + merge_sort(st, tmp, lo, mid); + merge_sort(st, tmp, mid, hi); + if (cmp_with_thunk(st, &data[mid - 1], &data[mid]) <= 0) + return; + while (i < mid && j < hi) + tmp[k++] = cmp_with_thunk(st, &data[j], &data[i]) < 0 ? data[j++] : data[i++]; + while (i < mid) + tmp[k++] = data[i++]; + /* The rest of the upper half is already in place */ + memcpy(&data[lo], &tmp[lo], (k - lo) * sizeof(*data)); +} + +/* + * Sort |st->data|, calling |st->comp| only via cmp_with_thunk(). + * Reported upstream in https://github.com/openssl/openssl/issues/22896#issuecomment-6952731417 + */ +static void sort_with_thunk(const OPENSSL_STACK *st) +{ + const void *stack[128]; + const void **tmp = stack; + + /* No |file| and |line|: a failed allocation reports no error, heap_sort() runs */ + if (st->num > (int)OSSL_NELEM(stack)) + tmp = CRYPTO_malloc(st->num * sizeof(*tmp), NULL, 0); + if (tmp == NULL) { + heap_sort(st); + return; + } + merge_sort(st, tmp, 0, st->num); + if (tmp != stack) + OPENSSL_free(tmp); +} + void OPENSSL_sk_sort(OPENSSL_STACK *st) { if (st != NULL && !st->sorted && st->comp != NULL) { - if (st->num > 1) + if (st->num > 1 && st->cmp_thunk != NULL) + sort_with_thunk(st); + else if (st->num > 1) qsort(st->data, st->num, sizeof(void *), st->comp); st->sorted = 1; /* empty or single-element stack is considered sorted */ } diff --git a/crypto/x509/v3_san.c b/crypto/x509/v3_san.c index f7ea1e0d75c06..6173ff51612dd 100644 --- a/crypto/x509/v3_san.c +++ b/crypto/x509/v3_san.c @@ -15,10 +15,10 @@ #include #include "ext_dat.h" -static GENERAL_NAMES *v2i_subject_alt(X509V3_EXT_METHOD *method, +static GENERAL_NAMES *v2i_subject_alt(const X509V3_EXT_METHOD *method, X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval); -static GENERAL_NAMES *v2i_issuer_alt(X509V3_EXT_METHOD *method, +static GENERAL_NAMES *v2i_issuer_alt(const X509V3_EXT_METHOD *method, X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval); static int copy_email(X509V3_CTX *ctx, GENERAL_NAMES *gens, int move_p); @@ -298,7 +298,7 @@ int GENERAL_NAME_print(BIO *out, GENERAL_NAME *gen) return 1; } -static GENERAL_NAMES *v2i_issuer_alt(X509V3_EXT_METHOD *method, +static GENERAL_NAMES *v2i_issuer_alt(const X509V3_EXT_METHOD *method, X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval) { @@ -374,7 +374,7 @@ static int copy_issuer(X509V3_CTX *ctx, GENERAL_NAMES *gens) return 0; } -static GENERAL_NAMES *v2i_subject_alt(X509V3_EXT_METHOD *method, +static GENERAL_NAMES *v2i_subject_alt(const X509V3_EXT_METHOD *method, X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval) { diff --git a/doc/man3/DEFINE_STACK_OF.pod b/doc/man3/DEFINE_STACK_OF.pod index eeb882f291e4e..d43def010c5e2 100644 --- a/doc/man3/DEFINE_STACK_OF.pod +++ b/doc/man3/DEFINE_STACK_OF.pod @@ -16,8 +16,9 @@ OPENSSL_sk_dup, OPENSSL_sk_find, OPENSSL_sk_find_ex, OPENSSL_sk_find_all, OPENSSL_sk_free, OPENSSL_sk_insert, OPENSSL_sk_is_sorted, OPENSSL_sk_new, OPENSSL_sk_new_null, OPENSSL_sk_new_reserve, OPENSSL_sk_num, OPENSSL_sk_pop, OPENSSL_sk_pop_free, OPENSSL_sk_push, OPENSSL_sk_reserve, OPENSSL_sk_set, -OPENSSL_sk_set_cmp_func, OPENSSL_sk_shift, OPENSSL_sk_sort, -OPENSSL_sk_unshift, OPENSSL_sk_value, OPENSSL_sk_zero +OPENSSL_sk_set_thunks, OPENSSL_sk_set_cmp_thunks, OPENSSL_sk_set_copy_thunks, +OPENSSL_sk_set_cmp_func, OPENSSL_sk_shift, +OPENSSL_sk_sort, OPENSSL_sk_unshift, OPENSSL_sk_value, OPENSSL_sk_zero - stack container =head1 SYNOPSIS @@ -240,8 +241,13 @@ OPENSSL_sk_free(), OPENSSL_sk_insert(), OPENSSL_sk_is_sorted(), OPENSSL_sk_new(), OPENSSL_sk_new_null(), OPENSSL_sk_new_reserve(), OPENSSL_sk_num(), OPENSSL_sk_pop(), OPENSSL_sk_pop_free(), OPENSSL_sk_push(), OPENSSL_sk_reserve(), OPENSSL_sk_set(), OPENSSL_sk_set_cmp_func(), -OPENSSL_sk_shift(), OPENSSL_sk_sort(), OPENSSL_sk_unshift(), -OPENSSL_sk_value(), OPENSSL_sk_zero(). +OPENSSL_sk_set_thunks(), OPENSSL_sk_set_cmp_thunks(), +OPENSSL_sk_set_copy_thunks(), OPENSSL_sk_shift(), OPENSSL_sk_sort(), +OPENSSL_sk_unshift(), OPENSSL_sk_value(), OPENSSL_sk_zero(). + +OPENSSL_sk_set_thunks(), OPENSSL_sk_set_cmp_thunks(), and +OPENSSL_sk_set_copy_thunks(), while public by necessity, are actually internal +and should not be used. =head1 RETURN VALUES @@ -298,6 +304,12 @@ B_sort>() should be called before these find operations. Before OpenSSL 3.3.0 B_push>() returned -1 if I was NULL. It was changed to return 0 in this condition as for other errors. +OPENSSL_sk_set_thunks() was added in OpenSSL 3.6.0. + +OPENSSL_sk_set_cmp_thunks() was added in OpenSSL 4.0.0. + +OPENSSL_sk_set_copy_thunks() was added in OpenSSL 4.1.0. + =head1 COPYRIGHT Copyright 2000-2025 The OpenSSL Project Authors. All Rights Reserved. diff --git a/fuzz/corpora b/fuzz/corpora deleted file mode 160000 index ce771805c094d..0000000000000 --- a/fuzz/corpora +++ /dev/null @@ -1 +0,0 @@ -Subproject commit ce771805c094d098c25a218bc8e9f7344eccbc5a diff --git a/gost-engine b/gost-engine deleted file mode 160000 index 74b1f4fddbc2d..0000000000000 --- a/gost-engine +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 74b1f4fddbc2d6de969815b1992ddc1ae7c643fe diff --git a/include/crypto/sparse_array.h b/include/crypto/sparse_array.h index 5ac2e359ee66d..e8de36a1f097e 100644 --- a/include/crypto/sparse_array.h +++ b/include/crypto/sparse_array.h @@ -22,6 +22,8 @@ extern "C" { #define DEFINE_SPARSE_ARRAY_OF_INTERNAL(type, ctype) \ SPARSE_ARRAY_OF(type); \ + typedef void (*sa_##type##_leaffunc)(ossl_uintmax_t idx, type *t); \ + typedef void (*sa_##type##_leaffunc_arg)(ossl_uintmax_t idx, type *t, void *arg); \ static ossl_unused ossl_inline SPARSE_ARRAY_OF(type) *ossl_sa_##type##_new(void) \ { \ return (SPARSE_ARRAY_OF(type) *)ossl_sa_new(); \ @@ -41,20 +43,40 @@ extern "C" { { \ return ossl_sa_num((OPENSSL_SA *)sa); \ } \ + static ossl_unused void \ + ossl_sa_##type##_doall_thunk(ossl_uintmax_t idx, void *leaf, void *arg) \ + { \ + sa_##type##_leaffunc fn = *(sa_##type##_leaffunc *)arg; \ + (*fn)(idx, (type *)leaf); \ + } \ static ossl_unused ossl_inline void \ ossl_sa_##type##_doall(const SPARSE_ARRAY_OF(type) *sa, \ void (*leaf)(ossl_uintmax_t, type *)) \ { \ - ossl_sa_doall((OPENSSL_SA *)sa, \ - (void (*)(ossl_uintmax_t, void *))leaf); \ + ossl_sa_doall_arg((OPENSSL_SA *)sa, ossl_sa_##type##_doall_thunk, &leaf); \ + } \ + struct ossl_sa_##type##_doall_thunk { \ + sa_##type##_leaffunc_arg fn; \ + void *arg; \ + }; \ + static ossl_unused void \ + ossl_sa_##type##_doall_arg_thunk(ossl_uintmax_t idx, void *leaf, void *arg) \ + { \ + struct ossl_sa_##type##_doall_thunk *t = arg; \ + \ + (*t->fn)(idx, (type *)leaf, t->arg); \ } \ static ossl_unused ossl_inline void \ ossl_sa_##type##_doall_arg(const SPARSE_ARRAY_OF(type) *sa, \ void (*leaf)(ossl_uintmax_t, type *, void *), \ void *arg) \ { \ + struct ossl_sa_##type##_doall_thunk t; \ + \ + t.fn = leaf; \ + t.arg = arg; \ ossl_sa_doall_arg((OPENSSL_SA *)sa, \ - (void (*)(ossl_uintmax_t, void *, void *))leaf, arg); \ + ossl_sa_##type##_doall_arg_thunk, &t); \ } \ static ossl_unused ossl_inline ctype *ossl_sa_##type##_get(const SPARSE_ARRAY_OF(type) *sa, ossl_uintmax_t n) \ { \ diff --git a/include/internal/cryptlib.h b/include/internal/cryptlib.h index a4451ca9eeee3..0b79a650610a3 100644 --- a/include/internal/cryptlib.h +++ b/include/internal/cryptlib.h @@ -154,6 +154,7 @@ int ossl_crypto_free_ex_index_ex(OSSL_LIB_CTX *ctx, int class_index, int idx); const void *ossl_bsearch(const void *key, const void *base, int num, int size, int (*cmp)(const void *, const void *), + int (*cmp_thunk)(int (*real_cmp_fn)(const void *, const void *), const void *, const void *), int flags); char *ossl_sk_ASN1_UTF8STRING2text(STACK_OF(ASN1_UTF8STRING) *text, diff --git a/include/internal/refcount.h b/include/internal/refcount.h index 52a588bc3cbe4..4d7df9ccd6dee 100644 --- a/include/internal/refcount.h +++ b/include/internal/refcount.h @@ -21,8 +21,9 @@ #define HAVE_C11_ATOMICS #endif -#if defined(HAVE_C11_ATOMICS) && defined(ATOMIC_INT_LOCK_FREE) \ - && ATOMIC_INT_LOCK_FREE > 0 +# if defined(HAVE_C11_ATOMICS) && defined(ATOMIC_INT_LOCK_FREE) \ + && ATOMIC_INT_LOCK_FREE > 0 \ + && 0 /// ClickHouse-specific patch: if we use atomics, tsan complains :( #define HAVE_ATOMICS 1 @@ -74,7 +75,8 @@ static inline int CRYPTO_GET_REF(CRYPTO_REF_COUNT *refcnt, int *ret) return 1; } -#elif defined(__GNUC__) && defined(__ATOMIC_RELAXED) && __GCC_ATOMIC_INT_LOCK_FREE > 0 +# elif defined(__GNUC__) && defined(__ATOMIC_RELAXED) && __GCC_ATOMIC_INT_LOCK_FREE > 0 \ + && 0 /// ClickHouse-specific patch: if we use atomics, tsan complains :( #define HAVE_ATOMICS 1 diff --git a/include/openssl/safestack.h.in b/include/openssl/safestack.h.in index a88409ba4151e..dd2d1560d25a3 100644 --- a/include/openssl/safestack.h.in +++ b/include/openssl/safestack.h.in @@ -36,34 +36,52 @@ extern "C" { #define STACK_OF(type) struct stack_st_##type /* Helper macro for internal use */ -#define SKM_DEFINE_STACK_OF_INTERNAL(t1, t2, t3) \ - STACK_OF(t1); \ - typedef int (*sk_##t1##_compfunc)(const t3 *const *a, const t3 *const *b); \ - typedef void (*sk_##t1##_freefunc)(t3 * a); \ - typedef t3 *(*sk_##t1##_copyfunc)(const t3 *a); \ - static ossl_unused ossl_inline t2 *ossl_check_##t1##_type(t2 *ptr) \ - { \ - return ptr; \ - } \ - static ossl_unused ossl_inline const OPENSSL_STACK *ossl_check_const_##t1##_sk_type(const STACK_OF(t1) *sk) \ - { \ - return (const OPENSSL_STACK *)sk; \ - } \ - static ossl_unused ossl_inline OPENSSL_STACK *ossl_check_##t1##_sk_type(STACK_OF(t1) *sk) \ - { \ - return (OPENSSL_STACK *)sk; \ - } \ - static ossl_unused ossl_inline OPENSSL_sk_compfunc ossl_check_##t1##_compfunc_type(sk_##t1##_compfunc cmp) \ - { \ - return (OPENSSL_sk_compfunc)cmp; \ - } \ - static ossl_unused ossl_inline OPENSSL_sk_copyfunc ossl_check_##t1##_copyfunc_type(sk_##t1##_copyfunc cpy) \ - { \ - return (OPENSSL_sk_copyfunc)cpy; \ - } \ - static ossl_unused ossl_inline OPENSSL_sk_freefunc ossl_check_##t1##_freefunc_type(sk_##t1##_freefunc fr) \ - { \ - return (OPENSSL_sk_freefunc)fr; \ +#define SKM_DEFINE_STACK_OF_INTERNAL(t1, t2, t3) \ + STACK_OF(t1); \ + typedef int (*sk_##t1##_compfunc)(const t3 *const *a, const t3 *const *b); \ + typedef void (*sk_##t1##_freefunc)(t3 * a); \ + typedef t3 *(*sk_##t1##_copyfunc)(const t3 *a); \ + static ossl_inline void sk_##t1##_freefunc_thunk(OPENSSL_sk_freefunc freefunc_arg, void *ptr) \ + { \ + sk_##t1##_freefunc freefunc = (sk_##t1##_freefunc)freefunc_arg; \ + freefunc((t3 *)ptr); \ + } \ + static ossl_inline void *sk_##t1##_copyfunc_thunk(OPENSSL_sk_copyfunc copyfunc_arg, const void *ptr) \ + { \ + sk_##t1##_copyfunc copyfunc = (sk_##t1##_copyfunc)copyfunc_arg; \ + return (void *)copyfunc((const t3 *)ptr); \ + } \ + static ossl_inline int sk_##t1##_cmpfunc_thunk(int (*cmp)(const void *, const void *), const void *a, const void *b) \ + { \ + int (*realcmp)(const t3 *const *a, const t3 *const *b) = (int (*)(const t3 *const *a, const t3 *const *b))(cmp); \ + const t3 *const *at = (const t3 *const *)a; \ + const t3 *const *bt = (const t3 *const *)b; \ + \ + return realcmp(at, bt); \ + } \ + static ossl_unused ossl_inline t2 *ossl_check_##t1##_type(t2 *ptr) \ + { \ + return ptr; \ + } \ + static ossl_unused ossl_inline const OPENSSL_STACK *ossl_check_const_##t1##_sk_type(const STACK_OF(t1) *sk) \ + { \ + return (const OPENSSL_STACK *)sk; \ + } \ + static ossl_unused ossl_inline OPENSSL_STACK *ossl_check_##t1##_sk_type(STACK_OF(t1) *sk) \ + { \ + return (OPENSSL_STACK *)sk; \ + } \ + static ossl_unused ossl_inline OPENSSL_sk_compfunc ossl_check_##t1##_compfunc_type(sk_##t1##_compfunc cmp) \ + { \ + return (OPENSSL_sk_compfunc)cmp; \ + } \ + static ossl_unused ossl_inline OPENSSL_sk_copyfunc ossl_check_##t1##_copyfunc_type(sk_##t1##_copyfunc cpy) \ + { \ + return (OPENSSL_sk_copyfunc)cpy; \ + } \ + static ossl_unused ossl_inline OPENSSL_sk_freefunc ossl_check_##t1##_freefunc_type(sk_##t1##_freefunc fr) \ + { \ + return (OPENSSL_sk_freefunc)fr; \ } #define SKM_DEFINE_STACK_OF(t1, t2, t3) \ @@ -71,6 +89,24 @@ extern "C" { typedef int (*sk_##t1##_compfunc)(const t3 *const *a, const t3 *const *b); \ typedef void (*sk_##t1##_freefunc)(t3 * a); \ typedef t3 *(*sk_##t1##_copyfunc)(const t3 *a); \ + static ossl_inline void sk_##t1##_freefunc_thunk(OPENSSL_sk_freefunc freefunc_arg, void *ptr) \ + { \ + sk_##t1##_freefunc freefunc = (sk_##t1##_freefunc)freefunc_arg; \ + freefunc((t3 *)ptr); \ + } \ + static ossl_inline void *sk_##t1##_copyfunc_thunk(OPENSSL_sk_copyfunc copyfunc_arg, const void *ptr) \ + { \ + sk_##t1##_copyfunc copyfunc = (sk_##t1##_copyfunc)copyfunc_arg; \ + return (void *)copyfunc((const t3 *)ptr); \ + } \ + static ossl_inline int sk_##t1##_cmpfunc_thunk(int (*cmp)(const void *, const void *), const void *a, const void *b) \ + { \ + int (*realcmp)(const t3 *const *a, const t3 *const *b) = (int (*)(const t3 *const *a, const t3 *const *b))(cmp); \ + const t3 *const *at = (const t3 *const *)a; \ + const t3 *const *bt = (const t3 *const *)b; \ + \ + return realcmp(at, bt); \ + } \ static ossl_unused ossl_inline int sk_##t1##_num(const STACK_OF(t1) *sk) \ { \ return OPENSSL_sk_num((const OPENSSL_STACK *)sk); \ @@ -81,15 +117,21 @@ extern "C" { } \ static ossl_unused ossl_inline STACK_OF(t1) *sk_##t1##_new(sk_##t1##_compfunc compare) \ { \ - return (STACK_OF(t1) *)OPENSSL_sk_new((OPENSSL_sk_compfunc)compare); \ + OPENSSL_STACK *ret = OPENSSL_sk_new((OPENSSL_sk_compfunc)compare); \ + return (STACK_OF(t1) *)OPENSSL_sk_install_thunks(ret, sk_##t1##_cmpfunc_thunk, sk_##t1##_copyfunc_thunk, \ + sk_##t1##_freefunc_thunk); \ } \ static ossl_unused ossl_inline STACK_OF(t1) *sk_##t1##_new_null(void) \ { \ - return (STACK_OF(t1) *)OPENSSL_sk_new_null(); \ + OPENSSL_STACK *ret = OPENSSL_sk_new_null(); \ + return (STACK_OF(t1) *)OPENSSL_sk_install_thunks(ret, sk_##t1##_cmpfunc_thunk, sk_##t1##_copyfunc_thunk, \ + sk_##t1##_freefunc_thunk); \ } \ static ossl_unused ossl_inline STACK_OF(t1) *sk_##t1##_new_reserve(sk_##t1##_compfunc compare, int n) \ { \ - return (STACK_OF(t1) *)OPENSSL_sk_new_reserve((OPENSSL_sk_compfunc)compare, n); \ + OPENSSL_STACK *ret = OPENSSL_sk_new_reserve((OPENSSL_sk_compfunc)compare, n); \ + return (STACK_OF(t1) *)OPENSSL_sk_install_thunks(ret, sk_##t1##_cmpfunc_thunk, sk_##t1##_copyfunc_thunk, \ + sk_##t1##_freefunc_thunk); \ } \ static ossl_unused ossl_inline int sk_##t1##_reserve(STACK_OF(t1) *sk, int n) \ { \ @@ -130,6 +172,7 @@ extern "C" { } \ static ossl_unused ossl_inline void sk_##t1##_pop_free(STACK_OF(t1) *sk, sk_##t1##_freefunc freefunc) \ { \ + sk = (STACK_OF(t1) *)OPENSSL_sk_install_free_thunk((OPENSSL_STACK *)sk, sk_##t1##_freefunc_thunk); \ OPENSSL_sk_pop_free((OPENSSL_STACK *)sk, (OPENSSL_sk_freefunc)freefunc); \ } \ static ossl_unused ossl_inline int sk_##t1##_insert(STACK_OF(t1) *sk, t2 *ptr, int idx) \ diff --git a/include/openssl/stack.h b/include/openssl/stack.h index 82ef52ab72a73..2324ade2d93f5 100644 --- a/include/openssl/stack.h +++ b/include/openssl/stack.h @@ -24,7 +24,9 @@ typedef struct stack_st OPENSSL_STACK; /* Use STACK_OF(...) instead */ typedef int (*OPENSSL_sk_compfunc)(const void *, const void *); typedef void (*OPENSSL_sk_freefunc)(void *); +typedef void (*OPENSSL_sk_freefunc_thunk)(OPENSSL_sk_freefunc, void *); typedef void *(*OPENSSL_sk_copyfunc)(const void *); +typedef void *(*OPENSSL_sk_copyfunc_thunk)(OPENSSL_sk_copyfunc, const void *); int OPENSSL_sk_num(const OPENSSL_STACK *); void *OPENSSL_sk_value(const OPENSSL_STACK *, int); @@ -34,9 +36,29 @@ void *OPENSSL_sk_set(OPENSSL_STACK *st, int i, const void *data); OPENSSL_STACK *OPENSSL_sk_new(OPENSSL_sk_compfunc cmp); OPENSSL_STACK *OPENSSL_sk_new_null(void); OPENSSL_STACK *OPENSSL_sk_new_reserve(OPENSSL_sk_compfunc c, int n); +OPENSSL_STACK *OPENSSL_sk_set_thunks(OPENSSL_STACK *st, OPENSSL_sk_freefunc_thunk f_thunk); +OPENSSL_STACK *OPENSSL_sk_set_cmp_thunks(OPENSSL_STACK *st, int (*c_thunk)(int (*)(const void *, const void *), const void *, const void *)); +OPENSSL_STACK *OPENSSL_sk_set_copy_thunks(OPENSSL_STACK *st, OPENSSL_sk_copyfunc_thunk cp_thunk); +/* + * The setters of the typed thunks are not in upstream OpenSSL 3.5, and only Clang CFI (cfi-icall) needs the thunks, + * so only CFI builds install them and the others keep the upstream ABI. + */ +#if defined(__has_feature) +#if __has_feature(cfi_icall_sanitizer) +#define OPENSSL_SK_TYPED_THUNKS +#endif +#endif +#ifdef OPENSSL_SK_TYPED_THUNKS +#define OPENSSL_sk_install_free_thunk(st, free_thunk) OPENSSL_sk_set_thunks((st), (free_thunk)) +#define OPENSSL_sk_install_thunks(st, cmp_thunk, copy_thunk, free_thunk) \ + OPENSSL_sk_set_thunks(OPENSSL_sk_set_copy_thunks(OPENSSL_sk_set_cmp_thunks((st), (cmp_thunk)), (copy_thunk)), (free_thunk)) +#else +#define OPENSSL_sk_install_free_thunk(st, free_thunk) (st) +#define OPENSSL_sk_install_thunks(st, cmp_thunk, copy_thunk, free_thunk) (st) +#endif int OPENSSL_sk_reserve(OPENSSL_STACK *st, int n); void OPENSSL_sk_free(OPENSSL_STACK *); -void OPENSSL_sk_pop_free(OPENSSL_STACK *st, void (*func)(void *)); +void OPENSSL_sk_pop_free(OPENSSL_STACK *st, OPENSSL_sk_freefunc func); OPENSSL_STACK *OPENSSL_sk_deep_copy(const OPENSSL_STACK *, OPENSSL_sk_copyfunc c, OPENSSL_sk_freefunc f); diff --git a/krb5 b/krb5 deleted file mode 160000 index f5bee90a501da..0000000000000 --- a/krb5 +++ /dev/null @@ -1 +0,0 @@ -Subproject commit f5bee90a501da6c351b3db46c1ae837b033a714c diff --git a/oqs-provider b/oqs-provider deleted file mode 160000 index 7bc597c04b534..0000000000000 --- a/oqs-provider +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 7bc597c04b534ddea9b6654481deb31ded8e1bbc diff --git a/pkcs11-provider b/pkcs11-provider deleted file mode 160000 index b55b94344af87..0000000000000 --- a/pkcs11-provider +++ /dev/null @@ -1 +0,0 @@ -Subproject commit b55b94344af87a058fd84d6e15390760a5585b1d diff --git a/providers/implementations/include/prov/digestcommon.h b/providers/implementations/include/prov/digestcommon.h index 48b0fd4d23932..1789e4ef6d3c2 100644 --- a/providers/implementations/include/prov/digestcommon.h +++ b/providers/implementations/include/prov/digestcommon.h @@ -93,11 +93,17 @@ extern "C" { CTX *inctx = (CTX *)vinctx; \ *outctx = *inctx; \ } \ + static OSSL_FUNC_digest_update_fn name##_internal_update; \ + static int name##_internal_update(void *ctx, const unsigned char *in, \ + size_t inl) \ + { \ + return upd(ctx, in, inl); \ + } \ PROV_FUNC_DIGEST_FINAL(name, dgstsize, fin) \ PROV_FUNC_DIGEST_GET_PARAM(name, blksize, dgstsize, flags) \ const OSSL_DISPATCH ossl_##name##_functions[] = { \ { OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))name##_newctx }, \ - { OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))upd }, \ + { OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))name##_internal_update }, \ { OSSL_FUNC_DIGEST_FINAL, (void (*)(void))name##_internal_final }, \ { OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))name##_freectx }, \ { OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))name##_dupctx }, \ diff --git a/providers/implementations/rands/drbg_ctr.c b/providers/implementations/rands/drbg_ctr.c index 57da1cfdf33df..fa4a7400ffbd2 100644 --- a/providers/implementations/rands/drbg_ctr.c +++ b/providers/implementations/rands/drbg_ctr.c @@ -25,6 +25,12 @@ #include "internal/provider.h" #include "internal/common.h" +#if defined(__has_feature) +# if __has_feature(memory_sanitizer) +# include +# endif +#endif + static OSSL_FUNC_rand_newctx_fn drbg_ctr_new_wrapper; static OSSL_FUNC_rand_freectx_fn drbg_ctr_free; static OSSL_FUNC_rand_instantiate_fn drbg_ctr_instantiate_wrapper; @@ -72,6 +78,12 @@ static void inc_128(PROV_DRBG_CTR *ctr) p[n] = (u8)c; c >>= 8; } while (n); + +#if defined(__has_feature) +# if __has_feature(memory_sanitizer) + __msan_unpoison(p, 16); +# endif +#endif } static void ctr_XOR(PROV_DRBG_CTR *ctr, const unsigned char *in, size_t inlen) diff --git a/providers/implementations/rands/seeding/rand_unix.c b/providers/implementations/rands/seeding/rand_unix.c index 0b8a9ec341d92..ed82495e693fc 100644 --- a/providers/implementations/rands/seeding/rand_unix.c +++ b/providers/implementations/rands/seeding/rand_unix.c @@ -351,8 +351,13 @@ static ssize_t syscall_random(void *buf, size_t buflen) * Note: Sometimes getentropy() can be provided but not implemented * internally. So we need to check errno for ENOSYS */ -#if !defined(__DragonFly__) && !defined(__NetBSD__) && !defined(__FreeBSD__) -#if defined(__GNUC__) && __GNUC__ >= 2 && defined(__ELF__) && !defined(__hpux) +# if !defined(__DragonFly__) && !defined(__NetBSD__) && !defined(__FreeBSD__) + + /// Disable the usage of "getentropy" function from libc (on static link time) to avoid dependency on too new libc version. + /// Otherwise, if we build ClickHouse on a system with new libc and run the built binary on a system with old libc, it will fail. + /// + /// Note that there is a fallback below to (1) runtime symbol lookup and (2) direct syscall, that are equivalent. +# if 0 && defined(__GNUC__) && __GNUC__>=2 && defined(__ELF__) && !defined(__hpux) extern int getentropy(void *buffer, size_t length) __attribute__((weak)); if (getentropy != NULL) { diff --git a/pyca-cryptography b/pyca-cryptography deleted file mode 160000 index 7e33b0e7739d6..0000000000000 --- a/pyca-cryptography +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 7e33b0e7739d633c77b8c478620167f693ed13f4 diff --git a/python-ecdsa b/python-ecdsa deleted file mode 160000 index 4096fa0171592..0000000000000 --- a/python-ecdsa +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 4096fa01715929e08b97e73f3173aee9d57f2a3f diff --git a/test/stack_test.c b/test/stack_test.c index 7bea2a2b7f324..d88ec15926003 100644 --- a/test/stack_test.c +++ b/test/stack_test.c @@ -269,6 +269,16 @@ static void SS_free(TST_SS *p) OPENSSL_free(p); } +static char *string_copy(const char *p) +{ + return OPENSSL_strdup(p); +} + +static void string_free(char *p) +{ + OPENSSL_free(p); +} + static int test_SS_stack(void) { STACK_OF(TST_SS) *s = sk_TST_SS_new_null(); @@ -343,6 +353,53 @@ static int test_SS_stack(void) return testresult; } +static int test_OPENSSL_STRING_deep_copy_mfail(void) +{ + STACK_OF(OPENSSL_STRING) *s = sk_OPENSSL_STRING_new_null(); + STACK_OF(OPENSSL_STRING) *r = NULL; + static const char *strings[] = { + "alpha", "beta", "gamma" + }; + char *p; + int i; + int testresult = 0; + + if (!TEST_ptr(s)) + goto end; + + for (i = 0; i < (int)OSSL_NELEM(strings); i++) { + p = OPENSSL_strdup(strings[i]); + if (!TEST_ptr(p) + || !TEST_int_eq(sk_OPENSSL_STRING_push(s, p), i + 1)) { + OPENSSL_free(p); + goto end; + } + } + + r = sk_OPENSSL_STRING_deep_copy(s, string_copy, string_free); + + if (r == NULL) + goto end; + + if (!TEST_int_eq(sk_OPENSSL_STRING_num(r), sk_OPENSSL_STRING_num(s))) + goto end; + + for (i = 0; i < sk_OPENSSL_STRING_num(s); i++) { + char *src = sk_OPENSSL_STRING_value(s, i); + char *dst = sk_OPENSSL_STRING_value(r, i); + + if (!TEST_ptr_ne(dst, src) + || !TEST_str_eq(dst, src)) + goto end; + } + + testresult = 1; +end: + sk_OPENSSL_STRING_pop_free(r, string_free); + sk_OPENSSL_STRING_pop_free(s, string_free); + return testresult; +} + static int test_SU_stack(void) { STACK_OF(TST_SU) *s = sk_TST_SU_new_null(); @@ -379,11 +436,43 @@ static int test_SU_stack(void) return testresult; } +static int test_int_stack_sort(void) +{ + STACK_OF(sint) *s = sk_sint_new(&int_compare); + int v[200]; + const int n = OSSL_NELEM(v); + int i; + int testresult = 0; + + /* unsorted, with duplicates */ + for (i = 0; i < n; i++) { + v[i] = (i * 37) % 10; + sk_sint_push(s, v + i); + } + if (!TEST_int_eq(sk_sint_num(s), n)) + goto end; + sk_sint_sort(s); + if (!TEST_ulong_eq(ERR_peek_error(), 0)) + goto end; + for (i = 1; i < n; i++) + if (!TEST_int_le(*sk_sint_value(s, i - 1), *sk_sint_value(s, i))) { + TEST_info("int stack sort %d", i); + goto end; + } + + testresult = 1; +end: + sk_sint_free(s); + return testresult; +} + int setup_tests(void) { ADD_ALL_TESTS(test_int_stack, 4); + ADD_TEST(test_int_stack_sort); ADD_ALL_TESTS(test_uchar_stack, 4); ADD_TEST(test_SS_stack); ADD_TEST(test_SU_stack); + ADD_TEST(test_OPENSSL_STRING_deep_copy_mfail); return 1; } diff --git a/tlsfuzzer b/tlsfuzzer deleted file mode 160000 index 61f45d9701294..0000000000000 --- a/tlsfuzzer +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 61f45d9701294fd87ef92d2a7e3dfb076653a562 diff --git a/tlslite-ng b/tlslite-ng deleted file mode 160000 index 77ef321dde1a9..0000000000000 --- a/tlslite-ng +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 77ef321dde1a9e6bcf94d73c80f8789a770d8031 diff --git a/util/libcrypto.num b/util/libcrypto.num index ceb4948839a7c..a2aa5224c97e3 100644 --- a/util/libcrypto.num +++ b/util/libcrypto.num @@ -5924,3 +5924,6 @@ OSSL_AA_DIST_POINT_free 6051 3_5_0 EXIST::FUNCTION: OSSL_AA_DIST_POINT_new 6052 3_5_0 EXIST::FUNCTION: OSSL_AA_DIST_POINT_it 6053 3_5_0 EXIST::FUNCTION: PEM_ASN1_write_bio_ctx 6054 3_5_0 EXIST::FUNCTION: +OPENSSL_sk_set_thunks 6055 3_5_0 EXIST::FUNCTION: +OPENSSL_sk_set_cmp_thunks 6056 3_5_0 EXIST::FUNCTION: +OPENSSL_sk_set_copy_thunks 6057 3_5_0 EXIST::FUNCTION: diff --git a/util/perl/OpenSSL/stackhash.pm b/util/perl/OpenSSL/stackhash.pm index 7c2459b8a4061..b017ea44b666f 100644 --- a/util/perl/OpenSSL/stackhash.pm +++ b/util/perl/OpenSSL/stackhash.pm @@ -28,9 +28,9 @@ sub generate_stack_macros_int { SKM_DEFINE_STACK_OF_INTERNAL(${nametype}, ${realtype}, ${plaintype}) #define sk_${nametype}_num(sk) OPENSSL_sk_num(ossl_check_const_${nametype}_sk_type(sk)) #define sk_${nametype}_value(sk, idx) ((${realtype} *)OPENSSL_sk_value(ossl_check_const_${nametype}_sk_type(sk), (idx))) -#define sk_${nametype}_new(cmp) ((STACK_OF(${nametype}) *)OPENSSL_sk_new(ossl_check_${nametype}_compfunc_type(cmp))) -#define sk_${nametype}_new_null() ((STACK_OF(${nametype}) *)OPENSSL_sk_new_null()) -#define sk_${nametype}_new_reserve(cmp, n) ((STACK_OF(${nametype}) *)OPENSSL_sk_new_reserve(ossl_check_${nametype}_compfunc_type(cmp), (n))) +#define sk_${nametype}_new(cmp) ((STACK_OF(${nametype}) *)OPENSSL_sk_install_thunks(OPENSSL_sk_new(ossl_check_${nametype}_compfunc_type(cmp)), sk_${nametype}_cmpfunc_thunk, sk_${nametype}_copyfunc_thunk, sk_${nametype}_freefunc_thunk)) +#define sk_${nametype}_new_null() ((STACK_OF(${nametype}) *)OPENSSL_sk_install_thunks(OPENSSL_sk_new_null(), sk_${nametype}_cmpfunc_thunk, sk_${nametype}_copyfunc_thunk, sk_${nametype}_freefunc_thunk)) +#define sk_${nametype}_new_reserve(cmp, n) ((STACK_OF(${nametype}) *)OPENSSL_sk_install_thunks(OPENSSL_sk_new_reserve(ossl_check_${nametype}_compfunc_type(cmp), (n)), sk_${nametype}_cmpfunc_thunk, sk_${nametype}_copyfunc_thunk, sk_${nametype}_freefunc_thunk)) #define sk_${nametype}_reserve(sk, n) OPENSSL_sk_reserve(ossl_check_${nametype}_sk_type(sk), (n)) #define sk_${nametype}_free(sk) OPENSSL_sk_free(ossl_check_${nametype}_sk_type(sk)) #define sk_${nametype}_zero(sk) OPENSSL_sk_zero(ossl_check_${nametype}_sk_type(sk)) @@ -40,7 +40,7 @@ SKM_DEFINE_STACK_OF_INTERNAL(${nametype}, ${realtype}, ${plaintype}) #define sk_${nametype}_unshift(sk, ptr) OPENSSL_sk_unshift(ossl_check_${nametype}_sk_type(sk), ossl_check_${nametype}_type(ptr)) #define sk_${nametype}_pop(sk) ((${realtype} *)OPENSSL_sk_pop(ossl_check_${nametype}_sk_type(sk))) #define sk_${nametype}_shift(sk) ((${realtype} *)OPENSSL_sk_shift(ossl_check_${nametype}_sk_type(sk))) -#define sk_${nametype}_pop_free(sk, freefunc) OPENSSL_sk_pop_free(ossl_check_${nametype}_sk_type(sk),ossl_check_${nametype}_freefunc_type(freefunc)) +#define sk_${nametype}_pop_free(sk, freefunc) OPENSSL_sk_pop_free(ossl_check_${nametype}_sk_type(sk), ossl_check_${nametype}_freefunc_type(freefunc)) #define sk_${nametype}_insert(sk, ptr, idx) OPENSSL_sk_insert(ossl_check_${nametype}_sk_type(sk), ossl_check_${nametype}_type(ptr), (idx)) #define sk_${nametype}_set(sk, idx, ptr) ((${realtype} *)OPENSSL_sk_set(ossl_check_${nametype}_sk_type(sk), (idx), ossl_check_${nametype}_type(ptr))) #define sk_${nametype}_find(sk, ptr) OPENSSL_sk_find(ossl_check_${nametype}_sk_type(sk), ossl_check_${nametype}_type(ptr)) diff --git a/wycheproof b/wycheproof deleted file mode 160000 index 2196000605e45..0000000000000 --- a/wycheproof +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 2196000605e45d91097147c9c71f26b72af58003