Re: [PATCH] crypto: xts,lrw - fix out-of-bounds write after kmalloc failure

2017-03-24 Thread Herbert Xu
On Thu, Mar 23, 2017 at 01:39:46PM -0700, Eric Biggers wrote:
> From: Eric Biggers 
> 
> In the generic XTS and LRW algorithms, for input data > 128 bytes, a
> temporary buffer is allocated to hold the values to be XOR'ed with the
> data before and after encryption or decryption.  If the allocation
> fails, the fixed-size buffer embedded in the request buffer is meant to
> be used as a fallback --- resulting in more calls to the ECB algorithm,
> but still producing the correct result.  However, we weren't correctly
> limiting subreq->cryptlen in this case, resulting in pre_crypt()
> overrunning the embedded buffer.  Fix this by setting subreq->cryptlen
> correctly.
> 
> Fixes: f1c131b45410 ("crypto: xts - Convert to skcipher")
> Fixes: 700cb3f5fe75 ("crypto: lrw - Convert to skcipher")
> Cc: sta...@vger.kernel.org # v4.10+
> Reported-by: Dmitry Vyukov 
> Signed-off-by: Eric Biggers 

Patch applied.  Thanks.
-- 
Email: Herbert Xu 
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt


Re: [PATCH] crypto: xts,lrw - fix out-of-bounds write after kmalloc failure

2017-03-23 Thread David Miller
From: Eric Biggers 
Date: Thu, 23 Mar 2017 13:39:46 -0700

> From: Eric Biggers 
> 
> In the generic XTS and LRW algorithms, for input data > 128 bytes, a
> temporary buffer is allocated to hold the values to be XOR'ed with the
> data before and after encryption or decryption.  If the allocation
> fails, the fixed-size buffer embedded in the request buffer is meant to
> be used as a fallback --- resulting in more calls to the ECB algorithm,
> but still producing the correct result.  However, we weren't correctly
> limiting subreq->cryptlen in this case, resulting in pre_crypt()
> overrunning the embedded buffer.  Fix this by setting subreq->cryptlen
> correctly.
> 
> Fixes: f1c131b45410 ("crypto: xts - Convert to skcipher")
> Fixes: 700cb3f5fe75 ("crypto: lrw - Convert to skcipher")
> Cc: sta...@vger.kernel.org # v4.10+
> Reported-by: Dmitry Vyukov 
> Signed-off-by: Eric Biggers 

Acked-by: David S. Miller 


[PATCH] crypto: xts,lrw - fix out-of-bounds write after kmalloc failure

2017-03-23 Thread Eric Biggers
From: Eric Biggers 

In the generic XTS and LRW algorithms, for input data > 128 bytes, a
temporary buffer is allocated to hold the values to be XOR'ed with the
data before and after encryption or decryption.  If the allocation
fails, the fixed-size buffer embedded in the request buffer is meant to
be used as a fallback --- resulting in more calls to the ECB algorithm,
but still producing the correct result.  However, we weren't correctly
limiting subreq->cryptlen in this case, resulting in pre_crypt()
overrunning the embedded buffer.  Fix this by setting subreq->cryptlen
correctly.

Fixes: f1c131b45410 ("crypto: xts - Convert to skcipher")
Fixes: 700cb3f5fe75 ("crypto: lrw - Convert to skcipher")
Cc: sta...@vger.kernel.org # v4.10+
Reported-by: Dmitry Vyukov 
Signed-off-by: Eric Biggers 
---
 crypto/lrw.c | 7 +--
 crypto/xts.c | 7 +--
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/crypto/lrw.c b/crypto/lrw.c
index ecd8474018e3..3ea095adafd9 100644
--- a/crypto/lrw.c
+++ b/crypto/lrw.c
@@ -286,8 +286,11 @@ static int init_crypt(struct skcipher_request *req, 
crypto_completion_t done)
 
subreq->cryptlen = LRW_BUFFER_SIZE;
if (req->cryptlen > LRW_BUFFER_SIZE) {
-   subreq->cryptlen = min(req->cryptlen, (unsigned)PAGE_SIZE);
-   rctx->ext = kmalloc(subreq->cryptlen, gfp);
+   unsigned int n = min(req->cryptlen, (unsigned int)PAGE_SIZE);
+
+   rctx->ext = kmalloc(n, gfp);
+   if (rctx->ext)
+   subreq->cryptlen = n;
}
 
rctx->src = req->src;
diff --git a/crypto/xts.c b/crypto/xts.c
index baeb34dd8582..c976bfac29da 100644
--- a/crypto/xts.c
+++ b/crypto/xts.c
@@ -230,8 +230,11 @@ static int init_crypt(struct skcipher_request *req, 
crypto_completion_t done)
 
subreq->cryptlen = XTS_BUFFER_SIZE;
if (req->cryptlen > XTS_BUFFER_SIZE) {
-   subreq->cryptlen = min(req->cryptlen, (unsigned)PAGE_SIZE);
-   rctx->ext = kmalloc(subreq->cryptlen, gfp);
+   unsigned int n = min(req->cryptlen, (unsigned int)PAGE_SIZE);
+
+   rctx->ext = kmalloc(n, gfp);
+   if (rctx->ext)
+   subreq->cryptlen = n;
}
 
rctx->src = req->src;
-- 
2.12.1.500.gab5fba24ee-goog