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=-8.4 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 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 8722AC2BA19 for ; Tue, 14 Apr 2020 16:25:32 +0000 (UTC) Received: from krantz.zx2c4.com (krantz.zx2c4.com [192.95.5.69]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id F04762075E for ; Tue, 14 Apr 2020 16:25:31 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="NsgcQPq0" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org F04762075E Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=wireguard-bounces@lists.zx2c4.com Received: by krantz.zx2c4.com (ZX2C4 Mail Server) with ESMTP id 58f71260; Tue, 14 Apr 2020 16:15:13 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) by krantz.zx2c4.com (ZX2C4 Mail Server) with ESMTP id 35d04645 for ; Tue, 14 Apr 2020 16:15:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1586881501; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=yW8t58m2bp1NhazlqC7/k8J+nPWsrZBYDWUsjTyk2qQ=; b=NsgcQPq0gDRRzr+WCv6O+IejAhACeGf91UcZUch7xVIEkk4F0++6WsAKwkQ+6nkMoq/jD7 lzSV3f1EuKP3Hxnp2JivHQcF6Vr/Z22M++foNVb8RdxR7eWsyL6QJbeO4NazIgQ9svAhXl 7AFa78Hn9YCF68TMh9hKXOVlm2kXUcc= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-87-m_CJjLwlPvyXxruKyZ4cKA-1; Tue, 14 Apr 2020 12:24:56 -0400 X-MC-Unique: m_CJjLwlPvyXxruKyZ4cKA-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 12123107ACC4; Tue, 14 Apr 2020 16:24:50 +0000 (UTC) Received: from llong.remote.csb (ovpn-118-173.rdu2.redhat.com [10.10.118.173]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0E02D118DEE; Tue, 14 Apr 2020 16:24:36 +0000 (UTC) Subject: Re: [PATCH v2 2/2] crypto: Remove unnecessary memzero_explicit() To: Christophe Leroy , Andrew Morton , David Howells , Jarkko Sakkinen , James Morris , "Serge E. Hallyn" , Linus Torvalds , Joe Perches , Matthew Wilcox , David Rientjes Cc: linux-mm@kvack.org, keyrings@vger.kernel.org, linux-kernel@vger.kernel.org, x86@kernel.org, linux-crypto@vger.kernel.org, linux-s390@vger.kernel.org, linux-pm@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org, linux-amlogic@lists.infradead.org, linux-mediatek@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org, intel-wired-lan@lists.osuosl.org, linux-ppp@vger.kernel.org, wireguard@lists.zx2c4.com, linux-wireless@vger.kernel.org, devel@driverdev.osuosl.org, linux-scsi@vger.kernel.org, target-devel@vger.kernel.org, linux-btrfs@vger.kernel.org, linux-cifs@vger.kernel.org, samba-technical@lists.samba.org, linux-fscrypt@vger.kernel.org, ecryptfs@vger.kernel.org, kasan-dev@googlegroups.com, linux-bluetooth@vger.kernel.org, linux-wpan@vger.kernel.org, linux-sctp@vger.kernel.org, linux-nfs@vger.kernel.org, tipc-discussion@lists.sourceforge.net, cocci@systeme.lip6.fr, linux-security-module@vger.kernel.org, linux-integrity@vger.kernel.org References: <20200413211550.8307-1-longman@redhat.com> <20200413222846.24240-1-longman@redhat.com> From: Waiman Long Organization: Red Hat Message-ID: Date: Tue, 14 Apr 2020 12:24:36 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Content-Transfer-Encoding: quoted-printable X-BeenThere: wireguard@lists.zx2c4.com X-Mailman-Version: 2.1.30rc1 Precedence: list List-Id: Development discussion of WireGuard List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: wireguard-bounces@lists.zx2c4.com Sender: "WireGuard" On 4/14/20 2:08 AM, Christophe Leroy wrote: > > > Le 14/04/2020 =C3=A0 00:28, Waiman Long a =C3=A9crit=C2=A0: >> Since kfree_sensitive() will do an implicit memzero_explicit(), there >> is no need to call memzero_explicit() before it. Eliminate those >> memzero_explicit() and simplify the call sites. For better correctness= , >> the setting of keylen is also moved down after the key pointer check. >> >> Signed-off-by: Waiman Long >> --- >> =C2=A0 .../allwinner/sun8i-ce/sun8i-ce-cipher.c=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0 | 19 +++++------------- >> =C2=A0 .../allwinner/sun8i-ss/sun8i-ss-cipher.c=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0 | 20 +++++-------------- >> =C2=A0 drivers/crypto/amlogic/amlogic-gxl-cipher.c=C2=A0=C2=A0 | 12 ++= +-------- >> =C2=A0 drivers/crypto/inside-secure/safexcel_hash.c=C2=A0 |=C2=A0 3 +-= - >> =C2=A0 4 files changed, 14 insertions(+), 40 deletions(-) >> >> diff --git a/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-cipher.c >> b/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-cipher.c >> index aa4e8fdc2b32..8358fac98719 100644 >> --- a/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-cipher.c >> +++ b/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-cipher.c >> @@ -366,10 +366,7 @@ void sun8i_ce_cipher_exit(struct crypto_tfm *tfm) >> =C2=A0 { >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 struct sun8i_cipher_tfm_ctx *op =3D cry= pto_tfm_ctx(tfm); >> =C2=A0 -=C2=A0=C2=A0=C2=A0 if (op->key) { >> -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 memzero_explicit(op->key, = op->keylen); >> -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 kfree(op->key); >> -=C2=A0=C2=A0=C2=A0 } >> +=C2=A0=C2=A0=C2=A0 kfree_sensitive(op->key); >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 crypto_free_sync_skcipher(op->fallback_= tfm); >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 pm_runtime_put_sync_suspend(op->ce->dev= ); >> =C2=A0 } >> @@ -391,14 +388,11 @@ int sun8i_ce_aes_setkey(struct crypto_skcipher >> *tfm, const u8 *key, >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 dev_dbg(ce->dev= , "ERROR: Invalid keylen %u\n", keylen); >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return -EINVAL; >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 } >> -=C2=A0=C2=A0=C2=A0 if (op->key) { >> -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 memzero_explicit(op->key, = op->keylen); >> -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 kfree(op->key); >> -=C2=A0=C2=A0=C2=A0 } >> -=C2=A0=C2=A0=C2=A0 op->keylen =3D keylen; >> +=C2=A0=C2=A0=C2=A0 kfree_sensitive(op->key); >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 op->key =3D kmemdup(key, keylen, GFP_KE= RNEL | GFP_DMA); >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (!op->key) >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return -ENOMEM; >> +=C2=A0=C2=A0=C2=A0 op->keylen =3D keylen; > > Does it matter at all to ensure op->keylen is not set when of->key is > NULL ? I'm not sure. > > But if it does, then op->keylen should be set to 0 when freeing op->key= .=20 My thinking is that if memory allocation fails, we just don't touch anything and return an error code. I will not explicitly set keylen to 0 in this case unless it is specified in the API documentation. Cheers, Longman