From 2d54b8e89723ac45dc17cec416be83c7fc86bb68 Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Tue, 3 Dec 2019 12:20:30 +0000 Subject: [PATCH] coap_io.c: Stop calls to coap_dtls_handle_timeout() dereferencing the session Wrap calls to coap_dtls_handle_timeout() with coap_session_reference() and coap_session_release() so that the session pointer can continue to be used. Found when a DTLS session timed out using OpenSSL. Fixes CID 1484379 --- src/coap_io.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/coap_io.c b/src/coap_io.c index 88ca5263..f7b4e388 100644 --- a/src/coap_io.c +++ b/src/coap_io.c @@ -1429,6 +1429,8 @@ coap_write(coap_context_t *ctx, while (tls_timeout > 0 && tls_timeout <= now) { coap_log(LOG_DEBUG, "** %s: DTLS retransmit timeout\n", coap_session_str(s)); + /* Make sure the session object is not deleted in any callbacks */ + coap_session_reference(s); coap_dtls_handle_timeout(s); if (s->tls) tls_timeout = coap_dtls_get_timeout(s, now); @@ -1436,6 +1438,7 @@ coap_write(coap_context_t *ctx, tls_timeout = 0; timeout = 1; } + coap_session_release(s); } if (tls_timeout > 0 && (timeout == 0 || tls_timeout - now < timeout)) timeout = tls_timeout - now; @@ -1448,6 +1451,8 @@ coap_write(coap_context_t *ctx, coap_tick_t tls_timeout = coap_dtls_get_timeout(s, now); while (tls_timeout > 0 && tls_timeout <= now) { coap_log(LOG_DEBUG, "** %s: DTLS retransmit timeout\n", coap_session_str(s)); + /* Make sure the session object is not deleted in any callbacks */ + coap_session_reference(s); coap_dtls_handle_timeout(s); if (s->tls) tls_timeout = coap_dtls_get_timeout(s, now); @@ -1455,6 +1460,7 @@ coap_write(coap_context_t *ctx, tls_timeout = 0; timeout = 1; } + coap_session_release(s); } if (tls_timeout > 0 && (timeout == 0 || tls_timeout - now < timeout)) timeout = tls_timeout - now;