]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
Use static array (length 256) for copy of OPENSSL_MALLOC_FAILURES
authorsftcd <stephen.farrell@cs.tcd.ie>
Mon, 25 Nov 2024 16:01:39 +0000 (16:01 +0000)
committerTomas Mraz <tomas@openssl.org>
Thu, 28 Nov 2024 16:01:28 +0000 (17:01 +0100)
Reviewed-by: Tom Cosgrove <tom.cosgrove@arm.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/26039)

crypto/mem.c
doc/man3/OPENSSL_malloc.pod
include/openssl/crypto.h.in

index 032f2a9cd17bb6c0173cb86baeb1942566ff4f0e..c6cdfb36e1e6a2ff55ace72d2bf0ac735fddd1e2 100644 (file)
@@ -38,7 +38,8 @@ static TSAN_QUALIFIER int free_count;
 #  define LOAD(x)      tsan_load(&x)
 # endif /* TSAN_REQUIRES_LOCKING */
 
-static char *md_failstring;
+static char md_failbuf[CRYPTO_MEM_CHECK_MAX_FS + 1];
+static char *md_failstring = NULL;
 static long md_count;
 static int md_fail_percent = 0;
 static int md_tracefd = -1;
@@ -164,9 +165,17 @@ static int shouldfail(void)
 void ossl_malloc_setup_failures(void)
 {
     const char *cp = getenv("OPENSSL_MALLOC_FAILURES");
-
-    if (cp != NULL && (md_failstring = strdup(cp)) != NULL)
-        parseit();
+    size_t cplen = 0;
+
+    if (cp != NULL) {
+        /* if the value is too long we'll just ignore it */
+        cplen = strlen(cp);
+        if (cplen <= CRYPTO_MEM_CHECK_MAX_FS) {
+            strncpy(md_failbuf, cp, CRYPTO_MEM_CHECK_MAX_FS);
+            md_failstring = md_failbuf;
+            parseit();
+        }
+    }
     if ((cp = getenv("OPENSSL_MALLOC_FD")) != NULL)
         md_tracefd = atoi(cp);
     if ((cp = getenv("OPENSSL_MALLOC_SEED")) != NULL)
index 06bb192609a9b9be494928a142ac7165d121ef21..fccf3bd788da2ba1a7442186110bc0f198a51c81 100644 (file)
@@ -171,7 +171,8 @@ It is a set of fields separated by semicolons, which each field is a count
 to 100).  If the count is zero, then it lasts forever.  For example,
 C<100;@25> or C<100@0;0@25> means the first 100 allocations pass, then all
 other allocations (until the program exits or crashes) have a 25% chance of
-failing.
+failing. The length of the value of B<OPENSSL_MALLOC_FAILURES> must be 256 or
+fewer characters.
 
 If the variable B<OPENSSL_MALLOC_FD> is parsed as a positive integer, then
 it is taken as an open file descriptor. This is used in conjunction with
index 101719ab5b3a8132ba1c1361915c5414df036167..e0ace5e5a064c6aff73dc40d8edce592b49f511c 100644 (file)
@@ -368,6 +368,9 @@ void OPENSSL_cleanse(void *ptr, size_t len);
 # define CRYPTO_MEM_CHECK_ENABLE  0x2   /* Control and mode bit */
 # define CRYPTO_MEM_CHECK_DISABLE 0x3   /* Control only */
 
+/* max allowed length for value of OPENSSL_MALLOC_FAILURES env var. */
+#  define CRYPTO_MEM_CHECK_MAX_FS 256
+
 void CRYPTO_get_alloc_counts(int *mcount, int *rcount, int *fcount);
 #  ifndef OPENSSL_NO_DEPRECATED_3_0
 #    define OPENSSL_mem_debug_push(info) \