Skip to content

Commit 763069b

Browse files
horiagherbertx
authored andcommitted
crypto: caam - handle zero-length AEAD output
Recent AEAD changes in testmgr framework introduced by commit a0d608e ("crypto: testmgr - unify the AEAD encryption and decryption test vectors") uncovered an error in the CAAM drivers, since they don't correctly handle the case when AEAD output length is zero. Add checks to avoid feeding zero-length req->dst to DMA API. Signed-off-by: Horia Geantă <horia.geanta@nxp.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 87870cf commit 763069b

3 files changed

Lines changed: 47 additions & 24 deletions

File tree

‎drivers/crypto/caam/caamalg.c‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,8 @@ static void caam_unmap(struct device *dev, struct scatterlist *src,
846846
if (dst != src) {
847847
if (src_nents)
848848
dma_unmap_sg(dev, src, src_nents, DMA_TO_DEVICE);
849-
dma_unmap_sg(dev, dst, dst_nents, DMA_FROM_DEVICE);
849+
if (dst_nents)
850+
dma_unmap_sg(dev, dst, dst_nents, DMA_FROM_DEVICE);
850851
} else {
851852
dma_unmap_sg(dev, src, src_nents, DMA_BIDIRECTIONAL);
852853
}
@@ -1038,7 +1039,9 @@ static void init_aead_job(struct aead_request *req,
10381039
out_options = in_options;
10391040

10401041
if (unlikely(req->src != req->dst)) {
1041-
if (edesc->dst_nents == 1) {
1042+
if (!edesc->dst_nents) {
1043+
dst_dma = 0;
1044+
} else if (edesc->dst_nents == 1) {
10421045
dst_dma = sg_dma_address(req->dst);
10431046
} else {
10441047
dst_dma = edesc->sec4_sg_dma +
@@ -1289,12 +1292,19 @@ static struct aead_edesc *aead_edesc_alloc(struct aead_request *req,
12891292
mapped_src_nents = 0;
12901293
}
12911294

1292-
mapped_dst_nents = dma_map_sg(jrdev, req->dst, dst_nents,
1293-
DMA_FROM_DEVICE);
1294-
if (unlikely(!mapped_dst_nents)) {
1295-
dev_err(jrdev, "unable to map destination\n");
1296-
dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1297-
return ERR_PTR(-ENOMEM);
1295+
/* Cover also the case of null (zero length) output data */
1296+
if (dst_nents) {
1297+
mapped_dst_nents = dma_map_sg(jrdev, req->dst,
1298+
dst_nents,
1299+
DMA_FROM_DEVICE);
1300+
if (unlikely(!mapped_dst_nents)) {
1301+
dev_err(jrdev, "unable to map destination\n");
1302+
dma_unmap_sg(jrdev, req->src, src_nents,
1303+
DMA_TO_DEVICE);
1304+
return ERR_PTR(-ENOMEM);
1305+
}
1306+
} else {
1307+
mapped_dst_nents = 0;
12981308
}
12991309
}
13001310

‎drivers/crypto/caam/caamalg_qi.c‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,8 @@ static void caam_unmap(struct device *dev, struct scatterlist *src,
802802
if (dst != src) {
803803
if (src_nents)
804804
dma_unmap_sg(dev, src, src_nents, DMA_TO_DEVICE);
805-
dma_unmap_sg(dev, dst, dst_nents, DMA_FROM_DEVICE);
805+
if (dst_nents)
806+
dma_unmap_sg(dev, dst, dst_nents, DMA_FROM_DEVICE);
806807
} else {
807808
dma_unmap_sg(dev, src, src_nents, DMA_BIDIRECTIONAL);
808809
}
@@ -955,13 +956,19 @@ static struct aead_edesc *aead_edesc_alloc(struct aead_request *req,
955956
mapped_src_nents = 0;
956957
}
957958

958-
mapped_dst_nents = dma_map_sg(qidev, req->dst, dst_nents,
959-
DMA_FROM_DEVICE);
960-
if (unlikely(!mapped_dst_nents)) {
961-
dev_err(qidev, "unable to map destination\n");
962-
dma_unmap_sg(qidev, req->src, src_nents, DMA_TO_DEVICE);
963-
qi_cache_free(edesc);
964-
return ERR_PTR(-ENOMEM);
959+
if (dst_nents) {
960+
mapped_dst_nents = dma_map_sg(qidev, req->dst,
961+
dst_nents,
962+
DMA_FROM_DEVICE);
963+
if (unlikely(!mapped_dst_nents)) {
964+
dev_err(qidev, "unable to map destination\n");
965+
dma_unmap_sg(qidev, req->src, src_nents,
966+
DMA_TO_DEVICE);
967+
qi_cache_free(edesc);
968+
return ERR_PTR(-ENOMEM);
969+
}
970+
} else {
971+
mapped_dst_nents = 0;
965972
}
966973
}
967974

‎drivers/crypto/caam/caamalg_qi2.c‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ static void caam_unmap(struct device *dev, struct scatterlist *src,
144144
if (dst != src) {
145145
if (src_nents)
146146
dma_unmap_sg(dev, src, src_nents, DMA_TO_DEVICE);
147-
dma_unmap_sg(dev, dst, dst_nents, DMA_FROM_DEVICE);
147+
if (dst_nents)
148+
dma_unmap_sg(dev, dst, dst_nents, DMA_FROM_DEVICE);
148149
} else {
149150
dma_unmap_sg(dev, src, src_nents, DMA_BIDIRECTIONAL);
150151
}
@@ -385,13 +386,18 @@ static struct aead_edesc *aead_edesc_alloc(struct aead_request *req,
385386
mapped_src_nents = 0;
386387
}
387388

388-
mapped_dst_nents = dma_map_sg(dev, req->dst, dst_nents,
389-
DMA_FROM_DEVICE);
390-
if (unlikely(!mapped_dst_nents)) {
391-
dev_err(dev, "unable to map destination\n");
392-
dma_unmap_sg(dev, req->src, src_nents, DMA_TO_DEVICE);
393-
qi_cache_free(edesc);
394-
return ERR_PTR(-ENOMEM);
389+
if (dst_nents) {
390+
mapped_dst_nents = dma_map_sg(dev, req->dst, dst_nents,
391+
DMA_FROM_DEVICE);
392+
if (unlikely(!mapped_dst_nents)) {
393+
dev_err(dev, "unable to map destination\n");
394+
dma_unmap_sg(dev, req->src, src_nents,
395+
DMA_TO_DEVICE);
396+
qi_cache_free(edesc);
397+
return ERR_PTR(-ENOMEM);
398+
}
399+
} else {
400+
mapped_dst_nents = 0;
395401
}
396402
} else {
397403
src_nents = sg_nents_for_len(req->src, req->assoclen +

0 commit comments

Comments
 (0)