cd0318
From: Daiki Ueno <ueno@gnu.org>
cd0318
Date: Fri, 19 Aug 2022 12:32:27 +0900
cd0318
Subject: [PATCH] build: allow GMP to be statically linked
cd0318
cd0318
Even though we set the custom allocator[1] to zeroize sensitive data,
cd0318
it can be easily invalidated if the application sets its own custom
cd0318
allocator.  An approach to prevent that is to link against a static
cd0318
library of GMP, so the use of GMP is privatized and the custom
cd0318
allocator configuration is not shared with other applications.
cd0318
cd0318
This patch allows libgnutls to be linked with the static library of
cd0318
GMP.  Note that, to this work libgmp.a needs to be compiled with -fPIC
cd0318
and libhogweed in Nettle is also linked to the static library of GMP.
cd0318
cd0318
1. https://gitlab.com/gnutls/gnutls/-/merge_requests/1554
cd0318
cd0318
Signed-off-by: Daiki Ueno <ueno@gnu.org>
cd0318
---
7e1ffc
diff --color -ruNp a/configure.ac b/configure.ac
7e1ffc
--- a/configure.ac	2022-12-15 11:06:16.782726043 +0100
7e1ffc
+++ b/configure.ac	2022-12-15 11:08:35.603451427 +0100
7e1ffc
@@ -744,6 +744,8 @@ AC_CHECK_FUNCS(nettle_cmac_kuznyechik_up
cd0318
 LIBS=$save_LIBS
cd0318
 
cd0318
 # Check sonames of the linked libraries needed for FIPS selftests.
cd0318
+save_CFLAGS=$CFLAGS
cd0318
+CFLAGS="$CFLAGS $GMP_CFLAGS"
cd0318
 save_LIBS=$LIBS
cd0318
 LIBS="$LIBS $GMP_LIBS"
cd0318
 AC_MSG_CHECKING([gmp soname])
7e1ffc
@@ -757,9 +759,14 @@ if test -z "$gmp_so"; then
cd0318
 	gmp_so=none
cd0318
 fi
cd0318
 AC_MSG_RESULT($gmp_so)
cd0318
-AC_DEFINE_UNQUOTED([GMP_LIBRARY_SONAME], ["$gmp_so"], [The soname of gmp library])
cd0318
+if test "$gmp_so" != none; then
cd0318
+	AC_DEFINE_UNQUOTED([GMP_LIBRARY_SONAME], ["$gmp_so"], [The soname of gmp library])
cd0318
+fi
cd0318
 LIBS=$save_LIBS
cd0318
+CFLAGS=$save_CFLAGS
cd0318
 
cd0318
+save_CFLAGS=$CFLAGS
cd0318
+CFLAGS="$CFLAGS $NETTLE_CFLAGS"
cd0318
 save_LIBS=$LIBS
cd0318
 LIBS="$LIBS $NETTLE_LIBS"
cd0318
 AC_MSG_CHECKING([nettle soname])
7e1ffc
@@ -775,7 +782,11 @@ fi
cd0318
 AC_MSG_RESULT($nettle_so)
cd0318
 AC_DEFINE_UNQUOTED([NETTLE_LIBRARY_SONAME], ["$nettle_so"], [The soname of nettle library])
cd0318
 LIBS=$save_LIBS
cd0318
+CFLAGS=$save_CFLAGS
cd0318
 
cd0318
+save_CFLAGS=$CFLAGS
cd0318
+# <nettle/bignum.h> includes <gmp.h>
cd0318
+CFLAGS="$CFLAGS $HOGWEED_CFLAGS $GMP_CFLAGS"
cd0318
 save_LIBS=$LIBS
cd0318
 LIBS="$LIBS $HOGWEED_LIBS"
cd0318
 AC_MSG_CHECKING([hogweed soname])
7e1ffc
@@ -791,6 +802,7 @@ fi
cd0318
 AC_MSG_RESULT($hogweed_so)
cd0318
 AC_DEFINE_UNQUOTED([HOGWEED_LIBRARY_SONAME], ["$hogweed_so"], [The soname of hogweed library])
cd0318
 LIBS=$save_LIBS
cd0318
+CFLAGS=$save_CFLAGS
cd0318
 
cd0318
 gnutls_so=libgnutls.so.`expr "$LT_CURRENT" - "$LT_AGE"`
cd0318
 AC_DEFINE_UNQUOTED([GNUTLS_LIBRARY_SONAME], ["$gnutls_so"], [The soname of gnutls library])
7e1ffc
diff --color -ruNp a/lib/fips.c b/lib/fips.c
7e1ffc
--- a/lib/fips.c	2022-12-15 11:06:16.868727731 +0100
7e1ffc
+++ b/lib/fips.c	2022-12-15 11:12:42.744303409 +0100
7e1ffc
@@ -155,7 +155,11 @@ void _gnutls_fips_mode_reset_zombie(void
cd0318
 #define GNUTLS_LIBRARY_NAME GNUTLS_LIBRARY_SONAME
cd0318
 #define NETTLE_LIBRARY_NAME NETTLE_LIBRARY_SONAME
cd0318
 #define HOGWEED_LIBRARY_NAME HOGWEED_LIBRARY_SONAME
cd0318
+
7e1ffc
+/* GMP can be statically linked */
cd0318
+#ifdef GMP_LIBRARY_SONAME
cd0318
 #define GMP_LIBRARY_NAME GMP_LIBRARY_SONAME
cd0318
+#endif
cd0318
 
cd0318
 #define HMAC_SIZE 32
cd0318
 #define HMAC_ALGO GNUTLS_MAC_SHA256
7e1ffc
@@ -173,7 +177,9 @@ struct hmac_file
cd0318
 	struct hmac_entry gnutls;
cd0318
 	struct hmac_entry nettle;
cd0318
 	struct hmac_entry hogweed;
cd0318
+#ifdef GMP_LIBRARY_SONAME
cd0318
 	struct hmac_entry gmp;
cd0318
+#endif
7e1ffc
 };
7e1ffc
 
7e1ffc
 struct lib_paths
7e1ffc
@@ -181,7 +187,9 @@ struct lib_paths
7e1ffc
 	char gnutls[GNUTLS_PATH_MAX];
7e1ffc
 	char nettle[GNUTLS_PATH_MAX];
7e1ffc
 	char hogweed[GNUTLS_PATH_MAX];
7e1ffc
+#ifdef GMP_LIBRARY_SONAME
7e1ffc
 	char gmp[GNUTLS_PATH_MAX];
7e1ffc
+#endif
7e1ffc
 };
