Qubes Security Bulletin #27: Xen 64-bit bit test instruction emulation broken (XSA 195)

in #utopian6 years ago

---===[ Qubes Security Bulletin #27 ]===---

<pre><code> November 22, 2016 Xen 64-bit bit test instruction emulation broken (XSA 195) <h1>Quick Summary <p dir="auto">The Xen Security Team has discovered a serious security bug (XSA 195)<br /> in the hypervisor instruction emulation code which might potentially<br /> lead to privilege escalation [1]: <p dir="auto">| When Xen needs to emulate [BT, or BT{C,R,S}] instruction (...) the<br /> | memory address and register operand are recalculated internally to<br /> | Xen. In this process, the high bits of an intermediate expression<br /> | were discarded, leading to both the memory location and the register<br /> | operand being wrong.<br /> |<br /> | The wrong memory location would have only a guest local effect<br /> | (either access to an unintended location, or a fault delivered to<br /> | the guest), whereas the wrong register value could lead to either a<br /> | host crash or an unintended host memory access. <p dir="auto">There have been also a few other Xen-related bugs that are being<br /> disclosed today by the Xen team [2], most likely with none or only<br /> minor impacts on Qubes OS (e.g. information leaks from the hypervisor<br /> memory, which might ease exploitation of other potential bugs, but<br /> unlikely to reveal any information from other VMs). <h1>Description of the bug <p dir="auto">The x86_emulate() function, which is internally used by the hypervisor<br /> to emulate guest instructions in some situations, contains an<br /> arithmetic bug resulting from implicit type conversions between 64-bit<br /> and 32-bit integers: <pre><code>byte_offset = op_bytes + (((-src.val-1) >> 3) & ~(op_bytes-1)); ea.mem.off += (src.val >> 3) & ~(op_bytes - 1); <p dir="auto">The '1' immediate above should have been suffixed by 'L', letting the<br /> compiler know it should not be clamped to 32-bit, which in turn causes<br /> the above expressions to incorrectly allow for huge offsets when<br /> emulating the BT* instructions. <p dir="auto">A malicious VM can trigger a condition in which: 1) the execution of<br /> the BT* instruction will be intercepted and emulated by the<br /> hypervisor, and 2) the calculated offset will fall far beyond the page<br /> mapped by the hypervisor for the purpose of emulating the instruction<br /> (the hypervisor needs to map a page from the guest to emulate the BT<br /> instruction when the destination operand is a memory address). This<br /> will likely lead to the host crash, but a possibility of exploiting<br /> this bug for privilege escalation cannot be excluded. <p dir="auto">The Xen Security team has pointed out in a discussion with us that a<br /> reliable exploitation will likely be complicated by the uncertainty of<br /> the Xen stack absolute address, as the emulated opcodes will use<br /> stack-relative addressing, which, among other things, would also<br /> depend on which VCPU the attacker's VM happens to be currently<br /> scheduled on. <h1>Impact <p dir="auto">In theory this is a critical bug allowing full privilege escalation<br /> from a gust to the hypervisor, but for the reasons described above<br /> reliable exploitation might not be trivial. Nevertheless, one should<br /> never underestimate creativity of exploit authors, and thus should<br /> assume the bug is exploitable and patch immediately, just to stay on<br /> the safe side. <h1>More discussion <p dir="auto">This is the second serious bug in Xen's x86_emulate() that has<br /> affected Qubes OS. The first such bug has been described in QSB18<br /> (March 2015). <p dir="auto">It's tempting to think that instruction emulation (on x86) might be<br /> required only for PV guests and could be ditched for HVM guests,<br /> specifically those using SLAT (HAP in Xen parlance). Sadly this<br /> doesn't seem to be feasible. The main culprit, as pointed out by Xen<br /> developers, is the MMIO virtualization. And while it might be possible<br /> to run some HVM guests without the need for instruction emulator, this<br /> doesn't seem feasible for the VMs with assigned PCI devices. This is<br /> rather unfortunate, especially for Qubes OS, as the VMs with PCI<br /> devices assigned, such as the USB or Network VMs, are often considered<br /> untrusted. <p dir="auto">Apparently the x86 architecture cannot escape its clumsiness despite<br /> all the efforts by Intel to add new technologies on top of it. <p dir="auto">Another solution might be to attempt to isolate the x86 instruction<br /> emulator, similarly to how the I/O emulator is already isolated in a<br /> dedicated stub domain on Qubes OS (thanks to Xen stub domains). The<br /> Xen Security Team pointed out, however, that this has already been<br /> attempted in the past, but the effort was ultimately gave up on, due<br /> to apparently an increase of the interface complexity required to make<br /> that work. It's unclear what the performance impact of such a change<br /> might be. <p dir="auto">On the other hand though some other, arguably more experimental,<br /> hypervisors, such as Nova [3], do isolate instruction-emulating code,<br /> together with most of the VMM functionality, away from the<br /> (micro)hypervisor. Again, it's unclear to us what the performance (or<br /> other) impact might be of that solution, but it surely looks<br /> interesting from the security point of view (at least considering x86<br /> platform). <p dir="auto">On the positive side though, the bug has been found by the Xen<br /> Security Team during the internal effort for improving critical code<br /> security, and the team promises even more efforts in the near future<br /> to further improve the hypervisor code quality. <p dir="auto">We shall stress one more time that the instruction emulator<br /> (implemented by the x86_emulate() function on Xen) discussed in this<br /> bulletin should not be confused with the I/O device emulator<br /> (typically implemented by the qemu process on many virtualization<br /> systems). The latter is already well isolated on Qubes OS, thanks to<br /> Xen's stub domain features. <h1>Patching <p dir="auto">The specific packages that resolve the problem discussed in this<br /> bulletin (and also brining a few other reliability patches to Xen)<br /> have been uploaded to the security-testing repository: <p dir="auto">For Qubes 3.1 and 3.2: <ul> <li>xen packages, version 4.6.3-24 <p dir="auto">The packages are to be installed in Dom0 via qubes-dom0-update command<br /> or via the Qubes graphical manager. <p dir="auto">A system restart will be required afterwards. <p dir="auto">If you use Anti Evil Maid, you will need to reseal your secret<br /> passphrase to new PCR values, as PCR18 will change because of a new<br /> xen.gz binary. <p dir="auto">These packages will be moved to the current repository over the coming<br /> days once they receive more testing from the community. <h1>Credits <p dir="auto">This bugs has been found by Xen Security Team, and reported via Xen<br /> Security mailing list. <p dir="auto">Additionally, we would like to thank members of the Xen Security Team<br /> for discussing with us the details of the bugs as well as sharing<br /> their thoughts on possible future workarounds. <p dir="auto">We also would like to thank Norman Feske of Genode Labs for explaining<br /> to us how does the Nova hypervisor handle x86 instruction emulation. <h1>References <p dir="auto"><br /><br /><span>[1] <a href="http://xenbits.xen.org/xsa/advisory-195.html" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">http://xenbits.xen.org/xsa/advisory-195.html<span> [2] <a href="http://xenbits.xen.org/xsa/" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">http://xenbits.xen.org/xsa/<span> [3] <a href="http://hypervisor.org/" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">http://hypervisor.org/ <p dir="auto">--<br /> The Qubes Security Team<br /><span> <a href="https://www.qubes-os.org/security/" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">https://www.qubes-os.org/security/