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:18:48 +0000 (12:18 +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 a09b0cef696c02294480d1db573f464e7cf8d654..b439030cfeac7e6c1a41a5d78238da0c6ef9105f 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 711ff02fb06b997b45d903f64683c6b81022d74b..59caa315801c15ae3a356c9f46adecb405c91503 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;