X509V3_EXT_add_nconf_sk, X509v3_add_ext: fix errors handling
authorPavel Kopyl <p.kopyl@samsung.com>
Tue, 7 Nov 2017 12:28:18 +0000 (15:28 +0300)
committerMatt Caswell <matt@openssl.org>
Wed, 21 Feb 2018 12:22:56 +0000 (12:22 +0000)
X509v3_add_ext: free 'sk' if the memory pointed to by it
was malloc-ed inside this function.
X509V3_EXT_add_nconf_sk: return an error if X509v3_add_ext() fails.
This prevents use of a freed memory in do_body:sk_X509_EXTENSION_num().

Reviewed-by: Rich Salz <rsalz@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/4698)

crypto/x509/x509_v3.c
crypto/x509v3/v3_conf.c

index 213e762f986bd1d29c959554bfab00e0edf5a130..cbadd9be6f58d0527559afa5ff955d02f9f3deca 100644 (file)
@@ -128,7 +128,8 @@ STACK_OF(X509_EXTENSION) *X509v3_add_ext(STACK_OF(X509_EXTENSION) **x,
     X509err(X509_F_X509V3_ADD_EXT, ERR_R_MALLOC_FAILURE);
  err2:
     X509_EXTENSION_free(new_ex);
-    sk_X509_EXTENSION_free(sk);
+    if (x != NULL && *x == NULL)
+        sk_X509_EXTENSION_free(sk);
     return (NULL);
 }
 
index f625ff542eea98807162327a40c621e46c0d1012..68ceab1dd354095112ac8089deecb95cacc176a5 100644 (file)
@@ -313,8 +313,12 @@ int X509V3_EXT_add_nconf_sk(CONF *conf, X509V3_CTX *ctx, const char *section,
             return 0;
         if (ctx->flags == X509V3_CTX_REPLACE)
             delete_ext(*sk, ext);
-        if (sk)
-            X509v3_add_ext(sk, ext, -1);
+        if (sk != NULL) {
+            if (X509v3_add_ext(sk, ext, -1) == NULL) {
+                X509_EXTENSION_free(ext);
+                return 0;
+            }
+        }
         X509_EXTENSION_free(ext);
     }
     return 1;