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