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

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