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 418D6C433F5 for ; Mon, 10 Oct 2022 11:13:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9B77D900002; Mon, 10 Oct 2022 07:13:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9678A6B0073; Mon, 10 Oct 2022 07:13:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 807E1900002; Mon, 10 Oct 2022 07:13:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 6F06A6B0071 for ; Mon, 10 Oct 2022 07:13:22 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 46A881C66C9 for ; Mon, 10 Oct 2022 11:13:22 +0000 (UTC) X-FDA: 80004778644.23.037451E Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf02.hostedemail.com (Postfix) with ESMTP id C4F818000F for ; Mon, 10 Oct 2022 11:13:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1665400401; 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=cjfm8RmJkrxBn+xSALMff74eWGCgI/d+h1W88CDZ86U=; b=W+JL2E1sO5U+3HHOKuqY9zuca2G5THtorYaADHSBGEbJM2D//vBYrc/DnL6bQJ3FYuyshI G585SqKhD5VTKix78MrOF/0Ns3e6EJPyfDbrNJte7Nxgf50zdVjE8RNB23YBghj3ocKAiN +bU+hKG3QmfUa3n4z5JBYsi5iPg2KLU= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-500-utO26pQtPQSNb1oVWV2XLg-1; Mon, 10 Oct 2022 07:13:16 -0400 X-MC-Unique: utO26pQtPQSNb1oVWV2XLg-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6CCC585A583; Mon, 10 Oct 2022 11:13:14 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.124]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 656F2B279E; Mon, 10 Oct 2022 11:13:07 +0000 (UTC) From: Florian Weimer To: Rick Edgecombe , "Jason A. Donenfeld" Cc: 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 References: <20220929222936.14584-1-rick.p.edgecombe@intel.com> <20220929222936.14584-29-rick.p.edgecombe@intel.com> Date: Mon, 10 Oct 2022 13:13:05 +0200 In-Reply-To: <20220929222936.14584-29-rick.p.edgecombe@intel.com> (Rick Edgecombe's message of "Thu, 29 Sep 2022 15:29:25 -0700") Message-ID: <87r0zg0w5a.fsf@oldenburg.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1665400401; 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=cjfm8RmJkrxBn+xSALMff74eWGCgI/d+h1W88CDZ86U=; b=lu8p4EkX7Sp1GxbYS4gYG+7DDB1Zh9m5rD+CR39Tj3bviExLngwEvtoyhZrz3LrsmdJuJw WSR+wulEZH1bekhFjmnbIMI9EkvgQImRuCJrefRr2TQWTZnGaLGGDRQA7Gn8xlUEy8X6pC drKhzFw2HK45Mp9YeCs8bXspnkS1Zx8= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=W+JL2E1s; spf=pass (imf02.hostedemail.com: domain of fweimer@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=fweimer@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1665400401; a=rsa-sha256; cv=none; b=1uqKzido8QPGuYQRQeE5ZdS/MmZtImDCYStLTtBoMy3cOS0ALr+tFm+jHzdyf7gzkWKP3e XAU3LEoQdIx9w6L18IUZvYdQV5M7cHqDvobBQGnE7tcpAoxtoWh+3fgjClO54LwQaGZfXo TzBMBRCBrYgBa4w47Li9MT+S/H4ZxMc= Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=W+JL2E1s; spf=pass (imf02.hostedemail.com: domain of fweimer@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=fweimer@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspam-User: X-Stat-Signature: mj1rufzr5x8josx8wttjeb41cmhia8h5 X-Rspamd-Queue-Id: C4F818000F X-Rspamd-Server: rspam01 X-HE-Tag: 1665400401-430053 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: * 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. 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