From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.1 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 97FF1C04AB7 for ; Tue, 14 May 2019 23:30:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 66DB5216FD for ; Tue, 14 May 2019 23:30:11 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="JBcRlSGs" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726148AbfENXaK (ORCPT ); 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-Google-Smtp-Source: APXvYqzCTYmrXIqVdifKvgQD1uMB4RIOkfPXRyOr1prDPNgAW4iOV27TXXqdLy8fcVROp3ZMhWdS+A== 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