Skip to contentSkip to conversation

libcrypto: Provide more __cleanup functions for zeroizing data

PATCH Superseded Thomas Huth 4 participants 20 messages Aug 13 – Sep 16

Subsystems (4)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover libcrypto: Provide more __cleanup functions for zeroizing data

Code that uses crypto-related structures (containing keys or context data) should zeroize their local structures on the stack after use to avoid leaking this sensitive material via the stack when the function returns. Using the __cleanup() marker is a very elegant way to assert that the data is zeroized without having to painfully verify that each early return in a function might miss it.

Thus this series introduces zeroization functions for many crypto-related structures that can be used with __cleanup(). The series focuses on the introduction of the functions - most call sights will be adjusted to use these new functions in separate patch series later (since each subsystem needs separate review from the corresponding maintainer). However, I already included the two "safexcel" patches, since they already got ack'ed by the maintainer Antoine, so I think they should be fine to go via the libcrypto tree.

Note there is one minor ugliness in patch 10: Since sha2.h is also used in the x86 purgatory code, and that code ships with its own implementation of string functions, we have to compile the purgatory.c file with -D__NO_FORTIFY now to be able to include <linux/string.h> in sha2.h. I hope that solution is OK (especially since the sha256.c file in the same folder gets that treatment already, too), if not - I'm certainly open for other suggestions here!

Thomas Huth (11):
lib/crypto: aes: Provide a wrapper function for zeroizing
crypto_aes_ctx
crypto: safexcel - Simplify the check for a valid AES key
crypto: safexcel - zeroize crypto_aes_ctx with
__cleanup(aes_zeroize_ctx)
lib/crypto: aes: Provide functions for zeroizing aes_key and
aes_enckey
lib/crypto: aes: Use aes_zeroize_*key() instead of memzero_explicit() lib/crypto: md5: Provide a function for zeroizing hmac_md5_ctx
structures
lib/crypto: md5: Use hmac_md5_zeroize_ctx() instead of
memzero_explicit()
lib/crypto: sha1: Provide a wrapper for zeroizing hmac_sha1_ctx lib/crypto: sha1: Use hmac_sha1_zeroize_ctx() instead of
memzero_explicit()
x86/purgatory: Compile purgatory.c with -D__NO_FORTIFY lib/crypto: sha2: Provide wrappers for zeroizing SHA2 hmac_sha*_ctx
structures

 arch/x86/purgatory/Makefile                   |  1 +
 .../crypto/inside-secure/safexcel_cipher.c    | 16 ++----
 drivers/crypto/inside-secure/safexcel_hash.c  |  3 +-
 include/crypto/aes.h                          | 40 +++++++++++++
 include/crypto/md5.h                          | 15 +++++
 include/crypto/sha1.h                         | 17 ++++++
 include/crypto/sha2.h                         | 57 +++++++++++++++++++
 lib/crypto/aes.c                              | 10 ++--
 lib/crypto/md5.c                              |  2 +-
 lib/crypto/sha1.c                             |  2 +-
 10 files changed, 143 insertions(+), 20 deletions(-)
Signature · 1 line
2.55.0
#1 lore raw

PATCH 1/11 lib/crypto: aes: Provide a wrapper function for zeroizing crypto_aes_ctx

From: Thomas Huth <[email protected]>

Several crypto drivers need to zeroize their local crypto_aes_ctx structures after use to avoid leaking key material on the stack. Currently some call sites do this with their own memzero_explicit() call, which is error-prone since it is easy to miss a return path (what already happened in some drivers). Some other call sites miss to clear crypto_aes_ctx completely.

Provide an aes_zeroize_ctx() helper that can be used with __cleanup() to automatically zeroize the context when it goes out of scope.

---
 include/crypto/aes.h | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
include/crypto/aes.h +14 -0
index 16fbfd93e2bd0..faf1d1b75a15f 100644
@@ -8,6 +8,7 @@
 
 #include <linux/types.h>
 #include <linux/crypto.h>
+#include <linux/string.h>
 
 #define AES_MIN_KEY_SIZE	16
 #define AES_MAX_KEY_SIZE	32
@@ -125,6 +126,19 @@ struct crypto_aes_ctx {
 	u32 key_length;
 };
 
+/**
+ * aes_zeroize_ctx - Clear a crypto_aes_ctx structure
+ * @ctx: The location of the context that should be zeroized
+ *
+ * This function explicitly fills the crypto_aes_ctx with zeroes. For
+ * example, use it with __cleanup() for local crypto_aes_ctx structures on
+ * the stack to avoid that their content is leaked when the context is left.
+ */
+static inline void aes_zeroize_ctx(struct crypto_aes_ctx *ctx)
+{
+	memzero_explicit(ctx, sizeof(*ctx));
+}
+
 /*
  * validate key length for AES algorithms
  */
Signature · 1 line
2.55.0
#2 lore raw

PATCH 2/11 crypto: safexcel - Simplify the check for a valid AES key

In reply to Thomas Huth

From: Thomas Huth <[email protected]>

safexcel_aead_setkey() currently uses aes_expandkey() to check for a valid AES key, but then does not use the crypto_aes_ctx afterwards anymore, i.e. this is just a wasteful way of checking the key length, and thus aes_check_keylen() should be used instead.
This also fixes a potential leak of sensitive data via the stack, since this function forgot to zeroize crypto_aes_ctx before returning to the caller.

---
 drivers/crypto/inside-secure/safexcel_cipher.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/crypto/inside-secure/safexcel_cipher.c +1 -2
