Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp6917335yba; Tue, 14 May 2019 16:31:13 -0700 (PDT) X-Google-Smtp-Source: APXvYqwbWP9TVN6pPwcFSB1fdU0ztR7y3tDJYSnS3tqxG2rA+fvrfk9ARNKUgERRvDn2iAoElH/8 X-Received: by 2002:a65:64ca:: with SMTP id t10mr40619815pgv.177.1557876673044; Tue, 14 May 2019 16:31:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1557876673; cv=none; d=google.com; s=arc-20160816; b=OO/unLolShDupoRBcjyYWyeytgA2a+5zLCemOh11YKGmtfFdYwLowuoR9eKzbp4Sa8 RUwBbz8fJsJS66Jbi9JrTrXK/lLwAeng4VapB8jplE9SlUsphqTNOVlCreK7oaTi9VFm n0TmKjKH4k6nNGTO1vpQ2NC9aVV4IJaiPYHe/0xJl/F7PLY+rPwr5I9/6QJ/5upuZ/xM KzQKcpOHVYV9CBpGP8dasCRDbTyEPLSx3U5WtPywTVEMNfv30nSoRw+oDPhEUzz+Bc3v WpEtVv0zAjvCW0cVPsPl5p4+o/vwD1zVvDvBxUnfsD5x0aRRJEzdBbBrkxzv1zzvbUUt Utvg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=dr0wEYjOsuwMkb0C7b01iPZFdmbTDV/YOBzLh8vitqk=; b=Df6ylo44DRYJkDqeosLSoJnX8qxEX8DfutysUIxe0iMLkvg17rmhj/y9OhMZ0Mz69p T5kpy8tKMuIV1r8KqoJ2b+8tb4dATU1pilu7Yg/6Fsg05yZ1jGUucVSmB/4aa0UQ+KZn ujtGgc8ab+TO1CejSc98AwOqolzXKiFptXWuBRf23I9sNEp8rTWhxBWTIW9ZEvcOfjNv SYv/8JO85TmqJRsHJpWIjWd/LJAbUYlMoj9QeLFUWbeK3Nr+d1GFIRBi1EPwZLSNwTF9 UBijwP7ug4A13QnbU+TuRXSGuufuJQEHLa8vHfeQ0qM2C3zP+Zn0VXbXuwiFekylJiad Sb3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=JBcRlSGs; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p24si159525plr.269.2019.05.14.16.30.52; Tue, 14 May 2019 16:31:13 -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; dkim=pass header.i=@chromium.org header.s=google header.b=JBcRlSGs; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726148AbfENXaK (ORCPT + 99 others); Tue, 14 May 2019 19:30:10 -0400 Received: from mail-pg1-f193.google.com ([209.85.215.193]:40671 "EHLO mail-pg1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726180AbfENXaJ (ORCPT ); Tue, 14 May 2019 19:30:09 -0400 Received: by mail-pg1-f193.google.com with SMTP id d31so327815pgl.7 for ; Tue, 14 May 2019 16:30:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=dr0wEYjOsuwMkb0C7b01iPZFdmbTDV/YOBzLh8vitqk=; b=JBcRlSGstwulLHIt6Ogy6o6t1rhwuzuWBvXh9EEQ4jLWv8XMz6eL5JJPx3FkwmM1v8 RV1mav5MwQ1+DxaAkYD25sM8lCwZTK3i74o+upCQL/pVJuWktp+zRAkAmfl+S4+s8k92 VmvENyV6ge9AZkbIj03Kzh74NWF2mPywHnNiI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=dr0wEYjOsuwMkb0C7b01iPZFdmbTDV/YOBzLh8vitqk=; b=ZAeaoLv39Dyq5pATbbzyLq3YbjB86QjLY8cnq6fjv8EAYafauSKPDbB6ssJATWdk9A PksTf3fFnw82pdCvYSJfpXJSYl3IQB0aRwoKiMjyiS7AkxuJUkl5J0eQSrUzJ2xXq3su 0to9GARZ2jRGJ4AEfLAyy9j74mwUYByOxwaBWIekvNlnZ4hqP+A9qL46ACEwYFJDab37 sIkaPJocaORG+uB0D1dHEzEC2eQDv6qvCKVGVVFmLLDqQR0a8tjyP0ZK+8cYWR2Hcm0J nfM1y41mjsOpPKz65NbmqVdLnrXik5B8yBydhJfMUhe+VbO494Hk/wCXWCaZ1is+3fM9 5oqA== X-Gm-Message-State: APjAAAUsO/ISksyGs6iyWbPlkc9+BKoCwGREMY5Am7CjGi+PbzYJpcX8 RZyshazWfmmrgbJ3xrKRyd/XGQ== X-Received: by 2002:aa7:8f22:: with SMTP id y2mr31978285pfr.22.1557876609003; Tue, 14 May 2019 16:30:09 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id 125sm215710pge.45.2019.05.14.16.30.07 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 14 May 2019 16:30:07 -0700 (PDT) Date: Tue, 14 May 2019 16:30:06 -0700 From: Kees Cook To: Eric Biggers Cc: linux-crypto@vger.kernel.org, Herbert Xu , Corentin Labbe , stable@vger.kernel.org Subject: Re: [PATCH] crypto: hash - fix incorrect HASH_MAX_DESCSIZE Message-ID: <201905141629.12E8DDADF@keescook> 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> 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 Ah, yikes! Nice catch. Thanks for fixing this. :) Reviewed-by: Kees Cook -- Kees Cook