2
0

secure-coding-practices.rst 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. =======================
  2. Secure Coding Practices
  3. =======================
  4. This document covers topics that both developers and security researchers must
  5. be aware of so that they can develop safe code and audit existing code
  6. properly.
  7. Reporting Security Bugs
  8. -----------------------
  9. For details on how to report security bugs or ask questions about potential
  10. security bugs, see the `Security Process wiki page
  11. <https://wiki.qemu.org/SecurityProcess>`_.
  12. General Secure C Coding Practices
  13. ---------------------------------
  14. Most CVEs (security bugs) reported against QEMU are not specific to
  15. virtualization or emulation. They are simply C programming bugs. Therefore
  16. it's critical to be aware of common classes of security bugs.
  17. There is a wide selection of resources available covering secure C coding. For
  18. example, the `CERT C Coding Standard
  19. <https://wiki.sei.cmu.edu/confluence/display/c/SEI+CERT+C+Coding+Standard>`_
  20. covers the most important classes of security bugs.
  21. Instead of describing them in detail here, only the names of the most important
  22. classes of security bugs are mentioned:
  23. * Buffer overflows
  24. * Use-after-free and double-free
  25. * Integer overflows
  26. * Format string vulnerabilities
  27. Some of these classes of bugs can be detected by analyzers. Static analysis is
  28. performed regularly by Coverity and the most obvious of these bugs are even
  29. reported by compilers. Dynamic analysis is possible with valgrind, tsan, and
  30. asan.
  31. Input Validation
  32. ----------------
  33. Inputs from the guest or external sources (e.g. network, files) cannot be
  34. trusted and may be invalid. Inputs must be checked before using them in a way
  35. that could crash the program, expose host memory to the guest, or otherwise be
  36. exploitable by an attacker.
  37. The most sensitive attack surface is device emulation. All hardware register
  38. accesses and data read from guest memory must be validated. A typical example
  39. is a device that contains multiple units that are selectable by the guest via
  40. an index register::
  41. typedef struct {
  42. ProcessingUnit unit[2];
  43. ...
  44. } MyDeviceState;
  45. static void mydev_writel(void *opaque, uint32_t addr, uint32_t val)
  46. {
  47. MyDeviceState *mydev = opaque;
  48. ProcessingUnit *unit;
  49. switch (addr) {
  50. case MYDEV_SELECT_UNIT:
  51. unit = &mydev->unit[val]; <-- this input wasn't validated!
  52. ...
  53. }
  54. }
  55. If ``val`` is not in range [0, 1] then an out-of-bounds memory access will take
  56. place when ``unit`` is dereferenced. The code must check that ``val`` is 0 or
  57. 1 and handle the case where it is invalid.
  58. Unexpected Device Accesses
  59. --------------------------
  60. The guest may access device registers in unusual orders or at unexpected
  61. moments. Device emulation code must not assume that the guest follows the
  62. typical "theory of operation" presented in driver writer manuals. The guest
  63. may make nonsense accesses to device registers such as starting operations
  64. before the device has been fully initialized.
  65. A related issue is that device emulation code must be prepared for unexpected
  66. device register accesses while asynchronous operations are in progress. A
  67. well-behaved guest might wait for a completion interrupt before accessing
  68. certain device registers. Device emulation code must handle the case where the
  69. guest overwrites registers or submits further requests before an ongoing
  70. request completes. Unexpected accesses must not cause memory corruption or
  71. leaks in QEMU.
  72. Invalid device register accesses can be reported with
  73. ``qemu_log_mask(LOG_GUEST_ERROR, ...)``. The ``-d guest_errors`` command-line
  74. option enables these log messages.
  75. Live Migration
  76. --------------
  77. Device state can be saved to disk image files and shared with other users.
  78. Live migration code must validate inputs when loading device state so an
  79. attacker cannot gain control by crafting invalid device states. Device state
  80. is therefore considered untrusted even though it is typically generated by QEMU
  81. itself.
  82. Guest Memory Access Races
  83. -------------------------
  84. Guests with multiple vCPUs may modify guest RAM while device emulation code is
  85. running. Device emulation code must copy in descriptors and other guest RAM
  86. structures and only process the local copy. This prevents
  87. time-of-check-to-time-of-use (TOCTOU) race conditions that could cause QEMU to
  88. crash when a vCPU thread modifies guest RAM while device emulation is
  89. processing it.