Alan Stokes | 1125e01 | 2023-10-13 12:31:10 +0100 | [diff] [blame] | 1 | // Copyright 2023 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 | |
Alan Stokes | 1508df2 | 2023-12-04 11:31:21 +0000 | [diff] [blame] | 15 | use crate::instance::{ApexData, ApkData, MicrodroidData}; |
Alan Stokes | 0375496 | 2023-11-06 15:36:09 +0000 | [diff] [blame] | 16 | use crate::payload::{get_apex_data_from_payload, to_metadata}; |
Shikha Panwar | ad0e535 | 2024-12-11 17:36:43 +0000 | [diff] [blame] | 17 | use crate::MicrodroidError; |
Alan Stokes | 1125e01 | 2023-10-13 12:31:10 +0100 | [diff] [blame] | 18 | use anyhow::{anyhow, ensure, Context, Result}; |
Nikita Ioffe | a73451a | 2025-01-28 17:09:22 +0000 | [diff] [blame^] | 19 | use apkmanifest::{get_manifest_info, ApkManifestInfo}; |
Alan Stokes | 1508df2 | 2023-12-04 11:31:21 +0000 | [diff] [blame] | 20 | use apkverify::{extract_signed_data, verify, V4Signature}; |
Alan Stokes | 1125e01 | 2023-10-13 12:31:10 +0100 | [diff] [blame] | 21 | use glob::glob; |
| 22 | use itertools::sorted; |
| 23 | use log::{info, warn}; |
Alan Stokes | 0375496 | 2023-11-06 15:36:09 +0000 | [diff] [blame] | 24 | use microdroid_metadata::{write_metadata, Metadata}; |
Alan Stokes | 1508df2 | 2023-12-04 11:31:21 +0000 | [diff] [blame] | 25 | use openssl::sha::sha512; |
Alan Stokes | 1125e01 | 2023-10-13 12:31:10 +0100 | [diff] [blame] | 26 | use rustutils::system_properties; |
Alan Stokes | 0375496 | 2023-11-06 15:36:09 +0000 | [diff] [blame] | 27 | use std::fs::OpenOptions; |
Alan Stokes | 1125e01 | 2023-10-13 12:31:10 +0100 | [diff] [blame] | 28 | use std::path::Path; |
| 29 | use std::process::{Child, Command}; |
| 30 | use std::str; |
| 31 | use std::time::SystemTime; |
| 32 | |
| 33 | pub const DM_MOUNTED_APK_PATH: &str = "/dev/block/mapper/microdroid-apk"; |
| 34 | |
| 35 | const MAIN_APK_PATH: &str = "/dev/block/by-name/microdroid-apk"; |
| 36 | const MAIN_APK_IDSIG_PATH: &str = "/dev/block/by-name/microdroid-apk-idsig"; |
| 37 | const MAIN_APK_DEVICE_NAME: &str = "microdroid-apk"; |
| 38 | const EXTRA_APK_PATH_PATTERN: &str = "/dev/block/by-name/extra-apk-*"; |
| 39 | const EXTRA_IDSIG_PATH_PATTERN: &str = "/dev/block/by-name/extra-idsig-*"; |
| 40 | |
| 41 | const APKDMVERITY_BIN: &str = "/system/bin/apkdmverity"; |
| 42 | |
| 43 | /// Verify payload before executing it. For APK payload, Full verification (which is slow) is done |
| 44 | /// when the root_hash values from the idsig file and the instance disk are different. This function |
| 45 | /// returns the verified root hash (for APK payload) and pubkeys (for APEX payloads) that can be |
| 46 | /// saved to the instance disk. |
| 47 | pub fn verify_payload( |
| 48 | metadata: &Metadata, |
| 49 | saved_data: Option<&MicrodroidData>, |
| 50 | ) -> Result<MicrodroidData> { |
| 51 | let start_time = SystemTime::now(); |
| 52 | |
| 53 | // Verify main APK |
| 54 | let root_hash_from_idsig = get_apk_root_hash_from_idsig(MAIN_APK_IDSIG_PATH)?; |
| 55 | let root_hash_trustful = |
| 56 | saved_data.map(|d| d.apk_data.root_hash_eq(root_hash_from_idsig.as_ref())).unwrap_or(false); |
| 57 | |
| 58 | // If root_hash can be trusted, pass it to apkdmverity so that it uses the passed root_hash |
| 59 | // instead of the value read from the idsig file. |
| 60 | let main_apk_argument = { |
| 61 | ApkDmverityArgument { |
| 62 | apk: MAIN_APK_PATH, |
| 63 | idsig: MAIN_APK_IDSIG_PATH, |
| 64 | name: MAIN_APK_DEVICE_NAME, |
| 65 | saved_root_hash: if root_hash_trustful { |
| 66 | Some(root_hash_from_idsig.as_ref()) |
| 67 | } else { |
| 68 | None |
| 69 | }, |
| 70 | } |
| 71 | }; |
| 72 | let mut apkdmverity_arguments = vec![main_apk_argument]; |
| 73 | |
| 74 | // Verify extra APKs |
| 75 | // For now, we can't read the payload config, so glob APKs and idsigs. |
| 76 | // Later, we'll see if it matches with the payload config. |
| 77 | |
| 78 | // sort globbed paths to match apks (extra-apk-{idx}) and idsigs (extra-idsig-{idx}) |
| 79 | // e.g. "extra-apk-0" corresponds to "extra-idsig-0" |
| 80 | let extra_apks = |
| 81 | sorted(glob(EXTRA_APK_PATH_PATTERN)?.collect::<Result<Vec<_>, _>>()?).collect::<Vec<_>>(); |
| 82 | let extra_idsigs = |
| 83 | sorted(glob(EXTRA_IDSIG_PATH_PATTERN)?.collect::<Result<Vec<_>, _>>()?).collect::<Vec<_>>(); |
| 84 | ensure!( |
| 85 | extra_apks.len() == extra_idsigs.len(), |
| 86 | "Extra apks/idsigs mismatch: {} apks but {} idsigs", |
| 87 | extra_apks.len(), |
| 88 | extra_idsigs.len() |
| 89 | ); |
| 90 | |
| 91 | let extra_root_hashes_from_idsig: Vec<_> = extra_idsigs |
| 92 | .iter() |
| 93 | .map(|idsig| { |
| 94 | get_apk_root_hash_from_idsig(idsig).expect("Can't find root hash from extra idsig") |
| 95 | }) |
| 96 | .collect(); |
| 97 | |
| 98 | let extra_root_hashes_trustful: Vec<_> = if let Some(data) = saved_data { |
| 99 | extra_root_hashes_from_idsig |
| 100 | .iter() |
| 101 | .enumerate() |
| 102 | .map(|(i, root_hash)| data.extra_apk_root_hash_eq(i, root_hash)) |
| 103 | .collect() |
| 104 | } else { |
| 105 | vec![false; extra_root_hashes_from_idsig.len()] |
| 106 | }; |
| 107 | let extra_apk_names: Vec<_> = |
| 108 | (0..extra_apks.len()).map(|i| format!("extra-apk-{}", i)).collect(); |
| 109 | |
| 110 | for (i, extra_apk) in extra_apks.iter().enumerate() { |
| 111 | apkdmverity_arguments.push({ |
| 112 | ApkDmverityArgument { |
| 113 | apk: extra_apk.to_str().unwrap(), |
| 114 | idsig: extra_idsigs[i].to_str().unwrap(), |
| 115 | name: &extra_apk_names[i], |
| 116 | saved_root_hash: if extra_root_hashes_trustful[i] { |
| 117 | Some(&extra_root_hashes_from_idsig[i]) |
| 118 | } else { |
| 119 | None |
| 120 | }, |
| 121 | } |
| 122 | }); |
| 123 | } |
| 124 | |
| 125 | // Start apkdmverity and wait for the dm-verify block |
| 126 | let mut apkdmverity_child = run_apkdmverity(&apkdmverity_arguments)?; |
| 127 | |
| 128 | // While waiting for apkdmverity to mount APK, gathers public keys and root digests from |
| 129 | // APEX payload. |
| 130 | let apex_data_from_payload = get_apex_data_from_payload(metadata)?; |
| 131 | |
Alan Stokes | 160f394 | 2023-11-10 10:29:00 +0000 | [diff] [blame] | 132 | // To prevent a TOCTOU attack, we need to make sure that when apexd verifies & mounts the |
| 133 | // APEXes it sees the same ones that we just read - so we write the metadata we just collected |
| 134 | // to a file (that the host can't access) that apexd will then verify against. See b/199371341. |
| 135 | write_apex_payload_data(saved_data, &apex_data_from_payload)?; |
Alan Stokes | 1125e01 | 2023-10-13 12:31:10 +0100 | [diff] [blame] | 136 | |
Alan Stokes | 9a7f67e | 2023-11-07 09:37:40 +0000 | [diff] [blame] | 137 | if cfg!(not(dice_changes)) { |
| 138 | // Start apexd to activate APEXes |
| 139 | system_properties::write("ctl.start", "apexd-vm")?; |
| 140 | } |
Alan Stokes | 1125e01 | 2023-10-13 12:31:10 +0100 | [diff] [blame] | 141 | |
| 142 | // TODO(inseob): add timeout |
| 143 | apkdmverity_child.wait()?; |
| 144 | |
| 145 | // Do the full verification if the root_hash is un-trustful. This requires the full scanning of |
| 146 | // the APK file and therefore can be very slow if the APK is large. Note that this step is |
| 147 | // taken only when the root_hash is un-trustful which can be either when this is the first boot |
| 148 | // of the VM or APK was updated in the host. |
| 149 | // TODO(jooyung): consider multithreading to make this faster |
Alan Stokes | 9b8b8ec | 2023-10-13 15:58:11 +0100 | [diff] [blame] | 150 | |
| 151 | let main_apk_data = |
| 152 | get_data_from_apk(DM_MOUNTED_APK_PATH, root_hash_from_idsig, root_hash_trustful)?; |
| 153 | |
Alan Stokes | 1125e01 | 2023-10-13 12:31:10 +0100 | [diff] [blame] | 154 | let extra_apks_data = extra_root_hashes_from_idsig |
| 155 | .into_iter() |
| 156 | .enumerate() |
| 157 | .map(|(i, extra_root_hash)| { |
| 158 | let mount_path = format!("/dev/block/mapper/{}", &extra_apk_names[i]); |
Alan Stokes | 9b8b8ec | 2023-10-13 15:58:11 +0100 | [diff] [blame] | 159 | get_data_from_apk(&mount_path, extra_root_hash, extra_root_hashes_trustful[i]) |
Alan Stokes | 1125e01 | 2023-10-13 12:31:10 +0100 | [diff] [blame] | 160 | }) |
| 161 | .collect::<Result<Vec<_>>>()?; |
| 162 | |
| 163 | info!("payload verification successful. took {:#?}", start_time.elapsed().unwrap()); |
| 164 | |
Alan Stokes | 9b8b8ec | 2023-10-13 15:58:11 +0100 | [diff] [blame] | 165 | // At this point, we can ensure that the root hashes from the idsig files are trusted, either |
| 166 | // because we have fully verified the APK signature (and apkdmverity checks all the data we |
| 167 | // verified is consistent with the root hash) or because we have the saved APK data which will |
| 168 | // be checked as identical to the data we have verified. |
| 169 | |
Alan Stokes | 1125e01 | 2023-10-13 12:31:10 +0100 | [diff] [blame] | 170 | Ok(MicrodroidData { |
Alan Stokes | 9b8b8ec | 2023-10-13 15:58:11 +0100 | [diff] [blame] | 171 | apk_data: main_apk_data, |
Alan Stokes | 1125e01 | 2023-10-13 12:31:10 +0100 | [diff] [blame] | 172 | extra_apks_data, |
| 173 | apex_data: apex_data_from_payload, |
| 174 | }) |
| 175 | } |
| 176 | |
Nikita Ioffe | a73451a | 2025-01-28 17:09:22 +0000 | [diff] [blame^] | 177 | fn validate_manifest_info(info: &ApkManifestInfo) -> Result<()> { |
| 178 | ensure!( |
| 179 | info.has_relaxed_rollback_protection_permission == info.rollback_index.is_some(), |
| 180 | MicrodroidError::PayloadVerificationFailed(String::from("to opt in relaxed rollback protection scheme manifest must request android.permission.USE_RELAXED_MICRODROID_ROLLBACK_PROTECTION permission and set the android.system.virtualmachine.ROLLBACK_INDEX property")) |
| 181 | ); |
| 182 | Ok(()) |
| 183 | } |
| 184 | |
Alan Stokes | 9b8b8ec | 2023-10-13 15:58:11 +0100 | [diff] [blame] | 185 | fn get_data_from_apk( |
| 186 | apk_path: &str, |
Alan Stokes | 1508df2 | 2023-12-04 11:31:21 +0000 | [diff] [blame] | 187 | root_hash: Box<[u8]>, |
Alan Stokes | 9b8b8ec | 2023-10-13 15:58:11 +0100 | [diff] [blame] | 188 | root_hash_trustful: bool, |
| 189 | ) -> Result<ApkData> { |
Alan Stokes | 1508df2 | 2023-12-04 11:31:21 +0000 | [diff] [blame] | 190 | let cert_hash = get_cert_hash_from_apk(apk_path, root_hash_trustful)?.to_vec(); |
Alan Stokes | 9b8b8ec | 2023-10-13 15:58:11 +0100 | [diff] [blame] | 191 | // Read package name etc from the APK manifest. In the unlikely event that they aren't present |
| 192 | // we use the default values. We simply put these values in the DICE node for the payload, and |
| 193 | // users of that can decide how to handle blank information - there's no reason for us |
| 194 | // to fail starting a VM even with such a weird APK. |
| 195 | let manifest_info = get_manifest_info(apk_path) |
| 196 | .map_err(|e| warn!("Failed to read manifest info from APK: {e:?}")) |
| 197 | .unwrap_or_default(); |
| 198 | |
Nikita Ioffe | a73451a | 2025-01-28 17:09:22 +0000 | [diff] [blame^] | 199 | validate_manifest_info(&manifest_info)?; |
| 200 | |
Alan Stokes | 9b8b8ec | 2023-10-13 15:58:11 +0100 | [diff] [blame] | 201 | Ok(ApkData { |
Alan Stokes | 1508df2 | 2023-12-04 11:31:21 +0000 | [diff] [blame] | 202 | root_hash: root_hash.into(), |
| 203 | cert_hash, |
Alan Stokes | 9b8b8ec | 2023-10-13 15:58:11 +0100 | [diff] [blame] | 204 | package_name: manifest_info.package, |
| 205 | version_code: manifest_info.version_code, |
| 206 | }) |
| 207 | } |
| 208 | |
Alan Stokes | 0375496 | 2023-11-06 15:36:09 +0000 | [diff] [blame] | 209 | fn write_apex_payload_data( |
| 210 | saved_data: Option<&MicrodroidData>, |
| 211 | apex_data_from_payload: &[ApexData], |
| 212 | ) -> Result<()> { |
| 213 | if let Some(saved_apex_data) = saved_data.map(|d| &d.apex_data) { |
| 214 | // We don't support APEX updates. (assuming that update will change root digest) |
| 215 | ensure!( |
| 216 | saved_apex_data == apex_data_from_payload, |
| 217 | MicrodroidError::PayloadChanged(String::from("APEXes have changed.")) |
| 218 | ); |
Alan Stokes | 0375496 | 2023-11-06 15:36:09 +0000 | [diff] [blame] | 219 | } |
Alan Stokes | 160f394 | 2023-11-10 10:29:00 +0000 | [diff] [blame] | 220 | let apex_metadata = to_metadata(apex_data_from_payload); |
| 221 | // Pass metadata(with public keys and root digests) to apexd so that it uses the passed |
| 222 | // metadata instead of the default one (/dev/block/by-name/payload-metadata) |
| 223 | OpenOptions::new() |
| 224 | .create_new(true) |
| 225 | .write(true) |
| 226 | .open("/apex/vm-payload-metadata") |
| 227 | .context("Failed to open /apex/vm-payload-metadata") |
| 228 | .and_then(|f| write_metadata(&apex_metadata, f))?; |
| 229 | |
Alan Stokes | 0375496 | 2023-11-06 15:36:09 +0000 | [diff] [blame] | 230 | Ok(()) |
| 231 | } |
| 232 | |
Alan Stokes | 1508df2 | 2023-12-04 11:31:21 +0000 | [diff] [blame] | 233 | fn get_apk_root_hash_from_idsig<P: AsRef<Path>>(idsig_path: P) -> Result<Box<[u8]>> { |
Alan Stokes | 1125e01 | 2023-10-13 12:31:10 +0100 | [diff] [blame] | 234 | Ok(V4Signature::from_idsig_path(idsig_path)?.hashing_info.raw_root_hash) |
| 235 | } |
| 236 | |
Alan Stokes | 1508df2 | 2023-12-04 11:31:21 +0000 | [diff] [blame] | 237 | fn get_cert_hash_from_apk(apk: &str, root_hash_trustful: bool) -> Result<[u8; 64]> { |
Alan Stokes | 1125e01 | 2023-10-13 12:31:10 +0100 | [diff] [blame] | 238 | let current_sdk = get_current_sdk()?; |
| 239 | |
Alan Stokes | 1508df2 | 2023-12-04 11:31:21 +0000 | [diff] [blame] | 240 | let signed_data = if !root_hash_trustful { |
Alan Stokes | 1125e01 | 2023-10-13 12:31:10 +0100 | [diff] [blame] | 241 | verify(apk, current_sdk).context(MicrodroidError::PayloadVerificationFailed(format!( |
| 242 | "failed to verify {}", |
| 243 | apk |
Alan Stokes | 9b8b8ec | 2023-10-13 15:58:11 +0100 | [diff] [blame] | 244 | ))) |
Alan Stokes | 1125e01 | 2023-10-13 12:31:10 +0100 | [diff] [blame] | 245 | } else { |
Alan Stokes | 1508df2 | 2023-12-04 11:31:21 +0000 | [diff] [blame] | 246 | extract_signed_data(apk, current_sdk) |
| 247 | }?; |
| 248 | Ok(sha512(signed_data.first_certificate_der()?)) |
Alan Stokes | 1125e01 | 2023-10-13 12:31:10 +0100 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | fn get_current_sdk() -> Result<u32> { |
| 252 | let current_sdk = system_properties::read("ro.build.version.sdk")?; |
| 253 | let current_sdk = current_sdk.ok_or_else(|| anyhow!("SDK version missing"))?; |
| 254 | current_sdk.parse().context("Malformed SDK version") |
| 255 | } |
| 256 | |
| 257 | struct ApkDmverityArgument<'a> { |
| 258 | apk: &'a str, |
| 259 | idsig: &'a str, |
| 260 | name: &'a str, |
Alan Stokes | 1508df2 | 2023-12-04 11:31:21 +0000 | [diff] [blame] | 261 | saved_root_hash: Option<&'a [u8]>, |
Alan Stokes | 1125e01 | 2023-10-13 12:31:10 +0100 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | fn run_apkdmverity(args: &[ApkDmverityArgument]) -> Result<Child> { |
| 265 | let mut cmd = Command::new(APKDMVERITY_BIN); |
| 266 | |
| 267 | for argument in args { |
| 268 | cmd.arg("--apk").arg(argument.apk).arg(argument.idsig).arg(argument.name); |
| 269 | if let Some(root_hash) = argument.saved_root_hash { |
Chris Wailes | 63b67d7 | 2024-08-19 16:23:21 -0700 | [diff] [blame] | 270 | cmd.arg(hex::encode(root_hash)); |
Alan Stokes | 1125e01 | 2023-10-13 12:31:10 +0100 | [diff] [blame] | 271 | } else { |
| 272 | cmd.arg("none"); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | cmd.spawn().context("Spawn apkdmverity") |
| 277 | } |