Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=0.3 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,HTML_MESSAGE,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 16A2DC4320A for ; Tue, 24 Aug 2021 05:32:50 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8E1EA61212 for ; Tue, 24 Aug 2021 05:32:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 8E1EA61212 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id ED8086B006C; Tue, 24 Aug 2021 01:32:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E61206B0071; Tue, 24 Aug 2021 01:32:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D01E36B0072; Tue, 24 Aug 2021 01:32:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0207.hostedemail.com [216.40.44.207]) by kanga.kvack.org (Postfix) with ESMTP id AEBE46B006C for ; Tue, 24 Aug 2021 01:32:48 -0400 (EDT) Received: from smtpin24.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 4598718F50 for ; Tue, 24 Aug 2021 05:32:48 +0000 (UTC) X-FDA: 78508854816.24.9A0D638 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by imf23.hostedemail.com (Postfix) with ESMTP id 02E1B90000B5 for ; Tue, 24 Aug 2021 05:32:47 +0000 (UTC) Received: by mail-pf1-f179.google.com with SMTP id g14so17323592pfm.1 for ; Mon, 23 Aug 2021 22:32:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=sNUkuBNzjh6mHgtxuLPtXSa2hqB56or14q/VbLvxOaY=; b=od1eK84YFCF7DHx7NZdPEC6GP7aC6vLcO6xdX8CRYWvFokk+llCCWiGk9dEsrIzrP/ OYEOMIH+l4EUHacDErHNo9VwoHwpGI+hR6/maHQib6atOG0SUtQ/GQ8eg3BKWrEerkFp hURfH6+4EYpOnjdtVoWAJvlVKWLCWyLZd4yODuR5o2Iv6B/RvI/qc2eYPQcsPSRa0I7M UfnNhBqz+lkAZ6bJsCRH6jI4WqMF43TvMqSoe3SohugNLBrNC30yf377tDnxqHYvhSWM BLcYIXAnEwdc5jfEqAod32ENeknjRJivnXoo8eOjEsZ4eA5li/BUh9kkhUtKKAext1i0 IHpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=sNUkuBNzjh6mHgtxuLPtXSa2hqB56or14q/VbLvxOaY=; b=jWrxeE3ehRmWNdBYJnXwaB2OcfjoYGnkaXKj1vgavxIG6j8ip1SY9CLt2R2UYtjDBF qjoQW1Auc6CYywFeMXNlLPQp3ZTIbxTs9EpojiGqHWHiCBeMXXVPFt9ZpUAvVceLBZDH vuAd6Wq00ZBu6Mh9kE/eudAylZpwqMvf414C5YzZZP1wxxs6QGbhxyWWJUKbxkAP1zW5 Pi7B/uFnmr6OBzb20KP6BePpZd7oZAElcLisvyII4AIlqZ66KtZk+ULok6Ht8Jb0NtaN usZHDmFrwI9NUSrkPTQLJDz7PsKVBdoazg4oQoBNp5L+3Z58Z6IMS/46H4ZDbIpQaGsq Nw3w== X-Gm-Message-State: AOAM531+Sa8pGOShQRjjfTyV/sFZ2NhQnEe82/EcdHpavAQ83BE7P4XS uP/mACNMEGognxKrRFuPH+c= X-Google-Smtp-Source: ABdhPJyx/908bsB+MVgIm2OI6Tij+5UwryPv4IiZ73IJqiM/cySmqk5MBt3YBBdROXYlFpyNm+GJuQ== X-Received: by 2002:a62:a20d:0:b029:35b:73da:dc8d with SMTP id m13-20020a62a20d0000b029035b73dadc8dmr37782211pff.54.1629783166744; Mon, 23 Aug 2021 22:32:46 -0700 (PDT) Received: from smtpclient.apple (c-24-6-216-183.hsd1.ca.comcast.net. [24.6.216.183]) by smtp.gmail.com with ESMTPSA id d15sm18080584pfd.115.2021.08.23.22.32.45 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 23 Aug 2021 22:32:46 -0700 (PDT) From: Nadav Amit Message-Id: Content-Type: multipart/alternative; boundary="Apple-Mail=_958B947E-7059-4857-BFDF-80C74FCED478" Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.120.0.1.13\)) Subject: Re: [RFC PATCH 4/4] x86/mm: write protect (most) page tables Date: Mon, 23 Aug 2021 22:32:44 -0700 In-Reply-To: <20210823132513.15836-5-rppt@kernel.org> Cc: Linux-MM , Andrew Morton , Andy Lutomirski , Dave Hansen , Ira Weiny , Kees Cook , Mike Rapoport , Peter Zijlstra , Rick Edgecombe , Vlastimil Babka , x86@kernel.org, linux-kernel@vger.kernel.org To: Mike Rapoport References: <20210823132513.15836-1-rppt@kernel.org> <20210823132513.15836-5-rppt@kernel.org> X-Mailer: Apple Mail (2.3654.120.0.1.13) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=gmail.com header.s=20161025 header.b=od1eK84Y; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf23.hostedemail.com: domain of nadavamit@gmail.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=nadavamit@gmail.com X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 02E1B90000B5 X-Stat-Signature: gdntqyeerkbbm5is7s38eim6p1zdmcsk X-HE-Tag: 1629783167-682931 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: --Apple-Mail=_958B947E-7059-4857-BFDF-80C74FCED478 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=us-ascii > On Aug 23, 2021, at 6:25 AM, Mike Rapoport wrote: > > From: Mike Rapoport > > Allocate page table using __GFP_PTE_MAPPED so that they will have 4K PTEs > in the direct map. This allows to switch _PAGE_RW bit each time a page > table page needs to be made writable or read-only. > > The writability of the page tables is toggled only in the lowest level page > table modifiction functions and immediately switched off. > > The page tables created early in the boot (including the direct map page > table) are not write protected. > > [ snip ] > +static void pgtable_write_set(void *pg_table, bool set) > +{ > + int level = 0; > + pte_t *pte; > + > + /* > + * Skip the page tables allocated from pgt_buf break area and from > + * memblock > + */ > + if (!after_bootmem) > + return; > + if (!PageTable(virt_to_page(pg_table))) > + return; > + > + pte = lookup_address((unsigned long)pg_table, &level); > + if (!pte || level != PG_LEVEL_4K) > + return; > + > + if (set) { > + if (pte_write(*pte)) > + return; > + > + WRITE_ONCE(*pte, pte_mkwrite(*pte)); I think that the pte_write() test (and the following one) might hide latent bugs. Either you know whether the PTE is write-protected or you need to protect against nested/concurrent calls to pgtable_write_set() by disabling preemption/IRQs. Otherwise, you risk in having someone else write-protecting the PTE after it is write-unprotected and before it is written - causing a crash, or write-unprotecting it after it is protected - which circumvents the protection. Therefore, I would think that instead you should have: VM_BUG_ON(pte_write(*pte)); // (or WARN_ON_ONCE()) In addition, if there are assumptions on the preemptability of the code, it would be nice to have some assertions. I think that the code assumes that all calls to pgtable_write_set() are done while holding the page-table lock. If that is the case, perhaps adding some lockdep assertion would also help to confirm the correctness. [ I put aside the lack of TLB flushes, which make the whole matter of delivered protection questionable. I presume that once PKS is used, this is not an issue. ] --Apple-Mail=_958B947E-7059-4857-BFDF-80C74FCED478 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=us-ascii
On Aug 23, 2021, at 6:25 AM, Mike Rapoport <rppt@kernel.org> = wrote:

From: Mike Rapoport <rppt@linux.ibm.com>

Allocate page table using __GFP_PTE_MAPPED so that they will = have 4K PTEs
in the direct map. This allows to switch = _PAGE_RW bit each time a page
table page needs to be made = writable or read-only.

The writability of = the page tables is toggled only in the lowest level page
table modifiction functions and immediately switched off.

The page tables created early in the boot = (including the direct map page
table) are not write = protected.



[ snip ]

+static void = pgtable_write_set(void *pg_table, bool set)
+{
+ = int level =3D 0;
+ pte_t *pte;
+
+ = /*
+  * Skip the page tables = allocated from pgt_buf break area and from
+  * = memblock
+  */
+ if = (!after_bootmem)
+ return;
+ if = (!PageTable(virt_to_page(pg_table)))
+ = return;
+
+ pte =3D = lookup_address((unsigned long)pg_table, &level);
+ if (!pte = || level !=3D PG_LEVEL_4K)
+ return;
+
+ = if (set) {
+ if (pte_write(*pte))
+ = = = return;
+
+ = WRITE_ONCE(*pte, pte_mkwrite(*pte));

I think that the pte_write() test (and the following one) = might hide
latent bugs. Either you know whether the = PTE is write-protected or you
need to protect = against nested/concurrent calls to pgtable_write_set()
by disabling preemption/IRQs.

Otherwise, you risk in having someone = else write-protecting the PTE
after it is = write-unprotected and before it is written - causing a crash,
or write-unprotecting it after it is protected - which = circumvents the
protection.

Therefore, I would think that instead = you should have:

= VM_BUG_ON(pte_write(*pte));  // (or = WARN_ON_ONCE())

In addition, if there are assumptions on the preemptability = of the code,
it would be nice to have some = assertions. I think that the code assumes
that all = calls to pgtable_write_set() are done while holding the
page-table lock. If that is the case, perhaps adding some = lockdep
assertion would also help to confirm the = correctness.

[ = I put aside the lack of TLB flushes, which make the whole matter = of
delivered protection questionable. I presume = that once PKS is used, 
this is not an issue. = ]

= --Apple-Mail=_958B947E-7059-4857-BFDF-80C74FCED478--