Skip to content

Commit

Permalink
crypto: xts,lrw - fix out-of-bounds write after kmalloc failure
Browse files Browse the repository at this point in the history
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: f1c131b ("crypto: xts - Convert to skcipher")
Fixes: 700cb3f ("crypto: lrw - Convert to skcipher")
Cc: [email protected] # v4.10+
Reported-by: Dmitry Vyukov <[email protected]>
Signed-off-by: Eric Biggers <[email protected]>
Acked-by: David S. Miller <[email protected]>
Signed-off-by: Herbert Xu <[email protected]>
  • Loading branch information
ebiggers authored and herbertx committed Mar 24, 2017
1 parent efc989f commit 9df0eb1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
7 changes: 5 additions & 2 deletions crypto/lrw.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 5 additions & 2 deletions crypto/xts.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 9df0eb1

Please sign in to comment.