Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp814393ybg; Tue, 9 Jun 2020 13:36:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz0Spss/Mjm8Q0yZLvXGWlNTLM6O13DD5q+rpSf2m+lq/iMm1Eaw6fOdspaGJrRpUItuNX7 X-Received: by 2002:a17:906:3590:: with SMTP id o16mr175667ejb.506.1591735010824; Tue, 09 Jun 2020 13:36:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591735010; cv=none; d=google.com; s=arc-20160816; b=JsiXUg/dPMH6K59QZbT4eJTDS3dm8pQneHaAxHUzaTxsqSx70G5r73efui+rnk9xkr 6rtEarIBJBKQ6cqt6ScoPCbzmO1eDG1Duyhzwznnf69nzeVgWvU7Vy6Vw0U2mxPPN0e+ cTQhlHLPUe1Nyl3VTOI8GSTo/8b7F6rC55A0bqDu5sVHU9MQpYYTkoiV02X51FjA6u+s ppW0Z0dI9iXnoWY5rY8Fo2Yvow8gwNaRfHvB0R1jzSbuOy9NOoWItKOqrBYC5JEKI7vR bsd8eoagaKNHrrjcMVoh94MYE6rwcVp/9y4XYs+K+mV2WPbGY/higvaLeWJLpQpltWn/ G8Bw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=7dtLX6XP7HnlQbHJsaggWu4QmeqwjrddfqDvUbVDx4s=; b=Q2je3YUTXIiM1Y4Nu4629SNemF2K6u5kXJU4CIrTb0eyJdDW766MV2hDpbWxoxpaLq uag+uEee+ss9ig5e64/0kKNtctE/GsLB0kqIhX+R4WGFImT8Y4GcpjIW0WqBDHNXHRZp QBzQBdn5HV+hIZYdaq04Q2Hk0oqI5FgacSwzHlkZTILRij+pLGr5AhrmJ6mSJ9VE1yjc ns2jX46LwJoC669lmEo+QOudMOJrLb1XnV4yR/CFc1n8BtcYdbiVbHzSZWDoHDjjNTfr +6J5NlEj2OcXfpWjG56BEfMdH0s8SBq25xASEx+5FWe1eDrwzIOpSK44Xa2gBZun7lU4 RwBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=U99j7Jwk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id rh3si11283313ejb.539.2020.06.09.13.36.22; Tue, 09 Jun 2020 13:36:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-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=@chromium.org header.s=google header.b=U99j7Jwk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732624AbgFISqq (ORCPT + 99 others); Tue, 9 Jun 2020 14:46:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50154 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728400AbgFISqn (ORCPT ); Tue, 9 Jun 2020 14:46:43 -0400 Received: from mail-pl1-x644.google.com (mail-pl1-x644.google.com [IPv6:2607:f8b0:4864:20::644]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EB4B0C05BD1E for ; Tue, 9 Jun 2020 11:46:42 -0700 (PDT) Received: by mail-pl1-x644.google.com with SMTP id bg4so8385194plb.3 for ; Tue, 09 Jun 2020 11:46:42 -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=7dtLX6XP7HnlQbHJsaggWu4QmeqwjrddfqDvUbVDx4s=; b=U99j7JwkXu9xDjTaNwOqMNsfY/1JGbl0JiVGPVKaBXgK5sILh3SCCo6eu1pc4epTXg OFJZw9ln4TBPmUC0pW0XxrKMH3JsdoIHM3vcI+UatnP+BjixVR5jhLwwCKfajn4CK89/ LsKyztUzZULA3YetUMhR5C2itWjI8K5031e9w= 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=7dtLX6XP7HnlQbHJsaggWu4QmeqwjrddfqDvUbVDx4s=; b=qp3lFyV2oe+tWsxgyY+ZgyCawEDZM6Qy19I4DixHko6l29pFsTkV7BAhEm9kvUelt/ QFHFJZ411o0tZ3RuWS04H8DcOHpCR0UYop9a7/p1bLu6hPpG2DapXWJj4kftchdD7L9w Fdj9zSjybNtTxrY7cfxpmZAWur1mOo85EA0gYSSOTtiedQlfNB2miik5ZQKU7OhbE83i s2Sp0DXdxms7P7aycBw9O3W3WX1YDSEsCReRKdYrl+4tPzX/D/RYyuFLEfSYzDxG/Vyc rvKusXoQix8sn/xC/1TKmxvGm2hsTSMNUsitoTX4aQ1Us7ZwfNItsrqZDES4LO+aBUCZ Odng== X-Gm-Message-State: AOAM532f8+Un4LAyhhRePLpxoyhyo6e4ZVb2fiMmn+I1mo2eXxxLF4mK AFGrFJeoPMLoe6/3tb13zgYsgw== X-Received: by 2002:a17:90a:3489:: with SMTP id p9mr6357379pjb.56.1591728402460; Tue, 09 Jun 2020 11:46:42 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id lt14sm3139209pjb.52.2020.06.09.11.46.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jun 2020 11:46:41 -0700 (PDT) Date: Tue, 9 Jun 2020 11:46:40 -0700 From: Kees Cook To: Alexander Popov Cc: Emese Revfy , Miguel Ojeda , Masahiro Yamada , Michal Marek , Andrew Morton , Masahiro Yamada , Thiago Jung Bauermann , Luis Chamberlain , Jessica Yu , Sven Schnelle , Iurii Zaikin , Catalin Marinas , Will Deacon , Vincenzo Frascino , Thomas Gleixner , Peter Collingbourne , Naohiro Aota , Alexander Monakov , Mathias Krause , PaX Team , Brad Spengler , Laura Abbott , Florian Weimer , kernel-hardening@lists.openwall.com, linux-kbuild@vger.kernel.org, x86@kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, gcc@gcc.gnu.org, notify@kernel.org Subject: Re: [PATCH 2/5] gcc-plugins/stackleak: Use asm instrumentation to avoid useless register saving Message-ID: <202006091143.AD1A662@keescook> References: <20200604134957.505389-1-alex.popov@linux.com> <20200604134957.505389-3-alex.popov@linux.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200604134957.505389-3-alex.popov@linux.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jun 04, 2020 at 04:49:54PM +0300, Alexander Popov wrote: > Let's improve the instrumentation to avoid this: > > 1. Make stackleak_track_stack() save all register that it works with. > Use no_caller_saved_registers attribute for that function. This attribute > is available for x86_64 and i386 starting from gcc-7. > > 2. Insert calling stackleak_track_stack() in asm: > asm volatile("call stackleak_track_stack" :: "r" (current_stack_pointer)) > Here we use ASM_CALL_CONSTRAINT trick from arch/x86/include/asm/asm.h. > The input constraint is taken into account during gcc shrink-wrapping > optimization. It is needed to be sure that stackleak_track_stack() call is > inserted after the prologue of the containing function, when the stack > frame is prepared. Very cool; nice work! > +static void add_stack_tracking(gimple_stmt_iterator *gsi) > +{ > + /* > + * The 'no_caller_saved_registers' attribute is used for > + * stackleak_track_stack(). If the compiler supports this attribute for > + * the target arch, we can add calling stackleak_track_stack() in asm. > + * That improves performance: we avoid useless operations with the > + * caller-saved registers in the functions from which we will remove > + * stackleak_track_stack() call during the stackleak_cleanup pass. > + */ > + if (lookup_attribute_spec(get_identifier("no_caller_saved_registers"))) > + add_stack_tracking_gasm(gsi); > + else > + add_stack_tracking_gcall(gsi); > +} The build_for_x86 flag is only ever used as an assert() test against no_caller_saved_registers, but we're able to test for that separately. Why does the architecture need to be tested? (i.e. when this flag becomes supported o other architectures, why must it still be x86-only?) -- Kees Cook