Skip to content

Commit 5003198

Browse files
gcabiddugregkh
authored andcommitted
crypto: qat - validate RSA CRT component lengths
commit b3ac787 upstream. The generic RSA key parser (rsa_helper.c) bounds each CRT component (p, q, dp, dq, qinv) by the modulus size n_sz, but qat_rsa_setkey_crt() allocates half-size DMA buffers (key_sz / 2) and right-aligns each component with: memcpy(dst + half_key_sz - len, src, len) When a CRT component is larger than half_key_sz the subtraction underflows and memcpy writes past the DMA buffer, causing memory corruption. Add a len > half_key_sz check next to the existing !len check for each of the five CRT components so the driver falls back to the non-CRT path instead of writing out of bounds. Fixes: 879f77e ("crypto: qat - Add RSA CRT mode") Cc: stable@vger.kernel.org Signed-off-by: Giovanni Cabiddu <giovanni.cabiddu@intel.com> Reviewed-by: Ahsan Atta <ahsan.atta@intel.com> Reviewed-by: Laurent M Coquerel <laurent.m.coquerel@intel.com> Tested-by: Laurent M Coquerel <laurent.m.coquerel@intel.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent e3155eb commit 5003198

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

drivers/crypto/intel/qat/qat_common/qat_asym_algs.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,7 +1032,7 @@ static void qat_rsa_setkey_crt(struct qat_rsa_ctx *ctx, struct rsa_key *rsa_key)
10321032
ptr = rsa_key->p;
10331033
len = rsa_key->p_sz;
10341034
qat_rsa_drop_leading_zeros(&ptr, &len);
1035-
if (!len)
1035+
if (!len || len > half_key_sz)
10361036
goto err;
10371037
ctx->p = dma_alloc_coherent(dev, half_key_sz, &ctx->dma_p, GFP_KERNEL);
10381038
if (!ctx->p)
@@ -1043,7 +1043,7 @@ static void qat_rsa_setkey_crt(struct qat_rsa_ctx *ctx, struct rsa_key *rsa_key)
10431043
ptr = rsa_key->q;
10441044
len = rsa_key->q_sz;
10451045
qat_rsa_drop_leading_zeros(&ptr, &len);
1046-
if (!len)
1046+
if (!len || len > half_key_sz)
10471047
goto free_p;
10481048
ctx->q = dma_alloc_coherent(dev, half_key_sz, &ctx->dma_q, GFP_KERNEL);
10491049
if (!ctx->q)
@@ -1054,7 +1054,7 @@ static void qat_rsa_setkey_crt(struct qat_rsa_ctx *ctx, struct rsa_key *rsa_key)
10541054
ptr = rsa_key->dp;
10551055
len = rsa_key->dp_sz;
10561056
qat_rsa_drop_leading_zeros(&ptr, &len);
1057-
if (!len)
1057+
if (!len || len > half_key_sz)
10581058
goto free_q;
10591059
ctx->dp = dma_alloc_coherent(dev, half_key_sz, &ctx->dma_dp,
10601060
GFP_KERNEL);
@@ -1066,7 +1066,7 @@ static void qat_rsa_setkey_crt(struct qat_rsa_ctx *ctx, struct rsa_key *rsa_key)
10661066
ptr = rsa_key->dq;
10671067
len = rsa_key->dq_sz;
10681068
qat_rsa_drop_leading_zeros(&ptr, &len);
1069-
if (!len)
1069+
if (!len || len > half_key_sz)
10701070
goto free_dp;
10711071
ctx->dq = dma_alloc_coherent(dev, half_key_sz, &ctx->dma_dq,
10721072
GFP_KERNEL);
@@ -1078,7 +1078,7 @@ static void qat_rsa_setkey_crt(struct qat_rsa_ctx *ctx, struct rsa_key *rsa_key)
10781078
ptr = rsa_key->qinv;
10791079
len = rsa_key->qinv_sz;
10801080
qat_rsa_drop_leading_zeros(&ptr, &len);
1081-
if (!len)
1081+
if (!len || len > half_key_sz)
10821082
goto free_dq;
10831083
ctx->qinv = dma_alloc_coherent(dev, half_key_sz, &ctx->dma_qinv,
10841084
GFP_KERNEL);

0 commit comments

Comments
 (0)