2
0

security.texi 7.7 KB

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