From c8f8bf7b9ef52f5306854e5f7a6ff7a5d45cbdc9 Mon Sep 17 00:00:00 2001 From: Robert Schulze Date: Fri, 6 Sep 2024 17:46:57 +0000 Subject: [PATCH 01/21] Remove submodules (cherry picked from commit ae2e0753685696f30915675d7f76a91bee56c339) (cherry picked from commit 3b933a6e933c46660bf9640153f81e896d855032) (cherry picked from commit 18138b6b80290940ec43554355fea51c0e3386a5) --- .gitmodules | 37 ------------------------------------- cloudflare-quiche | 1 - fuzz/corpora | 1 - gost-engine | 1 - krb5 | 1 - oqs-provider | 1 - pyca-cryptography | 1 - python-ecdsa | 1 - tlsfuzzer | 1 - tlslite-ng | 1 - wycheproof | 1 - 11 files changed, 47 deletions(-) delete mode 160000 cloudflare-quiche delete mode 160000 fuzz/corpora delete mode 160000 gost-engine delete mode 160000 krb5 delete mode 160000 oqs-provider delete mode 160000 pyca-cryptography delete mode 160000 python-ecdsa delete mode 160000 tlsfuzzer delete mode 160000 tlslite-ng delete mode 160000 wycheproof 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/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/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/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/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/wycheproof b/wycheproof deleted file mode 160000 index 2196000605e45..0000000000000 --- a/wycheproof +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 2196000605e45d91097147c9c71f26b72af58003 From 7d3e48271bcf7f98b9ef5dd9e48e197b451bb550 Mon Sep 17 00:00:00 2001 From: Robert Schulze Date: Fri, 26 Jan 2024 16:34:22 +0000 Subject: [PATCH 02/21] Msan fixes (cherry picked from commit 35f2869954dd2671ee2f02c808ec0ee34d74eee3) (cherry picked from commit e48372d2957bf13d03ae69f7a15ed9b41ed29bbb) (cherry picked from commit e4e3dc7b53e8ae6bd7e5c5a75f10df905cb2958c) --- crypto/bn/bn_intern.c | 10 ++++++++++ crypto/cpuid.c | 9 +++++++++ providers/implementations/rands/drbg_ctr.c | 12 ++++++++++++ 3 files changed, 31 insertions(+) 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/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/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) From dbd8308f6521d7ff130b07c97e5302bab41fd580 Mon Sep 17 00:00:00 2001 From: Robert Schulze Date: Thu, 25 Jan 2024 11:03:50 +0000 Subject: [PATCH 03/21] Remove dependency on "getrandom" function (cherry picked from commit a6387b8f1883fc074cd92c83a20fa5930accaba6) (cherry picked from commit e695f2b83f19d418dc76a80a289c16237a77cfd3) (cherry picked from commit 6992a92538ae18bfd73bdb9de689a59796e924e6) --- providers/implementations/rands/seeding/rand_unix.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) 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) { From c7aee197ec187b09a7bdeaf45d8c66ed871752be Mon Sep 17 00:00:00 2001 From: Robert Schulze Date: Mon, 11 Mar 2024 14:03:58 +0000 Subject: [PATCH 04/21] Remove dependency on glibc 2.14 sendmmsg/recvmmsg (cherry picked from commit a5c57bbdeca0042282b6427ee72c2abcbba4ec23) (cherry picked from commit 7d1f427605cfabaa51006ade4012a6ef07db5f89) (cherry picked from commit e58441d48f63e8de6dd7859063885ecfb7d6c2fd) --- crypto/bio/bss_dgram.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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 */ From 673dfb1aae2bb0daacf611812d00d2c26ab424a9 Mon Sep 17 00:00:00 2001 From: Robert Schulze Date: Wed, 20 Mar 2024 12:22:40 +0000 Subject: [PATCH 05/21] Suppress tsan failures: use locks instead of atomics ----- E Exception: Sanitizer assert found for instance ================== E WARNING: ThreadSanitizer: data race (pid=1) E Write of size 8 at 0x7b2800025d30 by thread T2 (mutexes: write M0, write M1): E #0 free (clickhouse+0x709a3e5) (BuildId: 706d92b17db171493f293d517643f726ee1b7b1e) E #1 CRYPTO_free build_docker/./contrib/openssl/crypto/mem.c:282:5 (clickhouse+0x2015f8ea) (BuildId: 706d92b17db171493f293d517643f726ee1b7b1e) E #2 EVP_PKEY_free build_docker/./contrib/openssl/crypto/evp/p_lib.c:1809:5 (clickhouse+0x2012a751) (BuildId: 706d92b17db171493f293d517643f726ee1b7b1e) E #3 Poco::Crypto::EVPPKey::~EVPPKey() build_docker/./base/poco/Crypto/src/EVPPKey.cpp:121:17 (clickhouse+0x1d00ffa9) (BuildId: 706d92b17db171493f293d517643f726ee1b7b1e) E #4 DB::CertificateReloader::Data::~Data() build_docker/./src/Server/CertificateReloader.h:71:12 (clickhouse+0x194fb42d) (BuildId: 706d92b17db171493f293d517643f726ee1b7b1e) E #5 std::__1::default_delete::operator()[abi:v15000](DB::CertificateReloader::Data const*) const build_docker/./contrib/llvm-project/libcxx/include/__memory/unique_ptr.h:48:5 (clickhouse+0x194fb42d) E #6 std::__1::__shared_ptr_pointer, std::__1::allocator>::__on_zero_shared() build_docker/./contrib/llvm-project/libcxx/include/__memory/shared_ptr.h:263:5 (clickhouse+0x194fb42d) E #7 std::__1::__shared_count::__release_shared[abi:v15000]() build_docker/./contrib/llvm-project/libcxx/include/__memory/shared_ptr.h:174:9 (clickhouse+0x194fade0) (BuildId: 706d92b17db171493f293d517643f726ee1b7b1e) E #8 std::__1::__shared_weak_count::__release_shared[abi:v15000]() build_docker/./contrib/llvm-project/libcxx/include/__memory/shared_ptr.h:215:27 (clickhouse+0x194fade0) E #9 std::__1::shared_ptr::~shared_ptr[abi:v15000]() build_docker/./contrib/llvm-project/libcxx/include/__memory/shared_ptr.h:702:23 (clickhouse+0x194fade0) E #10 std::__1::shared_ptr::operator=[abi:v15000](std::__1::shared_ptr&&) build_docker/./contrib/llvm-project/libcxx/include/__memory/shared_ptr.h:723:9 (clickhouse+0x194fade0) E #11 MultiVersion::set(std::__1::unique_ptr>&&) build_docker/./src/Common/MultiVersion.h:76:25 (clickhouse+0x194fade0) E #12 DB::CertificateReloader::tryLoad(Poco::Util::AbstractConfiguration const&) build_docker/./src/Server/CertificateReloader.cpp:83:18 (clickhouse+0x194f94ca) (BuildId: 706d92b17db171493f293d517643f726ee1b7b1e) E #13 DB::Server::main(std::__1::vector, std::__1::allocator>, std::__1::allocator, std::__1::allocator>>> const&)::$_6::operator()(Poco::AutoPtr, bool) const build_docker/./programs/server/Server.cpp:1546:45 (clickhouse+0xf384df7) (BuildId: 706d92b17db171493f293d517643f726ee1b7b1e) E #14 decltype(std::declval, std::__1::allocator>, std::__1::allocator, std::__1::allocator>>> const&)::$_6&>()(std::declval>(), std::declval())) std::__1::__invoke[abi:v15000], std::__1::allocator>, std::__1::allocator, std::__1::allocator>>> const&)::$_6&, Poco::AutoPtr, bool>(DB::Server::main(std::__1::vector, std::__1::allocator>, std::__1::allocator, std::__1::allocator>>> const&)::$_6&, Poco::AutoPtr&&, bool&&) build_docker/./contrib/llvm-project/libcxx/include/__functional/invoke.h:394:23 (clickhouse+0xf3827a9) (BuildId: 706d92b17db171493f293d517643f726ee1b7b1e) E #15 void std::__1::__invoke_void_return_wrapper::__call, std::__1::allocator>, std::__1::allocator, std::__1::allocator>>> const&)::$_6&, Poco::AutoPtr, bool>(DB::Server::main(std::__1::vector, std::__1::allocator>, std::__1::allocator, std::__1::allocator>>> const&)::$_6&, Poco::AutoPtr&&, bool&&) build_docker/./contrib/llvm-project/libcxx/include/__functional/invoke.h:479:9 (clickhouse+0xf3827a9) E #16 std::__1::__function::__default_alloc_func, std::__1::allocator>, std::__1::allocator, std::__1::allocator>>> const&)::$_6, void (Poco::AutoPtr, bool)>::operator()[abi:v15000](Poco::AutoPtr&&, bool&&) build_docker/./contrib/llvm-project/libcxx/include/__functional/function.h:235:12 (clickhouse+0xf3827a9) E #17 void std::__1::__function::__policy_invoker, bool)>::__call_impl, std::__1::allocator>, std::__1::allocator, std::__1::allocator>>> const&)::$_6, void (Poco::AutoPtr, bool)>>(std::__1::__function::__policy_storage const*, Poco::AutoPtr&&, bool) build_docker/./contrib/llvm-project/libcxx/include/__functional/function.h:716:16 (clickhouse+0xf3827a9) E #18 std::__1::__function::__policy_func, bool)>::operator()[abi:v15000](Poco::AutoPtr&&, bool&&) const build_docker/./contrib/llvm-project/libcxx/include/__functional/function.h:848:16 (clickhouse+0x19fd2cbe) (BuildId: 706d92b17db171493f293d517643f726ee1b7b1e) E #19 std::__1::function, bool)>::operator()(Poco::AutoPtr, bool) const build_docker/./contrib/llvm-project/libcxx/include/__functional/function.h:1187:12 (clickhouse+0x19fd2cbe) E #20 DB::ConfigReloader::reloadIfNewer(bool, bool, bool, bool) build_docker/./src/Common/Config/ConfigReloader.cpp:150:13 (clickhouse+0x19fd2cbe) E #21 DB::ConfigReloader::reload() build_docker/./src/Common/Config/ConfigReloader.h:51:21 (clickhouse+0xf38767c) (BuildId: 706d92b17db171493f293d517643f726ee1b7b1e) E #22 DB::Server::main(std::__1::vector, std::__1::allocator>, std::__1::allocator, std::__1::allocator>>> const&)::$_13::operator()() const build_docker/./programs/server/Server.cpp:1731:31 (clickhouse+0xf38767c) E #23 decltype(std::declval, std::__1::allocator>, std::__1::allocator, std::__1::allocator>>> const&)::$_13&>()()) std::__1::__invoke[abi:v15000], std::__1::allocator>, std::__1::allocator, std::__1::allocator>>> const&)::$_13&>(DB::Server::main(std::__1::vector, std::__1::allocator>, std::__1::allocator, std::__1::allocator>>> const&)::$_13&) build_docker/./contrib/llvm-project/libcxx/include/__functional/invoke.h:394:23 (clickhouse+0xf38767c) E #24 void std::__1::__invoke_void_return_wrapper::__call, std::__1::allocator>, std::__1::allocator, std::__1::allocator>>> const&)::$_13&>(DB::Server::main(std::__1::vector, std::__1::allocator>, std::__1::allocator, std::__1::allocator>>> const&)::$_13&) build_docker/./contrib/llvm-project/libcxx/include/__functional/invoke.h:479:9 (clickhouse+0xf38767c) E #25 std::__1::__function::__default_alloc_func, std::__1::allocator>, std::__1::allocator, std::__1::allocator>>> const&)::$_13, void ()>::operator()[abi:v15000]() build_docker/./contrib/llvm-project/libcxx/include/__functional/function.h:235:12 (clickhouse+0xf38767c) E #26 void std::__1::__function::__policy_invoker::__call_impl, std::__1::allocator>, std::__1::allocator, std::__1::allocator>>> const&)::$_13, void ()>>(std::__1::__function::__policy_storage const*) build_docker/./contrib/llvm-project/libcxx/include/__functional/function.h:716:16 (clickhouse+0xf38767c) E #27 std::__1::__function::__policy_func::operator()[abi:v15000]() const build_docker/./contrib/llvm-project/libcxx/include/__functional/function.h:848:16 (clickhouse+0x16907aa0) (BuildId: 706d92b17db171493f293d517643f726ee1b7b1e) E #28 std::__1::function::operator()() const build_docker/./contrib/llvm-project/libcxx/include/__functional/function.h:1187:12 (clickhouse+0x16907aa0) E #29 DB::Context::reloadConfig() const build_docker/./src/Interpreters/Context.cpp:4357:5 (clickhouse+0x16907aa0) E #30 DB::InterpreterSystemQuery::execute() build_docker/./src/Interpreters/InterpreterSystemQuery.cpp:577:29 (clickhouse+0x17e78c19) (BuildId: 706d92b17db171493f293d517643f726ee1b7b1e) E #31 DB::executeQueryImpl(char const*, char const*, std::__1::shared_ptr, DB::QueryFlags, DB::QueryProcessingStage::Enum, DB::ReadBuffer*) build_docker/./src/Interpreters/executeQuery.cpp:1195:40 (clickhouse+0x17e3e462) (BuildId: 706d92b17db171493f293d517643f726ee1b7b1e) E #32 DB::executeQuery(std::__1::basic_string, std::__1::allocator> const&, std::__1::shared_ptr, DB::QueryFlags, DB::QueryProcessingStage::Enum) build_docker/./src/Interpreters/executeQuery.cpp:1374:26 (clickhouse+0x17e39837) (BuildId: 706d92b17db171493f293d517643f726ee1b7b1e) E #33 DB::TCPHandler::runImpl() build_docker/./src/Server/TCPHandler.cpp:518:54 (clickhouse+0x195cc651) (BuildId: 706d92b17db171493f293d517643f726ee1b7b1e) E #34 DB::TCPHandler::run() build_docker/./src/Server/TCPHandler.cpp:2329:9 (clickhouse+0x195e8707) (BuildId: 706d92b17db171493f293d517643f726ee1b7b1e) E #35 Poco::Net::TCPServerConnection::start() build_docker/./base/poco/Net/src/TCPServerConnection.cpp:43:3 (clickhouse+0x1d00d942) (BuildId: 706d92b17db171493f293d517643f726ee1b7b1e) E #36 Poco::Net::TCPServerDispatcher::run() build_docker/./base/poco/Net/src/TCPServerDispatcher.cpp:115:20 (clickhouse+0x1d00e1b1) (BuildId: 706d92b17db171493f293d517643f726ee1b7b1e) E #37 Poco::PooledThread::run() build_docker/./base/poco/Foundation/src/ThreadPool.cpp:188:14 (clickhouse+0x1d20f2e6) (BuildId: 706d92b17db171493f293d517643f726ee1b7b1e) E #38 Poco::(anonymous namespace)::RunnableHolder::run() build_docker/./base/poco/Foundation/src/Thread.cpp:45:11 (clickhouse+0x1d20d5af) (BuildId: 706d92b17db171493f293d517643f726ee1b7b1e) E #39 Poco::ThreadImpl::runnableEntry(void*) build_docker/./base/poco/Foundation/src/Thread_POSIX.cpp:335:27 (clickhouse+0x1d20ba69) (BuildId: 706d92b17db171493f293d517643f726ee1b7b1e) E E Previous atomic write of size 4 at 0x7b2800025d30 by thread T3 (mutexes: write M2): E #0 CRYPTO_DOWN_REF build_docker/./contrib/openssl/include/internal/refcount.h:51:12 (clickhouse+0x2012a6e6) (BuildId: 706d92b17db171493f293d517643f726ee1b7b1e) E #1 EVP_PKEY_free build_docker/./contrib/openssl/crypto/evp/p_lib.c:1795:5 (clickhouse+0x2012a6e6) E #2 ssl_cert_clear_certs build_docker/./contrib/openssl/ssl/ssl_cert.c:246:9 (clickhouse+0x1ffafd37) (BuildId: 706d92b17db171493f293d517643f726ee1b7b1e) E #3 ssl_cert_free build_docker/./contrib/openssl/ssl/ssl_cert.c:277:5 (clickhouse+0x1ffafd37) E #4 ossl_ssl_connection_free build_docker/./contrib/openssl/ssl/ssl_lib.c:1458:5 (clickhouse+0x1ffba6af) (BuildId: 706d92b17db171493f293d517643f726ee1b7b1e) E #5 SSL_free build_docker/./contrib/openssl/ssl/ssl_lib.c:1417:9 (clickhouse+0x1ffb920e) (BuildId: 706d92b17db171493f293d517643f726ee1b7b1e) E #6 Poco::Net::SecureSocketImpl::reset() build_docker/./base/poco/NetSSL_OpenSSL/src/SecureSocketImpl.cpp:583:3 (clickhouse+0x1cfaac60) (BuildId: 706d92b17db171493f293d517643f726ee1b7b1e) E #7 Poco::Net::SecureSocketImpl::~SecureSocketImpl() build_docker/./base/poco/NetSSL_OpenSSL/src/SecureSocketImpl.cpp:80:3 (clickhouse+0x1cfaac60) E #8 Poco::Net::SecureStreamSocketImpl::~SecureStreamSocketImpl() build_docker/./base/poco/NetSSL_OpenSSL/src/SecureStreamSocketImpl.cpp:52:1 (clickhouse+0x1cfb15dd) (BuildId: 706d92b17db171493f293d517643f726ee1b7b1e) E #9 Poco::Net::SecureStreamSocketImpl::~SecureStreamSocketImpl() build_docker/./base/poco/NetSSL_OpenSSL/src/SecureStreamSocketImpl.cpp:43:1 (clickhouse+0x1cfb15dd) E #10 Poco::RefCountedObject::release() const build_docker/./base/poco/Foundation/include/Poco/RefCountedObject.h:86:13 (clickhouse+0x1cffc81e) (BuildId: 706d92b17db171493f293d517643f726ee1b7b1e) E #11 Poco::Net::Socket::~Socket() build_docker/./base/poco/Net/src/Socket.cpp:68:10 (clickhouse+0x1cffc81e) E #12 Poco::Net::StreamSocket::~StreamSocket() build_docker/./base/poco/Net/src/StreamSocket.cpp:63:1 (clickhouse+0x1d009c39) (BuildId: 706d92b17db171493f293d517643f726ee1b7b1e) E #13 Poco::Net::TCPConnectionNotification::~TCPConnectionNotification() build_docker/./base/poco/Net/src/TCPServerDispatcher.cpp:43:2 (clickhouse+0x1d00ef50) (BuildId: 706d92b17db171493f293d517643f726ee1b7b1e) E #14 Poco::Net::TCPConnectionNotification::~TCPConnectionNotification() build_docker/./base/poco/Net/src/TCPServerDispatcher.cpp:42:2 (clickhouse+0x1d00ef50) E #15 Poco::RefCountedObject::release() const build_docker/./base/poco/Foundation/include/Poco/RefCountedObject.h:86:13 (clickhouse+0x1d00e203) (BuildId: 706d92b17db171493f293d517643f726ee1b7b1e) E #16 Poco::AutoPtr::~AutoPtr() build_docker/./base/poco/Foundation/include/Poco/AutoPtr.h:91:19 (clickhouse+0x1d00e203) E #17 Poco::Net::TCPServerDispatcher::run() build_docker/./base/poco/Net/src/TCPServerDispatcher.cpp:122:3 (clickhouse+0x1d00e203) E #18 Poco::PooledThread::run() build_docker/./base/poco/Foundation/src/ThreadPool.cpp:188:14 (clickhouse+0x1d20f2e6) (BuildId: 706d92b17db171493f293d517643f726ee1b7b1e) E #19 Poco::(anonymous namespace)::RunnableHolder::run() build_docker/./base/poco/Foundation/src/Thread.cpp:45:11 (clickhouse+0x1d20d5af) (BuildId: 706d92b17db171493f293d517643f726ee1b7b1e) E #20 Poco::ThreadImpl::runnableEntry(void*) build_docker/./base/poco/Foundation/src/Thread_POSIX.cpp:335:27 (clickhouse+0x1d20ba69) (BuildId: 706d92b17db171493f293d517643f726ee1b7b1e) (cherry picked from commit 5b50434b7180034ff7b427cf76838098673e9232) (cherry picked from commit fb58a0f16aaa9a4e721056746f5ce2c6e5071cb6) (cherry picked from commit aa7664d0503e3cabea3f4000831be63068ded591) --- include/internal/refcount.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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 From 5ebf903b910e9efeb9949a9ed08d4dbe9ffb6ffa Mon Sep 17 00:00:00 2001 From: Robert Schulze Date: Tue, 11 Jun 2024 19:25:32 +0000 Subject: [PATCH 06/21] Suppress leaky memory warning During my testing (*), I got below stack about leaked memory. - at database startup, Azure is registered as an object storage (--> registerAzureObjectStorage) - this calls into Azure, then into curl, and then into OpenSSL - curl asks OpenSSL for a bunch of random numbers (--> function 'ossl_random' in curl's OpenSSL wrapper) - OpenSSL initializes the random number generator and stores it in some random number generator context object (--> *RAND_get0_public) - this object is registered via pthread_key_create and pthread_setspecific registered in TLS - if registerAzureObjectStorage was the only place which initializes the RNG, we could argue that the leaked memory does not matter anyways as it is released after shutdown - RAND_get0_public also registers a free handler (rand_delete_thread_state) that runs in TLS, so the memory is released also if registration is called from arbitrary other threads. In sum: this is a false positive. (*) https://github.com/ClickHouse/clickhouse-private/issues/10107#issuecomment-2161282313 ``` ================================================================= ==2757181==ERROR: LeakSanitizer: detected memory leaks Direct leak of 48 byte(s) in 1 object(s) allocated from: #0 0x5672f4a48083 in malloc (/data/ch4/build_asan/programs/clickhouse+0xa2b7083) (BuildId: 4d868624feaa8b5598d9d0b327749c877bfb6cfd) #1 0x567321d05c7e in CRYPTO_malloc build_asan/./contrib/openssl/crypto/mem.c:202:11 #2 0x567321d05c7e in CRYPTO_zalloc build_asan/./contrib/openssl/crypto/mem.c:222:11 #3 0x567321d5f9cf in EVP_RAND_CTX_new build_asan/./contrib/openssl/crypto/evp/evp_rand.c:353:11 #4 0x567321d627db in rand_new_drbg build_asan/./contrib/openssl/crypto/rand/rand_lib.c:658:11 #5 0x567321d61e7b in RAND_get0_public build_asan/./contrib/openssl/crypto/rand/rand_lib.c:777:16 #6 0x567321d61d4f in RAND_bytes_ex build_asan/./contrib/openssl/crypto/rand/rand_lib.c:378:12 #7 0x56731cfe9267 in ossl_random openssl.c #8 0x56731cf534ae in Curl_rand (/data/ch4/build_asan/programs/clickhouse+0x327c24ae) (BuildId: 4d868624feaa8b5598d9d0b327749c877bfb6cfd) #9 0x56731cf757dc in Curl_socketpair (/data/ch4/build_asan/programs/clickhouse+0x327e47dc) (BuildId: 4d868624feaa8b5598d9d0b327749c877bfb6cfd) #10 0x56731cf40bfe in Curl_multi_handle (/data/ch4/build_asan/programs/clickhouse+0x327afbfe) (BuildId: 4d868624feaa8b5598d9d0b327749c877bfb6cfd) #11 0x56731cecb260 in curl_easy_perform (/data/ch4/build_asan/programs/clickhouse+0x3273a260) (BuildId: 4d868624feaa8b5598d9d0b327749c877bfb6cfd) #12 0x56731cd4a351 in Azure::Core::Http::CurlConnection::CurlConnection(Azure::Core::Http::Request&, Azure::Core::Http::CurlTransportOptions const&, std::__1::basic_string, std::__1::allocator> const&, std::__1::basic_string, std::__1::allocator> const&) build_asan/./contrib/azure/sdk/core/azure-core/src/http/curl/curl.cpp:2441:24 #13 0x56731cd3176b in std::__1::__unique_if::__unique_single std::__1::make_unique[abi:v15000], std::__1::allocator> const&, std::__1::basic_string, std::__1::allocator> const&>(Azure::Core::Http::Request&, Azure::Core::H ttp::CurlTransportOptions const&, std::__1::basic_string, std::__1::allocator> const&, std::__1::basic_string, std::__1::allocator> const&) build_asan/./contrib/llvm-project/libcxx/include/__memory/unique_ptr.h:714:32 #14 0x56731cd3176b in Azure::Core::Http::_detail::CurlConnectionPool::ExtractOrCreateCurlConnection(Azure::Core::Http::Request&, Azure::Core::Http::CurlTransportOptions const&, bool) build_asan/./contrib/azure/sdk/core/azure-core/src/http/curl/curl.cpp:2126:10 #15 0x56731cd2f891 in Azure::Core::Http::CurlTransport::Send(Azure::Core::Http::Request&, Azure::Core::Context const&) build_asan/./contrib/azure/sdk/core/azure-core/src/http/curl/curl.cpp:351:48 #16 0x56731cdd66db in Azure::Core::Http::Policies::_internal::TransportPolicy::Send(Azure::Core::Http::Request&, Azure::Core::Http::Policies::NextHttpPolicy, Azure::Core::Context const&) const build_asan/./contrib/azure/sdk/core/azure-core/src/http/transport_policy.cpp:121:40 #17 0x56731cd7c75b in Azure::Core::Http::Policies::_internal::LogPolicy::Send(Azure::Core::Http::Request&, Azure::Core::Http::Policies::NextHttpPolicy, Azure::Core::Context const&) const build_asan/./contrib/azure/sdk/core/azure-core/src/http/log_policy.cpp:114:23 #18 0x56731cdd0218 in Azure::Core::Http::Policies::_internal::RequestActivityPolicy::Send(Azure::Core::Http::Request&, Azure::Core::Http::Policies::NextHttpPolicy, Azure::Core::Context const&) const build_asan/./contrib/azure/sdk/core/azure-core/src/http/request_activity_policy.cpp:110:23 #19 0x56731cec5f18 in Azure::Storage::_internal::StoragePerRetryPolicy::Send(Azure::Core::Http::Request&, Azure::Core::Http::Policies::NextHttpPolicy, Azure::Core::Context const&) const build_asan/./contrib/azure/sdk/storage/azure-storage-common/src/storage_per_retry_policy.cpp:57:23 #20 0x56731cec6d0b in Azure::Storage::_internal::StorageSwitchToSecondaryPolicy::Send(Azure::Core::Http::Request&, Azure::Core::Http::Policies::NextHttpPolicy, Azure::Core::Context const&) const build_asan/./contrib/azure/sdk/storage/azure-storage-common/src/storage_switch_to_secondary_policy.cpp:36:32 #21 0x56731cdcd019 in Azure::Core::Http::Policies::_internal::RetryPolicy::Send(Azure::Core::Http::Request&, Azure::Core::Http::Policies::NextHttpPolicy, Azure::Core::Context const&) const build_asan/./contrib/azure/sdk/core/azure-core/src/http/retry_policy.cpp:146:34 #22 0x56731cdd6e17 in Azure::Core::Http::Policies::_internal::TelemetryPolicy::Send(Azure::Core::Http::Request&, Azure::Core::Http::Policies::NextHttpPolicy, Azure::Core::Context const&) const build_asan/./contrib/azure/sdk/core/azure-core/src/http/telemetry_policy.cpp:23:21 #23 0x56731cdb0ef5 in Azure::Core::Http::Policies::_internal::RequestIdPolicy::Send(Azure::Core::Http::Request&, Azure::Core::Http::Policies::NextHttpPolicy, Azure::Core::Context const&) const build_asan/./contrib/azure/sdk/core/azure-core/inc/azure/core/http/policies/policy.hpp:453:27 #24 0x56731cdfc53e in Azure::Storage::_internal::StorageServiceVersionPolicy::Send(Azure::Core::Http::Request&, Azure::Core::Http::Policies::NextHttpPolicy, Azure::Core::Context const&) const build_asan/./contrib/azure/sdk/storage/azure-storage-common/inc/azure/storage/common/internal/storage_service_version_policy.hpp:34:25 #25 0x56731ce4bff1 in Azure::Core::Http::_internal::HttpPipeline::Send(Azure::Core::Http::Request&, Azure::Core::Context const&) const build_asan/./contrib/azure/sdk/core/azure-core/inc/azure/core/internal/http/pipeline.hpp:230:29 #26 0x56731ce4bff1 in Azure::Storage::Blobs::_detail::BlobContainerClient::Create(Azure::Core::Http::_internal::HttpPipeline&, Azure::Core::Url const&, Azure::Storage::Blobs::_detail::BlobContainerClient::CreateBlobContainerOptions const&, Azure::Core::Context const&) build_asan/./contrib/azure/sdk/storage/azure-storage-blobs/src/rest_client.cpp:1415:36 #27 0x56731ce0bdd8 in Azure::Storage::Blobs::BlobContainerClient::Create(Azure::Storage::Blobs::CreateBlobContainerOptions const&, Azure::Core::Context const&) const build_asan/./contrib/azure/sdk/storage/azure-storage-blobs/src/blob_container_client.cpp:258:12 #28 0x56731ce396dc in Azure::Storage::Blobs::BlobServiceClient::CreateBlobContainer(std::__1::basic_string, std::__1::allocator> const&, Azure::Storage::Blobs::CreateBlobContainerOptions const&, Azure::Core::Context const&) const build_asan/./contrib/azure/sdk/storage/azure-storage-blobs/src/blob_service_client.cpp:264:41 #29 0x56731018fcff in DB::getAzureBlobContainerClient(Poco::Util::AbstractConfiguration const&, std::__1::basic_string, std::__1::allocator> const&) build_asan/./src/Disks/ObjectStorages/AzureBlobStorage/AzureBlobStorageAuth.cpp:236:75 #30 0x5673115a7352 in DB::registerAzureObjectStorage(DB::ObjectStorageFactory&)::$_0::operator()(std::__1::basic_string, std::__1::allocator> const&, Poco::Util::AbstractConfiguration const&, std::__1::basic_string, std::__1::allocator> const&, std::__1::shared_ptr const&, bool) const build_asan/./src/Disks/ObjectStorages/ObjectStorageFactory.cpp:315:13 #31 0x5673115a7352 in decltype(std::declval()(std::declval, std::__1::allocator> const&>(), std::declval(), std::declval, std::__1::allocator> const&>(), std::declval const&>(), std::declval() )) std::__1::__invoke[abi:v15000], std::__1::allocator> const&, Poco::Util::AbstractConfiguration const&, std::__1::basic_string, std::__1::allocator> const&, std::__1::shared_ptr const&, bool>(DB::registerAzureObjectStorage(DB::ObjectStorageFactory&)::$_0&, std::__1::basic_string, std::__1::allocator> const&, Poco::Util::AbstractConfiguration const&, std::__1::basic_string, std::__1::allocator> const&, std::__1::shared_ptr const&, bool&&) build_asan/./contrib/llvm-project/libcxx/include/__functional/invoke.h:394:23 #32 0x5673115a7352 in std::__1::shared_ptr std::__1::__invoke_void_return_wrapper, false>::__call, std::__1::allocator> const&, Poco::Util::AbstractConfiguration const&, std::__1::basic_string, std::__1::allocator> const&, std::__1::shared_pt r const&, bool>(DB::registerAzureObjectStorage(DB::ObjectStorageFactory&)::$_0&, std::__1::basic_string, std::__1::allocator> const&, Poco::Util::AbstractConfiguration const&, std::__1::basic_string, std::__1::allocator> const&, std::__1::shared_ptr const&, bool&&) build_asan/./contrib/llvm-project/libcxx/include/__functional/invoke.h:470:16 #33 0x5673115a7352 in std::__1::__function::__default_alloc_func (std::__1::basic_string, std::__1::allocator> const&, Poco::Util::AbstractConfiguration const&, std::__1::basic_string, std::__1::allocator> const&, std::__1::shared_ptr const&, bool)>::operator()[abi:v 15000](std::__1::basic_string, std::__1::allocator> const&, Poco::Util::AbstractConfiguration const&, std::__1::basic_string, std::__1::allocator> const&, std::__1::shared_ptr const&, bool&&) build_asan/./contrib/llvm-project/libcxx/include/__functional/function.h:235:12 #34 0x5673115a7352 in std::__1::shared_ptr std::__1::__function::__policy_invoker (std::__1::basic_string, std::__1::allocator> const&, Poco::Util::AbstractConfiguration const&, std::__1::basic_string, std::__1::allocator> const&, std::__1::shared_ptr const&, bool)>::__call_impl (std::__1::basic_string, std::__1::allocator> const&, Poco::Util::AbstractConfiguration const&, std::__1::basic_string, std::__1::allocator> const&, std::__1::shared_ptr const&, bool)>>(std::__1::__function::__policy_storage const*, std::__1::basic_string, std::__1::allocator> const&, Poco::Util::AbstractConfiguration const&, std::__1::basic_string, std::__1::allocator> const&, std::__1::shared_ptr const&, bool) build_asan/./contrib/llvm-project/libcxx/include/__functional/function.h:716:16 #35 0x5673115a0780 in std::__1::__function::__policy_func (std::__1::basic_string, std::__1::allocator> const&, Poco::Util::AbstractConfiguration const&, std::__1::basic_string, std::__1::allocator> const&, std::__1::shared_ptr const&, bool)>::operator()[abi:v15000](std::__1::basic_string, std::_ _1::allocator> const&, Poco::Util::AbstractConfiguration const&, std::__1::basic_string, std::__1::allocator> const&, std::__1::shared_ptr const&, bool&&) const build_asan/./contrib/llvm-project/libcxx/include/__functional/function.h:848:16 #36 0x5673115a0780 in std::__1::function (std::__1::basic_string, std::__1::allocator> const&, Poco::Util::AbstractConfiguration const&, std::__1::basic_string, std::__1::allocator> const&, std::__1::shared_ptr const&, bool)>::operator()(std::__1::basic_string, std::__1::allocator> const&, Poco::Util::AbstractConfiguration const&, std::__1::basic_string, std::__1::allocator> const&, std::__1::shared_ptr const&, bool) const build_asan/./contrib/llvm-project/libcxx/include/__functional/function.h:1187:12 #37 0x5673115a0780 in DB::ObjectStorageFactory::create(std::__1::basic_string, std::__1::allocator> const&, Poco::Util::AbstractConfiguration const&, std::__1::basic_string, std::__1::allocator> const&, std::__1::shared_ptr const&, bool) const build_asan/./src/Disks/ObjectStorages/ObjectStorageFactory.cpp:135:12 #38 0x56731159ef45 in DB::registerDiskObjectStorage(DB::DiskFactory&, bool)::$_0::operator()(std::__1::basic_string, std::__1::allocator> const&, Poco::Util::AbstractConfiguration const&, std::__1::basic_string, std::__1::allocator> const&, std::__1::shared_ptr, std::__1::map, std::__1::allocator>, std::__1: :shared_ptr, std::__1::less, std::__1::allocator>>, std::__1::allocator, std::__1::allocator> const, std::__1::shared_ptr>>> const&, bool, bool) const build_asan/./src/Disks/ObjectStorages/RegisterDiskObjectStorage.cpp:27:64 #39 0x56731159ef45 in decltype(std::declval()(std::declval, std::__1::allocator> const&>(), std::declval(), std::declval, std::__1::allocator> const&>(), std::declval>(), std::declval, std::__1::allocator>, std::__1::shared_ptr, std::__1::less, std::__1::allocator>>, std::__1::allocator, std::__1::allocator> const, std::__1::shared_ptr>>> const&>(), std::declval(), std::declval())) std::__1::__invoke[abi:v15000], std::__1::allocator> const&, Poco::Util::AbstractConfiguration const&, std::__1::basic_string, std::__1::allocator> const&, std::__1::shared_ptr, std::__1::map, std::__1::allocator>, std::__1::shared_ptr, std::__1::less, std::__1::allocator>>, std::__1::allocator, std::__1::allocator> const, std::__1::shared_ptr>>> const&, bool, bool>(DB::registerDiskObjectStorage(DB::DiskFactory&, bool)::$_0&, std::__1::basic_string, std::__1::allocator> const&, Poco::Util::AbstractConfiguration const&, std::__1::basic_ string, std::__1::allocator> const&, std::__1::shared_ptr&&, std::__1::map, std::__1::allocator>, std::__1::shared_ptr, std::__1::less, std::__1::allocator>>, std::__1::allocator, std::__1::allocator> const, std::__1::shared_ptr>>> const&, bool&&, bool&&) build_asan/./contrib/llvm-project/libcxx/include/__functional/invoke.h:394:23 #40 0x56731159ef45 in std::__1::shared_ptr std::__1::__invoke_void_return_wrapper, false>::__call, std::__1::allocator> const&, Poco::Util::AbstractConfiguration const&, std::__1::basic_string, std::__1::allocator> const&, std::__1::shared_ptr, std::__1::map, std::__1::allocator>, std::__1::shared_ptr, std::__1::less, std::__1::allocator>>, std::__1::allocator, std::__1::allocator> const, std::__1::shared_ptr>>> const&, bool, bool>(DB::registerDiskObjectStorage(DB::DiskFactory&, bool)::$_0 &, std::__1::basic_string, std::__1::allocator> const&, Poco::Util::AbstractConfiguration const&, std::__1::basic_string, std::__1::allocator> const&, std::__1::shared_ptr&&, std::__1::map, std::__1::allocator>, std::__1::shared_ptr, std::__1::less, std::__1::allocator>>, std::__1::allocator, std::__1::allocator> const, std::__1::shared_ptr>>> const&, bool&&, bool&&) build_asan/./contrib/llvm-project/libcxx/include/__functional/invoke.h:470:16 #41 0x56731159ef45 in std::__1::__function::__default_alloc_func (std::__1::basic_string, std::__1::allocator> const&, Poco::Util::AbstractConfiguration const&, std::__1::basic_string, std::__1::allocator> const&, std::__1::shared_ptr, std::__1::map, std::__1::allocator>, std::__1::shared_ptr, std::__1::less, std::__1::allocator>>, std::__1::allocator, std::__1::allocator> const, std::__1::shared_ptr>>> const&, bool, bool)>::operator()[abi:v15000](std::__1::basic_string, std::__1::allocator > const&, Poco::Util::AbstractConfiguration const&, std::__1::basic_string, std::__1::allocator> const&, std::__1::shared_ptr&&, std::__1::map, std::__1::allocator>, std::__1::shared_ptr, std::__1::less, std::__1::allocator>>, std::__1::allocator, std::__1::allocator> const, std::__1::shared_ptr>>> const&, bool&&, bool&&) build_asan/./contrib/llvm-project/libcxx/include/__functional/function.h:235:12 ``` (cherry picked from commit 8eaedf35d2f9077f1ead2d89478812ba437d860b) (cherry picked from commit bd256c6e464bd42038fb992cbafcd4952748ac8e) (cherry picked from commit 6ed82ef911f00e0316b860fe54be2ce368a6f9c6) --- crypto/rand/rand_lib.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/crypto/rand/rand_lib.c b/crypto/rand/rand_lib.c index df70d1c2b1dad..3f0d6585f46e9 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; From 47cfd6a56dd0b98f580c91fad2d4d5c92f8b0642 Mon Sep 17 00:00:00 2001 From: Robert Schulze Date: Tue, 11 Jun 2024 20:59:59 +0000 Subject: [PATCH 07/21] Suppress leaky memory warning in OpenSSL's error handling The leak was reported in https://github.com/ClickHouse/ClickHouse/issues/63792 - a SSL session is established - somewhere down in OpenSSL, the per-thread error queue is clear'ed - when no error queue exists (e.g. because the thread was just spawned), a new error queue is allocated - the error queue is stored in TLS, and a free handler is registered which deletes it when the thread ends - Leak sanitizer does not understand ^^ (false positive) ``` Direct leak of 9944 byte(s) in 11 object(s) allocated from: #0 0x5571bf7e04cf in malloc (/usr/bin/clickhouse+0xa39a4cf) (BuildId: 06c4931100f632dde9d0ecd3cf6a67776742e29b) #1 0x5571ecbc06be in CRYPTO_malloc build_docker/./contrib/openssl/crypto/mem.c:202:11 #2 0x5571ecbc06be in CRYPTO_zalloc build_docker/./contrib/openssl/crypto/mem.c:222:11 #3 0x5571ecb03c6a in ossl_err_get_state_int build_docker/./contrib/openssl/crypto/err/err.c:691:17 #4 0x5571ecb037b5 in ERR_clear_error build_docker/./contrib/openssl/crypto/err/err.c:339:10 #5 0x5571ec9cf0d0 in state_machine build_docker/./contrib/openssl/ssl/statem/statem.c:366:5 #6 0x5571ec90c7d4 in SSL_do_handshake build_docker/./contrib/openssl/ssl/ssl_lib.c:4746:19 #7 0x5571e70cc501 in Poco::Net::SecureSocketImpl::connectSSL(bool) build_docker/./base/poco/NetSSL_OpenSSL/src/SecureSocketImpl.cpp:206:11 #8 0x5571e70cce6b in Poco::Net::SecureSocketImpl::connect(Poco::Net::SocketAddress const&, Poco::Timespan const&, bool) build_docker/./base/poco/NetSSL_OpenSSL/src/SecureSocketImpl.cpp:149:2 #9 0x5571e70d81f7 in Poco::Net::SecureStreamSocketImpl::connect(Poco::Net::SocketAddress const&, Poco::Timespan const&) build_docker/./base/poco/NetSSL_OpenSSL/src/SecureStreamSocketImpl.cpp:87:8 #10 0x5571e0ca708a in DB::Connection::connect(DB::ConnectionTimeouts const&) build_docker/./src/Client/Connection.cpp:158:29 ``` (cherry picked from commit 9e7862cea9fde8af56859f38714bba979a1de75d) (cherry picked from commit 737ce357c58a60a11ed8858292a59356e30baa07) (cherry picked from commit e8217c6f88d90457a63a8ca70128cf244fc7d6af) --- crypto/err/err.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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; From cb6587264590eba5e932127c66841936a62cd4f5 Mon Sep 17 00:00:00 2001 From: Robert Schulze Date: Tue, 18 Jun 2024 19:59:52 +0000 Subject: [PATCH 08/21] Suppress leaksan false positive (cherry picked from commit b50f55132d05d51e92a9bcd1e0f7698c831703f0) (cherry picked from commit b67a86667c968c4248c068d61771ef2d9d441fc8) (cherry picked from commit 52369e7604f4d43f2c2f4e720477d6919d144ee3) --- crypto/engine/eng_lib.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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; From ffe8be8de7686afaf2b4f1148978b227d41eba76 Mon Sep 17 00:00:00 2001 From: Robert Schulze Date: Sun, 30 Jun 2024 14:35:04 +0000 Subject: [PATCH 09/21] Suppress leaky memory warning, pt. II ClickHouse issue https://github.com/ClickHouse/clickhouse-private/issues/10107#issuecomment-2189755698 reports this memory leak: (note how it is very similar to https://github.com/ClickHouse/clickhouse-private/issues/10107#issue-2314912657) ``` Direct leak of 528 byte(s) in 11 object(s) allocated from: #0 0x561369af24cf in malloc (/usr/bin/clickhouse+0xa6cf4cf) (BuildId: 22880fad595a96b17eb9add20e7a01f8ded54c49) #1 0x561397b86a7e in CRYPTO_malloc build_docker/./contrib/openssl/crypto/mem.c:202:11 #2 0x561397b86a7e in CRYPTO_zalloc build_docker/./contrib/openssl/crypto/mem.c:222:11 #3 0x561397be07cf in EVP_RAND_CTX_new build_docker/./contrib/openssl/crypto/evp/evp_rand.c:353:11 #4 0x561397be35fb in rand_new_drbg build_docker/./contrib/openssl/crypto/rand/rand_lib.c:665:11 #5 0x561397be2a9b in RAND_get0_private build_docker/./contrib/openssl/crypto/rand/rand_lib.c:827:16 #6 0x561397be296f in RAND_priv_bytes_ex build_docker/./contrib/openssl/crypto/rand/rand_lib.c:356:12 #7 0x5613978d7cd8 in SSL_CTX_new_ex build_docker/./contrib/openssl/ssl/ssl_lib.c:4016:13 #8 0x561392d1eabd in ossl_connect_common openssl.c #9 0x561392d11e23 in ssl_cf_connect vtls.c #10 0x561392c13a1f in cf_setup_connect connect.c #11 0x561392c1b0e5 in cf_hc_connect cf-https-connect.c #12 0x561392c0a332 in Curl_conn_connect (/usr/bin/clickhouse+0x337e7332) (BuildId: 22880fad595a96b17eb9add20e7a01f8ded54c49) #13 0x561392c7a28a in multi_runsingle multi.c #14 0x561392c78f6d in curl_multi_perform (/usr/bin/clickhouse+0x33855f6d) (BuildId: 22880fad595a96b17eb9add20e7a01f8ded54c49) #15 0x561392bfd53e in curl_easy_perform (/usr/bin/clickhouse+0x337da53e) (BuildId: 22880fad595a96b17eb9add20e7a01f8ded54c49) #16 0x561392a7c411 in Azure::Core::Http::CurlConnection::CurlConnection(Azure::Core::Http::Request&, Azure::Core::Http::CurlTransportOptions const&, std::__1::basic_string, std::__1::allocator> const&, std::__1::basic_string, std::__1::allocator> const&) build_docker/./contrib/azure/sdk/core/azure-core/src/http/curl/curl.cpp:2441:24 #17 0x561392a6382b in std::__1::__unique_if::__unique_single std::__1::make_unique[abi:v15000], std::__1::allocator> const&, std::__1::basic_string, std::__1::allocator> const&>(Azure::Core::Http::Request&, Azure::Core::Http::CurlTransportOptions const&, std::__1::basic_string, std::__1::allocator> const&, std::__1::basic_string, std::__1::allocator> const&) build_docker/./contrib/llvm-project/libcxx/include/__memory/unique_ptr.h:714:32 #18 0x561392a6382b in Azure::Core::Http::_detail::CurlConnectionPool::ExtractOrCreateCurlConnection(Azure::Core::Http::Request&, Azure::Core::Http::CurlTransportOptions const&, bool) build_docker/./contrib/azure/sdk/core/azure-core/src/http/curl/curl.cpp:2126:10 #19 0x561392a61951 in Azure::Core::Http::CurlTransport::Send(Azure::Core::Http::Request&, Azure::Core::Context const&) build_docker/./contrib/azure/sdk/core/azure-core/src/http/curl/curl.cpp:351:48 #20 0x561392b0879b in Azure::Core::Http::Policies::_internal::TransportPolicy::Send(Azure::Core::Http::Request&, Azure::Core::Http::Policies::NextHttpPolicy, Azure::Core::Context const&) const build_docker/./contrib/azure/sdk/core/azure-core/src/http/transport_policy.cpp:121:40 #21 0x561392aae81b in Azure::Core::Http::Policies::_internal::LogPolicy::Send(Azure::Core::Http::Request&, Azure::Core::Http::Policies::NextHttpPolicy, Azure::Core::Context const&) const build_docker/./contrib/azure/sdk/core/azure-core/src/http/log_policy.cpp:114:23 #22 0x561392b022d8 in Azure::Core::Http::Policies::_internal::RequestActivityPolicy::Send(Azure::Core::Http::Request&, Azure::Core::Http::Policies::NextHttpPolicy, Azure::Core::Context const&) const build_docker/./contrib/azure/sdk/core/azure-core/src/http/request_activity_policy.cpp:110:23 #23 0x561392b2e02a in Azure::Storage::_internal::SharedKeyPolicy::Send(Azure::Core::Http::Request&, Azure::Core::Http::Policies::NextHttpPolicy, Azure::Core::Context const&) const build_docker/./contrib/azure/sdk/storage/azure-storage-common/inc/azure/storage/common/internal/shared_key_policy.hpp:36:25 #24 0x561392bf7fd8 in Azure::Storage::_internal::StoragePerRetryPolicy::Send(Azure::Core::Http::Request&, Azure::Core::Http::Policies::NextHttpPolicy, Azure::Core::Context const&) const build_docker/./contrib/azure/sdk/storage/azure-storage-common/src/storage_per_retry_policy.cpp:57:23 #25 0x561392bf8dcb in Azure::Storage::_internal::StorageSwitchToSecondaryPolicy::Send(Azure::Core::Http::Request&, Azure::Core::Http::Policies::NextHttpPolicy, Azure::Core::Context const&) const build_docker/./contrib/azure/sdk/storage/azure-storage-common/src/storage_switch_to_secondary_policy.cpp:36:32 #26 0x561392aff0d9 in Azure::Core::Http::Policies::_internal::RetryPolicy::Send(Azure::Core::Http::Request&, Azure::Core::Http::Policies::NextHttpPolicy, Azure::Core::Context const&) const build_docker/./contrib/azure/sdk/core/azure-core/src/http/retry_policy.cpp:146:34 #27 0x561392b08ed7 in Azure::Core::Http::Policies::_internal::TelemetryPolicy::Send(Azure::Core::Http::Request&, Azure::Core::Http::Policies::NextHttpPolicy, Azure::Core::Context const&) const build_docker/./contrib/azure/sdk/core/azure-core/src/http/telemetry_policy.cpp:23:21 #28 0x561392ae2fb5 in Azure::Core::Http::Policies::_internal::RequestIdPolicy::Send(Azure::Core::Http::Request&, Azure::Core::Http::Policies::NextHttpPolicy, Azure::Core::Context const&) const build_docker/./contrib/azure/sdk/core/azure-core/inc/azure/core/http/policies/policy.hpp:453:27 #29 0x561392b2e5fe in Azure::Storage::_internal::StorageServiceVersionPolicy::Send(Azure::Core::Http::Request&, Azure::Core::Http::Policies::NextHttpPolicy, Azure::Core::Context const&) const build_docker/./contrib/azure/sdk/storage/azure-storage-common/inc/azure/storage/common/internal/storage_service_version_policy.hpp:34:25 #30 0x561392b7e0b1 in Azure::Core::Http::_internal::HttpPipeline::Send(Azure::Core::Http::Request&, Azure::Core::Context const&) const build_docker/./contrib/azure/sdk/core/azure-core/inc/azure/core/internal/http/pipeline.hpp:230:29 #31 0x561392b7e0b1 in Azure::Storage::Blobs::_detail::BlobContainerClient::Create(Azure::Core::Http::_internal::HttpPipeline&, Azure::Core::Url const&, Azure::Storage::Blobs::_detail::BlobContainerClient::CreateBlobContainerOptions const&, Azure::Core::Context const&) build_docker/./contrib/azure/sdk/storage/azure-storage-blobs/src/rest_client.cpp:1415:36 #32 0x561392b3de98 in Azure::Storage::Blobs::BlobContainerClient::Create(Azure::Storage::Blobs::CreateBlobContainerOptions const&, Azure::Core::Context const&) const build_docker/./contrib/azure/sdk/storage/azure-storage-blobs/src/blob_container_client.cpp:258:12 ``` - at database startup, Azure is registered as an object storage (--> registerAzureObjectStorage) - this calls into Azure, then into curl, and then into OpenSSL - curl asks OpenSSL for a bunch of random numbers (--> function 'ossl_random' in curl's OpenSSL wrapper) - OpenSSL initializes the random number generator and stores it in some random number generator context object (--> *RAND_get0_private) - this object is registered via pthread_key_create and pthread_setspecific registered in TLS - if registerAzureObjectStorage was the only place which initializes the RNG, we could argue that the leaked memory does not matter anyways as it is released after shutdown - RAND_get0_public also registers a free handler (rand_delete_thread_state) that runs in TLS, so the memory is released also if registration is called from arbitrary other threads. In sum: this is a false positive. As a result, the fix is similar to earlier fix https://github.com/ClickHouse/openssl/commit/5c4b034785bf04f80380138cf49bf9743400f144 (cherry picked from commit 499f1ee900142f6b714dee99280d31df7b2eb4c0) (cherry picked from commit 5781b0ffd9aea9498fc9c0eedb176321d29bf879) (cherry picked from commit 8251cdd8df98599eb69fa86485bb855d3acef4e4) --- crypto/rand/rand_lib.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/crypto/rand/rand_lib.c b/crypto/rand/rand_lib.c index 3f0d6585f46e9..893cadd22bb79 100644 --- a/crypto/rand/rand_lib.c +++ b/crypto/rand/rand_lib.c @@ -916,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; From 19ca75b178c835ef9fd7ed0be77967d5d6a6a5ef Mon Sep 17 00:00:00 2001 From: Konstantin Bogdanov Date: Mon, 30 Jun 2025 08:27:07 +0200 Subject: [PATCH 10/21] Remove submodules (cherry picked from commit 6e1c68f6d03d3607c2ff468f2a59e3200dbabccd) (cherry picked from commit bc7c427151eca667ebd6d55c79546543e48dabd3) (cherry picked from commit baa57acef669433a54c351a42a4342baece27852) --- pkcs11-provider | 1 - 1 file changed, 1 deletion(-) delete mode 160000 pkcs11-provider 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 From 577c3943297f1492f47e0c8bc44bca9b703b8292 Mon Sep 17 00:00:00 2001 From: Konstantin Bogdanov Date: Mon, 27 Oct 2025 10:16:08 +0100 Subject: [PATCH 11/21] Add msan_unpoison around SHA3 assembly calls Related MSan error log: ==2046343==WARNING: MemorySanitizer: use-of-uninitialized-value #0 0x556f1dce43f2 in sample_scalar /home/thevar1able/nvmemount/clickhouse/contrib/openssl/crypto/ml_kem/ml_kem.c:449:47 #1 0x556f1dce43f2 in matrix_expand /home/thevar1able/nvmemount/clickhouse/contrib/openssl/crypto/ml_kem/ml_kem.c:999:21 #2 0x556f1dcdec79 in genkey /home/thevar1able/nvmemount/clickhouse/contrib/openssl/crypto/ml_kem/ml_kem.c:1391:10 #3 0x556f1dcde895 in ossl_ml_kem_genkey /home/thevar1able/nvmemount/clickhouse/contrib/openssl/crypto/ml_kem/ml_kem.c:1879:15 #4 0x556f1dd71e1b in ml_kem_gen /home/thevar1able/nvmemount/clickhouse/contrib/openssl/providers/implementations/keymgmt/ml_kem_kmgmt.c:771:13 #5 0x556f1da6add2 in evp_keymgmt_gen /home/thevar1able/nvmemount/clickhouse/contrib/openssl/crypto/evp/keymgmt_meth.c:464:11 #6 0x556f1da68160 in evp_keymgmt_util_gen /home/thevar1able/nvmemount/clickhouse/contrib/openssl/crypto/evp/keymgmt_lib.c:518:20 #7 0x556f1da8f2f4 in EVP_PKEY_generate /home/thevar1able/nvmemount/clickhouse/contrib/openssl/crypto/evp/pmeth_gn.c:189:13 #8 0x556f1da570d4 in evp_pkey_keygen /home/thevar1able/nvmemount/clickhouse/contrib/openssl/crypto/evp/evp_lib.c:1216:15 #9 0x556f1da570d4 in EVP_PKEY_Q_keygen /home/thevar1able/nvmemount/clickhouse/contrib/openssl/crypto/evp/evp_lib.c:1242:11 #10 0x556f1dd763f8 in mlx_kem_gen /home/thevar1able/nvmemount/clickhouse/contrib/openssl/providers/implementations/keymgmt/mlx_kmgmt.c:707:17 #11 0x556f1da6add2 in evp_keymgmt_gen /home/thevar1able/nvmemount/clickhouse/contrib/openssl/crypto/evp/keymgmt_meth.c:464:11 #12 0x556f1da68160 in evp_keymgmt_util_gen /home/thevar1able/nvmemount/clickhouse/contrib/openssl/crypto/evp/keymgmt_lib.c:518:20 #13 0x556f1da8f2f4 in EVP_PKEY_generate /home/thevar1able/nvmemount/clickhouse/contrib/openssl/crypto/evp/pmeth_gn.c:189:13 #14 0x556f1da8f972 in EVP_PKEY_keygen /home/thevar1able/nvmemount/clickhouse/contrib/openssl/crypto/evp/pmeth_gn.c:274:12 #15 0x556f1d8355b3 in ssl_generate_pkey_group /home/thevar1able/nvmemount/clickhouse/contrib/openssl/ssl/s3_lib.c:4935:9 #16 0x556f1d884598 in add_key_share /home/thevar1able/nvmemount/clickhouse/contrib/openssl/ssl/statem/extensions_clnt.c:661:25 #17 0x556f1d88417f in tls_construct_ctos_key_share /home/thevar1able/nvmemount/clickhouse/contrib/openssl/ssl/statem/extensions_clnt.c:766:18 #18 0x556f1d87cdec in tls_construct_extensions /home/thevar1able/nvmemount/clickhouse/contrib/openssl/ssl/statem/extensions.c:910:15 #19 0x556f1d8a777c in tls_construct_client_hello /home/thevar1able/nvmemount/clickhouse/contrib/openssl/ssl/statem/statem_clnt.c:1357:10 #20 0x556f1d8a2465 in write_state_machine /home/thevar1able/nvmemount/clickhouse/contrib/openssl/ssl/statem/statem.c:902:26 #21 0x556f1d8a2465 in state_machine /home/thevar1able/nvmemount/clickhouse/contrib/openssl/ssl/statem/statem.c:492:21 #22 0x556f1d8a1536 in ossl_statem_connect /home/thevar1able/nvmemount/clickhouse/contrib/openssl/ssl/statem/statem.c:301:12 #23 0x556f1d858378 in SSL_do_handshake /home/thevar1able/nvmemount/clickhouse/contrib/openssl/ssl/ssl_lib.c:5007:19 #24 0x556f1d858680 in SSL_connect /home/thevar1able/nvmemount/clickhouse/contrib/openssl/ssl/ssl_lib.c:2243:12 #25 0x556f1ab7f511 in Poco::Net::SecureSocketImpl::connectSSL(bool) /home/thevar1able/nvmemount/clickhouse/base/poco/NetSSL_OpenSSL/src/SecureS ocketImpl.cpp:205:11 #26 0x556f1ab80143 in Poco::Net::SecureSocketImpl::connect(Poco::Net::SocketAddress const&, Poco::Timespan const&, bool) /home/thevar1able/nvme mount/clickhouse/base/poco/NetSSL_OpenSSL/src/SecureSocketImpl.cpp:148:2 #27 0x556f1ab8dd4a in Poco::Net::SecureStreamSocketImpl::connect(Poco::Net::SocketAddress const&, Poco::Timespan const&) /home/thevar1able/nvme mount/clickhouse/base/poco/NetSSL_OpenSSL/src/SecureStreamSocketImpl.cpp:99:8 #28 0x556f1ab3d96d in Poco::Net::StreamSocket::connect(Poco::Net::SocketAddress const&, Poco::Timespan const&) /home/thevar1able/nvmemount/clic khouse/base/poco/Net/src/StreamSocket.cpp:89:10 #29 0x556f1aabfaa7 in Poco::Net::HTTPSession::connect(Poco::Net::SocketAddress const&) /home/thevar1able/nvmemount/clickhouse/base/poco/Net/src /HTTPSession.cpp:239:10 #30 0x556f1ab55f54 in Poco::Net::HTTPSClientSession::connect(Poco::Net::SocketAddress const&) /home/thevar1able/nvmemount/clickhouse/base/poco/ NetSSL_OpenSSL/src/HTTPSClientSession.cpp:182:16 #31 0x556f1aa86972 in Poco::Net::HTTPClientSession::reconnect(unsigned long*) /home/thevar1able/nvmemount/clickhouse/base/poco/Net/src/HTTPClie ntSession.cpp:471:13 #32 0x556ee2448df8 in DB::EndpointConnectionPool::PooledConnection::doConnect(unsigned long*) /home/thevar1able/ nvmemount/clickhouse/src/Common/HTTPConnectionPool.cpp:515:22 #33 0x556ee2448df8 in DB::EndpointConnectionPool::prepareNewConnection(DB::ConnectionTimeouts const&, unsigned l ong*) /home/thevar1able/nvmemount/clickhouse/src/Common/HTTPConnectionPool.cpp:690:25 #34 0x556ee2445eed in DB::EndpointConnectionPool::getConnection(DB::ConnectionTimeouts const&, unsigned long*) / home/thevar1able/nvmemount/clickhouse/src/Common/HTTPConnectionPool.cpp:603:16 #35 0x556ee248e8c9 in DB::makeHTTPSession(DB::HTTPConnectionGroupType, Poco::URI const&, DB::ConnectionTimeouts const&, DB::ProxyConfiguration const&, unsigned long*) /home/thevar1able/nvmemount/clickhouse/src/IO/HTTPCommon.cpp:63:29 #36 0x556ee91f3da5 in DB::ReadWriteBufferFromHTTP::callImpl(Poco::Net::HTTPResponse&, std::__1::basic_string, std::__1::allocator> const&, std::__1::optional const&, bool) const /home/thevar1able/nvmemount/clic khouse/src/IO/ReadWriteBufferFromHTTP.cpp:272:20 #37 0x556ee91f44b9 in DB::ReadWriteBufferFromHTTP::callWithRedirects(Poco::Net::HTTPResponse&, std::__1::basic_string, std::__1::allocator> const&, std::__1::optional const&) /home/thevar1able/nvmemount/clickho use/src/IO/ReadWriteBufferFromHTTP.cpp:290:19 #38 0x556ee91f6182 in DB::ReadWriteBufferFromHTTP::initialize() /home/thevar1able/nvmemount/clickhouse/src/IO/ReadWriteBufferFromHTTP.cpp:413:1 9 (cherry picked from commit 2720d8c08f0853b6388988fc600ca4b6370e4ed9) (cherry picked from commit bbe6690dbf06b237161d8b578ee43f4a15bf9f81) (cherry picked from commit c99788e87771379cf5f7ad385c16f05309f82433) --- crypto/sha/sha3.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) 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; From 22e83c0a5f4b1e2d64974c7f27316408a4cba714 Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Sun, 15 Feb 2026 17:32:11 +0100 Subject: [PATCH 12/21] Add __msan_unpoison for X25519 assembly output buffers x25519_scalar_mult and related functions may use assembly implementations (x25519-x86_64.s) that MSan cannot instrument. Add __msan_unpoison annotations after assembly writes, following the same pattern used in sha3.c, bn_intern.c, and eng_rdrand.c. (cherry picked from commit f3b2f0273ed59cef874db0d6943f8d7d1804bd31) (cherry picked from commit 66764a1148caf82c71371c0524f5a0ace0b942c8) (cherry picked from commit 9cbd8b3c8ca8640eceedb7d16b2163012acff5e3) --- crypto/ec/curve25519.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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)); } From 9321dc5620b7303e42acf540fae6d66e632b71d8 Mon Sep 17 00:00:00 2001 From: Konstantin Bogdanov Date: Sat, 4 Jul 2026 10:38:24 +0200 Subject: [PATCH 13/21] Use getauxval for ARM CPU feature detection on musl OSSL_IMPLEMENT_GETAUXVAL is only enabled for glibc >= 2.16, Android, FreeBSD and OpenBSD, so on musl OPENSSL_cpuid_setup fell back to the brute-force SIGILL-based probing: install a SIGILL handler, execute each optional instruction, and siglongjmp out of the handler when it traps. That scheme breaks under ThreadSanitizer with static libc interception: the sigaction/sigsetjmp/siglongjmp round-trip through TSan's interceptors during a library constructor leads to a null pointer call inside the probe, an unrecoverable startup failure in every TSan aarch64 musl binary (OPENSSL_cpuid_setup runs as an __attribute__((constructor)) before main). musl has always provided an unversioned getauxval, so take the same HWCAP-based path as glibc. This also removes the last SIGILL-probing user in the binary and keeps CPU detection free of signal-handler tricks. The __MUSL__ macro is defined globally by the ClickHouse musl toolchain files. (cherry picked from commit 26868a38972e80dd15f56212c3677d77363f5c74) (cherry picked from commit d1c98c3c7ebad8eccd0199d4f180a8e54a5de5af) (cherry picked from commit 80f76a2bf1322586102cd734fbf7af629dd463b0) --- crypto/armcap.c | 4 ++++ 1 file changed, 4 insertions(+) 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 From ae68e3aa80ce024efdd7d9f4ff561f95f12c868c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Mar=C3=ADn?= Date: Thu, 12 Nov 2026 17:06:48 +0100 Subject: [PATCH 14/21] Call stack and sparse array callbacks through typed thunks Backport of the upstream thunks: Clang CFI (cfi-icall) rejects calling the typed callbacks through the generic OPENSSL_sk_*func and sparse array function types. Every stack constructor installs all three thunks, as upstream does. Co-Authored-By: Claude Opus 5.5 (cherry picked from commit 8cf768ef218389d506c6a0ce7f657356ac36f3ce) --- crypto/bsearch.c | 17 ++++- crypto/objects/obj_dat.c | 2 +- crypto/property/property_query.c | 2 +- crypto/stack/stack.c | 73 ++++++++++++++++--- doc/man3/DEFINE_STACK_OF.pod | 20 ++++- include/crypto/sparse_array.h | 28 ++++++- include/internal/cryptlib.h | 1 + include/openssl/safestack.h.in | 121 +++++++++++++++++++++++-------- include/openssl/stack.h | 7 +- test/stack_test.c | 60 +++++++++++++++ util/libcrypto.num | 3 + util/perl/OpenSSL/stackhash.pm | 8 +- 12 files changed, 284 insertions(+), 58 deletions(-) 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/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/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/stack/stack.c b/crypto/stack/stack.c index 633e9f1bf2444..f54e8cf43a7cd 100644 --- a/crypto/stack/stack.c +++ b/crypto/stack/stack.c @@ -31,6 +31,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 +48,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 +100,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 +127,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 +241,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 +278,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 +366,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 +391,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 +401,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 +409,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 +418,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 +485,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); } 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/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/openssl/safestack.h.in b/include/openssl/safestack.h.in index a88409ba4151e..27545fcaaa342 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,33 @@ 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); \ + OPENSSL_sk_freefunc_thunk f_thunk; \ + \ + f_thunk = (OPENSSL_sk_freefunc_thunk)sk_##t1##_freefunc_thunk; \ + OPENSSL_sk_set_cmp_thunks(ret, sk_##t1##_cmpfunc_thunk); \ + OPENSSL_sk_set_copy_thunks(ret, sk_##t1##_copyfunc_thunk); \ + return (STACK_OF(t1) *)OPENSSL_sk_set_thunks(ret, f_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(); \ + OPENSSL_sk_freefunc_thunk f_thunk; \ + \ + f_thunk = (OPENSSL_sk_freefunc_thunk)sk_##t1##_freefunc_thunk; \ + OPENSSL_sk_set_cmp_thunks(ret, sk_##t1##_cmpfunc_thunk); \ + OPENSSL_sk_set_copy_thunks(ret, sk_##t1##_copyfunc_thunk); \ + return (STACK_OF(t1) *)OPENSSL_sk_set_thunks(ret, f_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); \ + OPENSSL_sk_freefunc_thunk f_thunk; \ + \ + f_thunk = (OPENSSL_sk_freefunc_thunk)sk_##t1##_freefunc_thunk; \ + OPENSSL_sk_set_cmp_thunks(ret, sk_##t1##_cmpfunc_thunk); \ + OPENSSL_sk_set_copy_thunks(ret, sk_##t1##_copyfunc_thunk); \ + return (STACK_OF(t1) *)OPENSSL_sk_set_thunks(ret, f_thunk); \ } \ static ossl_unused ossl_inline int sk_##t1##_reserve(STACK_OF(t1) *sk, int n) \ { \ @@ -130,6 +184,11 @@ extern "C" { } \ static ossl_unused ossl_inline void sk_##t1##_pop_free(STACK_OF(t1) *sk, sk_##t1##_freefunc freefunc) \ { \ + OPENSSL_sk_freefunc_thunk f_thunk; \ + \ + f_thunk = (OPENSSL_sk_freefunc_thunk)sk_##t1##_freefunc_thunk; \ + sk = (STACK_OF(t1) *)OPENSSL_sk_set_thunks((OPENSSL_STACK *)sk, f_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..0d3c8161798b5 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,12 @@ 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); 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/test/stack_test.c b/test/stack_test.c index 7bea2a2b7f324..642b26614c07e 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,55 @@ 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; + } + } + + MFAIL_start(); + r = sk_OPENSSL_STRING_deep_copy(s, string_copy, string_free); + MFAIL_end(); + + 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(); @@ -385,5 +444,6 @@ int setup_tests(void) ADD_ALL_TESTS(test_uchar_stack, 4); ADD_TEST(test_SS_stack); ADD_TEST(test_SU_stack); + ADD_MFAIL_TEST(test_OPENSSL_STRING_deep_copy_mfail); return 1; } 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..7582558907557 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_set_thunks(OPENSSL_sk_set_copy_thunks(OPENSSL_sk_set_cmp_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_set_thunks(OPENSSL_sk_set_copy_thunks(OPENSSL_sk_set_cmp_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_set_thunks(OPENSSL_sk_set_copy_thunks(OPENSSL_sk_set_cmp_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)) From a88c02ca62fc95b4e27f9fa15f4c56a3b67500f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Mar=C3=ADn?= Date: Thu, 12 Nov 2026 17:06:48 +0100 Subject: [PATCH 15/21] Call digest update functions with the dispatch table type Clang CFI (cfi-icall) rejects calling the typed update functions through OSSL_FUNC_digest_update_fn. Co-Authored-By: Claude Opus 5.5 (cherry picked from commit 6f9c8a513822b479175dcb7238b9d34ebd85c682) --- providers/implementations/include/prov/digestcommon.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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 }, \ From 337e0eccd99f299774deb49dda7e5cbf88fff35b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Mar=C3=ADn?= Date: Thu, 12 Nov 2026 17:06:48 +0100 Subject: [PATCH 16/21] Exempt the Poly1305 assembly calls from cfi-icall The assembly poly1305_init stores raw addresses of assembly routines, which have no CFI type. Co-Authored-By: Claude Opus 5.5 (cherry picked from commit d49613675567b887549ef3b9acb31839c3226339) --- crypto/poly1305/poly1305.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) 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; From 6e7be158c2e40698865839600dece4def3030e51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Mar=C3=ADn?= Date: Tue, 17 Nov 2026 13:41:46 +0100 Subject: [PATCH 17/21] Sort stacks through the comparison thunk OPENSSL_sk_sort passed the stack's comparison function to qsort, which calls it as int (*)(const void *, const void *), a type other than its own, and Clang CFI rejects the call. Pass qsort a comparison function of that type, which calls the stack's thunk; the stack being sorted is kept in a thread-local, because qsort passes no context. Co-Authored-By: Claude Opus 5.5 (cherry picked from commit b1ca56a8426a49cd12d14e10f5edccf0633983de) --- crypto/stack/stack.c | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/crypto/stack/stack.c b/crypto/stack/stack.c index f54e8cf43a7cd..c525a3b56d07d 100644 --- a/crypto/stack/stack.c +++ b/crypto/stack/stack.c @@ -528,11 +528,39 @@ void *OPENSSL_sk_set(OPENSSL_STACK *st, int i, const void *data) return (void *)st->data[i]; } +/* + * The stack being sorted by OPENSSL_sk_sort, for sort_cmp. + * + * st->comp cannot be passed to qsort directly: qsort calls it as + * int (*)(const void *, const void *), which is not the type it was defined + * with (e.g. ossl_provider_cmp), and Clang CFI rejects that call. Comparing + * through st->cmp_thunk fixes the type, as ossl_bsearch does, but the + * comparison function given to qsort receives only the two elements, not the + * stack that holds the thunk. qsort_r would pass it, but it is not available + * on every libc (glibc has it only since 2.8, and BSD's takes its arguments in + * another order), so the stack is kept here, per thread, for the duration of + * the sort. Reported upstream in + * https://github.com/openssl/openssl/issues/22896#issuecomment-6952731417 + */ +static _Thread_local const OPENSSL_STACK *sort_stack; + +/* Compares two elements of sort_stack through its thunk. */ +static int sort_cmp(const void *a, const void *b) +{ + return cmp_with_thunk(sort_stack, a, b); +} + void OPENSSL_sk_sort(OPENSSL_STACK *st) { if (st != NULL && !st->sorted && st->comp != NULL) { - if (st->num > 1) - qsort(st->data, st->num, sizeof(void *), st->comp); + if (st->num > 1) { + /* Restored after, as a comparison function may sort another stack. */ + const OPENSSL_STACK *outer = sort_stack; + + sort_stack = st; + qsort(st->data, st->num, sizeof(void *), sort_cmp); + sort_stack = outer; + } st->sorted = 1; /* empty or single-element stack is considered sorted */ } } From ae749151f7d0c30d176ede1b42b5101308a6e839 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Mar=C3=ADn?= Date: Tue, 17 Nov 2026 17:00:53 +0100 Subject: [PATCH 18/21] Build the stack test without the allocation failure framework The backported deep copy test uses MFAIL_start and ADD_MFAIL_TEST, which 3.5 does not have, so test/stack_test.c did not compile. Run it as a plain test. Co-Authored-By: Claude Opus 5.5 (cherry picked from commit b45d9ca5c7ad5c6d4fbd10ee20080db3905dc2ea) --- test/stack_test.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/stack_test.c b/test/stack_test.c index 642b26614c07e..1abe4403fee0d 100644 --- a/test/stack_test.c +++ b/test/stack_test.c @@ -376,9 +376,7 @@ static int test_OPENSSL_STRING_deep_copy_mfail(void) } } - MFAIL_start(); r = sk_OPENSSL_STRING_deep_copy(s, string_copy, string_free); - MFAIL_end(); if (r == NULL) goto end; @@ -444,6 +442,6 @@ int setup_tests(void) ADD_ALL_TESTS(test_uchar_stack, 4); ADD_TEST(test_SS_stack); ADD_TEST(test_SU_stack); - ADD_MFAIL_TEST(test_OPENSSL_STRING_deep_copy_mfail); + ADD_TEST(test_OPENSSL_STRING_deep_copy_mfail); return 1; } From 15fe39dbee67861e4e511bd63c4b37a2ad05655e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Mar=C3=ADn?= Date: Tue, 17 Nov 2026 17:01:04 +0100 Subject: [PATCH 19/21] Sort typed stacks with a merge sort through the thunk OPENSSL_sk_sort kept the stack being sorted in a thread-local for the comparison function given to qsort, which goes wrong when ASYNC jobs interleave sorts on one thread, and needs C11. Sort stacks that have a comparison thunk with a merge sort that calls it directly, using a local buffer for small stacks and a heap sort if a larger buffer cannot be allocated; stacks without a thunk keep using qsort. Co-Authored-By: Claude Opus 5.5 (cherry picked from commit e26ec66b6aab307956436cfee26378c33bb86d5f) --- crypto/stack/stack.c | 103 ++++++++++++++++++++++++++++++++----------- test/stack_test.c | 31 +++++++++++++ 2 files changed, 109 insertions(+), 25 deletions(-) diff --git a/crypto/stack/stack.c b/crypto/stack/stack.c index c525a3b56d07d..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 @@ -528,39 +529,91 @@ 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)); +} + /* - * The stack being sorted by OPENSSL_sk_sort, for sort_cmp. - * - * st->comp cannot be passed to qsort directly: qsort calls it as - * int (*)(const void *, const void *), which is not the type it was defined - * with (e.g. ossl_provider_cmp), and Clang CFI rejects that call. Comparing - * through st->cmp_thunk fixes the type, as ossl_bsearch does, but the - * comparison function given to qsort receives only the two elements, not the - * stack that holds the thunk. qsort_r would pass it, but it is not available - * on every libc (glibc has it only since 2.8, and BSD's takes its arguments in - * another order), so the stack is kept here, per thread, for the duration of - * the sort. Reported upstream in - * https://github.com/openssl/openssl/issues/22896#issuecomment-6952731417 + * Sort |st->data|, calling |st->comp| only via cmp_with_thunk(). + * Reported upstream in https://github.com/openssl/openssl/issues/22896#issuecomment-6952731417 */ -static _Thread_local const OPENSSL_STACK *sort_stack; - -/* Compares two elements of sort_stack through its thunk. */ -static int sort_cmp(const void *a, const void *b) +static void sort_with_thunk(const OPENSSL_STACK *st) { - return cmp_with_thunk(sort_stack, a, b); + 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) { - /* Restored after, as a comparison function may sort another stack. */ - const OPENSSL_STACK *outer = sort_stack; - - sort_stack = st; - qsort(st->data, st->num, sizeof(void *), sort_cmp); - sort_stack = outer; - } + 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/test/stack_test.c b/test/stack_test.c index 1abe4403fee0d..d88ec15926003 100644 --- a/test/stack_test.c +++ b/test/stack_test.c @@ -436,9 +436,40 @@ 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); From 3a617a6bf5e65d57fbc3dd5f9e2890a076946413 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Mar=C3=ADn?= Date: Tue, 17 Nov 2026 23:46:01 +0100 Subject: [PATCH 20/21] Install the stack thunks only in Clang CFI builds The setters of the typed stack thunks are not in upstream OpenSSL 3.5, so calling them from the public headers made code built with these headers fail to link against a stock libcrypto.so.3. Only Clang CFI (cfi-icall) needs the thunks, so install them only when it is enabled. Co-Authored-By: Claude Opus 5.5 (cherry picked from commit 3a6a2c41d7b26050d22e34af374e8e92feb81b31) --- include/openssl/safestack.h.in | 30 +++++++----------------------- include/openssl/stack.h | 17 +++++++++++++++++ util/perl/OpenSSL/stackhash.pm | 6 +++--- 3 files changed, 27 insertions(+), 26 deletions(-) diff --git a/include/openssl/safestack.h.in b/include/openssl/safestack.h.in index 27545fcaaa342..dd2d1560d25a3 100644 --- a/include/openssl/safestack.h.in +++ b/include/openssl/safestack.h.in @@ -118,32 +118,20 @@ extern "C" { static ossl_unused ossl_inline STACK_OF(t1) *sk_##t1##_new(sk_##t1##_compfunc compare) \ { \ OPENSSL_STACK *ret = OPENSSL_sk_new((OPENSSL_sk_compfunc)compare); \ - OPENSSL_sk_freefunc_thunk f_thunk; \ - \ - f_thunk = (OPENSSL_sk_freefunc_thunk)sk_##t1##_freefunc_thunk; \ - OPENSSL_sk_set_cmp_thunks(ret, sk_##t1##_cmpfunc_thunk); \ - OPENSSL_sk_set_copy_thunks(ret, sk_##t1##_copyfunc_thunk); \ - return (STACK_OF(t1) *)OPENSSL_sk_set_thunks(ret, f_thunk); \ + 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) \ { \ OPENSSL_STACK *ret = OPENSSL_sk_new_null(); \ - OPENSSL_sk_freefunc_thunk f_thunk; \ - \ - f_thunk = (OPENSSL_sk_freefunc_thunk)sk_##t1##_freefunc_thunk; \ - OPENSSL_sk_set_cmp_thunks(ret, sk_##t1##_cmpfunc_thunk); \ - OPENSSL_sk_set_copy_thunks(ret, sk_##t1##_copyfunc_thunk); \ - return (STACK_OF(t1) *)OPENSSL_sk_set_thunks(ret, f_thunk); \ + 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) \ { \ OPENSSL_STACK *ret = OPENSSL_sk_new_reserve((OPENSSL_sk_compfunc)compare, n); \ - OPENSSL_sk_freefunc_thunk f_thunk; \ - \ - f_thunk = (OPENSSL_sk_freefunc_thunk)sk_##t1##_freefunc_thunk; \ - OPENSSL_sk_set_cmp_thunks(ret, sk_##t1##_cmpfunc_thunk); \ - OPENSSL_sk_set_copy_thunks(ret, sk_##t1##_copyfunc_thunk); \ - return (STACK_OF(t1) *)OPENSSL_sk_set_thunks(ret, f_thunk); \ + 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) \ { \ @@ -184,11 +172,7 @@ extern "C" { } \ static ossl_unused ossl_inline void sk_##t1##_pop_free(STACK_OF(t1) *sk, sk_##t1##_freefunc freefunc) \ { \ - OPENSSL_sk_freefunc_thunk f_thunk; \ - \ - f_thunk = (OPENSSL_sk_freefunc_thunk)sk_##t1##_freefunc_thunk; \ - sk = (STACK_OF(t1) *)OPENSSL_sk_set_thunks((OPENSSL_STACK *)sk, f_thunk); \ - \ + 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 0d3c8161798b5..2324ade2d93f5 100644 --- a/include/openssl/stack.h +++ b/include/openssl/stack.h @@ -39,6 +39,23 @@ 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, OPENSSL_sk_freefunc func); diff --git a/util/perl/OpenSSL/stackhash.pm b/util/perl/OpenSSL/stackhash.pm index 7582558907557..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_set_thunks(OPENSSL_sk_set_copy_thunks(OPENSSL_sk_set_cmp_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_set_thunks(OPENSSL_sk_set_copy_thunks(OPENSSL_sk_set_cmp_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_set_thunks(OPENSSL_sk_set_copy_thunks(OPENSSL_sk_set_cmp_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}_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)) From c2ddefa8e1bdf9d78e35ca8372b822af1f4103f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Mar=C3=ADn?= Date: Mon, 23 Nov 2026 15:14:29 +0100 Subject: [PATCH 21/21] Match the `const` of `X509V3_EXT_V2I` in the alternative name methods `v2i_subject_alt` and `v2i_issuer_alt` take a non-const `X509V3_EXT_METHOD *`, but `do_ext_nconf` calls them through `X509V3_EXT_V2I`, which takes a `const` one. Clang CFI with `-fsanitize-cfi-icall-generalize-pointers` treats pointer types alike but keeps the `const` of the pointee, so creating a subject alternative name extension with `X509V3_EXT_conf_nid` traps. Both functions only pass the method to `v2i_GENERAL_NAME`, which already takes it as `const`. Co-Authored-By: Claude Opus 5.5 (cherry picked from commit 136446dc86e3cdb822be9fca6f5d6d13b153ec20) --- crypto/x509/v3_san.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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) {