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: 13530087 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 48E3FC47DDF for ; Thu, 25 Jan 2024 06:30:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C64478D0008; Thu, 25 Jan 2024 01:30:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B8DAF8D0001; Thu, 25 Jan 2024 01:30:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 993AA8D0008; Thu, 25 Jan 2024 01:30:03 -0500 (EST) 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 839EF8D0001 for ; Thu, 25 Jan 2024 01:30:03 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id BC69C1A0D2B for ; Thu, 25 Jan 2024 06:30:02 +0000 (UTC) X-FDA: 81716858244.13.62CD693 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by imf18.hostedemail.com (Postfix) with ESMTP id D653E1C0002 for ; Thu, 25 Jan 2024 06:30:00 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=b8rnb5dF; spf=pass (imf18.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.179 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=1706164200; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=5zflxddcDQBcU2vSu/tVz8rqPQxJomCL53KP/QLCzbU=; b=SFUow7Z5T5bUXtnYyOQHLRuwIi0DDcALOz3rsxpA40fZbOHpES+Q8gPcXCk4sJOChL/At9 fxQO+BD55kHQcb4GPWKfx4Yzyz+zv/bp8C2LWnfB/aote5er/8PqOR/aaSeu/7AdPYDOC0 1Qh32jlKGT3WuB4dzeYPH0dsyv6bh+Y= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706164200; a=rsa-sha256; cv=none; b=uF3f+zdEO4lnG79CzJ0StEeYQtppy03UbmcfiRso8WpC/Tv0Lpnij++29MaOeP7yEQ1oOV qlZFYuZMvywkICsTHlVGkx/7q8nm/yTGegLUuo5tBB1FiL0sHwP8YfmBD/oPzECGKI7YeG 3ZasO7rLGoIIOXlNFxctGEJHYS78l2U= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=b8rnb5dF; spf=pass (imf18.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-6ddc1fad6ddso959720b3a.0 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=kvack.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=b8rnb5dFsNk+0eez6dpcmq9ED+O1ibc1edHSIr7VabcGVh1orE4YlgtfhqqS86TdIe rwEj1ue/BJOzF8Y1dElqHzQJcbJ4GDmFDZpQvFgvsg7G2XY+hi76XQsTseqPfvM0Th0T sRJ1XEhPYnaVTCE5nOiQ/a68AucnsMT8gdrcQvfVGgP1JPgod7Bh2tvV0xRnCvAA5ftu ZEDwR8hmDldlUJNumZ3eb5aQDpLEt5jQlwuKj0DM6SzHnOIrkXnp/Lj6yP7Et0r/Zv+v 8pWIFqW9PZWFhqxdKpiOPcJIXamknA5dYV/pOOKp0Pm7DfUsxD1yB0PFrsiKny1EES/K w9iQ== 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=Z0/2C+jzERlNHHq/Sm6d9gnLfSd2g3Z97K5UpWhC50xYJMaF9xp83PcummSDG/kls0 Fgy+7QMblWl+SdrtHeA5OZMvPvqFNUFYdlAeNxeeLW1r5LxqeLlKKvAsHA+Pc/4PfZ5Z mXU+49hJHaM8nX/mCyiWIoiY9rLyLRVVbmD4kWybGz9eHsiB0f627gB6jzJ4HJ6jdBg7 IOpn6Jn0q4gC/iI7RkU+bpYP4yZ66A6++jKOnyZvJuV3o+33rWS/FrIARD5yoGdisRQM c6oAvo7pthhM6M1oD4LRh4X8FsZ0kgKnwZcmGPJdx69mCDDG2rsulixMgxiRtGBqUHBY uCOw== X-Gm-Message-State: AOJu0YwZyi3L++cZQ813dzD/bOGX61gxNRtiGw6fYGmInbVYxf8znQmt g+NrkeLGH3ldrObb1PMs+CwMjZq+ECQOgqg0WfbhRNXBjjkjW3C0UHi39kiHR+c= X-Google-Smtp-Source: AGHT+IGVLurI7ehwFwOqiPymyWwdcwBSGSxuQ1bEK3sHE/LCFMR0aIK1c3iw9hogS/394Qm8iTIo1Q== 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> MIME-Version: 1.0 X-Rspamd-Queue-Id: D653E1C0002 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: rs8gn4o4gy4s1ncdrdcxrx39ikfjt7e3 X-HE-Tag: 1706164200-178668 X-HE-Meta: U2FsdGVkX1/Qrrv/PDptnNqbZW+GG2cef3wKyRvTU5jRrXKfb4BS6V+QD8wLuGB3TxHzwa7G0LgXv4M3yDE45lO7GjVpa789zFsx+Of9ow85ESRaaSr1RhWrGEYGABtftUEafx9L544k/O7pr2wYpElpXP39xZIN1civARG50mCYTZN1hJe3qPX2FPI9eZb480bXS1aFUpW342SSNag6O3Z6tiX1K4OcsUgYAFlJwOvpGQlqLXhHxQAdyHS0JqVEKxkNRRZ9mbThlhY51oTj1FONdRYn8H44YCu8O4M04CXgLXoexGXxU/z6tTL+rAjhjdxmn8uRQ7E2lcOjP9yW3cbDr/phUUEb92rlrdxWwC3sb5hNGlp7uJN47Vv1S/qet8uTHIZkDB76GbArqtFtTKGVSW/XGFDerKUy3LFW6VysjFQSNLt9vvE+7K3Thr1fkJmdGYzHoDGjRl1wkdVxtJdKoZNPpXlw9FgK6uCEFHP/ZAzxGc0jBAZ7gKTDkcgdIYVPXPtVX1BsfgfkIHOsx1GKG7Oj9NSjwRX8DnnG96RTsJ3nF1bRnXdoYH+k5Nmlfb1xuZdPlnki71eGPylMyxWegBLnmmqgFZ5brXV4w4cNghOLF8h6IEKVCUOdFzYxTNvrNpdHRjK6NqoxTdi56BF+WjGrNBCEz4J5dO9l+dKDkF1+JrZJbbDRZvkL2BU11hLKaL07G276bNPL/aLOylvW9CTZhhFZUivKq3DoHJkQgyqvuYw+gtm47whYVC55JSddZkjHxu+5AzD18/xDN2hXlfmmJL48kgFTrAtydMIsduCBAuBjSUZM7V+niS6FlBjwek8wUEXqexkThCq6ekA8v3b2e9NcOI5717BXE4DR5VBFE8nMMs2qp1RN24yYCkSJNtTEP6QgWPsbx7Jh9Flstn2eA/oOcDSgG6MuM1y9A+XWQK7N8LtUiV5urkS0WNbEvCPNfw11Iijx5L3 rNRO9pEi 6DnSR5lbm7N0+oT/l+v+Q8NCBiKGq/vZPQeu00h7+Z6Rd3ZsT5Hc28AojiMWJE1oD2gkf5MHPOxxoHyEJ6TyZp2182HCwFi/aY2ejjqKE/qLbHEtPQaK/2iISBDNgZx1iggWiuPI+I/zJmJjJalrrFtw4utWUMzQqLJeUq+3r4wUil5YntrY0L393anrwzOfdSCavXdHnsU0cGOCi3ZP59TJNtCO16ehFuJdPopzKS3Ya0NtRhMQ4zi44Y6HUIQ4pDJRJsgc46PuXHYPaGdo3G7JEY4vHRfkiUpHVg//rNlMVFz5g2MNaBGwhxike14+CBk6YN7z8we5crXtxhCD71iBsjGKwcNFtWgZo06zJ2k5xHpxIFGEua6WFvx1Mr7bTEKJfBiTE5erl0yjG9z0fu6aje3ZA6FpK8ET3h+stimeBh3K3sPigjDC/iLcRFSCl9tUGcrh324qrChBJiGDkqOfqFxquxv9s/OetVgU6Gv3cJfAHFRTF1ZROlHZDeOiAC+Xx 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 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;