Blame SOURCES/0001-swtpm-Disable-OpenSSL-FIPS-mode-to-avoid-libtpms-fai.patch

f4d944
From a39c3792ba5677f25fea903b9f1a43740a5f2c0c Mon Sep 17 00:00:00 2001
f4d944
From: Stefan Berger <stefanb@linux.ibm.com>
f4d944
Date: Wed, 8 Jun 2022 09:19:07 -0400
f4d944
Subject: [PATCH] swtpm: Disable OpenSSL FIPS mode to avoid libtpms failures
f4d944
f4d944
While libtpms does not provide any means to disable FIPS-disabled crypto
f4d944
algorithms from being used, work around the issue by simply disabling the
f4d944
FIPS mode of OpenSSL if it is enabled. If it cannot be disabled, exit
f4d944
swtpm with a failure message that it cannot be disabled. If FIPS mode
f4d944
was successfully disabled, print out a message as well.
f4d944
f4d944
Buglink: https://bugzilla.redhat.com/show_bug.cgi?id=2090219
f4d944
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
f4d944
---
f4d944
 configure.ac              |   9 ++++
f4d944
 src/swtpm/Makefile.am     |   2 +
f4d944
 src/swtpm/cuse_tpm.c      |   5 ++
f4d944
 src/swtpm/fips.c          | 100 ++++++++++++++++++++++++++++++++++++++
f4d944
 src/swtpm/fips.h          |  43 ++++++++++++++++
f4d944
 src/swtpm/swtpm.c         |   3 ++
f4d944
 src/swtpm/swtpm_chardev.c |   3 ++
f4d944
 src/swtpm/utils.h         |   2 +
f4d944
 8 files changed, 167 insertions(+)
f4d944
 create mode 100644 src/swtpm/fips.c
f4d944
 create mode 100644 src/swtpm/fips.h
f4d944
f4d944
diff --git a/configure.ac b/configure.ac
f4d944
index ad3054e..30288c7 100644
f4d944
--- a/configure.ac
f4d944
+++ b/configure.ac
f4d944
@@ -156,6 +156,15 @@ openssl)
f4d944
 	AC_MSG_RESULT([Building with openssl crypto library])
f4d944
 	LIBCRYPTO_LIBS=$(pkg-config --libs libcrypto)
f4d944
 	AC_SUBST([LIBCRYPTO_LIBS])
f4d944
+	AC_CHECK_HEADERS([openssl/fips.h],
f4d944
+	                 [AC_DEFINE_UNQUOTED([HAVE_OPENSSL_FIPS_H], 1,
f4d944
+	                                     [whether openssl/fips.h is available])]
f4d944
+	                 )
f4d944
+	AC_CHECK_LIB(crypto,
f4d944
+		     [FIPS_mode_set],
f4d944
+		     [AC_DEFINE_UNQUOTED([HAVE_OPENSSL_FIPS_MODE_SET_API], 1,
f4d944
+		                         [whether FIPS_mode_set API is available])]
f4d944
+		     )
f4d944
 	;;
f4d944
 esac
f4d944
 
f4d944
diff --git a/src/swtpm/Makefile.am b/src/swtpm/Makefile.am
f4d944
index 5454a6f..2a65950 100644
f4d944
--- a/src/swtpm/Makefile.am
f4d944
+++ b/src/swtpm/Makefile.am
f4d944
@@ -11,6 +11,7 @@ noinst_HEADERS = \
f4d944
 	capabilities.h \
f4d944
 	common.h \
f4d944
 	ctrlchannel.h \
f4d944
+	fips.h \
f4d944
 	key.h \
f4d944
 	locality.h \
f4d944
 	logging.h \
f4d944
@@ -40,6 +41,7 @@ libswtpm_libtpms_la_SOURCES = \
f4d944
 	capabilities.c \
f4d944
 	common.c \
f4d944
 	ctrlchannel.c \
f4d944
+	fips.c \
f4d944
 	key.c \
f4d944
 	logging.c \
f4d944
 	mainloop.c \
f4d944
diff --git a/src/swtpm/cuse_tpm.c b/src/swtpm/cuse_tpm.c
f4d944
index 9dbc00d..3026e26 100644
f4d944
--- a/src/swtpm/cuse_tpm.c
f4d944
+++ b/src/swtpm/cuse_tpm.c
f4d944
@@ -1695,6 +1695,11 @@ int swtpm_cuse_main(int argc, char **argv, const char *prgname, const char *ifac
f4d944
         goto exit;
f4d944
     }
f4d944
 
