Message ID | 20241008-v5_user_cfi_series-v6-14-60d9fe073f37@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 BC833CF042B for <linux-mm@archiver.kernel.org>; Tue, 8 Oct 2024 22:38:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7B9C16B00AB; Tue, 8 Oct 2024 18:38:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 743A46B00AC; Tue, 8 Oct 2024 18:38:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5B8EF6B00AD; Tue, 8 Oct 2024 18:38:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 35DB96B00AB for <linux-mm@kvack.org>; Tue, 8 Oct 2024 18:38:30 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id E1CF6A0CA3 for <linux-mm@kvack.org>; Tue, 8 Oct 2024 22:38:26 +0000 (UTC) X-FDA: 82651900338.08.3E3B707 Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) by imf06.hostedemail.com (Postfix) with ESMTP id EDD4D18000A for <linux-mm@kvack.org>; Tue, 8 Oct 2024 22:38:27 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=YInKPItM; dmarc=none; spf=pass (imf06.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.174 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728426998; a=rsa-sha256; cv=none; b=Ei0JSZOeeLBqKYTkjUyGFrcbWZeiYqlSugGC6B4/2MW9sNQF+rxJveuU/K/8XO3j4et/Yr WcN/bG3rXOigfi6DCcBhbjsvmf9U5M+wxCwv8h7XBWMIrhTIWMzlXWmli5k0q3Zs47uO8j gyGK62eBu+Qm+HPquO3/7+d5C1DIrQg= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=YInKPItM; dmarc=none; spf=pass (imf06.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.174 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=1728426998; 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=dshDpmtrrJCSoHThKKW8S9emrK2dpYEGbp3GrIevPubeT5Dsbdy1bV64Az2MwEfAc7ZKXB MdaGcw/Um+Ctycl5lp2et4b79gtGe8P4UsntmIOublaqbzox6UXVkfHDh4J60cGEBviEIB ABilh0jJhfFO0oHNUUoD7cNWg0KE05c= Received: by mail-pg1-f174.google.com with SMTP id 41be03b00d2f7-7ea07610762so1959689a12.0 for <linux-mm@kvack.org>; Tue, 08 Oct 2024 15:38:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427107; x=1729031907; 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=YInKPItMMbSw+lsGxXnlguoF+ZpDV0KyyYBwDI+D11krF9w81tt+N1uiv6QNBJ1gu8 OI9vjMR9luaH0gP8fKPFG5frh+lf29xtN0kPlUEl/9apbPgd3iKrXkOwblSKO7DFSoGy xXdl1FOheTa0LnK2PckOeE/LRCoCxhEvqfok+xGe1giUheKwXNbb7F4WkpwfUajVvlUJ FfWH/GEypZt+JZn3MzhaJzu54/4fU+RzNkK49rMrLkFZ7icPzpwCCf97MD6LgCqO9Omz sdOIxe5/+8pgYUMAm9Ju+PtpIXjNAfXpR03UI9Yj7ldmJaWd14fV1/X/Plny8/2UQeGD kztA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427107; x=1729031907; 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=s4ecDSXgjFtYYkVkBarMYDTx06QdUKJ2mY9NMciQ2+4dDsucF6DCrZETUMZx70fYvM 9DW7x5G/q6wb26CyosJT6OePYsSYsp9NQonyndYtLdHx/FkcrkWXNXfwjA8C6fYubEH8 6/04Fnu/nSIy1PnK1GBpOIPa2TkQgzRtyiKvkHgX0C4r8QRwwJD6ScnDJ6NgnxsXZ/mo G9d8g9+NnBFwI2n4QdGbMlwZ/mElBliFpkh0/5tACCBNFcCUXaoUoRWRtkEfN0NtZWoW Hzghz7kMOg2tqe6P5qcydTkf8c0pweRijz5LNgx2tXzZ1PRPmLjyq5oCniImAOKPHqeK jv/w== X-Forwarded-Encrypted: i=1; AJvYcCWAfv1xKsNvbCWX4o848I5yqLmoW4U3ey30vJzBMp/XHv4xaNdfV+SZXSK808y73fmHSgumaBnHQQ==@kvack.org X-Gm-Message-State: AOJu0YznqCIifNJd0/N1gQfjYpY7UuxFDMJoCvKyBg027H45fvCvxCCA PNWHQknHKXBUsF9bXVBsWUwlFEsSwSvfLHrUNUbrRHcDIWeFUDSHgnFqjlUTQEE= X-Google-Smtp-Source: AGHT+IHWWiD7Qfl6QcTOp/Fh8ZvI7bj5RBVwxNM0ruqC/BqZklsNYndBJ5uZwxRV/4BsJaJcvUdg6w== X-Received: by 2002:a05:6a21:3983:b0:1cf:23cb:b927 with SMTP id adf61e73a8af0-1d8a3c5656dmr945264637.34.1728427106662; Tue, 08 Oct 2024 15:38:26 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.38.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:38:26 -0700 (PDT) From: Deepak Gupta <debug@rivosinc.com> Date: Tue, 08 Oct 2024 15:36:56 -0700 Subject: [PATCH v6 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: <20241008-v5_user_cfi_series-v6-14-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@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: rspam12 X-Rspamd-Queue-Id: EDD4D18000A X-Stat-Signature: 33j5r4cnsxkm5cybapdgqnijxzzfwfrn X-Rspam-User: X-HE-Tag: 1728427107-938218 X-HE-Meta: U2FsdGVkX19jYVoXX0EkYsgErEXvaHmIgTaY/EUsiIVVKnUImJ+smrFBprtvooblFqaZ27fZ3udcBiyt1XxVny8Bm/4MoB10g/bqyWBrsC1pPaCVxP8L/3CAuP5/ghiiSNMlnVfDYjzLN4W/pvBZiKIevP7+Qee+1B9y8nDXePUrAySvrtXZgkEcFXcOXZUvZIE1cldxePeesIv0LGJIwICWtmYAMOrlxrNDRrRZIWzPiVxK4zTisQv3qYS5hb5Na3TTEBrI63TGBRkaIYXrTZ2sP1Qy6cvJnDFAzxg3cPNyj0kBiEYxsRkubAIejd2YlbU0bNVyfqvYOiZi36GTTGYbWKAaS9N5H2eraOofr5Q/n56Jv+YQesfTmA1eRK8qPvErJMmPBLiM6+q36S8CjWHcSqkAgrvgA3HcqJRrfpSEkK6Z1ZKuZSTl0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Dmr4AkAOtFEVIvqWs7vzjuJchWpufgPg9Rlgzp9s8ubUtnr5mVfR3U1/cd10I4kLkDfBvflUw= 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(-)