Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Alan Stokes | 17fd36a | 2021-09-06 17:22:37 +0100 | [diff] [blame] | 17 | //! A tool to verify whether a CompOS instance image and key pair are valid. It starts a CompOS VM |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 18 | //! as part of this. The tool is intended to be run by odsign during boot. |
| 19 | |
Alan Stokes | 17fd36a | 2021-09-06 17:22:37 +0100 | [diff] [blame] | 20 | use anyhow::{bail, Context, Result}; |
| 21 | use compos_aidl_interface::binder::ProcessState; |
Alan Stokes | d21764c | 2021-10-25 15:33:40 +0100 | [diff] [blame] | 22 | use compos_common::compos_client::{VmInstance, VmParameters}; |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 23 | use compos_common::{ |
Victor Hsieh | 0a5ab4b | 2022-01-05 11:45:52 -0800 | [diff] [blame] | 24 | COMPOS_DATA_ROOT, CURRENT_INSTANCE_DIR, IDSIG_FILE, IDSIG_MANIFEST_APK_FILE, |
| 25 | INSTANCE_IMAGE_FILE, PENDING_INSTANCE_DIR, PRIVATE_KEY_BLOB_FILE, PUBLIC_KEY_FILE, |
| 26 | TEST_INSTANCE_DIR, |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 27 | }; |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 28 | use std::fs::{self, File}; |
| 29 | use std::io::Read; |
| 30 | use std::path::{Path, PathBuf}; |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 31 | |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 32 | const MAX_FILE_SIZE_BYTES: u64 = 8 * 1024; |
| 33 | |
Alan Stokes | 9a79ce9 | 2021-11-25 11:47:54 +0000 | [diff] [blame] | 34 | fn main() { |
Alan Stokes | 17fd36a | 2021-09-06 17:22:37 +0100 | [diff] [blame] | 35 | android_logger::init_once( |
| 36 | android_logger::Config::default() |
| 37 | .with_tag("compos_verify_key") |
| 38 | .with_min_level(log::Level::Info), |
| 39 | ); |
| 40 | |
Alan Stokes | 9a79ce9 | 2021-11-25 11:47:54 +0000 | [diff] [blame] | 41 | if let Err(e) = try_main() { |
| 42 | log::error!("{:?}", e); |
| 43 | std::process::exit(-1) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | fn try_main() -> Result<()> { |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 48 | let matches = clap::App::new("compos_verify_key") |
| 49 | .arg( |
| 50 | clap::Arg::with_name("instance") |
| 51 | .long("instance") |
| 52 | .takes_value(true) |
| 53 | .required(true) |
Alan Stokes | 9a79ce9 | 2021-11-25 11:47:54 +0000 | [diff] [blame] | 54 | .possible_values(&["pending", "current", "test"]), |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 55 | ) |
Alan Stokes | 9a79ce9 | 2021-11-25 11:47:54 +0000 | [diff] [blame] | 56 | .arg(clap::Arg::with_name("debug").long("debug")) |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 57 | .get_matches(); |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 58 | |
Alan Stokes | 9a79ce9 | 2021-11-25 11:47:54 +0000 | [diff] [blame] | 59 | let debug_mode = matches.is_present("debug"); |
| 60 | let (promote_if_valid, instance_dir) = match matches.value_of("instance").unwrap() { |
| 61 | "pending" => (true, PENDING_INSTANCE_DIR), |
| 62 | "current" => (false, CURRENT_INSTANCE_DIR), |
| 63 | "test" => (false, TEST_INSTANCE_DIR), |
| 64 | _ => unreachable!("Unexpected instance name"), |
| 65 | }; |
| 66 | |
| 67 | let instance_dir: PathBuf = [COMPOS_DATA_ROOT, instance_dir].iter().collect(); |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 68 | |
| 69 | if !instance_dir.is_dir() { |
Alan Stokes | 9a79ce9 | 2021-11-25 11:47:54 +0000 | [diff] [blame] | 70 | bail!("{:?} is not a directory", instance_dir); |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | // We need to start the thread pool to be able to receive Binder callbacks |
| 74 | ProcessState::start_thread_pool(); |
| 75 | |
Alan Stokes | 9a79ce9 | 2021-11-25 11:47:54 +0000 | [diff] [blame] | 76 | let result = verify(debug_mode, &instance_dir).and_then(|_| { |
| 77 | log::info!("Verified {:?}", instance_dir); |
| 78 | if promote_if_valid { |
| 79 | // If the instance is ok, then it must actually match the current system state, |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 80 | // so we promote it to current. |
Alan Stokes | 9a79ce9 | 2021-11-25 11:47:54 +0000 | [diff] [blame] | 81 | log::info!("Promoting to current"); |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 82 | promote_to_current(&instance_dir) |
| 83 | } else { |
| 84 | Ok(()) |
| 85 | } |
| 86 | }); |
| 87 | |
| 88 | if result.is_err() { |
| 89 | // This is best efforts, and we still want to report the original error as our result |
Alan Stokes | 9a79ce9 | 2021-11-25 11:47:54 +0000 | [diff] [blame] | 90 | log::info!("Removing {:?}", instance_dir); |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 91 | if let Err(e) = fs::remove_dir_all(&instance_dir) { |
Alan Stokes | 17fd36a | 2021-09-06 17:22:37 +0100 | [diff] [blame] | 92 | log::warn!("Failed to remove directory: {}", e); |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | result |
| 97 | } |
| 98 | |
Alan Stokes | 9a79ce9 | 2021-11-25 11:47:54 +0000 | [diff] [blame] | 99 | fn verify(debug_mode: bool, instance_dir: &Path) -> Result<()> { |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 100 | let blob = instance_dir.join(PRIVATE_KEY_BLOB_FILE); |
| 101 | let public_key = instance_dir.join(PUBLIC_KEY_FILE); |
Alan Stokes | 17fd36a | 2021-09-06 17:22:37 +0100 | [diff] [blame] | 102 | let instance_image = instance_dir.join(INSTANCE_IMAGE_FILE); |
Jooyung Han | ccae66d | 2021-12-20 11:54:36 +0900 | [diff] [blame] | 103 | let idsig = instance_dir.join(IDSIG_FILE); |
Victor Hsieh | 0a5ab4b | 2022-01-05 11:45:52 -0800 | [diff] [blame] | 104 | let idsig_manifest_apk = instance_dir.join(IDSIG_MANIFEST_APK_FILE); |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 105 | |
| 106 | let blob = read_small_file(blob).context("Failed to read key blob")?; |
| 107 | let public_key = read_small_file(public_key).context("Failed to read public key")?; |
Alan Stokes | 16e027f | 2021-10-04 17:57:31 +0100 | [diff] [blame] | 108 | let instance_image = File::open(instance_image).context("Failed to open instance image")?; |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 109 | |
Alan Stokes | d21764c | 2021-10-25 15:33:40 +0100 | [diff] [blame] | 110 | let virtualization_service = VmInstance::connect_to_virtualization_service()?; |
Alan Stokes | b4a0e91 | 2021-12-01 11:43:59 +0000 | [diff] [blame] | 111 | let vm_instance = VmInstance::start( |
| 112 | &*virtualization_service, |
| 113 | instance_image, |
Jooyung Han | ccae66d | 2021-12-20 11:54:36 +0900 | [diff] [blame] | 114 | &idsig, |
Victor Hsieh | 0a5ab4b | 2022-01-05 11:45:52 -0800 | [diff] [blame] | 115 | &idsig_manifest_apk, |
Alan Stokes | b4a0e91 | 2021-12-01 11:43:59 +0000 | [diff] [blame] | 116 | &VmParameters { debug_mode, ..Default::default() }, |
| 117 | )?; |
Alan Stokes | 17fd36a | 2021-09-06 17:22:37 +0100 | [diff] [blame] | 118 | let service = vm_instance.get_service()?; |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 119 | |
| 120 | let result = service.verifySigningKey(&blob, &public_key).context("Verifying signing key")?; |
| 121 | |
| 122 | if !result { |
| 123 | bail!("Key files are not valid"); |
| 124 | } |
| 125 | |
| 126 | Ok(()) |
| 127 | } |
| 128 | |
Alan Stokes | 9a79ce9 | 2021-11-25 11:47:54 +0000 | [diff] [blame] | 129 | fn promote_to_current(instance_dir: &Path) -> Result<()> { |
| 130 | let current_dir: PathBuf = [COMPOS_DATA_ROOT, CURRENT_INSTANCE_DIR].iter().collect(); |
| 131 | |
| 132 | // This may fail if the directory doesn't exist - which is fine, we only care about the rename |
| 133 | // succeeding. |
| 134 | let _ = fs::remove_dir_all(¤t_dir); |
| 135 | |
| 136 | fs::rename(&instance_dir, ¤t_dir).context("Unable to promote instance to current")?; |
| 137 | Ok(()) |
| 138 | } |
| 139 | |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 140 | fn read_small_file(file: PathBuf) -> Result<Vec<u8>> { |
| 141 | let mut file = File::open(file)?; |
| 142 | if file.metadata()?.len() > MAX_FILE_SIZE_BYTES { |
| 143 | bail!("File is too big"); |
| 144 | } |
| 145 | let mut data = vec![]; |
| 146 | file.read_to_end(&mut data)?; |
| 147 | Ok(data) |
| 148 | } |