summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorkenjiro <>2026-08-30 12:19:37 +0000
committerkenjiro <>2026-08-30 12:19:37 +0000
commit5931045c664b500427fb54c31fab4de3e92bb159 (patch)
tree6bcb237ddd3233dde465de629eee51c4c1a0ed2c /src/lib
parent6b13eef79d2dc54c1b0203f967b3fc8b8971df78 (diff)
downloadopenbsd-5931045c664b500427fb54c31fab4de3e92bb159.tar.gz
openbsd-5931045c664b500427fb54c31fab4de3e92bb159.tar.bz2
openbsd-5931045c664b500427fb54c31fab4de3e92bb159.zip
Make CRYPTO_cleanup_all_ex_data() a compatibility no-op
The ex_data callback registry is process-wide, but this API could free it while other threads were still using libcrypto, resulting in a use-after-free. Retain the public symbol as a compatibility no-op and mark it deprecated. Move the actual cleanup to an internal function called by OPENSSL_cleanup(). Replace the in-tree callers with OPENSSL_cleanup() at final shutdown to preserve cleanup behavior and coverage. Document both APIs and the requirement that OPENSSL_cleanup() only be called after all threads and components have stopped using libcrypto. ok tb
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/libcrypto/crypto.h5
-rw-r--r--src/lib/libcrypto/crypto_ex_data.c15
-rw-r--r--src/lib/libcrypto/crypto_init.c4
-rw-r--r--src/lib/libcrypto/crypto_internal.h3
-rw-r--r--src/lib/libcrypto/man/CRYPTO_set_ex_data.327
-rw-r--r--src/lib/libcrypto/man/OPENSSL_init_crypto.335
6 files changed, 73 insertions, 16 deletions
diff --git a/src/lib/libcrypto/crypto.h b/src/lib/libcrypto/crypto.h
index 9fcf868403..2a121dda85 100644
--- a/src/lib/libcrypto/crypto.h
+++ b/src/lib/libcrypto/crypto.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: crypto.h,v 1.80 2025/09/28 07:52:53 tb Exp $ */ 1/* $OpenBSD: crypto.h,v 1.81 2026/08/30 12:19:37 kenjiro Exp $ */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
4 * 4 *
@@ -305,8 +305,7 @@ void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad);
305 * (relative to the class type involved) */ 305 * (relative to the class type involved) */
306int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val); 306int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val);
307void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx); 307void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx);
308/* This function cleans up all "ex_data" state. It mustn't be called under 308/* Deprecated no-op retained for compatibility. */
309 * potential race-conditions. */
310void CRYPTO_cleanup_all_ex_data(void); 309void CRYPTO_cleanup_all_ex_data(void);
311 310
312void CRYPTO_lock(int mode, int type, const char *file, int line); 311void CRYPTO_lock(int mode, int type, const char *file, int line);
diff --git a/src/lib/libcrypto/crypto_ex_data.c b/src/lib/libcrypto/crypto_ex_data.c
index 233905f888..2066098653 100644
--- a/src/lib/libcrypto/crypto_ex_data.c
+++ b/src/lib/libcrypto/crypto_ex_data.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: crypto_ex_data.c,v 1.6 2025/06/15 15:58:56 tb Exp $ */ 1/* $OpenBSD: crypto_ex_data.c,v 1.7 2026/08/30 12:19:37 kenjiro Exp $ */
2/* 2/*
3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -19,6 +19,8 @@
19 19
20#include <openssl/crypto.h> 20#include <openssl/crypto.h>
21 21
22#include "crypto_internal.h"
23
22#define CRYPTO_EX_DATA_MAX_INDEX 32 24#define CRYPTO_EX_DATA_MAX_INDEX 32
23 25
24struct crypto_ex_data { 26struct crypto_ex_data {
@@ -151,6 +153,16 @@ LCRYPTO_ALIAS(CRYPTO_get_ex_new_index);
151void 153void
152CRYPTO_cleanup_all_ex_data(void) 154CRYPTO_cleanup_all_ex_data(void)
153{ 155{
156}
157LCRYPTO_ALIAS(CRYPTO_cleanup_all_ex_data);
158
159/*
160 * Free process-wide ex_data state during OPENSSL_cleanup(). The caller must
161 * ensure that no other thread is using libcrypto.
162 */
163void
164crypto_ex_data_cleanup(void)
165{
154 struct crypto_ex_data_class *class; 166 struct crypto_ex_data_class *class;
155 int i, j; 167 int i, j;
156 168
@@ -173,7 +185,6 @@ CRYPTO_cleanup_all_ex_data(void)
173 free(classes); 185 free(classes);
174 classes = NULL; 186 classes = NULL;
175} 187}
176LCRYPTO_ALIAS(CRYPTO_cleanup_all_ex_data);
177 188
178static void 189static void
179crypto_ex_data_clear(CRYPTO_EX_DATA *exdata) 190crypto_ex_data_clear(CRYPTO_EX_DATA *exdata)
diff --git a/src/lib/libcrypto/crypto_init.c b/src/lib/libcrypto/crypto_init.c
index ae4914e358..5b7ce3d4fa 100644
--- a/src/lib/libcrypto/crypto_init.c
+++ b/src/lib/libcrypto/crypto_init.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: crypto_init.c,v 1.26 2025/06/11 07:41:12 tb Exp $ */ 1/* $OpenBSD: crypto_init.c,v 1.27 2026/08/30 12:19:37 kenjiro Exp $ */
2/* 2/*
3 * Copyright (c) 2018 Bob Beck <beck@openbsd.org> 3 * Copyright (c) 2018 Bob Beck <beck@openbsd.org>
4 * 4 *
@@ -107,7 +107,7 @@ OPENSSL_cleanup(void)
107 /* This currently calls init... */ 107 /* This currently calls init... */
108 ERR_free_strings(); 108 ERR_free_strings();
109 109
110 CRYPTO_cleanup_all_ex_data(); 110 crypto_ex_data_cleanup();
111 EVP_cleanup(); 111 EVP_cleanup();
112 112
113 X509_VERIFY_PARAM_table_cleanup(); 113 X509_VERIFY_PARAM_table_cleanup();
diff --git a/src/lib/libcrypto/crypto_internal.h b/src/lib/libcrypto/crypto_internal.h
index 058245e95e..2dce750724 100644
--- a/src/lib/libcrypto/crypto_internal.h
+++ b/src/lib/libcrypto/crypto_internal.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: crypto_internal.h,v 1.16 2025/07/22 09:18:02 jsing Exp $ */ 1/* $OpenBSD: crypto_internal.h,v 1.17 2026/08/30 12:19:37 kenjiro Exp $ */
2/* 2/*
3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -299,5 +299,6 @@ crypto_ror_u64(uint64_t v, size_t shift)
299#endif 299#endif
300 300
301void crypto_cpu_caps_init(void); 301void crypto_cpu_caps_init(void);
302void crypto_ex_data_cleanup(void);
302 303
303#endif 304#endif
diff --git a/src/lib/libcrypto/man/CRYPTO_set_ex_data.3 b/src/lib/libcrypto/man/CRYPTO_set_ex_data.3
index ae2b04b528..00b5785533 100644
--- a/src/lib/libcrypto/man/CRYPTO_set_ex_data.3
+++ b/src/lib/libcrypto/man/CRYPTO_set_ex_data.3
@@ -1,4 +1,4 @@
1.\" $OpenBSD: CRYPTO_set_ex_data.3,v 1.17 2026/08/23 04:46:31 jsg Exp $ 1.\" $OpenBSD: CRYPTO_set_ex_data.3,v 1.18 2026/08/30 12:19:37 kenjiro Exp $
2.\" 2.\"
3.\" Copyright (c) 2023 Ingo Schwarze <schwarze@openbsd.org> 3.\" Copyright (c) 2023 Ingo Schwarze <schwarze@openbsd.org>
4.\" 4.\"
@@ -14,7 +14,7 @@
14.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16.\" 16.\"
17.Dd $Mdocdate: August 23 2026 $ 17.Dd $Mdocdate: August 30 2026 $
18.Dt CRYPTO_SET_EX_DATA 3 18.Dt CRYPTO_SET_EX_DATA 3
19.Os 19.Os
20.Sh NAME 20.Sh NAME
@@ -25,7 +25,8 @@
25.Nm CRYPTO_new_ex_data , 25.Nm CRYPTO_new_ex_data ,
26.Nm CRYPTO_set_ex_data , 26.Nm CRYPTO_set_ex_data ,
27.Nm CRYPTO_get_ex_data , 27.Nm CRYPTO_get_ex_data ,
28.Nm CRYPTO_free_ex_data 28.Nm CRYPTO_free_ex_data ,
29.Nm CRYPTO_cleanup_all_ex_data
29.Nd low-level functions for application specific data 30.Nd low-level functions for application specific data
30.Sh SYNOPSIS 31.Sh SYNOPSIS
31.Lb libcrypto 32.Lb libcrypto
@@ -89,6 +90,8 @@
89.Fa "void *parent" 90.Fa "void *parent"
90.Fa "CRYPTO_EX_DATA *ad" 91.Fa "CRYPTO_EX_DATA *ad"
91.Fc 92.Fc
93.Ft void
94.Fn CRYPTO_cleanup_all_ex_data void
92.Sh DESCRIPTION 95.Sh DESCRIPTION
93The library implements the functions documented in the 96The library implements the functions documented in the
94.Xr RSA_get_ex_new_index 3 97.Xr RSA_get_ex_new_index 3
@@ -342,6 +345,20 @@ Despite its name,
342does not free 345does not free
343.Fa ad 346.Fa ad
344itself. 347itself.
348.Pp
349.Fn CRYPTO_cleanup_all_ex_data
350is deprecated and has no effect.
351It is retained for source and binary compatibility only.
352Application programs should not call it after individual operations,
353from individual library components, or during process shutdown.
354No replacement call is needed.
355.Pp
356In particular,
357.Xr OPENSSL_cleanup 3
358is not a replacement for
359.Fn CRYPTO_cleanup_all_ex_data .
360It tears down process-wide library state and is only intended for final
361shutdown after all threads using libcrypto have stopped.
345.Sh RETURN VALUES 362.Sh RETURN VALUES
346.Fn CRYPTO_get_ex_new_index 363.Fn CRYPTO_get_ex_new_index
347returns a new index equal to or greater than 1 364returns a new index equal to or greater than 1
@@ -425,6 +442,10 @@ and
425.Fn CRYPTO_EX_dup 442.Fn CRYPTO_EX_dup
426first appeared in OpenSSL 0.9.5 and have been available since 443first appeared in OpenSSL 0.9.5 and have been available since
427.Ox 2.7 . 444.Ox 2.7 .
445.Pp
446.Fn CRYPTO_cleanup_all_ex_data
447first appeared in OpenSSL 0.9.7 and has been available since
448.Ox 3.2 .
428.Sh CAVEATS 449.Sh CAVEATS
429If a program installs callback functions, the last call to 450If a program installs callback functions, the last call to
430.Fn CRYPTO_get_ex_new_index 451.Fn CRYPTO_get_ex_new_index
diff --git a/src/lib/libcrypto/man/OPENSSL_init_crypto.3 b/src/lib/libcrypto/man/OPENSSL_init_crypto.3
index 5c29d55aa9..24756bc396 100644
--- a/src/lib/libcrypto/man/OPENSSL_init_crypto.3
+++ b/src/lib/libcrypto/man/OPENSSL_init_crypto.3
@@ -1,4 +1,4 @@
1.\" $OpenBSD: OPENSSL_init_crypto.3,v 1.7 2025/06/09 12:43:53 schwarze Exp $ 1.\" $OpenBSD: OPENSSL_init_crypto.3,v 1.8 2026/08/30 12:19:37 kenjiro Exp $
2.\" Copyright (c) 2018, 2020 Ingo Schwarze <schwarze@openbsd.org> 2.\" Copyright (c) 2018, 2020 Ingo Schwarze <schwarze@openbsd.org>
3.\" 3.\"
4.\" Permission to use, copy, modify, and distribute this software for any 4.\" Permission to use, copy, modify, and distribute this software for any
@@ -13,13 +13,14 @@
13.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15.\" 15.\"
16.Dd $Mdocdate: June 9 2025 $ 16.Dd $Mdocdate: August 30 2026 $
17.Dt OPENSSL_INIT_CRYPTO 3 17.Dt OPENSSL_INIT_CRYPTO 3
18.Os 18.Os
19.Sh NAME 19.Sh NAME
20.Nm OPENSSL_init_crypto , 20.Nm OPENSSL_init_crypto ,
21.Nm OPENSSL_init 21.Nm OPENSSL_init ,
22.Nd initialise the crypto library 22.Nm OPENSSL_cleanup
23.Nd initialise and shut down the crypto library
23.Sh SYNOPSIS 24.Sh SYNOPSIS
24.Lb libcrypto 25.Lb libcrypto
25.In openssl/crypto.h 26.In openssl/crypto.h
@@ -30,8 +31,13 @@
30.Fc 31.Fc
31.Ft void 32.Ft void
32.Fn OPENSSL_init void 33.Fn OPENSSL_init void
34.Ft void
35.Fn OPENSSL_cleanup void
33.Sh DESCRIPTION 36.Sh DESCRIPTION
34These functions are deprecated. 37.Fn OPENSSL_init_crypto
38and
39.Fn OPENSSL_init
40are deprecated.
35It is never useful for an application program 41It is never useful for an application program
36to call either of them explicitly. 42to call either of them explicitly.
37.Pp 43.Pp
@@ -84,6 +90,21 @@ the first one have any effect.
84.Pp 90.Pp
85.Fn OPENSSL_init 91.Fn OPENSSL_init
86has no effect at all. 92has no effect at all.
93.Pp
94.Fn OPENSSL_cleanup
95frees process-wide resources allocated by libcrypto.
96It is intended exclusively for the final shutdown of libcrypto in a process.
97Before calling it, the application has to ensure that all other threads
98that may use libcrypto have stopped and that no library component will use
99libcrypto again.
100After it returns, libcrypto cannot be reinitialized and no libcrypto or
101libssl function may be called.
102.Pp
103.Fn OPENSSL_cleanup
104is not a replacement for deprecated cleanup functions such as
105.Xr CRYPTO_cleanup_all_ex_data 3
106and must not be called after individual cryptographic operations or when an
107individual library component is finished with libcrypto.
87.Sh RETURN VALUES 108.Sh RETURN VALUES
88.Fn OPENSSL_init_crypto 109.Fn OPENSSL_init_crypto
89is intended to return 1 on success or 0 on error. 110is intended to return 1 on success or 0 on error.
@@ -101,6 +122,10 @@ It stopped having any effect in OpenSSL 1.1.1 and in
101.Fn OPENSSL_init_crypto 122.Fn OPENSSL_init_crypto
102first appeared in OpenSSL 1.1.0 and has been available since 123first appeared in OpenSSL 1.1.0 and has been available since
103.Ox 6.3 . 124.Ox 6.3 .
125.Pp
126.Fn OPENSSL_cleanup
127first appeared in OpenSSL 1.1.0 and has been available since
128.Ox 7.2 .
104.Sh BUGS 129.Sh BUGS
105.Fn OPENSSL_init_crypto 130.Fn OPENSSL_init_crypto
106silently ignores almost all kinds of errors. 131silently ignores almost all kinds of errors.