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; |
Pierre-Clément Tosi | 3729f65 | 2024-11-19 15:25:37 +0000 | [diff] [blame] | 19 | use crate::fdt::read_defer_rollback_protection; |
Pierre-Clément Tosi | 10bea6f | 2024-11-26 22:04:10 +0000 | [diff] [blame] | 20 | use crate::instance::EntryBody; |
| 21 | use crate::instance::Error as InstanceError; |
| 22 | use crate::instance::{get_recorded_entry, record_instance_entry}; |
Pierre-Clément Tosi | 10bea6f | 2024-11-26 22:04:10 +0000 | [diff] [blame] | 23 | use diced_open_dice::Hidden; |
Pierre-Clément Tosi | 3729f65 | 2024-11-19 15:25:37 +0000 | [diff] [blame] | 24 | use libfdt::Fdt; |
Pierre-Clément Tosi | 10bea6f | 2024-11-26 22:04:10 +0000 | [diff] [blame] | 25 | use log::{error, info}; |
| 26 | use pvmfw_avb::Capability; |
| 27 | use pvmfw_avb::VerifiedBootData; |
Andrew Walbran | eb75fb9 | 2024-12-13 15:37:30 +0000 | [diff] [blame] | 28 | use virtio_drivers::transport::pci::bus::{ConfigurationAccess, PciRoot}; |
Pierre-Clément Tosi | ba666fe | 2024-11-19 16:44:33 +0000 | [diff] [blame] | 29 | use vmbase::fdt::{pci::PciInfo, SwiotlbInfo}; |
| 30 | use vmbase::memory::init_shared_pool; |
Pierre-Clément Tosi | 10bea6f | 2024-11-26 22:04:10 +0000 | [diff] [blame] | 31 | use vmbase::rand; |
Pierre-Clément Tosi | ba666fe | 2024-11-19 16:44:33 +0000 | [diff] [blame] | 32 | use vmbase::virtio::pci; |
Pierre-Clément Tosi | 10bea6f | 2024-11-26 22:04:10 +0000 | [diff] [blame] | 33 | |
| 34 | /// Performs RBP based on the input payload, current DICE chain, and host-controlled platform. |
| 35 | /// |
| 36 | /// On success, returns a tuple containing: |
Pierre-Clément Tosi | 495c67a | 2024-11-27 16:32:04 +0000 | [diff] [blame] | 37 | /// - `new_instance`: true if the legacy instance.img solution was used and a new entry created; |
Pierre-Clément Tosi | 10bea6f | 2024-11-26 22:04:10 +0000 | [diff] [blame] | 38 | /// - `salt`: the salt representing the instance, to be used during DICE derivation; |
| 39 | /// - `defer_rollback_protection`: if RBP is being deferred. |
| 40 | pub fn perform_rollback_protection( |
| 41 | fdt: &Fdt, |
| 42 | verified_boot_data: &VerifiedBootData, |
| 43 | dice_inputs: &PartialInputs, |
Pierre-Clément Tosi | 10bea6f | 2024-11-26 22:04:10 +0000 | [diff] [blame] | 44 | cdi_seal: &[u8], |
| 45 | instance_hash: Option<Hidden>, |
| 46 | ) -> Result<(bool, Hidden, bool), RebootReason> { |
Pierre-Clément Tosi | 64f757b | 2025-01-22 16:13:18 +0000 | [diff] [blame] | 47 | if (should_defer_rollback_protection(fdt)? |
| 48 | && verified_boot_data.has_capability(Capability::SecretkeeperProtection)) |
| 49 | || verified_boot_data.has_capability(Capability::TrustySecurityVm) |
Pierre-Clément Tosi | 495c67a | 2024-11-27 16:32:04 +0000 | [diff] [blame] | 50 | { |
| 51 | perform_deferred_rollback_protection(verified_boot_data)?; |
| 52 | Ok((false, instance_hash.unwrap(), true)) |
Pierre-Clément Tosi | 10bea6f | 2024-11-26 22:04:10 +0000 | [diff] [blame] | 53 | } else if verified_boot_data.has_capability(Capability::RemoteAttest) { |
Pierre-Clément Tosi | 495c67a | 2024-11-27 16:32:04 +0000 | [diff] [blame] | 54 | perform_fixed_index_rollback_protection(verified_boot_data)?; |
| 55 | Ok((false, instance_hash.unwrap(), false)) |
Pierre-Clément Tosi | 10bea6f | 2024-11-26 22:04:10 +0000 | [diff] [blame] | 56 | } else { |
Pierre-Clément Tosi | ba666fe | 2024-11-19 16:44:33 +0000 | [diff] [blame] | 57 | perform_legacy_rollback_protection(fdt, dice_inputs, cdi_seal, instance_hash) |
Pierre-Clément Tosi | 495c67a | 2024-11-27 16:32:04 +0000 | [diff] [blame] | 58 | } |
| 59 | } |
Pierre-Clément Tosi | 10bea6f | 2024-11-26 22:04:10 +0000 | [diff] [blame] | 60 | |
Pierre-Clément Tosi | 495c67a | 2024-11-27 16:32:04 +0000 | [diff] [blame] | 61 | fn perform_deferred_rollback_protection( |
| 62 | verified_boot_data: &VerifiedBootData, |
| 63 | ) -> Result<(), RebootReason> { |
| 64 | info!("Deferring rollback protection"); |
| 65 | // rollback_index of the image is used as security_version and is expected to be > 0 to |
| 66 | // discourage implicit allocation. |
| 67 | if verified_boot_data.rollback_index == 0 { |
| 68 | error!("Expected positive rollback_index, found 0"); |
| 69 | Err(RebootReason::InvalidPayload) |
| 70 | } else { |
| 71 | Ok(()) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | fn perform_fixed_index_rollback_protection( |
| 76 | verified_boot_data: &VerifiedBootData, |
| 77 | ) -> Result<(), RebootReason> { |
| 78 | info!("Performing fixed-index rollback protection"); |
| 79 | let fixed_index = service_vm_version::VERSION; |
| 80 | let index = verified_boot_data.rollback_index; |
| 81 | if index != fixed_index { |
| 82 | error!("Rollback index mismatch: expected {fixed_index}, found {index}"); |
| 83 | Err(RebootReason::InvalidPayload) |
| 84 | } else { |
| 85 | Ok(()) |
| 86 | } |
| 87 | } |
| 88 | |
Pierre-Clément Tosi | 495c67a | 2024-11-27 16:32:04 +0000 | [diff] [blame] | 89 | /// Performs RBP using instance.img where updates require clearing old entries, causing new CDIs. |
| 90 | fn perform_legacy_rollback_protection( |
Pierre-Clément Tosi | ba666fe | 2024-11-19 16:44:33 +0000 | [diff] [blame] | 91 | fdt: &Fdt, |
Pierre-Clément Tosi | 495c67a | 2024-11-27 16:32:04 +0000 | [diff] [blame] | 92 | dice_inputs: &PartialInputs, |
Pierre-Clément Tosi | 495c67a | 2024-11-27 16:32:04 +0000 | [diff] [blame] | 93 | cdi_seal: &[u8], |
| 94 | instance_hash: Option<Hidden>, |
| 95 | ) -> Result<(bool, Hidden, bool), RebootReason> { |
| 96 | info!("Fallback to instance.img based rollback checks"); |
Pierre-Clément Tosi | ba666fe | 2024-11-19 16:44:33 +0000 | [diff] [blame] | 97 | let mut pci_root = initialize_instance_img_device(fdt)?; |
| 98 | let (recorded_entry, mut instance_img, header_index) = |
| 99 | get_recorded_entry(&mut pci_root, cdi_seal).map_err(|e| { |
| 100 | error!("Failed to get entry from instance.img: {e}"); |
| 101 | RebootReason::InternalError |
| 102 | })?; |
Pierre-Clément Tosi | 495c67a | 2024-11-27 16:32:04 +0000 | [diff] [blame] | 103 | let (new_instance, salt) = if let Some(entry) = recorded_entry { |
| 104 | check_dice_measurements_match_entry(dice_inputs, &entry)?; |
| 105 | let salt = instance_hash.unwrap_or(entry.salt); |
| 106 | (false, salt) |
| 107 | } else { |
| 108 | // New instance! |
| 109 | let salt = instance_hash.map_or_else(rand::random_array, Ok).map_err(|e| { |
| 110 | error!("Failed to generated instance.img salt: {e}"); |
| 111 | RebootReason::InternalError |
| 112 | })?; |
| 113 | |
| 114 | let entry = EntryBody::new(dice_inputs, &salt); |
| 115 | record_instance_entry(&entry, cdi_seal, &mut instance_img, header_index).map_err(|e| { |
| 116 | error!("Failed to get recorded entry in instance.img: {e}"); |
| 117 | RebootReason::InternalError |
| 118 | })?; |
| 119 | (true, salt) |
Pierre-Clément Tosi | 10bea6f | 2024-11-26 22:04:10 +0000 | [diff] [blame] | 120 | }; |
Pierre-Clément Tosi | 495c67a | 2024-11-27 16:32:04 +0000 | [diff] [blame] | 121 | Ok((new_instance, salt, false)) |
Pierre-Clément Tosi | 10bea6f | 2024-11-26 22:04:10 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | fn check_dice_measurements_match_entry( |
| 125 | dice_inputs: &PartialInputs, |
| 126 | entry: &EntryBody, |
| 127 | ) -> Result<(), RebootReason> { |
| 128 | ensure_dice_measurements_match_entry(dice_inputs, entry).map_err(|e| { |
| 129 | error!( |
| 130 | "Dice measurements do not match recorded entry. \ |
| 131 | This may be because of update: {e}" |
| 132 | ); |
| 133 | RebootReason::InternalError |
| 134 | })?; |
| 135 | |
| 136 | Ok(()) |
| 137 | } |
| 138 | |
| 139 | fn ensure_dice_measurements_match_entry( |
| 140 | dice_inputs: &PartialInputs, |
| 141 | entry: &EntryBody, |
| 142 | ) -> Result<(), InstanceError> { |
| 143 | if entry.code_hash != dice_inputs.code_hash { |
| 144 | Err(InstanceError::RecordedCodeHashMismatch) |
| 145 | } else if entry.auth_hash != dice_inputs.auth_hash { |
| 146 | Err(InstanceError::RecordedAuthHashMismatch) |
| 147 | } else if entry.mode() != dice_inputs.mode { |
| 148 | Err(InstanceError::RecordedDiceModeMismatch) |
| 149 | } else { |
| 150 | Ok(()) |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | fn should_defer_rollback_protection(fdt: &Fdt) -> Result<bool, RebootReason> { |
Pierre-Clément Tosi | 3729f65 | 2024-11-19 15:25:37 +0000 | [diff] [blame] | 155 | let defer_rbp = read_defer_rollback_protection(fdt).map_err(|e| { |
| 156 | error!("Failed to get defer-rollback-protection property in DT: {e}"); |
Pierre-Clément Tosi | 10bea6f | 2024-11-26 22:04:10 +0000 | [diff] [blame] | 157 | RebootReason::InvalidFdt |
| 158 | })?; |
Pierre-Clément Tosi | 3729f65 | 2024-11-19 15:25:37 +0000 | [diff] [blame] | 159 | Ok(defer_rbp.is_some()) |
Pierre-Clément Tosi | 10bea6f | 2024-11-26 22:04:10 +0000 | [diff] [blame] | 160 | } |
Pierre-Clément Tosi | ba666fe | 2024-11-19 16:44:33 +0000 | [diff] [blame] | 161 | |
| 162 | /// Set up PCI bus and VirtIO-blk device containing the instance.img partition. |
Andrew Walbran | eb75fb9 | 2024-12-13 15:37:30 +0000 | [diff] [blame] | 163 | fn initialize_instance_img_device( |
| 164 | fdt: &Fdt, |
| 165 | ) -> Result<PciRoot<impl ConfigurationAccess>, RebootReason> { |
Pierre-Clément Tosi | ba666fe | 2024-11-19 16:44:33 +0000 | [diff] [blame] | 166 | let pci_info = PciInfo::from_fdt(fdt).map_err(|e| { |
| 167 | error!("Failed to detect PCI from DT: {e}"); |
| 168 | RebootReason::InvalidFdt |
| 169 | })?; |
| 170 | let swiotlb_range = SwiotlbInfo::new_from_fdt(fdt) |
| 171 | .map_err(|e| { |
| 172 | error!("Failed to detect swiotlb from DT: {e}"); |
| 173 | RebootReason::InvalidFdt |
| 174 | })? |
| 175 | .and_then(|info| info.fixed_range()); |
| 176 | |
| 177 | let pci_root = pci::initialize(pci_info).map_err(|e| { |
| 178 | error!("Failed to initialize PCI: {e}"); |
| 179 | RebootReason::InternalError |
| 180 | })?; |
| 181 | init_shared_pool(swiotlb_range).map_err(|e| { |
| 182 | error!("Failed to initialize shared pool: {e}"); |
| 183 | RebootReason::InternalError |
| 184 | })?; |
| 185 | |
| 186 | Ok(pci_root) |
| 187 | } |