Xen Security Advisory 240 (CVE-2017-15595) - Unlimited recursion in linear pagetable de-typing
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256
Xen Security Advisory CVE-2017-15595 / XSA-240
version 6
Unlimited recursion in linear pagetable de-typing
UPDATES IN VERSION 6
====================
Yet another new patch, addressing another issue similar to the one addressed in v5.
ISSUE DESCRIPTION
=================
x86 PV guests are permitted to set up certain forms of what is often called "linear page tables", where pagetables contain references to other pagetables at the same level or higher. Certain restrictions apply in order to fit into Xen's page type handling system. An important restriction was missed, however: Stacking multiple layers of page tables of the same level on top of one another is not very useful, and the tearing down of such an arrangement involves recursion. With sufficiently many layers such recursion will result in a stack overflow, commonly resulting in Xen to crash.
IMPACT
======
A malicious or buggy PV guest may cause the hypervisor to crash, resulting in Denial of Service (DoS) affecting the entire host. Privilege escalation and information leaks cannot be excluded.
VULNERABLE SYSTEMS
==================
All Xen versions from at least 3.2 onwards are vulnerable. Earlier versions have not been checked.
Only x86 systems are affected. ARM systems are not affected.
Only x86 PV guests can leverage the vulnerability. x86 HVM guests cannot leverage the vulnerability.
MITIGATION
==========
Running only HVM guests will avoid this vulnerability.
For PV guests, the vulnerability can be avoided if the guest kernel is controlled by the host rather than guest administrator, provided that further steps are taken to prevent the guest administrator from loading code into the kernel (e.g. by disabling loadable modules etc) or from using other mechanisms which allow them to run code at kernel privilege.
CREDITS
=======
This issue was discovered by Jann Horn of Google Project Zero.
RESOLUTION
==========
Applying the appropriate attached patch series resolves this issue. The first patch fixes this known issue. The second, optional, patch in each series disables the linear pagetable option by default. It can be re-enabled by adding "pv-linear-pt=true' on the Xen command-line.
Note that neither Linux nor MiniOS use linear pagetables; NetBSD and Novell Netware, however, do use this feature when running in PV mode.
xsa240-unstable/*.patch xen-unstable xsa240-4.9/*.patch Xen 4.9.x xsa240-4.8/*.patch Xen 4.8.x xsa240-4.7/*.patch Xen 4.7.x xsa240-4.6/*.patch Xen 4.6.x xsa240-4.5/*.patch Xen 4.5.x
$ sha256sum xsa240* xsa240*/* e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 xsa240.meta e227126ea204d6049c8bfb694e3d2bcac10382733ca4ece1f0f7dc36c625ac1d xsa240-unstable/0001-x86-limit-linear-page-table-use-to-a-single-level.patch c6bb1f47754bf4f57bb9205509c525336ff55da8bcae8ae95415bdf1bba70209 xsa240-unstable/0002-x86-mm-Disable-PV-linear-pagetables-by-default.patch 549f56c7a3eacd1fc308a46ac5db32f2191d67e7e25aff501fb2ba26cc979feb xsa240-unstable/0003-x86-dont-wrongly-trigger-linear-page-table-assertion.patch d157826cbbae2f8815ccfb3aa23b936c5f7e7f5fb802cb85ae9e873723ac3d79 xsa240-unstable/0004-x86-dont-wrongly-trigger-linear-page-table-assertion-2.patch 2e8168449f8594c5faa50feb311eaa7e74b38c726853b1cfbaf2fbd4cbbdb476 xsa240-4.5/0001-x86-limit-linear-page-table-use-to-a-single-level.patch ea95ed56a5f85753a41ca1abbb4abd5ea791d7596c8a9d20ceaa53862ee5ce15 xsa240-4.5/0002-x86-mm-Disable-PV-linear-pagetables-by-default.patch 76ba9206fbab686cc8c3b8b21945532b7029cc4725ee1cc38666035152ba87ad xsa240-4.5/0003-x86-dont-wrongly-trigger-linear-page-table-assertion.patch c0851264065f22ea60d54df7a23cda68b24762c032d881bd3d77c5f5f7f65dad xsa240-4.5/0004-x86-dont-wrongly-trigger-linear-page-table-assertion-2.patch f7556cc4a13cd4334d79e73861f15dd619dfd6041a3ad89865b358035d2fcff8 xsa240-4.6/0001-x86-limit-linear-page-table-use-to-a-single-level.patch 24ad9f92938c112a6fcf46177d44597c49dc213a3644ce4fd59d4df507b072d3 xsa240-4.6/0002-x86-mm-Disable-PV-linear-pagetables-by-default.patch 2fe49415a8925dda416fd2594f935ec03c8070bea4a049c1ad242c646fef01e1 xsa240-4.6/0003-x86-dont-wrongly-trigger-linear-page-table-assertion.patch 0c78bddbf12cefe9e04001ca31c5b913b3914eb80b63b27d1918a10a24022e74 xsa240-4.6/0004-x86-dont-wrongly-trigger-linear-page-table-assertion-2.patch 5a9b8e0a4e55482826e6cf820ea01fbf6674fecf19b101f4578396e3fa98a8fd xsa240-4.7/0001-x86-limit-linear-page-table-use-to-a-single-level.patch acf9744b853c1c6ac071bcf5776d8d9463781bbdf658c2fecc59ee338c094750 xsa240-4.7/0002-x86-mm-Disable-PV-linear-pagetables-by-default.patch 637ef53ca2d6633104abff47be208d87836042af79082666cb818f0ad53b6452 xsa240-4.7/0003-x86-dont-wrongly-trigger-linear-page-table-assertion.patch 7acccf8867d779914e77a83efcb0dbd172e572de8f700b9ac18db1194c37f82f xsa240-4.7/0004-x86-dont-wrongly-trigger-linear-page-table-assertion-2.patch c0c624f51fb1bd9e31a2c120343164d545ab6e709ed4bb9e5dd89b5c4c4e49f8 xsa240-4.8/0001-x86-limit-linear-page-table-use-to-a-single-level.patch 7e3c2c4a4d0ee0a29abaa7aceffbb774b1f92aa81dfa4c5d1c5c5156b6bb0a3a xsa240-4.8/0002-x86-mm-Disable-PV-linear-pagetables-by-default.patch f174ee608a7f7f0601ae46edafbf443c90a87632609c1b4145fb0e2e5c4b1b51 xsa240-4.8/0003-x86-dont-wrongly-trigger-linear-page-table-assertion.patch eb74c580bdf0f972833ef058e7db50fe5f524d38d886776c66942712597a52c4 xsa240-4.8/0004-x86-dont-wrongly-trigger-linear-page-table-assertion-2.patch 7769866805e47b7a2109ba1778f263ddfa126bcb0b116612f76dfaca9bf70995 xsa240-4.9/0001-x86-limit-linear-page-table-use-to-a-single-level.patch 1053cb348dc8536cb12b4aca556ddc4c015810f8f0fd21a80dff37433ec082ea xsa240-4.9/0002-x86-mm-Disable-PV-linear-pagetables-by-default.patch 4b312010962a294660b2e78a9a5bfd9d537000ceda11e587f88fc49da560c0ff xsa240-4.9/0003-x86-dont-wrongly-trigger-linear-page-table-assertion.patch 9b79fb9b176e34b6f1c1208713537130c7ffe7e73e7b9a80e95b53209587628a xsa240-4.9/0004-x86-dont-wrongly-trigger-linear-page-table-assertion-2.patch $
DEPLOYMENT DURING EMBARGO
=========================
Deployment of the patches and/or mitigations described above (or others which are substantially similar) is permitted during the embargo, even on public-facing systems with untrusted guest users and administrators.
But: Distribution of updated software is prohibited (except to other members of the predisclosure list).
Predisclosure list members who wish to deploy significantly different patches and/or mitigations, please contact the Xen Project Security Team.
(Note: this during-embargo deployment notice is retained in post-embargo publicly released Xen Project advisories, even though it is then no longer applicable. This is to enable the community to have oversight of the Xen Project Security Team's decisionmaking.)
For more information about permissible uses of embargoed information, consult the Xen Project community's agreed Security Policy:
http://www.xenproject.org/security-policy.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
iQEcBAEBCAAGBQJaLstaAAoJEIP+FMlX6CvZxdMIAIzMc0f+pU0bvefVCMuE7LBN Xx3bSZ1Yd1V1c9/RmQMsxgv6BQ0hgoiAs9tP0PQ89UJ7CfJfWb221DMXuThhKSmo 1zxH4D0gyUzM01JxaCGjFAUAd30Aa9tZE9M5rgTVSyt3QiLL7IcjY6fks3+HBjMq 2phYB5p7vP9HRZbDbEnAEZRtUzMUUo8QlnjGYvc14wkr/e6quGNsWMKje82y42ha 5mNQ+4XfFI+PPBjAGV12aEHmwmx9PDPnrY59y2rkFoxGIZDjIRLjKS97ULlqq4WU UbpNgAgrIr2ntfLKQvXKp0oGv+IvxBAMLGzxLWZ0VwKjp6tSUjohnVjq0Twb/74= =luoB
-----END PGP SIGNATURE-----
From 284135d10dc42543997a4e3681ef4857b5718555 Mon Sep 17 00:00:00 2001
From: Jan Beulich <[email protected]>
Date: Wed, 27 Sep 2017 11:46:52 +0100
Subject: [PATCH 1/2] x86: limit linear page table use to a single level
That's the only way that they're meant to be used. Without such a restriction arbitrarily long chains of same-level page tables can be built, tearing down of which may then cause arbitrarily deep recursion, causing a stack overflow. To facilitate this restriction, a counter is being introduced to track both the number of same-level entries in a page table as well as the number of uses of a page table in another same-level one (counting into positive and negative direction respectively, utilizing the fact that both counts can't be non-zero at the same time).
Note that the added accounting introduces a restriction on the number of times a page can be used in other same-level page tables - more than 32k of such uses are no longer possible.
Note also that some put_page_and_type[_preemptible]() calls are replaced with open-coded equivalents. This seemed preferrable to adding "parent_table" to the matrix of functions.
Note further that cross-domain same-level page table references are no longer permitted (they probably never should have been).
This is XSA-240.
---
v5: Correct placement of code addition to _put_final_page_type().
v4: Introduce and use PGT_type_equal().
v3: Add BUILD_BUG_ON()s.
v2: Fix assertions in dec_linear_*() triggering during guest shutdown
when same-level page table references still exist. Disallow cross- domain same-level page table references.
--- xen/arch/x86/domain.c | 1 + xen/arch/x86/mm.c | 172 +++++++++++++++++++++++++++++++++++++++---- xen/include/asm-x86/domain.h | 2 + xen/include/asm-x86/mm.h | 25 +++++-- 4 files changed, 177 insertions(+), 23 deletions(-)
xen/arch/x86/domain.c +1 -0
@@ -1091,6 +1091,7 @@ int arch_set_info_guest( rc = -ERESTART ;/* Fallthrough */ case -ERESTART : + v->arch.old_guest_ptpg =NULL ; v->arch.old_guest_table =pagetable_get_page (v->arch.guest_table); v->arch.guest_table =pagetable_null ();
xen/arch/x86/mm.c +156 -16
350 lines not on this page · raw message
xen/include/asm-x86/domain.h +2 -0
@@ -531,6 +531,8 @@ struct arch_vcpupagetable_t guest_table_user;/* (MFN) x86/64 user-space pagetable */ pagetable_t guest_table;/* (MFN) guest notion of cr3 */ struct page_info *old_guest_table;/* partially destructed pagetable */ +struct page_info *old_guest_ptpg;/* containing page table of the */ +/* former, if any */ /* guest_table holds a ref to the page, and also a type-count unless * shadow refcounts are in use */ pagetable_t shadow_table[4 ];/* (MFN) shadow(s) of guest */
xen/include/asm-x86/mm.h +18 -7
@@ -141,11 +141,11 @@ struct page_infou32 tlbflush_timestamp;/* -* When PGT_partial is true then this field is valid and indicates -* that PTEs in the range [0, @nr_validated_ptes) have been validated. -* An extra page reference must be acquired (or not dropped) whenever -* PGT_partial gets set, and it must be dropped when the flag gets -* cleared. This is so that a get() leaving a page in partially +* When PGT_partial is true then the first two fields are valid and +* indicate that PTEs in the range [0, @nr_validated_ptes) have been +* validated. An extra page reference must be acquired (or not dropped) +* whenever PGT_partial gets set, and it must be dropped when the flag +* gets cleared. This is so that a get() leaving a page in partially * validated state (where the caller would drop the reference acquired * due to the getting of the type [apparently] failing [-ERESTART]) * would not accidentally result in a page left with zero general
@@ -169,10 +169,18 @@ struct page_info* put_page_from_lNe() (due to the apparent failure), and hence it * must be dropped when the put operation is resumed (and completes), * but it must not be acquired if picking up the page for validation. +* +* The 3rd field, @linear_pt_count, indicates +* - by a positive value, how many same-level page table entries a page +* table has, +* - by a negative value, in how many same-level page tables a page is +* in use. */ struct { -u16 nr_validated_ptes; -s8 partial_pte; +u16 nr_validated_ptes:PAGETABLE_ORDER +1 ; + u16 :16 - PAGETABLE_ORDER - 1 - 2; +s16 partial_pte:2 ; +s16 linear_pt_count; };/*
@@ -223,6 +231,9 @@ struct page_info#define PGT_count_width PG_shift(8)#define PGT_count_mask ((1UL<<PGT_count_width)-1) +/* Are the 'type mask' bits identical? */ +#define PGT_type_equal (x, y) (!(((x) ^ (y)) & PGT_type_mask)) +/* Cleared when the owning guest 'frees' this page. */ #define _PGC_allocated PG_shift(1)#define PGC_allocated PG_mask(1, 1)
Signature · 1 line
2.14.1
From 90c648b4fd3aa4f85ca51dda693b16f28605860b Mon Sep 17 00:00:00 2001
From: George Dunlap <[email protected]>
Date: Fri, 22 Sep 2017 11:46:55 +0100
Subject: [PATCH 2/2] x86/mm: Disable PV linear pagetables by default
Allowing pagetables to point to other pagetables of the same level (often called 'linear pagetables') has been included in Xen since its inception. But it is not used by the most common PV guests (Linux, NetBSD, minios), and has been the source of a number of subtle reference-counting bugs.
Add a command-line option to control whether PV linear pagetables are allowed (disabled by default).
---
Changes since v2:
- s/_/-/; in command-line option
- Added __read_mostly
--- docs/misc/xen-command-line.markdown | 15 +++++++++++++++ xen/arch/x86/mm.c | 10 ++++++++++ 2 files changed, 25 insertions(+)
docs/misc/xen-command-line.markdown +15 -0
@@ -1422,6 +1422,21 @@ The following resources are available: CDP, one COS will corespond two CBMs other than one with CAT, due to the sum of CBMs is fixed, that means actual `cos_max` in use will automatically reduce to half when CDP is enabled. + +### pv-linear-pt +> `= <boolean>` + +> Default: `false` + +Allow PV guests to have pagetable entries pointing to other pagetables +of the same level (i.e., allowing L2 PTEs to point to other L2 pages). +This technique is often called "linear pagetables", and is sometimes +used to allow operating systems a simple way to consistently map the +current process's pagetables into its own virtual address space. + +None of the most common PV operating systems (Linux, NetBSD, MiniOS) +use this technique, but there may be custom operating systems which +do. ### rcu-idle-timer-period-ms > `= <integer>`
xen/arch/x86/mm.c +10 -0
@@ -654,6 +654,9 @@ static void dec_linear_uses(struct page_info *pg)* frame if it is mapped by a different root table. This is sufficient and * also necessary to allow validation of a root table mapping itself. */ +static bool __read_mostly pv_linear_pt_enable = false; +boolean_param ("pv-linear-pt" , pv_linear_pt_enable); +#define define_get_linear_pagetable (level) \ static int \ get_##level##_linear_pagetable( \
@@ -663,6 +666,13 @@ get_##level##_linear_pagetable( \struct page_info *page; \unsigned long pfn; \ \ +if ( !pv_linear_pt_enable ) \ + { \ +gdprintk (XENLOG_WARNING , \ +"Attempt to create linear p.t. (feature disabled)\n" ); \ +return 0 ; \ + } \ + \ if ( (level##e_get_flags(pde) & _PAGE_RW) ) \ { \ gdprintk(XENLOG_WARNING, \
Signature · 1 line
2.14.1
From: Jan Beulich <[email protected]>
Subject: x86: don't wrongly trigger linear page table assertion
_put_page_type() may do multiple iterations until its cmpxchg() succeeds. It invokes set_tlbflush_timestamp() on the first iteration, however. Code inside the function takes care of this, but
- the assertion in _put_final_page_type() would trigger on the second iteration if time stamps in a debug build are permitted to be sufficiently much wider than the default 6 bits (see WRAP_MASK in flushtlb.c),
- it returning -EINTR (for a continuation to be scheduled) would leave the page inconsistent state (until the re-invocation completes).
Make the set_tlbflush_timestamp() invocation conditional, bypassing it (for now) only in the case we really can't tolerate the stamp to be stored.
This is part of XSA-240.
---
v2: Re-do from scratch.
TBD: The conditional used was chosen to be as little of a change in
behavior as possible. Since what the changed comment says applies to all page tables, the condition could be further relaxed such that only non-page-table pages would actually have their time stamp set here.
xen/arch/x86/mm.c +8 -15
@@ -2477,26 +2477,19 @@ static int _put_page_type(struct page_inbreak ; } -#ifdef CONFIG_PV_LINEAR_PT -if ( ptpg &&PGT_type_equal (x, ptpg->u.inuse.type_info) ) +if ( !ptpg || !PGT_type_equal (x, ptpg->u.inuse.type_info) ) {/* * set_tlbflush_timestamp() accesses the same union -* linear_pt_count lives in. Unvalidated page table pages, -* however, should occur during domain destruction only -* anyway. Updating of linear_pt_count luckily is not -* necessary anymore for a dying domain. +* linear_pt_count lives in. Pages (including page table ones), +* however, don't need their flush time stamp set except when +* the last reference is being dropped. For page table pages +* this happens in _put_final_page_type(). */ -ASSERT (page_get_owner (page)->is_dying); -ASSERT (page->linear_pt_count <0 ); -ASSERT (ptpg->linear_pt_count >0 ); - ptpg =NULL ; +set_tlbflush_timestamp (page); } -#else /* CONFIG_PV_LINEAR_PT */ -BUG_ON (ptpg &&PGT_type_equal (x, ptpg->u.inuse.type_info)); -#endif - -set_tlbflush_timestamp (page); +else +BUG_ON (!IS_ENABLED (CONFIG_PV_LINEAR_PT )); }else if (unlikely ((nx & (PGT_locked | PGT_count_mask)) == (PGT_locked |1 )) )
From: Jan Beulich <[email protected]>
Subject: x86: don't wrongly trigger linear page table assertion (2)
_put_final_page_type(), when free_page_type() has exited early to allow for preemption, should not update the time stamp, as the page continues to retain the typ which is in the process of being unvalidated. I can't see why the time stamp update was put on that path in the first place (albeit it may well have been me who had put it there years ago).
This is part of XSA-240.
xen/arch/x86/mm.c +0 -1
@@ -2436,7 +2436,6 @@ static int _put_final_page_type(struct p {ASSERT ((page->u.inuse.type_info & (PGT_count_mask|PGT_validated|PGT_partial)) ==1 ); -set_tlbflush_timestamp (page);smp_wmb (); page->u.inuse.type_info |= PGT_validated; }
From 78a763b94feed5f726e8dfe953fd84ef6bc1becb Mon Sep 17 00:00:00 2001
From: Jan Beulich <[email protected]>
Date: Thu, 28 Sep 2017 15:17:29 +0100
Subject: [PATCH 1/2] x86: limit linear page table use to a single level
That's the only way that they're meant to be used. Without such a restriction arbitrarily long chains of same-level page tables can be built, tearing down of which may then cause arbitrarily deep recursion, causing a stack overflow. To facilitate this restriction, a counter is being introduced to track both the number of same-level entries in a page table as well as the number of uses of a page table in another same-level one (counting into positive and negative direction respectively, utilizing the fact that both counts can't be non-zero at the same time).
Note that the added accounting introduces a restriction on the number of times a page can be used in other same-level page tables - more than 32k of such uses are no longer possible.
Note also that some put_page_and_type[_preemptible]() calls are replaced with open-coded equivalents. This seemed preferrable to adding "parent_table" to the matrix of functions.
Note further that cross-domain same-level page table references are no longer permitted (they probably never should have been).
This is XSA-240.
--- xen/arch/x86/domain.c | 1 + xen/arch/x86/mm.c | 171 ++++++++++++++++++++++++++++++++++++++----- xen/include/asm-x86/domain.h | 2 + xen/include/asm-x86/mm.h | 25 +++++-- xen/include/asm-x86/system.h | 46 ++++++++++++ 5 files changed, 221 insertions(+), 24 deletions(-)
xen/arch/x86/domain.c +1 -0
@@ -1031,6 +1031,7 @@ int arch_set_info_guest(case -EINTR : rc = -ERESTART ;case -ERESTART : + v->arch.old_guest_ptpg =NULL ; v->arch.old_guest_table =pagetable_get_page (v->arch.guest_table); v->arch.guest_table =pagetable_null ();
xen/arch/x86/mm.c +154 -17
351 lines not on this page · raw message
xen/include/asm-x86/domain.h +2 -0
@@ -453,6 +453,8 @@ struct arch_vcpupagetable_t guest_table_user;/* (MFN) x86/64 user-space pagetable */ pagetable_t guest_table;/* (MFN) guest notion of cr3 */ struct page_info *old_guest_table;/* partially destructed pagetable */ +struct page_info *old_guest_ptpg;/* containing page table of the */ +/* former, if any */ /* guest_table holds a ref to the page, and also a type-count unless * shadow refcounts are in use */ pagetable_t shadow_table[4 ];/* (MFN) shadow(s) of guest */
xen/include/asm-x86/mm.h +18 -7
@@ -119,11 +119,11 @@ struct page_infou32 tlbflush_timestamp;/* -* When PGT_partial is true then this field is valid and indicates -* that PTEs in the range [0, @nr_validated_ptes) have been validated. -* An extra page reference must be acquired (or not dropped) whenever -* PGT_partial gets set, and it must be dropped when the flag gets -* cleared. This is so that a get() leaving a page in partially +* When PGT_partial is true then the first two fields are valid and +* indicate that PTEs in the range [0, @nr_validated_ptes) have been +* validated. An extra page reference must be acquired (or not dropped) +* whenever PGT_partial gets set, and it must be dropped when the flag +* gets cleared. This is so that a get() leaving a page in partially * validated state (where the caller would drop the reference acquired * due to the getting of the type [apparently] failing [-ERESTART]) * would not accidentally result in a page left with zero general
@@ -147,10 +147,18 @@ struct page_info* put_page_from_lNe() (due to the apparent failure), and hence it * must be dropped when the put operation is resumed (and completes), * but it must not be acquired if picking up the page for validation. +* +* The 3rd field, @linear_pt_count, indicates +* - by a positive value, how many same-level page table entries a page +* table has, +* - by a negative value, in how many same-level page tables a page is +* in use. */ struct { -u16 nr_validated_ptes; -s8 partial_pte; +u16 nr_validated_ptes:PAGETABLE_ORDER +1 ; + u16 :16 - PAGETABLE_ORDER - 1 - 2; +s16 partial_pte:2 ; +s16 linear_pt_count; };/*
@@ -201,6 +209,9 @@ struct page_info#define PGT_count_width PG_shift(9)#define PGT_count_mask ((1UL<<PGT_count_width)-1) +/* Are the 'type mask' bits identical? */ +#define PGT_type_equal (x, y) (!(((x) ^ (y)) & PGT_type_mask)) +/* Cleared when the owning guest 'frees' this page. */ #define _PGC_allocated PG_shift(1)#define PGC_allocated PG_mask(1, 1)
xen/include/asm-x86/system.h +46 -0
52 lines not on this page · raw message
Signature · 1 line
2.14.1
From 7444f8a6c1a8df082c23a0c1acc542defcffc0ad Mon Sep 17 00:00:00 2001
From: George Dunlap <[email protected]>
Date: Fri, 22 Sep 2017 11:46:55 +0100
Subject: [PATCH 2/2] x86/mm: Disable PV linear pagetables by default
Allowing pagetables to point to other pagetables of the same level (often called 'linear pagetables') has been included in Xen since its inception. But it is not used by the most common PV guests (Linux, NetBSD, minios), and has been the source of a number of subtle reference-counting bugs.
Add a command-line option to control whether PV linear pagetables are allowed (disabled by default).
---
Changes since v2:
- s/_/-/; in command-line option
- Added __read_mostly
--- docs/misc/xen-command-line.markdown | 15 +++++++++++++++ xen/arch/x86/mm.c | 9 +++++++++ 2 files changed, 24 insertions(+)
docs/misc/xen-command-line.markdown +15 -0
@@ -1137,6 +1137,21 @@ The following resources are available: L3 cache occupancy. * `cmt` instructs Xen to enable/disable Cache Monitoring Technology. * `rmid_max` indicates the max value for rmid. + +### pv-linear-pt +> `= <boolean>` + +> Default: `false` + +Allow PV guests to have pagetable entries pointing to other pagetables +of the same level (i.e., allowing L2 PTEs to point to other L2 pages). +This technique is often called "linear pagetables", and is sometimes +used to allow operating systems a simple way to consistently map the +current process's pagetables into its own virtual address space. + +None of the most common PV operating systems (Linux, NetBSD, MiniOS) +use this technique, but there may be custom operating systems which +do. ### reboot > `= t[riple] | k[bd] | a[cpi] | p[ci] | e[fi] | n[o] [, [w]arm | [c]old]`
xen/arch/x86/mm.c +9 -0
@@ -734,6 +734,9 @@ static void dec_linear_uses(struct page_info *pg)* frame if it is mapped by a different root table. This is sufficient and * also necessary to allow validation of a root table mapping itself. */ +static bool_t __read_mostly pv_linear_pt_enable =0 ; +boolean_param ("pv-linear-pt" , pv_linear_pt_enable); +#define define_get_linear_pagetable (level) \ static int \ get_##level##_linear_pagetable( \
@@ -743,6 +746,12 @@ get_##level##_linear_pagetable( \struct page_info *page; \unsigned long pfn; \ \ +if ( !pv_linear_pt_enable ) \ + { \ +MEM_LOG ("Attempt to create linear p.t. (feature disabled)" ); \ +return 0 ; \ + } \ + \ if ( (level##e_get_flags(pde) & _PAGE_RW) ) \ { \MEM_LOG ("Attempt to create linear p.t. with write perms" ); \
Signature · 1 line
2.14.1
From: Jan Beulich <[email protected]>
Subject: x86: don't wrongly trigger linear page table assertion
_put_page_type() may do multiple iterations until its cmpxchg() succeeds. It invokes set_tlbflush_timestamp() on the first iteration, however. Code inside the function takes care of this, but
- the assertion in _put_final_page_type() would trigger on the second iteration if time stamps in a debug build are permitted to be sufficiently much wider than the default 6 bits (see WRAP_MASK in flushtlb.c),
- it returning -EINTR (for a continuation to be scheduled) would leave the page inconsistent state (until the re-invocation completes).
Make the set_tlbflush_timestamp() invocation conditional, bypassing it (for now) only in the case we really can't tolerate the stamp to be stored.
This is part of XSA-240.
xen/arch/x86/mm.c +7 -16
@@ -2446,29 +2446,20 @@ static int _put_page_type(struct page_inbreak ; } -if ( ptpg &&PGT_type_equal (x, ptpg->u.inuse.type_info) ) - { -/* -* set_tlbflush_timestamp() accesses the same union -* linear_pt_count lives in. Unvalidated page table pages, -* however, should occur during domain destruction only -* anyway. Updating of linear_pt_count luckily is not -* necessary anymore for a dying domain. -*/ -ASSERT (page_get_owner (page)->is_dying); -ASSERT (page->linear_pt_count <0 ); -ASSERT (ptpg->linear_pt_count >0 ); - ptpg =NULL ; - } -/* * Record TLB information for flush later. We do not stamp page * tables when running in shadow mode: * 1. Pointless, since it's the shadow pt's which must be tracked. * 2. Shadow mode reuses this field for shadowed page tables to * store flags info -- we don't want to conflict with that. +* Also page_set_tlbflush_timestamp() accesses the same union +* linear_pt_count lives in. Pages (including page table ones), +* however, don't need their flush time stamp set except when +* the last reference is being dropped. For page table pages +* this happens in _put_final_page_type(). */ -if ( !(shadow_mode_enabled (page_get_owner (page)) && +if ( (!ptpg || !PGT_type_equal (x, ptpg->u.inuse.type_info)) && + !(shadow_mode_enabled (page_get_owner (page)) && (page->count_info & PGC_page_table)) )page_set_tlbflush_timestamp (page); }
From: Jan Beulich <[email protected]>
Subject: x86: don't wrongly trigger linear page table assertion (2)
_put_final_page_type(), when free_page_type() has exited early to allow for preemption, should not update the time stamp, as the page continues to retain the typ which is in the process of being unvalidated. I can't see why the time stamp update was put on that path in the first place (albeit it may well have been me who had put it there years ago).
This is part of XSA-240.
xen/arch/x86/mm.c +0 -3
@@ -2355,9 +2355,6 @@ static int _put_final_page_type(struct p {ASSERT ((page->u.inuse.type_info & (PGT_count_mask|PGT_validated|PGT_partial)) ==1 ); -if ( !(shadow_mode_enabled (page_get_owner (page)) && - (page->count_info & PGC_page_table)) ) -page_set_tlbflush_timestamp (page);wmb (); page->u.inuse.type_info |= PGT_validated; }
From ce31198dd811479da34dfb66315f399dc4b98055 Mon Sep 17 00:00:00 2001
From: Jan Beulich <[email protected]>
Date: Thu, 28 Sep 2017 15:17:28 +0100
Subject: [PATCH 1/2] x86: limit linear page table use to a single level
That's the only way that they're meant to be used. Without such a restriction arbitrarily long chains of same-level page tables can be built, tearing down of which may then cause arbitrarily deep recursion, causing a stack overflow. To facilitate this restriction, a counter is being introduced to track both the number of same-level entries in a page table as well as the number of uses of a page table in another same-level one (counting into positive and negative direction respectively, utilizing the fact that both counts can't be non-zero at the same time).
Note that the added accounting introduces a restriction on the number of times a page can be used in other same-level page tables - more than 32k of such uses are no longer possible.
Note also that some put_page_and_type[_preemptible]() calls are replaced with open-coded equivalents. This seemed preferrable to adding "parent_table" to the matrix of functions.
Note further that cross-domain same-level page table references are no longer permitted (they probably never should have been).
This is XSA-240.
--- xen/arch/x86/domain.c | 1 + xen/arch/x86/mm.c | 171 ++++++++++++++++++++++++++++++++++++++----- xen/include/asm-x86/domain.h | 2 + xen/include/asm-x86/mm.h | 25 +++++-- 4 files changed, 175 insertions(+), 24 deletions(-)
xen/arch/x86/domain.c +1 -0
@@ -1131,6 +1131,7 @@ int arch_set_info_guest(case -EINTR : rc = -ERESTART ;case -ERESTART : + v->arch.old_guest_ptpg =NULL ; v->arch.old_guest_table =pagetable_get_page (v->arch.guest_table); v->arch.guest_table =pagetable_null ();
xen/arch/x86/mm.c +154 -17
351 lines not on this page · raw message
xen/include/asm-x86/domain.h +2 -0
@@ -512,6 +512,8 @@ struct arch_vcpupagetable_t guest_table_user;/* (MFN) x86/64 user-space pagetable */ pagetable_t guest_table;/* (MFN) guest notion of cr3 */ struct page_info *old_guest_table;/* partially destructed pagetable */ +struct page_info *old_guest_ptpg;/* containing page table of the */ +/* former, if any */ /* guest_table holds a ref to the page, and also a type-count unless * shadow refcounts are in use */ pagetable_t shadow_table[4 ];/* (MFN) shadow(s) of guest */
xen/include/asm-x86/mm.h +18 -7
@@ -124,11 +124,11 @@ struct page_infou32 tlbflush_timestamp;/* -* When PGT_partial is true then this field is valid and indicates -* that PTEs in the range [0, @nr_validated_ptes) have been validated. -* An extra page reference must be acquired (or not dropped) whenever -* PGT_partial gets set, and it must be dropped when the flag gets -* cleared. This is so that a get() leaving a page in partially +* When PGT_partial is true then the first two fields are valid and +* indicate that PTEs in the range [0, @nr_validated_ptes) have been +* validated. An extra page reference must be acquired (or not dropped) +* whenever PGT_partial gets set, and it must be dropped when the flag +* gets cleared. This is so that a get() leaving a page in partially * validated state (where the caller would drop the reference acquired * due to the getting of the type [apparently] failing [-ERESTART]) * would not accidentally result in a page left with zero general
@@ -152,10 +152,18 @@ struct page_info* put_page_from_lNe() (due to the apparent failure), and hence it * must be dropped when the put operation is resumed (and completes), * but it must not be acquired if picking up the page for validation. +* +* The 3rd field, @linear_pt_count, indicates +* - by a positive value, how many same-level page table entries a page +* table has, +* - by a negative value, in how many same-level page tables a page is +* in use. */ struct { -u16 nr_validated_ptes; -s8 partial_pte; +u16 nr_validated_ptes:PAGETABLE_ORDER +1 ; + u16 :16 - PAGETABLE_ORDER - 1 - 2; +s16 partial_pte:2 ; +s16 linear_pt_count; };/*
@@ -206,6 +214,9 @@ struct page_info#define PGT_count_width PG_shift(9)#define PGT_count_mask ((1UL<<PGT_count_width)-1) +/* Are the 'type mask' bits identical? */ +#define PGT_type_equal (x, y) (!(((x) ^ (y)) & PGT_type_mask)) +/* Cleared when the owning guest 'frees' this page. */ #define _PGC_allocated PG_shift(1)#define PGC_allocated PG_mask(1, 1)
Signature · 1 line
2.14.1
From eeea00c427376c269ffa34587f16fa1182d8c29d Mon Sep 17 00:00:00 2001
From: George Dunlap <[email protected]>
Date: Fri, 22 Sep 2017 11:46:55 +0100
Subject: [PATCH 2/2] x86/mm: Disable PV linear pagetables by default
Allowing pagetables to point to other pagetables of the same level (often called 'linear pagetables') has been included in Xen since its inception. But it is not used by the most common PV guests (Linux, NetBSD, minios), and has been the source of a number of subtle reference-counting bugs.
Add a command-line option to control whether PV linear pagetables are allowed (disabled by default).
---
Changes since v2:
- s/_/-/; in command-line option
- Added __read_mostly
--- docs/misc/xen-command-line.markdown | 15 +++++++++++++++ xen/arch/x86/mm.c | 9 +++++++++ 2 files changed, 24 insertions(+)
docs/misc/xen-command-line.markdown +15 -0
@@ -1231,6 +1231,21 @@ The following resources are available: the cache allocation. * `cat` instructs Xen to enable/disable Cache Allocation Technology. * `cos_max` indicates the max value for COS ID. + +### pv-linear-pt +> `= <boolean>` + +> Default: `false` + +Allow PV guests to have pagetable entries pointing to other pagetables +of the same level (i.e., allowing L2 PTEs to point to other L2 pages). +This technique is often called "linear pagetables", and is sometimes +used to allow operating systems a simple way to consistently map the +current process's pagetables into its own virtual address space. + +None of the most common PV operating systems (Linux, NetBSD, MiniOS) +use this technique, but there may be custom operating systems which +do. ### reboot > `= t[riple] | k[bd] | a[cpi] | p[ci] | P[ower] | e[fi] | n[o] [, [w]arm | [c]old]`
xen/arch/x86/mm.c +9 -0
@@ -727,6 +727,9 @@ static void dec_linear_uses(struct page_info *pg)* frame if it is mapped by a different root table. This is sufficient and * also necessary to allow validation of a root table mapping itself. */ +static bool_t __read_mostly pv_linear_pt_enable =0 ; +boolean_param ("pv-linear-pt" , pv_linear_pt_enable); +#define define_get_linear_pagetable (level) \ static int \ get_##level##_linear_pagetable( \
@@ -736,6 +739,12 @@ get_##level##_linear_pagetable( \struct page_info *page; \unsigned long pfn; \ \ +if ( !pv_linear_pt_enable ) \ + { \ +MEM_LOG ("Attempt to create linear p.t. (feature disabled)" ); \ +return 0 ; \ + } \ + \ if ( (level##e_get_flags(pde) & _PAGE_RW) ) \ { \MEM_LOG ("Attempt to create linear p.t. with write perms" ); \
Signature · 1 line
2.14.1
From: Jan Beulich <[email protected]>
Subject: x86: don't wrongly trigger linear page table assertion
_put_page_type() may do multiple iterations until its cmpxchg() succeeds. It invokes set_tlbflush_timestamp() on the first iteration, however. Code inside the function takes care of this, but
- the assertion in _put_final_page_type() would trigger on the second iteration if time stamps in a debug build are permitted to be sufficiently much wider than the default 6 bits (see WRAP_MASK in flushtlb.c),
- it returning -EINTR (for a continuation to be scheduled) would leave the page inconsistent state (until the re-invocation completes).
Make the set_tlbflush_timestamp() invocation conditional, bypassing it (for now) only in the case we really can't tolerate the stamp to be stored.
This is part of XSA-240.
xen/arch/x86/mm.c +7 -16
@@ -2498,29 +2498,20 @@ static int _put_page_type(struct page_inbreak ; } -if ( ptpg &&PGT_type_equal (x, ptpg->u.inuse.type_info) ) - { -/* -* set_tlbflush_timestamp() accesses the same union -* linear_pt_count lives in. Unvalidated page table pages, -* however, should occur during domain destruction only -* anyway. Updating of linear_pt_count luckily is not -* necessary anymore for a dying domain. -*/ -ASSERT (page_get_owner (page)->is_dying); -ASSERT (page->linear_pt_count <0 ); -ASSERT (ptpg->linear_pt_count >0 ); - ptpg =NULL ; - } -/* * Record TLB information for flush later. We do not stamp page * tables when running in shadow mode: * 1. Pointless, since it's the shadow pt's which must be tracked. * 2. Shadow mode reuses this field for shadowed page tables to * store flags info -- we don't want to conflict with that. +* Also page_set_tlbflush_timestamp() accesses the same union +* linear_pt_count lives in. Pages (including page table ones), +* however, don't need their flush time stamp set except when +* the last reference is being dropped. For page table pages +* this happens in _put_final_page_type(). */ -if ( !(shadow_mode_enabled (page_get_owner (page)) && +if ( (!ptpg || !PGT_type_equal (x, ptpg->u.inuse.type_info)) && + !(shadow_mode_enabled (page_get_owner (page)) && (page->count_info & PGC_page_table)) )page_set_tlbflush_timestamp (page); }
From: Jan Beulich <[email protected]>
Subject: x86: don't wrongly trigger linear page table assertion (2)
_put_final_page_type(), when free_page_type() has exited early to allow for preemption, should not update the time stamp, as the page continues to retain the typ which is in the process of being unvalidated. I can't see why the time stamp update was put on that path in the first place (albeit it may well have been me who had put it there years ago).
This is part of XSA-240.
xen/arch/x86/mm.c +0 -3
@@ -2445,9 +2445,6 @@ static int _put_final_page_type(struct p {ASSERT ((page->u.inuse.type_info & (PGT_count_mask|PGT_validated|PGT_partial)) ==1 ); -if ( !(shadow_mode_enabled (page_get_owner (page)) && - (page->count_info & PGC_page_table)) ) -page_set_tlbflush_timestamp (page);wmb (); page->u.inuse.type_info |= PGT_validated; }
From ea7513a3e3f28cfec59dda6e128b6b4968685762 Mon Sep 17 00:00:00 2001
From: Jan Beulich <[email protected]>
Date: Thu, 28 Sep 2017 15:17:27 +0100
Subject: [PATCH 1/2] x86: limit linear page table use to a single level
That's the only way that they're meant to be used. Without such a restriction arbitrarily long chains of same-level page tables can be built, tearing down of which may then cause arbitrarily deep recursion, causing a stack overflow. To facilitate this restriction, a counter is being introduced to track both the number of same-level entries in a page table as well as the number of uses of a page table in another same-level one (counting into positive and negative direction respectively, utilizing the fact that both counts can't be non-zero at the same time).
Note that the added accounting introduces a restriction on the number of times a page can be used in other same-level page tables - more than 32k of such uses are no longer possible.
Note also that some put_page_and_type[_preemptible]() calls are replaced with open-coded equivalents. This seemed preferrable to adding "parent_table" to the matrix of functions.
Note further that cross-domain same-level page table references are no longer permitted (they probably never should have been).
This is XSA-240.
--- xen/arch/x86/domain.c | 1 + xen/arch/x86/mm.c | 171 ++++++++++++++++++++++++++++++++++++++----- xen/include/asm-x86/domain.h | 2 + xen/include/asm-x86/mm.h | 25 +++++-- 4 files changed, 175 insertions(+), 24 deletions(-)
xen/arch/x86/domain.c +1 -0
@@ -1237,6 +1237,7 @@ int arch_set_info_guest(case -EINTR : rc = -ERESTART ;case -ERESTART : + v->arch.old_guest_ptpg =NULL ; v->arch.old_guest_table =pagetable_get_page (v->arch.guest_table); v->arch.guest_table =pagetable_null ();
xen/arch/x86/mm.c +154 -17
351 lines not on this page · raw message
xen/include/asm-x86/domain.h +2 -0
@@ -529,6 +529,8 @@ struct arch_vcpupagetable_t guest_table_user;/* (MFN) x86/64 user-space pagetable */ pagetable_t guest_table;/* (MFN) guest notion of cr3 */ struct page_info *old_guest_table;/* partially destructed pagetable */ +struct page_info *old_guest_ptpg;/* containing page table of the */ +/* former, if any */ /* guest_table holds a ref to the page, and also a type-count unless * shadow refcounts are in use */ pagetable_t shadow_table[4 ];/* (MFN) shadow(s) of guest */
xen/include/asm-x86/mm.h +18 -7
@@ -125,11 +125,11 @@ struct page_infou32 tlbflush_timestamp;/* -* When PGT_partial is true then this field is valid and indicates -* that PTEs in the range [0, @nr_validated_ptes) have been validated. -* An extra page reference must be acquired (or not dropped) whenever -* PGT_partial gets set, and it must be dropped when the flag gets -* cleared. This is so that a get() leaving a page in partially +* When PGT_partial is true then the first two fields are valid and +* indicate that PTEs in the range [0, @nr_validated_ptes) have been +* validated. An extra page reference must be acquired (or not dropped) +* whenever PGT_partial gets set, and it must be dropped when the flag +* gets cleared. This is so that a get() leaving a page in partially * validated state (where the caller would drop the reference acquired * due to the getting of the type [apparently] failing [-ERESTART]) * would not accidentally result in a page left with zero general
@@ -153,10 +153,18 @@ struct page_info* put_page_from_lNe() (due to the apparent failure), and hence it * must be dropped when the put operation is resumed (and completes), * but it must not be acquired if picking up the page for validation. +* +* The 3rd field, @linear_pt_count, indicates +* - by a positive value, how many same-level page table entries a page +* table has, +* - by a negative value, in how many same-level page tables a page is +* in use. */ struct { -u16 nr_validated_ptes; -s8 partial_pte; +u16 nr_validated_ptes:PAGETABLE_ORDER +1 ; + u16 :16 - PAGETABLE_ORDER - 1 - 2; +s16 partial_pte:2 ; +s16 linear_pt_count; };/*
@@ -207,6 +215,9 @@ struct page_info#define PGT_count_width PG_shift(9)#define PGT_count_mask ((1UL<<PGT_count_width)-1) +/* Are the 'type mask' bits identical? */ +#define PGT_type_equal (x, y) (!(((x) ^ (y)) & PGT_type_mask)) +/* Cleared when the owning guest 'frees' this page. */ #define _PGC_allocated PG_shift(1)#define PGC_allocated PG_mask(1, 1)
Signature · 1 line
2.14.1
From 9a4b34729f1bb92eea1e1efe52e6face9f0b17ae Mon Sep 17 00:00:00 2001
From: George Dunlap <[email protected]>
Date: Fri, 22 Sep 2017 11:46:55 +0100
Subject: [PATCH 2/2] x86/mm: Disable PV linear pagetables by default
Allowing pagetables to point to other pagetables of the same level (often called 'linear pagetables') has been included in Xen since its inception. But it is not used by the most common PV guests (Linux, NetBSD, minios), and has been the source of a number of subtle reference-counting bugs.
Add a command-line option to control whether PV linear pagetables are allowed (disabled by default).
---
Changes since v2:
- s/_/-/; in command-line option
- Added __read_mostly
--- docs/misc/xen-command-line.markdown | 15 +++++++++++++++ xen/arch/x86/mm.c | 9 +++++++++ 2 files changed, 24 insertions(+)
docs/misc/xen-command-line.markdown +15 -0
@@ -1280,6 +1280,21 @@ The following resources are available: CDP, one COS will corespond two CBMs other than one with CAT, due to the sum of CBMs is fixed, that means actual `cos_max` in use will automatically reduce to half when CDP is enabled. + +### pv-linear-pt +> `= <boolean>` + +> Default: `false` + +Allow PV guests to have pagetable entries pointing to other pagetables +of the same level (i.e., allowing L2 PTEs to point to other L2 pages). +This technique is often called "linear pagetables", and is sometimes +used to allow operating systems a simple way to consistently map the +current process's pagetables into its own virtual address space. + +None of the most common PV operating systems (Linux, NetBSD, MiniOS) +use this technique, but there may be custom operating systems which +do. ### reboot > `= t[riple] | k[bd] | a[cpi] | p[ci] | P[ower] | e[fi] | n[o] [, [w]arm | [c]old]`
xen/arch/x86/mm.c +9 -0
@@ -799,6 +799,9 @@ static void dec_linear_uses(struct page_info *pg)* frame if it is mapped by a different root table. This is sufficient and * also necessary to allow validation of a root table mapping itself. */ +static bool_t __read_mostly pv_linear_pt_enable =0 ; +boolean_param ("pv-linear-pt" , pv_linear_pt_enable); +#define define_get_linear_pagetable (level) \ static int \ get_##level##_linear_pagetable( \
@@ -808,6 +811,12 @@ get_##level##_linear_pagetable( \struct page_info *page; \unsigned long pfn; \ \ +if ( !pv_linear_pt_enable ) \ + { \ +MEM_LOG ("Attempt to create linear p.t. (feature disabled)" ); \ +return 0 ; \ + } \ + \ if ( (level##e_get_flags(pde) & _PAGE_RW) ) \ { \MEM_LOG ("Attempt to create linear p.t. with write perms" ); \
Signature · 1 line
2.14.1
From: Jan Beulich <[email protected]>
Subject: x86: don't wrongly trigger linear page table assertion
_put_page_type() may do multiple iterations until its cmpxchg() succeeds. It invokes set_tlbflush_timestamp() on the first iteration, however. Code inside the function takes care of this, but
- the assertion in _put_final_page_type() would trigger on the second iteration if time stamps in a debug build are permitted to be sufficiently much wider than the default 6 bits (see WRAP_MASK in flushtlb.c),
- it returning -EINTR (for a continuation to be scheduled) would leave the page inconsistent state (until the re-invocation completes).
Make the set_tlbflush_timestamp() invocation conditional, bypassing it (for now) only in the case we really can't tolerate the stamp to be stored.
This is part of XSA-240.
xen/arch/x86/mm.c +9 -21
@@ -2612,34 +2612,22 @@ static int _put_page_type(struct page_inbreak ; } -#ifdef CONFIG_PV_LINEAR_PT -if ( ptpg &&PGT_type_equal (x, ptpg->u.inuse.type_info) ) - { -/* -* set_tlbflush_timestamp() accesses the same union -* linear_pt_count lives in. Unvalidated page table pages, -* however, should occur during domain destruction only -* anyway. Updating of linear_pt_count luckily is not -* necessary anymore for a dying domain. -*/ -ASSERT (page_get_owner (page)->is_dying); -ASSERT (page->linear_pt_count <0 ); -ASSERT (ptpg->linear_pt_count >0 ); - ptpg =NULL ; - } -#else /* CONFIG_PV_LINEAR_PT */ -BUG_ON (ptpg &&PGT_type_equal (x, ptpg->u.inuse.type_info)); -#endif -/* * Record TLB information for flush later. We do not stamp page * tables when running in shadow mode: * 1. Pointless, since it's the shadow pt's which must be tracked. * 2. Shadow mode reuses this field for shadowed page tables to * store flags info -- we don't want to conflict with that. +* Also page_set_tlbflush_timestamp() accesses the same union +* linear_pt_count lives in. Pages (including page table ones), +* however, don't need their flush time stamp set except when +* the last reference is being dropped. For page table pages +* this happens in _put_final_page_type(). */ -if ( !(shadow_mode_enabled (page_get_owner (page)) && - (page->count_info & PGC_page_table)) ) +if ( ptpg &&PGT_type_equal (x, ptpg->u.inuse.type_info) ) +BUG_ON (!IS_ENABLED (CONFIG_PV_LINEAR_PT )); +else if ( !(shadow_mode_enabled (page_get_owner (page)) && + (page->count_info & PGC_page_table)) )page_set_tlbflush_timestamp (page); } else if ( unlikely((nx & (PGT_locked | PGT_count_mask)) ==
From: Jan Beulich <[email protected]>
Subject: x86: don't wrongly trigger linear page table assertion (2)
_put_final_page_type(), when free_page_type() has exited early to allow for preemption, should not update the time stamp, as the page continues to retain the typ which is in the process of being unvalidated. I can't see why the time stamp update was put on that path in the first place (albeit it may well have been me who had put it there years ago).
This is part of XSA-240.
xen/arch/x86/mm.c +0 -3
@@ -2559,9 +2559,6 @@ static int _put_final_page_type(struct p {ASSERT ((page->u.inuse.type_info & (PGT_count_mask|PGT_validated|PGT_partial)) ==1 ); -if ( !(shadow_mode_enabled (page_get_owner (page)) && - (page->count_info & PGC_page_table)) ) -page_set_tlbflush_timestamp (page);wmb (); page->u.inuse.type_info |= PGT_validated; }
From 2315b8c651e0cc31c9153d09c9912b8fbe632ad2 Mon Sep 17 00:00:00 2001
From: Jan Beulich <[email protected]>
Date: Thu, 28 Sep 2017 15:17:25 +0100
Subject: [PATCH 1/2] x86: limit linear page table use to a single level
That's the only way that they're meant to be used. Without such a restriction arbitrarily long chains of same-level page tables can be built, tearing down of which may then cause arbitrarily deep recursion, causing a stack overflow. To facilitate this restriction, a counter is being introduced to track both the number of same-level entries in a page table as well as the number of uses of a page table in another same-level one (counting into positive and negative direction respectively, utilizing the fact that both counts can't be non-zero at the same time).
Note that the added accounting introduces a restriction on the number of times a page can be used in other same-level page tables - more than 32k of such uses are no longer possible.
Note also that some put_page_and_type[_preemptible]() calls are replaced with open-coded equivalents. This seemed preferrable to adding "parent_table" to the matrix of functions.
Note further that cross-domain same-level page table references are no longer permitted (they probably never should have been).
This is XSA-240.
--- xen/arch/x86/domain.c | 1 + xen/arch/x86/mm.c | 171 ++++++++++++++++++++++++++++++++++++++----- xen/include/asm-x86/domain.h | 2 + xen/include/asm-x86/mm.h | 25 +++++-- 4 files changed, 175 insertions(+), 24 deletions(-)
xen/arch/x86/domain.c +1 -0
@@ -1245,6 +1245,7 @@ int arch_set_info_guest( rc = -ERESTART ;/* Fallthrough */ case -ERESTART : + v->arch.old_guest_ptpg =NULL ; v->arch.old_guest_table =pagetable_get_page (v->arch.guest_table); v->arch.guest_table =pagetable_null ();
xen/arch/x86/mm.c +154 -17
351 lines not on this page · raw message
xen/include/asm-x86/domain.h +2 -0
@@ -531,6 +531,8 @@ struct arch_vcpupagetable_t guest_table_user;/* (MFN) x86/64 user-space pagetable */ pagetable_t guest_table;/* (MFN) guest notion of cr3 */ struct page_info *old_guest_table;/* partially destructed pagetable */ +struct page_info *old_guest_ptpg;/* containing page table of the */ +/* former, if any */ /* guest_table holds a ref to the page, and also a type-count unless * shadow refcounts are in use */ pagetable_t shadow_table[4 ];/* (MFN) shadow(s) of guest */
xen/include/asm-x86/mm.h +18 -7
@@ -125,11 +125,11 @@ struct page_infou32 tlbflush_timestamp;/* -* When PGT_partial is true then this field is valid and indicates -* that PTEs in the range [0, @nr_validated_ptes) have been validated. -* An extra page reference must be acquired (or not dropped) whenever -* PGT_partial gets set, and it must be dropped when the flag gets -* cleared. This is so that a get() leaving a page in partially +* When PGT_partial is true then the first two fields are valid and +* indicate that PTEs in the range [0, @nr_validated_ptes) have been +* validated. An extra page reference must be acquired (or not dropped) +* whenever PGT_partial gets set, and it must be dropped when the flag +* gets cleared. This is so that a get() leaving a page in partially * validated state (where the caller would drop the reference acquired * due to the getting of the type [apparently] failing [-ERESTART]) * would not accidentally result in a page left with zero general
@@ -153,10 +153,18 @@ struct page_info* put_page_from_lNe() (due to the apparent failure), and hence it * must be dropped when the put operation is resumed (and completes), * but it must not be acquired if picking up the page for validation. +* +* The 3rd field, @linear_pt_count, indicates +* - by a positive value, how many same-level page table entries a page +* table has, +* - by a negative value, in how many same-level page tables a page is +* in use. */ struct { -u16 nr_validated_ptes; -s8 partial_pte; +u16 nr_validated_ptes:PAGETABLE_ORDER +1 ; + u16 :16 - PAGETABLE_ORDER - 1 - 2; +s16 partial_pte:2 ; +s16 linear_pt_count; };/*
@@ -207,6 +215,9 @@ struct page_info#define PGT_count_width PG_shift(9)#define PGT_count_mask ((1UL<<PGT_count_width)-1) +/* Are the 'type mask' bits identical? */ +#define PGT_type_equal (x, y) (!(((x) ^ (y)) & PGT_type_mask)) +/* Cleared when the owning guest 'frees' this page. */ #define _PGC_allocated PG_shift(1)#define PGC_allocated PG_mask(1, 1)
Signature · 1 line
2.14.1
From 41d579aad2fee971e5ce0279a9b559a0fdc74452 Mon Sep 17 00:00:00 2001
From: George Dunlap <[email protected]>
Date: Fri, 22 Sep 2017 11:46:55 +0100
Subject: [PATCH 2/2] x86/mm: Disable PV linear pagetables by default
Allowing pagetables to point to other pagetables of the same level (often called 'linear pagetables') has been included in Xen since its inception. But it is not used by the most common PV guests (Linux, NetBSD, minios), and has been the source of a number of subtle reference-counting bugs.
Add a command-line option to control whether PV linear pagetables are allowed (disabled by default).
---
Changes since v2:
- s/_/-/; in command-line option
- Added __read_mostly
--- docs/misc/xen-command-line.markdown | 15 +++++++++++++++ xen/arch/x86/mm.c | 9 +++++++++ 2 files changed, 24 insertions(+)
docs/misc/xen-command-line.markdown +15 -0
@@ -1350,6 +1350,21 @@ The following resources are available: CDP, one COS will corespond two CBMs other than one with CAT, due to the sum of CBMs is fixed, that means actual `cos_max` in use will automatically reduce to half when CDP is enabled. + +### pv-linear-pt +> `= <boolean>` + +> Default: `false` + +Allow PV guests to have pagetable entries pointing to other pagetables +of the same level (i.e., allowing L2 PTEs to point to other L2 pages). +This technique is often called "linear pagetables", and is sometimes +used to allow operating systems a simple way to consistently map the +current process's pagetables into its own virtual address space. + +None of the most common PV operating systems (Linux, NetBSD, MiniOS) +use this technique, but there may be custom operating systems which +do. ### reboot > `= t[riple] | k[bd] | a[cpi] | p[ci] | P[ower] | e[fi] | n[o] [, [w]arm | [c]old]`
xen/arch/x86/mm.c +9 -0
@@ -800,6 +800,9 @@ static void dec_linear_uses(struct page_info *pg)* frame if it is mapped by a different root table. This is sufficient and * also necessary to allow validation of a root table mapping itself. */ +static bool __read_mostly pv_linear_pt_enable = false; +boolean_param ("pv-linear-pt" , pv_linear_pt_enable); +#define define_get_linear_pagetable (level) \ static int \ get_##level##_linear_pagetable( \
@@ -809,6 +812,12 @@ get_##level##_linear_pagetable( \struct page_info *page; \unsigned long pfn; \ \ +if ( !pv_linear_pt_enable ) \ + { \ +MEM_LOG ("Attempt to create linear p.t. (feature disabled)" ); \ +return 0 ; \ + } \ + \ if ( (level##e_get_flags(pde) & _PAGE_RW) ) \ { \MEM_LOG ("Attempt to create linear p.t. with write perms" ); \
Signature · 1 line
2.14.1
From: Jan Beulich <[email protected]>
Subject: x86: don't wrongly trigger linear page table assertion
_put_page_type() may do multiple iterations until its cmpxchg() succeeds. It invokes set_tlbflush_timestamp() on the first iteration, however. Code inside the function takes care of this, but
- the assertion in _put_final_page_type() would trigger on the second iteration if time stamps in a debug build are permitted to be sufficiently much wider than the default 6 bits (see WRAP_MASK in flushtlb.c),
- it returning -EINTR (for a continuation to be scheduled) would leave the page inconsistent state (until the re-invocation completes).
Make the set_tlbflush_timestamp() invocation conditional, bypassing it (for now) only in the case we really can't tolerate the stamp to be stored.
This is part of XSA-240.
xen/arch/x86/mm.c +9 -21
@@ -2613,34 +2613,22 @@ static int _put_page_type(struct page_inbreak ; } -#ifdef CONFIG_PV_LINEAR_PT -if ( ptpg &&PGT_type_equal (x, ptpg->u.inuse.type_info) ) - { -/* -* set_tlbflush_timestamp() accesses the same union -* linear_pt_count lives in. Unvalidated page table pages, -* however, should occur during domain destruction only -* anyway. Updating of linear_pt_count luckily is not -* necessary anymore for a dying domain. -*/ -ASSERT (page_get_owner (page)->is_dying); -ASSERT (page->linear_pt_count <0 ); -ASSERT (ptpg->linear_pt_count >0 ); - ptpg =NULL ; - } -#else /* CONFIG_PV_LINEAR_PT */ -BUG_ON (ptpg &&PGT_type_equal (x, ptpg->u.inuse.type_info)); -#endif -/* * Record TLB information for flush later. We do not stamp page * tables when running in shadow mode: * 1. Pointless, since it's the shadow pt's which must be tracked. * 2. Shadow mode reuses this field for shadowed page tables to * store flags info -- we don't want to conflict with that. +* Also page_set_tlbflush_timestamp() accesses the same union +* linear_pt_count lives in. Pages (including page table ones), +* however, don't need their flush time stamp set except when +* the last reference is being dropped. For page table pages +* this happens in _put_final_page_type(). */ -if ( !(shadow_mode_enabled (page_get_owner (page)) && - (page->count_info & PGC_page_table)) ) +if ( ptpg &&PGT_type_equal (x, ptpg->u.inuse.type_info) ) +BUG_ON (!IS_ENABLED (CONFIG_PV_LINEAR_PT )); +else if ( !(shadow_mode_enabled (page_get_owner (page)) && + (page->count_info & PGC_page_table)) )page_set_tlbflush_timestamp (page); } else if ( unlikely((nx & (PGT_locked | PGT_count_mask)) ==
From: Jan Beulich <[email protected]>
Subject: x86: don't wrongly trigger linear page table assertion (2)
_put_final_page_type(), when free_page_type() has exited early to allow for preemption, should not update the time stamp, as the page continues to retain the typ which is in the process of being unvalidated. I can't see why the time stamp update was put on that path in the first place (albeit it may well have been me who had put it there years ago).
This is part of XSA-240.
xen/arch/x86/mm.c +0 -3
@@ -2560,9 +2560,6 @@ static int _put_final_page_type(struct p {ASSERT ((page->u.inuse.type_info & (PGT_count_mask|PGT_validated|PGT_partial)) ==1 ); -if ( !(shadow_mode_enabled (page_get_owner (page)) && - (page->count_info & PGC_page_table)) ) -page_set_tlbflush_timestamp (page);wmb (); page->u.inuse.type_info |= PGT_validated; }
From 867988237d3e472fe2c99e81ae733e103422566c Mon Sep 17 00:00:00 2001
From: Jan Beulich <[email protected]>
Date: Thu, 28 Sep 2017 15:17:25 +0100
Subject: [PATCH 1/2] x86: limit linear page table use to a single level
That's the only way that they're meant to be used. Without such a restriction arbitrarily long chains of same-level page tables can be built, tearing down of which may then cause arbitrarily deep recursion, causing a stack overflow. To facilitate this restriction, a counter is being introduced to track both the number of same-level entries in a page table as well as the number of uses of a page table in another same-level one (counting into positive and negative direction respectively, utilizing the fact that both counts can't be non-zero at the same time).
Note that the added accounting introduces a restriction on the number of times a page can be used in other same-level page tables - more than 32k of such uses are no longer possible.
Note also that some put_page_and_type[_preemptible]() calls are replaced with open-coded equivalents. This seemed preferrable to adding "parent_table" to the matrix of functions.
Note further that cross-domain same-level page table references are no longer permitted (they probably never should have been).
This is XSA-240.
--- xen/arch/x86/domain.c | 1 + xen/arch/x86/mm.c | 171 ++++++++++++++++++++++++++++++++++++++----- xen/include/asm-x86/domain.h | 2 + xen/include/asm-x86/mm.h | 25 +++++-- 4 files changed, 175 insertions(+), 24 deletions(-)
xen/arch/x86/domain.c +1 -0
@@ -1226,6 +1226,7 @@ int arch_set_info_guest( rc = -ERESTART ;/* Fallthrough */ case -ERESTART : + v->arch.old_guest_ptpg =NULL ; v->arch.old_guest_table =pagetable_get_page (v->arch.guest_table); v->arch.guest_table =pagetable_null ();
xen/arch/x86/mm.c +154 -17
351 lines not on this page · raw message
xen/include/asm-x86/domain.h +2 -0
@@ -527,6 +527,8 @@ struct arch_vcpupagetable_t guest_table_user;/* (MFN) x86/64 user-space pagetable */ pagetable_t guest_table;/* (MFN) guest notion of cr3 */ struct page_info *old_guest_table;/* partially destructed pagetable */ +struct page_info *old_guest_ptpg;/* containing page table of the */ +/* former, if any */ /* guest_table holds a ref to the page, and also a type-count unless * shadow refcounts are in use */ pagetable_t shadow_table[4 ];/* (MFN) shadow(s) of guest */
xen/include/asm-x86/mm.h +18 -7
@@ -124,11 +124,11 @@ struct page_infou32 tlbflush_timestamp;/* -* When PGT_partial is true then this field is valid and indicates -* that PTEs in the range [0, @nr_validated_ptes) have been validated. -* An extra page reference must be acquired (or not dropped) whenever -* PGT_partial gets set, and it must be dropped when the flag gets -* cleared. This is so that a get() leaving a page in partially +* When PGT_partial is true then the first two fields are valid and +* indicate that PTEs in the range [0, @nr_validated_ptes) have been +* validated. An extra page reference must be acquired (or not dropped) +* whenever PGT_partial gets set, and it must be dropped when the flag +* gets cleared. This is so that a get() leaving a page in partially * validated state (where the caller would drop the reference acquired * due to the getting of the type [apparently] failing [-ERESTART]) * would not accidentally result in a page left with zero general
@@ -152,10 +152,18 @@ struct page_info* put_page_from_lNe() (due to the apparent failure), and hence it * must be dropped when the put operation is resumed (and completes), * but it must not be acquired if picking up the page for validation. +* +* The 3rd field, @linear_pt_count, indicates +* - by a positive value, how many same-level page table entries a page +* table has, +* - by a negative value, in how many same-level page tables a page is +* in use. */ struct { -u16 nr_validated_ptes; -s8 partial_pte; +u16 nr_validated_ptes:PAGETABLE_ORDER +1 ; + u16 :16 - PAGETABLE_ORDER - 1 - 2; +s16 partial_pte:2 ; +s16 linear_pt_count; };/*
@@ -206,6 +214,9 @@ struct page_info#define PGT_count_width PG_shift(9)#define PGT_count_mask ((1UL<<PGT_count_width)-1) +/* Are the 'type mask' bits identical? */ +#define PGT_type_equal (x, y) (!(((x) ^ (y)) & PGT_type_mask)) +/* Cleared when the owning guest 'frees' this page. */ #define _PGC_allocated PG_shift(1)#define PGC_allocated PG_mask(1, 1)
Signature · 1 line
2.14.1
From e614979ce054044d9e19023f1ef10dae6e38baf4 Mon Sep 17 00:00:00 2001
From: George Dunlap <[email protected]>
Date: Fri, 22 Sep 2017 11:46:55 +0100
Subject: [PATCH 2/2] x86/mm: Disable PV linear pagetables by default
Allowing pagetables to point to other pagetables of the same level (often called 'linear pagetables') has been included in Xen since its inception. But it is not used by the most common PV guests (Linux, NetBSD, minios), and has been the source of a number of subtle reference-counting bugs.
Add a command-line option to control whether PV linear pagetables are allowed (disabled by default).
---
Changes since v2:
- s/_/-/; in command-line option
- Added __read_mostly
--- docs/misc/xen-command-line.markdown | 15 +++++++++++++++ xen/arch/x86/mm.c | 10 ++++++++++ 2 files changed, 25 insertions(+)
docs/misc/xen-command-line.markdown +15 -0
@@ -1374,6 +1374,21 @@ The following resources are available: CDP, one COS will corespond two CBMs other than one with CAT, due to the sum of CBMs is fixed, that means actual `cos_max` in use will automatically reduce to half when CDP is enabled. + +### pv-linear-pt +> `= <boolean>` + +> Default: `false` + +Allow PV guests to have pagetable entries pointing to other pagetables +of the same level (i.e., allowing L2 PTEs to point to other L2 pages). +This technique is often called "linear pagetables", and is sometimes +used to allow operating systems a simple way to consistently map the +current process's pagetables into its own virtual address space. + +None of the most common PV operating systems (Linux, NetBSD, MiniOS) +use this technique, but there may be custom operating systems which +do. ### reboot > `= t[riple] | k[bd] | a[cpi] | p[ci] | P[ower] | e[fi] | n[o] [, [w]arm | [c]old]`
xen/arch/x86/mm.c +10 -0
@@ -814,6 +814,9 @@ static void dec_linear_uses(struct page_info *pg)* frame if it is mapped by a different root table. This is sufficient and * also necessary to allow validation of a root table mapping itself. */ +static bool __read_mostly pv_linear_pt_enable = false; +boolean_param ("pv-linear-pt" , pv_linear_pt_enable); +#define define_get_linear_pagetable (level) \ static int \ get_##level##_linear_pagetable( \
@@ -823,6 +826,13 @@ get_##level##_linear_pagetable( \struct page_info *page; \unsigned long pfn; \ \ +if ( !pv_linear_pt_enable ) \ + { \ +gdprintk (XENLOG_WARNING , \ +"Attempt to create linear p.t. (feature disabled)\n" ); \
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.14.1
From: Jan Beulich <[email protected]>
Subject: x86: don't wrongly trigger linear page table assertion
_put_page_type() may do multiple iterations until its cmpxchg() succeeds. It invokes set_tlbflush_timestamp() on the first iteration, however. Code inside the function takes care of this, but
- the assertion in _put_final_page_type() would trigger on the second iteration if time stamps in a debug build are permitted to be sufficiently much wider than the default 6 bits (see WRAP_MASK in flushtlb.c),
- it returning -EINTR (for a continuation to be scheduled) would leave the page inconsistent state (until the re-invocation completes).
Make the set_tlbflush_timestamp() invocation conditional, bypassing it (for now) only in the case we really can't tolerate the stamp to be stored.
This is part of XSA-240.
xen/arch/x86/mm.c +9 -21
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: x86: don't wrongly trigger linear page table assertion (2)
_put_final_page_type(), when free_page_type() has exited early to allow for preemption, should not update the time stamp, as the page continues to retain the typ which is in the process of being unvalidated. I can't see why the time stamp update was put on that path in the first place (albeit it may well have been me who had put it there years ago).
This is part of XSA-240.
xen/arch/x86/mm.c +0 -3
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Part of the diff is not shown here. View the raw message.
Quoted 10 lines · AEDT Xen. org security team
Xen Security Advisory CVE-2017-15595 / XSA-240
version 6Unlimited recursion in linear pagetable de-typing
UPDATES IN VERSION 6
====================Yet another new patch, addressing another issue similar to the one addressed in v5.
Is there any news / information on what to patch on this for releases that already have xsa240 included such as 4.9.1 and 4.7.4?
Signature · 4 lines
Steven Haigh 📧 [email protected] 💻 http://www.crc.id.au 📞 +61 (3) 9001 6090 📱 0412 935 897
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 6 lines · Steven Haigh
Quoted 5 lines · AEDT Xen. org security team
Xen Security Advisory CVE-2017-15595 / XSA-240
version 6Unlimited recursion in linear pagetable de-typing
Quoted 8 lines
Quoted 5 lines
UPDATES IN VERSION 6
====================Yet another new patch, addressing another issue similar to the one addressed in v5.
Is there any news / information on what to patch on this for releases that already have xsa240 included such as 4.9.1 and 4.7.4?
Yes, looking through the advisory after it was sent out, I think we definitely should have provided instructions for downstreams for how to actually use the patches.
As discussed previously, the entire series should look like:
1) Patch 0001 from the original advisory
2) Patches from XSA 243
3) The "checked into tree" version of xsa240/0002
4) Patch xsa240-*/0003
5) Patch xsa240-*/0004
In other words, you should be able to apply xsa240 patch 4 directly on xsa240 patch 3.
Sorry again for the confusion.
-George
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel