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 6FEDAC47258 for ; Fri, 26 Jan 2024 00:44:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EE7B76B0088; Thu, 25 Jan 2024 19:44:57 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E99466B0089; Thu, 25 Jan 2024 19:44:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D38BC6B008A; Thu, 25 Jan 2024 19:44:57 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id C3BF86B0088 for ; Thu, 25 Jan 2024 19:44:57 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 917AE8049D for ; Fri, 26 Jan 2024 00:44:57 +0000 (UTC) X-FDA: 81719617434.19.B05D595 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) by imf29.hostedemail.com (Postfix) with ESMTP id B674D120007 for ; Fri, 26 Jan 2024 00:44:55 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="jBXD+/Dn"; dmarc=none; spf=pass (imf29.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.178 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=1706229895; 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=QtJqbwcd1aBKR50xMCAH1cHJy1bZ+E6DqnetizUbxLI=; b=p7oIjfRaQOMnkEJs9mjua6pmyA2FRxBHMxqbE10Bgwah9eHhISm9ZQ95a/f8SfQ8lIEiHr PbULMHjZDghwPEg/z2CSIotjPm4oxujN33vz1k4n5vjNjM2GH95j948JHNM/LIQnsx4Go2 yyGc+ilRW/uOXWY0+6wk09GQnih2mXU= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="jBXD+/Dn"; dmarc=none; spf=pass (imf29.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.178 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706229895; a=rsa-sha256; cv=none; b=j2bA+Afmo5CVhUeYD0BJZJ+/KwdA9ml2M1QZQPWnUojsHbiPa6Figc7Sk7YLRVXFgV8WoF cKNOw1jMU2OcgoPK3YaCt+UDTKAc+ig91V0OiEDKut8Z+rNaow1NggICSraUbrBUO6W6Ty 27ctDIDwloiB/Q1wlvvvkbEhUyhk/4E= Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-6dbb003be79so1091235b3a.0 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=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=QtJqbwcd1aBKR50xMCAH1cHJy1bZ+E6DqnetizUbxLI=; b=jBXD+/DnIRhvtcGYSHbBu8tS2wkhaaGHIX5pMJte7GQoywAJSPg0oV+wUOr7TPivQA 2z7d2hyEDk5cTHtV3Z9bKdTL59OT2EfdP0mbIDZ0QbR3YxISzNm9eJ2V1RSKw1SYIyi3 HlZfw/uc7O7NoxHKS2gDkxah10sFZgH9eMeDsk3BFDwyekW9AgnQs9DicfJLG5zrJFTK a09Z15N8N9cFlneCiJjAvnGcnazZvb2Dg9D/zJ/R3ARMbY0NrzBgtJFUkY4rO2NCJhK6 0Jfx6s5i7wGdF9ouXN9zxsKxmq+LYeiVSiBK3DfB1ZMv/lhrkBv9MdurgQHPVKff8eON rj/A== 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=OgUVtkOSpdOwkuns176DZiDvE+t8XUw03xrWVnjYJZ5oBpCiJx/HFJMZ7DpgbuXXqH 0Njtn1WOqGV3w7jPhpb2VHJqtbpxwr/ctanXcQH6kXGGLuKjQpJ72hwtdgG3BOte4a6p CVayOpQgKgmEuY6c6G8YJIBH8FWk6NRtF9ScjVRD+wDD00/gALsLqphalhg/FdRLeCUs HcC32/5mchna6jFXR+jjV9hAXo0WujHg7rd1Jq+iAX6qUdSW3BBkxnLpShLIxfi2aA3F 5Ux9ud/SSDBXcU5/JbxFaG4mWtIajwZaXW8pz6pUVvA0ryyWntz0OXtimpCzM+b0h7lr Qdrg== X-Gm-Message-State: AOJu0Yy34NpTGs7zFfiD2oGn3izt/V6w4lqkL7jto8QUFIAKIly4A5My wyQn4vlkMrV6/jusD0ennYZozLeeZWtwutIRqCxfRhJyL6WLmMhEWkPxwGePMrM= X-Google-Smtp-Source: AGHT+IH+Bxa7letLB9xYoBzAsfuPFFdoXoVju5Pp59vvbK+VMz3/T3dc9A+D3CKZT1bi+po/3jE8Ug== 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> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: B674D120007 X-Stat-Signature: hsqktzrj66i9gmrmhh36io889qtpdgsj X-HE-Tag: 1706229895-67149 X-HE-Meta: U2FsdGVkX18XdiTWBTIn+zDvQvbUDDGeaV/QDucp9ikgMaSoMmM8lKdZF5hRT11h6ELv02U9gaDYl9apvJ7T8XPM8A3vaYvUeAahpP6aFsWJOJuDkkkdBxUceYXM5DElnhBL6XjNfxxy8SgjVZQwRtNpSPvJihkj+5mQ7820htS/1M1YYlnp7kl8kJsFkmPenzVMZXVz21mXJEK4B+0HA2d+/qvRNUDsePztxER+lYo9S4DmrPTUtwjlcqfsyQqVWwyn+ooSndKPls5E3H/t+G49fYn0I8STOu3P+qqAuw+e0He5k4Qm8RxSNaic2e7xyAuhqjtnKcfaxT5GBc2RpOot/kryBSA3/d/w+W33XEOm8qWSrhk4MEN1+saJ3qPhxMLfPf27TVK0wL1Hn39+4lrlYzcoOeFP7OrEVVUbRKh1yjx35G0OOzx8fYtgFGL/WtQyhSUyxazySZxQoRvDZDu2VVn+gjW9vrTagSLroIxbKi8yjs+LM7LaVl2l9wkuya3nYJnWZJH+oNDz1KjVszxutA+5EU0m/jeBrmZl6mhjeqtlIFf1w0MyUxqRH4Ke2RjhEuh4U9L8uvHuooDdVXvvm5lLHf+YvsJfKjSnhBO/AJ2nLmb86FV6SvnKElg5A7N+vxRsTuoUyA2TQAIhwvJQkeqgZgrVlH1hAkfAYmdmxNAskCgV+2jTbxPztzGSHlH2newOmFZHpY2hNqvqYlFFms8MPygLDASp9cYiAuS5/vDSs6QUnfF7pJzspb5LIDUtwmYdvDje+0WCl1xP72L5AT5Eu4YtJUxSVm6lHQVorg2BQlXxJUOpG360NzT3NM4Fwm4cgzERhu8j4BD7oX5FH1qiFyZI22ZJepPkfL/L7N60NIg0Et1SVguTvYkN6bf99Kg8Blq3c4mjdL1pKbXs4erMR/4C/DD1HEWgvtcFtdQPi4BwIEOlL3LKNvNf8pK5nIeS8pPlB3lcWFC DTbNR+0J 1bsSV9eyxHawP7uRqW1+kvRSK/w+wvUkVppFEI7u8lMD6H2fni+yIcJb3vMg1WbmAPc2f19LuQX8HfqCb/nUg+CqujlKbyEg/ZAT8LqoqvDin/JxZ0gGKdhUbx+2btjWi8/4tF6gldvkTPxGgddC/rXH/Xamj51lrJLwVc5OkqsrviezOwuVPTT2dReu/1PiB3CqWpSg+Ze5nlDlHHi249ktVWpd2JGxWtSrfQz0E1kHZD5gaVODsRDSlEoe8qJK7df+B0NDNY3t0037kK7EIecxjmSsay9oGnf0fYnLUY3HUgkTRP9nJZVsvMibyZTmr9EMVhahVwK8Xjhm8pxU7aVA10b8pKs01r7QtNneVmttzRD/EdRLEo/TyBuuJVNg91qRuX0Nm7KE3ktmFyGMFhYGbyHZgZUMGh9Po+PcUXvqAYvldkvm8jHH3qaWIrYXRNukn6ppa4MkLFPBdugcQ4DeHLerO+ESYvC09zJetsdOKTdFqJHS2hnX85DWMOZDsLsiA 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 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