From patchwork Mon Nov 11 20:53:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13871347 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 1CFA5D3ABF6 for ; Mon, 11 Nov 2024 20:54:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 662DB6B00B2; Mon, 11 Nov 2024 15:54:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5EAAA6B00C1; Mon, 11 Nov 2024 15:54:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 43DFC6B00C2; Mon, 11 Nov 2024 15:54:25 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 240D46B00B2 for ; Mon, 11 Nov 2024 15:54:25 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id A25CFA038E for ; Mon, 11 Nov 2024 20:54:24 +0000 (UTC) X-FDA: 82775015400.08.45C1B47 Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) by imf29.hostedemail.com (Postfix) with ESMTP id 74F0F12001D for ; Mon, 11 Nov 2024 20:53:27 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=yWVfHQwi; spf=pass (imf29.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.51 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=1731358230; 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=AMPZIqdzSjfHq7xJb4YaIgEu2GPeUmiCYK4jkAGMc38=; b=E/jvKfwHYiC6j1pmAXg77EqoUT9eHzQ6zC5oL8Cp9Bd7D5p7YnDcsw3a0Y0WJ4ChewkmmW D622Vp3dMegTjNjaiuwtHIMyTD7PHzzmUEDMwtSJpRavxHg7rcDIdpWchnfaKh33/Kug+w gves9+RPaSqSdgKBFB8boClUbD1zNEI= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=yWVfHQwi; spf=pass (imf29.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.51 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731358230; a=rsa-sha256; cv=none; b=TSWjGLT6PdUp1Bvl/iylQ/yFfqyavYbEhLeOtxBkE6KvTM0Uq/idZ62i4Ho01dLPDNDbLk 8GK5J/7vnpcnvf2Y7eVRdht1X2t4QzaLFaeeBH+KA+cehSOPAtunLWEDfXsbMIdikaZsEi F2gZunf5E6FUS8K0tI7qjaPupPerxpc= Received: by mail-pj1-f51.google.com with SMTP id 98e67ed59e1d1-2e2ed2230d8so3946763a91.0 for ; Mon, 11 Nov 2024 12:54:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358461; x=1731963261; 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=AMPZIqdzSjfHq7xJb4YaIgEu2GPeUmiCYK4jkAGMc38=; b=yWVfHQwi+SwpUpDV1MHpYrX/sNLdIGrvx4nAyfVcRnGI0zHZekYyxhooX+m42qOgbq x2x4Zxu5QUEhBk/hedureZpt7FbtarakRMm5WqQ2RqpCoFwzt2hI4CpLQgPLxw4xhb42 5WWNf8BRow0xhC1G+EhhDb275RlrsP5+FlEnPzN0sCk1IPR+E+nmvVd/WJqJexuMamNO 56kNU5rgcRAK9aLzN0GUFpZdbXQ49vT7MfEpTuL2F1ucp4QJrWde94rVKdvJnb3xQdqZ C78LA2+2idckZjKGDuZinZEjOUMNEUFXOL+0dK24GwpOHancy7RH/Mqpr4lECjuBcArb HatA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358461; x=1731963261; 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=AMPZIqdzSjfHq7xJb4YaIgEu2GPeUmiCYK4jkAGMc38=; b=tMv8mRuooH7jwV4/K3LKQPg1oq49Mvw8mAlAmQ0Bi34+MZRJVYaOk4CkRjAWC9XMj/ Bh5k3txKdlvP0pOjOgCZ5eRfqd7Eo92gMOreBapDckCFKXnCyulvWbxElp5LYyJCfi/n TWnyWXCmubIMw60MFBzyV4czW1uLH0f1jlF5zdilhAEyjEp2isSPR+Xf08wmPZkzSKzW 7Ty0l4TtrW87ROxCWL0ycIy25Qq6rQzgroqRYIH69rQ685DSejXpZl9YFioVJTKFklln ZkJXvFGnwPq5E/BDOv8yuHU8LhCTeuR+NQLFT+OITpNh6Rq56krb9+TqFB9+mqEwJxRq ZbAA== X-Forwarded-Encrypted: i=1; AJvYcCUcN2+uOofwmU33ifLUiHtIaq9cIp4FI5WBaSPt64vPnwJwRbCZ5e7ehm1GP50RYaY8Laspomu77A==@kvack.org X-Gm-Message-State: AOJu0YxkSvuuYCtmt+S5LPD04CIUsD6XiRQ4dW+7XClcX5P/hW7DhyRy mOKvSQz1aAz8z5xVnNBeqYOkGnyZSQ6fIf64FdPu937WpkGhgwr1s1EIBZgcC14= X-Google-Smtp-Source: AGHT+IGRLWN5kqeXRNQn8yzpCXFOSvMQOUs+A8CKa7xWJ/vZOijjwVJZJo9LQV0BVahQpTbnhrdFqQ== X-Received: by 2002:a17:90b:2b50:b0:2e2:de27:db08 with SMTP id 98e67ed59e1d1-2e9b1770bffmr17545652a91.23.1731358461465; Mon, 11 Nov 2024 12:54:21 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.54.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:54:21 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:53:55 -0800 Subject: [PATCH v8 10/29] riscv mmu: write protect and shadow stack MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-10-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@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-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 74F0F12001D X-Stat-Signature: u4iftsupmip1ubazquy8uke355euy31j X-Rspam-User: X-HE-Tag: 1731358407-783262 X-HE-Meta: U2FsdGVkX19eiQwnwTs88gYGUYXG05jnaOT1TZHTcvtMK9HKhmDR9+O43+5bCPw+lds7r5xjhEtmenLNLGEUITst+k2b4Pq6wbD5OqS8sz10aNldS0bXeyY8lU3KWIGU74Jxjnnt1m21hxSypMTUjhTngLbH9sP5pR9JZE6FFvKCMjO9WYmHvli5yXIhMUfcEu3c+EHk845Kybi6kC8xw3+q+anI17QOmFUqD1EPkR+eZM2VrzugViWylOdBl17NSMXRy5g2ovpti0TWYZJ58YZwyr9PIAav4VSMKra6OecTtAwo55ZdhJYkJh3uXpFsOzXFFUtghNKvPX3dnGuMlhAllcE/KHWcX+hIUSiQzFmap+EzVPyUu6ypeRXv3dZVSGBFJG2LzXIWVgOFjRxrjRAwAfQBzcnJ177/glH3DMmu+EycviJKolQymerdV9DCyZGR+LYyFPrgWdAwoRQIYXW8OLyRs69mMzOhAodGET0iSsjPdd3SKOamE7BZTelFqGAbHXzW2iQ4cRthX4oKGkI8bQPF6Wi5cNEdhDzVOLZcmKNLOCrZXzWi+iXGDewZ+J/oYUwHh4mRFIL/W9Wbe5AfbEjftlay2vVUHWOcic9dCNDT33D7Y1tfevqFG5vOPt3rhvdLmwvWNExO+LZNMVXKvVOfbyDD6AExlVjqPepGPTLk1V88tp6BvH1mKsuuvP1JHANHIox/gwAUoICWbtIEGTT2pBJZxKdG+AmoPJWzM2g2lRc4h1y1PtyLuT8ghnJTEV0zAHVIgUpeAYjHtkHBKo75198TgC5Z+T10bHgcCMAqfo3K3zSLi4qocQw6nq9UUOIsKoeIKmr57uBXpW0J1LGJXSpoPxw7SrD1Z0bChFvUFHN5eUmUE4qYluJ5of0mbGkDqqyTdJx3AFfWLh5A1ZifTgu45bUijGtTryAdZcbXJukHkTlPySweMyq3dMoXoHDSNixKiX25rZd fm9H0O1P 4AsVq8Kq6lRIiYKYM7JcCIPY0PTUgfYEA4jEfoCzSnTKNEzeb3J0iDZkEcwEo+AIO9pHsZgRZNPMxqnIE5sEUSNu5kYnFJgRpWyJ9ybJpE+DgE7a9FLd8w7g98X6KnXd6aLQihA7L/4zpoYz/Nvh0Nqjx8hUDJLYzBU7nzq+ZhIU/XuOIXP/B30enVoT7S5wNlmfU+ulTGhhvZD35ua3Vf6p9+Tg8ZRagP8sAph0jNDsfI2lPSEx/vf3j9EQgF/wPtkRDthtfJBvwXqFqiyPl/qGSjXlPLqy4BzGyOW3RvMy42I901Ytm/AgD+GQedTr69HnGEatUgrZeRVtnbe71JqaMmyDSTADtwR7cD821/CEdR2Qv1NiRT/N4aiEdPoIfzs9/1IFwBISrUK1vlcPaS6SjXrWmCInZLgeQUWB/REZ9TCuomWtnyIcGP+hSdL4KEpegQgDv+xMfcQ0C8njM3rg+LA== 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