From d6259332d4fa1f9e0482ce71ca43ea7e550ad2f3 Mon Sep 17 00:00:00 2001
From: Deomid Ryabkov <rojer@cesanta.com>
Date: Mon, 28 Nov 2016 15:15:31 +0000
Subject: [PATCH] Fix use-after-free

Free keys and cert before conf, not after

PUBLISHED_FROM=5628f56eb6973a779179d641fb425d0f14db39cf
---
 mongoose.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/mongoose.c b/mongoose.c
index fb9da35b4..a7157be99 100644
--- a/mongoose.c
+++ b/mongoose.c
@@ -4428,7 +4428,6 @@ static enum mg_ssl_if_result mg_ssl_if_mbed_err(struct mg_connection *nc,
 
 static void mg_ssl_if_mbed_free_certs_and_keys(struct mg_ssl_if_ctx *ctx) {
   if (ctx->cert != NULL) {
-    ctx->conf->key_cert = NULL;
     mbedtls_x509_crt_free(ctx->cert);
     MG_FREE(ctx->cert);
     ctx->cert = NULL;
@@ -4500,11 +4499,11 @@ void mg_ssl_if_conn_free(struct mg_connection *nc) {
     mbedtls_ssl_free(ctx->ssl);
     MG_FREE(ctx->ssl);
   }
+  mg_ssl_if_mbed_free_certs_and_keys(ctx);
   if (ctx->conf != NULL) {
     mbedtls_ssl_config_free(ctx->conf);
     MG_FREE(ctx->conf);
   }
-  mg_ssl_if_mbed_free_certs_and_keys(ctx);
   memset(ctx, 0, sizeof(*ctx));
   MG_FREE(ctx);
 }
-- 
GitLab