From patchwork Tue Oct 29 23:44:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13855730 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 0BFF7D7494E for ; Tue, 29 Oct 2024 23:45:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E78B66B00A9; Tue, 29 Oct 2024 19:44:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E27E06B00AA; Tue, 29 Oct 2024 19:44:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C54226B00AB; Tue, 29 Oct 2024 19:44:55 -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 982086B00A9 for ; Tue, 29 Oct 2024 19:44:55 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 602691A07EE for ; Tue, 29 Oct 2024 23:44:55 +0000 (UTC) X-FDA: 82728271290.03.3D6F1B2 Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) by imf13.hostedemail.com (Postfix) with ESMTP id 94D512001E for ; Tue, 29 Oct 2024 23:44:27 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=vENxF+iW; spf=pass (imf13.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.177 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730245335; 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=WTy3jgo4L5ghf6CNp6bUsFhPOXP78WxPgtMQHKh4Cn4=; b=tEnemZpPIK1EOVxkvIIZD2ScjFOyj3BC+nFpJt6TEWJwk1Zywq0xbmpxouxmx7Zj1MDV/r nxnPKury103B9stqoRFxUMWBt+AHegKqWhcrQ7jXcMrZbKi3MCsMY5HI7ODOxMyS4eprrE GBFoWYlxoes7bzoYEjjDeiBGHfvIvj4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245335; a=rsa-sha256; cv=none; b=cENKMatkND7q/bW+pjSRwtHEtWISZALavKh/iPcOdzE9eVXG+stDUMvseXTzLDB9wTkCUg WDpF/LX8odI/XFl3CQuBAeqeiP82KltMaxzMzOD32OKAs3ElnO+5M59ujmqDW42tV7IAiM DLarbBGwWiQU0ySTyi6UwT8kg/v7u4Q= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=vENxF+iW; spf=pass (imf13.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.177 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pf1-f177.google.com with SMTP id d2e1a72fcca58-71e681bc315so285558b3a.0 for ; Tue, 29 Oct 2024 16:44:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245492; x=1730850292; 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=WTy3jgo4L5ghf6CNp6bUsFhPOXP78WxPgtMQHKh4Cn4=; b=vENxF+iWKE/vmc8PdzwOURNMjhFqk06snUjWRO/6b2235CGh55LG3vZKUeGymDwg4S yyVjnRbPUemkXbC2Gf61Y3KS6C1v1PpMDxNIkyXperO8KNQsUhqOCtgIUYVQ2YWD28Cp apTjYC9KrEVlwin06M9eJu4+a9dZCTHkPfAM1bmH770Xru9xE5+2dh5kCt+4qpFyRLqU PfMsVis9Ua3XEQCVm661xdLMrEkIZsmCYP4ZNhl46NPDAs8JbNtavRh99yu3bTew1oWk PfnlIWfpUoFUZOgimTL2Se1hKAOJbZp3dE5Nwk82ghwSCi5sjMVrTaKS9GlFIbc4HEHR Vz8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245492; x=1730850292; 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=WTy3jgo4L5ghf6CNp6bUsFhPOXP78WxPgtMQHKh4Cn4=; b=OaBt++n5Wn+QR1Aajbqer0J9dxP2Ylb+dp7HA/BJ3q+LYQMVBF8ioe7B6cAuZkajNr 1gZ1r/zvKqNy8bddl+A0Y/+avQGzNi2/tpsrCj5+Pe+sm84f0ddziveZ7vRm2sOQUz+q 5tYaSoPnFauVoPPoPNfG0hQGBUr7UYgTuZB9Vr58J5vBIIPb+aF+33917qzugcKQVHfG G6tyVt7D+x0rGeoxit+bLyAROHSA9ZLWzGCUkfH9Q46FGdjq7L4TPq2OQoq5gRd/67kU /P0/F4bd22QVYWcE+X36LpGnqdhyajsotAVPtIwE7p1WQ0UC5BVBgG/SxwrTE3hdKsT9 h47Q== X-Forwarded-Encrypted: i=1; AJvYcCVXlQr00u8NsAjcaVT1N6tbyi0jI8s/lyGUWaP+BXr7gUCs/kiItEMrqzXSZ9hHwrleTf29VBg8Kw==@kvack.org X-Gm-Message-State: AOJu0YyZGYTLoe+Y2fHGGEl/98zccqgIStaluUlwpaG9PIkJsve+cZzZ Fq0gZB2SXJ2lkQ9s0onT1w3GomyubzuuN7BPgY4oa/ffBBxovfSBtuJX2FbasC8= X-Google-Smtp-Source: AGHT+IGHQOnVKyl9WNd/iu5rfPsmyEZbKQZ5k/k8VUqC0xQ35expKJ9db4FlQ44USVq+RzbAsl599g== X-Received: by 2002:a05:6a00:14d3:b0:71e:7f08:492c with SMTP id d2e1a72fcca58-72096b9a8c8mr5130679b3a.1.1730245492144; Tue, 29 Oct 2024 16:44:52 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.44.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:44:51 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:14 -0700 Subject: [PATCH v7 14/32] riscv/mm: Implement map_shadow_stack() syscall MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-14-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@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-Queue-Id: 94D512001E X-Stat-Signature: cxqu3wi6mjqkx6cc8kou3j7oig7w5qpe X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1730245467-674413 X-HE-Meta: U2FsdGVkX18mYTF1Mol5Jgxz7e0F6wglw21VKyBVtdYAgCClj6l3Rv3W0/BDXGly9t8Uv+H+9k8BtgFNkcluko7QLQcbAgyrRRF7pdXYh1+Cq39r8f77XPjSs3s6Q+TgM/bYDccXDWkgpIPAF6Co4xIYu63T6yrd9hEI0kzXWcrLVT+nH1RwhPVwF/PvuDd7elKf5WY4ECcVS0r89HF4qKknH+tN6jw8QfyNb1JmpWq2IVDlLQdZ/jxxrwaQ7fUZhu2fp37BJ0xeh/8arlXRXE+tt8U9juLN6JLXnBYhDm5g4jwkNfnMao2zJoJ1tyMIDw1nnKwES45L1/iVFmHxMNlrB74wkcR2whupvwJ5IL98nQH94RK5XxB4NweskqYh7TmThmqvM3Ynrzy9qx3H0jUd+XGbi1hE4wFmtChlby3J/4x961H5g5pD6xRQ8PKQCoDQvErhHP10/znmQaMKgvjhpLIKzfcPHIgiGzKhyeRiDTLEDZIcWdP55E2xPMRmGe7nlOZfFfxmWvkTIAyDEkB3qBZDVDXldLjbEGrtzgFqu2TdFHVJXYUDQBtvkZfpEMmp44uUu3vCS8GQNp47F1m72zb32dN7ZSFV28bLZUXiQjOh1oj6xo1hg5zAH1QsyJtOeHYtJJgB9k5zxyp5OScd3KZJM2TmhB7QTRAmUGjRlBGhCirY6yZ6R7IzX6LXhZYS/FcRFRys0HSmGLdr6t+mlW/ZO5KtMkx6+U1UEYuQnlemk6c4LvJ8m/EkUpKCf2ehzwRzhoiYAY637W4z3O4P/qFFiONM5nTYYlxfnETM8zMjLwVn8GSOhwVGtYAO7CDYpHjxNDBt+9p2UoA47kifs61Vj3N0yiUNJfFUxlGBCJPdICHiuSDhqfJJcRVEDwRNcyQDcztyA7bn0Kp3oN1i4BBQzeHpDQijod2ZKpdu7qa9ubozKHEvRqcXJ5EaS9Gw3D8I7Zxt7mBLuob cb0i0NUg ZRp33WhsWyX0ZuxKhkP7zakIDTtJ4sRCWIPvHJWieBu1ns9JhUUc7qeiwtKH0lSBluxWHpUz57QnOrwTfYzD0khzPLT+/gikyqt2OLPnKtpalXasinOfFHQ9zlY4LdK4Ml0qmhS5zIx+0eaod9FHn6+utf7kRZUdeSFJhtlBU4bv2FiJJrqFskg3lsy5SNiehSeFg+C4e/KXTyl2gmRzVTgcuEGkayIp1bCrHsxeus9eIqdKWh/jfnc/8xU3dcMOiTsGaC4Dk2j9s5SYzjU7h3AK1y2LDpgkNwwAdhMP9YzsPFO97PTp8u3jQZcsxkChP3CuwqGoskMoa+5wVBZtS9UYiXeQvGuyLtAcjqDapHZiO8Jd+ALIvGRMY1b7qjfyccpAVFFJWPZGXMnlW4Z4Bw3GxBMdxc5OMv0JdGOaaUxeoKksBeOeJ84T81GR23KvRllcdTrTBGLG744H4qw6nLXjKZAEcJ1aS0h1JZiSOSVC9266GNJRY9IY10dJ/gx6k0d1m 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..96bb324abafb --- /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 */