gentleknife / rpms / libgcrypt

Forked from rpms/libgcrypt 4 years ago
Clone

Blame SOURCES/libgcrypt-1.8.3-fips-ctor.patch

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