Skip to contentSkip to conversation

big key: get rid of stack array allocation

PATCH In review Tycho Andersen 3 participants 7 messages Mar 13, 2018 – Mar 21, 2018

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

PATCH 1/2 big key: get rid of stack array allocation

We're interested in getting rid of all of the stack allocated arrays in the kernel [1]. This patch removes one in keys by switching to malloc/free. Note that we use kzalloc, to avoid leaking the nonce. I'm not sure this is really necessary, but extra paranoia seems prudent.

Manually tested using the program from the add_key man page to trigger big_key.

[1]: https://lkml.org/lkml/2018/3/7/621

CC David Howells <[email protected]>
CC James Morris <[email protected]>
CC "Serge E. Hallyn" <[email protected]>
CC Jason A. Donenfeld <[email protected]>
---
 security/keys/big_key.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
security/keys/big_key.c +9 -3
index fa728f662a6f..70f9f785c59d 100644
@@ -108,13 +108,18 @@ static int big_key_crypt(enum big_key_op op, struct big_key_buf *buf, size_t dat
 	 * an .update function, so there's no chance we'll wind up reusing the
 	 * key to encrypt updated data. Simply put: one key, one encryption.
 	 */
-	u8 zero_nonce[crypto_aead_ivsize(big_key_aead)];
+	u8 *zero_nonce;
+
+	zero_nonce = kzalloc(crypto_aead_ivsize(big_key_aead), GFP_KERNEL);
+	if (!zero_nonce)
+		return -ENOMEM;
 
 	aead_req = aead_request_alloc(big_key_aead, GFP_KERNEL);
-	if (!aead_req)
+	if (!aead_req) {
+		kfree(zero_nonce);
 		return -ENOMEM;
+	}
 
-	memset(zero_nonce, 0, sizeof(zero_nonce));
 	aead_request_set_crypt(aead_req, buf->sg, buf->sg, datalen, zero_nonce);
 	aead_request_set_callback(aead_req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
 	aead_request_set_ad(aead_req, 0);
@@ -131,6 +136,7 @@ static int big_key_crypt(enum big_key_op op, struct big_key_buf *buf, size_t dat
 error:
 	mutex_unlock(&big_key_aead_lock);
 	aead_request_free(aead_req);
+	kzfree(zero_nonce);
 	return ret;
 }
 
Signature · 1 line
2.15.1
#1 lore raw

PATCH 2/2 dh key: get rid of stack array allocation

Similarly to the previous patch, we would like to get rid of stack allocated arrays: https://lkml.org/lkml/2018/3/7/621

In this case, we can also use a malloc style approach to free the temporary buffer, being careful to also use kzfree to free them (indeed, at least one of these has a memzero_explicit, but it seems like maybe they both should?).

CC David Howells <[email protected]>
CC James Morris <[email protected]>
CC "Serge E. Hallyn" <[email protected]>
---
 security/keys/dh.c | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)
security/keys/dh.c +21 -6
index d1ea9f325f94..f02261b24759 100644
@@ -162,19 +162,27 @@ static int kdf_ctr(struct kdf_sdesc *sdesc, const u8 *src, unsigned int slen,
 			goto err;
 
 		if (zlen && h) {
-			u8 tmpbuffer[h];
+			u8 *tmpbuffer;
 			size_t chunk = min_t(size_t, zlen, h);
-			memset(tmpbuffer, 0, chunk);
+
+			err = -ENOMEM;
+			tmpbuffer = kzalloc(chunk, GFP_KERNEL);
+			if (!tmpbuffer)
+				goto err;
 
 			do {
 				err = crypto_shash_update(desc, tmpbuffer,
 							  chunk);
-				if (err)
+				if (err) {
+					kzfree(tmpbuffer);
 					goto err;
+				}
 
 				zlen -= chunk;
 				chunk = min_t(size_t, zlen, h);
 			} while (zlen);
+
+			kzfree(tmpbuffer);
 		}
 
 		if (src && slen) {
@@ -184,13 +192,20 @@ static int kdf_ctr(struct kdf_sdesc *sdesc, const u8 *src, unsigned int slen,
 		}
 
 		if (dlen < h) {
-			u8 tmpbuffer[h];
+			u8 *tmpbuffer;
+
+			err = -ENOMEM;
+			tmpbuffer = kzalloc(h, GFP_KERNEL);
+			if (!tmpbuffer)
+				goto err;
 
 			err = crypto_shash_final(desc, tmpbuffer);
-			if (err)
+			if (err) {
+				kzfree(tmpbuffer);
 				goto err;
+			}
 			memcpy(dst, tmpbuffer, dlen);
-			memzero_explicit(tmpbuffer, h);
+			kzfree(tmpbuffer);
 			return 0;
 		} else {
 			err = crypto_shash_final(desc, dst);
Signature · 1 line
2.15.1
maintainer Acked-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] dh key: get rid of stack array allocation

Quoted 8 lines · Tycho Andersen

Similarly to the previous patch, we would like to get rid of stack allocated arrays: https://lkml.org/lkml/2018/3/7/621

In this case, we can also use a malloc style approach to free the temporary buffer, being careful to also use kzfree to free them (indeed, at least one of these has a memzero_explicit, but it seems like maybe they both should?).

Quoted 4 lines
CC David Howells <[email protected]>
CC James Morris <[email protected]>
CC "Serge E. Hallyn" <[email protected]>

for both, thanks.

Quoted 65 lines
---
 security/keys/dh.c | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)
security/keys/dh.c +21 -6
index d1ea9f325f94..f02261b24759 100644
@@ -162,19 +162,27 @@ static int kdf_ctr(struct kdf_sdesc *sdesc, const u8 *src, unsigned int slen,
 			goto err;
 
 		if (zlen && h) {
-			u8 tmpbuffer[h];
+			u8 *tmpbuffer;
 			size_t chunk = min_t(size_t, zlen, h);
-			memset(tmpbuffer, 0, chunk);
+
+			err = -ENOMEM;
+			tmpbuffer = kzalloc(chunk, GFP_KERNEL);
+			if (!tmpbuffer)
+				goto err;
 
 			do {
 				err = crypto_shash_update(desc, tmpbuffer,
 							  chunk);
-				if (err)
+				if (err) {
+					kzfree(tmpbuffer);
 					goto err;
+				}
 
 				zlen -= chunk;
 				chunk = min_t(size_t, zlen, h);
 			} while (zlen);
+
+			kzfree(tmpbuffer);
 		}
 
 		if (src && slen) {
@@ -184,13 +192,20 @@ static int kdf_ctr(struct kdf_sdesc *sdesc, const u8 *src, unsigned int slen,
 		}
 
 		if (dlen < h) {
-			u8 tmpbuffer[h];
+			u8 *tmpbuffer;
+
+			err = -ENOMEM;
+			tmpbuffer = kzalloc(h, GFP_KERNEL);
+			if (!tmpbuffer)
+				goto err;
 
 			err = crypto_shash_final(desc, tmpbuffer);
-			if (err)
+			if (err) {
+				kzfree(tmpbuffer);
 				goto err;
+			}
 			memcpy(dst, tmpbuffer, dlen);
-			memzero_explicit(tmpbuffer, h);
+			kzfree(tmpbuffer);
 			return 0;
 		} else {
 			err = crypto_shash_final(desc, dst);
Signature · 1 line
2.15.1
#3 lore raw

Re: [PATCH 2/2] dh key: get rid of stack array allocation

In reply to Tycho Andersen
Quoted 39 lines · Tycho Andersen

Similarly to the previous patch, we would like to get rid of stack allocated arrays: https://lkml.org/lkml/2018/3/7/621

In this case, we can also use a malloc style approach to free the temporary buffer, being careful to also use kzfree to free them (indeed, at least one of these has a memzero_explicit, but it seems like maybe they both should?).

CC David Howells <[email protected]>
CC James Morris <[email protected]>
CC "Serge E. Hallyn" <[email protected]>
---
 security/keys/dh.c | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)
security/keys/dh.c +7 -3
index d1ea9f325f94..f02261b24759 100644
@@ -162,19 +162,27 @@ static int kdf_ctr(struct kdf_sdesc *sdesc, const u8 *src, unsigned int slen,
 			goto err;
 
 		if (zlen && h) {
-			u8 tmpbuffer[h];
+			u8 *tmpbuffer;
 			size_t chunk = min_t(size_t, zlen, h);
-			memset(tmpbuffer, 0, chunk);
+
+			err = -ENOMEM;
+			tmpbuffer = kzalloc(chunk, GFP_KERNEL);
+			if (!tmpbuffer)
+				goto err;
 
 			do {
 				err = crypto_shash_update(desc, tmpbuffer,
 							  chunk);
-				if (err)
+				if (err) {

security/keys/dh.c static int kdf_ctr(struct kdf_sdesc *sdesc, const u8 *src, unsigned int slen, · line 186

Quoted 10 lines
security/keys/dh.c +4 -0
+					kzfree(tmpbuffer);
 					goto err;
+				}
 
 				zlen -= chunk;
 				chunk = min_t(size_t, zlen, h);
 			} while (zlen);
+
+			kzfree(tmpbuffer);
 		}

This is just hashing zeroes. Why not use the zeroes at the end of the 'src' buffer which was allocated as 'outbuf' in __keyctl_dh_compute()? It's already the right size. It might even simplify the code a bit since crypto_shash_update() would no longer need to be in a loop.

Quoted 17 lines
security/keys/dh.c +6 -2
 		if (src && slen) {
@@ -184,13 +192,20 @@ static int kdf_ctr(struct kdf_sdesc *sdesc, const u8 *src, unsigned int slen,
 		}
 
 		if (dlen < h) {
-			u8 tmpbuffer[h];
+			u8 *tmpbuffer;
+
+			err = -ENOMEM;
+			tmpbuffer = kzalloc(h, GFP_KERNEL);
+			if (!tmpbuffer)
+				goto err;
 
 			err = crypto_shash_final(desc, tmpbuffer);
-			if (err)

security/keys/dh.c static int kdf_ctr(struct kdf_sdesc *sdesc, const u8 *src, unsigned int slen, · line 211

Quoted 10 lines
security/keys/dh.c +4 -1
+			if (err) {
+				kzfree(tmpbuffer);
 				goto err;
+			}
 			memcpy(dst, tmpbuffer, dlen);
-			memzero_explicit(tmpbuffer, h);
+			kzfree(tmpbuffer);
 			return 0;
 		} else {
 			err = crypto_shash_final(desc, dst);

Why not instead round the allocated size of 'outbuf' in keyctl_dh_compute_kdf() up to the next 'crypto_shash_digestsize()'-boundary? Then this temporary buffer wouldn't be needed at all.

It would be nice if people thought about how to properly solve the problems when doing these VLA conversions, rather than mindlessly replacing them with kmalloc...

Eric

#4 lore raw

Re: [PATCH 2/2] dh key: get rid of stack array allocation

Hi Eric,

Quoted 44 lines · Eric Biggers
Quoted 43 lines · Tycho Andersen

Similarly to the previous patch, we would like to get rid of stack allocated arrays: https://lkml.org/lkml/2018/3/7/621

In this case, we can also use a malloc style approach to free the temporary buffer, being careful to also use kzfree to free them (indeed, at least one of these has a memzero_explicit, but it seems like maybe they both should?).

CC David Howells <[email protected]>
CC James Morris <[email protected]>
CC "Serge E. Hallyn" <[email protected]>
---
 security/keys/dh.c | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)
security/keys/dh.c +9 -3
index d1ea9f325f94..f02261b24759 100644
@@ -162,19 +162,27 @@ static int kdf_ctr(struct kdf_sdesc *sdesc, const u8 *src, unsigned int slen,
 			goto err;
 
 		if (zlen && h) {
-			u8 tmpbuffer[h];
+			u8 *tmpbuffer;
 			size_t chunk = min_t(size_t, zlen, h);
-			memset(tmpbuffer, 0, chunk);
+
+			err = -ENOMEM;
+			tmpbuffer = kzalloc(chunk, GFP_KERNEL);
+			if (!tmpbuffer)
+				goto err;
 
 			do {
 				err = crypto_shash_update(desc, tmpbuffer,
 							  chunk);
-				if (err)
+				if (err) {
+					kzfree(tmpbuffer);
 					goto err;
+				}
 
Quoted 11 lines

security/keys/dh.c static int kdf_ctr(struct kdf_sdesc *sdesc, const u8 *src, unsigned int slen, · line 186

Quoted 6 lines
security/keys/dh.c +2 -0
 				zlen -= chunk;
 				chunk = min_t(size_t, zlen, h);
 			} while (zlen);
+
+			kzfree(tmpbuffer);
 		}

This is just hashing zeroes. Why not use the zeroes at the end of the 'src' buffer which was allocated as 'outbuf' in __keyctl_dh_compute()? It's already the right size. It might even simplify the code a bit since crypto_shash_update() would no longer need to be in a loop.

Can you clarify what you mean by the "end" here? It looks like the end is copied over with the user string just before it's passed into keyctl_dh_compute_kdf().

In any case, I agree that it's dumb to do this allocation in a loop now. What if instead we just do one big long allocation of zlen, hash it, and then free it? This has the advantage that it's not allocated two functions away from where it's used...

Quoted 23 lines
Quoted 23 lines
security/keys/dh.c +10 -3
 		if (src && slen) {
@@ -184,13 +192,20 @@ static int kdf_ctr(struct kdf_sdesc *sdesc, const u8 *src, unsigned int slen,
 		}
 
 		if (dlen < h) {
-			u8 tmpbuffer[h];
+			u8 *tmpbuffer;
+
+			err = -ENOMEM;
+			tmpbuffer = kzalloc(h, GFP_KERNEL);
+			if (!tmpbuffer)
+				goto err;
 
 			err = crypto_shash_final(desc, tmpbuffer);
-			if (err)
+			if (err) {
+				kzfree(tmpbuffer);
 				goto err;
+			}
 			memcpy(dst, tmpbuffer, dlen);
-			memzero_explicit(tmpbuffer, h);
+			kzfree(tmpbuffer);
Quoted 8 lines

security/keys/dh.c static int kdf_ctr(struct kdf_sdesc *sdesc, const u8 *src, unsigned int slen, · line 211

Quoted 4 lines
security/keys/dh.c +0 -0
 			return 0;
 		} else {
 			err = crypto_shash_final(desc, dst);

Why not instead round the allocated size of 'outbuf' in keyctl_dh_compute_kdf() up to the next 'crypto_shash_digestsize()'-boundary? Then this temporary buffer wouldn't be needed at all.

Thanks, I've made this change (and split it out into a separate patch) for v2.

Tycho

#5 lore raw
In reply to Tycho Andersen
Quoted 46 lines · Tycho Andersen

We're interested in getting rid of all of the stack allocated arrays in the kernel [1]. This patch removes one in keys by switching to malloc/free. Note that we use kzalloc, to avoid leaking the nonce. I'm not sure this is really necessary, but extra paranoia seems prudent.

Manually tested using the program from the add_key man page to trigger big_key.

[1]: https://lkml.org/lkml/2018/3/7/621

CC David Howells <[email protected]>
CC James Morris <[email protected]>
CC "Serge E. Hallyn" <[email protected]>
CC Jason A. Donenfeld <[email protected]>
---
 security/keys/big_key.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
security/keys/big_key.c +8 -3
index fa728f662a6f..70f9f785c59d 100644
@@ -108,13 +108,18 @@ static int big_key_crypt(enum big_key_op op, struct big_key_buf *buf, size_t dat
 	 * an .update function, so there's no chance we'll wind up reusing the
 	 * key to encrypt updated data. Simply put: one key, one encryption.
 	 */
-	u8 zero_nonce[crypto_aead_ivsize(big_key_aead)];
+	u8 *zero_nonce;
+
+	zero_nonce = kzalloc(crypto_aead_ivsize(big_key_aead), GFP_KERNEL);
+	if (!zero_nonce)
+		return -ENOMEM;
 
 	aead_req = aead_request_alloc(big_key_aead, GFP_KERNEL);
-	if (!aead_req)
+	if (!aead_req) {
+		kfree(zero_nonce);
 		return -ENOMEM;
+	}
 
-	memset(zero_nonce, 0, sizeof(zero_nonce));
 	aead_request_set_crypt(aead_req, buf->sg, buf->sg, datalen, zero_nonce);
 	aead_request_set_callback(aead_req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
 	aead_request_set_ad(aead_req, 0);

security/keys/big_key.c static int big_key_crypt(enum big_key_op op, struct big_key_buf *buf, size_t dat · line 140

Quoted 5 lines
security/keys/big_key.c +1 -0
@@ -131,6 +136,7 @@ static int big_key_crypt(enum big_key_op op, struct big_key_buf *buf, size_t dat
 error:
 	mutex_unlock(&big_key_aead_lock);
 	aead_request_free(aead_req);
+	kzfree(zero_nonce);
 	return ret;

A dynamic allocation here doesn't make sense -- the algorithm is hard-coded to AES-GCM, so the IV size is fixed. You should just include <crypto/gcm.h> and use GCM_AES_IV_LEN. As a sanity check you can add 'BUG_ON(crypto_aead_ivsize(big_key_aead) != GCM_AES_IV_LEN' to big_key_init().

kzfree() also doesn't make sense since the nonce is not secret information.

Thanks,

Eric

#6 lore raw

Hi Eric,

Quoted 58 lines · Eric Biggers
Quoted 46 lines · Tycho Andersen

We're interested in getting rid of all of the stack allocated arrays in the kernel [1]. This patch removes one in keys by switching to malloc/free. Note that we use kzalloc, to avoid leaking the nonce. I'm not sure this is really necessary, but extra paranoia seems prudent.

Manually tested using the program from the add_key man page to trigger big_key.

[1]: https://lkml.org/lkml/2018/3/7/621

CC David Howells <[email protected]>
CC James Morris <[email protected]>
CC "Serge E. Hallyn" <[email protected]>
CC Jason A. Donenfeld <[email protected]>
---
 security/keys/big_key.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
security/keys/big_key.c +8 -3
index fa728f662a6f..70f9f785c59d 100644
@@ -108,13 +108,18 @@ static int big_key_crypt(enum big_key_op op, struct big_key_buf *buf, size_t dat
 	 * an .update function, so there's no chance we'll wind up reusing the
 	 * key to encrypt updated data. Simply put: one key, one encryption.
 	 */
-	u8 zero_nonce[crypto_aead_ivsize(big_key_aead)];
+	u8 *zero_nonce;
+
+	zero_nonce = kzalloc(crypto_aead_ivsize(big_key_aead), GFP_KERNEL);
+	if (!zero_nonce)
+		return -ENOMEM;
 
 	aead_req = aead_request_alloc(big_key_aead, GFP_KERNEL);
-	if (!aead_req)
+	if (!aead_req) {
+		kfree(zero_nonce);
 		return -ENOMEM;
+	}
 
-	memset(zero_nonce, 0, sizeof(zero_nonce));
 	aead_request_set_crypt(aead_req, buf->sg, buf->sg, datalen, zero_nonce);
 	aead_request_set_callback(aead_req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
 	aead_request_set_ad(aead_req, 0);

security/keys/big_key.c static int big_key_crypt(enum big_key_op op, struct big_key_buf *buf, size_t dat · line 140

Quoted 5 lines
security/keys/big_key.c +1 -0
@@ -131,6 +136,7 @@ static int big_key_crypt(enum big_key_op op, struct big_key_buf *buf, size_t dat
 error:
 	mutex_unlock(&big_key_aead_lock);
 	aead_request_free(aead_req);
+	kzfree(zero_nonce);
 	return ret;

A dynamic allocation here doesn't make sense -- the algorithm is hard-coded to AES-GCM, so the IV size is fixed. You should just include <crypto/gcm.h> and use GCM_AES_IV_LEN. As a sanity check you can add 'BUG_ON(crypto_aead_ivsize(big_key_aead) != GCM_AES_IV_LEN' to big_key_init().

Quoted 1 line

kzfree() also doesn't make sense since the nonce is not secret information.

Thanks, I've fixed this for v2.

Cheers,

Tycho