Message ID | 20241001-v5_user_cfi_series-v1-14-3ba65b6e550f@rivosinc.com (mailing list archive) |
---|---|
State | New |
Headers | show
Return-Path: <owner-linux-mm@kvack.org> 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 35A76CEACE2 for <linux-mm@archiver.kernel.org>; Tue, 1 Oct 2024 16:07:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B486E2800A9; Tue, 1 Oct 2024 12:07:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AF711280068; Tue, 1 Oct 2024 12:07:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 971262800A9; Tue, 1 Oct 2024 12:07:41 -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 71605280068 for <linux-mm@kvack.org>; Tue, 1 Oct 2024 12:07:41 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 381E2A8E34 for <linux-mm@kvack.org>; Tue, 1 Oct 2024 16:07:41 +0000 (UTC) X-FDA: 82625513922.05.2BDBD30 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) by imf06.hostedemail.com (Postfix) with ESMTP id 36BED18000F for <linux-mm@kvack.org>; Tue, 1 Oct 2024 16:07:39 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=wRcQ81tb; dmarc=none; spf=pass (imf06.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1727798718; 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=MXCAuCvFcLdVAI8Nz17ZJ3qy+tQU7Y472IuewBGEY44qte2zdkXGEAdkVqsV1oi8bn11c0 ww5tMVpdq0amyFbMcTsyJD5IASJFTW/IXPNkM4d2qfnIdtDCKjhok/rtOfBhSSCPs5djSU MFz9IFqcYSIEQWVSe2mrnBv3BHbt32U= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727798718; a=rsa-sha256; cv=none; b=IPx9UBsBNNW4C13x/+MBp0HrnUJrRYogoQgUBKY65eaRXOZO9T3RlrW3G89oFuzoGSZ7YG PRtfGViZGVSrHh/0PLwCUSLFbTNmCPfFeUXlCyd6hWA4MgvcttE11faZg8ohwr2RA+1q1B MzPE2TDZV/gZ1z07x96ds3xq5gg/n/Y= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=wRcQ81tb; dmarc=none; spf=pass (imf06.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-20b49ee353cso40489315ad.2 for <linux-mm@kvack.org>; Tue, 01 Oct 2024 09:07:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1727798858; x=1728403658; 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=wRcQ81tbXAGCMa5sV1qBEjO2/pYKpy4zsDSnHIRRqP8mRBTxKB4cbcpsu6/QMlFney JKL7oUt0IZFoMAi4NM+1rbKjl6wfa0W7TJpeUt3WNzk7pZBqesuAmcgI42xmlxloO7jm domcjeJCb31chroJiHyT9cV20pt5v3sdy+tr1o/p1S/Q4bJbRclrdyMk4HbHxhmJtCfo eZ3ktkFROpEqAcQHQn5v1VjtozWMUsNt74eyY/XQ4jCPAsidpmlt3QE1IzzyDls7PqZW W9SVM3xl2VNeio/yrUiMd6Cl8zChLEIJu8ACAkHEgun2pCK7EnhQBtJHuE0fXNWu++uH ZLXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727798858; x=1728403658; 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=QxL94XDuTlPaYrBWUO53R+Z6Rs/4GIWMwI9zvqko69IgZSWAbyYH5fkmxoBDqsNCuS BJKEhEyA0YItVwHyP4jXdiFHMFEBeCeRtt3xIjn/tHV4qiW9T9w286BtDV6bIZpDuHcs mTJKj0FaYPDYyGVzq0KWS5SFcyj6+GUpcEV7TxziODV2A3m/QsO1Jq6jhDHyFVZ88ugz ondv4e/DwLNmmmbymMdGUDwc7eGJEcEjKEQk9GWOO/xiylWGYTUW0lPZ3U1/kH/P/4Y5 VWNl6hF5wuIoBUD0wBtmuTT8ChFC6jutF7dWkRmmKxC5fTMThNeqfyae1dRLa18JCRzX q8aQ== X-Forwarded-Encrypted: i=1; AJvYcCXeQO3U7+1a/Iest3iPCl8ZJ/hD73CmsGIpDyYZHPpzr1PzG95YV2Z2noJ1TFQ4Weq1gcpj6QfBww==@kvack.org X-Gm-Message-State: AOJu0YzzhF1FwesfCOWQqCJJUVyb/xjoqAsr/u9ifYAbKI/R9UI5rP/Q knV00ilATSdHIARWX0jSF1DAZS7jylDmhgfeXjpBE0lljU50n91yXRwx8brfXw4= X-Google-Smtp-Source: AGHT+IFru0DhFvQoGs6LhOQ3kHAjWn92S78/sQgjmR4BKwvX6aS8UH3SB8TB1kja/SuVDdI6S6eWBw== X-Received: by 2002:a17:90a:a886:b0:2e0:adbd:78ec with SMTP id 98e67ed59e1d1-2e1849d9954mr193359a91.39.1727798857996; Tue, 01 Oct 2024 09:07:37 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e06e1d7d47sm13843973a91.28.2024.10.01.09.07.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Oct 2024 09:07:37 -0700 (PDT) From: Deepak Gupta <debug@rivosinc.com> Date: Tue, 01 Oct 2024 09:06:19 -0700 Subject: [PATCH 14/33] riscv mmu: write protect and shadow stack MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20241001-v5_user_cfi_series-v1-14-3ba65b6e550f@rivosinc.com> References: <20241001-v5_user_cfi_series-v1-0-3ba65b6e550f@rivosinc.com> In-Reply-To: <20241001-v5_user_cfi_series-v1-0-3ba65b6e550f@rivosinc.com> To: Thomas Gleixner <tglx@linutronix.de>, Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>, Dave Hansen <dave.hansen@linux.intel.com>, x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>, Andrew Morton <akpm@linux-foundation.org>, "Liam R. Howlett" <Liam.Howlett@oracle.com>, Vlastimil Babka <vbabka@suse.cz>, Lorenzo Stoakes <lorenzo.stoakes@oracle.com>, Paul Walmsley <paul.walmsley@sifive.com>, Palmer Dabbelt <palmer@dabbelt.com>, Albert Ou <aou@eecs.berkeley.edu>, Conor Dooley <conor@kernel.org>, Rob Herring <robh@kernel.org>, Krzysztof Kozlowski <krzk+dt@kernel.org>, Arnd Bergmann <arnd@arndb.de>, Christian Brauner <brauner@kernel.org>, Peter Zijlstra <peterz@infradead.org>, Oleg Nesterov <oleg@redhat.com>, Eric Biederman <ebiederm@xmission.com>, Kees Cook <kees@kernel.org>, Jonathan Corbet <corbet@lwn.net>, Shuah Khan <shuah@kernel.org> 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 <debug@rivosinc.com> X-Mailer: b4 0.14.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 36BED18000F X-Stat-Signature: ozkx8pq18bpwan3ejoyr7ozeda1544p7 X-Rspam-User: X-HE-Tag: 1727798859-474189 X-HE-Meta: U2FsdGVkX1/fAg7aOcuWIuGGovRYsagfMSq9JPISu/FFBMz2otdckWGXJYGuHh0ufS2n09jDfyGfHdq1DSr/Cv7rYFknZBjP2sH/0If4nd9+EZ/qcn/jhbQXfSed3ZifG4d15kiqGUsr7Zu0ES/oZZblll4wESKCqpsa7d4qNlNVfDsXdvFW2Z4Tms3V5FJvWbTUkmeMZrnjy8zL/GRGO+3Mu9JV5nFJuumXsxNfNVlWMOXAEouh9Vr6ryNbSv6vVP+qRdZV39w9JT4N78PGHxvcdo8oHEITXzy9Bme7whT11N0BlX3R537l0RYaBfGbYR3AHwfEvkO3XhYoamzIIFT4q2w/Tepw5OALFKr6ehxImXVavcYTea1i3Ah3yi+ryoLJtD0Z0Kz+kstVuMz300Scm1A5AadQtIs81eTaCYmstOmE0Kra6/ByePthxBF7fC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YMvRiHsn8qtwoDf+C9nJxVEeABgyuCU2vvU= 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: <linux-mm.kvack.org> List-Subscribe: <mailto:majordomo@kvack.org> List-Unsubscribe: <mailto:majordomo@kvack.org> |
Series |
riscv control-flow integrity for usermode
|
expand
|
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
`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 <debug@rivosinc.com> Alexandre Ghiti <alexghiti@rivosinc.com> --- arch/riscv/include/asm/pgtable.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)