From patchwork Mon Nov 11 20:53:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13871348 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 BB25BD3ABF4 for ; Mon, 11 Nov 2024 20:54:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E2B726B00C2; Mon, 11 Nov 2024 15:54:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DB2E76B00C3; Mon, 11 Nov 2024 15:54:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B93326B00C4; Mon, 11 Nov 2024 15:54:27 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 971BA6B00C2 for ; Mon, 11 Nov 2024 15:54:27 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 53B37A038E for ; Mon, 11 Nov 2024 20:54:27 +0000 (UTC) X-FDA: 82775016072.10.29B7493 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) by imf26.hostedemail.com (Postfix) with ESMTP id 600EC14000C for ; Mon, 11 Nov 2024 20:53:54 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=n4YqddSn; spf=pass (imf26.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.174 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=1731358273; 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=WJnRmE7nzvvUdOgcQw+N8m7/qUjw6OBKlSgXkJR7mUo=; b=K2zCbhbGJoG/WIP99fZphZFoJYFxo7G/mfiji3HA0WEH9TonTk4GMSphkYPylXXpl37QBP 8BTmNet1kyq05m4ncHWez48knjFFfSkrrVjU3ngBvO1CZYFW+xQcNBRIiTBw3x7DQeWVQR m147FYmsnvvB1P7w3jXitkC6fkD8V7w= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=n4YqddSn; spf=pass (imf26.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.174 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731358273; a=rsa-sha256; cv=none; b=mPZ/HXBBkpzPQjQ2LVWDGpgZqIUlg3cDFyM+umGQkoGjDCLT2gcOUvG4XIJ0sCxSLR8GA/ iBMuyvSKvfCZ1aUA2s1oAgg689cfBUYWrTV5bIfMae3NlygtpQReyjQcT9i9H3MujwqUy/ W8Sao89779/ac5u8Dc3myZLFjdebeJI= Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-20ce5e3b116so43978045ad.1 for ; Mon, 11 Nov 2024 12:54:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358464; x=1731963264; 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=WJnRmE7nzvvUdOgcQw+N8m7/qUjw6OBKlSgXkJR7mUo=; b=n4YqddSnaD6ZPr+QAbC1lVE6mopfzOSSr/8QBw0TO+tn2FSSAfyMc7La8CPnpsdKv4 83LCrV+mHfIzUPttpkC9PCGmnUnPBlzTs9CS7J2dv4+OG624rff7RxH5NZROuh9qg9PX l0MpO3kwuIpBmGWRSGx4Dl7Cravd0CLmXsfT5+6nggNsZLAwsSafr/VYNhFk72y28OnX u8rZKjeKWf0Vfn6t/e0KYKSeI00f9m+nqfpVWpp4pxPYERVsnWKBXUTHiioGAbpdBeyL wzP+vcfgt9u3yGJgtA7gqDxgd3n2x0FAAXqiZDXOJ1e8KBlf5kJnOYTzB7wSgHU4FGQI ewYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358464; x=1731963264; 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=WJnRmE7nzvvUdOgcQw+N8m7/qUjw6OBKlSgXkJR7mUo=; b=WUEvD/6e5I+6rA+qZhZ7em3+x/J9ajzrdXyx+sfyKLP+BS+q6MnvPVw/nyHhIlG3zv gQnn/s/3eQZz0hhafUgWRxzQ4fdW86fkxmVNuapEr5cBO3Et1nbwF2v5s8DCl9bhY2p3 08iN5TYVGbokPoVq1C9jltYgPAoKDrdL4NdG14xoBNSJ8CYEHA8Ka7B7IEfMNu6gn5ou pmfy6RnwHZKQHUxsYSV1PVV1iSAEU0ERD1bWeEJGS39uG2989sk1uZdUVJZDxCze+UB/ 64lHdAO75OWONqJfLYEzHjgUstTnBCC/mMqqDinlmWxY2uVk85QDNFthcDDANsHB0CNw FCdw== X-Forwarded-Encrypted: i=1; AJvYcCXRYA332UOh5QgbLSJjcuzGJRd2hOiIqByxdWzMDUbzEaNTz/07TrnsB74eiAlTQkHVXRDBgMQJeg==@kvack.org X-Gm-Message-State: AOJu0YzL0a2lI3QzaV6mlVpizWX3tVNumOUPeEhVQoaoffO7rcUwmUBo ySOK7MJof3dZsmjYqJQvl0eiCZ55U1aKIJroesg+w4DEYCgEuf5L9LDh7Taxh2Y= X-Google-Smtp-Source: AGHT+IGBjJ2qmkhI/9KkSAaUQ4/thfZExOudz5gsftSVr6ezuggghKNqKZsYYiLOvkR07Zl8T47kbA== X-Received: by 2002:a17:903:2cd:b0:20b:a10c:9be3 with SMTP id d9443c01a7336-21183d4e6d4mr180460215ad.21.1731358464144; Mon, 11 Nov 2024 12:54:24 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.54.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:54:23 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:53:56 -0800 Subject: [PATCH v8 11/29] riscv/mm: Implement map_shadow_stack() syscall MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-11-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@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: 600EC14000C X-Stat-Signature: sirtupxad4o7teyiske9myq7h36n7ehy X-Rspam-User: X-HE-Tag: 1731358434-519078 X-HE-Meta: U2FsdGVkX1/IWoOH4NoHK84v/Vj8oWfBMzTVjD8k+w5s7Je8d5cp6HYNQxxPh3gdY0KHthtDK6sSeNYDP9lIOPLH+ZO+kDDxIdr6HrvOwuUrqw2uWS7haVYOYW5b1O5TfTIXDMMNU8TnnYJAcQooxcnGQDOSTS1Lp1QrbaLAX39o79PT9iJ2T+wFFTO7AUYh/d40AIqdMOlOeGefK2RWvXYEC2D+9xOqsUCeAclv+Mz7UUgsk8K6nXyS1CQ4Aw06CcDacNyynD0DCbA09qKidcflNGYEXq2ka1R/7ceauu5jw9cr1qUqkh/oa/OoI29FTcjCjb2J2WOvhTf/GJ4A2X2Y0CvYYl7bT5z9lYo6KuzxoZ4rXQ0a9gI4+IaF14t1H7IrLBv2SKyb+bKrR5aBo8L2awnBM7gyF2wJyUoTicnkqhcCZq54v5WtjIIj3/Es/l27ZE8YX8YjGycOk0iwpCpKt4uJ8wtzM5TfrEmR8rikOI4amHS2bv+gnI7O2fAjeBgrZAaV8Z32b5IfzfCLaKRp/Pfu8VgzYkSSB+X240+Zi0gxnGxGxfYX+Q66lMx3oXyFZJ0A8S86MWTs32BQYGNttwcOVhHG1YkScxZpR990NmMfdNBby4J3Xcjnx9O9dSv/+UQrWgu0uLCYTf3okMe/kQQa/mcAkxHN3yViWMoxQF2FWZGJS3TP/4oaxhpK2+AHrWpYH1rQGe1QUojhPvr2sgorVj7YxSaN7S6jwrll1ZbQV1NHZXttwycxk4O0tAKpKO1AAx0SzeHifqzFZIfI+nL1DkixTRHOzCVeO7Ej1la5lKuheid8aGbp2h1t+yMTNHoNFfa8pdgLA+JrsF9Rf++5I6XTjGHn/LL03PG9qC1D9M92seXLc1KK1KlIVLSYhzcxcY6sidg2YBNKsGphljKlVKU/9W1BMuhBL8tgrSTZnS/Y7Hce8xOXhWyRiqqccMC7LcpBLblS+ux 2pDBTe8Y rO7TbUjYEX0wYd4poyNvQ7SUTZAH1o1IPWeVfpDdL00GBa2f75IN5yGdw845be/r5D2TbdIWXQ6Q7qSJENkTNlU/eNcfnuoC6KNAZKDOWaQkY54G0GkCQrIZ6bRxzdFHY+FBb4hVedXqR3RXeumXPBmASB14fsk10G31PiSrtloY3GPN1bwngzoecDITPmRFGReKMk1vI0fLmyqe7MvuvbGYr43iBvE5yVB5CZmRXQ0cgYcG1yGyuLcgaVQr+3wxCc6bNTR0U//2Bw0S2Y8xzfW6jfJKDlUnkMXLgaGX0VyBkrNPeofTeTMuPpbFVlxRla0mDv/xj/Nc0sGKTZJgahLgG1ny52z/X/+YIxb3R7naGZOR+soOZ2bgKJrTIz2zJPGkRvI59Gg7Me/e3pp3B2r1CU4mpqc50cdCtJIt1o0LHNVJIk0SZlLSyS/TxvBg0kCfwTJkh/gDoYt+q7/AOfLqH9AyT4xQkcrpR/L9ljK4M5RG21o2wmtHcBbFVYsYNNC/c 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 30db92672ada..3f5a8eafe578 100644 --- a/arch/riscv/kernel/Makefile +++ b/arch/riscv/kernel/Makefile @@ -118,3 +118,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 */