Skip to contentSkip to conversation

nvme-auth: Fix off by one checks

PATCH Applied Dan Carpenter 3 participants 7 messages Jul 18, 2022 – Jul 25, 2022

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

PATCH 1/2 nvme-auth: Fix off by one checks

The > ARRAY_SIZE() checks need to be >= ARRAY_SIZE() to prevent reading one element beyond the end of the arrays.

Fixes a476416bb57b ("nvme: implement In-Band authentication")

---
The MAINTAINERS file needs to be updated for this new code.

 drivers/nvme/common/auth.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/nvme/common/auth.c +5 -5
index 0c86ebce59d2..bfb16fec0aed 100644
@@ -55,7 +55,7 @@ static struct nvme_auth_dhgroup_map {
 
 const char *nvme_auth_dhgroup_name(u8 dhgroup_id)
 {
-	if ((dhgroup_id > ARRAY_SIZE(dhgroup_map)) ||
+	if ((dhgroup_id >= ARRAY_SIZE(dhgroup_map)) ||
 	    !dhgroup_map[dhgroup_id].name ||
 	    !strlen(dhgroup_map[dhgroup_id].name))
 		return NULL;
@@ -65,7 +65,7 @@ EXPORT_SYMBOL_GPL(nvme_auth_dhgroup_name);
 
 const char *nvme_auth_dhgroup_kpp(u8 dhgroup_id)
 {
-	if ((dhgroup_id > ARRAY_SIZE(dhgroup_map)) ||
+	if ((dhgroup_id >= ARRAY_SIZE(dhgroup_map)) ||
 	    !dhgroup_map[dhgroup_id].kpp ||
 	    !strlen(dhgroup_map[dhgroup_id].kpp))
 		return NULL;
@@ -113,7 +113,7 @@ static struct nvme_dhchap_hash_map {
 
 const char *nvme_auth_hmac_name(u8 hmac_id)
 {
-	if ((hmac_id > ARRAY_SIZE(hash_map)) ||
+	if ((hmac_id >= ARRAY_SIZE(hash_map)) ||
 	    !hash_map[hmac_id].hmac ||
 	    !strlen(hash_map[hmac_id].hmac))
 		return NULL;
@@ -123,7 +123,7 @@ EXPORT_SYMBOL_GPL(nvme_auth_hmac_name);
 
 const char *nvme_auth_digest_name(u8 hmac_id)
 {
-	if ((hmac_id > ARRAY_SIZE(hash_map)) ||
+	if ((hmac_id >= ARRAY_SIZE(hash_map)) ||
 	    !hash_map[hmac_id].digest ||
 	    !strlen(hash_map[hmac_id].digest))
 		return NULL;
@@ -148,7 +148,7 @@ EXPORT_SYMBOL_GPL(nvme_auth_hmac_id);
 
 size_t nvme_auth_hmac_hash_len(u8 hmac_id)
 {
-	if ((hmac_id > ARRAY_SIZE(hash_map)) ||
+	if ((hmac_id >= ARRAY_SIZE(hash_map)) ||
 	    !hash_map[hmac_id].hmac ||
 	    !strlen(hash_map[hmac_id].hmac))
 		return 0;
Signature · 1 line
2.35.1
#1 lore raw

PATCH 1/1 nvme-auth: Uninitialized variable in nvme_auth_transform_key()

A couple of the early error gotos call kfree_sensitive(transformed_key); before "transformed_key" has been initialized.

Fixes a476416bb57b ("nvme: implement In-Band authentication")

---
I wrote this several times in different ways, and kept on doing it wrong so in the end I wrote it this way. It's a bigger diff, but I think it's the clearest way to write it.

 drivers/nvme/common/auth.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)
drivers/nvme/common/auth.c +16 -9
index bfb16fec0aed..1890193c7333 100644
@@ -278,26 +278,33 @@ u8 *nvme_auth_transform_key(struct nvme_dhchap_key *key, char *nqn)
 	shash->tfm = key_tfm;
 	ret = crypto_shash_setkey(key_tfm, key->key, key->len);
 	if (ret < 0)
-		goto out_free_shash;
+		goto out_free_transformed_key;
 	ret = crypto_shash_init(shash);
 	if (ret < 0)
-		goto out_free_shash;
+		goto out_free_transformed_key;
 	ret = crypto_shash_update(shash, nqn, strlen(nqn));
 	if (ret < 0)
-		goto out_free_shash;
+		goto out_free_transformed_key;
 	ret = crypto_shash_update(shash, "NVMe-over-Fabrics", 17);
 	if (ret < 0)
-		goto out_free_shash;
+		goto out_free_transformed_key;
 	ret = crypto_shash_final(shash, transformed_key);
+	if (ret < 0)
+		goto out_free_transformed_key;
+
+	kfree(shash);
+	crypto_free_shash(key_tfm);
+
+	return transformed_key;
+
+out_free_transformed_key:
+	kfree_sensitive(transformed_key);
 out_free_shash:
 	kfree(shash);
 out_free_key:
 	crypto_free_shash(key_tfm);
-	if (ret < 0) {
-		kfree_sensitive(transformed_key);
-		return ERR_PTR(ret);
-	}
-	return transformed_key;
+
+	return ERR_PTR(ret);
 }
 EXPORT_SYMBOL_GPL(nvme_auth_transform_key);
 
Signature · 1 line
2.35.1
#2 lore raw

Re: [PATCH] nvme-auth: Uninitialized variable in nvme_auth_transform_key()

Sorry, the subject was supposed to be [PATCH 2/2]. Do I need to resend for patchwork to accept it?

regards,
dan carpenter

#4 lore raw
In reply to Dan Carpenter

Hannes, can you review these please?

Quoted 64 lines · Dan Carpenter

The > ARRAY_SIZE() checks need to be >= ARRAY_SIZE() to prevent reading one element beyond the end of the arrays.

Fixes a476416bb57b ("nvme: implement In-Band authentication")

---
The MAINTAINERS file needs to be updated for this new code.

 drivers/nvme/common/auth.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/nvme/common/auth.c +5 -5
index 0c86ebce59d2..bfb16fec0aed 100644
@@ -55,7 +55,7 @@ static struct nvme_auth_dhgroup_map {
 
 const char *nvme_auth_dhgroup_name(u8 dhgroup_id)
 {
-	if ((dhgroup_id > ARRAY_SIZE(dhgroup_map)) ||
+	if ((dhgroup_id >= ARRAY_SIZE(dhgroup_map)) ||
 	    !dhgroup_map[dhgroup_id].name ||
 	    !strlen(dhgroup_map[dhgroup_id].name))
 		return NULL;
@@ -65,7 +65,7 @@ EXPORT_SYMBOL_GPL(nvme_auth_dhgroup_name);
 
 const char *nvme_auth_dhgroup_kpp(u8 dhgroup_id)
 {
-	if ((dhgroup_id > ARRAY_SIZE(dhgroup_map)) ||
+	if ((dhgroup_id >= ARRAY_SIZE(dhgroup_map)) ||
 	    !dhgroup_map[dhgroup_id].kpp ||
 	    !strlen(dhgroup_map[dhgroup_id].kpp))
 		return NULL;
@@ -113,7 +113,7 @@ static struct nvme_dhchap_hash_map {
 
 const char *nvme_auth_hmac_name(u8 hmac_id)
 {
-	if ((hmac_id > ARRAY_SIZE(hash_map)) ||
+	if ((hmac_id >= ARRAY_SIZE(hash_map)) ||
 	    !hash_map[hmac_id].hmac ||
 	    !strlen(hash_map[hmac_id].hmac))
 		return NULL;
@@ -123,7 +123,7 @@ EXPORT_SYMBOL_GPL(nvme_auth_hmac_name);
 
 const char *nvme_auth_digest_name(u8 hmac_id)
 {
-	if ((hmac_id > ARRAY_SIZE(hash_map)) ||
+	if ((hmac_id >= ARRAY_SIZE(hash_map)) ||
 	    !hash_map[hmac_id].digest ||
 	    !strlen(hash_map[hmac_id].digest))
 		return NULL;
@@ -148,7 +148,7 @@ EXPORT_SYMBOL_GPL(nvme_auth_hmac_id);
 
 size_t nvme_auth_hmac_hash_len(u8 hmac_id)
 {
-	if ((hmac_id > ARRAY_SIZE(hash_map)) ||
+	if ((hmac_id >= ARRAY_SIZE(hash_map)) ||
 	    !hash_map[hmac_id].hmac ||
 	    !strlen(hash_map[hmac_id].hmac))
 		return 0;
Signature · 1 line
2.35.1

---end quoted text---

Reviewed-by on v1 1/2
#5 lore raw
In reply to Dan Carpenter
Quoted 52 lines · Dan Carpenter

The > ARRAY_SIZE() checks need to be >= ARRAY_SIZE() to prevent reading one element beyond the end of the arrays.

Fixes a476416bb57b ("nvme: implement In-Band authentication")

---
The MAINTAINERS file needs to be updated for this new code.

  drivers/nvme/common/auth.c | 10 +++++-----
  1 file changed, 5 insertions(+), 5 deletions(-)
drivers/nvme/common/auth.c +4 -4
index 0c86ebce59d2..bfb16fec0aed 100644
@@ -55,7 +55,7 @@ static struct nvme_auth_dhgroup_map {
 
  const char *nvme_auth_dhgroup_name(u8 dhgroup_id)
  {
-	if ((dhgroup_id > ARRAY_SIZE(dhgroup_map)) ||
+	if ((dhgroup_id >= ARRAY_SIZE(dhgroup_map)) ||
  	    !dhgroup_map[dhgroup_id].name ||
  	    !strlen(dhgroup_map[dhgroup_id].name))
  		return NULL;
@@ -65,7 +65,7 @@ EXPORT_SYMBOL_GPL(nvme_auth_dhgroup_name);
 
  const char *nvme_auth_dhgroup_kpp(u8 dhgroup_id)
  {
-	if ((dhgroup_id > ARRAY_SIZE(dhgroup_map)) ||
+	if ((dhgroup_id >= ARRAY_SIZE(dhgroup_map)) ||
  	    !dhgroup_map[dhgroup_id].kpp ||
  	    !strlen(dhgroup_map[dhgroup_id].kpp))
  		return NULL;
@@ -113,7 +113,7 @@ static struct nvme_dhchap_hash_map {
 
  const char *nvme_auth_hmac_name(u8 hmac_id)
  {
-	if ((hmac_id > ARRAY_SIZE(hash_map)) ||
+	if ((hmac_id >= ARRAY_SIZE(hash_map)) ||
  	    !hash_map[hmac_id].hmac ||
  	    !strlen(hash_map[hmac_id].hmac))
  		return NULL;
@@ -123,7 +123,7 @@ EXPORT_SYMBOL_GPL(nvme_auth_hmac_name);
 
  const char *nvme_auth_digest_name(u8 hmac_id)
  {
-	if ((hmac_id > ARRAY_SIZE(hash_map)) ||
+	if ((hmac_id >= ARRAY_SIZE(hash_map)) ||
  	    !hash_map[hmac_id].digest ||
  	    !strlen(hash_map[hmac_id].digest))
  		return NULL;

drivers/nvme/common/auth.c EXPORT_SYMBOL_GPL(nvme_auth_hmac_id); · line 154

Quoted 8 lines
drivers/nvme/common/auth.c +1 -1
@@ -148,7 +148,7 @@ EXPORT_SYMBOL_GPL(nvme_auth_hmac_id);
 
  size_t nvme_auth_hmac_hash_len(u8 hmac_id)
  {
-	if ((hmac_id > ARRAY_SIZE(hash_map)) ||
+	if ((hmac_id >= ARRAY_SIZE(hash_map)) ||
  	    !hash_map[hmac_id].hmac ||
  	    !strlen(hash_map[hmac_id].hmac))
  		return 0;

Cheers,

Hannes

Signature · 5 lines
Dr. Hannes Reinecke                Kernel Storage Architect
[email protected]                              +49 911 74053 688
SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), Geschäftsführer: Ivo Totev, Andrew
Myers, Andrew McDonald, Martje Boudien Moerman
Reviewed-by on v1 1/1
#3 lore raw

Re: [PATCH] nvme-auth: Uninitialized variable in nvme_auth_transform_key()

In reply to Dan Carpenter
Quoted 50 lines · Dan Carpenter

A couple of the early error gotos call kfree_sensitive(transformed_key); before "transformed_key" has been initialized.

Fixes a476416bb57b ("nvme: implement In-Band authentication")

---
I wrote this several times in different ways, and kept on doing it wrong so in the end I wrote it this way. It's a bigger diff, but I think it's the clearest way to write it.

  drivers/nvme/common/auth.c | 25 ++++++++++++++++---------
  1 file changed, 16 insertions(+), 9 deletions(-)
drivers/nvme/common/auth.c +14 -4
index bfb16fec0aed..1890193c7333 100644
@@ -278,26 +278,33 @@ u8 *nvme_auth_transform_key(struct nvme_dhchap_key *key, char *nqn)
  	shash->tfm = key_tfm;
  	ret = crypto_shash_setkey(key_tfm, key->key, key->len);
  	if (ret < 0)
-		goto out_free_shash;
+		goto out_free_transformed_key;
  	ret = crypto_shash_init(shash);
  	if (ret < 0)
-		goto out_free_shash;
+		goto out_free_transformed_key;
  	ret = crypto_shash_update(shash, nqn, strlen(nqn));
  	if (ret < 0)
-		goto out_free_shash;
+		goto out_free_transformed_key;
  	ret = crypto_shash_update(shash, "NVMe-over-Fabrics", 17);
  	if (ret < 0)
-		goto out_free_shash;
+		goto out_free_transformed_key;
  	ret = crypto_shash_final(shash, transformed_key);
+	if (ret < 0)
+		goto out_free_transformed_key;
+
+	kfree(shash);
+	crypto_free_shash(key_tfm);
+
+	return transformed_key;
+
+out_free_transformed_key:
+	kfree_sensitive(transformed_key);
  out_free_shash:
  	kfree(shash);
  out_free_key:
  	crypto_free_shash(key_tfm);

drivers/nvme/common/auth.c u8 *nvme_auth_transform_key(struct nvme_dhchap_key *key, char *nqn) · line 310

Quoted 10 lines
drivers/nvme/common/auth.c +2 -5
-	if (ret < 0) {
-		kfree_sensitive(transformed_key);
-		return ERR_PTR(ret);
-	}
-	return transformed_key;
+
+	return ERR_PTR(ret);
  }
  EXPORT_SYMBOL_GPL(nvme_auth_transform_key);
 

Cheers,

Hannes

Signature · 5 lines
Dr. Hannes Reinecke                Kernel Storage Architect
[email protected]                              +49 911 74053 688
SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), Geschäftsführer: Ivo Totev, Andrew
Myers, Andrew McDonald, Martje Boudien Moerman