Skip to content

Commit e4630f9

Browse files
committed
[CRYPTO] blkcipher: Fix handling of kmalloc page straddling
The function blkcipher_get_spot tries to return a buffer of the specified length that does not straddle a page. It has an off-by-one bug so it may advance a page unnecessarily. What's worse, one of its callers doesn't provide a buffer that's sufficiently long for this operation. This patch fixes both problems. Thanks to Bob Gilligan for diagnosing this problem and providing a fix. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent b21010e commit e4630f9

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

‎crypto/blkcipher.c‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,13 @@ static inline void blkcipher_unmap_dst(struct blkcipher_walk *walk)
5959
scatterwalk_unmap(walk->dst.virt.addr, 1);
6060
}
6161

62+
/* Get a spot of the specified length that does not straddle a page.
63+
* The caller needs to ensure that there is enough space for this operation.
64+
*/
6265
static inline u8 *blkcipher_get_spot(u8 *start, unsigned int len)
6366
{
64-
if (offset_in_page(start + len) < len)
65-
return (u8 *)((unsigned long)(start + len) & PAGE_MASK);
66-
return start;
67+
u8 *end_page = (u8 *)(((unsigned long)(start + len - 1)) & PAGE_MASK);
68+
return start < end_page ? start : end_page;
6769
}
6870

6971
static inline unsigned int blkcipher_done_slow(struct crypto_blkcipher *tfm,
@@ -155,7 +157,8 @@ static inline int blkcipher_next_slow(struct blkcipher_desc *desc,
155157
if (walk->buffer)
156158
goto ok;
157159

158-
n = bsize * 2 + (alignmask & ~(crypto_tfm_ctx_alignment() - 1));
160+
n = bsize * 3 - (alignmask + 1) +
161+
(alignmask & ~(crypto_tfm_ctx_alignment() - 1));
159162
walk->buffer = kmalloc(n, GFP_ATOMIC);
160163
if (!walk->buffer)
161164
return blkcipher_walk_done(desc, walk, -ENOMEM);

0 commit comments

Comments
 (0)