From patchwork Mon Nov 11 20:53:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13871350 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 686A9D3ABF4 for ; Mon, 11 Nov 2024 20:54:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6B3926B00C6; Mon, 11 Nov 2024 15:54:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 63C306B00C7; Mon, 11 Nov 2024 15:54:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 48F476B00C8; Mon, 11 Nov 2024 15:54:33 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 2051F6B00C6 for ; Mon, 11 Nov 2024 15:54:33 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id D3C58AD72E for ; Mon, 11 Nov 2024 20:54:32 +0000 (UTC) X-FDA: 82775016408.22.E124FE1 Received: from mail-pg1-f177.google.com (mail-pg1-f177.google.com [209.85.215.177]) by imf19.hostedemail.com (Postfix) with ESMTP id B4FED1A0008 for ; Mon, 11 Nov 2024 20:53:39 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=0Pip85dt; dmarc=none; spf=pass (imf19.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.177 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731358409; a=rsa-sha256; cv=none; b=tcNaipmNzUoLe0J6UlrOWXU9rQavGQk7bKrwuAdt8RmCeFjZ2DRJVcs4x14Om8NezYE5Nk abM5c72xg78w+FNv50ddaHaVK+X5/6Ov360OJh00x+BvNf0Q+h9sq+9+c9WRbexdj1JbDP H00s2Sxg/LXtQKCNqWbjhRdLgbb0Uz0= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=0Pip85dt; dmarc=none; spf=pass (imf19.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.177 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=1731358409; 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=zttDuuB8Jg0F5HVXQ122SU8MUC6GU3do+W5lCjsZcck=; b=jvH9jcXOcodscEQsAlF2dzyI3aM1KCAkQxv638l1dEgY+9D8F729w2sXn1Y+lk2uwiT21R gEYj2n1qNPuUtfehME7DHMnAEJcalcCUUxq5f4QD1SwStTjNl5PnqYYBhT556GJc6XrtMV 1tMPmn7SGhU8bqQlRY4sB4+hxIeICys= Received: by mail-pg1-f177.google.com with SMTP id 41be03b00d2f7-7ee4c57b037so3563117a12.0 for ; Mon, 11 Nov 2024 12:54:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358470; x=1731963270; 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=zttDuuB8Jg0F5HVXQ122SU8MUC6GU3do+W5lCjsZcck=; b=0Pip85dtHawKAUyFlJvkwhyQvXXlraL6C1c73jqZCKtl51Z2TyfeheM1chEuzwGoFP pKl5dyWavuUTBgx2CBjEJKlBNQk9YKfci/wUDu6v2LHVCchnfu29PVTodz//ptlqAiUA 1rvtAkLEl439AJnwdm55MXt7dWtJgdEWQNngn5xY6zW3aJIDdUxKzjyexvYgSbuZck9l oZ7KsU+rxgBhMbPOHysz17xx2F3Sl2q+C4On0LG7TBDReDtrhf4+9yOUkjXiSn/lDLso seR1gp+LHhwUPylJF+PfjtEm5JE9hnALDdaWFmNHKSgOhLl2OmtY3sW71cjdETVzCVMz 1LeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358470; x=1731963270; 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=zttDuuB8Jg0F5HVXQ122SU8MUC6GU3do+W5lCjsZcck=; b=IWqXde4/cGmacWR4FjVOIkm58QYOYInr89rfoorWw1FWeKVh4XgcVITENlNZmilzvb 0zYLCj02r3/I3BniwIp+sj11ay9KMSb+x/LqCJ8MHdn9PfA7KLyEfCfxH67pVwq45D62 LzkM7ZthwoECvPGC5yg71KQZS3GfkYDUbE1S2UL6sMnhcQrG7MEmmUj7dKXKsVaqEfDX 7EjEpWDSuHvOW7x0JqTCaQRwCwctXCoPJMDtxKb7S9TqNHqI/yl9k+fhDAdFhjZbCASr ifuB1XmvU990e/vXa1QvBHcw0Dh6EtbTrOfpT6sbUj9A+lzJw2Mb6UjJPOAI0xlqLB+2 huvw== X-Forwarded-Encrypted: i=1; AJvYcCX2DS4Vf0PUbfNfP6Tmhp7H7d+cp2MAEIpJqpSCUhnT3wKYaqFy3DAQ6WI3Esk4Ioo5DFjd8WOjtA==@kvack.org X-Gm-Message-State: AOJu0YzyLpzYuoyiEEnvSevakDd5NB4k+GnqtqSvGRjnDXu1FgOlqjwp zbgZpHXcFpeV3fSXbYBAAftMhXzh4agZCSLSOEBBym9vD+d1yO5ThVxbyG/v++g= X-Google-Smtp-Source: AGHT+IFcd53uKcSM2vImgVUqVtCSgX2iqqP0I62SSblxY8DhVnmUdx3hmCD82D3gLR73X/oNRmz2JQ== X-Received: by 2002:a17:90b:17d2:b0:2e2:d3e9:eb33 with SMTP id 98e67ed59e1d1-2e9b16fcabdmr19214158a91.11.1731358469615; Mon, 11 Nov 2024 12:54:29 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.54.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:54:29 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:53:58 -0800 Subject: [PATCH v8 13/29] prctl: arch-agnostic prctl for shadow stack MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-13-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@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-Rspam-User: X-Rspamd-Queue-Id: B4FED1A0008 X-Rspamd-Server: rspam01 X-Stat-Signature: 13kno3ifuw8tb3zaj7r7eehhwqmwedzh X-HE-Tag: 1731358419-12188 X-HE-Meta: U2FsdGVkX1/uBIP5hWRVmlYz4xJ0HMx0A72WLMgV/npvM6CV5tSuRBmIIztegHExCoWZE0Q9TH1daew2X1tuFyFyH0yOrCYv4ghkVpsZT26tinozUasVZdm4aSMbuWvmpm1QMl1LkWnJGTM4UbQN1aV+rnDSQMIbo5GU5tozUXSCtN4By+xP4BzWZrosFSam25gCl5bV6bKw0qMpSUAbGwmIxu0D0hlHdCAcYMMy33Bqp8Y1Vw38tDOA0k+cpPItcqK1yhzXjp3nOuZ2/t/M7/4pZeLIik59LrCdhMCoN9qx0EkKp9/SOmeDpwH+D4IiAjSe0sNO2w5erXVsosVZRVWAJYqPixPYCIGc2pk2Ugkmb6pugfzSwdDkYnpz1wm2i/D3TRnJ95Oa599L8MzhQG474ViKSw7BwUKm6nKyXtrCAUnOMyBU8nrqbv4mmy33Em6FxDAfqovUQ+e4mc6cKLYXBvxKINzmcK17D2TgxZMAgH6LMW9v9pimK78PUB7b0dM1UE5AFG7chml/6CKl3YmTfR9UuSolX3Z8XicvleeupwnnflWHLxJJmd9Y9wzP5YCxBFlQFmsth2uFaXP5U/+vuoBbFYKMIHoo41XJE+wnoCPSiG7Qltr8neHXjezvQS9CP9pd9jNz3uvmV3sFBBM+hJWU7k0VFywfh2ZHsH9orhQgmfs8x1lMIQv0VzIkfJCB3aspg7hZ025maYMTceE+CHLRy4yRZp7jIHyq6C9jZe9HqYJV4KwawVBqJxoPQvDmuWdy1/ZA7/5Bugdy9Bxh4SYT1awQPZWEpR3e76zNVTnVdqn2hsyfSEBnJqSGeWW+UzuoFqC+KDsRnw+S9ve0dm4UAG/oZXfWZlK1xH1Gc3gSvLahtaFNHWeT7TCbH9jlUok7D9Iac2XUN5R+Kh+5x43wEMUvv6mYlxeqzkiJFneGPfZ1NeLbc+kxbnTv5Va09asC7uryDg/Ah/Y 90/mkp8N 7d1ohU8sRSGmgyjBel076vYmuOwUSYTilxJFUJsEFaudVYqNdhsDp5Xvu8hdUb6CVKy3s3fcG6I8ryJb6YkWxBqChpZRVB+g7YJXYdG22pR66Z0RTBfHf6B8bwyVEM90kKQ2t2OuMxgm59eeaqHbiCEc2+vF5dJNV0wNUI5AXtFHXKGBGcTHWN0qu9JvgLgxC4mUqxOkmh4CH+/imE0yY4zp+8mkxF0L9xgfMA7qR3dCjRvmK2zOvgG9/32YtbKOfQ2iHjjrex7+8oCNr6H2gCmyXeJbBDliVjPbrTDnmHdeHC7zbVcSVkAT9KTHtuUFDM1CEwaCD+0OA+Lgz5K7RZi6cqhybY0JI0nhC09bygjy0N31O510IRB++X17qaW6OB5U1rHJZWNp3m059/RrR/20oc33DVpaIAHUHUjhkzRMqYRYTgi/9oSG6FqQJ6w02Xeu84/MfomYpEsqolFRFd+1kyO97JO2niH21d/veA2GaDjk= 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 2291c02c74d2..772516264a38 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -4147,6 +4147,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 cefd656ebf43..c42ed44e86bc 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -330,5 +330,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;