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; |
| 22 | use compos_common::compos_client::VmInstance; |
| 23 | use compos_common::COMPOS_DATA_ROOT; |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 24 | use std::fs::{self, File}; |
| 25 | use std::io::Read; |
| 26 | use std::path::{Path, PathBuf}; |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 27 | |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 28 | const CURRENT_DIR: &str = "current"; |
| 29 | const PENDING_DIR: &str = "pending"; |
| 30 | const PRIVATE_KEY_BLOB_FILE: &str = "key.blob"; |
| 31 | const PUBLIC_KEY_FILE: &str = "key.pubkey"; |
| 32 | const INSTANCE_IMAGE_FILE: &str = "instance.img"; |
| 33 | |
| 34 | const MAX_FILE_SIZE_BYTES: u64 = 8 * 1024; |
| 35 | |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 36 | fn main() -> Result<()> { |
Alan Stokes | 17fd36a | 2021-09-06 17:22:37 +0100 | [diff] [blame^] | 37 | android_logger::init_once( |
| 38 | android_logger::Config::default() |
| 39 | .with_tag("compos_verify_key") |
| 40 | .with_min_level(log::Level::Info), |
| 41 | ); |
| 42 | |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 43 | let matches = clap::App::new("compos_verify_key") |
| 44 | .arg( |
| 45 | clap::Arg::with_name("instance") |
| 46 | .long("instance") |
| 47 | .takes_value(true) |
| 48 | .required(true) |
| 49 | .possible_values(&["pending", "current"]), |
| 50 | ) |
| 51 | .get_matches(); |
| 52 | let do_pending = matches.value_of("instance").unwrap() == "pending"; |
| 53 | |
| 54 | let instance_dir: PathBuf = |
| 55 | [COMPOS_DATA_ROOT, if do_pending { PENDING_DIR } else { CURRENT_DIR }].iter().collect(); |
| 56 | |
| 57 | if !instance_dir.is_dir() { |
| 58 | bail!("{} is not a directory", instance_dir.display()); |
| 59 | } |
| 60 | |
| 61 | // We need to start the thread pool to be able to receive Binder callbacks |
| 62 | ProcessState::start_thread_pool(); |
| 63 | |
| 64 | let result = verify(&instance_dir).and_then(|_| { |
| 65 | if do_pending { |
| 66 | // If the pending instance is ok, then it must actually match the current system state, |
| 67 | // so we promote it to current. |
Alan Stokes | 17fd36a | 2021-09-06 17:22:37 +0100 | [diff] [blame^] | 68 | log::info!("Promoting pending to current"); |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 69 | promote_to_current(&instance_dir) |
| 70 | } else { |
| 71 | Ok(()) |
| 72 | } |
| 73 | }); |
| 74 | |
| 75 | if result.is_err() { |
| 76 | // This is best efforts, and we still want to report the original error as our result |
Alan Stokes | 17fd36a | 2021-09-06 17:22:37 +0100 | [diff] [blame^] | 77 | log::info!("Removing {}", instance_dir.display()); |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 78 | if let Err(e) = fs::remove_dir_all(&instance_dir) { |
Alan Stokes | 17fd36a | 2021-09-06 17:22:37 +0100 | [diff] [blame^] | 79 | log::warn!("Failed to remove directory: {}", e); |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
| 83 | result |
| 84 | } |
| 85 | |
| 86 | fn verify(instance_dir: &Path) -> Result<()> { |
| 87 | let blob = instance_dir.join(PRIVATE_KEY_BLOB_FILE); |
| 88 | let public_key = instance_dir.join(PUBLIC_KEY_FILE); |
Alan Stokes | 17fd36a | 2021-09-06 17:22:37 +0100 | [diff] [blame^] | 89 | let instance_image = instance_dir.join(INSTANCE_IMAGE_FILE); |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 90 | |
| 91 | let blob = read_small_file(blob).context("Failed to read key blob")?; |
| 92 | let public_key = read_small_file(public_key).context("Failed to read public key")?; |
| 93 | |
Alan Stokes | 17fd36a | 2021-09-06 17:22:37 +0100 | [diff] [blame^] | 94 | let vm_instance = VmInstance::start(&instance_image)?; |
| 95 | let service = vm_instance.get_service()?; |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 96 | |
| 97 | let result = service.verifySigningKey(&blob, &public_key).context("Verifying signing key")?; |
| 98 | |
| 99 | if !result { |
| 100 | bail!("Key files are not valid"); |
| 101 | } |
| 102 | |
| 103 | Ok(()) |
| 104 | } |
| 105 | |
| 106 | fn read_small_file(file: PathBuf) -> Result<Vec<u8>> { |
| 107 | let mut file = File::open(file)?; |
| 108 | if file.metadata()?.len() > MAX_FILE_SIZE_BYTES { |
| 109 | bail!("File is too big"); |
| 110 | } |
| 111 | let mut data = vec![]; |
| 112 | file.read_to_end(&mut data)?; |
| 113 | Ok(data) |
| 114 | } |
| 115 | |
Alan Stokes | eb97d4a | 2021-08-26 14:24:32 +0100 | [diff] [blame] | 116 | fn promote_to_current(instance_dir: &Path) -> Result<()> { |
| 117 | let current_dir: PathBuf = [COMPOS_DATA_ROOT, CURRENT_DIR].iter().collect(); |
| 118 | |
| 119 | // This may fail if the directory doesn't exist - which is fine, we only care about the rename |
| 120 | // succeeding. |
| 121 | let _ = fs::remove_dir_all(¤t_dir); |
| 122 | |
| 123 | fs::rename(&instance_dir, ¤t_dir) |
| 124 | .context("Unable to promote pending instance to current")?; |
| 125 | Ok(()) |
| 126 | } |