Skip to contentSkip to conversation

crypto: qat - Don't use -EFAULT as error code during setkey()

PATCH In review Thomas Huth 3 participants 4 messages Aug 24 – Nov 20 1 message not in the archive

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

PATCH 1/1 crypto: qat - Don't use -EFAULT as error code during setkey()

EFAULT means "Bad Address", it's the error code that is meant to be returned when userspace provided the kerknel with a pointer to memory that is not part of its address space. However, the QAT driver abuses this error code during its setkey() function to signal that there was something wrong with the selected algorithm, which is just confusing.

The best choice for an error code would likely have been EINVAL here, but qat_alg_aead_init_sessions() seems to make an effort to use this for another error case already, so there is likely the desire to distinguish the different errors here. Thus use EOPNOTSUPP as a replacement now, so qat_alg_aead_init_sessions() continues to return two different error codes depending on what went wrong.

Anyway, instead of hard-coding the error codes in the function qat_alg_aead_init_sessions() again, let's also simply rather pass the error code from the called functions up to the caller instead, this way we can also get rid of the need to call memzero_explicit() here three times.

---
 .../crypto/intel/qat/qat_common/qat_algs.c    | 31 +++++++++----------
 1 file changed, 14 insertions(+), 17 deletions(-)
drivers/crypto/intel/qat/qat_common/qat_algs.c +14 -17
index 91663805d9e60..91da9cad1d89a 100644
@@ -135,7 +135,7 @@ static int qat_alg_do_precomputes(struct icp_qat_hw_auth_algo_blk *hash,
 		return 0;
 	}
 	default:
-		return -EFAULT;
+		return -EOPNOTSUPP;
 	}
 }
 
