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 091D1C004D4 for ; Fri, 20 Jan 2023 01:07:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 93AD96B0075; Thu, 19 Jan 2023 20:07:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8EB226B007B; Thu, 19 Jan 2023 20:07:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7B4856B007D; Thu, 19 Jan 2023 20:07:39 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 6C37C6B0075 for ; Thu, 19 Jan 2023 20:07:39 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 41353A7194 for ; Fri, 20 Jan 2023 01:07:39 +0000 (UTC) X-FDA: 80373389838.28.9F4F43A Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) by imf13.hostedemail.com (Postfix) with ESMTP id 7CAE42000B for ; Fri, 20 Jan 2023 01:07:37 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=IUA2bJA2; spf=pass (imf13.hostedemail.com: domain of keescook@chromium.org designates 209.85.216.53 as permitted sender) smtp.mailfrom=keescook@chromium.org; dmarc=pass (policy=none) header.from=chromium.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674176857; 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=fPHorCLnKFAaUXh/8WgKdTalHhbVSsdE/cMoZ2FJYp8=; b=aL6+oG21GfZYRwklCoBwRmQ4ErRsov586AfK67xuLqNuGSXeUPv/Mv/Fw752k0K76pdRo8 wmcFGauGYyyvut6FORQJqsY5N2I05iEqU1Xrue5rFaUSuWK9Ep6ySNTpujQOEq7FER1+ZR AUYeN1GAU3WnY+XOR9ZvtuGsB61fAkQ= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=IUA2bJA2; spf=pass (imf13.hostedemail.com: domain of keescook@chromium.org designates 209.85.216.53 as permitted sender) smtp.mailfrom=keescook@chromium.org; dmarc=pass (policy=none) header.from=chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674176857; a=rsa-sha256; cv=none; b=wKPXzu41X+PQWAnH61vkR+htjIrw+CAx7E7ZgdapGZltfxTlBlae5FmlVCVSXSrIeB+mFU rPdmhbKMCJiQVJgorsmRTU3a86mYiW33ugy63jtn6NcOa+ElYFpffCf1yuW3g01aiOyjOp UUu8FWKmA49f8yU0vXF9iTiOHXHw/FU= Received: by mail-pj1-f53.google.com with SMTP id u1-20020a17090a450100b0022936a63a21so7594709pjg.4 for ; Thu, 19 Jan 2023 17:07:37 -0800 (PST) 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:message-id:reply-to; bh=fPHorCLnKFAaUXh/8WgKdTalHhbVSsdE/cMoZ2FJYp8=; b=IUA2bJA2O+UUd9QC47MgS7ecQ0dFW+vV7HicI8gGMdf7pgA3kqYYezRIZjCZNw4l06 wc6H+qWCoTrdyrjFTxZXNfNGrw84DgPvPAdtkGDjrV53nzqBLeKsQDF4Kr2xzQ7U8A/Q v6CmDg1pYGdO0wh/J7t6BYvGpVoV/MTUw3boA= 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 :message-id:reply-to; bh=fPHorCLnKFAaUXh/8WgKdTalHhbVSsdE/cMoZ2FJYp8=; b=qcrTDsP7qVbR9Yo2NYd0riG0IFSsYmVltl5WWY+usAeFVSPQc8/dNElm061YfUR6NC TbvDZFaowOgB0gzvUfYbYtVResQiNMOc1lqr2EYbWB41vBiv77WS61QLeeta37zEY9r+ LW7NKcDX7xH1OEDrK9GNWcrwohpSfcVvJOEtWebWGbgQ1T68LeZTqvMkVSDvndfdQwe5 QGl31rArlUq6aSbeBDCJ8S6kXaJgUjCBmButkXsBlpLyCFBcMdKFZZwv+hvtIVdW51lR LVr+bwYUBXNsPj9FZmRexxu0MQOj6CG+Zy3wRdQWFFSZz37ghIuT1pU3Lgc0vi7o7+L5 qGyA== X-Gm-Message-State: AFqh2kr5GH6OxwifzxrMzeKqSi4He2/N59Mm5Be3KMp626t/Q/3eeF4k 37P0HjuBI/LsWOGivcfRLlJB8A== X-Google-Smtp-Source: AMrXdXteQiwZGYC6ZWkAuTK209ZRm5P4tsbVh1gdGPdLFoNByetVL9ycW3afunzU6DVSHhjAaDO+Wg== X-Received: by 2002:a17:902:bb8f:b0:194:8c5c:5c2a with SMTP id m15-20020a170902bb8f00b001948c5c5c2amr12841064pls.67.1674176856320; Thu, 19 Jan 2023 17:07:36 -0800 (PST) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id f5-20020a170902684500b0019488a36e2asm9580955pln.277.2023.01.19.17.07.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Jan 2023 17:07:35 -0800 (PST) Date: Thu, 19 Jan 2023 17:07:35 -0800 From: Kees Cook To: Rick Edgecombe 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 , Florian Weimer , "H . J . Lu" , Jann Horn , Jonathan Corbet , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , Weijiang Yang , "Kirill A . Shutemov" , John Allen , kcc@google.com, eranian@google.com, rppt@kernel.org, jamorris@linux.microsoft.com, dethoma@microsoft.com, akpm@linux-foundation.org, Andrew.Cooper3@citrix.com, christina.schimpe@intel.com Subject: Re: [PATCH v5 31/39] x86/shstk: Introduce map_shadow_stack syscall Message-ID: <202301191707.30AEEE21@keescook> References: <20230119212317.8324-1-rick.p.edgecombe@intel.com> <20230119212317.8324-32-rick.p.edgecombe@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230119212317.8324-32-rick.p.edgecombe@intel.com> X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: wj1bx5ynxtt8ush5xab9mxaoua9uhykw X-Rspamd-Queue-Id: 7CAE42000B X-HE-Tag: 1674176857-719216 X-HE-Meta: U2FsdGVkX18YLdDh63N9F7eJ24humG9UR/12rR8ms2zOZeEtrDx9sXcD2DDXtpDZm/THLed9vax4IKNg9kGRvXqx2WPdNwntFbSAVr+lPzE3/3J+ND4E/tvtqYFOe7aU1rQlcZe20YWkMMb6r5ZqVR7dgnAABbgfGnQlllpLQijjZsPkNqsU+utnXqyhdNWt7z5Qbm1kqYDMKuRJy0w7yIupMqkyyHRNhQSKEPzsJNpvMKWP6rgc4EAPd9tJmf3QWqC6oNy9gWH/WUCIyuhfa8/O/V08j2wYyqFWyxezXYHJX5D+RQrOPbJqINr5ClJyGV21x00Sz9Bc9uf6EtC3CqNDMJmoVp1Kfx29uHZpeudY89ItWIBgAt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qCYw0xCbzesbmvih+DCAKYeMImX8Wpr80yvRpaaTW67AXCDNCpPilgDUqwTOmunxnc/JzEAUO6CXogT45k0mR6uBaHXQHCkCHUAi/ZkhNqyiF4yu0Oaodxapta9dWkgpSsKaQ== 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 Thu, Jan 19, 2023 at 01:23:09PM -0800, Rick Edgecombe wrote: > 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(addr, stack_size, SHADOW_STACK_SET_TOKEN); > > Tested-by: Pengfei Xu > Tested-by: John Allen > Signed-off-by: Rick Edgecombe Reviewed-by: Kees Cook -- Kees Cook