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