mfd_noexec.rst 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ==================================
  3. Introduction of non-executable mfd
  4. ==================================
  5. :Author:
  6. Daniel Verkamp <dverkamp@chromium.org>
  7. Jeff Xu <jeffxu@chromium.org>
  8. :Contributor:
  9. Aleksa Sarai <cyphar@cyphar.com>
  10. Since Linux introduced the memfd feature, memfds have always had their
  11. execute bit set, and the memfd_create() syscall doesn't allow setting
  12. it differently.
  13. However, in a secure-by-default system, such as ChromeOS, (where all
  14. executables should come from the rootfs, which is protected by verified
  15. boot), this executable nature of memfd opens a door for NoExec bypass
  16. and enables “confused deputy attack”. E.g, in VRP bug [1]: cros_vm
  17. process created a memfd to share the content with an external process,
  18. however the memfd is overwritten and used for executing arbitrary code
  19. and root escalation. [2] lists more VRP of this kind.
  20. On the other hand, executable memfd has its legit use: runc uses memfd’s
  21. seal and executable feature to copy the contents of the binary then
  22. execute them. For such a system, we need a solution to differentiate runc's
  23. use of executable memfds and an attacker's [3].
  24. To address those above:
  25. - Let memfd_create() set X bit at creation time.
  26. - Let memfd be sealed for modifying X bit when NX is set.
  27. - Add a new pid namespace sysctl: vm.memfd_noexec to help applications in
  28. migrating and enforcing non-executable MFD.
  29. User API
  30. ========
  31. ``int memfd_create(const char *name, unsigned int flags)``
  32. ``MFD_NOEXEC_SEAL``
  33. When MFD_NOEXEC_SEAL bit is set in the ``flags``, memfd is created
  34. with NX. F_SEAL_EXEC is set and the memfd can't be modified to
  35. add X later. MFD_ALLOW_SEALING is also implied.
  36. This is the most common case for the application to use memfd.
  37. ``MFD_EXEC``
  38. When MFD_EXEC bit is set in the ``flags``, memfd is created with X.
  39. Note:
  40. ``MFD_NOEXEC_SEAL`` implies ``MFD_ALLOW_SEALING``. In case that
  41. an app doesn't want sealing, it can add F_SEAL_SEAL after creation.
  42. Sysctl:
  43. ========
  44. ``pid namespaced sysctl vm.memfd_noexec``
  45. The new pid namespaced sysctl vm.memfd_noexec has 3 values:
  46. - 0: MEMFD_NOEXEC_SCOPE_EXEC
  47. memfd_create() without MFD_EXEC nor MFD_NOEXEC_SEAL acts like
  48. MFD_EXEC was set.
  49. - 1: MEMFD_NOEXEC_SCOPE_NOEXEC_SEAL
  50. memfd_create() without MFD_EXEC nor MFD_NOEXEC_SEAL acts like
  51. MFD_NOEXEC_SEAL was set.
  52. - 2: MEMFD_NOEXEC_SCOPE_NOEXEC_ENFORCED
  53. memfd_create() without MFD_NOEXEC_SEAL will be rejected.
  54. The sysctl allows finer control of memfd_create for old software that
  55. doesn't set the executable bit; for example, a container with
  56. vm.memfd_noexec=1 means the old software will create non-executable memfd
  57. by default while new software can create executable memfd by setting
  58. MFD_EXEC.
  59. The value of vm.memfd_noexec is passed to child namespace at creation
  60. time. In addition, the setting is hierarchical, i.e. during memfd_create,
  61. we will search from current ns to root ns and use the most restrictive
  62. setting.
  63. [1] https://crbug.com/1305267
  64. [2] https://bugs.chromium.org/p/chromium/issues/list?q=type%3Dbug-security%20memfd%20escalation&can=1
  65. [3] https://lwn.net/Articles/781013/