Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp1472686yba; Thu, 16 May 2019 23:02:53 -0700 (PDT) X-Google-Smtp-Source: APXvYqwO2vwpK2h0S8S8vSyfd3M9UUim5lfQAYeBoGeJkuYxTyA9jL0Z12m3wpNHFUhY8EGLW5fD X-Received: by 2002:a17:902:15c5:: with SMTP id a5mr55422992plh.39.1558072973017; Thu, 16 May 2019 23:02:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558072973; cv=none; d=google.com; s=arc-20160816; b=VgcpnwP1f7XnzmVi7CHsE8crmD18vakfNgPQzgrEemUb/Lp9JNd1bIIw7SOnZngDAd vzREhSdxVIEb3AC5YQKdoUaA4+3oJYSnimqPAVTleuweiI/F46GQsS3p0L5H1JQ/CNgh 20FUmGbrcI0/S6itp+3revCiNdQygRVX0gf1bjk9DtaARoxhuPIHDAvPjEXuV6W0n+OZ bnFF9W3Hs+pNtHuXt4p8iqfZcw7Zp/7iGA3O173KSyn8apErhtcX7v60gxTp/T8eJzQY uS3Gyox5ovOxunDSAES3sExu+abphTzu/IU8BhPjGJ5tyvdAqr8yWaP54oxnHbWZasSB 13HA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=QTX3b5DfI3GeYIq+NFXgFu/zTuIyTKyvUK5YCXdI9P8=; b=NpG8M9I99H74pCU2+pfmX/DqA2gB44yQ/dcLCjE+YiygzrzyvfZ0MYbQgy6UcDE2Ct 2dz3lDttGf8t1hPeTVFZBcjpA7W2Ej+ng0LUYcL3pc1x6BnYb5rX62Svx12CE/GNPbd0 bMIdnaPC2P4Dr5k8t9xqKYEH7bvELWz4Rml8nP6gw2qE8IXtcCH+Mzsh/cN6v/QnW+E/ pC4hN16CpLfdvNUJ9QPftdvA9zAlPFuf9G4ru64WQ1Gyf/oWnpcupOrz8o3EleZ2tbyc 15TAc0rHbsl95s3XVPLmPUq0LwsUnxbAA0pvpqzbV2wKO9mP2KxzZ8x60QTcnBmNiTdP br7A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q205si8445358pfc.151.2019.05.16.23.02.38; Thu, 16 May 2019 23:02:52 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727481AbfEQGAb (ORCPT + 99 others); Fri, 17 May 2019 02:00:31 -0400 Received: from [128.1.224.119] ([128.1.224.119]:54052 "EHLO deadmen.hmeau.com" rhost-flags-FAIL-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1727242AbfEQGAa (ORCPT ); Fri, 17 May 2019 02:00:30 -0400 Received: from gondobar.mordor.me.apana.org.au ([192.168.128.4] helo=gondobar) by deadmen.hmeau.com with esmtps (Exim 4.89 #2 (Debian)) id 1hRVuv-0007s9-76; Fri, 17 May 2019 14:00:29 +0800 Received: from herbert by gondobar with local (Exim 4.89) (envelope-from ) id 1hRVut-0000YN-9B; Fri, 17 May 2019 14:00:27 +0800 Date: Fri, 17 May 2019 14:00:27 +0800 From: Herbert Xu To: Eric Biggers Cc: linux-crypto@vger.kernel.org, Corentin Labbe , stable@vger.kernel.org, Kees Cook Subject: Re: [PATCH] crypto: hash - fix incorrect HASH_MAX_DESCSIZE Message-ID: <20190517060027.wqowv4aazpcrr6pv@gondor.apana.org.au> References: <20190514231315.7729-1-ebiggers@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190514231315.7729-1-ebiggers@kernel.org> User-Agent: NeoMutt/20170113 (1.7.2) Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org On Tue, May 14, 2019 at 04:13:15PM -0700, Eric Biggers wrote: > From: Eric Biggers > > 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: b68a7ec1e9a3 ("crypto: hash - Remove VLA usage") > Reported-by: Corentin Labbe > Cc: # v4.20+ > Cc: Kees Cook > Signed-off-by: Eric Biggers > --- > crypto/hmac.c | 2 ++ > include/crypto/hash.h | 8 +++++++- > 2 files changed, 9 insertions(+), 1 deletion(-) Patch applied. Thanks. -- Email: Herbert Xu Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt