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::{ |
Alan Stokes | 388b88a | 2021-10-13 16:03:17 +0100 | [diff] [blame] | 24 | COMPOS_DATA_ROOT, CURRENT_INSTANCE_DIR, INSTANCE_IMAGE_FILE, PENDING_INSTANCE_DIR, |
Alan Stokes | 9a79ce9 | 2021-11-25 11:47:54 +0000 | [diff] [blame] | 25 | PRIVATE_KEY_BLOB_FILE, PUBLIC_KEY_FILE, TEST_INSTANCE_DIR, |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 26 | }; |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 27 | use std::fs::{self, File}; |
| 28 | use std::io::Read; |
| 29 | use std::path::{Path, PathBuf}; |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 30 | |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 31 | const MAX_FILE_SIZE_BYTES: u64 = 8 * 1024; |
| 32 | |
Alan Stokes | 9a79ce9 | 2021-11-25 11:47:54 +0000 | [diff] [blame] | 33 | fn main() { |
Alan Stokes | 17fd36a | 2021-09-06 17:22:37 +0100 | [diff] [blame] | 34 | android_logger::init_once( |
| 35 | android_logger::Config::default() |
| 36 | .with_tag("compos_verify_key") |
| 37 | .with_min_level(log::Level::Info), |
| 38 | ); |
| 39 | |
Alan Stokes | 9a79ce9 | 2021-11-25 11:47:54 +0000 | [diff] [blame] | 40 | if let Err(e) = try_main() { |
| 41 | log::error!("{:?}", e); |
| 42 | std::process::exit(-1) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | fn try_main() -> Result<()> { |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 47 | let matches = clap::App::new("compos_verify_key") |
| 48 | .arg( |
| 49 | clap::Arg::with_name("instance") |
| 50 | .long("instance") |
| 51 | .takes_value(true) |
| 52 | .required(true) |
Alan Stokes | 9a79ce9 | 2021-11-25 11:47:54 +0000 | [diff] [blame] | 53 | .possible_values(&["pending", "current", "test"]), |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 54 | ) |
Alan Stokes | 9a79ce9 | 2021-11-25 11:47:54 +0000 | [diff] [blame] | 55 | .arg(clap::Arg::with_name("debug").long("debug")) |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 56 | .get_matches(); |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 57 | |
Alan Stokes | 9a79ce9 | 2021-11-25 11:47:54 +0000 | [diff] [blame] | 58 | let debug_mode = matches.is_present("debug"); |
| 59 | let (promote_if_valid, instance_dir) = match matches.value_of("instance").unwrap() { |
| 60 | "pending" => (true, PENDING_INSTANCE_DIR), |
| 61 | "current" => (false, CURRENT_INSTANCE_DIR), |
| 62 | "test" => (false, TEST_INSTANCE_DIR), |
| 63 | _ => unreachable!("Unexpected instance name"), |
| 64 | }; |
| 65 | |
| 66 | let instance_dir: PathBuf = [COMPOS_DATA_ROOT, instance_dir].iter().collect(); |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 67 | |
| 68 | if !instance_dir.is_dir() { |
Alan Stokes | 9a79ce9 | 2021-11-25 11:47:54 +0000 | [diff] [blame] | 69 | bail!("{:?} is not a directory", instance_dir); |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | // We need to start the thread pool to be able to receive Binder callbacks |
| 73 | ProcessState::start_thread_pool(); |
| 74 | |
Alan Stokes | 9a79ce9 | 2021-11-25 11:47:54 +0000 | [diff] [blame] | 75 | let result = verify(debug_mode, &instance_dir).and_then(|_| { |
| 76 | log::info!("Verified {:?}", instance_dir); |
| 77 | if promote_if_valid { |
| 78 | // 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] | 79 | // so we promote it to current. |
Alan Stokes | 9a79ce9 | 2021-11-25 11:47:54 +0000 | [diff] [blame] | 80 | log::info!("Promoting to current"); |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 81 | promote_to_current(&instance_dir) |
| 82 | } else { |
| 83 | Ok(()) |
| 84 | } |
| 85 | }); |
| 86 | |
| 87 | if result.is_err() { |
| 88 | // 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] | 89 | log::info!("Removing {:?}", instance_dir); |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 90 | if let Err(e) = fs::remove_dir_all(&instance_dir) { |
Alan Stokes | 17fd36a | 2021-09-06 17:22:37 +0100 | [diff] [blame] | 91 | log::warn!("Failed to remove directory: {}", e); |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | result |
| 96 | } |
| 97 | |
Alan Stokes | 9a79ce9 | 2021-11-25 11:47:54 +0000 | [diff] [blame] | 98 | fn verify(debug_mode: bool, instance_dir: &Path) -> Result<()> { |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 99 | let blob = instance_dir.join(PRIVATE_KEY_BLOB_FILE); |
| 100 | let public_key = instance_dir.join(PUBLIC_KEY_FILE); |
Alan Stokes | 17fd36a | 2021-09-06 17:22:37 +0100 | [diff] [blame] | 101 | let instance_image = instance_dir.join(INSTANCE_IMAGE_FILE); |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 102 | |
| 103 | let blob = read_small_file(blob).context("Failed to read key blob")?; |
| 104 | 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] | 105 | 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] | 106 | |
Alan Stokes | d21764c | 2021-10-25 15:33:40 +0100 | [diff] [blame] | 107 | let virtualization_service = VmInstance::connect_to_virtualization_service()?; |
Alan Stokes | b4a0e91 | 2021-12-01 11:43:59 +0000 | [diff] [blame^] | 108 | let vm_instance = VmInstance::start( |
| 109 | &*virtualization_service, |
| 110 | instance_image, |
| 111 | &VmParameters { debug_mode, ..Default::default() }, |
| 112 | )?; |
Alan Stokes | 17fd36a | 2021-09-06 17:22:37 +0100 | [diff] [blame] | 113 | let service = vm_instance.get_service()?; |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 114 | |
| 115 | let result = service.verifySigningKey(&blob, &public_key).context("Verifying signing key")?; |
| 116 | |
| 117 | if !result { |
| 118 | bail!("Key files are not valid"); |
| 119 | } |
| 120 | |
| 121 | Ok(()) |
| 122 | } |
| 123 | |
Alan Stokes | 9a79ce9 | 2021-11-25 11:47:54 +0000 | [diff] [blame] | 124 | fn promote_to_current(instance_dir: &Path) -> Result<()> { |
| 125 | let current_dir: PathBuf = [COMPOS_DATA_ROOT, CURRENT_INSTANCE_DIR].iter().collect(); |
| 126 | |
| 127 | // This may fail if the directory doesn't exist - which is fine, we only care about the rename |
| 128 | // succeeding. |
| 129 | let _ = fs::remove_dir_all(¤t_dir); |
| 130 | |
| 131 | fs::rename(&instance_dir, ¤t_dir).context("Unable to promote instance to current")?; |
| 132 | Ok(()) |
| 133 | } |
| 134 | |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 135 | fn read_small_file(file: PathBuf) -> Result<Vec<u8>> { |
| 136 | let mut file = File::open(file)?; |
| 137 | if file.metadata()?.len() > MAX_FILE_SIZE_BYTES { |
| 138 | bail!("File is too big"); |
| 139 | } |
| 140 | let mut data = vec![]; |
| 141 | file.read_to_end(&mut data)?; |
| 142 | Ok(data) |
| 143 | } |