Skip to contentSkip to conversation

mac80211: aes-cmac: remove VLA usage

PATCH In review Gustavo A. R. Silva 2 participants 5 messages Mar 21, 2018

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mac80211: aes-cmac: remove VLA usage

In preparation to enabling -Wvla, remove VLAs and replace them with dynamic memory allocation instead.

The use of stack Variable Length Arrays needs to be avoided, as they can be a vector for stack exhaustion, which can be both a runtime bug or a security flaw. Also, in general, as code evolves it is easy to lose track of how big a VLA can get. Thus, we can end up having runtime failures that are hard to debug.

Also, fixed as part of the directive to remove all VLAs from the kernel: https://lkml.org/lkml/2018/3/7/621

---
 net/mac80211/aes_cmac.c | 36 ++++++++++++++++++++++++------------
 1 file changed, 24 insertions(+), 12 deletions(-)
net/mac80211/aes_cmac.c +24 -12
index 2fb6558..c9444bf 100644
@@ -27,30 +27,42 @@ static const u8 zero[CMAC_TLEN_256];
 void ieee80211_aes_cmac(struct crypto_shash *tfm, const u8 *aad,
 			const u8 *data, size_t data_len, u8 *mic)
 {
-	SHASH_DESC_ON_STACK(desc, tfm);
+	struct shash_desc *shash;
 	u8 out[AES_BLOCK_SIZE];
 
-	desc->tfm = tfm;
+	shash = kmalloc(sizeof(*shash) + crypto_shash_descsize(tfm),
+			GFP_KERNEL);
+	if (!shash)
+		return;
 
-	crypto_shash_init(desc);
-	crypto_shash_update(desc, aad, AAD_LEN);
-	crypto_shash_update(desc, data, data_len - CMAC_TLEN);
-	crypto_shash_finup(desc, zero, CMAC_TLEN, out);
+	shash->tfm = tfm;
+
+	crypto_shash_init(shash);
+	crypto_shash_update(shash, aad, AAD_LEN);
+	crypto_shash_update(shash, data, data_len - CMAC_TLEN);
+	crypto_shash_finup(shash, zero, CMAC_TLEN, out);
 
 	memcpy(mic, out, CMAC_TLEN);
+	kfree(shash);
 }
 
 void ieee80211_aes_cmac_256(struct crypto_shash *tfm, const u8 *aad,
 			    const u8 *data, size_t data_len, u8 *mic)
 {
-	SHASH_DESC_ON_STACK(desc, tfm);
+	struct shash_desc *shash;
+
+	shash = kmalloc(sizeof(*shash) + crypto_shash_descsize(tfm),
+			GFP_KERNEL);
+	if (!shash)
+		return;
 
-	desc->tfm = tfm;
+	shash->tfm = tfm;
 
-	crypto_shash_init(desc);
-	crypto_shash_update(desc, aad, AAD_LEN);
-	crypto_shash_update(desc, data, data_len - CMAC_TLEN_256);
-	crypto_shash_finup(desc, zero, CMAC_TLEN_256, mic);
+	crypto_shash_init(shash);
+	crypto_shash_update(shash, aad, AAD_LEN);
+	crypto_shash_update(shash, data, data_len - CMAC_TLEN_256);
+	crypto_shash_finup(shash, zero, CMAC_TLEN_256, mic);
+	kfree(shash);
 }
 
 struct crypto_shash *ieee80211_aes_cmac_key_setup(const u8 key[],
Signature · 1 line
2.7.4
maintainer
#1 lore raw
Quoted 24 lines · Gustavo A. R. Silva

In preparation to enabling -Wvla, remove VLAs and replace them with dynamic memory allocation instead.

The use of stack Variable Length Arrays needs to be avoided, as they can be a vector for stack exhaustion, which can be both a runtime bug or a security flaw. Also, in general, as code evolves it is easy to lose track of how big a VLA can get. Thus, we can end up having runtime failures that are hard to debug.

Also, fixed as part of the directive to remove all VLAs from the kernel: https://lkml.org/lkml/2018/3/7/621

---
 net/mac80211/aes_cmac.c | 36 ++++++++++++++++++++++++------------
 1 file changed, 24 insertions(+), 12 deletions(-)
net/mac80211/aes_cmac.c +0 -0
index 2fb6558..c9444bf 100644
@@ -27,30 +27,42 @@ static const u8 zero[CMAC_TLEN_256];
 void ieee80211_aes_cmac(struct crypto_shash *tfm, const u8 *aad,
 			const u8 *data, size_t data_len, u8 *mic)

net/mac80211/aes_cmac.c static const u8 zero[CMAC_TLEN_256]; · line 36

Quoted 10 lines
net/mac80211/aes_cmac.c +5 -2
 {
-	SHASH_DESC_ON_STACK(desc, tfm);
+	struct shash_desc *shash;
 	u8 out[AES_BLOCK_SIZE];
 
-	desc->tfm = tfm;
+	shash = kmalloc(sizeof(*shash) + crypto_shash_descsize(tfm),
+			GFP_KERNEL);
+	if (!shash)
+		return;

Honestly, this seems like a really bad idea - you're now hitting kmalloc for every TX/RX frame here.

SHA_DESC_ON_STACK() should just be fixed to not need a VLA, but take some sort of maximum, I guess?

johannes

#2 lore raw
Quoted 39 lines · Johannes Berg
Quoted 24 lines · Gustavo A. R. Silva

In preparation to enabling -Wvla, remove VLAs and replace them with dynamic memory allocation instead.

The use of stack Variable Length Arrays needs to be avoided, as they can be a vector for stack exhaustion, which can be both a runtime bug or a security flaw. Also, in general, as code evolves it is easy to lose track of how big a VLA can get. Thus, we can end up having runtime failures that are hard to debug.

Also, fixed as part of the directive to remove all VLAs from the kernel: https://lkml.org/lkml/2018/3/7/621

---
  net/mac80211/aes_cmac.c | 36 ++++++++++++++++++++++++------------
  1 file changed, 24 insertions(+), 12 deletions(-)
net/mac80211/aes_cmac.c +0 -0
index 2fb6558..c9444bf 100644
@@ -27,30 +27,42 @@ static const u8 zero[CMAC_TLEN_256];
  void ieee80211_aes_cmac(struct crypto_shash *tfm, const u8 *aad,
  			const u8 *data, size_t data_len, u8 *mic)

net/mac80211/aes_cmac.c static const u8 zero[CMAC_TLEN_256]; · line 36

Quoted 10 lines
net/mac80211/aes_cmac.c +5 -2
  {
-	SHASH_DESC_ON_STACK(desc, tfm);
+	struct shash_desc *shash;
  	u8 out[AES_BLOCK_SIZE];
 
-	desc->tfm = tfm;
+	shash = kmalloc(sizeof(*shash) + crypto_shash_descsize(tfm),
+			GFP_KERNEL);
+	if (!shash)
+		return;

Honestly, this seems like a really bad idea - you're now hitting kmalloc for every TX/RX frame here.

Quoted 3 lines

SHA_DESC_ON_STACK() should just be fixed to not need a VLA, but take some sort of maximum, I guess?

SHA_DESC_ON_STACK is currently being used in multiple places. But, yeah, I think we can define multiple macros of the same kind and adjust to the characteristics of each the component.

How big do you think tfm can get?

Thanks
--
Gustavo

maintainer
#3 lore raw
Quoted 6 lines · Gustavo A. R. Silva

SHA_DESC_ON_STACK is currently being used in multiple places. But, yeah, I think we can define multiple macros of the same kind and adjust to the characteristics of each the component.

How big do you think tfm can get?

I have no idea, I guess you'll have to take that with Herbert.

johannes

#4 lore raw
Quoted 6 lines · Johannes Berg
Quoted 5 lines · Gustavo A. R. Silva

SHA_DESC_ON_STACK is currently being used in multiple places. But, yeah, I think we can define multiple macros of the same kind and adjust to the characteristics of each the component.

Quoted 6 lines
Quoted 1 line

How big do you think tfm can get?

I have no idea, I guess you'll have to take that with Herbert.

johannes

I see. I'll contact him then.

Thanks for the feedback.
--
Gustavo