f4d944
+    if (disable_fips_mode() < 0) {
f4d944
+        ret = -1;
f4d944
+        goto exit;
f4d944
+    }
f4d944
+
f4d944
     if (tpmlib_register_callbacks(&cbs) != TPM_SUCCESS) {
f4d944
         ret = -1;
f4d944
         goto exit;
f4d944
diff --git a/src/swtpm/fips.c b/src/swtpm/fips.c
f4d944
new file mode 100644
f4d944
index 0000000..eeb2a0c
f4d944
--- /dev/null
f4d944
+++ b/src/swtpm/fips.c
f4d944
@@ -0,0 +1,100 @@
f4d944
+/*
f4d944
+ * fips.c -- FIPS mode related functions
f4d944
+ *
f4d944
+ * (c) Copyright IBM Corporation 2022.
f4d944
+ *
f4d944
+ * Author: Stefan Berger <stefanb@us.ibm.com>
f4d944
+ *
f4d944
+ * All rights reserved.
f4d944
+ *
f4d944
+ * Redistribution and use in source and binary forms, with or without
f4d944
+ * modification, are permitted provided that the following conditions are
f4d944
+ * met:
f4d944
+ *
f4d944
+ * Redistributions of source code must retain the above copyright notice,
f4d944
+ * this list of conditions and the following disclaimer.
f4d944
+ *
f4d944
+ * Redistributions in binary form must reproduce the above copyright
f4d944
+ * notice, this list of conditions and the following disclaimer in the
f4d944
+ * documentation and/or other materials provided with the distribution.
f4d944
+ *
f4d944
+ * Neither the names of the IBM Corporation nor the names of its
f4d944
+ * contributors may be used to endorse or promote products derived from
f4d944
+ * this software without specific prior written permission.
f4d944
+ *
f4d944
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
f4d944
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
f4d944
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
f4d944
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
f4d944
+ * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
f4d944
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
f4d944
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
f4d944
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
f4d944
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
f4d944
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
f4d944
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
f4d944
+ */
f4d944
+
f4d944
+#include "config.h"
f4d944
+
f4d944
+#include "fips.h"
f4d944
+#include "logging.h"
f4d944
+
f4d944
+#if defined(HAVE_OPENSSL_FIPS_H)
f4d944
+# include <openssl/fips.h>
f4d944
+#elif defined(HAVE_OPENSSL_FIPS_MODE_SET_API)
f4d944
+/* Cygwin has no fips.h but API exists */
f4d944
+extern int FIPS_mode(void);
f4d944
+extern int FIPS_mode_set(int);
f4d944
+#endif
f4d944
+
f4d944
+#if OPENSSL_VERSION_NUMBER >= 0x30000000L
f4d944
+# include <openssl/evp.h>
f4d944
+#endif
f4d944
+
f4d944
+#include <openssl/err.h>
f4d944
+
f4d944
+/*
f4d944
+ * disable_fips_mode: If possible, disable FIPS mode to avoid libtpms failures
f4d944
+ *
f4d944
+ * While libtpms does not provide a solution to disable deactivated algorithms
f4d944
+ * avoid libtpms failures due to FIPS mode enablement by disabling FIPS mode.
f4d944
+ *
f4d944
+ * Returns < 0 on error, 0 otherwise.
f4d944
+ */
f4d944
+#if defined(HAVE_OPENSSL_FIPS_H) || defined(HAVE_OPENSSL_FIPS_MODE_SET_API)
f4d944
+int disable_fips_mode(void)
f4d944
+{
f4d944
+#if OPENSSL_VERSION_NUMBER >= 0x30000000L
f4d944
+    int mode = EVP_default_properties_is_fips_enabled(NULL);
f4d944
+#else
f4d944
+    int mode = FIPS_mode();
f4d944
+#endif
f4d944
+    int ret = 0;
f4d944
+
f4d944
+    if (mode != 0) {
f4d944
+#if OPENSSL_VERSION_NUMBER >= 0x30000000L
f4d944
+        int rc = EVP_default_properties_enable_fips(NULL, 0);
f4d944
+#else
f4d944
+        int rc = FIPS_mode_set(0);
f4d944
+#endif
f4d944
+        if (rc == 1) {
f4d944
+            logprintf(STDOUT_FILENO,
f4d944
+                      "Warning: Disabled OpenSSL FIPS mode\n");
f4d944
+        } else {
f4d944
+            unsigned long err = ERR_get_error();
f4d944
+            logprintf(STDERR_FILENO,
f4d944
+                      "Failed to disable OpenSSL FIPS mode: %s\n",
f4d944
+                      ERR_error_string(err, NULL));
f4d944
+            ret = -1;
f4d944
+        }
f4d944
+    }
f4d944
+    return ret;
f4d944
+}
f4d944
+#else
f4d944
+/* OpenBSD & DragonFlyBSD case */
f4d944
+int disable_fips_mode(void)
f4d944
+{
f4d944
+    return 0;
f4d944
+}
f4d944
+#endif
f4d944
diff --git a/src/swtpm/fips.h b/src/swtpm/fips.h
f4d944
new file mode 100644
f4d944
index 0000000..14d4e9f
f4d944
--- /dev/null
f4d944
+++ b/src/swtpm/fips.h
f4d944
@@ -0,0 +1,43 @@
f4d944
+/*
f4d944
+ * fips.h -- FIPS mode related functions
f4d944
+ *
f4d944
+ * (c) Copyright IBM Corporation 2015.
f4d944
+ *
f4d944
+ * Author: Stefan Berger <stefanb@us.ibm.com>
f4d944
+ *
f4d944
+ * All rights reserved.
f4d944
+ *
f4d944
+ * Redistribution and use in source and binary forms, with or without
f4d944
+ * modification, are permitted provided that the following conditions are
f4d944
+ * met:
f4d944
+ *
f4d944
+ * Redistributions of source code must retain the above copyright notice,
f4d944
+ * this list of conditions and the following disclaimer.
f4d944
+ *
f4d944
+ * Redistributions in binary form must reproduce the above copyright
f4d944
+ * notice, this list of conditions and the following disclaimer in the
f4d944
+ * documentation and/or other materials provided with the distribution.
f4d944
+ *
f4d944
+ * Neither the names of the IBM Corporation nor the names of its
f4d944
+ * contributors may be used to endorse or promote products derived from
f4d944
+ * this software without specific prior written permission.
f4d944
+ *
f4d944
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
f4d944
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
f4d944
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
f4d944
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
f4d944
+ * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
f4d944
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
f4d944
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
f4d944
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
f4d944
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
f4d944
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
f4d944
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
f4d944
+ */
f4d944
+
f4d944
+#ifndef _SWTPM_UTILS_H_
f4d944
+#define _SWTPM_UTILS_H_
f4d944
+
f4d944
+int disable_fips_mode(void);
f4d944
+
f4d944
+#endif /* _SWTPM_UTILS_H_ */
f4d944
diff --git a/src/swtpm/swtpm.c b/src/swtpm/swtpm.c
f4d944
index 722a743..e618c56 100644
f4d944
--- a/src/swtpm/swtpm.c
f4d944
+++ b/src/swtpm/swtpm.c
f4d944
@@ -521,6 +521,9 @@ int swtpm_main(int argc, char **argv, const char *prgname, const char *iface)
f4d944
         daemonize_finish();
f4d944
     }
