From patchwork Thu Sep 12 23:16:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13802834 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 A7431EEE270 for ; Thu, 12 Sep 2024 23:17:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 357336B00A5; Thu, 12 Sep 2024 19:17:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2E02D6B00A6; Thu, 12 Sep 2024 19:17:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 10C7F6B00A7; Thu, 12 Sep 2024 19:17:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id E1B276B00A5 for ; Thu, 12 Sep 2024 19:17:57 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 67618C0D48 for ; Thu, 12 Sep 2024 23:17:57 +0000 (UTC) X-FDA: 82557650994.29.A138B14 Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44]) by imf04.hostedemail.com (Postfix) with ESMTP id 80F5040019 for ; Thu, 12 Sep 2024 23:17:55 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=dLl6tH4y; spf=pass (imf04.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.44 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726183021; a=rsa-sha256; cv=none; b=imUtD/FmM+tRizwAgk2J2bR7RaGYOh9qtW87UM6RK9uebMYqImeO/KWa8slzvPAe1T4qTt lkVO4+YwxjYjc4m1DfVEAq1yWazIinbNNtw7nK42zC4mU9P3sa0Tyh+lgVYWBQgmqewIKU B0cRu7ZqcK086+4S0Slv0ctTghzhfqY= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=dLl6tH4y; spf=pass (imf04.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.44 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=1726183021; 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=eiprnGA2BOGVGzJqIZ9S6uz8auKceb/MWPdqRC4IaOM=; b=j5xA7B6T/DCZ1/DV3VYvito6G2rc4yX3Dec7u4qY8kAk1UItvo55MGU2l0emyGmNW/W62H VhRo24s+uFWbxxYXa39B9SUASr7/FG4fpUy6cGnr6P+wiHWLuBQrZnch+wWt2tUPid3e8V VXe/RULfOh784vKXx0Ic0AhI1RGEGR0= Received: by mail-pj1-f44.google.com with SMTP id 98e67ed59e1d1-2d873dc644dso1259520a91.3 for ; Thu, 12 Sep 2024 16:17:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1726183074; x=1726787874; 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=eiprnGA2BOGVGzJqIZ9S6uz8auKceb/MWPdqRC4IaOM=; b=dLl6tH4y8OZeKzPytdtVskzL+G8Ynpyj+AfrCB+FAGvsKTSO4PNS48vHMp6iL6WA6z WWTAHm/BuTbLdiZUJvMnKUYEiNQ/fz2Q5wVa+EJmhGRUztIPxrGmVp7OXvAK/kPfZXNp IJALHGRBhuRZkYhKrkvpmaKm9aOIL6TLb1BTNN8qqrAhdLbmZfIq3OHpuzPoEtdXXRPX hPdgffywUdtbmc3tWNg3r0ZbJPaFsm1mVCBod/kH6W4khzPD84PIpC7quzMUOHZ1UURM IHbB+waYb0eqk/OzCqw2ZgeJ2ZoGIOQf4HMBt4R8RsWg+LANECad5m4VQ3GKvaW7xmRT 4niQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726183074; x=1726787874; 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=eiprnGA2BOGVGzJqIZ9S6uz8auKceb/MWPdqRC4IaOM=; b=DS+u6fSUT3bDiE4oAvxdWj32sCS7xEwunkvtcUShc1GclQ2uvndqZxi9zmUisaVJeS UmilN30OM8U3uWnrDtoeL1FJelC/uXfEPNhozj8BsTH119NzFklmaloZ9mxQWqtIJ6V+ iVK/BMyQcyk1fn59Ma6KYSuERk4WF+S6dH6QhWXW6zOEKVy1wSyNo+SvHcvyAfewvOoM Q4JjcWyOB/PXRwABA2RAjHwLM2Ut70RJzIaFrqPMUKYuzrSKDPiRuxDQVpvU59tIcdIC yNvMk0Jb8CZhOraI5ajtH7VGSiz9Lw5C1N9PYxbclZ2yvKm/Cn7qpR+7RO5zLch9WUTy HdBg== X-Forwarded-Encrypted: i=1; AJvYcCVDqnKer6wv97/te2a3Z1wZGmwmfB3jVJ5DlyIh6E8Zk77yplrFF8V9L563Xh8KjuNMwtz7rKiuMw==@kvack.org X-Gm-Message-State: AOJu0Yx0/S3/rNPSvB4FuFUd1n1F8IYu4Jm5tmhcLf/Mpx63/hafFHl0 nTffk4qxhryndNYKaP6wY+g1QzsAr+MGJtURwRuZUHfiW9dQvwdBlSpuaGUj+0Y= X-Google-Smtp-Source: AGHT+IEnJrdTR1Y5qPMoOCKu4YCibaPdfaIv67ki6aFPE+yQ7lsYshwSwmMRsNa90sGyf+0sNSEfcA== X-Received: by 2002:a17:90a:8a13:b0:2da:bd08:edc2 with SMTP id 98e67ed59e1d1-2db9ffcaaaamr5258868a91.9.1726183074122; Thu, 12 Sep 2024 16:17:54 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2db6c1ac69asm3157591a91.0.2024.09.12.16.17.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2024 16:17:53 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@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-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh@kernel.org, krzk+dt@kernel.org, oleg@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, peterz@infradead.org, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, kees@kernel.org, Liam.Howlett@oracle.com, vbabka@suse.cz, lorenzo.stoakes@oracle.com, shuah@kernel.org, brauner@kernel.org, samuel.holland@sifive.com, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, greentime.hu@sifive.com, charlie@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, xiao.w.wang@intel.com, ajones@ventanamicro.com, anup@brainfault.org, mchitale@ventanamicro.com, atishp@rivosinc.com, sameo@rivosinc.com, bjorn@rivosinc.com, alexghiti@rivosinc.com, david@redhat.com, libang.li@antgroup.com, jszhang@kernel.org, leobras@redhat.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, costa.shul@redhat.com, bhe@redhat.com, zong.li@sifive.com, puranjay@kernel.org, namcaov@gmail.com, antonb@tenstorrent.com, sorear@fastmail.com, quic_bjorande@quicinc.com, ancientmodern4@gmail.com, ben.dooks@codethink.co.uk, quic_zhonhan@quicinc.com, cuiyunhui@bytedance.com, yang.lee@linux.alibaba.com, ke.zhao@shingroup.cn, sunilvl@ventanamicro.com, tanzhasanwork@gmail.com, schwab@suse.de, dawei.li@shingroup.cn, rppt@kernel.org, willy@infradead.org, usama.anjum@collabora.com, osalvador@suse.de, ryan.roberts@arm.com, andrii@kernel.org, alx@kernel.org, catalin.marinas@arm.com, broonie@kernel.org, revest@chromium.org, bgray@linux.ibm.com, deller@gmx.de, zev@bewilderbeest.net Subject: [PATCH v4 11/30] riscv/mm : ensure PROT_WRITE leads to VM_READ | VM_WRITE Date: Thu, 12 Sep 2024 16:16:30 -0700 Message-ID: <20240912231650.3740732-12-debug@rivosinc.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240912231650.3740732-1-debug@rivosinc.com> References: <20240912231650.3740732-1-debug@rivosinc.com> MIME-Version: 1.0 X-Stat-Signature: ydu351a8giug3n7zs69mge5zri485gzj X-Rspamd-Queue-Id: 80F5040019 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1726183075-203360 X-HE-Meta: U2FsdGVkX1/XntOAChaXdbX1NJXGGEBz5wjs9JKIzmPcDSg5twthtQn6/dxMQL1SEykr5KUfPdZEs8P3TgKgDLhxYnssPrS3t+HtiRkcslnWbIWD0Cm8atsqjEZRFf3wUBrlY30QZ82QoOBkj6KpX+cnYmEKjujG9PK1duV0/1QixWHr4hoiPmmEp9jtR5v6bzetj64fy5D1L+alpHZ03zMy5Gj6wVkynj5SLcWtVGhxX0bE8rfEi4qKaxNtbVjzKLykiQR2w6p/L/IfsFnTODKjPdr2wOK+CnrxHThnE2AC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1yMfrQCXW5hQKY0nget8P7sUy4GXhWzu/569IjNpEKnUxyr1WFuvNufuIHABJvA7ftgEu81PE406Fw6kQmRiHMIPHzlRkBITB+GCe1BlCXa2g== 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(-) 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 089f3c9f56a3..af4337774fe5 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -181,6 +181,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 eb0649a61b4c..000b51c26943 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -324,7 +324,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 d0dfc85b209b..04023a464fac 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -47,6 +47,7 @@ #include #include #include +#include #include #include