index a8349b684693e..f07d043c67d45 100644
@@ -407,7 +407,6 @@ static int safexcel_aead_setkey(struct crypto_aead *ctfm, const u8 *key,
 	struct safexcel_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
 	struct safexcel_crypto_priv *priv = ctx->base.priv;
 	struct crypto_authenc_keys keys;
-	struct crypto_aes_ctx aes;
 	int err = -EINVAL, i;
 	const char *alg;
 
@@ -438,7 +437,7 @@ static int safexcel_aead_setkey(struct crypto_aead *ctfm, const u8 *key,
 			goto badkey;
 		break;
 	case SAFEXCEL_AES:
-		err = aes_expandkey(&aes, keys.enckey, keys.enckeylen);
+		err = aes_check_keylen(keys.enckeylen);
 		if (unlikely(err))
 			goto badkey;
 		break;
Signature · 1 line
2.55.0
#3 lore raw

PATCH 3/11 crypto: safexcel - zeroize crypto_aes_ctx with __cleanup(aes_zeroize_ctx)

In reply to Thomas Huth

From: Thomas Huth <[email protected]>

The code clears the crypto_aes_ctx in most cases already with memzero_explicit(), but safexcel_skcipher_aesxts_setkey() runs aes_expandkey() twice, and in case the second call fails, the context from the first call is leaked.

To fix this issue and to avoid future similar problems, let's use the new __cleanup(aes_zeroize_ctx) mechanism to make sure that we always clear the crypto_aes_ctx in all cases.

---
 drivers/crypto/inside-secure/safexcel_cipher.c | 13 ++++---------
 drivers/crypto/inside-secure/safexcel_hash.c   |  3 +--
 2 files changed, 5 insertions(+), 11 deletions(-)
drivers/crypto/inside-secure/safexcel_cipher.c +4 -9
index f07d043c67d45..b031cb9652ec3 100644
@@ -375,7 +375,7 @@ static int safexcel_skcipher_aes_setkey(struct crypto_skcipher *ctfm,
 	struct crypto_tfm *tfm = crypto_skcipher_tfm(ctfm);
 	struct safexcel_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
 	struct safexcel_crypto_priv *priv = ctx->base.priv;
-	struct crypto_aes_ctx aes;
+	struct crypto_aes_ctx aes __cleanup(aes_zeroize_ctx);
 	int ret, i;
 
 	ret = aes_expandkey(&aes, key, len);
@@ -396,7 +396,6 @@ static int safexcel_skcipher_aes_setkey(struct crypto_skcipher *ctfm,
 
 	ctx->key_len = len;
 
-	memzero_explicit(&aes, sizeof(aes));
 	return 0;
 }
 
@@ -1361,7 +1360,7 @@ static int safexcel_skcipher_aesctr_setkey(struct crypto_skcipher *ctfm,
 	struct crypto_tfm *tfm = crypto_skcipher_tfm(ctfm);
 	struct safexcel_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
 	struct safexcel_crypto_priv *priv = ctx->base.priv;
-	struct crypto_aes_ctx aes;
+	struct crypto_aes_ctx aes __cleanup(aes_zeroize_ctx);
 	int ret, i;
 	unsigned int keylen;
 
@@ -1387,7 +1386,6 @@ static int safexcel_skcipher_aesctr_setkey(struct crypto_skcipher *ctfm,
 
 	ctx->key_len = keylen;
 
-	memzero_explicit(&aes, sizeof(aes));
 	return 0;
 }
 
@@ -2541,7 +2539,7 @@ static int safexcel_skcipher_aesxts_setkey(struct crypto_skcipher *ctfm,
 	struct crypto_tfm *tfm = crypto_skcipher_tfm(ctfm);
 	struct safexcel_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
 	struct safexcel_crypto_priv *priv = ctx->base.priv;
-	struct crypto_aes_ctx aes;
+	struct crypto_aes_ctx aes __cleanup(aes_zeroize_ctx);
 	int ret, i;
 	unsigned int keylen;
 
@@ -2589,7 +2587,6 @@ static int safexcel_skcipher_aesxts_setkey(struct crypto_skcipher *ctfm,
 
 	ctx->key_len = keylen << 1;
 
-	memzero_explicit(&aes, sizeof(aes));
 	return 0;
 }
 
@@ -2755,12 +2752,11 @@ static int safexcel_aead_ccm_setkey(struct crypto_aead *ctfm, const u8 *key,
 	struct crypto_tfm *tfm = crypto_aead_tfm(ctfm);
 	struct safexcel_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
 	struct safexcel_crypto_priv *priv = ctx->base.priv;
-	struct crypto_aes_ctx aes;
+	struct crypto_aes_ctx aes __cleanup(aes_zeroize_ctx);
 	int ret, i;
 
 	ret = aes_expandkey(&aes, key, len);
 	if (ret) {
-		memzero_explicit(&aes, sizeof(aes));
 		return ret;
 	}
 
@@ -2789,7 +2785,6 @@ static int safexcel_aead_ccm_setkey(struct crypto_aead *ctfm, const u8 *key,
 	else
 		ctx->hash_alg = CONTEXT_CONTROL_CRYPTO_ALG_XCBC128;
 
-	memzero_explicit(&aes, sizeof(aes));
 	return 0;
 }
 
drivers/crypto/inside-secure/safexcel_hash.c +1 -2
index 3402e570d045c..20c17eb09495e 100644
@@ -1905,7 +1905,7 @@ static int safexcel_cbcmac_setkey(struct crypto_ahash *tfm, const u8 *key,
 				 unsigned int len)
 {
 	struct safexcel_ahash_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
-	struct crypto_aes_ctx aes;
+	struct crypto_aes_ctx aes __cleanup(aes_zeroize_ctx);
 	int ret, i;
 
 	ret = aes_expandkey(&aes, key, len);
@@ -1928,7 +1928,6 @@ static int safexcel_cbcmac_setkey(struct crypto_ahash *tfm, const u8 *key,
 	}
 	ctx->cbcmac  = true;
 
-	memzero_explicit(&aes, sizeof(aes));
 	return 0;
 }
 
Signature · 1 line
2.55.0
#4 lore raw

PATCH 4/11 lib/crypto: aes: Provide functions for zeroizing aes_key and aes_enckey

In reply to Thomas Huth

From: Thomas Huth <[email protected]>

Some crypto functions need to zeroize their local aes_key or aes_enckey structures after use to avoid leaking sensitive material on the stack. Provide aes_zeroize_key() and aes_zeroize_enckey() helper functions that can be used with __cleanup() to automatically zeroize the structs when they go out of scope.

---
 include/crypto/aes.h | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
include/crypto/aes.h +26 -0
index faf1d1b75a15f..d00d88b71690b 100644
@@ -102,6 +102,19 @@ struct aes_enckey {
 	union aes_enckey_arch k;
 };
 
+/**
+ * aes_zeroize_enckey() - Zeroize an aes_enckey structure
+ * @key: The location of the key structure that should be zeroized
+ *
+ * Explicitly fills the aes_enckey with zeroes. For example, use it with
+ * __cleanup() for local aes_enckey structures on the stack, so that their
+ * content is not leaked when the context is left.
+ */
+static inline void aes_zeroize_enckey(struct aes_enckey *key)
+{
+	memzero_explicit(key, sizeof(*key));
+}
+
 /**
  * struct aes_key - An AES key prepared for encryption and decryption
  * @aes_enckey: Common fields and the key prepared for encryption
@@ -116,6 +129,19 @@ struct aes_key {
 	union aes_invkey_arch inv_k;
 };
 
+/**
+ * aes_zeroize_key() - Zeroize an aes_key structure
+ * @key: The location of the key structure that should be zeroized
+ *
+ * Explicitly fills the aes_key with zeroes. For example, use it with
+ * __cleanup() for local aes_key structures on the stack, so that their
+ * content is not leaked when the context is left.
+ */
+static inline void aes_zeroize_key(struct aes_key *key)
+{
+	memzero_explicit(key, sizeof(*key));
+}
+
 /*
  * Please ensure that the first two fields are 16-byte aligned
  * relative to the start of the structure, i.e., don't move them!
Signature · 1 line
2.55.0
#5 lore raw

PATCH 5/11 lib/crypto: aes: Use aes_zeroize_*key() instead of memzero_explicit()

In reply to Thomas Huth

From: Thomas Huth <[email protected]>

It's only cosmetics here, but since we have the new aes_zeroize_key() and aes_zeroize_enckey() functions anyway, we can also use them here.

---
 lib/crypto/aes.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
lib/crypto/aes.c +5 -5
index f1549839b3de0..07c1d912ac365 100644
@@ -539,7 +539,7 @@ static void __init aes_fips_test(void)
 	if (memcmp(fips_test_data, data, sizeof(data)) != 0)
 		panic("aes: FIPS self-test failed (wrong plaintext)\n");
 
-	memzero_explicit(&key, sizeof(key));
+	aes_zeroize_key(&key);
 }
 
 #if IS_ENABLED(CONFIG_CRYPTO_LIB_AES_CBC_MACS)
@@ -827,7 +827,7 @@ static void __init aes_ecb_fips_test(void)
 	if (memcmp(fips_test_data, data, sizeof(data)) != 0)
 		panic("aes: ECB FIPS self-test failed (wrong plaintext)\n");
 
-	memzero_explicit(&key, sizeof(key));
+	aes_zeroize_key(&key);
 }
 #else /* CONFIG_CRYPTO_LIB_AES_ECB */
 static inline void aes_ecb_fips_test(void)
@@ -1040,7 +1040,7 @@ static void __init aes_cbc_fips_test(void)
 	if (memcmp(fips_test_data, data, sizeof(data)) != 0)
 		panic("aes: CBC FIPS self-test failed (wrong plaintext)\n");
 
-	memzero_explicit(&key, sizeof(key));
+	aes_zeroize_key(&key);
 }
 
 /* FIPS cryptographic algorithm self-test for AES-CBC-CTS */
@@ -1069,7 +1069,7 @@ static void __init aes_cbc_cts_fips_test(void)
 	if (memcmp(ptext, data, data_len) != 0)
 		panic("aes: CBC-CTS FIPS self-test failed (wrong plaintext)\n");
 
-	memzero_explicit(&key, sizeof(key));
+	aes_zeroize_key(&key);
 }
 #else /* CONFIG_CRYPTO_LIB_AES_CBC */
 static inline void aes_cbc_fips_test(void)
@@ -1194,7 +1194,7 @@ static void __init aes_ctr_fips_test(void)
 	if (memcmp(fips_test_data, data, sizeof(data)) != 0)
 		panic("aes: CTR FIPS self-test failed (wrong plaintext)\n");
 
-	memzero_explicit(&key, sizeof(key));
+	aes_zeroize_enckey(&key);
 }
 #else /* CONFIG_CRYPTO_LIB_AES_CTR */
 static inline void aes_ctr_fips_test(void)
Signature · 1 line
2.55.0
#6 lore raw

PATCH 6/11 lib/crypto: md5: Provide a function for zeroizing hmac_md5_ctx structures

In reply to Thomas Huth

From: Thomas Huth <[email protected]>

Some crypto code functions need to zeroize their local hmac_md5_ctx structures after use to avoid leaking sensitive material on the stack. Provide a hmac_md5_zeroize_ctx() helper function that can be used with __cleanup() to automatically zeroize the context when it goes out of scope.

---
 include/crypto/md5.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
include/crypto/md5.h +15 -0
index c47aedfe67ecd..8cf26fd965323 100644
@@ -4,6 +4,7 @@
 
 #include <crypto/hash.h>
 #include <linux/types.h>
+#include <linux/string.h>
 
 #define MD5_DIGEST_SIZE		16
 #define MD5_HMAC_BLOCK_SIZE	64
@@ -108,6 +109,20 @@ struct hmac_md5_ctx {
 	struct md5_block_state ostate;
 };
 
+/**
+ * hmac_md5_zeroize_ctx() - Zeroize an hmac_md5_ctx structure
+ * @ctx: The location of the context that should be zeroized
+ *
+ * This function explicitly fills the hmac_md5_ctx with zeroes. For
+ * example, use it with __cleanup() for local hmac_md5_ctx structures
+ * on the stack, so that their content is not leaked when the context is
+ * left. Note: This is only required when not using hmac_md5_final().
+ */
+static inline void hmac_md5_zeroize_ctx(struct hmac_md5_ctx *ctx)
+{
+	memzero_explicit(ctx, sizeof(*ctx));
+}
+
 /**
  * hmac_md5_preparekey() - Prepare a key for HMAC-MD5
  * @key: (output) the key structure to initialize
Signature · 1 line
2.55.0
#7 lore raw

PATCH 7/11 lib/crypto: md5: Use hmac_md5_zeroize_ctx() instead of memzero_explicit()

In reply to Thomas Huth

From: Thomas Huth <[email protected]>

It's only cosmetics, but since we have the new hmac_md5_zeroize_ctx() function anyway, we can also use it here.

---
 lib/crypto/md5.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/crypto/md5.c +1 -1
index 3d2b017a0525a..a8ee57600012d 100644
@@ -271,7 +271,7 @@ void hmac_md5_final(struct hmac_md5_ctx *ctx, u8 out[MD5_DIGEST_SIZE])
 	cpu_to_le32_array(ctx->ostate.h, ARRAY_SIZE(ctx->ostate.h));
 	memcpy(out, ctx->ostate.h, MD5_DIGEST_SIZE);
 
-	memzero_explicit(ctx, sizeof(*ctx));
+	hmac_md5_zeroize_ctx(ctx);
 }
 EXPORT_SYMBOL_GPL(hmac_md5_final);
 
Signature · 1 line
2.55.0
#8 lore raw

PATCH 8/11 lib/crypto: sha1: Provide a wrapper for zeroizing hmac_sha1_ctx

In reply to Thomas Huth

From: Thomas Huth <[email protected]>

Some kernel code needs to zeroize their local hmac_sha1_ctx structures after use to avoid leaking sensitive material on the stack. Provide an hmac_sha1_zeroize_ctx() helper that can be used with __cleanup() to automatically zeroize the context when it goes out of scope.

---
 include/crypto/sha1.h | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
include/crypto/sha1.h +17 -0
index 4d973e016cd69..888dfc62e1c65 100644
@@ -7,6 +7,7 @@
 #define _CRYPTO_SHA1_H
 
 #include <linux/types.h>
+#include <linux/string.h>
 
 #define SHA1_DIGEST_SIZE        20
 #define SHA1_BLOCK_SIZE         64
@@ -106,6 +107,22 @@ struct hmac_sha1_ctx {
 	struct sha1_block_state ostate;
 };
 
+/**
+ * hmac_sha1_zeroize_ctx() - Zeroize an hmac_sha1_ctx structure
+ * @ctx: The location of the context that should be zeroized
+ *
+ * This function explicitly fills the hmac_sha1_ctx with zeroes. For
+ * example, it can be used with __cleanup() for local hmac_sha1_ctx
+ * structures on the stack, so that their content is not leaked via the
+ * stack when the context is left. Note: This is only required when not
+ * using hmac_sha1_final() that already zeroizes the structure at the
+ * end.
+ */
+static inline void hmac_sha1_zeroize_ctx(struct hmac_sha1_ctx *ctx)
+{
+	memzero_explicit(ctx, sizeof(*ctx));
+}
+
 /**
  * hmac_sha1_preparekey() - Prepare a key for HMAC-SHA1
  * @key: (output) the key structure to initialize
Signature · 1 line
2.55.0
#9 lore raw

PATCH 9/11 lib/crypto: sha1: Use hmac_sha1_zeroize_ctx() instead of memzero_explicit()

In reply to Thomas Huth

From: Thomas Huth <[email protected]>

It's only cosmetics, but since we have the new hmac_sha1_zeroize_ctx() function anyway, we can also use it here.

---
 lib/crypto/sha1.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/crypto/sha1.c +1 -1
index b687b89d97cb4..c4361ef77166e 100644
@@ -275,7 +275,7 @@ void hmac_sha1_final(struct hmac_sha1_ctx *ctx, u8 out[SHA1_DIGEST_SIZE])
 	for (size_t i = 0; i < SHA1_DIGEST_SIZE; i += 4)
 		put_unaligned_be32(ctx->ostate.h[i / 4], out + i);
 
-	memzero_explicit(ctx, sizeof(*ctx));
+	hmac_sha1_zeroize_ctx(ctx);
 }
 EXPORT_SYMBOL_GPL(hmac_sha1_final);
 
Signature · 1 line
2.55.0
#10 lore raw

PATCH 10/11 x86/purgatory: Compile purgatory.c with -D__NO_FORTIFY

In reply to Thomas Huth

From: Thomas Huth <[email protected]>

purgatory.c includes both, the <crypto/sha2.h> header and the arch/x86/boot/string.h header. The latter provides its own prototypes for a lot of string functions which clash with the fortified macros from <linux/string.h>.
The next patch will add #include <linux/string.h> to sha2.h to be able to use memzero_explicit() there, so we have to compile the code in purgatory.c with -D__NO_FORTIFY to avoid compilation problems in this file.

---
 arch/x86/purgatory/Makefile | 1 +
 1 file changed, 1 insertion(+)
arch/x86/purgatory/Makefile +1 -0
index 5ce1d42630000..9191e3cffc30b 100644
@@ -12,6 +12,7 @@ $(obj)/sha256.o: $(srctree)/lib/crypto/sha256.c FORCE
 	$(call if_changed_rule,cc_o_c)
 
 CFLAGS_sha256.o := -D__DISABLE_EXPORTS -D__NO_FORTIFY
+CFLAGS_purgatory.o += -D__NO_FORTIFY
 
 # When profile-guided optimization is enabled, llvm emits two different
 # overlapping text sections, which is not supported by kexec. Remove profile
Signature · 1 line
2.55.0
#16 lore raw

PATCH 11/11 lib/crypto: sha2: Provide wrappers for zeroizing SHA2 hmac_sha*_ctx structures

In reply to Thomas Huth

From: Thomas Huth <[email protected]>

Some crypto code functions need to zeroize their local SHA2 hmac_sha*_ctx structures after use to avoid leaking sensitive material on the stack. Provide hmac_sha*_zeroize_ctx() helper functions that can be used with __cleanup() to automatically zeroize the context when it goes out of scope.

---
Note: These will be useful in some spots in the fs/smb/ code later.

 include/crypto/sha2.h | 57 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)
include/crypto/sha2.h +57 -0
index 7bb8fe169daf2..2b2b06ebff993 100644
@@ -7,6 +7,7 @@
 #define _CRYPTO_SHA2_H
 
 #include <linux/types.h>
+#include <linux/string.h>
 
 #define SHA224_DIGEST_SIZE	28
 #define SHA224_BLOCK_SIZE	64
@@ -218,6 +219,20 @@ struct hmac_sha224_ctx {
 	struct __hmac_sha256_ctx ctx;
 };
 
+/**
+ * hmac_sha224_zeroize_ctx() - Zeroize an hmac_sha224_ctx structure
+ * @ctx: The location of the context that should be zeroized
+ *
+ * This function explicitly fills the hmac_sha224_ctx with zeroes. For
+ * example, use it with __cleanup() for local hmac_sha224_ctx structures
+ * on the stack, so that their content is not leaked when the context is
+ * left. Note: This is only required when not using hmac_sha224_final().
+ */
+static inline void hmac_sha224_zeroize_ctx(struct hmac_sha224_ctx *ctx)
+{
+	memzero_explicit(ctx, sizeof(*ctx));
+}
+
 /**
  * hmac_sha224_preparekey() - Prepare a key for HMAC-SHA224
  * @key: (output) the key structure to initialize
@@ -422,6 +437,20 @@ struct hmac_sha256_ctx {
 	struct __hmac_sha256_ctx ctx;
 };
 
+/**
+ * hmac_sha256_zeroize_ctx() - Zeroize an hmac_sha256_ctx structure
+ * @ctx: The location of the context that should be zeroized
+ *
+ * This function explicitly fills the hmac_sha256_ctx with zeroes. For
+ * example, use it with __cleanup() for local hmac_sha256_ctx structures
+ * on the stack, so that their content is not leaked when the context is
+ * left. Note: This is only required when not using hmac_sha256_final().
+ */
+static inline void hmac_sha256_zeroize_ctx(struct hmac_sha256_ctx *ctx)
+{
+	memzero_explicit(ctx, sizeof(*ctx));
+}
+
 /**
  * hmac_sha256_preparekey() - Prepare a key for HMAC-SHA256
  * @key: (output) the key structure to initialize
@@ -631,6 +660,20 @@ struct hmac_sha384_ctx {
 	struct __hmac_sha512_ctx ctx;
 };
 
+/**
+ * hmac_sha384_zeroize_ctx() - Zeroize an hmac_sha384_ctx structure
+ * @ctx: The location of the context that should be zeroized
+ *
+ * This function explicitly fills the hmac_sha384_ctx with zeroes. For
+ * example, use it with __cleanup() for local hmac_sha384_ctx structures
+ * on the stack, so that their content is not leaked when the context is
+ * left. Note: This is only required when not using hmac_sha384_final().
+ */
+static inline void hmac_sha384_zeroize_ctx(struct hmac_sha384_ctx *ctx)
+{
+	memzero_explicit(ctx, sizeof(*ctx));
+}
+
 /**
  * hmac_sha384_preparekey() - Prepare a key for HMAC-SHA384
  * @key: (output) the key structure to initialize
@@ -806,6 +849,20 @@ struct hmac_sha512_ctx {
 	struct __hmac_sha512_ctx ctx;
 };
 
+/**
+ * hmac_sha512_zeroize_ctx() - Zeroize an hmac_sha512_ctx structure
+ * @ctx: The location of the context that should be zeroized
+ *
+ * This function explicitly fills the hmac_sha512_ctx with zeroes. For
+ * example, use it with __cleanup() for local hmac_sha512_ctx structures
+ * on the stack, so that their content is not leaked when the context is
+ * left. Note: This is only required when not using hmac_sha512_final().
+ */
+static inline void hmac_sha512_zeroize_ctx(struct hmac_sha512_ctx *ctx)
+{
+	memzero_explicit(ctx, sizeof(*ctx));
+}
+
 /**
  * hmac_sha512_preparekey() - Prepare a key for HMAC-SHA512
  * @key: (output) the key structure to initialize
Signature · 1 line
2.55.0
maintainer
#11 lore raw

Re: [PATCH 10/11] x86/purgatory: Compile purgatory.c with -D__NO_FORTIFY

In reply to Thomas Huth
Quoted 7 lines · Thomas Huth

From: Thomas Huth <[email protected]>

purgatory.c includes both, the <crypto/sha2.h> header and the arch/x86/boot/string.h header. The latter provides its own prototypes for a lot of string functions which clash with the fortified macros from <linux/string.h>.
The next patch will add #include <linux/string.h> to sha2.h to be able

This is exactly the problem - people are adding main include namespace headers from linux/ to arch/x86/boot, or, as you do, to purgatory, which are special
- and then they have to do all kinds of hacks so that it builds.

(btw, there's no "next patch" in git history).

Quoted 3 lines

to use memzero_explicit() there, so we have to compile the code in purgatory.c with -D__NO_FORTIFY to avoid compilation problems in this file.

How about you extract memzero_explicit() to a separate header in include/asm-generic/ or simply put it in include/asm-generic/string.h and then use that header instead?

Then you shouldn't have prototype clashes...

It does sound cleaner to me but you'd have to try it to see whether it actually works.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#12 lore raw

Re: [PATCH 10/11] x86/purgatory: Compile purgatory.c with -D__NO_FORTIFY

Quoted 19 lines · Borislav Petkov
Quoted 7 lines · Thomas Huth

From: Thomas Huth <[email protected]>

purgatory.c includes both, the <crypto/sha2.h> header and the arch/x86/boot/string.h header. The latter provides its own prototypes for a lot of string functions which clash with the fortified macros from <linux/string.h>.
The next patch will add #include <linux/string.h> to sha2.h to be able

This is exactly the problem - people are adding main include namespace headers from linux/ to arch/x86/boot, or, as you do, to purgatory, which are special
- and then they have to do all kinds of hacks so that it builds.

(btw, there's no "next patch" in git history).

Quoted 3 lines

to use memzero_explicit() there, so we have to compile the code in purgatory.c with -D__NO_FORTIFY to avoid compilation problems in this file.

Quoted 8 lines

How about you extract memzero_explicit() to a separate header in include/asm-generic/ or simply put it in include/asm-generic/string.h and then use that header instead?

Then you shouldn't have prototype clashes...

It does sound cleaner to me but you'd have to try it to see whether it actually works.

Thanks for the suggestion, I gave it a try, but I only ended up with another huger ugliness this way: Since memzero_explicit() needs memset(), that memset() has to be declared somewhere. Since I cannot include <linux/string.h> from the file where I put memzero_explicit(), we would need to make sure that all files that want to use <crypto/sha2.h> include either <linux/string.h> or arch/x86/boot/string.h or whatever before including sha2.h - i.e. this creates a likely unacceptable indirect dependency of sha2.h on including a string.h header first.

Adding the -D__NO_FORTIFY to the purgatory Makefile sounds like the lesser of the two evils to me, especially since there is already another line in that Makefile that adds -D__NO_FORTIFY to one of the other files there:

CFLAGS_sha256.o := -D__DISABLE_EXPORTS -D__NO_FORTIFY

Maybe I should rather change the patch to add -D__NO_FORTIFY unconditionally to generic PURGATORY_CFLAGS there? Something like:

arch/x86/purgatory/Makefile +2 -1
@@ -11,7 +11,7 @@ $(obj)/string.o: $(srctree)/arch/x86/boot/compressed/string.c FORCE
 $(obj)/sha256.o: $(srctree)/lib/crypto/sha256.c FORCE
        $(call if_changed_rule,cc_o_c)
 
-CFLAGS_sha256.o := -D__DISABLE_EXPORTS -D__NO_FORTIFY
+CFLAGS_sha256.o := -D__DISABLE_EXPORTS
 
 # When profile-guided optimization is enabled, llvm emits two different
 # overlapping text sections, which is not supported by kexec. Remove profile
@@ -37,6 +37,7 @@ PURGATORY_CFLAGS := -mcmodel=small -ffreestanding -fno-zero-initialized-in-bss -
 PURGATORY_CFLAGS += -fpic -fvisibility=hidden
 PURGATORY_CFLAGS += $(DISABLE_KSTACK_ERASE) -DDISABLE_BRANCH_PROFILING
 PURGATORY_CFLAGS += -fno-stack-protector
+PURGATORY_CFLAGS +=  -D__NO_FORTIFY
 
 # Default KBUILD_CFLAGS can have -pg option set when FTRACE is enabled. That
 # in turn leaves some undefined symbols like __fentry__ in purgatory and not

WDYT?

Thomas

maintainer
#17 lore raw
In reply to Thomas Huth

Hi Thomas,

Quoted 35 lines · Thomas Huth

Code that uses crypto-related structures (containing keys or context data) should zeroize their local structures on the stack after use to avoid leaking this sensitive material via the stack when the function returns. Using the __cleanup() marker is a very elegant way to assert that the data is zeroized without having to painfully verify that each early return in a function might miss it.

Thus this series introduces zeroization functions for many crypto-related structures that can be used with __cleanup(). The series focuses on the introduction of the functions - most call sights will be adjusted to use these new functions in separate patch series later (since each subsystem needs separate review from the corresponding maintainer). However, I already included the two "safexcel" patches, since they already got ack'ed by the maintainer Antoine, so I think they should be fine to go via the libcrypto tree.

Note there is one minor ugliness in patch 10: Since sha2.h is also used in the x86 purgatory code, and that code ships with its own implementation of string functions, we have to compile the purgatory.c file with -D__NO_FORTIFY now to be able to include <linux/string.h> in sha2.h. I hope that solution is OK (especially since the sha256.c file in the same folder gets that treatment already, too), if not - I'm certainly open for other suggestions here!

Thomas Huth (11):
lib/crypto: aes: Provide a wrapper function for zeroizing
crypto_aes_ctx
crypto: safexcel - Simplify the check for a valid AES key
crypto: safexcel - zeroize crypto_aes_ctx with
__cleanup(aes_zeroize_ctx)
lib/crypto: aes: Provide functions for zeroizing aes_key and
aes_enckey
lib/crypto: aes: Use aes_zeroize_*key() instead of memzero_explicit() lib/crypto: md5: Provide a function for zeroizing hmac_md5_ctx
structures

Quoted 8 lines

lib/crypto: md5: Use hmac_md5_zeroize_ctx() instead of
memzero_explicit()
lib/crypto: sha1: Provide a wrapper for zeroizing hmac_sha1_ctx lib/crypto: sha1: Use hmac_sha1_zeroize_ctx() instead of
memzero_explicit()
x86/purgatory: Compile purgatory.c with -D__NO_FORTIFY lib/crypto: sha2: Provide wrappers for zeroizing SHA2 hmac_sha*_ctx
structures

I'm taking a look at these again for 7.3. This is still missing quite a few of the crypto library structs. I would prefer to handle them all in a consistent way.

Also, given that we'll end up with a lot of these, we should be thoughtful about the kerneldoc. Using aes_enckey as an example:

/**
* aes_zeroize_enckey() - Zeroize an aes_enckey structure
* @key: The location of the key structure that should be zeroized *
* Explicitly fills the aes_enckey with zeroes. For example, use it with
* __cleanup() for local aes_enckey structures on the stack, so that their
* content is not leaked when the context is left.
*/

It's not clear what is meant by "context". And it doesn't explicitly connect the zeroization requirement to the lifetime of the struct, so I don't think it makes it clear when this should be called. It also says nothing about kfree_sensitive() which many users should use instead.

Meanwhile, aes_prepareenckey() already has the following, which at least connects the zeroization requirement to the lifetime of the key:

* The caller is responsible for zeroizing both the struct aes_enckey and the
* raw key once they are no longer needed.

How about we remove that and the free-form description of aes_zeroize_enckey(), and instead expand the comment on the struct itself:

/**
* struct aes_enckey - An AES key prepared for encryption
* ...
* Once prepared, users must zeroize this struct at the end of its lifetime.
* For stack-allocated structs, use __cleanup(aes_zeroize_enckey) to zeroize
* when the variable goes out of scope. For slab-allocated structs, use
* kfree_sensitive() or else call aes_zeroize_enckey() before freeing. */

That would connect the zeroization requirement to the actual struct and its lifetime, and make it clear how to handle each case. It would also make it clear that stack allocation is supported for this struct, which might not have been obvious before.

aes_zeroize_enckey() would then omit a free-form description, which makes sense as it's a trivial wrapper around memzero_explicit():

/**
* aes_zeroize_enckey() - Zeroize an aes_enckey structure
* @key: The aes_enckey to zeroize
*/
static inline void aes_zeroize_enckey(struct aes_enckey *key)

{
	memzero_explicit(key, sizeof(*key));
}

Similarly for all the other structs, of course.

(For the hash contexts, the struct comment should mention that the finalization function zeroizes as well.)

Does that sound good?

- Eric

maintainer
#18 lore raw
Quoted 104 lines · Eric Biggers

Hi Thomas,

Quoted 35 lines · Thomas Huth

Code that uses crypto-related structures (containing keys or context data) should zeroize their local structures on the stack after use to avoid leaking this sensitive material via the stack when the function returns. Using the __cleanup() marker is a very elegant way to assert that the data is zeroized without having to painfully verify that each early return in a function might miss it.

Thus this series introduces zeroization functions for many crypto-related structures that can be used with __cleanup(). The series focuses on the introduction of the functions - most call sights will be adjusted to use these new functions in separate patch series later (since each subsystem needs separate review from the corresponding maintainer). However, I already included the two "safexcel" patches, since they already got ack'ed by the maintainer Antoine, so I think they should be fine to go via the libcrypto tree.

Note there is one minor ugliness in patch 10: Since sha2.h is also used in the x86 purgatory code, and that code ships with its own implementation of string functions, we have to compile the purgatory.c file with -D__NO_FORTIFY now to be able to include <linux/string.h> in sha2.h. I hope that solution is OK (especially since the sha256.c file in the same folder gets that treatment already, too), if not - I'm certainly open for other suggestions here!

Thomas Huth (11):
lib/crypto: aes: Provide a wrapper function for zeroizing
crypto_aes_ctx
crypto: safexcel - Simplify the check for a valid AES key
crypto: safexcel - zeroize crypto_aes_ctx with
__cleanup(aes_zeroize_ctx)
lib/crypto: aes: Provide functions for zeroizing aes_key and
aes_enckey
lib/crypto: aes: Use aes_zeroize_*key() instead of memzero_explicit() lib/crypto: md5: Provide a function for zeroizing hmac_md5_ctx
structures

Quoted 8 lines

lib/crypto: md5: Use hmac_md5_zeroize_ctx() instead of
memzero_explicit()
lib/crypto: sha1: Provide a wrapper for zeroizing hmac_sha1_ctx lib/crypto: sha1: Use hmac_sha1_zeroize_ctx() instead of
memzero_explicit()
x86/purgatory: Compile purgatory.c with -D__NO_FORTIFY lib/crypto: sha2: Provide wrappers for zeroizing SHA2 hmac_sha*_ctx
structures

I'm taking a look at these again for 7.3. This is still missing quite a few of the crypto library structs. I would prefer to handle them all in a consistent way.

Also, given that we'll end up with a lot of these, we should be thoughtful about the kerneldoc. Using aes_enckey as an example:

/**
* aes_zeroize_enckey() - Zeroize an aes_enckey structure
* @key: The location of the key structure that should be zeroized *
* Explicitly fills the aes_enckey with zeroes. For example, use it with
* __cleanup() for local aes_enckey structures on the stack, so that their
* content is not leaked when the context is left.
*/

It's not clear what is meant by "context". And it doesn't explicitly connect the zeroization requirement to the lifetime of the struct, so I don't think it makes it clear when this should be called. It also says nothing about kfree_sensitive() which many users should use instead.

Meanwhile, aes_prepareenckey() already has the following, which at least connects the zeroization requirement to the lifetime of the key:

* The caller is responsible for zeroizing both the struct aes_enckey and the
* raw key once they are no longer needed.

How about we remove that and the free-form description of aes_zeroize_enckey(), and instead expand the comment on the struct itself:

/**
* struct aes_enckey - An AES key prepared for encryption
* ...
* Once prepared, users must zeroize this struct at the end of its lifetime.
* For stack-allocated structs, use __cleanup(aes_zeroize_enckey) to zeroize
* when the variable goes out of scope. For slab-allocated structs, use
* kfree_sensitive() or else call aes_zeroize_enckey() before freeing. */

That would connect the zeroization requirement to the actual struct and its lifetime, and make it clear how to handle each case. It would also make it clear that stack allocation is supported for this struct, which might not have been obvious before.

aes_zeroize_enckey() would then omit a free-form description, which makes sense as it's a trivial wrapper around memzero_explicit():

/**
* aes_zeroize_enckey() - Zeroize an aes_enckey structure
* @key: The aes_enckey to zeroize
*/
static inline void aes_zeroize_enckey(struct aes_enckey *key)

{
	memzero_explicit(key, sizeof(*key));
}
Quoted 6 lines

Similarly for all the other structs, of course.

(For the hash contexts, the struct comment should mention that the finalization function zeroizes as well.)

Does that sound good?

There's also the subtlety that __cleanup shouldn't be used in functions that are already using goto-based cleanup.

And any kernel code preparing a key also has the raw key too and has to zeroize that too, otherwise doing so for the prepared key is pointless.

... unless it's not actually a secret key, which sometimes it isn't. We've been getting a lot of zeroization patches that try to add zeroization for public keys, which is nonsense. And also for pointers to keys, which people confuse with the keys themselves.

There are also Rust bindings being proposed, and they handle zeroization by calling memzero_explicit() directly. So that's yet another case where these new functions are not applicable.

I'm increasingly thinking that we shouldn't try to explain all the zeroization stuff in the kerneldoc, and instead add a documentation file in Documentation/crypto/ that properly explains the conventions for crypto key zeroization in the kernel and defer to that.

The struct-specific zeroization functions are still worthwhile, but I think they should be seen as trivial wrappers around memzero_explicit(). So maybe just add all of them with minimal comments like I suggested above (maybe even just add them for all the algorithms in a single patch), and add a file Documentation/crypto/zeroization.rst separately.

- Eric

#19 lore raw
Quoted 132 lines · Eric Biggers
Quoted 104 lines · Eric Biggers

Hi Thomas,

Quoted 35 lines · Thomas Huth

Code that uses crypto-related structures (containing keys or context data) should zeroize their local structures on the stack after use to avoid leaking this sensitive material via the stack when the function returns. Using the __cleanup() marker is a very elegant way to assert that the data is zeroized without having to painfully verify that each early return in a function might miss it.

Thus this series introduces zeroization functions for many crypto-related structures that can be used with __cleanup(). The series focuses on the introduction of the functions - most call sights will be adjusted to use these new functions in separate patch series later (since each subsystem needs separate review from the corresponding maintainer). However, I already included the two "safexcel" patches, since they already got ack'ed by the maintainer Antoine, so I think they should be fine to go via the libcrypto tree.

Note there is one minor ugliness in patch 10: Since sha2.h is also used in the x86 purgatory code, and that code ships with its own implementation of string functions, we have to compile the purgatory.c file with -D__NO_FORTIFY now to be able to include <linux/string.h> in sha2.h. I hope that solution is OK (especially since the sha256.c file in the same folder gets that treatment already, too), if not - I'm certainly open for other suggestions here!

Thomas Huth (11):
lib/crypto: aes: Provide a wrapper function for zeroizing
crypto_aes_ctx
crypto: safexcel - Simplify the check for a valid AES key
crypto: safexcel - zeroize crypto_aes_ctx with
__cleanup(aes_zeroize_ctx)
lib/crypto: aes: Provide functions for zeroizing aes_key and
aes_enckey
lib/crypto: aes: Use aes_zeroize_*key() instead of memzero_explicit() lib/crypto: md5: Provide a function for zeroizing hmac_md5_ctx
structures

Quoted 8 lines

lib/crypto: md5: Use hmac_md5_zeroize_ctx() instead of
memzero_explicit()
lib/crypto: sha1: Provide a wrapper for zeroizing hmac_sha1_ctx lib/crypto: sha1: Use hmac_sha1_zeroize_ctx() instead of
memzero_explicit()
x86/purgatory: Compile purgatory.c with -D__NO_FORTIFY lib/crypto: sha2: Provide wrappers for zeroizing SHA2 hmac_sha*_ctx
structures

I'm taking a look at these again for 7.3. This is still missing quite a few of the crypto library structs. I would prefer to handle them all in a consistent way.

Also, given that we'll end up with a lot of these, we should be thoughtful about the kerneldoc. Using aes_enckey as an example:

/**
* aes_zeroize_enckey() - Zeroize an aes_enckey structure
* @key: The location of the key structure that should be zeroized *
* Explicitly fills the aes_enckey with zeroes. For example, use it with
* __cleanup() for local aes_enckey structures on the stack, so that their
* content is not leaked when the context is left.
*/

It's not clear what is meant by "context". And it doesn't explicitly connect the zeroization requirement to the lifetime of the struct, so I don't think it makes it clear when this should be called. It also says nothing about kfree_sensitive() which many users should use instead.

Meanwhile, aes_prepareenckey() already has the following, which at least connects the zeroization requirement to the lifetime of the key:

* The caller is responsible for zeroizing both the struct aes_enckey and the
* raw key once they are no longer needed.

How about we remove that and the free-form description of aes_zeroize_enckey(), and instead expand the comment on the struct itself:

/**
* struct aes_enckey - An AES key prepared for encryption
* ...
* Once prepared, users must zeroize this struct at the end of its lifetime.
* For stack-allocated structs, use __cleanup(aes_zeroize_enckey) to zeroize
* when the variable goes out of scope. For slab-allocated structs, use
* kfree_sensitive() or else call aes_zeroize_enckey() before freeing. */

That would connect the zeroization requirement to the actual struct and its lifetime, and make it clear how to handle each case. It would also make it clear that stack allocation is supported for this struct, which might not have been obvious before.

aes_zeroize_enckey() would then omit a free-form description, which makes sense as it's a trivial wrapper around memzero_explicit():

/**
* aes_zeroize_enckey() - Zeroize an aes_enckey structure
* @key: The aes_enckey to zeroize
*/
static inline void aes_zeroize_enckey(struct aes_enckey *key)

{
	memzero_explicit(key, sizeof(*key));
}
Quoted 6 lines

Similarly for all the other structs, of course.

(For the hash contexts, the struct comment should mention that the finalization function zeroizes as well.)

Does that sound good?

There's also the subtlety that __cleanup shouldn't be used in functions that are already using goto-based cleanup.

And any kernel code preparing a key also has the raw key too and has to zeroize that too, otherwise doing so for the prepared key is pointless.

... unless it's not actually a secret key, which sometimes it isn't. We've been getting a lot of zeroization patches that try to add zeroization for public keys, which is nonsense. And also for pointers to keys, which people confuse with the keys themselves.

There are also Rust bindings being proposed, and they handle zeroization by calling memzero_explicit() directly. So that's yet another case where these new functions are not applicable.

I'm increasingly thinking that we shouldn't try to explain all the zeroization stuff in the kerneldoc, and instead add a documentation file in Documentation/crypto/ that properly explains the conventions for crypto key zeroization in the kernel and defer to that.

Quoted 5 lines

The struct-specific zeroization functions are still worthwhile, but I think they should be seen as trivial wrappers around memzero_explicit(). So maybe just add all of them with minimal comments like I suggested above (maybe even just add them for all the algorithms in a single patch), and add a file Documentation/crypto/zeroization.rst separately.

Ok, makes sense, I can have a try to come up with a patch series for this within the next days.

However, I'm still stuck on the question how to handle the #include <linux/string.h> for the purgatory:

https://lore.kernel.org/lkml/[email protected]/

Do you maybe have any good suggestions how to tackle this?

Thomas

maintainer
#13 lore raw

Re: [PATCH 10/11] x86/purgatory: Compile purgatory.c with -D__NO_FORTIFY

In reply to Thomas Huth
Quoted 11 lines · Thomas Huth
arch/x86/purgatory/Makefile +1 -1
@@ -37,6 +37,7 @@ PURGATORY_CFLAGS := -mcmodel=small -ffreestanding
-fno-zero-initialized-in-bss -
 PURGATORY_CFLAGS += -fpic -fvisibility=hidden
 PURGATORY_CFLAGS += $(DISABLE_KSTACK_ERASE) -DDISABLE_BRANCH_PROFILING
 PURGATORY_CFLAGS += -fno-stack-protector
+PURGATORY_CFLAGS +=  -D__NO_FORTIFY
 
 # Default KBUILD_CFLAGS can have -pg option set when FTRACE is enabled. That
 # in turn leaves some undefined symbols like __fentry__ in purgatory and not

WDYT?

I guess... I probably don't care too much about string checks in the purgatory thing...

But please explain in more detail in the commit message what "compilation problems" are avoided by disabling those additional string checks.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
maintainer
#15 lore raw

Re: [PATCH 10/11] x86/purgatory: Compile purgatory.c with -D__NO_FORTIFY

In reply to Thomas Huth

On Tue, 18 Aug 2026, at 10:53, Thomas Huth wrote:
...

Quoted 18 lines

Maybe I should rather change the patch to add -D__NO_FORTIFY unconditionally to generic PURGATORY_CFLAGS there? Something like:

arch/x86/purgatory/Makefile +0 -0
@@ -11,7 +11,7 @@ $(obj)/string.o: 

$(srctree)/arch/x86/boot/compressed/string.c FORCE
$(obj)/sha256.o: $(srctree)/lib/crypto/sha256.c FORCE

         $(call if_changed_rule,cc_o_c)
arch/x86/purgatory/Makefile +1 -1
-CFLAGS_sha256.o := -D__DISABLE_EXPORTS -D__NO_FORTIFY
+CFLAGS_sha256.o := -D__DISABLE_EXPORTS
 
  # When profile-guided optimization is enabled, llvm emits two different
  # overlapping text sections, which is not supported by kexec. Remove profile

arch/x86/purgatory/Makefile PURGATORY_CFLAGS := -mcmodel=small -ffreestanding · line 43

Quoted 8 lines
arch/x86/purgatory/Makefile +1 -1
@@ -37,6 +37,7 @@ PURGATORY_CFLAGS := -mcmodel=small -ffreestanding 
-fno-zero-initialized-in-bss -
  PURGATORY_CFLAGS += -fpic -fvisibility=hidden
  PURGATORY_CFLAGS += $(DISABLE_KSTACK_ERASE) -DDISABLE_BRANCH_PROFILING
  PURGATORY_CFLAGS += -fno-stack-protector
+PURGATORY_CFLAGS +=  -D__NO_FORTIFY
 
  # Default KBUILD_CFLAGS can have -pg option set when FTRACE is enabled. That
  # in turn leaves some undefined symbols like __fentry__ in purgatory and not

This seems like the right solution to me.

#14 lore raw

Re: [PATCH 10/11] x86/purgatory: Compile purgatory.c with -D__NO_FORTIFY

In reply to Borislav Petkov
Quoted 11 lines · Borislav Petkov
Quoted 10 lines · Thomas Huth
arch/x86/purgatory/Makefile +1 -1
@@ -37,6 +37,7 @@ PURGATORY_CFLAGS := -mcmodel=small -ffreestanding
-fno-zero-initialized-in-bss -
  PURGATORY_CFLAGS += -fpic -fvisibility=hidden
  PURGATORY_CFLAGS += $(DISABLE_KSTACK_ERASE) -DDISABLE_BRANCH_PROFILING
  PURGATORY_CFLAGS += -fno-stack-protector
+PURGATORY_CFLAGS +=  -D__NO_FORTIFY
 
  # Default KBUILD_CFLAGS can have -pg option set when FTRACE is enabled. That
  # in turn leaves some undefined symbols like __fentry__ in purgatory and not
Quoted 7 lines
Quoted 1 line

WDYT?

I guess... I probably don't care too much about string checks in the purgatory thing...

But please explain in more detail in the commit message what "compilation problems" are avoided by disabling those additional string checks.

Thanks, I've updated the patch now accordingly in v4:

https://lore.kernel.org/all/[email protected]/

Thomas