brintos

brintos / linux-shallow public Read only

0
0
Text · 19.1 KiB · 4a7d953 Raw
447 lines · plain
1.. SPDX-License-Identifier: GPL-2.02 3Integrity Policy Enforcement (IPE) - Kernel Documentation4=========================================================5 6.. NOTE::7 8   This is documentation targeted at developers, instead of administrators.9   If you're looking for documentation on the usage of IPE, please see10   :doc:`IPE admin guide </admin-guide/LSM/ipe>`.11 12Historical Motivation13---------------------14 15The original issue that prompted IPE's implementation was the creation16of a locked-down system. This system would be born-secure, and have17strong integrity guarantees over both the executable code, and specific18*data files* on the system, that were critical to its function. These19specific data files would not be readable unless they passed integrity20policy. A mandatory access control system would be present, and21as a result, xattrs would have to be protected. This lead to a selection22of what would provide the integrity claims. At the time, there were two23main mechanisms considered that could guarantee integrity for the system24with these requirements:25 26  1. IMA + EVM Signatures27  2. DM-Verity28 29Both options were carefully considered, however the choice to use DM-Verity30over IMA+EVM as the *integrity mechanism* in the original use case of IPE31was due to three main reasons:32 33  1. Protection of additional attack vectors:34 35    * With IMA+EVM, without an encryption solution, the system is vulnerable36      to offline attack against the aforementioned specific data files.37 38      Unlike executables, read operations (like those on the protected data39      files), cannot be enforced to be globally integrity verified. This means40      there must be some form of selector to determine whether a read should41      enforce the integrity policy, or it should not.42 43      At the time, this was done with mandatory access control labels. An IMA44      policy would indicate what labels required integrity verification, which45      presented an issue: EVM would protect the label, but if an attacker could46      modify filesystem offline, the attacker could wipe all the xattrs -47      including the SELinux labels that would be used to determine whether the48      file should be subject to integrity policy.49 50      With DM-Verity, as the xattrs are saved as part of the Merkel tree, if51      offline mount occurs against the filesystem protected by dm-verity, the52      checksum no longer matches and the file fails to be read.53 54    * As userspace binaries are paged in Linux, dm-verity also offers the55      additional protection against a hostile block device. In such an attack,56      the block device reports the appropriate content for the IMA hash57      initially, passing the required integrity check. Then, on the page fault58      that accesses the real data, will report the attacker's payload. Since59      dm-verity will check the data when the page fault occurs (and the disk60      access), this attack is mitigated.61 62  2. Performance:63 64    * dm-verity provides integrity verification on demand as blocks are65      read versus requiring the entire file being read into memory for66      validation.67 68  3. Simplicity of signing:69 70    * No need for two signatures (IMA, then EVM): one signature covers71      an entire block device.72    * Signatures can be stored externally to the filesystem metadata.73    * The signature supports an x.509-based signing infrastructure.74 75The next step was to choose a *policy* to enforce the integrity mechanism.76The minimum requirements for the policy were:77 78  1. The policy itself must be integrity verified (preventing trivial79     attack against it).80  2. The policy itself must be resistant to rollback attacks.81  3. The policy enforcement must have a permissive-like mode.82  4. The policy must be able to be updated, in its entirety, without83     a reboot.84  5. Policy updates must be atomic.85  6. The policy must support *revocations* of previously authored86     components.87  7. The policy must be auditable, at any point-of-time.88 89IMA, as the only integrity policy mechanism at the time, was90considered against these list of requirements, and did not fulfill91all of the minimum requirements. Extending IMA to cover these92requirements was considered, but ultimately discarded for a93two reasons:94 95  1. Regression risk; many of these changes would result in96     dramatic code changes to IMA, which is already present in the97     kernel, and therefore might impact users.98 99  2. IMA was used in the system for measurement and attestation;100     separation of measurement policy from local integrity policy101     enforcement was considered favorable.102 103Due to these reasons, it was decided that a new LSM should be created,104whose responsibility would be only the local integrity policy enforcement.105 106Role and Scope107--------------108 109IPE, as its name implies, is fundamentally an integrity policy enforcement110solution; IPE does not mandate how integrity is provided, but instead111leaves that decision to the system administrator to set the security bar,112via the mechanisms that they select that suit their individual needs.113There are several different integrity solutions that provide a different114level of security guarantees; and IPE allows sysadmins to express policy for115theoretically all of them.116 117IPE does not have an inherent mechanism to ensure integrity on its own.118Instead, there are more effective layers available for building systems that119can guarantee integrity. It's important to note that the mechanism for proving120integrity is independent of the policy for enforcing that integrity claim.121 122Therefore, IPE was designed around:123 124  1. Easy integrations with integrity providers.125  2. Ease of use for platform administrators/sysadmins.126 127Design Rationale:128-----------------129 130IPE was designed after evaluating existing integrity policy solutions131in other operating systems and environments. In this survey of other132implementations, there were a few pitfalls identified:133 134  1. Policies were not readable by humans, usually requiring a binary135     intermediary format.136  2. A single, non-customizable action was implicitly taken as a default.137  3. Debugging the policy required manual steps to determine what rule was violated.138  4. Authoring a policy required an in-depth knowledge of the larger system,139     or operating system.140 141IPE attempts to avoid all of these pitfalls.142 143Policy144~~~~~~145 146Plain Text147^^^^^^^^^^148 149IPE's policy is plain-text. This introduces slightly larger policy files than150other LSMs, but solves two major problems that occurs with some integrity policy151solutions on other platforms.152 153The first issue is one of code maintenance and duplication. To author policies,154the policy has to be some form of string representation (be it structured,155through XML, JSON, YAML, etcetera), to allow the policy author to understand156what is being written. In a hypothetical binary policy design, a serializer157is necessary to write the policy from the human readable form, to the binary158form, and a deserializer is needed to interpret the binary form into a data159structure in the kernel.160 161Eventually, another deserializer will be needed to transform the binary from162back into the human-readable form with as much information preserved. This is because a163user of this access control system will have to keep a lookup table of a checksum164and the original file itself to try to understand what policies have been deployed165on this system and what policies have not. For a single user, this may be alright,166as old policies can be discarded almost immediately after the update takes hold.167For users that manage computer fleets in the thousands, if not hundreds of thousands,168with multiple different operating systems, and multiple different operational needs,169this quickly becomes an issue, as stale policies from years ago may be present,170quickly resulting in the need to recover the policy or fund extensive infrastructure171to track what each policy contains.172 173With now three separate serializer/deserializers, maintenance becomes costly. If the174policy avoids the binary format, there is only one required serializer: from the175human-readable form to the data structure in kernel, saving on code maintenance,176and retaining operability.177 178The second issue with a binary format is one of transparency. As IPE controls179access based on the trust of the system's resources, it's policy must also be180trusted to be changed. This is done through signatures, resulting in needing181signing as a process. Signing, as a process, is typically done with a182high security bar, as anything signed can be used to attack integrity183enforcement systems. It is also important that, when signing something, that184the signer is aware of what they are signing. A binary policy can cause185obfuscation of that fact; what signers see is an opaque binary blob. A186plain-text policy, on the other hand, the signers see the actual policy187submitted for signing.188 189Boot Policy190~~~~~~~~~~~191 192IPE, if configured appropriately, is able to enforce a policy as soon as a193kernel is booted and usermode starts. That implies some level of storage194of the policy to apply the minute usermode starts. Generally, that storage195can be handled in one of three ways:196 197  1. The policy file(s) live on disk and the kernel loads the policy prior198     to an code path that would result in an enforcement decision.199  2. The policy file(s) are passed by the bootloader to the kernel, who200     parses the policy.201  3. There is a policy file that is compiled into the kernel that is202     parsed and enforced on initialization.203 204The first option has problems: the kernel reading files from userspace205is typically discouraged and very uncommon in the kernel.206 207The second option also has problems: Linux supports a variety of bootloaders208across its entire ecosystem - every bootloader would have to support this209new methodology or there must be an independent source. It would likely210result in more drastic changes to the kernel startup than necessary.211 212The third option is the best but it's important to be aware that the policy213will take disk space against the kernel it's compiled in. It's important to214keep this policy generalized enough that userspace can load a new, more215complicated policy, but restrictive enough that it will not overauthorize216and cause security issues.217 218The initramfs provides a way that this bootup path can be established. The219kernel starts with a minimal policy, that trusts the initramfs only. Inside220the initramfs, when the real rootfs is mounted, but not yet transferred to,221it deploys and activates a policy that trusts the new root filesystem.222This prevents overauthorization at any step, and keeps the kernel policy223to a minimal size.224 225Startup226^^^^^^^227 228Not every system, however starts with an initramfs, so the startup policy229compiled into the kernel will need some flexibility to express how trust230is established for the next phase of the bootup. To this end, if we just231make the compiled-in policy a full IPE policy, it allows system builders232to express the first stage bootup requirements appropriately.233 234Updatable, Rebootless Policy235~~~~~~~~~~~~~~~~~~~~~~~~~~~~236 237As requirements change over time (vulnerabilities are found in previously238trusted applications, keys roll, etcetera). Updating a kernel to change the239meet those security goals is not always a suitable option, as updates are not240always risk-free, and blocking a security update leaves systems vulnerable.241This means IPE requires a policy that can be completely updated (allowing242revocations of existing policy) from a source external to the kernel (allowing243policies to be updated without updating the kernel).244 245Additionally, since the kernel is stateless between invocations, and reading246policy files off the disk from kernel space is a bad idea(tm), then the247policy updates have to be done rebootlessly.248 249To allow an update from an external source, it could be potentially malicious,250so this policy needs to have a way to be identified as trusted. This is251done via a signature chained to a trust source in the kernel. Arbitrarily,252this is  the ``SYSTEM_TRUSTED_KEYRING``, a keyring that is initially253populated at kernel compile-time, as this matches the expectation that the254author of the compiled-in policy described above is the same entity that can255deploy policy updates.256 257Anti-Rollback / Anti-Replay258~~~~~~~~~~~~~~~~~~~~~~~~~~~259 260Over time, vulnerabilities are found and trusted resources may not be261trusted anymore. IPE's policy has no exception to this. There can be262instances where a mistaken policy author deploys an insecure policy,263before correcting it with a secure policy.264 265Assuming that as soon as the insecure policy is signed, and an attacker266acquires the insecure policy, IPE needs a way to prevent rollback267from the secure policy update to the insecure policy update.268 269Initially, IPE's policy can have a policy_version that states the270minimum required version across all policies that can be active on271the system. This will prevent rollback while the system is live.272 273.. WARNING::274 275  However, since the kernel is stateless across boots, this policy276  version will be reset to 0.0.0 on the next boot. System builders277  need to be aware of this, and ensure the new secure policies are278  deployed ASAP after a boot to ensure that the window of279  opportunity is minimal for an attacker to deploy the insecure policy.280 281Implicit Actions:282~~~~~~~~~~~~~~~~~283 284The issue of implicit actions only becomes visible when you consider285a mixed level of security bars across multiple operations in a system.286For example, consider a system that has strong integrity guarantees287over both the executable code, and specific *data files* on the system,288that were critical to its function. In this system, three types of policies289are possible:290 291  1. A policy in which failure to match any rules in the policy results292     in the action being denied.293  2. A policy in which failure to match any rules in the policy results294     in the action being allowed.295  3. A policy in which the action taken when no rules are matched is296     specified by the policy author.297 298The first option could make a policy like this::299 300  op=EXECUTE integrity_verified=YES action=ALLOW301 302In the example system, this works well for the executables, as all303executables should have integrity guarantees, without exception. The304issue becomes with the second requirement about specific data files.305This would result in a policy like this (assuming each line is306evaluated in order)::307 308  op=EXECUTE integrity_verified=YES action=ALLOW309 310  op=READ integrity_verified=NO label=critical_t action=DENY311  op=READ action=ALLOW312 313This is somewhat clear if you read the docs, understand the policy314is executed in order and that the default is a denial; however, the315last line effectively changes that default to an ALLOW. This is316required, because in a realistic system, there are some unverified317reads (imagine appending to a log file).318 319The second option, matching no rules results in an allow, is clearer320for the specific data files::321 322  op=READ integrity_verified=NO label=critical_t action=DENY323 324And, like the first option, falls short with the execution scenario,325effectively needing to override the default::326 327  op=EXECUTE integrity_verified=YES action=ALLOW328  op=EXECUTE action=DENY329 330  op=READ integrity_verified=NO label=critical_t action=DENY331 332This leaves the third option. Instead of making users be clever333and override the default with an empty rule, force the end-user334to consider what the appropriate default should be for their335scenario and explicitly state it::336 337  DEFAULT op=EXECUTE action=DENY338  op=EXECUTE integrity_verified=YES action=ALLOW339 340  DEFAULT op=READ action=ALLOW341  op=READ integrity_verified=NO label=critical_t action=DENY342 343Policy Debugging:344~~~~~~~~~~~~~~~~~345 346When developing a policy, it is useful to know what line of the policy347is being violated to reduce debugging costs; narrowing the scope of the348investigation to the exact line that resulted in the action. Some integrity349policy systems do not provide this information, instead providing the350information that was used in the evaluation. This then requires a correlation351with the policy to evaluate what went wrong.352 353Instead, IPE just emits the rule that was matched. This limits the scope354of the investigation to the exact policy line (in the case of a specific355rule), or the section (in the case of a DEFAULT). This decreases iteration356and investigation times when policy failures are observed while evaluating357policies.358 359IPE's policy engine is also designed in a way that it makes it obvious to360a human of how to investigate a policy failure. Each line is evaluated in361the sequence that is written, so the algorithm is very simple to follow362for humans to recreate the steps and could have caused the failure. In other363surveyed systems, optimizations occur (sorting rules, for instance) when loading364the policy. In those systems, it requires multiple steps to debug, and the365algorithm may not always be clear to the end-user without reading the code first.366 367Simplified Policy:368~~~~~~~~~~~~~~~~~~369 370Finally, IPE's policy is designed for sysadmins, not kernel developers. Instead371of covering individual LSM hooks (or syscalls), IPE covers operations. This means372instead of sysadmins needing to know that the syscalls ``mmap``, ``mprotect``,373``execve``, and ``uselib`` must have rules protecting them, they must simple know374that they want to restrict code execution. This limits the amount of bypasses that375could occur due to a lack of knowledge of the underlying system; whereas the376maintainers of IPE, being kernel developers can make the correct choice to determine377whether something maps to these operations, and under what conditions.378 379Implementation Notes380--------------------381 382Anonymous Memory383~~~~~~~~~~~~~~~~384 385Anonymous memory isn't treated any differently from any other access in IPE.386When anonymous memory is mapped with ``+X``, it still comes into the ``file_mmap``387or ``file_mprotect`` hook, but with a ``NULL`` file object. This is submitted to388the evaluation, like any other file. However, all current trust properties will389evaluate to false, as they are all file-based and the operation is not390associated with a file.391 392.. WARNING::393 394  This also occurs with the ``kernel_load_data`` hook, when the kernel is395  loading data from a userspace buffer that is not backed by a file. In this396  scenario all current trust properties will also evaluate to false.397 398Securityfs Interface399~~~~~~~~~~~~~~~~~~~~400 401The per-policy securityfs tree is somewhat unique. For example, for402a standard securityfs policy tree::403 404  MyPolicy405    |- active406    |- delete407    |- name408    |- pkcs7409    |- policy410    |- update411    |- version412 413The policy is stored in the ``->i_private`` data of the MyPolicy inode.414 415Tests416-----417 418IPE has KUnit Tests for the policy parser. Recommended kunitconfig::419 420  CONFIG_KUNIT=y421  CONFIG_SECURITY=y422  CONFIG_SECURITYFS=y423  CONFIG_PKCS7_MESSAGE_PARSER=y424  CONFIG_SYSTEM_DATA_VERIFICATION=y425  CONFIG_FS_VERITY=y426  CONFIG_FS_VERITY_BUILTIN_SIGNATURES=y427  CONFIG_BLOCK=y428  CONFIG_MD=y429  CONFIG_BLK_DEV_DM=y430  CONFIG_DM_VERITY=y431  CONFIG_DM_VERITY_VERIFY_ROOTHASH_SIG=y432  CONFIG_NET=y433  CONFIG_AUDIT=y434  CONFIG_AUDITSYSCALL=y435  CONFIG_BLK_DEV_INITRD=y436 437  CONFIG_SECURITY_IPE=y438  CONFIG_IPE_PROP_DM_VERITY=y439  CONFIG_IPE_PROP_DM_VERITY_SIGNATURE=y440  CONFIG_IPE_PROP_FS_VERITY=y441  CONFIG_IPE_PROP_FS_VERITY_BUILTIN_SIG=y442  CONFIG_SECURITY_IPE_KUNIT_TEST=y443 444In addition, IPE has a python based integration445`test suite <https://github.com/microsoft/ipe/tree/test-suite>`_ that446can test both user interfaces and enforcement functionalities.447