Skip to contentSkip to conversation

crypto: qce - aead: unmap the scatterlist that was mapped

PATCH New Thomas Fourier 2 participants 2 messages Nov 27

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

PATCH 1/1 crypto: qce - aead: unmap the scatterlist that was mapped

qce_aead_async_req_handle() maps rctx->src_sg, which for CCM with src != dst is a table built by the driver and not req->src, but its error path unmaps req->src. The mapping result is also checked with "< 0" although dma_map_sg() returns 0 on failure.

Unmap rctx->src_sg like qce_aead_done() does and treat a zero return of dma_map_sg() as an error.

Fixes 5c0ecc2e81ec ("crypto: qce - Fix some error handling path")
---
 drivers/crypto/qce/aead.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/crypto/qce/aead.c +3 -3
index e0061bfab216..54cc6bb8fd4b 100644
@@ -450,8 +450,8 @@ qce_aead_async_req_handle(struct crypto_async_request *async_req)
 
 	if (diff_dst) {
 		src_nents = dma_map_sg(qce->dev, rctx->src_sg, rctx->src_nents, dir_src);
-		if (src_nents < 0) {
-			ret = src_nents;
+		if (!src_nents) {
+			ret = -EIO;
 			goto error_unmap_dst;
 		}
 	} else {
@@ -478,7 +478,7 @@ qce_aead_async_req_handle(struct crypto_async_request *async_req)
 	qce_dma_terminate_all(&qce->dma);
 error_unmap_src:
 	if (diff_dst)
-		dma_unmap_sg(qce->dev, req->src, rctx->src_nents, dir_src);
+		dma_unmap_sg(qce->dev, rctx->src_sg, rctx->src_nents, dir_src);
 error_unmap_dst:
 	dma_unmap_sg(qce->dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
 error_free:
Signature · 1 line
2.53.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] crypto: qce - Fixes trailer cites 5c0ecc2e81ec instead of 9363efb4181c It also noticed 1 related…

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] crypto: qce - Fixes trailer cites 5c0ecc2e81ec instead of 9363efb4181c
It also noticed 1 related pre-existing issue this patch does not introduce:
- [High] crypto: qce: missing error check in qce_aead_ccm_prepare_buf_assoclen() [1]
--

commit 377c9768f06fe7baadd13bf43eb833dcae74b8e1
Author: Thomas Fourier <[email protected]>

Quoted 7 lines

crypto: qce - aead: unmap the scatterlist that was mapped

qce_aead_async_req_handle() maps rctx->src_sg, which for CCM with src != dst is a table built by the driver and not req->src, but its error path unmaps req->src. The mapping result is also checked with "< 0" although dma_map_sg() returns 0 on failure.

Quoted 5 lines

Unmap rctx->src_sg like qce_aead_done() does and treat a zero return of dma_map_sg() as an error.

Fixes 5c0ecc2e81ec ("crypto: qce - Fix some error handling path")

[Severity: Low]
Both the src_nents < 0 check and the dma_unmap_sg(..., req->src, ...) call were introduced by 9363efb4181c ("crypto: qce - Add support for AEAD algorithms"), whereas 5c0ecc2e81ec ("crypto: qce - Fix some error handling path") only added ret = src_nents inside the existing error branch. Should the Fixes tag point to 9363efb4181c ("crypto: qce - Add support for AEAD algorithms") instead?

Quoted 16 lines
drivers/crypto/qce/aead.c +2 -2
index 8ce1a9f3c9d80..a40dd338d7b5d 100644
@@ -450,8 +450,8 @@ qce_aead_async_req_handle(struct crypto_async_request *async_req)
 
 	if (diff_dst) {
 		src_nents = dma_map_sg(qce->dev, rctx->src_sg, rctx->src_nents, dir_src);
-		if (src_nents < 0) {
-			ret = src_nents;
+		if (!src_nents) {
+			ret = -EIO;
 			goto error_unmap_dst;
 		}
 	} else {

drivers/crypto/qce/aead.c qce_aead_async_req_handle(struct crypto_async_request *async_req) · line 484

Quoted 8 lines
drivers/crypto/qce/aead.c +1 -1
@@ -478,7 +478,7 @@ qce_aead_async_req_handle(struct crypto_async_request *async_req)
 	qce_dma_terminate_all(&qce->dma);
 error_unmap_src:
 	if (diff_dst)
-		dma_unmap_sg(qce->dev, req->src, rctx->src_nents, dir_src);
+		dma_unmap_sg(qce->dev, rctx->src_sg, rctx->src_nents, dir_src);
 error_unmap_dst:
 	dma_unmap_sg(qce->dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
 error_free:

[Severity: High]
This problem wasn't introduced by this patch, but can qce_aead_ccm_prepare_buf_assoclen() pass a negative rctx->src_nents to sg_alloc_table() [1] when preparing rctx->src_sg?

drivers/crypto/qce/aead.c:qce_aead_ccm_prepare_buf_assoclen() {
    ...
	if (diff_dst)
		rctx->src_nents = sg_nents_for_len(req->src, totallen) + 1;
	else
		rctx->src_nents = sg_nents_for_len(req->src, totallen) + 2;

	gfp = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ? GFP_KERNEL : GFP_ATOMIC;
	ret = sg_alloc_table(&rctx->src_tbl, rctx->src_nents, gfp);
    ...
}

If a caller (such as an unprivileged user via AF_ALG) provides a source scatterlist shorter than totallen, sg_nents_for_len() returns -EINVAL. Adding 1 or 2 leaves rctx->src_nents negative, which is converted to a huge unsigned int nents in sg_alloc_table(), exhausting memory and triggering the OOM killer.

[1] https://sashiko.dev/#/bug/linux-49abb05c-1d69-4d3c-9e75-f9284781402e

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1