From patchwork Thu Jan 25 06:21:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13530084 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 867C8C48260 for ; Thu, 25 Jan 2024 06:29:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 10DC78D0005; Thu, 25 Jan 2024 01:29:52 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0BA958D0001; Thu, 25 Jan 2024 01:29:52 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E4E3B8D0005; Thu, 25 Jan 2024 01:29:51 -0500 (EST) 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 CDB478D0001 for ; Thu, 25 Jan 2024 01:29:51 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id AA7E51403B0 for ; Thu, 25 Jan 2024 06:29:51 +0000 (UTC) X-FDA: 81716857782.30.7D29231 Received: from mail-oi1-f171.google.com (mail-oi1-f171.google.com [209.85.167.171]) by imf20.hostedemail.com (Postfix) with ESMTP id EA4091C0011 for ; Thu, 25 Jan 2024 06:29:49 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="Ry/DkQec"; spf=pass (imf20.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.171 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706164190; a=rsa-sha256; cv=none; b=CSYUzUXyjjJxV3atvMOTTuWaCdEaP1bAXjnGZN6eoDsLLjqKK70+aOAMmyjmoHA6TsFgNI uSu2w+g0sClSM177PA43JXfbufVxo6Dv+ZTdJqyGdKjHqUdZEDqhMMbsOmVTOBYbMfxDxQ rrFyAuFJWlIfW1gAmLkib6PbCMitg10= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="Ry/DkQec"; spf=pass (imf20.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.171 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=1706164189; 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=kRzF2SZSpJKrBhoESLsApoQWcpLzxI/TelseFUZAskk=; b=qBhjMti33TdaO2qCO2TI1m6bFbwq+in5NP2ilJV3EJqpg8rEhPT+zx29QHGul/+1KUjO8n q1FWiQ8F4zMGy7UGQdpqwKNULz0LQWPyvPCGsh5F3Sgz/XhPENmsqQUEGD/gTA4BgA7itL I58KkLs15Ft4QrHwcCAqjuEoGxo5c40= Received: by mail-oi1-f171.google.com with SMTP id 5614622812f47-3bda741ad7dso4930401b6e.1 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=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=kRzF2SZSpJKrBhoESLsApoQWcpLzxI/TelseFUZAskk=; b=Ry/DkQecUD8af2K7Bz65xTr6/77ATvmqqBpMk9ICibvxEHIxr+PmQnDpqauavYij6q ktdJjy3LIQ0rFRMJimu79yMMttJdMrjuO1oHoKlm6Hn6IU5T9I3lkWvCLFN2bcWamzRp 57XugYkBWC8bK3pn5ibqRjcqfPryopyS6t6vkbD5pynkLDHJxWobnt1dtOgOYviEPQxM VW+wpYsMu1ftOlrO7UGNRJio3LHOxOFGF446vEyUAPUHgGWxg0KDbeQYlidufiCjfzhA WI3R2Uo9jjkpnvXztQkC25O3tsYK5z2AnJmeH11oj1cSLWh2GNAt1K/g378hAD6378u2 /b6Q== 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=bXf766DFCsPeu94Wh1DUkUunEfFylebFbVqfxUTmh30SUFvah8Ahww5mmCez2EImH+ vCLKXQT5rSPUgcbYg1I6saRZnOFssAItuk4CWYzjwkSIuqyF8A1W+7pIXFpEVNfWF62P +bHWWteQNOuLUscgeSl7BrvKyJe6lzx+0bKKgqfdwVIWkS4wV1HP0NCKpD+TukhUhyYQ wxmbn/JHTHWmJUqnpWRgAyyD0L/v+o785jJHQX21GdetD6yQgPpXoUF9Z/BG7NBJAfia DD4xsgNhoiEupZm3VQM/NuLmd0fu4R+4Y6Pp3wSF/K49MrTi2z0NBENOEftbg06dFG7V EkJw== X-Gm-Message-State: AOJu0YwX7LHOxSuNLHbZQfA7BrDBBl494X76QFTlPYbpYpoJzY9zlUfl RjZ73LxhoCN5tDJ0fBjJPtWY2li/6/juHcy2n3BN2ZVRjlFqDtZwl3OtyurnsOY= X-Google-Smtp-Source: AGHT+IErjNQJ/wKFk47rmBKTBBYuOvQY9ZOcxL+k+OB28eXMUoqPg8zllrGghQsiXCVST38v7+Fe9g== 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> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: EA4091C0011 X-Stat-Signature: qdqyadgbxtq517sp1jtz6onwki3ykqgo X-Rspam-User: X-HE-Tag: 1706164189-408628 X-HE-Meta: U2FsdGVkX1+T3CtGwBAfaMuAMareX0ECKlYS1om6u42ggL0MnAx/KHFWKTaJwn1QS/tVSSbp7C0eaks3i7I4VhlqXRW56T9bxNMEQnTvFTRZbNGM4vL83zQwEsS4frtMdTNK4x+lSuK56JoSxoInuK7obIwcc+fCy5FdViFUCFtxZ5tXOyyTYF5wwy1OUqAC1rVRWk6FwSd+HVMBAVlOzzWDzWDw/ht7Q7RCCSrnEdnKb1WlSQyVXvQYEe6tU26i3YaDljkoiZaaOWlhj3rCwMxzZR8MDL/7J6diHGN7YRntnjGz7ezOI8HJaNujTtorIxHmWcHDYcU8Ab+puGvH1cdZ+HCR/l7T3vUHkIDCKw5TxgZSP4IrToMBzoO29LCi5rUKoJNxmlCFIDIfxmx4YsqPGkstb9lOCQeEx8MmHg5K2CMd1w39ilnlcPFxZhDkceAi9o/WT3IZEPZAofs73RJa2Yt0E6bG+71s3CW8MazRnjqyvSBvUmP7DHOjHdUhOGLr0GvRRVgsGOibYWprETDP2EhSmJzEeYTe8kbiA6HWYzMQdE5+t8nBLtlKZWHARZtdOrOcHAOGIStS4YQ3Rb0afLK9OL15ilYOSGjw9+tTTWRg2fpOUrCLdmiABotsje6NahZXwDee+QSsft4RMeVsj9ACSptKKX6o/2skPrnWQPFAl/ZQFMPdGhRBENTFptl4SQX8FRXcU90V+zKognRPWaHeHjHWxr1DhS5X8qbhrJ9+m+x95w0vaPrpoE4SXXpvkVurEzwhAgFe9x2gslJFhAY3l0QfQqHeaCI3Ww0bYAXtXcvsztMr5+eIaeUBuQJZ0XpK+/lsKDixQYkCkzyfZD3c6gZvmxALcBfRLXnYJUMLPHsc0+t608BPWUQQRrUf0XVSVMi5tLmRhEmGLZNLBrz3CCvGjY/SSR08SepswvBnPBJZM44MC0dcqUaqiY79XhT0CoLC/VAFQDO dR7anQAA oM3g11xCHPub/MHRfXXdRC/dpeBXz4QZhINPczThqREjRT+yWmY00XCvLVVYgQG9L1IeZvYtfhZqAwDRTfPW9JKiBPGPsuyr13VdQIYNcS7pu1Ud5S9yzxg6ImNgdTIuJNSHrn0zwUrkBzkVEtzFEv1MI3Tk/wgd1VrQLXdbFg6JjgyOrWe0N8LkMx3or8PmHam+ypB8aFnur1iVfZeryMjLiwjubEkoYiC31auYzkiTPpB12fqy+fQn0JqzCpb2baQoeq9O2pmFBWFtzk2fUpxTrOmK1DsjEPykewjesEezzzcTlu0p3NyNnr3MoAY+WKmziJZJVkCZmGOxWstFR6sJmZWAayyIbtpN/Tp/7e55i4SDFwL6cT4blKTnIe7U5kOrOxpNhIW2JnVxMjjta5nN8PxNt8CTES/CNiuy0e1xrHJrFmTbcK5aruIu/41EsLQsN 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: 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