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

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