crypto: keembay-ocs: prevent underflow in sg_data_total - remainder
Subsystems (3)
PATCH 1/1 crypto: keembay-ocs: prevent underflow in sg_data_total - remainder
In the kmb_ocs_dma_prepare() function, the remainder is calculated as total % blk_sz, where total = sg_data_total + buf_cnt. In update requests, if buf_cnt is large and total is less than rctx->blk_sz, the remainder may exceed sg_data_total, which will lead to an overflow of sg_data_total - remainder. This results in an incorrect large value being passed to the sg_nents_for_len() function, which can lead to memory corruption.
Add a check to return -EINVAL if sg_data_total < remainder, so that the subtraction is always safe.
Found by Linux Verification Center (linuxtesting.org) with SVACE.
--- drivers/crypto/intel/keembay/keembay-ocs-hcu-core.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/crypto/intel/keembay/keembay-ocs-hcu-core.c +4 -1
@@ -246,8 +246,11 @@ static int kmb_ocs_dma_prepare(struct ahash_request *req)* HCU must be aligned to the block size; compute the remainder data to * be processed in the next request. */ -if (!(rctx->flags &REQ_FINAL )) +if (!(rctx->flags &REQ_FINAL )) { remainder = total % rctx->blk_sz; +if (rctx->sg_data_total < remainder) +return -EINVAL ; + }/* Determine the number of scatter gather list entries to process. */ nents =sg_nents_for_len (req->src, rctx->sg_data_total - remainder);
Signature · 1 line
2.53.0
Herbert Xu
maintainerQuoted 24 lines · Dmitriy Okunev
In the
kmb_ocs_dma_prepare()function, theremainderis calculated astotal % blk_sz, wheretotal = sg_data_total + buf_cnt. In update requests, ifbuf_cntis large andtotalis less thanrctx->blk_sz, theremaindermay exceedsg_data_total, which will lead to an overflow ofsg_data_total - remainder. This results in an incorrect large value being passed to thesg_nents_for_len()function, which can lead to memory corruption.Add a check to return
-EINVALifsg_data_total < remainder, so that the subtraction is always safe.Found by Linux Verification Center (linuxtesting.org) with SVACE.
Fixes 472b04444cd3 ("crypto: keembay - Add Keem Bay OCS HCU driver")Signed-off-by Dmitriy Okunev <[email protected]>--- drivers/crypto/intel/keembay/keembay-ocs-hcu-core.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)drivers/crypto/intel/keembay/keembay-ocs-hcu-core.c +0
-0
drivers/crypto/intel/keembay/keembay-ocs-hcu-core.c static int kmb_ocs_dma_prepare(struct ahash_request *req) · line 253
Quoted 9 lines
drivers/crypto/intel/keembay/keembay-ocs-hcu-core.c +4
-1@@ -246,8 +246,11 @@ static int kmb_ocs_dma_prepare(struct ahash_request *req)* HCU must be aligned to the block size; compute the remainder data to * be processed in the next request. */ -if (!(rctx->flags &REQ_FINAL )) +if (!(rctx->flags &REQ_FINAL )) { remainder = total % rctx->blk_sz; +if (rctx->sg_data_total < remainder) +return -EINVAL ; + }
This driver buffers data less than block size, so how can it get here with sg_data_total less than block size?
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