From patchwork Tue Oct 1 16:06:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13818414 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 D424FCEACE4 for ; Tue, 1 Oct 2024 16:07:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 753572800A6; Tue, 1 Oct 2024 12:07:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6B115280068; Tue, 1 Oct 2024 12:07:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 555862800A6; Tue, 1 Oct 2024 12:07:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 1FA4E280068 for ; Tue, 1 Oct 2024 12:07:34 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 969D516169E for ; Tue, 1 Oct 2024 16:07:33 +0000 (UTC) X-FDA: 82625513586.30.7A50B5B Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) by imf05.hostedemail.com (Postfix) with ESMTP id 9C6C5100008 for ; Tue, 1 Oct 2024 16:07:31 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=fwtGqYEO; spf=pass (imf05.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.51 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=1727798725; 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=caTG2x1q5ovRC8rVBZGdJGvZXvk6DkZXEsOA+mONLKmYrZBgt+ZbQ0q/9vEjxmlf25Fyr3 mUl3iWFKP1uUkINY5yDX8PNFF9GsfQBfCov50JdoKANBbyVtkInc60wecchjc6uhSA72v5 0fIZrFnr68VaIcXlEPsCEFi5MbZWBWM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727798725; a=rsa-sha256; cv=none; b=PuqU2lddLTRD97rgaFRHVA9IjT7EzSGO2PfCdlx5wPxBxP5R3mj/VVcr62DgNp/jgenGFM 8tzQIGj5zfZMMAOS1ugPEsWmFiAfnJk2J4xTeAPFdoNvaVsoJkgOwFgSYASGOM4b9v9oN8 X7u8ZZOgJE95fWajE8pF6vvA6N+5yeI= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=fwtGqYEO; spf=pass (imf05.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.51 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pj1-f51.google.com with SMTP id 98e67ed59e1d1-2e0b93157caso3395038a91.0 for ; Tue, 01 Oct 2024 09:07:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1727798850; x=1728403650; 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=fwtGqYEOX3dTkmmr42/eZKOewbQPhNn239b0dKqbY63SWEC5Bl4FPsLOa13N4J5ElT 5sU9qy6uSwXyl52T79vlXO8kgP+Iqpn1Ox0iHsstFOpRodmWGIkh8IN7VKsfiqROj3qr UlOgFYEarresXFfiqrA173w3Xjrkg8xM10zshr9iAvTbHfzNfjXrfz1iHHBQCJGhY4Ei q7HHhFVxSlragKFqQKCg39rtqX5CLAGAVLaUm6JMLENURNswz6Mkw6+/QqHw7lXAk2TX hnDBj3dZTtUB6kHuarF8d1JNGY/rOMKX5tkaZ4r12BgFzYoVRZAq9TXVH87J+uyAEh6G 1SZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727798850; x=1728403650; 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=CN+J5CeDPbddoWg0MqSkf/10PKDrC23d+M7+3c91DK4alWqLjr1kZvJxi81e9VbFTu fplyGv4fPlfeHTAkUZ1UUEO7wwLm8y+6hZqFX8YTxu9Scur+l6G9MpRGZKLy4a/b+Uw1 tcSbCKozQK3KUyAZqOcJFP+E3NwGZq1OkoKtctW+lakmg5iqePg02XxJsBHR9Fq/aWJZ eiU3YiENUHOqUnjOs5caOKT9ljauc3KRJalH7bcktCEDGnbuhpPIOrwHUExeTikX3Bbm DfN2t49W/tkT1Ilr6nngp+L0HHsI+/nqkv+gsX0JdQn8OOf1hWHULqc4xXNnrFTY7UAh kvdQ== X-Forwarded-Encrypted: i=1; AJvYcCUP6rgxEx/wSx6TT9U+3EeC3TX53tXwp4nfjxfm3cMvOGQ2Aho860VrIieEtV+wHOQ8XrqUe+2OVw==@kvack.org X-Gm-Message-State: AOJu0YxiAeFu2rpZZsJo6H1QE1R1Db/nQyQbaN5AJmnLoeilxogQ6ULn oNwEW8cHTFgO89/5kixuUEEFcZtTfGenTOwEWqgsaXVEGBf7dfJ5aEDuHehJ+xo= X-Google-Smtp-Source: AGHT+IGob07HibDqMTkhNXqspOJCbfYZqD3T/Q2DpzNezAZ4leoJ5tWyryGCIql/xTbKFf++6QnddA== X-Received: by 2002:a17:90b:11cf:b0:2da:88b3:cff8 with SMTP id 98e67ed59e1d1-2e1851c6ec4mr154101a91.6.1727798850248; Tue, 01 Oct 2024 09:07:30 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e06e1d7d47sm13843973a91.28.2024.10.01.09.07.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Oct 2024 09:07:28 -0700 (PDT) From: Deepak Gupta Date: Tue, 01 Oct 2024 09:06:16 -0700 Subject: [PATCH 11/33] riscv/mm : ensure PROT_WRITE leads to VM_READ | VM_WRITE MIME-Version: 1.0 Message-Id: <20241001-v5_user_cfi_series-v1-11-3ba65b6e550f@rivosinc.com> References: <20241001-v5_user_cfi_series-v1-0-3ba65b6e550f@rivosinc.com> In-Reply-To: <20241001-v5_user_cfi_series-v1-0-3ba65b6e550f@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: 9C6C5100008 X-Stat-Signature: jjku1f63akgcbkuxqy4yqutomotzdxaf X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1727798851-790889 X-HE-Meta: U2FsdGVkX1+h0Gz2rQ4qoXHI9VVxmh05OIqtGvmySSfB7d2R5NVoto3W8tHVZaWKx1Auf1Rk56lioxtv88hVAVNlPG/ErTNhwDkfK4zXQXWBCn90Xl83S11sH7YalBMQ8mbCx9rboiqJ10Gp2iqV2K4Hl0FrDdTJgi72uRoQf3L/oxPH30GwGJrrgx5FAHAmsM5rMzKaKwUcA2yx5535YdFDxH3UCHXjg4ZiSy+dJhS/zRc6DyxxfztFEJftG3dRSIq1MnOfBrvW6CxcaoNyN+/cuCfWi4RNlOX143v6jmP32+50i05oMYe6ReS7SNxfdg+pCkB/XJ0jpDfMwbGuGbuSfUqMst0mLXy7isOzk/Vdi17Jk9Enb/Yzr/xLcxlxiP9DSV1XBE38vHIadRVerfferN9nY1K7LIZss0uWE64b0hc3Gunh4vsB3gXjsL0w2wHkrz+xR3wOFvAiz7gmga8Qm7eMu8mbVcoB7dKSXQuOmnIAIRktkrYtBhvhs48RQfMKiuRbGRqg+0Lx4qXdZj65toc6JXHsFZvl/ev8yEmTym/gbM57iNhptaVqDeT0CuR1yIiWTSsLf9dO+bnuKKIEXoUx56S/K9M9Cdeou+pOe5m6SQ/MDCBLyYFBE4M9jUhCv/f5NSggeo9eT53cmgzYj1kuIvF9xNTOEtZZUfo42Ze8WPfNDG/jgqS6osEyXV07F2jRRhQgAZKnvBZMVBwS5Ia+yGkbg6LpZC2NtCBQf8bR9++iMqLiCxbhbRNs2CviYxiX3wqNj3mJJrSMmf89PxXNWmW9Nq76xbc3GREeO91FxkaWOxwyxqHXvy+i8yiTZSyGTS3h53f/Bo38jWcEGwfEJIvNyv27yBnIGBzP4FiuTXej1Iskt/IG+YmUnHxULjTkl7pOMjrnqBFwRhl++A4x/OWkzcn0+m4EFFwR5Kox0OB+x4urfXbXwi1+OLDtHzATLSuuo2F59TY y/1OCNnn 2wml6m7wEO50r+rc9qibCOKBYTCrNYPENZKQ+dOetGyfRdjEv/sGugB84mCMbfFZrxO+944xy3BvWR+l5yz1eieUKhBNmwUfzamD3QPzq6hhlHecaoTIDkbRcdX3nYaecyT9z62+iFI3fxBoYz7rrntGuInv5wSMacwjyNt9pErWKxczKAoM5qT0Bdd7Cd76nIH1RO1OdLRtF/ngO2IyZjAWVZBNZa0DEh9NXukkYn2N4HFfi1X8RvwRYGbWyGGj2LDegneGrWiQ6dX2w0sd/6maYda/Lb2jJOx12G3U13D58JRv7ArVHeRq1e5EfArFO63oFy3qZAuIf9FwAlyscRzqWbOkBnyzS1dFsVAlBaEZRDl81ABtZmGCvPlrweL9AT3gLPAyKB8bCoRIKiQoCPXfj4i9f+nikj2wDEXY4zbpfRPwRXU3GWYo1hv6PQv2s4JEg9/zOP+2wMAiFC3DEeQhJPXLZB4s6FJtrQ0IcLT62lY8NQ3R3RzAuMIM4UEtqR+eX 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