Skip to content

Commit f3a9fbb

Browse files
committed
Check *_get_ex_data() and *_set_ex_data() return values.
Closes #3389. Thanks to Qingpeng Du.
1 parent a06d936 commit f3a9fbb

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

ChangeLog.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
Broker:
55
- Fix handling of disconnected sessions for `per_listener_settings true`
6+
- Check return values of openssl *_get_ex_data() and *_set_ex_data() to
7+
prevent possible crash. This could occur only in extremely unlikely
8+
situations. See https://github.com/eclipse-mosquitto/mosquitto/issues/3389
9+
Closes #3389.
610

711

812
2.0.22 - 2025-07-11

lib/net_mosq.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,8 @@ static int net__init_ssl_ctx(struct mosquitto *mosq)
650650
EVP_PKEY *pkey;
651651
#endif
652652

653+
net__init_tls();
654+
653655
#ifndef WITH_BROKER
654656
if(mosq->user_ssl_ctx){
655657
mosq->ssl_ctx = mosq->user_ssl_ctx;
@@ -666,7 +668,6 @@ static int net__init_ssl_ctx(struct mosquitto *mosq)
666668
* has not been set, or if both of MOSQ_OPT_SSL_CTX and
667669
* MOSQ_OPT_SSL_CTX_WITH_DEFAULTS are set. */
668670
if(mosq->tls_cafile || mosq->tls_capath || mosq->tls_psk || mosq->tls_use_os_certs){
669-
net__init_tls();
670671
if(!mosq->ssl_ctx){
671672

672673
#if OPENSSL_VERSION_NUMBER < 0x10100000L
@@ -881,7 +882,11 @@ int net__socket_connect_step3(struct mosquitto *mosq, const char *host)
881882
return MOSQ_ERR_TLS;
882883
}
883884

884-
SSL_set_ex_data(mosq->ssl, tls_ex_index_mosq, mosq);
885+
if(!SSL_set_ex_data(mosq->ssl, tls_ex_index_mosq, mosq)){
886+
net__socket_close(mosq);
887+
net__print_ssl_error(mosq);
888+
return MOSQ_ERR_TLS;
889+
}
885890
bio = BIO_new_socket(mosq->sock, BIO_NOCLOSE);
886891
if(!bio){
887892
net__socket_close(mosq);

lib/tls_mosq.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ int mosquitto__server_certificate_verify(int preverify_ok, X509_STORE_CTX *ctx)
5555
if(!preverify_ok) return 0;
5656

5757
ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
58+
if(!ssl) return 0;
5859
mosq = SSL_get_ex_data(ssl, tls_ex_index_mosq);
5960
if(!mosq) return 0;
6061

src/net.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,11 @@ struct mosquitto *net__socket_accept(struct mosquitto__listener_sock *listensock
211211
context__cleanup(new_context, true);
212212
return NULL;
213213
}
214-
SSL_set_ex_data(new_context->ssl, tls_ex_index_context, new_context);
215-
SSL_set_ex_data(new_context->ssl, tls_ex_index_listener, new_context->listener);
214+
if(!SSL_set_ex_data(new_context->ssl, tls_ex_index_context, new_context)
215+
|| !SSL_set_ex_data(new_context->ssl, tls_ex_index_listener, new_context->listener)){
216+
context__cleanup(new_context, true);
217+
return NULL;
218+
}
216219
new_context->want_write = true;
217220
bio = BIO_new_socket(new_sock, BIO_NOCLOSE);
218221
SSL_set_bio(new_context->ssl, bio, bio);
@@ -321,6 +324,13 @@ int net__tls_server_ctx(struct mosquitto__listener *listener)
321324
FILE *dhparamfile;
322325
DH *dhparam = NULL;
323326

327+
if(tls_ex_index_context == -1){
328+
tls_ex_index_context = SSL_get_ex_new_index(0, "client context", NULL, NULL, NULL);
329+
}
330+
if(tls_ex_index_listener == -1){
331+
tls_ex_index_listener = SSL_get_ex_new_index(0, "listener", NULL, NULL, NULL);
332+
}
333+
324334
if(listener->ssl_ctx){
325335
SSL_CTX_free(listener->ssl_ctx);
326336
}
@@ -918,13 +928,6 @@ int net__socket_listen(struct mosquitto__listener *listener)
918928
}
919929
# ifdef FINAL_WITH_TLS_PSK
920930
if(listener->psk_hint){
921-
if(tls_ex_index_context == -1){
922-
tls_ex_index_context = SSL_get_ex_new_index(0, "client context", NULL, NULL, NULL);
923-
}
924-
if(tls_ex_index_listener == -1){
925-
tls_ex_index_listener = SSL_get_ex_new_index(0, "listener", NULL, NULL, NULL);
926-
}
927-
928931
if(listener->certfile == NULL || listener->keyfile == NULL){
929932
if(net__tls_server_ctx(listener)){
930933
return 1;

0 commit comments

Comments
 (0)