Received: by 2002:ab2:b82:0:b0:1f3:401:3cfb with SMTP id 2csp868157lqh; Thu, 28 Mar 2024 21:47:55 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXlNnveSlaG6m5rRWaPNDk8BopjfEm41ZgRu0+seGo472XpxBvyBNfivL4GzaPYCuyI4CLLAAYQRK4KmMj5rpq3QQ0ZffyWdW8v5Ig5sw== X-Google-Smtp-Source: AGHT+IEFveoRpxnPf7l/Py9jnBQ0CmGRK1tpSOHxs04+/pbEG7JAO2MqobHuVFxyXNDRBcPGjE27 X-Received: by 2002:a05:6871:5d2:b0:222:11f2:11d8 with SMTP id v18-20020a05687105d200b0022211f211d8mr1247667oan.26.1711687674734; Thu, 28 Mar 2024 21:47:54 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711687674; cv=pass; d=google.com; s=arc-20160816; b=i7ufcRT9ISFmMfz+RdLMK140h26mhzmkYz2hzBLfHZaLgNvjOt5/turngtfb/ITBda n/sRReyzFr49o/PLPnk82vt5W9fdoe2zMNvQBBsYuuV1aonav9e+h0uhS89PSghjMyM0 5s+GFftsNkrYqNgITWFOKFX9pxrJ4SvFtfuxulnVpMGCdvutKzaGnxZtLB/PhX4ZIvxI MT6Snjrr4CXFhGB66spWQNA/E7StHDrNbc7wEW/r3Q9ZOKjGSJ+GIEDMZOs5CqjT5ijp RSSWfAhhUz9MOX42yNCbNOw618LtoeN4K2TjsI8FSKLevi1LsYIvQOyvzUZrhkCPp3dY P4yg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=pODlhOn2Qlpzyvoy16i8OpTFZF278Z1sqN5yeQeAUQM=; fh=G0kzFKgDafCPLzhKhDW3rAiWxXtNGhVOEOfhGGego/g=; b=z00DaH69VfbhCpe/GEyU3jKRbuaZwlnQkFfRSw/QzBo5bgUbsnI3KO/iZ85fX0egal lgBEFeVm+1qzG2UJAGtX+0iqu/TkHh+e2c+glpN/vg41qvN88+k5eHgWarobTxBdj8ra JKfyErkjYdwQ5wZ3JQRI9pVGH72XKPuQzrcMH4GtepIhCZXf2OONqYm/ryNAFb38x1pB NWjKH3kj/PHc5w7DsOl4qav8PwTlpPIPP5l51GGcZXK74UgF7BjYnU2vtdjDtNa6jJlx GxXR/BplUbst523k5Io1EWthd0ma6kzBeP7zPfpeSYV6CugsYpxgG3ZY72fagNACsKfa ZK8A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=eFB+HMwA; 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-124126-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-124126-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id k80-20020a628453000000b006eabf1c9e13si2949424pfd.290.2024.03.28.21.47.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:47:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-124126-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=eFB+HMwA; 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-124126-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-124126-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 5F44E28A816 for ; Fri, 29 Mar 2024 04:47:54 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A412E4086C; Fri, 29 Mar 2024 04:46:19 +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="eFB+HMwA" Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) (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 E979540859 for ; Fri, 29 Mar 2024 04:46:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711687578; cv=none; b=dobYyL+CxLYhZ1RV9Oh2UGO2rTMIyA8l5MRVppOw7CMAcOGP5UyNtiJJO+94l7fmmMw0SpXpBBq3+psAywMcvR0pgEcwrTfBkYF0N8HX0Qh9AB9AXcKCmjNuGZedFACMf2V1Xbh3e+As9ysIlarEazSpTrnHYcm6QpfpnWQ94hU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711687578; c=relaxed/simple; bh=k3xGskk+M3dZNxtOtmuZmmKUWDnDnrW+6hd7RkQA5mU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=o5xAd7fEboZ1PRLO5IXCdy2mNkaUkH/qmTLqEVrLRdpm3q2zwWu4CaoiIfz5zDRjG0gEmFcPREsdAw08yUGalmUFovbdnkgNXlZw+Ny3zUWjEw5BFewWDHCxd22aaON8lsgKU6N6Uva/UGmO1q2oV6CmIy31bXUnfLbgDPAwr8Y= 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=eFB+HMwA; arc=none smtp.client-ip=209.85.210.174 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-pf1-f174.google.com with SMTP id d2e1a72fcca58-6e782e955adso1551904b3a.3 for ; Thu, 28 Mar 2024 21:46:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687576; x=1712292376; darn=vger.kernel.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=pODlhOn2Qlpzyvoy16i8OpTFZF278Z1sqN5yeQeAUQM=; b=eFB+HMwAOfJrV+eJ1S12u6R3ktPG+FiEi2rcW6NcDPC2lopPwdIgfQfsdxJgjYruQF jyqLkzoai3Vmk60xM74Wn3EsqxRn+GdZO8dM5sPWsXquiU1RIKCOoKGKd1FGAPAMsJHZ zbomgEH4Kn0oFwjUlxY9WhMEcE7Im3fDfbeNevAsLgBHxaxBfmJLK0RjYMySOwHTgDwE kXIQw0j0srK6Ao6dGQ0Oz6yXHLMmUhDyBFCOtZEiK2/I58p74ZXAsMdW9Uyg9cZBO28W 5HGRHz463U/KpwHKlX/ONFHN6vt2bdUz0wAN7DSgqGpZsozyfnHRKrJoRkeDK0gg40aG pTSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687576; x=1712292376; 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=pODlhOn2Qlpzyvoy16i8OpTFZF278Z1sqN5yeQeAUQM=; b=f8kI+zf6rUbKucm5zKFR413UCOI7Q86dEMiOvQHYglNsg8aryMoW8PjNrcKwav1rRV HF32hkMFIIDvZzbH2zOrhCIf4UbgNul+Foie7EVjyAAvO8KOZ7c6xsz2tvSjSS/ifMMm QOsbKNvEr56EsJYmWkQx9YCXB0ZRYUWzeeSAgfq9mhwqTiLUnIUbmfT7yzt1LjGkYw5s gGUOAFMoNbH4xPnxrDClbbk/A6EyyuwhKERUm2qu73fCPnbVy/0m2tQVOepuh73PuRW9 2SlsJrdTppDFpmh3xvqQjdSQpoGIog9R7X2qWg5N/ywMmVr9R1O0OWLjyp1TIsFjVNqi ef+g== X-Forwarded-Encrypted: i=1; AJvYcCXBJn4aEU4PYQ63UvEJoRWx0/Ta8vr4k5sKq6ubdzu39mB1OV+SryIpjkn6IUhiB1od6cg85qYGXRmgvq5s62Jvzy69QetwacjOEJad X-Gm-Message-State: AOJu0Yx6DoSUIC31cYsaaQ8QngzNyHRBMNnnn2kOmmkUUkZWqKJpuvYy 64DAytP9Yo1M/CmnryYBwddEtf6FkzJC4fL/1ZSZMhIIHPe4lj/Kg5Rtg902644= X-Received: by 2002:a05:6a00:3ccb:b0:6ea:74d4:a01c with SMTP id ln11-20020a056a003ccb00b006ea74d4a01cmr1489683pfb.14.1711687576218; Thu, 28 Mar 2024 21:46:16 -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.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:46:15 -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 09/27] riscv/mm : ensure PROT_WRITE leads to VM_READ | VM_WRITE Date: Thu, 28 Mar 2024 21:44:41 -0700 Message-Id: <20240329044459.3990638-10-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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit `arch_calc_vm_prot_bits` is implemented on risc-v to return VM_READ | VM_WRITE if PROT_WRITE is specified. Similarly `riscv_sys_mmap` is updated to convert all incoming PROT_WRITE to (PROT_WRITE | PROT_READ). This is to make sure that any existing apps using PROT_WRITE still work. Earlier `protection_map[VM_WRITE]` used to pick read-write PTE encodings. Now `protection_map[VM_WRITE]` will always pick PAGE_SHADOWSTACK PTE encodings for shadow stack. Above changes ensure that existing apps continue to work because underneath kernel will be picking `protection_map[VM_WRITE|VM_READ]` PTE encodings. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/mman.h | 24 ++++++++++++++++++++++++ arch/riscv/include/asm/pgtable.h | 1 + arch/riscv/kernel/sys_riscv.c | 11 +++++++++++ arch/riscv/mm/init.c | 2 +- mm/mmap.c | 1 + 5 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 arch/riscv/include/asm/mman.h diff --git a/arch/riscv/include/asm/mman.h b/arch/riscv/include/asm/mman.h new file mode 100644 index 000000000000..ef9fedf32546 --- /dev/null +++ b/arch/riscv/include/asm/mman.h @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __ASM_MMAN_H__ +#define __ASM_MMAN_H__ + +#include +#include +#include + +static inline unsigned long arch_calc_vm_prot_bits(unsigned long prot, + unsigned long pkey __always_unused) +{ + unsigned long ret = 0; + + /* + * If PROT_WRITE was specified, force it to VM_READ | VM_WRITE. + * Only VM_WRITE means shadow stack. + */ + if (prot & PROT_WRITE) + ret = (VM_READ | VM_WRITE); + return ret; +} +#define arch_calc_vm_prot_bits(prot, pkey) arch_calc_vm_prot_bits(prot, pkey) + +#endif /* ! __ASM_MMAN_H__ */ diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index 6066822e7396..4d5983bc6766 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -184,6 +184,7 @@ extern struct pt_alloc_ops pt_ops __initdata; #define PAGE_READ_EXEC __pgprot(_PAGE_BASE | _PAGE_READ | _PAGE_EXEC) #define PAGE_WRITE_EXEC __pgprot(_PAGE_BASE | _PAGE_READ | \ _PAGE_EXEC | _PAGE_WRITE) +#define PAGE_SHADOWSTACK __pgprot(_PAGE_BASE | _PAGE_WRITE) #define PAGE_COPY PAGE_READ #define PAGE_COPY_EXEC PAGE_READ_EXEC diff --git a/arch/riscv/kernel/sys_riscv.c b/arch/riscv/kernel/sys_riscv.c index f1c1416a9f1e..846c36b1b3d5 100644 --- a/arch/riscv/kernel/sys_riscv.c +++ b/arch/riscv/kernel/sys_riscv.c @@ -8,6 +8,8 @@ #include #include #include +#include +#include static long riscv_sys_mmap(unsigned long addr, unsigned long len, unsigned long prot, unsigned long flags, @@ -17,6 +19,15 @@ static long riscv_sys_mmap(unsigned long addr, unsigned long len, if (unlikely(offset & (~PAGE_MASK >> page_shift_offset))) return -EINVAL; + /* + * If only PROT_WRITE is specified then extend that to PROT_READ + * protection_map[VM_WRITE] is now going to select shadow stack encodings. + * So specifying PROT_WRITE actually should select protection_map [VM_WRITE | VM_READ] + * If user wants to create shadow stack then they should use `map_shadow_stack` syscall. + */ + if (unlikely((prot & PROT_WRITE) && !(prot & PROT_READ))) + prot |= PROT_READ; + return ksys_mmap_pgoff(addr, len, prot, flags, fd, offset >> (PAGE_SHIFT - page_shift_offset)); } diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c index fa34cf55037b..98e5ece4052a 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -299,7 +299,7 @@ pgd_t early_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE); static const pgprot_t protection_map[16] = { [VM_NONE] = PAGE_NONE, [VM_READ] = PAGE_READ, - [VM_WRITE] = PAGE_COPY, + [VM_WRITE] = PAGE_SHADOWSTACK, [VM_WRITE | VM_READ] = PAGE_COPY, [VM_EXEC] = PAGE_EXEC, [VM_EXEC | VM_READ] = PAGE_READ_EXEC, diff --git a/mm/mmap.c b/mm/mmap.c index d89770eaab6b..57a974f49b00 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -47,6 +47,7 @@ #include #include #include +#include #include #include -- 2.43.2