Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp4729390pxb; Tue, 31 Aug 2021 11:50:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyBp1XHWKE7W7Lm/Gv7hm6BhtKTLr/a+LNMoegyPFqzrAny7iiyjGZPrXy0SeukUStYbFxC X-Received: by 2002:a05:6638:13d6:: with SMTP id i22mr4163381jaj.13.1630435810187; Tue, 31 Aug 2021 11:50:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630435810; cv=none; d=google.com; s=arc-20160816; b=Z18UaazOOGsGxJIk8kqBAJYPsjiyEtBI0zp9cl0HXpJJjZMflFMxpGZETjTmNwKE/s DXb16do8CoGBjxZw8lMUwFK7iqeyWXTfh9nHbK5HDexjEufgUdb0DlUV6IdaVTt0tTgh 2QKoeok72nDp+JyeQ6Prucpim0oqUZf97xVfIgG+/81sCwZDfYImeHPRFizuXpZMQqvb qSQ1yv/I4OUOeeZrC/ZYkm7odEmPFIfN7c9gAcn2ADRObCqb0hWbqiXEqBcFBurYJuQE 1etajei4Dv0J0gC4Gxj3ilhsgtFdFf5TY7f84jUHlj2LrnWjGIRFOT9Oh7UHCpuwWWPJ /cEg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=NZ7om+PEjbZs2krGj/fcLmhe1iqA3wtrhKdDf4GtlPI=; b=lLTM8Ul82fjt2378RI2vO6ttTHfAbTYA2R4hDia9S433TVvmwEsHnWOi3Hpz7XOXB6 jXKK0LfygR8xk9BjoqkXKOdhQYnhx78beet2QS/rA9Q/BLnBpqOq3812EAG3HkvGRu0B 2CV525aOJa7XuelneGPheKi9IPGlWJOaj88IywaOWV7u7Wl5sxjzX3ZboNRWD5KKeK77 nxmue9gkaHp06vrRugh2HK0G4F5fKHDBUmvE9rLO9CM1bZki4x+kyjHTQz1n9/HbFqjC E+BUdf3QktH5Ia832a6C3EqWt9yMDyS/DiOiiGDejBJybGw8pY2jzAs5+A6uqUGEk3sh GG5A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=lOs8rPbw; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r17si18267515iln.147.2021.08.31.11.49.48; Tue, 31 Aug 2021 11:50:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=lOs8rPbw; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230366AbhHaSuf (ORCPT + 99 others); Tue, 31 Aug 2021 14:50:35 -0400 Received: from mail.kernel.org ([198.145.29.99]:58306 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230525AbhHaSuf (ORCPT ); Tue, 31 Aug 2021 14:50:35 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 4BE116108B; Tue, 31 Aug 2021 18:49:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1630435779; bh=s0v10fx2wIM2WlCJsoeN2cxqVtakw414FOy2ZKgaK/k=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=lOs8rPbwIvgF2gWdK9LiA+xkJyNXt5fI4N9QI2R6VxAUwYogt/zv92YvmSC5fX8Qg 424czAmCyFEF/lUi6/1kGH6Qb/I/M7PITzdzQENh3upsPCwm23NjyqAOjfByGgD/Ub EcvFc1LfhnwLgTNKf8e5DuLp0SPcUbOudppZ2Tr9y80MwduxTdts8AhsUChnacYSt3 FI8YgYrbQglRsdq13q8RO7CpLhYXgbEljdg7E4aciDrfoDhAIZ4Z/Ps0aJfLOvgJ6X LoV/k10g1NicZW5K7Vk0xl6ieqGOgTYHIBzY3xC04TYNUfAx+nPX2hf1v/ND57UDM6 8euhfydgPVahA== Date: Tue, 31 Aug 2021 11:49:37 -0700 From: Eric Biggers To: Sandy Harris Cc: Herbert Xu , Lukas Bulwahn , "David S . Miller" , Linux Crypto Mailing List , Nathan Chancellor , Nick Desaulniers , clang-built-linux@googlegroups.com, kernel-janitors@vger.kernel.org, LKML Subject: Re: [PATCH] crypto: sha512: remove imaginary and mystifying clearing of variables Message-ID: References: <20210822103107.28974-1-lukas.bulwahn@gmail.com> <20210827083842.GF21571@gondor.apana.org.au> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org On Sat, Aug 28, 2021 at 03:46:50PM +0800, Sandy Harris wrote: > On Fri, Aug 27, 2021 at 4:40 PM Herbert Xu wrote: > > > > On Sun, Aug 22, 2021 at 12:31:07PM +0200, Lukas Bulwahn wrote: > > > The function sha512_transform() assigns all local variables to 0 before > > > returning to its caller with the intent to erase sensitive data. > > > .... > > > > > > The assignments to clear a through h and t1/t2 are optimized out by the > > > compiler because they are unused after the assignments. > > Just no. > > You are right, there is a problem here. I thank you for pointing it > out & I've already fixed it in some of my own code. > > However, I think your solution is dead wrong. You are correct that > these assignments are useless because the compiler will optimise them > out, and that's a problem. However, it is not at all "mistiifying"; > they are there for an obvious reason, to avoid leaving state that > might be useful to an enemy. That is quite a small risk, but then it > is a small mitigation, so worth doing. > > The correct solution is not to just remove the assignments, but rather > to replace them with code that will not be optimised away, force the > compiler to do what we need. We already do that for operations that > clear various arrays and structures, using memzero_explicit() rather > than memset(). Similarly, we should replace the assignments with calls > to this macro: > > /* > clear a variable > in a way the compiler will not optimise out > */ > #define clear(x) memzero_explicit( &x, sizeof(x) ) Clearing of local variables is never guaranteed to work properly, as the compiler can create multiple copies and/or put them in registers. It's much more likely to work for arrays than simple variables though (and not cause the variable to be unnecessarily spilled from registers to the stack), so that is the only one the kernel really bothers with. - Eric