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 2FACCC433F5 for ; Mon, 10 Oct 2022 14:19:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D4F606B0071; Mon, 10 Oct 2022 10:19:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CD7E46B0073; Mon, 10 Oct 2022 10:19:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B2ACB8E0001; Mon, 10 Oct 2022 10:19:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 97EE16B0071 for ; Mon, 10 Oct 2022 10:19:51 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 2BA82A0E21 for ; Mon, 10 Oct 2022 14:19:50 +0000 (UTC) X-FDA: 80005248540.06.BFCBC49 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf16.hostedemail.com (Postfix) with ESMTP id 463AB18001E for ; Mon, 10 Oct 2022 14:19:48 +0000 (UTC) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id A7B8BB80E6A; Mon, 10 Oct 2022 14:19:46 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 48EB2C433C1; Mon, 10 Oct 2022 14:19:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1665411580; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=iw2ZRUonbp01bjCTCictotIIEnbTkDhyNLUSJiCb1ng=; b=k2ZrqZe9Kp/93qCrQj5JRsdxaPZkhnB2j2Z7CzRqpkmKDItzE1NwYG7L3kim9tAX62/ape pLk+YjffGVMXAaGP1efxYOU1AAIykJC+W9cOeqj/SptcZpIAevmCu5Yxc61sIxT4bt9sn4 NAEC47XgkJ8NXNDPEFol4xEKUcxcJH8= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id d87b5e13 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Mon, 10 Oct 2022 14:19:39 +0000 (UTC) Date: Mon, 10 Oct 2022 16:19:39 +0200 From: "Jason A. Donenfeld" To: Florian Weimer Cc: 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 , "H . J . Lu" , Jann Horn , Jonathan Corbet , Kees Cook , 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 Subject: Re: [PATCH v2 28/39] x86/cet/shstk: Introduce map_shadow_stack syscall Message-ID: References: <20220929222936.14584-1-rick.p.edgecombe@intel.com> <20220929222936.14584-29-rick.p.edgecombe@intel.com> <87r0zg0w5a.fsf@oldenburg.str.redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <87r0zg0w5a.fsf@oldenburg.str.redhat.com> ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1665411588; a=rsa-sha256; cv=none; b=dhFEBJYrWgpBK/cRuvX8xcfwOQbsvBadJbZ8kXx9bJSGxe4sg36ZI/kGFqxM+iAHNhhg5G kUW6daJN1h14dId78U5ebJLsKHj8nuepI+01A2od1MoqAkFzkDbAS2VYBDidpzWuC21OA8 SNStBItZFZvPVWkYMJDHc7P8istf2Mo= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=fail ("body hash did not verify") header.d=zx2c4.com header.s=20210105 header.b=k2ZrqZe9; dmarc=fail reason="SPF not aligned (relaxed)" header.from=zx2c4.com (policy=quarantine); spf=pass (imf16.hostedemail.com: domain of "SRS0=YJjh=2L=zx2c4.com=Jason@kernel.org" designates 145.40.68.75 as permitted sender) smtp.mailfrom="SRS0=YJjh=2L=zx2c4.com=Jason@kernel.org" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1665411588; 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=saOJM5cPRhODg9SxyYzITE3H286dyBaMsXaXgOtscp0=; b=uzLlmosQsT4RzuKR394178dH0Se58BeM5k+BA/ywRnh5aCk2F0nadI0KvBMWNLz9natAwl 3Uf8WT59rEO6+REVmeVf1G+GX2mGjlZ4nImM1iLrR8fmGghEV84q00eazYuHnqmBa1F2lt BoYFSaqW0FTjNvJB20AzQQ6taSFhBk0= X-Rspam-User: X-Stat-Signature: w8ykdigtfuz5qq4359kfrihegip3hszy X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 463AB18001E Authentication-Results: imf16.hostedemail.com; dkim=fail ("body hash did not verify") header.d=zx2c4.com header.s=20210105 header.b=k2ZrqZe9; dmarc=fail reason="SPF not aligned (relaxed)" header.from=zx2c4.com (policy=quarantine); spf=pass (imf16.hostedemail.com: domain of "SRS0=YJjh=2L=zx2c4.com=Jason@kernel.org" designates 145.40.68.75 as permitted sender) smtp.mailfrom="SRS0=YJjh=2L=zx2c4.com=Jason@kernel.org" X-Rspam: Yes X-HE-Tag: 1665411588-287911 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 Mon, Oct 10, 2022 at 01:13:05PM +0200, Florian Weimer wrote: > * Rick Edgecombe: > > > When operating with shadow stacks enabled, the kernel will automatically > > allocate shadow stacks for new threads, however in some cases userspace > > will need additional shadow stacks. The main example of this is the > > ucontext family of functions, which require userspace allocating and > > pivoting to userspace managed stacks. > > > > Unlike most other user memory permissions, shadow stacks need to be > > provisioned with special data in order to be useful. They need to be setup > > with a restore token so that userspace can pivot to them via the RSTORSSP > > instruction. But, the security design of shadow stack's is that they > > should not be written to except in limited circumstances. This presents a > > problem for userspace, as to how userspace can provision this special > > data, without allowing for the shadow stack to be generally writable. > > > > Previously, a new PROT_SHADOW_STACK was attempted, which could be > > mprotect()ed from RW permissions after the data was provisioned. This was > > found to not be secure enough, as other thread's could write to the > > shadow stack during the writable window. > > > > The kernel can use a special instruction, WRUSS, to write directly to > > userspace shadow stacks. So the solution can be that memory can be mapped > > as shadow stack permissions from the beginning (never generally writable > > in userspace), and the kernel itself can write the restore token. > > > > First, a new madvise() flag was explored, which could operate on the > > PROT_SHADOW_STACK memory. This had a couple downsides: > > 1. Extra checks were needed in mprotect() to prevent writable memory from > > ever becoming PROT_SHADOW_STACK. > > 2. Extra checks/vma state were needed in the new madvise() to prevent > > restore tokens being written into the middle of pre-used shadow stacks. > > It is ideal to prevent restore tokens being added at arbitrary > > locations, so the check was to make sure the shadow stack had never been > > written to. > > 3. It stood out from the rest of the madvise flags, as more of direct > > action than a hint at future desired behavior. > > > > So rather than repurpose two existing syscalls (mmap, madvise) that don't > > quite fit, just implement a new map_shadow_stack syscall to allow > > userspace to map and setup new shadow stacks in one step. While ucontext > > is the primary motivator, userspace may have other unforeseen reasons to > > setup it's own shadow stacks using the WRSS instruction. Towards this > > provide a flag so that stacks can be optionally setup securely for the > > common case of ucontext without enabling WRSS. Or potentially have the > > kernel set up the shadow stack in some new way. > > > > The following example demonstrates how to create a new shadow stack with > > map_shadow_stack: > > void *shstk = map_shadow_stack(adrr, stack_size, SHADOW_STACK_SET_TOKEN); > > Jason has recently been working on vDSO-based getrandom acceleration. > It needs a way for a userspace thread to allocate userspace memory in a > specific way. Jason proposed to use a vDSO call as the interface, not a > system call. Not quite so in the latest revision of that patch: https://lore.kernel.org/lkml/20220916125916.652546-1-Jason@zx2c4.com/ Jason > > Maybe this approach is applicable here as well? Or we can come up with > a more general interface for such per-thread allocations? > > Thanks, > Florian >