Skip to content

Commit 0451698

Browse files
committed
Merge branch 'linus' of git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
Pull crypto fixes from Herbert Xu: "This fixes the following issues: - Fix pointer size when caam is used with AArch64 boot loader on AArch32 kernel. - Fix ahash state corruption in marvell driver. - Fix buggy algif_aed tag handling. - Prevent mcryptd from being used with incompatible algorithms which can cause crashes" * 'linus' of git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6: crypto: algif_aead - fix uninitialized variable warning crypto: mcryptd - Check mcryptd algorithm compatibility crypto: algif_aead - fix AEAD tag memory handling crypto: caam - fix pointer size for AArch64 boot loader, AArch32 kernel crypto: marvell - Don't corrupt state of an STD req for re-stepped ahash crypto: marvell - Don't copy hash operation twice into the SRAM
2 parents cd66289 + 678b5c6 commit 0451698

File tree

4 files changed

+57
-37
lines changed

4 files changed

+57
-37
lines changed

crypto/algif_aead.c

+37-22
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@ static inline bool aead_sufficient_data(struct aead_ctx *ctx)
8181
{
8282
unsigned as = crypto_aead_authsize(crypto_aead_reqtfm(&ctx->aead_req));
8383

84-
return ctx->used >= ctx->aead_assoclen + as;
84+
/*
85+
* The minimum amount of memory needed for an AEAD cipher is
86+
* the AAD and in case of decryption the tag.
87+
*/
88+
return ctx->used >= ctx->aead_assoclen + (ctx->enc ? 0 : as);
8589
}
8690

