From patchwork Thu Jan 25 06:21:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 191864 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2553:b0:103:945f:af90 with SMTP id p19csp1454623dyi; Wed, 24 Jan 2024 22:35:48 -0800 (PST) X-Google-Smtp-Source: AGHT+IGw/74HeU4dmkg6wLPtwTzd3qf7trrSCDQiBu4NK4Nq6JQsAGxEQs9BfLoCMY0fJsN2Q/1p X-Received: by 2002:ac8:7f55:0:b0:42a:47f4:ebd6 with SMTP id g21-20020ac87f55000000b0042a47f4ebd6mr670826qtk.66.1706164547873; Wed, 24 Jan 2024 22:35:47 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706164547; cv=pass; d=google.com; s=arc-20160816; b=GBu3jauaTrsqx43llcNxHSfKZOcKd2/W+kdEOXoZ1HcbsBY/PPAw2wRLUW49WQfcTf vN0gCZT+r0WAj4UuVhc4Pk6vwj/lATKRLchvC41z1eREHTVeXGN6jdW2cOiVi4Gng9uU r+ew/NisxJvI+d0JrF/uX+TbkL6reZeOfLh0rVP3COGNcjUmaapqVMdrwmAb2BWYIBnB /INR8FJ2uDKpqE1BUIcx7Dta8t1IP3BaanO3dfrbAGmr+qWv4k2p2VVSx+QQpGQneth5 KfToZC9G3uKWOmAe/87yrNVTJS7xBchqV50S7VrPlI8fhnv77wEGpVjUteOJEoOGAM5f 67nA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=5zflxddcDQBcU2vSu/tVz8rqPQxJomCL53KP/QLCzbU=; fh=oy7V1wWqiaug32hsWRXm98kKNp5NFPTWbY7PiGr4deM=; b=y3ASgcXJuB5xGoQBmnXhHegteoX1mdh+1Ms+r4Obr3Rh3IQ88cBxz1PLvBygiqsM4V YJqLTNc00IKCTr5q+kkpOixqAvBSPR+5p742jyz0K/q3SOzVXqDIlZVx8eOhqFKriIp1 BeLcqXvA218igbCpNqcM8eVOX8JT0OwvDq8+Amgmc/C6BEww5P2Sdq64KFqHVpfaEzX+ iKfpFqAEF5tN2iYLM5E1Yl55goEvY43kmlFVTaBsuxjYQP2V5Ak/f3hx6+Nr7d+/dvq4 0cAXztzQZwhON61sQijMB357nk7FxWX+Yfs+UG1z+bzA7a8SjvSwahVigDmKANmI7B88 ye5g== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=tumVLHYM; arc=pass (i=1 spf=pass spfdomain=rivosinc.com dkim=pass dkdomain=rivosinc-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-38025-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-38025-ouuuleilei=gmail.com@vger.kernel.org" Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id e12-20020ac84e4c000000b0042a64074511si2264644qtw.524.2024.01.24.22.35.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:35:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-38025-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=tumVLHYM; arc=pass (i=1 spf=pass spfdomain=rivosinc.com dkim=pass dkdomain=rivosinc-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-38025-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-38025-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id CA65A1C2145D for ; Thu, 25 Jan 2024 06:35:04 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9D4EA11198; Thu, 25 Jan 2024 06:30:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b="tumVLHYM" Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 45C311B96B for ; Thu, 25 Jan 2024 06:30:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706164201; cv=none; b=DkLMddvab20UqvFZAawwXLPfx92bx3Do/R8+SCgPundzcppPiXZMW/CTfcNWph3MdQh1bwI7n82tffuVqQAdkuljm/1K3sNX3fqew5MDUk6vrn0etOqQcbogdMQ+yElqkIxQY3wTSuAgWVsvi3g4vjcLHDA3mI80gode0o5Mjvg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706164201; c=relaxed/simple; bh=zsH+L4+5U31JOanxoSfD/HI65SbmiKKd2XZ/ZrrZKO0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YxgdigiIcD8OLz0UN4Xtugk4KsrN1R/HjBGFf1R+R51GWRixP8b9L3iSsuKsQ39g+7gSTVV7Oj2Qx7GIKS9e/scyaFmXGdDvGRkuaru0Ep7hl8SRNXehbpt3EajCb7EfJFm3BoO3yOd1964e+B7xQUkSLc4fyTgEK5NEgF87Qmc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=tumVLHYM; arc=none smtp.client-ip=209.85.210.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-6ddcfbc5a5fso193059b3a.2 for ; Wed, 24 Jan 2024 22:30:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706164199; x=1706768999; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=5zflxddcDQBcU2vSu/tVz8rqPQxJomCL53KP/QLCzbU=; b=tumVLHYMRZNpwkx+IOHWvPpjC+R2gtSJtjnI78ZSjVpTWIllIdFm4fPMx/DnFffsNG 6VdIxxf78PH6bDNlM5zxnNZcW8CwYYftu2diMxujYBphdOwixpvuS1AWRF2N/idR//jI QTaECaVLCq+PSkv17QmMlxmwPZw5bWYj268RRoCqommGeCHR5SdctVOEsq+Cm28QaaIj vtM/ENI3zhH0vL7gy579Kyrmpy+g/sL0ccrV1u+yXSgMUYDDVTH+9H2VEdwm6OwAnOK/ X7nmyJgFVII9z2i9zrsgo3X4jjZZqF8/AUMqzm74MqKbrYzmsbNE6O9szw6yU+XTOcrT r1Rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706164199; x=1706768999; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=5zflxddcDQBcU2vSu/tVz8rqPQxJomCL53KP/QLCzbU=; b=nGM6/RZRvISRs50cxtSqOICO6UeboOz/riZXjKXWGBt6b7EGx9g/ASByEM+uunGeWo xalIIQa+XYwp3hvOFkAtFLkSJMa5WK6FoHzEAvHhwp9070HiOS2hfRIBqQL5/7SchAZ3 EKGgEHxwNGfK8vIfYgY95yJpQj+8FJzWtCPZInab7iUHLd2iHJGxnOmG93/XmGmnUQxx SwglXKEF9K5wvssneXOXqytxQ+KT+n57SzQ80hhEz2ON77k0Fi+6G6VAVr8NivHbDrC6 SBoU4qFInpoPUdYBIb+M2wbXX2GJfDq3NudyWb5/xruuqud2nfvXFqUqzEvT37TdwTrV oixA== X-Gm-Message-State: AOJu0Yx/dh71/KJw1bc1ngz1UBZtbdMlXs1j7immkAtnTfMsIrl9leKi mSnNg3YVmNBxFChr0orPUNxN59Lsp1DgwM2EGH2C5bGRHo0xLOWWX3ZyVKyCmeM= X-Received: by 2002:a05:6a00:198c:b0:6db:ea8b:52ae with SMTP id d12-20020a056a00198c00b006dbea8b52aemr360157pfl.66.1706164199621; Wed, 24 Jan 2024 22:29:59 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id t19-20020a056a00139300b006dd870b51b8sm3201139pfg.126.2024.01.24.22.29.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:29:59 -0800 (PST) From: debug@rivosinc.com To: rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, paul.walmsley@sifive.com, palmer@dabbelt.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com Cc: corbet@lwn.net, aou@eecs.berkeley.edu, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, guoren@kernel.org, samitolvanen@google.com, evan@rivosinc.com, xiao.w.wang@intel.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, waylingii@gmail.com, greentime.hu@sifive.com, heiko@sntech.de, jszhang@kernel.org, shikemeng@huaweicloud.com, david@redhat.com, charlie@rivosinc.com, panqinglin2020@iscas.ac.cn, willy@infradead.org, vincent.chen@sifive.com, andy.chiu@sifive.com, gerg@kernel.org, jeeheng.sia@starfivetech.com, mason.huo@starfivetech.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bhe@redhat.com, chenjiahao16@huawei.com, ruscur@russell.cc, bgray@linux.ibm.com, alx@kernel.org, baruch@tkos.co.il, zhangqing@loongson.cn, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, joey.gouly@arm.com, shr@devkernel.io, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC PATCH v1 17/28] prctl: arch-agnostic prctl for shadow stack Date: Wed, 24 Jan 2024 22:21:42 -0800 Message-ID: <20240125062739.1339782-18-debug@rivosinc.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240125062739.1339782-1-debug@rivosinc.com> References: <20240125062739.1339782-1-debug@rivosinc.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789043196929399201 X-GMAIL-MSGID: 1789043196929399201 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 configuation 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 | 22 ++++++++++++++++++++++ kernel/sys.c | 30 ++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/include/linux/mm.h b/include/linux/mm.h index 15c70fc677a3..df248764bcec 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -4170,5 +4170,8 @@ static inline bool pfn_is_unaccepted_memory(unsigned long pfn) return range_contains_unaccepted_memory(paddr, paddr + 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); #endif /* _LINUX_MM_H */ diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h index 370ed14b1ae0..3c66ed8f46d8 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -306,4 +306,26 @@ struct prctl_mm_map { # define PR_RISCV_V_VSTATE_CTRL_NEXT_MASK 0xc # define PR_RISCV_V_VSTATE_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 71 + +/* + * 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 72 +# 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 73 + #endif /* _LINUX_PRCTL_H */ diff --git a/kernel/sys.c b/kernel/sys.c index e219fcfa112d..96e8a6b5993a 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2301,6 +2301,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 @@ -2743,6 +2758,21 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, case PR_RISCV_V_GET_CONTROL: error = RISCV_V_GET_CONTROL(); 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;