mseal.rst 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. .. SPDX-License-Identifier: GPL-2.0
  2. =====================
  3. Introduction of mseal
  4. =====================
  5. :Author: Jeff Xu <jeffxu@chromium.org>
  6. Modern CPUs support memory permissions such as RW and NX bits. The memory
  7. permission feature improves security stance on memory corruption bugs, i.e.
  8. the attacker can’t just write to arbitrary memory and point the code to it,
  9. the memory has to be marked with X bit, or else an exception will happen.
  10. Memory sealing additionally protects the mapping itself against
  11. modifications. This is useful to mitigate memory corruption issues where a
  12. corrupted pointer is passed to a memory management system. For example,
  13. such an attacker primitive can break control-flow integrity guarantees
  14. since read-only memory that is supposed to be trusted can become writable
  15. or .text pages can get remapped. Memory sealing can automatically be
  16. applied by the runtime loader to seal .text and .rodata pages and
  17. applications can additionally seal security critical data at runtime.
  18. A similar feature already exists in the XNU kernel with the
  19. VM_FLAGS_PERMANENT flag [1] and on OpenBSD with the mimmutable syscall [2].
  20. SYSCALL
  21. =======
  22. mseal syscall signature
  23. -----------------------
  24. ``int mseal(void \* addr, size_t len, unsigned long flags)``
  25. **addr**/**len**: virtual memory address range.
  26. The address range set by **addr**/**len** must meet:
  27. - The start address must be in an allocated VMA.
  28. - The start address must be page aligned.
  29. - The end address (**addr** + **len**) must be in an allocated VMA.
  30. - no gap (unallocated memory) between start and end address.
  31. The ``len`` will be paged aligned implicitly by the kernel.
  32. **flags**: reserved for future use.
  33. **Return values**:
  34. - **0**: Success.
  35. - **-EINVAL**:
  36. * Invalid input ``flags``.
  37. * The start address (``addr``) is not page aligned.
  38. * Address range (``addr`` + ``len``) overflow.
  39. - **-ENOMEM**:
  40. * The start address (``addr``) is not allocated.
  41. * The end address (``addr`` + ``len``) is not allocated.
  42. * A gap (unallocated memory) between start and end address.
  43. - **-EPERM**:
  44. * sealing is supported only on 64-bit CPUs, 32-bit is not supported.
  45. **Note about error return**:
  46. - For above error cases, users can expect the given memory range is
  47. unmodified, i.e. no partial update.
  48. - There might be other internal errors/cases not listed here, e.g.
  49. error during merging/splitting VMAs, or the process reaching the maximum
  50. number of supported VMAs. In those cases, partial updates to the given
  51. memory range could happen. However, those cases should be rare.
  52. **Architecture support**:
  53. mseal only works on 64-bit CPUs, not 32-bit CPUs.
  54. **Idempotent**:
  55. users can call mseal multiple times. mseal on an already sealed memory
  56. is a no-action (not error).
  57. **no munseal**
  58. Once mapping is sealed, it can't be unsealed. The kernel should never
  59. have munseal, this is consistent with other sealing feature, e.g.
  60. F_SEAL_SEAL for file.
  61. Blocked mm syscall for sealed mapping
  62. -------------------------------------
  63. It might be important to note: **once the mapping is sealed, it will
  64. stay in the process's memory until the process terminates**.
  65. Example::
  66. *ptr = mmap(0, 4096, PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
  67. rc = mseal(ptr, 4096, 0);
  68. /* munmap will fail */
  69. rc = munmap(ptr, 4096);
  70. assert(rc < 0);
  71. Blocked mm syscall:
  72. - munmap
  73. - mmap
  74. - mremap
  75. - mprotect and pkey_mprotect
  76. - some destructive madvise behaviors: MADV_DONTNEED, MADV_FREE,
  77. MADV_DONTNEED_LOCKED, MADV_FREE, MADV_DONTFORK, MADV_WIPEONFORK
  78. The first set of syscalls to block is munmap, mremap, mmap. They can
  79. either leave an empty space in the address space, therefore allowing
  80. replacement with a new mapping with new set of attributes, or can
  81. overwrite the existing mapping with another mapping.
  82. mprotect and pkey_mprotect are blocked because they changes the
  83. protection bits (RWX) of the mapping.
  84. Certain destructive madvise behaviors, specifically MADV_DONTNEED,
  85. MADV_FREE, MADV_DONTNEED_LOCKED, and MADV_WIPEONFORK, can introduce
  86. risks when applied to anonymous memory by threads lacking write
  87. permissions. Consequently, these operations are prohibited under such
  88. conditions. The aforementioned behaviors have the potential to modify
  89. region contents by discarding pages, effectively performing a memset(0)
  90. operation on the anonymous memory.
  91. Kernel will return -EPERM for blocked syscalls.
  92. When blocked syscall return -EPERM due to sealing, the memory regions may
  93. or may not be changed, depends on the syscall being blocked:
  94. - munmap: munmap is atomic. If one of VMAs in the given range is
  95. sealed, none of VMAs are updated.
  96. - mprotect, pkey_mprotect, madvise: partial update might happen, e.g.
  97. when mprotect over multiple VMAs, mprotect might update the beginning
  98. VMAs before reaching the sealed VMA and return -EPERM.
  99. - mmap and mremap: undefined behavior.
  100. Use cases
  101. =========
  102. - glibc:
  103. The dynamic linker, during loading ELF executables, can apply sealing to
  104. mapping segments.
  105. - Chrome browser: protect some security sensitive data structures.
  106. When not to use mseal
  107. =====================
  108. Applications can apply sealing to any virtual memory region from userspace,
  109. but it is *crucial to thoroughly analyze the mapping's lifetime* prior to
  110. apply the sealing. This is because the sealed mapping *won’t be unmapped*
  111. until the process terminates or the exec system call is invoked.
  112. For example:
  113. - aio/shm
  114. aio/shm can call mmap and munmap on behalf of userspace, e.g.
  115. ksys_shmdt() in shm.c. The lifetimes of those mapping are not tied to
  116. the lifetime of the process. If those memories are sealed from userspace,
  117. then munmap will fail, causing leaks in VMA address space during the
  118. lifetime of the process.
  119. - ptr allocated by malloc (heap)
  120. Don't use mseal on the memory ptr return from malloc().
  121. malloc() is implemented by allocator, e.g. by glibc. Heap manager might
  122. allocate a ptr from brk or mapping created by mmap.
  123. If an app calls mseal on a ptr returned from malloc(), this can affect
  124. the heap manager's ability to manage the mappings; the outcome is
  125. non-deterministic.
  126. Example::
  127. ptr = malloc(size);
  128. /* don't call mseal on ptr return from malloc. */
  129. mseal(ptr, size);
  130. /* free will success, allocator can't shrink heap lower than ptr */
  131. free(ptr);
  132. mseal doesn't block
  133. ===================
  134. In a nutshell, mseal blocks certain mm syscall from modifying some of VMA's
  135. attributes, such as protection bits (RWX). Sealed mappings doesn't mean the
  136. memory is immutable.
  137. As Jann Horn pointed out in [3], there are still a few ways to write
  138. to RO memory, which is, in a way, by design. And those could be blocked
  139. by different security measures.
  140. Those cases are:
  141. - Write to read-only memory through /proc/self/mem interface (FOLL_FORCE).
  142. - Write to read-only memory through ptrace (such as PTRACE_POKETEXT).
  143. - userfaultfd.
  144. The idea that inspired this patch comes from Stephen Röttger’s work in V8
  145. CFI [4]. Chrome browser in ChromeOS will be the first user of this API.
  146. Reference
  147. =========
  148. - [1] https://github.com/apple-oss-distributions/xnu/blob/1031c584a5e37aff177559b9f69dbd3c8c3fd30a/osfmk/mach/vm_statistics.h#L274
  149. - [2] https://man.openbsd.org/mimmutable.2
  150. - [3] https://lore.kernel.org/lkml/CAG48ez3ShUYey+ZAFsU2i1RpQn0a5eOs2hzQ426FkcgnfUGLvA@mail.gmail.com
  151. - [4] https://docs.google.com/document/d/1O2jwK4dxI3nRcOJuPYkonhTkNQfbmwdvxQMyXgeaRHo/edit#heading=h.bvaojj9fu6hc