f4d944
 
f4d944
+    if (disable_fips_mode() < 0)
f4d944
+        goto error_seccomp_profile;
f4d944
+
f4d944
     rc = mainLoop(&mlp, notify_fd[0]);
f4d944
 
f4d944
 error_seccomp_profile:
f4d944
diff --git a/src/swtpm/swtpm_chardev.c b/src/swtpm/swtpm_chardev.c
f4d944
index 9710927..ab6d8fd 100644
f4d944
--- a/src/swtpm/swtpm_chardev.c
f4d944
+++ b/src/swtpm/swtpm_chardev.c
f4d944
@@ -573,6 +573,9 @@ int swtpm_chardev_main(int argc, char **argv, const char *prgname, const char *i
f4d944
         daemonize_finish();
f4d944
     }
f4d944
 
f4d944
+    if (disable_fips_mode() < 0)
f4d944
+        goto error_seccomp_profile;
f4d944
+
f4d944
     rc = mainLoop(&mlp, notify_fd[0]);
f4d944
 
f4d944
 error_seccomp_profile:
f4d944
diff --git a/src/swtpm/utils.h b/src/swtpm/utils.h
f4d944
index 7502442..b8acd89 100644
f4d944
--- a/src/swtpm/utils.h
f4d944
+++ b/src/swtpm/utils.h
f4d944
@@ -71,4 +71,6 @@ ssize_t writev_full(int fd, const struct iovec *iov, int iovcnt);
f4d944
 
f4d944
 ssize_t read_eintr(int fd, void *buffer, size_t buflen);
f4d944
 
f4d944
+int disable_fips_mode(void);
f4d944
+
f4d944
 #endif /* _SWTPM_UTILS_H_ */
f4d944
-- 
f4d944
2.36.1
f4d944