From patchwork Tue Oct 1 16:06:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13818420 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 B76B0CEACE4 for ; Tue, 1 Oct 2024 16:07:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C343E2800AC; Tue, 1 Oct 2024 12:07:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BE4C7280068; Tue, 1 Oct 2024 12:07:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A10C72800AC; Tue, 1 Oct 2024 12:07:49 -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 7689B280068 for ; Tue, 1 Oct 2024 12:07:49 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 258CA161536 for ; Tue, 1 Oct 2024 16:07:49 +0000 (UTC) X-FDA: 82625514258.06.6455EF2 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) by imf24.hostedemail.com (Postfix) with ESMTP id 301FA180008 for ; Tue, 1 Oct 2024 16:07:46 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=TAhvKiBt; spf=pass (imf24.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.175 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=1727798739; 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=x54qChiPtRwwjf8IO6kKG96mNjK0QKs021qHizpxCPM=; b=4gU1iZedBfPIUb7wArIEJYA8iACEfUKHUwJHEyEh3yhNZt6gjc5GhPQxwIxHsXcJ28J7qk gdSsLr46KN/6N8aofxU8qSEa4ak2ufV2WythHHe1pkFq97g09WWSAxhTp2c9ZSqX5+4voL LkPAwAcaAVM4MXcdvC20toWm/INmB74= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727798739; a=rsa-sha256; cv=none; b=aAuFjKQ6cd6TbEcbGhD3IY/YFcrwL7YiQbxgljN5jbmj/MjtOr+mzQuWmgGjldZBxdi13c Z7+1I3FVIa6TgarlDLvPhYusxHY1s1P/+JmuKynGmHD7tDUh3sZBct3fu9CgKWguS/Ilpt L5ExftKBnDY4yH8X1A/gotggdCTMTHo= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=TAhvKiBt; spf=pass (imf24.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.175 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-208cf673b8dso58108095ad.3 for ; Tue, 01 Oct 2024 09:07:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1727798866; x=1728403666; 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=x54qChiPtRwwjf8IO6kKG96mNjK0QKs021qHizpxCPM=; b=TAhvKiBtBkqD8a4GddUkFi2PZ7S95b0CZMCQ80RbmejCMEhxWIKfBauWIBgU8e9c49 2fByUPJDxHZTuBsH7h6vih5jG92j/G39bqZm+om+sodp3sXKK+6z+hOAvKW3iEWYO5Yy Tcn1RRP9qe20MXbUJJF4nOH//Oum2mSuNF9VGSs0HZK/1rg4BGnmJZQgfAhE/Rv9rP1h OYcZCui1//X5/x5+HvJ5IGmzf8Wbr4JTnpAu/XW2JNkbYNbjIooDXsFVlMWY6G42v8iQ je0YDtiuoZvkROikm6KmTLJdi3lN4jG3SB6WzLMVhkP1F3aj0HmWVwUeGGmcAsFNfQd/ g3Pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727798866; x=1728403666; 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=x54qChiPtRwwjf8IO6kKG96mNjK0QKs021qHizpxCPM=; b=P8nRQlN3XZWvB9QDjxGP0iyuOYij8pLUN3ZapKzxE0H61pGhQ9Ng9JB+LcbbxkhFs9 U1C7Lh8hMerBFMZC7hNO+qI7jptv4jZVFeYlTeCWlnCVuDfc5Qk1ZTeMtnFcMO7bZYOl r93RvF4jocgYlKgJ1sIHa7eb09UKDAMsyOrWyxMOiuTWYOux63nkKHzqTMfv1vX1MD6G nGMZW71RA9GSMrWXhS/sMeHs+6/8/YwJcQfw/9Aj2h99zn3mGbZp6KINFNn6YLpbLxVL TXCjp4hhGukVayQUeizOMV9r+7EMxfy56b7NYrdL5hBloWAOI+jV92qXMt9Iaygtz+KB 1xsQ== X-Forwarded-Encrypted: i=1; AJvYcCU7d8X7rDsjBsmCnLI6PUxe2EsSapqcLSB5/9P520UHHIRNg9DOPt+FxHAVj4JoHF7ezRlgkIg+yw==@kvack.org X-Gm-Message-State: AOJu0YyMwdXxWW4xhTZ6k6LqpMftLhKcdWTHDLM/n34QPan3w4Swp4UW 9FFzU+vlgeukJS7kWqr3buy915wnAjFVSFakTj/3lkGiOSi/T3yHAwRp/8JVovM= X-Google-Smtp-Source: AGHT+IGT8RHr/5+JTQ0k1Ky88XH3Z10xIEYuIYmcsWYD4PFQgSTYOHCrvexQxCleek4Q7uoQPWRCCg== X-Received: by 2002:a17:90b:202:b0:2c8:6bfa:bbf1 with SMTP id 98e67ed59e1d1-2e1846e8b42mr242090a91.23.1727798865872; Tue, 01 Oct 2024 09:07:45 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e06e1d7d47sm13843973a91.28.2024.10.01.09.07.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Oct 2024 09:07:45 -0700 (PDT) From: Deepak Gupta Date: Tue, 01 Oct 2024 09:06:22 -0700 Subject: [PATCH 17/33] prctl: arch-agnostic prctl for shadow stack MIME-Version: 1.0 Message-Id: <20241001-v5_user_cfi_series-v1-17-3ba65b6e550f@rivosinc.com> References: <20241001-v5_user_cfi_series-v1-0-3ba65b6e550f@rivosinc.com> In-Reply-To: <20241001-v5_user_cfi_series-v1-0-3ba65b6e550f@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-Stat-Signature: bbnzaonhiaq3qyrnc6tfypuck7fi74xb X-Rspamd-Queue-Id: 301FA180008 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1727798866-142347 X-HE-Meta: U2FsdGVkX18c57JwS4vbbFZVG+Lvyz1mGi11xkeFzj5UZEtNCNomWDYMWy6ytz1IptKz/suak/kXE/rC/u2PeSAf0laLsCZxOKWmghsyvZ+2zEj4Vl33rMTw/bz+LRtZ4DtRiI1K7hD+/j9xyoX7sPa3WTPRBbNSa6UtdwFr87hGDqR07djDWEFWjkJnJPiFCW4V+x+p5a8zywoXcqEASnwJzXtOM9yahyLX4Bf/HMApwZmoCT+ckawQo2Wn1RxqdRFzc08KQMIoDpRkWpGqE1j6lcDH4ybQOaaXRMrFhjgjFcErX+zdT6W75vllqsVuNem2MtffXjsDqjJdhB5fjoDqf6KmB6Vf3ODijA4H5HSo3slP8JE/aFAfPy7v5zX4ZlfriZJpLIl+DgoBqIcvfmJzwNzzGE5CTBOXr0wKHVjsd7vmiGuQRBozJ0o0boVX9VEYwVmZfbPKbcLk03e4TimCOYpOkDowoKsXIbPSZQlu0IFd1ObnU3F+GWh+QVSMpm00q2I/hUOpD3nIQ+cec+IyxJmRj9Bs9w7Yjw6UMJf9MTvp9dygqhKCN7f3ryB9l6fzj4Ajhs1Q0SKCul1ljQTj7h7P307khcsnDwO+tO7EmBAjph/E+N7/s9YTqJbxKywa86y4I5e73raGIi2Ge4XpwCxuBJ6OyhpOGdDvtTgj9RFc8o1XVHLn0TW8wW4J7bUS2hCH3aqP/D77/pIoN+Tr6HVq1AVokkPRkaLWW/ErtbwBnfP/Zor4An9KOkAzTxsWglMiCTVXlWBCQHnAiy2i89rY5E6z71tAhq6j4JVX90pjNrhCffFV2hvuQfpa3xxqAIfWhH1mQLFbQrzrBEEOBqr1Om5J/bItpeG7D6wCRgYhuPJfK/xAEivADUAJAbbLf0e5aJia5fsH7kWnTZVIhirg/3+3ANVqcaTDO19AzAgsHUBe/bv9pPktjiOPCT07/Gn89K7stl1RTj+ lXcfrK7K +XJZY3QMZ+XAD77LzatziyFtVqGS0DDeY9bBBnwzFdDh7yKOx9A4837IrNi11OBWlFhgrbD+W2yTeZS2HoxPkY104TqIwDTUI41H4NgeORCrobml9Na9e84AY0La7vGj9XakrBQOo8gIouSYOFbddY9D+EtK/C8+fwQhIrSjvz8EG1v/fGjUjDtPCa1odDw0iW2kuibp8C5qObohHQyd/45SHXrvoYofyvVdIYDoQk7c5sGraAxkQg4B8Gpw1kpf1FBxc8Rhd4Ye1sx6zdzYPW1vavauw+hgUavFEnpsnxKaZEIc5hW6ijg8jZVZ6uHkIs90TK20iDQ23olwjbdoFbHBstEaG4RQqKJRqxLy97Ix1tMhYzcr5GZcFmPKmck9+5VE7P5ZhiQIj3eQxNy+TGMYPFwj5/wQtjtPnlwJ/j3/O5eeUsMlumOXn9Fc/72FInV66279N6ow7eICuV05EpDm1pT9dW0KqnlLwGzbJ72rX59U= 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 Co-developed-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;