From patchwork Tue Oct 29 23:44:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13855732 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 5B41DD7494D for ; Tue, 29 Oct 2024 23:45:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 305506B00AD; Tue, 29 Oct 2024 19:45:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2B71D6B00AE; Tue, 29 Oct 2024 19:45:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 108F66B00AF; Tue, 29 Oct 2024 19:45:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id D74886B00AD for ; Tue, 29 Oct 2024 19:45:01 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 86291AC2E5 for ; Tue, 29 Oct 2024 23:45:01 +0000 (UTC) X-FDA: 82728272550.29.A69FC34 Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) by imf14.hostedemail.com (Postfix) with ESMTP id 92086100014 for ; Tue, 29 Oct 2024 23:44:31 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="0Uc8KU/y"; spf=pass (imf14.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730245341; 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=MX17XgayDE8GvInbCjC/4NqKFQNSI1sjKyctPVZ+mGg=; b=IfRJE2U+eXoA6qf+fRQgr+peQc6f5aKwu2t9AEYbZD9wAlOmvEbEQzK7SNfjF0OrerpZy+ UMXbH9FxJ2yfUvGobTyEgzAsHdJwPpmYVqOmouFvlsWZGn3Zo1HVsmp8kqHExNbehgYHqA vrotv1Jml6sUkh2k7d5B/cCP9j9qbcI= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245341; a=rsa-sha256; cv=none; b=f3wSHatsgnzv0dkWOo1WT1v/8ppGR5pu3Kq41KDlSUVD84TeqwPGpHpp6YEKsaAztqeuNS vELBwriIRK5I1iPZUHOgXbjK54SYUe59wld6THihuvxsAgS1T87E+iAhOeJgxNTZ3bNRG5 n4mXWWoWAnFhlkE4K2mT6JajuUsuiZA= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="0Uc8KU/y"; spf=pass (imf14.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-71e79f73aaeso4229402b3a.3 for ; Tue, 29 Oct 2024 16:44:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245498; x=1730850298; 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=MX17XgayDE8GvInbCjC/4NqKFQNSI1sjKyctPVZ+mGg=; b=0Uc8KU/yppNzOSpgNXbE+4+A5qNccQwlGs47m3JPxjvjb334sPF9nr8r82P0XN1ujV Uz0ZUWw8RvyoQPlKF5nI5zp0iWUX10JNKEggST4YNYsPEsc/Dag7srcqB5mhnWs3CWqs 5v4mNqniCZiEFAqydIAbKkfegAmC16a4Uqrfr3dTxaG6YbTUiFMMa5DclGMZlYHMOR8e 2jZuWc3WRA8OBrptxirozh8piW3BN4qgtmoL4pY2GN/yRJaAN8/lubQ9FC4NrpX6B2t0 PLF6Nwtd7Xk6ockqW70bTYOHv218dbWy6nU+SRJBxRO67QOxjllyw0EIuSwuHNn/es7I TR5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245498; x=1730850298; 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=MX17XgayDE8GvInbCjC/4NqKFQNSI1sjKyctPVZ+mGg=; b=gL/XAHQ2R729OdXPAA2HLtbAs94T1a3E6EKkrok4kcVJFs5vGHzvxCXxgtYIdIKSbx SYqLrKsJMMnTQT9kJl20Ijj/b8QbElSuyWwb4D1dMD9gjL1KX20gpaAkn3Lz0+zhmA8B bLx4aMUnMG2rTiLBkQm+njFKuhrGBcQrZ5msJTVpp73z3WyAkKz+gbeZ39BGPe6uRqf7 y6RMGIEkJnuaNK7qWyuJ0AidzHgNFvuGu664tbgXyv8zCEi6LPiuE1rhJWsrkFRUIk79 /QJblAt750wyLv+jyRNL+N69FXUOG+6/aXCIcz6mcEJ9bdNef3qPxxyy4l7wo6qncTdb 6MDg== X-Forwarded-Encrypted: i=1; AJvYcCWQfjLm8oHgNGRiD7/QVgxFf1cKgnvhmlFaT4K94fL0TREDKqXVal9Ob05VD3cEUt6ZNBF8tG+Yaw==@kvack.org X-Gm-Message-State: AOJu0YyaxxewrJZSGQr4s6XbNBXUnvu2oJYc7YF3ddk5k8qljpAXEQ/f z2tr3OaPh1YORw8ivR4vZXEeDvjtP0fi9OmQnM/KX3cYIxtFF/ve5zuT3+LxJno= X-Google-Smtp-Source: AGHT+IExoxYnLt1GvbT6MZVWFjKFOSbr7AEUolTL6dzHWvO6oME8gZh14LO3LJsC9IhYy2Z5JyMh8A== X-Received: by 2002:a05:6a00:4610:b0:71e:4fe4:282e with SMTP id d2e1a72fcca58-72063096385mr19719551b3a.28.1730245498419; Tue, 29 Oct 2024 16:44:58 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.44.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:44:57 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:16 -0700 Subject: [PATCH v7 16/32] prctl: arch-agnostic prctl for shadow stack MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-16-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@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: 92086100014 X-Stat-Signature: u3txu7ze7maho6io6wstuojbxt3xwnng X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1730245471-526809 X-HE-Meta: U2FsdGVkX19flp0C8GPc1AocCiILxslW2Dn1zYxaNXdBZCUyXu5l6zorr6ZSFb/p8Nh1ahteSC/nw0GYzKNKN2XNZRsfnuKXOYIwUqyJN87l+zDXEJ4Zdt17P2oUw+ccbKzZBYat4cbLbfCPXsd1vJ+kNaXxc0ixNfLS3eliboAeWseMQOEb+B4sQjBkUZcWWsJ8+1JuASXngcPeatSN/oIWiote6rDRGSDQnrmfSLgjHP4xBDI4MOKCjWPvJeJVKMmXV5e1NLF0Fzh0rB+xa2SZ94fSUhVjtRxASfoc4cd3llhhHeaZ3U6eZez3AwELJDFBzF6thjVAX7pmQS7hyI3w0PncVOMBC9PX2pn5n0vD2NO3V21AK4heM5x5lWqJhpTPYdfsxf5TIRc7L4fyuwC5SO9sZxZLipRTfc/D19T90AEtC6y5HHmmAl3W2z4STw+SPt35svNnAKrXtt5MuC2diSefvtYoTq4YeaesdCJszIPvXaUW6HGvHvKG3TZTGKXQC8Rwym5ZXOwyXcUWhORHdYCaoHog4frAU58r8cCu3eO/MBNyQBfSutwoykSfcazF/4uoNnS/EGGZJIG7xSA5LOS+EGOmEaCwxctY1J5BmcyC3vpGQILss1ha0/gxJfEmXiCSE0BG83WGmhED+L4zR5PwfRNZQ0kBDAHeIva99juCkCKJunUvQLZORlIHfQF0EqySR8MzcIhPgX3/nePQHylheUhkx5rh2VX7o5YZe1oC9DjA7UzY1P7+jQ/wSCq2fEc7hdnmfE8Ill5YPTAzlUgoDDC59k0LB6TQnkb6OJUR0Q9vey0wR3A+JE3NzPw4wCgxdsuwrFJK2qL9QNwTWmxXJYIi3PnsI75RoBgI1EUknpgPv7eKhrjxS6kHKfi08Z4zFxxvLp+NMdbzsNXiK5gewqEXGZBcVv/LGMoLwj3LC/cZgNXb5+8tcawZkA71nJ2AdpxbuNLcRjg RtP2sZcX G33jRe1uv+x0MyDYjAMq+Ia9O6OixZ9R4Gl0DWlfMMZGs6ZsJi0zYtDnZVFXb9Ioqwd+pwBgEiecFkHmQmc56LIVZafuRba9ha5lwbXUAcEdx4JuwMLjrOe5B1Syw9SOThadvYEN1srDJdtk0QmUHScyhl0T8o2LTzx5uAX/OfdDCAO+c2xFLvaTwI75G0hVq58YVyq7yWRZjGMKX7O+zlbEgHfDDaK3G8nZdw+e7+8+rmfYbW/7NY2msZqLZR8WTw7y075tr0FJb1YK6bM86k2EL4KD6cwyBjD1FL0rS7AB7jcEPSEBeOkEECRU9WkfOKyDhko2+ZmJjMoVUxT31EPRhxRk/gN4LqqxNzbRaSbUl4YBtg87HTMoQL3tWp15haguyQuvi260FHnZMzVmjtvLZ7YZiZz6eD/+2T2pVciSAjAI6lK8fxfv9XNYIaOmuWzyK/5u+bavH9qTiG1Yzpcvi+zBnHeF7kGEEqot+7bpl7RA= 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;