Received: by 2002:a05:7412:3290:b0:fa:6e18:a558 with SMTP id ev16csp312679rdb; Thu, 25 Jan 2024 16:54:29 -0800 (PST) X-Google-Smtp-Source: AGHT+IGtupNfNI10FD2jS+NHfhnNrpttAp4uk2g7/BomvwNSIP1XwIjAd1uSRO70roaRhjuOszFu X-Received: by 2002:a17:902:bc43:b0:1d3:bb4b:dc04 with SMTP id t3-20020a170902bc4300b001d3bb4bdc04mr214481plz.17.1706230469403; Thu, 25 Jan 2024 16:54:29 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706230469; cv=pass; d=google.com; s=arc-20160816; b=WDQFp7TVnyoWWnO4gAfbSBc10DioRMSbZZIBU0LJ6aK37Qu7rptUkwwlJbZB9RbXue RwYT4NqSWbYyF+kA0XZL2r64EJ0O7+9EvFkyPO5ULLAsWQLCg2SW9feinkQMgeG62DWU TSui+2n1eJERAe+CoXAlKkhItBkosJ1pbE1w7725Tn994Cm1EQqw5fxJa9UICFq8avbC qguvHTMvPyvuZuK6sCEVeSoNODPoldcPuJmLHj/LLZ8pZ6r/BoBUx1cO1TFMeGs7MMzu cv47WqoWsoMOtj1pRz1lrUIKErA7w0TXR23RbZu0UdcMovmYAr0XPC+spDWtWISLUosQ 13BA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=QtJqbwcd1aBKR50xMCAH1cHJy1bZ+E6DqnetizUbxLI=; fh=8SwXoOpF33PQ/+MngmeQQE7XWMRI6GaJi2LVQY2V2d4=; b=CCdUlt0OkROL2pbTZlhb9RbC0vSs0ifCjbZTY1jDXltwODEOg0K8vo9rSu4Kiqm7dh b/BLUrXIoaQahoC8HjEDSqS0H7Za6t39LLa7yfMSMfNFtdVKPKYxDe/e4OtugC+0nmFL 6G8mpFlwVFoVXWJTXxNhVAyVutxyJ6IuEv0OrnsQgp0tVOG9QH3LWrxnG0KX+RLDN7rV rtfhBXYcj2v9Xa1nrpNrtU4WKN4NGQ9Zl+BHOt9MLYRuUzNu5PJQV7rdjkQUvj7sdtnX 7Tw7/r7p564Jb5zumwCunhk71kk88MjhV2ANK7SPKpQZmZ32Jk24IOBdocf6UbnU4twP H56Q== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=jM69+poj; 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-39466-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-39466-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id c9-20020a170903234900b001d75b96c0e1si154785plh.462.2024.01.25.16.54.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Jan 2024 16:54:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-39466-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=jM69+poj; 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-39466-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-39466-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id DB9B1B210DB for ; Fri, 26 Jan 2024 00:45:09 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 84121138C; Fri, 26 Jan 2024 00:45:00 +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="jM69+poj" Received: from mail-pg1-f170.google.com (mail-pg1-f170.google.com [209.85.215.170]) (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 2A13E64C for ; Fri, 26 Jan 2024 00:44:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.170 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706229899; cv=none; b=Vp/Q1ubDqAzCQDpYOOiQxmIJAWN3OMEeHabD326kxfzjq3Za5F6HdSQGK/VEhE3v2anZCwZtiUHPVGxdp2Rw9ExssGBMgcALyfYP03lQ1Q3ZOSsVMMxtGSnbLmttN2rRfnExOZkc2ER6LRxYzIhXE2LNwrU5AQZd6jRKGjdkBdY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706229899; c=relaxed/simple; bh=bE4fpJuHj+yRAd5Uk//jR+Hp+5mYAmB2aXHCvPK+aUg=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=FVHmx/q0t47XW2VuJEjI3gyjhWmndKYGGt7ESNZg/hi6jirXdYJhEsnSUabld2YL9LGI7WGamUjrYhX+28Rc9c995J7MqMgsLy6WYpi/B3WFBG1qdYpu11z12FCkGxULLuAHXMlkoNDzWMTpNtTAfD1YAUVcZPSiRvUd8BYuKKs= 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=jM69+poj; arc=none smtp.client-ip=209.85.215.170 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-pg1-f170.google.com with SMTP id 41be03b00d2f7-5cf2d73a183so1021515a12.1 for ; Thu, 25 Jan 2024 16:44:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706229894; x=1706834694; darn=vger.kernel.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=QtJqbwcd1aBKR50xMCAH1cHJy1bZ+E6DqnetizUbxLI=; b=jM69+pojODvLVe2jrMKa/EaXdecb9QK8Y3YQImKQ4klnJfHOXd5nr0FUJSwOGtZuWj 4gXUeEwcAkZd3xqxrV3KOLd6CZW3ES9dTq3ouBLYUSmWCHzfVvJes7ph/euHe4zO0m77 MG3glO/7AP3oo4hRhXgneMINfaWZiKIrml/ScEeoElXizP97Dp6F0vlPI3nn9conbYVl uUlEfAPNhnVNsQnsG6qZIlO1ufQDDSq8Y3NBnMHkdbL4aH/Ev2pOp5RY7COr1kzGCwjw FEhZ3twQbYv+Tgd0d8RJh6mU3e1egU2N5wd/DUCidAn2Esgg7cfw2si1yCl0zQlIhXML GRyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706229894; x=1706834694; 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=QtJqbwcd1aBKR50xMCAH1cHJy1bZ+E6DqnetizUbxLI=; b=ZP+hxtiYP56bi8duJB2fO1EzJGx1nlRrA2FNP5P8Tn+CseM4pjr1COCQ+ePMj8kFWn ucYTBXt7oqSg4M1wEbBWZeX7SxUGCehox82pKVfzp7hvpyt3bw5MnjrDeKkimVCTitb1 Vi1SNie9Qfwdu36O4oYLZ3w8wmiUOFSTt3AlFmXE40OLHj+yXGTCJhX2HQ1UOMvHPj8D WSxbSplhOSwQlB4iVOXtcakSDfONfmc21SiARCNjKqpVJDQpu1Dju8gYrDSr8mbGPLML iZxZEeh2BsCrz7yDQ68K+8UC23khmLlemxhdIsp2FiS4DuS2XC4x2Tuk69kT+c6hFh7W APPg== X-Gm-Message-State: AOJu0Yz4GUuLXfcT7cAbMIxrFIg784aoH7r7wHnQ8zNCY8qX3LyhV23/ 5bbgqFePADzmOLeXwO09207ztPOIQG06s1DK9rl13vV2GjADp8ClI6pqB+yF/HQ= X-Received: by 2002:a05:6a20:12cb:b0:19c:6dc3:117e with SMTP id v11-20020a056a2012cb00b0019c6dc3117emr198322pzg.32.1706229894471; Thu, 25 Jan 2024 16:44:54 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id l2-20020a63f302000000b005cd8044c6fesm114817pgh.23.2024.01.25.16.44.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Jan 2024 16:44:54 -0800 (PST) Date: Thu, 25 Jan 2024 16:44:50 -0800 From: Deepak Gupta To: Charlie Jenkins 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, 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: On Thu, Jan 25, 2024 at 01:24:16PM -0800, Charlie Jenkins wrote: >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 noted >> + * 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 Yes I need to do that. Infact I need to gate CONFIG_RISCV_USER_CFI behind some riscv-gnu toolchain version as well. Becuase not all toolchain versions will recognize this. > >> +#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. Thanks. I'll gate behind CC_HAS_ASM_GOTO_TIED_OUTPUT. Earlier versions of GCC/CLANG won't have CFI support in them anyways. > >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]. noted, will do that. I copied it from gcc asm snippet `amoswap` somewhere in kernel. Goes without saying, I am terrible with gcc asm syntax. > >- Charlie > >> + : "r" (val) >> + : "memory" >> + : fault