Xen Security Advisory 224 - grant table operations mishandle reference counts
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256
Xen Security Advisory XSA-224
version 4
grant table operations mishandle reference counts
UPDATES IN VERSION 4
====================
Correct 4.5 backports (first patch had an issue which then was corrected by last one).
Public release.
ISSUE DESCRIPTION
=================
We have discovered a number of bugs in the code mapping and unmapping grant references.
* If a grant is mapped with both the GNTMAP_device_map and GNTMAP_host_map flags, but unmapped only with host_map, the device_map portion remains but the page reference counts are lowered as though it had been removed. This bug can be leveraged cause a page's reference counts and type counts to fall to zero while retaining writeable mappings to the page.
* Under some specific conditions, if a grant is mapped with both the GNTMAP_device_map and GNTMAP_host_map flags, the operation may not grab sufficient type counts. When the grant is then unmapped, the type count will be erroneously reduced. This bug can be leveraged cause a page's reference counts and type counts to fall to zero while retaining writeable mappings to the page.
* When a grant reference is given to an MMIO region (as opposed to a normal guest page), if the grant is mapped with only the GNTMAP_device_map flag set, a mapping is created at host_addr anyway. This does *not* cause reference counts to change, but there will be no record of this mapping, so it will not be considered when reporting whether the grant is still in use.
IMPACT
======
For the worst issue, a PV guest could gain a writeable mapping of its own pagetable, allowing it to escalate its privileges to that of the host.
VULNERABLE SYSTEMS
==================
All versions of Xen are vulnerable.
Only x86 systems are vulnerable.
Any system running untrusted PV guests is vulnerable.
Systems with untrusted HVM guests are only vulnerable if those guests are served by a trusted PV backend which is vulnerable: Namely, one which calls grant_map() with both the GNTMAP_device_map and GNTMAP_host_map flags. The security team is not aware of any backends which are vulnerable.
MITIGATION
==========
Running only HVM guests will avoid this vulnerability.
CREDITS
=======
This issue was discovered by Jan Beulich of SUSE.
RESOLUTION
==========
Applying the appropriate set of attached patched resolves this issue. Note that these patches are assumed to be applied on top of the XSA-218 ones; not doing so may cause at least mechanical problems of applying the ones here.
xsa224-unstable/*.patch xen-unstable
xsa224-4.8/*.patch Xen 4.8.x xsa224-4.7/*.patch Xen 4.7.x xsa224-4.6/*.patch Xen 4.6.x xsa224-4.5/*.patch Xen 4.5.x
$ sha256sum xsa224*/* db39535185c1879775b62873fbed1e6285300ec1e1bd5d09ac2d96a98ac6443c xsa224-unstable/0001-gnttab-Fix-handling-of-dev_bus_addr-during-unmap.patch 1588257f5b0c7113cd478475014f56fbeb6e79de7acbe67cf6d7a265e2b3fa15 xsa224-unstable/0002-gnttab-never-create-host-mapping-unless-asked-to.patch a7517ca0e253fb9fb5b1ea1e56d04167f32ef87be145462a15241af26e4e0d65 xsa224-unstable/0003-gnttab-correct-logic-to-get-page-references-during-m.patch 951217a88f9c945eb9f7933cd66615aef955206fab955020334ac54da05663fa xsa224-unstable/0004-gnttab-__gnttab_unmap_common_complete-is-all-or-noth.patch 190470fbd77fca58aab89a9bd034732525ce8f7ce7c417a0ca5d25b366639baa xsa224-4.5/0001-gnttab-Fix-handling-of-dev_bus_addr-during-unmap.patch 9374e4dd6666a63fb32e6cfbdc95071b0cc153ff7cb2d2efdd98468e0e079605 xsa224-4.5/0002-gnttab-never-create-host-mapping-unless-asked-to.patch d825e6fa5827e28e3755c92b274044666cc91b6a8cbc16e2081f43e0371991d4 xsa224-4.5/0003-gnttab-correct-logic-to-get-page-references-during-m.patch d3aaffaf487a84e43fe10f7dec5af72b64d1b2315440c36335a0ed8ec1439ca1 xsa224-4.5/0004-gnttab-__gnttab_unmap_common_complete-is-all-or-noth.patch c6cd6b82ef774bec5eaad5f32e767c917bc7ad2a73ee81d3f7eef67aaf1a1330 xsa224-4.6/0001-gnttab-Fix-handling-of-dev_bus_addr-during-unmap.patch db32d15757c9d147c7e89eebd10a16324e59141fbb5ce3feb87fc9bf01864a6a xsa224-4.6/0002-gnttab-never-create-host-mapping-unless-asked-to.patch 6bc9bbcf320d673822bd41545a014bd998294d06c5b38d79a6badf1a154ed0d6 xsa224-4.6/0003-gnttab-correct-logic-to-get-page-references-during-m.patch 088064fec3192928f205b34b808ca40fd685a8ba5037bb665ed0a4f87d6d4035 xsa224-4.6/0004-gnttab-__gnttab_unmap_common_complete-is-all-or-noth.patch cdd93fb950b823cf96fe52685f6394c1b5e0a1e3d7d3c961a5e781da83551a9f xsa224-4.7/0001-gnttab-Fix-handling-of-dev_bus_addr-during-unmap.patch 0583da31891084b2557a9623bc2b11a480e296004a8716b91c79fe28a824a6e0 xsa224-4.7/0002-gnttab-never-create-host-mapping-unless-asked-to.patch 2323bf581a835f152285b98ed2e4b5b503b0f67bd8e3449d33e8fe03b14ce064 xsa224-4.7/0003-gnttab-correct-logic-to-get-page-references-during-m.patch b4f4adb1ea850e0174e51f76da7e97769211977c71809bd62102d33d90444b09 xsa224-4.7/0004-gnttab-__gnttab_unmap_common_complete-is-all-or-noth.patch 88b20e6765f0bfffe7598215f3a8e25c0931dbe3c7223cb3c08f998842cfc14b xsa224-4.8/0001-gnttab-Fix-handling-of-dev_bus_addr-during-unmap.patch ce62c97f470d6fbf557f50be8936051e91592a6330527515b7cdb187a0d633b2 xsa224-4.8/0002-gnttab-never-create-host-mapping-unless-asked-to.patch 5fd8cd67737c6a038d6c47fcf3c5bd2d238f4ac361538d650292ee185bda8000 xsa224-4.8/0003-gnttab-correct-logic-to-get-page-references-during-m.patch f9c65c7f04063872602c609d2fc3caffc44716b3d378569969a7884abe881a19 xsa224-4.8/0004-gnttab-__gnttab_unmap_common_complete-is-all-or-noth.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
iQEcBAEBCAAGBQJZSQ3YAAoJEIP+FMlX6CvZD8IH/3wQYr87n4rcqeAZl9afsouD kvasR7upPlxm8gH9wEz5PCg4thPKh1X1dNQhefTUMujPGUdxGgz9DYISukHCksso Avf5Ge/76k2Xxw9CjH3T5pgYK7V95ABc0omfvHeL8gJjTOwPSrBZkUOEQxUrGSFp /Q4op0BIIyOUR4TdYyl5gWlQJC1leTCKSvsqju31BdvOg+kJF7aU1NUho5qgXTa8 lq/UjVFufZyhxK371q7eYKDZcFiDngzlRXu5Mz1uowTOJHWwgkRLFJ0kl5NK7O+j JWSwmJZxBw6yw+sxCwBTKkskxietGRQU9Febz+Aj3Urma3djiBNeazU9t0BEq0E= =PeqY
-----END PGP SIGNATURE-----
From 5a67915261681a1609c05dfe561d20be2669b94a Mon Sep 17 00:00:00 2001
From: George Dunlap <[email protected]>
Date: Thu, 15 Jun 2017 16:24:02 +0100
Subject: [PATCH 1/4] gnttab: Fix handling of dev_bus_addr during unmap
If a grant has been mapped with the GNTTAB_device_map flag, calling grant_unmap_ref() with dev_bus_addr set to zero should cause the GNTTAB_device_map part of the mapping to be left alone.
Unfortunately, at the moment, op->dev_bus_addr is implicitly checked before clearing the map and adjusting the pin count, but only the bits above 12; and it is not checked at all before dropping page references. This means a guest can repeatedly make such a call to cause the reference count to drop to zero, causing the page to be freed and re-used, even though it's still mapped in its pagetables.
To fix this, always check op->dev_bus_addr explicitly for being non-zero, as well as op->flag & GNTMAP_device_map, before doing operations on the device_map.
While we're here, make the logic a bit cleaner:
* Always initialize op->frame to zero and set it from act->frame, to reduce the chance of untrusted input being used
* Explicitly check the full dev_bus_addr against act->frame << PAGE_SHIFT, rather than ignoring the lower 12 bits
This is part of XSA-224.
--- xen/common/grant_table.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-)
xen/common/grant_table.c +11 -12
@@ -1089,8 +1089,6 @@ __gnttab_unmap_common( ld = current->domain; lgt = ld->grant_table; - op->frame = (unsigned long )(op->dev_bus_addr >>PAGE_SHIFT ); -if (unlikely (op->handle >= lgt->maptrack_limit) ) {gdprintk (XENLOG_INFO ,"Bad handle %#x\n" , op->handle);
@@ -1174,16 +1172,14 @@ __gnttab_unmap_common( goto act_release_out; } -if ( op->frame ==0 ) - { - op->frame = act->frame; - } -else + op->frame = act->frame; + +if ( op->dev_bus_addr ) { -if (unlikely (op->frame != act->frame) ) +if (unlikely (op->dev_bus_addr !=pfn_to_paddr (act->frame)) )PIN_FAIL (act_release_out, GNTST_general_error, -"Bad frame number doesn't match gntref. (%lx != %lx)\n" , - op->frame, act->frame); +"Bus address doesn't match gntref (%" PRIx64" != %" PRIpaddr")\n" , + op->dev_bus_addr,pfn_to_paddr (act->frame)); map->flags &= ~GNTMAP_device_map; }
@@ -1276,7 +1272,8 @@ __gnttab_unmap_common_complete(struct gnttab_unmap_common *op)else status = &status_entry (rgt, op->ref); -if (unlikely (op->frame != act->frame) ) +if ( op->dev_bus_addr && +unlikely (op->dev_bus_addr !=pfn_to_paddr (act->frame)) ) {/* * Suggests that __gntab_unmap_common failed early and so
@@ -1287,7 +1284,7 @@ __gnttab_unmap_common_complete(struct gnttab_unmap_common *op) pg =mfn_to_page (op->frame); -if ( op->flags & GNTMAP_device_map ) +if ( op->dev_bus_addr && (op->flags & GNTMAP_device_map) ) {if ( !is_iomem_page (_mfn (act->frame)) ) {
@@ -1358,6 +1355,7 @@ __gnttab_unmap_grant_ref(/* Intialise these in case common contains old state */ common->new_addr =0 ; common->rd =NULL ; + common->frame =0 ;__gnttab_unmap_common (common); op->status = common->status;
@@ -1422,6 +1420,7 @@ __gnttab_unmap_and_replace(/* Intialise these in case common contains old state */ common->dev_bus_addr =0 ; common->rd =NULL ; + common->frame =0 ;__gnttab_unmap_common (common); op->status = common->status;
Signature · 1 line
2.1.4
From 05a4454d13b98beda2bf76acd5aa4044010d7521 Mon Sep 17 00:00:00 2001
From: Jan Beulich <[email protected]>
Date: Fri, 2 Jun 2017 15:21:27 +0100
Subject: [PATCH 2/4] gnttab: never create host mapping unless asked to
We shouldn't create a host mapping unless asked to even in the case of mapping a granted MMIO page. In particular the mapping wouldn't be torn down when processing the matching unmap request.
This is part of XSA-224.
--- xen/common/grant_table.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
xen/common/grant_table.c +7 -4
@@ -911,10 +911,13 @@ __gnttab_map_grant_ref( goto undo_out; } - rc =create_grant_host_mapping ( - op->host_addr, frame, op->flags, cache_flags); -if ( rc != GNTST_okay ) - goto undo_out; +if ( op->flags & GNTMAP_host_map ) + { + rc =create_grant_host_mapping (op->host_addr, frame, op->flags, + cache_flags); +if ( rc != GNTST_okay ) + goto undo_out; + } }else if ( owner == rd || owner == dom_cow ) {
Signature · 1 line
2.1.4
From 686310db422808656a1e0a98d8f1d0d7c0746201 Mon Sep 17 00:00:00 2001
From: George Dunlap <[email protected]>
Date: Fri, 2 Jun 2017 15:21:27 +0100
Subject: [PATCH 3/4] gnttab: correct logic to get page references during map
requests
The rules for reference counting are somewhat complicated:
* Each of GNTTAB_host_map and GNTTAB_device_map need their own reference count
* If the mapping is writeable:
- GNTTAB_host_map needs a type count under only some conditions
- GNTTAB_device_map always needs a type count
If the mapping succeeds, we need to keep all of these; if the mapping fails, we need to release whatever references we have acquired so far.
Additionally, the code that does a lot of this calculation "inherits" a reference as part of the process of finding out who the owner is.
Finally, if the grant is mapped as writeable (without the GNTMAP_readonly flag), but the hypervisor cannot grab a PGT_writeable_page type, the entire operation should fail.
Unfortunately, the current code has several logic holes:
* If a grant is mapped only GNTTAB_device_map, and with a writeable mapping, but in conditions where a *host* type count is not necessary, the code will fail to grab the necessary type count.
* If a grant is mapped both GNTTAB_device_map and GNTTAB_host_map, with a writeable mapping, in conditions where the host type count is not necessary, *and* where the page cannot be changed to type PGT_writeable, the condition will not be detected.
In both cases, this means that on success, the type count will be erroneously reduced when the grant is unmapped. In the second case, the type count will be erroneously reduced on the failure path as well. (In the first case the failure path logic has the same hole as the reference grabbing logic.)
Additionally, the return value of get_page() is not checked; but this may fail even if the first get_page() succeeded due to a reference counting overflow.
First of all, simplify the restoration logic by explicitly counting the reference and type references acquired.
Consider each mapping type separately, explicitly marking the 'incoming' reference as used so we know when we need to grab a second one.
Finally, always check the return value of get_page[_type]() and go to the failure path if appropriate.
This is part of XSA-224.
--- xen/common/grant_table.c | 58 +++++++++++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 25 deletions(-)
xen/common/grant_table.c +33 -25
109 lines not on this page · raw message
Signature · 1 line
2.1.4
From 748aa3060753f876f221222994119dbf029ced60 Mon Sep 17 00:00:00 2001
From: Jan Beulich <[email protected]>
Date: Thu, 15 Jun 2017 16:25:27 +0100
Subject: [PATCH 4/4] gnttab: __gnttab_unmap_common_complete() is
all-or-nothing
All failures have to be detected in __gnttab_unmap_common(), the completion function must not skip part of its processing. In particular the GNTMAP_device_map related putting of page references and adjustment of pin count must not occur if __gnttab_unmap_common() signaled an error. Furthermore the function must not make adjustments to global state (here: clearing GNTTAB_device_map) before all possibly failing operations have been performed.
There's one exception for IOMMU related failures: As IOMMU manipulation occurs after GNTMAP_*_map have been cleared already, the related page reference and pin count adjustments need to be done nevertheless. A fundamental requirement for the correctness of this is that iommu_{,un}map_page() crash any affected DomU in case of failure.
The version check appears to be pointless (or could perhaps be a BUG_ON() or ASSERT()), but for the moment also move it.
This is part of XSA-224.
--- xen/common/grant_table.c | 106 ++++++++++++++++++-------------------- xen/include/asm-arm/grant_table.h | 2 +- xen/include/asm-x86/grant_table.h | 5 +- 3 files changed, 54 insertions(+), 59 deletions(-)
xen/common/grant_table.c +51 -55
235 lines not on this page · raw message
xen/include/asm-arm/grant_table.h +1 -1
@@ -9,7 +9,7 @@ void gnttab_clear_flag(unsigned long nr, uint16_t *addr);int create_grant_host_mapping (unsigned long gpaddr,unsigned long mfn,unsigned int flags,unsigned int cache_flags); -#define gnttab_host_mapping_get_page_type (op, d, rd) (0) +#define gnttab_host_mapping_get_page_type (ro, ld, rd) (0)int replace_grant_host_mapping (unsigned long gpaddr,unsigned long mfn,unsigned long new_gpaddr,unsigned int flags);void gnttab_mark_dirty (struct domain *d,unsigned long l);
xen/include/asm-x86/grant_table.h +2 -3
@@ -58,9 +58,8 @@ static inline void gnttab_clear_flag(unsigned int nr, uint16_t *st) }/* Foreign mappings of HHVM-guest pages do not modify the type count. */ -#define gnttab_host_mapping_get_page_type (op, ld, rd) \ - (!((op)->flags & GNTMAP_readonly) && \ - (((ld) == (rd)) || !paging_mode_external(rd))) +#define gnttab_host_mapping_get_page_type (ro, ld, rd) \ + (!(ro) && (((ld) == (rd)) || !paging_mode_external(rd)))/* Done implicitly when page tables are destroyed. */ #define gnttab_release_host_mappings (domain) ( paging_mode_external(domain) )
Signature · 1 line
2.1.4
From 0aa6bc3bba0aeec067feed2a7378d285d7529684 Mon Sep 17 00:00:00 2001
From: George Dunlap <[email protected]>
Date: Thu, 15 Jun 2017 16:24:02 +0100
Subject: [PATCH 1/4] gnttab: Fix handling of dev_bus_addr during unmap
If a grant has been mapped with the GNTTAB_device_map flag, calling grant_unmap_ref() with dev_bus_addr set to zero should cause the GNTTAB_device_map part of the mapping to be left alone.
Unfortunately, at the moment, op->dev_bus_addr is implicitly checked before clearing the map and adjusting the pin count, but only the bits above 12; and it is not checked at all before dropping page references. This means a guest can repeatedly make such a call to cause the reference count to drop to zero, causing the page to be freed and re-used, even though it's still mapped in its pagetables.
To fix this, always check op->dev_bus_addr explicitly for being non-zero, as well as op->flag & GNTMAP_device_map, before doing operations on the device_map.
While we're here, make the logic a bit cleaner:
* Always initialize op->frame to zero and set it from act->frame, to reduce the chance of untrusted input being used
* Explicitly check the full dev_bus_addr against act->frame << PAGE_SHIFT, rather than ignoring the lower 12 bits
This is part of XSA-224.
--- xen/common/grant_table.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-)
xen/common/grant_table.c +11 -12
@@ -899,8 +899,6 @@ __gnttab_unmap_common( ld = current->domain; lgt = ld->grant_table; - op->frame = (unsigned long )(op->dev_bus_addr >>PAGE_SHIFT ); -if (unlikely (op->handle >= lgt->maptrack_limit) ) {gdprintk (XENLOG_INFO ,"Bad handle (%d).\n" , op->handle);
@@ -957,16 +955,14 @@ __gnttab_unmap_common( op->ref = map->ref; act = &active_entry (rgt, map->ref); -if ( op->frame ==0 ) - { - op->frame = act->frame; - } -else + op->frame = act->frame; + +if ( op->dev_bus_addr ) { -if (unlikely (op->frame != act->frame) ) +if (unlikely (op->dev_bus_addr !=pfn_to_paddr (act->frame)) )PIN_FAIL (unmap_out, GNTST_general_error, -"Bad frame number doesn't match gntref. (%lx != %lx)\n" , - op->frame, act->frame); +"Bus address doesn't match gntref (%" PRIx64" != %" PRIpaddr")\n" , + op->dev_bus_addr,pfn_to_paddr (act->frame)); map->flags &= ~GNTMAP_device_map; }
@@ -1057,7 +1053,8 @@ __gnttab_unmap_common_complete(struct gnttab_unmap_common *op)else status = &status_entry (rgt, op->ref); -if (unlikely (op->frame != act->frame) ) +if ( op->dev_bus_addr && +unlikely (op->dev_bus_addr !=pfn_to_paddr (act->frame)) ) {/* * Suggests that __gntab_unmap_common failed early and so
@@ -1068,7 +1065,7 @@ __gnttab_unmap_common_complete(struct gnttab_unmap_common *op) pg =mfn_to_page (op->frame); -if ( op->flags & GNTMAP_device_map ) +if ( op->dev_bus_addr && (op->flags & GNTMAP_device_map) ) {if ( !is_iomem_page (act->frame) ) {
@@ -1136,6 +1133,7 @@ __gnttab_unmap_grant_ref(/* Intialise these in case common contains old state */ common->new_addr =0 ; common->rd =NULL ; + common->frame =0 ;__gnttab_unmap_common (common); op->status = common->status;
@@ -1200,6 +1198,7 @@ __gnttab_unmap_and_replace(/* Intialise these in case common contains old state */ common->dev_bus_addr =0 ; common->rd =NULL ; + common->frame =0 ;__gnttab_unmap_common (common); op->status = common->status;
Signature · 1 line
2.1.4
From 52078b99abbc534a5bda6f7d8ab2b403711a9bcf Mon Sep 17 00:00:00 2001
From: Jan Beulich <[email protected]>
Date: Fri, 2 Jun 2017 15:21:27 +0100
Subject: [PATCH 2/4] gnttab: never create host mapping unless asked to
We shouldn't create a host mapping unless asked to even in the case of mapping a granted MMIO page. In particular the mapping wouldn't be torn down when processing the matching unmap request.
This is part of XSA-224.
--- xen/common/grant_table.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
xen/common/grant_table.c +7 -4
@@ -736,10 +736,13 @@ __gnttab_map_grant_ref( goto undo_out; } - rc =create_grant_host_mapping ( - op->host_addr, frame, op->flags, cache_flags); -if ( rc != GNTST_okay ) - goto undo_out; +if ( op->flags & GNTMAP_host_map ) + { + rc =create_grant_host_mapping (op->host_addr, frame, op->flags, + cache_flags); +if ( rc != GNTST_okay ) + goto undo_out; + } }else if ( owner == rd || owner == dom_cow ) {
Signature · 1 line
2.1.4
From 5fd4726857a913e90de95623dc385f8856465839 Mon Sep 17 00:00:00 2001
From: George Dunlap <[email protected]>
Date: Fri, 2 Jun 2017 15:21:27 +0100
Subject: [PATCH 3/4] gnttab: correct logic to get page references during map
requests
The rules for reference counting are somewhat complicated:
* Each of GNTTAB_host_map and GNTTAB_device_map need their own reference count
* If the mapping is writeable:
- GNTTAB_host_map needs a type count under only some conditions
- GNTTAB_device_map always needs a type count
If the mapping succeeds, we need to keep all of these; if the mapping fails, we need to release whatever references we have acquired so far.
Additionally, the code that does a lot of this calculation "inherits" a reference as part of the process of finding out who the owner is.
Finally, if the grant is mapped as writeable (without the GNTMAP_readonly flag), but the hypervisor cannot grab a PGT_writeable_page type, the entire operation should fail.
Unfortunately, the current code has several logic holes:
* If a grant is mapped only GNTTAB_device_map, and with a writeable mapping, but in conditions where a *host* type count is not necessary, the code will fail to grab the necessary type count.
* If a grant is mapped both GNTTAB_device_map and GNTTAB_host_map, with a writeable mapping, in conditions where the host type count is not necessary, *and* where the page cannot be changed to type PGT_writeable, the condition will not be detected.
In both cases, this means that on success, the type count will be erroneously reduced when the grant is unmapped. In the second case, the type count will be erroneously reduced on the failure path as well. (In the first case the failure path logic has the same hole as the reference grabbing logic.)
Additionally, the return value of get_page() is not checked; but this may fail even if the first get_page() succeeded due to a reference counting overflow.
First of all, simplify the restoration logic by explicitly counting the reference and type references acquired.
Consider each mapping type separately, explicitly marking the 'incoming' reference as used so we know when we need to grab a second one.
Finally, always check the return value of get_page[_type]() and go to the failure path if appropriate.
This is part of XSA-224.
--- xen/common/grant_table.c | 58 +++++++++++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 25 deletions(-)
xen/common/grant_table.c +33 -25
109 lines not on this page · raw message
Signature · 1 line
2.1.4
From 414d97feb2e28ed131da33546ea81a919c30e285 Mon Sep 17 00:00:00 2001
From: Jan Beulich <[email protected]>
Date: Fri, 2 Jun 2017 15:51:58 +0100
Subject: [PATCH 4/4] gnttab: __gnttab_unmap_common_complete() is
all-or-nothing
All failures have to be detected in __gnttab_unmap_common(), the completion function must not skip part of its processing. In particular the GNTMAP_device_map related putting of page references and adjustment of pin count must not occur if __gnttab_unmap_common() signaled an error. Furthermore the function must not make adjustments to global state (here: clearing GNTTAB_device_map) before all possibly failing operations have been performed.
There's one exception for IOMMU related failures: As IOMMU manipulation occurs after GNTMAP_*_map have been cleared already, the related page reference and pin count adjustments need to be done nevertheless. A fundamental requirement for the correctness of this is that iommu_{,un}map_page() crash any affected DomU in case of failure.
The version check appears to be pointless (or could perhaps be a BUG_ON() or ASSERT()), but for the moment also move it.
This is part of XSA-224.
--- xen/common/grant_table.c | 105 ++++++++++++++++++-------------------- xen/include/asm-arm/grant_table.h | 2 +- xen/include/asm-x86/grant_table.h | 5 +- 3 files changed, 53 insertions(+), 59 deletions(-)
xen/common/grant_table.c +50 -55
220 lines not on this page · raw message
xen/include/asm-arm/grant_table.h +1 -1
@@ -10,7 +10,7 @@ void gnttab_clear_flag(unsigned long nr, uint16_t *addr);int create_grant_host_mapping (unsigned long gpaddr,unsigned long mfn,unsigned int flags,unsigned int cache_flags); -#define gnttab_host_mapping_get_page_type (op, d, rd) (0) +#define gnttab_host_mapping_get_page_type (ro, ld, rd) (0)int replace_grant_host_mapping (unsigned long gpaddr,unsigned long mfn,unsigned long new_gpaddr,unsigned int flags);void gnttab_mark_dirty (struct domain *d,unsigned long l);
xen/include/asm-x86/grant_table.h +2 -3
@@ -58,9 +58,8 @@ static inline void gnttab_clear_flag(unsigned int nr, uint16_t *st) }/* Foreign mappings of HHVM-guest pages do not modify the type count. */ -#define gnttab_host_mapping_get_page_type (op, ld, rd) \ - (!((op)->flags & GNTMAP_readonly) && \ - (((ld) == (rd)) || !paging_mode_external(rd))) +#define gnttab_host_mapping_get_page_type (ro, ld, rd) \ + (!(ro) && (((ld) == (rd)) || !paging_mode_external(rd)))/* Done implicitly when page tables are destroyed. */ #define gnttab_release_host_mappings (domain) ( paging_mode_external(domain) )
Signature · 1 line
2.1.4
From 62e73c9a3e11c6bffa18e20a97329ef7fb694635 Mon Sep 17 00:00:00 2001
From: George Dunlap <[email protected]>
Date: Thu, 15 Jun 2017 16:24:02 +0100
Subject: [PATCH 1/4] gnttab: Fix handling of dev_bus_addr during unmap
If a grant has been mapped with the GNTTAB_device_map flag, calling grant_unmap_ref() with dev_bus_addr set to zero should cause the GNTTAB_device_map part of the mapping to be left alone.
Unfortunately, at the moment, op->dev_bus_addr is implicitly checked before clearing the map and adjusting the pin count, but only the bits above 12; and it is not checked at all before dropping page references. This means a guest can repeatedly make such a call to cause the reference count to drop to zero, causing the page to be freed and re-used, even though it's still mapped in its pagetables.
To fix this, always check op->dev_bus_addr explicitly for being non-zero, as well as op->flag & GNTMAP_device_map, before doing operations on the device_map.
While we're here, make the logic a bit cleaner:
* Always initialize op->frame to zero and set it from act->frame, to reduce the chance of untrusted input being used
* Explicitly check the full dev_bus_addr against act->frame << PAGE_SHIFT, rather than ignoring the lower 12 bits
This is part of XSA-224.
--- xen/common/grant_table.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-)
xen/common/grant_table.c +11 -12
@@ -1076,8 +1076,6 @@ __gnttab_unmap_common( ld = current->domain; lgt = ld->grant_table; - op->frame = (unsigned long )(op->dev_bus_addr >>PAGE_SHIFT ); -if (unlikely (op->handle >= lgt->maptrack_limit) ) {gdprintk (XENLOG_INFO ,"Bad handle (%d).\n" , op->handle);
@@ -1161,16 +1159,14 @@ __gnttab_unmap_common( goto act_release_out; } -if ( op->frame ==0 ) - { - op->frame = act->frame; - } -else + op->frame = act->frame; + +if ( op->dev_bus_addr ) { -if (unlikely (op->frame != act->frame) ) +if (unlikely (op->dev_bus_addr !=pfn_to_paddr (act->frame)) )PIN_FAIL (act_release_out, GNTST_general_error, -"Bad frame number doesn't match gntref. (%lx != %lx)\n" , - op->frame, act->frame); +"Bus address doesn't match gntref (%" PRIx64" != %" PRIpaddr")\n" , + op->dev_bus_addr,pfn_to_paddr (act->frame)); map->flags &= ~GNTMAP_device_map; }
@@ -1263,7 +1259,8 @@ __gnttab_unmap_common_complete(struct gnttab_unmap_common *op)else status = &status_entry (rgt, op->ref); -if (unlikely (op->frame != act->frame) ) +if ( op->dev_bus_addr && +unlikely (op->dev_bus_addr !=pfn_to_paddr (act->frame)) ) {/* * Suggests that __gntab_unmap_common failed early and so
@@ -1274,7 +1271,7 @@ __gnttab_unmap_common_complete(struct gnttab_unmap_common *op) pg =mfn_to_page (op->frame); -if ( op->flags & GNTMAP_device_map ) +if ( op->dev_bus_addr && (op->flags & GNTMAP_device_map) ) {if ( !is_iomem_page (act->frame) ) {
@@ -1345,6 +1342,7 @@ __gnttab_unmap_grant_ref(/* Intialise these in case common contains old state */ common->new_addr =0 ; common->rd =NULL ; + common->frame =0 ;__gnttab_unmap_common (common); op->status = common->status;
@@ -1409,6 +1407,7 @@ __gnttab_unmap_and_replace(/* Intialise these in case common contains old state */ common->dev_bus_addr =0 ; common->rd =NULL ; + common->frame =0 ;__gnttab_unmap_common (common); op->status = common->status;
Signature · 1 line
2.1.4
From 82080ffca8cc95799b54e778923eaa20619ff961 Mon Sep 17 00:00:00 2001
From: Jan Beulich <[email protected]>
Date: Fri, 2 Jun 2017 15:21:27 +0100
Subject: [PATCH 2/4] gnttab: never create host mapping unless asked to
We shouldn't create a host mapping unless asked to even in the case of mapping a granted MMIO page. In particular the mapping wouldn't be torn down when processing the matching unmap request.
This is part of XSA-224.
--- xen/common/grant_table.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
xen/common/grant_table.c +7 -4
@@ -898,10 +898,13 @@ __gnttab_map_grant_ref( goto undo_out; } - rc =create_grant_host_mapping ( - op->host_addr, frame, op->flags, cache_flags); -if ( rc != GNTST_okay ) - goto undo_out; +if ( op->flags & GNTMAP_host_map ) + { + rc =create_grant_host_mapping (op->host_addr, frame, op->flags, + cache_flags); +if ( rc != GNTST_okay ) + goto undo_out; + } }else if ( owner == rd || owner == dom_cow ) {
Signature · 1 line
2.1.4
From 35b4cf7019e9c7631fbb462e5d907e5a3026a9c5 Mon Sep 17 00:00:00 2001
From: George Dunlap <[email protected]>
Date: Fri, 2 Jun 2017 15:21:27 +0100
Subject: [PATCH 3/4] gnttab: correct logic to get page references during map
requests
The rules for reference counting are somewhat complicated:
* Each of GNTTAB_host_map and GNTTAB_device_map need their own reference count
* If the mapping is writeable:
- GNTTAB_host_map needs a type count under only some conditions
- GNTTAB_device_map always needs a type count
If the mapping succeeds, we need to keep all of these; if the mapping fails, we need to release whatever references we have acquired so far.
Additionally, the code that does a lot of this calculation "inherits" a reference as part of the process of finding out who the owner is.
Finally, if the grant is mapped as writeable (without the GNTMAP_readonly flag), but the hypervisor cannot grab a PGT_writeable_page type, the entire operation should fail.
Unfortunately, the current code has several logic holes:
* If a grant is mapped only GNTTAB_device_map, and with a writeable mapping, but in conditions where a *host* type count is not necessary, the code will fail to grab the necessary type count.
* If a grant is mapped both GNTTAB_device_map and GNTTAB_host_map, with a writeable mapping, in conditions where the host type count is not necessary, *and* where the page cannot be changed to type PGT_writeable, the condition will not be detected.
In both cases, this means that on success, the type count will be erroneously reduced when the grant is unmapped. In the second case, the type count will be erroneously reduced on the failure path as well. (In the first case the failure path logic has the same hole as the reference grabbing logic.)
Additionally, the return value of get_page() is not checked; but this may fail even if the first get_page() succeeded due to a reference counting overflow.
First of all, simplify the restoration logic by explicitly counting the reference and type references acquired.
Consider each mapping type separately, explicitly marking the 'incoming' reference as used so we know when we need to grab a second one.
Finally, always check the return value of get_page[_type]() and go to the failure path if appropriate.
This is part of XSA-224.
--- xen/common/grant_table.c | 58 +++++++++++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 25 deletions(-)
xen/common/grant_table.c +33 -25
109 lines not on this page · raw message
Signature · 1 line
2.1.4
From 804036f102d6933ce05a4457fd15ecffea43d33b Mon Sep 17 00:00:00 2001
From: Jan Beulich <[email protected]>
Date: Thu, 15 Jun 2017 16:25:27 +0100
Subject: [PATCH 4/4] gnttab: __gnttab_unmap_common_complete() is
all-or-nothing
All failures have to be detected in __gnttab_unmap_common(), the completion function must not skip part of its processing. In particular the GNTMAP_device_map related putting of page references and adjustment of pin count must not occur if __gnttab_unmap_common() signaled an error. Furthermore the function must not make adjustments to global state (here: clearing GNTTAB_device_map) before all possibly failing operations have been performed.
There's one exception for IOMMU related failures: As IOMMU manipulation occurs after GNTMAP_*_map have been cleared already, the related page reference and pin count adjustments need to be done nevertheless. A fundamental requirement for the correctness of this is that iommu_{,un}map_page() crash any affected DomU in case of failure.
The version check appears to be pointless (or could perhaps be a BUG_ON() or ASSERT()), but for the moment also move it.
This is part of XSA-224.
--- xen/common/grant_table.c | 108 ++++++++++++++++++-------------------- xen/include/asm-arm/grant_table.h | 2 +- xen/include/asm-x86/grant_table.h | 5 +- 3 files changed, 55 insertions(+), 60 deletions(-)
xen/common/grant_table.c +52 -56
236 lines not on this page · raw message
xen/include/asm-arm/grant_table.h +1 -1
@@ -9,7 +9,7 @@ void gnttab_clear_flag(unsigned long nr, uint16_t *addr);int create_grant_host_mapping (unsigned long gpaddr,unsigned long mfn,unsigned int flags,unsigned int cache_flags); -#define gnttab_host_mapping_get_page_type (op, d, rd) (0) +#define gnttab_host_mapping_get_page_type (ro, ld, rd) (0)int replace_grant_host_mapping (unsigned long gpaddr,unsigned long mfn,unsigned long new_gpaddr,unsigned int flags);void gnttab_mark_dirty (struct domain *d,unsigned long l);
xen/include/asm-x86/grant_table.h +2 -3
@@ -58,9 +58,8 @@ static inline void gnttab_clear_flag(unsigned int nr, uint16_t *st) }/* Foreign mappings of HHVM-guest pages do not modify the type count. */ -#define gnttab_host_mapping_get_page_type (op, ld, rd) \ - (!((op)->flags & GNTMAP_readonly) && \ - (((ld) == (rd)) || !paging_mode_external(rd))) +#define gnttab_host_mapping_get_page_type (ro, ld, rd) \ + (!(ro) && (((ld) == (rd)) || !paging_mode_external(rd)))/* Done implicitly when page tables are destroyed. */ #define gnttab_release_host_mappings (domain) ( paging_mode_external(domain) )
Signature · 1 line
2.1.4
From fd97f5f5ba9375163c8d8771fe551bb4a6423b36 Mon Sep 17 00:00:00 2001
From: George Dunlap <[email protected]>
Date: Thu, 15 Jun 2017 16:24:02 +0100
Subject: [PATCH 1/4] gnttab: Fix handling of dev_bus_addr during unmap
If a grant has been mapped with the GNTTAB_device_map flag, calling grant_unmap_ref() with dev_bus_addr set to zero should cause the GNTTAB_device_map part of the mapping to be left alone.
Unfortunately, at the moment, op->dev_bus_addr is implicitly checked before clearing the map and adjusting the pin count, but only the bits above 12; and it is not checked at all before dropping page references. This means a guest can repeatedly make such a call to cause the reference count to drop to zero, causing the page to be freed and re-used, even though it's still mapped in its pagetables.
To fix this, always check op->dev_bus_addr explicitly for being non-zero, as well as op->flag & GNTMAP_device_map, before doing operations on the device_map.
While we're here, make the logic a bit cleaner:
* Always initialize op->frame to zero and set it from act->frame, to reduce the chance of untrusted input being used
* Explicitly check the full dev_bus_addr against act->frame << PAGE_SHIFT, rather than ignoring the lower 12 bits
This is part of XSA-224.
--- xen/common/grant_table.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-)
xen/common/grant_table.c +11 -12
@@ -1089,8 +1089,6 @@ __gnttab_unmap_common( ld = current->domain; lgt = ld->grant_table; - op->frame = (unsigned long )(op->dev_bus_addr >>PAGE_SHIFT ); -if (unlikely (op->handle >= lgt->maptrack_limit) ) {gdprintk (XENLOG_INFO ,"Bad handle (%d).\n" , op->handle);
@@ -1174,16 +1172,14 @@ __gnttab_unmap_common( goto act_release_out; } -if ( op->frame ==0 ) - { - op->frame = act->frame; - } -else + op->frame = act->frame; + +if ( op->dev_bus_addr ) { -if (unlikely (op->frame != act->frame) ) +if (unlikely (op->dev_bus_addr !=pfn_to_paddr (act->frame)) )PIN_FAIL (act_release_out, GNTST_general_error, -"Bad frame number doesn't match gntref. (%lx != %lx)\n" , - op->frame, act->frame); +"Bus address doesn't match gntref (%" PRIx64" != %" PRIpaddr")\n" , + op->dev_bus_addr,pfn_to_paddr (act->frame)); map->flags &= ~GNTMAP_device_map; }
@@ -1276,7 +1272,8 @@ __gnttab_unmap_common_complete(struct gnttab_unmap_common *op)else status = &status_entry (rgt, op->ref); -if (unlikely (op->frame != act->frame) ) +if ( op->dev_bus_addr && +unlikely (op->dev_bus_addr !=pfn_to_paddr (act->frame)) ) {/* * Suggests that __gntab_unmap_common failed early and so
@@ -1287,7 +1284,7 @@ __gnttab_unmap_common_complete(struct gnttab_unmap_common *op) pg =mfn_to_page (op->frame); -if ( op->flags & GNTMAP_device_map ) +if ( op->dev_bus_addr && (op->flags & GNTMAP_device_map) ) {if ( !is_iomem_page (act->frame) ) {
@@ -1358,6 +1355,7 @@ __gnttab_unmap_grant_ref(/* Intialise these in case common contains old state */ common->new_addr =0 ; common->rd =NULL ; + common->frame =0 ;__gnttab_unmap_common (common); op->status = common->status;
@@ -1422,6 +1420,7 @@ __gnttab_unmap_and_replace(/* Intialise these in case common contains old state */ common->dev_bus_addr =0 ; common->rd =NULL ; + common->frame =0 ;__gnttab_unmap_common (common); op->status = common->status;
Signature · 1 line
2.1.4
From 8894a0c20d920aada305aade0591c1e77167b1db Mon Sep 17 00:00:00 2001
From: Jan Beulich <[email protected]>
Date: Fri, 2 Jun 2017 15:21:27 +0100
Subject: [PATCH 2/4] gnttab: never create host mapping unless asked to
We shouldn't create a host mapping unless asked to even in the case of mapping a granted MMIO page. In particular the mapping wouldn't be torn down when processing the matching unmap request.
This is part of XSA-224.
--- xen/common/grant_table.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
xen/common/grant_table.c +7 -4
@@ -911,10 +911,13 @@ __gnttab_map_grant_ref( goto undo_out; } - rc =create_grant_host_mapping ( - op->host_addr, frame, op->flags, cache_flags); -if ( rc != GNTST_okay ) - goto undo_out; +if ( op->flags & GNTMAP_host_map ) + { + rc =create_grant_host_mapping (op->host_addr, frame, op->flags, + cache_flags); +if ( rc != GNTST_okay ) + goto undo_out; + } }else if ( owner == rd || owner == dom_cow ) {
Signature · 1 line
2.1.4
From 5d491e3cf32ff03552db9d66e842964fec06dcd4 Mon Sep 17 00:00:00 2001
From: George Dunlap <[email protected]>
Date: Fri, 2 Jun 2017 15:21:27 +0100
Subject: [PATCH 3/4] gnttab: correct logic to get page references during map
requests
The rules for reference counting are somewhat complicated:
* Each of GNTTAB_host_map and GNTTAB_device_map need their own reference count
* If the mapping is writeable:
- GNTTAB_host_map needs a type count under only some conditions
- GNTTAB_device_map always needs a type count
If the mapping succeeds, we need to keep all of these; if the mapping fails, we need to release whatever references we have acquired so far.
Additionally, the code that does a lot of this calculation "inherits" a reference as part of the process of finding out who the owner is.
Finally, if the grant is mapped as writeable (without the GNTMAP_readonly flag), but the hypervisor cannot grab a PGT_writeable_page type, the entire operation should fail.
Unfortunately, the current code has several logic holes:
* If a grant is mapped only GNTTAB_device_map, and with a writeable mapping, but in conditions where a *host* type count is not necessary, the code will fail to grab the necessary type count.
* If a grant is mapped both GNTTAB_device_map and GNTTAB_host_map, with a writeable mapping, in conditions where the host type count is not necessary, *and* where the page cannot be changed to type PGT_writeable, the condition will not be detected.
In both cases, this means that on success, the type count will be erroneously reduced when the grant is unmapped. In the second case, the type count will be erroneously reduced on the failure path as well. (In the first case the failure path logic has the same hole as the reference grabbing logic.)
Additionally, the return value of get_page() is not checked; but this may fail even if the first get_page() succeeded due to a reference counting overflow.
First of all, simplify the restoration logic by explicitly counting the reference and type references acquired.
Consider each mapping type separately, explicitly marking the 'incoming' reference as used so we know when we need to grab a second one.
Finally, always check the return value of get_page[_type]() and go to the failure path if appropriate.
This is part of XSA-224.
--- xen/common/grant_table.c | 58 +++++++++++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 25 deletions(-)
xen/common/grant_table.c +33 -25
109 lines not on this page · raw message
Signature · 1 line
2.1.4
From 3ad26b95cd9bacedad5ba503515cf6e618162be1 Mon Sep 17 00:00:00 2001
From: Jan Beulich <[email protected]>
Date: Thu, 15 Jun 2017 16:25:27 +0100
Subject: [PATCH 4/4] gnttab: __gnttab_unmap_common_complete() is
all-or-nothing
All failures have to be detected in __gnttab_unmap_common(), the completion function must not skip part of its processing. In particular the GNTMAP_device_map related putting of page references and adjustment of pin count must not occur if __gnttab_unmap_common() signaled an error. Furthermore the function must not make adjustments to global state (here: clearing GNTTAB_device_map) before all possibly failing operations have been performed.
There's one exception for IOMMU related failures: As IOMMU manipulation occurs after GNTMAP_*_map have been cleared already, the related page reference and pin count adjustments need to be done nevertheless. A fundamental requirement for the correctness of this is that iommu_{,un}map_page() crash any affected DomU in case of failure.
The version check appears to be pointless (or could perhaps be a BUG_ON() or ASSERT()), but for the moment also move it.
This is part of XSA-224.
--- xen/common/grant_table.c | 108 ++++++++++++++++++-------------------- xen/include/asm-arm/grant_table.h | 2 +- xen/include/asm-x86/grant_table.h | 5 +- 3 files changed, 55 insertions(+), 60 deletions(-)
xen/common/grant_table.c +52 -56
236 lines not on this page · raw message
xen/include/asm-arm/grant_table.h +1 -1
@@ -9,7 +9,7 @@ void gnttab_clear_flag(unsigned long nr, uint16_t *addr);int create_grant_host_mapping (unsigned long gpaddr,unsigned long mfn,unsigned int flags,unsigned int cache_flags); -#define gnttab_host_mapping_get_page_type (op, d, rd) (0) +#define gnttab_host_mapping_get_page_type (ro, ld, rd) (0)int replace_grant_host_mapping (unsigned long gpaddr,unsigned long mfn,unsigned long new_gpaddr,unsigned int flags);void gnttab_mark_dirty (struct domain *d,unsigned long l);
xen/include/asm-x86/grant_table.h +2 -3
@@ -58,9 +58,8 @@ static inline void gnttab_clear_flag(unsigned int nr, uint16_t *st) }/* Foreign mappings of HHVM-guest pages do not modify the type count. */ -#define gnttab_host_mapping_get_page_type (op, ld, rd) \ - (!((op)->flags & GNTMAP_readonly) && \ - (((ld) == (rd)) || !paging_mode_external(rd))) +#define gnttab_host_mapping_get_page_type (ro, ld, rd) \ + (!(ro) && (((ld) == (rd)) || !paging_mode_external(rd)))/* Done implicitly when page tables are destroyed. */ #define gnttab_release_host_mappings (domain) ( paging_mode_external(domain) )
Signature · 1 line
2.1.4
From 9808ed0b1ebc3a5d2aa08a9ff91fcf3ecb42bc9f Mon Sep 17 00:00:00 2001
From: George Dunlap <[email protected]>
Date: Thu, 15 Jun 2017 16:24:02 +0100
Subject: [PATCH 1/4] gnttab: Fix handling of dev_bus_addr during unmap
If a grant has been mapped with the GNTTAB_device_map flag, calling grant_unmap_ref() with dev_bus_addr set to zero should cause the GNTTAB_device_map part of the mapping to be left alone.
Unfortunately, at the moment, op->dev_bus_addr is implicitly checked before clearing the map and adjusting the pin count, but only the bits above 12; and it is not checked at all before dropping page references. This means a guest can repeatedly make such a call to cause the reference count to drop to zero, causing the page to be freed and re-used, even though it's still mapped in its pagetables.
To fix this, always check op->dev_bus_addr explicitly for being non-zero, as well as op->flag & GNTMAP_device_map, before doing operations on the device_map.
While we're here, make the logic a bit cleaner:
* Always initialize op->frame to zero and set it from act->frame, to reduce the chance of untrusted input being used
* Explicitly check the full dev_bus_addr against act->frame << PAGE_SHIFT, rather than ignoring the lower 12 bits
This is part of XSA-224.
--- xen/common/grant_table.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-)
xen/common/grant_table.c +11 -12
@@ -1085,8 +1085,6 @@ __gnttab_unmap_common( ld = current->domain; lgt = ld->grant_table; - op->frame = (unsigned long )(op->dev_bus_addr >>PAGE_SHIFT ); -if (unlikely (op->handle >= lgt->maptrack_limit) ) {gdprintk (XENLOG_INFO ,"Bad handle (%d).\n" , op->handle);
@@ -1169,16 +1167,14 @@ __gnttab_unmap_common( goto act_release_out; } -if ( op->frame ==0 ) - { - op->frame = act->frame; - } -else + op->frame = act->frame; + +if ( op->dev_bus_addr ) { -if (unlikely (op->frame != act->frame) ) +if (unlikely (op->dev_bus_addr !=pfn_to_paddr (act->frame)) )PIN_FAIL (act_release_out, GNTST_general_error, -"Bad frame number doesn't match gntref. (%lx != %lx)\n" , - op->frame, act->frame); +"Bus address doesn't match gntref (%" PRIx64" != %" PRIpaddr")\n" , + op->dev_bus_addr,pfn_to_paddr (act->frame)); map->flags &= ~GNTMAP_device_map; }
@@ -1271,7 +1267,8 @@ __gnttab_unmap_common_complete(struct gnttab_unmap_common *op)else status = &status_entry (rgt, op->ref); -if (unlikely (op->frame != act->frame) ) +if ( op->dev_bus_addr && +unlikely (op->dev_bus_addr !=pfn_to_paddr (act->frame)) ) {/* * Suggests that __gntab_unmap_common failed early and so
@@ -1282,7 +1279,7 @@ __gnttab_unmap_common_complete(struct gnttab_unmap_common *op) pg =mfn_to_page (op->frame); -if ( op->flags & GNTMAP_device_map ) +if ( op->dev_bus_addr && (op->flags & GNTMAP_device_map) ) {if ( !is_iomem_page (act->frame) ) {
@@ -1353,6 +1350,7 @@ __gnttab_unmap_grant_ref(/* Intialise these in case common contains old state */ common->new_addr =0 ; common->rd =NULL ; + common->frame =0 ;__gnttab_unmap_common (common); op->status = common->status;
@@ -1417,6 +1415,7 @@ __gnttab_unmap_and_replace(/* Intialise these in case common contains old state */ common->dev_bus_addr =0 ; common->rd =NULL ; + common->frame =0 ;__gnttab_unmap_common (common); op->status = common->status;
Signature · 1 line
2.1.4
From 2d6357522946bd5a105066db8079e5dd46cb3047 Mon Sep 17 00:00:00 2001
From: Jan Beulich <[email protected]>
Date: Fri, 2 Jun 2017 15:21:27 +0100
Subject: [PATCH 2/4] gnttab: never create host mapping unless asked to
We shouldn't create a host mapping unless asked to even in the case of mapping a granted MMIO page. In particular the mapping wouldn't be torn down when processing the matching unmap request.
This is part of XSA-224.
--- xen/common/grant_table.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
xen/common/grant_table.c +7 -4
@@ -907,10 +907,13 @@ __gnttab_map_grant_ref( goto undo_out; } - rc =create_grant_host_mapping ( - op->host_addr, frame, op->flags, cache_flags); -if ( rc != GNTST_okay ) - goto undo_out; +if ( op->flags & GNTMAP_host_map ) + { + rc =create_grant_host_mapping (op->host_addr, frame, op->flags, + cache_flags); +if ( rc != GNTST_okay ) + goto undo_out; + } }else if ( owner == rd || owner == dom_cow ) {
Signature · 1 line
2.1.4
From 4e718be6f59526927d5cd31ecd80c5c758dca3f5 Mon Sep 17 00:00:00 2001
From: George Dunlap <[email protected]>
Date: Fri, 2 Jun 2017 15:21:27 +0100
Subject: [PATCH 3/4] gnttab: correct logic to get page references during map
requests
The rules for reference counting are somewhat complicated:
* Each of GNTTAB_host_map and GNTTAB_device_map need their own reference count
* If the mapping is writeable:
- GNTTAB_host_map needs a type count under only some conditions
- GNTTAB_device_map always needs a type count
If the mapping succeeds, we need to keep all of these; if the mapping fails, we need to release whatever references we have acquired so far.
Additionally, the code that does a lot of this calculation "inherits" a reference as part of the process of finding out who the owner is.
Finally, if the grant is mapped as writeable (without the GNTMAP_readonly flag), but the hypervisor cannot grab a PGT_writeable_page type, the entire operation should fail.
Unfortunately, the current code has several logic holes:
* If a grant is mapped only GNTTAB_device_map, and with a writeable mapping, but in conditions where a *host* type count is not necessary, the code will fail to grab the necessary type count.
* If a grant is mapped both GNTTAB_device_map and GNTTAB_host_map, with a writeable mapping, in conditions where the host type count is not necessary, *and* where the page cannot be changed to type PGT_writeable, the condition will not be detected.
In both cases, this means that on success, the type count will be erroneously reduced when the grant is unmapped. In the second case, the type count will be erroneously reduced on the failure path as well. (In the first case the failure path logic has the same hole as the reference grabbing logic.)
Additionally, the return value of get_page() is not checked; but this may fail even if the first get_page() succeeded due to a reference counting overflow.
First of all, simplify the restoration logic by explicitly counting the reference and type references acquired.
Consider each mapping type separately, explicitly marking the 'incoming' reference as used so we know when we need to grab a second one.
Finally, always check the return value of get_page[_type]() and go to the failure path if appropriate.
This is part of XSA-224.
--- xen/common/grant_table.c | 58 +++++++++++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 25 deletions(-)
xen/common/grant_table.c +33 -25
109 lines not on this page · raw message
Signature · 1 line
2.1.4
From d27237abe37e45a1f245e23484062b09ff3477ed Mon Sep 17 00:00:00 2001
From: Jan Beulich <[email protected]>
Date: Thu, 15 Jun 2017 16:25:27 +0100
Subject: [PATCH 4/4] gnttab: __gnttab_unmap_common_complete() is
all-or-nothing
All failures have to be detected in __gnttab_unmap_common(), the completion function must not skip part of its processing. In particular the GNTMAP_device_map related putting of page references and adjustment of pin count must not occur if __gnttab_unmap_common() signaled an error. Furthermore the function must not make adjustments to global state (here: clearing GNTTAB_device_map) before all possibly failing operations have been performed.
There's one exception for IOMMU related failures: As IOMMU manipulation occurs after GNTMAP_*_map have been cleared already, the related page reference and pin count adjustments need to be done nevertheless. A fundamental requirement for the correctness of this is that iommu_{,un}map_page() crash any affected DomU in case of failure.
The version check appears to be pointless (or could perhaps be a BUG_ON() or ASSERT()), but for the moment also move it.
This is part of XSA-224.
--- xen/common/grant_table.c | 108 ++++++++++++++++++-------------------- xen/include/asm-arm/grant_table.h | 2 +- xen/include/asm-x86/grant_table.h | 5 +- 3 files changed, 55 insertions(+), 60 deletions(-)
xen/common/grant_table.c +52 -56
236 lines not on this page · raw message
xen/include/asm-arm/grant_table.h +1 -1
@@ -9,7 +9,7 @@ void gnttab_clear_flag(unsigned long nr, uint16_t *addr);int create_grant_host_mapping (unsigned long gpaddr,unsigned long mfn,unsigned int flags,unsigned int cache_flags); -#define gnttab_host_mapping_get_page_type (op, d, rd) (0) +#define gnttab_host_mapping_get_page_type (ro, ld, rd) (0)int replace_grant_host_mapping (unsigned long gpaddr,unsigned long mfn,unsigned long new_gpaddr,unsigned int flags);void gnttab_mark_dirty (struct domain *d,unsigned long l);
xen/include/asm-x86/grant_table.h +2 -3
@@ -58,9 +58,8 @@ static inline void gnttab_clear_flag(unsigned int nr, uint16_t *st) }/* Foreign mappings of HHVM-guest pages do not modify the type count. */ -#define gnttab_host_mapping_get_page_type (op, ld, rd) \ - (!((op)->flags & GNTMAP_readonly) && \ - (((ld) == (rd)) || !paging_mode_external(rd))) +#define gnttab_host_mapping_get_page_type (ro, ld, rd) \ + (!(ro) && (((ld) == (rd)) || !paging_mode_external(rd)))/* Done implicitly when page tables are destroyed. */ #define gnttab_release_host_mappings (domain) ( paging_mode_external(domain) )
Signature · 1 line
2.1.4
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel