From patchwork Fri Mar 14 21:39:25 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 14017528 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E525DC28B2F for ; Fri, 14 Mar 2025 21:39:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=WcqZsyHa5iZ2ZW6WMiFUBOGzTQsmkqNgxcgl2yBiZFg=; b=pqMtZSMc74u4zS 7JI9EKWoYMYPG57hat46tkSX632ysZe6YUBkmGnqCZ7YAc77+yV5pAiYH35vIYjrvm8W5NFa6DaJ7 fClF3eCeNAitweWLadveKrfuJCYAPuyCnKNlg6B7PMwegMHg2d40av3E5y1Lcebf1YnqKr+WtBoL6 rCTxOGlZT447vx9gg7E5V4lfyjceKauDxBM2uhE77IYRQNunHoiX9djCe8J5xKYeA8xgQrgi2Xhwv ZvuHxou+lUR4tH2uCfbrT48ZDMgRhIdLiYEsFeIMzzoiUfWQ9e/HdsbDBY9ftpCvNUDXjD3V6Cvy0 nukFyP2/FoQ61Hl3YcjQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1ttCkr-0000000FKQL-30af; Fri, 14 Mar 2025 21:39:45 +0000 Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1ttCkp-0000000FKNx-1g4O for linux-riscv@lists.infradead.org; Fri, 14 Mar 2025 21:39:44 +0000 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-224171d6826so63843005ad.3 for ; Fri, 14 Mar 2025 14:39:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741988383; x=1742593183; darn=lists.infradead.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=gQxyFEGdxJnYf6tF0dDNzkrR4Tdl8J2Aoq3Zv3gQ3iw=; b=Tn0aGr26oKQy5abDxKsb4qYz3o8hfTFb/B5QBjHz+1acbo/2AJA5r/orDgWxxu20pr t5siPwA6+DYHqa1F6L1UlGCZW76q7+5quxgq9oTiRKuM2e3mV5jaiCmebwEuXqEfIGJi cMpiVeEKx1FamJ9Clz47i9YhBWL0sPTLJWPqSPRNG2cQ68v4Nb0yPXrbSTOsXLa3hhNO oCPHlCJNaih0Z5E/0tucxwRAbbXXxI62udLovSHC//6pZSBDeOuNZDbL9O1F95VfeySs GbemYRu4GJ/lb6Dj9Z3BCdKEX1bAZWTpxaeKV660VpGG4fjXygHlJ1mCDDJoczgjcKAJ XDLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741988383; x=1742593183; 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=gQxyFEGdxJnYf6tF0dDNzkrR4Tdl8J2Aoq3Zv3gQ3iw=; b=jNSTN6k+fy+uzkXrC1NgG/lOXgLS05pMGT+s1qUF9dQb/wDhp/x55nIqI6VTt1xCCW KUaUZXttE3pTCj8GkJlPXqTaWF23sgakGhg0Dc7xCfn3+NGkVJpNJN1VNc1h80se9ZJB JrGd+Dp6TQJq39mm7s7gUOG29D1k8L76XRlTyJfo51pX6IEo2uN6bP8frRS97uTLKmaH l2kkCIBs+B2yJWJdh0RVr3fOXo0Y02/ou8JYfwgdiFu+wTfr/mNfVYqUKpoFvocopSJS 46vBuAd8gSEhgGh/MVHtPR3sQw8HVyrtKrh+vpxfejEs0YcbnPY7I+dj03k6P/yZQ/dE czwQ== X-Forwarded-Encrypted: i=1; AJvYcCXSkXI5NlTmhhzM2Zmu7PtMPw62E0mYZB6vkNjdCcOj6RFovhLVKyLCzOb+9DCQdEmf/a0kxdpAEkkNrQ==@lists.infradead.org X-Gm-Message-State: AOJu0Yw960fjKaIqcyjpxYTMdBwy+WMGV71bLc/kIWntr/d5qOU1uSK8 Z/X7G2gAH/oXUIp6/lb8ZQrbOjsnwZQzsNd88vmSlBFHFEx7GD4WN6HyU7zF5pM= X-Gm-Gg: ASbGncsuGjY1QM1aHnLnc+bJIGHhHkbhMW4F0XmZ2z1Ur+pcjKWLAGQcv2ABbsC0wbl qao2yBVgqQe2KTGgHElCYgcsMk5wnPb4PP4+60mheNR72kukFzQwAsqzbKIIQht17/0rzPKGcm3 e+KkZe9nUwCM6EwMot3RrSDV+ogpru3TzVd9vUJFHsTj6Rb0gGDGJrZfMyU41W+t9klYLDUUWCC LrU0GM4E7YS2STGQAfaRkVTCu3KQIwEO+ptauVTQDpTqOe/KEUtgLnfGj+3WrEC9Bs405XJoqCc iWAY9uCNqVnwDZvOdzu/yAZSRerWdS5whhJgPL8Wpd56+scKOzOJ5mQ= X-Google-Smtp-Source: AGHT+IG1XKNpuQB9eoNKiKGV8OaMyZRapBWvcssowC/o6htzWDndePZH0CVmNsc5LDFT4JSdbgFB4Q== X-Received: by 2002:a17:902:f693:b0:21d:3bd7:afdd with SMTP id d9443c01a7336-225e0868050mr56743905ad.0.1741988382861; Fri, 14 Mar 2025 14:39:42 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-225c68a6e09sm33368855ad.55.2025.03.14.14.39.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Mar 2025 14:39:42 -0700 (PDT) From: Deepak Gupta Date: Fri, 14 Mar 2025 14:39:25 -0700 Subject: [PATCH v12 06/28] riscv/mm : ensure PROT_WRITE leads to VM_READ | VM_WRITE MIME-Version: 1.0 Message-Id: <20250314-v5_user_cfi_series-v12-6-e51202b53138@rivosinc.com> References: <20250314-v5_user_cfi_series-v12-0-e51202b53138@rivosinc.com> In-Reply-To: <20250314-v5_user_cfi_series-v12-0-e51202b53138@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 , Jann Horn , Conor Dooley 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, Zong Li , Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250314_143943_451054_2AF10365 X-CRM114-Status: GOOD ( 20.25 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org `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. Reviewed-by: Zong Li Signed-off-by: Deepak Gupta Reviewed-by: Alexandre Ghiti --- arch/riscv/include/asm/mman.h | 25 +++++++++++++++++++++++++ arch/riscv/include/asm/pgtable.h | 1 + arch/riscv/kernel/sys_riscv.c | 10 ++++++++++ arch/riscv/mm/init.c | 2 +- 4 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..392c9c2d2e78 --- /dev/null +++ b/arch/riscv/include/asm/mman.h @@ -0,0 +1,25 @@ +/* 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 050fdc49b5ad..8c528cd7347a 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -178,6 +178,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 15b2eda4c364..9d6661638d0b 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -342,7 +342,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,