6b6d4b0
diff -up libgcrypt-1.8.3/src/global.c.fips-ctor libgcrypt-1.8.3/src/global.c
6b6d4b0
--- libgcrypt-1.8.3/src/global.c.fips-ctor	2017-11-23 19:25:58.000000000 +0100
d7ce942
+++ libgcrypt-1.8.3/src/global.c	2020-04-17 16:29:59.258218015 +0200
d7ce942
@@ -141,6 +141,34 @@ global_init (void)
6b6d4b0
 }
6b6d4b0
 
6b6d4b0
 
6b6d4b0
+#ifndef FIPS_MODULE_PATH
6b6d4b0
+#define FIPS_MODULE_PATH "/etc/system-fips"
6b6d4b0
+#endif
6b6d4b0
+
6b6d4b0
+void __attribute__ ((constructor)) _gcry_global_constructor (void)
6b6d4b0
+{
6b6d4b0
+  int rv;
6b6d4b0
+
6b6d4b0
+  rv = access (FIPS_MODULE_PATH, F_OK);
6b6d4b0
+  if (rv < 0 && errno != ENOENT)
6b6d4b0
+    rv = 0;
6b6d4b0
+
6b6d4b0
+  if (!rv)
6b6d4b0
+    {
d7ce942
+      int no_secmem_save;
d7ce942
+
d7ce942
+      /* it should be always 0 at this point but let's keep on the safe side */
d7ce942
+      no_secmem_save = no_secure_memory;
d7ce942
+      no_secure_memory = 1;
d7ce942
+      /* force selftests */
d7ce942
+      global_init();
6b6d4b0
+      _gcry_fips_run_selftests (0);
d7ce942
+      if (!fips_mode())
d7ce942
+         _gcry_random_close_fds ();
d7ce942
+      no_secure_memory = no_secmem_save;
6b6d4b0
+    }
6b6d4b0
+}
6b6d4b0
+
6b6d4b0
 /* This function is called by the macro fips_is_operational and makes
6b6d4b0
    sure that the minimal initialization has been done.  This is far
6b6d4b0
    from a perfect solution and hides problems with an improper
d7ce942
@@ -671,8 +699,7 @@ _gcry_vcontrol (enum gcry_ctl_cmds cmd,
6b6d4b0
 
6b6d4b0
     case GCRYCTL_FIPS_MODE_P:
6b6d4b0
       if (fips_mode ()
6b6d4b0
-          && !_gcry_is_fips_mode_inactive ()
6b6d4b0
-          && !no_secure_memory)
6b6d4b0
+          && !_gcry_is_fips_mode_inactive ())
6b6d4b0
 	rc = GPG_ERR_GENERAL; /* Used as TRUE value */
6b6d4b0
       break;
6b6d4b0
 
d7ce942
@@ -749,9 +776,9 @@ _gcry_vcontrol (enum gcry_ctl_cmds cmd,
6b6d4b0
       break;
6b6d4b0
 
6b6d4b0
     case GCRYCTL_SET_ENFORCED_FIPS_FLAG:
6b6d4b0
-      if (!any_init_done)
6b6d4b0
+      if (fips_mode ())
6b6d4b0
         {
911a1f2
-          /* Not yet initialized at all.  Set the enforced fips mode flag */
911a1f2
+          /* We are in FIPS mode, we can set the enforced fips mode flag. */
6b6d4b0
           _gcry_set_preferred_rng_type (0);
911a1f2
           _gcry_set_enforced_fips_mode ();
911a1f2
         }