security.rst 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. Security
  2. ========
  3. Overview
  4. --------
  5. This chapter explains the security requirements that QEMU is designed to meet
  6. and principles for securely deploying QEMU.
  7. Security Requirements
  8. ---------------------
  9. QEMU supports many different use cases, some of which have stricter security
  10. requirements than others. The community has agreed on the overall security
  11. requirements that users may depend on. These requirements define what is
  12. considered supported from a security perspective.
  13. Virtualization Use Case
  14. '''''''''''''''''''''''
  15. The virtualization use case covers cloud and virtual private server (VPS)
  16. hosting, as well as traditional data center and desktop virtualization. These
  17. use cases rely on hardware virtualization extensions to execute guest code
  18. safely on the physical CPU at close-to-native speed.
  19. The following entities are untrusted, meaning that they may be buggy or
  20. malicious:
  21. - Guest
  22. - User-facing interfaces (e.g. VNC, SPICE, WebSocket)
  23. - Network protocols (e.g. NBD, live migration)
  24. - User-supplied files (e.g. disk images, kernels, device trees)
  25. - Passthrough devices (e.g. PCI, USB)
  26. Bugs affecting these entities are evaluated on whether they can cause damage in
  27. real-world use cases and treated as security bugs if this is the case.
  28. Non-virtualization Use Case
  29. '''''''''''''''''''''''''''
  30. The non-virtualization use case covers emulation using the Tiny Code Generator
  31. (TCG). In principle the TCG and device emulation code used in conjunction with
  32. the non-virtualization use case should meet the same security requirements as
  33. the virtualization use case. However, for historical reasons much of the
  34. non-virtualization use case code was not written with these security
  35. requirements in mind.
  36. Bugs affecting the non-virtualization use case are not considered security
  37. bugs at this time. Users with non-virtualization use cases must not rely on
  38. QEMU to provide guest isolation or any security guarantees.
  39. Architecture
  40. ------------
  41. This section describes the design principles that ensure the security
  42. requirements are met.
  43. Guest Isolation
  44. '''''''''''''''
  45. Guest isolation is the confinement of guest code to the virtual machine. When
  46. guest code gains control of execution on the host this is called escaping the
  47. virtual machine. Isolation also includes resource limits such as throttling of
  48. CPU, memory, disk, or network. Guests must be unable to exceed their resource
  49. limits.
  50. QEMU presents an attack surface to the guest in the form of emulated devices.
  51. The guest must not be able to gain control of QEMU. Bugs in emulated devices
  52. could allow malicious guests to gain code execution in QEMU. At this point the
  53. guest has escaped the virtual machine and is able to act in the context of the
  54. QEMU process on the host.
  55. Guests often interact with other guests and share resources with them. A
  56. malicious guest must not gain control of other guests or access their data.
  57. Disk image files and network traffic must be protected from other guests unless
  58. explicitly shared between them by the user.
  59. Principle of Least Privilege
  60. ''''''''''''''''''''''''''''
  61. The principle of least privilege states that each component only has access to
  62. the privileges necessary for its function. In the case of QEMU this means that
  63. each process only has access to resources belonging to the guest.
  64. The QEMU process should not have access to any resources that are inaccessible
  65. to the guest. This way the guest does not gain anything by escaping into the
  66. QEMU process since it already has access to those same resources from within
  67. the guest.
  68. Following the principle of least privilege immediately fulfills guest isolation
  69. requirements. For example, guest A only has access to its own disk image file
  70. ``a.img`` and not guest B's disk image file ``b.img``.
  71. In reality certain resources are inaccessible to the guest but must be
  72. available to QEMU to perform its function. For example, host system calls are
  73. necessary for QEMU but are not exposed to guests. A guest that escapes into
  74. the QEMU process can then begin invoking host system calls.
  75. New features must be designed to follow the principle of least privilege.
  76. Should this not be possible for technical reasons, the security risk must be
  77. clearly documented so users are aware of the trade-off of enabling the feature.
  78. Isolation mechanisms
  79. ''''''''''''''''''''
  80. Several isolation mechanisms are available to realize this architecture of
  81. guest isolation and the principle of least privilege. With the exception of
  82. Linux seccomp, these mechanisms are all deployed by management tools that
  83. launch QEMU, such as libvirt. They are also platform-specific so they are only
  84. described briefly for Linux here.
  85. The fundamental isolation mechanism is that QEMU processes must run as
  86. unprivileged users. Sometimes it seems more convenient to launch QEMU as
  87. root to give it access to host devices (e.g. ``/dev/net/tun``) but this poses a
  88. huge security risk. File descriptor passing can be used to give an otherwise
  89. unprivileged QEMU process access to host devices without running QEMU as root.
  90. It is also possible to launch QEMU as a non-root user and configure UNIX groups
  91. for access to ``/dev/kvm``, ``/dev/net/tun``, and other device nodes.
  92. Some Linux distros already ship with UNIX groups for these devices by default.
  93. - SELinux and AppArmor make it possible to confine processes beyond the
  94. traditional UNIX process and file permissions model. They restrict the QEMU
  95. process from accessing processes and files on the host system that are not
  96. needed by QEMU.
  97. - Resource limits and cgroup controllers provide throughput and utilization
  98. limits on key resources such as CPU time, memory, and I/O bandwidth.
  99. - Linux namespaces can be used to make process, file system, and other system
  100. resources unavailable to QEMU. A namespaced QEMU process is restricted to only
  101. those resources that were granted to it.
  102. - Linux seccomp is available via the QEMU ``--sandbox`` option. It disables
  103. system calls that are not needed by QEMU, thereby reducing the host kernel
  104. attack surface.
  105. Sensitive configurations
  106. ------------------------
  107. There are aspects of QEMU that can have security implications which users &
  108. management applications must be aware of.
  109. Monitor console (QMP and HMP)
  110. '''''''''''''''''''''''''''''
  111. The monitor console (whether used with QMP or HMP) provides an interface
  112. to dynamically control many aspects of QEMU's runtime operation. Many of the
  113. commands exposed will instruct QEMU to access content on the host file system
  114. and/or trigger spawning of external processes.
  115. For example, the ``migrate`` command allows for the spawning of arbitrary
  116. processes for the purpose of tunnelling the migration data stream. The
  117. ``blockdev-add`` command instructs QEMU to open arbitrary files, exposing
  118. their content to the guest as a virtual disk.
  119. Unless QEMU is otherwise confined using technologies such as SELinux, AppArmor,
  120. or Linux namespaces, the monitor console should be considered to have privileges
  121. equivalent to those of the user account QEMU is running under.
  122. It is further important to consider the security of the character device backend
  123. over which the monitor console is exposed. It needs to have protection against
  124. malicious third parties which might try to make unauthorized connections, or
  125. perform man-in-the-middle attacks. Many of the character device backends do not
  126. satisfy this requirement and so must not be used for the monitor console.
  127. The general recommendation is that the monitor console should be exposed over
  128. a UNIX domain socket backend to the local host only. Use of the TCP based
  129. character device backend is inappropriate unless configured to use both TLS
  130. encryption and authorization control policy on client connections.
  131. In summary, the monitor console is considered a privileged control interface to
  132. QEMU and as such should only be made accessible to a trusted management
  133. application or user.