From: Herbert Xu Date: Tue, 27 Dec 2022 14:27:39 +0000 (+0100) Subject: lib/mpi: Fix buffer overrun when SG is too long X-Git-Tag: v6.6.7~3330^2~147 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=7361d1bc307b926cbca214ab67b641123c2d6357;p=platform%2Fkernel%2Flinux-starfive.git lib/mpi: Fix buffer overrun when SG is too long The helper mpi_read_raw_from_sgl sets the number of entries in the SG list according to nbytes. However, if the last entry in the SG list contains more data than nbytes, then it may overrun the buffer because it only allocates enough memory for nbytes. Fixes: 2d4d1eea540b ("lib/mpi: Add mpi sgl helpers") Reported-by: Roberto Sassu Signed-off-by: Herbert Xu Reviewed-by: Eric Biggers Signed-off-by: Herbert Xu --- diff --git a/lib/mpi/mpicoder.c b/lib/mpi/mpicoder.c index 39c4c67..3cb6bd1 100644 --- a/lib/mpi/mpicoder.c +++ b/lib/mpi/mpicoder.c @@ -504,7 +504,8 @@ MPI mpi_read_raw_from_sgl(struct scatterlist *sgl, unsigned int nbytes) while (sg_miter_next(&miter)) { buff = miter.addr; - len = miter.length; + len = min_t(unsigned, miter.length, nbytes); + nbytes -= len; for (x = 0; x < len; x++) { a <<= 8;