From patchwork Fri Mar 29 04:44:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610061 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 4C00CC6FD1F for ; Fri, 29 Mar 2024 04:46:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D4AD56B00B1; Fri, 29 Mar 2024 00:46:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CD2706B00B2; Fri, 29 Mar 2024 00:46:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AFF996B00B3; Fri, 29 Mar 2024 00:46:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 8FDF36B00B1 for ; Fri, 29 Mar 2024 00:46:43 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 60950C0843 for ; Fri, 29 Mar 2024 04:46:43 +0000 (UTC) X-FDA: 81948841086.23.453A028 Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) by imf04.hostedemail.com (Postfix) with ESMTP id B4E3040004 for ; Fri, 29 Mar 2024 04:46:41 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=kNXAhu39; spf=pass (imf04.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.180 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711687601; a=rsa-sha256; cv=none; b=s5P9duz9LmiEJTTehmNuW1y9ZBucRUy0/EJdELcrAwlWnMiCeJKvXOVzTjhb3bJT4MMSmg cBDvUG6OiNsoiwXDWsoHD8A6rswtX8214OopQJosaA7naPx2FJc35KccjmsusHyzAnCP/A ZJSXRp4pFWBkvTk3b3wqeYcdF6xZZo0= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=kNXAhu39; spf=pass (imf04.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.180 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=1711687601; 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=OSTo4BG6xg4Q2onv+lGORpCI7MnnKplqFj/mRoj65CU=; b=cB7n69249Bis631Z1eb3y9BNGE5ich6Nddr1WEIkO/IOIAGJ/3YyAq33BWbV505O4kD+v+ YSdIlRb3aq9Hn3bO42vrJuaLxX2ZW+Hv7ruE4kDHoQtVSgMDUwlb6eBC5Ruhul2dQzI+86 W5zSPN1oqoWmVLAPwvJU97yhL58VWAY= Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-6ea8a0d1a05so1782344b3a.1 for ; Thu, 28 Mar 2024 21:46:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687600; x=1712292400; 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=OSTo4BG6xg4Q2onv+lGORpCI7MnnKplqFj/mRoj65CU=; b=kNXAhu39r7Q8REV0ostWw7G8IMftId493m5DXxWIWYHi+WeOOFPIVNbZQEpI8UcVC0 IGEwQYoyDaz3QmDuduO0+Q7VrktUbmBoo8hxgB0T2aSsE/Fsjz3JOlLK3ZqXGWkA1stW lAFvwZWfGT3ecnhFu4D9W+EgVzOBzOy/6UQmpeqZxvoEXvOs1LHb3EtgG7+R+wILToZK eaHfV3dFt3icyjgJq3XlTqLvtMyBlljalDMOi/AKBTL+2kwO5HMmT5HQtlrrmKkcuVzJ 4w3XSELia+dkxmF9owkBCFjYUz0RBpwdyvsEDDsd+WbpwE9Jp4WXpXqOqE2Cx03Qk7N4 8l0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687600; x=1712292400; 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=OSTo4BG6xg4Q2onv+lGORpCI7MnnKplqFj/mRoj65CU=; b=s1oUBJ5NPvPEp4kI4b6Wh2zYWlOoLpOA53U6TWW3EMp5A/5+Lscc1hukvernTfmUCg rLvpu1WmlROMLv687ui0G/1nT57EA7v5wO2c5QwG4fw0Ep3jG36jIHDGeWDuacQ+dAzT 6uYHfB8Wdz+oBH8pyKHQYMKH5GOzfE/NXnkeCTlIt6urM5yUCrCRPT0gUmKO+XHRL5ib wjduGojZkxyDiMT5KQ3QT9MDYjjyBDAqKOTpXxLj9qCMRTajwLpCRHVIbz+qLKb+T8mp 7ZvG5mJPUpeBx1StZ8q8WoL/01+8g3f2k3Wg82sAmwtAytlVfBfnDtzmOpWlDQDCn7LF s+hA== X-Forwarded-Encrypted: i=1; AJvYcCWTFnjO8fIfY0Y33pPzv9EOSnV1dn3mCLK2M8OTaqmGw9DdwQPR7QVNR2ae03883qBGpxtcoQnamwpEkcp5mv1FKUA= X-Gm-Message-State: AOJu0YwCcsIwTjPglgSAmwxs262YQ4BnxHmCeR9KFhlfq4feSllMQVMk pxc8GpMqZRmYhxxEUUw5o21dTYhWw6oe10lsePrXZZKS44Jl3WWmnePFCZ/ubeg= X-Google-Smtp-Source: AGHT+IGy01J0/WrcquoydjAoH+JlQeZkJBLUoT7UPHOF1/Tfs8zhjBY9fqykQxytj7scXuJBpM8nqw== X-Received: by 2002:a05:6a20:5489:b0:1a5:6d3a:65ed with SMTP id i9-20020a056a20548900b001a56d3a65edmr2080319pzk.3.1711687600379; Thu, 28 Mar 2024 21:46:40 -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.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:46:39 -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 17/27] riscv: Implements arch agnostic shadow stack prctls Date: Thu, 28 Mar 2024 21:44:49 -0700 Message-Id: <20240329044459.3990638-18-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-Server: rspam08 X-Rspamd-Queue-Id: B4E3040004 X-Stat-Signature: fxybndyip6su7q6hkz4qkhakxfeqhc6d X-Rspam-User: X-HE-Tag: 1711687601-886572 X-HE-Meta: U2FsdGVkX19UUCSeDwvli/+bSdVnv99GjhM984RhIfO+L2X7FbPcyPU3aUg1ErcBL2NBG7OTvG4DeHIW99htm9myRdgLfADtArjzhXUsOYzA3bCkxk81c43yg2FdGqeueLsq7g0Ip4HXckm4wcNCwNTGZDZK2uS2C+P3QEPJjCW8s7AaUG+fwRidVLjIvWkoHt/IlPgs7NY6Wo1plyjM8PMblcFSlKCGS1BDC8gMumxhkTahexwLeFRp5RhXnjppuc1bIS7I7oDbgjOJfjLYy5CTypGmTr++1SfiUNgBnJ8yJTsLvDJzEM8WW2W9AdGjlVdG0ShMJu6S+OpO8qzsnBx2a370H3IqCzRmuxp1pKn7DeMF1eP8A2SlFgyMgD9vpczYGs50kLNG/QxyPdsL3TK0li7ZR+lZuswKuKdK1WGT3y6LkBBT6TqwP/rjl2wEgDCZ9NTeB0wpzIyRTVayBQV2xx2tNKhG2kYImm1YHgIh7g9xtbG/jW4+PmJQaUWA8OeUc7RVbqnDvCeZWCKO/imQp6t6+lxGe20EpnTgnOpNVV+j+xvf7slzc2WXkLmIWpxykDlr5ToTN+RF3uqB0GAFnLnVWS6HbV/YWD/ya+54o/rTcU5z28wXiiTX2ycH27+VmP/7bvgq1LGZVgm84zZPM9FGshSg7LWJ3DgPbjtBC/SNG9ujzsikKS4ZG1/XaLTAanECT00DKz3igznylQ66TR8/bLT3j/3H2YsJWlARBbEbWJxr1dTKVanScjtJ1ZGmv46uVcDVtbZRdpy5SPz6LMWxTTudWumtlW56wjCMByXmSqn2MZTGBiPdbVLs1UdQU3KuZ4vriTg4faDXOBdHdxd3/HNSKJcknrktp/v2B1bgRouJ7HIDhWlkfRlV3xP5/oqoW2cFQChSajsZVdFRVxGOoz1ASQwlyXF+5+s6fHGwtINoPLO31CT++fAR9DyFzB7XtVsJLBSVUMg 13/x207b tm5URCWmfaO11RtHWjb9X31aj1dZkMyb/TGnWvGjPAsBJkn4UPe5QsIznAGvMaJ6lTvRs02fxi5Rktpq1ycJA5XgDXaLu4sC3KizvXKJ6ppFZfHx3AVCv5xJ2LR0uqPNlSaDxwRjsaXhpVib0oGpNgBYc3GFjOmAw78wovSx/DYUO+YspqVv86cQS0odbIy+FnFZE2y6AOws6v3omVwYBb0DzH7CJAlNwBmYgvxz6rtxapY4= 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: Implement architecture agnostic prctls() interface for setting and getting shadow stack status. prctls implemented are PR_GET_SHADOW_STACK_STATUS, PR_SET_SHADOW_STACK_STATUS and PR_LOCK_SHADOW_STACK_STATUS. As part of PR_SET_SHADOW_STACK_STATUS/PR_GET_SHADOW_STACK_STATUS, only PR_SHADOW_STACK_ENABLE is implemented because RISCV allows each mode to write to their own shadow stack using `sspush` or `ssamoswap`. PR_LOCK_SHADOW_STACK_STATUS locks current configuration of shadow stack enabling. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/usercfi.h | 18 +++++- arch/riscv/kernel/process.c | 8 +++ arch/riscv/kernel/usercfi.c | 107 +++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/usercfi.h b/arch/riscv/include/asm/usercfi.h index b47574a7a8c9..a168ae0fa5d8 100644 --- a/arch/riscv/include/asm/usercfi.h +++ b/arch/riscv/include/asm/usercfi.h @@ -7,6 +7,7 @@ #ifndef __ASSEMBLY__ #include +#include struct task_struct; struct kernel_clone_args; @@ -14,7 +15,8 @@ struct kernel_clone_args; #ifdef CONFIG_RISCV_USER_CFI struct cfi_status { unsigned long ubcfi_en : 1; /* Enable for backward cfi. */ - unsigned long rsvd : ((sizeof(unsigned long)*8) - 1); + unsigned long ubcfi_locked : 1; + unsigned long rsvd : ((sizeof(unsigned long)*8) - 2); unsigned long user_shdw_stk; /* Current user shadow stack pointer */ unsigned long shdw_stk_base; /* Base address of shadow stack */ unsigned long shdw_stk_size; /* size of shadow stack */ @@ -26,6 +28,10 @@ void shstk_release(struct task_struct *tsk); void set_shstk_base(struct task_struct *task, unsigned long shstk_addr, unsigned long size); void set_active_shstk(struct task_struct *task, unsigned long shstk_addr); bool is_shstk_enabled(struct task_struct *task); +bool is_shstk_locked(struct task_struct *task); +void set_shstk_status(struct task_struct *task, bool enable); + +#define PR_SHADOW_STACK_SUPPORTED_STATUS_MASK (PR_SHADOW_STACK_ENABLE) #else @@ -56,6 +62,16 @@ static inline bool is_shstk_enabled(struct task_struct *task) return false; } +static inline bool is_shstk_locked(struct task_struct *task) +{ + return false; +} + +static inline void set_shstk_status(struct task_struct *task, bool enable) +{ + +} + #endif /* CONFIG_RISCV_USER_CFI */ #endif /* __ASSEMBLY__ */ diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index 9551017d16db..d6dfef22c274 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -145,6 +145,14 @@ void start_thread(struct pt_regs *regs, unsigned long pc, regs->epc = pc; regs->sp = sp; + /* + * clear shadow stack state on exec. + * libc will set it later via prctl. + */ + set_shstk_status(current, false); + set_shstk_base(current, 0, 0); + set_active_shstk(current, 0); + #ifdef CONFIG_64BIT regs->status &= ~SR_UXL; diff --git a/arch/riscv/kernel/usercfi.c b/arch/riscv/kernel/usercfi.c index 11ef7ab925c9..cdedf1f78b3e 100644 --- a/arch/riscv/kernel/usercfi.c +++ b/arch/riscv/kernel/usercfi.c @@ -24,6 +24,16 @@ bool is_shstk_enabled(struct task_struct *task) return task->thread_info.user_cfi_state.ubcfi_en ? true : false; } +bool is_shstk_allocated(struct task_struct *task) +{ + return task->thread_info.user_cfi_state.shdw_stk_base ? true : false; +} + +bool is_shstk_locked(struct task_struct *task) +{ + return task->thread_info.user_cfi_state.ubcfi_locked ? true : false; +} + void set_shstk_base(struct task_struct *task, unsigned long shstk_addr, unsigned long size) { task->thread_info.user_cfi_state.shdw_stk_base = shstk_addr; @@ -42,6 +52,23 @@ void set_active_shstk(struct task_struct *task, unsigned long shstk_addr) task->thread_info.user_cfi_state.user_shdw_stk = shstk_addr; } +void set_shstk_status(struct task_struct *task, bool enable) +{ + task->thread_info.user_cfi_state.ubcfi_en = enable ? 1 : 0; + + if (enable) + task->thread_info.envcfg |= ENVCFG_SSE; + else + task->thread_info.envcfg &= ~ENVCFG_SSE; + + csr_write(CSR_ENVCFG, task->thread_info.envcfg); +} + +void set_shstk_lock(struct task_struct *task) +{ + task->thread_info.user_cfi_state.ubcfi_locked = 1; +} + /* * If size is 0, then to be compatible with regular stack we want it to be as big as * regular stack. Else PAGE_ALIGN it and return back @@ -268,3 +295,83 @@ void shstk_release(struct task_struct *tsk) vm_munmap(base, size); set_shstk_base(tsk, 0, 0); } + +int arch_get_shadow_stack_status(struct task_struct *t, unsigned long __user *status) +{ + unsigned long bcfi_status = 0; + + if (!cpu_supports_shadow_stack()) + return -EINVAL; + + /* this means shadow stack is enabled on the task */ + bcfi_status |= (is_shstk_enabled(t) ? PR_SHADOW_STACK_ENABLE : 0); + + return copy_to_user(status, &bcfi_status, sizeof(bcfi_status)) ? -EFAULT : 0; +} + +int arch_set_shadow_stack_status(struct task_struct *t, unsigned long status) +{ + unsigned long size = 0, addr = 0; + bool enable_shstk = false; + + if (!cpu_supports_shadow_stack()) + return -EINVAL; + + /* Reject unknown flags */ + if (status & ~PR_SHADOW_STACK_SUPPORTED_STATUS_MASK) + return -EINVAL; + + /* bcfi status is locked and further can't be modified by user */ + if (is_shstk_locked(t)) + return -EINVAL; + + enable_shstk = status & PR_SHADOW_STACK_ENABLE; + /* Request is to enable shadow stack and shadow stack is not enabled already */ + if (enable_shstk && !is_shstk_enabled(t)) { + /* shadow stack was allocated and enable request again + * no need to support such usecase and return EINVAL. + */ + if (is_shstk_allocated(t)) + return -EINVAL; + + size = calc_shstk_size(0); + addr = allocate_shadow_stack(0, size, 0, false); + if (IS_ERR_VALUE(addr)) + return -ENOMEM; + set_shstk_base(t, addr, size); + set_active_shstk(t, addr + size); + } + + /* + * If a request to disable shadow stack happens, let's go ahead and release it + * Although, if CLONE_VFORKed child did this, then in that case we will end up + * not releasing the shadow stack (because it might be needed in parent). Although + * we will disable it for VFORKed child. And if VFORKed child tries to enable again + * then in that case, it'll get entirely new shadow stack because following condition + * are true + * - shadow stack was not enabled for vforked child + * - shadow stack base was anyways pointing to 0 + * This shouldn't be a big issue because we want parent to have availability of shadow + * stack whenever VFORKed child releases resources via exit or exec but at the same + * time we want VFORKed child to break away and establish new shadow stack if it desires + * + */ + if (!enable_shstk) + shstk_release(t); + + set_shstk_status(t, enable_shstk); + return 0; +} + +int arch_lock_shadow_stack_status(struct task_struct *task, + unsigned long arg) +{ + /* If shtstk not supported or not enabled on task, nothing to lock here */ + if (!cpu_supports_shadow_stack() || + !is_shstk_enabled(task)) + return -EINVAL; + + set_shstk_lock(task); + + return 0; +}