cd0318
 
7e1ffc
 /*
7e1ffc
@@ -245,8 +253,10 @@ static int handler(void *user, const cha
cd0318
 		return lib_handler(&p->nettle, section, name, value);
cd0318
 	} else if (!strcmp(section, HOGWEED_LIBRARY_NAME)) {
cd0318
 		return lib_handler(&p->hogweed, section, name, value);
cd0318
+#ifdef GMP_LIBRARY_SONAME
cd0318
 	} else if (!strcmp(section, GMP_LIBRARY_NAME)) {
cd0318
 		return lib_handler(&p->gmp, section, name, value);
cd0318
+#endif
cd0318
 	} else {
cd0318
 		return 0;
cd0318
 	}
7e1ffc
@@ -389,8 +399,10 @@ static int callback(struct dl_phdr_info
7e1ffc
 		_gnutls_str_cpy(paths->nettle, GNUTLS_PATH_MAX, path);
7e1ffc
 	else if (!strcmp(soname, HOGWEED_LIBRARY_SONAME))
7e1ffc
 		_gnutls_str_cpy(paths->hogweed, GNUTLS_PATH_MAX, path);
7e1ffc
+#ifdef GMP_LIBRARY_SONAME
7e1ffc
 	else if (!strcmp(soname, GMP_LIBRARY_SONAME))
7e1ffc
 		_gnutls_str_cpy(paths->gmp, GNUTLS_PATH_MAX, path);
7e1ffc
+#endif
7e1ffc
 	return 0;
7e1ffc
 }
7e1ffc
 
7e1ffc
@@ -411,10 +423,12 @@ static int load_lib_paths(struct lib_pat
7e1ffc
 		_gnutls_debug_log("Hogweed library path was not found\n");
7e1ffc
 		return gnutls_assert_val(GNUTLS_E_FILE_ERROR);
7e1ffc
 	}
7e1ffc
+#ifdef GMP_LIBRARY_SONAME
7e1ffc
 	if (paths->gmp[0] == '\0') {
7e1ffc
 		_gnutls_debug_log("Gmp library path was not found\n");
7e1ffc
 		return gnutls_assert_val(GNUTLS_E_FILE_ERROR);
7e1ffc
 	}
7e1ffc
+#endif
7e1ffc
 
7e1ffc
 	return GNUTLS_E_SUCCESS;
7e1ffc
 }
7e1ffc
@@ -467,9 +481,11 @@ static int check_binary_integrity(void)
7e1ffc
 	ret = check_lib_hmac(&hmac.hogweed, paths.hogweed);
cd0318
 	if (ret < 0)
cd0318
 		return ret;
cd0318
+#ifdef GMP_LIBRARY_SONAME
7e1ffc
 	ret = check_lib_hmac(&hmac.gmp, paths.gmp);
cd0318
 	if (ret < 0)
cd0318
 		return ret;
cd0318
+#endif
cd0318
 
cd0318
 	return 0;
cd0318
 }
7e1ffc
diff --color -ruNp a/lib/fipshmac.c b/lib/fipshmac.c
7e1ffc
--- a/lib/fipshmac.c	2022-12-15 11:06:16.785726102 +0100
7e1ffc
+++ b/lib/fipshmac.c	2022-12-15 11:13:34.533320156 +0100
7e1ffc
@@ -107,8 +107,10 @@ static int callback(struct dl_phdr_info
7e1ffc
 		return print_lib(path, soname);
7e1ffc
 	if (!strcmp(soname, HOGWEED_LIBRARY_SONAME))
7e1ffc
 		return print_lib(path, soname);
cd0318
+#ifdef GMP_LIBRARY_SONAME
7e1ffc
 	if (!strcmp(soname, GMP_LIBRARY_SONAME))
7e1ffc
 		return print_lib(path, soname);
cd0318
+#endif
7e1ffc
         return 0;
cd0318
 }
7e1ffc
 
7e1ffc
diff --color -ruNp a/lib/global.c b/lib/global.c
7e1ffc
--- a/lib/global.c	2022-12-15 11:06:16.061711888 +0100
7e1ffc
+++ b/lib/global.c	2022-12-15 11:08:35.604451446 +0100
7e1ffc
@@ -540,7 +540,9 @@ static const struct gnutls_library_confi
cd0318
 	{ "libgnutls-soname", GNUTLS_LIBRARY_SONAME },
cd0318
 	{ "libnettle-soname", NETTLE_LIBRARY_SONAME },
cd0318
 	{ "libhogweed-soname", HOGWEED_LIBRARY_SONAME },
cd0318
+#ifdef GMP_LIBRARY_SONAME
cd0318
 	{ "libgmp-soname", GMP_LIBRARY_SONAME },
cd0318
+#endif
cd0318
 	{ "hardware-features", HW_FEATURES },
cd0318
 	{ "tls-features", TLS_FEATURES },
cd0318
 	{ NULL, NULL }