From patchwork Tue Oct 1 16:06:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13818418 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 C2181CEACE4 for ; Tue, 1 Oct 2024 16:07:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2E31E2800AA; Tue, 1 Oct 2024 12:07:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2942E280068; Tue, 1 Oct 2024 12:07:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 06FB52800AA; Tue, 1 Oct 2024 12:07:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id DCA5C280068 for ; Tue, 1 Oct 2024 12:07:44 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 1E5731A16CD for ; Tue, 1 Oct 2024 16:07:44 +0000 (UTC) X-FDA: 82625514048.19.414305E Received: from mail-pg1-f179.google.com (mail-pg1-f179.google.com [209.85.215.179]) by imf26.hostedemail.com (Postfix) with ESMTP id 0884D14001D for ; Tue, 1 Oct 2024 16:07:41 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=EkXuYiL8; dmarc=none; spf=pass (imf26.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.179 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1727798721; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=LWmoBhDbyT20ZDpyj0YkBJbSnr4cmq0kZF5LWUGKIyA=; b=pl97rpbyMhxpGmu2KCwcq29SGaF7iMHLzq7J4Y/VRGyhSUxMNq5U1YlBVFrwrXJkbzbEPV YAVAxAnI9wXcriTpxp8sGoZobfj2nkYmhFIDKjrOwp0vqMZwifLMmtMe9hAfMv/E7Ip/hK lM42XyhFoUGipIc4F3jtnbFrloI3aAE= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727798721; a=rsa-sha256; cv=none; b=Zy93RnWLuMf/jycRN4aq3OzobmzWztbZPf6sYidnmhgdmf2UJZmxdN2cNBu1YwxnP3i/ar /tLBKD4QQGeqvDg4MXG7NqhsxclglzGLhas1EzhbJS4mfxdlMBIj4+57r9ZOM2pjW7Oi+r BguZhrE4Kpf2Ox0Yw1XSF3v02fbjEoQ= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=EkXuYiL8; dmarc=none; spf=pass (imf26.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.179 as permitted sender) smtp.mailfrom=debug@rivosinc.com Received: by mail-pg1-f179.google.com with SMTP id 41be03b00d2f7-7db637d1e4eso4451067a12.2 for ; Tue, 01 Oct 2024 09:07:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1727798861; x=1728403661; darn=kvack.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=LWmoBhDbyT20ZDpyj0YkBJbSnr4cmq0kZF5LWUGKIyA=; b=EkXuYiL8pFnxazP4Ba48oA3xoWMuCwAjkMU4dJZoX0R+k8E+a6lK54dW57qtsoiTN8 hhu2blVD7h1h2zj7V39je181gmhLYbL93vMM/OgCAvV6GPoKeWQIMqJzFjyvP2mRigBL osNU6v6o2J2AVkceqQlxMTYFzfElIvlKTZrZ2r4K2oK05KK5n++eFrJsXY99ElvhxuvW XcF2ZfGH0XycxkpTfSW3W5NC6ZiHS51WmSY034sT/MHuZAiFfq0syb5k9SICF5JOf0RW Bk0HJst5BHDQyZBX7R7Q74gYrjxHT0uAidWF8VGAWPGJ1eEY0m6Ma/02jkEBOI1lXpNk rtJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727798861; x=1728403661; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=LWmoBhDbyT20ZDpyj0YkBJbSnr4cmq0kZF5LWUGKIyA=; b=I+3EPzKBkO2AL9GEnMfeSJ6gZSP+OFc5jBc32op8xJDwGnNgo9WiE6I1tDO/3JQP41 O0Rn3LODleZDDWmqYGV+qIoX3YZZ0h9xD1XTMf9LNeOWKjnVghi6wOPTNX1DytYvfvyb Q9MPnpso4TT0ElQF1czf9Ei/3OKotxM3kM3hJ/kt/nE9BVdZUCEdm4BOmdW2c6TesAs/ hmmbKrUvxlytBI7plIQdGCoPqpFmGnxWdl6XVm/3Nrn5g9A3wgWJmKcgOoNxuSJLGTcl Od49Qx4Lz2qMrxqr0b8jHjOCJL+zMUW8xpEipc/YEGnf0myRmiWd7UcE6eM7inKrqbLi NW5Q== X-Forwarded-Encrypted: i=1; AJvYcCUUX6vR3MovoEbWUQV+icoDaummp9RYu3riO822PDWSSadC7vJcc+MO5iVT3j9T+6HpA/qHSZUiMw==@kvack.org X-Gm-Message-State: AOJu0YwaNTa38zwhHecN/FjtLKoyPFAlibTSi8cs8VpUZHAg8BuZ6lFJ k2zqe+xD0kcLLDFL0ZGtxtaLkNFLLZi077017e1ia5Xoupx1SVotgubE/YGDpOs= X-Google-Smtp-Source: AGHT+IF+DXMbQc9ATxScjl/51BoN0NDApnEeX0Qfv6KL8hdZd/y/ejK0opPN008FSUmQa/7I/9Jl0Q== X-Received: by 2002:a17:90a:88d:b0:2da:9490:900c with SMTP id 98e67ed59e1d1-2e1846aff5bmr230162a91.21.1727798860600; Tue, 01 Oct 2024 09:07:40 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e06e1d7d47sm13843973a91.28.2024.10.01.09.07.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Oct 2024 09:07:40 -0700 (PDT) From: Deepak Gupta Date: Tue, 01 Oct 2024 09:06:20 -0700 Subject: [PATCH 15/33] riscv/mm: Implement map_shadow_stack() syscall MIME-Version: 1.0 Message-Id: <20241001-v5_user_cfi_series-v1-15-3ba65b6e550f@rivosinc.com> References: <20241001-v5_user_cfi_series-v1-0-3ba65b6e550f@rivosinc.com> In-Reply-To: <20241001-v5_user_cfi_series-v1-0-3ba65b6e550f@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 0884D14001D X-Stat-Signature: tqfqmyw5gqo1u4ap9fd8xfw6he5oybq1 X-Rspam-User: X-HE-Tag: 1727798861-77775 X-HE-Meta: U2FsdGVkX1/0oBnvwaA1vCGHeKl01Za5W8Xp72tTzNfAj+PmP3gNRWKtk2zwmrazkqtKT7iYvBp1AlHRCmmC3DYnpzjGXN6knN+khdh6HxQiGFh4q1xIQc1PbX00X7EnsxFbtb0YckGkkxMNaacuos2nB8Vero6t2ljV7pdQBuEN+dYcH5OIGywby+oDRgwb/eh2WHxbh0HA+GU0hvH6WiEv3QmEZIzDgS0dY/0N/huP5hgH5jpx/RLC/21t3yHHIRDiKfswx6AxDpVVa2J4c2nqQm8XLneiKrGWUKCcWjGngomR0RRAUX5tF1PzEfz/C3mgqLapPY30HGp0aS5N91MHiN1p7IznFVPlpYRgGpvYcUMgbDDB8U0QJwNi09A8C33mruun72ME4uld5lC2To6fRvk46dkdS7FVISESM45HsOEW5K9Z9ikEah8Ahq1mZhrmDDgAGHR2Gj+oDyggLrZ2vUUaq7dgohA3+96j3jH4RbYzcQEkwoyeQuuJiOlP7BvUvZgbm5zMa5jbrvLvNQtElKB0aKZZEL1S8CxtPOpbGr7RmAcek1c9TWwGD3wqpxVPii1ZOJc66gaH43Ju/5Zo0ID561hZ8amaJ3rRrTnp+nZy6ofCVm+y4JpVnduAKpuJtSPNciUClbB3huuYW8SWOnCFumOL+0/0ciLyQw+RhMVf77p4QF+YNEFN0d4HXJfuUlfncr2w/SiN/pR3nDtNmF6waF/HZK/wVaBuDZaDNOo2ZJQUT7U+4JsCjxL1fpEAT8ejTDPg5XWtNo887q7uWB/JbeHgBr31azdHpHf9KS3Qc+OmP4KHch+aWE01jaOY87XoRq3bR1n7taKb09OEkt0nOUr7LMUnLwqEYj/LXo99pBX2owavctOU6EYUpdTpHkE4y1C+hj9rkqK1+OqpmTq7F3KpcMAKCBGYSegpylvnhKqmiaUwfm9D1mv7BIdwoxzhA/8EHoqRQfL vDSrzRLF UuWedtKyznId3XRKBgWnrPaLVQBBzR0vKBJKTgsIDjImUhY19ngFlioLJBx863izWbJYdxuX+rdjtuG4RLTXAOip/7DYKwpGmzgjXtUSj49v1sTKXEzlk7YJCcidEhVzRWJvg7lerKvuX3yAC/BXbxLp2XHJGSVvtNsNZEeO5y5W7mOpUEvpfhafRTfoqfltFnM/8VTrdLVWiNo92ZJI2gAAgXMrXUyE/vf3r1X7gBjXS+39ruV4v42/sK7WiT73PW0IjHfiPpJsreLngSpDVZvxCq69t1KznswttJ6IiriDV4NX/zuV0dQWmS7lxsCafiMxPgB3RDocA8eNAfBc/Llm6F1ICtPxrZj8BhcsR6L7gsUDrpWcw/ABuNH7UpztR+Ho3nXDBNXl+NYPraPeLjrZXqwKQjceqX5ba58nMiyZToRwPbwFUGEwkh5Iunvx4g8m8u8SmPsVIDFhIbJol/rW2uZKM6dM7Y5rdLLhhL8Dhhz+tLLjs28X/geCd53JVxglK 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: 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 compatibility 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 | 145 ++++++++++++++++++++++++++++++++++++++++ include/uapi/asm-generic/mman.h | 4 ++ 3 files changed, 151 insertions(+) diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile index 7f88cc4931f5..eb2c94dd0a9d 100644 --- a/arch/riscv/kernel/Makefile +++ b/arch/riscv/kernel/Makefile @@ -117,3 +117,5 @@ obj-$(CONFIG_COMPAT) += compat_vdso/ obj-$(CONFIG_64BIT) += pi/ obj-$(CONFIG_ACPI) += acpi.o obj-$(CONFIG_ACPI_NUMA) += acpi_numa.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..ce002eabbdc1 --- /dev/null +++ b/arch/riscv/kernel/usercfi.c @@ -0,0 +1,145 @@ +// 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) +{ + /* + * 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 - 1))) + 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..9cfb3c1e337d 100644 --- a/include/uapi/asm-generic/mman.h +++ b/include/uapi/asm-generic/mman.h @@ -19,4 +19,8 @@ #define MCL_FUTURE 2 /* lock all future mappings */ #define MCL_ONFAULT 4 /* lock all pages that are faulted in */ +/* Set up a restore token in the shadow stack */ +#define SHADOW_STACK_SET_TOKEN (1ULL << 0) +/* Set up a top of stack marker in the shadow stack */ +#define SHADOW_STACK_SET_MARKER (1ULL << 1) #endif /* __ASM_GENERIC_MMAN_H */