Received: by 2002:ab2:1149:0:b0:1f3:1f8c:d0c6 with SMTP id z9csp3036654lqz; Wed, 3 Apr 2024 16:47:47 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVUAftuSKAviMvnYaNYh7eZIS8MteHF13whgSfJpcDSWvDy2mbLxCuVWl6T01XZtehdftFXvjJLtcBHIb6Uf9wGilmgrn1gYuMzgqg6kg== X-Google-Smtp-Source: AGHT+IGDY1bHILpdggwPwz+/lts9u0ATR0581Dxq07unyeGXCAMWOq5TAakjpyzJNoqdHkQAqIj0 X-Received: by 2002:a17:906:b7c8:b0:a4e:a204:e646 with SMTP id fy8-20020a170906b7c800b00a4ea204e646mr418259ejb.64.1712188067041; Wed, 03 Apr 2024 16:47:47 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712188067; cv=pass; d=google.com; s=arc-20160816; b=IxkLqMDEIYj1raGjOuOmebC5A1AhamN+ibswmD8kbpKaql3u0AqEwSlMMT1biV5yeH pBS5+6bjzUPuuBwjkKQvpRgR6rEopk6AcpVNqWFGgdsogFgtEcyiOIYtsVG9hJXlrbHg wJGI8UPRHEukvUphNz31aO5Vc0LvM5t/MAR8fCBBuFsvsV8rWOc540pUsYSYEcDrY3j2 rhvwFnET7aqZsfmZf5vDHZxybAKe9OrUkRffZNeEagPNwVlV79M0cZ35lpcgLNTnREPQ A/nAXGmUeaZUoHI8IZYMWR5Uo5ywkzxWtZAY06b32HoXax5H+Xz4S6mYAysokJh73QU+ V6pw== 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=LYjXbKsZWObysa039N5fpdCtqQMANxENOQ8uS4k07nc=; fh=LTvMzZ9FfT5w/wIEBL+Fl8xiv+585399dMU267mLYjA=; b=I5xQ8JoVUtnwNyJJueLaEPJF9olEye0I/3ekqv/KlBSVqwCm1XtdWtnYs3921ZUzKg 9lGQ5Tj7RmjqyrVJcwDsYpgUoLhDQajOT9X31vDUDWvPI7frXpmx3IzppqNkexex8JMY MMTflj0RlT9O5Pg0ZpfXliEncvo0mYMBTVEfDu36ezfmOj3LEptLiMUaT7gJ3z0O8NT6 xQh6R53F+do5z4ZI3EUpvdflWv3z4ra9+7piZZQBlk+MRXo/Rvb6ViSJtD1ObefX31Sb RnR8VElCF/Z8hg3kvJuXKvi7ueqk+FE/4VbvWH4aK8IN/cPTNKmNMhnD+l1WYCovZID9 Be1Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=w9EXSwux; 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-130737-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-130737-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id hh14-20020a170906a94e00b00a4e29b0d4e9si6659544ejb.278.2024.04.03.16.47.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 16:47:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-130737-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=w9EXSwux; 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-130737-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-130737-linux.lists.archive=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 am.mirrors.kernel.org (Postfix) with ESMTPS id 142721F28378 for ; Wed, 3 Apr 2024 23:47:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E00F9159598; Wed, 3 Apr 2024 23:42:29 +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="w9EXSwux" Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.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 3269E15748E for ; Wed, 3 Apr 2024 23:42:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.175 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712187748; cv=none; b=gTbZCU86vsQxPApdepBTmi8XjO9JiLDZSJMrhxMhoWKMtnBG+DTKT/jx6T7UF1eflh+CEkyqeGHOJBKQ8pc0Fw1QBsw1B1W56HP4zdbEZdAr4mUzb8K/VlIzjPi584YS1BVB/EGSzjPipZ7lJJgiGRF3KE/vbDjjjuKU099FQ28= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712187748; c=relaxed/simple; bh=/UM8sBwS5Fq4+1ALaM23XG6iZwuJZw3OrcJGzFpN/Q8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WOT9rIVS6pegSANKXmLmSmq4txEJ84cpi6sJzR+Ef0o+LUtz4b2o3mchJcbd7JsWvxklcGrriB9wPxIceWXgIMbJ17umja76qMHhiU3ezLGUUYj8VFesX/EVHQBNaGPyZqe+hqXZ0YxJI3LSVUKdnuY+YSrXjYShJjqGT6b+3jw= 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=w9EXSwux; arc=none smtp.client-ip=209.85.214.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-pl1-f175.google.com with SMTP id d9443c01a7336-1e034607879so3174885ad.0 for ; Wed, 03 Apr 2024 16:42:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1712187746; x=1712792546; 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=LYjXbKsZWObysa039N5fpdCtqQMANxENOQ8uS4k07nc=; b=w9EXSwux6JYHAv9CW771NZ4igExmqXmbU4L7zYqPOB3u9e+M8EnZ9Hy0SeNnSypcL8 M/4E7nFXCkSbWdYkPwSR9r2yBjkRSZGDakqlzDvfogLSpVYOPLOYOOPhWGzsMvpn3nqg wOb1fiPhtxtM8Y3v3f2g8HfBxdBUMSr+BlZx/ub6MMzuJaGyCfz1ktawGg5iLyU8V6w4 N5Ie0DtsLZ2lXTVmbuUHVapNhCHAPcmO3t/1OrGpE2XjUaTf4W2zegHN0Ze78Hqvat69 0tkydq7IPYbWZlscXbC75FW86E+FOnyDs6VctrRUgrvZSUlx/IyFG5ti3g5wwryDaCWr Dl3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712187746; x=1712792546; 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=LYjXbKsZWObysa039N5fpdCtqQMANxENOQ8uS4k07nc=; b=RORGUaC1Vak1KjphzPRnvaFOqKMyhNFjRdXLnpRc62jSs/2Ak5PJQhSnYDdLdwJdT9 9quK9HAYKCABz9ZzQ2p7g1sRRxMvot6wtb6vqELiS5TIAr8w+kKL/DW++F0c+TjeA7FJ wpWpr1eeIkgjhCaeGAj+gfK7fpnPf6LISD9SzNy8jThx39FK56BIOpx6HxZMOQB9KPSk gKImVnG1NJsGVmECt2xOryebgMog8fKhCNvHGX6LZtOj2FwEGNK5/3sqef45x7IId3Qf ZcBPsYwYuxatlYbxAHSP5j5gwEwUlvPUE9qO+Vu/BfEKLV9+/3oajL5qCVc8xNcz60em cs0Q== X-Forwarded-Encrypted: i=1; AJvYcCX3rgtcdqLgKlrqjAO1HPkjUt4m5SkmBQ2knfnND6aBZ75hop4/BKAy9du83L9wPbU9nAwhBSTkXZ+t2Nf2QnOhtVjSCihyPAkxL2DJ X-Gm-Message-State: AOJu0YxKp8fSFiufXSmU7adCB7NAEDDCAmGZon6sBaMWArN80QWi7+ac DHbEJwnOsGC5xHjd5oIP7E4xvV3zE8nViLUka8iON2J/kOln5L1Gu8V1XnRvccA= X-Received: by 2002:a17:903:244d:b0:1e0:e85b:3389 with SMTP id l13-20020a170903244d00b001e0e85b3389mr1058260pls.3.1712187746310; Wed, 03 Apr 2024 16:42:26 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id b18-20020a170902d51200b001deeac592absm13899117plg.180.2024.04.03.16.42.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 16:42:17 -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, conor@kernel.org Cc: 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, corbet@lwn.net, 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 v3 18/29] riscv: Implements arch agnostic shadow stack prctls Date: Wed, 3 Apr 2024 16:35:06 -0700 Message-ID: <20240403234054.2020347-19-debug@rivosinc.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240403234054.2020347-1-debug@rivosinc.com> References: <20240403234054.2020347-1-debug@rivosinc.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 ef48a25b0eff..3fb8b23f629b 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; +} -- 2.43.2