From patchwork Tue Oct 8 22:36:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13827094 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 0823BCF0429 for ; Tue, 8 Oct 2024 22:38:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 19CE56B00A6; Tue, 8 Oct 2024 18:38:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 14DC76B00A7; Tue, 8 Oct 2024 18:38:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F08C06B00A8; Tue, 8 Oct 2024 18:38:21 -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 CDCEB6B00A6 for ; Tue, 8 Oct 2024 18:38:21 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 7F762A0298 for ; Tue, 8 Oct 2024 22:38:18 +0000 (UTC) X-FDA: 82651900002.24.95DD7D3 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) by imf21.hostedemail.com (Postfix) with ESMTP id AB1531C0011 for ; Tue, 8 Oct 2024 22:38:19 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=cP04C43z; spf=pass (imf21.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.178 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=1728426965; 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=Jf2x3pfGFzhqVTB4n6XHdAtkUN+75WMOXWy8ywZVarA=; b=7dpBm4e/LNFQ+BwXMREYL7FcNjWWKIUcEkne3JqV+DjbSFXr4EMR4Tf/nLzlxixf4sxJnC 0Z1nnXWzJaDJpv/V8B+rREWrMDU+/te60BG9Pp8moO/Nu0MB5QloF4J8HMalQmtvHvLOBl 7Hi0RHXaxTEkrIXBwxTlglS+UB9MtNU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728426965; a=rsa-sha256; cv=none; b=yktkj+nGEooQxG0RxKha/v5HhK9vaxpozeEDSezqMJEEFz6UDilbv2VpqCcThqqH8lUih+ W/6qiwBMB4kQCFKKIfEVIsuEbuJGbui4194zn5rvoEsPN+a+JIhCqTMY4PUlQZnfK7V9ox 0dKsS5vaZpm8WvpOl0qbPUkbxwD5lok= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=cP04C43z; spf=pass (imf21.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.178 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-71dfc1124cdso223510b3a.1 for ; Tue, 08 Oct 2024 15:38:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427098; x=1729031898; 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=Jf2x3pfGFzhqVTB4n6XHdAtkUN+75WMOXWy8ywZVarA=; b=cP04C43zN5DPrkGusGPilLbRY7Dr1/mPTqZW5uufXHUsAXNmK3+DTJ/wbw1rsDdDTg uANMOywRLzHv9Jbw57JxxmLK8LAUKVZONcNMCK0gVX7jTK0be5ERAwEkxGbkewGWQA2k A7LTOXfbS80KKA1KrLiYfqxsCwbXSc08t2rqp2P078few4WwgiieGck4iErWIiopkf7V sxhNb8Er0SzT3LUSwpowk1K2oZ8mSLMZHjR9+1FmiNCY31lSm2RvB//1JgHQEMY5smbG 7F7jAkt2oIv9ZgRWYEFnFSZN/HOcnqOy0FUszGeXoV44c0uuGeY8BKxqkIlbRLSSY7It EP1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427098; x=1729031898; 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=Jf2x3pfGFzhqVTB4n6XHdAtkUN+75WMOXWy8ywZVarA=; b=tEtM2QnOWSmI0lEqWIPD1M60JJufB2KDGO6EhTK2Y25MFVkqstHEj/WwYetr6awA4u u0bnO6gDu4f6A80CUcCWZhHTZavn3nopslTUXT8BqM/eg5HUtwbXCAaYhNrSmA1rtmV+ IXvDt/r7lHGUC+Rcpskz0s0cR4XdenuS1ZFuQZzhQmD4uRTA0ZB+4gwOZvzhTS5jTTII byyYTaUOezqjqoyvRnDOhPZN15mOt1Yjx6F8QjiQFBMcCYb2kjMdYi/24h/kx98w2kQd fjQKPlHHJbtqOu5Z9BQk5WPwPRqwer9oHdM0yttE5FLGbeek2vt3WmNCsoiaTc+fNRl7 7OTA== X-Forwarded-Encrypted: i=1; AJvYcCVtIq62af0Gi9hIVr5nk9RW8vM/Z3zZqItkKcCGbhmfU/gbGgUhLJefP7y+PLdpRWoqEqoOsIWyMw==@kvack.org X-Gm-Message-State: AOJu0Yzd8Tmse0IajYIrIumhDnrlA+ptuiNb52ZWIbgXZvxzcCdPolEQ Pw2bK6eZAhucIpxVGI7ht7P5qgRIa7ioy5CNFMYgcYJ7riCHhyrqXakCZ8nFB1Y= X-Google-Smtp-Source: AGHT+IHSAqsobXk9Y+XBm/TMrjzhYSrUi/BmV9fcp34QsDNZFgu887tqUAdrSExRA5PWk074Td46aA== X-Received: by 2002:a05:6a00:2e08:b0:71d:f459:6df3 with SMTP id d2e1a72fcca58-71e1d6a47f2mr971683b3a.10.1728427098329; Tue, 08 Oct 2024 15:38:18 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.38.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:38:18 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:36:53 -0700 Subject: [PATCH v6 11/33] riscv/mm : ensure PROT_WRITE leads to VM_READ | VM_WRITE MIME-Version: 1.0 Message-Id: <20241008-v5_user_cfi_series-v6-11-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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: AB1531C0011 X-Stat-Signature: 9qupjh4ewx19kafyk37ogtmbxbd11rp9 X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1728427099-319115 X-HE-Meta: U2FsdGVkX1/tKbU9d0ZOrX83R8sl+rpgdCgtPzuiEp/tXif0coLczFVhdFgDqlM6vsvKRBzWfS5rdVXdbGkTSK0YKsmCI9BlpUw1Jg3GYAVfoi9J+nRUfYyYftJobtTlCsAeah5ouePXc7GyonKbBPEfoJC7ew9esvpJCWJg5s9kzeB33KvKSpLIbBILCFX63XPYDZOIeQFOt0TE9o5QTY/TW2+sM8BxMaaMIrGQyg6KmWLm2+r+abRCIzKy91NnrBnNLj1esW1vQ0QxCNjbIWHKzdyGTJBjaQAdjMwwKgbKKy6uT2y1bn51bI5OJ7WNeJmn+BeQ6YJZ0+FOUT8cuFHvYNxn29u0fcrm+b30wYy9NSaktYIGSDL5WuJXxHIVk5VwlH8BzqohtF6wKl9iePstGM7y9jb/AMrf2ab0c/lwbJs/PNd401sA5tcNmOE+emLY5TVNLgf5NnhjZ54/hJdRIDPmWQma5zzVGIHsD4pQPnQ78puBVfKsmYZCLeZmVGYnJ8N8T7pOIZuQTkbDmnqHqnKNOXb5hS7i9eegMWSgNKt5fDR+n67n7+b2cu1r+XkQPddjSUhpzbIBJd7XkC1yU2kNN8sVBeyVhTnG2+EduYnQqgeX/XdHtZHIWVs40nsF/xKPOX8EGTMF1F433SIvoiOri6IKhHbRcHvkcUSeG6npTvtRNpBpLZ87SqR7c3pNhv0wqjBUg9N5TpRzrtmZt/2RP4Go3F1so9DNso48R19AuFaH14rgTMp/68F/Vezvaoj4x6h2gvW3xFlgLcvFewruMHp3vljp1N6K/ovHs3Gldnj9v7k9qubpG1qQh0U1NE5TtNxC4PssP4ml3u+1IBt3I/kVkAMdHYOLgItiiGQmRqSWWFwNEoSbrsgdzdr/U4TmLU3bYD8WRyF+Wvk9wzlgR1aoc/hky6gnJbhuI1LLGbZwHFmq9YGPfHM3DJsKjqDDlmGM9EsVHhs H4mNL6gb x9SdvvHCHk5B7slavNv8EdJ43haVJxjSioBorr3mu6TazXkpymwf/4efubig0daNEY01ZWmbKJFf5HFBOSZ98uC1zZPt9YNzMXBv3BRJlWteyAxV7rXfkkEhYb3dyF+qCdCLki6lV+qe1n1jfVguw/QuMp5xZ1tYoH86XW9BMQAP3UtP7yy2gtBOgv1CIANrazwwKgnSBxBckvX7vmbKncPfRZXS4pREhrudQPAWTXgVwRQgXyDQbeecXTMDZT8iy44q+OfJwguko8tAfoEArvY+hRjPAjopSI5EE75Sav0VycmZJu15up9n63v+u/EjQEil0HqhoRxPCVm6JNA7AET8gT6OIiDEAX+RvvnvcprImf81Y7v0VMKy0cJmVai8FLUSIDrur0+fXbG4MF4zOBWAUO8R3h4RbhpgGqlg02XD6bC6EHc1xtq80VsyolGq75sORcz7M2v8URaPb3c/4wYuxZFzTaRnX0jdEZ6ayx7k7TBfb+Bp+e/MbUlAnJGElklVtsjOxlCNIzLWu6ucvLUXm8xbDW6tP43vvTxmSP5TRLHS97xNXeLD5KZmoZIKEvJKqONAWPjU0pY8ckhNHccvi0OeW/tKL6//u3LrHJ6I/KLj8L35Y0TP4mA== 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: `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 | 10 ++++++++++ arch/riscv/mm/init.c | 2 +- mm/mmap.c | 1 + 5 files changed, 37 insertions(+), 1 deletion(-) 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 e79f15293492..4948a1f18ae8 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -177,6 +177,7 @@ extern struct pt_alloc_ops pt_ops __meminitdata; #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 d77afe05578f..43a448bf254b 100644 --- a/arch/riscv/kernel/sys_riscv.c +++ b/arch/riscv/kernel/sys_riscv.c @@ -7,6 +7,7 @@ #include #include +#include static long riscv_sys_mmap(unsigned long addr, unsigned long len, unsigned long prot, unsigned long flags, @@ -16,6 +17,15 @@ static long riscv_sys_mmap(unsigned long addr, unsigned long len, if (unlikely(offset & (~PAGE_MASK >> page_shift_offset))) return -EINVAL; + /* + * If 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 0e8c20adcd98..964810aeb405 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -326,7 +326,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 dd4b35a25aeb..b56f1e8cbfc6 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -47,6 +47,7 @@ #include #include #include +#include #include #include