mirror of
https://github.com/AuxXxilium/linux_dsm_epyc7002.git
synced 2024-11-25 02:30:52 +07:00
e1354400b2
The "hmac(sha3-224-generic)" algorithm has a descsize of 368 bytes,
which is greater than HASH_MAX_DESCSIZE (360) which is only enough for
sha3-224-generic. The check in shash_prepare_alg() doesn't catch this
because the HMAC template doesn't set descsize on the algorithms, but
rather sets it on each individual HMAC transform.
This causes a stack buffer overflow when SHASH_DESC_ON_STACK() is used
with hmac(sha3-224-generic).
Fix it by increasing HASH_MAX_DESCSIZE to the real maximum. Also add a
sanity check to hmac_init().
This was detected by the improved crypto self-tests in v5.2, by loading
the tcrypt module with CONFIG_CRYPTO_MANAGER_EXTRA_TESTS=y enabled. I
didn't notice this bug when I ran the self-tests by requesting the
algorithms via AF_ALG (i.e., not using tcrypt), probably because the
stack layout differs in the two cases and that made a difference here.
KASAN report:
BUG: KASAN: stack-out-of-bounds in memcpy include/linux/string.h:359 [inline]
BUG: KASAN: stack-out-of-bounds in shash_default_import+0x52/0x80 crypto/shash.c:223
Write of size 360 at addr ffff8880651defc8 by task insmod/3689
CPU: 2 PID: 3689 Comm: insmod Tainted: G E 5.1.0-10741-g35c99ffa20edd #11
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.10.2-1 04/01/2014
Call Trace:
__dump_stack lib/dump_stack.c:77 [inline]
dump_stack+0x86/0xc5 lib/dump_stack.c:113
print_address_description+0x7f/0x260 mm/kasan/report.c:188
__kasan_report+0x144/0x187 mm/kasan/report.c:317
kasan_report+0x12/0x20 mm/kasan/common.c:614
check_memory_region_inline mm/kasan/generic.c:185 [inline]
check_memory_region+0x137/0x190 mm/kasan/generic.c:191
memcpy+0x37/0x50 mm/kasan/common.c:125
memcpy include/linux/string.h:359 [inline]
shash_default_import+0x52/0x80 crypto/shash.c:223
crypto_shash_import include/crypto/hash.h:880 [inline]
hmac_import+0x184/0x240 crypto/hmac.c:102
hmac_init+0x96/0xc0 crypto/hmac.c:107
crypto_shash_init include/crypto/hash.h:902 [inline]
shash_digest_unaligned+0x9f/0xf0 crypto/shash.c:194
crypto_shash_digest+0xe9/0x1b0 crypto/shash.c:211
generate_random_hash_testvec.constprop.11+0x1ec/0x5b0 crypto/testmgr.c:1331
test_hash_vs_generic_impl+0x3f7/0x5c0 crypto/testmgr.c:1420
__alg_test_hash+0x26d/0x340 crypto/testmgr.c:1502
alg_test_hash+0x22e/0x330 crypto/testmgr.c:1552
alg_test.part.7+0x132/0x610 crypto/testmgr.c:4931
alg_test+0x1f/0x40 crypto/testmgr.c:4952
Fixes: b68a7ec1e9
("crypto: hash - Remove VLA usage")
Reported-by: Corentin Labbe <clabbe.montjoie@gmail.com>
Cc: <stable@vger.kernel.org> # v4.20+
Cc: Kees Cook <keescook@chromium.org>
Signed-off-by: Eric Biggers <ebiggers@google.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Tested-by: Corentin Labbe <clabbe.montjoie@gmail.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
268 lines
6.7 KiB
C
268 lines
6.7 KiB
C
/*
|
|
* Cryptographic API.
|
|
*
|
|
* HMAC: Keyed-Hashing for Message Authentication (RFC2104).
|
|
*
|
|
* Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
|
|
* Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
|
|
*
|
|
* The HMAC implementation is derived from USAGI.
|
|
* Copyright (c) 2002 Kazunori Miyazawa <miyazawa@linux-ipv6.org> / USAGI
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
* under the terms of the GNU General Public License as published by the Free
|
|
* Software Foundation; either version 2 of the License, or (at your option)
|
|
* any later version.
|
|
*
|
|
*/
|
|
|
|
#include <crypto/hmac.h>
|
|
#include <crypto/internal/hash.h>
|
|
#include <crypto/scatterwalk.h>
|
|
#include <linux/err.h>
|
|
#include <linux/init.h>
|
|
#include <linux/kernel.h>
|
|
#include <linux/module.h>
|
|
#include <linux/scatterlist.h>
|
|
#include <linux/string.h>
|
|
|
|
struct hmac_ctx {
|
|
struct crypto_shash *hash;
|
|
};
|
|
|
|
static inline void *align_ptr(void *p, unsigned int align)
|
|
{
|
|
return (void *)ALIGN((unsigned long)p, align);
|
|
}
|
|
|
|
static inline struct hmac_ctx *hmac_ctx(struct crypto_shash *tfm)
|
|
{
|
|
return align_ptr(crypto_shash_ctx_aligned(tfm) +
|
|
crypto_shash_statesize(tfm) * 2,
|
|
crypto_tfm_ctx_alignment());
|
|
}
|
|
|
|
static int hmac_setkey(struct crypto_shash *parent,
|
|
const u8 *inkey, unsigned int keylen)
|
|
{
|
|
int bs = crypto_shash_blocksize(parent);
|
|
int ds = crypto_shash_digestsize(parent);
|
|
int ss = crypto_shash_statesize(parent);
|
|
char *ipad = crypto_shash_ctx_aligned(parent);
|
|
char *opad = ipad + ss;
|
|
struct hmac_ctx *ctx = align_ptr(opad + ss,
|
|
crypto_tfm_ctx_alignment());
|
|
struct crypto_shash *hash = ctx->hash;
|
|
SHASH_DESC_ON_STACK(shash, hash);
|
|
unsigned int i;
|
|
|
|
shash->tfm = hash;
|
|
|
|
if (keylen > bs) {
|
|
int err;
|
|
|
|
err = crypto_shash_digest(shash, inkey, keylen, ipad);
|
|
if (err)
|
|
return err;
|
|
|
|
keylen = ds;
|
|
} else
|
|
memcpy(ipad, inkey, keylen);
|
|
|
|
memset(ipad + keylen, 0, bs - keylen);
|
|
memcpy(opad, ipad, bs);
|
|
|
|
for (i = 0; i < bs; i++) {
|
|
ipad[i] ^= HMAC_IPAD_VALUE;
|
|
opad[i] ^= HMAC_OPAD_VALUE;
|
|
}
|
|
|
|
return crypto_shash_init(shash) ?:
|
|
crypto_shash_update(shash, ipad, bs) ?:
|
|
crypto_shash_export(shash, ipad) ?:
|
|
crypto_shash_init(shash) ?:
|
|
crypto_shash_update(shash, opad, bs) ?:
|
|
crypto_shash_export(shash, opad);
|
|
}
|
|
|
|
static int hmac_export(struct shash_desc *pdesc, void *out)
|
|
{
|
|
struct shash_desc *desc = shash_desc_ctx(pdesc);
|
|
|
|
return crypto_shash_export(desc, out);
|
|
}
|
|
|
|
static int hmac_import(struct shash_desc *pdesc, const void *in)
|
|
{
|
|
struct shash_desc *desc = shash_desc_ctx(pdesc);
|
|
struct hmac_ctx *ctx = hmac_ctx(pdesc->tfm);
|
|
|
|
desc->tfm = ctx->hash;
|
|
|
|
return crypto_shash_import(desc, in);
|
|
}
|
|
|
|
static int hmac_init(struct shash_desc *pdesc)
|
|
{
|
|
return hmac_import(pdesc, crypto_shash_ctx_aligned(pdesc->tfm));
|
|
}
|
|
|
|
static int hmac_update(struct shash_desc *pdesc,
|
|
const u8 *data, unsigned int nbytes)
|
|
{
|
|
struct shash_desc *desc = shash_desc_ctx(pdesc);
|
|
|
|
return crypto_shash_update(desc, data, nbytes);
|
|
}
|
|
|
|
static int hmac_final(struct shash_desc *pdesc, u8 *out)
|
|
{
|
|
struct crypto_shash *parent = pdesc->tfm;
|
|
int ds = crypto_shash_digestsize(parent);
|
|
int ss = crypto_shash_statesize(parent);
|
|
char *opad = crypto_shash_ctx_aligned(parent) + ss;
|
|
struct shash_desc *desc = shash_desc_ctx(pdesc);
|
|
|
|
return crypto_shash_final(desc, out) ?:
|
|
crypto_shash_import(desc, opad) ?:
|
|
crypto_shash_finup(desc, out, ds, out);
|
|
}
|
|
|
|
static int hmac_finup(struct shash_desc *pdesc, const u8 *data,
|
|
unsigned int nbytes, u8 *out)
|
|
{
|
|
|
|
struct crypto_shash *parent = pdesc->tfm;
|
|
int ds = crypto_shash_digestsize(parent);
|
|
int ss = crypto_shash_statesize(parent);
|
|
char *opad = crypto_shash_ctx_aligned(parent) + ss;
|
|
struct shash_desc *desc = shash_desc_ctx(pdesc);
|
|
|
|
return crypto_shash_finup(desc, data, nbytes, out) ?:
|
|
crypto_shash_import(desc, opad) ?:
|
|
crypto_shash_finup(desc, out, ds, out);
|
|
}
|
|
|
|
static int hmac_init_tfm(struct crypto_tfm *tfm)
|
|
{
|
|
struct crypto_shash *parent = __crypto_shash_cast(tfm);
|
|
struct crypto_shash *hash;
|
|
struct crypto_instance *inst = (void *)tfm->__crt_alg;
|
|
struct crypto_shash_spawn *spawn = crypto_instance_ctx(inst);
|
|
struct hmac_ctx *ctx = hmac_ctx(parent);
|
|
|
|
hash = crypto_spawn_shash(spawn);
|
|
if (IS_ERR(hash))
|
|
return PTR_ERR(hash);
|
|
|
|
parent->descsize = sizeof(struct shash_desc) +
|
|
crypto_shash_descsize(hash);
|
|
if (WARN_ON(parent->descsize > HASH_MAX_DESCSIZE))
|
|
return -EINVAL;
|
|
|
|
ctx->hash = hash;
|
|
return 0;
|
|
}
|
|
|
|
static void hmac_exit_tfm(struct crypto_tfm *tfm)
|
|
{
|
|
struct hmac_ctx *ctx = hmac_ctx(__crypto_shash_cast(tfm));
|
|
crypto_free_shash(ctx->hash);
|
|
}
|
|
|
|
static int hmac_create(struct crypto_template *tmpl, struct rtattr **tb)
|
|
{
|
|
struct shash_instance *inst;
|
|
struct crypto_alg *alg;
|
|
struct shash_alg *salg;
|
|
int err;
|
|
int ds;
|
|
int ss;
|
|
|
|
err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
|
|
if (err)
|
|
return err;
|
|
|
|
salg = shash_attr_alg(tb[1], 0, 0);
|
|
if (IS_ERR(salg))
|
|
return PTR_ERR(salg);
|
|
alg = &salg->base;
|
|
|
|
/* The underlying hash algorithm must be unkeyed */
|
|
err = -EINVAL;
|
|
if (crypto_shash_alg_has_setkey(salg))
|
|
goto out_put_alg;
|
|
|
|
ds = salg->digestsize;
|
|
ss = salg->statesize;
|
|
if (ds > alg->cra_blocksize ||
|
|
ss < alg->cra_blocksize)
|
|
goto out_put_alg;
|
|
|
|
inst = shash_alloc_instance("hmac", alg);
|
|
err = PTR_ERR(inst);
|
|
if (IS_ERR(inst))
|
|
goto out_put_alg;
|
|
|
|
err = crypto_init_shash_spawn(shash_instance_ctx(inst), salg,
|
|
shash_crypto_instance(inst));
|
|
if (err)
|
|
goto out_free_inst;
|
|
|
|
inst->alg.base.cra_priority = alg->cra_priority;
|
|
inst->alg.base.cra_blocksize = alg->cra_blocksize;
|
|
inst->alg.base.cra_alignmask = alg->cra_alignmask;
|
|
|
|
ss = ALIGN(ss, alg->cra_alignmask + 1);
|
|
inst->alg.digestsize = ds;
|
|
inst->alg.statesize = ss;
|
|
|
|
inst->alg.base.cra_ctxsize = sizeof(struct hmac_ctx) +
|
|
ALIGN(ss * 2, crypto_tfm_ctx_alignment());
|
|
|
|
inst->alg.base.cra_init = hmac_init_tfm;
|
|
inst->alg.base.cra_exit = hmac_exit_tfm;
|
|
|
|
inst->alg.init = hmac_init;
|
|
inst->alg.update = hmac_update;
|
|
inst->alg.final = hmac_final;
|
|
inst->alg.finup = hmac_finup;
|
|
inst->alg.export = hmac_export;
|
|
inst->alg.import = hmac_import;
|
|
inst->alg.setkey = hmac_setkey;
|
|
|
|
err = shash_register_instance(tmpl, inst);
|
|
if (err) {
|
|
out_free_inst:
|
|
shash_free_instance(shash_crypto_instance(inst));
|
|
}
|
|
|
|
out_put_alg:
|
|
crypto_mod_put(alg);
|
|
return err;
|
|
}
|
|
|
|
static struct crypto_template hmac_tmpl = {
|
|
.name = "hmac",
|
|
.create = hmac_create,
|
|
.free = shash_free_instance,
|
|
.module = THIS_MODULE,
|
|
};
|
|
|
|
static int __init hmac_module_init(void)
|
|
{
|
|
return crypto_register_template(&hmac_tmpl);
|
|
}
|
|
|
|
static void __exit hmac_module_exit(void)
|
|
{
|
|
crypto_unregister_template(&hmac_tmpl);
|
|
}
|
|
|
|
subsys_initcall(hmac_module_init);
|
|
module_exit(hmac_module_exit);
|
|
|
|
MODULE_LICENSE("GPL");
|
|
MODULE_DESCRIPTION("HMAC hash algorithm");
|
|
MODULE_ALIAS_CRYPTO("hmac");
|