From patchwork Fri Mar 29 04:44:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610057 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 5347ECD1283 for ; Fri, 29 Mar 2024 04:46:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 81DAD6B00A9; Fri, 29 Mar 2024 00:46:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7A2166B00AA; Fri, 29 Mar 2024 00:46:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5A6766B00AB; Fri, 29 Mar 2024 00:46:31 -0400 (EDT) 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 35C146B00A9 for ; Fri, 29 Mar 2024 00:46:31 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 024F11A1092 for ; Fri, 29 Mar 2024 04:46:30 +0000 (UTC) X-FDA: 81948840582.21.2D2E4E9 Received: from mail-oa1-f43.google.com (mail-oa1-f43.google.com [209.85.160.43]) by imf06.hostedemail.com (Postfix) with ESMTP id 28C6F180007 for ; Fri, 29 Mar 2024 04:46:28 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=29WptZ+9; dmarc=none; spf=pass (imf06.hostedemail.com: domain of debug@rivosinc.com designates 209.85.160.43 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=1711687589; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=tRYuUV/HDlw+87fPFfw891sAepuDTsGv4tytTH7IM+U=; b=75OSTV2ZtQopg1gdYc8ywPu3M6ToP1fdYXcTjjEn1k/Umk5GY2NUJEIMRmU5W2JDljowJt nnys+5TmJ1R5FIY57Ja/48nAEB5ieFEzG8ozLP+RrPKoHz7Gf1YLeh3gnd8gQpdxo8GD+P l6MFPHv3Pdo3Er+QfAUl/jqpWPb64Ho= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=29WptZ+9; dmarc=none; spf=pass (imf06.hostedemail.com: domain of debug@rivosinc.com designates 209.85.160.43 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711687589; a=rsa-sha256; cv=none; b=30k0ecSvgVWmiJbTQdUJoi3f45pe9xuAiI2gcsb/pVg3sLNtBMxDE6cVyXBfV+0Uhl8MWb nHp06invcJf40gUhDCFfPdjETCKQTLBEcRZDYAMiGLMVGFW3ZNjXsl7ZMC8mPllTy3f7jD luGthJGmgoBY40LTk2JfyIdNYhwBVZA= Received: by mail-oa1-f43.google.com with SMTP id 586e51a60fabf-221816e3ab9so778219fac.2 for ; Thu, 28 Mar 2024 21:46:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687588; x=1712292388; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=tRYuUV/HDlw+87fPFfw891sAepuDTsGv4tytTH7IM+U=; b=29WptZ+9peZUk4JxkRl+bFz8h9bIly9YOgeinWGpUpcI6QJF/AQz8PGMjtFuNUOqWy RPkEpiKfViqo/LNTgGpSvHtsvRa44K9lxzw0YxAd8raGLDtyq79rn/JF61VmSauXXQD2 sDxw8MEZjcGrhGmJCq67n/Nbbn7kBGd8aAoOpNyeS2y0728ALi5/K7IfcTBpD79skfu0 HujesVyq04W2xDaCIY6NXjfAnB/H/mCE6sq3R1m82pbe+XecLlwOwEB2GU+zWu+dXtf+ zBc1jpSz/jZy0on5YbPEAo5nITM9fH7RMWOdlNMTNcZnRysqB724hd2F80LGtq4nRq7r D5eQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687588; x=1712292388; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=tRYuUV/HDlw+87fPFfw891sAepuDTsGv4tytTH7IM+U=; b=b5vXn3PixpCjCP+pdN7KzBYYJb2v0HIoOjVQfYMnmd+1HCweQLNvTMYuC/GQkY1qme 2oVnQWhagF+zpMXGUs9BtVQ63WislZE9JulE9IS6ccUS77I77XLxk4L+xRmMDQv80g6j Izeo5/4NVoeJln2N5bS0faVoS2U3pShUswpdFALHeTpU/+4ea93ZzIKeA3s10e9ostx/ zCylJhTTCZ8OHDdruxHEYCz9+BM5nhqGUIVulKEPvgV1eF7Cc7s+A8NvIP0lbnundOee fyZB0Gr6IPVHvLtRG3qAs9cuc8mkz472qnfSpA57K0akZgN2rqwSnF3wMQ7NOKi3ItTa HdqA== X-Forwarded-Encrypted: i=1; AJvYcCXl2FDhEnLmTHN5i1loD+B068ymp+9JpI+TnPIU9eYsaTRyC1TmAeJgZQPHUDWAarbDvXNrh+16NvWk7eQK5X9lELk= X-Gm-Message-State: AOJu0Yz9L60zUExcaVop5DNZWJD9QWB9eayV5XmZ5BSKypqdRLZAWY/0 93yYAekLe4H8Yf7STaLLPA9RqurprKSt/bbPLeBFjkwOT3fXgCvjimVdEKNDaAY= X-Google-Smtp-Source: AGHT+IExHTclA4kNK7S7bjrk+V0W0UHQqweUym9pGZMbj3dJny2cMk1eOLNjAwq+8+i1fBbvmLIbAg== X-Received: by 2002:a05:6870:c48:b0:22a:6ad4:5c4 with SMTP id lf8-20020a0568700c4800b0022a6ad405c4mr1448163oab.3.1711687588092; Thu, 28 Mar 2024 21:46:28 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i18-20020aa78b52000000b006ea7e972947sm2217120pfd.130.2024.03.28.21.46.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:46:27 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, tech-j-ext@lists.risc-v.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, charlie@rivosinc.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: [PATCH v2 13/27] riscv/mm: Implement map_shadow_stack() syscall Date: Thu, 28 Mar 2024 21:44:45 -0700 Message-Id: <20240329044459.3990638-14-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240329044459.3990638-1-debug@rivosinc.com> References: <20240329044459.3990638-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 28C6F180007 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: 7g4d6h5747icewep9q8s5mt8k1ua93rp X-HE-Tag: 1711687588-354577 X-HE-Meta: U2FsdGVkX199aT629JJY3fxRzoh/2g1FVflXNESF1PF4CGQ7HUPo4O65CZQHLsZJ1o+uNgQ3kxKE7HdDvC1mb1ZNMLe0HhNZX53SCRCyrCX+QrADw41PTNSiKpsyvwb8kt+T+gEH2jCN6Rdis9k3ryjVeW5p/qZBYfh18msvEW6rRHD9DjbYNRQUAjCeFqnwhmtO+D358gjuHIXvREhauoeQSd5McPRRJSC85OpxMEVKfj4udbcJDluHXZkbtYoV2ybsWE+YgjP07bBXTeRRegZ3/7lLF+vI8/9NjhNrBYqpVNEXQ6Z5rpEZ7CN1WdORcJ+MLjYEgKZzW7/ZuiuweBDDUVb+Yd+xH0cfer0VoxIVbgzRSZ18APsylqL9UiIRFNt2vt0lgqfjtYxzeEOAK2mgxtTNlB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FVu1qGH12E+mA3q9NgGbI2QzcE9OjVQHaPhLTmhzOQe7dgE2euCFvnfqDtfOozu/qDG1 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 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 | 149 ++++++++++++++++++++++++++++++++ include/uapi/asm-generic/mman.h | 1 + 3 files changed, 152 insertions(+) create mode 100644 arch/riscv/kernel/usercfi.c diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile index 604d6bf7e476..3bec82f4e94c 100644 --- a/arch/riscv/kernel/Makefile +++ b/arch/riscv/kernel/Makefile @@ -107,3 +107,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..c4ed0d4e33d6 --- /dev/null +++ b/arch/riscv/kernel/usercfi.c @@ -0,0 +1,149 @@ +// 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) +{ + /* + * Since shadow stack is supported only in 64bit configuration, + * ssamoswap.d is used below. CONFIG_RISCV_USER_CFI is dependent + * on 64BIT and compile of this file is dependent on CONFIG_RISCV_USER_CFI + * 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 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)) + 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 */