Blame SOURCES/openssl-1.1.1-cve-2019-1549.patch

b63792
diff -up openssl-1.1.1c/crypto/fips/fips.c.fork-safety openssl-1.1.1c/crypto/fips/fips.c
b63792
--- openssl-1.1.1c/crypto/fips/fips.c.fork-safety	2019-11-20 11:36:22.343506961 +0100
b63792
+++ openssl-1.1.1c/crypto/fips/fips.c	2019-11-21 17:44:32.920776849 +0100
b63792
@@ -472,7 +472,7 @@ int FIPS_module_mode_set(int onoff)
b63792
 
b63792
         fips_set_mode(onoff);
b63792
         /* force RNG reseed with entropy from getrandom() on next call */
b63792
-        rand_fork();
b63792
+        rand_force_reseed();
b63792
 
b63792
         ret = 1;
b63792
         goto end;
b63792
diff -up openssl-1.1.1c/crypto/include/internal/rand_int.h.fork-safety openssl-1.1.1c/crypto/include/internal/rand_int.h
b63792
--- openssl-1.1.1c/crypto/include/internal/rand_int.h.fork-safety	2019-11-20 11:36:22.382506277 +0100
b63792
+++ openssl-1.1.1c/crypto/include/internal/rand_int.h	2019-11-21 17:45:42.102456672 +0100
b63792
@@ -24,9 +24,9 @@
b63792
 typedef struct rand_pool_st RAND_POOL;
b63792
 
b63792
 void rand_cleanup_int(void);
b63792
+void rand_force_reseed(void);
b63792
 void rand_drbg_cleanup_int(void);
b63792
 void drbg_delete_thread_state(void);
b63792
-void rand_fork(void);
b63792
 
b63792
 /* Hardware-based seeding functions. */
b63792
 size_t rand_acquire_entropy_from_tsc(RAND_POOL *pool);
b63792
diff -up openssl-1.1.1c/crypto/init.c.fork-safety openssl-1.1.1c/crypto/init.c
b63792
--- openssl-1.1.1c/crypto/init.c.fork-safety	2019-05-28 15:12:21.000000000 +0200
b63792
+++ openssl-1.1.1c/crypto/init.c	2019-11-21 17:34:13.478597398 +0100
b63792
@@ -847,6 +847,5 @@ void OPENSSL_fork_parent(void)
b63792
 
b63792
 void OPENSSL_fork_child(void)
b63792
 {
b63792
-    rand_fork();
b63792
 }
b63792
 #endif
b63792
diff -up openssl-1.1.1c/crypto/rand/drbg_lib.c.fork-safety openssl-1.1.1c/crypto/rand/drbg_lib.c
b63792
--- openssl-1.1.1c/crypto/rand/drbg_lib.c.fork-safety	2019-11-20 11:36:22.383506260 +0100
b63792
+++ openssl-1.1.1c/crypto/rand/drbg_lib.c	2019-11-21 17:46:37.583397431 +0100
b63792
@@ -197,7 +197,7 @@ static RAND_DRBG *rand_drbg_new(int secu
b63792
     }
b63792
 
b63792
     drbg->secure = secure && CRYPTO_secure_allocated(drbg);
b63792
-    drbg->fork_count = rand_fork_count;
b63792
+    drbg->fork_id = openssl_get_fork_id();
b63792
     drbg->parent = parent;
b63792
 