@@ -186,7 +186,7 @@ static int qat_alg_aead_init_enc_session(struct crypto_aead *aead_tfm,
 		cpu_to_be32(ctx->hash_blocksize);
 
 	if (qat_alg_do_precomputes(hash, ctx, keys->authkey, keys->authkeylen))
-		return -EFAULT;
+		return -EOPNOTSUPP;
 
 	/* Request setup */
 	qat_alg_init_common_hdr(header);
@@ -273,7 +273,7 @@ static int qat_alg_aead_init_dec_session(struct crypto_aead *aead_tfm,
 		cpu_to_be32(ctx->hash_blocksize);
 
 	if (qat_alg_do_precomputes(hash, ctx, keys->authkey, keys->authkeylen))
-		return -EFAULT;
+		return -EOPNOTSUPP;
 
 	/* Request setup */
 	qat_alg_init_common_hdr(header);
@@ -472,28 +472,25 @@ static int qat_alg_aead_init_sessions(struct crypto_aead *tfm, const u8 *key,
 				      unsigned int keylen,  int mode)
 {
 	struct crypto_authenc_keys keys;
-	int alg;
-
-	if (crypto_authenc_extractkeys(&keys, key, keylen))
-		goto bad_key;
+	int alg, ret;
 
-	if (qat_alg_validate_key(keys.enckeylen, &alg, mode))
-		goto bad_key;
+	ret = crypto_authenc_extractkeys(&keys, key, keylen);
+	if (ret)
+		goto error;
 
-	if (qat_alg_aead_init_enc_session(tfm, alg, &keys, mode))
+	ret = qat_alg_validate_key(keys.enckeylen, &alg, mode);
+	if (ret)
 		goto error;
 
-	if (qat_alg_aead_init_dec_session(tfm, alg, &keys, mode))
+	ret = qat_alg_aead_init_enc_session(tfm, alg, &keys, mode);
+	if (ret)
 		goto error;
 
-	memzero_explicit(&keys, sizeof(keys));
-	return 0;
-bad_key:
-	memzero_explicit(&keys, sizeof(keys));
-	return -EINVAL;
+	ret = qat_alg_aead_init_dec_session(tfm, alg, &keys, mode);
+
 error:
 	memzero_explicit(&keys, sizeof(keys));
-	return -EFAULT;
+	return ret;
 }
 
 static int qat_alg_skcipher_init_sessions(struct qat_alg_skcipher_ctx *ctx,
Signature · 1 line
2.55.0
#1 lore raw

Am 24.08.26 um 14:24 schrieb Thomas Huth:

Quoted 86 lines

EFAULT means "Bad Address", it's the error code that is meant to be returned when userspace provided the kerknel with a pointer to memory that is not part of its address space. However, the QAT driver abuses this error code during its setkey() function to signal that there was something wrong with the selected algorithm, which is just confusing.

The best choice for an error code would likely have been EINVAL here, but qat_alg_aead_init_sessions() seems to make an effort to use this for another error case already, so there is likely the desire to distinguish the different errors here. Thus use EOPNOTSUPP as a replacement now, so qat_alg_aead_init_sessions() continues to return two different error codes depending on what went wrong.

Anyway, instead of hard-coding the error codes in the function qat_alg_aead_init_sessions() again, let's also simply rather pass the error code from the called functions up to the caller instead, this way we can also get rid of the need to call memzero_explicit() here three times.

---
  .../crypto/intel/qat/qat_common/qat_algs.c    | 31 +++++++++----------
  1 file changed, 14 insertions(+), 17 deletions(-)
drivers/crypto/intel/qat/qat_common/qat_algs.c +11 -15
index 91663805d9e60..91da9cad1d89a 100644
@@ -135,7 +135,7 @@ static int qat_alg_do_precomputes(struct icp_qat_hw_auth_algo_blk *hash,
  		return 0;
  	}
  	default:
-		return -EFAULT;
+		return -EOPNOTSUPP;
  	}
  }
 
@@ -186,7 +186,7 @@ static int qat_alg_aead_init_enc_session(struct crypto_aead *aead_tfm,
  		cpu_to_be32(ctx->hash_blocksize);
 
  	if (qat_alg_do_precomputes(hash, ctx, keys->authkey, keys->authkeylen))
-		return -EFAULT;
+		return -EOPNOTSUPP;
 
  	/* Request setup */
  	qat_alg_init_common_hdr(header);
@@ -273,7 +273,7 @@ static int qat_alg_aead_init_dec_session(struct crypto_aead *aead_tfm,
  		cpu_to_be32(ctx->hash_blocksize);
 
  	if (qat_alg_do_precomputes(hash, ctx, keys->authkey, keys->authkeylen))
-		return -EFAULT;
+		return -EOPNOTSUPP;
 
  	/* Request setup */
  	qat_alg_init_common_hdr(header);
@@ -472,28 +472,25 @@ static int qat_alg_aead_init_sessions(struct crypto_aead *tfm, const u8 *key,
  				      unsigned int keylen,  int mode)
  {
  	struct crypto_authenc_keys keys;
-	int alg;
-
-	if (crypto_authenc_extractkeys(&keys, key, keylen))
-		goto bad_key;
+	int alg, ret;
 
-	if (qat_alg_validate_key(keys.enckeylen, &alg, mode))
-		goto bad_key;
+	ret = crypto_authenc_extractkeys(&keys, key, keylen);
+	if (ret)
+		goto error;
 
-	if (qat_alg_aead_init_enc_session(tfm, alg, &keys, mode))
+	ret = qat_alg_validate_key(keys.enckeylen, &alg, mode);
+	if (ret)
  		goto error;
 
-	if (qat_alg_aead_init_dec_session(tfm, alg, &keys, mode))
+	ret = qat_alg_aead_init_enc_session(tfm, alg, &keys, mode);
+	if (ret)
  		goto error;
 
-	memzero_explicit(&keys, sizeof(keys));
-	return 0;
-bad_key:
-	memzero_explicit(&keys, sizeof(keys));

drivers/crypto/intel/qat/qat_common/qat_algs.c static int qat_alg_aead_init_sessions(struct crypto_aead *tfm, const u8 *key, · line 496

Quoted 10 lines
drivers/crypto/intel/qat/qat_common/qat_algs.c +3 -2
-	return -EINVAL;
+	ret = qat_alg_aead_init_dec_session(tfm, alg, &keys, mode);
+
  error:
  	memzero_explicit(&keys, sizeof(keys));
-	return -EFAULT;
+	return ret;
  }
 
  static int qat_alg_skcipher_init_sessions(struct qat_alg_skcipher_ctx *ctx,

Friendly ping! Anybody got a comment on this patch?

Thomas

A message that is not in the archive (sent off-list or not yet received): a0153735095f47aabfb471f12cff9cb7@BL1PR11MB5431.namprd11.prod.outlook.com

maintainer Acked-by
#3 lore raw
Quoted 3 lines · Thomas Huth

Am 24.08.26 um 14:24 schrieb Thomas Huth:

Quoted 2 lines

EFAULT means "Bad Address", it's the error code that is meant to be returned when userspace provided the kerknel with a pointer to

/s/kerknel/kernel

Quoted 36 lines
Quoted 36 lines

memory that is not part of its address space. However, the QAT driver abuses this error code during its setkey() function to signal that there was something wrong with the selected algorithm, which is just confusing.

The best choice for an error code would likely have been EINVAL here, but qat_alg_aead_init_sessions() seems to make an effort to use this for another error case already, so there is likely the desire to distinguish the different errors here. Thus use EOPNOTSUPP as a replacement now, so qat_alg_aead_init_sessions() continues to return two different error codes depending on what went wrong.

Anyway, instead of hard-coding the error codes in the function qat_alg_aead_init_sessions() again, let's also simply rather pass the error code from the called functions up to the caller instead, this way we can also get rid of the need to call memzero_explicit() here three times.

---
  .../crypto/intel/qat/qat_common/qat_algs.c    | 31 +++++++++----------
  1 file changed, 14 insertions(+), 17 deletions(-)
drivers/crypto/intel/qat/qat_common/qat_algs.c +1 -1
index 91663805d9e60..91da9cad1d89a 100644
@@ -135,7 +135,7 @@ static int qat_alg_do_precomputes(struct icp_qat_hw_auth_algo_blk *hash,
  		return 0;
  	}
  	default:
-		return -EFAULT;
+		return -EOPNOTSUPP;
  	}
  }
 
Quoted 6 lines

drivers/crypto/intel/qat/qat_common/qat_algs.c static int qat_alg_aead_init_enc_session(struct crypto_aead *aead_tfm, · line 189

Quoted 6 lines
drivers/crypto/intel/qat/qat_common/qat_algs.c +1 -1
@@ -186,7 +186,7 @@ static int qat_alg_aead_init_enc_session(struct crypto_aead *aead_tfm,
  		cpu_to_be32(ctx->hash_blocksize);
 
  	if (qat_alg_do_precomputes(hash, ctx, keys->authkey, keys->authkeylen))
-		return -EFAULT;
+		return -EOPNOTSUPP;

We could also change this into:

	ret = qat_alg_do_precomputes(hash, ctx, keys->authkey, keys->authkeylen));
	if (ret)
		return ret;
Quoted 50 lines
Quoted 50 lines
drivers/crypto/intel/qat/qat_common/qat_algs.c +12 -15
  	/* Request setup */
  	qat_alg_init_common_hdr(header);
@@ -273,7 +273,7 @@ static int qat_alg_aead_init_dec_session(struct crypto_aead *aead_tfm,
  		cpu_to_be32(ctx->hash_blocksize);
 
  	if (qat_alg_do_precomputes(hash, ctx, keys->authkey, keys->authkeylen))
-		return -EFAULT;
+		return -EOPNOTSUPP;
 
  	/* Request setup */
  	qat_alg_init_common_hdr(header);
@@ -472,28 +472,25 @@ static int qat_alg_aead_init_sessions(struct crypto_aead *tfm, const u8 *key,
  				      unsigned int keylen,  int mode)
  {
  	struct crypto_authenc_keys keys;
-	int alg;
-
-	if (crypto_authenc_extractkeys(&keys, key, keylen))
-		goto bad_key;
+	int alg, ret;
 
-	if (qat_alg_validate_key(keys.enckeylen, &alg, mode))
-		goto bad_key;
+	ret = crypto_authenc_extractkeys(&keys, key, keylen);
+	if (ret)
+		goto error;
 
-	if (qat_alg_aead_init_enc_session(tfm, alg, &keys, mode))
+	ret = qat_alg_validate_key(keys.enckeylen, &alg, mode);
+	if (ret)
  		goto error;
 
-	if (qat_alg_aead_init_dec_session(tfm, alg, &keys, mode))
+	ret = qat_alg_aead_init_enc_session(tfm, alg, &keys, mode);
+	if (ret)
  		goto error;
 
-	memzero_explicit(&keys, sizeof(keys));
-	return 0;
-bad_key:
-	memzero_explicit(&keys, sizeof(keys));
-	return -EINVAL;
+	ret = qat_alg_aead_init_dec_session(tfm, alg, &keys, mode);
+
  error:
  	memzero_explicit(&keys, sizeof(keys));
-	return -EFAULT;
+	return ret;
  }
 
Quoted 3 lines

drivers/crypto/intel/qat/qat_common/qat_algs.c static int qat_alg_aead_init_sessions(struct crypto_aead *tfm, const u8 *key, · line 496

Quoted 1 line
drivers/crypto/intel/qat/qat_common/qat_algs.c +0 -0
  static int qat_alg_skcipher_init_sessions(struct qat_alg_skcipher_ctx *ctx,

Friendly ping! Anybody got a comment on this patch?

Apologies, I thought this was already merged!

You might have to re-send it as I see that it is marked as Deferred in patchwork.

Also, you can add my tag:

Regards,

Signature · 1 line
Giovanni
maintainer
#4 lore raw
In reply to Thomas Huth
Quoted 20 lines · Thomas Huth

EFAULT means "Bad Address", it's the error code that is meant to be returned when userspace provided the kerknel with a pointer to memory that is not part of its address space. However, the QAT driver abuses this error code during its setkey() function to signal that there was something wrong with the selected algorithm, which is just confusing.

The best choice for an error code would likely have been EINVAL here, but qat_alg_aead_init_sessions() seems to make an effort to use this for another error case already, so there is likely the desire to distinguish the different errors here. Thus use EOPNOTSUPP as a replacement now, so qat_alg_aead_init_sessions() continues to return two different error codes depending on what went wrong.

Anyway, instead of hard-coding the error codes in the function qat_alg_aead_init_sessions() again, let's also simply rather pass the error code from the called functions up to the caller instead, this way we can also get rid of the need to call memzero_explicit() here three times.

Quoted 4 lines
---
 .../crypto/intel/qat/qat_common/qat_algs.c    | 31 +++++++++----------
 1 file changed, 14 insertions(+), 17 deletions(-)

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