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

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