From patchwork Wed Feb 5 01:21:56 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13960370 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 E17FEC02193 for ; Wed, 5 Feb 2025 01:22:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 61E17280014; Tue, 4 Feb 2025 20:22:16 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5A700280001; Tue, 4 Feb 2025 20:22:16 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3AA6D280014; Tue, 4 Feb 2025 20:22:16 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 1C996280001 for ; Tue, 4 Feb 2025 20:22:16 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id CB54C1C6257 for ; Wed, 5 Feb 2025 01:22:15 +0000 (UTC) X-FDA: 83084140230.04.B384A86 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) by imf19.hostedemail.com (Postfix) with ESMTP id C45901A000C for ; Wed, 5 Feb 2025 01:22:13 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=v5KBGVMA; spf=pass (imf19.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.181 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=1738718533; 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=rjuyUsrRmjsdgE2eHiV1ew63cZ7gXvE8Gyqp5e7FHmI=; b=8Z3rtJRtw3vAtlZyUmKM1TpnbphlHPWyaAh6ZcB2c9GOH9YIz0EOmd5jKNrqGFVtcM+Y9J z37wV5P8LP8/9OtQbYl4k+7Rs2hcxxR/syikTImh7ps6j8nExFO938MeDRUKVde9+bEUuu gHUAujHgzcNq+Yh/y9KO7YcszXe/nTM= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=v5KBGVMA; spf=pass (imf19.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.181 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738718533; a=rsa-sha256; cv=none; b=p1EYfnGXmFZP+JVRy3FyCMeDcpDCoXqMHwcPKDPra9nENILhPNBJz2M0U0amUM88ONVSon B2gN0blL49+//8TljmETsDYbzlrfmt4JPDa9Vs/dd2JU1eauiYq2u+ClpMUgwU5RwxmuBi EFWBAqH4IMHyIvHKMQRnucohO0Ok3UU= Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-21f0bc811dbso5060885ad.1 for ; Tue, 04 Feb 2025 17:22:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1738718533; x=1739323333; 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=rjuyUsrRmjsdgE2eHiV1ew63cZ7gXvE8Gyqp5e7FHmI=; b=v5KBGVMAq+6c06WSljXHEBijtMu38bzc0Of8HRJek4u4WdxF1Bo/er70UAK16JhY1C b91fSBC0HXuD639ZjNi+BMVlEX+uCSHmG0E/a+xGq80SpuE5px892DsI+Uu2pSz53jB9 er515QlAwL2Hy99DAkQtD8Z8je10niEaWVctYaWxPY4ckdxs5C1wa8YNpe2LQMgVDnE5 csqi2sOKOXj69LfARExlsuqCSnKniteDVkrnOtLIh1NctcgJBE3ENNqQSsXP8jXW1UA4 l45ALeuGaSzsxu10PowEo+qsbTbsP2lYMXwJNjCaRn0qHzxdAFP6faYe8DBOjQ9AGyHf NpHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738718533; x=1739323333; 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=rjuyUsrRmjsdgE2eHiV1ew63cZ7gXvE8Gyqp5e7FHmI=; b=KQMpE1iXytg96Q7bIe6/ys5WH/M7NIT5/uV9or3RDrt+pIDgJv3OpPFoS7lPzgDeG9 mHr10CH493hGjlalW/IGoL/JNNMmjzfN21BtIh/xUJ8Pr1brTCnZNqHSVvzcgiaceNBG akRgTnJ/pb+IDhOMAPpu9PDxnYC4gwFZIJ/GAjXUI4BEFsoD4QpjoI4Hlp77BUDDHPQT b+CsyN8LnyPaCWrkvJQmwwwBsEB0YGVgXw89LseVFdz5IRm3IT8CBVC9Ec5ZDvTwYNCV bw4ALV8/96Z0FGuzWVhPU7vOV+ohv8ZfC1jN9DtkMFmatOHmae/KJuJ7ijyY4A8E20ww y9FQ== X-Forwarded-Encrypted: i=1; AJvYcCX3DnDIVR6Wsp3I9RLoYO3SMU35QU2WRx/ixOyNf87AovXZwDwrxQo5JW5MpK8fYCEjzSrioJDQ0Q==@kvack.org X-Gm-Message-State: AOJu0YwHzXO47bNqDlDHuHTl9aCAhsRvhN10JWNvX4DBzEoJuCBZFTbI S8u0F2QLl6JYGBzQ8h6HPecnhtLH8WtTOfL5tTyTTljJ+2RelL8ugmiApMB12kk= X-Gm-Gg: ASbGncvK46RK/hbzB7sMARK300WiPyTkK3342h8LXOf4ldRDRaRhIut0qwx3K2t0V1G +FtCGD1LyY37IoGfO9pZckLIrh4V3YssBxTiSrZBaz2yxtVFdpNeNkNx4KyaacET4ohv+kXoEq+ +LvrhkuTkoUpsMUTt5tXxoEE+ciIyHAg17NrICWa+ehbl0GVo/FoWaMyLH8e1I5x7vz+ak1pTL7 Oi2gWX1T0ELUSxrcT8XU5UPs6YyICB7lwkY07Pg2TwfpN04L5vASY5dQQeJwhJ+DSM1MVst2VKf nYJRWbC6dmuqrdZXCbhomoHZAQ== X-Google-Smtp-Source: AGHT+IHlwnAptZSky9Y1DYo277tRu5dBk5BHHEzzWIoM+u4E8FH3BeXq2ANIOmZF+k6xGQHG5TCG8g== X-Received: by 2002:a05:6a00:2306:b0:725:4915:c10 with SMTP id d2e1a72fcca58-73027296652mr8559134b3a.10.1738718531190; Tue, 04 Feb 2025 17:22:11 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72fe69cec0fsm11457202b3a.137.2025.02.04.17.22.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Feb 2025 17:22:10 -0800 (PST) From: Deepak Gupta Date: Tue, 04 Feb 2025 17:21:56 -0800 Subject: [PATCH v9 09/26] riscv mmu: write protect and shadow stack MIME-Version: 1.0 Message-Id: <20250204-v5_user_cfi_series-v9-9-b37a49c5205c@rivosinc.com> References: <20250204-v5_user_cfi_series-v9-0-b37a49c5205c@rivosinc.com> In-Reply-To: <20250204-v5_user_cfi_series-v9-0-b37a49c5205c@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 , Jann Horn , Conor Dooley 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: rspam05 X-Rspamd-Queue-Id: C45901A000C X-Stat-Signature: xhgj6o1567m56nr1h3f7893at3opmtwu X-Rspam-User: X-HE-Tag: 1738718533-602248 X-HE-Meta: U2FsdGVkX1+Xhof7nZ7ktWcc0XTLHFCjxz5aBGnz63enBtYjg8u2XypYNo+JUtRSxDKSYGYegalWqteY3v4lFKn9WESwZ9gmmluBCZhFa2Dt9qpUe8o8hWENnLhEs/Bg4qqa1DT1aFje1Mp6vIsMq4JMWvSXvbzwpQfl1gxiDcnW1/sB7Ko6ffL/EEinZ3ezJ9gHkdBKxPNwjhJGZ2PXx+bnipHj//uZb6K+kvoOI/4LGvg9JwC0UpU8fNYwhu/0mjfvPIpPk5zHWS53oeGElfAKa0xXGXyXeUJpIBVeo+Ei7ijH07u6b2zN2pq7g3acvzVnbtL7xBCfzd/mDl2h1OkTvE838HPD16wdstUmUcN5kkqMG0nhXMoXWLsgoxpXslDEO9QiFip6FSKgYh8808FTN5r3QdmhnFW7YL6keMHRLMy6JLDyHk/S3kloKTuTXkxlzWhepXHbfzS3IlaFIbw3tWhbnvhwCoKgHoRGbcm2cbcZwjr/FJwcclaQwbrmJB0GxLPI1nlB5z3XXzBQgBnaboPabi0mqLhISG8u0LFm2+oKvGN04DreMkKp9sHg6QD2/krNazNS05DTFO7K1ZBqm0vFCGPJUe2VukMo/OjL+Pb1wUwmZHth1kZe8DPBAvTYaEOkfhWymGQ8u7rZHXnBr2V8ivkJ+BmkYUkg43UIt1FVFJLFPgXivblIENwR0Gy/0LSxA2JtOOl9iQsOpJ31MLjWP3BLNuyQRs/5kLhWwMmBDmJNlWddqg4R1Kb+7FAUxcoU/H74ZEMxst/xq+TU19qVvsxWXurr8BHP6rLhBo0iMRdtPD6JLkjHrDFiAyQ9cLqoAEEvdI8S3OqY+J7Bylp6xjnQ+caDUOV6fRwoay9jHSSI9pPgG9sqf3m6qyk3cshWQ0i+SqCgp7Ygg4MgjmSbcuOvgRlUZLiXRayoO2VYBHL6yMMoutNPjoCr5UEmirE7eky2Vdw4sXn J5syD71V /zSA9lU9ILJpG0bTvhEt25sTlIW0e2GyvKL3gwQFExUd1c/YyStSst4C56fhk0PdgcQaXgxE7HvEgExCSwIaw/AdTd1DdFQszbTERmgptOcuy5LhI9H4WCR8t66mlUsJ/MzCb2qbZVCXWG/3mxCvd3cqQk5iNwL8pTSpf96LHVsplx7rJrg72h05I2S7WUyAy/c37Bqfq8Q8UxG/tycc3KmjTyO6GIjWlTDayC/2x7dZ/Nbqm7PtKaa4Lyc7sOIhuazlP8IAUHn4ukNNrRBsMqxhAXUNo/amPwm2jaZpjB2RSISHtBRQ1Lznwhrdch8TWISsakKdkTxolQAtYsehXhg/tNZet0gsOiQMM7wte2zpaOswkPAmVNOjFKMBr6zTOuYBg9bm4g2EcQISMWwdkvlNksiuHUXXF59cQU/m6Idc12iQi2NlDVN8lLsaxK6hGr6pivXPTJFuLxHQ= 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 ccd2fa34afb8..54707686f042 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