Pierre-Clément Tosi | 10bea6f | 2024-11-26 22:04:10 +0000 | [diff] [blame] | 1 | // Copyright 2024, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | //! Support for guest-specific rollback protection (RBP). |
| 16 | |
| 17 | use crate::dice::PartialInputs; |
| 18 | use crate::entry::RebootReason; |
| 19 | use crate::instance::EntryBody; |
| 20 | use crate::instance::Error as InstanceError; |
| 21 | use crate::instance::{get_recorded_entry, record_instance_entry}; |
| 22 | use cstr::cstr; |
| 23 | use diced_open_dice::Hidden; |
| 24 | use libfdt::{Fdt, FdtNode}; |
| 25 | use log::{error, info}; |
| 26 | use pvmfw_avb::Capability; |
| 27 | use pvmfw_avb::VerifiedBootData; |
| 28 | use virtio_drivers::transport::pci::bus::PciRoot; |
| 29 | use vmbase::rand; |
| 30 | |
| 31 | /// Performs RBP based on the input payload, current DICE chain, and host-controlled platform. |
| 32 | /// |
| 33 | /// On success, returns a tuple containing: |
| 34 | /// - `new_instance`: true if a new entry was created using the legacy instance.img solution; |
| 35 | /// - `salt`: the salt representing the instance, to be used during DICE derivation; |
| 36 | /// - `defer_rollback_protection`: if RBP is being deferred. |
| 37 | pub fn perform_rollback_protection( |
| 38 | fdt: &Fdt, |
| 39 | verified_boot_data: &VerifiedBootData, |
| 40 | dice_inputs: &PartialInputs, |
| 41 | pci_root: &mut PciRoot, |
| 42 | cdi_seal: &[u8], |
| 43 | instance_hash: Option<Hidden>, |
| 44 | ) -> Result<(bool, Hidden, bool), RebootReason> { |
| 45 | let defer_rollback_protection = should_defer_rollback_protection(fdt)? |
| 46 | && verified_boot_data.has_capability(Capability::SecretkeeperProtection); |
| 47 | let (new_instance, salt) = if defer_rollback_protection { |
| 48 | info!("Guest OS is capable of Secretkeeper protection, deferring rollback protection"); |
| 49 | // rollback_index of the image is used as security_version and is expected to be > 0 to |
| 50 | // discourage implicit allocation. |
| 51 | if verified_boot_data.rollback_index == 0 { |
| 52 | error!("Expected positive rollback_index, found 0"); |
| 53 | return Err(RebootReason::InvalidPayload); |
| 54 | }; |
| 55 | (false, instance_hash.unwrap()) |
| 56 | } else if verified_boot_data.has_capability(Capability::RemoteAttest) { |
| 57 | info!("Service VM capable of remote attestation detected, performing version checks"); |
| 58 | if service_vm_version::VERSION != verified_boot_data.rollback_index { |
| 59 | // For RKP VM, we only boot if the version in the AVB footer of its kernel matches |
| 60 | // the one embedded in pvmfw at build time. |
| 61 | // This prevents the pvmfw from booting a roll backed RKP VM. |
| 62 | error!( |
| 63 | "Service VM version mismatch: expected {}, found {}", |
| 64 | service_vm_version::VERSION, |
| 65 | verified_boot_data.rollback_index |
| 66 | ); |
| 67 | return Err(RebootReason::InvalidPayload); |
| 68 | } |
| 69 | (false, instance_hash.unwrap()) |
| 70 | } else if verified_boot_data.has_capability(Capability::TrustySecurityVm) { |
| 71 | // The rollback protection of Trusty VMs are handled by AuthMgr, so we don't need to |
| 72 | // handle it here. |
| 73 | info!("Trusty Security VM detected"); |
| 74 | (false, instance_hash.unwrap()) |
| 75 | } else { |
| 76 | info!("Fallback to instance.img based rollback checks"); |
| 77 | let (recorded_entry, mut instance_img, header_index) = |
| 78 | get_recorded_entry(pci_root, cdi_seal).map_err(|e| { |
| 79 | error!("Failed to get entry from instance.img: {e}"); |
| 80 | RebootReason::InternalError |
| 81 | })?; |
| 82 | let (new_instance, salt) = if let Some(entry) = recorded_entry { |
| 83 | check_dice_measurements_match_entry(dice_inputs, &entry)?; |
| 84 | let salt = instance_hash.unwrap_or(entry.salt); |
| 85 | (false, salt) |
| 86 | } else { |
| 87 | // New instance! |
| 88 | let salt = instance_hash.map_or_else(rand::random_array, Ok).map_err(|e| { |
| 89 | error!("Failed to generated instance.img salt: {e}"); |
| 90 | RebootReason::InternalError |
| 91 | })?; |
| 92 | |
| 93 | let entry = EntryBody::new(dice_inputs, &salt); |
| 94 | record_instance_entry(&entry, cdi_seal, &mut instance_img, header_index).map_err( |
| 95 | |e| { |
| 96 | error!("Failed to get recorded entry in instance.img: {e}"); |
| 97 | RebootReason::InternalError |
| 98 | }, |
| 99 | )?; |
| 100 | (true, salt) |
| 101 | }; |
| 102 | (new_instance, salt) |
| 103 | }; |
| 104 | |
| 105 | Ok((new_instance, salt, defer_rollback_protection)) |
| 106 | } |
| 107 | |
| 108 | fn check_dice_measurements_match_entry( |
| 109 | dice_inputs: &PartialInputs, |
| 110 | entry: &EntryBody, |
| 111 | ) -> Result<(), RebootReason> { |
| 112 | ensure_dice_measurements_match_entry(dice_inputs, entry).map_err(|e| { |
| 113 | error!( |
| 114 | "Dice measurements do not match recorded entry. \ |
| 115 | This may be because of update: {e}" |
| 116 | ); |
| 117 | RebootReason::InternalError |
| 118 | })?; |
| 119 | |
| 120 | Ok(()) |
| 121 | } |
| 122 | |
| 123 | fn ensure_dice_measurements_match_entry( |
| 124 | dice_inputs: &PartialInputs, |
| 125 | entry: &EntryBody, |
| 126 | ) -> Result<(), InstanceError> { |
| 127 | if entry.code_hash != dice_inputs.code_hash { |
| 128 | Err(InstanceError::RecordedCodeHashMismatch) |
| 129 | } else if entry.auth_hash != dice_inputs.auth_hash { |
| 130 | Err(InstanceError::RecordedAuthHashMismatch) |
| 131 | } else if entry.mode() != dice_inputs.mode { |
| 132 | Err(InstanceError::RecordedDiceModeMismatch) |
| 133 | } else { |
| 134 | Ok(()) |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | fn should_defer_rollback_protection(fdt: &Fdt) -> Result<bool, RebootReason> { |
| 139 | let node = avf_untrusted_node(fdt)?; |
| 140 | let defer_rbp = node |
| 141 | .getprop(cstr!("defer-rollback-protection")) |
| 142 | .map_err(|e| { |
| 143 | error!("Failed to get defer-rollback-protection property in DT: {e}"); |
| 144 | RebootReason::InvalidFdt |
| 145 | })? |
| 146 | .is_some(); |
| 147 | Ok(defer_rbp) |
| 148 | } |
| 149 | |
| 150 | fn avf_untrusted_node(fdt: &Fdt) -> Result<FdtNode, RebootReason> { |
| 151 | let node = fdt.node(cstr!("/avf/untrusted")).map_err(|e| { |
| 152 | error!("Failed to get /avf/untrusted node: {e}"); |
| 153 | RebootReason::InvalidFdt |
| 154 | })?; |
| 155 | node.ok_or_else(|| { |
| 156 | error!("/avf/untrusted node is missing in DT"); |
| 157 | RebootReason::InvalidFdt |
| 158 | }) |
| 159 | } |