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 04E74C021AA for ; Wed, 19 Feb 2025 13:53:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8A6F4440160; Wed, 19 Feb 2025 08:53:28 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8806B440156; Wed, 19 Feb 2025 08:53:28 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6F87B440160; Wed, 19 Feb 2025 08:53:28 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 4EEF1440156 for ; Wed, 19 Feb 2025 08:53:28 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id AD5671C9F17 for ; Wed, 19 Feb 2025 13:53:17 +0000 (UTC) X-FDA: 83136836034.27.8AF198E Received: from mail-qt1-f173.google.com (mail-qt1-f173.google.com [209.85.160.173]) by imf24.hostedemail.com (Postfix) with ESMTP id D0A5818001E for ; Wed, 19 Feb 2025 13:53:15 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=BClMI4gu; spf=pass (imf24.hostedemail.com: domain of jackmanb@google.com designates 209.85.160.173 as permitted sender) smtp.mailfrom=jackmanb@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1739973195; 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=UdmAiMEeKrPEDg3p+jo+2xFKJGKxXYyTYb14zoBH7Bo=; b=cWsk4sIv0x1QVGq7cZKvaiNsrKpDY1yqEYu5Ls39SlwsfepgPhfGx91eJGP41sJs56452R d9ab44Xm6rQFcFEDR2GHSbE//eH6uAjm0bw2LqJIHezDxxnvI9GM7dYOzI4Tb7S6fkUQrg hzKOT7Z0tltlRhVhBGbqAaV26gl1/2Q= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=BClMI4gu; spf=pass (imf24.hostedemail.com: domain of jackmanb@google.com designates 209.85.160.173 as permitted sender) smtp.mailfrom=jackmanb@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1739973195; a=rsa-sha256; cv=none; b=zYwxuTaViC3l9WTRXcP2K1okt4t9gC9HASRdrQ68UvYv2HAGcWd7xgMK32ee7q12zCyTis +5/zfjCYNXlYHLCD1RWJRjKBp2us0mrvyTTJamX3S8cAPQA3hr6MPaDYi1tTTQh0aFbARP mpAe6QmiyOx0FGi4OfUiFSZl3GPIu00= Received: by mail-qt1-f173.google.com with SMTP id d75a77b69052e-472098e6e75so310831cf.1 for ; Wed, 19 Feb 2025 05:53:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1739973195; x=1740577995; darn=kvack.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=UdmAiMEeKrPEDg3p+jo+2xFKJGKxXYyTYb14zoBH7Bo=; b=BClMI4gu+IL1TL7k94VHY1EczjnAU/YrZYowipNO3f9eYlme5RD7gK/g1Vz/sWqcr9 DI6oQxMUr1lf4dp0Vb9zliWGKPdyrQlnzHVsKFXbNCJloxU24nxSCzTr3JkxIci3kKOQ BePwIlRam/qh9G5EMNrzCoVs2ZmisHZ1hJqVQxOWgloz5ePJSrILEc6aONs3no8cLZ+e 5NpOwukcdAIu4/xCtZUI0CItNCOB4mfZ16w3sEMY+aDjGSPD/A1iReFeW9RfOwCEPHn8 IeLJn45PvPiewf9SN2xkZCbz/G+fZ/g9cHBi0JEW049l++Dr8vJt9tAXXfCW6lGEApSD hc0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739973195; x=1740577995; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=UdmAiMEeKrPEDg3p+jo+2xFKJGKxXYyTYb14zoBH7Bo=; b=wpf+w6asKVMUhdzqJNeDEuHtmGnDgAHbC8G1+6CXL0OVaOKWSiCry5tVR9hovXQIQw HUzsmSARGj6Vce+1bm3S8CiHauJTRzwBDoxaxLEyH25WPvMrXtmi1nRBykzWia/Qvg9o OFciWmjnfwOrr93wJjKC2MJdCVlp/zNjSieVj9Xy1YbnVLpC/Xz+zFk0uypKJZKWdW4O LVOHflwgAI7CCpX9afrWdmJrg5oncL8z6OjbP+Kg2BXaC+VRHuI+TPaYMYTNP04aOKL8 PmvyNtMN5QxI7s34JtYt/iwNUbS+sWZl8zg/CyTH7T1KNC8p4o1qPsNwZ3JACQOcbDQY hezw== X-Forwarded-Encrypted: i=1; AJvYcCWwLBIBEFtp7lPBtdeHLrrgY1PtA1boUAl2Y0h5d+LOJNL/Z1MR3oL1HaxB/IdwvW4i5AWZbV+zUQ==@kvack.org X-Gm-Message-State: AOJu0YxprYBASBfUtaemh1aCiuGtqBhilUnBfS0VSx258L8FXIYo6v8B fyynTUg1+ATpv0WaUHrzoxkCAmqU5fhN3OT3BU+4hMISfHJXMq+NBeQtyJSyCNzza6ujbevBBkc fVuzbXmRwNDmURN5urIKFeg9QQGZc6nkBnTVc X-Gm-Gg: ASbGncv/NBANBBuBzopj+MLaqOMS7YY/01o+JKkUbpbpIJ/lKcLkfirqHn1bJ7VTECE 5D8/40l4mA4Pq6o8LX9KoYNc0GLBDkl8QL2ezvToyEghlcUrqFHkOlBUXhiAP16UlGGX5t9o7pg IjRrgrNQaphtiLVo95a6GX6ntOjXA= X-Google-Smtp-Source: AGHT+IENNUmIuZuxSQylUApjIg4cKkgEiog4c3mRT0+zySA3cwktAnc8eGuiyh3/xKClKXO5dwBsFhWfn2uiV/6urfI= X-Received: by 2002:a05:622a:85:b0:471:b772:c717 with SMTP id d75a77b69052e-472097c2b32mr3540781cf.26.1739973194577; Wed, 19 Feb 2025 05:53:14 -0800 (PST) MIME-Version: 1.0 References: <20250110-asi-rfc-v2-v2-0-8419288bc805@google.com> <20250110-asi-rfc-v2-v2-3-8419288bc805@google.com> <20250219105503.GKZ7W4h6QW1CNj48U9@fat_crate.local> In-Reply-To: <20250219105503.GKZ7W4h6QW1CNj48U9@fat_crate.local> From: Brendan Jackman Date: Wed, 19 Feb 2025 14:53:03 +0100 X-Gm-Features: AWEUYZnAiIsbh62TRn4xLoxaltzDiT1dCIBY-Pnf2HWy-DCtrTT9-DxdH-ccpBo Message-ID: Subject: Re: [PATCH RFC v2 03/29] mm: asi: Introduce ASI core API To: Borislav Petkov Cc: Thomas Gleixner , Ingo Molnar , Dave Hansen , "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Richard Henderson , Matt Turner , Vineet Gupta , Russell King , Catalin Marinas , Will Deacon , Guo Ren , Brian Cain , Huacai Chen , WANG Xuerui , Geert Uytterhoeven , Michal Simek , Thomas Bogendoerfer , Dinh Nguyen , Jonas Bonn , Stefan Kristiansson , Stafford Horne , "James E.J. Bottomley" , Helge Deller , Michael Ellerman , Nicholas Piggin , Christophe Leroy , Naveen N Rao , Madhavan Srinivasan , Paul Walmsley , Palmer Dabbelt , Albert Ou , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , Yoshinori Sato , Rich Felker , John Paul Adrian Glaubitz , "David S. Miller" , Andreas Larsson , Richard Weinberger , Anton Ivanov , Johannes Berg , Chris Zankel , Max Filippov , Arnd Bergmann , Andrew Morton , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Valentin Schneider , Uladzislau Rezki , Christoph Hellwig , Masami Hiramatsu , Mathieu Desnoyers , Mike Rapoport , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Dennis Zhou , Tejun Heo , Christoph Lameter , Sean Christopherson , Paolo Bonzini , Ard Biesheuvel , Josh Poimboeuf , Pawan Gupta , x86@kernel.org, linux-kernel@vger.kernel.org, linux-alpha@vger.kernel.org, linux-snps-arc@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-csky@vger.kernel.org, linux-hexagon@vger.kernel.org, loongarch@lists.linux.dev, linux-m68k@lists.linux-m68k.org, linux-mips@vger.kernel.org, linux-openrisc@vger.kernel.org, linux-parisc@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-sh@vger.kernel.org, sparclinux@vger.kernel.org, linux-um@lists.infradead.org, linux-arch@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, Ofir Weisse , Junaid Shahid Content-Type: text/plain; charset="UTF-8" X-Rspam-User: X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: D0A5818001E X-Stat-Signature: 1pfg57or4316c5ta5if99fxx1ux5oeit X-HE-Tag: 1739973195-462615 X-HE-Meta: U2FsdGVkX18dbIgM98hrlXkm58CVjBpkAx8sdPNqt/9r+2lGZXSzfBmo5RGXi8Ts1tcuKHqmY8oFwDoRMnFlT1aJgflvp42kaRc0Bx9XhDexo0bPkit1jcWtMJkwtCgMKdvLHnqJhd8PfRQCfvs8TgfMr9eYutxSCrFwaO6QyyNSc5P5cdy+YEnHodUA+u5dFjlHoA+yLNlOprfDTI/nUSne4g+mAAz3Y6PeHn+UUCz+sltdGrYBOzwVFK37vcgQJXRW4mHPJZQgiZ+YaLHUNGD5DI1fmIF04QUsO2R04PqWxFIi6UTkilui8q4s4zkTQfrBa0b4X7NIwH0pFNGKdDHO9/dWjdO0yrqbds9YouDCYQoEEE6tc5lbTCCBDOdw3yRznMCwchPaFNDeQ/cPK8d+q7xaiirezcxWeDpsgUDi1FD0neOGOjSco7fQA0NCJEVvzQGBQC2COi7ibU7yRfV1a8WiNEqxhYHGaMhfd517+Y3oPyUMZIXT+2tVYFZF1UuAx+tQfkvrgDsUseraqssns4x6TRjVpRwsXQsaE2x9cCdjwaAd9AuFYPFqVZIE2f7PjirKIKHPThzXZ0ykMbqAxO2uOsa1KQoOHXv08+vcZC4vlu+/iDaAzOM8Br7eZ4H35SvCg873S5p1x5T19auX0BDEHoVqisIkdlRY5pm0jbmgiPymzXR5jJogbBlUOE1QnzltzOJ2pInX97DRjUoyi6ADSMzbrufyK8Qq0Y9HaImbeTgTsnuW6VxTtwSdMMkdJEKvRN5+5VovLrtEyPF9vN2yXlfP6axPRmuKljt9qeft1pSpcSfDbDiPa2rYdc/HFe9/lWP0EmOZ3K8D0o/Fa8PBazvWczObQIzrF3zgtUL69eUaCjNN1c2mJ3mFvSqFWr/tnFU0YGkx+xGhwJP5eNqrFJ/Ff1OMhFqOCr3pg4GH6zO3JYfgKcQ0/YnKQopinRFGTI42osk0tjy wYLNZdST m4KcfB1xgyngGWnQdB25KuhN/9xs2gBjPez8R7YG+RhudT3lVX59J0OWjVcRDST2o3k3l5dc2G3fERTIT3tbNFH6bcy4j37up4ThKAjnecpyy1qj14pfL2CdmSxWT+sh2n4XZ7j9YGV/7Qp/mYRPYwcnuSXYGhHP6weGphM3KEop1L9fl/EcRAMJatwweEsOobCZr 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: List-Subscribe: List-Unsubscribe: Argh, sorry, GMail switched back to HTML mode somehow. Maybe I have to get a proper mail client after all. Here's the clean version. On Wed, 19 Feb 2025 at 11:57, Borislav Petkov wrote: > > > + * Runtime usage: > > + * > > + * 1. Call asi_enter() to switch to the restricted address space. This can't be > > + * from an interrupt or exception handler and preemption must be disabled. > > + * > > + * 2. Execute untrusted code. > > + * > > + * 3. Call asi_relax() to inform the ASI subsystem that untrusted code execution > > + * is finished. This doesn't cause any address space change. This can't be > > + * from an interrupt or exception handler and preemption must be disabled. > > + * > > + * 4. Either: > > + * > > + * a. Go back to 1. > > + * > > + * b. Call asi_exit() before returning to userspace. This immediately > > + * switches to the unrestricted address space. > > So only from reading this, it does sound weird. Maybe the code does it > differently - I'll see soon - but this basically says: > > I asi_enter(), do something, asi_relax() and then I decide to do something > more and to asi_enter() again!? And then I can end it all with a *single* > asi_exit() call? > > Hm, definitely weird API. Why? OK, sounds like I need to rewrite this explanation! It's only been read before by people who already knew how this thing worked so this might take a few attempts to make it clear. Maybe the best way to make it clear is to explain this with reference to KVM. At a super high level, That looks like: ioctl(KVM_RUN) { enter_from_user_mode() while !need_userspace_handling() { asi_enter(); // part 1 vmenter(); // part 2 asi_relax(); // part 3 } asi _exit(); // part 4b exit_to_user_mode() } So part 4a is just referring to continuation of the loop. This explanation was written when that was the only user of this API so it was probably clearer, now we have userspace it seems a bit odd. With my pseudocode above, does it make more sense? If so I'll try to think of a better way to explain it. > /* > * Leave the "tense" state if we are in it, i.e. end the critical section. We > * will stay relaxed until the next asi_enter. > */ > void asi_relax(void); > > Yeah, so there's no API functions balance between enter() and relax()... asi_enter() is actually balanced with asi_relax(). The comment says "if we are in it" because technically if you call this asi_relax() outside of the critical section, it's a nop. But, there's no reason to do that, so we could definitely change the comment and WARN if that happens. > > > +#define ASI_TAINT_OTHER_MM_CONTROL ((asi_taints_t)BIT(6)) > > +#define ASI_NUM_TAINTS 6 > > +static_assert(BITS_PER_BYTE * sizeof(asi_taints_t) >= ASI_NUM_TAINTS); > > Why is this a typedef at all to make the code more unreadable than it needs to > be? Why not a simple unsigned int or char or whatever you need? My thinking was just that it's nicer to see asi_taints_t and know that it means "it holds taint flags and it's big enough" instead of having to remember the space needed for these flags. But yeah I'm fine with making it a raw integer type. > > +#define ASI_TAINTS_CONTROL_MASK \ > > + (ASI_TAINT_USER_CONTROL | ASI_TAINT_GUEST_CONTROL | ASI_TAINT_OTHER_MM_CONTROL) > > + > > +#define ASI_TAINTS_DATA_MASK \ > > + (ASI_TAINT_KERNEL_DATA | ASI_TAINT_USER_DATA | ASI_TAINT_OTHER_MM_DATA) > > + > > +#define ASI_TAINTS_GUEST_MASK (ASI_TAINT_GUEST_DATA | ASI_TAINT_GUEST_CONTROL) > > +#define ASI_TAINTS_USER_MASK (ASI_TAINT_USER_DATA | ASI_TAINT_USER_CONTROL) > > + > > +/* The taint policy tells ASI how a class interacts with the CPU taints */ > > +struct asi_taint_policy { > > + /* > > + * What taints would necessitate a flush when entering the domain, to > > + * protect it from attack by prior domains? > > + */ > > + asi_taints_t prevent_control; > > So if those necessitate a flush, why isn't this var called "taints_to_flush" > or whatever which exactly explains what it is? Well it needs to be disambiguated from the field below (currently protect_data) but it could be control_to_flush (and data_to_flush). The downside of that is that having one say "prevent" and one say "protect" is quite meaningful. prevent_control is describing things we need to do to protect the system from this domain, protect_data is about protecting the domain from the system. However, while that difference is meaningful it might not actually be helpful for the reader of the code so I'm not wed to it. Also worth noting that we could just combine these fields. At present they should have disjoint bits set. But, they're used in separate contexts and have separate (although conceptually very similar) meanings, so I think that would reduce clarity. > > Spellchecker please. Go over your whole set. Ack, I've set up a local thingy to spellcheck all my commits so hopefully you should encounter less of that noise in future. For the pronouns stuff I will do my best but you might still spot violations in older text, sorry about that. > > + /* What taints should be set when entering the domain? */ > > + asi_taints_t set; > > > So "required_taints" or so... hm? What this field is describing is: when we run the untrusted code, what happens? I don't mean "what does the kernel do" but what physically happens on the CPU from an exploit point of view. For example setting ASI_TAINT_USER_DATA in this field means "when we run the untrusted code (i.e. userspace), userspace data gets left behind in sidechannels". "Should be set" in the comment means "this field should be set to record that a thing has happened" not "this field being set is a requirement for some API" or something. So I don't think "required" is right but this is hard to name. That commentary should also be expanded I think, since "should be set" is pretty ambiguous. And maybe if we called it "to_set" it would be more obvious that "set" is a verb? I'm very open to suggestions. > > > + > > +void asi_init_mm_state(struct mm_struct *mm); > > + > > +int asi_init_class(enum asi_class_id class_id, struct asi_taint_policy *taint_policy); > > +void asi_uninit_class(enum asi_class_id class_id); > > "uninit", meh. "exit" perhaps? or "destroy" > > And you have "asi_destroy" already so I guess you can do: > > asi_class_init() > asi_class_destroy() > > to have the namespace correct. Yeah, not sure what I was thinking here! > > > +static __always_inline bool asi_is_tense(void) > > +{ > > + return !asi_is_relaxed(); > > +} > > So can we tone down the silly helpers above? You don't really need > asi_is_tense() for example. It is still very intuitive if I read > > if (!asi_is_relaxed()) > Yep that sounds good.