8791
static void aead_reset_ctx(struct aead_ctx *ctx)
@@ -416,7 +420,7 @@ static int aead_recvmsg_async(struct socket *sock, struct msghdr *msg,
416420
unsigned int i, reqlen = GET_REQ_SIZE(tfm);
417421
int err = -ENOMEM;
418422
unsigned long used;
419-
size_t outlen;
423+
size_t outlen = 0;
420424
size_t usedpages = 0;
421425

422426
lock_sock(sk);
@@ -426,12 +430,15 @@ static int aead_recvmsg_async(struct socket *sock, struct msghdr *msg,
426430
goto unlock;
427431
}
428432

429-
used = ctx->used;
430-
outlen = used;
431-
432433
if (!aead_sufficient_data(ctx))
433434
goto unlock;
434435

436+
used = ctx->used;
437+
if (ctx->enc)
438+
outlen = used + as;
439+
else
440+
outlen = used - as;
441+
435442
req = sock_kmalloc(sk, reqlen, GFP_KERNEL);
436443
if (unlikely(!req))
437444
goto unlock;
@@ -445,7 +452,7 @@ static int aead_recvmsg_async(struct socket *sock, struct msghdr *msg,
445452
aead_request_set_ad(req, ctx->aead_assoclen);
446453
aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
447454
aead_async_cb, sk);
448-
used -= ctx->aead_assoclen + (ctx->enc ? as : 0);
455+
used -= ctx->aead_assoclen;
449456

450457
/* take over all tx sgls from ctx */
451458
areq->tsgl = sock_kmalloc(sk, sizeof(*areq->tsgl) * sgl->cur,
@@ -461,7 +468,7 @@ static int aead_recvmsg_async(struct socket *sock, struct msghdr *msg,
461468
areq->tsgls = sgl->cur;
462469

463470
/* create rx sgls */
464-
while (iov_iter_count(&msg->msg_iter)) {
471+
while (outlen > usedpages && iov_iter_count(&msg->msg_iter)) {
465472
size_t seglen = min_t(size_t, iov_iter_count(&msg->msg_iter),
466473
(outlen - usedpages));
467474

@@ -491,16 +498,14 @@ static int aead_recvmsg_async(struct socket *sock, struct msghdr *msg,
491498

492499
last_rsgl = rsgl;
493500

494-
/* we do not need more iovecs as we have sufficient memory */
495-
if (outlen <= usedpages)
496-
break;
497-
498501
iov_iter_advance(&msg->msg_iter, err);
499502
}
500-
err = -EINVAL;
503+
501504
/* ensure output buffer is sufficiently large */
502-
if (usedpages < outlen)
503-
goto free;
505+
if (usedpages < outlen) {
506+
err = -EINVAL;
507+
goto unlock;
508+
}
504509

505510
aead_request_set_crypt(req, areq->tsgl, areq->first_rsgl.sgl.sg, used,
506511
areq->iv);
@@ -571,6 +576,7 @@ static int aead_recvmsg_sync(struct socket *sock, struct msghdr *msg, int flags)
571576
goto unlock;
572577
}
573578

579+
/* data length provided by caller via sendmsg/sendpage */
574580
used = ctx->used;
575581

576582
/*
@@ -585,16 +591,27 @@ static int aead_recvmsg_sync(struct socket *sock, struct msghdr *msg, int flags)
585591
if (!aead_sufficient_data(ctx))
586592
goto unlock;
587593

588-
outlen = used;
594+
/*
595+
* Calculate the minimum output buffer size holding the result of the
596+
* cipher operation. When encrypting data, the receiving buffer is
597+
* larger by the tag length compared to the input buffer as the
598+
* encryption operation generates the tag. For decryption, the input
599+
* buffer provides the tag which is consumed resulting in only the
600+
* plaintext without a buffer for the tag returned to the caller.
601+
*/
602+
if (ctx->enc)
603+
outlen = used + as;
604+
else
605+
outlen = used - as;
589606

590607
/*
591608
* The cipher operation input data is reduced by the associated data
592609
* length as this data is processed separately later on.
593610
*/
594-
used -= ctx->aead_assoclen + (ctx->enc ? as : 0);
611+
used -= ctx->aead_assoclen;
595612

596613
/* convert iovecs of output buffers into scatterlists */
597-
while (iov_iter_count(&msg->msg_iter)) {
614+
while (outlen > usedpages && iov_iter_count(&msg->msg_iter)) {
598615
size_t seglen = min_t(size_t, iov_iter_count(&msg->msg_iter),
599616
(outlen - usedpages));
600617

@@ -621,16 +638,14 @@ static int aead_recvmsg_sync(struct socket *sock, struct msghdr *msg, int flags)
621638

622639
last_rsgl = rsgl;
623640

624-
/* we do not need more iovecs as we have sufficient memory */
625-
if (outlen <= usedpages)
626-
break;
627641
iov_iter_advance(&msg->msg_iter, err);
628642
}
629643

630-
err = -EINVAL;
631644
/* ensure output buffer is sufficiently large */
632-
if (usedpages < outlen)
645+
if (usedpages < outlen) {
646+
err = -EINVAL;
633647
goto unlock;
648+
}
634649

635650
sg_mark_end(sgl->sg + sgl->cur - 1);
636651
aead_request_set_crypt(&ctx->aead_req, sgl->sg, ctx->first_rsgl.sgl.sg,

crypto/mcryptd.c

+12-7
Original file line numberDiff line numberDiff line change
@@ -254,18 +254,22 @@ static void *mcryptd_alloc_instance(struct crypto_alg *alg, unsigned int head,
254254
goto out;
255255
}
256256

257-
static inline void mcryptd_check_internal(struct rtattr **tb, u32 *type,
257+
static inline bool mcryptd_check_internal(struct rtattr **tb, u32 *type,
258258
u32 *mask)
259259
{
260260
struct crypto_attr_type *algt;
261261

262262
algt = crypto_get_attr_type(tb);
263263
if (IS_ERR(algt))
264-
return;
265-
if ((algt->type & CRYPTO_ALG_INTERNAL))
266-
*type |= CRYPTO_ALG_INTERNAL;
267-
if ((algt->mask & CRYPTO_ALG_INTERNAL))
268-
*mask |= CRYPTO_ALG_INTERNAL;
264+
return false;
265+
266+
*type |= algt->type & CRYPTO_ALG_INTERNAL;
267+
*mask |= algt->mask & CRYPTO_ALG_INTERNAL;
268+
269+
if (*type & *mask & CRYPTO_ALG_INTERNAL)
270+
return true;
271+
else
272+
return false;
269273
}
270274

271275
static int mcryptd_hash_init_tfm(struct crypto_tfm *tfm)
@@ -492,7 +496,8 @@ static int mcryptd_create_hash(struct crypto_template *tmpl, struct rtattr **tb,
492496
u32 mask = 0;
493497
int err;
494498

495-
mcryptd_check_internal(tb, &type, &mask);
499+
if (!mcryptd_check_internal(tb, &type, &mask))
500+
return -EINVAL;
496501

497502
halg = ahash_attr_alg(tb[1], type, mask);
498503
if (IS_ERR(halg))

drivers/crypto/caam/ctrl.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -558,8 +558,9 @@ static int caam_probe(struct platform_device *pdev)
558558
* Enable DECO watchdogs and, if this is a PHYS_ADDR_T_64BIT kernel,
559559
* long pointers in master configuration register
560560
*/
561-
clrsetbits_32(&ctrl->mcr, MCFGR_AWCACHE_MASK, MCFGR_AWCACHE_CACH |
562-
MCFGR_AWCACHE_BUFF | MCFGR_WDENABLE | MCFGR_LARGE_BURST |
561+
clrsetbits_32(&ctrl->mcr, MCFGR_AWCACHE_MASK | MCFGR_LONG_PTR,
562+
MCFGR_AWCACHE_CACH | MCFGR_AWCACHE_BUFF |
563+
MCFGR_WDENABLE | MCFGR_LARGE_BURST |
563564
(sizeof(dma_addr_t) == sizeof(u64) ? MCFGR_LONG_PTR : 0));
564565

565566
/*

drivers/crypto/marvell/hash.c

+5-6
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,11 @@ static void mv_cesa_ahash_std_step(struct ahash_request *req)
168168
mv_cesa_adjust_op(engine, &creq->op_tmpl);
169169
memcpy_toio(engine->sram, &creq->op_tmpl, sizeof(creq->op_tmpl));
170170

171-
digsize = crypto_ahash_digestsize(crypto_ahash_reqtfm(req));
172-
for (i = 0; i < digsize / 4; i++)
173-
writel_relaxed(creq->state[i], engine->regs + CESA_IVDIG(i));
174-
175-
mv_cesa_adjust_op(engine, &creq->op_tmpl);
176-
memcpy_toio(engine->sram, &creq->op_tmpl, sizeof(creq->op_tmpl));
171+
if (!sreq->offset) {
172+
digsize = crypto_ahash_digestsize(crypto_ahash_reqtfm(req));
173+
for (i = 0; i < digsize / 4; i++)
174+
writel_relaxed(creq->state[i], engine->regs + CESA_IVDIG(i));
175+
}
177176

178177
if (creq->cache_ptr)
179178
memcpy_toio(engine->sram + CESA_SA_DATA_SRAM_OFFSET,

0 commit comments

Comments
 (0)