Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp927053rwb; Tue, 4 Oct 2022 12:40:35 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6XmOtmVv1PW5gMA/Y6eGtHhrmZFlfxfbmqyEbFaSQX/HSU+YVQy7IPb77t/kkVluesWGv6 X-Received: by 2002:a17:906:5d11:b0:787:807e:5b8a with SMTP id g17-20020a1709065d1100b00787807e5b8amr20498070ejt.559.1664912434842; Tue, 04 Oct 2022 12:40:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664912434; cv=none; d=google.com; s=arc-20160816; b=oQBVXkqh46E1DqsS8taWnWnZ7U30kw31y17O3PhkrVhjSyLlpgQYsYBjbloOaJJZI+ 9+3w15/8D1R5ZQSDlih73oeHciJATKs8FrrItO2Nlf/M4lk0r28YTQArG5o8IlVz4TTx QyX1xk3740AsaBashxD1/J/ra2fvZXejQR0tHiI9YVv4vnBChhi2gmguPCCthJg8hGk/ Yyd4/vnWGILSbhyrvY+HAzg12oAHLO/vnoljqWtiCVY7FeYKub7zA5ryZVVs6LsBue65 XpdzZu/4GyDfWEtnJDMfxKvpHYLQL51UTiQPEIWBa1zReUPM+D534zCUY+zcqhtJfahd XA8g== 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=2IH8tEaBNUJUhlSQeqfFFR10kAzhCNDmBQkNCBhNbwo=; b=ZGkfj/x+nDud2fgjaMhg49TWfCWEoCVNIPwmNqcYkDlmBCjBOJPJ2vaLzv79GoyuAV OGyEiMFk6q6nmJpiXoVxuV/DAvnjOXv4x5HzXRpIKEuGp2uzVYiFFAg8r57cOv5N18ZI v6DqxD7/E6X3M4eHJNQ/HEpQKBhopa6/YJ/lUukB8FypIm+jaVw8smg9rSKIsshJ9uXu FKjrTMEYTqeD6nb9VFvbZJCHg59gR0/6MY7dAESF4M9fx47fHRB6J9F8BPPL4CKlSpzr tnio8rBrbPIiIQdQ1l0CYYzUlk3BtN6v3GJ6xfodXc0CgsOSUWfG0nTsP83HFK9LpElo IzCg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=IdypEmuX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id dt14-20020a170907728e00b00789e061a56fsi8535411ejc.98.2022.10.04.12.40.08; Tue, 04 Oct 2022 12:40:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=IdypEmuX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S229525AbiJDTcU (ORCPT + 99 others); Tue, 4 Oct 2022 15:32:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229623AbiJDTcS (ORCPT ); Tue, 4 Oct 2022 15:32:18 -0400 Received: from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com [IPv6:2607:f8b0:4864:20::1036]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E541A696F6 for ; Tue, 4 Oct 2022 12:32:16 -0700 (PDT) Received: by mail-pj1-x1036.google.com with SMTP id 70so13714271pjo.4 for ; Tue, 04 Oct 2022 12:32:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date; bh=2IH8tEaBNUJUhlSQeqfFFR10kAzhCNDmBQkNCBhNbwo=; b=IdypEmuXWawFBdcHvEhd6ksx6DklfESbyWquWbCkuj85gFh8vAi3YTFtL6x1TFDBdo 4vpS7F1oLY/TRy8lXUYx/Tzbe3wKWlW2rY647p4DR2Zx0xz9Vmk2lZntCiHnmMqPhacs S9dn+pkGTWgmV8c5ZA8O42VK4MOFeIf7uXPI0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date; bh=2IH8tEaBNUJUhlSQeqfFFR10kAzhCNDmBQkNCBhNbwo=; b=NVSItPFDHKdgdVR0zf+9PLTYgX0cRV7aB1nOTfbP84wnYgEGjXmfu6DxUOVWBKUCKc LnIyoXzp+0lxvwXLAmDoDrCbSLTb9sNadVNcHbqNdfMxyDl2Wwx+ht0EaOB3KSNQOobz tbcLvh55HI9SpeFw412xmNVcdV3/n2hyNJQxaJKqCMUAbfw4ME0rBId5UclUWr4TliJR TW0QxGDVyVm3X+1cl+hx/Zi/uBCd6DhTNdywPWfp29f6Ba/Qj3+N2oPK1rJQxgRvSZjj k6d51wMBAyMnebMHNjp8B7T0cxVdZJx2mhuRWVf703zv/FO5PcNF1OCQo+D4oiXzAkhC cpkQ== X-Gm-Message-State: ACrzQf0yFX4ZCMXs5S5/recdKLDRlTI2GOKdkK/WK9u3iWLgA8Raj8ES zpZd4Lv/c2DgRPfdJyfmcF00vw== X-Received: by 2002:a17:90b:380e:b0:202:d747:a044 with SMTP id mq14-20020a17090b380e00b00202d747a044mr1255562pjb.170.1664911935982; Tue, 04 Oct 2022 12:32:15 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id a4-20020a170902ecc400b00179f370dbe7sm9265341plh.287.2022.10.04.12.32.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Oct 2022 12:32:15 -0700 (PDT) Date: Tue, 4 Oct 2022 12:32:14 -0700 From: Kees Cook To: David Laight Cc: 'Dave Hansen' , Rick Edgecombe , "x86@kernel.org" , "H . Peter Anvin" , Thomas Gleixner , Ingo Molnar , "linux-kernel@vger.kernel.org" , "linux-doc@vger.kernel.org" , "linux-mm@kvack.org" , "linux-arch@vger.kernel.org" , "linux-api@vger.kernel.org" , Arnd Bergmann , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H . J . Lu" , Jann Horn , Jonathan Corbet , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , "Ravi V . Shankar" , Weijiang Yang , "Kirill A . Shutemov" , "joao.moreira@intel.com" , John Allen , "kcc@google.com" , "eranian@google.com" , "rppt@kernel.org" , "jamorris@linux.microsoft.com" , "dethoma@microsoft.com" , Yu-cheng Yu Subject: Re: [PATCH v2 24/39] x86/cet/shstk: Add user-mode shadow stack support Message-ID: <202210041229.99F8CB38B@keescook> References: <20220929222936.14584-1-rick.p.edgecombe@intel.com> <20220929222936.14584-25-rick.p.edgecombe@intel.com> <202210031203.EB0DC0B7DD@keescook> <474d3aca-0cf0-8962-432b-77ac914cc563@intel.com> <4b9c6208d1174c27a795cef487eb97b5@AcuMS.aculab.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4b9c6208d1174c27a795cef487eb97b5@AcuMS.aculab.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Oct 04, 2022 at 10:17:57AM +0000, David Laight wrote: > From: Dave Hansen > > Sent: 03 October 2022 21:05 > > > > On 10/3/22 12:43, Kees Cook wrote: > > >> +static inline void set_clr_bits_msrl(u32 msr, u64 set, u64 clear) > > >> +{ > > >> + u64 val, new_val; > > >> + > > >> + rdmsrl(msr, val); > > >> + new_val = (val & ~clear) | set; > > >> + > > >> + if (new_val != val) > > >> + wrmsrl(msr, new_val); > > >> +} > > > I always get uncomfortable when I see these kinds of generalized helper > > > functions for touching cpu bits, etc. It just begs for future attacker > > > abuse to muck with arbitrary bits -- even marked inline there is a risk > > > the compiler will ignore that in some circumstances (not as currently > > > used in the code, but I'm imagining future changes leading to such a > > > condition). Will you humor me and change this to a macro instead? That'll > > > force it always inline (even __always_inline isn't always inline): > > > > Oh, are you thinking that this is dangerous because it's so surgical and > > non-intrusive? It's even more powerful to an attacker than, say > > wrmsrl(), because there they actually have to know what the existing > > value is to update it. With this helper, it's quite easy to flip an > > individual bit without disturbing the neighboring bits. > > > > Is that it? > > > > I don't _like_ the #defines, but doing one here doesn't seem too onerous > > considering how critical MSRs are. > > How often is the 'msr' number not a compile-time constant? > Adding rd/wrmsr variants that verify this would reduce the > attack surface as well. Oh, yes! I do this all the time with FORTIFY shenanigans. Right, so, instead of a macro, the "cannot be un-inlined" could be enforced with this (untested): static __always_inline void set_clr_bits_msrl(u32 msr, u64 set, u64 clear) { u64 val, new_val; BUILD_BUG_ON(!__builtin_constant_p(msr) || !__builtin_constant_p(set) || !__builtin_constant_p(clear)); rdmsrl(msr, val); new_val = (val & ~clear) | set; if (new_val != val) wrmsrl(msr, new_val); } -- Kees Cook