* [PATCH v2 0/6] crypto: Add __cleanup functions for zeroizing aes_cmac_key & aes_cmac_ctx
@ 2026-08-07 12:58 Thomas Huth
2026-08-07 12:58 ` [PATCH v2 1/6] crypto: Provide wrapper functions for zeroizing aes_cmac_key and aes_cmac_ctx Thomas Huth
` (5 more replies)
0 siblings, 6 replies; 9+ messages in thread
From: Thomas Huth @ 2026-08-07 12:58 UTC (permalink / raw)
To: Eric Biggers
Cc: Herbert Xu, David S. Miller, linux-crypto, linux-kernel,
Steve French, Namjae Jeon
Code that uses AES-CMAC might need to zeroize their local aes_cmac_key
and/or aes_cmac_ctx structures after use to avoid leaking sensitive
material on the stack.
The first patch introduces an aes_cmac_zeroize_key() and an
aes_cmac_zeroize_ctx() helper function that can be used with __cleanup()
to automatically clear the key and context when they go out of scope.
Patches 2 - 4 add the __cleanup markers to spots in the code that
missed to clean up the structures so far.
The final two patches are just cosmetics and change some memzero_explicit()
calls to use the __cleanup() way instead.
Note: There are more spots in the smb, tcp-ao and bluetooth code that
need some zeroization of sensitive data on the stack. I will tackle
those in later patches, this series here focuses on the new __cleanup
wrappers.
v2:
- Update comments and positions of the functions in the 1st patch
- Also clear tmp[] in the bluetooth patch (the patch is pointless otherwise)
Thomas Huth (6):
crypto: Provide wrapper functions for zeroizing aes_cmac_key and
aes_cmac_ctx
smb: clear the aes_cmac_key and aes_cmac_ctx when done
net/tcp-ao: clear the aes_cmac_key when done
Bluetooth: SMP: clear the aes_cmac_key when done
lib/crypto: aes: Use _cleanup() for aes_cmac_key instead of
memzero_explicit()
mac80211: fils_aead: Use _cleanup for aes_cmac_key instead of
memzero_explicit
fs/smb/client/smb2transport.c | 4 ++--
fs/smb/server/auth.c | 2 +-
include/crypto/aes-cbc-macs.h | 34 ++++++++++++++++++++++++++++++++++
lib/crypto/aes.c | 3 +--
net/bluetooth/smp.c | 3 ++-
net/ipv4/tcp_ao.c | 2 +-
net/mac80211/fils_aead.c | 3 +--
7 files changed, 42 insertions(+), 9 deletions(-)
--
2.55.0
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v2 1/6] crypto: Provide wrapper functions for zeroizing aes_cmac_key and aes_cmac_ctx
2026-08-07 12:58 [PATCH v2 0/6] crypto: Add __cleanup functions for zeroizing aes_cmac_key & aes_cmac_ctx Thomas Huth
@ 2026-08-07 12:58 ` Thomas Huth
2026-08-07 12:58 ` [PATCH v2 2/6] smb: clear the aes_cmac_key and aes_cmac_ctx when done Thomas Huth
` (4 subsequent siblings)
5 siblings, 0 replies; 9+ messages in thread
From: Thomas Huth @ 2026-08-07 12:58 UTC (permalink / raw)
To: Eric Biggers, Herbert Xu, David S. Miller
Cc: linux-crypto, linux-kernel, Steve French, Namjae Jeon
From: Thomas Huth <thuth@redhat.com>
Code that uses AES-CMAC might need to zeroize their local aes_cmac_key
and/or aes_cmac_ctx structures after use to avoid leaking sensitive
material on the stack.
Provide an aes_cmac_zeroize_key() and an aes_cmac_zeroize_ctx() helper
function that can be used with __cleanup() to automatically clear
the key and context when they go out of scope.
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
include/crypto/aes-cbc-macs.h | 34 ++++++++++++++++++++++++++++++++++
1 file changed, 34 insertions(+)
diff --git a/include/crypto/aes-cbc-macs.h b/include/crypto/aes-cbc-macs.h
index e61df108b926d..06e8a22f8a0ac 100644
--- a/include/crypto/aes-cbc-macs.h
+++ b/include/crypto/aes-cbc-macs.h
@@ -8,6 +8,7 @@
#define _CRYPTO_AES_CBC_MACS_H
#include <crypto/aes.h>
+#include <linux/string.h>
/**
* struct aes_cmac_key - Prepared key for AES-CMAC or AES-XCBC-MAC
@@ -24,6 +25,19 @@ struct aes_cmac_key {
} k_final[2];
};
+/**
+ * aes_cmac_zeroize_key() - Zeroize an aes_cmac_key structure
+ * @key: The location of the key structure that should be zeroized
+ *
+ * Explicitly fills the aes_cmac_key with zeroes. This should be done once
+ * the key is not required anymore to avoid that its contents are leaked
+ * on the stack or heap (if not using kfree_sensitive()).
+ */
+static inline void aes_cmac_zeroize_key(struct aes_cmac_key *key)
+{
+ memzero_explicit(key, sizeof(*key));
+}
+
/**
* struct aes_cmac_ctx - Context for computing an AES-CMAC or AES-XCBC-MAC value
* @key: Pointer to the key struct. A pointer is used rather than a copy of the
@@ -40,6 +54,19 @@ struct aes_cmac_ctx {
u8 h[AES_BLOCK_SIZE];
};
+/**
+ * aes_cmac_zeroize_ctx() - Zeroize an aes_cmac_ctx structure
+ * @ctx: The location of the context that should be zeroized
+ *
+ * Explicitly fills the aes_cmac_ctx with zeroes. This should be done once
+ * the context is not required anymore to avoid that its contents are
+ * leaked on the stack or heap. Only required if not using aes_cmac_final().
+ */
+static inline void aes_cmac_zeroize_ctx(struct aes_cmac_ctx *ctx)
+{
+ memzero_explicit(ctx, sizeof(*ctx));
+}
+
/**
* aes_cmac_preparekey() - Prepare a key for AES-CMAC
* @key: (output) The key struct to initialize
@@ -47,6 +74,10 @@ struct aes_cmac_ctx {
* @key_len: Length of the raw key in bytes. The supported values are
* AES_KEYSIZE_128, AES_KEYSIZE_192, and AES_KEYSIZE_256.
*
+ * On success, the caller should ensure that the prepared key is zeroized
+ * at the end of its lifetime, e.g. by calling aes_cmac_zeroize_key() or
+ * kfree_sensitive().
+ *
* Context: Any context.
* Return: 0 on success or -EINVAL if the given key length is invalid. No other
* errors are possible, so callers that always pass a valid key length
@@ -79,6 +110,9 @@ void aes_xcbcmac_preparekey(struct aes_cmac_key *key,
*
* This supports both AES-CMAC and AES-XCBC-MAC. Which one is done depends on
* whether aes_cmac_preparekey() or aes_xcbcmac_preparekey() was called.
+ *
+ * The caller should ensure that the context is zeroized at the end of its
+ * lifetime, e.g. by calling aes_cmac_final() or aes_cmac_zeroize_ctx().
*/
static inline void aes_cmac_init(struct aes_cmac_ctx *ctx,
const struct aes_cmac_key *key)
--
2.55.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v2 2/6] smb: clear the aes_cmac_key and aes_cmac_ctx when done
2026-08-07 12:58 [PATCH v2 0/6] crypto: Add __cleanup functions for zeroizing aes_cmac_key & aes_cmac_ctx Thomas Huth
2026-08-07 12:58 ` [PATCH v2 1/6] crypto: Provide wrapper functions for zeroizing aes_cmac_key and aes_cmac_ctx Thomas Huth
@ 2026-08-07 12:58 ` Thomas Huth
2026-08-07 12:58 ` [PATCH v2 3/6] net/tcp-ao: clear the aes_cmac_key " Thomas Huth
` (3 subsequent siblings)
5 siblings, 0 replies; 9+ messages in thread
From: Thomas Huth @ 2026-08-07 12:58 UTC (permalink / raw)
To: Eric Biggers, Steve French, Namjae Jeon
Cc: Herbert Xu, David S. Miller, linux-crypto, linux-kernel,
Steve French, Paulo Alcantara, Ronnie Sahlberg, Shyam Prasad N,
Tom Talpey, Bharath SM, Sergey Senozhatsky, linux-cifs,
samba-technical
From: Thomas Huth <thuth@redhat.com>
Clear the local crypto-related structures via __cleanup() functions
when we're done with them to avoid that sensitive data could leak on
the stack.
Note: cmac_ctx in ksmbd_sign_smb3_pdu() gets cleared in aes_cmac_final()
already, so this does not need a __cleanup() marker.
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
fs/smb/client/smb2transport.c | 4 ++--
fs/smb/server/auth.c | 2 +-
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/fs/smb/client/smb2transport.c b/fs/smb/client/smb2transport.c
index 1143ee52470a7..d23566da2ac81 100644
--- a/fs/smb/client/smb2transport.c
+++ b/fs/smb/client/smb2transport.c
@@ -464,8 +464,8 @@ smb3_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
unsigned char smb3_signature[SMB2_CMACAES_SIZE];
struct kvec *iov = rqst->rq_iov;
struct smb2_hdr *shdr = (struct smb2_hdr *)iov[0].iov_base;
- struct aes_cmac_key cmac_key;
- struct aes_cmac_ctx cmac_ctx;
+ struct aes_cmac_key cmac_key __cleanup(aes_cmac_zeroize_key);
+ struct aes_cmac_ctx cmac_ctx __cleanup(aes_cmac_zeroize_ctx);
struct smb_rqst drqst;
u8 key[SMB3_SIGN_KEY_SIZE];
diff --git a/fs/smb/server/auth.c b/fs/smb/server/auth.c
index 4e7b6f0e6b8cd..e8d1c068a43e8 100644
--- a/fs/smb/server/auth.c
+++ b/fs/smb/server/auth.c
@@ -509,7 +509,7 @@ void ksmbd_sign_smb2_pdu(struct ksmbd_conn *conn, char *key, struct kvec *iov,
void ksmbd_sign_smb3_pdu(struct ksmbd_conn *conn, char *key, struct kvec *iov,
int n_vec, char *sig)
{
- struct aes_cmac_key cmac_key;
+ struct aes_cmac_key cmac_key __cleanup(aes_cmac_zeroize_key);
struct aes_cmac_ctx cmac_ctx;
int i;
--
2.55.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v2 3/6] net/tcp-ao: clear the aes_cmac_key when done
2026-08-07 12:58 [PATCH v2 0/6] crypto: Add __cleanup functions for zeroizing aes_cmac_key & aes_cmac_ctx Thomas Huth
2026-08-07 12:58 ` [PATCH v2 1/6] crypto: Provide wrapper functions for zeroizing aes_cmac_key and aes_cmac_ctx Thomas Huth
2026-08-07 12:58 ` [PATCH v2 2/6] smb: clear the aes_cmac_key and aes_cmac_ctx when done Thomas Huth
@ 2026-08-07 12:58 ` Thomas Huth
2026-08-07 22:22 ` Jakub Kicinski
2026-08-07 12:58 ` [PATCH v2 4/6] Bluetooth: SMP: " Thomas Huth
` (2 subsequent siblings)
5 siblings, 1 reply; 9+ messages in thread
From: Thomas Huth @ 2026-08-07 12:58 UTC (permalink / raw)
To: Eric Biggers, Eric Dumazet, Neal Cardwell, David S. Miller,
Jakub Kicinski, Paolo Abeni
Cc: Herbert Xu, linux-crypto, linux-kernel, Steve French, Namjae Jeon,
Kuniyuki Iwashima, Simon Horman, netdev
From: Thomas Huth <thuth@redhat.com>
Clear the local aes_cmac_key structure via __cleanup() function
when we're done with it to avoid that sensitive data could leak on
the stack.
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
net/ipv4/tcp_ao.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/net/ipv4/tcp_ao.c b/net/ipv4/tcp_ao.c
index e4ec60a334963..0626cbf60620c 100644
--- a/net/ipv4/tcp_ao.c
+++ b/net/ipv4/tcp_ao.c
@@ -141,7 +141,7 @@ void tcp_ao_calc_traffic_key(const struct tcp_ao_key *mkt, u8 *traffic_key,
traffic_key);
return;
case TCP_AO_ALGO_AES_128_CMAC: {
- struct aes_cmac_key k;
+ struct aes_cmac_key k __cleanup(aes_cmac_zeroize_key);
aes_cmac_preparekey(&k, mkt->key, AES_KEYSIZE_128);
aes_cmac(&k, input, input_len, traffic_key);
--
2.55.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v2 4/6] Bluetooth: SMP: clear the aes_cmac_key when done
2026-08-07 12:58 [PATCH v2 0/6] crypto: Add __cleanup functions for zeroizing aes_cmac_key & aes_cmac_ctx Thomas Huth
` (2 preceding siblings ...)
2026-08-07 12:58 ` [PATCH v2 3/6] net/tcp-ao: clear the aes_cmac_key " Thomas Huth
@ 2026-08-07 12:58 ` Thomas Huth
2026-08-07 12:58 ` [PATCH v2 5/6] lib/crypto: aes: Use __cleanup() for aes_cmac_key instead of memzero_explicit() Thomas Huth
2026-08-07 12:58 ` [PATCH v2 6/6] mac80211: fils_aead: " Thomas Huth
5 siblings, 0 replies; 9+ messages in thread
From: Thomas Huth @ 2026-08-07 12:58 UTC (permalink / raw)
To: Eric Biggers, Marcel Holtmann, Luiz Augusto von Dentz
Cc: Herbert Xu, David S. Miller, linux-crypto, linux-kernel,
Steve French, Namjae Jeon, linux-bluetooth
From: Thomas Huth <thuth@redhat.com>
Clear the local aes_cmac_key structure via __cleanup() function
when we're done with it to avoid that sensitive data could leak on
the stack.
While we're at it, also clear the tmp[] array here that is populated
with a raw version of the original key and thus would leak the same
information via the stack otherwise.
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
net/bluetooth/smp.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/net/bluetooth/smp.c b/net/bluetooth/smp.c
index c4470958b0d57..4a32e4f80b48a 100644
--- a/net/bluetooth/smp.c
+++ b/net/bluetooth/smp.c
@@ -164,7 +164,7 @@ static inline void swap_buf(const u8 *src, u8 *dst, size_t len)
static int smp_aes_cmac(const u8 k[16], const u8 *m, size_t len, u8 mac[16])
{
uint8_t tmp[16], mac_msb[16], msg_msb[CMAC_MSG_MAX];
- struct aes_cmac_key key;
+ struct aes_cmac_key key __cleanup(aes_cmac_zeroize_key);
int err;
if (len > CMAC_MSG_MAX)
@@ -178,6 +178,7 @@ static int smp_aes_cmac(const u8 k[16], const u8 *m, size_t len, u8 mac[16])
SMP_DBG("key %16phN", k);
err = aes_cmac_preparekey(&key, tmp, 16);
+ memzero_explicit(tmp, sizeof(tmp));
if (WARN_ON_ONCE(err)) /* Should never happen, as 16 is valid keylen */
return err;
aes_cmac(&key, msg_msb, len, mac_msb);
--
2.55.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v2 5/6] lib/crypto: aes: Use __cleanup() for aes_cmac_key instead of memzero_explicit()
2026-08-07 12:58 [PATCH v2 0/6] crypto: Add __cleanup functions for zeroizing aes_cmac_key & aes_cmac_ctx Thomas Huth
` (3 preceding siblings ...)
2026-08-07 12:58 ` [PATCH v2 4/6] Bluetooth: SMP: " Thomas Huth
@ 2026-08-07 12:58 ` Thomas Huth
2026-08-07 12:58 ` [PATCH v2 6/6] mac80211: fils_aead: " Thomas Huth
5 siblings, 0 replies; 9+ messages in thread
From: Thomas Huth @ 2026-08-07 12:58 UTC (permalink / raw)
To: Eric Biggers, Jason A. Donenfeld, Ard Biesheuvel
Cc: Herbert Xu, David S. Miller, linux-crypto, linux-kernel,
Steve French, Namjae Jeon
From: Thomas Huth <thuth@redhat.com>
By using __cleanup(aes_cmac_zeroize_key) for clearing the key data,
we can save one line of code here.
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
lib/crypto/aes.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/lib/crypto/aes.c b/lib/crypto/aes.c
index 41aaa82cb1a1c..f1549839b3de0 100644
--- a/lib/crypto/aes.c
+++ b/lib/crypto/aes.c
@@ -740,7 +740,7 @@ EXPORT_SYMBOL_NS_GPL(aes_cbcmac_final, "CRYPTO_INTERNAL");
/* FIPS cryptographic algorithm self-test for AES-CMAC */
static void __init aes_cmac_fips_test(void)
{
- struct aes_cmac_key key;
+ struct aes_cmac_key key __cleanup(aes_cmac_zeroize_key);
u8 mac[AES_BLOCK_SIZE];
if (aes_cmac_preparekey(&key, fips_test_key, sizeof(fips_test_key)) !=
@@ -749,7 +749,6 @@ static void __init aes_cmac_fips_test(void)
aes_cmac(&key, fips_test_data, sizeof(fips_test_data), mac);
if (memcmp(fips_test_aes_cmac_value, mac, sizeof(mac)) != 0)
panic("aes: CMAC FIPS self-test failed (wrong MAC)\n");
- memzero_explicit(&key, sizeof(key));
}
#else /* CONFIG_CRYPTO_LIB_AES_CBC_MACS */
static inline void aes_cmac_fips_test(void)
--
2.55.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v2 6/6] mac80211: fils_aead: Use __cleanup() for aes_cmac_key instead of memzero_explicit()
2026-08-07 12:58 [PATCH v2 0/6] crypto: Add __cleanup functions for zeroizing aes_cmac_key & aes_cmac_ctx Thomas Huth
` (4 preceding siblings ...)
2026-08-07 12:58 ` [PATCH v2 5/6] lib/crypto: aes: Use __cleanup() for aes_cmac_key instead of memzero_explicit() Thomas Huth
@ 2026-08-07 12:58 ` Thomas Huth
2026-08-07 13:00 ` Johannes Berg
5 siblings, 1 reply; 9+ messages in thread
From: Thomas Huth @ 2026-08-07 12:58 UTC (permalink / raw)
To: Eric Biggers, Johannes Berg
Cc: Herbert Xu, David S. Miller, linux-crypto, linux-kernel,
Steve French, Namjae Jeon, linux-wireless
From: Thomas Huth <thuth@redhat.com>
By using __cleanup(aes_cmac_zeroize_key) for clearing the key data,
we can save one line of code here.
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
net/mac80211/fils_aead.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/net/mac80211/fils_aead.c b/net/mac80211/fils_aead.c
index d2f4a17eab990..293590976489a 100644
--- a/net/mac80211/fils_aead.c
+++ b/net/mac80211/fils_aead.c
@@ -24,7 +24,7 @@ static int aes_s2v(const u8 *in_key, size_t key_len,
size_t num_elem, const u8 *addr[], size_t len[], u8 *v)
{
u8 d[AES_BLOCK_SIZE], tmp[AES_BLOCK_SIZE] = {};
- struct aes_cmac_key key;
+ struct aes_cmac_key key __cleanup(aes_cmac_zeroize_key);
struct aes_cmac_ctx ctx;
size_t i;
int res;
@@ -62,7 +62,6 @@ static int aes_s2v(const u8 *in_key, size_t key_len,
aes_cmac_update(&ctx, d, AES_BLOCK_SIZE);
aes_cmac_final(&ctx, v);
- memzero_explicit(&key, sizeof(key));
return 0;
}
--
2.55.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH v2 6/6] mac80211: fils_aead: Use __cleanup() for aes_cmac_key instead of memzero_explicit()
2026-08-07 12:58 ` [PATCH v2 6/6] mac80211: fils_aead: " Thomas Huth
@ 2026-08-07 13:00 ` Johannes Berg
0 siblings, 0 replies; 9+ messages in thread
From: Johannes Berg @ 2026-08-07 13:00 UTC (permalink / raw)
To: Thomas Huth, Eric Biggers
Cc: Herbert Xu, David S. Miller, linux-crypto, linux-kernel,
Steve French, Namjae Jeon, linux-wireless
On Fri, 2026-08-07 at 14:58 +0200, Thomas Huth wrote:
> From: Thomas Huth <thuth@redhat.com>
>
> By using __cleanup(aes_cmac_zeroize_key) for clearing the key data,
> we can save one line of code here.
I'm assuming this won't be going via my tree, since it's part of a
bigger series.
Acked-by: Johannes Berg <johannes@sipsolutions.net>
johannes
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v2 3/6] net/tcp-ao: clear the aes_cmac_key when done
2026-08-07 12:58 ` [PATCH v2 3/6] net/tcp-ao: clear the aes_cmac_key " Thomas Huth
@ 2026-08-07 22:22 ` Jakub Kicinski
0 siblings, 0 replies; 9+ messages in thread
From: Jakub Kicinski @ 2026-08-07 22:22 UTC (permalink / raw)
To: Thomas Huth
Cc: Eric Biggers, Eric Dumazet, Neal Cardwell, David S. Miller,
Paolo Abeni, Herbert Xu, linux-crypto, linux-kernel, Steve French,
Namjae Jeon, Kuniyuki Iwashima, Simon Horman, netdev
On Fri, 7 Aug 2026 14:58:40 +0200 Thomas Huth wrote:
> - struct aes_cmac_key k;
> + struct aes_cmac_key k __cleanup(aes_cmac_zeroize_key);
Nak, we don't like the auto-cleanup stuff in networking.
If y'all need a garbage collector there're jobs programming in Java.
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2026-08-07 22:22 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-07 12:58 [PATCH v2 0/6] crypto: Add __cleanup functions for zeroizing aes_cmac_key & aes_cmac_ctx Thomas Huth
2026-08-07 12:58 ` [PATCH v2 1/6] crypto: Provide wrapper functions for zeroizing aes_cmac_key and aes_cmac_ctx Thomas Huth
2026-08-07 12:58 ` [PATCH v2 2/6] smb: clear the aes_cmac_key and aes_cmac_ctx when done Thomas Huth
2026-08-07 12:58 ` [PATCH v2 3/6] net/tcp-ao: clear the aes_cmac_key " Thomas Huth
2026-08-07 22:22 ` Jakub Kicinski
2026-08-07 12:58 ` [PATCH v2 4/6] Bluetooth: SMP: " Thomas Huth
2026-08-07 12:58 ` [PATCH v2 5/6] lib/crypto: aes: Use __cleanup() for aes_cmac_key instead of memzero_explicit() Thomas Huth
2026-08-07 12:58 ` [PATCH v2 6/6] mac80211: fils_aead: " Thomas Huth
2026-08-07 13:00 ` Johannes Berg
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox