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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id CE192C433F5 for ; Tue, 4 Oct 2022 19:32:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EAF676B0071; Tue, 4 Oct 2022 15:32:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E5F0F6B0073; Tue, 4 Oct 2022 15:32:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CD8196B0074; Tue, 4 Oct 2022 15:32:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id BABA86B0071 for ; Tue, 4 Oct 2022 15:32:17 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 832D780E63 for ; Tue, 4 Oct 2022 19:32:17 +0000 (UTC) X-FDA: 79984263114.03.2B39870 Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) by imf26.hostedemail.com (Postfix) with ESMTP id 0B5AF140024 for ; Tue, 4 Oct 2022 19:32:16 +0000 (UTC) Received: by mail-pj1-f42.google.com with SMTP id 8-20020a17090a0b8800b00205d8564b11so13901459pjr.5 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=ZGy/22JCMJlSYlO3Xo0BU6rZdvQT9br6ch2j12RRHim/S/V+VgvI3UiJmtddQDiYkC BKqxJlHmV+FpZLbBbYIsCwW/2n23tx47McNNEqPVs7DFJk0XLN//tPux1xitWsicrCu7 0URkFjVRd+XOcALgEwDLh+EcmhXazLVb9UR5hZTHr4sp3dDivarTw8vBJYvzMkub52dw sS2aDEAJyDDImNxOXeF2dBGhKxLAenoATNwGcVLC7REzjQwTi+zMMWQylwDvyOK8jYW4 7S4FjNFipCc+3GWZ/P/3bMlXS4q6FwMkLzTspk4NkuS0IQ7PbtvcvjXOiETw3Q96UjHj DmvQ== X-Gm-Message-State: ACrzQf0bG+lHwqwTzhfmpoNW5nbfJuBMlxv2/yfgeOah1J3TMcfYwauc OIydNzZx0COAcq9AN59V3KvOTw== X-Google-Smtp-Source: AMsMyM7ZgnbRL5j5vxtAYQskFyiI9m6Jng0jRwmWtojPF0jt9sUxdX7NDJFfugkkHWlwVBeeLzBCdQ== 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> ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1664911937; h=from:from:sender: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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=2IH8tEaBNUJUhlSQeqfFFR10kAzhCNDmBQkNCBhNbwo=; b=lttI5Wwm6aiNE5+v34BiIhXuru+JbJNU+lcMjH9TlUExcDDpdY2ILasbt5V1ie0Bnn72Gc sPLww50+96ErzKofJq6YaOWoQO1KkJYnLQqQSvK9GlO600qMcCOaUzgiwvC0Xr8c8ykJkJ xgZmJpw5R1dsGP0ul+WWd4CqMrWhVLE= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=IdypEmuX; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf26.hostedemail.com: domain of keescook@chromium.org designates 209.85.216.42 as permitted sender) smtp.mailfrom=keescook@chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1664911937; a=rsa-sha256; cv=none; b=NifrfcsJINDOpG0rv9iyf3mynd0SURSjgFaDgMM7M0Ow5yQIEcUB17w2Oyb2q1oJ5CrTzm JJ1TJu6oRByf7AqfEF/MGXI5TEwTxJCn0T9KvC4P3kQgs4dTUIjAyh4EjUOE+QQcNt019c MG+cc4DBnLqw0uz0kqS9zCmlxcPSqYU= X-Rspam-User: X-Stat-Signature: x7q8acic6gwcaaa93qtcd75nsxhtekyo X-Rspamd-Queue-Id: 0B5AF140024 Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=IdypEmuX; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf26.hostedemail.com: domain of keescook@chromium.org designates 209.85.216.42 as permitted sender) smtp.mailfrom=keescook@chromium.org X-Rspamd-Server: rspam01 X-HE-Tag: 1664911936-902806 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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