Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp1573501rdb; Wed, 24 Jan 2024 22:33:43 -0800 (PST) X-Google-Smtp-Source: AGHT+IEEmr8dBf01fXP+auMldCVoHQkSEW6AY5Q6U+6r+twJ4E/DNp2mn6uwvk3RXF7JdFkOWWVx X-Received: by 2002:a05:622a:30d:b0:42a:6801:47ce with SMTP id q13-20020a05622a030d00b0042a680147cemr575654qtw.2.1706164423011; Wed, 24 Jan 2024 22:33:43 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706164423; cv=pass; d=google.com; s=arc-20160816; b=DQLjXZ94xz5NRdwRRrCOG4jpAbe585z944N1hMt507abKzx6Ky6fuENAv5qHMRHX7b bLZ0StOeq1yVa5XS7pi8CsxHah/iefMlmyd1nrs+QQDh0W1WBuiHUr1X7FafiFB32s/x T5mfW0bDRMizdcSzApLrTw6Gg7wLnPl4WDub5iOYn0ecVjMPIYQ7HZfHHv18sCALStIJ oddF51/TbMUmATeozpahwq/utVvVtGGD1OxkyMJgd9L4fBAbimQDqXgl41m8hGLwdKUi cI1d12qsI97LdmKuJFhvQ+sYX3Ee9MIBOGOvu4xbw7TLeyktqmN7zlB1MCLDHeXicVFu 9Xyw== 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=kRzF2SZSpJKrBhoESLsApoQWcpLzxI/TelseFUZAskk=; fh=oy7V1wWqiaug32hsWRXm98kKNp5NFPTWbY7PiGr4deM=; b=WK7Sv7pjGR5G2eDGds2rAQ75+zFjH1F4ZotdOWxLssBg6VmCVs6FGz5KXOBvORPVvf 7dKoDESpZ5VEVPQxDB9JztKr9ZxqqOqRRM1Fp0NDudQd3UHQbvkzEIURLzATbhj/NhtK Z32Jv+TOK4j5k6DBH3qVoUPftSw8gy4pY88vEWXBW3lDZ2v1y6AFi2WOxfphan+BfdF0 gNVKtrVhfdpVPyagkgg+3RLKWOtCujSYabyAegbW7wbK1rwiA1Jpv4befSAArjOPa2oH rNHuGoZOl8qVY/tmBSt1mMz5pMYtXBZvSNsTWHieypRvN7n/RuZQnD8NwM0OCaqpoamb ABhQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=xuxqjw0i; 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-38021-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-38021-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id h12-20020ac85e0c000000b0042a3a67b7desi9600038qtx.744.2024.01.24.22.33.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:33:42 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-38021-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=xuxqjw0i; 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-38021-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-38021-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id B49CF1C24CF1 for ; Thu, 25 Jan 2024 06:33:42 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E1A1D1B7FA; Thu, 25 Jan 2024 06:29:53 +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="xuxqjw0i" Received: from mail-ot1-f54.google.com (mail-ot1-f54.google.com [209.85.210.54]) (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 E63F31B5B2 for ; Thu, 25 Jan 2024 06:29:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.54 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706164192; cv=none; b=nHLTvsOs8aazsmg18lncKMqC43MPV9YwgGzUXEMAGAFzCWZ0GPzvwAfeTG0vXPWsYrchgT90JCJw28ytMJ02V+M/kgs9U7QqVbcEeRHwfYYFI2hTdA7Ao2Jy4BK8aC+VdRc18AO5pqWD7UjS+Zt1NoSOuoB9xTY2TndOxRynXhg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706164192; c=relaxed/simple; bh=b7csYQ90Z6oxnGYgcETNSZcrDyRQTBIZ6wMjGUl4d3s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hQEKvYysN8R+1O0P14S/4FthAUB8WAN1DnwSKFY2ANgmjomwXKYe+TIJFPv2mIYzASaGMv+UJLLFWXe7dH1XleCLJFUEjaGqZnl8I7wTa14EaVgkFILlzdW2EFQorOAiq2YXkz6sCxTsLgSznVH+tADGvM3btWHnk1idf8v6hH4= 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=xuxqjw0i; arc=none smtp.client-ip=209.85.210.54 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-ot1-f54.google.com with SMTP id 46e09a7af769-6e0df9aa43dso4890443a34.0 for ; Wed, 24 Jan 2024 22:29:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706164189; x=1706768989; 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=kRzF2SZSpJKrBhoESLsApoQWcpLzxI/TelseFUZAskk=; b=xuxqjw0iQnVHKByG+iRb+j4XMO8FMJxJviiQe4ZgKYYf1g10WBQXqFRqO62Y1PfMnU +x1DMxAsCdTHKhAg2JmaykCmOBPCSGoNUzYac7Vt8C3sbeOL0/7vptaqV8OU1PvdcYj+ Hso1QXBs/kf4a7TDGM+RJ2ymLCyw6B7YYguWqzDF0fgrITqaXGKiM99dcUSk5OpSMtCu xc920ps+vmTtLnXAmdfA3eVCtZ79G7XGObuavJJvB5J3VJ9FT5n2l89WHIRszo/MqwGG Z8nU7zyubzAJn7aE7iCdShKCWf6V7q89HirufQdcZ2RBk74J3yJXEPANsYBSYiMlGpnF JfJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706164189; x=1706768989; 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=kRzF2SZSpJKrBhoESLsApoQWcpLzxI/TelseFUZAskk=; b=pFObbXEpVkZM8GNj+0bc48ssnKRsc64+E35wCS83Uq2dOUFbvRcC9kCt5GS/MNznol 3iZWmtnOb2NSvOuhrwQUVorLbRYVBHu1+cWQdgvhInhUQWeVSyRrYE4VA78jkpY9wre8 R/nl74Av9C01YNxZ8uh8HUxmINnln6hiOTCDdV9I1D6kt9fMnnDJMGIi6gICSXuDqky4 z02uu1/5MFvWjcnItVFMar5eOrkWqpUeNfZRMCuE4B3wVxGHjseZtBgDrM+zgK4K4Q6u zav2o+tYlRL82HyRJ2RTUPDB8T60A7jELfd/MoyhDtqHbuqgH9HzBEzZCOmTTcICAw3q 2wOA== X-Gm-Message-State: AOJu0YwkSavz+naHlgx2/CNiBY/IXj/fGHtKMEIMZcPD4hy/z5R5kFZi XQtfuopR6PCzOeJMfn2uBpAVm9BQThH6DFfa8cGmk9Pj09Xn5WB/3DBzZhP92Bc= X-Received: by 2002:aca:181a:0:b0:3bd:cc3b:a894 with SMTP id h26-20020aca181a000000b003bdcc3ba894mr464457oih.93.1706164189075; Wed, 24 Jan 2024 22:29:49 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id t19-20020a056a00139300b006dd870b51b8sm3201139pfg.126.2024.01.24.22.29.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:29:48 -0800 (PST) From: debug@rivosinc.com To: rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, paul.walmsley@sifive.com, palmer@dabbelt.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com Cc: corbet@lwn.net, aou@eecs.berkeley.edu, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, guoren@kernel.org, samitolvanen@google.com, evan@rivosinc.com, xiao.w.wang@intel.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, waylingii@gmail.com, greentime.hu@sifive.com, heiko@sntech.de, jszhang@kernel.org, shikemeng@huaweicloud.com, david@redhat.com, charlie@rivosinc.com, panqinglin2020@iscas.ac.cn, willy@infradead.org, vincent.chen@sifive.com, andy.chiu@sifive.com, gerg@kernel.org, jeeheng.sia@starfivetech.com, mason.huo@starfivetech.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bhe@redhat.com, chenjiahao16@huawei.com, ruscur@russell.cc, bgray@linux.ibm.com, alx@kernel.org, baruch@tkos.co.il, zhangqing@loongson.cn, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, joey.gouly@arm.com, shr@devkernel.io, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC PATCH v1 14/28] riscv mmu: write protect and shadow stack Date: Wed, 24 Jan 2024 22:21:39 -0800 Message-ID: <20240125062739.1339782-15-debug@rivosinc.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240125062739.1339782-1-debug@rivosinc.com> References: <20240125062739.1339782-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 From: Deepak Gupta `fork` implements copy on write (COW) by making pages readonly in child and parent both. ptep_set_wrprotect and pte_wrprotect clears _PAGE_WRITE in PTE. Assumption is that page is readable and on fault copy on write happens. To implement COW on such pages, clearing up W bit makes them XWR = 000. This will result in wrong PTE setting which says no perms but V=1 and PFN field pointing to final page. Instead desired behavior is to turn it into a readable page, take an access (load/store) fault on sspush/sspop (shadow stack) and then perform COW on such pages. This way regular reads would still be allowed and not lead to COW maintaining current behavior of COW on non-shadow stack but writeable memory. On the other hand it doesn't interfere with existing COW for read-write memory. Assumption is always that _PAGE_READ must have been set and thus setting _PAGE_READ is harmless. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/pgtable.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index 9477108e727d..9802e8d48616 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -398,7 +398,7 @@ static inline int pte_special(pte_t pte) static inline pte_t pte_wrprotect(pte_t pte) { - return __pte(pte_val(pte) & ~(_PAGE_WRITE)); + return __pte((pte_val(pte) & ~(_PAGE_WRITE)) | (_PAGE_READ)); } /* static inline pte_t pte_mkread(pte_t pte) */ @@ -594,7 +594,15 @@ static inline int ptep_test_and_clear_young(struct vm_area_struct *vma, static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long address, pte_t *ptep) { - atomic_long_and(~(unsigned long)_PAGE_WRITE, (atomic_long_t *)ptep); + volatile pte_t read_pte = *ptep; + /* + * ptep_set_wrprotect can be called for shadow stack ranges too. + * shadow stack memory is XWR = 010 and thus clearing _PAGE_WRITE will lead to + * encoding 000b which is wrong encoding with V = 1. This should lead to page fault + * but we dont want this wrong configuration to be set in page tables. + */ + atomic_long_set((atomic_long_t *)ptep, + ((pte_val(read_pte) & ~(unsigned long)_PAGE_WRITE) | _PAGE_READ)); } #define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH -- 2.43.0