ipe.rst 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. .. SPDX-License-Identifier: GPL-2.0
  2. Integrity Policy Enforcement (IPE) - Kernel Documentation
  3. =========================================================
  4. .. NOTE::
  5. This is documentation targeted at developers, instead of administrators.
  6. If you're looking for documentation on the usage of IPE, please see
  7. :doc:`IPE admin guide </admin-guide/LSM/ipe>`.
  8. Historical Motivation
  9. ---------------------
  10. The original issue that prompted IPE's implementation was the creation
  11. of a locked-down system. This system would be born-secure, and have
  12. strong integrity guarantees over both the executable code, and specific
  13. *data files* on the system, that were critical to its function. These
  14. specific data files would not be readable unless they passed integrity
  15. policy. A mandatory access control system would be present, and
  16. as a result, xattrs would have to be protected. This lead to a selection
  17. of what would provide the integrity claims. At the time, there were two
  18. main mechanisms considered that could guarantee integrity for the system
  19. with these requirements:
  20. 1. IMA + EVM Signatures
  21. 2. DM-Verity
  22. Both options were carefully considered, however the choice to use DM-Verity
  23. over IMA+EVM as the *integrity mechanism* in the original use case of IPE
  24. was due to three main reasons:
  25. 1. Protection of additional attack vectors:
  26. * With IMA+EVM, without an encryption solution, the system is vulnerable
  27. to offline attack against the aforementioned specific data files.
  28. Unlike executables, read operations (like those on the protected data
  29. files), cannot be enforced to be globally integrity verified. This means
  30. there must be some form of selector to determine whether a read should
  31. enforce the integrity policy, or it should not.
  32. At the time, this was done with mandatory access control labels. An IMA
  33. policy would indicate what labels required integrity verification, which
  34. presented an issue: EVM would protect the label, but if an attacker could
  35. modify filesystem offline, the attacker could wipe all the xattrs -
  36. including the SELinux labels that would be used to determine whether the
  37. file should be subject to integrity policy.
  38. With DM-Verity, as the xattrs are saved as part of the Merkel tree, if
  39. offline mount occurs against the filesystem protected by dm-verity, the
  40. checksum no longer matches and the file fails to be read.
  41. * As userspace binaries are paged in Linux, dm-verity also offers the
  42. additional protection against a hostile block device. In such an attack,
  43. the block device reports the appropriate content for the IMA hash
  44. initially, passing the required integrity check. Then, on the page fault
  45. that accesses the real data, will report the attacker's payload. Since
  46. dm-verity will check the data when the page fault occurs (and the disk
  47. access), this attack is mitigated.
  48. 2. Performance:
  49. * dm-verity provides integrity verification on demand as blocks are
  50. read versus requiring the entire file being read into memory for
  51. validation.
  52. 3. Simplicity of signing:
  53. * No need for two signatures (IMA, then EVM): one signature covers
  54. an entire block device.
  55. * Signatures can be stored externally to the filesystem metadata.
  56. * The signature supports an x.509-based signing infrastructure.
  57. The next step was to choose a *policy* to enforce the integrity mechanism.
  58. The minimum requirements for the policy were:
  59. 1. The policy itself must be integrity verified (preventing trivial
  60. attack against it).
  61. 2. The policy itself must be resistant to rollback attacks.
  62. 3. The policy enforcement must have a permissive-like mode.
  63. 4. The policy must be able to be updated, in its entirety, without
  64. a reboot.
  65. 5. Policy updates must be atomic.
  66. 6. The policy must support *revocations* of previously authored
  67. components.
  68. 7. The policy must be auditable, at any point-of-time.
  69. IMA, as the only integrity policy mechanism at the time, was
  70. considered against these list of requirements, and did not fulfill
  71. all of the minimum requirements. Extending IMA to cover these
  72. requirements was considered, but ultimately discarded for a
  73. two reasons:
  74. 1. Regression risk; many of these changes would result in
  75. dramatic code changes to IMA, which is already present in the
  76. kernel, and therefore might impact users.
  77. 2. IMA was used in the system for measurement and attestation;
  78. separation of measurement policy from local integrity policy
  79. enforcement was considered favorable.
  80. Due to these reasons, it was decided that a new LSM should be created,
  81. whose responsibility would be only the local integrity policy enforcement.
  82. Role and Scope
  83. --------------
  84. IPE, as its name implies, is fundamentally an integrity policy enforcement
  85. solution; IPE does not mandate how integrity is provided, but instead
  86. leaves that decision to the system administrator to set the security bar,
  87. via the mechanisms that they select that suit their individual needs.
  88. There are several different integrity solutions that provide a different
  89. level of security guarantees; and IPE allows sysadmins to express policy for
  90. theoretically all of them.
  91. IPE does not have an inherent mechanism to ensure integrity on its own.
  92. Instead, there are more effective layers available for building systems that
  93. can guarantee integrity. It's important to note that the mechanism for proving
  94. integrity is independent of the policy for enforcing that integrity claim.
  95. Therefore, IPE was designed around:
  96. 1. Easy integrations with integrity providers.
  97. 2. Ease of use for platform administrators/sysadmins.
  98. Design Rationale:
  99. -----------------
  100. IPE was designed after evaluating existing integrity policy solutions
  101. in other operating systems and environments. In this survey of other
  102. implementations, there were a few pitfalls identified:
  103. 1. Policies were not readable by humans, usually requiring a binary
  104. intermediary format.
  105. 2. A single, non-customizable action was implicitly taken as a default.
  106. 3. Debugging the policy required manual steps to determine what rule was violated.
  107. 4. Authoring a policy required an in-depth knowledge of the larger system,
  108. or operating system.
  109. IPE attempts to avoid all of these pitfalls.
  110. Policy
  111. ~~~~~~
  112. Plain Text
  113. ^^^^^^^^^^
  114. IPE's policy is plain-text. This introduces slightly larger policy files than
  115. other LSMs, but solves two major problems that occurs with some integrity policy
  116. solutions on other platforms.
  117. The first issue is one of code maintenance and duplication. To author policies,
  118. the policy has to be some form of string representation (be it structured,
  119. through XML, JSON, YAML, etcetera), to allow the policy author to understand
  120. what is being written. In a hypothetical binary policy design, a serializer
  121. is necessary to write the policy from the human readable form, to the binary
  122. form, and a deserializer is needed to interpret the binary form into a data
  123. structure in the kernel.
  124. Eventually, another deserializer will be needed to transform the binary from
  125. back into the human-readable form with as much information preserved. This is because a
  126. user of this access control system will have to keep a lookup table of a checksum
  127. and the original file itself to try to understand what policies have been deployed
  128. on this system and what policies have not. For a single user, this may be alright,
  129. as old policies can be discarded almost immediately after the update takes hold.
  130. For users that manage computer fleets in the thousands, if not hundreds of thousands,
  131. with multiple different operating systems, and multiple different operational needs,
  132. this quickly becomes an issue, as stale policies from years ago may be present,
  133. quickly resulting in the need to recover the policy or fund extensive infrastructure
  134. to track what each policy contains.
  135. With now three separate serializer/deserializers, maintenance becomes costly. If the
  136. policy avoids the binary format, there is only one required serializer: from the
  137. human-readable form to the data structure in kernel, saving on code maintenance,
  138. and retaining operability.
  139. The second issue with a binary format is one of transparency. As IPE controls
  140. access based on the trust of the system's resources, it's policy must also be
  141. trusted to be changed. This is done through signatures, resulting in needing
  142. signing as a process. Signing, as a process, is typically done with a
  143. high security bar, as anything signed can be used to attack integrity
  144. enforcement systems. It is also important that, when signing something, that
  145. the signer is aware of what they are signing. A binary policy can cause
  146. obfuscation of that fact; what signers see is an opaque binary blob. A
  147. plain-text policy, on the other hand, the signers see the actual policy
  148. submitted for signing.
  149. Boot Policy
  150. ~~~~~~~~~~~
  151. IPE, if configured appropriately, is able to enforce a policy as soon as a
  152. kernel is booted and usermode starts. That implies some level of storage
  153. of the policy to apply the minute usermode starts. Generally, that storage
  154. can be handled in one of three ways:
  155. 1. The policy file(s) live on disk and the kernel loads the policy prior
  156. to an code path that would result in an enforcement decision.
  157. 2. The policy file(s) are passed by the bootloader to the kernel, who
  158. parses the policy.
  159. 3. There is a policy file that is compiled into the kernel that is
  160. parsed and enforced on initialization.
  161. The first option has problems: the kernel reading files from userspace
  162. is typically discouraged and very uncommon in the kernel.
  163. The second option also has problems: Linux supports a variety of bootloaders
  164. across its entire ecosystem - every bootloader would have to support this
  165. new methodology or there must be an independent source. It would likely
  166. result in more drastic changes to the kernel startup than necessary.
  167. The third option is the best but it's important to be aware that the policy
  168. will take disk space against the kernel it's compiled in. It's important to
  169. keep this policy generalized enough that userspace can load a new, more
  170. complicated policy, but restrictive enough that it will not overauthorize
  171. and cause security issues.
  172. The initramfs provides a way that this bootup path can be established. The
  173. kernel starts with a minimal policy, that trusts the initramfs only. Inside
  174. the initramfs, when the real rootfs is mounted, but not yet transferred to,
  175. it deploys and activates a policy that trusts the new root filesystem.
  176. This prevents overauthorization at any step, and keeps the kernel policy
  177. to a minimal size.
  178. Startup
  179. ^^^^^^^
  180. Not every system, however starts with an initramfs, so the startup policy
  181. compiled into the kernel will need some flexibility to express how trust
  182. is established for the next phase of the bootup. To this end, if we just
  183. make the compiled-in policy a full IPE policy, it allows system builders
  184. to express the first stage bootup requirements appropriately.
  185. Updatable, Rebootless Policy
  186. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  187. As requirements change over time (vulnerabilities are found in previously
  188. trusted applications, keys roll, etcetera). Updating a kernel to change the
  189. meet those security goals is not always a suitable option, as updates are not
  190. always risk-free, and blocking a security update leaves systems vulnerable.
  191. This means IPE requires a policy that can be completely updated (allowing
  192. revocations of existing policy) from a source external to the kernel (allowing
  193. policies to be updated without updating the kernel).
  194. Additionally, since the kernel is stateless between invocations, and reading
  195. policy files off the disk from kernel space is a bad idea(tm), then the
  196. policy updates have to be done rebootlessly.
  197. To allow an update from an external source, it could be potentially malicious,
  198. so this policy needs to have a way to be identified as trusted. This is
  199. done via a signature chained to a trust source in the kernel. Arbitrarily,
  200. this is the ``SYSTEM_TRUSTED_KEYRING``, a keyring that is initially
  201. populated at kernel compile-time, as this matches the expectation that the
  202. author of the compiled-in policy described above is the same entity that can
  203. deploy policy updates.
  204. Anti-Rollback / Anti-Replay
  205. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  206. Over time, vulnerabilities are found and trusted resources may not be
  207. trusted anymore. IPE's policy has no exception to this. There can be
  208. instances where a mistaken policy author deploys an insecure policy,
  209. before correcting it with a secure policy.
  210. Assuming that as soon as the insecure policy is signed, and an attacker
  211. acquires the insecure policy, IPE needs a way to prevent rollback
  212. from the secure policy update to the insecure policy update.
  213. Initially, IPE's policy can have a policy_version that states the
  214. minimum required version across all policies that can be active on
  215. the system. This will prevent rollback while the system is live.
  216. .. WARNING::
  217. However, since the kernel is stateless across boots, this policy
  218. version will be reset to 0.0.0 on the next boot. System builders
  219. need to be aware of this, and ensure the new secure policies are
  220. deployed ASAP after a boot to ensure that the window of
  221. opportunity is minimal for an attacker to deploy the insecure policy.
  222. Implicit Actions:
  223. ~~~~~~~~~~~~~~~~~
  224. The issue of implicit actions only becomes visible when you consider
  225. a mixed level of security bars across multiple operations in a system.
  226. For example, consider a system that has strong integrity guarantees
  227. over both the executable code, and specific *data files* on the system,
  228. that were critical to its function. In this system, three types of policies
  229. are possible:
  230. 1. A policy in which failure to match any rules in the policy results
  231. in the action being denied.
  232. 2. A policy in which failure to match any rules in the policy results
  233. in the action being allowed.
  234. 3. A policy in which the action taken when no rules are matched is
  235. specified by the policy author.
  236. The first option could make a policy like this::
  237. op=EXECUTE integrity_verified=YES action=ALLOW
  238. In the example system, this works well for the executables, as all
  239. executables should have integrity guarantees, without exception. The
  240. issue becomes with the second requirement about specific data files.
  241. This would result in a policy like this (assuming each line is
  242. evaluated in order)::
  243. op=EXECUTE integrity_verified=YES action=ALLOW
  244. op=READ integrity_verified=NO label=critical_t action=DENY
  245. op=READ action=ALLOW
  246. This is somewhat clear if you read the docs, understand the policy
  247. is executed in order and that the default is a denial; however, the
  248. last line effectively changes that default to an ALLOW. This is
  249. required, because in a realistic system, there are some unverified
  250. reads (imagine appending to a log file).
  251. The second option, matching no rules results in an allow, is clearer
  252. for the specific data files::
  253. op=READ integrity_verified=NO label=critical_t action=DENY
  254. And, like the first option, falls short with the execution scenario,
  255. effectively needing to override the default::
  256. op=EXECUTE integrity_verified=YES action=ALLOW
  257. op=EXECUTE action=DENY
  258. op=READ integrity_verified=NO label=critical_t action=DENY
  259. This leaves the third option. Instead of making users be clever
  260. and override the default with an empty rule, force the end-user
  261. to consider what the appropriate default should be for their
  262. scenario and explicitly state it::
  263. DEFAULT op=EXECUTE action=DENY
  264. op=EXECUTE integrity_verified=YES action=ALLOW
  265. DEFAULT op=READ action=ALLOW
  266. op=READ integrity_verified=NO label=critical_t action=DENY
  267. Policy Debugging:
  268. ~~~~~~~~~~~~~~~~~
  269. When developing a policy, it is useful to know what line of the policy
  270. is being violated to reduce debugging costs; narrowing the scope of the
  271. investigation to the exact line that resulted in the action. Some integrity
  272. policy systems do not provide this information, instead providing the
  273. information that was used in the evaluation. This then requires a correlation
  274. with the policy to evaluate what went wrong.
  275. Instead, IPE just emits the rule that was matched. This limits the scope
  276. of the investigation to the exact policy line (in the case of a specific
  277. rule), or the section (in the case of a DEFAULT). This decreases iteration
  278. and investigation times when policy failures are observed while evaluating
  279. policies.
  280. IPE's policy engine is also designed in a way that it makes it obvious to
  281. a human of how to investigate a policy failure. Each line is evaluated in
  282. the sequence that is written, so the algorithm is very simple to follow
  283. for humans to recreate the steps and could have caused the failure. In other
  284. surveyed systems, optimizations occur (sorting rules, for instance) when loading
  285. the policy. In those systems, it requires multiple steps to debug, and the
  286. algorithm may not always be clear to the end-user without reading the code first.
  287. Simplified Policy:
  288. ~~~~~~~~~~~~~~~~~~
  289. Finally, IPE's policy is designed for sysadmins, not kernel developers. Instead
  290. of covering individual LSM hooks (or syscalls), IPE covers operations. This means
  291. instead of sysadmins needing to know that the syscalls ``mmap``, ``mprotect``,
  292. ``execve``, and ``uselib`` must have rules protecting them, they must simple know
  293. that they want to restrict code execution. This limits the amount of bypasses that
  294. could occur due to a lack of knowledge of the underlying system; whereas the
  295. maintainers of IPE, being kernel developers can make the correct choice to determine
  296. whether something maps to these operations, and under what conditions.
  297. Implementation Notes
  298. --------------------
  299. Anonymous Memory
  300. ~~~~~~~~~~~~~~~~
  301. Anonymous memory isn't treated any differently from any other access in IPE.
  302. When anonymous memory is mapped with ``+X``, it still comes into the ``file_mmap``
  303. or ``file_mprotect`` hook, but with a ``NULL`` file object. This is submitted to
  304. the evaluation, like any other file. However, all current trust properties will
  305. evaluate to false, as they are all file-based and the operation is not
  306. associated with a file.
  307. .. WARNING::
  308. This also occurs with the ``kernel_load_data`` hook, when the kernel is
  309. loading data from a userspace buffer that is not backed by a file. In this
  310. scenario all current trust properties will also evaluate to false.
  311. Securityfs Interface
  312. ~~~~~~~~~~~~~~~~~~~~
  313. The per-policy securityfs tree is somewhat unique. For example, for
  314. a standard securityfs policy tree::
  315. MyPolicy
  316. |- active
  317. |- delete
  318. |- name
  319. |- pkcs7
  320. |- policy
  321. |- update
  322. |- version
  323. The policy is stored in the ``->i_private`` data of the MyPolicy inode.
  324. Tests
  325. -----
  326. IPE has KUnit Tests for the policy parser. Recommended kunitconfig::
  327. CONFIG_KUNIT=y
  328. CONFIG_SECURITY=y
  329. CONFIG_SECURITYFS=y
  330. CONFIG_PKCS7_MESSAGE_PARSER=y
  331. CONFIG_SYSTEM_DATA_VERIFICATION=y
  332. CONFIG_FS_VERITY=y
  333. CONFIG_FS_VERITY_BUILTIN_SIGNATURES=y
  334. CONFIG_BLOCK=y
  335. CONFIG_MD=y
  336. CONFIG_BLK_DEV_DM=y
  337. CONFIG_DM_VERITY=y
  338. CONFIG_DM_VERITY_VERIFY_ROOTHASH_SIG=y
  339. CONFIG_NET=y
  340. CONFIG_AUDIT=y
  341. CONFIG_AUDITSYSCALL=y
  342. CONFIG_BLK_DEV_INITRD=y
  343. CONFIG_SECURITY_IPE=y
  344. CONFIG_IPE_PROP_DM_VERITY=y
  345. CONFIG_IPE_PROP_DM_VERITY_SIGNATURE=y
  346. CONFIG_IPE_PROP_FS_VERITY=y
  347. CONFIG_IPE_PROP_FS_VERITY_BUILTIN_SIG=y
  348. CONFIG_SECURITY_IPE_KUNIT_TEST=y
  349. In addition, IPE has a python based integration
  350. `test suite <https://github.com/microsoft/ipe/tree/test-suite>`_ that
  351. can test both user interfaces and enforcement functionalities.