Received: by 2002:ab2:b82:0:b0:1f3:401:3cfb with SMTP id 2csp868368lqh; Thu, 28 Mar 2024 21:48:41 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUlkfSIZWJQYW8l3ZMgRAw9SemFVYbckjaIn4LPsgLv5Qall58bNfjZQEZDsxfjKXwA+rM5ey0WYhEcf/qDtPWRhPD0Vpw6VtC61VN8vw== X-Google-Smtp-Source: AGHT+IHuyn2e30ZxSq+YR9lOP+yk0gWB7QwA42WT7YUMIk+zPvXxxpqNuAAXtuYzhznVHPCINpxi X-Received: by 2002:a05:6512:3128:b0:513:af26:8cd0 with SMTP id p8-20020a056512312800b00513af268cd0mr826173lfd.68.1711687721385; Thu, 28 Mar 2024 21:48:41 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711687721; cv=pass; d=google.com; s=arc-20160816; b=a/U+RVINFHsTvWv/RMtCYZrk0QvG+kSNSwypPaUFICLZE1cCzB1X7GML3jz1mFCECo FaRDsk9G08PkW3RvmCUIzu6o/DvtlThfwIOUXUXePK6zWvvQPkqqlwer4XqHqoI1sFb9 FfIuMHiI72lOpoixSkLefH17WO5WEboQO9V2SEmrFjyuGKfPHWWiRotq9LoVtsPdWfQM F9sPaxbRZpidY6Vf11BnRg4dwTj9ABI19FFAucfrNU1hWWA9JSZ+2m5pNIzgX9ftuGom 9h3cFX5Jth/GjUD5jCxUyhty+USuWul/Q3T1XOQrD6lffx75nv/rFw0qlefXg2oDH7i5 yk4g== 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=+4XcZbT52YyPYTbBbJHKVVBDZyhFzr+R5No8SlxzOH0=; fh=0Vn6oJ8Rdc/GZaeflhksNLmaS6THx0PpfW7rb427EWc=; b=J3OXXOIl7NhSEstHfkKfCZHOINMvNcIUEaAqhY7wV7uS3purnxHNMiKm/o9rDvKJ4q NwVVrkaoPOse6TGQkbE8PLvl+cw7RuY7AMqdrqO33kN0TTkZ/BP8A5zOe80lMLaXe96K 6TJvNF0xEiH0ZREN8kI4bkzyRIobr/4fywRZVvtPqjn9b9aEeBOzGrIh3GyWM2HCAYt3 +bCWpJB94cHwKLfaYsFNfEqp8HifK2O042IN6DqJb0uEMxVpwdt+Ei/hla7O76MNUjXk /FIjYE8PatnsH5ACYkteTThaFohPAq/ruDOut78s4yoynX5ZaJeBdQgTsHdMfIn/mZ+z NdDA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=G+da6zSF; 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-124129-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-124129-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id qw41-20020a1709066a2900b00a4e37413bb4si203510ejc.563.2024.03.28.21.48.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:48:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-124129-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=G+da6zSF; 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-124129-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-124129-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 am.mirrors.kernel.org (Postfix) with ESMTPS id ECC051F22601 for ; Fri, 29 Mar 2024 04:48:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5F8F244C6B; Fri, 29 Mar 2024 04:46:28 +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="G+da6zSF" Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) (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 CAE194436D for ; Fri, 29 Mar 2024 04:46:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.182 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711687587; cv=none; b=p9sul9rD8w3zQkQADKvr9j8pq4y9Xr+fETgEkyhNH+TaQPQQYxu6PoU7f+ngVvievOUD0QPX9URzVucVb0Etxem8t+pGPSI5iox2BwA0PFbFW3wv4xtDBHxwHXRApw61s1YTB9mWAzgDqe9FjyqzOer3ms5h5lTHftcxvX1cn08= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711687587; c=relaxed/simple; bh=7I6zjgCFV6RndlqIEMECkC47T7n+mKP10TbnVazBOuA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=YUM7LnHv4rkoH0XIc7taljbUCjRy3M8NSXFFQL7aQTM5z5mqwKbIn++i1gt2XIWcf/GAeTf4M/nUKzAYLNI6fjUwMVrhfQnmiidIZd58e6hmxKDu7dfNHanjpt86jeVLGzN4hFFmYiCXyF/rY3Lu1JURCSAVGAIMgy23qKgUmpE= 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=G+da6zSF; arc=none smtp.client-ip=209.85.210.182 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-pf1-f182.google.com with SMTP id d2e1a72fcca58-6e6b22af648so2012692b3a.0 for ; Thu, 28 Mar 2024 21:46:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687585; x=1712292385; 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=+4XcZbT52YyPYTbBbJHKVVBDZyhFzr+R5No8SlxzOH0=; b=G+da6zSFsHX3UgMCnG11T5CqVbMdBqvSGEKLyoidUP58R6vsbI1gMyYb/rtXlM2x9q lZAKpMspurfr8lRn6e161UQqgfdMQyR74oHnYHGkIPKJ1eDg+ptyC89pvYJDif1tjY07 zVpzy3pbA2OY8Lho+HQKIMnqlOjYIiy4yfu5KoZGlYV5rR8sKGfJO3FtZJM6oPtpgagH ar3Y9RSmkhZ1MDj3WQ9W5FGmjHJkExpho21JWRizYDLizvJ0kZoyN2Po8sXaIH2AyWyS 9zMlMXDoGeQI/zr3yYai/EdmoJfrA3dghbi9TE5MkltPhYtSxbnpsmr89a+J4MS2FEqe hByw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687585; x=1712292385; 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=+4XcZbT52YyPYTbBbJHKVVBDZyhFzr+R5No8SlxzOH0=; b=o4LKrH2MV6W184zIg3T/e6rJtDXTaC4vvWTjxYEVk8cg3EcRUweQVWkiyW5tdfU2jm QgrLAlKpOKStYLxjlogsqyW7X3aclTIiF4tbY/1FCGFNV++QOEzFLR1QYfYs9fvQT2pA MTmGAK9aBr3ZdmtCRARFu51YW8AKunhazBcRVHeobC2OAcnaCKf1seyTc7DY6MavXeMT RZrrtmBAlqVVDNO4GYBmaQLheIOFohRAu2gUP76gdMwnRDJoHiCW2Zb/XylKYeNJqRD4 IylUv8gyr4FwlGcShEMggsCmgZUXiE71z05+n8wWYHMbKO2WMe76uwHTPJE7gKH5Q4Af Pn9Q== X-Forwarded-Encrypted: i=1; AJvYcCWcupRu+hDap5Ojdac+ZIDuv5g9AZeLVd8wkHnQq8lUCt8bdpumFJr7GbnVagKVuE+fqk/wi45+2XEMP4rhb6oxz9H+FnFja8c4bHoZ X-Gm-Message-State: AOJu0Yw62JniFRnX6yEmjKRSVVGYzZ8py2jyv8TMigrKyxBw4viZeQ9Q QVb985dqKeU88Rb+uBjNa2UvHZb8KpaFUQVdZFIC29MRrtrrq8pGhhkcAWI5lKg= X-Received: by 2002:a05:6a00:a29:b0:6ea:df49:c8b1 with SMTP id p41-20020a056a000a2900b006eadf49c8b1mr2139229pfh.13.1711687585128; Thu, 28 Mar 2024 21:46:25 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i18-20020aa78b52000000b006ea7e972947sm2217120pfd.130.2024.03.28.21.46.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:46:24 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@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-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, tech-j-ext@lists.risc-v.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, charlie@rivosinc.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: [PATCH v2 12/27] riscv mmu: write protect and shadow stack Date: Thu, 28 Mar 2024 21:44:44 -0700 Message-Id: <20240329044459.3990638-13-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240329044459.3990638-1-debug@rivosinc.com> References: <20240329044459.3990638-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 `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 9b837239d3e8..7a1c2a98d272 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) */ @@ -581,7 +581,15 @@ static inline pte_t ptep_get_and_clear(struct mm_struct *mm, 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.2