From patchwork Tue Oct 29 23:44:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13855729 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 B4082D7494F for ; Tue, 29 Oct 2024 23:44:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 63B316B00A8; Tue, 29 Oct 2024 19:44:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5EA646B00A9; Tue, 29 Oct 2024 19:44:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 43B906B00AA; Tue, 29 Oct 2024 19:44:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 23CAD6B00A8 for ; Tue, 29 Oct 2024 19:44:53 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id D8F291C1413 for ; Tue, 29 Oct 2024 23:44:52 +0000 (UTC) X-FDA: 82728271752.12.014DACA Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) by imf24.hostedemail.com (Postfix) with ESMTP id 3D90F180013 for ; Tue, 29 Oct 2024 23:44:47 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=iKioVAwl; spf=pass (imf24.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.169 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=1730245436; 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=c0B+zv7jbCWDse6UooVJF1UXIWGMB9DKJ0N/6xrNRow=; b=pH8VeuMNuNWkQ8mgJiMOVsTsDlJ7Vpy4bLwCieHj391af/UAm0bknh+k/w3zt7Bi8/4u+v tR2x0W/cxBrWHfIQn2M9045QtZodewSSlexVNV0wvxclfbR0MVCak/ZT6Go6mk7R0WC13k XyZP/mzTEnCo1EwBA3EyB00jMP5DdBs= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=iKioVAwl; spf=pass (imf24.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.169 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245436; a=rsa-sha256; cv=none; b=CqsLgCWuE8Yj/G1OzfQjLdDASEC405nf7DlljKPG//ehc6Zeixu5wv5Crv8iLgDBHqC3Ym B87gVRBtfSmoDCh3sGzq0wKJZOVIkVN8QwoOwSPeHko87AjPXJDY/bVRTyQ/2OMS6SSB5s NbP4UAODXoQn8WRb+l0DvymF8noMQbo= Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-72041ff06a0so4003023b3a.2 for ; Tue, 29 Oct 2024 16:44:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245490; x=1730850290; 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=c0B+zv7jbCWDse6UooVJF1UXIWGMB9DKJ0N/6xrNRow=; b=iKioVAwlF/o0RQn+M74X8EwFnkdBYhtveyrPoKFRSP0KogByA/lcTw7RZjUY1DWTww IwJ/xJF16OOwLBLaDX48UAPOeUtwzzHRYQ0bVpPoNmCkqb8y6q9bF8UEumQHtiIbr02E yMtL8BHjySBxl9Mx22FYGp1N71/OskxvNLL2OVxCVPOFVm5yslHB5V4YKGjLPZ7G4RWM TueCgls2UkWFdRHN+cmwwJIGNJcr1rxAa3cOJrJnL6bgVDMN4YAuvcBwbi+37euzAAxE ZZSReh+V6kx0m+/EjsM+aUnGk7gC0QU/N38yPH5tWCn1JHJoNGHvZoyyukUO3F0ZdJVQ JTGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245490; x=1730850290; 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=c0B+zv7jbCWDse6UooVJF1UXIWGMB9DKJ0N/6xrNRow=; b=c/w+sofMywQz/UmrrBZUc9gqtWDNVCrS9HTbZy9sUIQyV1f/feKmw33J/J/a5zQOka VAzQIk21IFNYHwbQgji26YPI6vykHldlD2LhCaBtg/o3kNkEJyCsBnyAm10JtG0QhQNc 1TtYT14XFGzGLkHfeQYbCJ3xy48KkLjlETsN+WMxSt2/CDTWbAjaiMiVsBCNfkSgOVvs 4QGxhW21BV6cvtcK5kZNCelhdWadpUT2buDGDFIfmXuudvqrxnZtHo7koy0OMJRDX0A2 hoe5gQRkGx5O54lCgkKZAXn9Qh8Tt1hiMziCTasu7kdD2qB1xUKQYYo26F8BhzpNTtUn BX0g== X-Forwarded-Encrypted: i=1; AJvYcCUo6NgenMF7PcrXqEO2UYc4SRWBfb601+F43jJfDOafS67ZY2tlilUTdF/N27hFQRAOaNJiHRBPFg==@kvack.org X-Gm-Message-State: AOJu0YyQzhBFFRKOh0I6atENeEuvy502qsRcfEoFi9hwYVzjN+dvHjh2 53g1+3z95vPGExv4ti6MmDcmBXonuR+oPhaX8BrdELcXHJZeBz65I7mAyLwncNk= X-Google-Smtp-Source: AGHT+IHBXcg721hFEfeXndFELLRbZaKej/qAWRXm0LpVc3pKofZsRnhCap8fGfOK1cqNWExKjLYj9A== X-Received: by 2002:a05:6a00:3d4d:b0:71e:a3:935b with SMTP id d2e1a72fcca58-7206306ebbfmr19631100b3a.25.1730245489674; Tue, 29 Oct 2024 16:44:49 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.44.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:44:49 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:13 -0700 Subject: [PATCH v7 13/32] riscv mmu: write protect and shadow stack MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-13-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@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-Rspam-User: X-Stat-Signature: 5n6mb1rd93abzj3kcja3n93dmmbzrfcs X-Rspamd-Queue-Id: 3D90F180013 X-Rspamd-Server: rspam11 X-HE-Tag: 1730245487-44482 X-HE-Meta: U2FsdGVkX1+OQeBmU63DlgwWVGJLQUeuW4DBZvFJt+u/jajCLuibrjDenDUBrAczdGmpUNjlnWZraOMe8nVQp4RAerHQEaJES2+4Y+FP0aUl61L22gFvq/Qo10YaefJpVivmzFwNOWipKNbJrQl0USc+4wE5FRgjSIT3Spuhxi2/t+irVd1RYFK7V/km6GnNmUrFLnqV96dGlkFo0NWK8q6mGzfJEUJU60qsiiBSZsZf2+GT+e/kSiX/dy9C1Gncur58p+AQmKBZ7Mbn73w+DjQ1nKQUMPKhDCabGGoROJ0DOnFAXSbjzncTS3Ni7Tha1drOapmeDV1tcpzB456LxOjYKOCd0B8mZFB6Nyhxf/GVBDgtFFi3gH/Yt8ehyojrPWQxmpnRhoz0kJWqcsKF3GD/CQceI7WrV2xR7gAF5MPTVVgC+LCfk5HqB9YZZAk7TCqIlKccA1+ra9TYgCLXn+kOGMht0UyNZ9zPwIhi7cptXwiYAMEwfmoB6TtVCnL6hvQhh3eUSMU86wFD3qxQO3sFvp2MRJJTI5DvLz3jv1MhF6bbh9Gk6Sr9oadGtfLgyclh0J6iIF5KCPa3gH2PFzcXIlW1i4wBa9OYjge7aIjDnxKs9HQo448LKzBNHWQ5T72GxBOcy5ms5/K6rBoVmSxN+bON0rRCZ4BDz+RIXxFudfnXXLaUHysaUsORLZyXkRXASmuI4pDF1RVfSi6+ZYD2XXRC/UTtTXVon6esjx6dUW1xJJ8FhWozO8K56mDFC8hvr6Eb+6eiWi/pcn/V/7lYNfEdE9debAnGzBrET28g2bSX049Owh3soK57nlkTbaBI8b6CKqHDgVTUiRy7C/rsNlzIunXgjvxS8gYxDDSl/9Qf0DfFFNxCiYOyRgarYDMxxjAZJNcyeolWo0WChPDS1ELYbp74EZMM7yTvOW3GDto6UUlG4YMMAsDp82gV3OPage4EeNZD7ekFWVP efkcHpM5 sOs/O1QUZNZzcK1y+9C3pGXH0bAhUKmzQ1+/18/jCLo0atCbm1OvVzhs66mdJsRqy46LmrxQk6OVbFCNtoWhOUUcrDt50JJ4I4iB/XxOwU4W25nEagSNOfvR3o8X4yu6Fl+maKP3cf3LlKW43jtgb4hhXvYVpQzmm7iSHOuyUWDfpsYbArQm0UD51IhbgnpRWgsSmAPxr+8Ql5s8eOTzMiJn0cAyrBXRoHrVBrbfXi0GuDuIwns6I3SBLEWSeEt9JQCWwFrXe86Wi2u7/8PXNnmY4PbZAygPQYpi8vwA/LcR3Tc9ntNvNU+09o6DRq3FakcBPifuFrkpN5asoLc3OC1BYYQezKMk9tM/sDWG7/276qT5o1QR2TMpF9hw98gVN6S4vx10wNvu3vo+QwdoFp4pB/DAAvww5Uzj2NTAjpR/A8YvqOXLKK+ICmZ8tPma3vnJUlykjlZyMDN0= 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: `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 shadow stack 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 Alexandre Ghiti --- 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 7963ab11d924..fdab7d74437d 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -411,7 +411,7 @@ static inline int pte_devmap(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) */ @@ -612,7 +612,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); + pte_t read_pte = READ_ONCE(*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