Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp4388360ybp; Mon, 7 Oct 2019 07:47:13 -0700 (PDT) X-Google-Smtp-Source: APXvYqzIHOCCZNPVHQkvrLdzUUO8Xp1mCksP3a+/jclJJn23GIVQ9G8TSdHjNf7Xc/kzY7fU/4tp X-Received: by 2002:aa7:d995:: with SMTP id u21mr29424511eds.271.1570459633768; Mon, 07 Oct 2019 07:47:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570459633; cv=none; d=google.com; s=arc-20160816; b=ibbQfQXzWbtmaX4j/SAxWoOdLBQ6XHXzMR5bFQ7WZDH1e0q6Z/FbNwRECSjkeq5mYm SGTKibakYg5yXgK50+qKRXCk8aGvCMyCG9AEQtuf5pfOckphszsTls0GSB5boEX1Vyff 694uDi6Pse8FHoZbzolF/eAtb1dM8haS0lW45JUkkDFdmSSAmOodKRTGrIJVhlzE1/qt ZVyNvmVBYFtu3FSjqviOiZZkGQhp5R9Mrf9FipcMD7H7+ZCrbe2E0FPB0LDG+ouC1Jcz hNYK52kFUzQPl1HKB/ZkzmutedwkTZLoHGXDXyIYoZbUcyohwhvPiHjKTu6f4fqY0rby 3pvQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=7LKk/MQUH9PrRGrgWgJu6sQTRpVfMAcu12gSHONSwaQ=; b=unNsbfQ+ebHQHt8rmFICQsyjskN44XO25s9k/i+si5zRBhmFsqXWSwkKX7TH/Z2HZ0 eaM67u2vl6Ac/+g7pUvv4mDdLSOynPu0lACY+qtaN/wFwpKOmhzJw/zeUNJ2XlW/MLR7 rt58+s4he9xBSc6o1aKQtA3E/Fuc0JelKBmRBI/aYDqKXShN/0RVNLbkG+B7KRLw+RCT weRQUx4uLxZlgJo3YfQNbiCh1QzptoKQzRolyvX3sM2/y+V76pCRD9urZy1KomtRUGfy TupXoQm4XIc58s4xWEYDVZJ3yPV23EBx4OhqXnXj4PXa5MAZZPGkZnwcQ2EFe5hUMI8J xobQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=odUBTvBV; 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=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i17si7328641ejy.374.2019.10.07.07.46.45; Mon, 07 Oct 2019 07:47: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=fail header.i=@gmail.com header.s=20161025 header.b=odUBTvBV; 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=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728059AbfJGOqG (ORCPT + 99 others); Mon, 7 Oct 2019 10:46:06 -0400 Received: from mail-wm1-f66.google.com ([209.85.128.66]:34296 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726334AbfJGOqF (ORCPT ); Mon, 7 Oct 2019 10:46:05 -0400 Received: by mail-wm1-f66.google.com with SMTP id y135so200863wmc.1; Mon, 07 Oct 2019 07:46:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=7LKk/MQUH9PrRGrgWgJu6sQTRpVfMAcu12gSHONSwaQ=; b=odUBTvBVB0ZmzaHCSWCJ6I1SZNze2wQrGoiGGMdma0cAgPHGgqm7Ii29Oo/bAVdf0D HHoPB69lGGhTa7g5KGhZFZxZFuzxG68pW6BvZcoGrALrMcc5KkfIMcHOREPQoXU7kNPK 3+MI+D3fNoV2LbKW+iNbQF1lxkuGoZqdgeL2rj0ovZdwKEGYBOey1Bzj/xomXYpxgJRf okvssGH6OEP5VKGx6VwK7V6FZVdOZNXbcmrujG7M0DYCGIZQGAYxtj4MoktfzMsMQLxW LaBB4rJdO9CXTkL6DvtVn+mv7PrkUA7fr22A5lzX4bPQxw8c+zQ+R0ga1FEu5VOMZYjI ZcMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:from:to:cc:subject:message-id :references:mime-version:content-disposition:in-reply-to:user-agent; bh=7LKk/MQUH9PrRGrgWgJu6sQTRpVfMAcu12gSHONSwaQ=; b=acDqWpKP6on/hdJ5YzoFqn9oVEJvJn3w00Xv32O23vSCLUthYSqHlsqLUGfakXWWMa YgysVSnfi4VBoB4yrmZQcnJZCVJBTM2xd5q+FK84w2tyXCnOdcYyigxccd9xcw8K0eFW 2xkbQZ0n1miETOEPafY3PkGPfoy4AWDd5cxe1OMCHjFTXxBxF/JkVEQQU8dWjPbbM3fQ PF79x+1XGZVZqk8R8xC2alJLZij6j4UWba3o5fr6ByxLuSu8Us7FPyRYOY1c7BIwKW7M uAf+FJPq7xwWs3B+3ZohtuPEur74NIhTa4J8GnjLldNWSlKy0n6w+meSVe2NA95kwJzI X93Q== X-Gm-Message-State: APjAAAUa17YgjvBKaWlPmP34spAUjxu/htto3Pkh9u5tYkuNmWtsYME7 V2cl+qE56vkFmeHqp/w/6c0= X-Received: by 2002:a1c:4c12:: with SMTP id z18mr20487068wmf.45.1570459563086; Mon, 07 Oct 2019 07:46:03 -0700 (PDT) Received: from gmail.com (2E8B0CD5.catv.pool.telekom.hu. [46.139.12.213]) by smtp.gmail.com with ESMTPSA id o9sm36091179wrh.46.2019.10.07.07.46.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Oct 2019 07:46:02 -0700 (PDT) Date: Mon, 7 Oct 2019 16:46:00 +0200 From: Ingo Molnar To: Hans de Goede Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H . Peter Anvin" , Herbert Xu , Ard Biesheuvel , linux-crypto@vger.kernel.org, x86@kernel.org, linux-kernel@vger.kernel.org, Arvind Sankar , Stephan Mueller Subject: Re: [PATCH v2 5.4 regression fix] x86/boot: Provide memzero_explicit Message-ID: <20191007144600.GB59713@gmail.com> References: <20191007134724.4019-1-hdegoede@redhat.com> <20191007140022.GA29008@gmail.com> <1dc3c53d-785e-f9a4-1b4c-3374c94ae0a7@redhat.com> <20191007142230.GA117630@gmail.com> <2982b666-e310-afb7-40eb-e536ce95e23d@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <2982b666-e310-afb7-40eb-e536ce95e23d@redhat.com> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org * Hans de Goede wrote: > Hi, > > On 07-10-2019 16:22, Ingo Molnar wrote: > > > > * Hans de Goede wrote: > > > > > Hi, > > > > > > On 07-10-2019 16:00, Ingo Molnar wrote: > > > > > > > > * Hans de Goede wrote: > > > > > > > > > The purgatory code now uses the shared lib/crypto/sha256.c sha256 > > > > > implementation. This needs memzero_explicit, implement this. > > > > > > > > > > Reported-by: Arvind Sankar > > > > > Fixes: 906a4bb97f5d ("crypto: sha256 - Use get/put_unaligned_be32 to get input, memzero_explicit") > > > > > Signed-off-by: Hans de Goede > > > > > --- > > > > > Changes in v2: > > > > > - Add barrier_data() call after the memset, making the function really > > > > > explicit. Using barrier_data() works fine in the purgatory (build) > > > > > environment. > > > > > --- > > > > > arch/x86/boot/compressed/string.c | 6 ++++++ > > > > > 1 file changed, 6 insertions(+) > > > > > > > > > > diff --git a/arch/x86/boot/compressed/string.c b/arch/x86/boot/compressed/string.c > > > > > index 81fc1eaa3229..654a7164a702 100644 > > > > > --- a/arch/x86/boot/compressed/string.c > > > > > +++ b/arch/x86/boot/compressed/string.c > > > > > @@ -50,6 +50,12 @@ void *memset(void *s, int c, size_t n) > > > > > return s; > > > > > } > > > > > +void memzero_explicit(void *s, size_t count) > > > > > +{ > > > > > + memset(s, 0, count); > > > > > + barrier_data(s); > > > > > +} > > > > > > > > So the barrier_data() is only there to keep LTO from optimizing out the > > > > seemingly unused function? > > > > > > I believe that Stephan Mueller (who suggested adding the barrier) > > > was also worried about people using this as an example for other > > > "explicit" functions which actually might get inlined. > > > > > > This is not so much about protecting against LTO as it is against > > > protecting against inlining, which in this case boils down to the > > > same thing. Also this change makes the arch/x86/boot/compressed/string.c > > > and lib/string.c versions identical which seems like a good thing to me > > > (except for the code duplication part of it). > > > > > > But I agree a comment would be good, how about: > > > > > > void memzero_explicit(void *s, size_t count) > > > { > > > memset(s, 0, count); > > > /* Avoid the memset getting optimized away if we ever get inlined */ > > > barrier_data(s); > > > } > > > > Well, the standard construct for preventing inlining would be 'noinline', > > right? Any reason that wouldn't work? > > Good question. I guess the worry is that modern compilers are getting > more aggressive with optimizing and then even if not inlined if the > function gets compiled in the same scope, then the compiler might > still notice it is only every writing to the memory passed in; and > then optimize it away of the write happens to memory which lifetime > ends immediately afterwards. I mean removing the call is not inlining, > so compiler developers might decide that that is still fine to do. > > IMHO with trickycode like this is is best to just use the proven > version from lib/string.c > > I guess I made the comment to specific though, so how about: > > void memzero_explicit(void *s, size_t count) > { > memset(s, 0, count); > /* Tell the compiler to never remove / optimize away the memset */ > barrier_data(s); > } Ok, I guess this will work. Thanks, Ingo