]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
Don't leak memory if realloc fails.
authorDr. Stephen Henson <steve@openssl.org>
Wed, 11 May 2016 20:14:57 +0000 (21:14 +0100)
committerDr. Stephen Henson <steve@openssl.org>
Thu, 12 May 2016 11:02:38 +0000 (12:02 +0100)
RT#4403

Reviewed-by: Viktor Dukhovni <viktor@openssl.org>
apps/apps.c
apps/engine.c
crypto/modes/ocb128.c
ssl/ssl_rsa.c
ssl/t1_ext.c

index 537d43ab358b87d6b9fb1c94ac405af6e0a5f055..c7e01b0cc450de73efaf38f2d18b07fe0a45ee92 100644 (file)
@@ -176,8 +176,6 @@ int chopup_args(ARGS *arg, char *buf)
     if (arg->size == 0) {
         arg->size = 20;
         arg->argv = app_malloc(sizeof(*arg->argv) * arg->size, "argv space");
-        if (arg->argv == NULL)
-            return 0;
     }
 
     for (p = buf;;) {
@@ -189,11 +187,12 @@ int chopup_args(ARGS *arg, char *buf)
 
         /* The start of something good :-) */
         if (arg->argc >= arg->size) {
+            char **tmp;
             arg->size += 20;
-            arg->argv = OPENSSL_realloc(arg->argv,
-                                        sizeof(*arg->argv) * arg->size);
-            if (arg->argv == NULL)
+            tmp = OPENSSL_realloc(arg->argv, sizeof(*arg->argv) * arg->size);
+            if (tmp == NULL)
                 return 0;
+            arg->argv = tmp;
         }
         quoted = *p == '\'' || *p == '"';
         if (quoted)
index b60bfbc29416a536c5456f697a4638ef46e7879a..3b395b1c7d24cde2d60ebbfd592382edd15d15a6 100644 (file)
@@ -107,13 +107,17 @@ static int append_buf(char **buf, int *size, const char *s)
     }
 
     if (strlen(*buf) + strlen(s) >= (unsigned int)*size) {
+        char *tmp;
         *size += 256;
-        *buf = OPENSSL_realloc(*buf, *size);
+        tmp = OPENSSL_realloc(*buf, *size);
+        if (tmp == NULL) {
+            OPENSSL_free(*buf);
+            *buf = NULL;
+            return 0;
+        }
+        *buf = tmp;
     }
 
-    if (*buf == NULL)
-        return 0;
-
     if (**buf != '\0')
         OPENSSL_strlcat(*buf, ", ", *size);
     OPENSSL_strlcat(*buf, s, *size);
index 3c17aa52875a5a7664832c25a38f92a90f2237a5..cb99d094abbaf3c64234a9eb64f31d9d5ac71d92 100644 (file)
@@ -147,6 +147,7 @@ static OCB_BLOCK *ocb_lookup_l(OCB128_CONTEXT *ctx, size_t idx)
 
     /* We don't have it - so calculate it */
     if (idx >= ctx->max_l_index) {
+        void *tmp_ptr;
         /*
          * Each additional entry allows to process almost double as
          * much data, so that in linear world the table will need to
@@ -157,10 +158,11 @@ static OCB_BLOCK *ocb_lookup_l(OCB128_CONTEXT *ctx, size_t idx)
          * the index.
          */
         ctx->max_l_index += (idx - ctx->max_l_index + 4) & ~3;
-        ctx->l =
+        tmp_ptr =
             OPENSSL_realloc(ctx->l, ctx->max_l_index * sizeof(OCB_BLOCK));
-        if (ctx->l == NULL)
+        if (tmp_ptr == NULL) /* prevent ctx->l from being clobbered */
             return NULL;
+        ctx->l = tmp_ptr;
     }
     while (l_index < idx) {
         ocb_double(ctx->l + l_index, ctx->l + l_index + 1);
index f1280ad01fe29ac8e8dbfdf8fbf83ef51606ef36..88dce79aceca493f217569a3da87b86749ca2e3a 100644 (file)
@@ -940,6 +940,7 @@ int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
 int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
 {
     unsigned char *serverinfo = NULL;
+    unsigned char *tmp;
     size_t serverinfo_length = 0;
     unsigned char *extension = 0;
     long extension_length = 0;
@@ -999,12 +1000,13 @@ int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
             goto end;
         }
         /* Append the decoded extension to the serverinfo buffer */
-        serverinfo =
+        tmp =
             OPENSSL_realloc(serverinfo, serverinfo_length + extension_length);
-        if (serverinfo == NULL) {
+        if (tmp == NULL) {
             SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_MALLOC_FAILURE);
             goto end;
         }
+        serverinfo = tmp;
         memcpy(serverinfo + serverinfo_length, extension, extension_length);
         serverinfo_length += extension_length;
 
index 3bbe1fd82697560e96c76286f996ae4d87951ce0..281613185ec8d0f8cb441efbe2483914eaf017b3 100644 (file)
@@ -205,7 +205,7 @@ static int custom_ext_meth_add(custom_ext_methods *exts,
                                void *add_arg,
                                custom_ext_parse_cb parse_cb, void *parse_arg)
 {
-    custom_ext_method *meth;
+    custom_ext_method *meth, *tmp;
     /*
      * Check application error: if add_cb is not set free_cb will never be
      * called.
@@ -225,15 +225,17 @@ static int custom_ext_meth_add(custom_ext_methods *exts,
     /* Search for duplicate */
     if (custom_ext_find(exts, ext_type))
         return 0;
-    exts->meths = OPENSSL_realloc(exts->meths,
-                                  (exts->meths_count +
-                                   1) * sizeof(custom_ext_method));
+    tmp = OPENSSL_realloc(exts->meths,
+                          (exts->meths_count + 1) * sizeof(custom_ext_method));
 
-    if (!exts->meths) {
+    if (tmp == NULL) {
+        OPENSSL_free(exts->meths);
+        exts->meths = NULL;
         exts->meths_count = 0;
         return 0;
     }
 
+    exts->meths = tmp;
     meth = exts->meths + exts->meths_count;
     memset(meth, 0, sizeof(*meth));
     meth->parse_cb = parse_cb;