From mboxrd@z Thu Jan 1 00:00:00 1970 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 43C57C47258 for ; Thu, 25 Jan 2024 21:24:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 80C306B007E; Thu, 25 Jan 2024 16:24:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7BB646B0087; Thu, 25 Jan 2024 16:24:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 635456B0088; Thu, 25 Jan 2024 16:24:25 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 4F7C16B007E for ; Thu, 25 Jan 2024 16:24:25 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id DAE9C1603AF for ; Thu, 25 Jan 2024 21:24:24 +0000 (UTC) X-FDA: 81719112048.16.3C53D81 Received: from mail-pg1-f177.google.com (mail-pg1-f177.google.com [209.85.215.177]) by imf03.hostedemail.com (Postfix) with ESMTP id E4B7920019 for ; Thu, 25 Jan 2024 21:24:22 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=3eqQZ4wD; spf=pass (imf03.hostedemail.com: domain of charlie@rivosinc.com designates 209.85.215.177 as permitted sender) smtp.mailfrom=charlie@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706217863; a=rsa-sha256; cv=none; b=wun5GhyOB0xJCSHhSkortzKda1/n8shhL6qWgBH6i5PVBfaGZcLc57+6TSDLNm9Aiu4l5u c4SkopxslkRybbuTbUZQVidxWs3299enGzvbh2Nrimc5C+hwCNkl0jZBnBwHbto7CSFDDF AVA5x0OJBKdBm4ErC0VBJ8xjSATqQe8= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=3eqQZ4wD; spf=pass (imf03.hostedemail.com: domain of charlie@rivosinc.com designates 209.85.215.177 as permitted sender) smtp.mailfrom=charlie@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706217863; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=iZoT2G19TBg11JNaEabcqd35tlnxoe/xDz/ZFeMpMZQ=; b=ZGBQK9AelYWZIK/LfH6FJzdK/OQL6QFsILnJpRz4Kec34+x57Vy3K1XXY7JYZBtphC16EU 9Eq+cOdcDLfFazpbQA311BdLDwBN2mGxa1P97qwNVseL10Oy3By0NVFqXc8qnh5NeY6aK8 y8sOiqz58sCTItjEM4T97f3sgeyHd2A= Received: by mail-pg1-f177.google.com with SMTP id 41be03b00d2f7-5cfd95130c6so2241467a12.1 for ; Thu, 25 Jan 2024 13:24:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706217861; x=1706822661; darn=kvack.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=iZoT2G19TBg11JNaEabcqd35tlnxoe/xDz/ZFeMpMZQ=; b=3eqQZ4wDctSLDC38C86e3AZLiln2P47edaf0p6e8IoQUJSz9DEuneEPVHE/YbEklBW hAQ7z+3K4cMArhO+ICx1nb0yPsj40mfthpFIE+UR7kXGSLn7xaNijo+IsPboR06KFKTn CxbGRZeW7Fjcszduou+K1RR6avxjnsUWgkxGOoi405Ozze/VWRDeTFsqFCISLC8WMTyF cmmlCNjJf0I8ruF6yKLncD2BYl3TxvTgQOkVMP5slGiLxP/BgvkPoXA1/uQIBX7VgzKc Xvrjw9+yRs1xlXJxDSWfqBQ6NGEKxYu6z3w1vFG0f5m2uRY+hdDk8+Ay84NS/xVVrXjd 0yGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706217861; x=1706822661; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=iZoT2G19TBg11JNaEabcqd35tlnxoe/xDz/ZFeMpMZQ=; b=KimhlNlHVXUruVbnsCQ6gSUEsdJ8kd42jMfH8NiTSS4JZBnA0UahxER8H/8/H1OX+V 9DdaJCd4Fev1nwvBBBPgoSi0e2A7y1OjuK2gVMcflpeRjKwLXuXCgFi9Ry+/oEjj7y0I AnmVT0Qm/Jl2F8kzJks32Yts4IbPOqxHrLVZT2RgYP9/Wp2BTVWRiyX84mpZMinKGh1k vqZb9g63U6gUnO39RvDvPy9L90FO97Af13RHTrgLT5u84+HH83Kea422JpJnymVqdMgG 0WfL7h7CKbxdE9ykECTv/s82ysanlSkO45O3x4vdiD4VWzymADGkrL3OeO85IkORSLBO 6uoA== X-Gm-Message-State: AOJu0Yx9X1oeSGMnQfuU++rC/entZyr4lgB7Vnm6CWSVNSbhcjaY1JNY Jr11DtDeHOIAboIZzpSSdlFJbxSqTnXpwF+veR4aFn+NOKXDOzGbM03ddFrQhE8= X-Google-Smtp-Source: AGHT+IGvjaE/JTMbYh7Rdf5yyt9F1/RX+/vud6/Lnw+WvAKkovbLy31AhW1vaTpjsJmHDLgapvOZag== X-Received: by 2002:a17:902:db09:b0:1d0:98d8:955c with SMTP id m9-20020a170902db0900b001d098d8955cmr344975plx.124.1706217861416; Thu, 25 Jan 2024 13:24:21 -0800 (PST) Received: from ghost ([12.44.203.122]) by smtp.gmail.com with ESMTPSA id ml5-20020a17090334c500b001d8921f2058sm989116plb.205.2024.01.25.13.24.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Jan 2024 13:24:21 -0800 (PST) Date: Thu, 25 Jan 2024 13:24:16 -0800 From: Charlie Jenkins To: debug@rivosinc.com Cc: rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, paul.walmsley@sifive.com, palmer@dabbelt.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, corbet@lwn.net, aou@eecs.berkeley.edu, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, shuah@kernel.org, brauner@kernel.org, guoren@kernel.org, samitolvanen@google.com, evan@rivosinc.com, xiao.w.wang@intel.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, waylingii@gmail.com, greentime.hu@sifive.com, heiko@sntech.de, jszhang@kernel.org, shikemeng@huaweicloud.com, david@redhat.com, panqinglin2020@iscas.ac.cn, willy@infradead.org, vincent.chen@sifive.com, andy.chiu@sifive.com, gerg@kernel.org, jeeheng.sia@starfivetech.com, mason.huo@starfivetech.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bhe@redhat.com, chenjiahao16@huawei.com, ruscur@russell.cc, bgray@linux.ibm.com, alx@kernel.org, baruch@tkos.co.il, zhangqing@loongson.cn, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, joey.gouly@arm.com, shr@devkernel.io, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: Re: [RFC PATCH v1 15/28] riscv/mm: Implement map_shadow_stack() syscall Message-ID: References: <20240125062739.1339782-1-debug@rivosinc.com> <20240125062739.1339782-16-debug@rivosinc.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240125062739.1339782-16-debug@rivosinc.com> X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: E4B7920019 X-Stat-Signature: 54fg67kpikp9891oq9nnuyxmk7g4musz X-Rspam-User: X-HE-Tag: 1706217862-173146 X-HE-Meta: U2FsdGVkX1/ZAmqXUoLS2IDT8dJmw9k3X9cCpOoPw/V4nvycamDpXanyBQ5m9ICKthdrAkZ+eYKC/A7NEaj4BFy1R7FcxgVr+lAn+R6dE+1GwCtT8/ZEwiFwp+IoMnAWoxZULfo8GyZavSRbpzLIt/2dg+s2GNOls3E271bGlXAArWqbJ1AUu0TavZe9+dSjH5P6EvJjmX006A0yR8h2GJf7fbQVnpxBnFaOTWHK4MS0ykPuwtw12i6fkUWU9sov9Q2MDk0vJH+G/fRL15Qq2YdgEHlelB2IwozX31XK9DzRM0z+6iFIpvRrp0JYLuW8NuPIzafwQivZqc7/Rgi0rOmUn+yJYh1cWjPpjEPR/weeq0XoW7tSbBVpJU4qU3L2pfiqLLnOIeh2/ALujVKo/rmGk3BsP4nfmvt4dwWMkPxmkkTbqTa2cLBlOqliDAz6YoxHJCTEFkCmtLeHq1V9vHHRaKBuGzxl147fFT6qXJIlWvEdciHdqU18qeTKRH71x9NNN8h//+BnjxGcSmS+8ICU1HD9wndVLLtuCUFdrDuoQWklKf/K8ugCfUMhvyBC94HYD/X7OR5g43Y974AI3qdHBp8n+5T+LGbzpVgvc020juAPBLUcpMIq78k91lruNQHdsWZPRplBZCBvSSLLNPfo+Z/EzD3n9RIb92YKY7vMfSLbjZNNCf/0QRGPm/bUFq054SNBygu2Sw/TX3SqHXOvF6soccJFE+77WeBftq1ekAMPlBqpRtd2JYA1NgYaq11g/9K7GXyTtvjZA/50RCmFJgpykB+jAeh5Rb6dY2A8INqJcs4kIXoPzdDxcv1iOWSXklc2iBXLRd/W/k15dgrzszOfX8Ua2Qu/89hnm1A/GlZmezWBNcy2jc7b2KaW7c+keCw3iFpWX3EBPnVlcIGZWGqW4MQ9+W5pD+H67Fb0V0Cz8r7uzmIqOp5QO23Vt+HLTEiMgACfebHk0Jd 5AQw7odj kFfiZAd9LWPdZP75Brjq6eOwq4nzdQNLu3QbZQbNzFkpGRVDXYMdIIgCz+sHgZ8GxMf5bwkMT2UpKxOKAVUxQB9zZeF69LDviDkcX8vAqBfmYE5Poo72TuNdkRMrw5qYWJJUi9zUE0AHoM7zlEwwNQ1+qXG7xhxQxiC79V/lCUA0CuYG6K4TDYBdAABkXO2juzvXLRnLlS20KcQyUuM04pwtzqmeUGUENFdMvcvXZ+3f3CEOlFWK/BXeLsuLQewNNJSuSGvYuPgU5a5TphcLt/CC60FtgxKvEvOw3OHl6M9Cy+4taZf4VU/wzhHeHowjslPfcKtSsqHl30rWbf+DbzL8jmSu/dIQGsDV2vnOXd+lO4GANMUyIxS63E+w3dUiTnI19GVy4vI0Q4cQZ3Paunh4YhuASSIUdWhWL/pZRYUlfFdlHP4j3CBYOeDQPZC+avtusBHcb2HD8aI+bkX48zDirjrSqKh9ZAoogwOIHJmlfIOGVC09RZIduvg== 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: On Wed, Jan 24, 2024 at 10:21:40PM -0800, debug@rivosinc.com wrote: > From: Deepak Gupta > > 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 guarded control stacks 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 | 150 ++++++++++++++++++++++++++++++++ > include/uapi/asm-generic/mman.h | 1 + > 3 files changed, 153 insertions(+) > create mode 100644 arch/riscv/kernel/usercfi.c > > diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile > index fee22a3d1b53..8c668269e886 100644 > --- a/arch/riscv/kernel/Makefile > +++ b/arch/riscv/kernel/Makefile > @@ -102,3 +102,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..35ede2cbc05b > --- /dev/null > +++ b/arch/riscv/kernel/usercfi.c > @@ -0,0 +1,150 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* > + * Copyright (C) 2023 Rivos, Inc. Nit: Should be updated to 2024 > + * 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) > +{ > + /* > + * 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_volatile_goto( > + ".option push\n" > + ".option arch, +zicfiss\n" > +#ifdef CONFIG_64BIT > + "1: ssamoswap.d %0, %2, %1\n" > +#else > + "1: ssamoswap.w %0, %2, %1\n" A SSAMOSWAP macro that conditionally defines this would be cleaner > +#endif > + _ASM_EXTABLE(1b, %l[fault]) > + RISCV_ACQUIRE_BARRIER > + ".option pop\n" > + : "=r" (swap), "+A" (*addr) I just ran into this on one of my patches that not every compiler supports output args in asm goto blocks. You need to guard this with the kconfig option CC_HAS_ASM_GOTO_TIED_OUTPUT. Unfortunately, that means that this code needs two versions, or you can choose to gate CFI behind this option, it's supported by recent versions of GCC/CLANG. For readability it is also nice to use labels for the asm variables such as `"=r" (swap)` can be `[swap] "=r" (swap)` and then replace %0 with %[swap]. - Charlie > + : "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_SHADOWSTACK, flags, > + VM_SHADOW_STACK, 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.0 >