Received: by 2002:ab2:b82:0:b0:1f3:401:3cfb with SMTP id 2csp868465lqh; Thu, 28 Mar 2024 21:49:05 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWBMNZOFd7e7pzv5M9dnKaY3Ti7i2UxaccXAKcHhIYSnk1ipc7qpyeHjpiCvtxxhsqZGH9oX/CDrmTMAESz1IZwmqf0ZujRu9i1IO/JOg== X-Google-Smtp-Source: AGHT+IFWe4z4gKUU6hC5BUbcns753YURjX68i3CNEhnXPT9Dp0Z5VKgdaRTOgnerMLco3f2QY9sD X-Received: by 2002:a17:906:33d1:b0:a4b:56be:1e9c with SMTP id w17-20020a17090633d100b00a4b56be1e9cmr709130eja.36.1711687745517; Thu, 28 Mar 2024 21:49:05 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711687745; cv=pass; d=google.com; s=arc-20160816; b=m3jr3XDwdU+4nS8U13Wk8qV+u66emWR3krPpdfMmEEDhKekohksmX+u4jrXvFbZhVQ dyQxG/l1+PByBNLaZQiKXROOuJ1U7wUvXwIXdU5nLe+mnz2Ona/mh8jqANjwLv5Pb9EJ JPwDPjVnJLXhyt4ePXdmvcu9AQqq7sUK5ENcG4Kw2PNaLJQgrqHlhOaGCYivigbFcD9r zAZcivNqp32xgPT6Jnlqmrls8PYeITKsqc4t9zmVrPn3jstmZPZE0I9+3dFdLh1Y4bnA SoALJvwQwBfaUuycsaH2CdahSCqfmOLUexnrj6EM4Cjen1PS071pT2Zk740xlED6I6eS SRpg== 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=tRYuUV/HDlw+87fPFfw891sAepuDTsGv4tytTH7IM+U=; fh=4gHo3D8Tyy9JlxuJHdNGt9EmwmnJOuVOr11rKIlZZL0=; b=pMEN2i5oV+0vrbAKHfsuOBYxigMYKBnJvR1Czm+U8Ybq2oGyatbbrVxfdUU5HN//oI 1VlSHo5BU1JkAMVwQ+YWi+PL4AJa4Z3zkduYLtwziY9iTaPBlgDlN8P4ighW8T4WjEIz XbWYaoyaNs1X+NLlenT3uePOIemqowXEzWWUKhLJyZS3xmifB6/i/NxigNQngkRc2ivQ +Eqjfi12eR/0baoV5pt6nU3Pp5cxdKEEPvmMbKSBbh5zA4e2WxvIMzFTRGThFWa8slQe gmD4s63LEyw5ZtfUM74QwQn6axJy3p664F75UVN0K/EyRmJw+AtRu4s1MdqqSYD13+eL 4Prw==; 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=uPiEyU3T; 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-124130-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-124130-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 m7-20020a1709061ec700b00a474ece2f35si1385884ejj.844.2024.03.28.21.49.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:49:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-124130-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=uPiEyU3T; 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-124130-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-124130-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 1D2031F2272C for ; Fri, 29 Mar 2024 04:49:05 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 93185482C3; Fri, 29 Mar 2024 04:46:32 +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="uPiEyU3T" Received: from mail-oa1-f45.google.com (mail-oa1-f45.google.com [209.85.160.45]) (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 0F54B450F2 for ; Fri, 29 Mar 2024 04:46:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.45 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711687591; cv=none; b=fS4iTGwTNSDdtFB60Exei/Vz8vM1gHCZAE590BYQTXuUTwVERLEsDk9Zxe0WMsfmhwBXswJTis1ei+OOKouqqZrbnlz6tz3Dhsi1/IsDRp0Lhf2YnGliRYcprLYcrXU9MlolvDIGW1BuXP4JC3Oo77aNDkBSmay28ujuQhdyQe0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711687591; c=relaxed/simple; bh=Mjj/W7YJP1oRliEKJvU21wwE+mKCGNlF947ECwK1HzU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=sZ6qR4O0EFGJu5zbhtt96k4jGifm6G9PMpk9Wu9pPq9kiVOwKG/NB8lQ1V9LzMmXAAvWL9xs+I5HWt+hbepq4RVAOS2xDFbA3GNyE4UFraKjy+B1mMjwbEVT+au3ddVM4KwW7qNvn3TquRLsZBdjNS+VYHna40tgJLdRz5hFpD8= 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=uPiEyU3T; arc=none smtp.client-ip=209.85.160.45 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-oa1-f45.google.com with SMTP id 586e51a60fabf-221e6bf537dso1043660fac.1 for ; Thu, 28 Mar 2024 21:46:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687588; x=1712292388; 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=tRYuUV/HDlw+87fPFfw891sAepuDTsGv4tytTH7IM+U=; b=uPiEyU3TNDa6T9SK2m2EIvcqe5B0dFgGqnfJL3EDUn6AT1gcZVftnfbIN14CgSAfPS K7ucBJLitMfAZJ+7Yi2uMtU2hX/7mM5tNzsvkqZ1OfS6Ts4d3DpX4wp0Lg9CjpmHCXNx nPjvNY+2WGpsc/iFvWj+V4S2wfRa0pupnaWlHPUtEeg/ndUUtB3a7vWTRHIP5KTvoUnK S+412jXabqC2tApeJ7cbx9Tfu+wD+Y8YYi67UJoTfc2Se9i4Al/AoDGCj2mRcDeIOEXe Qn6PnZfX0fFuIF5/9+5cC7/nSMIFOeE//DAcZdhUJBoZm1GDFPweuPeNG8M6A8m84/lT jUAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687588; x=1712292388; 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=tRYuUV/HDlw+87fPFfw891sAepuDTsGv4tytTH7IM+U=; b=mMp9mPCf+lFy6eIcyeOJNSEYRloWfURCAQZJlakb43n1gDkR+Lc511XRyENwZUIoc9 PqOP6Gc3gHBA8X6TXRfEYH/W++C9LCif1Pak9s2pHJaoXWIaYADff+8htAEakqaQGrtj TzCzkIgFHAAnNSE4/W3RnhhAo/bR83eYkyPaDFn75lXynRxiH9FyXeFXtzlPtmuiSlSI /4hYvScFyrzImkrLn+132L2LMgSpxoc0k7a+fE1+ECAP6xY3wd52y7RWUjWIKhV29Vcv dpj+GG2QDuOwKHiLblVsPm/paj9Zv6ub3gWiycKPCaR2Hh82CRohQnRtRQ7dVuBrPEkX JAOw== X-Forwarded-Encrypted: i=1; AJvYcCUqTF8ds4FAYDSqksD6oQFwkZRdFjGn5xdyjZ1JO2+4Hp+A9cFDa0rMZOCOSQxlZ4JmMjGiW7OYlIOBrbu/SQCC5o2G6ZlnyT+IzZbO X-Gm-Message-State: AOJu0YzTvO2/tM2bC+/BTcZj0sZn7qORKecuFqZ2bdpkLheQ6faKgjlo NnkkAZnSTO80q+zO/Cao9LsvDO9YyMKPN9ykpEj6RVGzdctY9ys3PF+8KbUQ9DM= X-Received: by 2002:a05:6870:c48:b0:22a:6ad4:5c4 with SMTP id lf8-20020a0568700c4800b0022a6ad405c4mr1448163oab.3.1711687588092; Thu, 28 Mar 2024 21:46:28 -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.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:46:27 -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 13/27] riscv/mm: Implement map_shadow_stack() syscall Date: Thu, 28 Mar 2024 21:44:45 -0700 Message-Id: <20240329044459.3990638-14-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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit As discussed extensively in the changelog for the addition of this syscall on x86 ("x86/shstk: Introduce map_shadow_stack syscall") the existing mmap() and madvise() syscalls do not map entirely well onto the security requirements for shadow stack memory since they lead to windows where memory is allocated but not yet protected or stacks which are not properly and safely initialised. Instead a new syscall map_shadow_stack() has been defined which allocates and initialises a shadow stack page. This patch implements this syscall for riscv. riscv doesn't require token to be setup by kernel because user mode can do that by itself. However to provide compatiblity and portability with other architectues, user mode can specify token set flag. Signed-off-by: Deepak Gupta --- arch/riscv/kernel/Makefile | 2 + arch/riscv/kernel/usercfi.c | 149 ++++++++++++++++++++++++++++++++ include/uapi/asm-generic/mman.h | 1 + 3 files changed, 152 insertions(+) create mode 100644 arch/riscv/kernel/usercfi.c diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile index 604d6bf7e476..3bec82f4e94c 100644 --- a/arch/riscv/kernel/Makefile +++ b/arch/riscv/kernel/Makefile @@ -107,3 +107,5 @@ obj-$(CONFIG_COMPAT) += compat_vdso/ obj-$(CONFIG_64BIT) += pi/ obj-$(CONFIG_ACPI) += acpi.o + +obj-$(CONFIG_RISCV_USER_CFI) += usercfi.o diff --git a/arch/riscv/kernel/usercfi.c b/arch/riscv/kernel/usercfi.c new file mode 100644 index 000000000000..c4ed0d4e33d6 --- /dev/null +++ b/arch/riscv/kernel/usercfi.c @@ -0,0 +1,149 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2024 Rivos, Inc. + * Deepak Gupta + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define SHSTK_ENTRY_SIZE sizeof(void *) + +/* + * Writes on shadow stack can either be `sspush` or `ssamoswap`. `sspush` can happen + * implicitly on current shadow stack pointed to by CSR_SSP. `ssamoswap` takes pointer to + * shadow stack. To keep it simple, we plan to use `ssamoswap` to perform writes on shadow + * stack. + */ +static noinline unsigned long amo_user_shstk(unsigned long *addr, unsigned long val) +{ + /* + * Since shadow stack is supported only in 64bit configuration, + * ssamoswap.d is used below. CONFIG_RISCV_USER_CFI is dependent + * on 64BIT and compile of this file is dependent on CONFIG_RISCV_USER_CFI + * In case ssamoswap faults, return -1. + * Never expect -1 on shadow stack. Expect return addresses and zero + */ + unsigned long swap = -1; + + __enable_user_access(); + asm goto( + ".option push\n" + ".option arch, +zicfiss\n" + "1: ssamoswap.d %[swap], %[val], %[addr]\n" + _ASM_EXTABLE(1b, %l[fault]) + RISCV_ACQUIRE_BARRIER + ".option pop\n" + : [swap] "=r" (swap), [addr] "+A" (*addr) + : [val] "r" (val) + : "memory" + : fault + ); + __disable_user_access(); + return swap; +fault: + __disable_user_access(); + return -1; +} + +/* + * Create a restore token on the shadow stack. A token is always XLEN wide + * and aligned to XLEN. + */ +static int create_rstor_token(unsigned long ssp, unsigned long *token_addr) +{ + unsigned long addr; + + /* Token must be aligned */ + if (!IS_ALIGNED(ssp, SHSTK_ENTRY_SIZE)) + return -EINVAL; + + /* On RISC-V we're constructing token to be function of address itself */ + addr = ssp - SHSTK_ENTRY_SIZE; + + if (amo_user_shstk((unsigned long __user *)addr, (unsigned long) ssp) == -1) + return -EFAULT; + + if (token_addr) + *token_addr = addr; + + return 0; +} + +static unsigned long allocate_shadow_stack(unsigned long addr, unsigned long size, + unsigned long token_offset, + bool set_tok) +{ + int flags = MAP_ANONYMOUS | MAP_PRIVATE; + struct mm_struct *mm = current->mm; + unsigned long populate, tok_loc = 0; + + if (addr) + flags |= MAP_FIXED_NOREPLACE; + + mmap_write_lock(mm); + addr = do_mmap(NULL, addr, size, PROT_READ, flags, + VM_SHADOW_STACK | VM_WRITE, 0, &populate, NULL); + mmap_write_unlock(mm); + + if (!set_tok || IS_ERR_VALUE(addr)) + goto out; + + if (create_rstor_token(addr + token_offset, &tok_loc)) { + vm_munmap(addr, size); + return -EINVAL; + } + + addr = tok_loc; + +out: + return addr; +} + +SYSCALL_DEFINE3(map_shadow_stack, unsigned long, addr, unsigned long, size, unsigned int, flags) +{ + bool set_tok = flags & SHADOW_STACK_SET_TOKEN; + unsigned long aligned_size = 0; + + if (!cpu_supports_shadow_stack()) + return -EOPNOTSUPP; + + /* Anything other than set token should result in invalid param */ + if (flags & ~SHADOW_STACK_SET_TOKEN) + return -EINVAL; + + /* + * Unlike other architectures, on RISC-V, SSP pointer is held in CSR_SSP and is available + * CSR in all modes. CSR accesses are performed using 12bit index programmed in instruction + * itself. This provides static property on register programming and writes to CSR can't + * be unintentional from programmer's perspective. As long as programmer has guarded areas + * which perform writes to CSR_SSP properly, shadow stack pivoting is not possible. Since + * CSR_SSP is writeable by user mode, it itself can setup a shadow stack token subsequent + * to allocation. Although in order to provide portablity with other architecture (because + * `map_shadow_stack` is arch agnostic syscall), RISC-V will follow expectation of a token + * flag in flags and if provided in flags, setup a token at the base. + */ + + /* If there isn't space for a token */ + if (set_tok && size < SHSTK_ENTRY_SIZE) + return -ENOSPC; + + if (addr && (addr % PAGE_SIZE)) + return -EINVAL; + + aligned_size = PAGE_ALIGN(size); + if (aligned_size < size) + return -EOVERFLOW; + + return allocate_shadow_stack(addr, aligned_size, size, set_tok); +} diff --git a/include/uapi/asm-generic/mman.h b/include/uapi/asm-generic/mman.h index 57e8195d0b53..0c0ac6214de6 100644 --- a/include/uapi/asm-generic/mman.h +++ b/include/uapi/asm-generic/mman.h @@ -19,4 +19,5 @@ #define MCL_FUTURE 2 /* lock all future mappings */ #define MCL_ONFAULT 4 /* lock all pages that are faulted in */ +#define SHADOW_STACK_SET_TOKEN (1ULL << 0) /* Set up a restore token in the shadow stack */ #endif /* __ASM_GENERIC_MMAN_H */ -- 2.43.2