From patchwork Tue May 2 14:01:36 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Stancek X-Patchwork-Id: 9708129 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id B06BD60349 for ; Tue, 2 May 2017 14:01:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A180928449 for ; Tue, 2 May 2017 14:01:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 960362845D; Tue, 2 May 2017 14:01:45 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 476F628449 for ; Tue, 2 May 2017 14:01:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751120AbdEBOBo (ORCPT ); Tue, 2 May 2017 10:01:44 -0400 Received: from mx1.redhat.com ([209.132.183.28]:41190 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751093AbdEBOBo (ORCPT ); Tue, 2 May 2017 10:01:44 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 6A991F0045; Tue, 2 May 2017 14:01:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 6A991F0045 Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=jstancek@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 6A991F0045 Received: from dustball.brq.redhat.com (dustball.brq.redhat.com [10.34.26.57]) by smtp.corp.redhat.com (Postfix) with ESMTP id 49EEA17CD7; Tue, 2 May 2017 14:01:37 +0000 (UTC) Subject: Re: [bug] sha1-avx2 and read beyond To: linux-crypto@vger.kernel.org References: <1950313665.4516034.1493507041652.JavaMail.zimbra@redhat.com> Cc: herbert@gondor.apana.org.au, Benjamin Coddington , jstancek@redhat.com From: Jan Stancek Message-ID: <678884b8-89d0-92a9-f5b4-2962f963f07b@redhat.com> Date: Tue, 2 May 2017 16:01:36 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.7.0 MIME-Version: 1.0 In-Reply-To: <1950313665.4516034.1493507041652.JavaMail.zimbra@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Tue, 02 May 2017 14:01:38 +0000 (UTC) Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP On 04/30/2017 01:04 AM, Jan Stancek wrote: > Hi, > > I'm seeing rare crashes during NFS cthon with krb5 auth. After > some digging I arrived at potential problem with sha1-avx2. > > Problem appears to be that sha1_transform_avx2() reads beyond > number of blocks you pass, if it is an odd number. It appears > to try read one block more. It's not just odd vs even number of blocks. It appears to be doing read ahead (in size of 2 blocks). For example, for data starting at page offset 1 with length 3967, it still crashes on access to subsequent page. Patch below fixes it for me, but it feels more like workaround. Regards, Jan diff --git a/arch/x86/crypto/sha1_ssse3_glue.c b/arch/x86/crypto/sha1_ssse3_glue.c index fc61739150e7..736128267715 100644 --- a/arch/x86/crypto/sha1_ssse3_glue.c +++ b/arch/x86/crypto/sha1_ssse3_glue.c @@ -212,10 +212,41 @@ static bool avx2_usable(void) static void sha1_apply_transform_avx2(u32 *digest, const char *data, unsigned int rounds) { + const char *last; + unsigned int rounds_avx2; + /* Select the optimal transform based on data block size */ - if (rounds >= SHA1_AVX2_BLOCK_OPTSIZE) - sha1_transform_avx2(digest, data, rounds); - else + if (rounds < SHA1_AVX2_BLOCK_OPTSIZE) + goto avx; + + /* + * sha1_transform_avx2() can read ahead couple blocks, which + * can cause problems if it crosses page boundary and next + * page doesn't exist. It operates on even number of blocks. + * Code below checks for worst case, where it can access + * up to 3 consecutive blocks after data end. In that case + * sha1_transform_avx2() is passed 3 blocks less and rest + * of data is handled by sha1_transform_avx(). + * + * +----------+---------+---------+---------+ + * 2x SHA1_BLOCK_SIZE | 2*SHA1_BLOCK_SIZE + * +----------+---------+---------+---------+ + * ^ data end + */ + last = data + (rounds + 3) * SHA1_BLOCK_SIZE - 1; + if (offset_in_page(last) >= 3 * SHA1_BLOCK_SIZE) { + rounds_avx2 = rounds; + } else { + rounds_avx2 = rounds - 3; + if (rounds_avx2 < SHA1_AVX2_BLOCK_OPTSIZE) + goto avx; + } + + sha1_transform_avx2(digest, data, rounds_avx2); + data += SHA1_BLOCK_SIZE * rounds_avx2; + rounds -= rounds_avx2; +avx: + if (rounds) sha1_transform_avx(digest, data, rounds); }