From patchwork Tue Oct 8 22:36:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13827100 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 009FCCF0429 for ; Tue, 8 Oct 2024 22:38:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F31886B00B0; Tue, 8 Oct 2024 18:38:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EE2136B00B1; Tue, 8 Oct 2024 18:38:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B89CB6B00B2; Tue, 8 Oct 2024 18:38:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 967516B00B0 for ; Tue, 8 Oct 2024 18:38:38 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id A5FCE1C52A7 for ; Tue, 8 Oct 2024 22:38:36 +0000 (UTC) X-FDA: 82651900716.16.A69231B Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by imf07.hostedemail.com (Postfix) with ESMTP id 755F340004 for ; Tue, 8 Oct 2024 22:38:36 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=CmfR6lV5; dmarc=none; spf=pass (imf07.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728427016; a=rsa-sha256; cv=none; b=YaaK2AIjTU2JYu2UIX0KXjYd0qenqIcThd4vHiWIjJXI/lbTZB2W90GnI/SrgQhuGg/2vD wCaopAR8VSR3WJ3P8fV6vXuTYbPmKh+//8I6U2olKvWXDNlb+VFYyXEm5nRZ2SJL+vV43t fAVbouikH3hq8GaC7FXHtGyBMDv/nPg= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=CmfR6lV5; dmarc=none; spf=pass (imf07.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728427016; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=1UgKthNvwCJdtX42RjeHROfs17OIsFbY3pdtzFTA+rQ=; b=5oXfpB2CFoCZ/2bcJc4gMfR//JIzlyAju4jeRQdB43+8Tyc+8UCvcOIIO2UShSTZN1D3ub /Un4wAqtI0qNMS5F4AnelL2QwpYOWsychv8eYk92RHItlndux59XMNPkndTc9xX1EVwfCp +YWGdTa+zDiEo6Xl3SFnpMBuHYpcl3E= Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-71e10ae746aso1244766b3a.2 for ; Tue, 08 Oct 2024 15:38:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427115; x=1729031915; darn=kvack.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=1UgKthNvwCJdtX42RjeHROfs17OIsFbY3pdtzFTA+rQ=; b=CmfR6lV5uxu0U7Dv1vr3H+XgvH3BP6u7PjaMbwiIdtUp5Q4F7puRjFfyXweVf8c0F+ SvFXpLW6DH4SEJ4srzoUTt9cV+rfa6UXX0eKehZiSzygk+sapmkZhtv/4FZfwtwBsnPO GVJ06Ce2yP/3FgwcojS4xymxd4D9WhGfC2LB5zFFQNkGEWQmN8rWmHOniC3Uyk/bLieX nBvsMq8hWQuzACZvFaRPUdgw0loGkU0FzSKuH+2fVFQrzzqTX0RI5J0p64Yc2t0gAmTE RFRTV1yW+4hkAh3lHfFJLUKpugqyA0KV4XSNnF9/JrRUQBcpa5/Ep0OZnChEFXN0OCmw OheQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427115; x=1729031915; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1UgKthNvwCJdtX42RjeHROfs17OIsFbY3pdtzFTA+rQ=; b=AQ07sZgTl6dBYXtlYEUA3B1lIao4cfO6/LKXaEyO5stvqxzTAnL2S0k81M89Kh4Jwn dMZbCZKU6RrjMxclTQIAnzLkvvMMdbwvOqlO6ZtLexaVKqXlsQ7mQZ4x49LvtjR5NbVc RuH3FzgFrwGspdclcJvpoTt1YGqwQpvAPTkn1ni66WqX5bNAKW+J/PILnE6bhBZEJfar jYmzUlemR1sqYaWTHVDRSll2aEgf/g95mXHX57l6XOmBLlIBFgDFhBJHyZsF7BXp7gBK kwf1YXiSSAu7TbchA4xOp6vQlK+qIihySct9DveW1V2Gj6UFNdO0Az4of17EV0llNnuP YzxA== X-Forwarded-Encrypted: i=1; AJvYcCUGg9GwZiO4VHcN+NHicvzURr+5rpbPA1XQNa2PVS/4C7ESeQuEoNSlJEflys5Tpxii77aGV/8FyQ==@kvack.org X-Gm-Message-State: AOJu0Ywjsv0N3vkK/YaUaTKj15mb3Mnd11K1Rgx2Cts4P33BpzwP/hvs FyIy7hWSQ5TwxHbEANTBMqqcTnWy3e8mBoybW5alZoL+2vGsgZFJ8y3vXAUZK2c= X-Google-Smtp-Source: AGHT+IGY6A1n9vb88kLWbxlmOlKkbeCFyOVTdVthRp7tlV6ruNsdclzAw5LAouQ/eSKzZkmvFgjuWw== X-Received: by 2002:a05:6a00:8d0:b0:71d:fe40:7e68 with SMTP id d2e1a72fcca58-71e1db647efmr649067b3a.1.1728427115200; Tue, 08 Oct 2024 15:38:35 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.38.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:38:34 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:36:59 -0700 Subject: [PATCH v6 17/33] prctl: arch-agnostic prctl for shadow stack MIME-Version: 1.0 Message-Id: <20241008-v5_user_cfi_series-v6-17-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-Rspamd-Queue-Id: 755F340004 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: d7nqqkxr6hcqxrbuj9915g8geumqc4zs X-HE-Tag: 1728427116-280415 X-HE-Meta: U2FsdGVkX18DXJumDEdn9p8+70ca3ROVD/ip/hkHDLsTR6vXWIPBNggd20IdSVgiuuj6QlLQ9j5iYAMUBhbPA/+DePgW5p59krwth1abYyxaourfvHfxEHymN04Vivq08MJCdcXKycbytngJN3D5aj6oxtbsnnzsgWD4WcbiVfLotd8HC/BHI35yeZZ2SDQgQ7Fg6fZJf8mbjFTyeVG/cyfzSqE8SYG73TZeMVGp8FD4NN1uesnWt9voUY07qwgfbNm6SzNxQoqib/5EiolRQK7ZHYVk2R1TDVEXK2UbQgeCvjQCcNINKQJWnoGRldLZPPfCGsdZib3Ry1M/zi0Oru4YhAuvHMy76mMOZatsAxC5PXUXSbHhc2Bw9IMUQ0gXL/Yqe7z1AZxNhxgptO5OnCuwbr8cYcOg6NJum0IWACM0Cg7w53ZtpaU4TXREjzJYNo721u3PoHYiegSzZgSAgFRjoY24K6WMl6Jq0DhJ2XkFdiNISqponlVOueWl3raMIj0nz5zGGryC2UfLvmZ9cY9jOad9Q8u3eW9uT474sk7PDLMNzJVVRjB7uSajeYa5uD/lrJDL/P6iM6bceFaLyc3Egb879QpV3iHEWXDDSRHCXluNURyfBYbfTsQbKrVSAdidKg+yl/sKVDoAT+RvkRI1vVp7icjTLi8CG/E4BlWDpWqS+SDjdewPXUf3wIBe/1IIPG68qQ2ToxJVNkjvMT/RNX006OZnp4cHtbXZHfcvoYVqjYTfn97evrkpXMafm/rLvgvlG5M3KPfjfwM1wFBu5UBQBGuOjGb84DDv0F4KUQn4+EheHZD4HTWBx/Dk0e0ONAuxCUY8Vj7Hz8iWOJ7VwQKq+AWdqm09migOra7ShaAoNAL1aC1SYrvFs7tVeMUs6oAx9KLgER6aQIOz+OKoT4Qj5Hs9r+5NjuwXiUva5wKn10NV2l0XQqnA4Q6WaThWw+Fwlu3hjNbJOvy yqXsl97W 2fPROEPhJVzJyo4hioftgsNxvnmf2Iz3x/i90KkexThf32qc/XhSXVfQqVrYCTP0QMHZTRYHfu4FYv2QoY8cUR8xgtSHC0s4NUVQHNMKwXq3CtyNrA6v79nGpd4xpOQNvGEi0Oi07jUkxtvf5LAWGAmLa0umSFpZR+OtUbapwnbeZRY4AQ0rgkiFYSXlTexmqA0Rg8AhJtH0Ky8dCbMPl2YdlgRbjbu9/v7YHwyBfvSEMFS2z0Nx4x158K3Z9kdAVu7tx0mxwkCt9Nd1W2kFYUZt82SlS+ce1eRq6ZbRGzGWwKspwAD9C50PjsR3/1HwFyOOT1j+pRTua8F3I/0EEd2UlaatnQcgaO+++Nrbh4gas8fX7gNPe/RI+jeoJJh4HbG8koVNyB2OHNz3cgKjCpN2OJVB9QO/yKq+K5eq7h8eKSAmY16Gor6WATFRSEWnq2CEAzj0CYLZrrJ35KEihDBYXDane3rPUwjHWpXU+uPaIISPtDC2Pvnc6Mj/n5YsHikRkVaGttWjf7ITJXfu0sK2+2himn2YLcETanxplrBV7aXe07ZrxFQkS/Jt1cMLq5EFJhZp8qmC1lH3yNSTQr6+tuZqJxmvhwxNm 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: From: Mark Brown Three architectures (x86, aarch64, riscv) have announced support for shadow stacks with fairly similar functionality. While x86 is using arch_prctl() to control the functionality neither arm64 nor riscv uses that interface so this patch adds arch-agnostic prctl() support to get and set status of shadow stacks and lock the current configuration to prevent further changes, with support for turning on and off individual subfeatures so applications can limit their exposure to features that they do not need. The features are: - PR_SHADOW_STACK_ENABLE: Tracking and enforcement of shadow stacks, including allocation of a shadow stack if one is not already allocated. - PR_SHADOW_STACK_WRITE: Writes to specific addresses in the shadow stack. - PR_SHADOW_STACK_PUSH: Push additional values onto the shadow stack. - PR_SHADOW_STACK_DISABLE: Allow to disable shadow stack. Note once locked, disable must fail. These features are expected to be inherited by new threads and cleared on exec(), unknown features should be rejected for enable but accepted for locking (in order to allow for future proofing). This is based on a patch originally written by Deepak Gupta but later modified by Mark Brown for arm's GCS patch series. Signed-off-by: Mark Brown Signed-off-by: Deepak Gupta --- include/linux/mm.h | 3 +++ include/uapi/linux/prctl.h | 21 +++++++++++++++++++++ kernel/sys.c | 30 ++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/include/linux/mm.h b/include/linux/mm.h index 57533b9cae95..54e2b3f1cc30 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -4146,6 +4146,9 @@ static inline bool pfn_is_unaccepted_memory(unsigned long pfn) { return range_contains_unaccepted_memory(pfn << PAGE_SHIFT, PAGE_SIZE); } +int arch_get_shadow_stack_status(struct task_struct *t, unsigned long __user *status); +int arch_set_shadow_stack_status(struct task_struct *t, unsigned long status); +int arch_lock_shadow_stack_status(struct task_struct *t, unsigned long status); void vma_pgtable_walk_begin(struct vm_area_struct *vma); void vma_pgtable_walk_end(struct vm_area_struct *vma); diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h index 35791791a879..b8d7b6361754 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -327,5 +327,26 @@ struct prctl_mm_map { # define PR_PPC_DEXCR_CTRL_SET_ONEXEC 0x8 /* Set the aspect on exec */ # define PR_PPC_DEXCR_CTRL_CLEAR_ONEXEC 0x10 /* Clear the aspect on exec */ # define PR_PPC_DEXCR_CTRL_MASK 0x1f +/* + * Get the current shadow stack configuration for the current thread, + * this will be the value configured via PR_SET_SHADOW_STACK_STATUS. + */ +#define PR_GET_SHADOW_STACK_STATUS 74 + +/* + * Set the current shadow stack configuration. Enabling the shadow + * stack will cause a shadow stack to be allocated for the thread. + */ +#define PR_SET_SHADOW_STACK_STATUS 75 +# define PR_SHADOW_STACK_ENABLE (1UL << 0) +# define PR_SHADOW_STACK_WRITE (1UL << 1) +# define PR_SHADOW_STACK_PUSH (1UL << 2) + +/* + * Prevent further changes to the specified shadow stack + * configuration. All bits may be locked via this call, including + * undefined bits. + */ +#define PR_LOCK_SHADOW_STACK_STATUS 76 #endif /* _LINUX_PRCTL_H */ diff --git a/kernel/sys.c b/kernel/sys.c index 4da31f28fda8..3d38a9c7c5c9 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2324,6 +2324,21 @@ int __weak arch_prctl_spec_ctrl_set(struct task_struct *t, unsigned long which, return -EINVAL; } +int __weak arch_get_shadow_stack_status(struct task_struct *t, unsigned long __user *status) +{ + return -EINVAL; +} + +int __weak arch_set_shadow_stack_status(struct task_struct *t, unsigned long status) +{ + return -EINVAL; +} + +int __weak arch_lock_shadow_stack_status(struct task_struct *t, unsigned long status) +{ + return -EINVAL; +} + #define PR_IO_FLUSHER (PF_MEMALLOC_NOIO | PF_LOCAL_THROTTLE) #ifdef CONFIG_ANON_VMA_NAME @@ -2784,6 +2799,21 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, case PR_RISCV_SET_ICACHE_FLUSH_CTX: error = RISCV_SET_ICACHE_FLUSH_CTX(arg2, arg3); break; + case PR_GET_SHADOW_STACK_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error = arch_get_shadow_stack_status(me, (unsigned long __user *) arg2); + break; + case PR_SET_SHADOW_STACK_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error = arch_set_shadow_stack_status(me, arg2); + break; + case PR_LOCK_SHADOW_STACK_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error = arch_lock_shadow_stack_status(me, arg2); + break; default: error = -EINVAL; break;