Skip to contentSkip to conversation

crypto: nx: Fix memcpy() over-reading in nonce

PATCH In review Kees Cook 3 participants 4 messages Jun 16, 2021 – Jun 24, 2021

Subsystems (4)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 crypto: nx: Fix memcpy() over-reading in nonce

Fix typo in memcpy() where size should be CTR_RFC3686_NONCE_SIZE.

Fixes 030f4e968741 ("crypto: nx - Fix reentrancy bugs")
---
 drivers/crypto/nx/nx-aes-ctr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/crypto/nx/nx-aes-ctr.c +1 -1
index 13f518802343..6120e350ff71 100644
@@ -118,7 +118,7 @@ static int ctr3686_aes_nx_crypt(struct skcipher_request *req)
 	struct nx_crypto_ctx *nx_ctx = crypto_skcipher_ctx(tfm);
 	u8 iv[16];
 
-	memcpy(iv, nx_ctx->priv.ctr.nonce, CTR_RFC3686_IV_SIZE);
+	memcpy(iv, nx_ctx->priv.ctr.nonce, CTR_RFC3686_NONCE_SIZE);
 	memcpy(iv + CTR_RFC3686_NONCE_SIZE, req->iv, CTR_RFC3686_IV_SIZE);
 	iv[12] = iv[13] = iv[14] = 0;
 	iv[15] = 1;
Signature · 1 line
2.25.1
#1 lore raw
Quoted 5 lines · Kees Cook

Fix typo in memcpy() where size should be CTR_RFC3686_NONCE_SIZE.

Fixes 030f4e968741 ("crypto: nx - Fix reentrancy bugs")

Thanks.

Quoted 9 lines
---
 drivers/crypto/nx/nx-aes-ctr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/crypto/nx/nx-aes-ctr.c +0 -0
index 13f518802343..6120e350ff71 100644

drivers/crypto/nx/nx-aes-ctr.c static int ctr3686_aes_nx_crypt(struct skcipher_request *req) · line 124

Quoted 8 lines
drivers/crypto/nx/nx-aes-ctr.c +1 -1
@@ -118,7 +118,7 @@ static int ctr3686_aes_nx_crypt(struct skcipher_request *req)
 	struct nx_crypto_ctx *nx_ctx = crypto_skcipher_ctx(tfm);
 	u8 iv[16];
 
-	memcpy(iv, nx_ctx->priv.ctr.nonce, CTR_RFC3686_IV_SIZE);
+	memcpy(iv, nx_ctx->priv.ctr.nonce, CTR_RFC3686_NONCE_SIZE);
 	memcpy(iv + CTR_RFC3686_NONCE_SIZE, req->iv, CTR_RFC3686_IV_SIZE);
 	iv[12] = iv[13] = iv[14] = 0;
 	iv[15] = 1;

Where IV_SIZE is 8 and NONCE_SIZE is 4.

And iv is 16 bytes, so it's not a buffer overflow.

But priv.ctr.nonce is 4 bytes, and at the end of the struct, so it reads 4 bytes past the end of the nx_crypto_ctx, which is not good.

But then immediately overwrites whatever it read with req->iv.

So seems pretty harmless in practice?

cheers

author
#2 lore raw
Quoted 29 lines · Michael Ellerman
Quoted 5 lines · Kees Cook

Fix typo in memcpy() where size should be CTR_RFC3686_NONCE_SIZE.

Fixes 030f4e968741 ("crypto: nx - Fix reentrancy bugs")

Thanks.

Quoted 9 lines
---
 drivers/crypto/nx/nx-aes-ctr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/crypto/nx/nx-aes-ctr.c +0 -0
index 13f518802343..6120e350ff71 100644

drivers/crypto/nx/nx-aes-ctr.c static int ctr3686_aes_nx_crypt(struct skcipher_request *req) · line 124

Quoted 8 lines
drivers/crypto/nx/nx-aes-ctr.c +1 -1
@@ -118,7 +118,7 @@ static int ctr3686_aes_nx_crypt(struct skcipher_request *req)
 	struct nx_crypto_ctx *nx_ctx = crypto_skcipher_ctx(tfm);
 	u8 iv[16];
 
-	memcpy(iv, nx_ctx->priv.ctr.nonce, CTR_RFC3686_IV_SIZE);
+	memcpy(iv, nx_ctx->priv.ctr.nonce, CTR_RFC3686_NONCE_SIZE);
 	memcpy(iv + CTR_RFC3686_NONCE_SIZE, req->iv, CTR_RFC3686_IV_SIZE);
 	iv[12] = iv[13] = iv[14] = 0;
 	iv[15] = 1;

Where IV_SIZE is 8 and NONCE_SIZE is 4.

Quoted 8 lines

And iv is 16 bytes, so it's not a buffer overflow.

But priv.ctr.nonce is 4 bytes, and at the end of the struct, so it reads 4 bytes past the end of the nx_crypto_ctx, which is not good.

But then immediately overwrites whatever it read with req->iv.

So seems pretty harmless in practice?

Right -- there's no damage done, but future memcpy() FORTIFY work alerts on this, so I'm going through cleaning all of these up. :)

Signature · 1 line
Kees Cook
maintainer
#3 lore raw
In reply to Kees Cook
Quoted 8 lines · Kees Cook

Fix typo in memcpy() where size should be CTR_RFC3686_NONCE_SIZE.

Fixes 030f4e968741 ("crypto: nx - Fix reentrancy bugs")
---
 drivers/crypto/nx/nx-aes-ctr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Patch applied. Thanks.

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt