From patchwork Fri Mar 29 04:44:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610059 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 4613FC6FD1F for ; Fri, 29 Mar 2024 04:46:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AB2116B00AD; Fri, 29 Mar 2024 00:46:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A629E6B00AE; Fri, 29 Mar 2024 00:46:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8659A6B00AF; Fri, 29 Mar 2024 00:46:37 -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 5B8E96B00AD for ; Fri, 29 Mar 2024 00:46:37 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id E30281410CF for ; Fri, 29 Mar 2024 04:46:36 +0000 (UTC) X-FDA: 81948840792.12.595B4AC Received: from mail-oi1-f175.google.com (mail-oi1-f175.google.com [209.85.167.175]) by imf22.hostedemail.com (Postfix) with ESMTP id 226FCC000F for ; Fri, 29 Mar 2024 04:46:34 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=H1l1+P0E; spf=pass (imf22.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.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=1711687595; 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=RhhwcNmyZsW8Rn+io2bmrU6vZaKqFl7C8RSpnBjD5D4=; b=GZBJ8NSHjDOsD9FVJn0a4Len5KxpKZNADccBSsAY/fRy5bgemru9pXRjckO1s9GsbjfAB7 ElD3CqCDzGcvcIYRYbxGge3pM7idmIbKOJ9v2chpJN7Dv5Ink6GJ9vhZCOTWDQIPRpiKbQ t4v3HwfJ8ni2y8SaAa3Q5f4TliHNT7s= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711687595; a=rsa-sha256; cv=none; b=EiBLJT57oCIgahxUqNDLFt1PV3dzi4Kixeh3KZgwzW8a0n/8Eu/JWQEm/uh+5WvhiohRy3 9IadRUDU7fUDGzJRDk2qSJW0i4uvZpodJcNAWsVLYUsoythZL28X7XQHcZlQ0CFh6/42jQ 33cFaMSRo8rH0RbrbgUAZORVI62kVbY= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=H1l1+P0E; spf=pass (imf22.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.175 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-oi1-f175.google.com with SMTP id 5614622812f47-3c3df13fe31so1005762b6e.0 for ; Thu, 28 Mar 2024 21:46:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687594; x=1712292394; 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=RhhwcNmyZsW8Rn+io2bmrU6vZaKqFl7C8RSpnBjD5D4=; b=H1l1+P0Eu8z+EbUZQzT+A6FPVb1nLL1ylp1Oz7f0XBM/ITHjMT8auH66zHESbL4rg8 aA5u6PxQqTV48iHuqja0b+MSMi663cwFdbP9QyIzxAO8zHlKwEzYJPQumG8+GshT2Vp0 Y9scNw/3iD9kbhkKwb06NMOLsz/P3RrG7IRkp+noZs2xZWV6gf5VzBhAobH1i2NjF/cI h4USNQ36+FksLgwRXy3fXASvjyghIttYJhnknJ91tPhz9ScglfznnNC5x8IXOsGyT5YW 52RhYNH3oXqKxFzDvP8Qks7OVhgQX/y8YHO1mrO6wsQNqB6Y6tehWxPbswLAGY70Jlk4 yFNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687594; x=1712292394; 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=RhhwcNmyZsW8Rn+io2bmrU6vZaKqFl7C8RSpnBjD5D4=; b=Uc0/U1c8jL0cohJmQXGb4s13AaCAIVDqLZowHN7MbPL2dMjObaL/eAj24lsJF+Mf8Q SJp0LHuLPRBTSxW1xe9qfU+XgeyASV01xGw2PPfEEY5UzTP50525RwFnCjQFQ/QPN9dF KVvBJVxZfGM7AtSgvtdV4OP5PqTHCNQlHjVf/o3LZ5yXAF7CLz7uPsElCRCxQpXWwpzL C4ubiYRa2zrFiwP4/l4sxY+SxLdLSGCQLGLHw9SeY2rO9EyWCetC2SFW0zXc+IyOVu0c jJWfoVHRmchjf6khNVnAqHGn5HYHMEh8fhy6ZcA7SXVgk4XP8rEfOkn+EjGjPmNhXIsP X32Q== X-Forwarded-Encrypted: i=1; AJvYcCX5CQiIj3bR/69ws1S9NAu3VXzC5h0J8xhoOsIu3tQhCzFbwsdrBLmUaM59QJEDUDuzP90hJJbfHaqztgfSgtQTIXk= X-Gm-Message-State: AOJu0YzInFoAYkmqSaslgl6nD6i9KFo1djU/t02gEMHAOTAzKl8Xpz7h Ye6zrWBxGtlN0/Aef/SU4Tyf5SfuHOL3myDfPigPKajBxdl+cAVqhY4+WV29U9s= X-Google-Smtp-Source: AGHT+IHzCsJ/EULMJ2X0/zuKDOZ5gM2wFJf32HTwGAn7YaJ1oTsNWG5xRVVZi9OrlqC1Skwk/hVE2A== X-Received: by 2002:a05:6808:2d4:b0:3c3:d6d7:e57e with SMTP id a20-20020a05680802d400b003c3d6d7e57emr1229439oid.15.1711687594157; Thu, 28 Mar 2024 21:46:34 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i18-20020aa78b52000000b006ea7e972947sm2217120pfd.130.2024.03.28.21.46.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:46:33 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, tech-j-ext@lists.risc-v.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, charlie@rivosinc.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: [PATCH v2 15/27] prctl: arch-agnostic prctl for shadow stack Date: Thu, 28 Mar 2024 21:44:47 -0700 Message-Id: <20240329044459.3990638-16-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240329044459.3990638-1-debug@rivosinc.com> References: <20240329044459.3990638-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 226FCC000F X-Rspam-User: X-Stat-Signature: otouxded1jznu9wi8migwi91ybtq4i74 X-Rspamd-Server: rspam03 X-HE-Tag: 1711687594-311507 X-HE-Meta: U2FsdGVkX18Grkv0/4UZy1R7bFLslF31qWT95DElmBpFr+czMYDWmr4h8H+CAW/vZeycJXGoUmGJq8rUomW6O0tLjbgI7AmULIdWpyULbQwm689ohSP5tGSlOF8bW5JOZiRqgZGZd3tkIbFXi9V/GPnKDuYJHP2u8v9pUOm//Ailo7d8c3SEHplg9cOjZnAvWb3tM8a3NIljEgl6dqsyKzITjKLQomk+6uIui08IlCdKNspl4/n2CgpNUnEAV1E0O/vgo4nzlp85wbO6/KF2FoR+O31K9l3q1KZmn+Qpt5UINoHST5sRODmv+Ns5ULKJV6kfk639ALSgh+rfy1Gs1vJs7QKc8cEWMVoKAo3ywCax6ZJmDHB9FkWWyE3XbvR8OD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FpcUUC4DInAqP/avBr/tIfb9kaBzfTww5jFVV76G0mA3felnYa8bddPnFG8N0A/Vl74oBc4vG5GaU4RT/ISbafOuhirw4jPqArV99FP+w/pcgsCcx+9guVCA== 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 Signed-off-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 9e6a4fbfccac..5ea49b9232fe 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -4209,5 +4209,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 f8e543f1e38a..242e9f147791 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2315,6 +2315,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 @@ -2757,6 +2772,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;