b63792
     if (parent == NULL) {
b63792
@@ -583,6 +583,7 @@ int RAND_DRBG_generate(RAND_DRBG *drbg,
b63792
                        int prediction_resistance,
b63792
                        const unsigned char *adin, size_t adinlen)
b63792
 {
b63792
+    int fork_id;
b63792
     int reseed_required = 0;
b63792
 
b63792
     if (drbg->state != DRBG_READY) {
b63792
@@ -608,8 +609,10 @@ int RAND_DRBG_generate(RAND_DRBG *drbg,
b63792
         return 0;
b63792
     }
b63792
 
b63792
-    if (drbg->fork_count != rand_fork_count) {
b63792
-        drbg->fork_count = rand_fork_count;
b63792
+    fork_id = openssl_get_fork_id();
b63792
+
b63792
+    if (drbg->fork_id != fork_id) {
b63792
+        drbg->fork_id = fork_id;
b63792
         reseed_required = 1;
b63792
     }
b63792
 
b63792
@@ -1011,6 +1014,20 @@ size_t rand_drbg_seedlen(RAND_DRBG *drbg
b63792
     return min_entropy > min_entropylen ? min_entropy : min_entropylen;
b63792
 }
b63792
 
b63792
+void rand_force_reseed(void)
b63792
+{
b63792
+    RAND_DRBG *drbg;
b63792
+
b63792
+    drbg = RAND_DRBG_get0_master();
b63792
+    drbg->fork_id = 0;
b63792
+
b63792
+    drbg = RAND_DRBG_get0_private();
b63792
+    drbg->fork_id = 0;
b63792
+
b63792
+    drbg = RAND_DRBG_get0_public();
b63792
+    drbg->fork_id = 0;
b63792
+}
b63792
+
b63792
 /* Implements the default OpenSSL RAND_add() method */
b63792
 static int drbg_add(const void *buf, int num, double randomness)
b63792
 {
b63792
diff -up openssl-1.1.1c/crypto/rand/rand_lcl.h.fork-safety openssl-1.1.1c/crypto/rand/rand_lcl.h
b63792
--- openssl-1.1.1c/crypto/rand/rand_lcl.h.fork-safety	2019-11-20 11:36:22.383506260 +0100
b63792
+++ openssl-1.1.1c/crypto/rand/rand_lcl.h	2019-11-21 17:34:13.485597265 +0100
b63792
@@ -176,12 +176,12 @@ struct rand_drbg_st {
b63792
     int secure; /* 1: allocated on the secure heap, 0: otherwise */
b63792
     int type; /* the nid of the underlying algorithm */
b63792
     /*
b63792
-     * Stores the value of the rand_fork_count global as of when we last
b63792
-     * reseeded.  The DRBG reseeds automatically whenever drbg->fork_count !=
b63792
-     * rand_fork_count.  Used to provide fork-safety and reseed this DRBG in
b63792
-     * the child process.
b63792
+     * Stores the return value of openssl_get_fork_id() as of when we last
b63792
+     * reseeded.  The DRBG reseeds automatically whenever drbg->fork_id !=
b63792
+     * openssl_get_fork_id().  Used to provide fork-safety and reseed this
b63792
+     * DRBG in the child process.
b63792
      */
b63792
-    int fork_count;
b63792
+    int fork_id;
b63792
     unsigned short flags; /* various external flags */
b63792
 
b63792
     /*
b63792
@@ -273,19 +273,6 @@ struct rand_drbg_st {
b63792
 /* The global RAND method, and the global buffer and DRBG instance. */
b63792
 extern RAND_METHOD rand_meth;
b63792
 
b63792
-/*
b63792
- * A "generation count" of forks.  Incremented in the child process after a
b63792
- * fork.  Since rand_fork_count is increment-only, and only ever written to in
b63792
- * the child process of the fork, which is guaranteed to be single-threaded, no
b63792
- * locking is needed for normal (read) accesses; the rest of pthread fork
b63792
- * processing is assumed to introduce the necessary memory barriers.  Sibling
b63792
- * children of a given parent will produce duplicate values, but this is not
b63792
- * problematic because the reseeding process pulls input from the system CSPRNG
b63792
- * and/or other global sources, so the siblings will end up generating
b63792
- * different output streams.
b63792
- */
b63792
-extern int rand_fork_count;
b63792
-
b63792
 /* DRBG helpers */
b63792
 int rand_drbg_restart(RAND_DRBG *drbg,
b63792
                       const unsigned char *buffer, size_t len, size_t entropy);
b63792
diff -up openssl-1.1.1c/crypto/rand/rand_lib.c.fork-safety openssl-1.1.1c/crypto/rand/rand_lib.c
b63792
--- openssl-1.1.1c/crypto/rand/rand_lib.c.fork-safety	2019-11-20 11:36:22.374506418 +0100
b63792
+++ openssl-1.1.1c/crypto/rand/rand_lib.c	2019-11-21 17:34:13.487597227 +0100
b63792
@@ -30,8 +30,6 @@ static CRYPTO_RWLOCK *rand_meth_lock;
b63792
 static const RAND_METHOD *default_RAND_meth;
b63792
 static CRYPTO_ONCE rand_init = CRYPTO_ONCE_STATIC_INIT;
b63792
 
b63792
-int rand_fork_count;
b63792
-
b63792
 static CRYPTO_RWLOCK *rand_nonce_lock;
b63792
 static int rand_nonce_count;
b63792
 
b63792
@@ -303,11 +301,6 @@ void rand_drbg_cleanup_additional_data(R
b63792
     rand_pool_reattach(pool, out);
b63792
 }
b63792
 
b63792
-void rand_fork(void)
b63792
-{
b63792
-    rand_fork_count++;
b63792
-}
b63792
-
b63792
 DEFINE_RUN_ONCE_STATIC(do_rand_init)
b63792
 {
b63792
 #ifndef OPENSSL_NO_ENGINE
b63792
diff -up openssl-1.1.1c/crypto/threads_none.c.fork-safety openssl-1.1.1c/crypto/threads_none.c
b63792
--- openssl-1.1.1c/crypto/threads_none.c.fork-safety	2019-05-28 15:12:21.000000000 +0200
b63792
+++ openssl-1.1.1c/crypto/threads_none.c	2019-11-21 17:34:13.489597189 +0100
b63792
@@ -12,6 +12,11 @@
b63792
 
b63792
 #if !defined(OPENSSL_THREADS) || defined(CRYPTO_TDEBUG)
b63792
 
b63792
+# if defined(OPENSSL_SYS_UNIX)
b63792
+#  include <sys/types.h>
b63792
+#  include <unistd.h>
b63792
+# endif
b63792
+
b63792
 CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
b63792
 {
b63792
     CRYPTO_RWLOCK *lock;
b63792
@@ -133,4 +138,12 @@ int openssl_init_fork_handlers(void)
b63792
     return 0;
b63792
 }
b63792
 
b63792
+int openssl_get_fork_id(void)
b63792
+{
b63792
+# if defined(OPENSSL_SYS_UNIX)
b63792
+    return getpid();
b63792
+# else
b63792
+    return 0;
b63792
+# endif
b63792
+}
b63792
 #endif
b63792
diff -up openssl-1.1.1c/crypto/threads_pthread.c.fork-safety openssl-1.1.1c/crypto/threads_pthread.c
b63792
--- openssl-1.1.1c/crypto/threads_pthread.c.fork-safety	2019-05-28 15:12:21.000000000 +0200
b63792
+++ openssl-1.1.1c/crypto/threads_pthread.c	2019-11-21 17:34:13.492597131 +0100
b63792
@@ -12,6 +12,11 @@
b63792
 
b63792
 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG) && !defined(OPENSSL_SYS_WINDOWS)
b63792
 
b63792
+# if defined(OPENSSL_SYS_UNIX)
b63792
+#  include <sys/types.h>
b63792
+#  include <unistd.h>
b63792
+#endif
b63792
+
b63792
 # ifdef PTHREAD_RWLOCK_INITIALIZER
b63792
 #  define USE_RWLOCK
b63792
 # endif
b63792
@@ -193,4 +198,9 @@ int openssl_init_fork_handlers(void)
b63792
 # endif
b63792
     return 0;
b63792
 }
b63792
+
b63792
+int openssl_get_fork_id(void)
b63792
+{
b63792
+    return getpid();
b63792
+}
b63792
 #endif
b63792
diff -up openssl-1.1.1c/crypto/threads_win.c.fork-safety openssl-1.1.1c/crypto/threads_win.c
b63792
--- openssl-1.1.1c/crypto/threads_win.c.fork-safety	2019-05-28 15:12:21.000000000 +0200
b63792
+++ openssl-1.1.1c/crypto/threads_win.c	2019-11-21 17:34:13.495597074 +0100
b63792
@@ -164,4 +164,8 @@ int openssl_init_fork_handlers(void)
b63792
     return 0;
b63792
 }
b63792
 
b63792
+int openssl_get_fork_id(void)
b63792
+{
b63792
+    return 0;
b63792
+}
b63792
 #endif
b63792
diff -up openssl-1.1.1c/include/internal/cryptlib.h.fork-safety openssl-1.1.1c/include/internal/cryptlib.h
b63792
--- openssl-1.1.1c/include/internal/cryptlib.h.fork-safety	2019-05-28 15:12:21.000000000 +0200
b63792
+++ openssl-1.1.1c/include/internal/cryptlib.h	2019-11-21 17:34:13.497597036 +0100
b63792
@@ -80,6 +80,7 @@ extern unsigned int OPENSSL_ia32cap_P[];
b63792
 void OPENSSL_showfatal(const char *fmta, ...);
b63792
 void crypto_cleanup_all_ex_data_int(void);
b63792
 int openssl_init_fork_handlers(void);
b63792
+int openssl_get_fork_id(void);
b63792
 
b63792
 char *ossl_safe_getenv(const char *name);
b63792
 
b63792
diff -up openssl-1.1.1c/test/drbgtest.c.fork-safety openssl-1.1.1c/test/drbgtest.c
b63792
--- openssl-1.1.1c/test/drbgtest.c.fork-safety	2019-11-20 11:36:22.384506242 +0100
b63792
+++ openssl-1.1.1c/test/drbgtest.c	2019-11-21 17:34:13.499596998 +0100
b63792
@@ -22,6 +22,13 @@
b63792
 # include <windows.h>
b63792
 #endif
b63792
 
b63792
+
b63792
+#if defined(OPENSSL_SYS_UNIX)
b63792
+# include <sys/types.h>
b63792
+# include <sys/wait.h>
b63792
+# include <unistd.h>
b63792
+#endif
b63792
+
b63792
 #include "testutil.h"
b63792
 #include "drbgtest.h"
b63792
 
b63792
@@ -696,6 +703,40 @@ static int test_drbg_reseed(int expect_s
b63792
     return 1;
b63792
 }
b63792
 
b63792
+
b63792
+#if defined(OPENSSL_SYS_UNIX)
b63792
+/*
b63792
+ * Test whether master, public and private DRBG are reseeded after
b63792
+ * forking the process.
b63792
+ */
b63792
+static int test_drbg_reseed_after_fork(RAND_DRBG *master,
b63792
+                                       RAND_DRBG *public,
b63792
+                                       RAND_DRBG *private)
b63792
+{
b63792
+    pid_t pid;
b63792
+    int status=0;
b63792
+
b63792
+    pid = fork();
b63792
+    if (!TEST_int_ge(pid, 0))
b63792
+        return 0;
b63792
+
b63792
+    if (pid > 0) {
b63792
+        /* I'm the parent; wait for the child and check its exit code */
b63792
+        return TEST_int_eq(waitpid(pid, &status, 0), pid) && TEST_int_eq(status, 0);
b63792
+    }
b63792
+
b63792
+    /* I'm the child; check whether all three DRBGs reseed. */
b63792
+    if (!TEST_true(test_drbg_reseed(1, master, public, private, 1, 1, 1, 0)))
b63792
+        status = 1;
b63792
+
b63792
+    /* Remove hooks  */
b63792
+    unhook_drbg(master);
b63792
+    unhook_drbg(public);
b63792
+    unhook_drbg(private);
b63792
+    exit(status);
b63792
+}
b63792
+#endif
b63792
+
b63792
 /*
b63792
  * Test whether the default rand_method (RAND_OpenSSL()) is
b63792
  * setup correctly, in particular whether reseeding  works
b63792
@@ -786,6 +827,10 @@ static int test_rand_drbg_reseed(void)
b63792
         goto error;
b63792
     reset_drbg_hook_ctx();
b63792
 
b63792
+#if defined(OPENSSL_SYS_UNIX)
b63792
+    if (!TEST_true(test_drbg_reseed_after_fork(master, public, private)))
b63792
+        goto error;
b63792
+#endif
b63792
 
b63792
     /* fill 'randomness' buffer with some arbitrary data */
b63792
     memset(rand_add_buf, 'r', sizeof(rand_add_buf));